rmixl_gpio.c revision 1.1.2.2 1 1.1.2.2 rmind /* $NetBSD: rmixl_gpio.c,v 1.1.2.2 2011/04/21 01:41:13 rmind Exp $ */
2 1.1.2.2 rmind /* $NetBSD: rmixl_gpio.c,v 1.1.2.2 2011/04/21 01:41:13 rmind Exp $ */
3 1.1.2.2 rmind /*-
4 1.1.2.2 rmind * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1.2.2 rmind * All rights reserved.
6 1.1.2.2 rmind *
7 1.1.2.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 rmind * by Matt Thomas
9 1.1.2.2 rmind *
10 1.1.2.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 rmind * modification, are permitted provided that the following conditions
12 1.1.2.2 rmind * are met:
13 1.1.2.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 rmind * documentation and/or other materials provided with the distribution.
18 1.1.2.2 rmind *
19 1.1.2.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 rmind */
31 1.1.2.2 rmind #include <sys/cdefs.h>
32 1.1.2.2 rmind __KERNEL_RCSID(0, "$NetBSD: rmixl_gpio.c,v 1.1.2.2 2011/04/21 01:41:13 rmind Exp $");
33 1.1.2.2 rmind
34 1.1.2.2 rmind #define _INTR_PRIVATE
35 1.1.2.2 rmind
36 1.1.2.2 rmind #include "locators.h"
37 1.1.2.2 rmind #include "gpio.h"
38 1.1.2.2 rmind
39 1.1.2.2 rmind #include <sys/param.h>
40 1.1.2.2 rmind #include <sys/evcnt.h>
41 1.1.2.2 rmind #include <sys/atomic.h>
42 1.1.2.2 rmind
43 1.1.2.2 rmind #include <uvm/uvm_extern.h>
44 1.1.2.2 rmind
45 1.1.2.2 rmind #include <machine/intr.h>
46 1.1.2.2 rmind
47 1.1.2.2 rmind #include <mips/cpu.h>
48 1.1.2.2 rmind
49 1.1.2.2 rmind #include <machine/bus.h>
50 1.1.2.2 rmind
51 1.1.2.2 rmind #include <mips/rmi/rmixlreg.h>
52 1.1.2.2 rmind #include <mips/rmi/rmixlvar.h>
53 1.1.2.2 rmind #include <mips/rmi/rmixl_obiovar.h>
54 1.1.2.2 rmind
55 1.1.2.2 rmind #if NGPIO > 0
56 1.1.2.2 rmind #include <sys/gpio.h>
57 1.1.2.2 rmind #include <dev/gpio/gpiovar.h>
58 1.1.2.2 rmind #endif
59 1.1.2.2 rmind
60 1.1.2.2 rmind struct gpio_softc {
61 1.1.2.2 rmind device_t gpio_dev;
62 1.1.2.2 rmind void *gpio_ih;
63 1.1.2.2 rmind bus_space_tag_t gpio_memt;
64 1.1.2.2 rmind bus_space_handle_t gpio_memh;
65 1.1.2.2 rmind uint32_t gpio_enable_mask;
66 1.1.2.2 rmind uint32_t gpio_edge_mask;
67 1.1.2.2 rmind uint32_t gpio_edge_falling_mask;
68 1.1.2.2 rmind uint32_t gpio_edge_rising_mask;
69 1.1.2.2 rmind uint32_t gpio_level_mask;
70 1.1.2.2 rmind uint32_t gpio_level_hi_mask;
71 1.1.2.2 rmind uint32_t gpio_level_lo_mask;
72 1.1.2.2 rmind uint32_t gpio_inuse_mask;
73 1.1.2.2 rmind #if NGPIO > 0
74 1.1.2.2 rmind struct gpio_chipset_tag gpio_chipset;
75 1.1.2.2 rmind gpio_pin_t gpio_pins[RMIXL_GPIO_NSIGNALS];
76 1.1.2.2 rmind #endif
77 1.1.2.2 rmind };
78 1.1.2.2 rmind
79 1.1.2.2 rmind #define GPIO_READ(gpio, reg) \
80 1.1.2.2 rmind bus_space_read_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg))
81 1.1.2.2 rmind #define GPIO_WRITE(gpio, reg, val) \
82 1.1.2.2 rmind bus_space_write_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg), (val))
83 1.1.2.2 rmind
84 1.1.2.2 rmind static int gpio_match(device_t, cfdata_t, void *);
85 1.1.2.2 rmind static void gpio_attach(device_t, device_t, void *);
86 1.1.2.2 rmind
87 1.1.2.2 rmind CFATTACH_DECL_NEW(rmixl_gpio,
88 1.1.2.2 rmind sizeof(struct gpio_softc),
89 1.1.2.2 rmind gpio_match, gpio_attach,
90 1.1.2.2 rmind NULL, NULL);
91 1.1.2.2 rmind
92 1.1.2.2 rmind #if NGPIO > 0
93 1.1.2.2 rmind
94 1.1.2.2 rmind static int
95 1.1.2.2 rmind rmixl_gpio_pin_read(void *arg, int pin)
96 1.1.2.2 rmind {
97 1.1.2.2 rmind struct gpio_softc * const gpio = arg;
98 1.1.2.2 rmind
99 1.1.2.2 rmind KASSERT(((1 << pin) & RMIXL_GPIO_INPUT_MASK) != 0);
100 1.1.2.2 rmind return (GPIO_READ(gpio, RMIXL_GPIO_INPUT) >> pin) & 1;
101 1.1.2.2 rmind }
102 1.1.2.2 rmind
103 1.1.2.2 rmind static void
104 1.1.2.2 rmind rmixl_gpio_pin_write(void *arg, int pin, int value)
105 1.1.2.2 rmind {
106 1.1.2.2 rmind struct gpio_softc * const gpio = arg;
107 1.1.2.2 rmind uint32_t mask = 1 << pin;
108 1.1.2.2 rmind uint32_t old, new;
109 1.1.2.2 rmind
110 1.1.2.2 rmind KASSERT(((1 << pin) & RMIXL_GPIO_OUTPUT_MASK) != 0);
111 1.1.2.2 rmind
112 1.1.2.2 rmind old = GPIO_READ(gpio, RMIXL_GPIO_OUTPUT);
113 1.1.2.2 rmind if (value)
114 1.1.2.2 rmind new = old | mask;
115 1.1.2.2 rmind else
116 1.1.2.2 rmind new = old & ~mask;
117 1.1.2.2 rmind
118 1.1.2.2 rmind if (old != new)
119 1.1.2.2 rmind GPIO_WRITE(gpio, RMIXL_GPIO_OUTPUT, new);
120 1.1.2.2 rmind }
121 1.1.2.2 rmind
122 1.1.2.2 rmind static void
123 1.1.2.2 rmind rmixl_gpio_pin_ctl(void *arg, int pin, int flags)
124 1.1.2.2 rmind {
125 1.1.2.2 rmind struct gpio_softc * const gpio = arg;
126 1.1.2.2 rmind uint32_t mask = 1 << pin;
127 1.1.2.2 rmind uint32_t old, new;
128 1.1.2.2 rmind
129 1.1.2.2 rmind KASSERT((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT));
130 1.1.2.2 rmind old = GPIO_READ(gpio, RMIXL_GPIO_IO_DIR);
131 1.1.2.2 rmind new = old;
132 1.1.2.2 rmind switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
133 1.1.2.2 rmind case GPIO_PIN_INPUT: new &= ~mask; break;
134 1.1.2.2 rmind case GPIO_PIN_OUTPUT: new |= mask; break;
135 1.1.2.2 rmind default: return;
136 1.1.2.2 rmind }
137 1.1.2.2 rmind if (old != new)
138 1.1.2.2 rmind GPIO_WRITE(gpio, RMIXL_GPIO_IO_DIR, new);
139 1.1.2.2 rmind }
140 1.1.2.2 rmind
141 1.1.2.2 rmind static void
142 1.1.2.2 rmind gpio_defer(device_t self)
143 1.1.2.2 rmind {
144 1.1.2.2 rmind struct gpio_softc * const gpio = device_private(self);
145 1.1.2.2 rmind struct gpio_chipset_tag * const gp = &gpio->gpio_chipset;
146 1.1.2.2 rmind struct gpiobus_attach_args gba;
147 1.1.2.2 rmind gpio_pin_t *pins;
148 1.1.2.2 rmind uint32_t mask, dir, valueout, valuein;
149 1.1.2.2 rmind int pin;
150 1.1.2.2 rmind
151 1.1.2.2 rmind gp->gp_cookie = gpio;
152 1.1.2.2 rmind gp->gp_pin_read = rmixl_gpio_pin_read;
153 1.1.2.2 rmind gp->gp_pin_write = rmixl_gpio_pin_write;
154 1.1.2.2 rmind gp->gp_pin_ctl = rmixl_gpio_pin_ctl;
155 1.1.2.2 rmind
156 1.1.2.2 rmind gba.gba_gc = gp;
157 1.1.2.2 rmind gba.gba_pins = gpio->gpio_pins;
158 1.1.2.2 rmind gba.gba_npins = __arraycount(gpio->gpio_pins);
159 1.1.2.2 rmind
160 1.1.2.2 rmind dir = GPIO_READ(gpio, RMIXL_GPIO_IO_DIR);
161 1.1.2.2 rmind valueout = GPIO_READ(gpio, RMIXL_GPIO_OUTPUT);
162 1.1.2.2 rmind valuein = GPIO_READ(gpio, RMIXL_GPIO_INPUT);
163 1.1.2.2 rmind for (pin = 0, mask = 1, pins = gpio->gpio_pins;
164 1.1.2.2 rmind pin < RMIXL_GPIO_NSIGNALS; pin++, mask <<= 1, pins++) {
165 1.1.2.2 rmind pins->pin_num = pin;
166 1.1.2.2 rmind if (gpio->gpio_inuse_mask & mask) {
167 1.1.2.2 rmind if ((gpio->gpio_inuse_mask & RMIXL_GPIO_INPUT_MASK))
168 1.1.2.2 rmind pins->pin_caps = GPIO_PIN_INPUT;
169 1.1.2.2 rmind } else {
170 1.1.2.2 rmind int caps = 0;
171 1.1.2.2 rmind if ((gpio->gpio_inuse_mask & RMIXL_GPIO_INPUT_MASK))
172 1.1.2.2 rmind caps |= GPIO_PIN_INPUT;
173 1.1.2.2 rmind if ((gpio->gpio_inuse_mask & RMIXL_GPIO_OUTPUT_MASK))
174 1.1.2.2 rmind caps |= GPIO_PIN_INPUT;
175 1.1.2.2 rmind pins->pin_caps = caps;
176 1.1.2.2 rmind }
177 1.1.2.2 rmind pins->pin_flags =
178 1.1.2.2 rmind (dir & mask) ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
179 1.1.2.2 rmind pins->pin_state =
180 1.1.2.2 rmind (((dir & mask) ? valueout : valuein) & mask)
181 1.1.2.2 rmind ? GPIO_PIN_LOW
182 1.1.2.2 rmind : GPIO_PIN_HIGH;
183 1.1.2.2 rmind }
184 1.1.2.2 rmind
185 1.1.2.2 rmind config_found_ia(self, "gpiobus", &gba, gpiobus_print);
186 1.1.2.2 rmind }
187 1.1.2.2 rmind #endif /* NGPIO > 0 */
188 1.1.2.2 rmind
189 1.1.2.2 rmind int
190 1.1.2.2 rmind gpio_match(device_t parent, cfdata_t cfdata, void *aux)
191 1.1.2.2 rmind {
192 1.1.2.2 rmind struct obio_attach_args *oa = aux;
193 1.1.2.2 rmind
194 1.1.2.2 rmind if (oa->obio_addr == RMIXL_IO_DEV_GPIO) /* XXX XLS */
195 1.1.2.2 rmind return 1;
196 1.1.2.2 rmind
197 1.1.2.2 rmind return 0;
198 1.1.2.2 rmind }
199 1.1.2.2 rmind
200 1.1.2.2 rmind void
201 1.1.2.2 rmind gpio_attach(device_t parent, device_t self, void *aux)
202 1.1.2.2 rmind {
203 1.1.2.2 rmind struct obio_attach_args * const oa = aux;
204 1.1.2.2 rmind struct gpio_softc * const gpio = device_private(self);
205 1.1.2.2 rmind int error;
206 1.1.2.2 rmind
207 1.1.2.2 rmind gpio->gpio_dev = self;
208 1.1.2.2 rmind
209 1.1.2.2 rmind if (oa->obio_intr == OBIOCF_INTR_DEFAULT)
210 1.1.2.2 rmind panic("\n%s: no intr assigned", device_xname(self));
211 1.1.2.2 rmind
212 1.1.2.2 rmind if (oa->obio_size == OBIOCF_SIZE_DEFAULT)
213 1.1.2.2 rmind oa->obio_size = 0x1000;
214 1.1.2.2 rmind
215 1.1.2.2 rmind gpio->gpio_memt = oa->obio_eb_bst;
216 1.1.2.2 rmind error = bus_space_map(oa->obio_eb_bst, oa->obio_addr, oa->obio_size,
217 1.1.2.2 rmind 0, &gpio->gpio_memh);
218 1.1.2.2 rmind
219 1.1.2.2 rmind if (error) {
220 1.1.2.2 rmind aprint_error(": failed to map register %#"
221 1.1.2.2 rmind PRIxBUSSIZE "@%#" PRIxBUSADDR ": %d\n",
222 1.1.2.2 rmind oa->obio_size, oa->obio_addr, error);
223 1.1.2.2 rmind return;
224 1.1.2.2 rmind }
225 1.1.2.2 rmind
226 1.1.2.2 rmind #ifdef NOTYET
227 1.1.2.2 rmind if (oa->obio_intr != OBIOCF_INTR_DEFAULT) {
228 1.1.2.2 rmind gpio->gpio_ih = intr_establish(oa->obio_intr,
229 1.1.2.2 rmind IPL_HIGH, IST_LEVEL, pic_handle_intr, &gpio->gpio_pic);
230 1.1.2.2 rmind KASSERT(gpio->gpio_ih != NULL);
231 1.1.2.2 rmind aprint_normal(", intr %d", oa->obio_intr);
232 1.1.2.2 rmind }
233 1.1.2.2 rmind #endif
234 1.1.2.2 rmind aprint_normal("\n");
235 1.1.2.2 rmind #if NGPIO > 0
236 1.1.2.2 rmind config_interrupts(self, gpio_defer);
237 1.1.2.2 rmind #endif
238 1.1.2.2 rmind }
239