gpio_opb.c revision 1.4.4.1 1 1.4.4.1 rpaulo /* $NetBSD: gpio_opb.c,v 1.4.4.1 2006/09/09 02:42:22 rpaulo 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.4.4.1 rpaulo #include <sys/gpio.h>
42 1.4.4.1 rpaulo #include <dev/gpio/gpiovar.h>
43 1.4.4.1 rpaulo
44 1.1 shige #include <powerpc/ibm4xx/dcr405gp.h>
45 1.1 shige #include <powerpc/ibm4xx/dev/opbvar.h>
46 1.1 shige #include <powerpc/ibm4xx/dev/gpioreg.h>
47 1.1 shige
48 1.4.4.1 rpaulo struct gpio_opb_softc {
49 1.1 shige struct device sc_dev; /* device generic */
50 1.4.4.1 rpaulo /* GPIO interface */
51 1.4.4.1 rpaulo bus_space_tag_t sc_gpio_iot;
52 1.4.4.1 rpaulo bus_space_handle_t sc_gpio_ioh;
53 1.4.4.1 rpaulo struct gpio_chipset_tag sc_gpio_gc;
54 1.4.4.1 rpaulo gpio_pin_t sc_gpio_pins[GPIO_NPINS];
55 1.1 shige };
56 1.1 shige
57 1.4.4.1 rpaulo static int gpio_opb_match(struct device *, struct cfdata *, void *);
58 1.4.4.1 rpaulo static void gpio_opb_attach(struct device *, struct device *, void *);
59 1.1 shige
60 1.4.4.1 rpaulo CFATTACH_DECL(opbgpio, sizeof(struct gpio_opb_softc),
61 1.4.4.1 rpaulo gpio_opb_match, gpio_opb_attach, NULL, NULL);
62 1.1 shige
63 1.4.4.1 rpaulo static int gpio_opb_pin_read(void *, int);
64 1.4.4.1 rpaulo static void gpio_opb_pin_write(void *, int, int);
65 1.4.4.1 rpaulo static void gpio_opb_pin_ctl(void *, int, int);
66 1.1 shige
67 1.1 shige
68 1.1 shige static int
69 1.4.4.1 rpaulo gpio_opb_match(struct device *parent, struct cfdata *cf, void *aux)
70 1.1 shige {
71 1.4.4.1 rpaulo struct opb_attach_args *oaa = aux;
72 1.1 shige
73 1.1 shige if (strcmp(oaa->opb_name, cf->cf_name) != 0)
74 1.4.4.1 rpaulo return 0;
75 1.1 shige
76 1.4.4.1 rpaulo return 1;
77 1.1 shige }
78 1.1 shige
79 1.1 shige static void
80 1.4.4.1 rpaulo gpio_opb_attach(struct device *parent, struct device *self, void *aux)
81 1.1 shige {
82 1.4.4.1 rpaulo struct gpio_opb_softc *sc = (struct gpio_opb_softc *)self;
83 1.1 shige struct opb_attach_args *oaa = aux;
84 1.4.4.1 rpaulo struct gpiobus_attach_args gba;
85 1.4.4.1 rpaulo int i;
86 1.4.4.1 rpaulo uint32_t reg1, reg2, reg3;
87 1.1 shige
88 1.1 shige aprint_naive(": GPIO controller\n");
89 1.1 shige aprint_normal(": On-Chip GPIO controller\n");
90 1.1 shige
91 1.4.4.1 rpaulo /* Map GPIO I/O space */
92 1.4.4.1 rpaulo sc->sc_gpio_iot = oaa->opb_bt;
93 1.4.4.1 rpaulo bus_space_map(sc->sc_gpio_iot, oaa->opb_addr,
94 1.4.4.1 rpaulo GPIO_NREG, 0, &sc->sc_gpio_ioh);
95 1.4.4.1 rpaulo
96 1.4.4.1 rpaulo /* Read current register status */
97 1.4.4.1 rpaulo reg1 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_IR);
98 1.4.4.1 rpaulo reg2 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_TCR);
99 1.4.4.1 rpaulo reg3 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_ODR);
100 1.4.4.1 rpaulo
101 1.4.4.1 rpaulo /* Initialize ping array */
102 1.4.4.1 rpaulo for (i = 0 ; i < GPIO_NPINS ; i++) {
103 1.4.4.1 rpaulo int p = i + 1;
104 1.4.4.1 rpaulo sc->sc_gpio_pins[i].pin_num = i;
105 1.4.4.1 rpaulo sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INOUT
106 1.4.4.1 rpaulo | GPIO_PIN_OPENDRAIN
107 1.4.4.1 rpaulo | GPIO_PIN_TRISTATE;
108 1.4.4.1 rpaulo
109 1.4.4.1 rpaulo /* current defaults */
110 1.4.4.1 rpaulo sc->sc_gpio_pins[i].pin_flags =
111 1.4.4.1 rpaulo ((reg3 >> GPIO_PIN_SHIFT(p)) & 0x01)
112 1.4.4.1 rpaulo ? GPIO_PIN_OPENDRAIN
113 1.4.4.1 rpaulo : (((reg2 >> GPIO_PIN_SHIFT(p)) & 0x01)
114 1.4.4.1 rpaulo ? GPIO_PIN_INOUT
115 1.4.4.1 rpaulo : GPIO_PIN_TRISTATE);
116 1.4.4.1 rpaulo sc->sc_gpio_pins[i].pin_state =
117 1.4.4.1 rpaulo ((reg1 >> GPIO_PIN_SHIFT(p)) & 0x01);
118 1.4.4.1 rpaulo sc->sc_gpio_pins[i].pin_mapped = 0;
119 1.4.4.1 rpaulo }
120 1.4.4.1 rpaulo
121 1.4.4.1 rpaulo /* Create controller tag */
122 1.4.4.1 rpaulo sc->sc_gpio_gc.gp_cookie = sc;
123 1.4.4.1 rpaulo sc->sc_gpio_gc.gp_pin_read = gpio_opb_pin_read;
124 1.4.4.1 rpaulo sc->sc_gpio_gc.gp_pin_write = gpio_opb_pin_write;
125 1.4.4.1 rpaulo sc->sc_gpio_gc.gp_pin_ctl = gpio_opb_pin_ctl;
126 1.4.4.1 rpaulo
127 1.4.4.1 rpaulo gba.gba_gc = &sc->sc_gpio_gc;
128 1.4.4.1 rpaulo gba.gba_pins = sc->sc_gpio_pins;
129 1.4.4.1 rpaulo gba.gba_npins = GPIO_NPINS;
130 1.4.4.1 rpaulo
131 1.4.4.1 rpaulo /* Attach GPIO framework */
132 1.4.4.1 rpaulo (void) config_found(&sc->sc_dev, &gba, gpiobus_print);
133 1.1 shige }
134 1.1 shige
135 1.1 shige static int
136 1.4.4.1 rpaulo gpio_opb_pin_read(void *arg, int pin)
137 1.4.4.1 rpaulo {
138 1.4.4.1 rpaulo struct gpio_opb_softc *sc = arg;
139 1.4.4.1 rpaulo uint32_t data;
140 1.4.4.1 rpaulo int p;
141 1.1 shige
142 1.4.4.1 rpaulo p = pin % GPIO_NPINS;
143 1.4.4.1 rpaulo p = p + 1;
144 1.1 shige
145 1.4.4.1 rpaulo data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_IR);
146 1.1 shige
147 1.4.4.1 rpaulo return (data >> GPIO_PIN_SHIFT(p)) & 0x01;
148 1.1 shige }
149 1.1 shige
150 1.1 shige static void
151 1.4.4.1 rpaulo gpio_opb_pin_write(void *arg, int pin, int value)
152 1.1 shige {
153 1.4.4.1 rpaulo struct gpio_opb_softc *sc = arg;
154 1.4.4.1 rpaulo uint32_t data;
155 1.4.4.1 rpaulo int p;
156 1.4.4.1 rpaulo
157 1.4.4.1 rpaulo p = pin % GPIO_NPINS;
158 1.4.4.1 rpaulo p = p + 1;
159 1.4.4.1 rpaulo
160 1.4.4.1 rpaulo data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_OR);
161 1.4.4.1 rpaulo if (value == 0) {
162 1.4.4.1 rpaulo data &= ~(1 << GPIO_PIN_SHIFT(p));
163 1.4.4.1 rpaulo } else if (value == 1) {
164 1.4.4.1 rpaulo data |= (1 << GPIO_PIN_SHIFT(p));
165 1.4.4.1 rpaulo }
166 1.1 shige
167 1.4.4.1 rpaulo bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_OR, data);
168 1.1 shige }
169 1.1 shige
170 1.1 shige static void
171 1.4.4.1 rpaulo gpio_opb_pin_ctl(void *arg, int pin, int flags)
172 1.1 shige {
173 1.4.4.1 rpaulo struct gpio_opb_softc *sc = arg;
174 1.4.4.1 rpaulo uint32_t data;
175 1.4.4.1 rpaulo int p;
176 1.4.4.1 rpaulo
177 1.4.4.1 rpaulo p = pin % GPIO_NPINS;
178 1.4.4.1 rpaulo p = p + 1;
179 1.4.4.1 rpaulo
180 1.4.4.1 rpaulo if (flags & GPIO_PIN_INOUT) {
181 1.4.4.1 rpaulo /* GPIOn_ODR register bit is 0 */
182 1.4.4.1 rpaulo data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
183 1.4.4.1 rpaulo GPIO_ODR);
184 1.4.4.1 rpaulo data &= ~(1 << GPIO_PIN_SHIFT(p));
185 1.4.4.1 rpaulo bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
186 1.4.4.1 rpaulo GPIO_ODR, data);
187 1.4.4.1 rpaulo /* GPIOn_TCR register bit is 1 */
188 1.4.4.1 rpaulo data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
189 1.4.4.1 rpaulo GPIO_TCR);
190 1.4.4.1 rpaulo data |= (1 << GPIO_PIN_SHIFT(p));
191 1.4.4.1 rpaulo bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
192 1.4.4.1 rpaulo GPIO_TCR, data);
193 1.4.4.1 rpaulo }
194 1.4.4.1 rpaulo
195 1.4.4.1 rpaulo if (flags & GPIO_PIN_TRISTATE) {
196 1.4.4.1 rpaulo /* GPIOn_ODR register bit is 0 */
197 1.4.4.1 rpaulo data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
198 1.4.4.1 rpaulo GPIO_ODR);
199 1.4.4.1 rpaulo data &= ~(1 << GPIO_PIN_SHIFT(p));
200 1.4.4.1 rpaulo bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
201 1.4.4.1 rpaulo GPIO_ODR, data);
202 1.4.4.1 rpaulo /* GPIOn_TCR register bit is 0 */
203 1.4.4.1 rpaulo data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
204 1.4.4.1 rpaulo GPIO_TCR);
205 1.4.4.1 rpaulo data &= ~(1 << GPIO_PIN_SHIFT(p));
206 1.4.4.1 rpaulo bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
207 1.4.4.1 rpaulo GPIO_TCR, data);
208 1.4.4.1 rpaulo }
209 1.4.4.1 rpaulo
210 1.4.4.1 rpaulo if (flags & GPIO_PIN_OPENDRAIN) {
211 1.4.4.1 rpaulo /* GPIOn_ODR register bit is 1 */
212 1.4.4.1 rpaulo data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
213 1.4.4.1 rpaulo GPIO_ODR);
214 1.4.4.1 rpaulo data |= (1 << GPIO_PIN_SHIFT(p));
215 1.4.4.1 rpaulo bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
216 1.4.4.1 rpaulo GPIO_ODR, data);
217 1.4.4.1 rpaulo }
218 1.1 shige }
219