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