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 static int is_5315 = 0;
32 static struct resource ar5315_eth_res[] = {
34 .name = "eth0_membase",
35 .flags = IORESOURCE_MEM,
36 .start = AR5315_ENET0,
37 .end = AR5315_ENET0 + 0x2000,
41 .flags = IORESOURCE_IRQ,
42 .start = AR5315_IRQ_ENET0_INTRS,
43 .end = AR5315_IRQ_ENET0_INTRS,
47 static struct ar531x_eth ar5315_eth_data = {
50 .reset_base = AR5315_RESET,
51 .reset_mac = AR5315_RESET_ENET0,
52 .reset_phy = AR5315_RESET_EPHY0,
53 .phy_base = AR5315_ENET0
56 static struct platform_device ar5315_eth = {
59 .dev.platform_data = &ar5315_eth_data,
60 .resource = ar5315_eth_res,
61 .num_resources = ARRAY_SIZE(ar5315_eth_res)
64 static struct platform_device ar5315_wmac = {
66 .name = "ar531x-wmac",
67 /* FIXME: add resources */
70 static struct resource ar5315_spiflash_res[] = {
73 .flags = IORESOURCE_MEM,
74 .start = KSEG1ADDR(AR5315_SPI_READ),
75 .end = KSEG1ADDR(AR5315_SPI_READ) + 0x800000,
79 .flags = IORESOURCE_MEM,
85 static struct platform_device ar5315_spiflash = {
88 .resource = ar5315_spiflash_res,
89 .num_resources = ARRAY_SIZE(ar5315_spiflash_res)
92 static __initdata struct platform_device *ar5315_devs[4];
96 static void *flash_regs;
98 static inline __u32 spiflash_regread32(int reg)
100 volatile __u32 *data = (__u32 *)(flash_regs + reg);
105 static inline void spiflash_regwrite32(int reg, __u32 data)
107 volatile __u32 *addr = (__u32 *)(flash_regs + reg);
112 #define SPI_FLASH_CTL 0x00
113 #define SPI_FLASH_OPCODE 0x04
114 #define SPI_FLASH_DATA 0x08
116 static __u8 spiflash_probe(void)
121 reg = spiflash_regread32(SPI_FLASH_CTL);
122 } while (reg & SPI_CTL_BUSY);
124 spiflash_regwrite32(SPI_FLASH_OPCODE, 0xab);
126 reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 4 |
127 (1 << 4) | SPI_CTL_START;
129 spiflash_regwrite32(SPI_FLASH_CTL, reg);
132 reg = spiflash_regread32(SPI_FLASH_CTL);
133 } while (reg & SPI_CTL_BUSY);
135 reg = (__u32) spiflash_regread32(SPI_FLASH_DATA);
142 #define STM_8MBIT_SIGNATURE 0x13
143 #define STM_16MBIT_SIGNATURE 0x14
144 #define STM_32MBIT_SIGNATURE 0x15
145 #define STM_64MBIT_SIGNATURE 0x16
146 #define STM_128MBIT_SIGNATURE 0x17
149 static char __init *ar5315_flash_limit(void)
154 /* probe the flash chip size */
155 flash_regs = ioremap_nocache(ar5315_spiflash_res[1].start, ar5315_spiflash_res[1].end - ar5315_spiflash_res[1].start);
156 sig = spiflash_probe();
160 case STM_8MBIT_SIGNATURE:
161 flash_size = 0x00100000;
163 case STM_16MBIT_SIGNATURE:
164 flash_size = 0x00200000;
166 case STM_32MBIT_SIGNATURE:
167 flash_size = 0x00400000;
169 case STM_64MBIT_SIGNATURE:
170 flash_size = 0x00800000;
172 case STM_128MBIT_SIGNATURE:
173 flash_size = 0x01000000;
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;
184 struct ar531x_boarddata *bcfg;
191 /* Find board configuration */
192 ar531x_find_config(ar5315_flash_limit());
193 bcfg = (struct ar531x_boarddata *) board_config;
196 /* Detect the hardware based on the device ID */
197 devid = sysRegRead(AR5315_SREV) & AR5315_REV_MAJ >> AR5315_REV_MAJ_S;
200 mips_machtype = MACH_ATHEROS_AR2317;
202 /* FIXME: how can we detect AR2316? */
205 mips_machtype = MACH_ATHEROS_AR2315;
210 config = (struct ar531x_config *) kzalloc(sizeof(struct ar531x_config), GFP_KERNEL);
211 config->board = board_config;
212 config->radio = radio_config;
214 config->tag = (u_int16_t) (sysRegRead(AR5315_SREV) & AR5315_REV_CHIP);
216 ar5315_eth_data.board_config = board_config;
217 ar5315_eth_data.macaddr = bcfg->enet0Mac;
218 ar5315_wmac.dev.platform_data = config;
220 ar5315_devs[dev++] = &ar5315_eth;
221 ar5315_devs[dev++] = &ar5315_wmac;
222 ar5315_devs[dev++] = &ar5315_spiflash;
224 return platform_add_devices(ar5315_devs, dev);
229 * Called when an interrupt is received, this function
230 * determines exactly which interrupt it was, and it
231 * invokes the appropriate handler.
233 * Implicitly, we also define interrupt priority by
234 * choosing which to dispatch first.
236 asmlinkage void ar5315_irq_dispatch(void)
238 int pending = read_c0_status() & read_c0_cause();
240 if (pending & CAUSEF_IP3)
241 do_IRQ(AR5315_IRQ_WLAN0_INTRS);
242 else if (pending & CAUSEF_IP4)
243 do_IRQ(AR5315_IRQ_ENET0_INTRS);
244 else if (pending & CAUSEF_IP2) {
245 unsigned int ar531x_misc_intrs = sysRegRead(AR5315_ISR) & sysRegRead(AR5315_IMR);
247 if (ar531x_misc_intrs & AR5315_ISR_TIMER)
248 do_IRQ(AR531X_MISC_IRQ_TIMER);
249 else if (ar531x_misc_intrs & AR5315_ISR_AHB)
250 do_IRQ(AR531X_MISC_IRQ_AHB_PROC);
251 else if (ar531x_misc_intrs & AR5315_ISR_GPIO) {
252 sysRegWrite(AR5315_ISR, sysRegRead(AR5315_IMR) | ~AR5315_ISR_GPIO);
253 } else if (ar531x_misc_intrs & AR5315_ISR_UART0)
254 do_IRQ(AR531X_MISC_IRQ_UART0);
255 else if (ar531x_misc_intrs & AR5315_ISR_WD)
256 do_IRQ(AR531X_MISC_IRQ_WATCHDOG);
258 do_IRQ(AR531X_MISC_IRQ_NONE);
259 } else if (pending & CAUSEF_IP7)
260 do_IRQ(AR531X_IRQ_CPU_CLOCK);
262 do_IRQ(AR531X_IRQ_NONE);
265 static void ar5315_halt(void)
270 static void ar5315_power_off(void)
276 static void ar5315_restart(char *command)
281 /* reset the system */
282 sysRegWrite(AR5315_COLD_RESET,AR5317_RESET_SYSTEM);
285 * Cold reset does not work on the AR2315/6, use the GPIO reset bits a workaround.
288 reg = sysRegRead(AR5315_GPIO_DO);
289 reg &= ~(1 << AR5315_RESET_GPIO);
290 sysRegWrite(AR5315_GPIO_DO, reg);
291 (void)sysRegRead(AR5315_GPIO_DO); /* flush write to hardware */
297 * This table is indexed by bits 5..4 of the CLOCKCTL1 register
298 * to determine the predevisor value.
300 static int __initdata CLOCKCTL1_PREDIVIDE_TABLE[4] = {
307 static int __initdata PLLC_DIVIDE_TABLE[5] = {
315 static unsigned int __init
316 ar5315_sys_clk(unsigned int clockCtl)
318 unsigned int pllcCtrl,cpuDiv;
319 unsigned int pllcOut,refdiv,fdiv,divby2;
322 pllcCtrl = sysRegRead(AR5315_PLLC_CTL);
323 refdiv = (pllcCtrl & PLLC_REF_DIV_M) >> PLLC_REF_DIV_S;
324 refdiv = CLOCKCTL1_PREDIVIDE_TABLE[refdiv];
325 fdiv = (pllcCtrl & PLLC_FDBACK_DIV_M) >> PLLC_FDBACK_DIV_S;
326 divby2 = (pllcCtrl & PLLC_ADD_FDBACK_DIV_M) >> PLLC_ADD_FDBACK_DIV_S;
328 pllcOut = (40000000/refdiv)*(2*divby2)*fdiv;
331 /* clkm input selected */
332 switch(clockCtl & CPUCLK_CLK_SEL_M) {
335 clkDiv = PLLC_DIVIDE_TABLE[(pllcCtrl & PLLC_CLKM_DIV_M) >> PLLC_CLKM_DIV_S];
338 clkDiv = PLLC_DIVIDE_TABLE[(pllcCtrl & PLLC_CLKC_DIV_M) >> PLLC_CLKC_DIV_S];
345 cpuDiv = (clockCtl & CPUCLK_CLK_DIV_M) >> CPUCLK_CLK_DIV_S;
346 cpuDiv = cpuDiv * 2 ?: 1;
347 return (pllcOut/(clkDiv * cpuDiv));
350 static inline unsigned int ar5315_cpu_frequency(void)
352 return ar5315_sys_clk(sysRegRead(AR5315_CPUCLK));
355 static inline unsigned int ar5315_apb_frequency(void)
357 return ar5315_sys_clk(sysRegRead(AR5315_AMBACLK));
360 static void __init ar5315_time_init(void)
362 mips_hpt_frequency = ar5315_cpu_frequency() / 2;
367 /* Enable the specified AR531X_MISC_IRQ interrupt */
369 ar5315_misc_intr_enable(unsigned int irq)
373 imr = sysRegRead(AR5315_IMR);
376 case AR531X_MISC_IRQ_TIMER:
377 imr |= AR5315_ISR_TIMER;
380 case AR531X_MISC_IRQ_AHB_PROC:
381 imr |= AR5315_ISR_AHB;
384 case AR531X_MISC_IRQ_AHB_DMA:
388 case AR531X_MISC_IRQ_GPIO:
389 imr |= AR5315_ISR_GPIO;
392 case AR531X_MISC_IRQ_UART0:
393 imr |= AR5315_ISR_UART0;
397 case AR531X_MISC_IRQ_WATCHDOG:
398 imr |= AR5315_ISR_WD;
401 case AR531X_MISC_IRQ_LOCAL:
406 sysRegWrite(AR5315_IMR, imr);
407 imr=sysRegRead(AR5315_IMR); /* flush write buffer */
410 /* Disable the specified AR531X_MISC_IRQ interrupt */
412 ar5315_misc_intr_disable(unsigned int irq)
416 imr = sysRegRead(AR5315_IMR);
419 case AR531X_MISC_IRQ_TIMER:
420 imr &= (~AR5315_ISR_TIMER);
423 case AR531X_MISC_IRQ_AHB_PROC:
424 imr &= (~AR5315_ISR_AHB);
427 case AR531X_MISC_IRQ_AHB_DMA:
431 case AR531X_MISC_IRQ_GPIO:
432 imr &= ~AR5315_ISR_GPIO;
435 case AR531X_MISC_IRQ_UART0:
436 imr &= (~AR5315_ISR_UART0);
439 case AR531X_MISC_IRQ_WATCHDOG:
440 imr &= (~AR5315_ISR_WD);
443 case AR531X_MISC_IRQ_LOCAL:
448 sysRegWrite(AR5315_IMR, imr);
449 sysRegRead(AR5315_IMR); /* flush write buffer */
452 /* Turn on the specified AR531X_MISC_IRQ interrupt */
454 ar5315_misc_intr_startup(unsigned int irq)
456 ar5315_misc_intr_enable(irq);
460 /* Turn off the specified AR531X_MISC_IRQ interrupt */
462 ar5315_misc_intr_shutdown(unsigned int irq)
464 ar5315_misc_intr_disable(irq);
468 ar5315_misc_intr_ack(unsigned int irq)
470 ar5315_misc_intr_disable(irq);
474 ar5315_misc_intr_end(unsigned int irq)
476 if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
477 ar5315_misc_intr_enable(irq);
480 static struct irq_chip ar5315_misc_intr_controller = {
481 .typename = "AR5315 misc",
482 .startup = ar5315_misc_intr_startup,
483 .shutdown = ar5315_misc_intr_shutdown,
484 .enable = ar5315_misc_intr_enable,
485 .disable = ar5315_misc_intr_disable,
486 .ack = ar5315_misc_intr_ack,
487 .end = ar5315_misc_intr_end,
490 static irqreturn_t ar5315_ahb_proc_handler(int cpl, void *dev_id)
492 sysRegWrite(AR5315_AHB_ERR0,AHB_ERROR_DET);
493 sysRegRead(AR5315_AHB_ERR1);
495 printk("AHB fatal error\n");
496 machine_restart("AHB error"); /* Catastrophic failure */
501 static struct irqaction ar5315_ahb_proc_interrupt = {
502 .handler = ar5315_ahb_proc_handler,
503 .flags = SA_INTERRUPT,
504 .name = "ar5315_ahb_proc_interrupt",
508 static struct irqaction cascade = {
509 .handler = no_action,
510 .flags = SA_INTERRUPT,
514 void ar5315_misc_intr_init(int irq_base)
518 for (i = irq_base; i < irq_base + AR531X_MISC_IRQ_COUNT; i++) {
519 irq_desc[i].status = IRQ_DISABLED;
520 irq_desc[i].action = NULL;
521 irq_desc[i].depth = 1;
522 irq_desc[i].chip = &ar5315_misc_intr_controller;
524 setup_irq(AR531X_MISC_IRQ_AHB_PROC, &ar5315_ahb_proc_interrupt);
525 setup_irq(AR5315_IRQ_MISC_INTRS, &cascade);
528 void __init ar5315_prom_init(void)
533 memcfg = sysRegRead(AR5315_MEM_CFG);
534 memsize = 1 + ((memcfg & SDRAM_DATA_WIDTH_M) >> SDRAM_DATA_WIDTH_S);
535 memsize <<= 1 + ((memcfg & SDRAM_COL_WIDTH_M) >> SDRAM_COL_WIDTH_S);
536 memsize <<= 1 + ((memcfg & SDRAM_ROW_WIDTH_M) >> SDRAM_ROW_WIDTH_S);
538 add_memory_region(0, memsize, BOOT_MEM_RAM);
540 /* Initialize it to AR2315 for now. Real detection will be done
541 * in ar5315_init_devices() */
542 mips_machtype = MACH_ATHEROS_AR2315;
545 void __init ar5315_plat_setup(void)
547 unsigned int config = read_c0_config();
549 /* Clear any lingering AHB errors */
550 write_c0_config(config & ~0x3);
551 sysRegWrite(AR5315_AHB_ERR0,AHB_ERROR_DET);
552 sysRegRead(AR5315_AHB_ERR1);
553 sysRegWrite(AR5315_WDC, WDC_IGNORE_EXPIRATION);
555 board_time_init = ar5315_time_init;
557 _machine_restart = ar5315_restart;
558 _machine_halt = ar5315_halt;
559 pm_power_off = ar5315_power_off;
561 serial_setup(KSEG1ADDR(AR5315_UART0), ar5315_apb_frequency());
564 arch_initcall(ar5315_init_devices);