bmips_cpu.c 12 KB

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