xref: /netbsd-src/sys/arch/arm/imx/imx31_gpio.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: imx31_gpio.c,v 1.10 2021/08/07 16:18:44 thorpej Exp $	*/
2825088edSmatt /*-
3825088edSmatt  * Copyright (c) 2007 The NetBSD Foundation, Inc.
4825088edSmatt  * All rights reserved.
5825088edSmatt  *
6825088edSmatt  * This code is derived from software contributed to The NetBSD Foundation
7825088edSmatt  * by Matt Thomas
8825088edSmatt  *
9825088edSmatt  * Redistribution and use in source and binary forms, with or without
10825088edSmatt  * modification, are permitted provided that the following conditions
11825088edSmatt  * are met:
12825088edSmatt  * 1. Redistributions of source code must retain the above copyright
13825088edSmatt  *    notice, this list of conditions and the following disclaimer.
14825088edSmatt  * 2. Redistributions in binary form must reproduce the above copyright
15825088edSmatt  *    notice, this list of conditions and the following disclaimer in the
16825088edSmatt  *    documentation and/or other materials provided with the distribution.
17825088edSmatt  *
18825088edSmatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19825088edSmatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20825088edSmatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21825088edSmatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22825088edSmatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23825088edSmatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24825088edSmatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25825088edSmatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26825088edSmatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27825088edSmatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28825088edSmatt  * POSSIBILITY OF SUCH DAMAGE.
29825088edSmatt  */
30825088edSmatt #include <sys/cdefs.h>
31*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: imx31_gpio.c,v 1.10 2021/08/07 16:18:44 thorpej Exp $");
32825088edSmatt 
33825088edSmatt #define _INTR_PRIVATE
34825088edSmatt 
35825088edSmatt #include "locators.h"
36825088edSmatt #include "gpio.h"
37825088edSmatt 
38825088edSmatt #include <sys/param.h>
39825088edSmatt #include <sys/evcnt.h>
4028a551c9Smatt #include <sys/atomic.h>
41825088edSmatt 
42825088edSmatt #include <uvm/uvm_extern.h>
43825088edSmatt 
44825088edSmatt #include <machine/intr.h>
45825088edSmatt 
46825088edSmatt #include <arm/cpu.h>
47825088edSmatt #include <arm/armreg.h>
48825088edSmatt #include <arm/cpufunc.h>
49825088edSmatt 
50ed9977b1Sdyoung #include <sys/bus.h>
51825088edSmatt 
52825088edSmatt #include <arm/imx/imx31reg.h>
53825088edSmatt #include <arm/imx/imx31var.h>
54d8a4fe3fSbsh #include <arm/imx/imxgpioreg.h>
55825088edSmatt #include <arm/pic/picvar.h>
56825088edSmatt 
57825088edSmatt #if NGPIO > 0
58825088edSmatt #include <sys/gpio.h>
59825088edSmatt #include <dev/gpio/gpiovar.h>
60825088edSmatt #endif
61825088edSmatt 
62825088edSmatt static void gpio_pic_block_irqs(struct pic_softc *, size_t, uint32_t);
63825088edSmatt static void gpio_pic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
64825088edSmatt static int gpio_pic_find_pending_irqs(struct pic_softc *);
65825088edSmatt static void gpio_pic_establish_irq(struct pic_softc *, struct intrsource *);
66825088edSmatt 
67825088edSmatt const struct pic_ops gpio_pic_ops = {
68825088edSmatt 	.pic_block_irqs = gpio_pic_block_irqs,
69825088edSmatt 	.pic_unblock_irqs = gpio_pic_unblock_irqs,
70825088edSmatt 	.pic_find_pending_irqs = gpio_pic_find_pending_irqs,
71825088edSmatt 	.pic_establish_irq = gpio_pic_establish_irq,
72825088edSmatt };
73825088edSmatt 
74825088edSmatt struct gpio_softc {
75825088edSmatt 	struct pic_softc gpio_pic;
76825088edSmatt 	bus_space_tag_t gpio_memt;
77825088edSmatt 	bus_space_handle_t gpio_memh;
78825088edSmatt 	uint32_t gpio_enable_mask;
79825088edSmatt 	uint32_t gpio_edge_mask;
80825088edSmatt 	uint32_t gpio_level_mask;
81825088edSmatt #if NGPIO > 0
82825088edSmatt 	struct gpio_chipset_tag gpio_chipset;
83825088edSmatt 	gpio_pin_t gpio_pins[32];
84825088edSmatt #endif
85825088edSmatt };
86825088edSmatt 
87825088edSmatt #define	PIC_TO_SOFTC(pic) \
88825088edSmatt 	((struct gpio_softc *)((char *)(pic) - \
89825088edSmatt 		offsetof(struct gpio_softc, gpio_pic)))
90825088edSmatt 
91825088edSmatt #define	GPIO_READ(gpio, reg) \
92825088edSmatt 	bus_space_read_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg))
93825088edSmatt #define	GPIO_WRITE(gpio, reg, val) \
94825088edSmatt 	bus_space_write_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg), (val))
95825088edSmatt 
96825088edSmatt void
gpio_pic_unblock_irqs(struct pic_softc * pic,size_t irq_base,uint32_t irq_mask)97825088edSmatt gpio_pic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
98825088edSmatt {
99825088edSmatt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
100825088edSmatt 	KASSERT(irq_base == 0);
101825088edSmatt 
102825088edSmatt 	gpio->gpio_enable_mask |= irq_mask;
103825088edSmatt 	/*
104825088edSmatt 	 * If this a level source, ack it now.  If it's still asserted
105825088edSmatt 	 * it'll come back.
106825088edSmatt 	 */
107825088edSmatt 	if (irq_mask & gpio->gpio_level_mask)
108825088edSmatt 		GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
109825088edSmatt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
110825088edSmatt }
111825088edSmatt 
112825088edSmatt void
gpio_pic_block_irqs(struct pic_softc * pic,size_t irq_base,uint32_t irq_mask)113825088edSmatt gpio_pic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
114825088edSmatt {
115825088edSmatt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
116825088edSmatt 	KASSERT(irq_base == 0);
117825088edSmatt 
118825088edSmatt 	gpio->gpio_enable_mask &= ~irq_mask;
119825088edSmatt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
120825088edSmatt }
121825088edSmatt 
122825088edSmatt int
gpio_pic_find_pending_irqs(struct pic_softc * pic)123825088edSmatt gpio_pic_find_pending_irqs(struct pic_softc *pic)
124825088edSmatt {
125825088edSmatt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
126825088edSmatt 	uint32_t v;
127825088edSmatt 	uint32_t pending;
128825088edSmatt 
129825088edSmatt 	v = GPIO_READ(gpio, GPIO_ISR);
130825088edSmatt 	pending = (v & gpio->gpio_enable_mask);
131825088edSmatt 	if (pending == 0)
132825088edSmatt 		return 0;
133825088edSmatt 
134825088edSmatt 	/*
135825088edSmatt 	 * Disable the pending interrupts.
136825088edSmatt 	 */
137825088edSmatt 	gpio->gpio_enable_mask &= ~pending;
138825088edSmatt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
139825088edSmatt 
140825088edSmatt 	/*
141825088edSmatt 	 * If any of the sources are edge triggered, ack them now so
142825088edSmatt 	 * we won't lose them.
143825088edSmatt 	 */
144825088edSmatt 	if (v & gpio->gpio_edge_mask)
145825088edSmatt 		GPIO_WRITE(gpio, GPIO_ISR, v & gpio->gpio_edge_mask);
146825088edSmatt 
147825088edSmatt 	/*
148825088edSmatt 	 * Now find all the pending bits and mark them as pending.
149825088edSmatt 	 */
150825088edSmatt 	do {
151825088edSmatt 		int irq;
152825088edSmatt 		KASSERT(pending != 0);
153825088edSmatt 		irq = 31 - __builtin_clz(pending);
154825088edSmatt 		pending &= ~__BIT(irq);
155825088edSmatt 		pic_mark_pending(&gpio->gpio_pic, irq);
156825088edSmatt 	} while (pending != 0);
157825088edSmatt 
158825088edSmatt 	return 1;
159825088edSmatt }
160825088edSmatt 
161825088edSmatt #define GPIO_TYPEMAP \
162825088edSmatt 	((GPIO_ICR_LEVEL_LOW << (2*IST_LEVEL_LOW)) | \
163825088edSmatt 	 (GPIO_ICR_LEVEL_HIGH << (2*IST_LEVEL_HIGH)) | \
164825088edSmatt 	 (GPIO_ICR_EDGE_RISING << (2*IST_EDGE_RISING)) | \
165825088edSmatt 	 (GPIO_ICR_EDGE_FALLING << (2*IST_EDGE_FALLING)))
166825088edSmatt 
167825088edSmatt void
gpio_pic_establish_irq(struct pic_softc * pic,struct intrsource * is)168825088edSmatt gpio_pic_establish_irq(struct pic_softc *pic, struct intrsource *is)
169825088edSmatt {
170825088edSmatt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
171825088edSmatt 	KASSERT(is->is_irq < 32);
172825088edSmatt 	uint32_t irq_mask = __BIT(is->is_irq);
173825088edSmatt 	uint32_t v;
174825088edSmatt 	unsigned int icr_shift, icr_reg;
175825088edSmatt 	unsigned int gtype;
176825088edSmatt 
177825088edSmatt 	/*
178825088edSmatt 	 * Make sure the irq isn't enabled and not asserting.
179825088edSmatt 	 */
180825088edSmatt 	gpio->gpio_enable_mask &= ~irq_mask;
181825088edSmatt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
182825088edSmatt 	GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
183825088edSmatt 
184825088edSmatt 	/*
185825088edSmatt 	 * Convert the type to a gpio type and figure out which bits in what
186825088edSmatt 	 * register we have to tweak.
187825088edSmatt 	 */
188825088edSmatt 	gtype = (GPIO_TYPEMAP >> (2 * is->is_type)) & 3;
189825088edSmatt 	icr_shift = (is->is_irq & 0x0f) << 1;
190825088edSmatt 	icr_reg = GPIO_ICR1 + ((is->is_irq & 0x10) >> 2);
191825088edSmatt 
192825088edSmatt 	/*
193825088edSmatt 	 * Set the interrupt type.
194825088edSmatt 	 */
195825088edSmatt 	v = GPIO_READ(gpio, icr_reg);
196825088edSmatt 	v &= ~(3 << icr_shift);
197825088edSmatt 	v |= gtype << icr_shift;
198825088edSmatt 	GPIO_WRITE(gpio, icr_reg, v);
199825088edSmatt 
200825088edSmatt 	/*
201825088edSmatt 	 * Mark it as input.
202825088edSmatt 	 */
203825088edSmatt 	v = GPIO_READ(gpio, GPIO_DIR);
204825088edSmatt 	v &= ~irq_mask;
205825088edSmatt 	GPIO_WRITE(gpio, GPIO_DIR, v);
206825088edSmatt 
207825088edSmatt 	/*
208825088edSmatt 	 * Now record the type of interrupt.
209825088edSmatt 	 */
210825088edSmatt 	if (gtype == GPIO_ICR_EDGE_RISING || gtype == GPIO_ICR_EDGE_FALLING) {
211825088edSmatt 		gpio->gpio_edge_mask |= irq_mask;
212825088edSmatt 		gpio->gpio_level_mask &= ~irq_mask;
213825088edSmatt 	} else {
214825088edSmatt 		gpio->gpio_edge_mask &= ~irq_mask;
215825088edSmatt 		gpio->gpio_level_mask |= irq_mask;
216825088edSmatt 	}
217825088edSmatt }
218825088edSmatt 
219825088edSmatt static int gpio_match(device_t, cfdata_t, void *);
220825088edSmatt static void gpio_attach(device_t, device_t, void *);
221825088edSmatt 
222cbab9cadSchs CFATTACH_DECL_NEW(imxgpio,
223825088edSmatt 	sizeof(struct gpio_softc),
224825088edSmatt 	gpio_match, gpio_attach,
225825088edSmatt 	NULL, NULL);
226825088edSmatt 
227825088edSmatt #if NGPIO > 0
228825088edSmatt 
229825088edSmatt static int
imxgpio_pin_read(void * arg,int pin)230825088edSmatt imxgpio_pin_read(void *arg, int pin)
231825088edSmatt {
232825088edSmatt 	struct gpio_softc * const gpio = arg;
233825088edSmatt 
234825088edSmatt 	return (GPIO_READ(gpio, GPIO_DR) >> pin) & 1;
235825088edSmatt }
236825088edSmatt 
237825088edSmatt static void
imxgpio_pin_write(void * arg,int pin,int value)238825088edSmatt imxgpio_pin_write(void *arg, int pin, int value)
239825088edSmatt {
240825088edSmatt 	struct gpio_softc * const gpio = arg;
241825088edSmatt 	uint32_t mask = 1 << pin;
242825088edSmatt 	uint32_t old, new;
243825088edSmatt 
244825088edSmatt 	old = GPIO_READ(gpio, GPIO_DR);
245825088edSmatt 	if (value)
246825088edSmatt 		new = old | mask;
247825088edSmatt 	else
248825088edSmatt 		new = old & ~mask;
249825088edSmatt 
250825088edSmatt 	if (old != new)
251825088edSmatt 		GPIO_WRITE(gpio, GPIO_DR, new);
252825088edSmatt }
253825088edSmatt 
254825088edSmatt static void
imxgpio_pin_ctl(void * arg,int pin,int flags)255825088edSmatt imxgpio_pin_ctl(void *arg, int pin, int flags)
256825088edSmatt {
257825088edSmatt 	struct gpio_softc * const gpio = arg;
258825088edSmatt 	uint32_t mask = 1 << pin;
259825088edSmatt 	uint32_t old, new;
260825088edSmatt 
261825088edSmatt 	old = GPIO_READ(gpio, GPIO_DIR);
262825088edSmatt 	new = old;
263825088edSmatt 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
264825088edSmatt 	case GPIO_PIN_INPUT:	new |= mask; break;
265825088edSmatt 	case GPIO_PIN_OUTPUT:	new &= ~mask; break;
266825088edSmatt 	default:		return;
267825088edSmatt 	}
268825088edSmatt 	if (old != new)
269825088edSmatt 		GPIO_WRITE(gpio, GPIO_DIR, new);
270825088edSmatt }
271825088edSmatt 
272825088edSmatt static void
gpio_defer(device_t self)273825088edSmatt gpio_defer(device_t self)
274825088edSmatt {
275cbab9cadSchs 	struct gpio_softc * const gpio = device_private(self);
276825088edSmatt 	struct gpio_chipset_tag * const gp = &gpio->gpio_chipset;
277825088edSmatt 	struct gpiobus_attach_args gba;
278825088edSmatt 	gpio_pin_t *pins;
279825088edSmatt 	uint32_t mask, dir, value;
280825088edSmatt 	int pin;
281825088edSmatt 
282825088edSmatt 	gp->gp_cookie = gpio;
283825088edSmatt 	gp->gp_pin_read = imxgpio_pin_read;
284825088edSmatt 	gp->gp_pin_write = imxgpio_pin_write;
285825088edSmatt 	gp->gp_pin_ctl = imxgpio_pin_ctl;
286825088edSmatt 
287825088edSmatt 	gba.gba_gc = gp;
288825088edSmatt 	gba.gba_pins = gpio->gpio_pins;
289825088edSmatt 	gba.gba_npins = __arraycount(gpio->gpio_pins);
290825088edSmatt 
291825088edSmatt 	dir = GPIO_READ(gpio, GPIO_DIR);
292825088edSmatt 	value = GPIO_READ(gpio, GPIO_DR);
293825088edSmatt 	for (pin = 0, mask = 1, pins = gpio->gpio_pins;
294825088edSmatt 	     pin < 32; pin++, mask <<= 1, pins++) {
295825088edSmatt 		pins->pin_num = pin;
296825088edSmatt 		if ((gpio->gpio_edge_mask|gpio->gpio_level_mask) & mask)
297825088edSmatt 			pins->pin_caps = GPIO_PIN_INPUT;
298825088edSmatt 		else
299825088edSmatt 			pins->pin_caps = GPIO_PIN_INPUT|GPIO_PIN_OUTPUT;
300825088edSmatt 		pins->pin_flags =
301825088edSmatt 		    (dir & mask) ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
302825088edSmatt 		pins->pin_state =
303825088edSmatt 		    (value & mask) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
304825088edSmatt 	}
305825088edSmatt 
306*c7fb772bSthorpej 	config_found(self, &gba, gpiobus_print, CFARGS_NONE);
307825088edSmatt }
308825088edSmatt #endif /* NGPIO > 0 */
309825088edSmatt 
310825088edSmatt int
gpio_match(device_t parent,cfdata_t cfdata,void * aux)311825088edSmatt gpio_match(device_t parent, cfdata_t cfdata, void *aux)
312825088edSmatt {
313825088edSmatt 	struct ahb_attach_args *ahba = aux;
314825088edSmatt 	bus_space_handle_t memh;
315825088edSmatt 	bus_size_t size;
316825088edSmatt 	int error;
317825088edSmatt 
318825088edSmatt 	if (ahba->ahba_addr != GPIO1_BASE
319825088edSmatt 	    && ahba->ahba_addr != GPIO2_BASE
320825088edSmatt 	    && ahba->ahba_addr != GPIO3_BASE)
321825088edSmatt 		return 0;
322825088edSmatt 
323825088edSmatt 	size = (ahba->ahba_size == AHBCF_SIZE_DEFAULT) ? GPIO_SIZE : ahba->ahba_size;
324825088edSmatt 
325825088edSmatt 	error = bus_space_map(ahba->ahba_memt, ahba->ahba_addr, size, 0, &memh);
326825088edSmatt 	if (error)
327825088edSmatt 		return 0;
328825088edSmatt 
329825088edSmatt 	bus_space_unmap(ahba->ahba_memt, memh, size);
330825088edSmatt 	return 1;
331825088edSmatt }
332825088edSmatt 
333825088edSmatt void
gpio_attach(device_t parent,device_t self,void * aux)334825088edSmatt gpio_attach(device_t parent, device_t self, void *aux)
335825088edSmatt {
336825088edSmatt 	struct ahb_attach_args * const ahba = aux;
337cbab9cadSchs 	struct gpio_softc * const gpio = device_private(self);
338825088edSmatt 	int error;
339825088edSmatt 
340825088edSmatt 	if (ahba->ahba_size == AHBCF_SIZE_DEFAULT)
341825088edSmatt 		ahba->ahba_size = GPIO_SIZE;
342825088edSmatt 
343825088edSmatt 	gpio->gpio_memt = ahba->ahba_memt;
344825088edSmatt 	error = bus_space_map(ahba->ahba_memt, ahba->ahba_addr, ahba->ahba_size,
345825088edSmatt 	    0, &gpio->gpio_memh);
346825088edSmatt 
347825088edSmatt 	if (error) {
348825088edSmatt 		aprint_error(": failed to map register %#lx@%#lx: %d\n",
349825088edSmatt 		    ahba->ahba_size, ahba->ahba_addr, error);
350825088edSmatt 		return;
351825088edSmatt 	}
352825088edSmatt 
353825088edSmatt 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT) {
354825088edSmatt 		gpio->gpio_pic.pic_ops = &gpio_pic_ops;
355cbab9cadSchs 		strlcpy(gpio->gpio_pic.pic_name, device_xname(self),
356825088edSmatt 		    sizeof(gpio->gpio_pic.pic_name));
357825088edSmatt 		gpio->gpio_pic.pic_maxsources = 32;
358825088edSmatt 		pic_add(&gpio->gpio_pic, ahba->ahba_irqbase);
359825088edSmatt 		aprint_normal(": interrupts %d..%d",
360825088edSmatt 		    ahba->ahba_irqbase, ahba->ahba_irqbase + 31);
361825088edSmatt 	}
362825088edSmatt 	aprint_normal("\n");
363825088edSmatt #if NGPIO > 0
364825088edSmatt 	config_interrupts(self, gpio_defer);
365825088edSmatt #endif
366825088edSmatt }
367