blob: b99a02a9e20e92f7541a26478f4b54ac130719ff [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 Ryazanov3b12308f2014-10-29 03:18:39 +0400161static void ar5312_restart(char *command)
162{
163 /* reset the system */
164 local_irq_disable();
165 while (1)
166 ar5312_rst_reg_write(AR5312_RESET, AR5312_RESET_SYSTEM);
167}
168
169/*
170 * This table is indexed by bits 5..4 of the CLOCKCTL1 register
171 * to determine the predevisor value.
172 */
173static unsigned clockctl1_predivide_table[4] __initdata = { 1, 2, 4, 5 };
174
175static unsigned __init ar5312_cpu_frequency(void)
176{
177 u32 scratch, devid, clock_ctl1;
178 u32 predivide_mask, multiplier_mask, doubler_mask;
179 unsigned predivide_shift, multiplier_shift;
180 unsigned predivide_select, predivisor, multiplier;
181
182 /* Trust the bootrom's idea of cpu frequency. */
183 scratch = ar5312_rst_reg_read(AR5312_SCRATCH);
184 if (scratch)
185 return scratch;
186
187 devid = ar5312_rst_reg_read(AR5312_REV);
188 devid = (devid & AR5312_REV_MAJ) >> AR5312_REV_MAJ_S;
189 if (devid == AR5312_REV_MAJ_AR2313) {
190 predivide_mask = AR2313_CLOCKCTL1_PREDIVIDE_MASK;
191 predivide_shift = AR2313_CLOCKCTL1_PREDIVIDE_SHIFT;
192 multiplier_mask = AR2313_CLOCKCTL1_MULTIPLIER_MASK;
193 multiplier_shift = AR2313_CLOCKCTL1_MULTIPLIER_SHIFT;
194 doubler_mask = AR2313_CLOCKCTL1_DOUBLER_MASK;
195 } else { /* AR5312 and AR2312 */
196 predivide_mask = AR5312_CLOCKCTL1_PREDIVIDE_MASK;
197 predivide_shift = AR5312_CLOCKCTL1_PREDIVIDE_SHIFT;
198 multiplier_mask = AR5312_CLOCKCTL1_MULTIPLIER_MASK;
199 multiplier_shift = AR5312_CLOCKCTL1_MULTIPLIER_SHIFT;
200 doubler_mask = AR5312_CLOCKCTL1_DOUBLER_MASK;
201 }
202
203 /*
204 * Clocking is derived from a fixed 40MHz input clock.
205 *
206 * cpu_freq = input_clock * MULT (where MULT is PLL multiplier)
207 * sys_freq = cpu_freq / 4 (used for APB clock, serial,
208 * flash, Timer, Watchdog Timer)
209 *
210 * cnt_freq = cpu_freq / 2 (use for CPU count/compare)
211 *
212 * So, for example, with a PLL multiplier of 5, we have
213 *
214 * cpu_freq = 200MHz
215 * sys_freq = 50MHz
216 * cnt_freq = 100MHz
217 *
218 * We compute the CPU frequency, based on PLL settings.
219 */
220
221 clock_ctl1 = ar5312_rst_reg_read(AR5312_CLOCKCTL1);
222 predivide_select = (clock_ctl1 & predivide_mask) >> predivide_shift;
223 predivisor = clockctl1_predivide_table[predivide_select];
224 multiplier = (clock_ctl1 & multiplier_mask) >> multiplier_shift;
225
226 if (clock_ctl1 & doubler_mask)
227 multiplier <<= 1;
228
229 return (40000000 / predivisor) * multiplier;
230}
231
232static inline unsigned ar5312_sys_frequency(void)
233{
234 return ar5312_cpu_frequency() / 4;
235}
236
237void __init ar5312_plat_time_init(void)
238{
239 mips_hpt_frequency = ar5312_cpu_frequency() / 2;
240}
241
242void __init ar5312_plat_mem_setup(void)
243{
244 void __iomem *sdram_base;
245 u32 memsize, memcfg, bank0_ac, bank1_ac;
246
247 /* Detect memory size */
248 sdram_base = ioremap_nocache(AR5312_SDRAMCTL_BASE,
249 AR5312_SDRAMCTL_SIZE);
250 memcfg = __raw_readl(sdram_base + AR5312_MEM_CFG1);
251 bank0_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC0);
252 bank1_ac = ATH25_REG_MS(memcfg, AR5312_MEM_CFG1_AC1);
253 memsize = (bank0_ac ? (1 << (bank0_ac + 1)) : 0) +
254 (bank1_ac ? (1 << (bank1_ac + 1)) : 0);
255 memsize <<= 20;
256 add_memory_region(0, memsize, BOOT_MEM_RAM);
257 iounmap(sdram_base);
258
259 ar5312_rst_base = ioremap_nocache(AR5312_RST_BASE, AR5312_RST_SIZE);
260
261 /* Clear any lingering AHB errors */
262 ar5312_rst_reg_read(AR5312_PROCADDR);
263 ar5312_rst_reg_read(AR5312_DMAADDR);
264 ar5312_rst_reg_write(AR5312_WDT_CTRL, AR5312_WDT_CTRL_IGNORE);
265
266 _machine_restart = ar5312_restart;
267}