ti_gpio.c revision 1.2 1 /* $NetBSD: ti_gpio.c,v 1.2 2019/10/29 22:19:13 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ti_gpio.c,v 1.2 2019/10/29 22:19:13 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/systm.h>
37 #include <sys/mutex.h>
38 #include <sys/kmem.h>
39 #include <sys/gpio.h>
40 #include <sys/bitops.h>
41
42 #include <dev/fdt/fdtvar.h>
43 #include <dev/gpio/gpiovar.h>
44
45 #include <arm/ti/ti_prcm.h>
46
47 #define GPIO_OE 0x34
48 #define GPIO_DATAIN 0x38
49 #define GPIO_CLEARDATAOUT 0x90
50 #define GPIO_SETDATAOUT 0x94
51
52 static const struct of_compat_data compat_data[] = {
53 /* compatible reg offset */
54 { "ti,omap3-gpio", 0x0 },
55 { "ti,omap4-gpio", 0x100 },
56 { NULL }
57 };
58
59 struct ti_gpio_softc {
60 device_t sc_dev;
61 bus_space_tag_t sc_bst;
62 bus_space_handle_t sc_bsh;
63 kmutex_t sc_lock;
64 bus_size_t sc_regoff;
65
66 struct gpio_chipset_tag sc_gp;
67 gpio_pin_t sc_pins[32];
68 device_t sc_gpiodev;
69 };
70
71 struct ti_gpio_pin {
72 struct ti_gpio_softc *pin_sc;
73 u_int pin_nr;
74 int pin_flags;
75 bool pin_actlo;
76 };
77
78 #define RD4(sc, reg) \
79 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg) + (sc)->sc_regoff)
80 #define WR4(sc, reg, val) \
81 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg) + (sc)->sc_regoff, (val))
82
83 static int ti_gpio_match(device_t, cfdata_t, void *);
84 static void ti_gpio_attach(device_t, device_t, void *);
85
86 CFATTACH_DECL_NEW(ti_gpio, sizeof(struct ti_gpio_softc),
87 ti_gpio_match, ti_gpio_attach, NULL, NULL);
88
89 static int
90 ti_gpio_ctl(struct ti_gpio_softc *sc, u_int pin, int flags)
91 {
92 uint32_t oe;
93
94 KASSERT(mutex_owned(&sc->sc_lock));
95
96 oe = RD4(sc, GPIO_OE);
97 if (flags & GPIO_PIN_INPUT)
98 oe |= __BIT(pin);
99 else if (flags & GPIO_PIN_OUTPUT)
100 oe &= ~__BIT(pin);
101 WR4(sc, GPIO_OE, oe);
102
103 return 0;
104 }
105
106 static void *
107 ti_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
108 {
109 struct ti_gpio_softc * const sc = device_private(dev);
110 struct ti_gpio_pin *gpin;
111 const u_int *gpio = data;
112 int error;
113
114 if (len != 12)
115 return NULL;
116
117 const uint8_t pin = be32toh(gpio[1]) & 0xff;
118 const bool actlo = be32toh(gpio[2]) & 1;
119
120 if (pin >= __arraycount(sc->sc_pins))
121 return NULL;
122
123 mutex_enter(&sc->sc_lock);
124 error = ti_gpio_ctl(sc, pin, flags);
125 mutex_exit(&sc->sc_lock);
126
127 if (error != 0)
128 return NULL;
129
130 gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
131 gpin->pin_sc = sc;
132 gpin->pin_nr = pin;
133 gpin->pin_flags = flags;
134 gpin->pin_actlo = actlo;
135
136 return gpin;
137 }
138
139 static void
140 ti_gpio_release(device_t dev, void *priv)
141 {
142 struct ti_gpio_softc * const sc = device_private(dev);
143 struct ti_gpio_pin *pin = priv;
144
145 mutex_enter(&sc->sc_lock);
146 ti_gpio_ctl(pin->pin_sc, pin->pin_nr, GPIO_PIN_INPUT);
147 mutex_exit(&sc->sc_lock);
148
149 kmem_free(pin, sizeof(*pin));
150 }
151
152 static int
153 ti_gpio_read(device_t dev, void *priv, bool raw)
154 {
155 struct ti_gpio_softc * const sc = device_private(dev);
156 struct ti_gpio_pin *pin = priv;
157 uint32_t data;
158 int val;
159
160 KASSERT(sc == pin->pin_sc);
161
162 const uint32_t data_mask = __BIT(pin->pin_nr);
163
164 /* No lock required for reads */
165 data = RD4(sc, GPIO_DATAIN);
166 val = __SHIFTOUT(data, data_mask);
167 if (!raw && pin->pin_actlo)
168 val = !val;
169
170 return val;
171 }
172
173 static void
174 ti_gpio_write(device_t dev, void *priv, int val, bool raw)
175 {
176 struct ti_gpio_softc * const sc = device_private(dev);
177 struct ti_gpio_pin *pin = priv;
178
179 KASSERT(sc == pin->pin_sc);
180
181 const uint32_t data_mask = __BIT(pin->pin_nr);
182
183 if (!raw && pin->pin_actlo)
184 val = !val;
185
186 const u_int data_reg = val ? GPIO_SETDATAOUT : GPIO_CLEARDATAOUT;
187
188 WR4(sc, data_reg, data_mask);
189 }
190
191 static struct fdtbus_gpio_controller_func ti_gpio_funcs = {
192 .acquire = ti_gpio_acquire,
193 .release = ti_gpio_release,
194 .read = ti_gpio_read,
195 .write = ti_gpio_write,
196 };
197
198 static int
199 ti_gpio_pin_read(void *priv, int pin)
200 {
201 struct ti_gpio_softc * const sc = priv;
202 uint32_t data;
203 int val;
204
205 KASSERT(pin < __arraycount(sc->sc_pins));
206
207 const uint32_t data_mask = __BIT(pin);
208
209 data = RD4(sc, GPIO_DATAIN);
210 val = __SHIFTOUT(data, data_mask);
211
212 return val;
213 }
214
215 static void
216 ti_gpio_pin_write(void *priv, int pin, int val)
217 {
218 struct ti_gpio_softc * const sc = priv;
219
220 KASSERT(pin < __arraycount(sc->sc_pins));
221
222 const u_int data_reg = val ? GPIO_SETDATAOUT : GPIO_CLEARDATAOUT;
223 const uint32_t data_mask = __BIT(pin);
224
225 WR4(sc, data_reg, data_mask);
226 }
227
228 static void
229 ti_gpio_pin_ctl(void *priv, int pin, int flags)
230 {
231 struct ti_gpio_softc * const sc = priv;
232
233 KASSERT(pin < __arraycount(sc->sc_pins));
234
235 mutex_enter(&sc->sc_lock);
236 ti_gpio_ctl(sc, pin, flags);
237 mutex_exit(&sc->sc_lock);
238 }
239
240 static void
241 ti_gpio_attach_ports(struct ti_gpio_softc *sc)
242 {
243 struct gpio_chipset_tag *gp = &sc->sc_gp;
244 struct gpiobus_attach_args gba;
245 u_int pin;
246
247 gp->gp_cookie = sc;
248 gp->gp_pin_read = ti_gpio_pin_read;
249 gp->gp_pin_write = ti_gpio_pin_write;
250 gp->gp_pin_ctl = ti_gpio_pin_ctl;
251
252 for (pin = 0; pin < __arraycount(sc->sc_pins); pin++) {
253 sc->sc_pins[pin].pin_num = pin;
254 sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
255 sc->sc_pins[pin].pin_state = ti_gpio_pin_read(sc, pin);
256 }
257
258 memset(&gba, 0, sizeof(gba));
259 gba.gba_gc = gp;
260 gba.gba_pins = sc->sc_pins;
261 gba.gba_npins = __arraycount(sc->sc_pins);
262 sc->sc_gpiodev = config_found_ia(sc->sc_dev, "gpiobus", &gba, NULL);
263 }
264
265 static int
266 ti_gpio_match(device_t parent, cfdata_t cf, void *aux)
267 {
268 struct fdt_attach_args * const faa = aux;
269
270 return of_match_compat_data(faa->faa_phandle, compat_data);
271 }
272
273 static void
274 ti_gpio_attach(device_t parent, device_t self, void *aux)
275 {
276 struct ti_gpio_softc * const sc = device_private(self);
277 struct fdt_attach_args * const faa = aux;
278 const int phandle = faa->faa_phandle;
279 const char *modname;
280 bus_addr_t addr;
281 bus_size_t size;
282
283 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
284 aprint_error(": couldn't get registers\n");
285 return;
286 }
287 if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
288 aprint_error(": couldn't enable module\n");
289 return;
290 }
291
292 sc->sc_dev = self;
293 sc->sc_bst = faa->faa_bst;
294 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
295 aprint_error(": couldn't map registers\n");
296 return;
297 }
298 sc->sc_regoff = of_search_compatible(phandle, compat_data)->data;
299 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
300
301 modname = fdtbus_get_string(phandle, "ti,hwmods");
302 if (modname == NULL)
303 modname = fdtbus_get_string(OF_parent(phandle), "ti,hwmods");
304
305 aprint_naive("\n");
306 aprint_normal(": GPIO (%s)\n", modname);
307
308 fdtbus_register_gpio_controller(self, phandle, &ti_gpio_funcs);
309
310 ti_gpio_attach_ports(sc);
311 }
312