88ade07f976ce9042eb239d857b2f7cbe1e380f7
[pandora-kernel.git] / arch / sparc / kernel / leon_kernel.c
1 /*
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>
10 #include <linux/of.h>
11 #include <linux/of_platform.h>
12 #include <linux/interrupt.h>
13 #include <linux/of_device.h>
14
15 #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>
20 #include <asm/traps.h>
21 #include <asm/cacheflush.h>
22
23 #include "prom.h"
24 #include "irq.h"
25
26 struct leon3_irqctrl_regs_map *leon3_irqctrl_regs; /* interrupt controller base address */
27 struct leon3_gptimer_regs_map *leon3_gptimer_regs; /* timer controller base address */
28 struct amba_apb_device leon_percpu_timer_dev[16];
29
30 int leondebug_irq_disable;
31 int leon_debug_irqout;
32 static int dummy_master_l10_counter;
33
34 unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
35 unsigned 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 */
39 int sparc_leon_eirq_get(int eirq, int cpu)
40 {
41         return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
42 }
43
44 irqreturn_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 */
51 void 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
69 static 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
84 static 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
94 static 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
105 void __init leon_init_timers(irq_handler_t counter_fn)
106 {
107         int irq;
108         struct device_node *rootnp, *np;
109         struct property *pp;
110         int len;
111         int cpu, icsel;
112
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
118         /*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                 np = of_find_node_by_name(rootnp, "01_00d");
125                 if (!np)
126                         goto bad;
127         }
128         pp = of_find_property(np, "reg", &len);
129         if (!pp)
130                 goto bad;
131         leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
132
133         /* Find GPTIMER Timer Registers base address otherwise bail out. */
134         np = of_find_node_by_name(rootnp, "GAISLER_GPTIMER");
135         if (!np) {
136                 np = of_find_node_by_name(np, "01_011");
137                 if (!np)
138                         goto bad;
139         }
140         pp = of_find_property(np, "reg", &len);
141         if (!pp)
142                 goto bad;
143         leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)pp->value;
144         pp = of_find_property(np, "interrupts", &len);
145         if (!pp)
146                 goto bad;
147         leon3_gptimer_irq = *(unsigned int *)pp->value;
148
149         if (leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq) {
150                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].val, 0);
151                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].rld,
152                                       (((1000000 / HZ) - 1)));
153                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl, 0);
154
155 #ifdef CONFIG_SMP
156                 leon_percpu_timer_dev[0].start = (int)leon3_gptimer_regs;
157                 leon_percpu_timer_dev[0].irq = leon3_gptimer_irq+1;
158
159                 if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
160                       (1<<LEON3_GPTIMER_SEPIRQ))) {
161                         prom_printf("irq timer not configured with separate irqs\n");
162                         BUG();
163                 }
164
165                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].val, 0);
166                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].rld, (((1000000/HZ) - 1)));
167                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl, 0);
168 # endif
169
170                 /*
171                  * The IRQ controller may (if implemented) consist of multiple
172                  * IRQ controllers, each mapped on a 4Kb boundary.
173                  * Each CPU may be routed to different IRQCTRLs, however
174                  * we assume that all CPUs (in SMP system) is routed to the
175                  * same IRQ Controller, and for non-SMP only one IRQCTRL is
176                  * accessed anyway.
177                  * In AMP systems, Linux must run on CPU0 for the time being.
178                  */
179                 cpu = sparc_leon3_cpuid();
180                 icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
181                 icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
182                 leon3_irqctrl_regs += icsel;
183         } else {
184                 goto bad;
185         }
186
187         irq = request_irq(leon3_gptimer_irq,
188                           counter_fn,
189                           (IRQF_DISABLED | SA_STATIC_ALLOC), "timer", NULL);
190
191         if (irq) {
192                 printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
193                        LEON_INTERRUPT_TIMER1);
194                 prom_halt();
195         }
196
197 # ifdef CONFIG_SMP
198         {
199                 unsigned long flags;
200                 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_percpu_timer_dev[0].irq - 1)];
201
202                 /* For SMP we use the level 14 ticker, however the bootup code
203                  * has copied the firmwares level 14 vector into boot cpu's
204                  * trap table, we must fix this now or we get squashed.
205                  */
206                 local_irq_save(flags);
207
208                 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
209
210                 /* Adjust so that we jump directly to smpleon_ticker */
211                 trap_table->inst_three += smpleon_ticker - real_irq_entry;
212
213                 local_flush_cache_all();
214                 local_irq_restore(flags);
215         }
216 # endif
217
218         if (leon3_gptimer_regs) {
219                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl,
220                                       LEON3_GPTIMER_EN |
221                                       LEON3_GPTIMER_RL |
222                                       LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
223
224 #ifdef CONFIG_SMP
225                 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl,
226                                       LEON3_GPTIMER_EN |
227                                       LEON3_GPTIMER_RL |
228                                       LEON3_GPTIMER_LD |
229                                       LEON3_GPTIMER_IRQEN);
230 #endif
231
232         }
233         return;
234 bad:
235         printk(KERN_ERR "No Timer/irqctrl found\n");
236         BUG();
237         return;
238 }
239
240 void leon_clear_clock_irq(void)
241 {
242 }
243
244 void leon_load_profile_irq(int cpu, unsigned int limit)
245 {
246         BUG();
247 }
248
249
250
251
252 void __init leon_trans_init(struct device_node *dp)
253 {
254         if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
255                 struct property *p;
256                 p = of_find_property(dp, "mid", (void *)0);
257                 if (p) {
258                         int mid;
259                         dp->name = prom_early_alloc(5 + 1);
260                         memcpy(&mid, p->value, p->length);
261                         sprintf((char *)dp->name, "cpu%.2d", mid);
262                 }
263         }
264 }
265
266 void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
267
268 void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
269 {
270         if (prom_amba_init &&
271             strcmp(dp->type, "ambapp") == 0 &&
272             strcmp(dp->name, "ambapp0") == 0) {
273                 prom_amba_init(dp, nextp);
274         }
275 }
276
277 #ifdef CONFIG_SMP
278
279 void leon_set_cpu_int(int cpu, int level)
280 {
281         unsigned long mask;
282         mask = get_irqmask(level);
283         LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
284 }
285
286 static void leon_clear_ipi(int cpu, int level)
287 {
288         unsigned long mask;
289         mask = get_irqmask(level);
290         LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
291 }
292
293 static void leon_set_udt(int cpu)
294 {
295 }
296
297 void leon_clear_profile_irq(int cpu)
298 {
299 }
300
301 void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
302 {
303         unsigned long mask, flags, *addr;
304         mask = get_irqmask(irq_nr);
305         local_irq_save(flags);
306         addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
307         LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
308         local_irq_restore(flags);
309 }
310
311 #endif
312
313 void __init leon_init_IRQ(void)
314 {
315         sparc_init_timers = leon_init_timers;
316
317         BTFIXUPSET_CALL(enable_irq, leon_enable_irq, BTFIXUPCALL_NORM);
318         BTFIXUPSET_CALL(disable_irq, leon_disable_irq, BTFIXUPCALL_NORM);
319         BTFIXUPSET_CALL(enable_pil_irq, leon_enable_irq, BTFIXUPCALL_NORM);
320         BTFIXUPSET_CALL(disable_pil_irq, leon_disable_irq, BTFIXUPCALL_NORM);
321
322         BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
323                         BTFIXUPCALL_NORM);
324         BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
325                         BTFIXUPCALL_NOP);
326
327 #ifdef CONFIG_SMP
328         BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
329         BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
330         BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
331 #endif
332
333 }
334
335 void __init leon_init(void)
336 {
337         of_pdt_build_more = &leon_node_init;
338 }