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