imx31_gpio.c revision 1.1.2.2 1 1.1.2.1 matt /*-
2 1.1.2.1 matt * Copyright (c) 2007 The NetBSD Foundation, Inc.
3 1.1.2.1 matt * All rights reserved.
4 1.1.2.1 matt *
5 1.1.2.1 matt * This code is derived from software contributed to The NetBSD Foundation
6 1.1.2.1 matt * by Matt Thomas
7 1.1.2.1 matt *
8 1.1.2.1 matt * Redistribution and use in source and binary forms, with or without
9 1.1.2.1 matt * modification, are permitted provided that the following conditions
10 1.1.2.1 matt * are met:
11 1.1.2.1 matt * 1. Redistributions of source code must retain the above copyright
12 1.1.2.1 matt * notice, this list of conditions and the following disclaimer.
13 1.1.2.1 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.1 matt * notice, this list of conditions and the following disclaimer in the
15 1.1.2.1 matt * documentation and/or other materials provided with the distribution.
16 1.1.2.1 matt * 3. All advertising materials mentioning features or use of this software
17 1.1.2.1 matt * must display the following acknowledgement:
18 1.1.2.1 matt * This product includes software developed by the NetBSD
19 1.1.2.1 matt * Foundation, Inc. and its contributors.
20 1.1.2.1 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
21 1.1.2.1 matt * contributors may be used to endorse or promote products derived
22 1.1.2.1 matt * from this software without specific prior written permission.
23 1.1.2.1 matt *
24 1.1.2.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.1.2.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.1.2.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.1.2.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.1.2.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.1.2.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.1.2.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.1.2.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.1.2.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.1.2.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.1.2.1 matt * POSSIBILITY OF SUCH DAMAGE.
35 1.1.2.1 matt */
36 1.1.2.1 matt #include <sys/cdefs.h>
37 1.1.2.2 matt __KERNEL_RCSID(0, "$NetBSD: imx31_gpio.c,v 1.1.2.2 2007/09/11 02:32:26 matt Exp $");
38 1.1.2.1 matt
39 1.1.2.1 matt #define _INTR_PRIVATE
40 1.1.2.2 matt
41 1.1.2.2 matt #include "locators.h"
42 1.1.2.2 matt #include "gpio.h"
43 1.1.2.1 matt
44 1.1.2.1 matt #include <sys/param.h>
45 1.1.2.1 matt #include <sys/evcnt.h>
46 1.1.2.1 matt
47 1.1.2.1 matt #include <uvm/uvm_extern.h>
48 1.1.2.1 matt
49 1.1.2.1 matt #include <machine/intr.h>
50 1.1.2.1 matt
51 1.1.2.1 matt #include <arm/cpu.h>
52 1.1.2.1 matt #include <arm/armreg.h>
53 1.1.2.1 matt #include <arm/cpufunc.h>
54 1.1.2.1 matt
55 1.1.2.1 matt #include <machine/atomic.h>
56 1.1.2.1 matt #include <machine/bus.h>
57 1.1.2.1 matt
58 1.1.2.1 matt #include <arm/imx/imx31reg.h>
59 1.1.2.2 matt #include <arm/imx/imx31var.h>
60 1.1.2.2 matt #include <arm/pic/picvar.h>
61 1.1.2.2 matt
62 1.1.2.2 matt #if NGPIO > 0
63 1.1.2.2 matt #include <sys/gpio.h>
64 1.1.2.2 matt #include <dev/gpio/gpiovar.h>
65 1.1.2.2 matt #endif
66 1.1.2.1 matt
67 1.1.2.1 matt static void gpio_pic_block_irqs(struct pic_softc *, size_t, uint32_t);
68 1.1.2.1 matt static void gpio_pic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
69 1.1.2.1 matt static int gpio_pic_find_pending_irqs(struct pic_softc *);
70 1.1.2.1 matt static void gpio_pic_establish_irq(struct pic_softc *, int, int, int);
71 1.1.2.1 matt
72 1.1.2.1 matt const struct pic_ops gpio_pic_ops = {
73 1.1.2.1 matt .pic_block_irqs = gpio_pic_block_irqs,
74 1.1.2.1 matt .pic_unblock_irqs = gpio_pic_unblock_irqs,
75 1.1.2.2 matt .pic_find_pending_irqs = gpio_pic_find_pending_irqs,
76 1.1.2.2 matt .pic_establish_irq = gpio_pic_establish_irq,
77 1.1.2.1 matt };
78 1.1.2.1 matt
79 1.1.2.2 matt struct gpio_softc {
80 1.1.2.2 matt struct device gpio_dev;
81 1.1.2.2 matt struct pic_softc gpio_pic;
82 1.1.2.2 matt bus_space_tag_t gpio_memt;
83 1.1.2.2 matt bus_space_handle_t gpio_memh;
84 1.1.2.1 matt uint32_t gpio_enable_mask;
85 1.1.2.1 matt uint32_t gpio_edge_mask;
86 1.1.2.1 matt uint32_t gpio_level_mask;
87 1.1.2.2 matt #if NGPIO > 0
88 1.1.2.2 matt struct gpio_chipset_tag gpio_chipset;
89 1.1.2.2 matt gpio_pin_t gpio_pins[32];
90 1.1.2.2 matt #endif
91 1.1.2.1 matt };
92 1.1.2.1 matt
93 1.1.2.2 matt #define PIC_TO_SOFTC(pic) \
94 1.1.2.2 matt ((struct gpio_softc *)((char *)(pic) - \
95 1.1.2.2 matt offsetof(struct gpio_softc, gpio_pic)))
96 1.1.2.2 matt
97 1.1.2.2 matt #define GPIO_READ(gpio, reg) \
98 1.1.2.2 matt bus_space_read_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg))
99 1.1.2.2 matt #define GPIO_WRITE(gpio, reg, val) \
100 1.1.2.2 matt bus_space_write_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg), (val))
101 1.1.2.2 matt
102 1.1.2.1 matt void
103 1.1.2.1 matt gpio_pic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
104 1.1.2.1 matt {
105 1.1.2.2 matt struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
106 1.1.2.1 matt KASSERT(irq_base == 0);
107 1.1.2.2 matt
108 1.1.2.2 matt gpio->gpio_enable_mask |= irq_mask;
109 1.1.2.1 matt /*
110 1.1.2.1 matt * If this a level source, ack it now. If it's still asserted
111 1.1.2.1 matt * it'll come back.
112 1.1.2.1 matt */
113 1.1.2.2 matt if (irq_mask & gpio->gpio_level_mask)
114 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
115 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
116 1.1.2.1 matt }
117 1.1.2.1 matt
118 1.1.2.1 matt void
119 1.1.2.1 matt gpio_pic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
120 1.1.2.1 matt {
121 1.1.2.2 matt struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
122 1.1.2.1 matt KASSERT(irq_base == 0);
123 1.1.2.2 matt
124 1.1.2.2 matt gpio->gpio_enable_mask &= ~irq_mask;
125 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
126 1.1.2.1 matt }
127 1.1.2.1 matt
128 1.1.2.1 matt int
129 1.1.2.1 matt gpio_pic_find_pending_irqs(struct pic_softc *pic)
130 1.1.2.1 matt {
131 1.1.2.2 matt struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
132 1.1.2.1 matt uint32_t v;
133 1.1.2.1 matt uint32_t pending;
134 1.1.2.1 matt
135 1.1.2.2 matt v = GPIO_READ(gpio, GPIO_ISR);
136 1.1.2.2 matt pending = (v & gpio->gpio_enable_mask);
137 1.1.2.1 matt if (pending == 0)
138 1.1.2.1 matt return 0;
139 1.1.2.1 matt
140 1.1.2.1 matt /*
141 1.1.2.1 matt * Disable the pending interrupts.
142 1.1.2.1 matt */
143 1.1.2.2 matt gpio->gpio_enable_mask &= ~pending;
144 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
145 1.1.2.1 matt
146 1.1.2.1 matt /*
147 1.1.2.1 matt * If any of the sources are edge triggered, ack them now so
148 1.1.2.1 matt * we won't lose them.
149 1.1.2.1 matt */
150 1.1.2.2 matt if (v & gpio->gpio_edge_mask)
151 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_ISR, v & gpio->gpio_edge_mask);
152 1.1.2.1 matt
153 1.1.2.1 matt /*
154 1.1.2.1 matt * Now find all the pending bits and mark them as pending.
155 1.1.2.1 matt */
156 1.1.2.1 matt do {
157 1.1.2.2 matt int irq;
158 1.1.2.2 matt KASSERT(pending != 0);
159 1.1.2.1 matt irq = 31 - __builtin_clz(pending);
160 1.1.2.2 matt pending &= ~__BIT(irq);
161 1.1.2.2 matt pic_mark_pending(&gpio->gpio_pic, irq);
162 1.1.2.1 matt } while (pending != 0);
163 1.1.2.1 matt
164 1.1.2.1 matt return 1;
165 1.1.2.1 matt }
166 1.1.2.1 matt
167 1.1.2.1 matt #define GPIO_TYPEMAP \
168 1.1.2.2 matt ((GPIO_ICR_LEVEL_LOW << (2*IST_LEVEL_LOW)) | \
169 1.1.2.2 matt (GPIO_ICR_LEVEL_HIGH << (2*IST_LEVEL_HIGH)) | \
170 1.1.2.2 matt (GPIO_ICR_EDGE_RISING << (2*IST_EDGE_RISING)) | \
171 1.1.2.2 matt (GPIO_ICR_EDGE_FALLING << (2*IST_EDGE_FALLING)))
172 1.1.2.1 matt
173 1.1.2.1 matt void
174 1.1.2.1 matt gpio_pic_establish_irq(struct pic_softc *pic, int irq, int ipl, int type)
175 1.1.2.1 matt {
176 1.1.2.2 matt struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
177 1.1.2.1 matt KASSERT(irq < 32);
178 1.1.2.2 matt uint32_t irq_mask = __BIT(irq);
179 1.1.2.2 matt uint32_t v;
180 1.1.2.2 matt unsigned int icr_shift, icr_reg;
181 1.1.2.2 matt unsigned int gtype;
182 1.1.2.1 matt
183 1.1.2.1 matt /*
184 1.1.2.1 matt * Make sure the irq isn't enabled and not asserting.
185 1.1.2.1 matt */
186 1.1.2.2 matt gpio->gpio_enable_mask &= ~irq_mask;
187 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
188 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
189 1.1.2.1 matt
190 1.1.2.1 matt /*
191 1.1.2.1 matt * Convert the type to a gpio type and figure out which bits in what
192 1.1.2.1 matt * register we have to tweak.
193 1.1.2.1 matt */
194 1.1.2.2 matt gtype = (GPIO_TYPEMAP >> (2 * type)) & 3;
195 1.1.2.1 matt icr_shift = (irq & 0x0f) << 1;
196 1.1.2.2 matt icr_reg = GPIO_ICR1 + ((irq & 0x10) >> 2);
197 1.1.2.1 matt
198 1.1.2.1 matt /*
199 1.1.2.1 matt * Set the interrupt type.
200 1.1.2.1 matt */
201 1.1.2.2 matt v = GPIO_READ(gpio, icr_reg);
202 1.1.2.1 matt v &= ~(3 << icr_shift);
203 1.1.2.1 matt v |= gtype << icr_shift;
204 1.1.2.2 matt GPIO_WRITE(gpio, icr_reg, v);
205 1.1.2.1 matt
206 1.1.2.1 matt /*
207 1.1.2.1 matt * Mark it as input.
208 1.1.2.1 matt */
209 1.1.2.2 matt v = GPIO_READ(gpio, GPIO_DIR);
210 1.1.2.1 matt v &= ~irq_mask;
211 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_DIR, v);
212 1.1.2.1 matt
213 1.1.2.1 matt /*
214 1.1.2.1 matt * Now record the type of interrupt.
215 1.1.2.1 matt */
216 1.1.2.2 matt if (gtype == GPIO_ICR_EDGE_RISING || gtype == GPIO_ICR_EDGE_FALLING) {
217 1.1.2.2 matt gpio->gpio_edge_mask |= irq_mask;
218 1.1.2.2 matt gpio->gpio_level_mask &= ~irq_mask;
219 1.1.2.1 matt } else {
220 1.1.2.2 matt gpio->gpio_edge_mask &= ~irq_mask;
221 1.1.2.2 matt gpio->gpio_level_mask |= irq_mask;
222 1.1.2.2 matt }
223 1.1.2.2 matt }
224 1.1.2.2 matt
225 1.1.2.2 matt static int gpio_match(device_t, cfdata_t, void *);
226 1.1.2.2 matt static void gpio_attach(device_t, device_t, void *);
227 1.1.2.2 matt
228 1.1.2.2 matt CFATTACH_DECL(imxgpio,
229 1.1.2.2 matt sizeof(struct gpio_softc),
230 1.1.2.2 matt gpio_match, gpio_attach,
231 1.1.2.2 matt NULL, NULL);
232 1.1.2.2 matt
233 1.1.2.2 matt #if NGPIO > 0
234 1.1.2.2 matt
235 1.1.2.2 matt static int
236 1.1.2.2 matt imxgpio_pin_read(void *arg, int pin)
237 1.1.2.2 matt {
238 1.1.2.2 matt struct gpio_softc * const gpio = arg;
239 1.1.2.2 matt
240 1.1.2.2 matt return (GPIO_READ(gpio, GPIO_DR) >> pin) & 1;
241 1.1.2.2 matt }
242 1.1.2.2 matt
243 1.1.2.2 matt static void
244 1.1.2.2 matt imxgpio_pin_write(void *arg, int pin, int value)
245 1.1.2.2 matt {
246 1.1.2.2 matt struct gpio_softc * const gpio = arg;
247 1.1.2.2 matt uint32_t mask = 1 << pin;
248 1.1.2.2 matt uint32_t old, new;
249 1.1.2.2 matt
250 1.1.2.2 matt old = GPIO_READ(gpio, GPIO_DR);
251 1.1.2.2 matt if (value)
252 1.1.2.2 matt new = old | mask;
253 1.1.2.2 matt else
254 1.1.2.2 matt new = old & ~mask;
255 1.1.2.2 matt
256 1.1.2.2 matt if (old != new)
257 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_DR, new);
258 1.1.2.2 matt }
259 1.1.2.2 matt
260 1.1.2.2 matt static void
261 1.1.2.2 matt imxgpio_pin_ctl(void *arg, int pin, int flags)
262 1.1.2.2 matt {
263 1.1.2.2 matt struct gpio_softc * const gpio = arg;
264 1.1.2.2 matt uint32_t mask = 1 << pin;
265 1.1.2.2 matt uint32_t old, new;
266 1.1.2.2 matt
267 1.1.2.2 matt old = GPIO_READ(gpio, GPIO_DIR);
268 1.1.2.2 matt new = old;
269 1.1.2.2 matt switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
270 1.1.2.2 matt case GPIO_PIN_INPUT: new |= mask; break;
271 1.1.2.2 matt case GPIO_PIN_OUTPUT: new &= ~mask; break;
272 1.1.2.2 matt default: return;
273 1.1.2.1 matt }
274 1.1.2.2 matt if (old != new)
275 1.1.2.2 matt GPIO_WRITE(gpio, GPIO_DIR, new);
276 1.1.2.1 matt }
277 1.1.2.1 matt
278 1.1.2.2 matt static void
279 1.1.2.2 matt gpio_defer(device_t self)
280 1.1.2.2 matt {
281 1.1.2.2 matt struct gpio_softc * const gpio = (void *) self;
282 1.1.2.2 matt struct gpio_chipset_tag * const gp = &gpio->gpio_chipset;
283 1.1.2.2 matt struct gpiobus_attach_args gba;
284 1.1.2.2 matt gpio_pin_t *pins;
285 1.1.2.2 matt uint32_t mask, dir, value;
286 1.1.2.2 matt int pin;
287 1.1.2.2 matt
288 1.1.2.2 matt gp->gp_cookie = gpio;
289 1.1.2.2 matt gp->gp_pin_read = imxgpio_pin_read;
290 1.1.2.2 matt gp->gp_pin_write = imxgpio_pin_write;
291 1.1.2.2 matt gp->gp_pin_ctl = imxgpio_pin_ctl;
292 1.1.2.2 matt
293 1.1.2.2 matt gba.gba_gc = gp;
294 1.1.2.2 matt gba.gba_pins = gpio->gpio_pins;
295 1.1.2.2 matt gba.gba_npins = __arraycount(gpio->gpio_pins);
296 1.1.2.2 matt
297 1.1.2.2 matt dir = GPIO_READ(gpio, GPIO_DIR);
298 1.1.2.2 matt value = GPIO_READ(gpio, GPIO_DR);
299 1.1.2.2 matt for (pin = 0, mask = 1, pins = gpio->gpio_pins;
300 1.1.2.2 matt pin < 32; pin++, mask <<= 1, pins++) {
301 1.1.2.2 matt pins->pin_num = pin;
302 1.1.2.2 matt if ((gpio->gpio_edge_mask|gpio->gpio_level_mask) & mask)
303 1.1.2.2 matt pins->pin_caps = GPIO_PIN_INPUT;
304 1.1.2.2 matt else
305 1.1.2.2 matt pins->pin_caps = GPIO_PIN_INPUT|GPIO_PIN_OUTPUT;
306 1.1.2.2 matt pins->pin_flags =
307 1.1.2.2 matt (dir & mask) ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
308 1.1.2.2 matt pins->pin_state =
309 1.1.2.2 matt (value & mask) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
310 1.1.2.2 matt }
311 1.1.2.2 matt
312 1.1.2.2 matt config_found_ia(self, "gpiobus", &gba, gpiobus_print);
313 1.1.2.2 matt }
314 1.1.2.2 matt #endif /* NGPIO > 0 */
315 1.1.2.1 matt
316 1.1.2.1 matt int
317 1.1.2.2 matt gpio_match(device_t parent, cfdata_t cfdata, void *aux)
318 1.1.2.1 matt {
319 1.1.2.2 matt struct ahb_attach_args *ahba = aux;
320 1.1.2.1 matt bus_space_handle_t memh;
321 1.1.2.2 matt bus_size_t size;
322 1.1.2.1 matt int error;
323 1.1.2.1 matt
324 1.1.2.2 matt if (ahba->ahba_addr != GPIO1_BASE
325 1.1.2.2 matt && ahba->ahba_addr != GPIO2_BASE
326 1.1.2.2 matt && ahba->ahba_addr != GPIO3_BASE)
327 1.1.2.1 matt return 0;
328 1.1.2.1 matt
329 1.1.2.2 matt size = (ahba->ahba_size == AHBCF_SIZE_DEFAULT) ? GPIO_SIZE : ahba->ahba_size;
330 1.1.2.1 matt
331 1.1.2.2 matt error = bus_space_map(ahba->ahba_memt, ahba->ahba_addr, size, 0, &memh);
332 1.1.2.1 matt if (error)
333 1.1.2.1 matt return 0;
334 1.1.2.1 matt
335 1.1.2.2 matt bus_space_unmap(ahba->ahba_memt, memh, size);
336 1.1.2.1 matt return 1;
337 1.1.2.1 matt }
338 1.1.2.1 matt
339 1.1.2.2 matt void
340 1.1.2.2 matt gpio_attach(device_t parent, device_t self, void *aux)
341 1.1.2.1 matt {
342 1.1.2.2 matt struct ahb_attach_args * const ahba = aux;
343 1.1.2.2 matt struct gpio_softc * const gpio = (void *) self;
344 1.1.2.2 matt bus_size_t size;
345 1.1.2.1 matt int error;
346 1.1.2.1 matt
347 1.1.2.2 matt size = (ahba->ahba_size == AHBCF_SIZE_DEFAULT) ? GPIO_SIZE : ahba->ahba_size;
348 1.1.2.2 matt
349 1.1.2.2 matt gpio->gpio_memt = ahba->ahba_memt;
350 1.1.2.2 matt error = bus_space_map(ahba->ahba_memt, ahba->ahba_addr, ahba->ahba_size,
351 1.1.2.2 matt 0, &gpio->gpio_memh);
352 1.1.2.1 matt
353 1.1.2.1 matt if (error) {
354 1.1.2.2 matt aprint_error(": failed to map register %#lx@%#lx: %d\n",
355 1.1.2.2 matt ahba->ahba_size, ahba->ahba_addr, error);
356 1.1.2.1 matt return;
357 1.1.2.1 matt }
358 1.1.2.1 matt
359 1.1.2.2 matt if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT) {
360 1.1.2.2 matt gpio->gpio_pic.pic_ops = &gpio_pic_ops;
361 1.1.2.2 matt strlcpy(gpio->gpio_pic.pic_name, self->dv_xname,
362 1.1.2.2 matt sizeof(gpio->gpio_pic.pic_name));
363 1.1.2.2 matt gpio->gpio_pic.pic_maxsources = 32;
364 1.1.2.2 matt pic_add(&gpio->gpio_pic, ahba->ahba_irqbase);
365 1.1.2.1 matt }
366 1.1.2.2 matt #if NGPIO > 0
367 1.1.2.2 matt config_interrupts(self, gpio_defer);
368 1.1.2.2 matt #endif
369 1.1.2.1 matt }
370