blob: 91a978f4d83beadb507b6c4313a658fc5ca3deee [file] [log] [blame]
Konrad Eisele5213a782009-08-17 00:13:29 +00001/*
2 * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
3 * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/errno.h>
9#include <linux/mutex.h>
Konrad Eisele5213a782009-08-17 00:13:29 +000010#include <linux/of.h>
11#include <linux/of_platform.h>
12#include <linux/interrupt.h>
13#include <linux/of_device.h>
Konrad Eisele84017072009-08-31 22:08:13 +000014
Konrad Eisele5213a782009-08-17 00:13:29 +000015#include <asm/oplib.h>
16#include <asm/timer.h>
17#include <asm/prom.h>
18#include <asm/leon.h>
19#include <asm/leon_amba.h>
Konrad Eisele84017072009-08-31 22:08:13 +000020#include <asm/traps.h>
21#include <asm/cacheflush.h>
Konrad Eisele5213a782009-08-17 00:13:29 +000022
23#include "prom.h"
24#include "irq.h"
25
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +000026struct leon3_irqctrl_regs_map *leon3_irqctrl_regs; /* interrupt controller base address */
27struct leon3_gptimer_regs_map *leon3_gptimer_regs; /* timer controller base address */
Konrad Eisele5213a782009-08-17 00:13:29 +000028struct amba_apb_device leon_percpu_timer_dev[16];
29
30int leondebug_irq_disable;
31int leon_debug_irqout;
32static int dummy_master_l10_counter;
33
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +000034unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
Konrad Eisele5213a782009-08-17 00:13:29 +000035unsigned int sparc_leon_eirq;
36#define LEON_IMASK ((&leon3_irqctrl_regs->mask[0]))
37
38/* Return the IRQ of the pending IRQ on the extended IRQ controller */
39int sparc_leon_eirq_get(int eirq, int cpu)
40{
41 return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
42}
43
44irqreturn_t sparc_leon_eirq_isr(int dummy, void *dev_id)
45{
46 printk(KERN_ERR "sparc_leon_eirq_isr: ERROR EXTENDED IRQ\n");
47 return IRQ_HANDLED;
48}
49
50/* The extended IRQ controller has been found, this function registers it */
51void sparc_leon_eirq_register(int eirq)
52{
53 int irq;
54
55 /* Register a "BAD" handler for this interrupt, it should never happen */
56 irq = request_irq(eirq, sparc_leon_eirq_isr,
57 (IRQF_DISABLED | SA_STATIC_ALLOC), "extirq", NULL);
58
59 if (irq) {
60 printk(KERN_ERR
61 "sparc_leon_eirq_register: unable to attach IRQ%d\n",
62 eirq);
63 } else {
64 sparc_leon_eirq = eirq;
65 }
66
67}
68
69static inline unsigned long get_irqmask(unsigned int irq)
70{
71 unsigned long mask;
72
73 if (!irq || ((irq > 0xf) && !sparc_leon_eirq)
74 || ((irq > 0x1f) && sparc_leon_eirq)) {
75 printk(KERN_ERR
76 "leon_get_irqmask: false irq number: %d\n", irq);
77 mask = 0;
78 } else {
79 mask = LEON_HARD_INT(irq);
80 }
81 return mask;
82}
83
84static void leon_enable_irq(unsigned int irq_nr)
85{
86 unsigned long mask, flags;
87 mask = get_irqmask(irq_nr);
88 local_irq_save(flags);
89 LEON3_BYPASS_STORE_PA(LEON_IMASK,
90 (LEON3_BYPASS_LOAD_PA(LEON_IMASK) | (mask)));
91 local_irq_restore(flags);
92}
93
94static void leon_disable_irq(unsigned int irq_nr)
95{
96 unsigned long mask, flags;
97 mask = get_irqmask(irq_nr);
98 local_irq_save(flags);
99 LEON3_BYPASS_STORE_PA(LEON_IMASK,
100 (LEON3_BYPASS_LOAD_PA(LEON_IMASK) & ~(mask)));
101 local_irq_restore(flags);
102
103}
104
105void __init leon_init_timers(irq_handler_t counter_fn)
106{
107 int irq;
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000108 struct device_node *rootnp, *np;
109 struct property *pp;
110 int len;
Daniel Hellstrome2305e32011-01-04 01:41:30 +0000111 int cpu, icsel;
Konrad Eisele5213a782009-08-17 00:13:29 +0000112
113 leondebug_irq_disable = 0;
114 leon_debug_irqout = 0;
115 master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
116 dummy_master_l10_counter = 0;
117
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000118 /*Find IRQMP IRQ Controller Registers base address otherwise bail out.*/
119 rootnp = of_find_node_by_path("/ambapp0");
120 if (!rootnp)
121 goto bad;
122 np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
123 if (!np)
124 goto bad;
125 pp = of_find_property(np, "reg", &len);
126 if (!pp)
127 goto bad;
128 leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
129
130 /* Find GPTIMER Timer Registers base address otherwise bail out. */
131 np = of_find_node_by_name(rootnp, "GAISLER_GPTIMER");
132 if (!np)
133 goto bad;
134 pp = of_find_property(np, "reg", &len);
135 if (!pp)
136 goto bad;
137 leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)pp->value;
138 pp = of_find_property(np, "interrupts", &len);
139 if (!pp)
140 goto bad;
141 leon3_gptimer_irq = *(unsigned int *)pp->value;
142
143 if (leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq) {
Konrad Eisele5213a782009-08-17 00:13:29 +0000144 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].val, 0);
145 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].rld,
Daniel Hellstromb690c422010-10-29 13:25:24 -0700146 (((1000000 / HZ) - 1)));
Konrad Eisele5213a782009-08-17 00:13:29 +0000147 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl, 0);
148
Konrad Eisele84017072009-08-31 22:08:13 +0000149#ifdef CONFIG_SMP
150 leon_percpu_timer_dev[0].start = (int)leon3_gptimer_regs;
151 leon_percpu_timer_dev[0].irq = leon3_gptimer_irq+1;
152
153 if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
154 (1<<LEON3_GPTIMER_SEPIRQ))) {
Frans Pop0da2b302010-02-12 12:08:51 -0800155 prom_printf("irq timer not configured with separate irqs\n");
Konrad Eisele84017072009-08-31 22:08:13 +0000156 BUG();
157 }
158
159 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].val, 0);
Daniel Hellstromb690c422010-10-29 13:25:24 -0700160 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].rld, (((1000000/HZ) - 1)));
Konrad Eisele84017072009-08-31 22:08:13 +0000161 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl, 0);
162# endif
163
Daniel Hellstrome2305e32011-01-04 01:41:30 +0000164 /*
165 * The IRQ controller may (if implemented) consist of multiple
166 * IRQ controllers, each mapped on a 4Kb boundary.
167 * Each CPU may be routed to different IRQCTRLs, however
168 * we assume that all CPUs (in SMP system) is routed to the
169 * same IRQ Controller, and for non-SMP only one IRQCTRL is
170 * accessed anyway.
171 * In AMP systems, Linux must run on CPU0 for the time being.
172 */
173 cpu = sparc_leon3_cpuid();
174 icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
175 icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
176 leon3_irqctrl_regs += icsel;
Konrad Eisele5213a782009-08-17 00:13:29 +0000177 } else {
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000178 goto bad;
Konrad Eisele5213a782009-08-17 00:13:29 +0000179 }
180
181 irq = request_irq(leon3_gptimer_irq,
182 counter_fn,
183 (IRQF_DISABLED | SA_STATIC_ALLOC), "timer", NULL);
184
185 if (irq) {
186 printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
187 LEON_INTERRUPT_TIMER1);
188 prom_halt();
189 }
190
Konrad Eisele84017072009-08-31 22:08:13 +0000191# ifdef CONFIG_SMP
192 {
193 unsigned long flags;
194 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_percpu_timer_dev[0].irq - 1)];
195
196 /* For SMP we use the level 14 ticker, however the bootup code
197 * has copied the firmwares level 14 vector into boot cpu's
198 * trap table, we must fix this now or we get squashed.
199 */
200 local_irq_save(flags);
201
202 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
203
204 /* Adjust so that we jump directly to smpleon_ticker */
205 trap_table->inst_three += smpleon_ticker - real_irq_entry;
206
207 local_flush_cache_all();
208 local_irq_restore(flags);
209 }
210# endif
211
Konrad Eisele5213a782009-08-17 00:13:29 +0000212 if (leon3_gptimer_regs) {
213 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl,
214 LEON3_GPTIMER_EN |
215 LEON3_GPTIMER_RL |
216 LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
Konrad Eisele84017072009-08-31 22:08:13 +0000217
218#ifdef CONFIG_SMP
219 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl,
220 LEON3_GPTIMER_EN |
221 LEON3_GPTIMER_RL |
222 LEON3_GPTIMER_LD |
223 LEON3_GPTIMER_IRQEN);
224#endif
225
Konrad Eisele5213a782009-08-17 00:13:29 +0000226 }
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000227 return;
228bad:
229 printk(KERN_ERR "No Timer/irqctrl found\n");
230 BUG();
231 return;
Konrad Eisele5213a782009-08-17 00:13:29 +0000232}
233
234void leon_clear_clock_irq(void)
235{
236}
237
238void leon_load_profile_irq(int cpu, unsigned int limit)
239{
240 BUG();
241}
242
243
244
245
246void __init leon_trans_init(struct device_node *dp)
247{
248 if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
249 struct property *p;
250 p = of_find_property(dp, "mid", (void *)0);
251 if (p) {
252 int mid;
253 dp->name = prom_early_alloc(5 + 1);
254 memcpy(&mid, p->value, p->length);
255 sprintf((char *)dp->name, "cpu%.2d", mid);
256 }
257 }
258}
259
260void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
261
262void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
263{
264 if (prom_amba_init &&
265 strcmp(dp->type, "ambapp") == 0 &&
266 strcmp(dp->name, "ambapp0") == 0) {
267 prom_amba_init(dp, nextp);
268 }
269}
270
Konrad Eisele84017072009-08-31 22:08:13 +0000271#ifdef CONFIG_SMP
272
273void leon_set_cpu_int(int cpu, int level)
274{
275 unsigned long mask;
276 mask = get_irqmask(level);
277 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
278}
279
280static void leon_clear_ipi(int cpu, int level)
281{
282 unsigned long mask;
283 mask = get_irqmask(level);
284 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
285}
286
287static void leon_set_udt(int cpu)
288{
289}
290
291void leon_clear_profile_irq(int cpu)
292{
293}
294
295void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
296{
297 unsigned long mask, flags, *addr;
298 mask = get_irqmask(irq_nr);
299 local_irq_save(flags);
300 addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
301 LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
302 local_irq_restore(flags);
303}
304
305#endif
306
Konrad Eisele5213a782009-08-17 00:13:29 +0000307void __init leon_init_IRQ(void)
308{
309 sparc_init_timers = leon_init_timers;
310
311 BTFIXUPSET_CALL(enable_irq, leon_enable_irq, BTFIXUPCALL_NORM);
312 BTFIXUPSET_CALL(disable_irq, leon_disable_irq, BTFIXUPCALL_NORM);
313 BTFIXUPSET_CALL(enable_pil_irq, leon_enable_irq, BTFIXUPCALL_NORM);
314 BTFIXUPSET_CALL(disable_pil_irq, leon_disable_irq, BTFIXUPCALL_NORM);
315
316 BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
317 BTFIXUPCALL_NORM);
318 BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
319 BTFIXUPCALL_NOP);
320
321#ifdef CONFIG_SMP
322 BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
323 BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
324 BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
325#endif
326
327}
328
329void __init leon_init(void)
330{
Andres Salomoned418502010-10-10 21:51:25 -0600331 of_pdt_build_more = &leon_node_init;
Konrad Eisele5213a782009-08-17 00:13:29 +0000332}