2 * Sonics Silicon Backplane
3 * Broadcom ChipCommon core driver
5 * Copyright 2005, Broadcom Corporation
6 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
7 * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
9 * Licensed under the GNU/GPL. See COPYING for details.
12 #include <linux/ssb/ssb.h>
13 #include <linux/ssb/ssb_regs.h>
14 #include <linux/export.h>
15 #include <linux/pci.h>
16 #include <linux/bcm47xx_wdt.h>
18 #include "ssb_private.h"
24 SSB_CHIPCO_CLKSRC_PCI,
25 /* Crystal slow clock oscillator */
26 SSB_CHIPCO_CLKSRC_XTALOS,
27 /* Low power oscillator */
28 SSB_CHIPCO_CLKSRC_LOPWROS,
32 static inline u32 chipco_write32_masked(struct ssb_chipcommon *cc, u16 offset,
36 value |= chipco_read32(cc, offset) & ~mask;
37 chipco_write32(cc, offset, value);
42 void ssb_chipco_set_clockmode(struct ssb_chipcommon *cc,
43 enum ssb_clkmode mode)
45 struct ssb_device *ccdev = cc->dev;
53 /* We support SLOW only on 6..9 */
54 if (ccdev->id.revision >= 10 && mode == SSB_CLKMODE_SLOW)
55 mode = SSB_CLKMODE_DYNAMIC;
57 if (cc->capabilities & SSB_CHIPCO_CAP_PMU)
58 return; /* PMU controls clockmode, separated function needed */
59 SSB_WARN_ON(ccdev->id.revision >= 20);
61 /* chipcommon cores prior to rev6 don't support dynamic clock control */
62 if (ccdev->id.revision < 6)
65 /* ChipCommon cores rev10+ need testing */
66 if (ccdev->id.revision >= 10)
69 if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL))
73 case SSB_CLKMODE_SLOW: /* For revs 6..9 only */
74 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
75 tmp |= SSB_CHIPCO_SLOWCLKCTL_FSLOW;
76 chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp);
78 case SSB_CLKMODE_FAST:
79 if (ccdev->id.revision < 10) {
80 ssb_pci_xtal(bus, SSB_GPIO_XTAL, 1); /* Force crystal on */
81 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
82 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW;
83 tmp |= SSB_CHIPCO_SLOWCLKCTL_IPLL;
84 chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp);
86 chipco_write32(cc, SSB_CHIPCO_SYSCLKCTL,
87 (chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL) |
88 SSB_CHIPCO_SYSCLKCTL_FORCEHT));
89 /* udelay(150); TODO: not available in early init */
92 case SSB_CLKMODE_DYNAMIC:
93 if (ccdev->id.revision < 10) {
94 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
95 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW;
96 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_IPLL;
97 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_ENXTAL;
98 if ((tmp & SSB_CHIPCO_SLOWCLKCTL_SRC) !=
99 SSB_CHIPCO_SLOWCLKCTL_SRC_XTAL)
100 tmp |= SSB_CHIPCO_SLOWCLKCTL_ENXTAL;
101 chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp);
103 /* For dynamic control, we have to release our xtal_pu
105 if (tmp & SSB_CHIPCO_SLOWCLKCTL_ENXTAL)
106 ssb_pci_xtal(bus, SSB_GPIO_XTAL, 0);
108 chipco_write32(cc, SSB_CHIPCO_SYSCLKCTL,
109 (chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL) &
110 ~SSB_CHIPCO_SYSCLKCTL_FORCEHT));
118 /* Get the Slow Clock Source */
119 static enum ssb_clksrc chipco_pctl_get_slowclksrc(struct ssb_chipcommon *cc)
121 struct ssb_bus *bus = cc->dev->bus;
122 u32 uninitialized_var(tmp);
124 if (cc->dev->id.revision < 6) {
125 if (bus->bustype == SSB_BUSTYPE_SSB ||
126 bus->bustype == SSB_BUSTYPE_PCMCIA)
127 return SSB_CHIPCO_CLKSRC_XTALOS;
128 if (bus->bustype == SSB_BUSTYPE_PCI) {
129 pci_read_config_dword(bus->host_pci, SSB_GPIO_OUT, &tmp);
131 return SSB_CHIPCO_CLKSRC_PCI;
132 return SSB_CHIPCO_CLKSRC_XTALOS;
135 if (cc->dev->id.revision < 10) {
136 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
139 return SSB_CHIPCO_CLKSRC_LOPWROS;
141 return SSB_CHIPCO_CLKSRC_XTALOS;
143 return SSB_CHIPCO_CLKSRC_PCI;
146 return SSB_CHIPCO_CLKSRC_XTALOS;
149 /* Get maximum or minimum (depending on get_max flag) slowclock frequency. */
150 static int chipco_pctl_clockfreqlimit(struct ssb_chipcommon *cc, int get_max)
152 int uninitialized_var(limit);
153 enum ssb_clksrc clocksrc;
157 clocksrc = chipco_pctl_get_slowclksrc(cc);
158 if (cc->dev->id.revision < 6) {
160 case SSB_CHIPCO_CLKSRC_PCI:
163 case SSB_CHIPCO_CLKSRC_XTALOS:
169 } else if (cc->dev->id.revision < 10) {
171 case SSB_CHIPCO_CLKSRC_LOPWROS:
173 case SSB_CHIPCO_CLKSRC_XTALOS:
174 case SSB_CHIPCO_CLKSRC_PCI:
175 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
176 divisor = (tmp >> 16) + 1;
181 tmp = chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL);
182 divisor = (tmp >> 16) + 1;
187 case SSB_CHIPCO_CLKSRC_LOPWROS:
193 case SSB_CHIPCO_CLKSRC_XTALOS:
199 case SSB_CHIPCO_CLKSRC_PCI:
211 static void chipco_powercontrol_init(struct ssb_chipcommon *cc)
213 struct ssb_bus *bus = cc->dev->bus;
215 if (bus->chip_id == 0x4321) {
216 if (bus->chip_rev == 0)
217 chipco_write32(cc, SSB_CHIPCO_CHIPCTL, 0x3A4);
218 else if (bus->chip_rev == 1)
219 chipco_write32(cc, SSB_CHIPCO_CHIPCTL, 0xA4);
222 if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL))
225 if (cc->dev->id.revision >= 10) {
226 /* Set Idle Power clock rate to 1Mhz */
227 chipco_write32(cc, SSB_CHIPCO_SYSCLKCTL,
228 (chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL) &
229 0x0000FFFF) | 0x00040000);
233 maxfreq = chipco_pctl_clockfreqlimit(cc, 1);
234 chipco_write32(cc, SSB_CHIPCO_PLLONDELAY,
235 (maxfreq * 150 + 999999) / 1000000);
236 chipco_write32(cc, SSB_CHIPCO_FREFSELDELAY,
237 (maxfreq * 15 + 999999) / 1000000);
241 /* http://bcm-v4.sipsolutions.net/802.11/PmuFastPwrupDelay */
242 static u16 pmu_fast_powerup_delay(struct ssb_chipcommon *cc)
244 struct ssb_bus *bus = cc->dev->bus;
246 switch (bus->chip_id) {
258 /* http://bcm-v4.sipsolutions.net/802.11/ClkctlFastPwrupDelay */
259 static void calc_fast_powerup_delay(struct ssb_chipcommon *cc)
261 struct ssb_bus *bus = cc->dev->bus;
266 if (bus->bustype != SSB_BUSTYPE_PCI)
269 if (cc->capabilities & SSB_CHIPCO_CAP_PMU) {
270 cc->fast_pwrup_delay = pmu_fast_powerup_delay(cc);
274 if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL))
277 minfreq = chipco_pctl_clockfreqlimit(cc, 0);
278 pll_on_delay = chipco_read32(cc, SSB_CHIPCO_PLLONDELAY);
279 tmp = (((pll_on_delay + 2) * 1000000) + (minfreq - 1)) / minfreq;
280 SSB_WARN_ON(tmp & ~0xFFFF);
282 cc->fast_pwrup_delay = tmp;
285 static u32 ssb_chipco_alp_clock(struct ssb_chipcommon *cc)
287 if (cc->capabilities & SSB_CHIPCO_CAP_PMU)
288 return ssb_pmu_get_alp_clock(cc);
293 static u32 ssb_chipco_watchdog_get_max_timer(struct ssb_chipcommon *cc)
297 if (cc->capabilities & SSB_CHIPCO_CAP_PMU) {
298 if (cc->dev->id.revision < 26)
301 nb = (cc->dev->id.revision >= 37) ? 32 : 24;
308 return (1 << nb) - 1;
311 u32 ssb_chipco_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt, u32 ticks)
313 struct ssb_chipcommon *cc = bcm47xx_wdt_get_drvdata(wdt);
315 if (cc->dev->bus->bustype != SSB_BUSTYPE_SSB)
318 return ssb_chipco_watchdog_timer_set(cc, ticks);
321 u32 ssb_chipco_watchdog_timer_set_ms(struct bcm47xx_wdt *wdt, u32 ms)
323 struct ssb_chipcommon *cc = bcm47xx_wdt_get_drvdata(wdt);
326 if (cc->dev->bus->bustype != SSB_BUSTYPE_SSB)
329 ticks = ssb_chipco_watchdog_timer_set(cc, cc->ticks_per_ms * ms);
330 return ticks / cc->ticks_per_ms;
333 static int ssb_chipco_watchdog_ticks_per_ms(struct ssb_chipcommon *cc)
335 struct ssb_bus *bus = cc->dev->bus;
337 if (cc->capabilities & SSB_CHIPCO_CAP_PMU) {
338 /* based on 32KHz ILP clock */
341 if (cc->dev->id.revision < 18)
342 return ssb_clockspeed(bus) / 1000;
344 return ssb_chipco_alp_clock(cc) / 1000;
348 void ssb_chipcommon_init(struct ssb_chipcommon *cc)
351 return; /* We don't have a ChipCommon */
353 spin_lock_init(&cc->gpio_lock);
355 if (cc->dev->id.revision >= 11)
356 cc->status = chipco_read32(cc, SSB_CHIPCO_CHIPSTAT);
357 ssb_dbg("chipcommon status is 0x%x\n", cc->status);
359 if (cc->dev->id.revision >= 20) {
360 chipco_write32(cc, SSB_CHIPCO_GPIOPULLUP, 0);
361 chipco_write32(cc, SSB_CHIPCO_GPIOPULLDOWN, 0);
365 chipco_powercontrol_init(cc);
366 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_FAST);
367 calc_fast_powerup_delay(cc);
369 if (cc->dev->bus->bustype == SSB_BUSTYPE_SSB) {
370 cc->ticks_per_ms = ssb_chipco_watchdog_ticks_per_ms(cc);
371 cc->max_timer_ms = ssb_chipco_watchdog_get_max_timer(cc) / cc->ticks_per_ms;
375 void ssb_chipco_suspend(struct ssb_chipcommon *cc)
379 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW);
382 void ssb_chipco_resume(struct ssb_chipcommon *cc)
386 chipco_powercontrol_init(cc);
387 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_FAST);
390 /* Get the processor clock */
391 void ssb_chipco_get_clockcpu(struct ssb_chipcommon *cc,
392 u32 *plltype, u32 *n, u32 *m)
394 *n = chipco_read32(cc, SSB_CHIPCO_CLOCK_N);
395 *plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT);
401 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_MIPS);
404 /* 5350 uses m2 to control mips */
405 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_M2);
408 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_SB);
413 /* Get the bus clock */
414 void ssb_chipco_get_clockcontrol(struct ssb_chipcommon *cc,
415 u32 *plltype, u32 *n, u32 *m)
417 *n = chipco_read32(cc, SSB_CHIPCO_CLOCK_N);
418 *plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT);
420 case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */
421 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_MIPS);
423 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
424 if (cc->dev->bus->chip_id != 0x5365) {
425 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_M2);
430 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_SB);
434 void ssb_chipco_timing_init(struct ssb_chipcommon *cc,
437 struct ssb_device *dev = cc->dev;
438 struct ssb_bus *bus = dev->bus;
441 /* set register for external IO to control LED. */
442 chipco_write32(cc, SSB_CHIPCO_PROG_CFG, 0x11);
443 tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT; /* Waitcount-3 = 10ns */
444 tmp |= DIV_ROUND_UP(40, ns) << SSB_PROG_WCNT_1_SHIFT; /* Waitcount-1 = 40ns */
445 tmp |= DIV_ROUND_UP(240, ns); /* Waitcount-0 = 240ns */
446 chipco_write32(cc, SSB_CHIPCO_PROG_WAITCNT, tmp); /* 0x01020a0c for a 100Mhz clock */
448 /* Set timing for the flash */
449 tmp = DIV_ROUND_UP(10, ns) << SSB_FLASH_WCNT_3_SHIFT; /* Waitcount-3 = 10nS */
450 tmp |= DIV_ROUND_UP(10, ns) << SSB_FLASH_WCNT_1_SHIFT; /* Waitcount-1 = 10nS */
451 tmp |= DIV_ROUND_UP(120, ns); /* Waitcount-0 = 120nS */
452 if ((bus->chip_id == 0x5365) ||
453 (dev->id.revision < 9))
454 chipco_write32(cc, SSB_CHIPCO_FLASH_WAITCNT, tmp);
455 if ((bus->chip_id == 0x5365) ||
456 (dev->id.revision < 9) ||
457 ((bus->chip_id == 0x5350) && (bus->chip_rev == 0)))
458 chipco_write32(cc, SSB_CHIPCO_PCMCIA_MEMWAIT, tmp);
460 if (bus->chip_id == 0x5350) {
462 tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT; /* Waitcount-3 = 10ns */
463 tmp |= DIV_ROUND_UP(20, ns) << SSB_PROG_WCNT_2_SHIFT; /* Waitcount-2 = 20ns */
464 tmp |= DIV_ROUND_UP(100, ns) << SSB_PROG_WCNT_1_SHIFT; /* Waitcount-1 = 100ns */
465 tmp |= DIV_ROUND_UP(120, ns); /* Waitcount-0 = 120ns */
466 chipco_write32(cc, SSB_CHIPCO_PROG_WAITCNT, tmp); /* 0x01020a0c for a 100Mhz clock */
470 /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
471 u32 ssb_chipco_watchdog_timer_set(struct ssb_chipcommon *cc, u32 ticks)
474 enum ssb_clkmode clkmode;
476 maxt = ssb_chipco_watchdog_get_max_timer(cc);
477 if (cc->capabilities & SSB_CHIPCO_CAP_PMU) {
480 else if (ticks > maxt)
482 chipco_write32(cc, SSB_CHIPCO_PMU_WATCHDOG, ticks);
484 clkmode = ticks ? SSB_CLKMODE_FAST : SSB_CLKMODE_DYNAMIC;
485 ssb_chipco_set_clockmode(cc, clkmode);
489 chipco_write32(cc, SSB_CHIPCO_WATCHDOG, ticks);
494 void ssb_chipco_irq_mask(struct ssb_chipcommon *cc, u32 mask, u32 value)
496 chipco_write32_masked(cc, SSB_CHIPCO_IRQMASK, mask, value);
499 u32 ssb_chipco_irq_status(struct ssb_chipcommon *cc, u32 mask)
501 return chipco_read32(cc, SSB_CHIPCO_IRQSTAT) & mask;
504 u32 ssb_chipco_gpio_in(struct ssb_chipcommon *cc, u32 mask)
506 return chipco_read32(cc, SSB_CHIPCO_GPIOIN) & mask;
509 u32 ssb_chipco_gpio_out(struct ssb_chipcommon *cc, u32 mask, u32 value)
514 spin_lock_irqsave(&cc->gpio_lock, flags);
515 res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOOUT, mask, value);
516 spin_unlock_irqrestore(&cc->gpio_lock, flags);
521 u32 ssb_chipco_gpio_outen(struct ssb_chipcommon *cc, u32 mask, u32 value)
526 spin_lock_irqsave(&cc->gpio_lock, flags);
527 res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOOUTEN, mask, value);
528 spin_unlock_irqrestore(&cc->gpio_lock, flags);
533 u32 ssb_chipco_gpio_control(struct ssb_chipcommon *cc, u32 mask, u32 value)
538 spin_lock_irqsave(&cc->gpio_lock, flags);
539 res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOCTL, mask, value);
540 spin_unlock_irqrestore(&cc->gpio_lock, flags);
544 EXPORT_SYMBOL(ssb_chipco_gpio_control);
546 u32 ssb_chipco_gpio_intmask(struct ssb_chipcommon *cc, u32 mask, u32 value)
551 spin_lock_irqsave(&cc->gpio_lock, flags);
552 res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOIRQ, mask, value);
553 spin_unlock_irqrestore(&cc->gpio_lock, flags);
558 u32 ssb_chipco_gpio_polarity(struct ssb_chipcommon *cc, u32 mask, u32 value)
563 spin_lock_irqsave(&cc->gpio_lock, flags);
564 res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOPOL, mask, value);
565 spin_unlock_irqrestore(&cc->gpio_lock, flags);
570 u32 ssb_chipco_gpio_pullup(struct ssb_chipcommon *cc, u32 mask, u32 value)
575 if (cc->dev->id.revision < 20)
578 spin_lock_irqsave(&cc->gpio_lock, flags);
579 res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOPULLUP, mask, value);
580 spin_unlock_irqrestore(&cc->gpio_lock, flags);
585 u32 ssb_chipco_gpio_pulldown(struct ssb_chipcommon *cc, u32 mask, u32 value)
590 if (cc->dev->id.revision < 20)
593 spin_lock_irqsave(&cc->gpio_lock, flags);
594 res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOPULLDOWN, mask, value);
595 spin_unlock_irqrestore(&cc->gpio_lock, flags);
600 #ifdef CONFIG_SSB_SERIAL
601 int ssb_chipco_serial_init(struct ssb_chipcommon *cc,
602 struct ssb_serial_port *ports)
604 struct ssb_bus *bus = cc->dev->bus;
610 unsigned int ccrev = cc->dev->id.revision;
612 plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT);
613 irq = ssb_mips_irq(cc->dev);
615 if (plltype == SSB_PLLTYPE_1) {
617 baud_base = ssb_calc_clock_rate(plltype,
618 chipco_read32(cc, SSB_CHIPCO_CLOCK_N),
619 chipco_read32(cc, SSB_CHIPCO_CLOCK_M2));
623 /* BCM5354 uses constant 25MHz clock */
624 baud_base = 25000000;
626 /* Set the override bit so we don't divide it */
627 chipco_write32(cc, SSB_CHIPCO_CORECTL,
628 chipco_read32(cc, SSB_CHIPCO_CORECTL)
629 | SSB_CHIPCO_CORECTL_UARTCLK0);
630 } else if ((ccrev >= 11) && (ccrev != 15)) {
631 baud_base = ssb_chipco_alp_clock(cc);
634 /* Turn off UART clock before switching clocksource. */
635 chipco_write32(cc, SSB_CHIPCO_CORECTL,
636 chipco_read32(cc, SSB_CHIPCO_CORECTL)
637 & ~SSB_CHIPCO_CORECTL_UARTCLKEN);
639 /* Set the override bit so we don't divide it */
640 chipco_write32(cc, SSB_CHIPCO_CORECTL,
641 chipco_read32(cc, SSB_CHIPCO_CORECTL)
642 | SSB_CHIPCO_CORECTL_UARTCLK0);
644 /* Re-enable the UART clock. */
645 chipco_write32(cc, SSB_CHIPCO_CORECTL,
646 chipco_read32(cc, SSB_CHIPCO_CORECTL)
647 | SSB_CHIPCO_CORECTL_UARTCLKEN);
649 } else if (ccrev >= 3) {
650 /* Internal backplane clock */
651 baud_base = ssb_clockspeed(bus);
652 div = chipco_read32(cc, SSB_CHIPCO_CLKDIV)
653 & SSB_CHIPCO_CLKDIV_UART;
655 /* Fixed internal backplane clock */
656 baud_base = 88000000;
660 /* Clock source depends on strapping if UartClkOverride is unset */
662 !(chipco_read32(cc, SSB_CHIPCO_CORECTL) & SSB_CHIPCO_CORECTL_UARTCLK0)) {
663 if ((cc->capabilities & SSB_CHIPCO_CAP_UARTCLK) ==
664 SSB_CHIPCO_CAP_UARTCLK_INT) {
665 /* Internal divided backplane clock */
668 /* Assume external clock of 1.8432 MHz */
674 /* Determine the registers of the UARTs */
675 n = (cc->capabilities & SSB_CHIPCO_CAP_NRUART);
676 for (i = 0; i < n; i++) {
677 void __iomem *cc_mmio;
678 void __iomem *uart_regs;
680 cc_mmio = cc->dev->bus->mmio + (cc->dev->core_index * SSB_CORE_SIZE);
681 uart_regs = cc_mmio + SSB_CHIPCO_UART0_DATA;
682 /* Offset changed at after rev 0 */
684 uart_regs += (i * 8);
686 uart_regs += (i * 256);
689 ports[i].regs = uart_regs;
691 ports[i].baud_base = baud_base;
692 ports[i].reg_shift = 0;
697 #endif /* CONFIG_SSB_SERIAL */