xref: /freebsd/sys/arm/freescale/imx/imx_gpio.c (revision d8fffc3704a0d7f4ea5b866f689e58480ddebaa1)
10495dcefSRuslan Bukin /*-
20495dcefSRuslan Bukin  * Copyright (c) 2012, 2013 The FreeBSD Foundation
30495dcefSRuslan Bukin  *
40495dcefSRuslan Bukin  * This software was developed by Oleksandr Rybalko under sponsorship
50495dcefSRuslan Bukin  * from the FreeBSD Foundation.
60495dcefSRuslan Bukin  *
70495dcefSRuslan Bukin  * Redistribution and use in source and binary forms, with or without
80495dcefSRuslan Bukin  * modification, are permitted provided that the following conditions
90495dcefSRuslan Bukin  * are met:
100495dcefSRuslan Bukin  * 1.	Redistributions of source code must retain the above copyright
110495dcefSRuslan Bukin  *	notice, this list of conditions and the following disclaimer.
120495dcefSRuslan Bukin  * 2.	Redistributions in binary form must reproduce the above copyright
130495dcefSRuslan Bukin  *	notice, this list of conditions and the following disclaimer in the
140495dcefSRuslan Bukin  *	documentation and/or other materials provided with the distribution.
150495dcefSRuslan Bukin  *
160495dcefSRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170495dcefSRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180495dcefSRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190495dcefSRuslan Bukin  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200495dcefSRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210495dcefSRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220495dcefSRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230495dcefSRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240495dcefSRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250495dcefSRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260495dcefSRuslan Bukin  * SUCH DAMAGE.
270495dcefSRuslan Bukin  */
280495dcefSRuslan Bukin 
290495dcefSRuslan Bukin /*
300495dcefSRuslan Bukin  * Freescale i.MX515 GPIO driver.
310495dcefSRuslan Bukin  */
320495dcefSRuslan Bukin 
330495dcefSRuslan Bukin #include <sys/cdefs.h>
34e6c43510SIan Lepore #include "opt_platform.h"
35e6c43510SIan Lepore 
360495dcefSRuslan Bukin #include <sys/param.h>
370495dcefSRuslan Bukin #include <sys/systm.h>
380495dcefSRuslan Bukin #include <sys/bus.h>
390495dcefSRuslan Bukin 
400495dcefSRuslan Bukin #include <sys/kernel.h>
410495dcefSRuslan Bukin #include <sys/module.h>
420495dcefSRuslan Bukin #include <sys/rman.h>
430495dcefSRuslan Bukin #include <sys/lock.h>
440495dcefSRuslan Bukin #include <sys/mutex.h>
450495dcefSRuslan Bukin #include <sys/gpio.h>
46e6c43510SIan Lepore #include <sys/proc.h>
470495dcefSRuslan Bukin 
480495dcefSRuslan Bukin #include <machine/bus.h>
49e6c43510SIan Lepore #include <machine/intr.h>
500495dcefSRuslan Bukin #include <machine/resource.h>
510495dcefSRuslan Bukin 
527836352bSLuiz Otavio O Souza #include <dev/gpio/gpiobusvar.h>
530495dcefSRuslan Bukin #include <dev/ofw/openfirm.h>
540495dcefSRuslan Bukin #include <dev/ofw/ofw_bus.h>
550495dcefSRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
560495dcefSRuslan Bukin 
5790a5fc32SEmmanuel Vadot #if defined(__aarch64__)
5894bc2117SOleksandr Tymoshenko #define	IMX_ENABLE_CLOCKS
5994bc2117SOleksandr Tymoshenko #endif
6094bc2117SOleksandr Tymoshenko 
6194bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
62be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
6394bc2117SOleksandr Tymoshenko #endif
6494bc2117SOleksandr Tymoshenko 
650495dcefSRuslan Bukin #include "gpio_if.h"
660495dcefSRuslan Bukin 
6759c3cb81SAndrew Turner #ifdef INTRNG
68e6c43510SIan Lepore #include "pic_if.h"
69e6c43510SIan Lepore #endif
700495dcefSRuslan Bukin 
710495dcefSRuslan Bukin #define	WRITE4(_sc, _r, _v)						\
720495dcefSRuslan Bukin 	    bus_space_write_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r), (_v))
730495dcefSRuslan Bukin #define	READ4(_sc, _r)							\
740495dcefSRuslan Bukin 	    bus_space_read_4((_sc)->sc_iot, (_sc)->sc_ioh, (_r))
750495dcefSRuslan Bukin #define	SET4(_sc, _r, _m)						\
760495dcefSRuslan Bukin 	    WRITE4((_sc), (_r), READ4((_sc), (_r)) | (_m))
770495dcefSRuslan Bukin #define	CLEAR4(_sc, _r, _m)						\
780495dcefSRuslan Bukin 	    WRITE4((_sc), (_r), READ4((_sc), (_r)) & ~(_m))
790495dcefSRuslan Bukin 
800495dcefSRuslan Bukin /* Registers definition for Freescale i.MX515 GPIO controller */
810495dcefSRuslan Bukin 
820495dcefSRuslan Bukin #define	IMX_GPIO_DR_REG		0x000 /* Pin Data */
830495dcefSRuslan Bukin #define	IMX_GPIO_OE_REG		0x004 /* Set Pin Output */
840495dcefSRuslan Bukin #define	IMX_GPIO_PSR_REG	0x008 /* Pad Status */
850495dcefSRuslan Bukin #define	IMX_GPIO_ICR1_REG	0x00C /* Interrupt Configuration */
860495dcefSRuslan Bukin #define	IMX_GPIO_ICR2_REG	0x010 /* Interrupt Configuration */
870495dcefSRuslan Bukin #define		GPIO_ICR_COND_LOW	0
880495dcefSRuslan Bukin #define		GPIO_ICR_COND_HIGH	1
890495dcefSRuslan Bukin #define		GPIO_ICR_COND_RISE	2
900495dcefSRuslan Bukin #define		GPIO_ICR_COND_FALL	3
91e3f2d7afSIan Lepore #define		GPIO_ICR_COND_MASK	0x3
920495dcefSRuslan Bukin #define	IMX_GPIO_IMR_REG	0x014 /* Interrupt Mask Register */
930495dcefSRuslan Bukin #define	IMX_GPIO_ISR_REG	0x018 /* Interrupt Status Register */
940495dcefSRuslan Bukin #define	IMX_GPIO_EDGE_REG	0x01C /* Edge Detect Register */
950495dcefSRuslan Bukin 
96f7b96696SSvatopluk Kraus #ifdef INTRNG
97f7b96696SSvatopluk Kraus #define	DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
98f7b96696SSvatopluk Kraus     GPIO_INTR_LEVEL_LOW | GPIO_INTR_LEVEL_HIGH | GPIO_INTR_EDGE_RISING | \
99e3f2d7afSIan Lepore     GPIO_INTR_EDGE_FALLING | GPIO_INTR_EDGE_BOTH)
100f7b96696SSvatopluk Kraus #else
1010495dcefSRuslan Bukin #define	DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
102f7b96696SSvatopluk Kraus #endif
103f7b96696SSvatopluk Kraus 
1040495dcefSRuslan Bukin #define	NGPIO		32
1050495dcefSRuslan Bukin 
10659c3cb81SAndrew Turner #ifdef INTRNG
107bff6be3eSSvatopluk Kraus struct gpio_irqsrc {
108bff6be3eSSvatopluk Kraus 	struct intr_irqsrc	gi_isrc;
109bff6be3eSSvatopluk Kraus 	u_int			gi_irq;
110e2ff80bcSSvatopluk Kraus 	uint32_t		gi_mode;
111bff6be3eSSvatopluk Kraus };
112bff6be3eSSvatopluk Kraus #endif
113bff6be3eSSvatopluk Kraus 
1140495dcefSRuslan Bukin struct imx51_gpio_softc {
1150495dcefSRuslan Bukin 	device_t		dev;
1167836352bSLuiz Otavio O Souza 	device_t		sc_busdev;
1170495dcefSRuslan Bukin 	struct mtx		sc_mtx;
118e6c43510SIan Lepore 	struct resource		*sc_res[3]; /* 1 x mem, 2 x IRQ */
119e6c43510SIan Lepore 	void			*gpio_ih[2];
1200495dcefSRuslan Bukin 	bus_space_tag_t		sc_iot;
1210495dcefSRuslan Bukin 	bus_space_handle_t	sc_ioh;
1220495dcefSRuslan Bukin 	int			gpio_npins;
1230495dcefSRuslan Bukin 	struct gpio_pin		gpio_pins[NGPIO];
12459c3cb81SAndrew Turner #ifdef INTRNG
125bff6be3eSSvatopluk Kraus 	struct gpio_irqsrc 	gpio_pic_irqsrc[NGPIO];
126bff6be3eSSvatopluk Kraus #endif
12794bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
12894bc2117SOleksandr Tymoshenko 	clk_t			clk;
12994bc2117SOleksandr Tymoshenko #endif
1300495dcefSRuslan Bukin };
1310495dcefSRuslan Bukin 
1320495dcefSRuslan Bukin static struct ofw_compat_data compat_data[] = {
13394bc2117SOleksandr Tymoshenko 	{"fsl,imx8mq-gpio",	1},
1340495dcefSRuslan Bukin 	{"fsl,imx6q-gpio",	1},
1350495dcefSRuslan Bukin 	{"fsl,imx53-gpio",	1},
1360495dcefSRuslan Bukin 	{"fsl,imx51-gpio",	1},
137*d8fffc37STom Jones 	{"fsl,imx35-gpio",	1},
1380495dcefSRuslan Bukin 	{NULL,			0}
1390495dcefSRuslan Bukin };
1400495dcefSRuslan Bukin 
1410495dcefSRuslan Bukin static struct resource_spec imx_gpio_spec[] = {
1420495dcefSRuslan Bukin 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
1430495dcefSRuslan Bukin 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
1440495dcefSRuslan Bukin 	{ SYS_RES_IRQ,		1,	RF_ACTIVE },
1450495dcefSRuslan Bukin 	{ -1, 0 }
1460495dcefSRuslan Bukin };
147333559f2SIan Lepore #define	FIRST_IRQRES	1
148333559f2SIan Lepore #define	NUM_IRQRES	2
1490495dcefSRuslan Bukin 
1500495dcefSRuslan Bukin /*
1510495dcefSRuslan Bukin  * Helpers
1520495dcefSRuslan Bukin  */
1530495dcefSRuslan Bukin static void imx51_gpio_pin_configure(struct imx51_gpio_softc *,
1540495dcefSRuslan Bukin     struct gpio_pin *, uint32_t);
1550495dcefSRuslan Bukin 
1560495dcefSRuslan Bukin /*
1570495dcefSRuslan Bukin  * Driver stuff
1580495dcefSRuslan Bukin  */
1590495dcefSRuslan Bukin static int imx51_gpio_probe(device_t);
1600495dcefSRuslan Bukin static int imx51_gpio_attach(device_t);
1610495dcefSRuslan Bukin static int imx51_gpio_detach(device_t);
1620495dcefSRuslan Bukin 
1630495dcefSRuslan Bukin /*
1640495dcefSRuslan Bukin  * GPIO interface
1650495dcefSRuslan Bukin  */
1667836352bSLuiz Otavio O Souza static device_t imx51_gpio_get_bus(device_t);
1670495dcefSRuslan Bukin static int imx51_gpio_pin_max(device_t, int *);
1680495dcefSRuslan Bukin static int imx51_gpio_pin_getcaps(device_t, uint32_t, uint32_t *);
1690495dcefSRuslan Bukin static int imx51_gpio_pin_getflags(device_t, uint32_t, uint32_t *);
1700495dcefSRuslan Bukin static int imx51_gpio_pin_getname(device_t, uint32_t, char *);
1710495dcefSRuslan Bukin static int imx51_gpio_pin_setflags(device_t, uint32_t, uint32_t);
1720495dcefSRuslan Bukin static int imx51_gpio_pin_set(device_t, uint32_t, unsigned int);
1730495dcefSRuslan Bukin static int imx51_gpio_pin_get(device_t, uint32_t, unsigned int *);
1740495dcefSRuslan Bukin static int imx51_gpio_pin_toggle(device_t, uint32_t pin);
1750495dcefSRuslan Bukin 
17659c3cb81SAndrew Turner #ifdef INTRNG
177bff6be3eSSvatopluk Kraus static int
gpio_pic_map_fdt(struct imx51_gpio_softc * sc,struct intr_map_data_fdt * daf,u_int * irqp,uint32_t * modep)178f7b96696SSvatopluk Kraus gpio_pic_map_fdt(struct imx51_gpio_softc *sc, struct intr_map_data_fdt *daf,
179f7b96696SSvatopluk Kraus     u_int *irqp, uint32_t *modep)
180e6c43510SIan Lepore {
181f7b96696SSvatopluk Kraus 	u_int irq;
182e2ff80bcSSvatopluk Kraus 	uint32_t mode;
183e6c43510SIan Lepore 
184bff6be3eSSvatopluk Kraus 	/*
185bff6be3eSSvatopluk Kraus 	 * From devicetree/bindings/gpio/fsl-imx-gpio.txt:
186bff6be3eSSvatopluk Kraus 	 *  #interrupt-cells:  2. The first cell is the GPIO number. The second
187bff6be3eSSvatopluk Kraus 	 *  cell bits[3:0] is used to specify trigger type and level flags:
188bff6be3eSSvatopluk Kraus 	 *    1 = low-to-high edge triggered.
189bff6be3eSSvatopluk Kraus 	 *    2 = high-to-low edge triggered.
190bff6be3eSSvatopluk Kraus 	 *    4 = active high level-sensitive.
191bff6be3eSSvatopluk Kraus 	 *    8 = active low level-sensitive.
192e3f2d7afSIan Lepore 	 * We can do any single one of these modes, and also edge low+high
193e3f2d7afSIan Lepore 	 * (i.e., trigger on both edges); other combinations are not supported.
194bff6be3eSSvatopluk Kraus 	 */
195bff6be3eSSvatopluk Kraus 
196f7b96696SSvatopluk Kraus 	if (daf->ncells != 2) {
197143d3973SSvatopluk Kraus 		device_printf(sc->dev, "Invalid #interrupt-cells\n");
198bff6be3eSSvatopluk Kraus 		return (EINVAL);
199e6c43510SIan Lepore 	}
200e6c43510SIan Lepore 
201f7b96696SSvatopluk Kraus 	irq = daf->cells[0];
202bff6be3eSSvatopluk Kraus 	if (irq >= sc->gpio_npins) {
203143d3973SSvatopluk Kraus 		device_printf(sc->dev, "Invalid interrupt number %u\n", irq);
204bff6be3eSSvatopluk Kraus 		return (EINVAL);
205bff6be3eSSvatopluk Kraus 	}
206f7b96696SSvatopluk Kraus 	switch (daf->cells[1]) {
207bff6be3eSSvatopluk Kraus 	case 1:
208e2ff80bcSSvatopluk Kraus 		mode = GPIO_INTR_EDGE_RISING;
209bff6be3eSSvatopluk Kraus 		break;
210bff6be3eSSvatopluk Kraus 	case 2:
211e2ff80bcSSvatopluk Kraus 		mode = GPIO_INTR_EDGE_FALLING;
212bff6be3eSSvatopluk Kraus 		break;
213e3f2d7afSIan Lepore 	case 3:
214e3f2d7afSIan Lepore 		mode = GPIO_INTR_EDGE_BOTH;
215e3f2d7afSIan Lepore 		break;
216bff6be3eSSvatopluk Kraus 	case 4:
217e2ff80bcSSvatopluk Kraus 		mode = GPIO_INTR_LEVEL_HIGH;
218bff6be3eSSvatopluk Kraus 		break;
219bff6be3eSSvatopluk Kraus 	case 8:
220e2ff80bcSSvatopluk Kraus 		mode = GPIO_INTR_LEVEL_LOW;
221bff6be3eSSvatopluk Kraus 		break;
222bff6be3eSSvatopluk Kraus 	default:
223e2ff80bcSSvatopluk Kraus 		device_printf(sc->dev, "Unsupported interrupt mode 0x%2x\n",
224f7b96696SSvatopluk Kraus 		    daf->cells[1]);
225bff6be3eSSvatopluk Kraus 		return (ENOTSUP);
226bff6be3eSSvatopluk Kraus 	}
227bff6be3eSSvatopluk Kraus 	*irqp = irq;
228e2ff80bcSSvatopluk Kraus 	if (modep != NULL)
229e2ff80bcSSvatopluk Kraus 		*modep = mode;
230bff6be3eSSvatopluk Kraus 	return (0);
231bff6be3eSSvatopluk Kraus }
232bff6be3eSSvatopluk Kraus 
233bff6be3eSSvatopluk Kraus static int
gpio_pic_map_gpio(struct imx51_gpio_softc * sc,struct intr_map_data_gpio * dag,u_int * irqp,uint32_t * modep)234f7b96696SSvatopluk Kraus gpio_pic_map_gpio(struct imx51_gpio_softc *sc, struct intr_map_data_gpio *dag,
235f7b96696SSvatopluk Kraus     u_int *irqp, uint32_t *modep)
236f7b96696SSvatopluk Kraus {
237f7b96696SSvatopluk Kraus 	u_int irq;
238f7b96696SSvatopluk Kraus 
239f7b96696SSvatopluk Kraus 	irq = dag->gpio_pin_num;
240f7b96696SSvatopluk Kraus 	if (irq >= sc->gpio_npins) {
241f7b96696SSvatopluk Kraus 		device_printf(sc->dev, "Invalid interrupt number %u\n", irq);
242f7b96696SSvatopluk Kraus 		return (EINVAL);
243f7b96696SSvatopluk Kraus 	}
244f7b96696SSvatopluk Kraus 
245e3f2d7afSIan Lepore 	switch (dag->gpio_intr_mode) {
246e3f2d7afSIan Lepore 	case GPIO_INTR_LEVEL_LOW:
247e3f2d7afSIan Lepore 	case GPIO_INTR_LEVEL_HIGH:
248e3f2d7afSIan Lepore 	case GPIO_INTR_EDGE_RISING:
249e3f2d7afSIan Lepore 	case GPIO_INTR_EDGE_FALLING:
250e3f2d7afSIan Lepore 	case GPIO_INTR_EDGE_BOTH:
251e3f2d7afSIan Lepore 		break;
252e3f2d7afSIan Lepore 	default:
253f7b96696SSvatopluk Kraus 		device_printf(sc->dev, "Unsupported interrupt mode 0x%8x\n",
254e3f2d7afSIan Lepore 		    dag->gpio_intr_mode);
255f7b96696SSvatopluk Kraus 		return (EINVAL);
256f7b96696SSvatopluk Kraus 	}
257f7b96696SSvatopluk Kraus 
258f7b96696SSvatopluk Kraus 	*irqp = irq;
259f7b96696SSvatopluk Kraus 	if (modep != NULL)
260e3f2d7afSIan Lepore 		*modep = dag->gpio_intr_mode;
261f7b96696SSvatopluk Kraus 	return (0);
262f7b96696SSvatopluk Kraus }
263f7b96696SSvatopluk Kraus 
264f7b96696SSvatopluk Kraus static int
gpio_pic_map(struct imx51_gpio_softc * sc,struct intr_map_data * data,u_int * irqp,uint32_t * modep)265f7b96696SSvatopluk Kraus gpio_pic_map(struct imx51_gpio_softc *sc, struct intr_map_data *data,
266f7b96696SSvatopluk Kraus     u_int *irqp, uint32_t *modep)
267f7b96696SSvatopluk Kraus {
268f7b96696SSvatopluk Kraus 
269f7b96696SSvatopluk Kraus 	switch (data->type) {
270f7b96696SSvatopluk Kraus 	case INTR_MAP_DATA_FDT:
271f7b96696SSvatopluk Kraus 		return (gpio_pic_map_fdt(sc, (struct intr_map_data_fdt *)data,
272f7b96696SSvatopluk Kraus 		    irqp, modep));
273f7b96696SSvatopluk Kraus 	case INTR_MAP_DATA_GPIO:
274f7b96696SSvatopluk Kraus 		return (gpio_pic_map_gpio(sc, (struct intr_map_data_gpio *)data,
275f7b96696SSvatopluk Kraus 		    irqp, modep));
276f7b96696SSvatopluk Kraus 	default:
277f7b96696SSvatopluk Kraus 		return (ENOTSUP);
278f7b96696SSvatopluk Kraus 	}
279f7b96696SSvatopluk Kraus }
280f7b96696SSvatopluk Kraus 
281f7b96696SSvatopluk Kraus static int
gpio_pic_map_intr(device_t dev,struct intr_map_data * data,struct intr_irqsrc ** isrcp)282bff6be3eSSvatopluk Kraus gpio_pic_map_intr(device_t dev, struct intr_map_data *data,
283bff6be3eSSvatopluk Kraus     struct intr_irqsrc **isrcp)
284bff6be3eSSvatopluk Kraus {
285bff6be3eSSvatopluk Kraus 	int error;
286bff6be3eSSvatopluk Kraus 	u_int irq;
287bff6be3eSSvatopluk Kraus 	struct imx51_gpio_softc *sc;
288bff6be3eSSvatopluk Kraus 
289bff6be3eSSvatopluk Kraus 	sc = device_get_softc(dev);
290f7b96696SSvatopluk Kraus 	error = gpio_pic_map(sc, data, &irq, NULL);
291f7b96696SSvatopluk Kraus 	if (error == 0)
292bff6be3eSSvatopluk Kraus 		*isrcp = &sc->gpio_pic_irqsrc[irq].gi_isrc;
293bff6be3eSSvatopluk Kraus 	return (error);
294bff6be3eSSvatopluk Kraus }
295bff6be3eSSvatopluk Kraus 
296bff6be3eSSvatopluk Kraus static int
gpio_pic_teardown_intr(device_t dev,struct intr_irqsrc * isrc,struct resource * res,struct intr_map_data * data)2978294827aSIan Lepore gpio_pic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
2988294827aSIan Lepore     struct resource *res, struct intr_map_data *data)
2998294827aSIan Lepore {
3008294827aSIan Lepore 	struct imx51_gpio_softc *sc;
3018294827aSIan Lepore 	struct gpio_irqsrc *gi;
3028294827aSIan Lepore 
3038294827aSIan Lepore 	sc = device_get_softc(dev);
3048294827aSIan Lepore 	if (isrc->isrc_handlers == 0) {
3058294827aSIan Lepore 		gi = (struct gpio_irqsrc *)isrc;
306e2ff80bcSSvatopluk Kraus 		gi->gi_mode = GPIO_INTR_CONFORM;
3078294827aSIan Lepore 
3088294827aSIan Lepore 		// XXX Not sure this is necessary
3098294827aSIan Lepore 		mtx_lock_spin(&sc->sc_mtx);
3108294827aSIan Lepore 		CLEAR4(sc, IMX_GPIO_IMR_REG, (1U << gi->gi_irq));
3118294827aSIan Lepore 		WRITE4(sc, IMX_GPIO_ISR_REG, (1U << gi->gi_irq));
3128294827aSIan Lepore 		mtx_unlock_spin(&sc->sc_mtx);
3138294827aSIan Lepore 	}
3148294827aSIan Lepore 	return (0);
3158294827aSIan Lepore }
3168294827aSIan Lepore 
3178294827aSIan Lepore static int
gpio_pic_setup_intr(device_t dev,struct intr_irqsrc * isrc,struct resource * res,struct intr_map_data * data)318bff6be3eSSvatopluk Kraus gpio_pic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
319bff6be3eSSvatopluk Kraus     struct resource *res, struct intr_map_data *data)
320e6c43510SIan Lepore {
321e6c43510SIan Lepore 	struct imx51_gpio_softc *sc;
322bff6be3eSSvatopluk Kraus 	struct gpio_irqsrc *gi;
323e3f2d7afSIan Lepore 	int error;
324e3f2d7afSIan Lepore 	u_int icfg, irq, reg, shift, wrk;
325e2ff80bcSSvatopluk Kraus 	uint32_t mode;
326e6c43510SIan Lepore 
327f7b96696SSvatopluk Kraus 	if (data == NULL)
328f7b96696SSvatopluk Kraus 		return (ENOTSUP);
329f7b96696SSvatopluk Kraus 
330e6c43510SIan Lepore 	sc = device_get_softc(dev);
331bff6be3eSSvatopluk Kraus 	gi = (struct gpio_irqsrc *)isrc;
332e6c43510SIan Lepore 
333bff6be3eSSvatopluk Kraus 	/* Get config for interrupt. */
334f7b96696SSvatopluk Kraus 	error = gpio_pic_map(sc, data, &irq, &mode);
335bff6be3eSSvatopluk Kraus 	if (error != 0)
336bff6be3eSSvatopluk Kraus 		return (error);
337bff6be3eSSvatopluk Kraus 	if (gi->gi_irq != irq)
338bff6be3eSSvatopluk Kraus 		return (EINVAL);
339bff6be3eSSvatopluk Kraus 
340bff6be3eSSvatopluk Kraus 	/* Compare config if this is not first setup. */
341e2ff80bcSSvatopluk Kraus 	if (isrc->isrc_handlers != 0)
342e2ff80bcSSvatopluk Kraus 		return (gi->gi_mode == mode ? 0 : EINVAL);
343e2ff80bcSSvatopluk Kraus 	gi->gi_mode = mode;
344e3f2d7afSIan Lepore 
345e3f2d7afSIan Lepore 	/*
346e3f2d7afSIan Lepore 	 * To interrupt on both edges we have to use the EDGE register.  The
347e3f2d7afSIan Lepore 	 * manual says it only exists for backwards compatibilty with older imx
348e3f2d7afSIan Lepore 	 * chips, but it's also the only way to configure interrupting on both
349e3f2d7afSIan Lepore 	 * edges.  If the EDGE bit is on, the corresponding ICRn bit is ignored.
350e3f2d7afSIan Lepore 	 */
351e3f2d7afSIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
352e3f2d7afSIan Lepore 	if (mode == GPIO_INTR_EDGE_BOTH) {
353e3f2d7afSIan Lepore 		SET4(sc, IMX_GPIO_EDGE_REG, (1u << irq));
354e3f2d7afSIan Lepore 	} else {
355e3f2d7afSIan Lepore 		CLEAR4(sc, IMX_GPIO_EDGE_REG, (1u << irq));
356e2ff80bcSSvatopluk Kraus 		switch (mode) {
357e3f2d7afSIan Lepore 		default:
358e3f2d7afSIan Lepore 			/* silence warnings; default can't actually happen. */
359e3f2d7afSIan Lepore 			/* FALLTHROUGH */
360e2ff80bcSSvatopluk Kraus 		case GPIO_INTR_LEVEL_LOW:
361e6c43510SIan Lepore 			icfg = GPIO_ICR_COND_LOW;
362e2ff80bcSSvatopluk Kraus 			break;
363e2ff80bcSSvatopluk Kraus 		case GPIO_INTR_LEVEL_HIGH:
364e6c43510SIan Lepore 			icfg = GPIO_ICR_COND_HIGH;
365e2ff80bcSSvatopluk Kraus 			break;
366e2ff80bcSSvatopluk Kraus 		case GPIO_INTR_EDGE_RISING:
367e6c43510SIan Lepore 			icfg = GPIO_ICR_COND_RISE;
368e2ff80bcSSvatopluk Kraus 			break;
369e2ff80bcSSvatopluk Kraus 		case GPIO_INTR_EDGE_FALLING:
370e2ff80bcSSvatopluk Kraus 			icfg = GPIO_ICR_COND_FALL;
371e2ff80bcSSvatopluk Kraus 			break;
372e6c43510SIan Lepore 		}
373e6c43510SIan Lepore 		if (irq < 16) {
374e6c43510SIan Lepore 			reg = IMX_GPIO_ICR1_REG;
375e6c43510SIan Lepore 			shift = 2 * irq;
376e6c43510SIan Lepore 		} else {
377e6c43510SIan Lepore 			reg = IMX_GPIO_ICR2_REG;
378e6c43510SIan Lepore 			shift = 2 * (irq - 16);
379e6c43510SIan Lepore 		}
380e6c43510SIan Lepore 		wrk = READ4(sc, reg);
381e3f2d7afSIan Lepore 		wrk &= ~(GPIO_ICR_COND_MASK << shift);
382e6c43510SIan Lepore 		wrk |= icfg << shift;
383e6c43510SIan Lepore 		WRITE4(sc, reg, wrk);
384e3f2d7afSIan Lepore 	}
385e3f2d7afSIan Lepore 	WRITE4(sc, IMX_GPIO_ISR_REG, (1u << irq));
386e3f2d7afSIan Lepore 	SET4(sc, IMX_GPIO_IMR_REG, (1u << irq));
387e6c43510SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
388e3f2d7afSIan Lepore 
389bff6be3eSSvatopluk Kraus 	return (0);
390e6c43510SIan Lepore }
391e6c43510SIan Lepore 
392e6c43510SIan Lepore /*
3938294827aSIan Lepore  * this is mask_intr
3948294827aSIan Lepore  */
3958294827aSIan Lepore static void
gpio_pic_disable_intr(device_t dev,struct intr_irqsrc * isrc)3968294827aSIan Lepore gpio_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
3978294827aSIan Lepore {
3988294827aSIan Lepore 	struct imx51_gpio_softc *sc;
3998294827aSIan Lepore 	u_int irq;
4008294827aSIan Lepore 
4018294827aSIan Lepore 	sc = device_get_softc(dev);
4028294827aSIan Lepore 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
4038294827aSIan Lepore 
4048294827aSIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
4058294827aSIan Lepore 	CLEAR4(sc, IMX_GPIO_IMR_REG, (1U << irq));
4068294827aSIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
4078294827aSIan Lepore }
4088294827aSIan Lepore 
4098294827aSIan Lepore /*
410e6c43510SIan Lepore  * this is unmask_intr
411e6c43510SIan Lepore  */
412e6c43510SIan Lepore static void
gpio_pic_enable_intr(device_t dev,struct intr_irqsrc * isrc)413bff6be3eSSvatopluk Kraus gpio_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
414e6c43510SIan Lepore {
415e6c43510SIan Lepore 	struct imx51_gpio_softc *sc;
416bff6be3eSSvatopluk Kraus 	u_int irq;
417e6c43510SIan Lepore 
418e6c43510SIan Lepore 	sc = device_get_softc(dev);
419bff6be3eSSvatopluk Kraus 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
420e6c43510SIan Lepore 
421e6c43510SIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
422bff6be3eSSvatopluk Kraus 	SET4(sc, IMX_GPIO_IMR_REG, (1U << irq));
423e6c43510SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
424e6c43510SIan Lepore }
425e6c43510SIan Lepore 
426e6c43510SIan Lepore static void
gpio_pic_post_filter(device_t dev,struct intr_irqsrc * isrc)4272b3ad188SAdrian Chadd gpio_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
428e6c43510SIan Lepore {
429e6c43510SIan Lepore 	struct imx51_gpio_softc *sc;
430bff6be3eSSvatopluk Kraus 	u_int irq;
431e6c43510SIan Lepore 
432e6c43510SIan Lepore 	sc = device_get_softc(dev);
433bff6be3eSSvatopluk Kraus 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
434e6c43510SIan Lepore 
435e6c43510SIan Lepore 	arm_irq_memory_barrier(0);
436e6c43510SIan Lepore         /* EOI.  W1C reg so no r-m-w, no locking needed. */
437bff6be3eSSvatopluk Kraus 	WRITE4(sc, IMX_GPIO_ISR_REG, (1U << irq));
438e6c43510SIan Lepore }
439e6c43510SIan Lepore 
440e6c43510SIan Lepore static void
gpio_pic_post_ithread(device_t dev,struct intr_irqsrc * isrc)4412b3ad188SAdrian Chadd gpio_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
442e6c43510SIan Lepore {
443e3f2d7afSIan Lepore 	struct imx51_gpio_softc *sc;
444e3f2d7afSIan Lepore 	u_int irq;
445e3f2d7afSIan Lepore 
446e3f2d7afSIan Lepore 	sc = device_get_softc(dev);
447e3f2d7afSIan Lepore 	irq = ((struct gpio_irqsrc *)isrc)->gi_irq;
448e6c43510SIan Lepore 
449e6c43510SIan Lepore 	arm_irq_memory_barrier(0);
450e3f2d7afSIan Lepore 	/* EOI.  W1C reg so no r-m-w, no locking needed. */
451e3f2d7afSIan Lepore 	WRITE4(sc, IMX_GPIO_ISR_REG, (1U << irq));
452bff6be3eSSvatopluk Kraus 	gpio_pic_enable_intr(dev, isrc);
453e6c43510SIan Lepore }
454e6c43510SIan Lepore 
455e6c43510SIan Lepore static void
gpio_pic_pre_ithread(device_t dev,struct intr_irqsrc * isrc)4562b3ad188SAdrian Chadd gpio_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
457e6c43510SIan Lepore {
458bff6be3eSSvatopluk Kraus 	gpio_pic_disable_intr(dev, isrc);
459e6c43510SIan Lepore }
460e6c43510SIan Lepore 
461e6c43510SIan Lepore static int
gpio_pic_filter(void * arg)462e6c43510SIan Lepore gpio_pic_filter(void *arg)
463e6c43510SIan Lepore {
464e6c43510SIan Lepore 	struct imx51_gpio_softc *sc;
465bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
466e6c43510SIan Lepore 	uint32_t i, interrupts;
467e6c43510SIan Lepore 
468e6c43510SIan Lepore 	sc = arg;
469e6c43510SIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
470e6c43510SIan Lepore 	interrupts = READ4(sc, IMX_GPIO_ISR_REG) & READ4(sc, IMX_GPIO_IMR_REG);
471e6c43510SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
472e6c43510SIan Lepore 
473e6c43510SIan Lepore 	for (i = 0; interrupts != 0; i++, interrupts >>= 1) {
474e6c43510SIan Lepore 		if ((interrupts & 0x1) == 0)
475e6c43510SIan Lepore 			continue;
476bff6be3eSSvatopluk Kraus 		isrc = &sc->gpio_pic_irqsrc[i].gi_isrc;
477bff6be3eSSvatopluk Kraus 		if (intr_isrc_dispatch(isrc, curthread->td_intr_frame) != 0) {
478bff6be3eSSvatopluk Kraus 			gpio_pic_disable_intr(sc->dev, isrc);
479bff6be3eSSvatopluk Kraus 			gpio_pic_post_filter(sc->dev, isrc);
480bff6be3eSSvatopluk Kraus 			device_printf(sc->dev, "Stray irq %u disabled\n", i);
481bff6be3eSSvatopluk Kraus 		}
482e6c43510SIan Lepore 	}
483e6c43510SIan Lepore 
484e6c43510SIan Lepore 	return (FILTER_HANDLED);
485e6c43510SIan Lepore }
486bff6be3eSSvatopluk Kraus 
487bff6be3eSSvatopluk Kraus /*
4888294827aSIan Lepore  * Initialize our isrcs and register them with intrng.
489bff6be3eSSvatopluk Kraus  */
490bff6be3eSSvatopluk Kraus static int
gpio_pic_register_isrcs(struct imx51_gpio_softc * sc)491bff6be3eSSvatopluk Kraus gpio_pic_register_isrcs(struct imx51_gpio_softc *sc)
492bff6be3eSSvatopluk Kraus {
493bff6be3eSSvatopluk Kraus 	int error;
494bff6be3eSSvatopluk Kraus 	uint32_t irq;
495bff6be3eSSvatopluk Kraus 	const char *name;
496bff6be3eSSvatopluk Kraus 
497bff6be3eSSvatopluk Kraus 	name = device_get_nameunit(sc->dev);
498bff6be3eSSvatopluk Kraus 	for (irq = 0; irq < NGPIO; irq++) {
499bff6be3eSSvatopluk Kraus 		sc->gpio_pic_irqsrc[irq].gi_irq = irq;
500e2ff80bcSSvatopluk Kraus 		sc->gpio_pic_irqsrc[irq].gi_mode = GPIO_INTR_CONFORM;
501bff6be3eSSvatopluk Kraus 
502bff6be3eSSvatopluk Kraus 		error = intr_isrc_register(&sc->gpio_pic_irqsrc[irq].gi_isrc,
503bff6be3eSSvatopluk Kraus 		    sc->dev, 0, "%s,%u", name, irq);
504bff6be3eSSvatopluk Kraus 		if (error != 0) {
505bff6be3eSSvatopluk Kraus 			/* XXX call intr_isrc_deregister() */
506bff6be3eSSvatopluk Kraus 			device_printf(sc->dev, "%s failed", __func__);
507bff6be3eSSvatopluk Kraus 			return (error);
508bff6be3eSSvatopluk Kraus 		}
509bff6be3eSSvatopluk Kraus 	}
510bff6be3eSSvatopluk Kraus 	return (0);
511bff6be3eSSvatopluk Kraus }
512e6c43510SIan Lepore #endif
513e6c43510SIan Lepore 
514e6c43510SIan Lepore /*
515e6c43510SIan Lepore  *
516e6c43510SIan Lepore  */
5170495dcefSRuslan Bukin static void
imx51_gpio_pin_configure(struct imx51_gpio_softc * sc,struct gpio_pin * pin,unsigned int flags)5180495dcefSRuslan Bukin imx51_gpio_pin_configure(struct imx51_gpio_softc *sc, struct gpio_pin *pin,
5190495dcefSRuslan Bukin     unsigned int flags)
5200495dcefSRuslan Bukin {
521d8cf9c4fSIan Lepore 	u_int newflags, pad;
5220495dcefSRuslan Bukin 
523e6c43510SIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
5240495dcefSRuslan Bukin 
5250495dcefSRuslan Bukin 	/*
526d8cf9c4fSIan Lepore 	 * Manage input/output; other flags not supported yet (maybe not ever,
527d8cf9c4fSIan Lepore 	 * since we have no connection to the pad config registers from here).
528d8cf9c4fSIan Lepore 	 *
529d8cf9c4fSIan Lepore 	 * When setting a pin to output, honor the PRESET_[LOW,HIGH] flags if
530f1334ab7SGordon Bergling 	 * present.  Otherwise, for glitchless transitions on pins with pulls,
531d8cf9c4fSIan Lepore 	 * read the current state of the pad and preset the DR register to drive
532d8cf9c4fSIan Lepore 	 * the current value onto the pin before enabling the pin for output.
533bcfa4e67SIan Lepore 	 *
534bcfa4e67SIan Lepore 	 * Note that changes to pin->gp_flags must be acccumulated in newflags
535bcfa4e67SIan Lepore 	 * and stored with a single writeback to gp_flags at the end, to enable
536d8cf9c4fSIan Lepore 	 * unlocked reads of that value elsewhere. This is only about unlocked
537d8cf9c4fSIan Lepore 	 * access to gp_flags from elsewhere; we still use locking in this
538d8cf9c4fSIan Lepore 	 * function to protect r-m-w access to the hardware registers.
5390495dcefSRuslan Bukin 	 */
5400495dcefSRuslan Bukin 	if (flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) {
541bcfa4e67SIan Lepore 		newflags = pin->gp_flags & ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
5420495dcefSRuslan Bukin 		if (flags & GPIO_PIN_OUTPUT) {
543d8cf9c4fSIan Lepore 			if (flags & GPIO_PIN_PRESET_LOW) {
544d8cf9c4fSIan Lepore 				pad = 0;
545d8cf9c4fSIan Lepore 			} else if (flags & GPIO_PIN_PRESET_HIGH) {
546d8cf9c4fSIan Lepore 				pad = 1;
547d8cf9c4fSIan Lepore 			} else {
548d8cf9c4fSIan Lepore 				if (flags & GPIO_PIN_OPENDRAIN)
549d8cf9c4fSIan Lepore 					pad = READ4(sc, IMX_GPIO_PSR_REG);
550d8cf9c4fSIan Lepore 				else
551d8cf9c4fSIan Lepore 					pad = READ4(sc, IMX_GPIO_DR_REG);
552d8cf9c4fSIan Lepore 				pad = (pad >> pin->gp_pin) & 1;
553d8cf9c4fSIan Lepore 			}
554bcfa4e67SIan Lepore 			newflags |= GPIO_PIN_OUTPUT;
555d8cf9c4fSIan Lepore 			SET4(sc, IMX_GPIO_DR_REG, (pad << pin->gp_pin));
556e6c43510SIan Lepore 			SET4(sc, IMX_GPIO_OE_REG, (1U << pin->gp_pin));
557bcfa4e67SIan Lepore 		} else {
558bcfa4e67SIan Lepore 			newflags |= GPIO_PIN_INPUT;
559e6c43510SIan Lepore 			CLEAR4(sc, IMX_GPIO_OE_REG, (1U << pin->gp_pin));
5600495dcefSRuslan Bukin 		}
561bcfa4e67SIan Lepore 		pin->gp_flags = newflags;
5620495dcefSRuslan Bukin 	}
5630495dcefSRuslan Bukin 
564e6c43510SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
5650495dcefSRuslan Bukin }
5660495dcefSRuslan Bukin 
5677836352bSLuiz Otavio O Souza static device_t
imx51_gpio_get_bus(device_t dev)5687836352bSLuiz Otavio O Souza imx51_gpio_get_bus(device_t dev)
5697836352bSLuiz Otavio O Souza {
5707836352bSLuiz Otavio O Souza 	struct imx51_gpio_softc *sc;
5717836352bSLuiz Otavio O Souza 
5727836352bSLuiz Otavio O Souza 	sc = device_get_softc(dev);
5737836352bSLuiz Otavio O Souza 
5747836352bSLuiz Otavio O Souza 	return (sc->sc_busdev);
5757836352bSLuiz Otavio O Souza }
5767836352bSLuiz Otavio O Souza 
5770495dcefSRuslan Bukin static int
imx51_gpio_pin_max(device_t dev,int * maxpin)5780495dcefSRuslan Bukin imx51_gpio_pin_max(device_t dev, int *maxpin)
5790495dcefSRuslan Bukin {
580e6c43510SIan Lepore 	struct imx51_gpio_softc *sc;
5810495dcefSRuslan Bukin 
582e6c43510SIan Lepore 	sc = device_get_softc(dev);
583e6c43510SIan Lepore 	*maxpin = sc->gpio_npins - 1;
584e6c43510SIan Lepore 
5850495dcefSRuslan Bukin 	return (0);
5860495dcefSRuslan Bukin }
5870495dcefSRuslan Bukin 
5880495dcefSRuslan Bukin static int
imx51_gpio_pin_getcaps(device_t dev,uint32_t pin,uint32_t * caps)5890495dcefSRuslan Bukin imx51_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
5900495dcefSRuslan Bukin {
5910495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
5920495dcefSRuslan Bukin 
5930495dcefSRuslan Bukin 	sc = device_get_softc(dev);
5940495dcefSRuslan Bukin 
5950dbb1fc8SIan Lepore 	if (pin >= sc->gpio_npins)
5960495dcefSRuslan Bukin 		return (EINVAL);
5970495dcefSRuslan Bukin 
5980dbb1fc8SIan Lepore 	*caps = sc->gpio_pins[pin].gp_caps;
5990495dcefSRuslan Bukin 
6000495dcefSRuslan Bukin 	return (0);
6010495dcefSRuslan Bukin }
6020495dcefSRuslan Bukin 
6030495dcefSRuslan Bukin static int
imx51_gpio_pin_getflags(device_t dev,uint32_t pin,uint32_t * flags)6040495dcefSRuslan Bukin imx51_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
6050495dcefSRuslan Bukin {
6060495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
6070495dcefSRuslan Bukin 
6080495dcefSRuslan Bukin 	sc = device_get_softc(dev);
6090495dcefSRuslan Bukin 
6100dbb1fc8SIan Lepore 	if (pin >= sc->gpio_npins)
6110495dcefSRuslan Bukin 		return (EINVAL);
6120495dcefSRuslan Bukin 
6130dbb1fc8SIan Lepore 	*flags = sc->gpio_pins[pin].gp_flags;
6140495dcefSRuslan Bukin 
6150495dcefSRuslan Bukin 	return (0);
6160495dcefSRuslan Bukin }
6170495dcefSRuslan Bukin 
6180495dcefSRuslan Bukin static int
imx51_gpio_pin_getname(device_t dev,uint32_t pin,char * name)6190495dcefSRuslan Bukin imx51_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
6200495dcefSRuslan Bukin {
6210495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
6220495dcefSRuslan Bukin 
6230495dcefSRuslan Bukin 	sc = device_get_softc(dev);
6240dbb1fc8SIan Lepore 	if (pin >= sc->gpio_npins)
6250495dcefSRuslan Bukin 		return (EINVAL);
6260495dcefSRuslan Bukin 
627e6c43510SIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
6280dbb1fc8SIan Lepore 	memcpy(name, sc->gpio_pins[pin].gp_name, GPIOMAXNAME);
629e6c43510SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
6300495dcefSRuslan Bukin 
6310495dcefSRuslan Bukin 	return (0);
6320495dcefSRuslan Bukin }
6330495dcefSRuslan Bukin 
6340495dcefSRuslan Bukin static int
imx51_gpio_pin_setflags(device_t dev,uint32_t pin,uint32_t flags)6350495dcefSRuslan Bukin imx51_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
6360495dcefSRuslan Bukin {
6370495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
6380495dcefSRuslan Bukin 
6390495dcefSRuslan Bukin 	sc = device_get_softc(dev);
6400495dcefSRuslan Bukin 
6410dbb1fc8SIan Lepore 	if (pin >= sc->gpio_npins)
6420495dcefSRuslan Bukin 		return (EINVAL);
6430495dcefSRuslan Bukin 
6440dbb1fc8SIan Lepore 	imx51_gpio_pin_configure(sc, &sc->gpio_pins[pin], flags);
6450495dcefSRuslan Bukin 
6460495dcefSRuslan Bukin 	return (0);
6470495dcefSRuslan Bukin }
6480495dcefSRuslan Bukin 
6490495dcefSRuslan Bukin static int
imx51_gpio_pin_set(device_t dev,uint32_t pin,unsigned int value)6500495dcefSRuslan Bukin imx51_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
6510495dcefSRuslan Bukin {
6520495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
6530495dcefSRuslan Bukin 
6540495dcefSRuslan Bukin 	sc = device_get_softc(dev);
6550495dcefSRuslan Bukin 
6560dbb1fc8SIan Lepore 	if (pin >= sc->gpio_npins)
6570495dcefSRuslan Bukin 		return (EINVAL);
6580495dcefSRuslan Bukin 
659e6c43510SIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
6600495dcefSRuslan Bukin 	if (value)
6610dbb1fc8SIan Lepore 		SET4(sc, IMX_GPIO_DR_REG, (1U << pin));
6620495dcefSRuslan Bukin 	else
6630dbb1fc8SIan Lepore 		CLEAR4(sc, IMX_GPIO_DR_REG, (1U << pin));
664e6c43510SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
6650495dcefSRuslan Bukin 
6660495dcefSRuslan Bukin 	return (0);
6670495dcefSRuslan Bukin }
6680495dcefSRuslan Bukin 
6690495dcefSRuslan Bukin static int
imx51_gpio_pin_get(device_t dev,uint32_t pin,unsigned int * val)6700495dcefSRuslan Bukin imx51_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
6710495dcefSRuslan Bukin {
6720495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
6730495dcefSRuslan Bukin 
6740495dcefSRuslan Bukin 	sc = device_get_softc(dev);
6750495dcefSRuslan Bukin 
6760dbb1fc8SIan Lepore 	if (pin >= sc->gpio_npins)
6770495dcefSRuslan Bukin 		return (EINVAL);
6780495dcefSRuslan Bukin 
679e780d0fcSIan Lepore 	/*
680e780d0fcSIan Lepore 	 * Normally a pin set for output can be read by reading the DR reg which
681e780d0fcSIan Lepore 	 * indicates what value is being driven to that pin.  The exception is
682e780d0fcSIan Lepore 	 * pins configured for open-drain mode, in which case we have to read
683e780d0fcSIan Lepore 	 * the pad status register in case the pin is being driven externally.
684e780d0fcSIan Lepore 	 * Doing so requires that the SION bit be configured in pinmux, which
685e780d0fcSIan Lepore 	 * isn't the case for most normal gpio pins, so only try to read via PSR
686e780d0fcSIan Lepore 	 * if the OPENDRAIN flag is set, and it's the user's job to correctly
687e780d0fcSIan Lepore 	 * configure SION along with open-drain output mode for those pins.
688e780d0fcSIan Lepore 	 */
689e780d0fcSIan Lepore 	if (sc->gpio_pins[pin].gp_flags & GPIO_PIN_OPENDRAIN)
690c5922310SIan Lepore 		*val = (READ4(sc, IMX_GPIO_PSR_REG) >> pin) & 1;
691e780d0fcSIan Lepore 	else
692e780d0fcSIan Lepore 		*val = (READ4(sc, IMX_GPIO_DR_REG) >> pin) & 1;
6930495dcefSRuslan Bukin 
6940495dcefSRuslan Bukin 	return (0);
6950495dcefSRuslan Bukin }
6960495dcefSRuslan Bukin 
6970495dcefSRuslan Bukin static int
imx51_gpio_pin_toggle(device_t dev,uint32_t pin)6980495dcefSRuslan Bukin imx51_gpio_pin_toggle(device_t dev, uint32_t pin)
6990495dcefSRuslan Bukin {
7000495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
7010495dcefSRuslan Bukin 
7020495dcefSRuslan Bukin 	sc = device_get_softc(dev);
7030495dcefSRuslan Bukin 
7040dbb1fc8SIan Lepore 	if (pin >= sc->gpio_npins)
7050495dcefSRuslan Bukin 		return (EINVAL);
7060495dcefSRuslan Bukin 
707e6c43510SIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
7080495dcefSRuslan Bukin 	WRITE4(sc, IMX_GPIO_DR_REG,
7090dbb1fc8SIan Lepore 	    (READ4(sc, IMX_GPIO_DR_REG) ^ (1U << pin)));
710e6c43510SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
7110495dcefSRuslan Bukin 
7120495dcefSRuslan Bukin 	return (0);
7130495dcefSRuslan Bukin }
7140495dcefSRuslan Bukin 
7150495dcefSRuslan Bukin static int
imx51_gpio_pin_access_32(device_t dev,uint32_t first_pin,uint32_t clear_pins,uint32_t change_pins,uint32_t * orig_pins)716e1275c68SIan Lepore imx51_gpio_pin_access_32(device_t dev, uint32_t first_pin, uint32_t clear_pins,
717e1275c68SIan Lepore     uint32_t change_pins, uint32_t *orig_pins)
718e1275c68SIan Lepore {
719e1275c68SIan Lepore 	struct imx51_gpio_softc *sc;
720e1275c68SIan Lepore 
721e1275c68SIan Lepore 	if (first_pin != 0)
722e1275c68SIan Lepore 		return (EINVAL);
723e1275c68SIan Lepore 
724e1275c68SIan Lepore 	sc = device_get_softc(dev);
725e1275c68SIan Lepore 
726e1275c68SIan Lepore 	if (orig_pins != NULL)
727d8cf9c4fSIan Lepore 		*orig_pins = READ4(sc, IMX_GPIO_DR_REG);
728e1275c68SIan Lepore 
729e1275c68SIan Lepore 	if ((clear_pins | change_pins) != 0) {
730e1275c68SIan Lepore 		mtx_lock_spin(&sc->sc_mtx);
731e1275c68SIan Lepore 		WRITE4(sc, IMX_GPIO_DR_REG,
732e1275c68SIan Lepore 		    (READ4(sc, IMX_GPIO_DR_REG) & ~clear_pins) ^ change_pins);
733e1275c68SIan Lepore 		mtx_unlock_spin(&sc->sc_mtx);
734e1275c68SIan Lepore 	}
735e1275c68SIan Lepore 
736e1275c68SIan Lepore 	return (0);
737e1275c68SIan Lepore }
738e1275c68SIan Lepore 
739e1275c68SIan Lepore static int
imx51_gpio_pin_config_32(device_t dev,uint32_t first_pin,uint32_t num_pins,uint32_t * pin_flags)740e1275c68SIan Lepore imx51_gpio_pin_config_32(device_t dev, uint32_t first_pin, uint32_t num_pins,
741e1275c68SIan Lepore     uint32_t *pin_flags)
742e1275c68SIan Lepore {
743e1275c68SIan Lepore 	struct imx51_gpio_softc *sc;
744e1275c68SIan Lepore 	u_int i;
745e1275c68SIan Lepore 	uint32_t bit, drclr, drset, flags, oeclr, oeset, pads;
746e1275c68SIan Lepore 
747e1275c68SIan Lepore 	sc = device_get_softc(dev);
748e1275c68SIan Lepore 
749e1275c68SIan Lepore 	if (first_pin != 0 || num_pins > sc->gpio_npins)
750e1275c68SIan Lepore 		return (EINVAL);
751e1275c68SIan Lepore 
752e1275c68SIan Lepore 	drclr = drset = oeclr = oeset = 0;
753d8cf9c4fSIan Lepore 	pads = READ4(sc, IMX_GPIO_DR_REG);
754e1275c68SIan Lepore 
755e1275c68SIan Lepore 	for (i = 0; i < num_pins; ++i) {
756e1275c68SIan Lepore 		bit = 1u << i;
757e1275c68SIan Lepore 		flags = pin_flags[i];
758e1275c68SIan Lepore 		if (flags & GPIO_PIN_INPUT) {
759e1275c68SIan Lepore 			oeclr |= bit;
760e1275c68SIan Lepore 		} else if (flags & GPIO_PIN_OUTPUT) {
761e1275c68SIan Lepore 			oeset |= bit;
762e1275c68SIan Lepore 			if (flags & GPIO_PIN_PRESET_LOW)
763e1275c68SIan Lepore 				drclr |= bit;
764e1275c68SIan Lepore 			else if (flags & GPIO_PIN_PRESET_HIGH)
765e1275c68SIan Lepore 				drset |= bit;
766e1275c68SIan Lepore 			else /* Drive whatever it's now pulled to. */
767e1275c68SIan Lepore 				drset |= pads & bit;
768e1275c68SIan Lepore 		}
769e1275c68SIan Lepore 	}
770e1275c68SIan Lepore 
771e1275c68SIan Lepore 	mtx_lock_spin(&sc->sc_mtx);
772e1275c68SIan Lepore 	WRITE4(sc, IMX_GPIO_DR_REG,
773e1275c68SIan Lepore 	    (READ4(sc, IMX_GPIO_DR_REG) & ~drclr) | drset);
774e1275c68SIan Lepore 	WRITE4(sc, IMX_GPIO_OE_REG,
775e1275c68SIan Lepore 	    (READ4(sc, IMX_GPIO_OE_REG) & ~oeclr) | oeset);
776e1275c68SIan Lepore 	mtx_unlock_spin(&sc->sc_mtx);
777e1275c68SIan Lepore 
778e1275c68SIan Lepore 	return (0);
779e1275c68SIan Lepore }
780e1275c68SIan Lepore 
781e1275c68SIan Lepore static int
imx51_gpio_probe(device_t dev)7820495dcefSRuslan Bukin imx51_gpio_probe(device_t dev)
7830495dcefSRuslan Bukin {
7840495dcefSRuslan Bukin 
7850495dcefSRuslan Bukin 	if (!ofw_bus_status_okay(dev))
7860495dcefSRuslan Bukin 		return (ENXIO);
7870495dcefSRuslan Bukin 
7880495dcefSRuslan Bukin 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
7890495dcefSRuslan Bukin 		device_set_desc(dev, "Freescale i.MX GPIO Controller");
7900495dcefSRuslan Bukin 		return (BUS_PROBE_DEFAULT);
7910495dcefSRuslan Bukin 	}
7920495dcefSRuslan Bukin 
7930495dcefSRuslan Bukin 	return (ENXIO);
7940495dcefSRuslan Bukin }
7950495dcefSRuslan Bukin 
7960495dcefSRuslan Bukin static int
imx51_gpio_attach(device_t dev)7970495dcefSRuslan Bukin imx51_gpio_attach(device_t dev)
7980495dcefSRuslan Bukin {
7990495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
800e6c43510SIan Lepore 	int i, irq, unit;
80194bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
80294bc2117SOleksandr Tymoshenko 	int err;
80394bc2117SOleksandr Tymoshenko #endif
8040495dcefSRuslan Bukin 
8050495dcefSRuslan Bukin 	sc = device_get_softc(dev);
806e6c43510SIan Lepore 	sc->dev = dev;
807e6c43510SIan Lepore 	sc->gpio_npins = NGPIO;
808e6c43510SIan Lepore 
809e6c43510SIan Lepore 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->dev), NULL, MTX_SPIN);
8100495dcefSRuslan Bukin 
81194bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
81294bc2117SOleksandr Tymoshenko 	if (clk_get_by_ofw_index(sc->dev, 0, 0, &sc->clk) != 0) {
81394bc2117SOleksandr Tymoshenko 		device_printf(dev, "could not get clock");
81494bc2117SOleksandr Tymoshenko 		return (ENOENT);
81594bc2117SOleksandr Tymoshenko 	}
81694bc2117SOleksandr Tymoshenko 
81794bc2117SOleksandr Tymoshenko 	err = clk_enable(sc->clk);
81894bc2117SOleksandr Tymoshenko 	if (err != 0) {
81994bc2117SOleksandr Tymoshenko 		device_printf(sc->dev, "could not enable ipg clock\n");
82094bc2117SOleksandr Tymoshenko 		return (err);
82194bc2117SOleksandr Tymoshenko 	}
82294bc2117SOleksandr Tymoshenko #endif
82394bc2117SOleksandr Tymoshenko 
8240495dcefSRuslan Bukin 	if (bus_alloc_resources(dev, imx_gpio_spec, sc->sc_res)) {
8250495dcefSRuslan Bukin 		device_printf(dev, "could not allocate resources\n");
826876c1bd8SLuiz Otavio O Souza 		bus_release_resources(dev, imx_gpio_spec, sc->sc_res);
827876c1bd8SLuiz Otavio O Souza 		mtx_destroy(&sc->sc_mtx);
8280495dcefSRuslan Bukin 		return (ENXIO);
8290495dcefSRuslan Bukin 	}
8300495dcefSRuslan Bukin 
8310495dcefSRuslan Bukin 	sc->sc_iot = rman_get_bustag(sc->sc_res[0]);
8320495dcefSRuslan Bukin 	sc->sc_ioh = rman_get_bushandle(sc->sc_res[0]);
8330495dcefSRuslan Bukin 	/*
834e6c43510SIan Lepore 	 * Mask off all interrupts in hardware, then set up interrupt handling.
8350495dcefSRuslan Bukin 	 */
836e6c43510SIan Lepore 	WRITE4(sc, IMX_GPIO_IMR_REG, 0);
837e6c43510SIan Lepore 	for (irq = 0; irq < 2; irq++) {
83859c3cb81SAndrew Turner #ifdef INTRNG
839e6c43510SIan Lepore 		if ((bus_setup_intr(dev, sc->sc_res[1 + irq], INTR_TYPE_CLK,
840e6c43510SIan Lepore 		    gpio_pic_filter, NULL, sc, &sc->gpio_ih[irq]))) {
8410495dcefSRuslan Bukin 			device_printf(dev,
8420495dcefSRuslan Bukin 			    "WARNING: unable to register interrupt handler\n");
843876c1bd8SLuiz Otavio O Souza 			imx51_gpio_detach(dev);
8440495dcefSRuslan Bukin 			return (ENXIO);
8450495dcefSRuslan Bukin 		}
846e6c43510SIan Lepore #endif
8470495dcefSRuslan Bukin 	}
8480495dcefSRuslan Bukin 
849e6c43510SIan Lepore 	unit = device_get_unit(dev);
8500495dcefSRuslan Bukin 	for (i = 0; i < sc->gpio_npins; i++) {
8510495dcefSRuslan Bukin  		sc->gpio_pins[i].gp_pin = i;
8520495dcefSRuslan Bukin  		sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
8530495dcefSRuslan Bukin  		sc->gpio_pins[i].gp_flags =
854e6c43510SIan Lepore  		    (READ4(sc, IMX_GPIO_OE_REG) & (1U << i)) ? GPIO_PIN_OUTPUT :
8550495dcefSRuslan Bukin  		    GPIO_PIN_INPUT;
8560495dcefSRuslan Bukin  		snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME,
8575002d69fSIan Lepore  		    "GPIO%d_IO%02d", unit + 1, i);
8580495dcefSRuslan Bukin 	}
859e6c43510SIan Lepore 
86059c3cb81SAndrew Turner #ifdef INTRNG
861bff6be3eSSvatopluk Kraus 	gpio_pic_register_isrcs(sc);
8622b3ad188SAdrian Chadd 	intr_pic_register(dev, OF_xref_from_node(ofw_bus_get_node(dev)));
863e6c43510SIan Lepore #endif
8647836352bSLuiz Otavio O Souza 	sc->sc_busdev = gpiobus_attach_bus(dev);
865e6c43510SIan Lepore 
8667836352bSLuiz Otavio O Souza 	if (sc->sc_busdev == NULL) {
8677836352bSLuiz Otavio O Souza 		imx51_gpio_detach(dev);
8687836352bSLuiz Otavio O Souza 		return (ENXIO);
8697836352bSLuiz Otavio O Souza 	}
8700495dcefSRuslan Bukin 
8717836352bSLuiz Otavio O Souza 	return (0);
8720495dcefSRuslan Bukin }
8730495dcefSRuslan Bukin 
8740495dcefSRuslan Bukin static int
imx51_gpio_detach(device_t dev)8750495dcefSRuslan Bukin imx51_gpio_detach(device_t dev)
8760495dcefSRuslan Bukin {
877876c1bd8SLuiz Otavio O Souza 	int irq;
8780495dcefSRuslan Bukin 	struct imx51_gpio_softc *sc;
87994bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
88094bc2117SOleksandr Tymoshenko 	int error;
88194bc2117SOleksandr Tymoshenko #endif
8820495dcefSRuslan Bukin 
8830495dcefSRuslan Bukin 	sc = device_get_softc(dev);
8840495dcefSRuslan Bukin 
88594bc2117SOleksandr Tymoshenko #ifdef IMX_ENABLE_CLOCKS
88694bc2117SOleksandr Tymoshenko 	error = clk_disable(sc->clk);
88794bc2117SOleksandr Tymoshenko 	if (error != 0) {
88894bc2117SOleksandr Tymoshenko 		device_printf(sc->dev, "could not disable ipg clock\n");
88994bc2117SOleksandr Tymoshenko 		return (error);
89094bc2117SOleksandr Tymoshenko 	}
89194bc2117SOleksandr Tymoshenko #endif
89294bc2117SOleksandr Tymoshenko 
8937836352bSLuiz Otavio O Souza 	gpiobus_detach_bus(dev);
894333559f2SIan Lepore 	for (irq = 0; irq < NUM_IRQRES; irq++) {
895876c1bd8SLuiz Otavio O Souza 		if (sc->gpio_ih[irq])
896333559f2SIan Lepore 			bus_teardown_intr(dev, sc->sc_res[irq + FIRST_IRQRES],
897333559f2SIan Lepore 			    sc->gpio_ih[irq]);
898876c1bd8SLuiz Otavio O Souza 	}
8990495dcefSRuslan Bukin 	bus_release_resources(dev, imx_gpio_spec, sc->sc_res);
9000495dcefSRuslan Bukin 	mtx_destroy(&sc->sc_mtx);
9010495dcefSRuslan Bukin 
9020495dcefSRuslan Bukin 	return(0);
9030495dcefSRuslan Bukin }
9040495dcefSRuslan Bukin 
905ef4a3514SIan Lepore static phandle_t
imx51_gpio_get_node(device_t bus,device_t dev)906ef4a3514SIan Lepore imx51_gpio_get_node(device_t bus, device_t dev)
907ef4a3514SIan Lepore {
908ef4a3514SIan Lepore 	/*
909ef4a3514SIan Lepore 	 * Share controller node with gpiobus device
910ef4a3514SIan Lepore 	 */
911ef4a3514SIan Lepore 	return ofw_bus_get_node(bus);
912ef4a3514SIan Lepore }
913ef4a3514SIan Lepore 
9140495dcefSRuslan Bukin static device_method_t imx51_gpio_methods[] = {
9150495dcefSRuslan Bukin 	DEVMETHOD(device_probe,		imx51_gpio_probe),
9160495dcefSRuslan Bukin 	DEVMETHOD(device_attach,	imx51_gpio_attach),
9170495dcefSRuslan Bukin 	DEVMETHOD(device_detach,	imx51_gpio_detach),
9180495dcefSRuslan Bukin 
91959c3cb81SAndrew Turner #ifdef INTRNG
920e6c43510SIan Lepore 	/* Interrupt controller interface */
921e6c43510SIan Lepore 	DEVMETHOD(pic_disable_intr,	gpio_pic_disable_intr),
922e6c43510SIan Lepore 	DEVMETHOD(pic_enable_intr,	gpio_pic_enable_intr),
923bff6be3eSSvatopluk Kraus 	DEVMETHOD(pic_map_intr,		gpio_pic_map_intr),
924bff6be3eSSvatopluk Kraus 	DEVMETHOD(pic_setup_intr,	gpio_pic_setup_intr),
925bff6be3eSSvatopluk Kraus 	DEVMETHOD(pic_teardown_intr,	gpio_pic_teardown_intr),
926e6c43510SIan Lepore 	DEVMETHOD(pic_post_filter,	gpio_pic_post_filter),
927e6c43510SIan Lepore 	DEVMETHOD(pic_post_ithread,	gpio_pic_post_ithread),
928e6c43510SIan Lepore 	DEVMETHOD(pic_pre_ithread,	gpio_pic_pre_ithread),
929e6c43510SIan Lepore #endif
930e6c43510SIan Lepore 
931ef4a3514SIan Lepore 	/* OFW methods */
932ef4a3514SIan Lepore 	DEVMETHOD(ofw_bus_get_node,	imx51_gpio_get_node),
933ef4a3514SIan Lepore 
9340495dcefSRuslan Bukin 	/* GPIO protocol */
9357836352bSLuiz Otavio O Souza 	DEVMETHOD(gpio_get_bus,		imx51_gpio_get_bus),
9360495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_max,		imx51_gpio_pin_max),
9370495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_getname,	imx51_gpio_pin_getname),
9380495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_getflags,	imx51_gpio_pin_getflags),
9390495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_getcaps,	imx51_gpio_pin_getcaps),
9400495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_setflags,	imx51_gpio_pin_setflags),
9410495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_get,		imx51_gpio_pin_get),
9420495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_set,		imx51_gpio_pin_set),
9430495dcefSRuslan Bukin 	DEVMETHOD(gpio_pin_toggle,	imx51_gpio_pin_toggle),
944e1275c68SIan Lepore 	DEVMETHOD(gpio_pin_access_32,	imx51_gpio_pin_access_32),
945e1275c68SIan Lepore 	DEVMETHOD(gpio_pin_config_32,	imx51_gpio_pin_config_32),
9460495dcefSRuslan Bukin 	{0, 0},
9470495dcefSRuslan Bukin };
9480495dcefSRuslan Bukin 
9490495dcefSRuslan Bukin static driver_t imx51_gpio_driver = {
9500495dcefSRuslan Bukin 	"gpio",
9510495dcefSRuslan Bukin 	imx51_gpio_methods,
9520495dcefSRuslan Bukin 	sizeof(struct imx51_gpio_softc),
9530495dcefSRuslan Bukin };
9540495dcefSRuslan Bukin 
955ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx51_gpio, simplebus, imx51_gpio_driver, 0, 0,
956ea538dabSJohn Baldwin     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
957