blob: 8591cf124ecf486fad30dae963a48adab5e95b81 [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;
Daniel Hellstrom7279b822011-01-27 01:26:58 +000033unsigned long amba_system_id;
Konrad Eisele5213a782009-08-17 00:13:29 +000034
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +000035unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +000036unsigned long leon3_gptimer_idx; /* Timer Index (0..6) within Timer Core */
Konrad Eisele5213a782009-08-17 00:13:29 +000037unsigned int sparc_leon_eirq;
38#define LEON_IMASK ((&leon3_irqctrl_regs->mask[0]))
39
40/* Return the IRQ of the pending IRQ on the extended IRQ controller */
41int sparc_leon_eirq_get(int eirq, int cpu)
42{
43 return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
44}
45
46irqreturn_t sparc_leon_eirq_isr(int dummy, void *dev_id)
47{
48 printk(KERN_ERR "sparc_leon_eirq_isr: ERROR EXTENDED IRQ\n");
49 return IRQ_HANDLED;
50}
51
52/* The extended IRQ controller has been found, this function registers it */
53void sparc_leon_eirq_register(int eirq)
54{
55 int irq;
56
57 /* Register a "BAD" handler for this interrupt, it should never happen */
58 irq = request_irq(eirq, sparc_leon_eirq_isr,
59 (IRQF_DISABLED | SA_STATIC_ALLOC), "extirq", NULL);
60
61 if (irq) {
62 printk(KERN_ERR
63 "sparc_leon_eirq_register: unable to attach IRQ%d\n",
64 eirq);
65 } else {
66 sparc_leon_eirq = eirq;
67 }
68
69}
70
71static inline unsigned long get_irqmask(unsigned int irq)
72{
73 unsigned long mask;
74
75 if (!irq || ((irq > 0xf) && !sparc_leon_eirq)
76 || ((irq > 0x1f) && sparc_leon_eirq)) {
77 printk(KERN_ERR
78 "leon_get_irqmask: false irq number: %d\n", irq);
79 mask = 0;
80 } else {
81 mask = LEON_HARD_INT(irq);
82 }
83 return mask;
84}
85
Sam Ravnborg6baa9b22011-04-18 11:25:44 +000086static void leon_unmask_irq(struct irq_data *data)
Konrad Eisele5213a782009-08-17 00:13:29 +000087{
88 unsigned long mask, flags;
Sam Ravnborg6baa9b22011-04-18 11:25:44 +000089
90 mask = (unsigned long)data->chip_data;
Konrad Eisele5213a782009-08-17 00:13:29 +000091 local_irq_save(flags);
92 LEON3_BYPASS_STORE_PA(LEON_IMASK,
93 (LEON3_BYPASS_LOAD_PA(LEON_IMASK) | (mask)));
94 local_irq_restore(flags);
95}
96
Sam Ravnborg6baa9b22011-04-18 11:25:44 +000097static void leon_mask_irq(struct irq_data *data)
Konrad Eisele5213a782009-08-17 00:13:29 +000098{
99 unsigned long mask, flags;
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000100
101 mask = (unsigned long)data->chip_data;
Konrad Eisele5213a782009-08-17 00:13:29 +0000102 local_irq_save(flags);
103 LEON3_BYPASS_STORE_PA(LEON_IMASK,
104 (LEON3_BYPASS_LOAD_PA(LEON_IMASK) & ~(mask)));
105 local_irq_restore(flags);
106
107}
108
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000109static unsigned int leon_startup_irq(struct irq_data *data)
110{
111 irq_link(data->irq);
112 leon_unmask_irq(data);
113 return 0;
114}
115
116static void leon_shutdown_irq(struct irq_data *data)
117{
118 leon_mask_irq(data);
119 irq_unlink(data->irq);
120}
121
122static struct irq_chip leon_irq = {
123 .name = "leon",
124 .irq_startup = leon_startup_irq,
125 .irq_shutdown = leon_shutdown_irq,
126 .irq_mask = leon_mask_irq,
127 .irq_unmask = leon_unmask_irq,
128};
129
130static unsigned int leon_build_device_irq(struct platform_device *op,
131 unsigned int real_irq)
132{
133 unsigned int irq;
134 unsigned long mask;
135
136 irq = 0;
137 mask = get_irqmask(real_irq);
138 if (mask == 0)
139 goto out;
140
141 irq = irq_alloc(real_irq, real_irq);
142 if (irq == 0)
143 goto out;
144
145 irq_set_chip_and_handler_name(irq, &leon_irq,
146 handle_simple_irq, "edge");
147 irq_set_chip_data(irq, (void *)mask);
148
149out:
150 return irq;
151}
152
Konrad Eisele5213a782009-08-17 00:13:29 +0000153void __init leon_init_timers(irq_handler_t counter_fn)
154{
155 int irq;
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000156 struct device_node *rootnp, *np, *nnp;
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000157 struct property *pp;
158 int len;
Daniel Hellstrome2305e32011-01-04 01:41:30 +0000159 int cpu, icsel;
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000160 int ampopts;
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000161 int err;
Konrad Eisele5213a782009-08-17 00:13:29 +0000162
163 leondebug_irq_disable = 0;
164 leon_debug_irqout = 0;
165 master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
166 dummy_master_l10_counter = 0;
167
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000168 rootnp = of_find_node_by_path("/ambapp0");
169 if (!rootnp)
170 goto bad;
Daniel Hellstrom7279b822011-01-27 01:26:58 +0000171
172 /* Find System ID: GRLIB build ID and optional CHIP ID */
173 pp = of_find_property(rootnp, "systemid", &len);
174 if (pp)
175 amba_system_id = *(unsigned long *)pp->value;
176
177 /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000178 np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
Daniel Hellstrom9742e722011-01-04 01:41:31 +0000179 if (!np) {
180 np = of_find_node_by_name(rootnp, "01_00d");
181 if (!np)
182 goto bad;
183 }
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000184 pp = of_find_property(np, "reg", &len);
185 if (!pp)
186 goto bad;
187 leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
188
189 /* Find GPTIMER Timer Registers base address otherwise bail out. */
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000190 nnp = rootnp;
191 do {
192 np = of_find_node_by_name(nnp, "GAISLER_GPTIMER");
193 if (!np) {
194 np = of_find_node_by_name(nnp, "01_011");
195 if (!np)
196 goto bad;
197 }
198
199 ampopts = 0;
200 pp = of_find_property(np, "ampopts", &len);
201 if (pp) {
202 ampopts = *(int *)pp->value;
203 if (ampopts == 0) {
204 /* Skip this instance, resource already
205 * allocated by other OS */
206 nnp = np;
207 continue;
208 }
209 }
210
211 /* Select Timer-Instance on Timer Core. Default is zero */
212 leon3_gptimer_idx = ampopts & 0x7;
213
214 pp = of_find_property(np, "reg", &len);
215 if (pp)
216 leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)
217 pp->value;
218 pp = of_find_property(np, "interrupts", &len);
219 if (pp)
220 leon3_gptimer_irq = *(unsigned int *)pp->value;
221 } while (0);
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000222
223 if (leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq) {
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000224 LEON3_BYPASS_STORE_PA(
225 &leon3_gptimer_regs->e[leon3_gptimer_idx].val, 0);
226 LEON3_BYPASS_STORE_PA(
227 &leon3_gptimer_regs->e[leon3_gptimer_idx].rld,
228 (((1000000 / HZ) - 1)));
229 LEON3_BYPASS_STORE_PA(
230 &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
Konrad Eisele5213a782009-08-17 00:13:29 +0000231
Konrad Eisele84017072009-08-31 22:08:13 +0000232#ifdef CONFIG_SMP
233 leon_percpu_timer_dev[0].start = (int)leon3_gptimer_regs;
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000234 leon_percpu_timer_dev[0].irq = leon3_gptimer_irq + 1 +
235 leon3_gptimer_idx;
Konrad Eisele84017072009-08-31 22:08:13 +0000236
237 if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
238 (1<<LEON3_GPTIMER_SEPIRQ))) {
Frans Pop0da2b302010-02-12 12:08:51 -0800239 prom_printf("irq timer not configured with separate irqs\n");
Konrad Eisele84017072009-08-31 22:08:13 +0000240 BUG();
241 }
242
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000243 LEON3_BYPASS_STORE_PA(
244 &leon3_gptimer_regs->e[leon3_gptimer_idx+1].val, 0);
245 LEON3_BYPASS_STORE_PA(
246 &leon3_gptimer_regs->e[leon3_gptimer_idx+1].rld,
247 (((1000000/HZ) - 1)));
248 LEON3_BYPASS_STORE_PA(
249 &leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl, 0);
Konrad Eisele84017072009-08-31 22:08:13 +0000250# endif
251
Daniel Hellstrome2305e32011-01-04 01:41:30 +0000252 /*
253 * The IRQ controller may (if implemented) consist of multiple
254 * IRQ controllers, each mapped on a 4Kb boundary.
255 * Each CPU may be routed to different IRQCTRLs, however
256 * we assume that all CPUs (in SMP system) is routed to the
257 * same IRQ Controller, and for non-SMP only one IRQCTRL is
258 * accessed anyway.
259 * In AMP systems, Linux must run on CPU0 for the time being.
260 */
261 cpu = sparc_leon3_cpuid();
262 icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
263 icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
264 leon3_irqctrl_regs += icsel;
Konrad Eisele5213a782009-08-17 00:13:29 +0000265 } else {
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000266 goto bad;
Konrad Eisele5213a782009-08-17 00:13:29 +0000267 }
268
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000269 irq = leon_build_device_irq(NULL, leon3_gptimer_irq + leon3_gptimer_idx);
270 err = request_irq(irq, counter_fn, IRQF_TIMER, "timer", NULL);
Konrad Eisele5213a782009-08-17 00:13:29 +0000271
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000272 if (err) {
Konrad Eisele5213a782009-08-17 00:13:29 +0000273 printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
274 LEON_INTERRUPT_TIMER1);
275 prom_halt();
276 }
277
Konrad Eisele84017072009-08-31 22:08:13 +0000278# ifdef CONFIG_SMP
279 {
280 unsigned long flags;
281 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_percpu_timer_dev[0].irq - 1)];
282
283 /* For SMP we use the level 14 ticker, however the bootup code
284 * has copied the firmwares level 14 vector into boot cpu's
285 * trap table, we must fix this now or we get squashed.
286 */
287 local_irq_save(flags);
288
289 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
290
291 /* Adjust so that we jump directly to smpleon_ticker */
292 trap_table->inst_three += smpleon_ticker - real_irq_entry;
293
294 local_flush_cache_all();
295 local_irq_restore(flags);
296 }
297# endif
298
Konrad Eisele5213a782009-08-17 00:13:29 +0000299 if (leon3_gptimer_regs) {
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000300 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
Konrad Eisele5213a782009-08-17 00:13:29 +0000301 LEON3_GPTIMER_EN |
302 LEON3_GPTIMER_RL |
303 LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
Konrad Eisele84017072009-08-31 22:08:13 +0000304
305#ifdef CONFIG_SMP
Daniel Hellstrom2791c1a2011-01-04 01:41:32 +0000306 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
Konrad Eisele84017072009-08-31 22:08:13 +0000307 LEON3_GPTIMER_EN |
308 LEON3_GPTIMER_RL |
309 LEON3_GPTIMER_LD |
310 LEON3_GPTIMER_IRQEN);
311#endif
312
Konrad Eisele5213a782009-08-17 00:13:29 +0000313 }
Daniel Hellstrom53aea7c2011-01-04 01:41:29 +0000314 return;
315bad:
316 printk(KERN_ERR "No Timer/irqctrl found\n");
317 BUG();
318 return;
Konrad Eisele5213a782009-08-17 00:13:29 +0000319}
320
321void leon_clear_clock_irq(void)
322{
323}
324
325void leon_load_profile_irq(int cpu, unsigned int limit)
326{
327 BUG();
328}
329
330
331
332
333void __init leon_trans_init(struct device_node *dp)
334{
335 if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
336 struct property *p;
337 p = of_find_property(dp, "mid", (void *)0);
338 if (p) {
339 int mid;
340 dp->name = prom_early_alloc(5 + 1);
341 memcpy(&mid, p->value, p->length);
342 sprintf((char *)dp->name, "cpu%.2d", mid);
343 }
344 }
345}
346
347void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
348
349void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
350{
351 if (prom_amba_init &&
352 strcmp(dp->type, "ambapp") == 0 &&
353 strcmp(dp->name, "ambapp0") == 0) {
354 prom_amba_init(dp, nextp);
355 }
356}
357
Konrad Eisele84017072009-08-31 22:08:13 +0000358#ifdef CONFIG_SMP
359
360void leon_set_cpu_int(int cpu, int level)
361{
362 unsigned long mask;
363 mask = get_irqmask(level);
364 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
365}
366
367static void leon_clear_ipi(int cpu, int level)
368{
369 unsigned long mask;
370 mask = get_irqmask(level);
371 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
372}
373
374static void leon_set_udt(int cpu)
375{
376}
377
378void leon_clear_profile_irq(int cpu)
379{
380}
381
382void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
383{
384 unsigned long mask, flags, *addr;
385 mask = get_irqmask(irq_nr);
386 local_irq_save(flags);
387 addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
388 LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
389 local_irq_restore(flags);
390}
391
392#endif
393
Konrad Eisele5213a782009-08-17 00:13:29 +0000394void __init leon_init_IRQ(void)
395{
Sam Ravnborg6baa9b22011-04-18 11:25:44 +0000396 sparc_irq_config.init_timers = leon_init_timers;
397 sparc_irq_config.build_device_irq = leon_build_device_irq;
Konrad Eisele5213a782009-08-17 00:13:29 +0000398
399 BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
400 BTFIXUPCALL_NORM);
401 BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
402 BTFIXUPCALL_NOP);
403
404#ifdef CONFIG_SMP
405 BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
406 BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
407 BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
408#endif
409
410}
411
412void __init leon_init(void)
413{
Andres Salomoned418502010-10-10 21:51:25 -0600414 of_pdt_build_more = &leon_node_init;
Konrad Eisele5213a782009-08-17 00:13:29 +0000415}