bmips_cpu.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_BCM6328_MISC_STRAPBUS 0x1a40
  25. #define STRAPBUS_6328_FCVO_SHIFT 7
  26. #define STRAPBUS_6328_FCVO_MASK (0x1f << STRAPBUS_6328_FCVO_SHIFT)
  27. #define REG_BCM6348_PERF_MIPSPLLCFG 0x34
  28. #define MIPSPLLCFG_6348_M1CPU_SHIFT 6
  29. #define MIPSPLLCFG_6348_M1CPU_MASK (0x7 << MIPSPLLCFG_6348_M1CPU_SHIFT)
  30. #define MIPSPLLCFG_6348_N2_SHIFT 15
  31. #define MIPSPLLCFG_6348_N2_MASK (0x1F << MIPSPLLCFG_6348_N2_SHIFT)
  32. #define MIPSPLLCFG_6348_N1_SHIFT 20
  33. #define MIPSPLLCFG_6348_N1_MASK (0x7 << MIPSPLLCFG_6348_N1_SHIFT)
  34. #define REG_BCM6358_DDR_DMIPSPLLCFG 0x12b8
  35. #define DMIPSPLLCFG_6358_M1_SHIFT 0
  36. #define DMIPSPLLCFG_6358_M1_MASK (0xff << DMIPSPLLCFG_6358_M1_SHIFT)
  37. #define DMIPSPLLCFG_6358_N1_SHIFT 23
  38. #define DMIPSPLLCFG_6358_N1_MASK (0x3f << DMIPSPLLCFG_6358_N1_SHIFT)
  39. #define DMIPSPLLCFG_6358_N2_SHIFT 29
  40. #define DMIPSPLLCFG_6358_N2_MASK (0x7 << DMIPSPLLCFG_6358_N2_SHIFT)
  41. #define REG_BCM63268_MISC_STRAPBUS 0x1814
  42. #define STRAPBUS_63268_FCVO_SHIFT 21
  43. #define STRAPBUS_63268_FCVO_MASK (0xf << STRAPBUS_63268_FCVO_SHIFT)
  44. struct bmips_cpu_priv;
  45. struct bmips_cpu_hw {
  46. int (*get_cpu_desc)(struct bmips_cpu_priv *priv, char *buf, int size);
  47. ulong (*get_cpu_freq)(struct bmips_cpu_priv *);
  48. int (*get_cpu_count)(struct bmips_cpu_priv *);
  49. };
  50. struct bmips_cpu_priv {
  51. void __iomem *regs;
  52. const struct bmips_cpu_hw *hw;
  53. };
  54. /* Specific CPU Ops */
  55. static int bmips_short_cpu_desc(struct bmips_cpu_priv *priv, char *buf,
  56. int size)
  57. {
  58. unsigned short cpu_id;
  59. unsigned char cpu_rev;
  60. u32 val;
  61. val = readl_be(priv->regs);
  62. cpu_id = (val & REV_CHIPID_MASK) >> REV_CHIPID_SHIFT;
  63. cpu_rev = (val & REV_REVID_MASK) >> REV_REVID_SHIFT;
  64. snprintf(buf, size, "BCM%04X%02X", cpu_id, cpu_rev);
  65. return 0;
  66. }
  67. static int bmips_long_cpu_desc(struct bmips_cpu_priv *priv, char *buf,
  68. int size)
  69. {
  70. unsigned int cpu_id;
  71. unsigned char cpu_rev;
  72. u32 val;
  73. val = readl_be(priv->regs);
  74. cpu_id = (val & REV_LONG_CHIPID_MASK) >> REV_LONG_CHIPID_SHIFT;
  75. cpu_rev = (val & REV_REVID_MASK) >> REV_REVID_SHIFT;
  76. snprintf(buf, size, "BCM%05X%02X", cpu_id, cpu_rev);
  77. return 0;
  78. }
  79. static ulong bcm3380_get_cpu_freq(struct bmips_cpu_priv *priv)
  80. {
  81. return 333000000;
  82. }
  83. static ulong bcm6328_get_cpu_freq(struct bmips_cpu_priv *priv)
  84. {
  85. unsigned int mips_pll_fcvo;
  86. mips_pll_fcvo = readl_be(priv->regs + REG_BCM6328_MISC_STRAPBUS);
  87. mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_6328_FCVO_MASK)
  88. >> STRAPBUS_6328_FCVO_SHIFT;
  89. switch (mips_pll_fcvo) {
  90. case 0x12:
  91. case 0x14:
  92. case 0x19:
  93. return 160000000;
  94. case 0x1c:
  95. return 192000000;
  96. case 0x13:
  97. case 0x15:
  98. return 200000000;
  99. case 0x1a:
  100. return 384000000;
  101. case 0x16:
  102. return 400000000;
  103. default:
  104. return 320000000;
  105. }
  106. }
  107. static ulong bcm6338_get_cpu_freq(struct bmips_cpu_priv *priv)
  108. {
  109. return 240000000;
  110. }
  111. static ulong bcm6348_get_cpu_freq(struct bmips_cpu_priv *priv)
  112. {
  113. unsigned int tmp, n1, n2, m1;
  114. tmp = readl_be(priv->regs + REG_BCM6348_PERF_MIPSPLLCFG);
  115. n1 = (tmp & MIPSPLLCFG_6348_N1_MASK) >> MIPSPLLCFG_6348_N1_SHIFT;
  116. n2 = (tmp & MIPSPLLCFG_6348_N2_MASK) >> MIPSPLLCFG_6348_N2_SHIFT;
  117. m1 = (tmp & MIPSPLLCFG_6348_M1CPU_MASK) >> MIPSPLLCFG_6348_M1CPU_SHIFT;
  118. return (16 * 1000000 * (n1 + 1) * (n2 + 2)) / (m1 + 1);
  119. }
  120. static ulong bcm6358_get_cpu_freq(struct bmips_cpu_priv *priv)
  121. {
  122. unsigned int tmp, n1, n2, m1;
  123. tmp = readl_be(priv->regs + REG_BCM6358_DDR_DMIPSPLLCFG);
  124. n1 = (tmp & DMIPSPLLCFG_6358_N1_MASK) >> DMIPSPLLCFG_6358_N1_SHIFT;
  125. n2 = (tmp & DMIPSPLLCFG_6358_N2_MASK) >> DMIPSPLLCFG_6358_N2_SHIFT;
  126. m1 = (tmp & DMIPSPLLCFG_6358_M1_MASK) >> DMIPSPLLCFG_6358_M1_SHIFT;
  127. return (16 * 1000000 * n1 * n2) / m1;
  128. }
  129. static ulong bcm63268_get_cpu_freq(struct bmips_cpu_priv *priv)
  130. {
  131. unsigned int mips_pll_fcvo;
  132. mips_pll_fcvo = readl_be(priv->regs + REG_BCM63268_MISC_STRAPBUS);
  133. mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_63268_FCVO_MASK)
  134. >> STRAPBUS_63268_FCVO_SHIFT;
  135. switch (mips_pll_fcvo) {
  136. case 0x3:
  137. case 0xe:
  138. return 320000000;
  139. case 0xa:
  140. return 333000000;
  141. case 0x2:
  142. case 0xb:
  143. case 0xf:
  144. return 400000000;
  145. default:
  146. return 0;
  147. }
  148. }
  149. static int bcm6328_get_cpu_count(struct bmips_cpu_priv *priv)
  150. {
  151. u32 val = readl_be(priv->regs + REG_BCM6328_OTP);
  152. if (val & BCM6328_TP1_DISABLED)
  153. return 1;
  154. else
  155. return 2;
  156. }
  157. static int bcm6345_get_cpu_count(struct bmips_cpu_priv *priv)
  158. {
  159. return 1;
  160. }
  161. static int bcm6358_get_cpu_count(struct bmips_cpu_priv *priv)
  162. {
  163. return 2;
  164. }
  165. static const struct bmips_cpu_hw bmips_cpu_bcm3380 = {
  166. .get_cpu_desc = bmips_short_cpu_desc,
  167. .get_cpu_freq = bcm3380_get_cpu_freq,
  168. .get_cpu_count = bcm6358_get_cpu_count,
  169. };
  170. static const struct bmips_cpu_hw bmips_cpu_bcm6328 = {
  171. .get_cpu_desc = bmips_long_cpu_desc,
  172. .get_cpu_freq = bcm6328_get_cpu_freq,
  173. .get_cpu_count = bcm6328_get_cpu_count,
  174. };
  175. static const struct bmips_cpu_hw bmips_cpu_bcm6338 = {
  176. .get_cpu_desc = bmips_short_cpu_desc,
  177. .get_cpu_freq = bcm6338_get_cpu_freq,
  178. .get_cpu_count = bcm6345_get_cpu_count,
  179. };
  180. static const struct bmips_cpu_hw bmips_cpu_bcm6348 = {
  181. .get_cpu_desc = bmips_short_cpu_desc,
  182. .get_cpu_freq = bcm6348_get_cpu_freq,
  183. .get_cpu_count = bcm6345_get_cpu_count,
  184. };
  185. static const struct bmips_cpu_hw bmips_cpu_bcm6358 = {
  186. .get_cpu_desc = bmips_short_cpu_desc,
  187. .get_cpu_freq = bcm6358_get_cpu_freq,
  188. .get_cpu_count = bcm6358_get_cpu_count,
  189. };
  190. static const struct bmips_cpu_hw bmips_cpu_bcm63268 = {
  191. .get_cpu_desc = bmips_long_cpu_desc,
  192. .get_cpu_freq = bcm63268_get_cpu_freq,
  193. .get_cpu_count = bcm6358_get_cpu_count,
  194. };
  195. /* Generic CPU Ops */
  196. static int bmips_cpu_get_desc(struct udevice *dev, char *buf, int size)
  197. {
  198. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  199. const struct bmips_cpu_hw *hw = priv->hw;
  200. return hw->get_cpu_desc(priv, buf, size);
  201. }
  202. static int bmips_cpu_get_info(struct udevice *dev, struct cpu_info *info)
  203. {
  204. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  205. const struct bmips_cpu_hw *hw = priv->hw;
  206. info->cpu_freq = hw->get_cpu_freq(priv);
  207. info->features = BIT(CPU_FEAT_L1_CACHE);
  208. info->features |= BIT(CPU_FEAT_MMU);
  209. info->features |= BIT(CPU_FEAT_DEVICE_ID);
  210. return 0;
  211. }
  212. static int bmips_cpu_get_count(struct udevice *dev)
  213. {
  214. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  215. const struct bmips_cpu_hw *hw = priv->hw;
  216. return hw->get_cpu_count(priv);
  217. }
  218. static int bmips_cpu_get_vendor(struct udevice *dev, char *buf, int size)
  219. {
  220. snprintf(buf, size, "Broadcom");
  221. return 0;
  222. }
  223. static const struct cpu_ops bmips_cpu_ops = {
  224. .get_desc = bmips_cpu_get_desc,
  225. .get_info = bmips_cpu_get_info,
  226. .get_count = bmips_cpu_get_count,
  227. .get_vendor = bmips_cpu_get_vendor,
  228. };
  229. /* BMIPS CPU driver */
  230. int bmips_cpu_bind(struct udevice *dev)
  231. {
  232. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  233. plat->cpu_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  234. "reg", -1);
  235. plat->device_id = read_c0_prid();
  236. return 0;
  237. }
  238. int bmips_cpu_probe(struct udevice *dev)
  239. {
  240. struct bmips_cpu_priv *priv = dev_get_priv(dev);
  241. const struct bmips_cpu_hw *hw =
  242. (const struct bmips_cpu_hw *)dev_get_driver_data(dev);
  243. fdt_addr_t addr;
  244. fdt_size_t size;
  245. addr = dev_get_addr_size_index(dev_get_parent(dev), 0, &size);
  246. if (addr == FDT_ADDR_T_NONE)
  247. return -EINVAL;
  248. priv->regs = ioremap(addr, size);
  249. priv->hw = hw;
  250. return 0;
  251. }
  252. static const struct udevice_id bmips_cpu_ids[] = {
  253. {
  254. .compatible = "brcm,bcm3380-cpu",
  255. .data = (ulong)&bmips_cpu_bcm3380,
  256. }, {
  257. .compatible = "brcm,bcm6328-cpu",
  258. .data = (ulong)&bmips_cpu_bcm6328,
  259. }, {
  260. .compatible = "brcm,bcm6338-cpu",
  261. .data = (ulong)&bmips_cpu_bcm6338,
  262. }, {
  263. .compatible = "brcm,bcm6348-cpu",
  264. .data = (ulong)&bmips_cpu_bcm6348,
  265. }, {
  266. .compatible = "brcm,bcm6358-cpu",
  267. .data = (ulong)&bmips_cpu_bcm6358,
  268. }, {
  269. .compatible = "brcm,bcm63268-cpu",
  270. .data = (ulong)&bmips_cpu_bcm63268,
  271. },
  272. { /* sentinel */ }
  273. };
  274. U_BOOT_DRIVER(bmips_cpu_drv) = {
  275. .name = "bmips_cpu",
  276. .id = UCLASS_CPU,
  277. .of_match = bmips_cpu_ids,
  278. .bind = bmips_cpu_bind,
  279. .probe = bmips_cpu_probe,
  280. .priv_auto_alloc_size = sizeof(struct bmips_cpu_priv),
  281. .ops = &bmips_cpu_ops,
  282. .flags = DM_FLAG_PRE_RELOC,
  283. };
  284. #ifdef CONFIG_DISPLAY_CPUINFO
  285. int print_cpuinfo(void)
  286. {
  287. struct cpu_info cpu;
  288. struct udevice *dev;
  289. int err;
  290. char desc[100];
  291. err = uclass_get_device(UCLASS_CPU, 0, &dev);
  292. if (err)
  293. return 0;
  294. err = cpu_get_info(dev, &cpu);
  295. if (err)
  296. return 0;
  297. err = cpu_get_desc(dev, desc, sizeof(desc));
  298. if (err)
  299. return 0;
  300. printf("Chip ID: %s, MIPS: ", desc);
  301. print_freq(cpu.cpu_freq, "\n");
  302. return 0;
  303. }
  304. #endif