gpio_opb.c revision 1.6.70.1 1 1.6.70.1 rmind /* $NetBSD: gpio_opb.c,v 1.6.70.1 2010/05/30 05:17:02 rmind Exp $ */
2 1.1 shige
3 1.1 shige /*
4 1.1 shige * Copyright (c) 2004 Shigeyuki Fukushima.
5 1.1 shige * All rights reserved.
6 1.1 shige *
7 1.1 shige * Redistribution and use in source and binary forms, with or without
8 1.1 shige * modification, are permitted provided that the following conditions
9 1.1 shige * are met:
10 1.1 shige * 1. Redistributions of source code must retain the above copyright
11 1.1 shige * notice, this list of conditions and the following disclaimer.
12 1.1 shige * 2. Redistributions in binary form must reproduce the above
13 1.1 shige * copyright notice, this list of conditions and the following
14 1.1 shige * disclaimer in the documentation and/or other materials provided
15 1.1 shige * with the distribution.
16 1.1 shige * 3. The name of the author may not be used to endorse or promote
17 1.1 shige * products derived from this software without specific prior
18 1.1 shige * written permission.
19 1.1 shige *
20 1.1 shige * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 1.1 shige * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 1.1 shige * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 shige * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 1.1 shige * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 shige * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 1.1 shige * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 shige * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 1.1 shige * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 1.1 shige * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 1.1 shige * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 shige */
32 1.1 shige
33 1.1 shige #include "locators.h"
34 1.1 shige
35 1.1 shige #include <sys/param.h>
36 1.1 shige #include <sys/device.h>
37 1.1 shige #include <sys/systm.h>
38 1.1 shige
39 1.1 shige #include <machine/pio.h>
40 1.1 shige
41 1.5 shige #include <sys/gpio.h>
42 1.5 shige #include <dev/gpio/gpiovar.h>
43 1.5 shige
44 1.1 shige #include <powerpc/ibm4xx/dev/opbvar.h>
45 1.1 shige #include <powerpc/ibm4xx/dev/gpioreg.h>
46 1.1 shige
47 1.5 shige struct gpio_opb_softc {
48 1.1 shige struct device sc_dev; /* device generic */
49 1.5 shige /* GPIO interface */
50 1.5 shige bus_space_tag_t sc_gpio_iot;
51 1.5 shige bus_space_handle_t sc_gpio_ioh;
52 1.5 shige struct gpio_chipset_tag sc_gpio_gc;
53 1.5 shige gpio_pin_t sc_gpio_pins[GPIO_NPINS];
54 1.1 shige };
55 1.1 shige
56 1.5 shige static int gpio_opb_match(struct device *, struct cfdata *, void *);
57 1.5 shige static void gpio_opb_attach(struct device *, struct device *, void *);
58 1.1 shige
59 1.5 shige CFATTACH_DECL(opbgpio, sizeof(struct gpio_opb_softc),
60 1.5 shige gpio_opb_match, gpio_opb_attach, NULL, NULL);
61 1.1 shige
62 1.5 shige static int gpio_opb_pin_read(void *, int);
63 1.5 shige static void gpio_opb_pin_write(void *, int, int);
64 1.5 shige static void gpio_opb_pin_ctl(void *, int, int);
65 1.1 shige
66 1.1 shige
67 1.1 shige static int
68 1.5 shige gpio_opb_match(struct device *parent, struct cfdata *cf, void *aux)
69 1.1 shige {
70 1.5 shige struct opb_attach_args *oaa = aux;
71 1.1 shige
72 1.1 shige if (strcmp(oaa->opb_name, cf->cf_name) != 0)
73 1.5 shige return 0;
74 1.1 shige
75 1.5 shige return 1;
76 1.1 shige }
77 1.1 shige
78 1.1 shige static void
79 1.5 shige gpio_opb_attach(struct device *parent, struct device *self, void *aux)
80 1.1 shige {
81 1.5 shige struct gpio_opb_softc *sc = (struct gpio_opb_softc *)self;
82 1.1 shige struct opb_attach_args *oaa = aux;
83 1.5 shige struct gpiobus_attach_args gba;
84 1.5 shige int i;
85 1.5 shige uint32_t reg1, reg2, reg3;
86 1.1 shige
87 1.1 shige aprint_naive(": GPIO controller\n");
88 1.1 shige aprint_normal(": On-Chip GPIO controller\n");
89 1.1 shige
90 1.5 shige /* Map GPIO I/O space */
91 1.5 shige sc->sc_gpio_iot = oaa->opb_bt;
92 1.5 shige bus_space_map(sc->sc_gpio_iot, oaa->opb_addr,
93 1.5 shige GPIO_NREG, 0, &sc->sc_gpio_ioh);
94 1.5 shige
95 1.5 shige /* Read current register status */
96 1.5 shige reg1 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_IR);
97 1.5 shige reg2 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_TCR);
98 1.5 shige reg3 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_ODR);
99 1.5 shige
100 1.6 simonb /* Initialize pins array */
101 1.5 shige for (i = 0 ; i < GPIO_NPINS ; i++) {
102 1.5 shige int p = i + 1;
103 1.5 shige sc->sc_gpio_pins[i].pin_num = i;
104 1.5 shige sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INOUT
105 1.5 shige | GPIO_PIN_OPENDRAIN
106 1.5 shige | GPIO_PIN_TRISTATE;
107 1.5 shige
108 1.5 shige /* current defaults */
109 1.5 shige sc->sc_gpio_pins[i].pin_flags =
110 1.5 shige ((reg3 >> GPIO_PIN_SHIFT(p)) & 0x01)
111 1.5 shige ? GPIO_PIN_OPENDRAIN
112 1.5 shige : (((reg2 >> GPIO_PIN_SHIFT(p)) & 0x01)
113 1.5 shige ? GPIO_PIN_INOUT
114 1.5 shige : GPIO_PIN_TRISTATE);
115 1.5 shige sc->sc_gpio_pins[i].pin_state =
116 1.5 shige ((reg1 >> GPIO_PIN_SHIFT(p)) & 0x01);
117 1.5 shige sc->sc_gpio_pins[i].pin_mapped = 0;
118 1.5 shige }
119 1.5 shige
120 1.5 shige /* Create controller tag */
121 1.5 shige sc->sc_gpio_gc.gp_cookie = sc;
122 1.5 shige sc->sc_gpio_gc.gp_pin_read = gpio_opb_pin_read;
123 1.5 shige sc->sc_gpio_gc.gp_pin_write = gpio_opb_pin_write;
124 1.5 shige sc->sc_gpio_gc.gp_pin_ctl = gpio_opb_pin_ctl;
125 1.5 shige
126 1.5 shige gba.gba_gc = &sc->sc_gpio_gc;
127 1.5 shige gba.gba_pins = sc->sc_gpio_pins;
128 1.5 shige gba.gba_npins = GPIO_NPINS;
129 1.5 shige
130 1.5 shige /* Attach GPIO framework */
131 1.5 shige (void) config_found(&sc->sc_dev, &gba, gpiobus_print);
132 1.1 shige }
133 1.1 shige
134 1.1 shige static int
135 1.5 shige gpio_opb_pin_read(void *arg, int pin)
136 1.5 shige {
137 1.5 shige struct gpio_opb_softc *sc = arg;
138 1.5 shige uint32_t data;
139 1.5 shige int p;
140 1.1 shige
141 1.5 shige p = pin % GPIO_NPINS;
142 1.5 shige p = p + 1;
143 1.1 shige
144 1.5 shige data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_IR);
145 1.1 shige
146 1.5 shige return (data >> GPIO_PIN_SHIFT(p)) & 0x01;
147 1.1 shige }
148 1.1 shige
149 1.1 shige static void
150 1.5 shige gpio_opb_pin_write(void *arg, int pin, int value)
151 1.1 shige {
152 1.5 shige struct gpio_opb_softc *sc = arg;
153 1.5 shige uint32_t data;
154 1.5 shige int p;
155 1.5 shige
156 1.5 shige p = pin % GPIO_NPINS;
157 1.5 shige p = p + 1;
158 1.5 shige
159 1.5 shige data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_OR);
160 1.5 shige if (value == 0) {
161 1.5 shige data &= ~(1 << GPIO_PIN_SHIFT(p));
162 1.5 shige } else if (value == 1) {
163 1.5 shige data |= (1 << GPIO_PIN_SHIFT(p));
164 1.5 shige }
165 1.1 shige
166 1.5 shige bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_OR, data);
167 1.1 shige }
168 1.1 shige
169 1.1 shige static void
170 1.5 shige gpio_opb_pin_ctl(void *arg, int pin, int flags)
171 1.1 shige {
172 1.5 shige struct gpio_opb_softc *sc = arg;
173 1.5 shige uint32_t data;
174 1.5 shige int p;
175 1.5 shige
176 1.5 shige p = pin % GPIO_NPINS;
177 1.5 shige p = p + 1;
178 1.5 shige
179 1.5 shige if (flags & GPIO_PIN_INOUT) {
180 1.5 shige /* GPIOn_ODR register bit is 0 */
181 1.5 shige data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
182 1.5 shige GPIO_ODR);
183 1.5 shige data &= ~(1 << GPIO_PIN_SHIFT(p));
184 1.5 shige bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
185 1.5 shige GPIO_ODR, data);
186 1.5 shige /* GPIOn_TCR register bit is 1 */
187 1.5 shige data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
188 1.5 shige GPIO_TCR);
189 1.5 shige data |= (1 << GPIO_PIN_SHIFT(p));
190 1.5 shige bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
191 1.5 shige GPIO_TCR, data);
192 1.5 shige }
193 1.5 shige
194 1.5 shige if (flags & GPIO_PIN_TRISTATE) {
195 1.5 shige /* GPIOn_ODR register bit is 0 */
196 1.5 shige data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
197 1.5 shige GPIO_ODR);
198 1.5 shige data &= ~(1 << GPIO_PIN_SHIFT(p));
199 1.5 shige bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
200 1.5 shige GPIO_ODR, data);
201 1.5 shige /* GPIOn_TCR register bit is 0 */
202 1.5 shige data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
203 1.5 shige GPIO_TCR);
204 1.5 shige data &= ~(1 << GPIO_PIN_SHIFT(p));
205 1.5 shige bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
206 1.5 shige GPIO_TCR, data);
207 1.5 shige }
208 1.5 shige
209 1.5 shige if (flags & GPIO_PIN_OPENDRAIN) {
210 1.5 shige /* GPIOn_ODR register bit is 1 */
211 1.5 shige data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
212 1.5 shige GPIO_ODR);
213 1.5 shige data |= (1 << GPIO_PIN_SHIFT(p));
214 1.5 shige bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
215 1.5 shige GPIO_ODR, data);
216 1.5 shige }
217 1.1 shige }
218