2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2003 Atheros Communications, Inc., All Rights Reserved.
7 * Copyright (C) 2006 FON Technology, SL.
8 * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
9 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
13 * Platform devices for Atheros SoCs
16 #include <linux/autoconf.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/mtd/physmap.h>
22 #include <linux/platform_device.h>
23 #include <linux/kernel.h>
24 #include <linux/reboot.h>
25 #include <asm/bootinfo.h>
26 #include <asm/reboot.h>
33 #define AR531X_IRQ_WLAN0_INTRS MIPS_CPU_IRQ_BASE+2 /* C0_CAUSE: 0x0400 */
34 #define AR531X_IRQ_ENET0_INTRS MIPS_CPU_IRQ_BASE+3 /* C0_CAUSE: 0x0800 */
35 #define AR531X_IRQ_ENET1_INTRS MIPS_CPU_IRQ_BASE+4 /* C0_CAUSE: 0x1000 */
36 #define AR531X_IRQ_WLAN1_INTRS MIPS_CPU_IRQ_BASE+5 /* C0_CAUSE: 0x2000 */
37 #define AR531X_IRQ_MISC_INTRS MIPS_CPU_IRQ_BASE+6 /* C0_CAUSE: 0x4000 */
40 static struct platform_device
*ar5312_devs
[5];
42 static struct resource ar5312_eth0_res
[] = {
44 .name
= "eth_membase",
45 .flags
= IORESOURCE_MEM
,
46 .start
= KSEG1ADDR(AR531X_ENET0
),
47 .end
= KSEG1ADDR(AR531X_ENET0
+ 0x2000),
51 .flags
= IORESOURCE_IRQ
,
52 .start
= AR531X_IRQ_ENET0_INTRS
,
53 .end
= AR531X_IRQ_ENET0_INTRS
,
58 static struct resource ar5312_eth1_res
[] = {
60 .name
= "eth_membase",
61 .flags
= IORESOURCE_MEM
,
62 .start
= KSEG1ADDR(AR531X_ENET1
),
63 .end
= KSEG1ADDR(AR531X_ENET1
+ 0x2000),
67 .flags
= IORESOURCE_IRQ
,
68 .start
= AR531X_IRQ_ENET1_INTRS
,
69 .end
= AR531X_IRQ_ENET1_INTRS
,
74 static struct ar531x_eth ar5312_eth0_data
= {
77 .reset_base
= AR531X_RESET
,
78 .reset_mac
= AR531X_RESET_ENET0
,
79 .reset_phy
= AR531X_RESET_EPHY0
,
82 static struct ar531x_eth ar5312_eth1_data
= {
85 .reset_base
= AR531X_RESET
,
86 .reset_mac
= AR531X_RESET_ENET1
,
87 .reset_phy
= AR531X_RESET_EPHY1
,
90 static struct platform_device ar5312_eth
[] = {
94 .dev
.platform_data
= &ar5312_eth0_data
,
95 .resource
= ar5312_eth0_res
,
96 .num_resources
= ARRAY_SIZE(ar5312_eth0_res
)
100 .name
= "ar531x-eth",
101 .dev
.platform_data
= &ar5312_eth1_data
,
102 .resource
= ar5312_eth1_res
,
103 .num_resources
= ARRAY_SIZE(ar5312_eth1_res
)
107 static struct platform_device ar5312_wmac
[] = {
110 .name
= "ar531x-wmac",
114 .name
= "ar531x-wmac",
119 static struct physmap_flash_data ar5312_flash_data
= {
123 static struct resource ar5312_flash_resource
= {
124 .start
= AR531X_FLASH
,
125 .end
= AR531X_FLASH
+ 0x400000 - 1,
126 .flags
= IORESOURCE_MEM
,
129 static struct platform_device ar5312_physmap_flash
= {
130 .name
= "physmap-flash",
133 .platform_data
= &ar5312_flash_data
,
136 .resource
= &ar5312_flash_resource
,
141 * NB: This mapping size is larger than the actual flash size,
142 * but this shouldn't be a problem here, because the flash
143 * will simply be mapped multiple times.
145 static char __init
*ar5312_flash_limit(void)
148 /* Configure flash bank 0 */
152 (0x01 << FLASHCTL_IDCY_S
) |
153 (0x07 << FLASHCTL_WST1_S
) |
154 (0x07 << FLASHCTL_WST2_S
) |
155 (sysRegRead(AR531X_FLASHCTL0
) & FLASHCTL_MW
);
157 sysRegWrite(AR531X_FLASHCTL0
, ctl
);
159 /* Disable other flash banks */
160 sysRegWrite(AR531X_FLASHCTL1
,
161 sysRegRead(AR531X_FLASHCTL1
) & ~(FLASHCTL_E
| FLASHCTL_AC
));
163 sysRegWrite(AR531X_FLASHCTL2
,
164 sysRegRead(AR531X_FLASHCTL2
) & ~(FLASHCTL_E
| FLASHCTL_AC
));
166 return (char *) KSEG1ADDR(AR531X_FLASH
+ 0x400000);
169 static struct ar531x_config __init
*init_wmac(int unit
)
171 struct ar531x_config
*config
;
173 config
= (struct ar531x_config
*) kzalloc(sizeof(struct ar531x_config
), GFP_KERNEL
);
174 config
->board
= board_config
;
175 config
->radio
= radio_config
;
177 config
->tag
= (u_int16_t
) ((sysRegRead(AR531X_REV
) >> AR531X_REV_WMAC_MIN_S
) & AR531X_REV_CHIP
);
182 int __init
ar5312_init_devices(void)
187 if (mips_machtype
!= MACH_ATHEROS_AR5312
)
190 ar531x_find_config(ar5312_flash_limit());
191 ar5312_eth0_data
.board_config
= board_config
;
192 ar5312_eth1_data
.board_config
= board_config
;
193 ar5312_devs
[dev
++] = &ar5312_physmap_flash
;
194 ar5312_devs
[dev
++] = &ar5312_eth
[0];
195 ar5312_devs
[dev
++] = &ar5312_eth
[1];
197 radio
= radio_config
+ AR531X_RADIO_MASK_OFF
;
198 if (*((u32
*) radio
) & AR531X_RADIO0_MASK
) {
199 ar5312_wmac
[0].dev
.platform_data
= init_wmac(0);
200 ar5312_devs
[dev
++] = &ar5312_wmac
[0];
202 if (*((u32
*) radio
) & AR531X_RADIO1_MASK
) {
203 ar5312_wmac
[1].dev
.platform_data
= init_wmac(1);
204 ar5312_devs
[dev
++] = &ar5312_wmac
[1];
207 return platform_add_devices(ar5312_devs
, dev
);
212 * Called when an interrupt is received, this function
213 * determines exactly which interrupt it was, and it
214 * invokes the appropriate handler.
216 * Implicitly, we also define interrupt priority by
217 * choosing which to dispatch first.
219 asmlinkage
void ar5312_irq_dispatch(void)
221 int pending
= read_c0_status() & read_c0_cause();
223 if (pending
& CAUSEF_IP2
)
224 do_IRQ(AR531X_IRQ_WLAN0_INTRS
);
225 else if (pending
& CAUSEF_IP3
)
226 do_IRQ(AR531X_IRQ_ENET0_INTRS
);
227 else if (pending
& CAUSEF_IP4
)
228 do_IRQ(AR531X_IRQ_ENET1_INTRS
);
229 else if (pending
& CAUSEF_IP5
)
230 do_IRQ(AR531X_IRQ_WLAN1_INTRS
);
231 else if (pending
& CAUSEF_IP6
) {
232 unsigned int ar531x_misc_intrs
= sysRegRead(AR531X_ISR
) & sysRegRead(AR531X_IMR
);
234 if (ar531x_misc_intrs
& AR531X_ISR_TIMER
) {
235 do_IRQ(AR531X_MISC_IRQ_TIMER
);
236 (void)sysRegRead(AR531X_TIMER
);
237 } else if (ar531x_misc_intrs
& AR531X_ISR_AHBPROC
)
238 do_IRQ(AR531X_MISC_IRQ_AHB_PROC
);
239 else if ((ar531x_misc_intrs
& AR531X_ISR_UART0
))
240 do_IRQ(AR531X_MISC_IRQ_UART0
);
241 else if (ar531x_misc_intrs
& AR531X_ISR_WD
)
242 do_IRQ(AR531X_MISC_IRQ_WATCHDOG
);
244 do_IRQ(AR531X_MISC_IRQ_NONE
);
245 } else if (pending
& CAUSEF_IP7
) {
246 do_IRQ(AR531X_IRQ_CPU_CLOCK
);
249 do_IRQ(AR531X_IRQ_NONE
);
252 static void ar5312_halt(void)
257 static void ar5312_power_off(void)
263 static void ar5312_restart(char *command
)
265 /* reset the system */
266 for(;;) sysRegWrite(AR531X_RESET
, AR531X_RESET_SYSTEM
);
271 * This table is indexed by bits 5..4 of the CLOCKCTL1 register
272 * to determine the predevisor value.
274 static int __initdata CLOCKCTL1_PREDIVIDE_TABLE
[4] = {
282 static unsigned int __init
ar5312_cpu_frequency(void)
285 unsigned int predivide_mask
, predivide_shift
;
286 unsigned int multiplier_mask
, multiplier_shift
;
287 unsigned int clockCtl1
, preDivideSelect
, preDivisor
, multiplier
;
288 unsigned int doubler_mask
;
289 unsigned int wisoc_revision
;
291 /* Trust the bootrom's idea of cpu frequency. */
292 if ((result
= sysRegRead(AR5312_SCRATCH
)))
295 wisoc_revision
= (sysRegRead(AR531X_REV
) & AR531X_REV_MAJ
) >> AR531X_REV_MAJ_S
;
296 if (wisoc_revision
== AR531X_REV_MAJ_AR2313
) {
297 predivide_mask
= AR2313_CLOCKCTL1_PREDIVIDE_MASK
;
298 predivide_shift
= AR2313_CLOCKCTL1_PREDIVIDE_SHIFT
;
299 multiplier_mask
= AR2313_CLOCKCTL1_MULTIPLIER_MASK
;
300 multiplier_shift
= AR2313_CLOCKCTL1_MULTIPLIER_SHIFT
;
301 doubler_mask
= AR2313_CLOCKCTL1_DOUBLER_MASK
;
302 } else { /* AR5312 and AR2312 */
303 predivide_mask
= AR5312_CLOCKCTL1_PREDIVIDE_MASK
;
304 predivide_shift
= AR5312_CLOCKCTL1_PREDIVIDE_SHIFT
;
305 multiplier_mask
= AR5312_CLOCKCTL1_MULTIPLIER_MASK
;
306 multiplier_shift
= AR5312_CLOCKCTL1_MULTIPLIER_SHIFT
;
307 doubler_mask
= AR5312_CLOCKCTL1_DOUBLER_MASK
;
311 * Clocking is derived from a fixed 40MHz input clock.
313 * cpuFreq = InputClock * MULT (where MULT is PLL multiplier)
314 * sysFreq = cpuFreq / 4 (used for APB clock, serial,
315 * flash, Timer, Watchdog Timer)
317 * cntFreq = cpuFreq / 2 (use for CPU count/compare)
319 * So, for example, with a PLL multiplier of 5, we have
325 * We compute the CPU frequency, based on PLL settings.
328 clockCtl1
= sysRegRead(AR5312_CLOCKCTL1
);
329 preDivideSelect
= (clockCtl1
& predivide_mask
) >> predivide_shift
;
330 preDivisor
= CLOCKCTL1_PREDIVIDE_TABLE
[preDivideSelect
];
331 multiplier
= (clockCtl1
& multiplier_mask
) >> multiplier_shift
;
333 if (clockCtl1
& doubler_mask
) {
334 multiplier
= multiplier
<< 1;
336 return (40000000 / preDivisor
) * multiplier
;
339 static inline int ar5312_sys_frequency(void)
341 return ar5312_cpu_frequency() / 4;
344 static void __init
ar5312_time_init(void)
346 mips_hpt_frequency
= ar5312_cpu_frequency() / 2;
350 /* Enable the specified AR531X_MISC_IRQ interrupt */
352 ar5312_misc_intr_enable(unsigned int irq
)
356 imr
= sysRegRead(AR531X_IMR
);
357 imr
|= (1 << (irq
- AR531X_MISC_IRQ_BASE
- 1));
358 sysRegWrite(AR531X_IMR
, imr
);
359 sysRegRead(AR531X_IMR
); /* flush write buffer */
362 /* Disable the specified AR531X_MISC_IRQ interrupt */
364 ar5312_misc_intr_disable(unsigned int irq
)
368 imr
= sysRegRead(AR531X_IMR
);
369 imr
&= ~(1 << (irq
- AR531X_MISC_IRQ_BASE
- 1));
370 sysRegWrite(AR531X_IMR
, imr
);
371 sysRegRead(AR531X_IMR
); /* flush write buffer */
374 /* Turn on the specified AR531X_MISC_IRQ interrupt */
376 ar5312_misc_intr_startup(unsigned int irq
)
378 ar5312_misc_intr_enable(irq
);
382 /* Turn off the specified AR531X_MISC_IRQ interrupt */
384 ar5312_misc_intr_shutdown(unsigned int irq
)
386 ar5312_misc_intr_disable(irq
);
390 ar5312_misc_intr_ack(unsigned int irq
)
392 ar5312_misc_intr_disable(irq
);
396 ar5312_misc_intr_end(unsigned int irq
)
398 if (!(irq_desc
[irq
].status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))
399 ar5312_misc_intr_enable(irq
);
402 static struct irq_chip ar5312_misc_intr_controller
= {
403 .typename
= "AR5312 misc",
404 .startup
= ar5312_misc_intr_startup
,
405 .shutdown
= ar5312_misc_intr_shutdown
,
406 .enable
= ar5312_misc_intr_enable
,
407 .disable
= ar5312_misc_intr_disable
,
408 .ack
= ar5312_misc_intr_ack
,
409 .end
= ar5312_misc_intr_end
,
412 static irqreturn_t
ar5312_ahb_proc_handler(int cpl
, void *dev_id
)
414 u32 proc1
= sysRegRead(AR531X_PROC1
);
415 u32 procAddr
= sysRegRead(AR531X_PROCADDR
); /* clears error state */
416 u32 dma1
= sysRegRead(AR531X_DMA1
);
417 u32 dmaAddr
= sysRegRead(AR531X_DMAADDR
); /* clears error state */
419 printk("AHB interrupt: PROCADDR=0x%8.8x PROC1=0x%8.8x DMAADDR=0x%8.8x DMA1=0x%8.8x\n",
420 procAddr
, proc1
, dmaAddr
, dma1
);
422 machine_restart("AHB error"); /* Catastrophic failure */
427 static struct irqaction ar5312_ahb_proc_interrupt
= {
428 .handler
= ar5312_ahb_proc_handler
,
429 .flags
= SA_INTERRUPT
,
430 .name
= "ar5312_ahb_proc_interrupt",
434 static struct irqaction cascade
= {
435 .handler
= no_action
,
436 .flags
= SA_INTERRUPT
,
440 void __init
ar5312_misc_intr_init(int irq_base
)
444 for (i
= irq_base
; i
< irq_base
+ AR531X_MISC_IRQ_COUNT
; i
++) {
445 irq_desc
[i
].status
= IRQ_DISABLED
;
446 irq_desc
[i
].action
= NULL
;
447 irq_desc
[i
].depth
= 1;
448 irq_desc
[i
].chip
= &ar5312_misc_intr_controller
;
450 setup_irq(AR531X_MISC_IRQ_AHB_PROC
, &ar5312_ahb_proc_interrupt
);
451 setup_irq(AR531X_IRQ_MISC_INTRS
, &cascade
);
455 void __init
ar5312_plat_setup(void)
457 /* Clear any lingering AHB errors */
458 sysRegRead(AR531X_PROCADDR
);
459 sysRegRead(AR531X_DMAADDR
);
460 sysRegWrite(AR531X_WD_CTRL
, AR531X_WD_CTRL_IGNORE_EXPIRATION
);
462 board_time_init
= ar5312_time_init
;
464 _machine_restart
= ar5312_restart
;
465 _machine_halt
= ar5312_halt
;
466 pm_power_off
= ar5312_power_off
;
468 serial_setup(KSEG1ADDR(AR531X_UART0
), ar5312_sys_frequency());
471 arch_initcall(ar5312_init_devices
);