augpio.c revision 1.1 1 1.1 gdamore /* $NetBSD: augpio.c,v 1.1 2006/02/12 20:49:34 gdamore Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Itronix Inc.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Written by Garrett D'Amore for Itronix Inc.
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 Itronix Inc. 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 ITRONIX INC. ``AS IS'' AND
22 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
32 1.1 gdamore */
33 1.1 gdamore
34 1.1 gdamore #include <sys/cdefs.h>
35 1.1 gdamore __KERNEL_RCSID(0, "$NetBSD: augpio.c,v 1.1 2006/02/12 20:49:34 gdamore Exp $");
36 1.1 gdamore
37 1.1 gdamore #include <sys/types.h>
38 1.1 gdamore #include <sys/param.h>
39 1.1 gdamore #include <sys/systm.h>
40 1.1 gdamore #include <sys/errno.h>
41 1.1 gdamore #include <sys/device.h>
42 1.1 gdamore #include <sys/gpio.h>
43 1.1 gdamore #include <sys/kernel.h>
44 1.1 gdamore
45 1.1 gdamore #include <dev/gpio/gpiovar.h>
46 1.1 gdamore
47 1.1 gdamore #include <machine/bus.h>
48 1.1 gdamore #include <machine/cpu.h>
49 1.1 gdamore
50 1.1 gdamore #include <mips/alchemy/include/aubusvar.h>
51 1.1 gdamore #include <mips/alchemy/include/aureg.h>
52 1.1 gdamore #include <mips/alchemy/dev/augpioreg.h>
53 1.1 gdamore #include <mips/alchemy/dev/augpiovar.h>
54 1.1 gdamore
55 1.1 gdamore struct augpio_softc {
56 1.1 gdamore struct device sc_dev;
57 1.1 gdamore struct gpio_chipset_tag sc_gc;
58 1.1 gdamore gpio_pin_t sc_pins[AUGPIO_NPINS];
59 1.1 gdamore int sc_npins;
60 1.1 gdamore int sc_isgpio2;
61 1.1 gdamore };
62 1.1 gdamore
63 1.1 gdamore static int augpio_pin_read(void *, int);
64 1.1 gdamore static void augpio_pin_write(void *, int, int);
65 1.1 gdamore static void augpio_pin_ctl(void *, int, int);
66 1.1 gdamore static int augpio_match(struct device *, struct cfdata *, void *);
67 1.1 gdamore static void augpio_attach(struct device *, struct device *, void *);
68 1.1 gdamore
69 1.1 gdamore CFATTACH_DECL(augpio, sizeof(struct augpio_softc),
70 1.1 gdamore augpio_match, augpio_attach, NULL, NULL);
71 1.1 gdamore
72 1.1 gdamore #define GETREG(x) \
73 1.1 gdamore (*(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(x))
74 1.1 gdamore #define PUTREG(x, v) \
75 1.1 gdamore ((*(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(x)) = (v))
76 1.1 gdamore
77 1.1 gdamore static int augpio_found = 0;
78 1.1 gdamore
79 1.1 gdamore int
80 1.1 gdamore augpio_match(struct device *parent, struct cfdata *match, void *aux)
81 1.1 gdamore {
82 1.1 gdamore struct aubus_attach_args *aa = (struct aubus_attach_args *)aux;
83 1.1 gdamore
84 1.1 gdamore if (strcmp(aa->aa_name, "augpio") != 0)
85 1.1 gdamore return 0;
86 1.1 gdamore
87 1.1 gdamore if (augpio_found)
88 1.1 gdamore return 0;
89 1.1 gdamore
90 1.1 gdamore return 1;
91 1.1 gdamore }
92 1.1 gdamore
93 1.1 gdamore void
94 1.1 gdamore augpio_attach(struct device *parent, struct device *self, void *aux)
95 1.1 gdamore {
96 1.1 gdamore int pin;
97 1.1 gdamore
98 1.1 gdamore struct augpio_softc *sc = (struct augpio_softc *)self;
99 1.1 gdamore struct aubus_attach_args *aa = aux;
100 1.1 gdamore struct gpiobus_attach_args gba;
101 1.1 gdamore
102 1.1 gdamore sc->sc_npins = 0;
103 1.1 gdamore
104 1.1 gdamore printf(": Alchemy GPIO");
105 1.1 gdamore if (aa->aa_addrs[0] == SYS_BASE) {
106 1.1 gdamore for (pin = 0; pin < aa->aa_addrs[1]; pin++) {
107 1.1 gdamore
108 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_num = pin;
109 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_caps =
110 1.1 gdamore GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
111 1.1 gdamore GPIO_PIN_TRISTATE;
112 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_flags =
113 1.1 gdamore au_gpio_ctl(pin, 0);
114 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_state =
115 1.1 gdamore au_gpio_read(pin);
116 1.1 gdamore sc->sc_npins++;
117 1.1 gdamore }
118 1.1 gdamore sc->sc_isgpio2 = 0;
119 1.1 gdamore printf(", primary block");
120 1.1 gdamore
121 1.1 gdamore } else if (aa->aa_addrs[0] == GPIO2_BASE) {
122 1.1 gdamore
123 1.1 gdamore /* initialize GPIO2 block */
124 1.1 gdamore uint32_t val;
125 1.1 gdamore val = AUGPIO_GPIO2_ENABLE_MR | AUGPIO_GPIO2_ENABLE_CE;
126 1.1 gdamore PUTREG(GPIO2_BASE + AUGPIO_GPIO2_ENABLE, val);
127 1.1 gdamore delay(10);
128 1.1 gdamore val &= ~AUGPIO_GPIO2_ENABLE_MR;
129 1.1 gdamore PUTREG(GPIO2_BASE + AUGPIO_GPIO2_ENABLE, val);
130 1.1 gdamore
131 1.1 gdamore for (pin = 0; pin < aa->aa_addrs[1]; pin++) {
132 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_num = pin;
133 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_caps =
134 1.1 gdamore GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
135 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_flags =
136 1.1 gdamore au_gpio2_ctl(pin, 0);
137 1.1 gdamore sc->sc_pins[sc->sc_npins].pin_state =
138 1.1 gdamore au_gpio2_read(pin);
139 1.1 gdamore sc->sc_npins++;
140 1.1 gdamore }
141 1.1 gdamore
142 1.1 gdamore printf(", secondary block");
143 1.1 gdamore sc->sc_isgpio2 = 1;
144 1.1 gdamore
145 1.1 gdamore } else {
146 1.1 gdamore printf(", unidentified block");
147 1.1 gdamore }
148 1.1 gdamore printf("\n");
149 1.1 gdamore
150 1.1 gdamore sc->sc_gc.gp_cookie = sc;
151 1.1 gdamore sc->sc_gc.gp_pin_read = augpio_pin_read;
152 1.1 gdamore sc->sc_gc.gp_pin_write = augpio_pin_write;
153 1.1 gdamore sc->sc_gc.gp_pin_ctl = augpio_pin_ctl;
154 1.1 gdamore
155 1.1 gdamore gba.gba_gc = &sc->sc_gc;
156 1.1 gdamore gba.gba_pins = sc->sc_pins;
157 1.1 gdamore gba.gba_npins = sc->sc_npins;
158 1.1 gdamore
159 1.1 gdamore config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
160 1.1 gdamore }
161 1.1 gdamore
162 1.1 gdamore int
163 1.1 gdamore augpio_pin_read(void *arg, int pin)
164 1.1 gdamore {
165 1.1 gdamore struct augpio_softc *sc = arg;
166 1.1 gdamore
167 1.1 gdamore return (sc->sc_isgpio2 ? au_gpio2_read(pin) : au_gpio_read(pin));
168 1.1 gdamore }
169 1.1 gdamore
170 1.1 gdamore void
171 1.1 gdamore augpio_pin_write(void *arg, int pin, int value)
172 1.1 gdamore {
173 1.1 gdamore struct augpio_softc *sc = arg;
174 1.1 gdamore
175 1.1 gdamore if (sc->sc_isgpio2)
176 1.1 gdamore au_gpio2_write(pin, value);
177 1.1 gdamore else
178 1.1 gdamore au_gpio_write(pin, value);
179 1.1 gdamore }
180 1.1 gdamore
181 1.1 gdamore void
182 1.1 gdamore augpio_pin_ctl(void *arg, int pin, int flags)
183 1.1 gdamore {
184 1.1 gdamore struct augpio_softc *sc = arg;
185 1.1 gdamore
186 1.1 gdamore if (sc->sc_isgpio2)
187 1.1 gdamore (void) au_gpio2_ctl(pin, flags);
188 1.1 gdamore else
189 1.1 gdamore (void) au_gpio_ctl(pin, flags);
190 1.1 gdamore }
191 1.1 gdamore
192 1.1 gdamore int
193 1.1 gdamore au_gpio_read(int pin)
194 1.1 gdamore {
195 1.1 gdamore
196 1.1 gdamore pin = 1 << pin;
197 1.1 gdamore return ((GETREG(SYS_BASE + AUGPIO_SYS_PINSTATERD) & pin) ?
198 1.1 gdamore GPIO_PIN_HIGH : GPIO_PIN_LOW);
199 1.1 gdamore }
200 1.1 gdamore
201 1.1 gdamore void
202 1.1 gdamore au_gpio_write(int pin, int value)
203 1.1 gdamore {
204 1.1 gdamore
205 1.1 gdamore pin = 1 << pin;
206 1.1 gdamore if (value) {
207 1.1 gdamore PUTREG(SYS_BASE + AUGPIO_SYS_OUTPUTSET, pin);
208 1.1 gdamore } else {
209 1.1 gdamore PUTREG(SYS_BASE + AUGPIO_SYS_OUTPUTCLR, pin);
210 1.1 gdamore }
211 1.1 gdamore }
212 1.1 gdamore
213 1.1 gdamore int
214 1.1 gdamore au_gpio_ctl(int pin, int flags)
215 1.1 gdamore {
216 1.1 gdamore uint32_t tri, out;
217 1.1 gdamore int old;
218 1.1 gdamore
219 1.1 gdamore old = 0;
220 1.1 gdamore
221 1.1 gdamore pin = 1 << pin;
222 1.1 gdamore tri = GETREG(SYS_BASE + AUGPIO_SYS_TRIOUTRD);
223 1.1 gdamore out = GETREG(SYS_BASE + AUGPIO_SYS_OUTPUTRD);
224 1.1 gdamore old = 0;
225 1.1 gdamore
226 1.1 gdamore if (tri & pin) {
227 1.1 gdamore old |= GPIO_PIN_INPUT;
228 1.1 gdamore } else {
229 1.1 gdamore old |= GPIO_PIN_OUTPUT;
230 1.1 gdamore }
231 1.1 gdamore
232 1.1 gdamore if (flags & (GPIO_PIN_TRISTATE|GPIO_PIN_INPUT)) {
233 1.1 gdamore PUTREG(SYS_BASE + AUGPIO_SYS_TRIOUTCLR, pin);
234 1.1 gdamore } else if (flags & GPIO_PIN_OUTPUT) {
235 1.1 gdamore if (pin & out) {
236 1.1 gdamore PUTREG(SYS_BASE + AUGPIO_SYS_OUTPUTSET, pin);
237 1.1 gdamore } else {
238 1.1 gdamore PUTREG(SYS_BASE + AUGPIO_SYS_OUTPUTCLR, pin);
239 1.1 gdamore }
240 1.1 gdamore }
241 1.1 gdamore
242 1.1 gdamore return old;
243 1.1 gdamore }
244 1.1 gdamore
245 1.1 gdamore int
246 1.1 gdamore au_gpio2_read(int pin)
247 1.1 gdamore {
248 1.1 gdamore
249 1.1 gdamore pin = 1 << pin;
250 1.1 gdamore
251 1.1 gdamore return ((GETREG(GPIO2_BASE + AUGPIO_GPIO2_PINSTATE) & pin) ?
252 1.1 gdamore GPIO_PIN_HIGH : GPIO_PIN_LOW);
253 1.1 gdamore }
254 1.1 gdamore
255 1.1 gdamore void
256 1.1 gdamore au_gpio2_write(int pin, int value)
257 1.1 gdamore {
258 1.1 gdamore
259 1.1 gdamore pin = 1 << pin;
260 1.1 gdamore
261 1.1 gdamore if (value) {
262 1.1 gdamore PUTREG(GPIO2_BASE + AUGPIO_GPIO2_OUTPUT, (pin | (pin << 16)));
263 1.1 gdamore } else {
264 1.1 gdamore PUTREG(GPIO2_BASE + AUGPIO_GPIO2_OUTPUT, (pin << 16));
265 1.1 gdamore }
266 1.1 gdamore }
267 1.1 gdamore
268 1.1 gdamore int
269 1.1 gdamore au_gpio2_ctl(int pin, int flags)
270 1.1 gdamore {
271 1.1 gdamore uint32_t dir;
272 1.1 gdamore int old;
273 1.1 gdamore
274 1.1 gdamore pin = 1 << pin;
275 1.1 gdamore
276 1.1 gdamore dir = GETREG(GPIO2_BASE + AUGPIO_GPIO2_DIR);
277 1.1 gdamore old = dir;
278 1.1 gdamore
279 1.1 gdamore if (dir & pin) {
280 1.1 gdamore old |= GPIO_PIN_OUTPUT;
281 1.1 gdamore } else {
282 1.1 gdamore old |= GPIO_PIN_INPUT;
283 1.1 gdamore }
284 1.1 gdamore
285 1.1 gdamore if (flags & GPIO_PIN_INPUT) {
286 1.1 gdamore dir |= pin;
287 1.1 gdamore PUTREG(GPIO2_BASE + AUGPIO_GPIO2_DIR, pin);
288 1.1 gdamore } else if (flags & GPIO_PIN_OUTPUT) {
289 1.1 gdamore dir &= ~pin;
290 1.1 gdamore PUTREG(GPIO2_BASE + AUGPIO_GPIO2_DIR, pin);
291 1.1 gdamore }
292 1.1 gdamore
293 1.1 gdamore return old;
294 1.1 gdamore }
295