2 * Sonics Silicon Backplane
3 * Broadcom ChipCommon core driver
5 * Copyright 2005, Broadcom Corporation
6 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
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/export.h>
14 #include <linux/pci.h>
16 #include "ssb_private.h"
22 SSB_CHIPCO_CLKSRC_PCI
,
23 /* Crystal slow clock oscillator */
24 SSB_CHIPCO_CLKSRC_XTALOS
,
25 /* Low power oscillator */
26 SSB_CHIPCO_CLKSRC_LOPWROS
,
30 static inline u32
chipco_write32_masked(struct ssb_chipcommon
*cc
, u16 offset
,
34 value
|= chipco_read32(cc
, offset
) & ~mask
;
35 chipco_write32(cc
, offset
, value
);
40 void ssb_chipco_set_clockmode(struct ssb_chipcommon
*cc
,
41 enum ssb_clkmode mode
)
43 struct ssb_device
*ccdev
= cc
->dev
;
51 /* We support SLOW only on 6..9 */
52 if (ccdev
->id
.revision
>= 10 && mode
== SSB_CLKMODE_SLOW
)
53 mode
= SSB_CLKMODE_DYNAMIC
;
55 if (cc
->capabilities
& SSB_CHIPCO_CAP_PMU
)
56 return; /* PMU controls clockmode, separated function needed */
57 SSB_WARN_ON(ccdev
->id
.revision
>= 20);
59 /* chipcommon cores prior to rev6 don't support dynamic clock control */
60 if (ccdev
->id
.revision
< 6)
63 /* ChipCommon cores rev10+ need testing */
64 if (ccdev
->id
.revision
>= 10)
67 if (!(cc
->capabilities
& SSB_CHIPCO_CAP_PCTL
))
71 case SSB_CLKMODE_SLOW
: /* For revs 6..9 only */
72 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
73 tmp
|= SSB_CHIPCO_SLOWCLKCTL_FSLOW
;
74 chipco_write32(cc
, SSB_CHIPCO_SLOWCLKCTL
, tmp
);
76 case SSB_CLKMODE_FAST
:
77 if (ccdev
->id
.revision
< 10) {
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 chipco_write32(cc
, SSB_CHIPCO_SYSCLKCTL
,
85 (chipco_read32(cc
, SSB_CHIPCO_SYSCLKCTL
) |
86 SSB_CHIPCO_SYSCLKCTL_FORCEHT
));
87 /* udelay(150); TODO: not available in early init */
90 case SSB_CLKMODE_DYNAMIC
:
91 if (ccdev
->id
.revision
< 10) {
92 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
93 tmp
&= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW
;
94 tmp
&= ~SSB_CHIPCO_SLOWCLKCTL_IPLL
;
95 tmp
&= ~SSB_CHIPCO_SLOWCLKCTL_ENXTAL
;
96 if ((tmp
& SSB_CHIPCO_SLOWCLKCTL_SRC
) !=
97 SSB_CHIPCO_SLOWCLKCTL_SRC_XTAL
)
98 tmp
|= SSB_CHIPCO_SLOWCLKCTL_ENXTAL
;
99 chipco_write32(cc
, SSB_CHIPCO_SLOWCLKCTL
, tmp
);
101 /* For dynamic control, we have to release our xtal_pu
103 if (tmp
& SSB_CHIPCO_SLOWCLKCTL_ENXTAL
)
104 ssb_pci_xtal(bus
, SSB_GPIO_XTAL
, 0);
106 chipco_write32(cc
, SSB_CHIPCO_SYSCLKCTL
,
107 (chipco_read32(cc
, SSB_CHIPCO_SYSCLKCTL
) &
108 ~SSB_CHIPCO_SYSCLKCTL_FORCEHT
));
116 /* Get the Slow Clock Source */
117 static enum ssb_clksrc
chipco_pctl_get_slowclksrc(struct ssb_chipcommon
*cc
)
119 struct ssb_bus
*bus
= cc
->dev
->bus
;
120 u32
uninitialized_var(tmp
);
122 if (cc
->dev
->id
.revision
< 6) {
123 if (bus
->bustype
== SSB_BUSTYPE_SSB
||
124 bus
->bustype
== SSB_BUSTYPE_PCMCIA
)
125 return SSB_CHIPCO_CLKSRC_XTALOS
;
126 if (bus
->bustype
== SSB_BUSTYPE_PCI
) {
127 pci_read_config_dword(bus
->host_pci
, SSB_GPIO_OUT
, &tmp
);
129 return SSB_CHIPCO_CLKSRC_PCI
;
130 return SSB_CHIPCO_CLKSRC_XTALOS
;
133 if (cc
->dev
->id
.revision
< 10) {
134 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
137 return SSB_CHIPCO_CLKSRC_LOPWROS
;
139 return SSB_CHIPCO_CLKSRC_XTALOS
;
141 return SSB_CHIPCO_CLKSRC_PCI
;
144 return SSB_CHIPCO_CLKSRC_XTALOS
;
147 /* Get maximum or minimum (depending on get_max flag) slowclock frequency. */
148 static int chipco_pctl_clockfreqlimit(struct ssb_chipcommon
*cc
, int get_max
)
150 int uninitialized_var(limit
);
151 enum ssb_clksrc clocksrc
;
155 clocksrc
= chipco_pctl_get_slowclksrc(cc
);
156 if (cc
->dev
->id
.revision
< 6) {
158 case SSB_CHIPCO_CLKSRC_PCI
:
161 case SSB_CHIPCO_CLKSRC_XTALOS
:
167 } else if (cc
->dev
->id
.revision
< 10) {
169 case SSB_CHIPCO_CLKSRC_LOPWROS
:
171 case SSB_CHIPCO_CLKSRC_XTALOS
:
172 case SSB_CHIPCO_CLKSRC_PCI
:
173 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
174 divisor
= (tmp
>> 16) + 1;
179 tmp
= chipco_read32(cc
, SSB_CHIPCO_SYSCLKCTL
);
180 divisor
= (tmp
>> 16) + 1;
185 case SSB_CHIPCO_CLKSRC_LOPWROS
:
191 case SSB_CHIPCO_CLKSRC_XTALOS
:
197 case SSB_CHIPCO_CLKSRC_PCI
:
209 static void chipco_powercontrol_init(struct ssb_chipcommon
*cc
)
211 struct ssb_bus
*bus
= cc
->dev
->bus
;
213 if (bus
->chip_id
== 0x4321) {
214 if (bus
->chip_rev
== 0)
215 chipco_write32(cc
, SSB_CHIPCO_CHIPCTL
, 0x3A4);
216 else if (bus
->chip_rev
== 1)
217 chipco_write32(cc
, SSB_CHIPCO_CHIPCTL
, 0xA4);
220 if (!(cc
->capabilities
& SSB_CHIPCO_CAP_PCTL
))
223 if (cc
->dev
->id
.revision
>= 10) {
224 /* Set Idle Power clock rate to 1Mhz */
225 chipco_write32(cc
, SSB_CHIPCO_SYSCLKCTL
,
226 (chipco_read32(cc
, SSB_CHIPCO_SYSCLKCTL
) &
227 0x0000FFFF) | 0x00040000);
231 maxfreq
= chipco_pctl_clockfreqlimit(cc
, 1);
232 chipco_write32(cc
, SSB_CHIPCO_PLLONDELAY
,
233 (maxfreq
* 150 + 999999) / 1000000);
234 chipco_write32(cc
, SSB_CHIPCO_FREFSELDELAY
,
235 (maxfreq
* 15 + 999999) / 1000000);
239 /* http://bcm-v4.sipsolutions.net/802.11/PmuFastPwrupDelay */
240 static u16
pmu_fast_powerup_delay(struct ssb_chipcommon
*cc
)
242 struct ssb_bus
*bus
= cc
->dev
->bus
;
244 switch (bus
->chip_id
) {
256 /* http://bcm-v4.sipsolutions.net/802.11/ClkctlFastPwrupDelay */
257 static void calc_fast_powerup_delay(struct ssb_chipcommon
*cc
)
259 struct ssb_bus
*bus
= cc
->dev
->bus
;
264 if (bus
->bustype
!= SSB_BUSTYPE_PCI
)
267 if (cc
->capabilities
& SSB_CHIPCO_CAP_PMU
) {
268 cc
->fast_pwrup_delay
= pmu_fast_powerup_delay(cc
);
272 if (!(cc
->capabilities
& SSB_CHIPCO_CAP_PCTL
))
275 minfreq
= chipco_pctl_clockfreqlimit(cc
, 0);
276 pll_on_delay
= chipco_read32(cc
, SSB_CHIPCO_PLLONDELAY
);
277 tmp
= (((pll_on_delay
+ 2) * 1000000) + (minfreq
- 1)) / minfreq
;
278 SSB_WARN_ON(tmp
& ~0xFFFF);
280 cc
->fast_pwrup_delay
= tmp
;
283 void ssb_chipcommon_init(struct ssb_chipcommon
*cc
)
286 return; /* We don't have a ChipCommon */
287 if (cc
->dev
->id
.revision
>= 11)
288 cc
->status
= chipco_read32(cc
, SSB_CHIPCO_CHIPSTAT
);
289 ssb_dprintk(KERN_INFO PFX
"chipcommon status is 0x%x\n", cc
->status
);
291 if (cc
->dev
->id
.revision
>= 20) {
292 chipco_write32(cc
, SSB_CHIPCO_GPIOPULLUP
, 0);
293 chipco_write32(cc
, SSB_CHIPCO_GPIOPULLDOWN
, 0);
297 chipco_powercontrol_init(cc
);
298 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_FAST
);
299 calc_fast_powerup_delay(cc
);
302 void ssb_chipco_suspend(struct ssb_chipcommon
*cc
)
306 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_SLOW
);
309 void ssb_chipco_resume(struct ssb_chipcommon
*cc
)
313 chipco_powercontrol_init(cc
);
314 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_FAST
);
317 /* Get the processor clock */
318 void ssb_chipco_get_clockcpu(struct ssb_chipcommon
*cc
,
319 u32
*plltype
, u32
*n
, u32
*m
)
321 *n
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_N
);
322 *plltype
= (cc
->capabilities
& SSB_CHIPCO_CAP_PLLT
);
328 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_MIPS
);
331 /* 5350 uses m2 to control mips */
332 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_M2
);
335 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_SB
);
340 /* Get the bus clock */
341 void ssb_chipco_get_clockcontrol(struct ssb_chipcommon
*cc
,
342 u32
*plltype
, u32
*n
, u32
*m
)
344 *n
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_N
);
345 *plltype
= (cc
->capabilities
& SSB_CHIPCO_CAP_PLLT
);
347 case SSB_PLLTYPE_6
: /* 100/200 or 120/240 only */
348 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_MIPS
);
350 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
351 if (cc
->dev
->bus
->chip_id
!= 0x5365) {
352 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_M2
);
357 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_SB
);
361 void ssb_chipco_timing_init(struct ssb_chipcommon
*cc
,
364 struct ssb_device
*dev
= cc
->dev
;
365 struct ssb_bus
*bus
= dev
->bus
;
368 /* set register for external IO to control LED. */
369 chipco_write32(cc
, SSB_CHIPCO_PROG_CFG
, 0x11);
370 tmp
= DIV_ROUND_UP(10, ns
) << SSB_PROG_WCNT_3_SHIFT
; /* Waitcount-3 = 10ns */
371 tmp
|= DIV_ROUND_UP(40, ns
) << SSB_PROG_WCNT_1_SHIFT
; /* Waitcount-1 = 40ns */
372 tmp
|= DIV_ROUND_UP(240, ns
); /* Waitcount-0 = 240ns */
373 chipco_write32(cc
, SSB_CHIPCO_PROG_WAITCNT
, tmp
); /* 0x01020a0c for a 100Mhz clock */
375 /* Set timing for the flash */
376 tmp
= DIV_ROUND_UP(10, ns
) << SSB_FLASH_WCNT_3_SHIFT
; /* Waitcount-3 = 10nS */
377 tmp
|= DIV_ROUND_UP(10, ns
) << SSB_FLASH_WCNT_1_SHIFT
; /* Waitcount-1 = 10nS */
378 tmp
|= DIV_ROUND_UP(120, ns
); /* Waitcount-0 = 120nS */
379 if ((bus
->chip_id
== 0x5365) ||
380 (dev
->id
.revision
< 9))
381 chipco_write32(cc
, SSB_CHIPCO_FLASH_WAITCNT
, tmp
);
382 if ((bus
->chip_id
== 0x5365) ||
383 (dev
->id
.revision
< 9) ||
384 ((bus
->chip_id
== 0x5350) && (bus
->chip_rev
== 0)))
385 chipco_write32(cc
, SSB_CHIPCO_PCMCIA_MEMWAIT
, tmp
);
387 if (bus
->chip_id
== 0x5350) {
389 tmp
= DIV_ROUND_UP(10, ns
) << SSB_PROG_WCNT_3_SHIFT
; /* Waitcount-3 = 10ns */
390 tmp
|= DIV_ROUND_UP(20, ns
) << SSB_PROG_WCNT_2_SHIFT
; /* Waitcount-2 = 20ns */
391 tmp
|= DIV_ROUND_UP(100, ns
) << SSB_PROG_WCNT_1_SHIFT
; /* Waitcount-1 = 100ns */
392 tmp
|= DIV_ROUND_UP(120, ns
); /* Waitcount-0 = 120ns */
393 chipco_write32(cc
, SSB_CHIPCO_PROG_WAITCNT
, tmp
); /* 0x01020a0c for a 100Mhz clock */
397 /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
398 void ssb_chipco_watchdog_timer_set(struct ssb_chipcommon
*cc
, u32 ticks
)
401 chipco_write32(cc
, SSB_CHIPCO_WATCHDOG
, ticks
);
404 void ssb_chipco_irq_mask(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
406 chipco_write32_masked(cc
, SSB_CHIPCO_IRQMASK
, mask
, value
);
409 u32
ssb_chipco_irq_status(struct ssb_chipcommon
*cc
, u32 mask
)
411 return chipco_read32(cc
, SSB_CHIPCO_IRQSTAT
) & mask
;
414 u32
ssb_chipco_gpio_in(struct ssb_chipcommon
*cc
, u32 mask
)
416 return chipco_read32(cc
, SSB_CHIPCO_GPIOIN
) & mask
;
419 u32
ssb_chipco_gpio_out(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
421 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOOUT
, mask
, value
);
424 u32
ssb_chipco_gpio_outen(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
426 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOOUTEN
, mask
, value
);
429 u32
ssb_chipco_gpio_control(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
431 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOCTL
, mask
, value
);
433 EXPORT_SYMBOL(ssb_chipco_gpio_control
);
435 u32
ssb_chipco_gpio_intmask(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
437 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOIRQ
, mask
, value
);
440 u32
ssb_chipco_gpio_polarity(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
442 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOPOL
, mask
, value
);
445 #ifdef CONFIG_SSB_SERIAL
446 int ssb_chipco_serial_init(struct ssb_chipcommon
*cc
,
447 struct ssb_serial_port
*ports
)
449 struct ssb_bus
*bus
= cc
->dev
->bus
;
455 unsigned int ccrev
= cc
->dev
->id
.revision
;
457 plltype
= (cc
->capabilities
& SSB_CHIPCO_CAP_PLLT
);
458 irq
= ssb_mips_irq(cc
->dev
);
460 if (plltype
== SSB_PLLTYPE_1
) {
462 baud_base
= ssb_calc_clock_rate(plltype
,
463 chipco_read32(cc
, SSB_CHIPCO_CLOCK_N
),
464 chipco_read32(cc
, SSB_CHIPCO_CLOCK_M2
));
468 /* BCM5354 uses constant 25MHz clock */
469 baud_base
= 25000000;
471 /* Set the override bit so we don't divide it */
472 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
473 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
474 | SSB_CHIPCO_CORECTL_UARTCLK0
);
475 } else if ((ccrev
>= 11) && (ccrev
!= 15)) {
476 /* Fixed ALP clock */
477 baud_base
= 20000000;
478 if (cc
->capabilities
& SSB_CHIPCO_CAP_PMU
) {
479 /* FIXME: baud_base is different for devices with a PMU */
484 /* Turn off UART clock before switching clocksource. */
485 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
486 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
487 & ~SSB_CHIPCO_CORECTL_UARTCLKEN
);
489 /* Set the override bit so we don't divide it */
490 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
491 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
492 | SSB_CHIPCO_CORECTL_UARTCLK0
);
494 /* Re-enable the UART clock. */
495 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
496 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
497 | SSB_CHIPCO_CORECTL_UARTCLKEN
);
499 } else if (ccrev
>= 3) {
500 /* Internal backplane clock */
501 baud_base
= ssb_clockspeed(bus
);
502 div
= chipco_read32(cc
, SSB_CHIPCO_CLKDIV
)
503 & SSB_CHIPCO_CLKDIV_UART
;
505 /* Fixed internal backplane clock */
506 baud_base
= 88000000;
510 /* Clock source depends on strapping if UartClkOverride is unset */
512 !(chipco_read32(cc
, SSB_CHIPCO_CORECTL
) & SSB_CHIPCO_CORECTL_UARTCLK0
)) {
513 if ((cc
->capabilities
& SSB_CHIPCO_CAP_UARTCLK
) ==
514 SSB_CHIPCO_CAP_UARTCLK_INT
) {
515 /* Internal divided backplane clock */
518 /* Assume external clock of 1.8432 MHz */
524 /* Determine the registers of the UARTs */
525 n
= (cc
->capabilities
& SSB_CHIPCO_CAP_NRUART
);
526 for (i
= 0; i
< n
; i
++) {
527 void __iomem
*cc_mmio
;
528 void __iomem
*uart_regs
;
530 cc_mmio
= cc
->dev
->bus
->mmio
+ (cc
->dev
->core_index
* SSB_CORE_SIZE
);
531 uart_regs
= cc_mmio
+ SSB_CHIPCO_UART0_DATA
;
532 /* Offset changed at after rev 0 */
534 uart_regs
+= (i
* 8);
536 uart_regs
+= (i
* 256);
539 ports
[i
].regs
= uart_regs
;
541 ports
[i
].baud_base
= baud_base
;
542 ports
[i
].reg_shift
= 0;
547 #endif /* CONFIG_SSB_SERIAL */