xref: /freebsd/sys/arm/freescale/imx/imx_gpio.c (revision 4f52dfbb)
1 /*-
2  * Copyright (c) 2012, 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Oleksandr Rybalko under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1.	Redistributions of source code must retain the above copyright
12  *	notice, this list of conditions and the following disclaimer.
13  * 2.	Redistributions in binary form must reproduce the above copyright
14  *	notice, this list of conditions and the following disclaimer in the
15  *	documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Freescale i.MX515 GPIO driver.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_platform.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/rman.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/gpio.h>
49 #include <sys/proc.h>
50 
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 #include <machine/resource.h>
54 
55 #include <dev/gpio/gpiobusvar.h>
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59 
60 #include "gpio_if.h"
61 
62 #ifdef INTRNG
63 #include "pic_if.h"
64 #endif
65 
66 #define	WRITE4(_sc, _r, _v)						\
67 	    bus_space_write_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r), (_v))
68 #define	READ4(_sc, _r)							\
69 	    bus_space_read_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r))
70 #define	SET4(_sc, _r, _m)						\
71 	    WRITE4((_sc), (_r), READ4((_sc), (_r)) | (_m))
72 #define	CLEAR4(_sc, _r, _m)						\
73 	    WRITE4((_sc), (_r), READ4((_sc), (_r)) & ~(_m))
74 
75 /* Registers definition for Freescale i.MX515 GPIO controller */
76 
77 #define	IMX_GPIO_DR_REG		0x000 /* Pin Data */
78 #define	IMX_GPIO_OE_REG		0x004 /* Set Pin Output */
79 #define	IMX_GPIO_PSR_REG	0x008 /* Pad Status */
80 #define	IMX_GPIO_ICR1_REG	0x00C /* Interrupt Configuration */
81 #define	IMX_GPIO_ICR2_REG	0x010 /* Interrupt Configuration */
82 #define		GPIO_ICR_COND_LOW	0
83 #define		GPIO_ICR_COND_HIGH	1
84 #define		GPIO_ICR_COND_RISE	2
85 #define		GPIO_ICR_COND_FALL	3
86 #define		GPIO_ICR_COND_MASK	0x3
87 #define	IMX_GPIO_IMR_REG	0x014 /* Interrupt Mask Register */
88 #define	IMX_GPIO_ISR_REG	0x018 /* Interrupt Status Register */
89 #define	IMX_GPIO_EDGE_REG	0x01C /* Edge Detect Register */
90 
91 #ifdef INTRNG
92 #define	DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
93     GPIO_INTR_LEVEL_LOW | GPIO_INTR_LEVEL_HIGH | GPIO_INTR_EDGE_RISING | \
94     GPIO_INTR_EDGE_FALLING | GPIO_INTR_EDGE_BOTH)
95 #else
96 #define	DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
97 #endif
98 
99 #define	NGPIO		32
100 
101 #ifdef INTRNG
102 struct gpio_irqsrc {
103 	struct intr_irqsrc	gi_isrc;
104 	u_int			gi_irq;
105 	uint32_t		gi_mode;
106 };
107 #endif
108 
109 struct imx51_gpio_softc {
110 	device_t		dev;
111 	device_t		sc_busdev;
112 	struct mtx		sc_mtx;
113 	struct resource		*sc_res[3]; /* 1 x mem, 2 x IRQ */
114 	void			*gpio_ih[2];
115 	bus_space_tag_t		sc_iot;
116 	bus_space_handle_t	sc_ioh;
117 	int			gpio_npins;
118 	struct gpio_pin		gpio_pins[NGPIO];
119 #ifdef INTRNG
120 	struct gpio_irqsrc 	gpio_pic_irqsrc[NGPIO];
121 #endif
122 };
123 
124 static struct ofw_compat_data compat_data[] = {
125 	{"fsl,imx6q-gpio",  1},
126 	{"fsl,imx53-gpio",  1},
127 	{"fsl,imx51-gpio",  1},
128 	{NULL,	            0}
129 };
130 
131 static struct resource_spec imx_gpio_spec[] = {
132 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
133 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
134 	{ SYS_RES_IRQ,		1,	RF_ACTIVE },
135 	{ -1, 0 }
136 };
137 
138 /*
139  * Helpers
140  */
141 static void imx51_gpio_pin_configure(struct imx51_gpio_softc *,
142     struct gpio_pin *, uint32_t);
143 
144 /*
145  * Driver stuff
146  */
147 static int imx51_gpio_probe(device_t);
148 static int imx51_gpio_attach(device_t);
149 static int imx51_gpio_detach(device_t);
150 
151 /*
152  * GPIO interface
153  */
154 static device_t imx51_gpio_get_bus(device_t);
155 static int imx51_gpio_pin_max(device_t, int *);
156 static int imx51_gpio_pin_getcaps(device_t, uint32_t, uint32_t *);
157 static int imx51_gpio_pin_getflags(device_t, uint32_t, uint32_t *);
158 static int imx51_gpio_pin_getname(device_t, uint32_t, char *);
159 static int imx51_gpio_pin_setflags(device_t, uint32_t, uint32_t);
160 static int imx51_gpio_pin_set(device_t, uint32_t, unsigned int);
161 static int imx51_gpio_pin_get(device_t, uint32_t, unsigned int *);
162 static int imx51_gpio_pin_toggle(device_t, uint32_t pin);
163 
164 #ifdef INTRNG
165 static int
166 gpio_pic_map_fdt(struct imx51_gpio_softc *sc, struct intr_map_data_fdt *daf,
167     u_int *irqp, uint32_t *modep)
168 {
169 	u_int irq;
170 	uint32_t mode;
171 
172 	/*
173 	 * From devicetree/bindings/gpio/fsl-imx-gpio.txt:
174 	 *  #interrupt-cells:  2. The first cell is the GPIO number. The second
175 	 *  cell bits[3:0] is used to specify trigger type and level flags:
176 	 *    1 = low-to-high edge triggered.
177 	 *    2 = high-to-low edge triggered.
178 	 *    4 = active high level-sensitive.
179 	 *    8 = active low level-sensitive.
180 	 * We can do any single one of these modes, and also edge low+high
181 	 * (i.e., trigger on both edges); other combinations are not supported.
182 	 */
183 
184 	if (daf->ncells != 2) {
185 		device_printf(sc->dev, "Invalid #interrupt-cells\n");
186 		return (EINVAL);
187 	}
188 
189 	irq = daf->cells[0];
190 	if (irq >= sc->gpio_npins) {
191 		device_printf(sc->dev, "Invalid interrupt number %u\n", irq);
192 		return (EINVAL);
193 	}
194 	switch (daf->cells[1]) {
195 	case 1:
196 		mode = GPIO_INTR_EDGE_RISING;
197 		break;
198 	case 2:
199 		mode = GPIO_INTR_EDGE_FALLING;
200 		break;
201 	case 3:
202 		mode = GPIO_INTR_EDGE_BOTH;
203 		break;
204 	case 4:
205 		mode = GPIO_INTR_LEVEL_HIGH;
206 		break;
207 	case 8:
208 		mode = GPIO_INTR_LEVEL_LOW;
209 		break;
210 	default:
211 		device_printf(sc->dev, "Unsupported interrupt mode 0x%2x\n",
212 		    daf->cells[1]);
213 		return (ENOTSUP);
214 	}
215 	*irqp = irq;
216 	if (modep != NULL)
217 		*modep = mode;
218 	return (0);
219 }
220 
221 static int
222 gpio_pic_map_gpio(struct imx51_gpio_softc *sc, struct intr_map_data_gpio *dag,
223     u_int *irqp, uint32_t *modep)
224 {
225 	u_int irq;
226 
227 	irq = dag->gpio_pin_num;
228 	if (irq >= sc->gpio_npins) {
229 		device_printf(sc->dev, "Invalid interrupt number %u\n", irq);
230 		return (EINVAL);
231 	}
232 
233 	switch (dag->gpio_intr_mode) {
234 	case GPIO_INTR_LEVEL_LOW:
235 	case GPIO_INTR_LEVEL_HIGH:
236 	case GPIO_INTR_EDGE_RISING:
237 	case GPIO_INTR_EDGE_FALLING:
238 	case GPIO_INTR_EDGE_BOTH:
239 		break;
240 	default:
241 		device_printf(sc->dev, "Unsupported interrupt mode 0x%8x\n",
242 		    dag->gpio_intr_mode);
243 		return (EINVAL);
244 	}
245 
246 	*irqp = irq;
247 	if (modep != NULL)
248 		*modep = dag->gpio_intr_mode;
249 	return (0);
250 }
251 
252 static int
253 gpio_pic_map(struct imx51_gpio_softc *sc, struct intr_map_data *data,
254     u_int *irqp, uint32_t *modep)
255 {
256 
257 	switch (data->type) {
258 	case INTR_MAP_DATA_FDT:
259 		return (gpio_pic_map_fdt(sc, (struct intr_map_data_fdt *)data,
260 		    irqp, modep));
261 	case INTR_MAP_DATA_GPIO:
262 		return (gpio_pic_map_gpio(sc, (struct intr_map_data_gpio *)data,
263 		    irqp, modep));
264 	default:
265 		return (ENOTSUP);
266 	}
267 }
268 
269 static int
270 gpio_pic_map_intr(device_t dev, struct intr_map_data *data,
271     struct intr_irqsrc **isrcp)
272 {
273 	int error;
274 	u_int irq;
275 	struct imx51_gpio_softc *sc;
276 
277 	sc = device_get_softc(dev);
278 	error = gpio_pic_map(sc, data, &irq, NULL);
279 	if (error == 0)
280 		*isrcp = &sc->gpio_pic_irqsrc[irq].gi_isrc;
281 	return (error);
282 }
283 
284 static int
285 gpio_pic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
286     struct resource *res, struct intr_map_data *data)
287 {
288 	struct imx51_gpio_softc *sc;
289 	struct gpio_irqsrc *gi;
290 
291 	sc = device_get_softc(dev);
292 	if (isrc->isrc_handlers == 0) {
293 		gi = (struct gpio_irqsrc *)isrc;
294 		gi->gi_mode = GPIO_INTR_CONFORM;
295 
296 		// XXX Not sure this is necessary
297 		mtx_lock_spin(&sc->sc_mtx);
298 		CLEAR4(sc, IMX_GPIO_IMR_REG, (1U << gi->gi_irq));
299 		WRITE4(sc, IMX_GPIO_ISR_REG, (1U << gi->gi_irq));
300 		mtx_unlock_spin(&sc->sc_mtx);
301 	}
302 	return (0);
303 }
304 
305 static int
306 gpio_pic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
307     struct resource *res, struct intr_map_data *data)
308 {
309 	struct imx51_gpio_softc *sc;
310 	struct gpio_irqsrc *gi;
311 	int error;
312 	u_int icfg, irq, reg, shift, wrk;
313 	uint32_t mode;
314 
315 	if (data == NULL)
316 		return (ENOTSUP);
317 
318 	sc = device_get_softc(dev);
319 	gi = (struct gpio_irqsrc *)isrc;
320 
321 	/* Get config for interrupt. */
322 	error = gpio_pic_map(sc, data, &irq, &mode);
323 	if (error != 0)
324 		return (error);
325 	if (gi->gi_irq != irq)
326 		return (EINVAL);
327 
328 	/* Compare config if this is not first setup. */
329 	if (isrc->isrc_handlers != 0)
330 		return (gi->gi_mode == mode ? 0 : EINVAL);
331 	gi->gi_mode = mode;
332 
333 	/*
334 	 * To interrupt on both edges we have to use the EDGE register.  The
335 	 * manual says it only exists for backwards compatibilty with older imx
336 	 * chips, but it's also the only way to configure interrupting on both
337 	 * edges.  If the EDGE bit is on, the corresponding ICRn bit is ignored.
338 	 */
339 	mtx_lock_spin(&sc->sc_mtx);
340 	if (mode == GPIO_INTR_EDGE_BOTH) {
341 		SET4(sc, IMX_GPIO_EDGE_REG, (1u << irq));
342 	} else {
343 		CLEAR4(sc, IMX_GPIO_EDGE_REG, (1u << irq));
344 		switch (mode) {
345 		default:
346 			/* silence warnings; default can't actually happen. */
347 			/* FALLTHROUGH */
348 		case GPIO_INTR_LEVEL_LOW:
349 			icfg = GPIO_ICR_COND_LOW;
350 			break;
351 		case GPIO_INTR_LEVEL_HIGH:
352 			icfg = GPIO_ICR_COND_HIGH;
353 			break;
354 		case GPIO_INTR_EDGE_RISING:
355 			icfg = GPIO_ICR_COND_RISE;
356 			break;
357 		case GPIO_INTR_EDGE_FALLING:
358 			icfg = GPIO_ICR_COND_FALL;
359 			break;
360 		}
361 		if (irq < 16) {
362 			reg = IMX_GPIO_ICR1_REG;
363 			shift = 2 * irq;
364 		} else {
365 			reg = IMX_GPIO_ICR2_REG;
366 			shift = 2 * (irq - 16);
367 		}
368 		wrk = READ4(sc, reg);
369 		wrk &= ~(GPIO_ICR_COND_MASK << shift);
370 		wrk |= icfg << shift;
371 		WRITE4(sc, reg, wrk);
372 	}
373 	WRITE4(sc, IMX_GPIO_ISR_REG, (1u << irq));
374 	SET4(sc, IMX_GPIO_IMR_REG, (1u << irq));
375 	mtx_unlock_spin(&sc->sc_mtx);
376 
377 	return (0);
378 }
379 
380 /*
381  * this is mask_intr
382  */
383 static void
384 gpio_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
385 {
386 	struct imx51_gpio_softc *sc;
387 	u_int irq;
388 
389 	sc = device_get_softc(dev);
390 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
391 
392 	mtx_lock_spin(&sc->sc_mtx);
393 	CLEAR4(sc, IMX_GPIO_IMR_REG, (1U << irq));
394 	mtx_unlock_spin(&sc->sc_mtx);
395 }
396 
397 /*
398  * this is unmask_intr
399  */
400 static void
401 gpio_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
402 {
403 	struct imx51_gpio_softc *sc;
404 	u_int irq;
405 
406 	sc = device_get_softc(dev);
407 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
408 
409 	mtx_lock_spin(&sc->sc_mtx);
410 	SET4(sc, IMX_GPIO_IMR_REG, (1U << irq));
411 	mtx_unlock_spin(&sc->sc_mtx);
412 }
413 
414 static void
415 gpio_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
416 {
417 	struct imx51_gpio_softc *sc;
418 	u_int irq;
419 
420 	sc = device_get_softc(dev);
421 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
422 
423 	arm_irq_memory_barrier(0);
424         /* EOI.  W1C reg so no r-m-w, no locking needed. */
425 	WRITE4(sc, IMX_GPIO_ISR_REG, (1U << irq));
426 }
427 
428 static void
429 gpio_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
430 {
431 	struct imx51_gpio_softc *sc;
432 	u_int irq;
433 
434 	sc = device_get_softc(dev);
435 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
436 
437 	arm_irq_memory_barrier(0);
438 	/* EOI.  W1C reg so no r-m-w, no locking needed. */
439 	WRITE4(sc, IMX_GPIO_ISR_REG, (1U << irq));
440 	gpio_pic_enable_intr(dev, isrc);
441 }
442 
443 static void
444 gpio_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
445 {
446 	gpio_pic_disable_intr(dev, isrc);
447 }
448 
449 static int
450 gpio_pic_filter(void *arg)
451 {
452 	struct imx51_gpio_softc *sc;
453 	struct intr_irqsrc *isrc;
454 	uint32_t i, interrupts;
455 
456 	sc = arg;
457 	mtx_lock_spin(&sc->sc_mtx);
458 	interrupts = READ4(sc, IMX_GPIO_ISR_REG) & READ4(sc, IMX_GPIO_IMR_REG);
459 	mtx_unlock_spin(&sc->sc_mtx);
460 
461 	for (i = 0; interrupts != 0; i++, interrupts >>= 1) {
462 		if ((interrupts & 0x1) == 0)
463 			continue;
464 		isrc = &sc->gpio_pic_irqsrc[i].gi_isrc;
465 		if (intr_isrc_dispatch(isrc, curthread->td_intr_frame) != 0) {
466 			gpio_pic_disable_intr(sc->dev, isrc);
467 			gpio_pic_post_filter(sc->dev, isrc);
468 			device_printf(sc->dev, "Stray irq %u disabled\n", i);
469 		}
470 	}
471 
472 	return (FILTER_HANDLED);
473 }
474 
475 /*
476  * Initialize our isrcs and register them with intrng.
477  */
478 static int
479 gpio_pic_register_isrcs(struct imx51_gpio_softc *sc)
480 {
481 	int error;
482 	uint32_t irq;
483 	const char *name;
484 
485 	name = device_get_nameunit(sc->dev);
486 	for (irq = 0; irq < NGPIO; irq++) {
487 		sc->gpio_pic_irqsrc[irq].gi_irq = irq;
488 		sc->gpio_pic_irqsrc[irq].gi_mode = GPIO_INTR_CONFORM;
489 
490 		error = intr_isrc_register(&sc->gpio_pic_irqsrc[irq].gi_isrc,
491 		    sc->dev, 0, "%s,%u", name, irq);
492 		if (error != 0) {
493 			/* XXX call intr_isrc_deregister() */
494 			device_printf(sc->dev, "%s failed", __func__);
495 			return (error);
496 		}
497 	}
498 	return (0);
499 }
500 #endif
501 
502 /*
503  *
504  */
505 static void
506 imx51_gpio_pin_configure(struct imx51_gpio_softc *sc, struct gpio_pin *pin,
507     unsigned int flags)
508 {
509 	u_int newflags, pad;
510 
511 	mtx_lock_spin(&sc->sc_mtx);
512 
513 	/*
514 	 * Manage input/output; other flags not supported yet (maybe not ever,
515 	 * since we have no connection to the pad config registers from here).
516 	 *
517 	 * When setting a pin to output, honor the PRESET_[LOW,HIGH] flags if
518 	 * present.  Otherwise, for glitchless transistions on pins with pulls,
519 	 * read the current state of the pad and preset the DR register to drive
520 	 * the current value onto the pin before enabling the pin for output.
521 	 *
522 	 * Note that changes to pin->gp_flags must be acccumulated in newflags
523 	 * and stored with a single writeback to gp_flags at the end, to enable
524 	 * unlocked reads of that value elsewhere. This is only about unlocked
525 	 * access to gp_flags from elsewhere; we still use locking in this
526 	 * function to protect r-m-w access to the hardware registers.
527 	 */
528 	if (flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) {
529 		newflags = pin->gp_flags & ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
530 		if (flags & GPIO_PIN_OUTPUT) {
531 			if (flags & GPIO_PIN_PRESET_LOW) {
532 				pad = 0;
533 			} else if (flags & GPIO_PIN_PRESET_HIGH) {
534 				pad = 1;
535 			} else {
536 				if (flags & GPIO_PIN_OPENDRAIN)
537 					pad = READ4(sc, IMX_GPIO_PSR_REG);
538 				else
539 					pad = READ4(sc, IMX_GPIO_DR_REG);
540 				pad = (pad >> pin->gp_pin) & 1;
541 			}
542 			newflags |= GPIO_PIN_OUTPUT;
543 			SET4(sc, IMX_GPIO_DR_REG, (pad << pin->gp_pin));
544 			SET4(sc, IMX_GPIO_OE_REG, (1U << pin->gp_pin));
545 		} else {
546 			newflags |= GPIO_PIN_INPUT;
547 			CLEAR4(sc, IMX_GPIO_OE_REG, (1U << pin->gp_pin));
548 		}
549 		pin->gp_flags = newflags;
550 	}
551 
552 	mtx_unlock_spin(&sc->sc_mtx);
553 }
554 
555 static device_t
556 imx51_gpio_get_bus(device_t dev)
557 {
558 	struct imx51_gpio_softc *sc;
559 
560 	sc = device_get_softc(dev);
561 
562 	return (sc->sc_busdev);
563 }
564 
565 static int
566 imx51_gpio_pin_max(device_t dev, int *maxpin)
567 {
568 	struct imx51_gpio_softc *sc;
569 
570 	sc = device_get_softc(dev);
571 	*maxpin = sc->gpio_npins - 1;
572 
573 	return (0);
574 }
575 
576 static int
577 imx51_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
578 {
579 	struct imx51_gpio_softc *sc;
580 
581 	sc = device_get_softc(dev);
582 
583 	if (pin >= sc->gpio_npins)
584 		return (EINVAL);
585 
586 	*caps = sc->gpio_pins[pin].gp_caps;
587 
588 	return (0);
589 }
590 
591 static int
592 imx51_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
593 {
594 	struct imx51_gpio_softc *sc;
595 
596 	sc = device_get_softc(dev);
597 
598 	if (pin >= sc->gpio_npins)
599 		return (EINVAL);
600 
601 	*flags = sc->gpio_pins[pin].gp_flags;
602 
603 	return (0);
604 }
605 
606 static int
607 imx51_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
608 {
609 	struct imx51_gpio_softc *sc;
610 
611 	sc = device_get_softc(dev);
612 	if (pin >= sc->gpio_npins)
613 		return (EINVAL);
614 
615 	mtx_lock_spin(&sc->sc_mtx);
616 	memcpy(name, sc->gpio_pins[pin].gp_name, GPIOMAXNAME);
617 	mtx_unlock_spin(&sc->sc_mtx);
618 
619 	return (0);
620 }
621 
622 static int
623 imx51_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
624 {
625 	struct imx51_gpio_softc *sc;
626 
627 	sc = device_get_softc(dev);
628 
629 	if (pin >= sc->gpio_npins)
630 		return (EINVAL);
631 
632 	imx51_gpio_pin_configure(sc, &sc->gpio_pins[pin], flags);
633 
634 	return (0);
635 }
636 
637 static int
638 imx51_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
639 {
640 	struct imx51_gpio_softc *sc;
641 
642 	sc = device_get_softc(dev);
643 
644 	if (pin >= sc->gpio_npins)
645 		return (EINVAL);
646 
647 	mtx_lock_spin(&sc->sc_mtx);
648 	if (value)
649 		SET4(sc, IMX_GPIO_DR_REG, (1U << pin));
650 	else
651 		CLEAR4(sc, IMX_GPIO_DR_REG, (1U << pin));
652 	mtx_unlock_spin(&sc->sc_mtx);
653 
654 	return (0);
655 }
656 
657 static int
658 imx51_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
659 {
660 	struct imx51_gpio_softc *sc;
661 
662 	sc = device_get_softc(dev);
663 
664 	if (pin >= sc->gpio_npins)
665 		return (EINVAL);
666 
667 	/*
668 	 * Normally a pin set for output can be read by reading the DR reg which
669 	 * indicates what value is being driven to that pin.  The exception is
670 	 * pins configured for open-drain mode, in which case we have to read
671 	 * the pad status register in case the pin is being driven externally.
672 	 * Doing so requires that the SION bit be configured in pinmux, which
673 	 * isn't the case for most normal gpio pins, so only try to read via PSR
674 	 * if the OPENDRAIN flag is set, and it's the user's job to correctly
675 	 * configure SION along with open-drain output mode for those pins.
676 	 */
677 	if (sc->gpio_pins[pin].gp_flags & GPIO_PIN_OPENDRAIN)
678 		*val = (READ4(sc, IMX_GPIO_PSR_REG) >> pin) & 1;
679 	else
680 		*val = (READ4(sc, IMX_GPIO_DR_REG) >> pin) & 1;
681 
682 	return (0);
683 }
684 
685 static int
686 imx51_gpio_pin_toggle(device_t dev, uint32_t pin)
687 {
688 	struct imx51_gpio_softc *sc;
689 
690 	sc = device_get_softc(dev);
691 
692 	if (pin >= sc->gpio_npins)
693 		return (EINVAL);
694 
695 	mtx_lock_spin(&sc->sc_mtx);
696 	WRITE4(sc, IMX_GPIO_DR_REG,
697 	    (READ4(sc, IMX_GPIO_DR_REG) ^ (1U << pin)));
698 	mtx_unlock_spin(&sc->sc_mtx);
699 
700 	return (0);
701 }
702 
703 static int
704 imx51_gpio_pin_access_32(device_t dev, uint32_t first_pin, uint32_t clear_pins,
705     uint32_t change_pins, uint32_t *orig_pins)
706 {
707 	struct imx51_gpio_softc *sc;
708 
709 	if (first_pin != 0)
710 		return (EINVAL);
711 
712 	sc = device_get_softc(dev);
713 
714 	if (orig_pins != NULL)
715 		*orig_pins = READ4(sc, IMX_GPIO_DR_REG);
716 
717 	if ((clear_pins | change_pins) != 0) {
718 		mtx_lock_spin(&sc->sc_mtx);
719 		WRITE4(sc, IMX_GPIO_DR_REG,
720 		    (READ4(sc, IMX_GPIO_DR_REG) & ~clear_pins) ^ change_pins);
721 		mtx_unlock_spin(&sc->sc_mtx);
722 	}
723 
724 	return (0);
725 }
726 
727 static int
728 imx51_gpio_pin_config_32(device_t dev, uint32_t first_pin, uint32_t num_pins,
729     uint32_t *pin_flags)
730 {
731 	struct imx51_gpio_softc *sc;
732 	u_int i;
733 	uint32_t bit, drclr, drset, flags, oeclr, oeset, pads;
734 
735 	sc = device_get_softc(dev);
736 
737 	if (first_pin != 0 || num_pins > sc->gpio_npins)
738 		return (EINVAL);
739 
740 	drclr = drset = oeclr = oeset = 0;
741 	pads = READ4(sc, IMX_GPIO_DR_REG);
742 
743 	for (i = 0; i < num_pins; ++i) {
744 		bit = 1u << i;
745 		flags = pin_flags[i];
746 		if (flags & GPIO_PIN_INPUT) {
747 			oeclr |= bit;
748 		} else if (flags & GPIO_PIN_OUTPUT) {
749 			oeset |= bit;
750 			if (flags & GPIO_PIN_PRESET_LOW)
751 				drclr |= bit;
752 			else if (flags & GPIO_PIN_PRESET_HIGH)
753 				drset |= bit;
754 			else /* Drive whatever it's now pulled to. */
755 				drset |= pads & bit;
756 		}
757 	}
758 
759 	mtx_lock_spin(&sc->sc_mtx);
760 	WRITE4(sc, IMX_GPIO_DR_REG,
761 	    (READ4(sc, IMX_GPIO_DR_REG) & ~drclr) | drset);
762 	WRITE4(sc, IMX_GPIO_OE_REG,
763 	    (READ4(sc, IMX_GPIO_OE_REG) & ~oeclr) | oeset);
764 	mtx_unlock_spin(&sc->sc_mtx);
765 
766 	return (0);
767 }
768 
769 static int
770 imx51_gpio_probe(device_t dev)
771 {
772 
773 	if (!ofw_bus_status_okay(dev))
774 		return (ENXIO);
775 
776 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
777 		device_set_desc(dev, "Freescale i.MX GPIO Controller");
778 		return (BUS_PROBE_DEFAULT);
779 	}
780 
781 	return (ENXIO);
782 }
783 
784 static int
785 imx51_gpio_attach(device_t dev)
786 {
787 	struct imx51_gpio_softc *sc;
788 	int i, irq, unit;
789 
790 	sc = device_get_softc(dev);
791 	sc->dev = dev;
792 	sc->gpio_npins = NGPIO;
793 
794 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->dev), NULL, MTX_SPIN);
795 
796 	if (bus_alloc_resources(dev, imx_gpio_spec, sc->sc_res)) {
797 		device_printf(dev, "could not allocate resources\n");
798 		bus_release_resources(dev, imx_gpio_spec, sc->sc_res);
799 		mtx_destroy(&sc->sc_mtx);
800 		return (ENXIO);
801 	}
802 
803 	sc->sc_iot = rman_get_bustag(sc->sc_res[0]);
804 	sc->sc_ioh = rman_get_bushandle(sc->sc_res[0]);
805 	/*
806 	 * Mask off all interrupts in hardware, then set up interrupt handling.
807 	 */
808 	WRITE4(sc, IMX_GPIO_IMR_REG, 0);
809 	for (irq = 0; irq < 2; irq++) {
810 #ifdef INTRNG
811 		if ((bus_setup_intr(dev, sc->sc_res[1 + irq], INTR_TYPE_CLK,
812 		    gpio_pic_filter, NULL, sc, &sc->gpio_ih[irq]))) {
813 			device_printf(dev,
814 			    "WARNING: unable to register interrupt handler\n");
815 			imx51_gpio_detach(dev);
816 			return (ENXIO);
817 		}
818 #endif
819 	}
820 
821 	unit = device_get_unit(dev);
822 	for (i = 0; i < sc->gpio_npins; i++) {
823  		sc->gpio_pins[i].gp_pin = i;
824  		sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
825  		sc->gpio_pins[i].gp_flags =
826  		    (READ4(sc, IMX_GPIO_OE_REG) & (1U << i)) ? GPIO_PIN_OUTPUT :
827  		    GPIO_PIN_INPUT;
828  		snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME,
829  		    "GPIO%d_IO%02d", unit + 1, i);
830 	}
831 
832 #ifdef INTRNG
833 	gpio_pic_register_isrcs(sc);
834 	intr_pic_register(dev, OF_xref_from_node(ofw_bus_get_node(dev)));
835 #endif
836 	sc->sc_busdev = gpiobus_attach_bus(dev);
837 
838 	if (sc->sc_busdev == NULL) {
839 		imx51_gpio_detach(dev);
840 		return (ENXIO);
841 	}
842 
843 	return (0);
844 }
845 
846 static int
847 imx51_gpio_detach(device_t dev)
848 {
849 	int irq;
850 	struct imx51_gpio_softc *sc;
851 
852 	sc = device_get_softc(dev);
853 
854 	gpiobus_detach_bus(dev);
855 	for (irq = 1; irq <= 2; irq++) {
856 		if (sc->gpio_ih[irq])
857 			bus_teardown_intr(dev, sc->sc_res[irq], sc->gpio_ih[irq]);
858 	}
859 	bus_release_resources(dev, imx_gpio_spec, sc->sc_res);
860 	mtx_destroy(&sc->sc_mtx);
861 
862 	return(0);
863 }
864 
865 static device_method_t imx51_gpio_methods[] = {
866 	DEVMETHOD(device_probe,		imx51_gpio_probe),
867 	DEVMETHOD(device_attach,	imx51_gpio_attach),
868 	DEVMETHOD(device_detach,	imx51_gpio_detach),
869 
870 #ifdef INTRNG
871 	/* Interrupt controller interface */
872 	DEVMETHOD(pic_disable_intr,	gpio_pic_disable_intr),
873 	DEVMETHOD(pic_enable_intr,	gpio_pic_enable_intr),
874 	DEVMETHOD(pic_map_intr,		gpio_pic_map_intr),
875 	DEVMETHOD(pic_setup_intr,	gpio_pic_setup_intr),
876 	DEVMETHOD(pic_teardown_intr,	gpio_pic_teardown_intr),
877 	DEVMETHOD(pic_post_filter,	gpio_pic_post_filter),
878 	DEVMETHOD(pic_post_ithread,	gpio_pic_post_ithread),
879 	DEVMETHOD(pic_pre_ithread,	gpio_pic_pre_ithread),
880 #endif
881 
882 	/* GPIO protocol */
883 	DEVMETHOD(gpio_get_bus,		imx51_gpio_get_bus),
884 	DEVMETHOD(gpio_pin_max,		imx51_gpio_pin_max),
885 	DEVMETHOD(gpio_pin_getname,	imx51_gpio_pin_getname),
886 	DEVMETHOD(gpio_pin_getflags,	imx51_gpio_pin_getflags),
887 	DEVMETHOD(gpio_pin_getcaps,	imx51_gpio_pin_getcaps),
888 	DEVMETHOD(gpio_pin_setflags,	imx51_gpio_pin_setflags),
889 	DEVMETHOD(gpio_pin_get,		imx51_gpio_pin_get),
890 	DEVMETHOD(gpio_pin_set,		imx51_gpio_pin_set),
891 	DEVMETHOD(gpio_pin_toggle,	imx51_gpio_pin_toggle),
892 	DEVMETHOD(gpio_pin_access_32,	imx51_gpio_pin_access_32),
893 	DEVMETHOD(gpio_pin_config_32,	imx51_gpio_pin_config_32),
894 	{0, 0},
895 };
896 
897 static driver_t imx51_gpio_driver = {
898 	"gpio",
899 	imx51_gpio_methods,
900 	sizeof(struct imx51_gpio_softc),
901 };
902 static devclass_t imx51_gpio_devclass;
903 
904 EARLY_DRIVER_MODULE(imx51_gpio, simplebus, imx51_gpio_driver,
905     imx51_gpio_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
906