vr4181giu.c revision 1.5.8.1 1 1.5.8.1 thorpej /* $NetBSD: vr4181giu.c,v 1.5.8.1 2020/12/14 14:37:56 thorpej 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.5.8.1 thorpej __KERNEL_RCSID(0, "$NetBSD: vr4181giu.c,v 1.5.8.1 2020/12/14 14:37:56 thorpej Exp $");
39 1.1 igy
40 1.1 igy #include <sys/param.h>
41 1.1 igy #include <sys/device.h>
42 1.5.8.1 thorpej #include <sys/kmem.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 bus_space_tag_t sc_iot;
62 1.1 igy bus_space_handle_t sc_ioh;
63 1.1 igy vrip_chipset_tag_t sc_vc;
64 1.1 igy void *sc_ih;
65 1.1 igy u_int32_t sc_intr_mode[MAX_GIU4181INTR];
66 1.1 igy TAILQ_HEAD(, vr4181giu_intr_entry)
67 1.1 igy sc_intr_head[MAX_GIU4181INTR];
68 1.1 igy struct hpcio_chip sc_iochip;
69 1.1 igy struct hpcio_attach_args sc_haa;
70 1.1 igy };
71 1.1 igy
72 1.4 chs static int vr4181giu_match(device_t, cfdata_t, void *);
73 1.4 chs static void vr4181giu_attach(device_t, device_t, void *);
74 1.1 igy
75 1.4 chs static void vr4181giu_callback(device_t);
76 1.4 chs static int vr4181giu_print(void *, const char *);
77 1.4 chs static int vr4181giu_port_read(hpcio_chip_t, int);
78 1.4 chs static void vr4181giu_port_write(hpcio_chip_t, int, int);
79 1.4 chs static void vr4181giu_update(hpcio_chip_t);
80 1.4 chs static void vr4181giu_dump(hpcio_chip_t);
81 1.4 chs static hpcio_chip_t vr4181giu_getchip(void *, int);
82 1.1 igy static void *vr4181giu_intr_establish(hpcio_chip_t, int, int,
83 1.1 igy int (*)(void *),void *);
84 1.4 chs static void vr4181giu_intr_disestablish(hpcio_chip_t, void *);
85 1.4 chs static void vr4181giu_intr_clear(hpcio_chip_t, void *);
86 1.4 chs static void vr4181giu_register_iochip(hpcio_chip_t, hpcio_chip_t);
87 1.4 chs static int vr4181giu_intr(void *);
88 1.1 igy
89 1.1 igy static struct hpcio_chip vr4181giu_iochip = {
90 1.1 igy .hc_portread = vr4181giu_port_read,
91 1.1 igy .hc_portwrite = vr4181giu_port_write,
92 1.1 igy .hc_intr_establish = vr4181giu_intr_establish,
93 1.1 igy .hc_intr_disestablish = vr4181giu_intr_disestablish,
94 1.1 igy .hc_intr_clear = vr4181giu_intr_clear,
95 1.1 igy .hc_register_iochip = vr4181giu_register_iochip,
96 1.1 igy .hc_update = vr4181giu_update,
97 1.1 igy .hc_dump = vr4181giu_dump,
98 1.1 igy };
99 1.1 igy
100 1.4 chs CFATTACH_DECL_NEW(vr4181giu, sizeof(struct vr4181giu_softc),
101 1.1 igy vr4181giu_match, vr4181giu_attach, NULL, NULL);
102 1.1 igy
103 1.1 igy static int
104 1.4 chs vr4181giu_match(device_t parent, cfdata_t match, void *aux)
105 1.1 igy {
106 1.1 igy return (2); /* 1st attach group of vrip */
107 1.1 igy }
108 1.1 igy
109 1.1 igy static void
110 1.4 chs vr4181giu_attach(device_t parent, device_t self, void *aux)
111 1.1 igy {
112 1.4 chs struct vr4181giu_softc *sc = device_private(self);
113 1.1 igy struct vrip_attach_args *va = aux;
114 1.1 igy int i;
115 1.1 igy
116 1.1 igy sc->sc_iot = va->va_iot;
117 1.1 igy sc->sc_vc = va->va_vc;
118 1.1 igy
119 1.1 igy if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
120 1.1 igy 0 /* no cache */, &sc->sc_ioh)) {
121 1.1 igy printf(": can't map i/o space\n");
122 1.1 igy return;
123 1.1 igy }
124 1.1 igy
125 1.1 igy for (i = 0; i < MAX_GIU4181INTR; i++)
126 1.1 igy TAILQ_INIT(&sc->sc_intr_head[i]);
127 1.1 igy
128 1.1 igy if (!(sc->sc_ih
129 1.1 igy = vrip_intr_establish(va->va_vc, va->va_unit, 0,
130 1.1 igy IPL_BIO, vr4181giu_intr, sc))) {
131 1.4 chs printf("%s: can't establish interrupt\n", device_xname(self));
132 1.1 igy return;
133 1.1 igy }
134 1.1 igy
135 1.1 igy /*
136 1.1 igy * fill hpcio_chip structure
137 1.1 igy */
138 1.1 igy sc->sc_iochip = vr4181giu_iochip; /* structure copy */
139 1.1 igy sc->sc_iochip.hc_chipid = VRIP_IOCHIP_VR4181GIU;
140 1.4 chs sc->sc_iochip.hc_name = device_xname(self);
141 1.1 igy sc->sc_iochip.hc_sc = sc;
142 1.1 igy /* Register functions to upper interface */
143 1.1 igy vrip_register_gpio(va->va_vc, &sc->sc_iochip);
144 1.1 igy
145 1.1 igy printf("\n");
146 1.1 igy
147 1.1 igy /*
148 1.1 igy * hpcio I/F
149 1.1 igy */
150 1.1 igy sc->sc_haa.haa_busname = HPCIO_BUSNAME;
151 1.1 igy sc->sc_haa.haa_sc = sc;
152 1.1 igy sc->sc_haa.haa_getchip = vr4181giu_getchip;
153 1.1 igy sc->sc_haa.haa_iot = sc->sc_iot;
154 1.1 igy while (config_found(self, &sc->sc_haa, vr4181giu_print)) ;
155 1.1 igy
156 1.1 igy /*
157 1.1 igy * GIU-ISA bridge
158 1.1 igy */
159 1.1 igy #if 1 /* XXX Sometimes mounting root device failed. Why? XXX*/
160 1.1 igy config_defer(self, vr4181giu_callback);
161 1.1 igy #else
162 1.1 igy vr4181giu_callback(self);
163 1.1 igy #endif
164 1.1 igy }
165 1.1 igy
166 1.1 igy static void
167 1.4 chs vr4181giu_callback(device_t self)
168 1.1 igy {
169 1.1 igy struct vr4181giu_softc *sc = (void *) self;
170 1.1 igy
171 1.1 igy sc->sc_haa.haa_busname = "vrisab";
172 1.1 igy config_found(self, &sc->sc_haa, vr4181giu_print);
173 1.1 igy }
174 1.1 igy
175 1.1 igy static int
176 1.1 igy vr4181giu_print(void *aux, const char *pnp)
177 1.1 igy {
178 1.1 igy if (pnp)
179 1.1 igy return (QUIET);
180 1.1 igy return (UNCONF);
181 1.1 igy }
182 1.1 igy
183 1.1 igy static int
184 1.1 igy vr4181giu_port_read(hpcio_chip_t hc, int port)
185 1.1 igy {
186 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
187 1.1 igy u_int16_t r;
188 1.1 igy
189 1.1 igy if (port < 0 || 32 <= port)
190 1.1 igy panic("vr4181giu_port_read: invalid gpio port");
191 1.1 igy
192 1.1 igy if (port < 16) {
193 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
194 1.1 igy VR4181GIU_PIOD_L_REG_W)
195 1.1 igy & 1 << port;
196 1.1 igy } else {
197 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
198 1.1 igy VR4181GIU_PIOD_H_REG_W)
199 1.1 igy & 1 << (port - 16);
200 1.1 igy }
201 1.1 igy return r ? 1 : 0;
202 1.1 igy }
203 1.1 igy
204 1.1 igy static void
205 1.1 igy vr4181giu_port_write(hpcio_chip_t hc, int port, int onoff)
206 1.1 igy {
207 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
208 1.1 igy u_int16_t r;
209 1.1 igy
210 1.1 igy if (port < 16) {
211 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
212 1.1 igy VR4181GIU_PIOD_L_REG_W);
213 1.1 igy if (onoff) {
214 1.1 igy r |= 1 << port;
215 1.1 igy } else {
216 1.1 igy r &= ~(1 << port);
217 1.1 igy }
218 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
219 1.1 igy VR4181GIU_PIOD_L_REG_W, r);
220 1.1 igy } else {
221 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
222 1.1 igy VR4181GIU_PIOD_H_REG_W);
223 1.1 igy if (onoff) {
224 1.1 igy r |= 1 << (port - 16);
225 1.1 igy } else {
226 1.1 igy r &= ~(1 << (port - 16));
227 1.1 igy }
228 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
229 1.1 igy VR4181GIU_PIOD_H_REG_W, r);
230 1.1 igy }
231 1.1 igy }
232 1.1 igy
233 1.1 igy /*
234 1.1 igy * XXXXXXXXXXXXXXXXXXXXXXXX
235 1.1 igy */
236 1.1 igy static void
237 1.1 igy vr4181giu_update(hpcio_chip_t hc)
238 1.1 igy {
239 1.1 igy }
240 1.1 igy
241 1.1 igy static void
242 1.1 igy vr4181giu_dump(hpcio_chip_t hc)
243 1.1 igy {
244 1.1 igy }
245 1.1 igy
246 1.1 igy static hpcio_chip_t
247 1.1 igy vr4181giu_getchip(void* scx, int chipid)
248 1.1 igy {
249 1.1 igy struct vr4181giu_softc *sc = scx;
250 1.1 igy
251 1.1 igy return (&sc->sc_iochip);
252 1.1 igy }
253 1.1 igy
254 1.1 igy static void *
255 1.1 igy vr4181giu_intr_establish(
256 1.1 igy hpcio_chip_t hc,
257 1.1 igy int port, /* GPIO pin # */
258 1.1 igy int mode, /* GIU trigger setting */
259 1.1 igy int (*ih_fun)(void *),
260 1.1 igy void *ih_arg)
261 1.1 igy {
262 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
263 1.1 igy struct vr4181giu_intr_entry *ih;
264 1.1 igy int s;
265 1.1 igy u_int32_t mask;
266 1.1 igy u_int32_t raw_intr_type;
267 1.1 igy int regmod;
268 1.1 igy int reghl;
269 1.1 igy int bitoff;
270 1.1 igy u_int16_t r;
271 1.1 igy
272 1.1 igy /*
273 1.1 igy * trigger mode translation
274 1.1 igy *
275 1.1 igy * VR4181 only support for four type of interrupt trigger
276 1.1 igy * listed below:
277 1.1 igy *
278 1.1 igy * 1. high level
279 1.1 igy * 2. low level
280 1.1 igy * 3. rising edge
281 1.1 igy * 4. falling edge
282 1.1 igy *
283 1.1 igy * argument mode is a bitmap as following:
284 1.1 igy *
285 1.1 igy * 001 detection trigger (1:edge/0:level )
286 1.1 igy * 010 signal hold/through (1:hold/0:through)
287 1.1 igy * 100 detection level (1:high/0:low )
288 1.1 igy *
289 1.1 igy * possible mode value is 000B to 111B.
290 1.1 igy *
291 1.1 igy * 000 HPCIO_INTR_LEVEL_LOW_THROUGH
292 1.1 igy * 001 HPCIO_INTR_EDGE_THROUGH
293 1.1 igy * 010 HPCIO_INTR_LEVEL_LOW_HOLD
294 1.1 igy * 011 HPCIO_INTR_EDGE_HOLD
295 1.1 igy * 100 HPCIO_INTR_LEVEL_HIGH_THROUGH
296 1.1 igy * 101 falling edge and through?
297 1.1 igy * 110 HPCIO_INTR_LEVEL_HIGH_HOLD
298 1.1 igy * 111 falling edge and hold?
299 1.1 igy */
300 1.1 igy
301 1.1 igy static u_int32_t intr_mode_trans[8] = {
302 1.1 igy VR4181GIU_INTTYP_LOW_LEVEL, /* 000 */
303 1.1 igy VR4181GIU_INTTYP_RISING_EDGE, /* 001 */
304 1.1 igy VR4181GIU_INTTYP_LOW_LEVEL, /* 010 */
305 1.1 igy VR4181GIU_INTTYP_RISING_EDGE, /* 011 */
306 1.1 igy VR4181GIU_INTTYP_HIGH_LEVEL, /* 100 */
307 1.1 igy VR4181GIU_INTTYP_FALLING_EDGE, /* 101 */
308 1.1 igy VR4181GIU_INTTYP_HIGH_LEVEL, /* 110 */
309 1.1 igy VR4181GIU_INTTYP_FALLING_EDGE, /* 111 */
310 1.1 igy };
311 1.1 igy
312 1.1 igy raw_intr_type = intr_mode_trans[mode];
313 1.1 igy if (raw_intr_type == VR4181GIU_INTTYP_INVALID)
314 1.1 igy panic("vr4181giu_intr_establish: invalid interrupt mode.");
315 1.1 igy
316 1.1 igy if (port < 0 || MAX_GIU4181INTR <= port)
317 1.1 igy panic("vr4181giu_intr_establish: invalid interrupt line.");
318 1.1 igy if (!TAILQ_EMPTY(&sc->sc_intr_head[port])
319 1.1 igy && raw_intr_type != sc->sc_intr_mode[port])
320 1.1 igy panic("vr4181giu_intr_establish: "
321 1.1 igy "cannot use one line with two modes at a time.");
322 1.1 igy else
323 1.1 igy sc->sc_intr_mode[port] = raw_intr_type;
324 1.1 igy mask = (1 << port);
325 1.1 igy
326 1.1 igy s = splhigh();
327 1.1 igy
328 1.5.8.1 thorpej ih = kmem_alloc(sizeof *ih, KM_SLEEP);
329 1.1 igy ih->ih_port = port;
330 1.1 igy ih->ih_fun = ih_fun;
331 1.1 igy ih->ih_arg = ih_arg;
332 1.1 igy TAILQ_INSERT_TAIL(&sc->sc_intr_head[port], ih, ih_link);
333 1.1 igy
334 1.1 igy /*
335 1.1 igy * setup GIU registers
336 1.1 igy */
337 1.1 igy
338 1.1 igy /* disable interrupt at first */
339 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W);
340 1.1 igy r &= ~mask;
341 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W, r);
342 1.1 igy
343 1.1 igy /* mode */
344 1.1 igy regmod = port >> 3;
345 1.1 igy bitoff = (port & 0x7) << 1;
346 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
347 1.1 igy VR4181GIU_MODE0_REG_W + regmod);
348 1.1 igy r &= ~(0x3 << bitoff);
349 1.1 igy r |= (VR4181GIU_MODE_IN | VR4181GIU_MODE_GPIO) << bitoff;
350 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
351 1.1 igy VR4181GIU_MODE0_REG_W + regmod, r);
352 1.1 igy /* interrupt type */
353 1.1 igy reghl = port < 8 ? 2 : 0; /* high byte: 0x0, lowbyte: 0x2 */
354 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
355 1.1 igy VR4181GIU_INTTYP_REG + reghl);
356 1.1 igy r &= ~(0x3 << bitoff);
357 1.1 igy r |= raw_intr_type << bitoff;
358 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
359 1.1 igy VR4181GIU_INTTYP_REG + reghl, r);
360 1.1 igy
361 1.1 igy /* clear status */
362 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
363 1.1 igy VR4181GIU_INTSTAT_REG_W, mask);
364 1.1 igy
365 1.1 igy /* unmask */
366 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTMASK_REG_W);
367 1.1 igy r &= ~mask;
368 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTMASK_REG_W, r);
369 1.1 igy
370 1.1 igy /* enable */
371 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W);
372 1.1 igy r |= mask;
373 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTEN_REG_W, r);
374 1.1 igy
375 1.1 igy splx(s);
376 1.1 igy
377 1.1 igy return ih;
378 1.1 igy }
379 1.1 igy
380 1.1 igy static void
381 1.1 igy vr4181giu_intr_disestablish(hpcio_chip_t hc, void *arg)
382 1.1 igy {
383 1.1 igy }
384 1.1 igy
385 1.1 igy static void
386 1.1 igy vr4181giu_intr_clear(hpcio_chip_t hc, void *arg)
387 1.1 igy {
388 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
389 1.1 igy struct vr4181giu_intr_entry *ih = arg;
390 1.1 igy
391 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh,
392 1.1 igy VR4181GIU_INTSTAT_REG_W, 1 << ih->ih_port);
393 1.1 igy }
394 1.1 igy
395 1.1 igy static void
396 1.1 igy vr4181giu_register_iochip(hpcio_chip_t hc, hpcio_chip_t iochip)
397 1.1 igy {
398 1.1 igy struct vr4181giu_softc *sc = hc->hc_sc;
399 1.1 igy
400 1.1 igy vrip_register_gpio(sc->sc_vc, iochip);
401 1.1 igy }
402 1.1 igy
403 1.1 igy /*
404 1.1 igy * interrupt handler
405 1.1 igy */
406 1.1 igy static int
407 1.1 igy vr4181giu_intr(void *arg)
408 1.1 igy {
409 1.1 igy struct vr4181giu_softc *sc = arg;
410 1.1 igy int i;
411 1.1 igy u_int16_t r;
412 1.1 igy
413 1.1 igy r = bus_space_read_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTSTAT_REG_W);
414 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_ioh, VR4181GIU_INTSTAT_REG_W, r);
415 1.1 igy
416 1.1 igy for (i = 0; i < MAX_GIU4181INTR; i++) {
417 1.1 igy if (r & (1 << i)) {
418 1.1 igy struct vr4181giu_intr_entry *ih;
419 1.1 igy TAILQ_FOREACH(ih, &sc->sc_intr_head[i], ih_link) {
420 1.1 igy ih->ih_fun(ih->ih_arg);
421 1.1 igy }
422 1.1 igy }
423 1.1 igy }
424 1.1 igy
425 1.1 igy return 0;
426 1.1 igy }
427