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