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