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