bmips_cpu.c 11 KB

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