argpio.c revision 1.3 1 1.3 gdamore /* $NetBSD: argpio.c,v 1.3 2006/09/04 05:17:26 gdamore Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Garrett D'Amore
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Written by Garrett D'Amore.
8 1.1 gdamore *
9 1.1 gdamore * Redistribution and use in source and binary forms, with or without
10 1.1 gdamore * modification, are permitted provided that the following conditions
11 1.1 gdamore * are met:
12 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
13 1.1 gdamore * notice, this list of conditions and the following disclaimer.
14 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
16 1.1 gdamore * documentation and/or other materials provided with the distribution.
17 1.1 gdamore * 3. The name of the author may not be used to endorse
18 1.1 gdamore * or promote products derived from this software without specific
19 1.1 gdamore * prior written permission.
20 1.1 gdamore *
21 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 gdamore * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 gdamore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.1 gdamore * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 gdamore * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 gdamore * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.1 gdamore * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 1.1 gdamore * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 gdamore */
33 1.1 gdamore
34 1.1 gdamore #include <sys/cdefs.h>
35 1.3 gdamore __KERNEL_RCSID(0, "$NetBSD: argpio.c,v 1.3 2006/09/04 05:17:26 gdamore Exp $");
36 1.1 gdamore
37 1.1 gdamore #include <sys/param.h>
38 1.1 gdamore #include <sys/conf.h>
39 1.1 gdamore #include <sys/kernel.h>
40 1.1 gdamore #include <sys/types.h>
41 1.1 gdamore #include <sys/device.h>
42 1.1 gdamore #include <sys/gpio.h>
43 1.1 gdamore
44 1.1 gdamore #include <machine/bus.h>
45 1.1 gdamore #include <machine/intr.h>
46 1.1 gdamore
47 1.1 gdamore #include <mips/atheros/include/arbusvar.h>
48 1.1 gdamore
49 1.1 gdamore #include <dev/gpio/gpiovar.h>
50 1.1 gdamore #include <dev/sysmon/sysmonvar.h>
51 1.1 gdamore #include <dev/sysmon/sysmon_taskq.h>
52 1.1 gdamore
53 1.1 gdamore #include <mips/atheros/dev/argpioreg.h>
54 1.1 gdamore
55 1.1 gdamore /*
56 1.1 gdamore * General Plan:
57 1.1 gdamore *
58 1.1 gdamore * Register GPIOs for all pins that are _not_ associated with the reset
59 1.1 gdamore * pin. (Possibly also not the sytem LED.)
60 1.1 gdamore */
61 1.1 gdamore
62 1.1 gdamore struct argpio_softc {
63 1.1 gdamore struct device sc_dev;
64 1.1 gdamore struct gpio_chipset_tag sc_gc;
65 1.1 gdamore gpio_pin_t sc_pins[ARGPIO_NPINS];
66 1.1 gdamore int sc_npins;
67 1.1 gdamore bus_space_tag_t sc_st;
68 1.1 gdamore bus_space_handle_t sc_sh;
69 1.1 gdamore bus_size_t sc_size;
70 1.1 gdamore int sc_caps;
71 1.1 gdamore struct sysmon_pswitch sc_resetbtn;
72 1.1 gdamore void *sc_ih;
73 1.1 gdamore int sc_rstpin;
74 1.3 gdamore int sc_ledpin;
75 1.1 gdamore };
76 1.1 gdamore
77 1.1 gdamore static int argpio_match(struct device *, struct cfdata *, void *);
78 1.1 gdamore static void argpio_attach(struct device *, struct device *, void *);
79 1.1 gdamore static int argpio_intr(void *);
80 1.1 gdamore static void argpio_reset_pressed(void *);
81 1.1 gdamore static void argpio_ctl(void *, int, int);
82 1.1 gdamore static void argpio_write(void *, int, int);
83 1.1 gdamore static int argpio_read(void *, int);
84 1.1 gdamore
85 1.1 gdamore CFATTACH_DECL(argpio, sizeof (struct argpio_softc), argpio_match,
86 1.1 gdamore argpio_attach, NULL, NULL);
87 1.1 gdamore
88 1.1 gdamore #define INPUT(pin) (1 << (pin)) /* input bit */
89 1.1 gdamore #define INTR(pin) (1 << ((pin) + 8)) /* interrupt bit */
90 1.1 gdamore #define SERIAL(pin) (1 << ((pin) + 16)) /* serial mux bit */
91 1.1 gdamore
92 1.1 gdamore #define GETREG(sc, o) bus_space_read_4(sc->sc_st, sc->sc_sh, o)
93 1.1 gdamore #define PUTREG(sc, o, v) bus_space_write_4(sc->sc_st, sc->sc_sh, o, v)
94 1.1 gdamore #define FLUSH(sc) bus_space_barrier(sc->sc_st, sc->sc_sh, \
95 1.1 gdamore 0, 12, BUS_SPACE_BARRIER_SYNC)
96 1.1 gdamore
97 1.1 gdamore int
98 1.1 gdamore argpio_match(struct device *parent, struct cfdata *match, void *aux)
99 1.1 gdamore {
100 1.1 gdamore struct arbus_attach_args *aa = aux;
101 1.1 gdamore
102 1.1 gdamore return ((strcmp(aa->aa_name, "argpio") == 0) ? 1 : 0);
103 1.1 gdamore }
104 1.1 gdamore
105 1.1 gdamore void
106 1.1 gdamore argpio_attach(struct device *parent, struct device *self, void *aux)
107 1.1 gdamore {
108 1.1 gdamore struct argpio_softc *sc = (struct argpio_softc *)self;
109 1.1 gdamore struct arbus_attach_args *aa = aux;
110 1.1 gdamore struct gpiobus_attach_args gba;
111 1.3 gdamore prop_number_t pn;
112 1.3 gdamore int i;
113 1.1 gdamore uint32_t reg;
114 1.1 gdamore
115 1.1 gdamore sc->sc_st = aa->aa_bst;
116 1.1 gdamore sc->sc_npins = ARGPIO_NPINS;
117 1.1 gdamore sc->sc_size = aa->aa_size;
118 1.3 gdamore sc->sc_ledpin = -1;
119 1.3 gdamore sc->sc_rstpin = -1;
120 1.1 gdamore
121 1.1 gdamore if (bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
122 1.1 gdamore &sc->sc_sh) != 0) {
123 1.1 gdamore printf(": unable to map registers!\n");
124 1.1 gdamore return;
125 1.1 gdamore }
126 1.1 gdamore
127 1.1 gdamore sc->sc_gc.gp_cookie = sc;
128 1.1 gdamore sc->sc_gc.gp_pin_read = argpio_read;
129 1.1 gdamore sc->sc_gc.gp_pin_write = argpio_write;
130 1.1 gdamore sc->sc_gc.gp_pin_ctl = argpio_ctl;
131 1.1 gdamore
132 1.1 gdamore aprint_normal(": Atheros AR531X GPIO");
133 1.3 gdamore pn = prop_dictionary_get(device_properties(&sc->sc_dev), "reset-pin");
134 1.3 gdamore if (pn != NULL) {
135 1.3 gdamore KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
136 1.3 gdamore sc->sc_rstpin = (int)prop_number_integer_value(pn);
137 1.3 gdamore aprint_normal(", reset button pin %d", sc->sc_rstpin);
138 1.1 gdamore }
139 1.3 gdamore pn = prop_dictionary_get(device_properties(&sc->sc_dev), "sysled-pin");
140 1.3 gdamore if (pn != NULL) {
141 1.3 gdamore KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
142 1.3 gdamore sc->sc_ledpin = (int)prop_number_integer_value(pn);
143 1.3 gdamore aprint_normal(", system led pin %d", sc->sc_ledpin);
144 1.1 gdamore }
145 1.3 gdamore
146 1.1 gdamore printf("\n");
147 1.1 gdamore
148 1.3 gdamore if (sc->sc_ledpin) {
149 1.3 gdamore sc->sc_ih = arbus_intr_establish(aa->aa_cirq, aa->aa_mirq,
150 1.3 gdamore argpio_intr, sc);
151 1.1 gdamore if (sc->sc_ih == NULL) {
152 1.1 gdamore aprint_error("%s: couldn't establish interrupt\n",
153 1.1 gdamore sc->sc_dev.dv_xname);
154 1.1 gdamore }
155 1.1 gdamore }
156 1.1 gdamore
157 1.1 gdamore if (sc->sc_ih) {
158 1.1 gdamore sysmon_task_queue_init();
159 1.1 gdamore
160 1.1 gdamore sc->sc_resetbtn.smpsw_name = sc->sc_dev.dv_xname;
161 1.1 gdamore sc->sc_resetbtn.smpsw_type = PSWITCH_TYPE_RESET;
162 1.1 gdamore if (sysmon_pswitch_register(&sc->sc_resetbtn) != 0)
163 1.1 gdamore printf("%s: unable to register reset button\n",
164 1.1 gdamore sc->sc_dev.dv_xname);
165 1.1 gdamore }
166 1.1 gdamore
167 1.1 gdamore reg = GETREG(sc, GPIO_CR);
168 1.1 gdamore
169 1.1 gdamore for (i = 0; i < sc->sc_npins; i++) {
170 1.1 gdamore gpio_pin_t *pp;
171 1.1 gdamore
172 1.1 gdamore pp = &sc->sc_pins[i];
173 1.1 gdamore
174 1.3 gdamore if (i == sc->sc_rstpin) {
175 1.1 gdamore /* configure as interrupt for reset */
176 1.1 gdamore pp->pin_caps = GPIO_PIN_INPUT;
177 1.1 gdamore reg &= ~SERIAL(i);
178 1.1 gdamore reg |= INPUT(i);
179 1.1 gdamore /* only if we were able to set up the handler, tho' */
180 1.1 gdamore if (sc->sc_ih != NULL)
181 1.1 gdamore reg |= INTR(i);
182 1.1 gdamore
183 1.3 gdamore } else if (i == sc->sc_ledpin) {
184 1.1 gdamore /* configure as output for LED */
185 1.1 gdamore pp->pin_caps = GPIO_PIN_OUTPUT;
186 1.1 gdamore reg &= ~SERIAL(i);
187 1.1 gdamore reg &= ~INPUT(i);
188 1.1 gdamore reg &= ~INTR(i);
189 1.1 gdamore
190 1.1 gdamore } else {
191 1.1 gdamore if (reg & SERIAL(i)) {
192 1.1 gdamore /* pin multiplexed with serial bit */
193 1.1 gdamore pp->pin_caps = 0;
194 1.1 gdamore } else {
195 1.1 gdamore pp->pin_caps = GPIO_PIN_INPUT |
196 1.1 gdamore GPIO_PIN_OUTPUT;
197 1.1 gdamore }
198 1.1 gdamore }
199 1.1 gdamore }
200 1.1 gdamore
201 1.1 gdamore PUTREG(sc, GPIO_CR, reg);
202 1.1 gdamore FLUSH(sc);
203 1.1 gdamore
204 1.1 gdamore gba.gba_gc = &sc->sc_gc;
205 1.1 gdamore gba.gba_pins = sc->sc_pins;
206 1.1 gdamore gba.gba_npins = sc->sc_npins;
207 1.1 gdamore config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
208 1.1 gdamore }
209 1.1 gdamore
210 1.1 gdamore void
211 1.1 gdamore argpio_ctl(void *arg, int pin, int flags)
212 1.1 gdamore {
213 1.1 gdamore struct argpio_softc *sc = arg;
214 1.1 gdamore uint32_t reg;
215 1.1 gdamore
216 1.1 gdamore reg = GETREG(sc, GPIO_CR);
217 1.1 gdamore if (reg & (SERIAL(pin) | INTR(pin))) {
218 1.1 gdamore printf("pin %d cannot be changed!\n", pin);
219 1.1 gdamore /* don't allow changes to these bits */
220 1.1 gdamore return;
221 1.1 gdamore }
222 1.1 gdamore if (flags & GPIO_PIN_INPUT) {
223 1.1 gdamore reg |= INPUT(pin);
224 1.1 gdamore } else {
225 1.1 gdamore reg &= ~INPUT(pin);
226 1.1 gdamore }
227 1.1 gdamore
228 1.1 gdamore PUTREG(sc, GPIO_CR, reg);
229 1.1 gdamore FLUSH(sc);
230 1.1 gdamore }
231 1.1 gdamore
232 1.1 gdamore void
233 1.1 gdamore argpio_write(void *arg, int pin, int value)
234 1.1 gdamore {
235 1.1 gdamore struct argpio_softc *sc = arg;
236 1.1 gdamore uint32_t reg;
237 1.1 gdamore
238 1.1 gdamore reg = GETREG(sc, GPIO_DO);
239 1.1 gdamore if (value)
240 1.1 gdamore reg &= ~(1 << pin);
241 1.1 gdamore else
242 1.1 gdamore reg |= (1 << pin);
243 1.1 gdamore PUTREG(sc, GPIO_DO, reg);
244 1.1 gdamore FLUSH(sc);
245 1.1 gdamore }
246 1.1 gdamore
247 1.1 gdamore int
248 1.1 gdamore argpio_read(void *arg, int pin)
249 1.1 gdamore {
250 1.1 gdamore struct argpio_softc *sc = arg;
251 1.1 gdamore
252 1.1 gdamore return ((GETREG(sc, GPIO_DI) & (1 << pin)) ?
253 1.1 gdamore GPIO_PIN_HIGH : GPIO_PIN_LOW);
254 1.1 gdamore }
255 1.1 gdamore
256 1.1 gdamore void
257 1.1 gdamore argpio_reset_pressed(void *arg)
258 1.1 gdamore {
259 1.1 gdamore struct argpio_softc *sc = arg;
260 1.1 gdamore int x;
261 1.1 gdamore
262 1.1 gdamore sysmon_pswitch_event(&sc->sc_resetbtn, PSWITCH_EVENT_PRESSED);
263 1.1 gdamore
264 1.1 gdamore /* reenable the interrupt */
265 1.1 gdamore x = splhigh();
266 1.1 gdamore PUTREG(sc, GPIO_CR,
267 1.1 gdamore GETREG(sc, GPIO_CR) | INTR(sc->sc_rstpin));
268 1.1 gdamore splx(x);
269 1.1 gdamore }
270 1.1 gdamore
271 1.1 gdamore int
272 1.1 gdamore argpio_intr(void *arg)
273 1.1 gdamore {
274 1.1 gdamore struct argpio_softc *sc = arg;
275 1.1 gdamore
276 1.1 gdamore if (sc->sc_rstpin < 0)
277 1.1 gdamore return 0;
278 1.1 gdamore
279 1.1 gdamore /* this is an edge triggered interrupt, so disable it for now */
280 1.1 gdamore PUTREG(sc, GPIO_CR, GETREG(sc, GPIO_CR) & ~INTR(sc->sc_rstpin));
281 1.1 gdamore
282 1.1 gdamore /* no other interrupt on this, so we have to claim it */
283 1.1 gdamore sysmon_task_queue_sched(0, argpio_reset_pressed, sc);
284 1.1 gdamore
285 1.1 gdamore return 1;
286 1.1 gdamore }
287