frodo.c revision 1.27.40.1 1 /* $NetBSD: frodo.c,v 1.27.40.1 2008/04/03 12:42:15 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1997 Michael Smith. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 /*
65 * Support for the "Frodo" (a.k.a. "Apollo Utility") chip found
66 * in HP Apollo 9000/4xx workstations, as well as
67 * HP 9000/362 and 9000/382 controllers.
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: frodo.c,v 1.27.40.1 2008/04/03 12:42:15 mjf Exp $");
72
73 #define _HP300_INTR_H_PRIVATE
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 #include <sys/device.h>
80
81 #include <machine/bus.h>
82 #include <machine/cpu.h>
83 #include <machine/intr.h>
84 #include <machine/hp300spu.h>
85
86 #include <hp300/dev/intiovar.h>
87
88 #include <hp300/dev/frodoreg.h>
89 #include <hp300/dev/frodovar.h>
90
91 /*
92 * Description of a Frodo interrupt handler.
93 */
94 struct frodo_interhand {
95 int (*ih_fn)(void *);
96 void *ih_arg;
97 int ih_priority;
98 };
99
100 struct frodo_softc {
101 device_t sc_dev; /* generic device glue */
102 volatile uint8_t *sc_regs; /* register base */
103 struct frodo_interhand sc_intr[FRODO_NINTR]; /* interrupt handlers */
104 int sc_ipl;
105 void *sc_ih; /* out interrupt cookie */
106 int sc_refcnt; /* number of interrupt refs */
107 struct bus_space_tag sc_tag; /* bus space tag */
108 };
109
110 static int frodomatch(device_t, cfdata_t, void *);
111 static void frodoattach(device_t, device_t, void *);
112
113 static int frodoprint(void *, const char *);
114 static int frodosubmatch(device_t, cfdata_t, const int *, void *);
115
116 static int frodointr(void *);
117
118 static void frodo_imask(struct frodo_softc *, uint16_t, uint16_t);
119
120 CFATTACH_DECL_NEW(frodo, sizeof(struct frodo_softc),
121 frodomatch, frodoattach, NULL, NULL);
122
123 static const struct frodo_device frodo_subdevs[] = {
124 { "dnkbd", FRODO_APCI_OFFSET(0), FRODO_INTR_APCI0 },
125 { "com", FRODO_APCI_OFFSET(1), FRODO_INTR_APCI1 },
126 { "com", FRODO_APCI_OFFSET(2), FRODO_INTR_APCI2 },
127 { "com", FRODO_APCI_OFFSET(3), FRODO_INTR_APCI3 },
128 { NULL, 0, 0 }
129 };
130
131 static int
132 frodomatch(device_t parent, cfdata_t cf, void *aux)
133 {
134 struct intio_attach_args *ia = aux;
135 static int frodo_matched = 0;
136
137 /* only allow one instance */
138 if (frodo_matched)
139 return 0;
140
141 if (strcmp(ia->ia_modname, "frodo") != 0)
142 return 0;
143
144 if (badaddr((void *)ia->ia_addr))
145 return 0;
146
147 frodo_matched = 1;
148 return 1;
149 }
150
151 static void
152 frodoattach(device_t parent, device_t self, void *aux)
153 {
154 struct frodo_softc *sc = device_private(self);
155 struct intio_attach_args *ia = aux;
156 bus_space_tag_t bst = &sc->sc_tag;
157 const struct frodo_device *fd;
158 struct frodo_attach_args fa;
159
160 sc->sc_dev = self;
161 sc->sc_regs = (volatile uint8_t *)ia->ia_addr;
162 sc->sc_ipl = ia->ia_ipl;
163
164 if ((FRODO_READ(sc, FRODO_IISR) & FRODO_IISR_SERVICE) == 0)
165 aprint_error(": service mode enabled");
166 aprint_normal("\n");
167
168 /* Initialize bus_space_tag_t for frodo */
169 frodo_init_bus_space(bst);
170
171 /* Clear all of the interrupt handlers. */
172 memset(sc->sc_intr, 0, sizeof(sc->sc_intr));
173 sc->sc_refcnt = 0;
174
175 /*
176 * Disable all of the interrupt lines; we reenable them
177 * as subdevices attach.
178 */
179 frodo_imask(sc, 0, 0xffff);
180
181 /* Clear any pending interrupts. */
182 FRODO_WRITE(sc, FRODO_PIC_PU, 0xff);
183 FRODO_WRITE(sc, FRODO_PIC_PL, 0xff);
184
185 /* Set interrupt polarities. */
186 FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10);
187
188 /* ...and configure for edge triggering. */
189 FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf);
190
191 /*
192 * We defer hooking up our interrupt handler until
193 * a subdevice hooks up theirs.
194 */
195 sc->sc_ih = NULL;
196
197 /* ... and attach subdevices. */
198 for (fd = frodo_subdevs; fd->fd_name != NULL; fd++) {
199 /*
200 * Skip the first serial port if we're not a 425e;
201 * it's mapped to the DCA at select code 9 on all
202 * other models.
203 */
204 if (fd->fd_offset == FRODO_APCI_OFFSET(1) &&
205 mmuid != MMUID_425_E)
206 continue;
207 fa.fa_name = fd->fd_name;
208 fa.fa_bst = bst;
209 fa.fa_base = ia->ia_iobase;
210 fa.fa_offset = fd->fd_offset;
211 fa.fa_line = fd->fd_line;
212 config_found_sm_loc(self, "frodo", NULL, &fa, frodoprint,
213 frodosubmatch);
214 }
215 }
216
217 static int
218 frodosubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
219 {
220 struct frodo_attach_args *fa = aux;
221
222 if (cf->frodocf_offset != FRODO_UNKNOWN_OFFSET &&
223 cf->frodocf_offset != fa->fa_offset)
224 return 0;
225
226 return config_match(parent, cf, aux);
227 }
228
229 static int
230 frodoprint(void *aux, const char *pnp)
231 {
232 struct frodo_attach_args *fa = aux;
233
234 if (pnp)
235 aprint_normal("%s at %s", fa->fa_name, pnp);
236 aprint_normal(" offset 0x%x", fa->fa_offset);
237 return UNCONF;
238 }
239
240 void
241 frodo_intr_establish(device_t frdev, int (*func)(void *), void *arg,
242 int line, int priority)
243 {
244 struct frodo_softc *sc = device_private(frdev);
245 struct hp300_intrhand *ih = sc->sc_ih;
246
247 if (line < 0 || line >= FRODO_NINTR) {
248 aprint_error_dev(frdev, "bad interrupt line %d\n", line);
249 goto lose;
250 }
251 if (sc->sc_intr[line].ih_fn != NULL) {
252 aprint_error_dev(frdev, "interrupt line %d already used\n",
253 line);
254 goto lose;
255 }
256
257 /* Install the handler. */
258 sc->sc_intr[line].ih_fn = func;
259 sc->sc_intr[line].ih_arg = arg;
260 sc->sc_intr[line].ih_priority = priority;
261
262 /*
263 * If this is the first one, establish the frodo
264 * interrupt handler. If not, reestablish at a
265 * higher priority if necessary.
266 */
267 if (ih == NULL || ih->ih_priority < priority) {
268 if (ih != NULL)
269 intr_disestablish(ih);
270 sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, priority);
271 }
272
273 sc->sc_refcnt++;
274
275 /* Enable the interrupt line. */
276 frodo_imask(sc, (1 << line), 0);
277 return;
278 lose:
279 panic("frodo_intr_establish");
280 }
281
282 void
283 frodo_intr_disestablish(device_t frdev, int line)
284 {
285 struct frodo_softc *sc = device_private(frdev);
286 struct hp300_intrhand *ih = sc->sc_ih;
287 int newpri;
288
289 if (sc->sc_intr[line].ih_fn == NULL) {
290 printf("%s: no handler for line %d\n",
291 device_xname(sc->sc_dev), line);
292 panic("frodo_intr_disestablish");
293 }
294
295 sc->sc_intr[line].ih_fn = NULL;
296 frodo_imask(sc, 0, (1 << line));
297
298 /* If this was the last, unhook ourselves. */
299 if (sc->sc_refcnt-- == 1) {
300 intr_disestablish(ih);
301 return;
302 }
303
304 /* Lower our priority, if appropriate. */
305 for (newpri = 0, line = 0; line < FRODO_NINTR; line++)
306 if (sc->sc_intr[line].ih_fn != NULL &&
307 sc->sc_intr[line].ih_priority > newpri)
308 newpri = sc->sc_intr[line].ih_priority;
309
310 if (newpri != ih->ih_priority) {
311 intr_disestablish(ih);
312 sc->sc_ih = intr_establish(frodointr, sc, sc->sc_ipl, newpri);
313 }
314 }
315
316 static int
317 frodointr(void *arg)
318 {
319 struct frodo_softc *sc = arg;
320 struct frodo_interhand *fih;
321 int line, taken = 0;
322
323 /* Any interrupts pending? */
324 if (FRODO_GETPEND(sc) == 0)
325 return 0;
326
327 do {
328 /*
329 * Get pending interrupt; this also clears it for us.
330 */
331 line = FRODO_IPEND(sc);
332 fih = &sc->sc_intr[line];
333 if (fih->ih_fn == NULL ||
334 (*fih->ih_fn)(fih->ih_arg) == 0)
335 printf("%s: spurious interrupt on line %d\n",
336 device_xname(sc->sc_dev), line);
337 if (taken++ > 100)
338 panic("frodointr: looping!");
339 } while (FRODO_GETPEND(sc) != 0);
340
341 return 1;
342 }
343
344 static void
345 frodo_imask(struct frodo_softc *sc, uint16_t set, uint16_t clear)
346 {
347 uint16_t imask;
348
349 imask = FRODO_GETMASK(sc);
350
351 imask |= set;
352 imask &= ~clear;
353
354 FRODO_SETMASK(sc, imask);
355 }
356
357 /*
358 * frodo chip specific bus_space(9) support functions.
359 */
360 static uint8_t frodo_bus_space_read_sparse_1(bus_space_tag_t,
361 bus_space_handle_t, bus_size_t);
362 static void frodo_bus_space_write_sparse_1(bus_space_tag_t,
363 bus_space_handle_t, bus_size_t, uint8_t);
364
365 static void frodo_bus_space_read_multi_sparse_1(bus_space_tag_t,
366 bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
367 static void frodo_bus_space_write_multi_sparse_1(bus_space_tag_t,
368 bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
369
370 static void frodo_bus_space_read_region_sparse_1(bus_space_tag_t,
371 bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
372 static void frodo_bus_space_write_region_sparse_1(bus_space_tag_t,
373 bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
374
375 static void frodo_bus_space_set_multi_sparse_1(bus_space_tag_t,
376 bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
377
378 static void frodo_bus_space_set_region_sparse_1(bus_space_tag_t,
379 bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
380
381 /*
382 * frodo_init_bus_space():
383 * Initialize bus_space functions in bus_space_tag_t
384 * for frodo devices which have sparse address space.
385 */
386 void
387 frodo_init_bus_space(bus_space_tag_t bst)
388 {
389
390 memset(bst, 0, sizeof(struct bus_space_tag));
391 bst->bustype = HP300_BUS_SPACE_INTIO;
392
393 bst->bsr1 = frodo_bus_space_read_sparse_1;
394 bst->bsw1 = frodo_bus_space_write_sparse_1;
395
396 bst->bsrm1 = frodo_bus_space_read_multi_sparse_1;
397 bst->bswm1 = frodo_bus_space_write_multi_sparse_1;
398
399 bst->bsrr1 = frodo_bus_space_read_region_sparse_1;
400 bst->bswr1 = frodo_bus_space_write_region_sparse_1;
401
402 bst->bssm1 = frodo_bus_space_set_multi_sparse_1;
403
404 bst->bssr1 = frodo_bus_space_set_region_sparse_1;
405 }
406
407 static uint8_t
408 frodo_bus_space_read_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
409 bus_size_t offset)
410 {
411
412 return *(volatile uint8_t *)(bsh + (offset << 2));
413 }
414
415 static void
416 frodo_bus_space_write_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
417 bus_size_t offset, uint8_t val)
418 {
419
420 *(volatile uint8_t *)(bsh + (offset << 2)) = val;
421 }
422
423 static void
424 frodo_bus_space_read_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
425 bus_size_t offset, uint8_t *addr, bus_size_t len)
426 {
427
428 __asm volatile (
429 " movl %0,%%a0 ;\n"
430 " movl %1,%%a1 ;\n"
431 " movl %2,%%d0 ;\n"
432 "1: movb %%a0@,%%a1@+ ;\n"
433 " subql #1,%%d0 ;\n"
434 " jne 1b"
435 :
436 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
437 : "%a0","%a1","%d0");
438 }
439
440 static void
441 frodo_bus_space_write_multi_sparse_1(bus_space_tag_t bst,
442 bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr,
443 bus_size_t len)
444 {
445
446 __asm volatile (
447 " movl %0,%%a0 ;\n"
448 " movl %1,%%a1 ;\n"
449 " movl %2,%%d0 ;\n"
450 "1: movb %%a1@+,%%a0@ ;\n"
451 " subql #1,%%d0 ;\n"
452 " jne 1b"
453 :
454 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
455 : "%a0","%a1","%d0");
456 }
457
458 static void
459 frodo_bus_space_read_region_sparse_1(bus_space_tag_t bst,
460 bus_space_handle_t bsh, bus_size_t offset, uint8_t *addr, bus_size_t len)
461 {
462 __asm volatile (
463 " movl %0,%%a0 ;\n"
464 " movl %1,%%a1 ;\n"
465 " movl %2,%%d0 ;\n"
466 "1: movb %%a0@,%%a1@+ ;\n"
467 " addql #4,%%a0 ;\n"
468 " subql #1,%%d0 ;\n"
469 " jne 1b"
470 :
471 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
472 : "%a0","%a1","%d0");
473 }
474
475 static void
476 frodo_bus_space_write_region_sparse_1(bus_space_tag_t bst,
477 bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr,
478 bus_size_t len)
479 {
480
481 __asm volatile (
482 " movl %0,%%a0 ;\n"
483 " movl %1,%%a1 ;\n"
484 " movl %2,%%d0 ;\n"
485 "1: movb %%a1@+,%%a0@ ;\n"
486 " addql #4,%%a0 ;\n"
487 " subql #1,%%d0 ;\n"
488 " jne 1b"
489 :
490 : "r" (bsh + (offset << 2)), "g" (addr), "g" (len)
491 : "%a0","%a1","%d0");
492 }
493
494 static void
495 frodo_bus_space_set_multi_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
496 bus_size_t offset, uint8_t val, bus_size_t count)
497 {
498 __asm volatile (
499 " movl %0,%%a0 ;\n"
500 " movl %1,%%d1 ;\n"
501 " movl %2,%%d0 ;\n"
502 "1: movb %%d1,%%a0@ ;\n"
503 " subql #1,%%d0 ;\n"
504 " jne 1b"
505 :
506 : "r" (bsh + (offset << 2)), "g" (val), "g" (count)
507 : "%a0","%d0","%d1");
508 }
509
510 static void
511 frodo_bus_space_set_region_sparse_1(bus_space_tag_t bst, bus_space_handle_t bsh,
512 bus_size_t offset, uint8_t val, bus_size_t count)
513 {
514
515 __asm volatile (
516 " movl %0,%%a0 ;\n"
517 " movl %1,%%d1 ;\n"
518 " movl %2,%%d0 ;\n"
519 "1: movb %%d1,%%a0@ ;\n"
520 " addql #4,%%a0 ;\n"
521 " subql #1,%%d0 ;\n"
522 " jne 1b"
523 :
524 : "r" (bsh + (offset << 2)), "g" (val), "g" (count)
525 : "%a0","%d0","%d1");
526 }
527