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