1 /* $NetBSD: gemini_gpio.c,v 1.1 2008/11/20 22:36:36 cliff Exp $ */
4 * $NetBSD: omap2_gpio.c,v 1.6 2008/11/19 06:26:27 matt Exp
8 * Copyright (c) 2007 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: gemini_gpio.c,v 1.1 2008/11/20 22:36:36 cliff Exp $");
42 #include "geminigmac.h"
43 #include "opt_gemini.h"
45 #include <sys/param.h>
46 #include <sys/evcnt.h>
47 #include <sys/atomic.h>
49 #include <uvm/uvm_extern.h>
51 #include <machine/intr.h>
54 #include <arm/armreg.h>
55 #include <arm/cpufunc.h>
57 #include <machine/bus.h>
59 #include <arm/gemini/gemini_reg.h>
60 #include <arm/gemini/gemini_obiovar.h>
61 #include <arm/gemini/gemini_gpiovar.h>
62 #include <arm/pic/picvar.h>
66 #include <dev/gpio/gpiovar.h>
69 static void gpio_pic_block_irqs(struct pic_softc
*, size_t, uint32_t);
70 static void gpio_pic_unblock_irqs(struct pic_softc
*, size_t, uint32_t);
71 static int gpio_pic_find_pending_irqs(struct pic_softc
*);
72 static void gpio_pic_establish_irq(struct pic_softc
*, struct intrsource
*);
74 const struct pic_ops gpio_pic_ops
= {
75 .pic_block_irqs
= gpio_pic_block_irqs
,
76 .pic_unblock_irqs
= gpio_pic_unblock_irqs
,
77 .pic_find_pending_irqs
= gpio_pic_find_pending_irqs
,
78 .pic_establish_irq
= gpio_pic_establish_irq
,
83 struct pic_softc gpio_pic
;
84 struct intrsource
*gpio_is
;
85 bus_space_tag_t gpio_memt
;
86 bus_space_handle_t gpio_memh
;
87 uint32_t gpio_enable_mask
;
88 uint32_t gpio_edge_mask
;
89 uint32_t gpio_edge_falling_mask
;
90 uint32_t gpio_edge_rising_mask
;
91 uint32_t gpio_level_mask
;
92 uint32_t gpio_level_hi_mask
;
93 uint32_t gpio_level_lo_mask
;
94 uint32_t gpio_inuse_mask
;
96 struct gpio_chipset_tag gpio_chipset
;
97 gpio_pin_t gpio_pins
[32];
101 #define PIC_TO_SOFTC(pic) \
102 ((struct gpio_softc *)((char *)(pic) - \
103 offsetof(struct gpio_softc, gpio_pic)))
105 #define GPIO_READ(gpio, reg) \
106 bus_space_read_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg))
107 #define GPIO_WRITE(gpio, reg, val) \
108 bus_space_write_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg), (val))
111 gpio_pic_unblock_irqs(struct pic_softc
*pic
, size_t irq_base
, uint32_t irq_mask
)
113 struct gpio_softc
* const gpio
= PIC_TO_SOFTC(pic
);
114 KASSERT(irq_base
== 0);
116 gpio
->gpio_enable_mask
|= irq_mask
;
118 * If this a level source, ack it now. If it's still asserted
121 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRENB
, gpio
->gpio_enable_mask
);
122 if (irq_mask
& gpio
->gpio_level_mask
)
123 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRCLR
,
124 irq_mask
& gpio
->gpio_level_mask
);
128 gpio_pic_block_irqs(struct pic_softc
*pic
, size_t irq_base
, uint32_t irq_mask
)
130 struct gpio_softc
* const gpio
= PIC_TO_SOFTC(pic
);
131 KASSERT(irq_base
== 0);
133 gpio
->gpio_enable_mask
&= ~irq_mask
;
134 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRENB
, ~irq_mask
);
136 * If any of the sources are edge triggered, ack them now so
137 * we won't lose them.
139 if (irq_mask
& gpio
->gpio_edge_mask
)
140 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRCLR
,
141 irq_mask
& gpio
->gpio_edge_mask
);
145 gpio_pic_find_pending_irqs(struct pic_softc
*pic
)
147 struct gpio_softc
* const gpio
= PIC_TO_SOFTC(pic
);
150 pending
= GPIO_READ(gpio
, GEMINI_GPIO_INTRMSKSTATE
);
151 KASSERT((pending
& ~gpio
->gpio_enable_mask
) == 0);
156 * Now find all the pending bits and mark them as pending.
158 (void) pic_mark_pending_sources(&gpio
->gpio_pic
, 0, pending
);
164 gpio_pic_establish_irq(struct pic_softc
*pic
, struct intrsource
*is
)
166 struct gpio_softc
* const gpio
= PIC_TO_SOFTC(pic
);
167 KASSERT(is
->is_irq
< 32);
168 uint32_t irq_mask
= __BIT(is
->is_irq
);
172 struct intrsource
*maybe_is
;
176 * Make sure the irq isn't enabled and not asserting.
178 gpio
->gpio_enable_mask
&= ~irq_mask
;
179 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRENB
, gpio
->gpio_enable_mask
);
180 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRCLR
, irq_mask
);
183 * Convert the type to a gpio type and figure out which bits in what
184 * register we have to tweak.
186 gpio
->gpio_edge_rising_mask
&= ~irq_mask
;
187 gpio
->gpio_edge_falling_mask
&= ~irq_mask
;
188 gpio
->gpio_level_hi_mask
&= ~irq_mask
;
189 gpio
->gpio_level_lo_mask
&= ~irq_mask
;
190 switch (is
->is_type
) {
191 case IST_LEVEL_LOW
: gpio
->gpio_level_lo_mask
|= irq_mask
; break;
192 case IST_LEVEL_HIGH
: gpio
->gpio_level_hi_mask
|= irq_mask
; break;
193 case IST_EDGE_FALLING
: gpio
->gpio_edge_falling_mask
|= irq_mask
; break;
194 case IST_EDGE_RISING
: gpio
->gpio_edge_rising_mask
|= irq_mask
; break;
196 gpio
->gpio_edge_rising_mask
|= irq_mask
;
197 gpio
->gpio_edge_falling_mask
|= irq_mask
;
200 panic("%s: unknown is_type %d\n", __FUNCTION__
, is
->is_type
);
202 gpio
->gpio_edge_mask
=
203 gpio
->gpio_edge_rising_mask
| gpio
->gpio_edge_falling_mask
;
204 gpio
->gpio_level_mask
=
205 gpio
->gpio_level_hi_mask
|gpio
->gpio_level_lo_mask
;
206 gpio
->gpio_inuse_mask
|= irq_mask
;
209 * Set the interrupt type.
211 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRTRIG
, gpio
->gpio_level_mask
);
212 GPIO_WRITE(gpio
, GEMINI_GPIO_INTREDGEBOTH
,
213 gpio
->gpio_edge_rising_mask
& gpio
->gpio_edge_falling_mask
);
214 GPIO_WRITE(gpio
, GEMINI_GPIO_INTRDIR
,
215 gpio
->gpio_edge_falling_mask
| gpio
->gpio_level_lo_mask
);
218 * Mark it as input by clearning bit(s) in PINDIR reg
220 v
= GPIO_READ(gpio
, GEMINI_GPIO_PINDIR
);
222 GPIO_WRITE(gpio
, GEMINI_GPIO_PINDIR
, v
);
224 for (i
= 0, maybe_is
= NULL
; i
< 32; i
++) {
225 if ((is
= pic
->pic_sources
[i
]) != NULL
) {
226 if (maybe_is
== NULL
|| is
->is_ipl
> maybe_is
->is_ipl
)
230 if (maybe_is
!= NULL
) {
233 is
->is_ipl
= maybe_is
->is_ipl
;
234 (*is
->is_pic
->pic_ops
->pic_establish_irq
)(is
->is_pic
, is
);
239 static int gpio_match(device_t
, cfdata_t
, void *);
240 static void gpio_attach(device_t
, device_t
, void *);
242 CFATTACH_DECL_NEW(geminigpio
,
243 sizeof(struct gpio_softc
),
244 gpio_match
, gpio_attach
,
247 #if NGPIO > 0 || NGEMINIGMAC > 0
250 geminigpio_pin_read(void *arg
, int pin
)
252 struct gpio_softc
* const gpio
= device_private(arg
);
254 return (GPIO_READ(gpio
, GEMINI_GPIO_DATAIN
) >> pin
) & 1;
258 geminigpio_pin_write(void *arg
, int pin
, int value
)
260 struct gpio_softc
* const gpio
= device_private(arg
);
261 uint32_t mask
= 1 << pin
;
264 GPIO_WRITE(gpio
, GEMINI_GPIO_DATASET
, mask
);
266 GPIO_WRITE(gpio
, GEMINI_GPIO_DATACLR
, mask
);
270 geminigpio_pin_ctl(void *arg
, int pin
, int flags
)
272 struct gpio_softc
* const gpio
= device_private(arg
);
273 uint32_t mask
= 1 << pin
;
276 old
= GPIO_READ(gpio
, GEMINI_GPIO_PINDIR
);
278 switch (flags
& (GPIO_PIN_INPUT
|GPIO_PIN_OUTPUT
)) {
279 case GPIO_PIN_INPUT
: new &= ~mask
; break;
280 case GPIO_PIN_OUTPUT
: new |= mask
; break;
284 GPIO_WRITE(gpio
, GEMINI_GPIO_PINDIR
, new);
288 gpio_defer(device_t self
)
290 struct gpio_softc
* const gpio
= device_private(self
);
291 struct gpio_chipset_tag
* const gp
= &gpio
->gpio_chipset
;
292 struct gpiobus_attach_args gba
;
294 uint32_t mask
, dir
, valueout
, valuein
;
297 gp
->gp_cookie
= gpio
->gpio_dev
;
298 gp
->gp_pin_read
= geminigpio_pin_read
;
299 gp
->gp_pin_write
= geminigpio_pin_write
;
300 gp
->gp_pin_ctl
= geminigpio_pin_ctl
;
303 gba
.gba_pins
= gpio
->gpio_pins
;
304 gba
.gba_npins
= __arraycount(gpio
->gpio_pins
);
306 dir
= GPIO_READ(gpio
, GEMINI_GPIO_PINDIR
);
307 valueout
= GPIO_READ(gpio
, GEMINI_GPIO_DATAOUT
);
308 valuein
= GPIO_READ(gpio
, GEMINI_GPIO_DATAIN
);
309 for (pin
= 0, mask
= 1, pins
= gpio
->gpio_pins
;
310 pin
< 32; pin
++, mask
<<= 1, pins
++) {
312 if (gpio
->gpio_inuse_mask
& mask
)
313 pins
->pin_caps
= GPIO_PIN_INPUT
;
315 pins
->pin_caps
= GPIO_PIN_INPUT
|GPIO_PIN_OUTPUT
;
317 (dir
& mask
) ? GPIO_PIN_OUTPUT
: GPIO_PIN_INPUT
;
319 (((dir
& mask
) ? valueout
: valuein
) & mask
)
324 config_found_ia(self
, "gpiobus", &gba
, gpiobus_print
);
326 #endif /* NGPIO > 0 */
329 gpio_match(device_t parent
, cfdata_t cfdata
, void *aux
)
331 struct obio_attach_args
*oa
= aux
;
333 if (oa
->obio_addr
== GEMINI_GPIO0_BASE
334 || oa
->obio_addr
== GEMINI_GPIO1_BASE
335 || oa
->obio_addr
== GEMINI_GPIO2_BASE
)
342 gpio_attach(device_t parent
, device_t self
, void *aux
)
344 struct obio_attach_args
* const oa
= aux
;
345 struct gpio_softc
* const gpio
= device_private(self
);
348 if (oa
->obio_intr
== OBIOCF_INTR_DEFAULT
)
349 panic("\n%s: no intr assigned", device_xname(self
));
351 if (oa
->obio_size
== OBIOCF_SIZE_DEFAULT
)
352 oa
->obio_size
= GEMINI_GPIO_SIZE
;
354 gpio
->gpio_dev
= self
;
355 gpio
->gpio_memt
= oa
->obio_iot
;
356 error
= bus_space_map(oa
->obio_iot
, oa
->obio_addr
, oa
->obio_size
,
357 0, &gpio
->gpio_memh
);
360 aprint_error(": failed to map register %#lx@%#lx: %d\n",
361 oa
->obio_size
, oa
->obio_addr
, error
);
365 if (oa
->obio_intrbase
!= OBIOCF_INTRBASE_DEFAULT
) {
366 gpio
->gpio_pic
.pic_ops
= &gpio_pic_ops
;
367 strlcpy(gpio
->gpio_pic
.pic_name
, device_xname(self
),
368 sizeof(gpio
->gpio_pic
.pic_name
));
369 gpio
->gpio_pic
.pic_maxsources
= 32;
370 pic_add(&gpio
->gpio_pic
, oa
->obio_intrbase
);
371 aprint_normal(": interrupts %d..%d",
372 oa
->obio_intrbase
, oa
->obio_intrbase
+ 31);
373 gpio
->gpio_is
= intr_establish(oa
->obio_intr
,
374 IPL_HIGH
, IST_LEVEL_HIGH
, pic_handle_intr
, &gpio
->gpio_pic
);
375 KASSERT(gpio
->gpio_is
!= NULL
);
376 aprint_normal(", intr %d", oa
->obio_intr
);
380 config_interrupts(self
, gpio_defer
);