vrgiu.c revision 1.3 1 /* $NetBSD: vrgiu.c,v 1.3 1999/12/04 10:15:34 takemura Exp $ */
2
3 /*-
4 * Copyright (c) 1999
5 * Shin Takemura and PocketBSD Project. All rights reserved.
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the PocketBSD project
18 * and its contributors.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #define TAILQ_FOREACH(var, head, field) \
43 for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
44 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
45
46 #include <mips/cpuregs.h>
47 #include <machine/bus.h>
48
49 #include <hpcmips/vr/vripreg.h>
50 #include <hpcmips/vr/vripvar.h>
51 #include <hpcmips/vr/vrgiureg.h>
52
53 #include "locators.h"
54
55 #define VRGIUDEBUG
56 #ifdef VRGIUDEBUG
57 #define DEBUG_IO 1
58 #define DEBUG_INTR 2
59 int vrgiu_debug = 1;
60 #define DPRINTF(flag, arg) if (vrgiu_debug & flag) printf arg;
61 #else
62 #define DPRINTF(flag, arg)
63 #endif
64
65 #define LEGAL_INTR_PORT(x) ((x) >= 0 && (x) < MAX_GPIO_INOUT)
66 #define LEGAL_OUT_PORT(x) ((x) >= 0 && (x) < MAX_GPIO_OUT)
67
68 int vrgiu_match __P((struct device*, struct cfdata*, void*));
69 void vrgiu_attach __P((struct device*, struct device*, void*));
70 int vrgiu_intr __P((void*));
71 int vrgiu_print __P((void*, const char*));
72 void vrgiu_callback __P((struct device*));
73
74 void vrgiu_dump_regs(struct vrgiu_softc *sc);
75 void vrgiu_dump_iosetting(struct vrgiu_softc *sc);
76 u_int32_t vrgiu_regread_4 __P((vrgiu_chipset_tag_t, bus_addr_t));
77 u_int16_t vrgiu_regread __P((vrgiu_chipset_tag_t, bus_addr_t));
78 void vrgiu_regwrite_4 __P((vrgiu_chipset_tag_t, bus_addr_t, u_int32_t));
79 void vrgiu_regwrite __P((vrgiu_chipset_tag_t, bus_addr_t, u_int16_t));
80
81 int vrgiu_port_register __P((vrgiu_chipset_tag_t, enum gpio_name, int));
82 int vrgiu_port_read __P((vrgiu_chipset_tag_t, vrgiu_gpioreg_t*));
83 int vrgiu_port_write __P((vrgiu_chipset_tag_t, enum gpio_name, int));
84
85 void *vrgiu_intr_establish __P((vrgiu_chipset_tag_t, int, int, int, int (*)(void *), void*));
86 void vrgiu_intr_disestablish __P((vrgiu_chipset_tag_t, void*));
87
88 struct vrgiu_function_tag vrgiu_functions = {
89 vrgiu_port_register,
90 vrgiu_port_read,
91 vrgiu_port_write,
92 vrgiu_regread_4,
93 vrgiu_regwrite_4,
94 vrgiu_intr_establish,
95 vrgiu_intr_disestablish
96 };
97
98 struct cfattach vrgiu_ca = {
99 sizeof(struct vrgiu_softc), vrgiu_match, vrgiu_attach
100 };
101
102 int
103 vrgiu_match(parent, cf, aux)
104 struct device *parent;
105 struct cfdata *cf;
106 void *aux;
107 {
108 return 2; /* 1st attach group of vrip */
109 }
110
111 void
112 vrgiu_attach(parent, self, aux)
113 struct device *parent;
114 struct device *self;
115 void *aux;
116 {
117 struct vrip_attach_args *va = aux;
118 struct vrgiu_softc *sc = (void*)self;
119 struct gpbus_attach_args gpa;
120 int i;
121
122 sc->sc_vc = va->va_vc;
123 sc->sc_iot = va->va_iot;
124 bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
125 0 /* no cache */, &sc->sc_ioh);
126 /*
127 * Disable all interrupts.
128 */
129 sc->sc_intr_mask = 0;
130 #ifdef WINCE_DEFAULT_SETTING
131 #warning WINCE_DEFAULT_SETTING
132 #else
133 #ifdef VRGIUDEBUG
134 if (vrgiu_debug & DEBUG_IO) {
135 printf("\nWIN setting: ");
136 vrgiu_dump_iosetting(sc);
137 printf("\n");
138 }
139 #endif /* VRGIUDEBUG */
140 vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
141 #endif
142
143 for (i = 0; i < MAX_GPIO_INOUT; i++)
144 TAILQ_INIT(&sc->sc_intr_head[i]);
145 if (!(sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_intr, IPL_BIO,
146 vrgiu_intr, sc))) {
147 printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
148 return;
149 }
150 vrgiu_functions.gf_intr_establish = vrgiu_intr_establish;
151 vrgiu_functions.gf_intr_disestablish = vrgiu_intr_disestablish;
152 /*
153 * Register functions to upper interface.
154 */
155 vrip_giu_function_register(va->va_vc, &vrgiu_functions, self);
156 #ifdef VRGIUDEBUG
157 /* Display port status (Input/Output) for debugging */
158 if (vrgiu_debug & DEBUG_IO) {
159 vrgiu_gpioreg_t preg;
160 printf("I/O setting: ");
161 vrgiu_dump_iosetting(sc);
162 printf("\n");
163 vrgiu_port_read(sc, &preg);
164 printf(" data:");
165 bitdisp64(preg);
166 }
167 #endif /* VRGIUDEBUG */
168 /*
169 * General purpose bus
170 */
171 for (i = 0; i< MAX_GPIO_INOUT; i++)
172 sc->sc_gpio_map[i] = GIUPORT_NOTDEF;
173 gpa.gpa_busname = "gpbus";
174 gpa.gpa_gc = sc;
175 gpa.gpa_gf = &vrgiu_functions;
176 config_found(self, &gpa, vrgiu_print);
177 /*
178 * GIU-ISA bridge
179 */
180 #if 1 /* XXX Sometimes mounting root device failed. Why? XXX*/
181 config_defer(self, vrgiu_callback);
182 #else
183 vrgiu_callback(self);
184 #endif
185 }
186
187 void
188 vrgiu_callback(self)
189 struct device *self;
190 {
191 struct vrgiu_softc *sc = (void*)self;
192 struct gpbus_attach_args gpa;
193
194 gpa.gpa_busname = "vrisab";
195 gpa.gpa_gc = sc;
196 gpa.gpa_gf = &vrgiu_functions;
197 config_found(self, &gpa, vrgiu_print);
198 }
199
200 int
201 vrgiu_print(aux, pnp)
202 void *aux;
203 const char *pnp;
204 {
205 if (pnp)
206 return (QUIET);
207 return (UNCONF);
208 }
209
210 void
211 vrgiu_dump_iosetting(sc)
212 struct vrgiu_softc *sc;
213 {
214 long iosel, inten, useupdn, termupdn;
215 u_int32_t m;
216 iosel= vrgiu_regread_4(sc, GIUIOSEL_REG);
217 inten= vrgiu_regread_4(sc, GIUINTEN_REG);
218 useupdn = vrgiu_regread(sc, GIUUSEUPDN_REG_W);
219 termupdn = vrgiu_regread(sc, GIUTERMUPDN_REG_W);
220 for (m = 0x80000000; m; m >>=1)
221 printf ("%c" , (useupdn&m) ?
222 ((termupdn&m) ? 'U' : 'D') :
223 ((iosel&m) ? 'o' : ((inten&m)?'I':'i')));
224 }
225
226 void
227 vrgiu_dump_regs(sc)
228 struct vrgiu_softc *sc;
229 {
230 if (sc == NULL) {
231 panic("%s(%d): VRGIU device not initialized\n",
232 __FILE__, __LINE__);
233 }
234 printf(" IOSEL: %08x\n", vrgiu_regread_4(sc, GIUIOSEL_REG));
235 printf(" PIOD: %08x\n", vrgiu_regread_4(sc, GIUPIOD_REG));
236 printf(" PODAT: %08x\n", vrgiu_regread_4(sc, GIUPODAT_REG));
237 printf(" INTSTAT: %08x\n", vrgiu_regread_4(sc, GIUINTSTAT_REG));
238 printf(" INTEN: %08x\n", vrgiu_regread_4(sc, GIUINTEN_REG));
239 printf(" INTTYP: %08x\n", vrgiu_regread_4(sc, GIUINTTYP_REG));
240 printf(" INTALSEL: %08x\n", vrgiu_regread_4(sc, GIUINTALSEL_REG));
241 printf(" INTHTSEL: %08x\n", vrgiu_regread_4(sc, GIUINTHTSEL_REG));
242 }
243 /*
244 * GIU regster access method.
245 */
246 u_int32_t
247 vrgiu_regread_4(vc, offs)
248 vrgiu_chipset_tag_t vc;
249 bus_addr_t offs;
250 {
251 struct vrgiu_softc *sc = (void*)vc;
252 u_int16_t reg[2];
253 bus_space_read_region_2 (sc->sc_iot, sc->sc_ioh, offs, reg, 2);
254 return reg[0]|(reg[1]<<16);
255 }
256
257 u_int16_t
258 vrgiu_regread(vc, off)
259 vrgiu_chipset_tag_t vc;
260 bus_addr_t off;
261 {
262 struct vrgiu_softc *sc = (void*)vc;
263 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, off);
264 }
265
266 void
267 vrgiu_regwrite_4(vc, offs, data)
268 vrgiu_chipset_tag_t vc;
269 bus_addr_t offs;
270 u_int32_t data;
271 {
272 struct vrgiu_softc *sc = (void*)vc;
273
274 u_int16_t reg[2];
275 reg[0] = data & 0xffff;
276 reg[1] = (data>>16)&0xffff;
277 bus_space_write_region_2 (sc->sc_iot, sc->sc_ioh, offs, reg, 2);
278 }
279
280 void
281 vrgiu_regwrite(vc, off, data)
282 vrgiu_chipset_tag_t vc;
283 bus_addr_t off;
284 u_int16_t data;
285 {
286 struct vrgiu_softc *sc = (void*)vc;
287 bus_space_write_2(sc->sc_iot, sc->sc_ioh, off, data);
288 }
289 /*
290 * Assign Platform independent port name to GPIO # map.
291 */
292 int
293 vrgiu_port_register(ic, gpio, port)
294 vrgiu_chipset_tag_t ic;
295 enum gpio_name gpio;
296 int port;
297 {
298 struct vrgiu_softc *sc = (void*)ic;
299 if (sc->sc_gpio_map[gpio] != GIUPORT_NOTDEF)
300 panic("vrgiu_port_register: already defined port.");
301 sc->sc_gpio_map[gpio] = port;
302 return 0;
303 }
304 /*
305 * PORT
306 */
307 int
308 vrgiu_port_read(vc, reg)
309 vrgiu_chipset_tag_t vc;
310 vrgiu_gpioreg_t *reg;
311 {
312 (*reg)[0] = vrgiu_regread_4(vc, GIUPIOD_REG);
313 (*reg)[1] = vrgiu_regread_4(vc, GIUPODAT_REG);
314 return 0;
315 }
316
317 int
318 vrgiu_port_write(vc, gpio, onoff)
319 vrgiu_chipset_tag_t vc;
320 enum gpio_name gpio;
321 int onoff;
322 {
323 struct vrgiu_softc *sc = (void*)vc;
324 vrgiu_gpioreg_t reg;
325 int port, bank;
326
327 if (!LEGAL_OUT_PORT(gpio))
328 panic("vrgiu_port_write: illegal gpio name");
329 if ((port = sc->sc_gpio_map[gpio]) == GIUPORT_NOTDEF) {
330 printf ("vrgiu_port_write: not defined port name%d\n", gpio);
331 return 0;
332 }
333 if (!LEGAL_OUT_PORT(port))
334 panic("vrgiu_port_write: illegal gpio port");
335
336 vrgiu_port_read(vc, ®);
337 bank = port < 32 ? 0 : 1;
338 if (bank == 1)
339 port -= 32;
340
341 if (onoff)
342 reg[bank] |= (1<<port);
343 else
344 reg[bank] &= ~(1<<port);
345 vrgiu_regwrite_4(vc, GIUPIOD_REG, reg[0]);
346 vrgiu_regwrite_4(vc, GIUPODAT_REG, reg[1]);
347
348 return 0;
349 }
350 /*
351 * For before autoconfiguration.
352 */
353 void
354 __vrgiu_out(port, data)
355 int port;
356 int data;
357 {
358 u_int16_t reg;
359 u_int32_t addr;
360 int offs;
361
362 if (!LEGAL_OUT_PORT(port))
363 panic("__vrgiu_out: illegal gpio port");
364 if (port < 16) {
365 addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPIOD_L_REG_W));
366 offs = port;
367 } else if (port < 32) {
368 addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPIOD_H_REG_W));
369 offs = port - 16;
370 } else if (port < 48) {
371 addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPODAT_L_REG_W));
372 offs = port - 32;
373 } else {
374 addr = MIPS_PHYS_TO_KSEG1((VRIP_GIU_ADDR + GIUPODAT_H_REG_W));
375 offs = port - 48;
376 panic ("__vrgiu_out: not coded yet.");
377 }
378 printf ("__vrgiu_out: addr %08x bit %d\n", addr, offs);
379
380 wbflush();
381 reg = *((volatile u_int16_t*)addr);
382 if (data) {
383 reg |= (1 << offs);
384 } else {
385 reg &= ~(1 << offs);
386 }
387 *((volatile u_int16_t*)addr) = reg;
388 wbflush();
389 }
390 /*
391 * Interrupt staff
392 */
393 void *
394 vrgiu_intr_establish(ic, port, mode, level, ih_fun, ih_arg)
395 vrgiu_chipset_tag_t ic;
396 int port; /* GPIO pin # */
397 int mode; /* GIU trigger setting */
398 int level; /* XXX not yet */
399 int (*ih_fun) __P((void*));
400 void *ih_arg;
401 {
402 struct vrgiu_softc *sc = (void*)ic;
403 int s;
404 u_int32_t reg, mask;
405 struct vrgiu_intr_entry *ih;
406
407 if (!LEGAL_INTR_PORT(port))
408 panic ("vrgiu_intr_establish: bogus interrupt line.");
409 if (sc->sc_intr_mode[port] && mode != sc->sc_intr_mode[port])
410 panic ("vrgiu_intr_establish: bogus interrupt type.");
411 else
412 sc->sc_intr_mode[port] = mode;
413 mask = (1 << port);
414
415 s = splhigh();
416
417 if (!(ih = malloc(sizeof(struct vrgiu_intr_entry), M_DEVBUF, M_NOWAIT)))
418 panic ("vrgiu_intr_establish: no memory.");
419
420 ih->ih_port = port;
421 ih->ih_fun = ih_fun;
422 ih->ih_arg = ih_arg;
423 TAILQ_INSERT_TAIL(&sc->sc_intr_head[port], ih, ih_link);
424 #ifdef WINCE_DEFAULT_SETTING
425 #warning WINCE_DEFAULT_SETTING
426 #else
427 /*
428 * Setup registers
429 */
430 /* Input mode */
431 reg = vrgiu_regread_4(sc, GIUIOSEL_REG);
432 reg &= ~mask;
433 vrgiu_regwrite_4(sc, GIUIOSEL_REG, reg);
434
435 /* interrupt type */
436 reg = vrgiu_regread_4(sc, GIUINTTYP_REG);
437 DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "edge" : "level"));
438 if (mode & VRGIU_INTR_EDGE) {
439 DPRINTF(DEBUG_INTR, ("edge]"));
440 reg |= mask; /* edge */
441 } else {
442 DPRINTF(DEBUG_INTR, ("level]"));
443 reg &= ~mask; /* level */
444 }
445 vrgiu_regwrite_4(sc, GIUINTTYP_REG, reg);
446
447 /* interrupt level */
448 if (!(mode & VRGIU_INTR_EDGE)) {
449 reg = vrgiu_regread_4(sc, GIUINTALSEL_REG);
450 DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "high" : "low"));
451 if (mode & VRGIU_INTR_HIGH) {
452 DPRINTF(DEBUG_INTR, ("high]"));
453 reg |= mask; /* high */
454 } else {
455 DPRINTF(DEBUG_INTR, ("low]"));
456 reg &= ~mask; /* low */
457 }
458 vrgiu_regwrite_4(sc, GIUINTALSEL_REG, reg);
459 }
460 /* hold or through */
461 reg = vrgiu_regread_4(sc, GIUINTHTSEL_REG);
462 DPRINTF(DEBUG_INTR, ("[%s->",reg & mask ? "hold" : "through"));
463 if (mode & VRGIU_INTR_HOLD) {
464 DPRINTF(DEBUG_INTR, ("hold]"));
465 reg |= mask; /* hold */
466 } else {
467 DPRINTF(DEBUG_INTR, ("through]"));
468 reg &= ~mask; /* through */
469 }
470 vrgiu_regwrite_4(sc, GIUINTHTSEL_REG, reg);
471 #endif
472 /*
473 * clear interrupt status
474 */
475 reg = vrgiu_regread_4(sc, GIUINTSTAT_REG);
476 reg &= ~mask;
477 vrgiu_regwrite_4(sc, GIUINTSTAT_REG, reg);
478 /*
479 * enable interrupt
480 */
481 #ifdef WINCE_DEFAULT_SETTING
482 #warning WINCE_DEFAULT_SETTING
483 #else
484 sc->sc_intr_mask |= mask;
485 vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
486 /* Unmask GIU level 2 mask register */
487 vrip_intr_setmask2(sc->sc_vc, sc->sc_ih, (1<<port), 1);
488 #endif
489 splx(s);
490
491 DPRINTF(DEBUG_INTR, ("\n"));
492 #if 0 && defined VRGIUDEBUG
493 vrgiu_dump_regs(sc);
494 #endif
495
496 return ih;
497 }
498
499 void
500 vrgiu_intr_disestablish(ic, arg)
501 vrgiu_chipset_tag_t ic;
502 void *arg;
503 {
504 struct vrgiu_intr_entry *ihe = arg;
505 struct vrgiu_softc *sc = (void*)ic;
506 int port = ihe->ih_port;
507 struct vrgiu_intr_entry *ih;
508 int s;
509
510 s = splhigh();
511 TAILQ_FOREACH(ih, &sc->sc_intr_head[port], ih_link) {
512 if (ih == ihe) {
513 TAILQ_REMOVE(&sc->sc_intr_head[port], ih, ih_link);
514 free(ih, M_DEVBUF);
515 if (TAILQ_EMPTY(&sc->sc_intr_head[port])) {
516 /* Disable interrupt */
517 #ifdef WINCE_DEFAULT_SETTING
518 #warning WINCE_DEFAULT_SETTING
519 #else
520 sc->sc_intr_mask &= ~(1<<port);
521 vrgiu_regwrite_4(sc, GIUINTEN_REG, sc->sc_intr_mask);
522 #endif
523 }
524 splx(s);
525 return;
526 }
527 }
528 panic("vrgiu_intr_disetablish: no such a handle.");
529 /* NOTREACHED */
530 }
531
532 int
533 vrgiu_intr(arg)
534 void *arg;
535 {
536 #ifdef DUMP_GIU_LEVEL2_INTR
537 #warning DUMP_GIU_LEVEL2_INTR
538 static u_int32_t oreg;
539 #endif
540 struct vrgiu_softc *sc = arg;
541 int i;
542 u_int32_t reg;
543 /* Get Level 2 interrupt status */
544 vrip_intr_get_status2 (sc->sc_vc, sc->sc_ih, ®);
545 #ifdef DUMP_GIU_LEVEL2_INTR
546 #warning DUMP_GIU_LEVEL2_INTR
547 {
548 u_int32_t uedge, dedge, j;
549 for (j = 0x80000000; j > 0; j >>=1)
550 printf ("%c" , reg&j ? '|' : '.');
551 uedge = (reg ^ oreg) & reg;
552 dedge = (reg ^ oreg) & ~reg;
553 if (uedge || dedge) {
554 for (j = 0; j < 32; j++) {
555 if (uedge & (1 << j))
556 printf ("+%d", j);
557 else if (dedge & (1 << j))
558 printf ("-%d", j);
559 }
560 }
561 oreg = reg;
562 printf ("\n");
563 }
564 #endif
565 /* Clear interrupt */
566 vrgiu_regwrite_4(sc, GIUINTSTAT_REG, vrgiu_regread_4(sc, GIUINTSTAT_REG));
567
568 /* Dispatch handler */
569 for (i = 0; i < MAX_GPIO_INOUT; i++) {
570 if (reg & (1 << i)) {
571 register struct vrgiu_intr_entry *ih;
572 TAILQ_FOREACH(ih, &sc->sc_intr_head[i], ih_link) {
573 ih->ih_fun(ih->ih_arg);
574 }
575 }
576 }
577
578 return 0;
579 }
580