frodo.c revision 1.32.42.1 1 /* $NetBSD: frodo.c,v 1.32.42.1 2021/03/22 02:00:56 thorpej 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
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 * Copyright (c) 1997 Michael Smith. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 /*
58 * Support for the "Frodo" (a.k.a. "Apollo Utility") chip found
59 * in HP Apollo 9000/4xx workstations, as well as 9000/382 controllers.
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: frodo.c,v 1.32.42.1 2021/03/22 02:00:56 thorpej Exp $");
64
65 #define _HP300_INTR_H_PRIVATE
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/syslog.h>
71 #include <sys/device.h>
72
73 #include <machine/bus.h>
74 #include <machine/cpu.h>
75 #include <machine/intr.h>
76 #include <machine/hp300spu.h>
77
78 #include <hp300/dev/intiovar.h>
79
80 #include <hp300/dev/frodoreg.h>
81 #include <hp300/dev/frodovar.h>
82
83 /*
84 * Description of a Frodo interrupt handler.
85 */
86 struct frodo_interhand {
87 int (*ih_fn)(void *);
88 void *ih_arg;
89 int ih_priority;
90 };
91
92 struct frodo_softc {
93 device_t sc_dev; /* generic device glue */
94 volatile uint8_t *sc_regs; /* register base */
95 struct frodo_interhand sc_intr[FRODO_NINTR]; /* interrupt handlers */
96 int sc_ipl;
97 void *sc_ih; /* out interrupt cookie */
98 int sc_refcnt; /* number of interrupt refs */
99 struct bus_space_tag sc_tag; /* bus space tag */
100 };
101
102 static int frodomatch(device_t, cfdata_t, void *);
103 static void frodoattach(device_t, device_t, void *);
104
105 static int frodoprint(void *, const char *);
106 static int frodosubmatch(device_t, cfdata_t, const int *, void *);
107
108 static int frodointr(void *);
109
110 static void frodo_imask(struct frodo_softc *, uint16_t, uint16_t);
111
112 CFATTACH_DECL_NEW(frodo, sizeof(struct frodo_softc),
113 frodomatch, frodoattach, NULL, NULL);
114
115 static const struct frodo_device frodo_subdevs[] = {
116 { "dnkbd", FRODO_APCI_OFFSET(0), FRODO_INTR_APCI0 },
117 { "com", FRODO_APCI_OFFSET(1), FRODO_INTR_APCI1 },
118 { "com", FRODO_APCI_OFFSET(2), FRODO_INTR_APCI2 },
119 { "com", FRODO_APCI_OFFSET(3), FRODO_INTR_APCI3 },
120 { "mcclock", FRODO_CALENDAR, FRODO_INTR_CALENDAR },
121 { NULL, 0, 0 }
122 };
123
124 static int
125 frodomatch(device_t parent, cfdata_t cf, void *aux)
126 {
127 struct intio_attach_args *ia = aux;
128 static int frodo_matched = 0;
129
130 /* only allow one instance */
131 if (frodo_matched)
132 return 0;
133
134 if (strcmp(ia->ia_modname, "frodo") != 0)
135 return 0;
136
137 if (badaddr((void *)ia->ia_addr))
138 return 0;
139
140 frodo_matched = 1;
141 return 1;
142 }
143
144 static void
145 frodoattach(device_t parent, device_t self, void *aux)
146 {
147 struct frodo_softc *sc = device_private(self);
148 struct intio_attach_args *ia = aux;
149 bus_space_tag_t bst = &sc->sc_tag;
150 const struct frodo_device *fd;
151 struct frodo_attach_args fa;
152
153 sc->sc_dev = self;
154 sc->sc_regs = (volatile uint8_t *)ia->ia_addr;
155 sc->sc_ipl = ia->ia_ipl;
156
157 if ((FRODO_READ(sc, FRODO_IISR) & FRODO_IISR_SERVICE) == 0)
158 aprint_error(": service mode enabled");
159 aprint_normal("\n");
160
161 /* Initialize bus_space_tag_t for frodo */
162 frodo_init_bus_space(bst);
163
164 /* Clear all of the interrupt handlers. */
165 memset(sc->sc_intr, 0, sizeof(sc->sc_intr));
166 sc->sc_refcnt = 0;
167
168 /*
169 * Disable all of the interrupt lines; we reenable them
170 * as subdevices attach.
171 */
172 frodo_imask(sc, 0, 0xffff);
173
174 /* Clear any pending interrupts. */
175 FRODO_WRITE(sc, FRODO_PIC_PU, 0xff);
176 FRODO_WRITE(sc, FRODO_PIC_PL, 0xff);
177
178 /* Set interrupt polarities. */
179 FRODO_WRITE(sc, FRODO_PIO_IPR, 0x10);
180
181 /* ...and configure for edge triggering. */
182 FRODO_WRITE(sc, FRODO_PIO_IELR, 0xcf);
183
184 /*
185 * We defer hooking up our interrupt handler until
186 * a subdevice hooks up theirs.
187 */
188 sc->sc_ih = NULL;
189
190 /* ... and attach subdevices. */
191 for (fd = frodo_subdevs; fd->fd_name != NULL; fd++) {
192 /*
193 * Skip the first serial port if we're not a 425e;
194 * it's mapped to the DCA at select code 9 on all
195 * other models.
196 */
197 if (fd->fd_offset == FRODO_APCI_OFFSET(1) &&
198 mmuid != MMUID_425_E)
199 continue;
200 /*
201 * The mcclock is available only on a 425e.
202 */
203 if (fd->fd_offset == FRODO_CALENDAR && mmuid != MMUID_425_E)
204 continue;
205 fa.fa_name = fd->fd_name;
206 fa.fa_bst = bst;
207 fa.fa_base = ia->ia_iobase;
208 fa.fa_offset = fd->fd_offset;
209 fa.fa_line = fd->fd_line;
210 config_found(self, &fa, frodoprint,
211 CFARG_SUBMATCH, frodosubmatch,
212 CFARG_IATTR, "frodo",
213 CFARG_EOL);
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