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