bmips_cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
  4. *
  5. * Derived from linux/arch/mips/bcm63xx/cpu.c:
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
  8. */
  9. #include <common.h>
  10. #include <cpu.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <asm/io.h>
  14. #define REV_CHIPID_SHIFT 16
  15. #define REV_CHIPID_MASK (0xffff << REV_CHIPID_SHIFT)
  16. #define REV_LONG_CHIPID_SHIFT 12
  17. #define REV_LONG_CHIPID_MASK (0xfffff << REV_LONG_CHIPID_SHIFT)
  18. #define REV_REVID_SHIFT 0
  19. #define REV_REVID_MASK (0xff << REV_REVID_SHIFT)
  20. #define REG_BCM6328_OTP 0x62c
  21. #define BCM6328_TP1_DISABLED BIT(9)
  22. #define REG_BCM6318_STRAP_OVRDBUS 0x900
  23. #define OVRDBUS_6318_FREQ_SHIFT 23
  24. #define OVRDBUS_6318_FREQ_MASK (0x3 << OVRDBUS_6318_FREQ_SHIFT)
  25. #define REG_BCM6328_MISC_STRAPBUS 0x1a40
  26. #define STRAPBUS_6328_FCVO_SHIFT 7
  27. #define STRAPBUS_6328_FCVO_MASK (0x1f << STRAPBUS_6328_FCVO_SHIFT)
  28. #define REG_BCM6348_PERF_MIPSPLLCFG 0x34
  29. #define MIPSPLLCFG_6348_M1CPU_SHIFT 6
  30. #define MIPSPLLCFG_6348_M1CPU_MASK (0x7 << MIPSPLLCFG_6348_M1CPU_SHIFT)
  31. #define MIPSPLLCFG_6348_N2_SHIFT 15
  32. #define MIPSPLLCFG_6348_N2_MASK (0x1F << MIPSPLLCFG_6348_N2_SHIFT)
  33. #define MIPSPLLCFG_6348_N1_SHIFT 20
  34. #define MIPSPLLCFG_6348_N1_MASK (0x7 << MIPSPLLCFG_6348_N1_SHIFT)
  35. #define REG_BCM6358_DDR_DMIPSPLLCFG 0x12b8
  36. #define DMIPSPLLCFG_6358_M1_SHIFT 0
  37. #define DMIPSPLLCFG_6358_M1_MASK (0xff << DMIPSPLLCFG_6358_M1_SHIFT)
  38. #define DMIPSPLLCFG_6358_N1_SHIFT 23
  39. #define DMIPSPLLCFG_6358_N1_MASK (0x3f << DMIPSPLLCFG_6358_N1_SHIFT)
  40. #define DMIPSPLLCFG_6358_N2_SHIFT 29
  41. #define DMIPSPLLCFG_6358_N2_MASK (0x7 << DMIPSPLLCFG_6358_N2_SHIFT)
  42. #define REG_BCM6362_MISC_STRAPBUS 0x1814
  43. #define STRAPBUS_6362_FCVO_SHIFT 1
  44. #define STRAPBUS_6362_FCVO_MASK (0x1f << STRAPBUS_6362_FCVO_SHIFT)
  45. #define REG_BCM6368_DDR_DMIPSPLLCFG 0x12a0
  46. #define DMIPSPLLCFG_6368_P1_SHIFT 0
  47. #define DMIPSPLLCFG_6368_P1_MASK (0xf << DMIPSPLLCFG_6368_P1_SHIFT)
  48. #define DMIPSPLLCFG_6368_P2_SHIFT 4
  49. #define DMIPSPLLCFG_6368_P2_MASK (0xf << DMIPSPLLCFG_6368_P2_SHIFT)
  50. #define DMIPSPLLCFG_6368_NDIV_SHIFT 16
  51. #define DMIPSPLLCFG_6368_NDIV_MASK (0x1ff << DMIPSPLLCFG_6368_NDIV_SHIFT)
  52. #define REG_BCM6368_DDR_DMIPSPLLDIV 0x12a4
  53. #define DMIPSPLLDIV_6368_MDIV_SHIFT 0
  54. #define DMIPSPLLDIV_6368_MDIV_MASK (0xff << DMIPSPLLDIV_6368_MDIV_SHIFT)
  55. #define REG_BCM63268_MISC_STRAPBUS 0x1814
  56. #define STRAPBUS_63268_FCVO_SHIFT 21
  57. #define STRAPBUS_63268_FCVO_MASK (0xf << STRAPBUS_63268_FCVO_SHIFT)
  58. struct bmips_cpu_priv;
  59. struct bmips_cpu_hw {
  60. int (*get_cpu_desc)(struct bmips_cpu_priv *priv, char *buf, int size);
  61. ulong (*get_cpu_freq)(struct bmips_cpu_priv *);
  62. int (*get_cpu_count)(struct bmips_cpu_priv *);
  63. };
  64. struct bmips_cpu_priv {
  65. void __iomem *regs;
  66. const struct bmips_cpu_hw *hw;
  67. };
  68. /* Specific CPU Ops */
  69. static int bmips_short_cpu_desc(struct bmips_cpu_priv *priv, char *buf,
  70. int size)
  71. {
  72. unsigned short cpu_id;
  73. unsigned char cpu_rev;
  74. u32 val;
  75. val = readl_be(priv->regs);
  76. cpu_id = (val & REV_CHIPID_MASK) >> REV_CHIPID_SHIFT;
  77. cpu_rev = (val & REV_REVID_MASK) >> REV_REVID_SHIFT;
  78. snprintf(buf, size, "BCM%04X%02X", cpu_id, cpu_rev);
  79. return 0;
  80. }
  81. static int bmips_long_cpu_desc(struct bmips_cpu_priv *priv, char *buf,
  82. int size)
  83. {
  84. unsigned int cpu_id;
  85. unsigned char cpu_rev;
  86. u32 val;
  87. val = readl_be(priv->regs);
  88. cpu_id = (val & REV_LONG_CHIPID_MASK) >> REV_LONG_CHIPID_SHIFT;
  89. cpu_rev = (val & REV_REVID_MASK) >> REV_REVID_SHIFT;
  90. snprintf(buf, size, "BCM%05X%02X", cpu_id, cpu_rev);
  91. return 0;
  92. }
  93. static ulong bcm3380_get_cpu_freq(struct bmips_cpu_priv *priv)
  94. {
  95. return 333000000;
  96. }
  97. static ulong bcm6318_get_cpu_freq(struct bmips_cpu_priv *priv)
  98. {
  99. unsigned int mips_pll_fcvo;
  100. mips_pll_fcvo = readl_be(priv->regs + REG_BCM6318_STRAP_OVRDBUS);
  101. mips_pll_fcvo = (mips_pll_fcvo & OVRDBUS_6318_FREQ_MASK)
  102. >> OVRDBUS_6318_FREQ_SHIFT;
  103. switch (mips_pll_fcvo) {
  104. case 0:
  105. return 166000000;
  106. case 1:
  107. return 400000000;
  108. case 2:
  109. return 250000000;
  110. case 3:
  111. return 333000000;
  112. default:
  113. return 0;
  114. }
  115. }
  116. static ulong bcm6328_get_cpu_freq(struct bmips_cpu_priv *priv)
  117. {
  118. unsigned int mips_pll_fcvo;
  119. mips_pll_fcvo = readl_be(priv->regs + REG_BCM6328_MISC_STRAPBUS);
  120. mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_6328_FCVO_MASK)
  121. >> STRAPBUS_6328_FCVO_SHIFT;
  122. switch (mips_pll_fcvo) {
  123. case 0x12:
  124. case 0x14:
  125. case 0x19:
  126. return 160000000;
  127. case 0x1c:
  128. return 192000000;
  129. case 0x13:
  130. case 0x15:
  131. return 200000000;
  132. case 0x1a:
  133. return 384000000;
  134. case 0x16:
  135. return 400000000;
  136. default:
  137. return 320000000;
  138. }
  139. }
  140. static ulong bcm6338_get_cpu_freq(struct bmips_cpu_priv *priv)
  141. {
  142. return 240000000;
  143. }
  144. static ulong bcm6348_get_cpu_freq(struct bmips_cpu_priv *priv)
  145. {
  146. unsigned int tmp, n1, n2, m1;
  147. tmp = readl_be(priv->regs + REG_BCM6348_PERF_MIPSPLLCFG);
  148. n1 = (tmp & MIPSPLLCFG_6348_N1_MASK) >> MIPSPLLCFG_6348_N1_SHIFT;
  149. n2 = (tmp & MIPSPLLCFG_6348_N2_MASK) >> MIPSPLLCFG_6348_N2_SHIFT;
  150. m1 = (tmp & MIPSPLLCFG_6348_M1CPU_MASK) >> MIPSPLLCFG_6348_M1CPU_SHIFT;
  151. return (16 * 1000000 * (n1 + 1) * (n2 + 2)) / (m1 + 1);
  152. }
  153. static ulong bcm6358_get_cpu_freq(struct bmips_cpu_priv *priv)
  154. {
  155. unsigned int tmp, n1, n2, m1;
  156. tmp = readl_be(priv->regs + REG_BCM6358_DDR_DMIPSPLLCFG);
  157. n1 = (tmp & DMIPSPLLCFG_6358_N1_MASK) >> DMIPSPLLCFG_6358_N1_SHIFT;
  158. n2 = (tmp & DMIPSPLLCFG_6358_N2_MASK) >> DMIPSPLLCFG_6358_N2_SHIFT;
  159. m1 = (tmp & DMIPSPLLCFG_6358_M1_MASK) >> DMIPSPLLCFG_6358_M1_SHIFT;
  160. return (16 * 1000000 * n1 * n2) / m1;
  161. }
  162. static ulong bcm6362_get_cpu_freq(struct bmips_cpu_priv *priv)
  163. {
  164. unsigned int mips_pll_fcvo;
  165. mips_pll_fcvo = readl_be(priv->regs + REG_BCM6362_MISC_STRAPBUS);
  166. mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_6362_FCVO_MASK)
  167. >> STRAPBUS_6362_FCVO_SHIFT;
  168. switch (mips_pll_fcvo) {
  169. case 0x03:
  170. case 0x0b:
  171. case 0x13:
  172. case 0x1b:
  173. return 240000000;
  174. case 0x04:
  175. case 0x0c:
  176. case 0x14:
  177. case 0x1c:
  178. return 160000000;
  179. case 0x05:
  180. case 0x0e:
  181. case 0x16:
  182. case 0x1e:
  183. case 0x1f:
  184. return 400000000;
  185. case 0x06:
  186. return 440000000;
  187. case 0x07:
  188. case 0x17:
  189. return 384000000;
  190. case 0x15:
  191. case 0x1d:
  192. return 200000000;
  193. default:
  194. return 320000000;
  195. }
  196. }
  197. static ulong bcm6368_get_cpu_freq(struct bmips_cpu_priv *priv)
  198. {
  199. unsigned int tmp, p1, p2, ndiv, m1;
  200. tmp = readl_be(priv->regs + REG_BCM6368_DDR_DMIPSPLLCFG);
  201. p1 = (tmp & DMIPSPLLCFG_6368_P1_MASK) >> DMIPSPLLCFG_6368_P1_SHIFT;
  202. p2 = (tmp & DMIPSPLLCFG_6368_P2_MASK) >> DMIPSPLLCFG_6368_P2_SHIFT;
  203. ndiv = (tmp & DMIPSPLLCFG_6368_NDIV_MASK) >>
  204. DMIPSPLLCFG_6368_NDIV_SHIFT;
  205. tmp = readl_be(priv->regs + REG_BCM6368_DDR_DMIPSPLLDIV);
  206. m1 = (tmp & DMIPSPLLDIV_6368_MDIV_MASK) >> DMIPSPLLDIV_6368_MDIV_SHIFT;
  207. return (((64 * 1000000) / p1) * p2 * ndiv) / m1;
  208. }
  209. static ulong bcm63268_get_cpu_freq(struct bmips_cpu_priv *priv)
  210. {
  211. unsigned int mips_pll_fcvo;
  212. mips_pll_fcvo = readl_be(priv->regs + REG_BCM63268_MISC_STRAPBUS);
  213. mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_63268_FCVO_MASK)
  214. >> STRAPBUS_63268_FCVO_SHIFT;
  215. switch (mips_pll_fcvo) {
  216. case 0x3:
  217. case 0xe:
  218. return 320000000;
  219. case 0xa:
  220. return 333000000;
  221. case 0x2:
  222. case 0xb:
  223. case 0xf:
  224. return 400000000;
  225. default:
  226. return 0;
  227. }
  228. }
  229. static int bcm6328_get_cpu_count(struct bmips_cpu_priv *priv)
  230. {
  231. u32 val = readl_be(priv->regs + REG_BCM6328_OTP);
  232. if (val & BCM6328_TP1_DISABLED)
  233. return 1;
  234. else
  235. return 2;
  236. }
  237. static int bcm6345_get_cpu_count(struct bmips_cpu_priv *priv)
  238. {
  239. return 1;
  240. }
  241. static int bcm6358_get_cpu_count(struct bmips_cpu_priv *priv)
  242. {
  243. return 2;
  244. }
  245. static const struct bmips_cpu_hw bmips_cpu_bcm3380 = {
  246. .get_cpu_desc = bmips_short_cpu_desc,
  247. .get_cpu_freq = bcm3380_get_cpu_freq,
  248. .get_cpu_count = bcm6358_get_cpu_count,
  249. };
  250. static const struct bmips_cpu_hw bmips_cpu_bcm6318 = {
  251. .get_cpu_desc = bmips_short_cpu_desc,
  252. .get_cpu_freq = bcm6318_get_cpu_freq,
  253. .get_cpu_count = bcm6345_get_cpu_count,
  254. };
  255. static const struct bmips_cpu_hw bmips_cpu_bcm6328 = {
  256. .get_cpu_desc = bmips_long_cpu_desc,
  257. .get_cpu_freq = bcm6328_get_cpu_freq,
  258. .get_cpu_count = bcm6328_get_cpu_count,
  259. };
  260. static const struct bmips_cpu_hw bmips_cpu_bcm6338 = {
  261. .get_cpu_desc = bmips_short_cpu_desc,
  262. .get_cpu_freq = bcm6338_get_cpu_freq,
  263. .get_cpu_count = bcm6345_get_cpu_count,
  264. };
  265. static const struct bmips_cpu_hw bmips_cpu_bcm6348 = {
  266. .get_cpu_desc = bmips_short_cpu_desc,
  267. .get_cpu_freq = bcm6348_get_cpu_freq,
  268. .get_cpu_count = bcm6345_get_cpu_count,
  269. };
  270. static const struct bmips_cpu_hw bmips_cpu_bcm6358 = {
  271. .get_cpu_desc = bmips_short_cpu_desc,
  272. .get_cpu_freq = bcm6358_get_cpu_freq,
  273. .get_cpu_count = bcm6358_get_cpu_count,
  274. };
  275. static const struct bmips_cpu_hw bmips_cpu_bcm6362 = {
  276. .get_cpu_desc = bmips_short_cpu_desc,
  277. .get_cpu_freq = bcm6362_get_cpu_freq,
  278. .get_cpu_count = bcm6358_get_cpu_count,
  279. };
  280. static const struct bmips_cpu_hw bmips_cpu_bcm6368 = {
  281. .get_cpu_desc = bmips_short_cpu_desc,
  282. .get_cpu_freq = bcm6368_get_cpu_freq,
  283. .get_cpu_count = bcm6358_get_cpu_count,
  284. };
  285. static const struct bmips_cpu_hw bmips_cpu_bcm63268 = {
  286. .get_cpu_desc = bmips_long_cpu_desc,
  287. .get_cpu_freq = bcm63268_get_cpu_freq,
  288. .get_cpu_count = bcm6358_get_cpu_count,
  289. };
  290. /* Generic CPU Ops */
  291. static int bmips_cpu_get_desc(struct udevice *dev, char *buf, int size)
  292. {
  293. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  294. const struct bmips_cpu_hw *hw = priv->hw;
  295. return hw->get_cpu_desc(priv, buf, size);
  296. }
  297. static int bmips_cpu_get_info(struct udevice *dev, struct cpu_info *info)
  298. {
  299. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  300. const struct bmips_cpu_hw *hw = priv->hw;
  301. info->cpu_freq = hw->get_cpu_freq(priv);
  302. info->features = BIT(CPU_FEAT_L1_CACHE);
  303. info->features |= BIT(CPU_FEAT_MMU);
  304. info->features |= BIT(CPU_FEAT_DEVICE_ID);
  305. return 0;
  306. }
  307. static int bmips_cpu_get_count(struct udevice *dev)
  308. {
  309. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  310. const struct bmips_cpu_hw *hw = priv->hw;
  311. return hw->get_cpu_count(priv);
  312. }
  313. static int bmips_cpu_get_vendor(struct udevice *dev, char *buf, int size)
  314. {
  315. snprintf(buf, size, "Broadcom");
  316. return 0;
  317. }
  318. static const struct cpu_ops bmips_cpu_ops = {
  319. .get_desc = bmips_cpu_get_desc,
  320. .get_info = bmips_cpu_get_info,
  321. .get_count = bmips_cpu_get_count,
  322. .get_vendor = bmips_cpu_get_vendor,
  323. };
  324. /* BMIPS CPU driver */
  325. int bmips_cpu_bind(struct udevice *dev)
  326. {
  327. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  328. plat->cpu_id = dev_read_u32_default(dev, "reg", -1);
  329. plat->device_id = read_c0_prid();
  330. return 0;
  331. }
  332. int bmips_cpu_probe(struct udevice *dev)
  333. {
  334. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  335. const struct bmips_cpu_hw *hw =
  336. (const struct bmips_cpu_hw *)dev_get_driver_data(dev);
  337. priv->regs = dev_remap_addr(dev);
  338. if (!priv->regs)
  339. return -EINVAL;
  340. priv->hw = hw;
  341. return 0;
  342. }
  343. static const struct udevice_id bmips_cpu_ids[] = {
  344. {
  345. .compatible = "brcm,bcm3380-cpu",
  346. .data = (ulong)&bmips_cpu_bcm3380,
  347. }, {
  348. .compatible = "brcm,bcm6318-cpu",
  349. .data = (ulong)&bmips_cpu_bcm6318,
  350. }, {
  351. .compatible = "brcm,bcm6328-cpu",
  352. .data = (ulong)&bmips_cpu_bcm6328,
  353. }, {
  354. .compatible = "brcm,bcm6338-cpu",
  355. .data = (ulong)&bmips_cpu_bcm6338,
  356. }, {
  357. .compatible = "brcm,bcm6348-cpu",
  358. .data = (ulong)&bmips_cpu_bcm6348,
  359. }, {
  360. .compatible = "brcm,bcm6358-cpu",
  361. .data = (ulong)&bmips_cpu_bcm6358,
  362. }, {
  363. .compatible = "brcm,bcm6362-cpu",
  364. .data = (ulong)&bmips_cpu_bcm6362,
  365. }, {
  366. .compatible = "brcm,bcm6368-cpu",
  367. .data = (ulong)&bmips_cpu_bcm6368,
  368. }, {
  369. .compatible = "brcm,bcm63268-cpu",
  370. .data = (ulong)&bmips_cpu_bcm63268,
  371. },
  372. { /* sentinel */ }
  373. };
  374. U_BOOT_DRIVER(bmips_cpu_drv) = {
  375. .name = "bmips_cpu",
  376. .id = UCLASS_CPU,
  377. .of_match = bmips_cpu_ids,
  378. .bind = bmips_cpu_bind,
  379. .probe = bmips_cpu_probe,
  380. .priv_auto_alloc_size = sizeof(struct bmips_cpu_priv),
  381. .ops = &bmips_cpu_ops,
  382. .flags = DM_FLAG_PRE_RELOC,
  383. };
  384. #ifdef CONFIG_DISPLAY_CPUINFO
  385. int print_cpuinfo(void)
  386. {
  387. struct cpu_info cpu;
  388. struct udevice *dev;
  389. int err;
  390. char desc[100];
  391. err = uclass_get_device(UCLASS_CPU, 0, &dev);
  392. if (err)
  393. return 0;
  394. err = cpu_get_info(dev, &cpu);
  395. if (err)
  396. return 0;
  397. err = cpu_get_desc(dev, desc, sizeof(desc));
  398. if (err)
  399. return 0;
  400. printf("Chip ID: %s, MIPS: ", desc);
  401. print_freq(cpu.cpu_freq, "\n");
  402. return 0;
  403. }
  404. #endif