bmips_cpu.c 13 KB

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