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