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 void chipco_write32_masked(struct ssb_chipcommon
*cc
, u16 offset
,
46 value
|= chipco_read32(cc
, offset
) & ~mask
;
47 chipco_write32(cc
, offset
, value
);
50 void ssb_chipco_set_clockmode(struct ssb_chipcommon
*cc
,
51 enum ssb_clkmode mode
)
53 struct ssb_device
*ccdev
= cc
->dev
;
60 /* chipcommon cores prior to rev6 don't support dynamic clock control */
61 if (ccdev
->id
.revision
< 6)
63 /* chipcommon cores rev10 are a whole new ball game */
64 if (ccdev
->id
.revision
>= 10)
66 if (!(cc
->capabilities
& SSB_CHIPCO_CAP_PCTL
))
70 case SSB_CLKMODE_SLOW
:
71 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
72 tmp
|= SSB_CHIPCO_SLOWCLKCTL_FSLOW
;
73 chipco_write32(cc
, SSB_CHIPCO_SLOWCLKCTL
, tmp
);
75 case SSB_CLKMODE_FAST
:
76 ssb_pci_xtal(bus
, SSB_GPIO_XTAL
, 1); /* Force crystal on */
77 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
78 tmp
&= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW
;
79 tmp
|= SSB_CHIPCO_SLOWCLKCTL_IPLL
;
80 chipco_write32(cc
, SSB_CHIPCO_SLOWCLKCTL
, tmp
);
82 case SSB_CLKMODE_DYNAMIC
:
83 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
84 tmp
&= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW
;
85 tmp
&= ~SSB_CHIPCO_SLOWCLKCTL_IPLL
;
86 tmp
&= ~SSB_CHIPCO_SLOWCLKCTL_ENXTAL
;
87 if ((tmp
& SSB_CHIPCO_SLOWCLKCTL_SRC
) != SSB_CHIPCO_SLOWCLKCTL_SRC_XTAL
)
88 tmp
|= SSB_CHIPCO_SLOWCLKCTL_ENXTAL
;
89 chipco_write32(cc
, SSB_CHIPCO_SLOWCLKCTL
, tmp
);
91 /* for dynamic control, we have to release our xtal_pu "force on" */
92 if (tmp
& SSB_CHIPCO_SLOWCLKCTL_ENXTAL
)
93 ssb_pci_xtal(bus
, SSB_GPIO_XTAL
, 0);
100 /* Get the Slow Clock Source */
101 static enum ssb_clksrc
chipco_pctl_get_slowclksrc(struct ssb_chipcommon
*cc
)
103 struct ssb_bus
*bus
= cc
->dev
->bus
;
104 u32
uninitialized_var(tmp
);
106 if (cc
->dev
->id
.revision
< 6) {
107 if (bus
->bustype
== SSB_BUSTYPE_SSB
||
108 bus
->bustype
== SSB_BUSTYPE_PCMCIA
)
109 return SSB_CHIPCO_CLKSRC_XTALOS
;
110 if (bus
->bustype
== SSB_BUSTYPE_PCI
) {
111 pci_read_config_dword(bus
->host_pci
, SSB_GPIO_OUT
, &tmp
);
113 return SSB_CHIPCO_CLKSRC_PCI
;
114 return SSB_CHIPCO_CLKSRC_XTALOS
;
117 if (cc
->dev
->id
.revision
< 10) {
118 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
121 return SSB_CHIPCO_CLKSRC_LOPWROS
;
123 return SSB_CHIPCO_CLKSRC_XTALOS
;
125 return SSB_CHIPCO_CLKSRC_PCI
;
128 return SSB_CHIPCO_CLKSRC_XTALOS
;
131 /* Get maximum or minimum (depending on get_max flag) slowclock frequency. */
132 static int chipco_pctl_clockfreqlimit(struct ssb_chipcommon
*cc
, int get_max
)
134 int uninitialized_var(limit
);
135 enum ssb_clksrc clocksrc
;
139 clocksrc
= chipco_pctl_get_slowclksrc(cc
);
140 if (cc
->dev
->id
.revision
< 6) {
142 case SSB_CHIPCO_CLKSRC_PCI
:
145 case SSB_CHIPCO_CLKSRC_XTALOS
:
151 } else if (cc
->dev
->id
.revision
< 10) {
153 case SSB_CHIPCO_CLKSRC_LOPWROS
:
155 case SSB_CHIPCO_CLKSRC_XTALOS
:
156 case SSB_CHIPCO_CLKSRC_PCI
:
157 tmp
= chipco_read32(cc
, SSB_CHIPCO_SLOWCLKCTL
);
158 divisor
= (tmp
>> 16) + 1;
163 tmp
= chipco_read32(cc
, SSB_CHIPCO_SYSCLKCTL
);
164 divisor
= (tmp
>> 16) + 1;
169 case SSB_CHIPCO_CLKSRC_LOPWROS
:
175 case SSB_CHIPCO_CLKSRC_XTALOS
:
181 case SSB_CHIPCO_CLKSRC_PCI
:
193 static void chipco_powercontrol_init(struct ssb_chipcommon
*cc
)
195 struct ssb_bus
*bus
= cc
->dev
->bus
;
197 if (bus
->chip_id
== 0x4321) {
198 if (bus
->chip_rev
== 0)
199 chipco_write32(cc
, SSB_CHIPCO_CHIPCTL
, 0x3A4);
200 else if (bus
->chip_rev
== 1)
201 chipco_write32(cc
, SSB_CHIPCO_CHIPCTL
, 0xA4);
204 if (!(cc
->capabilities
& SSB_CHIPCO_CAP_PCTL
))
207 if (cc
->dev
->id
.revision
>= 10) {
208 /* Set Idle Power clock rate to 1Mhz */
209 chipco_write32(cc
, SSB_CHIPCO_SYSCLKCTL
,
210 (chipco_read32(cc
, SSB_CHIPCO_SYSCLKCTL
) &
211 0x0000FFFF) | 0x00040000);
215 maxfreq
= chipco_pctl_clockfreqlimit(cc
, 1);
216 chipco_write32(cc
, SSB_CHIPCO_PLLONDELAY
,
217 (maxfreq
* 150 + 999999) / 1000000);
218 chipco_write32(cc
, SSB_CHIPCO_FREFSELDELAY
,
219 (maxfreq
* 15 + 999999) / 1000000);
223 static void calc_fast_powerup_delay(struct ssb_chipcommon
*cc
)
225 struct ssb_bus
*bus
= cc
->dev
->bus
;
230 if (bus
->bustype
!= SSB_BUSTYPE_PCI
)
232 if (!(cc
->capabilities
& SSB_CHIPCO_CAP_PCTL
))
235 minfreq
= chipco_pctl_clockfreqlimit(cc
, 0);
236 pll_on_delay
= chipco_read32(cc
, SSB_CHIPCO_PLLONDELAY
);
237 tmp
= (((pll_on_delay
+ 2) * 1000000) + (minfreq
- 1)) / minfreq
;
238 SSB_WARN_ON(tmp
& ~0xFFFF);
240 cc
->fast_pwrup_delay
= tmp
;
243 void ssb_chipcommon_init(struct ssb_chipcommon
*cc
)
246 return; /* We don't have a ChipCommon */
247 chipco_powercontrol_init(cc
);
248 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_FAST
);
249 calc_fast_powerup_delay(cc
);
252 void ssb_chipco_suspend(struct ssb_chipcommon
*cc
, pm_message_t state
)
256 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_SLOW
);
259 void ssb_chipco_resume(struct ssb_chipcommon
*cc
)
263 chipco_powercontrol_init(cc
);
264 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_FAST
);
267 /* Get the processor clock */
268 void ssb_chipco_get_clockcpu(struct ssb_chipcommon
*cc
,
269 u32
*plltype
, u32
*n
, u32
*m
)
271 *n
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_N
);
272 *plltype
= (cc
->capabilities
& SSB_CHIPCO_CAP_PLLT
);
278 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_MIPS
);
281 /* 5350 uses m2 to control mips */
282 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_M2
);
285 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_SB
);
290 /* Get the bus clock */
291 void ssb_chipco_get_clockcontrol(struct ssb_chipcommon
*cc
,
292 u32
*plltype
, u32
*n
, u32
*m
)
294 *n
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_N
);
295 *plltype
= (cc
->capabilities
& SSB_CHIPCO_CAP_PLLT
);
297 case SSB_PLLTYPE_6
: /* 100/200 or 120/240 only */
298 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_MIPS
);
300 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
301 if (cc
->dev
->bus
->chip_id
!= 0x5365) {
302 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_M2
);
307 *m
= chipco_read32(cc
, SSB_CHIPCO_CLOCK_SB
);
311 void ssb_chipco_timing_init(struct ssb_chipcommon
*cc
,
314 struct ssb_device
*dev
= cc
->dev
;
315 struct ssb_bus
*bus
= dev
->bus
;
318 /* set register for external IO to control LED. */
319 chipco_write32(cc
, SSB_CHIPCO_PROG_CFG
, 0x11);
320 tmp
= DIV_ROUND_UP(10, ns
) << SSB_PROG_WCNT_3_SHIFT
; /* Waitcount-3 = 10ns */
321 tmp
|= DIV_ROUND_UP(40, ns
) << SSB_PROG_WCNT_1_SHIFT
; /* Waitcount-1 = 40ns */
322 tmp
|= DIV_ROUND_UP(240, ns
); /* Waitcount-0 = 240ns */
323 chipco_write32(cc
, SSB_CHIPCO_PROG_WAITCNT
, tmp
); /* 0x01020a0c for a 100Mhz clock */
325 /* Set timing for the flash */
326 tmp
= DIV_ROUND_UP(10, ns
) << SSB_FLASH_WCNT_3_SHIFT
; /* Waitcount-3 = 10nS */
327 tmp
|= DIV_ROUND_UP(10, ns
) << SSB_FLASH_WCNT_1_SHIFT
; /* Waitcount-1 = 10nS */
328 tmp
|= DIV_ROUND_UP(120, ns
); /* Waitcount-0 = 120nS */
329 if ((bus
->chip_id
== 0x5365) ||
330 (dev
->id
.revision
< 9))
331 chipco_write32(cc
, SSB_CHIPCO_FLASH_WAITCNT
, tmp
);
332 if ((bus
->chip_id
== 0x5365) ||
333 (dev
->id
.revision
< 9) ||
334 ((bus
->chip_id
== 0x5350) && (bus
->chip_rev
== 0)))
335 chipco_write32(cc
, SSB_CHIPCO_PCMCIA_MEMWAIT
, tmp
);
337 if (bus
->chip_id
== 0x5350) {
339 tmp
= DIV_ROUND_UP(10, ns
) << SSB_PROG_WCNT_3_SHIFT
; /* Waitcount-3 = 10ns */
340 tmp
|= DIV_ROUND_UP(20, ns
) << SSB_PROG_WCNT_2_SHIFT
; /* Waitcount-2 = 20ns */
341 tmp
|= DIV_ROUND_UP(100, ns
) << SSB_PROG_WCNT_1_SHIFT
; /* Waitcount-1 = 100ns */
342 tmp
|= DIV_ROUND_UP(120, ns
); /* Waitcount-0 = 120ns */
343 chipco_write32(cc
, SSB_CHIPCO_PROG_WAITCNT
, tmp
); /* 0x01020a0c for a 100Mhz clock */
347 /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */
348 void ssb_chipco_watchdog_timer_set(struct ssb_chipcommon
*cc
, u32 ticks
)
351 chipco_write32(cc
, SSB_CHIPCO_WATCHDOG
, ticks
);
354 u32
ssb_chipco_gpio_in(struct ssb_chipcommon
*cc
, u32 mask
)
356 return chipco_read32(cc
, SSB_CHIPCO_GPIOIN
) & mask
;
359 void ssb_chipco_gpio_out(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
361 chipco_write32_masked(cc
, SSB_CHIPCO_GPIOOUT
, mask
, value
);
364 void ssb_chipco_gpio_outen(struct ssb_chipcommon
*cc
, u32 mask
, u32 value
)
366 chipco_write32_masked(cc
, SSB_CHIPCO_GPIOOUTEN
, mask
, value
);
369 #ifdef CONFIG_SSB_SERIAL
370 int ssb_chipco_serial_init(struct ssb_chipcommon
*cc
,
371 struct ssb_serial_port
*ports
)
373 struct ssb_bus
*bus
= cc
->dev
->bus
;
380 plltype
= (cc
->capabilities
& SSB_CHIPCO_CAP_PLLT
);
381 irq
= ssb_mips_irq(cc
->dev
);
383 if (plltype
== SSB_PLLTYPE_1
) {
385 baud_base
= ssb_calc_clock_rate(plltype
,
386 chipco_read32(cc
, SSB_CHIPCO_CLOCK_N
),
387 chipco_read32(cc
, SSB_CHIPCO_CLOCK_M2
));
390 if (cc
->dev
->id
.revision
>= 11) {
391 /* Fixed ALP clock */
392 baud_base
= 20000000;
394 /* Set the override bit so we don't divide it */
395 chipco_write32(cc
, SSB_CHIPCO_CORECTL
,
396 SSB_CHIPCO_CORECTL_UARTCLK0
);
397 } else if (cc
->dev
->id
.revision
>= 3) {
398 /* Internal backplane clock */
399 baud_base
= ssb_clockspeed(bus
);
400 div
= chipco_read32(cc
, SSB_CHIPCO_CLKDIV
)
401 & SSB_CHIPCO_CLKDIV_UART
;
403 /* Fixed internal backplane clock */
404 baud_base
= 88000000;
408 /* Clock source depends on strapping if UartClkOverride is unset */
409 if ((cc
->dev
->id
.revision
> 0) &&
410 !(chipco_read32(cc
, SSB_CHIPCO_CORECTL
) & SSB_CHIPCO_CORECTL_UARTCLK0
)) {
411 if ((cc
->capabilities
& SSB_CHIPCO_CAP_UARTCLK
) ==
412 SSB_CHIPCO_CAP_UARTCLK_INT
) {
413 /* Internal divided backplane clock */
416 /* Assume external clock of 1.8432 MHz */
422 /* Determine the registers of the UARTs */
423 n
= (cc
->capabilities
& SSB_CHIPCO_CAP_NRUART
);
424 for (i
= 0; i
< n
; i
++) {
425 void __iomem
*cc_mmio
;
426 void __iomem
*uart_regs
;
428 cc_mmio
= cc
->dev
->bus
->mmio
+ (cc
->dev
->core_index
* SSB_CORE_SIZE
);
429 uart_regs
= cc_mmio
+ SSB_CHIPCO_UART0_DATA
;
430 /* Offset changed at after rev 0 */
431 if (cc
->dev
->id
.revision
== 0)
432 uart_regs
+= (i
* 8);
434 uart_regs
+= (i
* 256);
437 ports
[i
].regs
= uart_regs
;
439 ports
[i
].baud_base
= baud_base
;
440 ports
[i
].reg_shift
= 0;
445 #endif /* CONFIG_SSB_SERIAL */