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
)
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 void ssb_chipco_irq_mask(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
358 chipco_write32_masked(cc
, SSB_CHIPCO_IRQMASK
, mask
, value
);
361 u32
ssb_chipco_irq_status(struct ssb_chipcommon
*cc
, u32 mask
)
363 return chipco_read32(cc
, SSB_CHIPCO_IRQSTAT
) & mask
;
366 u32
ssb_chipco_gpio_in(struct ssb_chipcommon
*cc
, u32 mask
)
368 return chipco_read32(cc
, SSB_CHIPCO_GPIOIN
) & mask
;
371 u32
ssb_chipco_gpio_out(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
373 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOOUT
, mask
, value
);
376 u32
ssb_chipco_gpio_outen(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
378 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOOUTEN
, mask
, value
);
381 u32
ssb_chipco_gpio_control(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
383 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOCTL
, mask
, value
);
386 u32
ssb_chipco_gpio_intmask(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
388 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOIRQ
, mask
, value
);
391 u32
ssb_chipco_gpio_polarity(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
393 return chipco_write32_masked(cc
, SSB_CHIPCO_GPIOPOL
, mask
, value
);
396 #ifdef CONFIG_SSB_SERIAL
397 int ssb_chipco_serial_init(struct ssb_chipcommon
*cc
,
398 struct ssb_serial_port
*ports
)
400 struct ssb_bus
*bus
= cc
->dev
->bus
;
406 unsigned int ccrev
= cc
->dev
->id
.revision
;
408 plltype
= (cc
->capabilities
& SSB_CHIPCO_CAP_PLLT
);
409 irq
= ssb_mips_irq(cc
->dev
);
411 if (plltype
== SSB_PLLTYPE_1
) {
413 baud_base
= ssb_calc_clock_rate(plltype
,
414 chipco_read32(cc
, SSB_CHIPCO_CLOCK_N
),
415 chipco_read32(cc
, SSB_CHIPCO_CLOCK_M2
));
419 /* BCM5354 uses constant 25MHz clock */
420 baud_base
= 25000000;
422 /* Set the override bit so we don't divide it */
423 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
424 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
425 | SSB_CHIPCO_CORECTL_UARTCLK0
);
426 } else if ((ccrev
>= 11) && (ccrev
!= 15)) {
427 /* Fixed ALP clock */
428 baud_base
= 20000000;
429 if (cc
->capabilities
& SSB_CHIPCO_CAP_PMU
) {
430 /* FIXME: baud_base is different for devices with a PMU */
435 /* Turn off UART clock before switching clocksource. */
436 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
437 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
438 & ~SSB_CHIPCO_CORECTL_UARTCLKEN
);
440 /* Set the override bit so we don't divide it */
441 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
442 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
443 | SSB_CHIPCO_CORECTL_UARTCLK0
);
445 /* Re-enable the UART clock. */
446 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
447 chipco_read32(cc
, SSB_CHIPCO_CORECTL
)
448 | SSB_CHIPCO_CORECTL_UARTCLKEN
);
450 } else if (ccrev
>= 3) {
451 /* Internal backplane clock */
452 baud_base
= ssb_clockspeed(bus
);
453 div
= chipco_read32(cc
, SSB_CHIPCO_CLKDIV
)
454 & SSB_CHIPCO_CLKDIV_UART
;
456 /* Fixed internal backplane clock */
457 baud_base
= 88000000;
461 /* Clock source depends on strapping if UartClkOverride is unset */
463 !(chipco_read32(cc
, SSB_CHIPCO_CORECTL
) & SSB_CHIPCO_CORECTL_UARTCLK0
)) {
464 if ((cc
->capabilities
& SSB_CHIPCO_CAP_UARTCLK
) ==
465 SSB_CHIPCO_CAP_UARTCLK_INT
) {
466 /* Internal divided backplane clock */
469 /* Assume external clock of 1.8432 MHz */
475 /* Determine the registers of the UARTs */
476 n
= (cc
->capabilities
& SSB_CHIPCO_CAP_NRUART
);
477 for (i
= 0; i
< n
; i
++) {
478 void __iomem
*cc_mmio
;
479 void __iomem
*uart_regs
;
481 cc_mmio
= cc
->dev
->bus
->mmio
+ (cc
->dev
->core_index
* SSB_CORE_SIZE
);
482 uart_regs
= cc_mmio
+ SSB_CHIPCO_UART0_DATA
;
483 /* Offset changed at after rev 0 */
485 uart_regs
+= (i
* 8);
487 uart_regs
+= (i
* 256);
490 ports
[i
].regs
= uart_regs
;
492 ports
[i
].baud_base
= baud_base
;
493 ports
[i
].reg_shift
= 0;
498 #endif /* CONFIG_SSB_SERIAL */