obio.c revision 1.8 1 1.8 bsh /* $NetBSD: obio.c,v 1.8 2010/03/13 11:26:42 bsh 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.5 nonaka #include <arm/xscale/pxa2x0cpu.h>
46 1.1 bsh #include <arm/xscale/pxa2x0reg.h>
47 1.1 bsh #include <arm/xscale/pxa2x0var.h>
48 1.1 bsh #include <arm/xscale/pxa2x0_gpio.h>
49 1.1 bsh #include <arm/sa11x0/sa11x0_var.h>
50 1.1 bsh #include <evbarm/g42xxeb/g42xxeb_reg.h>
51 1.1 bsh #include <evbarm/g42xxeb/g42xxeb_var.h>
52 1.1 bsh
53 1.1 bsh #include "locators.h"
54 1.1 bsh
55 1.1 bsh /* prototypes */
56 1.8 bsh static int obio_match(device_t, cfdata_t, void *);
57 1.8 bsh static void obio_attach(device_t, device_t, void *);
58 1.8 bsh static int obio_search(device_t, cfdata_t, const int *, void *);
59 1.1 bsh static int obio_print(void *, const char *);
60 1.1 bsh
61 1.1 bsh /* attach structures */
62 1.8 bsh CFATTACH_DECL_NEW(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.6 matt softint_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.6 matt obio_softint(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.7 matt int spl_save = curcpl();
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.8 bsh aprint_normal(" addr 0x%lx", oba->oba_addr);
192 1.1 bsh if (oba->oba_intr > 0)
193 1.8 bsh aprint_normal(" 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.8 bsh obio_attach(device_t parent, device_t self, void *aux)
205 1.1 bsh {
206 1.8 bsh struct obio_softc *sc = device_private(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.8 bsh sc->sc_dev = self;
213 1.8 bsh
214 1.1 bsh /* tweak memory access timing for CS3.
215 1.1 bsh the value set by redboot is too slow */
216 1.1 bsh if (bus_space_map(iot, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE, 0,
217 1.1 bsh &sc->sc_memctl_ioh))
218 1.1 bsh goto fail;
219 1.1 bsh bus_space_write_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1,
220 1.1 bsh (0xffff & bus_space_read_4(iot, sc->sc_memctl_ioh, MEMCTL_MSC1))
221 1.1 bsh | (0x6888 << 16));
222 1.1 bsh
223 1.1 bsh /* Map on-board FPGA registers */
224 1.1 bsh sc->sc_iot = iot;
225 1.1 bsh if (bus_space_map(iot, G42XXEB_PLDREG_BASE, G42XXEB_PLDREG_SIZE,
226 1.1 bsh 0, &(sc->sc_obioreg_ioh)))
227 1.1 bsh goto fail;
228 1.1 bsh
229 1.1 bsh /*
230 1.1 bsh * Mask all interrupts.
231 1.1 bsh * They are later unmasked at each device's attach routine.
232 1.1 bsh */
233 1.1 bsh sc->sc_intr_mask = 0xffff;
234 1.1 bsh bus_space_write_2(iot, sc->sc_obioreg_ioh, G42XXEB_INTMASK,
235 1.1 bsh sc->sc_intr_mask );
236 1.1 bsh
237 1.1 bsh #if 0
238 1.1 bsh sc->sc_intr = 8; /* GPIO0 */
239 1.1 bsh #endif
240 1.1 bsh sc->sc_intr_pending = 0;
241 1.1 bsh
242 1.1 bsh for (i=0; i < G42XXEB_N_INTS; ++i) {
243 1.1 bsh sc->sc_handler[i].func = obio_spurious;
244 1.1 bsh sc->sc_handler[i].arg = (void *)i;
245 1.1 bsh }
246 1.1 bsh
247 1.1 bsh obio_peripheral_reset(sc, 1, 0);
248 1.1 bsh
249 1.1 bsh /*
250 1.1 bsh * establish interrupt handler.
251 1.1 bsh * level is very high to allow high priority sub-interrupts.
252 1.1 bsh */
253 1.1 bsh sc->sc_ipl = IPL_AUDIO;
254 1.1 bsh sc->sc_ih = pxa2x0_gpio_intr_establish(0, IST_EDGE_FALLING, sc->sc_ipl,
255 1.1 bsh obio_intr, sc);
256 1.6 matt sc->sc_si = softint_establish(SOFTINT_NET, obio_softint, sc);
257 1.1 bsh
258 1.1 bsh reg = bus_space_read_2(iot, sc->sc_obioreg_ioh, G42XXEB_PLDVER);
259 1.1 bsh aprint_normal(": board %d version %x\n", reg>>8, reg & 0xff);
260 1.1 bsh
261 1.1 bsh /*
262 1.1 bsh * Attach each devices
263 1.1 bsh */
264 1.2 drochner config_search_ia(obio_search, self, "obio", NULL);
265 1.1 bsh return;
266 1.1 bsh
267 1.1 bsh fail:
268 1.8 bsh aprint_error( "%s: can't map FPGA registers\n", self->dv_xname );
269 1.1 bsh }
270 1.1 bsh
271 1.1 bsh int
272 1.8 bsh obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
273 1.1 bsh {
274 1.8 bsh struct obio_softc *sc = device_private(parent);
275 1.1 bsh struct obio_attach_args oba;
276 1.1 bsh
277 1.1 bsh oba.oba_sc = sc;
278 1.1 bsh oba.oba_iot = sc->sc_iot;
279 1.1 bsh oba.oba_addr = cf->cf_loc[OBIOCF_ADDR];
280 1.1 bsh oba.oba_intr = cf->cf_loc[OBIOCF_INTR];
281 1.1 bsh
282 1.1 bsh if (config_match(parent, cf, &oba) > 0)
283 1.1 bsh config_attach(parent, cf, &oba, obio_print);
284 1.1 bsh
285 1.1 bsh return 0;
286 1.1 bsh }
287 1.1 bsh
288 1.1 bsh void *
289 1.1 bsh obio_intr_establish(struct obio_softc *sc, int irq, int ipl,
290 1.1 bsh int type, int (*func)(void *), void *arg)
291 1.1 bsh {
292 1.1 bsh int save;
293 1.1 bsh int regidx, sft;
294 1.1 bsh uint16_t reg;
295 1.1 bsh static const uint8_t ist_code[] = {
296 1.1 bsh 0,
297 1.1 bsh G42XXEB_INT_EDGE_FALLING, /* pulse */
298 1.1 bsh G42XXEB_INT_EDGE_FALLING, /* IST_EDGE */
299 1.1 bsh G42XXEB_INT_LEVEL_LOW, /* IST_LEVEL */
300 1.1 bsh G42XXEB_INT_LEVEL_HIGH, /* IST_LEVEL_HIGH */
301 1.1 bsh G42XXEB_INT_EDGE_RISING, /* IST_EDGE_RISING */
302 1.1 bsh G42XXEB_INT_EDGE_BOTH, /* IST_EDGE_BOTH */
303 1.1 bsh };
304 1.1 bsh
305 1.1 bsh if (irq < 0 || G42XXEB_N_INTS <= irq)
306 1.1 bsh panic("Bad irq no. for obio (%d)", irq);
307 1.1 bsh
308 1.1 bsh if (type < 0 || IST_EDGE_BOTH < type)
309 1.1 bsh panic("Bad interrupt type for obio (%d)", type);
310 1.1 bsh
311 1.1 bsh regidx = G42XXEB_INTCNTL;
312 1.1 bsh sft = 3 * irq;
313 1.1 bsh if (irq >= 5) {
314 1.1 bsh regidx = G42XXEB_INTCNTH;
315 1.1 bsh sft -= 3*5;
316 1.1 bsh }
317 1.1 bsh
318 1.1 bsh save = disable_interrupts(I32_bit);
319 1.1 bsh
320 1.1 bsh sc->sc_handler[irq].func = func;
321 1.1 bsh sc->sc_handler[irq].arg = arg;
322 1.1 bsh sc->sc_handler[irq].level = ipl;
323 1.1 bsh
324 1.1 bsh /* set interrupt type */
325 1.1 bsh reg = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx);
326 1.1 bsh bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx,
327 1.1 bsh (reg & ~(7<<sft)) | (ist_code[type] << sft));
328 1.1 bsh
329 1.1 bsh #ifdef DEBUG
330 1.1 bsh printf("INTCTL=%x,%x\n",
331 1.1 bsh bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTL),
332 1.1 bsh bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTH));
333 1.1 bsh #endif
334 1.1 bsh
335 1.1 bsh sc->sc_intr_mask &= ~(1U << irq);
336 1.1 bsh obio_update_intrmask(sc);
337 1.1 bsh
338 1.1 bsh restore_interrupts(save);
339 1.1 bsh
340 1.1 bsh #if 0
341 1.1 bsh if (ipl > sc->sc_ipl) {
342 1.1 bsh pxa2x0_update_intr_masks(sc->sc_intr, ipl);
343 1.1 bsh sc->sc_ipl = ipl;
344 1.1 bsh }
345 1.1 bsh #endif
346 1.1 bsh
347 1.1 bsh return &sc->sc_handler[irq];
348 1.1 bsh }
349 1.1 bsh
350 1.1 bsh void
351 1.1 bsh obio_intr_disestablish(struct obio_softc *sc, int irq, int (* func)(void *))
352 1.1 bsh {
353 1.1 bsh int error = 0;
354 1.1 bsh int save;
355 1.1 bsh
356 1.1 bsh save = disable_interrupts(I32_bit);
357 1.1 bsh
358 1.1 bsh if (sc->sc_handler[irq].func != func)
359 1.1 bsh error = 1;
360 1.1 bsh else {
361 1.1 bsh sc->sc_handler[irq].func = obio_spurious;
362 1.1 bsh sc->sc_handler[irq].level = IPL_NONE;
363 1.1 bsh
364 1.1 bsh sc->sc_intr_pending &= ~(1U << irq);
365 1.1 bsh sc->sc_intr_mask |= (1U << irq);
366 1.1 bsh obio_update_intrmask(sc);
367 1.1 bsh }
368 1.1 bsh
369 1.1 bsh restore_interrupts(save);
370 1.1 bsh
371 1.1 bsh if (error)
372 1.1 bsh aprint_error("%s: bad intr_disestablish\n",
373 1.8 bsh device_xname(sc->sc_dev));
374 1.1 bsh }
375 1.1 bsh
376 1.1 bsh void
377 1.1 bsh obio_intr_mask(struct obio_softc *sc, struct obio_handler *ih)
378 1.1 bsh {
379 1.1 bsh int irqno;
380 1.1 bsh int save;
381 1.1 bsh
382 1.1 bsh irqno = ih - sc->sc_handler;
383 1.1 bsh #ifdef DIAGNOSTIC
384 1.1 bsh if (ih == NULL || ih->func==NULL || irqno < 0 ||
385 1.1 bsh irqno >= G42XXEB_N_INTS)
386 1.1 bsh panic("Bad arg for obio_intr_mask");
387 1.1 bsh #endif
388 1.1 bsh
389 1.1 bsh save = disable_interrupts(I32_bit);
390 1.1 bsh sc->sc_intr_mask |= 1U<<irqno;
391 1.1 bsh obio_update_intrmask(sc);
392 1.1 bsh restore_interrupts(save);
393 1.1 bsh }
394 1.1 bsh
395 1.1 bsh void
396 1.1 bsh obio_intr_unmask(struct obio_softc *sc, struct obio_handler *ih)
397 1.1 bsh {
398 1.1 bsh int irqno;
399 1.1 bsh int save;
400 1.1 bsh
401 1.1 bsh irqno = ih - sc->sc_handler;
402 1.1 bsh #ifdef DIAGNOSTIC
403 1.1 bsh if (ih == NULL || ih->func==NULL || irqno < 0 ||
404 1.1 bsh irqno >= G42XXEB_N_INTS)
405 1.1 bsh panic("Bad arg for obio_intr_unmask");
406 1.1 bsh #endif
407 1.1 bsh
408 1.1 bsh save = disable_interrupts(I32_bit);
409 1.1 bsh sc->sc_intr_mask &= ~(1U<<irqno);
410 1.1 bsh obio_update_intrmask(sc);
411 1.1 bsh restore_interrupts(save);
412 1.1 bsh }
413 1.1 bsh
414 1.1 bsh void
415 1.1 bsh obio_peripheral_reset(struct obio_softc *bsc, int no, int onoff)
416 1.1 bsh {
417 1.1 bsh uint16_t reg;
418 1.1 bsh
419 1.1 bsh reg = bus_space_read_2(bsc->sc_iot, bsc->sc_obioreg_ioh,
420 1.1 bsh G42XXEB_RST);
421 1.1 bsh bus_space_write_2(bsc->sc_iot, bsc->sc_obioreg_ioh, G42XXEB_RST,
422 1.1 bsh onoff ? (reg & ~RST_EXT(no)) : (reg | RST_EXT(no)));
423 1.1 bsh }
424 1.1 bsh
425