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/platform_device.h>
22 #include <linux/kernel.h>
23 #include <linux/reboot.h>
24 #include <asm/bootinfo.h>
25 #include <asm/reboot.h>
31 #define AR531X_IRQ_MISC_INTRS MIPS_CPU_IRQ_BASE+2 /* C0_CAUSE: 0x0400 */
32 #define AR531X_IRQ_WLAN0_INTRS MIPS_CPU_IRQ_BASE+3 /* C0_CAUSE: 0x0800 */
33 #define AR531X_IRQ_ENET0_INTRS MIPS_CPU_IRQ_BASE+4 /* C0_CAUSE: 0x1000 */
34 #define AR531X_IRQ_LCBUS_PCI MIPS_CPU_IRQ_BASE+5 /* C0_CAUSE: 0x2000 */
35 #define AR531X_IRQ_WLAN0_POLL MIPS_CPU_IRQ_BASE+6 /* C0_CAUSE: 0x4000 */
37 static struct resource ar5315_eth_res
[] = {
39 .name
= "eth_membase",
40 .flags
= IORESOURCE_MEM
,
41 .start
= AR5315_ENET0
,
42 .end
= AR5315_ENET0
+ 0x2000,
46 .flags
= IORESOURCE_IRQ
,
47 .start
= AR531X_IRQ_ENET0_INTRS
,
48 .end
= AR531X_IRQ_ENET0_INTRS
,
52 static struct ar531x_eth ar5315_eth_data
= {
55 .reset_base
= AR5315_RESET
,
56 .reset_mac
= AR5315_RESET_ENET0
,
57 .reset_phy
= AR5315_RESET_EPHY0
,
60 static struct platform_device ar5315_eth
= {
63 .dev
.platform_data
= &ar5315_eth_data
,
64 .resource
= ar5315_eth_res
,
65 .num_resources
= ARRAY_SIZE(ar5315_eth_res
)
68 static struct platform_device ar5315_wmac
= {
70 .name
= "ar531x-wmac",
71 /* FIXME: add resources */
74 static struct resource ar5315_spiflash_res
[] = {
77 .flags
= IORESOURCE_MEM
,
78 .start
= KSEG1ADDR(AR5315_SPI_READ
),
79 .end
= KSEG1ADDR(AR5315_SPI_READ
) + 0x800000,
83 .flags
= IORESOURCE_MEM
,
89 static struct platform_device ar5315_spiflash
= {
92 .resource
= ar5315_spiflash_res
,
93 .num_resources
= ARRAY_SIZE(ar5315_spiflash_res
)
96 static __initdata
struct platform_device
*ar5315_devs
[4];
100 static void *flash_regs
;
102 static inline __u32
spiflash_regread32(int reg
)
104 volatile __u32
*data
= (__u32
*)(flash_regs
+ reg
);
109 static inline void spiflash_regwrite32(int reg
, __u32 data
)
111 volatile __u32
*addr
= (__u32
*)(flash_regs
+ reg
);
116 #define SPI_FLASH_CTL 0x00
117 #define SPI_FLASH_OPCODE 0x04
118 #define SPI_FLASH_DATA 0x08
120 static __u8
spiflash_probe(void)
125 reg
= spiflash_regread32(SPI_FLASH_CTL
);
126 } while (reg
& SPI_CTL_BUSY
);
128 spiflash_regwrite32(SPI_FLASH_OPCODE
, 0xab);
130 reg
= (reg
& ~SPI_CTL_TX_RX_CNT_MASK
) | 4 |
131 (1 << 4) | SPI_CTL_START
;
133 spiflash_regwrite32(SPI_FLASH_CTL
, reg
);
136 reg
= spiflash_regread32(SPI_FLASH_CTL
);
137 } while (reg
& SPI_CTL_BUSY
);
139 reg
= (__u32
) spiflash_regread32(SPI_FLASH_DATA
);
146 #define STM_8MBIT_SIGNATURE 0x13
147 #define STM_16MBIT_SIGNATURE 0x14
148 #define STM_32MBIT_SIGNATURE 0x15
149 #define STM_64MBIT_SIGNATURE 0x16
152 static char __init
*ar5315_flash_limit(void)
157 /* probe the flash chip size */
158 flash_regs
= ioremap_nocache(ar5315_spiflash_res
[1].start
, ar5315_spiflash_res
[1].end
- ar5315_spiflash_res
[1].start
);
159 sig
= spiflash_probe();
163 case STM_8MBIT_SIGNATURE
:
164 flash_size
= 0x00100000;
166 case STM_16MBIT_SIGNATURE
:
167 flash_size
= 0x00200000;
169 case STM_32MBIT_SIGNATURE
:
170 flash_size
= 0x00400000;
172 case STM_64MBIT_SIGNATURE
:
173 flash_size
= 0x00800000;
177 ar5315_spiflash_res
[0].end
= ar5315_spiflash_res
[0].start
+ flash_size
;
178 return (char *) ar5315_spiflash_res
[0].end
;
181 int __init
ar5315_init_devices(void)
183 struct ar531x_config
*config
;
186 if (mips_machtype
!= MACH_ATHEROS_AR5315
)
189 ar531x_find_config(ar5315_flash_limit());
191 config
= (struct ar531x_config
*) kzalloc(sizeof(struct ar531x_config
), GFP_KERNEL
);
192 config
->board
= board_config
;
193 config
->radio
= radio_config
;
195 config
->tag
= (u_int16_t
) (sysRegRead(AR5315_SREV
) & REV_CHIP
);
197 ar5315_eth_data
.board_config
= board_config
;
198 ar5315_wmac
.dev
.platform_data
= config
;
200 ar5315_devs
[dev
++] = &ar5315_eth
;
201 ar5315_devs
[dev
++] = &ar5315_wmac
;
202 ar5315_devs
[dev
++] = &ar5315_spiflash
;
204 return platform_add_devices(ar5315_devs
, dev
);
209 * Called when an interrupt is received, this function
210 * determines exactly which interrupt it was, and it
211 * invokes the appropriate handler.
213 * Implicitly, we also define interrupt priority by
214 * choosing which to dispatch first.
216 asmlinkage
void ar5315_irq_dispatch(void)
218 int pending
= read_c0_status() & read_c0_cause();
220 if (pending
& CAUSEF_IP3
)
221 do_IRQ(AR531X_IRQ_WLAN0_INTRS
);
222 else if (pending
& CAUSEF_IP4
)
223 do_IRQ(AR531X_IRQ_ENET0_INTRS
);
224 else if (pending
& CAUSEF_IP2
) {
225 unsigned int ar531x_misc_intrs
= sysRegRead(AR5315_ISR
) & sysRegRead(AR5315_IMR
);
227 if (ar531x_misc_intrs
& AR5315_ISR_TIMER
)
228 do_IRQ(AR531X_MISC_IRQ_TIMER
);
229 else if (ar531x_misc_intrs
& AR5315_ISR_AHB
)
230 do_IRQ(AR531X_MISC_IRQ_AHB_PROC
);
231 else if (ar531x_misc_intrs
& AR5315_ISR_GPIO
) {
232 sysRegWrite(AR5315_ISR
, sysRegRead(AR5315_IMR
) | ~AR5315_ISR_GPIO
);
233 } else if (ar531x_misc_intrs
& AR5315_ISR_UART0
)
234 do_IRQ(AR531X_MISC_IRQ_UART0
);
235 else if (ar531x_misc_intrs
& AR5315_ISR_WD
)
236 do_IRQ(AR531X_MISC_IRQ_WATCHDOG
);
238 do_IRQ(AR531X_MISC_IRQ_NONE
);
239 } else if (pending
& CAUSEF_IP7
)
240 do_IRQ(AR531X_IRQ_CPU_CLOCK
);
242 do_IRQ(AR531X_IRQ_NONE
);
245 static void ar5315_halt(void)
250 static void ar5315_power_off(void)
256 static void ar5315_restart(char *command
)
261 /* reset the system */
262 sysRegWrite(AR5315_COLD_RESET
,AR5317_RESET_SYSTEM
);
265 * Cold reset does not work on the AR2315/6, use the GPIO reset bits a workaround.
268 reg
= sysRegRead(AR5315_GPIO_DO
);
269 reg
&= ~(1 << AR5315_RESET_GPIO
);
270 sysRegWrite(AR5315_GPIO_DO
, reg
);
271 (void)sysRegRead(AR5315_GPIO_DO
); /* flush write to hardware */
277 * This table is indexed by bits 5..4 of the CLOCKCTL1 register
278 * to determine the predevisor value.
280 static int __initdata CLOCKCTL1_PREDIVIDE_TABLE
[4] = {
287 static int __initdata PLLC_DIVIDE_TABLE
[5] = {
295 static unsigned int __init
296 ar5315_sys_clk(unsigned int clockCtl
)
298 unsigned int pllcCtrl
,cpuDiv
;
299 unsigned int pllcOut
,refdiv
,fdiv
,divby2
;
302 pllcCtrl
= sysRegRead(AR5315_PLLC_CTL
);
303 refdiv
= (pllcCtrl
& PLLC_REF_DIV_M
) >> PLLC_REF_DIV_S
;
304 refdiv
= CLOCKCTL1_PREDIVIDE_TABLE
[refdiv
];
305 fdiv
= (pllcCtrl
& PLLC_FDBACK_DIV_M
) >> PLLC_FDBACK_DIV_S
;
306 divby2
= (pllcCtrl
& PLLC_ADD_FDBACK_DIV_M
) >> PLLC_ADD_FDBACK_DIV_S
;
308 pllcOut
= (40000000/refdiv
)*(2*divby2
)*fdiv
;
311 /* clkm input selected */
312 switch(clockCtl
& CPUCLK_CLK_SEL_M
) {
315 clkDiv
= PLLC_DIVIDE_TABLE
[(pllcCtrl
& PLLC_CLKM_DIV_M
) >> PLLC_CLKM_DIV_S
];
318 clkDiv
= PLLC_DIVIDE_TABLE
[(pllcCtrl
& PLLC_CLKC_DIV_M
) >> PLLC_CLKC_DIV_S
];
325 cpuDiv
= (clockCtl
& CPUCLK_CLK_DIV_M
) >> CPUCLK_CLK_DIV_S
;
326 cpuDiv
= cpuDiv
* 2 ?: 1;
327 return (pllcOut
/(clkDiv
* cpuDiv
));
330 static inline unsigned int ar5315_cpu_frequency(void)
332 return ar5315_sys_clk(sysRegRead(AR5315_CPUCLK
));
335 static inline unsigned int ar5315_apb_frequency(void)
337 return ar5315_sys_clk(sysRegRead(AR5315_AMBACLK
));
340 static void __init
ar5315_time_init(void)
342 mips_hpt_frequency
= ar5315_cpu_frequency() / 2;
347 /* Enable the specified AR531X_MISC_IRQ interrupt */
349 ar5315_misc_intr_enable(unsigned int irq
)
353 imr
= sysRegRead(AR5315_IMR
);
356 case AR531X_MISC_IRQ_TIMER
:
357 imr
|= AR5315_ISR_TIMER
;
360 case AR531X_MISC_IRQ_AHB_PROC
:
361 imr
|= AR5315_ISR_AHB
;
364 case AR531X_MISC_IRQ_AHB_DMA
:
368 case AR531X_MISC_IRQ_GPIO
:
369 imr
|= AR5315_ISR_GPIO
;
372 case AR531X_MISC_IRQ_UART0
:
373 imr
|= AR5315_ISR_UART0
;
377 case AR531X_MISC_IRQ_WATCHDOG
:
378 imr
|= AR5315_ISR_WD
;
381 case AR531X_MISC_IRQ_LOCAL
:
386 sysRegWrite(AR5315_IMR
, imr
);
387 imr
=sysRegRead(AR5315_IMR
); /* flush write buffer */
388 //printk("enable Interrupt irq 0x%x imr 0x%x \n",irq,imr);
392 /* Disable the specified AR531X_MISC_IRQ interrupt */
394 ar5315_misc_intr_disable(unsigned int irq
)
398 imr
= sysRegRead(AR5315_IMR
);
401 case AR531X_MISC_IRQ_TIMER
:
402 imr
&= (~AR5315_ISR_TIMER
);
405 case AR531X_MISC_IRQ_AHB_PROC
:
406 imr
&= (~AR5315_ISR_AHB
);
409 case AR531X_MISC_IRQ_AHB_DMA
:
413 case AR531X_MISC_IRQ_GPIO
:
414 imr
&= ~AR5315_ISR_GPIO
;
417 case AR531X_MISC_IRQ_UART0
:
418 imr
&= (~AR5315_ISR_UART0
);
421 case AR531X_MISC_IRQ_WATCHDOG
:
422 imr
&= (~AR5315_ISR_WD
);
425 case AR531X_MISC_IRQ_LOCAL
:
430 sysRegWrite(AR5315_IMR
, imr
);
431 sysRegRead(AR5315_IMR
); /* flush write buffer */
434 /* Turn on the specified AR531X_MISC_IRQ interrupt */
436 ar5315_misc_intr_startup(unsigned int irq
)
438 ar5315_misc_intr_enable(irq
);
442 /* Turn off the specified AR531X_MISC_IRQ interrupt */
444 ar5315_misc_intr_shutdown(unsigned int irq
)
446 ar5315_misc_intr_disable(irq
);
450 ar5315_misc_intr_ack(unsigned int irq
)
452 ar5315_misc_intr_disable(irq
);
456 ar5315_misc_intr_end(unsigned int irq
)
458 if (!(irq_desc
[irq
].status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))
459 ar5315_misc_intr_enable(irq
);
462 static struct irq_chip ar5315_misc_intr_controller
= {
463 .typename
= "AR5315 misc",
464 .startup
= ar5315_misc_intr_startup
,
465 .shutdown
= ar5315_misc_intr_shutdown
,
466 .enable
= ar5315_misc_intr_enable
,
467 .disable
= ar5315_misc_intr_disable
,
468 .ack
= ar5315_misc_intr_ack
,
469 .end
= ar5315_misc_intr_end
,
472 static irqreturn_t
ar5315_ahb_proc_handler(int cpl
, void *dev_id
)
474 sysRegWrite(AR5315_AHB_ERR0
,AHB_ERROR_DET
);
475 sysRegRead(AR5315_AHB_ERR1
);
477 printk("AHB fatal error\n");
478 machine_restart("AHB error"); /* Catastrophic failure */
483 static struct irqaction ar5315_ahb_proc_interrupt
= {
484 .handler
= ar5315_ahb_proc_handler
,
485 .flags
= SA_INTERRUPT
,
486 .name
= "ar5315_ahb_proc_interrupt",
490 static struct irqaction cascade
= {
491 .handler
= no_action
,
492 .flags
= SA_INTERRUPT
,
496 void ar5315_misc_intr_init(int irq_base
)
500 for (i
= irq_base
; i
< irq_base
+ AR531X_MISC_IRQ_COUNT
; i
++) {
501 irq_desc
[i
].status
= IRQ_DISABLED
;
502 irq_desc
[i
].action
= NULL
;
503 irq_desc
[i
].depth
= 1;
504 irq_desc
[i
].chip
= &ar5315_misc_intr_controller
;
506 setup_irq(AR531X_MISC_IRQ_AHB_PROC
, &ar5315_ahb_proc_interrupt
);
507 setup_irq(AR531X_IRQ_MISC_INTRS
, &cascade
);
510 void __init
ar5315_plat_setup(void)
512 unsigned int config
= read_c0_config();
514 /* Clear any lingering AHB errors */
515 write_c0_config(config
& ~0x3);
516 sysRegWrite(AR5315_AHB_ERR0
,AHB_ERROR_DET
);
517 sysRegRead(AR5315_AHB_ERR1
);
518 sysRegWrite(AR5315_WDC
, WDC_IGNORE_EXPIRATION
);
520 board_time_init
= ar5315_time_init
;
522 _machine_restart
= ar5315_restart
;
523 _machine_halt
= ar5315_halt
;
524 pm_power_off
= ar5315_power_off
;
526 serial_setup(KSEG1ADDR(AR5315_UART0
), ar5315_apb_frequency());
529 arch_initcall(ar5315_init_devices
);