2 * Sonics Silicon Backplane
3 * Broadcom ChipCommon core driver
5 * Copyright 2005, Broadcom Corporation
6 * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
8 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/ssb/ssb.h>
12 #include <linux/ssb/ssb_regs.h>
13 #include <linux/pci.h>
15 #include "ssb_private.h"
21 SSB_CHIPCO_CLKSRC_PCI,
22 /* Crystal slow clock oscillator */
23 SSB_CHIPCO_CLKSRC_XTALOS,
24 /* Low power oscillator */
25 SSB_CHIPCO_CLKSRC_LOPWROS,
29 static inline u32 chipco_read32(struct ssb_chipcommon *cc,
32 return ssb_read32(cc->dev, offset);
35 static inline void chipco_write32(struct ssb_chipcommon *cc,
39 ssb_write32(cc->dev, offset, value);
42 static inline u32 chipco_write32_masked(struct ssb_chipcommon *cc, u16 offset,
46 value |= chipco_read32(cc, offset) & ~mask;
47 chipco_write32(cc, offset, value);
52 void ssb_chipco_set_clockmode(struct ssb_chipcommon *cc,
53 enum ssb_clkmode mode)
55 struct ssb_device *ccdev = cc->dev;
62 /* chipcommon cores prior to rev6 don't support dynamic clock control */
63 if (ccdev->id.revision < 6)
65 /* chipcommon cores rev10 are a whole new ball game */
66 if (ccdev->id.revision >= 10)
68 if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL))
72 case SSB_CLKMODE_SLOW:
73 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
74 tmp |= SSB_CHIPCO_SLOWCLKCTL_FSLOW;
75 chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp);
77 case SSB_CLKMODE_FAST:
78 ssb_pci_xtal(bus, SSB_GPIO_XTAL, 1); /* Force crystal on */
79 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
80 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW;
81 tmp |= SSB_CHIPCO_SLOWCLKCTL_IPLL;
82 chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp);
84 case SSB_CLKMODE_DYNAMIC:
85 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
86 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW;
87 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_IPLL;
88 tmp &= ~SSB_CHIPCO_SLOWCLKCTL_ENXTAL;
89 if ((tmp & SSB_CHIPCO_SLOWCLKCTL_SRC) != SSB_CHIPCO_SLOWCLKCTL_SRC_XTAL)
90 tmp |= SSB_CHIPCO_SLOWCLKCTL_ENXTAL;
91 chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp);
93 /* for dynamic control, we have to release our xtal_pu "force on" */
94 if (tmp & SSB_CHIPCO_SLOWCLKCTL_ENXTAL)
95 ssb_pci_xtal(bus, SSB_GPIO_XTAL, 0);
102 /* Get the Slow Clock Source */
103 static enum ssb_clksrc chipco_pctl_get_slowclksrc(struct ssb_chipcommon *cc)
105 struct ssb_bus *bus = cc->dev->bus;
106 u32 uninitialized_var(tmp);
108 if (cc->dev->id.revision < 6) {
109 if (bus->bustype == SSB_BUSTYPE_SSB ||
110 bus->bustype == SSB_BUSTYPE_PCMCIA)
111 return SSB_CHIPCO_CLKSRC_XTALOS;
112 if (bus->bustype == SSB_BUSTYPE_PCI) {
113 pci_read_config_dword(bus->host_pci, SSB_GPIO_OUT, &tmp);
115 return SSB_CHIPCO_CLKSRC_PCI;
116 return SSB_CHIPCO_CLKSRC_XTALOS;
119 if (cc->dev->id.revision < 10) {
120 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
123 return SSB_CHIPCO_CLKSRC_LOPWROS;
125 return SSB_CHIPCO_CLKSRC_XTALOS;
127 return SSB_CHIPCO_CLKSRC_PCI;
130 return SSB_CHIPCO_CLKSRC_XTALOS;
133 /* Get maximum or minimum (depending on get_max flag) slowclock frequency. */
134 static int chipco_pctl_clockfreqlimit(struct ssb_chipcommon *cc, int get_max)
136 int uninitialized_var(limit);
137 enum ssb_clksrc clocksrc;
141 clocksrc = chipco_pctl_get_slowclksrc(cc);
142 if (cc->dev->id.revision < 6) {
144 case SSB_CHIPCO_CLKSRC_PCI:
147 case SSB_CHIPCO_CLKSRC_XTALOS:
153 } else if (cc->dev->id.revision < 10) {
155 case SSB_CHIPCO_CLKSRC_LOPWROS:
157 case SSB_CHIPCO_CLKSRC_XTALOS:
158 case SSB_CHIPCO_CLKSRC_PCI:
159 tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL);
160 divisor = (tmp >> 16) + 1;
165 tmp = chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL);
166 divisor = (tmp >> 16) + 1;
171 case SSB_CHIPCO_CLKSRC_LOPWROS:
177 case SSB_CHIPCO_CLKSRC_XTALOS:
183 case SSB_CHIPCO_CLKSRC_PCI:
195 static void chipco_powercontrol_init(struct ssb_chipcommon *cc)
197 struct ssb_bus *bus = cc->dev->bus;
199 if (bus->chip_id == 0x4321) {
200 if (bus->chip_rev == 0)
201 chipco_write32(cc, SSB_CHIPCO_CHIPCTL, 0x3A4);
202 else if (bus->chip_rev == 1)
203 chipco_write32(cc, SSB_CHIPCO_CHIPCTL, 0xA4);
206 if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL))
209 if (cc->dev->id.revision >= 10) {
210 /* Set Idle Power clock rate to 1Mhz */
211 chipco_write32(cc, SSB_CHIPCO_SYSCLKCTL,
212 (chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL) &
213 0x0000FFFF) | 0x00040000);
217 maxfreq = chipco_pctl_clockfreqlimit(cc, 1);
218 chipco_write32(cc, SSB_CHIPCO_PLLONDELAY,
219 (maxfreq * 150 + 999999) / 1000000);
220 chipco_write32(cc, SSB_CHIPCO_FREFSELDELAY,
221 (maxfreq * 15 + 999999) / 1000000);
225 static void calc_fast_powerup_delay(struct ssb_chipcommon *cc)
227 struct ssb_bus *bus = cc->dev->bus;
232 if (bus->bustype != SSB_BUSTYPE_PCI)
234 if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL))
237 minfreq = chipco_pctl_clockfreqlimit(cc, 0);
238 pll_on_delay = chipco_read32(cc, SSB_CHIPCO_PLLONDELAY);
239 tmp = (((pll_on_delay + 2) * 1000000) + (minfreq - 1)) / minfreq;
240 SSB_WARN_ON(tmp & ~0xFFFF);
242 cc->fast_pwrup_delay = tmp;
245 void ssb_chipcommon_init(struct ssb_chipcommon *cc)
248 return; /* We don't have a ChipCommon */
249 chipco_powercontrol_init(cc);
250 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_FAST);
251 calc_fast_powerup_delay(cc);
254 void ssb_chipco_suspend(struct ssb_chipcommon *cc, pm_message_t state)
258 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW);
261 void ssb_chipco_resume(struct ssb_chipcommon *cc)
265 chipco_powercontrol_init(cc);
266 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_FAST);
269 /* Get the processor clock */
270 void ssb_chipco_get_clockcpu(struct ssb_chipcommon *cc,
271 u32 *plltype, u32 *n, u32 *m)
273 *n = chipco_read32(cc, SSB_CHIPCO_CLOCK_N);
274 *plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT);
280 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_MIPS);
283 /* 5350 uses m2 to control mips */
284 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_M2);
287 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_SB);
292 /* Get the bus clock */
293 void ssb_chipco_get_clockcontrol(struct ssb_chipcommon *cc,
294 u32 *plltype, u32 *n, u32 *m)
296 *n = chipco_read32(cc, SSB_CHIPCO_CLOCK_N);
297 *plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT);
299 case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */
300 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_MIPS);
302 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
303 if (cc->dev->bus->chip_id != 0x5365) {
304 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_M2);
309 *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_SB);
313 void ssb_chipco_timing_init(struct ssb_chipcommon *cc,
316 struct ssb_device *dev = cc->dev;
317 struct ssb_bus *bus = dev->bus;
320 /* set register for external IO to control LED. */
321 chipco_write32(cc, SSB_CHIPCO_PROG_CFG, 0x11);
322 tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT; /* Waitcount-3 = 10ns */
323 tmp |= DIV_ROUND_UP(40, ns) << SSB_PROG_WCNT_1_SHIFT; /* Waitcount-1 = 40ns */
324 tmp |= DIV_ROUND_UP(240, ns); /* Waitcount-0 = 240ns */
325 chipco_write32(cc, SSB_CHIPCO_PROG_WAITCNT, tmp); /* 0x01020a0c for a 100Mhz clock */
327 /* Set timing for the flash */
328 tmp = DIV_ROUND_UP(10, ns) << SSB_FLASH_WCNT_3_SHIFT; /* Waitcount-3 = 10nS */
329 tmp |= DIV_ROUND_UP(10, ns) << SSB_FLASH_WCNT_1_SHIFT; /* Waitcount-1 = 10nS */
330 tmp |= DIV_ROUND_UP(120, ns); /* Waitcount-0 = 120nS */
331 if ((bus->chip_id == 0x5365) ||
332 (dev->id.revision < 9))
333 chipco_write32(cc, SSB_CHIPCO_FLASH_WAITCNT, tmp);
334 if ((bus->chip_id == 0x5365) ||
335 (dev->id.revision < 9) ||
336 ((bus->chip_id == 0x5350) && (bus->chip_rev == 0)))
337 chipco_write32(cc, SSB_CHIPCO_PCMCIA_MEMWAIT, tmp);
339 if (bus->chip_id == 0x5350) {
341 tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT; /* Waitcount-3 = 10ns */
342 tmp |= DIV_ROUND_UP(20, ns) << SSB_PROG_WCNT_2_SHIFT; /* Waitcount-2 = 20ns */
343 tmp |= DIV_ROUND_UP(100, ns) << SSB_PROG_WCNT_1_SHIFT; /* Waitcount-1 = 100ns */
344 tmp |= DIV_ROUND_UP(120, ns); /* Waitcount-0 = 120ns */
345 chipco_write32(cc, SSB_CHIPCO_PROG_WAITCNT, tmp); /* 0x01020a0c for a 100Mhz clock */
349 /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
350 void ssb_chipco_watchdog_timer_set(struct ssb_chipcommon *cc, u32 ticks)
353 chipco_write32(cc, SSB_CHIPCO_WATCHDOG, ticks);
356 u32 ssb_chipco_gpio_in(struct ssb_chipcommon *cc, u32 mask)
358 return chipco_read32(cc, SSB_CHIPCO_GPIOIN) & mask;
360 EXPORT_SYMBOL(ssb_chipco_gpio_in);
362 u32 ssb_chipco_gpio_out(struct ssb_chipcommon *cc, u32 mask, u32 value)
364 return chipco_write32_masked(cc, SSB_CHIPCO_GPIOOUT, mask, value);
366 EXPORT_SYMBOL(ssb_chipco_gpio_out);
368 u32 ssb_chipco_gpio_outen(struct ssb_chipcommon *cc, u32 mask, u32 value)
370 return chipco_write32_masked(cc, SSB_CHIPCO_GPIOOUTEN, mask, value);
372 EXPORT_SYMBOL(ssb_chipco_gpio_outen);
374 u32 ssb_chipco_gpio_control(struct ssb_chipcommon *cc, u32 mask, u32 value)
376 return chipco_write32_masked(cc, SSB_CHIPCO_GPIOCTL, mask, value);
378 EXPORT_SYMBOL(ssb_chipco_gpio_control);
380 u32 ssb_chipco_gpio_intmask(struct ssb_chipcommon *cc, u32 mask, u32 value)
382 return chipco_write32_masked(cc, SSB_CHIPCO_GPIOIRQ, mask, value);
384 EXPORT_SYMBOL(ssb_chipco_gpio_intmask);
386 u32 ssb_chipco_gpio_polarity(struct ssb_chipcommon *cc, u32 mask, u32 value)
388 return chipco_write32_masked(cc, SSB_CHIPCO_GPIOPOL, mask, value);
390 EXPORT_SYMBOL(ssb_chipco_gpio_polarity);
392 #ifdef CONFIG_SSB_SERIAL
393 int ssb_chipco_serial_init(struct ssb_chipcommon *cc,
394 struct ssb_serial_port *ports)
396 struct ssb_bus *bus = cc->dev->bus;
402 unsigned int ccrev = cc->dev->id.revision;
404 plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT);
405 irq = ssb_mips_irq(cc->dev);
407 if (plltype == SSB_PLLTYPE_1) {
409 baud_base = ssb_calc_clock_rate(plltype,
410 chipco_read32(cc, SSB_CHIPCO_CLOCK_N),
411 chipco_read32(cc, SSB_CHIPCO_CLOCK_M2));
415 /* BCM5354 uses constant 25MHz clock */
416 baud_base = 25000000;
418 /* Set the override bit so we don't divide it */
419 chipco_write32(cc, SSB_CHIPCO_CORECTL,
420 chipco_read32(cc, SSB_CHIPCO_CORECTL)
421 | SSB_CHIPCO_CORECTL_UARTCLK0);
422 } else if ((ccrev >= 11) && (ccrev != 15)) {
423 /* Fixed ALP clock */
424 baud_base = 20000000;
425 if (cc->capabilities & SSB_CHIPCO_CAP_PMU) {
426 /* FIXME: baud_base is different for devices with a PMU */
431 /* Turn off UART clock before switching clocksource. */
432 chipco_write32(cc, SSB_CHIPCO_CORECTL,
433 chipco_read32(cc, SSB_CHIPCO_CORECTL)
434 & ~SSB_CHIPCO_CORECTL_UARTCLKEN);
436 /* Set the override bit so we don't divide it */
437 chipco_write32(cc, SSB_CHIPCO_CORECTL,
438 chipco_read32(cc, SSB_CHIPCO_CORECTL)
439 | SSB_CHIPCO_CORECTL_UARTCLK0);
441 /* Re-enable the UART clock. */
442 chipco_write32(cc, SSB_CHIPCO_CORECTL,
443 chipco_read32(cc, SSB_CHIPCO_CORECTL)
444 | SSB_CHIPCO_CORECTL_UARTCLKEN);
446 } else if (ccrev >= 3) {
447 /* Internal backplane clock */
448 baud_base = ssb_clockspeed(bus);
449 div = chipco_read32(cc, SSB_CHIPCO_CLKDIV)
450 & SSB_CHIPCO_CLKDIV_UART;
452 /* Fixed internal backplane clock */
453 baud_base = 88000000;
457 /* Clock source depends on strapping if UartClkOverride is unset */
459 !(chipco_read32(cc, SSB_CHIPCO_CORECTL) & SSB_CHIPCO_CORECTL_UARTCLK0)) {
460 if ((cc->capabilities & SSB_CHIPCO_CAP_UARTCLK) ==
461 SSB_CHIPCO_CAP_UARTCLK_INT) {
462 /* Internal divided backplane clock */
465 /* Assume external clock of 1.8432 MHz */
471 /* Determine the registers of the UARTs */
472 n = (cc->capabilities & SSB_CHIPCO_CAP_NRUART);
473 for (i = 0; i < n; i++) {
474 void __iomem *cc_mmio;
475 void __iomem *uart_regs;
477 cc_mmio = cc->dev->bus->mmio + (cc->dev->core_index * SSB_CORE_SIZE);
478 uart_regs = cc_mmio + SSB_CHIPCO_UART0_DATA;
479 /* Offset changed at after rev 0 */
481 uart_regs += (i * 8);
483 uart_regs += (i * 256);
486 ports[i].regs = uart_regs;
488 ports[i].baud_base = baud_base;
489 ports[i].reg_shift = 0;
494 #endif /* CONFIG_SSB_SERIAL */