xref: /netbsd-src/sys/arch/evbarm/g42xxeb/obio.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: obio.c,v 1.11 2012/10/27 17:17:47 chs Exp $ */
2 
3 /*
4  * Copyright (c) 2002, 2003, 2005  Genetec corp.  All rights reserved.
5  * Written by Hiroyuki Bessho for Genetec corp.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of Genetec corp. may not be used to endorse
16  *    or promote products derived from this software without specific prior
17  *    written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORP.
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/kernel.h>
37 #include <sys/reboot.h>
38 
39 #include <machine/cpu.h>
40 #include <sys/bus.h>
41 #include <machine/intr.h>
42 #include <arm/cpufunc.h>
43 
44 #include <arm/mainbus/mainbus.h>
45 #include <arm/xscale/pxa2x0cpu.h>
46 #include <arm/xscale/pxa2x0reg.h>
47 #include <arm/xscale/pxa2x0var.h>
48 #include <arm/xscale/pxa2x0_gpio.h>
49 #include <arm/sa11x0/sa11x0_var.h>
50 #include <evbarm/g42xxeb/g42xxeb_reg.h>
51 #include <evbarm/g42xxeb/g42xxeb_var.h>
52 
53 #include "locators.h"
54 
55 /* prototypes */
56 static int	obio_match(device_t, cfdata_t, void *);
57 static void	obio_attach(device_t, device_t, void *);
58 static int 	obio_search(device_t, cfdata_t, const int *, void *);
59 static int	obio_print(void *, const char *);
60 
61 /* attach structures */
62 CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc), obio_match, obio_attach,
63     NULL, NULL);
64 
65 static int
66 obio_spurious(void *arg)
67 {
68 	int irqno = (int)arg;
69 
70 	printf("Spurious interrupt %d on On-board peripheral", irqno);
71 	return 1;
72 }
73 
74 
75 /*
76  * interrupt handler for GPIO0 (on-board peripherals)
77  *
78  * On G4250ebx, 10 interrupts are ORed through on-board logic,
79  * and routed to GPIO0 of PXA250 processor.
80  */
81 static int
82 obio_intr(void *arg)
83 {
84 	int irqno, pending;
85 	struct obio_softc *sc = (struct obio_softc *)arg;
86 	int n=0;
87 
88 #define get_pending(sc) \
89 	(bus_space_read_2( sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTSTS1) \
90 	& ~(sc->sc_intr_pending|sc->sc_intr_mask))
91 
92 #ifdef DEBUG
93 	printf("obio_intr: pend=%x, mask=%x, pend=%x, mask=%x\n",
94 	    bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTSTS1),
95 	    bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTMASK),
96 	    sc->sc_intr_pending,
97 	    sc->sc_intr_mask);
98 #endif
99 
100 	for (pending = get_pending(sc);
101 	     (irqno = find_first_bit(pending)) >= 0;
102 	     pending = get_pending(sc)) {
103 
104 		/* reset pending bit */
105 		bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh,
106 		    G42XXEB_INTSTS1, ~(1<<irqno));
107 
108 #if 0
109 		if (sc->sc_handler[irqno].level > saved_spl_level) {
110 			int spl_save = _splraise(sc->sc_handler[irqno].level);
111 			(* sc->sc_handler[irqno].func)(
112 				sc->sc_handler[irqno].arg);
113 			splx(spl_save);
114 		}
115 		else
116 #endif
117 		{
118 			int psw = disable_interrupts(I32_bit); /* XXX */
119 
120 			/* mask this interrupt until software
121 			   interrupt is handled. */
122 			sc->sc_intr_pending |= (1U<<irqno);
123 			obio_update_intrmask(sc);
124 
125 			restore_interrupts(psw);
126 			++n;
127 		}
128 #ifdef DIAGNOSTIC
129 		if (n > 1000)
130 			panic("obio_intr: stayed too long");
131 #endif
132 	}
133 
134 	if (n > 0) {
135 		/* handle it later */
136 		softint_schedule(sc->sc_si);
137 	}
138 
139 	/* GPIO interrupt is edge triggered.  make a pulse
140 	   to let Cotulla notice when other interrupts are
141 	   still pending */
142 	bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh,
143 	    G42XXEB_INTMASK, 0xffff);
144 	obio_update_intrmask(sc);
145 
146 	return 1;
147 }
148 
149 static void
150 obio_softint(void *arg)
151 {
152 	struct obio_softc *sc = (struct obio_softc *)arg;
153 	int irqno;
154 	int spl_save = curcpl();
155 	int psw;
156 
157 	psw = disable_interrupts(I32_bit);
158 	while ((irqno = find_first_bit(sc->sc_intr_pending)) >= 0) {
159 		sc->sc_intr_pending &= ~(1U<<irqno);
160 
161 		restore_interrupts(psw);
162 
163 		_splraise(sc->sc_handler[irqno].level);
164 		(* sc->sc_handler[irqno].func)(
165 			sc->sc_handler[irqno].arg);
166 		splx(spl_save);
167 
168 		psw = disable_interrupts(I32_bit);
169 	}
170 
171 	/* assert(sc->sc_intr_pending==0) */
172 
173 	bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh,
174 	    G42XXEB_INTMASK, 0xffff);
175 	obio_update_intrmask(sc);
176 
177 	restore_interrupts(psw);
178 }
179 
180 /*
181  * int obio_print(void *aux, const char *name)
182  * print configuration info for children
183  */
184 
185 static int
186 obio_print(void *aux, const char *name)
187 {
188 	struct obio_attach_args *oba = (struct obio_attach_args*)aux;
189 
190 	if (oba->oba_addr != OBIOCF_ADDR_DEFAULT)
191                 aprint_normal(" addr 0x%lx", oba->oba_addr);
192         if (oba->oba_intr > 0)
193                 aprint_normal(" intr %d", oba->oba_intr);
194         return (UNCONF);
195 }
196 
197 int
198 obio_match(device_t parent, cfdata_t match, void *aux)
199 {
200 	return 1;
201 }
202 
203 void
204 obio_attach(device_t parent, device_t self, void *aux)
205 {
206 	struct obio_softc *sc = device_private(self);
207 	struct sa11x0_attach_args *sa = aux;
208 	bus_space_tag_t iot = sa->sa_iot;
209 	int i;
210 	uint16_t reg;
211 
212 	sc->sc_dev = self;
213 
214 	/* tweak memory access timing for CS3.
215 	   the value set by redboot is too slow */
216 	if (bus_space_map(iot, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE, 0,
217 		&sc->sc_memctl_ioh))
218 		goto fail;
219 	bus_space_write_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1,
220 	    (0xffff & bus_space_read_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1))
221 	    | (0x6888 << 16));
222 
223 	/* Map on-board FPGA registers */
224 	sc->sc_iot = iot;
225 	if (bus_space_map(iot, G42XXEB_PLDREG_BASE, G42XXEB_PLDREG_SIZE,
226 	    0, &(sc->sc_obioreg_ioh)))
227 		goto fail;
228 
229 	/*
230 	 *  Mask all interrupts.
231 	 *  They are later unmasked at each device's attach routine.
232 	 */
233 	sc->sc_intr_mask = 0xffff;
234 	bus_space_write_2(iot, sc->sc_obioreg_ioh, G42XXEB_INTMASK,
235 	    sc->sc_intr_mask );
236 
237 #if 0
238 	sc->sc_intr = 8;		/* GPIO0 */
239 #endif
240 	sc->sc_intr_pending = 0;
241 
242 	for (i=0; i < G42XXEB_N_INTS; ++i) {
243 		sc->sc_handler[i].func = obio_spurious;
244 		sc->sc_handler[i].arg = (void *)i;
245 	}
246 
247 	obio_peripheral_reset(sc, 1, 0);
248 
249 	/*
250 	 * establish interrupt handler.
251 	 * level is very high to allow high priority sub-interrupts.
252 	 */
253 	sc->sc_ipl = IPL_AUDIO;
254 	sc->sc_ih = pxa2x0_gpio_intr_establish(0, IST_EDGE_FALLING, sc->sc_ipl,
255 	    obio_intr, sc);
256 	sc->sc_si = softint_establish(SOFTINT_NET, obio_softint, sc);
257 
258 	reg = bus_space_read_2(iot, sc->sc_obioreg_ioh, G42XXEB_PLDVER);
259 	aprint_normal(": board %d version %x\n", reg>>8, reg & 0xff);
260 
261 	/*
262 	 *  Attach each devices
263 	 */
264 	config_search_ia(obio_search, self, "obio", NULL);
265 	return;
266 
267  fail:
268 	aprint_error_dev(self, "can't map FPGA registers\n");
269 }
270 
271 int
272 obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
273 {
274 	struct obio_softc *sc = device_private(parent);
275 	struct obio_attach_args oba;
276 
277 	oba.oba_sc = sc;
278         oba.oba_iot = sc->sc_iot;
279         oba.oba_addr = cf->cf_loc[OBIOCF_ADDR];
280         oba.oba_intr = cf->cf_loc[OBIOCF_INTR];
281 
282         if (config_match(parent, cf, &oba) > 0)
283                 config_attach(parent, cf, &oba, obio_print);
284 
285         return 0;
286 }
287 
288 void *
289 obio_intr_establish(struct obio_softc *sc, int irq, int ipl,
290     int type, int (*func)(void *), void *arg)
291 {
292 	int save;
293 	int regidx, sft;
294 	uint16_t reg;
295 	static const uint8_t ist_code[] = {
296 		0,
297 		G42XXEB_INT_EDGE_FALLING, /* pulse */
298 		G42XXEB_INT_EDGE_FALLING, /* IST_EDGE */
299 		G42XXEB_INT_LEVEL_LOW,	   /* IST_LEVEL */
300 		G42XXEB_INT_LEVEL_HIGH,   /* IST_LEVEL_HIGH */
301 		G42XXEB_INT_EDGE_RISING,  /* IST_EDGE_RISING */
302 		G42XXEB_INT_EDGE_BOTH,	   /* IST_EDGE_BOTH */
303 	};
304 
305 	if (irq < 0 || G42XXEB_N_INTS <= irq)
306 		panic("Bad irq no. for obio (%d)", irq);
307 
308 	if (type < 0 || IST_EDGE_BOTH < type)
309 		panic("Bad interrupt type for obio (%d)", type);
310 
311 	regidx = G42XXEB_INTCNTL;
312 	sft = 3 * irq;
313 	if (irq >= 5) {
314 		regidx = G42XXEB_INTCNTH;
315 		sft -= 3*5;
316 	}
317 
318 	save = disable_interrupts(I32_bit);
319 
320 	sc->sc_handler[irq].func = func;
321 	sc->sc_handler[irq].arg = arg;
322 	sc->sc_handler[irq].level = ipl;
323 
324 	/* set interrupt type */
325 	reg = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx);
326 	bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx,
327 	    (reg & ~(7<<sft)) | (ist_code[type] << sft));
328 
329 #ifdef DEBUG
330 	printf("INTCTL=%x,%x\n",
331 	    bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTL),
332 	    bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTH));
333 #endif
334 
335 	sc->sc_intr_mask &= ~(1U << irq);
336 	obio_update_intrmask(sc);
337 
338 	restore_interrupts(save);
339 
340 #if 0
341 	if (ipl > sc->sc_ipl) {
342 		pxa2x0_update_intr_masks(sc->sc_intr, ipl);
343 		sc->sc_ipl = ipl;
344 	}
345 #endif
346 
347 	return &sc->sc_handler[irq];
348 }
349 
350 void
351 obio_intr_disestablish(struct obio_softc *sc, int irq, int (* func)(void *))
352 {
353 	int error = 0;
354 	int save;
355 
356 	save = disable_interrupts(I32_bit);
357 
358 	if (sc->sc_handler[irq].func != func)
359 		error = 1;
360 	else {
361 		sc->sc_handler[irq].func = obio_spurious;
362 		sc->sc_handler[irq].level = IPL_NONE;
363 
364 		sc->sc_intr_pending &= ~(1U << irq);
365 		sc->sc_intr_mask |= (1U << irq);
366 		obio_update_intrmask(sc);
367 	}
368 
369 	restore_interrupts(save);
370 
371 	if (error)
372 		aprint_error_dev(sc->sc_dev, "bad intr_disestablish\n");
373 }
374 
375 void
376 obio_intr_mask(struct obio_softc *sc, struct obio_handler *ih)
377 {
378 	int irqno;
379 	int save;
380 
381 	irqno = ih - sc->sc_handler;
382 #ifdef DIAGNOSTIC
383 	if (ih == NULL || ih->func==NULL || irqno < 0 ||
384 	    irqno >= G42XXEB_N_INTS)
385 		panic("Bad arg for obio_intr_mask");
386 #endif
387 
388 	save = disable_interrupts(I32_bit);
389 	sc->sc_intr_mask |= 1U<<irqno;
390 	obio_update_intrmask(sc);
391 	restore_interrupts(save);
392 }
393 
394 void
395 obio_intr_unmask(struct obio_softc *sc, struct obio_handler *ih)
396 {
397 	int irqno;
398 	int save;
399 
400 	irqno = ih - sc->sc_handler;
401 #ifdef DIAGNOSTIC
402 	if (ih == NULL || ih->func==NULL || irqno < 0 ||
403 	    irqno >= G42XXEB_N_INTS)
404 		panic("Bad arg for obio_intr_unmask");
405 #endif
406 
407 	save = disable_interrupts(I32_bit);
408 	sc->sc_intr_mask &= ~(1U<<irqno);
409 	obio_update_intrmask(sc);
410 	restore_interrupts(save);
411 }
412 
413 void
414 obio_peripheral_reset(struct obio_softc *bsc, int no, int onoff)
415 {
416 	uint16_t reg;
417 
418 	reg = bus_space_read_2(bsc->sc_iot, bsc->sc_obioreg_ioh,
419 	    G42XXEB_RST);
420 	bus_space_write_2(bsc->sc_iot, bsc->sc_obioreg_ioh, G42XXEB_RST,
421 	    onoff ?  (reg & ~RST_EXT(no)) : (reg | RST_EXT(no)));
422 }
423 
424