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-2009 Felix Fietkau <nbd@openwrt.org>
10 * Copyright (C) 2012 Alexandros C. Couloumbis <alex@ozo.com>
14 * Platform devices for Atheros AR5312 SoCs
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/bitops.h>
20 #include <linux/irqdomain.h>
21 #include <linux/interrupt.h>
22 #include <linux/reboot.h>
23 #include <asm/bootinfo.h>
24 #include <asm/reboot.h>
27 #include <ath25_platform.h>
31 #include "ar5312_regs.h"
33 static void __iomem *ar5312_rst_base;
34 static struct irq_domain *ar5312_misc_irq_domain;
36 static inline u32 ar5312_rst_reg_read(u32 reg)
38 return __raw_readl(ar5312_rst_base + reg);
41 static inline void ar5312_rst_reg_write(u32 reg, u32 val)
43 __raw_writel(val, ar5312_rst_base + reg);
46 static inline void ar5312_rst_reg_mask(u32 reg, u32 mask, u32 val)
48 u32 ret = ar5312_rst_reg_read(reg);
52 ar5312_rst_reg_write(reg, ret);
55 static irqreturn_t ar5312_ahb_err_handler(int cpl, void *dev_id)
57 u32 proc1 = ar5312_rst_reg_read(AR5312_PROC1);
58 u32 proc_addr = ar5312_rst_reg_read(AR5312_PROCADDR); /* clears error */
59 u32 dma1 = ar5312_rst_reg_read(AR5312_DMA1);
60 u32 dma_addr = ar5312_rst_reg_read(AR5312_DMAADDR); /* clears error */
62 pr_emerg("AHB interrupt: PROCADDR=0x%8.8x PROC1=0x%8.8x DMAADDR=0x%8.8x DMA1=0x%8.8x\n",
63 proc_addr, proc1, dma_addr, dma1);
65 machine_restart("AHB error"); /* Catastrophic failure */
69 static struct irqaction ar5312_ahb_err_interrupt = {
70 .handler = ar5312_ahb_err_handler,
71 .name = "ar5312-ahb-error",
74 static void ar5312_misc_irq_handler(unsigned irq, struct irq_desc *desc)
76 u32 pending = ar5312_rst_reg_read(AR5312_ISR) &
77 ar5312_rst_reg_read(AR5312_IMR);
78 unsigned nr, misc_irq = 0;
81 struct irq_domain *domain = irq_get_handler_data(irq);
84 misc_irq = irq_find_mapping(domain, nr);
88 generic_handle_irq(misc_irq);
89 if (nr == AR5312_MISC_IRQ_TIMER)
90 ar5312_rst_reg_read(AR5312_TIMER);
96 /* Enable the specified AR5312_MISC_IRQ interrupt */
97 static void ar5312_misc_irq_unmask(struct irq_data *d)
99 ar5312_rst_reg_mask(AR5312_IMR, 0, BIT(d->hwirq));
102 /* Disable the specified AR5312_MISC_IRQ interrupt */
103 static void ar5312_misc_irq_mask(struct irq_data *d)
105 ar5312_rst_reg_mask(AR5312_IMR, BIT(d->hwirq), 0);
106 ar5312_rst_reg_read(AR5312_IMR); /* flush write buffer */
109 static struct irq_chip ar5312_misc_irq_chip = {
110 .name = "ar5312-misc",
111 .irq_unmask = ar5312_misc_irq_unmask,
112 .irq_mask = ar5312_misc_irq_mask,
115 static int ar5312_misc_irq_map(struct irq_domain *d, unsigned irq,
118 irq_set_chip_and_handler(irq, &ar5312_misc_irq_chip, handle_level_irq);
122 static struct irq_domain_ops ar5312_misc_irq_domain_ops = {
123 .map = ar5312_misc_irq_map,
126 static void ar5312_irq_dispatch(void)
128 u32 pending = read_c0_status() & read_c0_cause();
130 if (pending & CAUSEF_IP2)
131 do_IRQ(AR5312_IRQ_WLAN0);
132 else if (pending & CAUSEF_IP5)
133 do_IRQ(AR5312_IRQ_WLAN1);
134 else if (pending & CAUSEF_IP6)
135 do_IRQ(AR5312_IRQ_MISC);
136 else if (pending & CAUSEF_IP7)
137 do_IRQ(ATH25_IRQ_CPU_CLOCK);
139 spurious_interrupt();
142 void __init ar5312_arch_init_irq(void)
144 struct irq_domain *domain;
147 ath25_irq_dispatch = ar5312_irq_dispatch;
149 domain = irq_domain_add_linear(NULL, AR5312_MISC_IRQ_COUNT,
150 &ar5312_misc_irq_domain_ops, NULL);
152 panic("Failed to add IRQ domain");
154 irq = irq_create_mapping(domain, AR5312_MISC_IRQ_AHB_PROC);
155 setup_irq(irq, &ar5312_ahb_err_interrupt);
157 irq_set_chained_handler(AR5312_IRQ_MISC, ar5312_misc_irq_handler);
158 irq_set_handler_data(AR5312_IRQ_MISC, domain);
160 ar5312_misc_irq_domain = domain;
163 static void __init ar5312_flash_init(void)
165 void __iomem *flashctl_base;
168 flashctl_base = ioremap_nocache(AR5312_FLASHCTL_BASE,
169 AR5312_FLASHCTL_SIZE);
172 * Configure flash bank 0.
173 * Assume 8M window size. Flash will be aliased if it's smaller
175 ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL0);
176 ctl &= AR5312_FLASHCTL_MW;
177 ctl |= AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC_8M | AR5312_FLASHCTL_RBLE;
178 ctl |= 0x01 << AR5312_FLASHCTL_IDCY_S;
179 ctl |= 0x07 << AR5312_FLASHCTL_WST1_S;
180 ctl |= 0x07 << AR5312_FLASHCTL_WST2_S;
181 __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL0);
183 /* Disable other flash banks */
184 ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL1);
185 ctl &= ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
186 __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL1);
187 ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL2);
188 ctl &= ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
189 __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL2);
191 iounmap(flashctl_base);
194 void __init ar5312_init_devices(void)
196 struct ath25_boarddata *config;
200 /* Locate board/radio config data */
201 ath25_find_config(AR5312_FLASH_BASE, AR5312_FLASH_SIZE);
202 config = ath25_board.config;
204 /* AR2313 has CPU minor rev. 10 */
205 if ((current_cpu_data.processor_id & 0xff) == 0x0a)
206 ath25_soc = ATH25_SOC_AR2313;
208 /* AR2312 shares the same Silicon ID as AR5312 */
209 else if (config->flags & BD_ISCASPER)
210 ath25_soc = ATH25_SOC_AR2312;
212 /* Everything else is probably AR5312 or compatible */
214 ath25_soc = ATH25_SOC_AR5312;
217 static void ar5312_restart(char *command)
219 /* reset the system */
222 ar5312_rst_reg_write(AR5312_RESET, AR5312_RESET_SYSTEM);
226 * This table is indexed by bits 5..4 of the CLOCKCTL1 register
227 * to determine the predevisor value.
229 static unsigned clockctl1_predivide_table[4] __initdata = { 1, 2, 4, 5 };
231 static unsigned __init ar5312_cpu_frequency(void)
233 u32 scratch, devid, clock_ctl1;
234 u32 predivide_mask, multiplier_mask, doubler_mask;
235 unsigned predivide_shift, multiplier_shift;
236 unsigned predivide_select, predivisor, multiplier;
238 /* Trust the bootrom's idea of cpu frequency. */
239 scratch = ar5312_rst_reg_read(AR5312_SCRATCH);
243 devid = ar5312_rst_reg_read(AR5312_REV);
244 devid = (devid & AR5312_REV_MAJ) >> AR5312_REV_MAJ_S;
245 if (devid == AR5312_REV_MAJ_AR2313) {
246 predivide_mask = AR2313_CLOCKCTL1_PREDIVIDE_MASK;
247 predivide_shift = AR2313_CLOCKCTL1_PREDIVIDE_SHIFT;
248 multiplier_mask = AR2313_CLOCKCTL1_MULTIPLIER_MASK;
249 multiplier_shift = AR2313_CLOCKCTL1_MULTIPLIER_SHIFT;
250 doubler_mask = AR2313_CLOCKCTL1_DOUBLER_MASK;
251 } else { /* AR5312 and AR2312 */
252 predivide_mask = AR5312_CLOCKCTL1_PREDIVIDE_MASK;
253 predivide_shift = AR5312_CLOCKCTL1_PREDIVIDE_SHIFT;
254 multiplier_mask = AR5312_CLOCKCTL1_MULTIPLIER_MASK;
255 multiplier_shift = AR5312_CLOCKCTL1_MULTIPLIER_SHIFT;
256 doubler_mask = AR5312_CLOCKCTL1_DOUBLER_MASK;
260 * Clocking is derived from a fixed 40MHz input clock.
262 * cpu_freq = input_clock * MULT (where MULT is PLL multiplier)
263 * sys_freq = cpu_freq / 4 (used for APB clock, serial,
264 * flash, Timer, Watchdog Timer)
266 * cnt_freq = cpu_freq / 2 (use for CPU count/compare)
268 * So, for example, with a PLL multiplier of 5, we have
274 * We compute the CPU frequency, based on PLL settings.
277 clock_ctl1 = ar5312_rst_reg_read(AR5312_CLOCKCTL1);
278 predivide_select = (clock_ctl1 & predivide_mask) >> predivide_shift;
279 predivisor = clockctl1_predivide_table[predivide_select];
280 multiplier = (clock_ctl1 & multiplier_mask) >> multiplier_shift;
282 if (clock_ctl1 & doubler_mask)
285 return (40000000 / predivisor) * multiplier;
288 static inline unsigned ar5312_sys_frequency(void)
290 return ar5312_cpu_frequency() / 4;
293 void __init ar5312_plat_time_init(void)
295 mips_hpt_frequency = ar5312_cpu_frequency() / 2;
298 void __init ar5312_plat_mem_setup(void)
300 void __iomem *sdram_base;
301 u32 memsize, memcfg, bank0_ac, bank1_ac;
304 /* Detect memory size */
305 sdram_base = ioremap_nocache(AR5312_SDRAMCTL_BASE,
306 AR5312_SDRAMCTL_SIZE);
307 memcfg = __raw_readl(sdram_base + AR5312_MEM_CFG1);
308 bank0_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC0);
309 bank1_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC1);
310 memsize = (bank0_ac ? (1 << (bank0_ac + 1)) : 0) +
311 (bank1_ac ? (1 << (bank1_ac + 1)) : 0);
313 add_memory_region(0, memsize, BOOT_MEM_RAM);
316 ar5312_rst_base = ioremap_nocache(AR5312_RST_BASE, AR5312_RST_SIZE);
318 devid = ar5312_rst_reg_read(AR5312_REV);
319 devid >>= AR5312_REV_WMAC_MIN_S;
320 devid &= AR5312_REV_CHIP;
321 ath25_board.devid = (u16)devid;
323 /* Clear any lingering AHB errors */
324 ar5312_rst_reg_read(AR5312_PROCADDR);
325 ar5312_rst_reg_read(AR5312_DMAADDR);
326 ar5312_rst_reg_write(AR5312_WDT_CTRL, AR5312_WDT_CTRL_IGNORE);
328 _machine_restart = ar5312_restart;
331 void __init ar5312_arch_init(void)
333 unsigned irq = irq_create_mapping(ar5312_misc_irq_domain,
334 AR5312_MISC_IRQ_UART0);
336 ath25_serial_setup(AR5312_UART0_BASE, irq, ar5312_sys_frequency());