No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / evbarm / g42xxeb / obio.c
blob267b11d98c7c61df7d94dbb107f160e7683893e0
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2002, 2003, 2005 Genetec corp. All rights reserved.
5 * Written by Hiroyuki Bessho for Genetec corp.
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.
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.
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>
39 #include <machine/cpu.h>
40 #include <machine/bus.h>
41 #include <machine/intr.h>
42 #include <arm/cpufunc.h>
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>
53 #include "locators.h"
55 /* prototypes */
56 static int obio_match(struct device *, struct cfdata *, void *);
57 static void obio_attach(struct device *, struct device *, void *);
58 static int obio_search(struct device *, struct cfdata *,
59 const int *, void *);
60 static int obio_print(void *, const char *);
62 /* attach structures */
63 CFATTACH_DECL(obio, sizeof(struct obio_softc), obio_match, obio_attach,
64 NULL, NULL);
66 static int
67 obio_spurious(void *arg)
69 int irqno = (int)arg;
71 printf("Spurious interrupt %d on On-board peripheral", irqno);
72 return 1;
77 * interrupt handler for GPIO0 (on-board peripherals)
79 * On G4250ebx, 10 interrupts are ORed through on-board logic,
80 * and routed to GPIO0 of PXA250 processor.
82 static int
83 obio_intr(void *arg)
85 int irqno, pending;
86 struct obio_softc *sc = (struct obio_softc *)arg;
87 int n=0;
89 #define get_pending(sc) \
90 (bus_space_read_2( sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTSTS1) \
91 & ~(sc->sc_intr_pending|sc->sc_intr_mask))
93 #ifdef DEBUG
94 printf("obio_intr: pend=%x, mask=%x, pend=%x, mask=%x\n",
95 bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTSTS1),
96 bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTMASK),
97 sc->sc_intr_pending,
98 sc->sc_intr_mask);
99 #endif
101 for (pending = get_pending(sc);
102 (irqno = find_first_bit(pending)) >= 0;
103 pending = get_pending(sc)) {
105 /* reset pending bit */
106 bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh,
107 G42XXEB_INTSTS1, ~(1<<irqno));
109 #if 0
110 if (sc->sc_handler[irqno].level > saved_spl_level) {
111 int spl_save = _splraise(sc->sc_handler[irqno].level);
112 (* sc->sc_handler[irqno].func)(
113 sc->sc_handler[irqno].arg);
114 splx(spl_save);
116 else
117 #endif
119 int psw = disable_interrupts(I32_bit); /* XXX */
121 /* mask this interrupt until software
122 interrupt is handled. */
123 sc->sc_intr_pending |= (1U<<irqno);
124 obio_update_intrmask(sc);
126 restore_interrupts(psw);
127 ++n;
129 #ifdef DIAGNOSTIC
130 if (n > 1000)
131 panic("obio_intr: stayed too long");
132 #endif
135 if (n > 0) {
136 /* handle it later */
137 softint_schedule(sc->sc_si);
140 /* GPIO interrupt is edge triggered. make a pulse
141 to let Cotulla notice when other interrupts are
142 still pending */
143 bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh,
144 G42XXEB_INTMASK, 0xffff);
145 obio_update_intrmask(sc);
147 return 1;
150 static void
151 obio_softint(void *arg)
153 struct obio_softc *sc = (struct obio_softc *)arg;
154 int irqno;
155 int spl_save = curcpl();
156 int psw;
158 psw = disable_interrupts(I32_bit);
159 while ((irqno = find_first_bit(sc->sc_intr_pending)) >= 0) {
160 sc->sc_intr_pending &= ~(1U<<irqno);
162 restore_interrupts(psw);
164 _splraise(sc->sc_handler[irqno].level);
165 (* sc->sc_handler[irqno].func)(
166 sc->sc_handler[irqno].arg);
167 splx(spl_save);
169 psw = disable_interrupts(I32_bit);
172 /* assert(sc->sc_intr_pending==0) */
174 bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh,
175 G42XXEB_INTMASK, 0xffff);
176 obio_update_intrmask(sc);
178 restore_interrupts(psw);
182 * int obio_print(void *aux, const char *name)
183 * print configuration info for children
186 static int
187 obio_print(void *aux, const char *name)
189 struct obio_attach_args *oba = (struct obio_attach_args*)aux;
191 if (oba->oba_addr != OBIOCF_ADDR_DEFAULT)
192 printf(" addr 0x%lx", oba->oba_addr);
193 if (oba->oba_intr > 0)
194 printf(" intr %d", oba->oba_intr);
195 return (UNCONF);
199 obio_match(struct device *parent, struct cfdata *match, void *aux)
201 return 1;
204 void
205 obio_attach(struct device *parent, struct device *self, void *aux)
207 struct obio_softc *sc = (struct obio_softc*)self;
208 struct sa11x0_attach_args *sa = (struct sa11x0_attach_args *)aux;
209 bus_space_tag_t iot = sa->sa_iot;
210 int i;
211 uint16_t reg;
213 /* tweak memory access timing for CS3.
214 the value set by redboot is too slow */
215 if (bus_space_map(iot, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE, 0,
216 &sc->sc_memctl_ioh))
217 goto fail;
218 bus_space_write_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1,
219 (0xffff & bus_space_read_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1))
220 | (0x6888 << 16));
222 /* Map on-board FPGA registers */
223 sc->sc_iot = iot;
224 if (bus_space_map(iot, G42XXEB_PLDREG_BASE, G42XXEB_PLDREG_SIZE,
225 0, &(sc->sc_obioreg_ioh)))
226 goto fail;
229 * Mask all interrupts.
230 * They are later unmasked at each device's attach routine.
232 sc->sc_intr_mask = 0xffff;
233 bus_space_write_2(iot, sc->sc_obioreg_ioh, G42XXEB_INTMASK,
234 sc->sc_intr_mask );
236 #if 0
237 sc->sc_intr = 8; /* GPIO0 */
238 #endif
239 sc->sc_intr_pending = 0;
241 for (i=0; i < G42XXEB_N_INTS; ++i) {
242 sc->sc_handler[i].func = obio_spurious;
243 sc->sc_handler[i].arg = (void *)i;
246 obio_peripheral_reset(sc, 1, 0);
249 * establish interrupt handler.
250 * level is very high to allow high priority sub-interrupts.
252 sc->sc_ipl = IPL_AUDIO;
253 sc->sc_ih = pxa2x0_gpio_intr_establish(0, IST_EDGE_FALLING, sc->sc_ipl,
254 obio_intr, sc);
255 sc->sc_si = softint_establish(SOFTINT_NET, obio_softint, sc);
257 reg = bus_space_read_2(iot, sc->sc_obioreg_ioh, G42XXEB_PLDVER);
258 aprint_normal(": board %d version %x\n", reg>>8, reg & 0xff);
261 * Attach each devices
263 config_search_ia(obio_search, self, "obio", NULL);
264 return;
266 fail:
267 printf( "%s: can't map FPGA registers\n", self->dv_xname );
271 obio_search(struct device *parent, struct cfdata *cf,
272 const int *ldesc, void *aux)
274 struct obio_softc *sc = (struct obio_softc *)parent;
275 struct obio_attach_args oba;
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];
282 if (config_match(parent, cf, &oba) > 0)
283 config_attach(parent, cf, &oba, obio_print);
285 return 0;
288 void *
289 obio_intr_establish(struct obio_softc *sc, int irq, int ipl,
290 int type, int (*func)(void *), void *arg)
292 int save;
293 int regidx, sft;
294 uint16_t reg;
295 static const uint8_t ist_code[] = {
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 */
305 if (irq < 0 || G42XXEB_N_INTS <= irq)
306 panic("Bad irq no. for obio (%d)", irq);
308 if (type < 0 || IST_EDGE_BOTH < type)
309 panic("Bad interrupt type for obio (%d)", type);
311 regidx = G42XXEB_INTCNTL;
312 sft = 3 * irq;
313 if (irq >= 5) {
314 regidx = G42XXEB_INTCNTH;
315 sft -= 3*5;
318 save = disable_interrupts(I32_bit);
320 sc->sc_handler[irq].func = func;
321 sc->sc_handler[irq].arg = arg;
322 sc->sc_handler[irq].level = ipl;
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));
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
335 sc->sc_intr_mask &= ~(1U << irq);
336 obio_update_intrmask(sc);
338 restore_interrupts(save);
340 #if 0
341 if (ipl > sc->sc_ipl) {
342 pxa2x0_update_intr_masks(sc->sc_intr, ipl);
343 sc->sc_ipl = ipl;
345 #endif
347 return &sc->sc_handler[irq];
350 void
351 obio_intr_disestablish(struct obio_softc *sc, int irq, int (* func)(void *))
353 int error = 0;
354 int save;
356 save = disable_interrupts(I32_bit);
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;
364 sc->sc_intr_pending &= ~(1U << irq);
365 sc->sc_intr_mask |= (1U << irq);
366 obio_update_intrmask(sc);
369 restore_interrupts(save);
371 if (error)
372 aprint_error("%s: bad intr_disestablish\n",
373 sc->sc_dev.dv_xname);
376 void
377 obio_intr_mask(struct obio_softc *sc, struct obio_handler *ih)
379 int irqno;
380 int save;
382 irqno = ih - sc->sc_handler;
383 #ifdef DIAGNOSTIC
384 if (ih == NULL || ih->func==NULL || irqno < 0 ||
385 irqno >= G42XXEB_N_INTS)
386 panic("Bad arg for obio_intr_mask");
387 #endif
389 save = disable_interrupts(I32_bit);
390 sc->sc_intr_mask |= 1U<<irqno;
391 obio_update_intrmask(sc);
392 restore_interrupts(save);
395 void
396 obio_intr_unmask(struct obio_softc *sc, struct obio_handler *ih)
398 int irqno;
399 int save;
401 irqno = ih - sc->sc_handler;
402 #ifdef DIAGNOSTIC
403 if (ih == NULL || ih->func==NULL || irqno < 0 ||
404 irqno >= G42XXEB_N_INTS)
405 panic("Bad arg for obio_intr_unmask");
406 #endif
408 save = disable_interrupts(I32_bit);
409 sc->sc_intr_mask &= ~(1U<<irqno);
410 obio_update_intrmask(sc);
411 restore_interrupts(save);
414 void
415 obio_peripheral_reset(struct obio_softc *bsc, int no, int onoff)
417 uint16_t reg;
419 reg = bus_space_read_2(bsc->sc_iot, bsc->sc_obioreg_ioh,
420 G42XXEB_RST);
421 bus_space_write_2(bsc->sc_iot, bsc->sc_obioreg_ioh, G42XXEB_RST,
422 onoff ? (reg & ~RST_EXT(no)) : (reg | RST_EXT(no)));