xref: /openbmc/linux/arch/sparc/kernel/leon_kernel.c (revision b6dcefde)
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/slab.h>
11 #include <linux/of.h>
12 #include <linux/of_platform.h>
13 #include <linux/interrupt.h>
14 #include <linux/of_device.h>
15 
16 #include <asm/oplib.h>
17 #include <asm/timer.h>
18 #include <asm/prom.h>
19 #include <asm/leon.h>
20 #include <asm/leon_amba.h>
21 #include <asm/traps.h>
22 #include <asm/cacheflush.h>
23 
24 #include "prom.h"
25 #include "irq.h"
26 
27 struct leon3_irqctrl_regs_map *leon3_irqctrl_regs; /* interrupt controller base address, initialized by amba_init() */
28 struct leon3_gptimer_regs_map *leon3_gptimer_regs; /* timer controller base address, initialized by amba_init() */
29 struct amba_apb_device leon_percpu_timer_dev[16];
30 
31 int leondebug_irq_disable;
32 int leon_debug_irqout;
33 static int dummy_master_l10_counter;
34 
35 unsigned long leon3_gptimer_irq; /* interrupt controller irq number, initialized by amba_init() */
36 unsigned 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 */
40 int sparc_leon_eirq_get(int eirq, int cpu)
41 {
42 	return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
43 }
44 
45 irqreturn_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 */
52 void 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 
70 static 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 
85 static 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 
95 static 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 
106 void __init leon_init_timers(irq_handler_t counter_fn)
107 {
108 	int irq;
109 
110 	leondebug_irq_disable = 0;
111 	leon_debug_irqout = 0;
112 	master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
113 	dummy_master_l10_counter = 0;
114 
115 	if (leon3_gptimer_regs && leon3_irqctrl_regs) {
116 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].val, 0);
117 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].rld,
118 				      (((1000000 / 100) - 1)));
119 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl, 0);
120 
121 #ifdef CONFIG_SMP
122 		leon_percpu_timer_dev[0].start = (int)leon3_gptimer_regs;
123 		leon_percpu_timer_dev[0].irq = leon3_gptimer_irq+1;
124 
125 		if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
126 		      (1<<LEON3_GPTIMER_SEPIRQ))) {
127 			prom_printf("irq timer not configured with seperate irqs \n");
128 			BUG();
129 		}
130 
131 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].val, 0);
132 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].rld, (((1000000/100) - 1)));
133 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl, 0);
134 # endif
135 
136 	} else {
137 		printk(KERN_ERR "No Timer/irqctrl found\n");
138 		BUG();
139 	}
140 
141 	irq = request_irq(leon3_gptimer_irq,
142 			  counter_fn,
143 			  (IRQF_DISABLED | SA_STATIC_ALLOC), "timer", NULL);
144 
145 	if (irq) {
146 		printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
147 		       LEON_INTERRUPT_TIMER1);
148 		prom_halt();
149 	}
150 
151 # ifdef CONFIG_SMP
152 	{
153 		unsigned long flags;
154 		struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_percpu_timer_dev[0].irq - 1)];
155 
156 		/* For SMP we use the level 14 ticker, however the bootup code
157 		 * has copied the firmwares level 14 vector into boot cpu's
158 		 * trap table, we must fix this now or we get squashed.
159 		 */
160 		local_irq_save(flags);
161 
162 		patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
163 
164 		/* Adjust so that we jump directly to smpleon_ticker */
165 		trap_table->inst_three += smpleon_ticker - real_irq_entry;
166 
167 		local_flush_cache_all();
168 		local_irq_restore(flags);
169 	}
170 # endif
171 
172 	if (leon3_gptimer_regs) {
173 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[0].ctrl,
174 				      LEON3_GPTIMER_EN |
175 				      LEON3_GPTIMER_RL |
176 				      LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
177 
178 #ifdef CONFIG_SMP
179 		LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[1].ctrl,
180 				      LEON3_GPTIMER_EN |
181 				      LEON3_GPTIMER_RL |
182 				      LEON3_GPTIMER_LD |
183 				      LEON3_GPTIMER_IRQEN);
184 #endif
185 
186 	}
187 }
188 
189 void leon_clear_clock_irq(void)
190 {
191 }
192 
193 void leon_load_profile_irq(int cpu, unsigned int limit)
194 {
195 	BUG();
196 }
197 
198 
199 
200 
201 void __init leon_trans_init(struct device_node *dp)
202 {
203 	if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
204 		struct property *p;
205 		p = of_find_property(dp, "mid", (void *)0);
206 		if (p) {
207 			int mid;
208 			dp->name = prom_early_alloc(5 + 1);
209 			memcpy(&mid, p->value, p->length);
210 			sprintf((char *)dp->name, "cpu%.2d", mid);
211 		}
212 	}
213 }
214 
215 void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
216 
217 void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
218 {
219 	if (prom_amba_init &&
220 	    strcmp(dp->type, "ambapp") == 0 &&
221 	    strcmp(dp->name, "ambapp0") == 0) {
222 		prom_amba_init(dp, nextp);
223 	}
224 }
225 
226 #ifdef CONFIG_SMP
227 
228 void leon_set_cpu_int(int cpu, int level)
229 {
230 	unsigned long mask;
231 	mask = get_irqmask(level);
232 	LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
233 }
234 
235 static void leon_clear_ipi(int cpu, int level)
236 {
237 	unsigned long mask;
238 	mask = get_irqmask(level);
239 	LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
240 }
241 
242 static void leon_set_udt(int cpu)
243 {
244 }
245 
246 void leon_clear_profile_irq(int cpu)
247 {
248 }
249 
250 void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
251 {
252 	unsigned long mask, flags, *addr;
253 	mask = get_irqmask(irq_nr);
254 	local_irq_save(flags);
255 	addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
256 	LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
257 	local_irq_restore(flags);
258 }
259 
260 #endif
261 
262 void __init leon_init_IRQ(void)
263 {
264 	sparc_init_timers = leon_init_timers;
265 
266 	BTFIXUPSET_CALL(enable_irq, leon_enable_irq, BTFIXUPCALL_NORM);
267 	BTFIXUPSET_CALL(disable_irq, leon_disable_irq, BTFIXUPCALL_NORM);
268 	BTFIXUPSET_CALL(enable_pil_irq, leon_enable_irq, BTFIXUPCALL_NORM);
269 	BTFIXUPSET_CALL(disable_pil_irq, leon_disable_irq, BTFIXUPCALL_NORM);
270 
271 	BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
272 			BTFIXUPCALL_NORM);
273 	BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
274 			BTFIXUPCALL_NOP);
275 
276 #ifdef CONFIG_SMP
277 	BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
278 	BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
279 	BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
280 #endif
281 
282 }
283 
284 void __init leon_init(void)
285 {
286 	prom_build_more = &leon_node_init;
287 }
288