obio.c revision 1.12 1 /* $NetBSD: obio.c,v 1.12 2021/04/24 23:36:32 thorpej 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(self, NULL,
265 CFARG_SEARCH, obio_search,
266 CFARG_EOL);
267 return;
268
269 fail:
270 aprint_error_dev(self, "can't map FPGA registers\n");
271 }
272
273 int
274 obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
275 {
276 struct obio_softc *sc = device_private(parent);
277 struct obio_attach_args oba;
278
279 oba.oba_sc = sc;
280 oba.oba_iot = sc->sc_iot;
281 oba.oba_addr = cf->cf_loc[OBIOCF_ADDR];
282 oba.oba_intr = cf->cf_loc[OBIOCF_INTR];
283
284 if (config_probe(parent, cf, &oba))
285 config_attach(parent, cf, &oba, obio_print, CFARG_EOL);
286
287 return 0;
288 }
289
290 void *
291 obio_intr_establish(struct obio_softc *sc, int irq, int ipl,
292 int type, int (*func)(void *), void *arg)
293 {
294 int save;
295 int regidx, sft;
296 uint16_t reg;
297 static const uint8_t ist_code[] = {
298 0,
299 G42XXEB_INT_EDGE_FALLING, /* pulse */
300 G42XXEB_INT_EDGE_FALLING, /* IST_EDGE */
301 G42XXEB_INT_LEVEL_LOW, /* IST_LEVEL */
302 G42XXEB_INT_LEVEL_HIGH, /* IST_LEVEL_HIGH */
303 G42XXEB_INT_EDGE_RISING, /* IST_EDGE_RISING */
304 G42XXEB_INT_EDGE_BOTH, /* IST_EDGE_BOTH */
305 };
306
307 if (irq < 0 || G42XXEB_N_INTS <= irq)
308 panic("Bad irq no. for obio (%d)", irq);
309
310 if (type < 0 || IST_EDGE_BOTH < type)
311 panic("Bad interrupt type for obio (%d)", type);
312
313 regidx = G42XXEB_INTCNTL;
314 sft = 3 * irq;
315 if (irq >= 5) {
316 regidx = G42XXEB_INTCNTH;
317 sft -= 3*5;
318 }
319
320 save = disable_interrupts(I32_bit);
321
322 sc->sc_handler[irq].func = func;
323 sc->sc_handler[irq].arg = arg;
324 sc->sc_handler[irq].level = ipl;
325
326 /* set interrupt type */
327 reg = bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx);
328 bus_space_write_2(sc->sc_iot, sc->sc_obioreg_ioh, regidx,
329 (reg & ~(7<<sft)) | (ist_code[type] << sft));
330
331 #ifdef DEBUG
332 printf("INTCTL=%x,%x\n",
333 bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTL),
334 bus_space_read_2(sc->sc_iot, sc->sc_obioreg_ioh, G42XXEB_INTCNTH));
335 #endif
336
337 sc->sc_intr_mask &= ~(1U << irq);
338 obio_update_intrmask(sc);
339
340 restore_interrupts(save);
341
342 #if 0
343 if (ipl > sc->sc_ipl) {
344 pxa2x0_update_intr_masks(sc->sc_intr, ipl);
345 sc->sc_ipl = ipl;
346 }
347 #endif
348
349 return &sc->sc_handler[irq];
350 }
351
352 void
353 obio_intr_disestablish(struct obio_softc *sc, int irq, int (* func)(void *))
354 {
355 int error = 0;
356 int save;
357
358 save = disable_interrupts(I32_bit);
359
360 if (sc->sc_handler[irq].func != func)
361 error = 1;
362 else {
363 sc->sc_handler[irq].func = obio_spurious;
364 sc->sc_handler[irq].level = IPL_NONE;
365
366 sc->sc_intr_pending &= ~(1U << irq);
367 sc->sc_intr_mask |= (1U << irq);
368 obio_update_intrmask(sc);
369 }
370
371 restore_interrupts(save);
372
373 if (error)
374 aprint_error_dev(sc->sc_dev, "bad intr_disestablish\n");
375 }
376
377 void
378 obio_intr_mask(struct obio_softc *sc, struct obio_handler *ih)
379 {
380 int irqno;
381 int save;
382
383 irqno = ih - sc->sc_handler;
384 #ifdef DIAGNOSTIC
385 if (ih == NULL || ih->func==NULL || irqno < 0 ||
386 irqno >= G42XXEB_N_INTS)
387 panic("Bad arg for obio_intr_mask");
388 #endif
389
390 save = disable_interrupts(I32_bit);
391 sc->sc_intr_mask |= 1U<<irqno;
392 obio_update_intrmask(sc);
393 restore_interrupts(save);
394 }
395
396 void
397 obio_intr_unmask(struct obio_softc *sc, struct obio_handler *ih)
398 {
399 int irqno;
400 int save;
401
402 irqno = ih - sc->sc_handler;
403 #ifdef DIAGNOSTIC
404 if (ih == NULL || ih->func==NULL || irqno < 0 ||
405 irqno >= G42XXEB_N_INTS)
406 panic("Bad arg for obio_intr_unmask");
407 #endif
408
409 save = disable_interrupts(I32_bit);
410 sc->sc_intr_mask &= ~(1U<<irqno);
411 obio_update_intrmask(sc);
412 restore_interrupts(save);
413 }
414
415 void
416 obio_peripheral_reset(struct obio_softc *bsc, int no, int onoff)
417 {
418 uint16_t reg;
419
420 reg = bus_space_read_2(bsc->sc_iot, bsc->sc_obioreg_ioh,
421 G42XXEB_RST);
422 bus_space_write_2(bsc->sc_iot, bsc->sc_obioreg_ioh, G42XXEB_RST,
423 onoff ? (reg & ~RST_EXT(no)) : (reg | RST_EXT(no)));
424 }
425
426