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