epgpio.c revision 1.5 1 1.5 chs /* $NetBSD: epgpio.c,v 1.5 2012/10/27 17:17:37 chs Exp $ */
2 1.1 hamajima
3 1.1 hamajima /*
4 1.1 hamajima * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
5 1.1 hamajima *
6 1.1 hamajima * Redistribution and use in source and binary forms, with or without
7 1.1 hamajima * modification, are permitted provided that the following conditions
8 1.1 hamajima * are met:
9 1.1 hamajima * 1. Redistributions of source code must retain the above copyright
10 1.1 hamajima * notice, this list of conditions and the following disclaimer.
11 1.1 hamajima * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 hamajima * notice, this list of conditions and the following disclaimer in the
13 1.1 hamajima * documentation and/or other materials provided with the distribution.
14 1.1 hamajima *
15 1.1 hamajima * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 hamajima * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 hamajima * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 hamajima * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 hamajima * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 hamajima * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 hamajima * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 hamajima * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 hamajima * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 hamajima * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 hamajima * SUCH DAMAGE.
26 1.1 hamajima */
27 1.1 hamajima
28 1.1 hamajima #include <sys/cdefs.h>
29 1.5 chs __KERNEL_RCSID(0, "$NetBSD: epgpio.c,v 1.5 2012/10/27 17:17:37 chs Exp $");
30 1.1 hamajima
31 1.1 hamajima #include <sys/param.h>
32 1.1 hamajima #include <sys/systm.h>
33 1.1 hamajima #include <sys/kernel.h>
34 1.1 hamajima #include <sys/device.h>
35 1.4 dyoung #include <sys/bus.h>
36 1.1 hamajima #include <machine/intr.h>
37 1.3 he #include <sys/gpio.h>
38 1.1 hamajima #include <dev/gpio/gpiovar.h>
39 1.1 hamajima #include <arm/ep93xx/ep93xxvar.h>
40 1.1 hamajima #include <arm/ep93xx/epsocvar.h>
41 1.1 hamajima #include <arm/ep93xx/epgpioreg.h>
42 1.1 hamajima #include <arm/ep93xx/epgpiovar.h>
43 1.2 kenh #include "opt_ep93xx_gpio_mask.h"
44 1.1 hamajima #include "gpio.h"
45 1.1 hamajima #include "locators.h"
46 1.1 hamajima
47 1.1 hamajima #ifdef EPGPIO_DEBUG
48 1.1 hamajima int epgpio_debug = EPGPIO_DEBUG;
49 1.1 hamajima #define DPRINTFN(n,x) if (epgpio_debug>(n)) printf x;
50 1.1 hamajima #else
51 1.1 hamajima #define DPRINTFN(n,x)
52 1.1 hamajima #endif
53 1.1 hamajima
54 1.1 hamajima #define EPGPIO_NPORTS 8
55 1.1 hamajima #define EPGPIO_NPINS 8
56 1.1 hamajima
57 1.1 hamajima struct port_info {
58 1.1 hamajima struct epgpio_softc *sc;
59 1.1 hamajima int unit;
60 1.1 hamajima #if NGPIO > 0
61 1.1 hamajima struct gpio_chipset_tag gpio_chipset;
62 1.1 hamajima gpio_pin_t pins[EPGPIO_NPINS];
63 1.2 kenh int gpio_mask;
64 1.2 kenh int gpio_npins;
65 1.1 hamajima #endif
66 1.1 hamajima bus_size_t pxdr;
67 1.1 hamajima bus_size_t pxddr;
68 1.1 hamajima bus_size_t xinten;
69 1.1 hamajima bus_size_t xinttype1;
70 1.1 hamajima bus_size_t xinttype2;
71 1.1 hamajima bus_size_t xeoi;
72 1.1 hamajima bus_size_t xdb;
73 1.1 hamajima };
74 1.1 hamajima
75 1.1 hamajima struct intr_req {
76 1.1 hamajima int irq;
77 1.1 hamajima int (*ih_func)(void *);
78 1.1 hamajima int (*ireq_func)(void *);
79 1.1 hamajima void *ireq_arg;
80 1.1 hamajima void *cookie;
81 1.1 hamajima };
82 1.1 hamajima
83 1.1 hamajima struct epgpio_softc {
84 1.1 hamajima bus_space_tag_t sc_iot;
85 1.1 hamajima bus_space_handle_t sc_ioh;
86 1.1 hamajima struct port_info sc_port[EPGPIO_NPORTS];
87 1.1 hamajima struct intr_req sc_ireq_combine;
88 1.1 hamajima struct intr_req sc_ireq_f[EPGPIO_NPINS];
89 1.1 hamajima };
90 1.1 hamajima
91 1.5 chs static int epgpio_match(device_t, cfdata_t, void *);
92 1.5 chs static void epgpio_attach(device_t, device_t, void *);
93 1.1 hamajima
94 1.1 hamajima #if NGPIO > 0
95 1.1 hamajima static int epgpiobus_print(void *, const char *);
96 1.1 hamajima static int epgpio_pin_read(void *, int);
97 1.1 hamajima static void epgpio_pin_write(void *, int, int);
98 1.1 hamajima static void epgpio_pin_ctl(void *, int, int);
99 1.1 hamajima #endif
100 1.1 hamajima
101 1.5 chs static int epgpio_search(device_t, cfdata_t, const int *, void *);
102 1.1 hamajima static int epgpio_print(void *, const char *);
103 1.1 hamajima
104 1.1 hamajima static int epgpio_intr_combine(void* arg);
105 1.1 hamajima static int epgpio_intr_f(void* arg, int);
106 1.1 hamajima static int epgpio_intr_0(void* arg);
107 1.1 hamajima static int epgpio_intr_1(void* arg);
108 1.1 hamajima static int epgpio_intr_2(void* arg);
109 1.1 hamajima static int epgpio_intr_3(void* arg);
110 1.1 hamajima static int epgpio_intr_4(void* arg);
111 1.1 hamajima static int epgpio_intr_5(void* arg);
112 1.1 hamajima static int epgpio_intr_6(void* arg);
113 1.1 hamajima static int epgpio_intr_7(void* arg);
114 1.1 hamajima
115 1.1 hamajima static void epgpio_bit_set(struct epgpio_softc *, bus_size_t, int);
116 1.1 hamajima static void epgpio_bit_clear(struct epgpio_softc *, bus_size_t, int);
117 1.1 hamajima
118 1.5 chs CFATTACH_DECL_NEW(epgpio, sizeof(struct epgpio_softc),
119 1.1 hamajima epgpio_match, epgpio_attach, NULL, NULL);
120 1.1 hamajima
121 1.1 hamajima static int
122 1.5 chs epgpio_match(device_t parent, cfdata_t match, void *aux)
123 1.1 hamajima {
124 1.1 hamajima return 2;
125 1.1 hamajima }
126 1.1 hamajima
127 1.1 hamajima static void
128 1.5 chs epgpio_attach(device_t parent, device_t self, void *aux)
129 1.1 hamajima {
130 1.5 chs struct epgpio_softc *sc = device_private(self);
131 1.1 hamajima struct epsoc_attach_args *sa = aux;
132 1.1 hamajima struct port_info *pi;
133 1.1 hamajima #if NGPIO > 0
134 1.1 hamajima struct gpiobus_attach_args gba;
135 1.1 hamajima int dir, val;
136 1.2 kenh int i, j, pin;
137 1.1 hamajima #endif
138 1.1 hamajima
139 1.1 hamajima printf("\n");
140 1.1 hamajima sc->sc_iot = sa->sa_iot;
141 1.1 hamajima
142 1.1 hamajima if (bus_space_map(sa->sa_iot, sa->sa_addr,
143 1.1 hamajima sa->sa_size, 0, &sc->sc_ioh)){
144 1.5 chs printf("%s: Cannot map registers", device_xname(self));
145 1.1 hamajima return;
146 1.1 hamajima }
147 1.1 hamajima
148 1.1 hamajima /* PORT A */
149 1.1 hamajima pi = &sc->sc_port[0];
150 1.1 hamajima pi->unit = 0;
151 1.1 hamajima pi->sc = sc;
152 1.1 hamajima pi->pxdr = EP93XX_GPIO_PADR;
153 1.1 hamajima pi->pxddr = EP93XX_GPIO_PADDR;
154 1.1 hamajima pi->xinten = EP93XX_GPIO_AIntEn;
155 1.1 hamajima pi->xinttype1 = EP93XX_GPIO_AIntType1;
156 1.1 hamajima pi->xinttype2 = EP93XX_GPIO_AIntType2;
157 1.1 hamajima pi->xeoi = EP93XX_GPIO_AEOI;
158 1.1 hamajima pi->xdb = EP93XX_GPIO_ADB;
159 1.2 kenh #if NGPIO > 0
160 1.2 kenh pi->gpio_mask = EPGPIO_PORT_A_MASK;
161 1.2 kenh #endif
162 1.1 hamajima bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xinten, 0);
163 1.1 hamajima /* PORT B */
164 1.1 hamajima pi = &sc->sc_port[1];
165 1.1 hamajima pi->unit = 1;
166 1.1 hamajima pi->sc = sc;
167 1.1 hamajima pi->pxdr = EP93XX_GPIO_PBDR;
168 1.1 hamajima pi->pxddr = EP93XX_GPIO_PBDDR;
169 1.1 hamajima pi->xinten = EP93XX_GPIO_BIntEn;
170 1.1 hamajima pi->xinttype1 = EP93XX_GPIO_BIntType1;
171 1.1 hamajima pi->xinttype2 = EP93XX_GPIO_BIntType2;
172 1.1 hamajima pi->xeoi = EP93XX_GPIO_BEOI;
173 1.1 hamajima pi->xdb = EP93XX_GPIO_BDB;
174 1.2 kenh #if NGPIO > 0
175 1.2 kenh pi->gpio_mask = EPGPIO_PORT_B_MASK;
176 1.2 kenh #endif
177 1.1 hamajima bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xinten, 0);
178 1.1 hamajima /* PORT C */
179 1.1 hamajima pi = &sc->sc_port[2];
180 1.1 hamajima pi->unit = 2;
181 1.1 hamajima pi->sc = sc;
182 1.1 hamajima pi->pxdr = EP93XX_GPIO_PCDR;
183 1.1 hamajima pi->pxddr = EP93XX_GPIO_PCDDR;
184 1.1 hamajima pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
185 1.2 kenh #if NGPIO > 0
186 1.2 kenh pi->gpio_mask = EPGPIO_PORT_C_MASK;
187 1.2 kenh #endif
188 1.1 hamajima /* PORT D */
189 1.1 hamajima pi = &sc->sc_port[3];
190 1.1 hamajima pi->unit = 3;
191 1.1 hamajima pi->sc = sc;
192 1.1 hamajima pi->pxdr = EP93XX_GPIO_PDDR;
193 1.1 hamajima pi->pxddr = EP93XX_GPIO_PDDDR;
194 1.1 hamajima pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
195 1.2 kenh #if NGPIO > 0
196 1.2 kenh pi->gpio_mask = EPGPIO_PORT_D_MASK;
197 1.2 kenh #endif
198 1.1 hamajima /* PORT E */
199 1.1 hamajima pi = &sc->sc_port[4];
200 1.1 hamajima pi->unit = 4;
201 1.1 hamajima pi->sc = sc;
202 1.1 hamajima pi->pxdr = EP93XX_GPIO_PEDR;
203 1.1 hamajima pi->pxddr = EP93XX_GPIO_PEDDR;
204 1.1 hamajima pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
205 1.2 kenh #if NGPIO > 0
206 1.2 kenh pi->gpio_mask = EPGPIO_PORT_E_MASK;
207 1.2 kenh #endif
208 1.1 hamajima /* PORT F */
209 1.1 hamajima pi = &sc->sc_port[5];
210 1.1 hamajima pi->unit = 5;
211 1.1 hamajima pi->sc = sc;
212 1.1 hamajima pi->pxdr = EP93XX_GPIO_PFDR;
213 1.1 hamajima pi->pxddr = EP93XX_GPIO_PFDDR;
214 1.1 hamajima pi->xinten = EP93XX_GPIO_FIntEn;
215 1.1 hamajima pi->xinttype1 = EP93XX_GPIO_FIntType1;
216 1.1 hamajima pi->xinttype2 = EP93XX_GPIO_FIntType2;
217 1.1 hamajima pi->xeoi = EP93XX_GPIO_FEOI;
218 1.1 hamajima pi->xdb = EP93XX_GPIO_FDB;
219 1.2 kenh #if NGPIO > 0
220 1.2 kenh pi->gpio_mask = EPGPIO_PORT_F_MASK;
221 1.2 kenh #endif
222 1.1 hamajima bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xinten, 0);
223 1.1 hamajima /* PORT G */
224 1.1 hamajima pi = &sc->sc_port[6];
225 1.1 hamajima pi->unit = 6;
226 1.1 hamajima pi->sc = sc;
227 1.1 hamajima pi->pxdr = EP93XX_GPIO_PGDR;
228 1.1 hamajima pi->pxddr = EP93XX_GPIO_PGDDR;
229 1.1 hamajima pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
230 1.2 kenh #if NGPIO > 0
231 1.2 kenh pi->gpio_mask = EPGPIO_PORT_G_MASK;
232 1.2 kenh #endif
233 1.1 hamajima /* PORT H */
234 1.1 hamajima pi = &sc->sc_port[7];
235 1.1 hamajima pi->unit = 7;
236 1.1 hamajima pi->sc = sc;
237 1.1 hamajima pi->pxdr = EP93XX_GPIO_PHDR;
238 1.1 hamajima pi->pxddr = EP93XX_GPIO_PHDDR;
239 1.1 hamajima pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
240 1.2 kenh #if NGPIO > 0
241 1.2 kenh pi->gpio_mask = EPGPIO_PORT_H_MASK;
242 1.2 kenh #endif
243 1.1 hamajima
244 1.1 hamajima /* PORT A & B */
245 1.1 hamajima sc->sc_ireq_combine.irq = EP93XX_GPIO_INTR;
246 1.1 hamajima sc->sc_ireq_combine.ih_func = epgpio_intr_combine;
247 1.1 hamajima /* PORT F */
248 1.1 hamajima sc->sc_ireq_f[0].irq = EP93XX_GPIO0_INTR;
249 1.1 hamajima sc->sc_ireq_f[0].ih_func = epgpio_intr_0;
250 1.1 hamajima sc->sc_ireq_f[1].irq = EP93XX_GPIO1_INTR;
251 1.1 hamajima sc->sc_ireq_f[1].ih_func = epgpio_intr_1;
252 1.1 hamajima sc->sc_ireq_f[2].irq = EP93XX_GPIO2_INTR;
253 1.1 hamajima sc->sc_ireq_f[2].ih_func = epgpio_intr_2;
254 1.1 hamajima sc->sc_ireq_f[3].irq = EP93XX_GPIO3_INTR;
255 1.1 hamajima sc->sc_ireq_f[3].ih_func = epgpio_intr_3;
256 1.1 hamajima sc->sc_ireq_f[4].irq = EP93XX_GPIO4_INTR;
257 1.1 hamajima sc->sc_ireq_f[4].ih_func = epgpio_intr_4;
258 1.1 hamajima sc->sc_ireq_f[5].irq = EP93XX_GPIO5_INTR;
259 1.1 hamajima sc->sc_ireq_f[5].ih_func = epgpio_intr_5;
260 1.1 hamajima sc->sc_ireq_f[6].irq = EP93XX_GPIO6_INTR;
261 1.1 hamajima sc->sc_ireq_f[6].ih_func = epgpio_intr_6;
262 1.1 hamajima sc->sc_ireq_f[7].irq = EP93XX_GPIO7_INTR;
263 1.1 hamajima sc->sc_ireq_f[7].ih_func = epgpio_intr_7;
264 1.1 hamajima
265 1.1 hamajima #if NGPIO > 0
266 1.1 hamajima /* initialize and attach gpio(4) */
267 1.1 hamajima for (i = 0; i < EPGPIO_NPORTS; i++) {
268 1.1 hamajima pi = &sc->sc_port[i];
269 1.2 kenh /*
270 1.2 kenh * If this port is completely disabled for gpio attachment,
271 1.2 kenh * then skip it.
272 1.2 kenh */
273 1.2 kenh if (pi->gpio_mask == 0x00)
274 1.2 kenh continue;
275 1.2 kenh
276 1.1 hamajima dir = bus_space_read_4(sc->sc_iot, sc->sc_ioh, pi->pxddr) & 0xff;
277 1.1 hamajima val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, pi->pxdr) & 0xff;
278 1.2 kenh
279 1.2 kenh /*
280 1.2 kenh * pin_num doesn't seem to be used for anything in the GPIO
281 1.2 kenh * code. So we're going to use it to refer to the REAL pin
282 1.2 kenh * on the port. Just to keep things straight below:
283 1.2 kenh *
284 1.2 kenh * pin - The pin number as seen by the GPIO code
285 1.2 kenh * j - The ACTUAL pin on the port
286 1.2 kenh */
287 1.2 kenh
288 1.2 kenh for (j = 0, pin = 0; j < EPGPIO_NPINS; j++) {
289 1.2 kenh if (pi->gpio_mask & (1 << j)) {
290 1.2 kenh pi->pins[pin].pin_num = j;
291 1.2 kenh pi->pins[pin].pin_caps = (GPIO_PIN_INPUT
292 1.2 kenh | GPIO_PIN_OUTPUT);
293 1.2 kenh if((dir >> j) & 0x01)
294 1.2 kenh pi->pins[pin].pin_flags =
295 1.2 kenh GPIO_PIN_OUTPUT;
296 1.2 kenh else
297 1.2 kenh pi->pins[pin].pin_flags =
298 1.2 kenh GPIO_PIN_INPUT;
299 1.2 kenh if((val >> j) & 0x01)
300 1.2 kenh pi->pins[pin].pin_state = GPIO_PIN_HIGH;
301 1.2 kenh else
302 1.2 kenh pi->pins[pin].pin_state = GPIO_PIN_LOW;
303 1.2 kenh pin++;
304 1.2 kenh }
305 1.1 hamajima }
306 1.1 hamajima pi->gpio_chipset.gp_cookie = pi;
307 1.1 hamajima pi->gpio_chipset.gp_pin_read = epgpio_pin_read;
308 1.1 hamajima pi->gpio_chipset.gp_pin_write = epgpio_pin_write;
309 1.1 hamajima pi->gpio_chipset.gp_pin_ctl = epgpio_pin_ctl;
310 1.1 hamajima gba.gba_gc = &pi->gpio_chipset;
311 1.1 hamajima gba.gba_pins = pi->pins;
312 1.2 kenh gba.gba_npins = pin;
313 1.1 hamajima config_found_ia(self, "gpiobus", &gba, epgpiobus_print);
314 1.1 hamajima }
315 1.1 hamajima #endif
316 1.1 hamajima
317 1.1 hamajima config_search_ia(epgpio_search, self, "epgpio", epgpio_print);
318 1.1 hamajima }
319 1.1 hamajima
320 1.1 hamajima #if NGPIO > 0
321 1.1 hamajima static int
322 1.1 hamajima epgpiobus_print(void *aux, const char *name)
323 1.1 hamajima {
324 1.1 hamajima struct gpiobus_attach_args *gba = aux;
325 1.1 hamajima struct port_info *pi = (struct port_info *)gba->gba_gc->gp_cookie;
326 1.1 hamajima
327 1.1 hamajima gpiobus_print(aux, name);
328 1.1 hamajima aprint_normal(": port %c", pi->unit+'A');
329 1.1 hamajima
330 1.1 hamajima return (UNCONF);
331 1.1 hamajima }
332 1.1 hamajima #endif
333 1.1 hamajima
334 1.1 hamajima
335 1.1 hamajima static int
336 1.5 chs epgpio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
337 1.1 hamajima {
338 1.5 chs struct epgpio_softc *sc = device_private(parent);
339 1.1 hamajima struct epgpio_attach_args ga;
340 1.1 hamajima
341 1.1 hamajima ga.ga_gc = sc;
342 1.1 hamajima ga.ga_iot = sc->sc_iot;
343 1.1 hamajima ga.ga_port = cf->cf_loc[EPGPIOCF_PORT];
344 1.1 hamajima ga.ga_bit1 = cf->cf_loc[EPGPIOCF_BIT1];
345 1.1 hamajima ga.ga_bit2 = cf->cf_loc[EPGPIOCF_BIT2];
346 1.1 hamajima
347 1.1 hamajima if (config_match(parent, cf, &ga) > 0)
348 1.1 hamajima config_attach(parent, cf, &ga, epgpio_print);
349 1.1 hamajima
350 1.1 hamajima return 0;
351 1.1 hamajima }
352 1.1 hamajima
353 1.1 hamajima static int
354 1.1 hamajima epgpio_print(void *aux, const char *name)
355 1.1 hamajima {
356 1.5 chs struct epgpio_attach_args *ga = aux;
357 1.5 chs struct epgpio_softc *sc = ga->ga_gc;
358 1.1 hamajima
359 1.1 hamajima aprint_normal(":");
360 1.1 hamajima if (ga->ga_port > -1)
361 1.1 hamajima aprint_normal(" port %c", sc->sc_port[ga->ga_port].unit+'A');
362 1.1 hamajima if (ga->ga_bit1 > -1)
363 1.1 hamajima aprint_normal(" bit1 %d", ga->ga_bit1);
364 1.1 hamajima if (ga->ga_bit2 > -1)
365 1.1 hamajima aprint_normal(" bit2 %d", ga->ga_bit2);
366 1.1 hamajima
367 1.1 hamajima return (UNCONF);
368 1.1 hamajima }
369 1.1 hamajima
370 1.1 hamajima int
371 1.1 hamajima epgpio_read(struct epgpio_softc *sc, epgpio_port port, int bit)
372 1.1 hamajima {
373 1.1 hamajima struct port_info *pi = &sc->sc_port[port];
374 1.1 hamajima
375 1.1 hamajima #if NGPIO > 0
376 1.1 hamajima pi->pins[bit].pin_caps = 0;
377 1.1 hamajima #endif
378 1.1 hamajima return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, pi->pxdr) >> bit) & 1;
379 1.1 hamajima }
380 1.1 hamajima
381 1.1 hamajima void
382 1.1 hamajima epgpio_set(struct epgpio_softc *sc, epgpio_port port, int bit)
383 1.1 hamajima {
384 1.1 hamajima struct port_info *pi = &sc->sc_port[port];
385 1.1 hamajima
386 1.1 hamajima #if NGPIO > 0
387 1.1 hamajima pi->pins[bit].pin_caps = 0;
388 1.1 hamajima #endif
389 1.1 hamajima epgpio_bit_set(sc, pi->pxdr, bit);
390 1.1 hamajima }
391 1.1 hamajima
392 1.1 hamajima void
393 1.1 hamajima epgpio_clear(struct epgpio_softc *sc, epgpio_port port, int bit)
394 1.1 hamajima {
395 1.1 hamajima struct port_info *pi = &sc->sc_port[port];
396 1.1 hamajima
397 1.1 hamajima #if NGPIO > 0
398 1.1 hamajima pi->pins[bit].pin_caps = 0;
399 1.1 hamajima #endif
400 1.1 hamajima epgpio_bit_clear(sc, pi->pxdr, bit);
401 1.1 hamajima }
402 1.1 hamajima
403 1.1 hamajima void
404 1.1 hamajima epgpio_in(struct epgpio_softc *sc, epgpio_port port, int bit)
405 1.1 hamajima {
406 1.1 hamajima struct port_info *pi = &sc->sc_port[port];
407 1.1 hamajima
408 1.1 hamajima #if NGPIO > 0
409 1.1 hamajima pi->pins[bit].pin_caps = 0;
410 1.1 hamajima #endif
411 1.1 hamajima epgpio_bit_clear(sc, pi->pxddr, bit);
412 1.1 hamajima }
413 1.1 hamajima
414 1.1 hamajima void
415 1.1 hamajima epgpio_out(struct epgpio_softc *sc, epgpio_port port, int bit)
416 1.1 hamajima {
417 1.1 hamajima struct port_info *pi = &sc->sc_port[port];
418 1.1 hamajima
419 1.1 hamajima #if NGPIO > 0
420 1.1 hamajima pi->pins[bit].pin_caps = 0;
421 1.1 hamajima #endif
422 1.1 hamajima epgpio_bit_set(sc, pi->pxddr, bit);
423 1.1 hamajima }
424 1.1 hamajima
425 1.1 hamajima void *
426 1.1 hamajima epgpio_intr_establish(struct epgpio_softc *sc, epgpio_port port, int bit,
427 1.1 hamajima int flag, int ipl, int (*ireq_func)(void *), void *arg) {
428 1.1 hamajima struct port_info *pi;
429 1.1 hamajima struct intr_req *intq;
430 1.1 hamajima
431 1.1 hamajima DPRINTFN(1, ("epgpio_intr_establish: port=%d, bit=%d, flag=%#x\n",port,bit,flag));
432 1.1 hamajima
433 1.1 hamajima if (bit < 0 || bit >= EPGPIO_NPINS)
434 1.1 hamajima return 0;
435 1.1 hamajima
436 1.1 hamajima switch (port) {
437 1.1 hamajima case PORT_A:
438 1.1 hamajima case PORT_B:
439 1.1 hamajima intq = &sc->sc_ireq_combine;
440 1.1 hamajima break;
441 1.1 hamajima case PORT_F:
442 1.1 hamajima intq = &sc->sc_ireq_f[bit];
443 1.1 hamajima break;
444 1.1 hamajima default:
445 1.1 hamajima return 0;
446 1.1 hamajima };
447 1.1 hamajima
448 1.1 hamajima if (intq->ireq_func)
449 1.1 hamajima return 0; /* already used */
450 1.1 hamajima
451 1.1 hamajima intq->ireq_func = ireq_func;
452 1.1 hamajima intq->ireq_arg = arg;
453 1.1 hamajima
454 1.1 hamajima pi = &sc->sc_port[port];
455 1.1 hamajima epgpio_bit_clear(sc, pi->xinten, bit);
456 1.1 hamajima epgpio_in(sc, port, bit);
457 1.1 hamajima #if NGPIO > 0
458 1.1 hamajima pi->pins[bit].pin_caps = 0;
459 1.1 hamajima #endif
460 1.1 hamajima
461 1.1 hamajima if (flag & EDGE_TRIGGER)
462 1.1 hamajima epgpio_bit_set(sc, pi->xinttype1, bit);
463 1.1 hamajima else /* LEVEL_SENSE */
464 1.1 hamajima epgpio_bit_clear(sc, pi->xinttype1, bit);
465 1.1 hamajima if (flag & RISING_EDGE) /* or HIGH_LEVEL */
466 1.1 hamajima epgpio_bit_set(sc, pi->xinttype2, bit);
467 1.1 hamajima else /* FALLING_EDGE or LOW_LEVEL */
468 1.1 hamajima epgpio_bit_clear(sc, pi->xinttype2, bit);
469 1.1 hamajima if (flag & DEBOUNCE)
470 1.1 hamajima epgpio_bit_set(sc, pi->xdb, bit);
471 1.1 hamajima else
472 1.1 hamajima epgpio_bit_clear(sc, pi->xdb, bit);
473 1.1 hamajima
474 1.1 hamajima if (!intq->cookie)
475 1.1 hamajima intq->cookie = ep93xx_intr_establish(intq->irq, ipl,
476 1.1 hamajima intq->ih_func, pi);
477 1.1 hamajima bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xeoi, 1 << bit);
478 1.1 hamajima epgpio_bit_set(sc, pi->xinten, bit);
479 1.1 hamajima return intq->cookie;
480 1.1 hamajima }
481 1.1 hamajima
482 1.1 hamajima void
483 1.1 hamajima epgpio_intr_disestablish(struct epgpio_softc *sc, epgpio_port port, int bit)
484 1.1 hamajima {
485 1.1 hamajima struct port_info *pi;
486 1.1 hamajima struct intr_req *intq;
487 1.1 hamajima
488 1.1 hamajima DPRINTFN(1, ("epgpio_intr_disestablish: port=%d, bit=%d\n",port,bit));
489 1.1 hamajima
490 1.1 hamajima if (bit < 0 || bit >= EPGPIO_NPINS)
491 1.1 hamajima return;
492 1.1 hamajima
493 1.1 hamajima switch (port) {
494 1.1 hamajima case PORT_A:
495 1.1 hamajima case PORT_B:
496 1.1 hamajima intq = &sc->sc_ireq_combine;
497 1.1 hamajima break;
498 1.1 hamajima case PORT_F:
499 1.1 hamajima intq = &sc->sc_ireq_f[bit];
500 1.1 hamajima break;
501 1.1 hamajima default:
502 1.1 hamajima return;
503 1.1 hamajima };
504 1.1 hamajima
505 1.1 hamajima if (!intq->ireq_func)
506 1.1 hamajima return;
507 1.1 hamajima
508 1.1 hamajima pi = &sc->sc_port[port];
509 1.1 hamajima epgpio_bit_clear(sc, pi->xinten, bit);
510 1.1 hamajima intq->ireq_func = 0;
511 1.1 hamajima intq->ireq_arg = 0;
512 1.1 hamajima ep93xx_intr_disestablish(intq->cookie);
513 1.1 hamajima intq->cookie = 0;
514 1.1 hamajima }
515 1.1 hamajima
516 1.1 hamajima static int
517 1.1 hamajima epgpio_intr_combine(void *arg)
518 1.1 hamajima {
519 1.1 hamajima struct port_info *pi = arg;
520 1.1 hamajima struct epgpio_softc *sc = pi->sc;
521 1.1 hamajima struct intr_req *intq = &sc->sc_ireq_combine;
522 1.1 hamajima int err = 0;
523 1.1 hamajima
524 1.1 hamajima DPRINTFN(1, ("epgpio_intr_combine\n"));
525 1.1 hamajima
526 1.1 hamajima if (intq->ireq_func)
527 1.1 hamajima err = (*intq->ireq_func)(intq->ireq_arg);
528 1.1 hamajima epgpio_bit_set(sc, pi->xeoi, 0xff);
529 1.1 hamajima return err;
530 1.1 hamajima }
531 1.1 hamajima
532 1.1 hamajima static int
533 1.1 hamajima epgpio_intr_f(void *arg, int bit)
534 1.1 hamajima {
535 1.1 hamajima struct port_info *pi = arg;
536 1.1 hamajima struct epgpio_softc *sc = pi->sc;
537 1.1 hamajima struct intr_req *intq = &sc->sc_ireq_f[bit];
538 1.1 hamajima int err = 0;
539 1.1 hamajima
540 1.1 hamajima DPRINTFN(1, ("epgpio_intr_%d\n", bit));
541 1.1 hamajima
542 1.1 hamajima if (intq->ireq_func)
543 1.1 hamajima err = (*intq->ireq_func)(intq->ireq_arg);
544 1.1 hamajima epgpio_bit_set(sc, pi->xeoi, bit);
545 1.1 hamajima return err;
546 1.1 hamajima }
547 1.1 hamajima
548 1.1 hamajima static int
549 1.1 hamajima epgpio_intr_0(void *arg)
550 1.1 hamajima {
551 1.1 hamajima return epgpio_intr_f(arg, 0);
552 1.1 hamajima }
553 1.1 hamajima
554 1.1 hamajima static int
555 1.1 hamajima epgpio_intr_1(void *arg)
556 1.1 hamajima {
557 1.1 hamajima return epgpio_intr_f(arg, 1);
558 1.1 hamajima }
559 1.1 hamajima
560 1.1 hamajima static int
561 1.1 hamajima epgpio_intr_2(void *arg)
562 1.1 hamajima {
563 1.1 hamajima return epgpio_intr_f(arg, 2);
564 1.1 hamajima }
565 1.1 hamajima
566 1.1 hamajima static int
567 1.1 hamajima epgpio_intr_3(void *arg)
568 1.1 hamajima {
569 1.1 hamajima return epgpio_intr_f(arg, 3);
570 1.1 hamajima }
571 1.1 hamajima
572 1.1 hamajima static int
573 1.1 hamajima epgpio_intr_4(void *arg)
574 1.1 hamajima {
575 1.1 hamajima return epgpio_intr_f(arg, 4);
576 1.1 hamajima }
577 1.1 hamajima
578 1.1 hamajima static int
579 1.1 hamajima epgpio_intr_5(void *arg)
580 1.1 hamajima {
581 1.1 hamajima return epgpio_intr_f(arg, 5);
582 1.1 hamajima }
583 1.1 hamajima
584 1.1 hamajima static int
585 1.1 hamajima epgpio_intr_6(void *arg)
586 1.1 hamajima {
587 1.1 hamajima return epgpio_intr_f(arg, 6);
588 1.1 hamajima }
589 1.1 hamajima
590 1.1 hamajima static int
591 1.1 hamajima epgpio_intr_7(void *arg)
592 1.1 hamajima {
593 1.1 hamajima return epgpio_intr_f(arg, 7);
594 1.1 hamajima }
595 1.1 hamajima
596 1.1 hamajima #if NGPIO > 0
597 1.1 hamajima static int
598 1.1 hamajima epgpio_pin_read(void *arg, int pin)
599 1.1 hamajima {
600 1.1 hamajima struct port_info *pi = arg;
601 1.1 hamajima struct epgpio_softc *sc = pi->sc;
602 1.1 hamajima
603 1.2 kenh pin %= pi->gpio_npins;
604 1.1 hamajima if (!pi->pins[pin].pin_caps)
605 1.1 hamajima return 0; /* EBUSY? */
606 1.1 hamajima
607 1.1 hamajima return (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
608 1.2 kenh pi->pxdr) >> pi->pins[pin].pin_num) & 1;
609 1.1 hamajima }
610 1.1 hamajima
611 1.1 hamajima static void
612 1.1 hamajima epgpio_pin_write(void *arg, int pin, int val)
613 1.1 hamajima {
614 1.1 hamajima struct port_info *pi = arg;
615 1.1 hamajima struct epgpio_softc *sc = pi->sc;
616 1.1 hamajima
617 1.2 kenh pin %= pi->gpio_npins;
618 1.1 hamajima if (!pi->pins[pin].pin_caps)
619 1.1 hamajima return;
620 1.1 hamajima
621 1.1 hamajima if (val)
622 1.2 kenh epgpio_bit_set(sc, pi->pxdr, pi->pins[pin].pin_num);
623 1.1 hamajima else
624 1.2 kenh epgpio_bit_clear(sc, pi->pxdr, pi->pins[pin].pin_num);
625 1.1 hamajima }
626 1.1 hamajima
627 1.1 hamajima static void
628 1.1 hamajima epgpio_pin_ctl(void *arg, int pin, int flags)
629 1.1 hamajima {
630 1.1 hamajima struct port_info *pi = arg;
631 1.1 hamajima struct epgpio_softc *sc = pi->sc;
632 1.1 hamajima
633 1.2 kenh pin %= pi->gpio_npins;
634 1.1 hamajima if (!pi->pins[pin].pin_caps)
635 1.1 hamajima return;
636 1.1 hamajima
637 1.1 hamajima if (flags & GPIO_PIN_INPUT)
638 1.2 kenh epgpio_bit_clear(sc, pi->pxddr, pi->pins[pin].pin_num);
639 1.1 hamajima else if (flags & GPIO_PIN_OUTPUT)
640 1.2 kenh epgpio_bit_set(sc, pi->pxddr, pi->pins[pin].pin_num);
641 1.1 hamajima }
642 1.1 hamajima #endif
643 1.1 hamajima
644 1.1 hamajima static void
645 1.1 hamajima epgpio_bit_set(struct epgpio_softc *sc, bus_size_t reg, int bit)
646 1.1 hamajima {
647 1.1 hamajima int t = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg) & 0xff;
648 1.1 hamajima bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, t | (1 << bit));
649 1.1 hamajima }
650 1.1 hamajima
651 1.1 hamajima static void
652 1.1 hamajima epgpio_bit_clear(struct epgpio_softc *sc, bus_size_t reg, int bit)
653 1.1 hamajima {
654 1.1 hamajima int t = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg) & 0xff;
655 1.1 hamajima bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, t & ~(1 << bit));
656 1.1 hamajima }
657