blob: fdab7f854f80be72d57c80f8ad00056e75ad8d27 [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 */
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +000035unsigned long leon3_gptimer_idx; /* Timer Index (0..6) within Timer Core */
Konrad Eisele5213a782009-08-17 00:13:29 +000036unsigned int sparc_leon_eirq;
37#define LEON_IMASK ((&leon3_irqctrl_regs->mask[0]))
38
39/* Return the IRQ of the pending IRQ on the extended IRQ controller */
40int sparc_leon_eirq_get(int eirq, int cpu)
41{
42 return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
43}
44
45irqreturn_t sparc_leon_eirq_isr(int dummy, void *dev_id)
46{
47 printk(KERN_ERR "sparc_leon_eirq_isr: ERROR EXTENDED IRQ\n");
48 return IRQ_HANDLED;
49}
50
51/* The extended IRQ controller has been found, this function registers it */
52void sparc_leon_eirq_register(int eirq)
53{
54 int irq;
55
56 /* Register a "BAD" handler for this interrupt, it should never happen */
57 irq = request_irq(eirq, sparc_leon_eirq_isr,
58 (IRQF_DISABLED | SA_STATIC_ALLOC), "extirq", NULL);
59
60 if (irq) {
61 printk(KERN_ERR
62 "sparc_leon_eirq_register: unable to attach IRQ%d\n",
63 eirq);
64 } else {
65 sparc_leon_eirq = eirq;
66 }
67
68}
69
70static inline unsigned long get_irqmask(unsigned int irq)
71{
72 unsigned long mask;
73
74 if (!irq || ((irq > 0xf) && !sparc_leon_eirq)
75 || ((irq > 0x1f) && sparc_leon_eirq)) {
76 printk(KERN_ERR
77 "leon_get_irqmask: false irq number: %d\n", irq);
78 mask = 0;
79 } else {
80 mask = LEON_HARD_INT(irq);
81 }
82 return mask;
83}
84
85static void leon_enable_irq(unsigned int irq_nr)
86{
87 unsigned long mask, flags;
88 mask = get_irqmask(irq_nr);
89 local_irq_save(flags);
90 LEON3_BYPASS_STORE_PA(LEON_IMASK,
91 (LEON3_BYPASS_LOAD_PA(LEON_IMASK) | (mask)));
92 local_irq_restore(flags);
93}
94
95static void leon_disable_irq(unsigned int irq_nr)
96{
97 unsigned long mask, flags;
98 mask = get_irqmask(irq_nr);
99 local_irq_save(flags);
100 LEON3_BYPASS_STORE_PA(LEON_IMASK,
101 (LEON3_BYPASS_LOAD_PA(LEON_IMASK) & ~(mask)));
102 local_irq_restore(flags);
103
104}
105
106void __init leon_init_timers(irq_handler_t counter_fn)
107{
108 int irq;
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000109 struct device_node *rootnp, *np, *nnp;
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000110 struct property *pp;
111 int len;
Daniel Hellstrome2305e32011-01-04 01:41:30 +0000112 int cpu, icsel;
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000113 int ampopts;
Konrad Eisele5213a782009-08-17 00:13:29 +0000114
115 leondebug_irq_disable = 0;
116 leon_debug_irqout = 0;
117 master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
118 dummy_master_l10_counter = 0;
119
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000120 /*Find IRQMP IRQ Controller Registers base address otherwise bail out.*/
121 rootnp = of_find_node_by_path("/ambapp0");
122 if (!rootnp)
123 goto bad;
124 np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
Daniel Hellstrom9742e722011-01-04 01:41:31 +0000125 if (!np) {
126 np = of_find_node_by_name(rootnp, "01_00d");
127 if (!np)
128 goto bad;
129 }
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000130 pp = of_find_property(np, "reg", &len);
131 if (!pp)
132 goto bad;
133 leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
134
135 /* Find GPTIMER Timer Registers base address otherwise bail out. */
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000136 nnp = rootnp;
137 do {
138 np = of_find_node_by_name(nnp, "GAISLER_GPTIMER");
139 if (!np) {
140 np = of_find_node_by_name(nnp, "01_011");
141 if (!np)
142 goto bad;
143 }
144
145 ampopts = 0;
146 pp = of_find_property(np, "ampopts", &len);
147 if (pp) {
148 ampopts = *(int *)pp->value;
149 if (ampopts == 0) {
150 /* Skip this instance, resource already
151 * allocated by other OS */
152 nnp = np;
153 continue;
154 }
155 }
156
157 /* Select Timer-Instance on Timer Core. Default is zero */
158 leon3_gptimer_idx = ampopts & 0x7;
159
160 pp = of_find_property(np, "reg", &len);
161 if (pp)
162 leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)
163 pp->value;
164 pp = of_find_property(np, "interrupts", &len);
165 if (pp)
166 leon3_gptimer_irq = *(unsigned int *)pp->value;
167 } while (0);
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000168
169 if (leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq) {
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000170 LEON3_BYPASS_STORE_PA(
171 &leon3_gptimer_regs->e[leon3_gptimer_idx].val, 0);
172 LEON3_BYPASS_STORE_PA(
173 &leon3_gptimer_regs->e[leon3_gptimer_idx].rld,
174 (((1000000 / HZ) - 1)));
175 LEON3_BYPASS_STORE_PA(
176 &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
Konrad Eisele5213a782009-08-17 00:13:29 +0000177
Konrad Eisele84017072009-08-31 22:08:13 +0000178#ifdef CONFIG_SMP
179 leon_percpu_timer_dev[0].start = (int)leon3_gptimer_regs;
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000180 leon_percpu_timer_dev[0].irq = leon3_gptimer_irq + 1 +
181 leon3_gptimer_idx;
Konrad Eisele84017072009-08-31 22:08:13 +0000182
183 if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
184 (1<<LEON3_GPTIMER_SEPIRQ))) {
Frans Pop0da2b302010-02-12 12:08:51 -0800185 prom_printf("irq timer not configured with separate irqs\n");
Konrad Eisele84017072009-08-31 22:08:13 +0000186 BUG();
187 }
188
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000189 LEON3_BYPASS_STORE_PA(
190 &leon3_gptimer_regs->e[leon3_gptimer_idx+1].val, 0);
191 LEON3_BYPASS_STORE_PA(
192 &leon3_gptimer_regs->e[leon3_gptimer_idx+1].rld,
193 (((1000000/HZ) - 1)));
194 LEON3_BYPASS_STORE_PA(
195 &leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl, 0);
Konrad Eisele84017072009-08-31 22:08:13 +0000196# endif
197
Daniel Hellstrome2305e32011-01-04 01:41:30 +0000198 /*
199 * The IRQ controller may (if implemented) consist of multiple
200 * IRQ controllers, each mapped on a 4Kb boundary.
201 * Each CPU may be routed to different IRQCTRLs, however
202 * we assume that all CPUs (in SMP system) is routed to the
203 * same IRQ Controller, and for non-SMP only one IRQCTRL is
204 * accessed anyway.
205 * In AMP systems, Linux must run on CPU0 for the time being.
206 */
207 cpu = sparc_leon3_cpuid();
208 icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
209 icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
210 leon3_irqctrl_regs += icsel;
Konrad Eisele5213a782009-08-17 00:13:29 +0000211 } else {
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000212 goto bad;
Konrad Eisele5213a782009-08-17 00:13:29 +0000213 }
214
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000215 irq = request_irq(leon3_gptimer_irq+leon3_gptimer_idx,
Konrad Eisele5213a782009-08-17 00:13:29 +0000216 counter_fn,
217 (IRQF_DISABLED | SA_STATIC_ALLOC), "timer", NULL);
218
219 if (irq) {
220 printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
221 LEON_INTERRUPT_TIMER1);
222 prom_halt();
223 }
224
Konrad Eisele84017072009-08-31 22:08:13 +0000225# ifdef CONFIG_SMP
226 {
227 unsigned long flags;
228 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_percpu_timer_dev[0].irq - 1)];
229
230 /* For SMP we use the level 14 ticker, however the bootup code
231 * has copied the firmwares level 14 vector into boot cpu's
232 * trap table, we must fix this now or we get squashed.
233 */
234 local_irq_save(flags);
235
236 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
237
238 /* Adjust so that we jump directly to smpleon_ticker */
239 trap_table->inst_three += smpleon_ticker - real_irq_entry;
240
241 local_flush_cache_all();
242 local_irq_restore(flags);
243 }
244# endif
245
Konrad Eisele5213a782009-08-17 00:13:29 +0000246 if (leon3_gptimer_regs) {
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000247 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
Konrad Eisele5213a782009-08-17 00:13:29 +0000248 LEON3_GPTIMER_EN |
249 LEON3_GPTIMER_RL |
250 LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
Konrad Eisele84017072009-08-31 22:08:13 +0000251
252#ifdef CONFIG_SMP
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000253 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
Konrad Eisele84017072009-08-31 22:08:13 +0000254 LEON3_GPTIMER_EN |
255 LEON3_GPTIMER_RL |
256 LEON3_GPTIMER_LD |
257 LEON3_GPTIMER_IRQEN);
258#endif
259
Konrad Eisele5213a782009-08-17 00:13:29 +0000260 }
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000261 return;
262bad:
263 printk(KERN_ERR "No Timer/irqctrl found\n");
264 BUG();
265 return;
Konrad Eisele5213a782009-08-17 00:13:29 +0000266}
267
268void leon_clear_clock_irq(void)
269{
270}
271
272void leon_load_profile_irq(int cpu, unsigned int limit)
273{
274 BUG();
275}
276
277
278
279
280void __init leon_trans_init(struct device_node *dp)
281{
282 if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
283 struct property *p;
284 p = of_find_property(dp, "mid", (void *)0);
285 if (p) {
286 int mid;
287 dp->name = prom_early_alloc(5 + 1);
288 memcpy(&mid, p->value, p->length);
289 sprintf((char *)dp->name, "cpu%.2d", mid);
290 }
291 }
292}
293
294void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
295
296void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
297{
298 if (prom_amba_init &&
299 strcmp(dp->type, "ambapp") == 0 &&
300 strcmp(dp->name, "ambapp0") == 0) {
301 prom_amba_init(dp, nextp);
302 }
303}
304
Konrad Eisele84017072009-08-31 22:08:13 +0000305#ifdef CONFIG_SMP
306
307void leon_set_cpu_int(int cpu, int level)
308{
309 unsigned long mask;
310 mask = get_irqmask(level);
311 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
312}
313
314static void leon_clear_ipi(int cpu, int level)
315{
316 unsigned long mask;
317 mask = get_irqmask(level);
318 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
319}
320
321static void leon_set_udt(int cpu)
322{
323}
324
325void leon_clear_profile_irq(int cpu)
326{
327}
328
329void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
330{
331 unsigned long mask, flags, *addr;
332 mask = get_irqmask(irq_nr);
333 local_irq_save(flags);
334 addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
335 LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
336 local_irq_restore(flags);
337}
338
339#endif
340
Konrad Eisele5213a782009-08-17 00:13:29 +0000341void __init leon_init_IRQ(void)
342{
343 sparc_init_timers = leon_init_timers;
344
345 BTFIXUPSET_CALL(enable_irq, leon_enable_irq, BTFIXUPCALL_NORM);
346 BTFIXUPSET_CALL(disable_irq, leon_disable_irq, BTFIXUPCALL_NORM);
347 BTFIXUPSET_CALL(enable_pil_irq, leon_enable_irq, BTFIXUPCALL_NORM);
348 BTFIXUPSET_CALL(disable_pil_irq, leon_disable_irq, BTFIXUPCALL_NORM);
349
350 BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
351 BTFIXUPCALL_NORM);
352 BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
353 BTFIXUPCALL_NOP);
354
355#ifdef CONFIG_SMP
356 BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
357 BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
358 BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
359#endif
360
361}
362
363void __init leon_init(void)
364{
Andres Salomoned418502010-10-10 21:51:25 -0600365 of_pdt_build_more = &leon_node_init;
Konrad Eisele5213a782009-08-17 00:13:29 +0000366}