vr4181giu.c revision 1.3 1 1.3 christos /* $NetBSD: vr4181giu.c,v 1.3 2005/12/11 12:17:34 christos Exp $ */
2 1.1 igy
3 1.1 igy /*-
4 1.1 igy * Copyright (c) 1999-2001
5 1.1 igy * Shin Takemura and PocketBSD Project. All rights reserved.
6 1.1 igy *
7 1.1 igy * Redistribution and use in source and binary forms, with or without
8 1.1 igy * modification, are permitted provided that the following conditions
9 1.1 igy * are met:
10 1.1 igy * 1. Redistributions of source code must retain the above copyright
11 1.1 igy * notice, this list of conditions and the following disclaimer.
12 1.1 igy * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 igy * notice, this list of conditions and the following disclaimer in the
14 1.1 igy * documentation and/or other materials provided with the distribution.
15 1.1 igy * 3. All advertising materials mentioning features or use of this software
16 1.1 igy * must display the following acknowledgement:
17 1.1 igy * This product includes software developed by the PocketBSD project
18 1.1 igy * and its contributors.
19 1.1 igy * 4. Neither the name of the project nor the names of its contributors
20 1.1 igy * may be used to endorse or promote products derived from this software
21 1.1 igy * without specific prior written permission.
22 1.1 igy *
23 1.1 igy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 igy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 igy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 igy * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 igy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 igy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 igy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 igy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 igy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 igy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 igy * SUCH DAMAGE.
34 1.1 igy *
35 1.1 igy */
36 1.2 lukem
37 1.2 lukem #include <sys/cdefs.h>
38 1.3 christos __KERNEL_RCSID(0, "$NetBSD: vr4181giu.c,v 1.3 2005/12/11 12:17:34 christos Exp $");
39 1.1 igy
40 1.1 igy #include <sys/param.h>
41 1.1 igy #include <sys/device.h>
42 1.1 igy #include <sys/malloc.h>
43 1.1 igy #include <sys/queue.h>
44 1.1 igy #include <sys/systm.h>
45 1.1 igy
46 1.1 igy #include <machine/bus.h>
47 1.1 igy
48 1.1 igy #include <hpcmips/vr/vripif.h>
49 1.1 igy #include <hpcmips/vr/vr4181giureg.h>
50 1.1 igy
51 1.1 igy #define MAX_GIU4181INTR 16
52 1.1 igy
53 1.1 igy struct vr4181giu_intr_entry {
54 1.1 igy int ih_port;
55 1.1 igy int (*ih_fun)(void *);
56 1.1 igy void *ih_arg;
57 1.1 igy TAILQ_ENTRY(vr4181giu_intr_entry) ih_link;
58 1.1 igy };
59 1.1 igy
60 1.1 igy struct vr4181giu_softc {
61 1.1 igy struct device sc_dev;
62 1.1 igy bus_space_tag_t sc_iot;
63 1.1 igy bus_space_handle_t sc_ioh;
64 1.1 igy vrip_chipset_tag_t sc_vc;
65 1.1 igy void *sc_ih;
66 1.1 igy u_int32_t sc_intr_mode[MAX_GIU4181INTR];
67 1.1 igy TAILQ_HEAD(, vr4181giu_intr_entry)
68 1.1 igy sc_intr_head[MAX_GIU4181INTR];
69 1.1 igy struct hpcio_chip sc_iochip;
70 1.1 igy struct hpcio_attach_args sc_haa;
71 1.1 igy };
72 1.1 igy
73 1.1 igy static int vr4181giu_match(struct device *, struct cfdata *, void *);
74 1.1 igy static void vr4181giu_attach(struct device *, struct device *, void *);
75 1.1 igy
76 1.1 igy static void vr4181giu_callback(struct device *self);
77 1.1 igy static int vr4181giu_print(void *aux, const char *pnp);
78 1.1 igy static int vr4181giu_port_read(hpcio_chip_t hc, int port);
79 1.1 igy static void vr4181giu_port_write(hpcio_chip_t hc, int port, int onoff);
80 1.1 igy static void vr4181giu_update(hpcio_chip_t hc);
81 1.1 igy static void vr4181giu_dump(hpcio_chip_t hc);
82 1.1 igy static hpcio_chip_t vr4181giu_getchip(void* scx, int chipid);
83 1.1 igy static void *vr4181giu_intr_establish(hpcio_chip_t, int, int,
84 1.1 igy int (*)(void *),void *);
85 1.1 igy static void vr4181giu_intr_disestablish(hpcio_chip_t hc, void *arg);
86 1.1 igy static void vr4181giu_intr_clear(hpcio_chip_t hc, void *arg);
87 1.1 igy static void vr4181giu_register_iochip(hpcio_chip_t hc, hpcio_chip_t iochip);
88 1.1 igy static int vr4181giu_intr(void *arg);
89 1.1 igy
90 1.1 igy
91 1.1 igy
92 1.1 igy static struct hpcio_chip vr4181giu_iochip = {
93 1.1 igy .hc_portread = vr4181giu_port_read,
94 1.1 igy .hc_portwrite = vr4181giu_port_write,
95 1.1 igy .hc_intr_establish = vr4181giu_intr_establish,
96 1.1 igy .hc_intr_disestablish = vr4181giu_intr_disestablish,
97 1.1 igy .hc_intr_clear = vr4181giu_intr_clear,
98 1.1 igy .hc_register_iochip = vr4181giu_register_iochip,
99 1.1 igy .hc_update = vr4181giu_update,
100 1.1 igy .hc_dump = vr4181giu_dump,
101 1.1 igy };
102 1.1 igy
103 1.1 igy CFATTACH_DECL(vr4181giu, sizeof(struct vr4181giu_softc),
104 1.1 igy vr4181giu_match, vr4181giu_attach, NULL, NULL);
105 1.1 igy
106 1.1 igy static int
107 1.1 igy vr4181giu_match(struct device *parent, struct cfdata *match, void *aux)
108 1.1 igy {
109 1.1 igy return (2); /* 1st attach group of vrip */
110 1.1 igy }
111 1.1 igy
112 1.1 igy static void
113 1.1 igy vr4181giu_attach(struct device *parent, struct device *self, void *aux)
114 1.1 igy {
115 1.1 igy struct vr4181giu_softc *sc = (struct vr4181giu_softc*) self;
116 1.1 igy struct vrip_attach_args *va = aux;
117 1.1 igy int i;
118 1.1 igy
119 1.1 igy sc->sc_iot = va->va_iot;
120 1.1 igy sc->sc_vc = va->va_vc;
121 1.1 igy
122 1.1 igy if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
123 1.1 igy 0 /* no cache */, &sc->sc_ioh)) {
124 1.1 igy printf(": can't map i/o space\n");
125 1.1 igy return;
126 1.1 igy }
127 1.1 igy
128 1.1 igy for (i = 0; i < MAX_GIU4181INTR; i++)
129 1.1 igy TAILQ_INIT(&sc->sc_intr_head[i]);
130 1.1 igy
131 1.1 igy if (!(sc->sc_ih
132 1.1 igy = vrip_intr_establish(va->va_vc, va->va_unit, 0,
133 1.1 igy IPL_BIO, vr4181giu_intr, sc))) {
134 1.1 igy printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
135 1.1 igy return;
136 1.1 igy }
137 1.1 igy
138 1.1 igy /*
139 1.1 igy * fill hpcio_chip structure
140 1.1 igy */
141 1.1 igy sc->sc_iochip = vr4181giu_iochip; /* structure copy */
142 1.1 igy sc->sc_iochip.hc_chipid = VRIP_IOCHIP_VR4181GIU;
143 1.1 igy sc->sc_iochip.hc_name = sc->sc_dev.dv_xname;
144 1.1 igy sc->sc_iochip.hc_sc = sc;
145 1.1 igy /* Register functions to upper interface */
146 1.1 igy vrip_register_gpio(va->va_vc, &sc->sc_iochip);
147 1.1 igy
148 1.1 igy printf("\n");
149 1.1 igy
150 1.1 igy /*
151 1.1 igy * hpcio I/F
152 1.1 igy */
153 1.1 igy sc->sc_haa.haa_busname = HPCIO_BUSNAME;
154 1.1 igy sc->sc_haa.haa_sc = sc;
155 1.1 igy sc->sc_haa.haa_getchip = vr4181giu_getchip;
156 1.1 igy sc->sc_haa.haa_iot = sc->sc_iot;
157 1.1 igy while (config_found(self, &sc->sc_haa, vr4181giu_print)) ;
158 1.1 igy
159 1.1 igy /*
160 1.1 igy * GIU-ISA bridge
161 1.1 igy */
162 1.1 igy #if 1 /* XXX Sometimes mounting root device failed. Why? XXX*/
163 1.1 igy config_defer(self, vr4181giu_callback);
164 1.1 igy #else
165 1.1 igy vr4181giu_callback(self);
166 1.1 igy #endif
167 1.1 igy }
168 1.1 igy
169 1.1 igy static void
170 1.1 igy vr4181giu_callback(struct device *self)
171 1.1 igy {
172 1.1 igy struct vr4181giu_softc *sc = (void *) self;
173 1.1 igy
174 1.1 igy sc->sc_haa.haa_busname = "vrisab";
175 1.1 igy config_found(self, &sc->sc_haa, vr4181giu_print);
176 1.1 igy }
177 1.1 igy
178 1.1 igy static int
179 1.1 igy vr4181giu_print(void *aux, const char *pnp)
180 1.1 igy {
181 1.1 igy if (pnp)
182 1.1 igy return (QUIET);
183 1.1 igy return (UNCONF);
184 1.1 igy }
185 1.1 igy
186 1.1 igy static int
187 1.1 igy vr4181giu_port_read(hpcio_chip_t hc, int port)
188 1.1 igy {
189 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
190 1.1 igy u_int16_t r;
191 1.1 igy
192 1.1 igy if (port < 0 || 32 <= port)
193 1.1 igy panic("vr4181giu_port_read: invalid gpio port");
194 1.1 igy
195 1.1 igy if (port < 16) {
196 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
197 1.1 igy VR4181GIU_PIOD_L_REG_W)
198 1.1 igy & 1 << port;
199 1.1 igy } else {
200 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
201 1.1 igy VR4181GIU_PIOD_H_REG_W)
202 1.1 igy & 1 << (port - 16);
203 1.1 igy }
204 1.1 igy return r ? 1 : 0;
205 1.1 igy }
206 1.1 igy
207 1.1 igy static void
208 1.1 igy vr4181giu_port_write(hpcio_chip_t hc, int port, int onoff)
209 1.1 igy {
210 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
211 1.1 igy u_int16_t r;
212 1.1 igy
213 1.1 igy if (port < 16) {
214 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
215 1.1 igy VR4181GIU_PIOD_L_REG_W);
216 1.1 igy if (onoff) {
217 1.1 igy r |= 1 << port;
218 1.1 igy } else {
219 1.1 igy r &= ~(1 << port);
220 1.1 igy }
221 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
222 1.1 igy VR4181GIU_PIOD_L_REG_W, r);
223 1.1 igy } else {
224 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
225 1.1 igy VR4181GIU_PIOD_H_REG_W);
226 1.1 igy if (onoff) {
227 1.1 igy r |= 1 << (port - 16);
228 1.1 igy } else {
229 1.1 igy r &= ~(1 << (port - 16));
230 1.1 igy }
231 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
232 1.1 igy VR4181GIU_PIOD_H_REG_W, r);
233 1.1 igy }
234 1.1 igy }
235 1.1 igy
236 1.1 igy /*
237 1.1 igy * XXXXXXXXXXXXXXXXXXXXXXXX
238 1.1 igy */
239 1.1 igy static void
240 1.1 igy vr4181giu_update(hpcio_chip_t hc)
241 1.1 igy {
242 1.1 igy }
243 1.1 igy
244 1.1 igy static void
245 1.1 igy vr4181giu_dump(hpcio_chip_t hc)
246 1.1 igy {
247 1.1 igy }
248 1.1 igy
249 1.1 igy static hpcio_chip_t
250 1.1 igy vr4181giu_getchip(void* scx, int chipid)
251 1.1 igy {
252 1.1 igy struct vr4181giu_softc *sc = scx;
253 1.1 igy
254 1.1 igy return (&sc->sc_iochip);
255 1.1 igy }
256 1.1 igy
257 1.1 igy static void *
258 1.1 igy vr4181giu_intr_establish(
259 1.1 igy hpcio_chip_t hc,
260 1.1 igy int port, /* GPIO pin # */
261 1.1 igy int mode, /* GIU trigger setting */
262 1.1 igy int (*ih_fun)(void *),
263 1.1 igy void *ih_arg)
264 1.1 igy {
265 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
266 1.1 igy struct vr4181giu_intr_entry *ih;
267 1.1 igy int s;
268 1.1 igy u_int32_t mask;
269 1.1 igy u_int32_t raw_intr_type;
270 1.1 igy int regmod;
271 1.1 igy int reghl;
272 1.1 igy int bitoff;
273 1.1 igy u_int16_t r;
274 1.1 igy
275 1.1 igy /*
276 1.1 igy * trigger mode translation
277 1.1 igy *
278 1.1 igy * VR4181 only support for four type of interrupt trigger
279 1.1 igy * listed below:
280 1.1 igy *
281 1.1 igy * 1. high level
282 1.1 igy * 2. low level
283 1.1 igy * 3. rising edge
284 1.1 igy * 4. falling edge
285 1.1 igy *
286 1.1 igy * argument mode is a bitmap as following:
287 1.1 igy *
288 1.1 igy * 001 detection trigger (1:edge/0:level )
289 1.1 igy * 010 signal hold/through (1:hold/0:through)
290 1.1 igy * 100 detection level (1:high/0:low )
291 1.1 igy *
292 1.1 igy * possible mode value is 000B to 111B.
293 1.1 igy *
294 1.1 igy * 000 HPCIO_INTR_LEVEL_LOW_THROUGH
295 1.1 igy * 001 HPCIO_INTR_EDGE_THROUGH
296 1.1 igy * 010 HPCIO_INTR_LEVEL_LOW_HOLD
297 1.1 igy * 011 HPCIO_INTR_EDGE_HOLD
298 1.1 igy * 100 HPCIO_INTR_LEVEL_HIGH_THROUGH
299 1.1 igy * 101 falling edge and through?
300 1.1 igy * 110 HPCIO_INTR_LEVEL_HIGH_HOLD
301 1.1 igy * 111 falling edge and hold?
302 1.1 igy */
303 1.1 igy
304 1.1 igy static u_int32_t intr_mode_trans[8] = {
305 1.1 igy VR4181GIU_INTTYP_LOW_LEVEL, /* 000 */
306 1.1 igy VR4181GIU_INTTYP_RISING_EDGE, /* 001 */
307 1.1 igy VR4181GIU_INTTYP_LOW_LEVEL, /* 010 */
308 1.1 igy VR4181GIU_INTTYP_RISING_EDGE, /* 011 */
309 1.1 igy VR4181GIU_INTTYP_HIGH_LEVEL, /* 100 */
310 1.1 igy VR4181GIU_INTTYP_FALLING_EDGE, /* 101 */
311 1.1 igy VR4181GIU_INTTYP_HIGH_LEVEL, /* 110 */
312 1.1 igy VR4181GIU_INTTYP_FALLING_EDGE, /* 111 */
313 1.1 igy };
314 1.1 igy
315 1.1 igy raw_intr_type = intr_mode_trans[mode];
316 1.1 igy if (raw_intr_type == VR4181GIU_INTTYP_INVALID)
317 1.1 igy panic("vr4181giu_intr_establish: invalid interrupt mode.");
318 1.1 igy
319 1.1 igy if (port < 0 || MAX_GIU4181INTR <= port)
320 1.1 igy panic("vr4181giu_intr_establish: invalid interrupt line.");
321 1.1 igy if (!TAILQ_EMPTY(&sc->sc_intr_head[port])
322 1.1 igy && raw_intr_type != sc->sc_intr_mode[port])
323 1.1 igy panic("vr4181giu_intr_establish: "
324 1.1 igy "cannot use one line with two modes at a time.");
325 1.1 igy else
326 1.1 igy sc->sc_intr_mode[port] = raw_intr_type;
327 1.1 igy mask = (1 << port);
328 1.1 igy
329 1.1 igy s = splhigh();
330 1.1 igy
331 1.1 igy if ((ih = malloc(sizeof *ih, M_DEVBUF, M_NOWAIT)) == NULL)
332 1.1 igy panic("vr4181giu_intr_establish: memory exhausted.");
333 1.1 igy
334 1.1 igy ih->ih_port = port;
335 1.1 igy ih->ih_fun = ih_fun;
336 1.1 igy ih->ih_arg = ih_arg;
337 1.1 igy TAILQ_INSERT_TAIL(&sc->sc_intr_head[port], ih, ih_link);
338 1.1 igy
339 1.1 igy /*
340 1.1 igy * setup GIU registers
341 1.1 igy */
342 1.1 igy
343 1.1 igy /* disable interrupt at first */
344 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W);
345 1.1 igy r &= ~mask;
346 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W, r);
347 1.1 igy
348 1.1 igy /* mode */
349 1.1 igy regmod = port >> 3;
350 1.1 igy bitoff = (port & 0x7) << 1;
351 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
352 1.1 igy VR4181GIU_MODE0_REG_W + regmod);
353 1.1 igy r &= ~(0x3 << bitoff);
354 1.1 igy r |= (VR4181GIU_MODE_IN | VR4181GIU_MODE_GPIO) << bitoff;
355 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
356 1.1 igy VR4181GIU_MODE0_REG_W + regmod, r);
357 1.1 igy /* interrupt type */
358 1.1 igy reghl = port < 8 ? 2 : 0; /* high byte: 0x0, lowbyte: 0x2 */
359 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
360 1.1 igy VR4181GIU_INTTYP_REG + reghl);
361 1.1 igy r &= ~(0x3 << bitoff);
362 1.1 igy r |= raw_intr_type << bitoff;
363 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
364 1.1 igy VR4181GIU_INTTYP_REG + reghl, r);
365 1.1 igy
366 1.1 igy /* clear status */
367 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
368 1.1 igy VR4181GIU_INTSTAT_REG_W, mask);
369 1.1 igy
370 1.1 igy /* unmask */
371 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTMASK_REG_W);
372 1.1 igy r &= ~mask;
373 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTMASK_REG_W, r);
374 1.1 igy
375 1.1 igy /* enable */
376 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W);
377 1.1 igy r |= mask;
378 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W, r);
379 1.1 igy
380 1.1 igy splx(s);
381 1.1 igy
382 1.1 igy return ih;
383 1.1 igy }
384 1.1 igy
385 1.1 igy static void
386 1.1 igy vr4181giu_intr_disestablish(hpcio_chip_t hc, void *arg)
387 1.1 igy {
388 1.1 igy }
389 1.1 igy
390 1.1 igy static void
391 1.1 igy vr4181giu_intr_clear(hpcio_chip_t hc, void *arg)
392 1.1 igy {
393 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
394 1.1 igy struct vr4181giu_intr_entry *ih = arg;
395 1.1 igy
396 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
397 1.1 igy VR4181GIU_INTSTAT_REG_W, 1 << ih->ih_port);
398 1.1 igy }
399 1.1 igy
400 1.1 igy static void
401 1.1 igy vr4181giu_register_iochip(hpcio_chip_t hc, hpcio_chip_t iochip)
402 1.1 igy {
403 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
404 1.1 igy
405 1.1 igy vrip_register_gpio(sc->sc_vc, iochip);
406 1.1 igy }
407 1.1 igy
408 1.1 igy /*
409 1.1 igy * interrupt handler
410 1.1 igy */
411 1.1 igy static int
412 1.1 igy vr4181giu_intr(void *arg)
413 1.1 igy {
414 1.1 igy struct vr4181giu_softc *sc = arg;
415 1.1 igy int i;
416 1.1 igy u_int16_t r;
417 1.1 igy
418 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTSTAT_REG_W);
419 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTSTAT_REG_W, r);
420 1.1 igy
421 1.1 igy for (i = 0; i < MAX_GIU4181INTR; i++) {
422 1.1 igy if (r & (1 << i)) {
423 1.1 igy struct vr4181giu_intr_entry *ih;
424 1.1 igy TAILQ_FOREACH(ih, &sc->sc_intr_head[i], ih_link) {
425 1.1 igy ih->ih_fun(ih->ih_arg);
426 1.1 igy }
427 1.1 igy }
428 1.1 igy }
429 1.1 igy
430 1.1 igy return 0;
431 1.1 igy }
432