blob: 41bd56d6ab23e537de6318edcf88cf77c3180e6b [file] [log] [blame]
Sergey Ryazanov3b12308f2014-10-29 03:18:39 +04001/*
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
4 * for more details.
5 *
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>
11 */
12
13/*
14 * Platform devices for Atheros AR5312 SoCs
15 */
16
17#include <linux/init.h>
18#include <linux/kernel.h>
Sergey Ryazanov1753e742014-10-29 03:18:41 +040019#include <linux/bitops.h>
20#include <linux/irqdomain.h>
21#include <linux/interrupt.h>
Sergey Ryazanov3b12308f2014-10-29 03:18:39 +040022#include <linux/reboot.h>
23#include <asm/bootinfo.h>
24#include <asm/reboot.h>
25#include <asm/time.h>
26
27#include "devices.h"
28#include "ar5312.h"
29#include "ar5312_regs.h"
30
31static void __iomem *ar5312_rst_base;
Sergey Ryazanov1753e742014-10-29 03:18:41 +040032static struct irq_domain *ar5312_misc_irq_domain;
Sergey Ryazanov3b12308f2014-10-29 03:18:39 +040033
34static inline u32 ar5312_rst_reg_read(u32 reg)
35{
36 return __raw_readl(ar5312_rst_base + reg);
37}
38
39static inline void ar5312_rst_reg_write(u32 reg, u32 val)
40{
41 __raw_writel(val, ar5312_rst_base + reg);
42}
43
44static inline void ar5312_rst_reg_mask(u32 reg, u32 mask, u32 val)
45{
46 u32 ret = ar5312_rst_reg_read(reg);
47
48 ret &= ~mask;
49 ret |= val;
50 ar5312_rst_reg_write(reg, ret);
51}
52
Sergey Ryazanov1753e742014-10-29 03:18:41 +040053static irqreturn_t ar5312_ahb_err_handler(int cpl, void *dev_id)
54{
55 u32 proc1 = ar5312_rst_reg_read(AR5312_PROC1);
56 u32 proc_addr = ar5312_rst_reg_read(AR5312_PROCADDR); /* clears error */
57 u32 dma1 = ar5312_rst_reg_read(AR5312_DMA1);
58 u32 dma_addr = ar5312_rst_reg_read(AR5312_DMAADDR); /* clears error */
59
60 pr_emerg("AHB interrupt: PROCADDR=0x%8.8x PROC1=0x%8.8x DMAADDR=0x%8.8x DMA1=0x%8.8x\n",
61 proc_addr, proc1, dma_addr, dma1);
62
63 machine_restart("AHB error"); /* Catastrophic failure */
64 return IRQ_HANDLED;
65}
66
67static struct irqaction ar5312_ahb_err_interrupt = {
68 .handler = ar5312_ahb_err_handler,
69 .name = "ar5312-ahb-error",
70};
71
72static void ar5312_misc_irq_handler(unsigned irq, struct irq_desc *desc)
73{
74 u32 pending = ar5312_rst_reg_read(AR5312_ISR) &
75 ar5312_rst_reg_read(AR5312_IMR);
76 unsigned nr, misc_irq = 0;
77
78 if (pending) {
79 struct irq_domain *domain = irq_get_handler_data(irq);
80
81 nr = __ffs(pending);
82 misc_irq = irq_find_mapping(domain, nr);
83 }
84
85 if (misc_irq) {
86 generic_handle_irq(misc_irq);
87 if (nr == AR5312_MISC_IRQ_TIMER)
88 ar5312_rst_reg_read(AR5312_TIMER);
89 } else {
90 spurious_interrupt();
91 }
92}
93
94/* Enable the specified AR5312_MISC_IRQ interrupt */
95static void ar5312_misc_irq_unmask(struct irq_data *d)
96{
97 ar5312_rst_reg_mask(AR5312_IMR, 0, BIT(d->hwirq));
98}
99
100/* Disable the specified AR5312_MISC_IRQ interrupt */
101static void ar5312_misc_irq_mask(struct irq_data *d)
102{
103 ar5312_rst_reg_mask(AR5312_IMR, BIT(d->hwirq), 0);
104 ar5312_rst_reg_read(AR5312_IMR); /* flush write buffer */
105}
106
107static struct irq_chip ar5312_misc_irq_chip = {
108 .name = "ar5312-misc",
109 .irq_unmask = ar5312_misc_irq_unmask,
110 .irq_mask = ar5312_misc_irq_mask,
111};
112
113static int ar5312_misc_irq_map(struct irq_domain *d, unsigned irq,
114 irq_hw_number_t hw)
115{
116 irq_set_chip_and_handler(irq, &ar5312_misc_irq_chip, handle_level_irq);
117 return 0;
118}
119
120static struct irq_domain_ops ar5312_misc_irq_domain_ops = {
121 .map = ar5312_misc_irq_map,
122};
123
124static void ar5312_irq_dispatch(void)
125{
126 u32 pending = read_c0_status() & read_c0_cause();
127
128 if (pending & CAUSEF_IP2)
129 do_IRQ(AR5312_IRQ_WLAN0);
130 else if (pending & CAUSEF_IP5)
131 do_IRQ(AR5312_IRQ_WLAN1);
132 else if (pending & CAUSEF_IP6)
133 do_IRQ(AR5312_IRQ_MISC);
134 else if (pending & CAUSEF_IP7)
135 do_IRQ(ATH25_IRQ_CPU_CLOCK);
136 else
137 spurious_interrupt();
138}
139
140void __init ar5312_arch_init_irq(void)
141{
142 struct irq_domain *domain;
143 unsigned irq;
144
145 ath25_irq_dispatch = ar5312_irq_dispatch;
146
147 domain = irq_domain_add_linear(NULL, AR5312_MISC_IRQ_COUNT,
148 &ar5312_misc_irq_domain_ops, NULL);
149 if (!domain)
150 panic("Failed to add IRQ domain");
151
152 irq = irq_create_mapping(domain, AR5312_MISC_IRQ_AHB_PROC);
153 setup_irq(irq, &ar5312_ahb_err_interrupt);
154
155 irq_set_chained_handler(AR5312_IRQ_MISC, ar5312_misc_irq_handler);
156 irq_set_handler_data(AR5312_IRQ_MISC, domain);
157
158 ar5312_misc_irq_domain = domain;
159}
160
Sergey Ryazanova7473712014-10-29 03:18:44 +0400161static void __init ar5312_flash_init(void)
162{
163 void __iomem *flashctl_base;
164 u32 ctl;
165
166 flashctl_base = ioremap_nocache(AR5312_FLASHCTL_BASE,
167 AR5312_FLASHCTL_SIZE);
168
169 /*
170 * Configure flash bank 0.
171 * Assume 8M window size. Flash will be aliased if it's smaller
172 */
173 ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL0);
174 ctl &= AR5312_FLASHCTL_MW;
175 ctl |= AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC_8M | AR5312_FLASHCTL_RBLE;
176 ctl |= 0x01 << AR5312_FLASHCTL_IDCY_S;
177 ctl |= 0x07 << AR5312_FLASHCTL_WST1_S;
178 ctl |= 0x07 << AR5312_FLASHCTL_WST2_S;
179 __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL0);
180
181 /* Disable other flash banks */
182 ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL1);
183 ctl &= ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
184 __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL1);
185 ctl = __raw_readl(flashctl_base + AR5312_FLASHCTL2);
186 ctl &= ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
187 __raw_writel(ctl, flashctl_base + AR5312_FLASHCTL2);
188
189 iounmap(flashctl_base);
190}
191
192void __init ar5312_init_devices(void)
193{
194 ar5312_flash_init();
195
196 /* Locate board/radio config data */
197 ath25_find_config(AR5312_FLASH_BASE, AR5312_FLASH_SIZE);
198}
199
Sergey Ryazanov3b12308f2014-10-29 03:18:39 +0400200static void ar5312_restart(char *command)
201{
202 /* reset the system */
203 local_irq_disable();
204 while (1)
205 ar5312_rst_reg_write(AR5312_RESET, AR5312_RESET_SYSTEM);
206}
207
208/*
209 * This table is indexed by bits 5..4 of the CLOCKCTL1 register
210 * to determine the predevisor value.
211 */
212static unsigned clockctl1_predivide_table[4] __initdata = { 1, 2, 4, 5 };
213
214static unsigned __init ar5312_cpu_frequency(void)
215{
216 u32 scratch, devid, clock_ctl1;
217 u32 predivide_mask, multiplier_mask, doubler_mask;
218 unsigned predivide_shift, multiplier_shift;
219 unsigned predivide_select, predivisor, multiplier;
220
221 /* Trust the bootrom's idea of cpu frequency. */
222 scratch = ar5312_rst_reg_read(AR5312_SCRATCH);
223 if (scratch)
224 return scratch;
225
226 devid = ar5312_rst_reg_read(AR5312_REV);
227 devid = (devid & AR5312_REV_MAJ) >> AR5312_REV_MAJ_S;
228 if (devid == AR5312_REV_MAJ_AR2313) {
229 predivide_mask = AR2313_CLOCKCTL1_PREDIVIDE_MASK;
230 predivide_shift = AR2313_CLOCKCTL1_PREDIVIDE_SHIFT;
231 multiplier_mask = AR2313_CLOCKCTL1_MULTIPLIER_MASK;
232 multiplier_shift = AR2313_CLOCKCTL1_MULTIPLIER_SHIFT;
233 doubler_mask = AR2313_CLOCKCTL1_DOUBLER_MASK;
234 } else { /* AR5312 and AR2312 */
235 predivide_mask = AR5312_CLOCKCTL1_PREDIVIDE_MASK;
236 predivide_shift = AR5312_CLOCKCTL1_PREDIVIDE_SHIFT;
237 multiplier_mask = AR5312_CLOCKCTL1_MULTIPLIER_MASK;
238 multiplier_shift = AR5312_CLOCKCTL1_MULTIPLIER_SHIFT;
239 doubler_mask = AR5312_CLOCKCTL1_DOUBLER_MASK;
240 }
241
242 /*
243 * Clocking is derived from a fixed 40MHz input clock.
244 *
245 * cpu_freq = input_clock * MULT (where MULT is PLL multiplier)
246 * sys_freq = cpu_freq / 4 (used for APB clock, serial,
247 * flash, Timer, Watchdog Timer)
248 *
249 * cnt_freq = cpu_freq / 2 (use for CPU count/compare)
250 *
251 * So, for example, with a PLL multiplier of 5, we have
252 *
253 * cpu_freq = 200MHz
254 * sys_freq = 50MHz
255 * cnt_freq = 100MHz
256 *
257 * We compute the CPU frequency, based on PLL settings.
258 */
259
260 clock_ctl1 = ar5312_rst_reg_read(AR5312_CLOCKCTL1);
261 predivide_select = (clock_ctl1 & predivide_mask) >> predivide_shift;
262 predivisor = clockctl1_predivide_table[predivide_select];
263 multiplier = (clock_ctl1 & multiplier_mask) >> multiplier_shift;
264
265 if (clock_ctl1 & doubler_mask)
266 multiplier <<= 1;
267
268 return (40000000 / predivisor) * multiplier;
269}
270
271static inline unsigned ar5312_sys_frequency(void)
272{
273 return ar5312_cpu_frequency() / 4;
274}
275
276void __init ar5312_plat_time_init(void)
277{
278 mips_hpt_frequency = ar5312_cpu_frequency() / 2;
279}
280
281void __init ar5312_plat_mem_setup(void)
282{
283 void __iomem *sdram_base;
284 u32 memsize, memcfg, bank0_ac, bank1_ac;
285
286 /* Detect memory size */
287 sdram_base = ioremap_nocache(AR5312_SDRAMCTL_BASE,
288 AR5312_SDRAMCTL_SIZE);
289 memcfg = __raw_readl(sdram_base + AR5312_MEM_CFG1);
290 bank0_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC0);
291 bank1_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC1);
292 memsize = (bank0_ac ? (1 << (bank0_ac + 1)) : 0) +
293 (bank1_ac ? (1 << (bank1_ac + 1)) : 0);
294 memsize <<= 20;
295 add_memory_region(0, memsize, BOOT_MEM_RAM);
296 iounmap(sdram_base);
297
298 ar5312_rst_base = ioremap_nocache(AR5312_RST_BASE, AR5312_RST_SIZE);
299
300 /* Clear any lingering AHB errors */
301 ar5312_rst_reg_read(AR5312_PROCADDR);
302 ar5312_rst_reg_read(AR5312_DMAADDR);
303 ar5312_rst_reg_write(AR5312_WDT_CTRL, AR5312_WDT_CTRL_IGNORE);
304
305 _machine_restart = ar5312_restart;
306}
Sergey Ryazanov1ac91b12014-10-29 03:18:43 +0400307
308void __init ar5312_arch_init(void)
309{
310 unsigned irq = irq_create_mapping(ar5312_misc_irq_domain,
311 AR5312_MISC_IRQ_UART0);
312
313 ath25_serial_setup(AR5312_UART0_BASE, irq, ar5312_sys_frequency());
314}