2 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
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>
8 * SPDX-License-Identifier: GPL-2.0+
17 DECLARE_GLOBAL_DATA_PTR;
19 #define REV_CHIPID_SHIFT 16
20 #define REV_CHIPID_MASK (0xffff << REV_CHIPID_SHIFT)
21 #define REV_LONG_CHIPID_SHIFT 12
22 #define REV_LONG_CHIPID_MASK (0xfffff << REV_LONG_CHIPID_SHIFT)
23 #define REV_REVID_SHIFT 0
24 #define REV_REVID_MASK (0xff << REV_REVID_SHIFT)
26 #define REG_BCM6328_OTP 0x62c
27 #define BCM6328_TP1_DISABLED BIT(9)
29 #define REG_BCM6318_STRAP_OVRDBUS 0x900
30 #define OVRDBUS_6318_FREQ_SHIFT 23
31 #define OVRDBUS_6318_FREQ_MASK (0x3 << OVRDBUS_6318_FREQ_SHIFT)
33 #define REG_BCM6328_MISC_STRAPBUS 0x1a40
34 #define STRAPBUS_6328_FCVO_SHIFT 7
35 #define STRAPBUS_6328_FCVO_MASK (0x1f << STRAPBUS_6328_FCVO_SHIFT)
37 #define REG_BCM6348_PERF_MIPSPLLCFG 0x34
38 #define MIPSPLLCFG_6348_M1CPU_SHIFT 6
39 #define MIPSPLLCFG_6348_M1CPU_MASK (0x7 << MIPSPLLCFG_6348_M1CPU_SHIFT)
40 #define MIPSPLLCFG_6348_N2_SHIFT 15
41 #define MIPSPLLCFG_6348_N2_MASK (0x1F << MIPSPLLCFG_6348_N2_SHIFT)
42 #define MIPSPLLCFG_6348_N1_SHIFT 20
43 #define MIPSPLLCFG_6348_N1_MASK (0x7 << MIPSPLLCFG_6348_N1_SHIFT)
45 #define REG_BCM6358_DDR_DMIPSPLLCFG 0x12b8
46 #define DMIPSPLLCFG_6358_M1_SHIFT 0
47 #define DMIPSPLLCFG_6358_M1_MASK (0xff << DMIPSPLLCFG_6358_M1_SHIFT)
48 #define DMIPSPLLCFG_6358_N1_SHIFT 23
49 #define DMIPSPLLCFG_6358_N1_MASK (0x3f << DMIPSPLLCFG_6358_N1_SHIFT)
50 #define DMIPSPLLCFG_6358_N2_SHIFT 29
51 #define DMIPSPLLCFG_6358_N2_MASK (0x7 << DMIPSPLLCFG_6358_N2_SHIFT)
53 #define REG_BCM6362_MISC_STRAPBUS 0x1814
54 #define STRAPBUS_6362_FCVO_SHIFT 1
55 #define STRAPBUS_6362_FCVO_MASK (0x1f << STRAPBUS_6362_FCVO_SHIFT)
57 #define REG_BCM6368_DDR_DMIPSPLLCFG 0x12a0
58 #define DMIPSPLLCFG_6368_P1_SHIFT 0
59 #define DMIPSPLLCFG_6368_P1_MASK (0xf << DMIPSPLLCFG_6368_P1_SHIFT)
60 #define DMIPSPLLCFG_6368_P2_SHIFT 4
61 #define DMIPSPLLCFG_6368_P2_MASK (0xf << DMIPSPLLCFG_6368_P2_SHIFT)
62 #define DMIPSPLLCFG_6368_NDIV_SHIFT 16
63 #define DMIPSPLLCFG_6368_NDIV_MASK (0x1ff << DMIPSPLLCFG_6368_NDIV_SHIFT)
64 #define REG_BCM6368_DDR_DMIPSPLLDIV 0x12a4
65 #define DMIPSPLLDIV_6368_MDIV_SHIFT 0
66 #define DMIPSPLLDIV_6368_MDIV_MASK (0xff << DMIPSPLLDIV_6368_MDIV_SHIFT)
68 #define REG_BCM63268_MISC_STRAPBUS 0x1814
69 #define STRAPBUS_63268_FCVO_SHIFT 21
70 #define STRAPBUS_63268_FCVO_MASK (0xf << STRAPBUS_63268_FCVO_SHIFT)
72 struct bmips_cpu_priv;
75 int (*get_cpu_desc)(struct bmips_cpu_priv *priv, char *buf, int size);
76 ulong (*get_cpu_freq)(struct bmips_cpu_priv *);
77 int (*get_cpu_count)(struct bmips_cpu_priv *);
80 struct bmips_cpu_priv {
82 const struct bmips_cpu_hw *hw;
85 /* Specific CPU Ops */
86 static int bmips_short_cpu_desc(struct bmips_cpu_priv *priv, char *buf,
89 unsigned short cpu_id;
90 unsigned char cpu_rev;
93 val = readl_be(priv->regs);
94 cpu_id = (val & REV_CHIPID_MASK) >> REV_CHIPID_SHIFT;
95 cpu_rev = (val & REV_REVID_MASK) >> REV_REVID_SHIFT;
97 snprintf(buf, size, "BCM%04X%02X", cpu_id, cpu_rev);
102 static int bmips_long_cpu_desc(struct bmips_cpu_priv *priv, char *buf,
106 unsigned char cpu_rev;
109 val = readl_be(priv->regs);
110 cpu_id = (val & REV_LONG_CHIPID_MASK) >> REV_LONG_CHIPID_SHIFT;
111 cpu_rev = (val & REV_REVID_MASK) >> REV_REVID_SHIFT;
113 snprintf(buf, size, "BCM%05X%02X", cpu_id, cpu_rev);
118 static ulong bcm3380_get_cpu_freq(struct bmips_cpu_priv *priv)
123 static ulong bcm6318_get_cpu_freq(struct bmips_cpu_priv *priv)
125 unsigned int mips_pll_fcvo;
127 mips_pll_fcvo = readl_be(priv->regs + REG_BCM6318_STRAP_OVRDBUS);
128 mips_pll_fcvo = (mips_pll_fcvo & OVRDBUS_6318_FREQ_MASK)
129 >> OVRDBUS_6318_FREQ_SHIFT;
131 switch (mips_pll_fcvo) {
145 static ulong bcm6328_get_cpu_freq(struct bmips_cpu_priv *priv)
147 unsigned int mips_pll_fcvo;
149 mips_pll_fcvo = readl_be(priv->regs + REG_BCM6328_MISC_STRAPBUS);
150 mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_6328_FCVO_MASK)
151 >> STRAPBUS_6328_FCVO_SHIFT;
153 switch (mips_pll_fcvo) {
172 static ulong bcm6338_get_cpu_freq(struct bmips_cpu_priv *priv)
177 static ulong bcm6348_get_cpu_freq(struct bmips_cpu_priv *priv)
179 unsigned int tmp, n1, n2, m1;
181 tmp = readl_be(priv->regs + REG_BCM6348_PERF_MIPSPLLCFG);
182 n1 = (tmp & MIPSPLLCFG_6348_N1_MASK) >> MIPSPLLCFG_6348_N1_SHIFT;
183 n2 = (tmp & MIPSPLLCFG_6348_N2_MASK) >> MIPSPLLCFG_6348_N2_SHIFT;
184 m1 = (tmp & MIPSPLLCFG_6348_M1CPU_MASK) >> MIPSPLLCFG_6348_M1CPU_SHIFT;
186 return (16 * 1000000 * (n1 + 1) * (n2 + 2)) / (m1 + 1);
189 static ulong bcm6358_get_cpu_freq(struct bmips_cpu_priv *priv)
191 unsigned int tmp, n1, n2, m1;
193 tmp = readl_be(priv->regs + REG_BCM6358_DDR_DMIPSPLLCFG);
194 n1 = (tmp & DMIPSPLLCFG_6358_N1_MASK) >> DMIPSPLLCFG_6358_N1_SHIFT;
195 n2 = (tmp & DMIPSPLLCFG_6358_N2_MASK) >> DMIPSPLLCFG_6358_N2_SHIFT;
196 m1 = (tmp & DMIPSPLLCFG_6358_M1_MASK) >> DMIPSPLLCFG_6358_M1_SHIFT;
198 return (16 * 1000000 * n1 * n2) / m1;
201 static ulong bcm6362_get_cpu_freq(struct bmips_cpu_priv *priv)
203 unsigned int mips_pll_fcvo;
205 mips_pll_fcvo = readl_be(priv->regs + REG_BCM6362_MISC_STRAPBUS);
206 mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_6362_FCVO_MASK)
207 >> STRAPBUS_6362_FCVO_SHIFT;
209 switch (mips_pll_fcvo) {
239 static ulong bcm6368_get_cpu_freq(struct bmips_cpu_priv *priv)
241 unsigned int tmp, p1, p2, ndiv, m1;
243 tmp = readl_be(priv->regs + REG_BCM6368_DDR_DMIPSPLLCFG);
244 p1 = (tmp & DMIPSPLLCFG_6368_P1_MASK) >> DMIPSPLLCFG_6368_P1_SHIFT;
245 p2 = (tmp & DMIPSPLLCFG_6368_P2_MASK) >> DMIPSPLLCFG_6368_P2_SHIFT;
246 ndiv = (tmp & DMIPSPLLCFG_6368_NDIV_MASK) >>
247 DMIPSPLLCFG_6368_NDIV_SHIFT;
249 tmp = readl_be(priv->regs + REG_BCM6368_DDR_DMIPSPLLDIV);
250 m1 = (tmp & DMIPSPLLDIV_6368_MDIV_MASK) >> DMIPSPLLDIV_6368_MDIV_SHIFT;
252 return (((64 * 1000000) / p1) * p2 * ndiv) / m1;
255 static ulong bcm63268_get_cpu_freq(struct bmips_cpu_priv *priv)
257 unsigned int mips_pll_fcvo;
259 mips_pll_fcvo = readl_be(priv->regs + REG_BCM63268_MISC_STRAPBUS);
260 mips_pll_fcvo = (mips_pll_fcvo & STRAPBUS_63268_FCVO_MASK)
261 >> STRAPBUS_63268_FCVO_SHIFT;
263 switch (mips_pll_fcvo) {
278 static int bcm6328_get_cpu_count(struct bmips_cpu_priv *priv)
280 u32 val = readl_be(priv->regs + REG_BCM6328_OTP);
282 if (val & BCM6328_TP1_DISABLED)
288 static int bcm6345_get_cpu_count(struct bmips_cpu_priv *priv)
293 static int bcm6358_get_cpu_count(struct bmips_cpu_priv *priv)
298 static const struct bmips_cpu_hw bmips_cpu_bcm3380 = {
299 .get_cpu_desc = bmips_short_cpu_desc,
300 .get_cpu_freq = bcm3380_get_cpu_freq,
301 .get_cpu_count = bcm6358_get_cpu_count,
304 static const struct bmips_cpu_hw bmips_cpu_bcm6318 = {
305 .get_cpu_desc = bmips_short_cpu_desc,
306 .get_cpu_freq = bcm6318_get_cpu_freq,
307 .get_cpu_count = bcm6345_get_cpu_count,
310 static const struct bmips_cpu_hw bmips_cpu_bcm6328 = {
311 .get_cpu_desc = bmips_long_cpu_desc,
312 .get_cpu_freq = bcm6328_get_cpu_freq,
313 .get_cpu_count = bcm6328_get_cpu_count,
316 static const struct bmips_cpu_hw bmips_cpu_bcm6338 = {
317 .get_cpu_desc = bmips_short_cpu_desc,
318 .get_cpu_freq = bcm6338_get_cpu_freq,
319 .get_cpu_count = bcm6345_get_cpu_count,
322 static const struct bmips_cpu_hw bmips_cpu_bcm6348 = {
323 .get_cpu_desc = bmips_short_cpu_desc,
324 .get_cpu_freq = bcm6348_get_cpu_freq,
325 .get_cpu_count = bcm6345_get_cpu_count,
328 static const struct bmips_cpu_hw bmips_cpu_bcm6358 = {
329 .get_cpu_desc = bmips_short_cpu_desc,
330 .get_cpu_freq = bcm6358_get_cpu_freq,
331 .get_cpu_count = bcm6358_get_cpu_count,
334 static const struct bmips_cpu_hw bmips_cpu_bcm6362 = {
335 .get_cpu_desc = bmips_short_cpu_desc,
336 .get_cpu_freq = bcm6362_get_cpu_freq,
337 .get_cpu_count = bcm6358_get_cpu_count,
340 static const struct bmips_cpu_hw bmips_cpu_bcm6368 = {
341 .get_cpu_desc = bmips_short_cpu_desc,
342 .get_cpu_freq = bcm6368_get_cpu_freq,
343 .get_cpu_count = bcm6358_get_cpu_count,
346 static const struct bmips_cpu_hw bmips_cpu_bcm63268 = {
347 .get_cpu_desc = bmips_long_cpu_desc,
348 .get_cpu_freq = bcm63268_get_cpu_freq,
349 .get_cpu_count = bcm6358_get_cpu_count,
352 /* Generic CPU Ops */
353 static int bmips_cpu_get_desc(struct udevice *dev, char *buf, int size)
355 struct bmips_cpu_priv *priv = dev_get_priv(dev);
356 const struct bmips_cpu_hw *hw = priv->hw;
358 return hw->get_cpu_desc(priv, buf, size);
361 static int bmips_cpu_get_info(struct udevice *dev, struct cpu_info *info)
363 struct bmips_cpu_priv *priv = dev_get_priv(dev);
364 const struct bmips_cpu_hw *hw = priv->hw;
366 info->cpu_freq = hw->get_cpu_freq(priv);
367 info->features = BIT(CPU_FEAT_L1_CACHE);
368 info->features |= BIT(CPU_FEAT_MMU);
369 info->features |= BIT(CPU_FEAT_DEVICE_ID);
374 static int bmips_cpu_get_count(struct udevice *dev)
376 struct bmips_cpu_priv *priv = dev_get_priv(dev);
377 const struct bmips_cpu_hw *hw = priv->hw;
379 return hw->get_cpu_count(priv);
382 static int bmips_cpu_get_vendor(struct udevice *dev, char *buf, int size)
384 snprintf(buf, size, "Broadcom");
389 static const struct cpu_ops bmips_cpu_ops = {
390 .get_desc = bmips_cpu_get_desc,
391 .get_info = bmips_cpu_get_info,
392 .get_count = bmips_cpu_get_count,
393 .get_vendor = bmips_cpu_get_vendor,
396 /* BMIPS CPU driver */
397 int bmips_cpu_bind(struct udevice *dev)
399 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
401 plat->cpu_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
403 plat->device_id = read_c0_prid();
408 int bmips_cpu_probe(struct udevice *dev)
410 struct bmips_cpu_priv *priv = dev_get_priv(dev);
411 const struct bmips_cpu_hw *hw =
412 (const struct bmips_cpu_hw *)dev_get_driver_data(dev);
416 addr = devfdt_get_addr_size_index(dev_get_parent(dev), 0, &size);
417 if (addr == FDT_ADDR_T_NONE)
420 priv->regs = ioremap(addr, size);
426 static const struct udevice_id bmips_cpu_ids[] = {
428 .compatible = "brcm,bcm3380-cpu",
429 .data = (ulong)&bmips_cpu_bcm3380,
431 .compatible = "brcm,bcm6318-cpu",
432 .data = (ulong)&bmips_cpu_bcm6318,
434 .compatible = "brcm,bcm6328-cpu",
435 .data = (ulong)&bmips_cpu_bcm6328,
437 .compatible = "brcm,bcm6338-cpu",
438 .data = (ulong)&bmips_cpu_bcm6338,
440 .compatible = "brcm,bcm6348-cpu",
441 .data = (ulong)&bmips_cpu_bcm6348,
443 .compatible = "brcm,bcm6358-cpu",
444 .data = (ulong)&bmips_cpu_bcm6358,
446 .compatible = "brcm,bcm6362-cpu",
447 .data = (ulong)&bmips_cpu_bcm6362,
449 .compatible = "brcm,bcm6368-cpu",
450 .data = (ulong)&bmips_cpu_bcm6368,
452 .compatible = "brcm,bcm63268-cpu",
453 .data = (ulong)&bmips_cpu_bcm63268,
458 U_BOOT_DRIVER(bmips_cpu_drv) = {
461 .of_match = bmips_cpu_ids,
462 .bind = bmips_cpu_bind,
463 .probe = bmips_cpu_probe,
464 .priv_auto_alloc_size = sizeof(struct bmips_cpu_priv),
465 .ops = &bmips_cpu_ops,
466 .flags = DM_FLAG_PRE_RELOC,
469 #ifdef CONFIG_DISPLAY_CPUINFO
470 int print_cpuinfo(void)
477 err = uclass_get_device(UCLASS_CPU, 0, &dev);
481 err = cpu_get_info(dev, &cpu);
485 err = cpu_get_desc(dev, desc, sizeof(desc));
489 printf("Chip ID: %s, MIPS: ", desc);
490 print_freq(cpu.cpu_freq, "\n");