1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
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 AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, 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 /*
30 * Vybrid Family General-Purpose Input/Output (GPIO)
31 * Chapter 7, Vybrid Reference Manual, Rev. 5, 07/2013
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/rman.h>
41 #include <sys/timeet.h>
42 #include <sys/timetc.h>
43 #include <sys/watchdog.h>
44 #include <sys/mutex.h>
45 #include <sys/gpio.h>
46
47 #include <dev/gpio/gpiobusvar.h>
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
54 #include <machine/intr.h>
55
56 #include "gpio_if.h"
57
58 #include <arm/freescale/vybrid/vf_common.h>
59 #include <arm/freescale/vybrid/vf_port.h>
60
61 #define GPIO_PDOR(n) (0x00 + 0x40 * (n >> 5))
62 #define GPIO_PSOR(n) (0x04 + 0x40 * (n >> 5))
63 #define GPIO_PCOR(n) (0x08 + 0x40 * (n >> 5))
64 #define GPIO_PTOR(n) (0x0C + 0x40 * (n >> 5))
65 #define GPIO_PDIR(n) (0x10 + 0x40 * (n >> 5))
66
67 #define GPIO_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
68 #define GPIO_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
69
70 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
71
72 /*
73 * GPIO interface
74 */
75 static device_t vf_gpio_get_bus(device_t);
76 static int vf_gpio_pin_max(device_t, int *);
77 static int vf_gpio_pin_getcaps(device_t, uint32_t, uint32_t *);
78 static int vf_gpio_pin_getname(device_t, uint32_t, char *);
79 static int vf_gpio_pin_getflags(device_t, uint32_t, uint32_t *);
80 static int vf_gpio_pin_setflags(device_t, uint32_t, uint32_t);
81 static int vf_gpio_pin_set(device_t, uint32_t, unsigned int);
82 static int vf_gpio_pin_get(device_t, uint32_t, unsigned int *);
83 static int vf_gpio_pin_toggle(device_t, uint32_t pin);
84
85 struct vf_gpio_softc {
86 struct resource *res[1];
87 bus_space_tag_t bst;
88 bus_space_handle_t bsh;
89
90 device_t sc_busdev;
91 struct mtx sc_mtx;
92 int gpio_npins;
93 struct gpio_pin gpio_pins[NGPIO];
94 };
95
96 struct vf_gpio_softc *gpio_sc;
97
98 static struct resource_spec vf_gpio_spec[] = {
99 { SYS_RES_MEMORY, 0, RF_ACTIVE },
100 { -1, 0 }
101 };
102
103 static int
vf_gpio_probe(device_t dev)104 vf_gpio_probe(device_t dev)
105 {
106
107 if (!ofw_bus_status_okay(dev))
108 return (ENXIO);
109
110 if (!ofw_bus_is_compatible(dev, "fsl,mvf600-gpio"))
111 return (ENXIO);
112
113 device_set_desc(dev, "Vybrid Family GPIO Unit");
114 return (BUS_PROBE_DEFAULT);
115 }
116
117 static int
vf_gpio_attach(device_t dev)118 vf_gpio_attach(device_t dev)
119 {
120 struct vf_gpio_softc *sc;
121 int i;
122
123 sc = device_get_softc(dev);
124 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
125
126 if (bus_alloc_resources(dev, vf_gpio_spec, sc->res)) {
127 device_printf(dev, "could not allocate resources\n");
128 mtx_destroy(&sc->sc_mtx);
129 return (ENXIO);
130 }
131
132 /* Memory interface */
133 sc->bst = rman_get_bustag(sc->res[0]);
134 sc->bsh = rman_get_bushandle(sc->res[0]);
135
136 gpio_sc = sc;
137
138 sc->gpio_npins = NGPIO;
139
140 for (i = 0; i < sc->gpio_npins; i++) {
141 sc->gpio_pins[i].gp_pin = i;
142 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
143 sc->gpio_pins[i].gp_flags =
144 (READ4(sc, GPIO_PDOR(i)) & (1 << (i % 32))) ?
145 GPIO_PIN_OUTPUT: GPIO_PIN_INPUT;
146 snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME,
147 "vf_gpio%d.%d", device_get_unit(dev), i);
148 }
149
150 sc->sc_busdev = gpiobus_attach_bus(dev);
151 if (sc->sc_busdev == NULL) {
152 bus_release_resources(dev, vf_gpio_spec, sc->res);
153 mtx_destroy(&sc->sc_mtx);
154 return (ENXIO);
155 }
156
157 return (0);
158 }
159
160 static device_t
vf_gpio_get_bus(device_t dev)161 vf_gpio_get_bus(device_t dev)
162 {
163 struct vf_gpio_softc *sc;
164
165 sc = device_get_softc(dev);
166
167 return (sc->sc_busdev);
168 }
169
170 static int
vf_gpio_pin_max(device_t dev,int * maxpin)171 vf_gpio_pin_max(device_t dev, int *maxpin)
172 {
173
174 *maxpin = NGPIO - 1;
175 return (0);
176 }
177
178 static int
vf_gpio_pin_getname(device_t dev,uint32_t pin,char * name)179 vf_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
180 {
181 struct vf_gpio_softc *sc;
182 int i;
183
184 sc = device_get_softc(dev);
185 for (i = 0; i < sc->gpio_npins; i++) {
186 if (sc->gpio_pins[i].gp_pin == pin)
187 break;
188 }
189
190 if (i >= sc->gpio_npins)
191 return (EINVAL);
192
193 GPIO_LOCK(sc);
194 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
195 GPIO_UNLOCK(sc);
196
197 return (0);
198 }
199
200 static int
vf_gpio_pin_getcaps(device_t dev,uint32_t pin,uint32_t * caps)201 vf_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
202 {
203 struct vf_gpio_softc *sc;
204 int i;
205
206 sc = device_get_softc(dev);
207 for (i = 0; i < sc->gpio_npins; i++) {
208 if (sc->gpio_pins[i].gp_pin == pin)
209 break;
210 }
211
212 if (i >= sc->gpio_npins)
213 return (EINVAL);
214
215 GPIO_LOCK(sc);
216 *caps = sc->gpio_pins[i].gp_caps;
217 GPIO_UNLOCK(sc);
218
219 return (0);
220 }
221
222 static int
vf_gpio_pin_getflags(device_t dev,uint32_t pin,uint32_t * flags)223 vf_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
224 {
225 struct vf_gpio_softc *sc;
226 int i;
227
228 sc = device_get_softc(dev);
229 for (i = 0; i < sc->gpio_npins; i++) {
230 if (sc->gpio_pins[i].gp_pin == pin)
231 break;
232 }
233
234 if (i >= sc->gpio_npins)
235 return (EINVAL);
236
237 GPIO_LOCK(sc);
238 *flags = sc->gpio_pins[i].gp_flags;
239 GPIO_UNLOCK(sc);
240
241 return (0);
242 }
243
244 static int
vf_gpio_pin_get(device_t dev,uint32_t pin,unsigned int * val)245 vf_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
246 {
247 struct vf_gpio_softc *sc;
248 int i;
249
250 sc = device_get_softc(dev);
251 for (i = 0; i < sc->gpio_npins; i++) {
252 if (sc->gpio_pins[i].gp_pin == pin)
253 break;
254 }
255
256 if (i >= sc->gpio_npins)
257 return (EINVAL);
258
259 GPIO_LOCK(sc);
260 *val = (READ4(sc, GPIO_PDIR(i)) & (1 << (i % 32))) ? 1 : 0;
261 GPIO_UNLOCK(sc);
262
263 return (0);
264 }
265
266 static int
vf_gpio_pin_toggle(device_t dev,uint32_t pin)267 vf_gpio_pin_toggle(device_t dev, uint32_t pin)
268 {
269 struct vf_gpio_softc *sc;
270 int i;
271
272 sc = device_get_softc(dev);
273 for (i = 0; i < sc->gpio_npins; i++) {
274 if (sc->gpio_pins[i].gp_pin == pin)
275 break;
276 }
277
278 if (i >= sc->gpio_npins)
279 return (EINVAL);
280
281 GPIO_LOCK(sc);
282 WRITE4(sc, GPIO_PTOR(i), (1 << (i % 32)));
283 GPIO_UNLOCK(sc);
284
285 return (0);
286 }
287
288 static void
vf_gpio_pin_configure(struct vf_gpio_softc * sc,struct gpio_pin * pin,unsigned int flags)289 vf_gpio_pin_configure(struct vf_gpio_softc *sc, struct gpio_pin *pin,
290 unsigned int flags)
291 {
292
293 GPIO_LOCK(sc);
294
295 /*
296 * Manage input/output
297 */
298 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
299 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
300 if (flags & GPIO_PIN_OUTPUT) {
301 pin->gp_flags |= GPIO_PIN_OUTPUT;
302
303 } else {
304 pin->gp_flags |= GPIO_PIN_INPUT;
305 WRITE4(sc, GPIO_PCOR(pin->gp_pin),
306 (1 << (pin->gp_pin % 32)));
307 }
308 }
309
310 GPIO_UNLOCK(sc);
311 }
312
313 static int
vf_gpio_pin_setflags(device_t dev,uint32_t pin,uint32_t flags)314 vf_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
315 {
316 struct vf_gpio_softc *sc;
317 int i;
318
319 sc = device_get_softc(dev);
320 for (i = 0; i < sc->gpio_npins; i++) {
321 if (sc->gpio_pins[i].gp_pin == pin)
322 break;
323 }
324
325 if (i >= sc->gpio_npins)
326 return (EINVAL);
327
328 vf_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
329
330 return (0);
331 }
332
333 static int
vf_gpio_pin_set(device_t dev,uint32_t pin,unsigned int value)334 vf_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
335 {
336 struct vf_gpio_softc *sc;
337 int i;
338
339 sc = device_get_softc(dev);
340 for (i = 0; i < sc->gpio_npins; i++) {
341 if (sc->gpio_pins[i].gp_pin == pin)
342 break;
343 }
344
345 if (i >= sc->gpio_npins)
346 return (EINVAL);
347
348 GPIO_LOCK(sc);
349 if (value)
350 WRITE4(sc, GPIO_PSOR(i), (1 << (i % 32)));
351 else
352 WRITE4(sc, GPIO_PCOR(i), (1 << (i % 32)));
353 GPIO_UNLOCK(sc);
354
355 return (0);
356 }
357
358 static device_method_t vf_gpio_methods[] = {
359 DEVMETHOD(device_probe, vf_gpio_probe),
360 DEVMETHOD(device_attach, vf_gpio_attach),
361
362 /* GPIO protocol */
363 DEVMETHOD(gpio_get_bus, vf_gpio_get_bus),
364 DEVMETHOD(gpio_pin_max, vf_gpio_pin_max),
365 DEVMETHOD(gpio_pin_getname, vf_gpio_pin_getname),
366 DEVMETHOD(gpio_pin_getcaps, vf_gpio_pin_getcaps),
367 DEVMETHOD(gpio_pin_getflags, vf_gpio_pin_getflags),
368 DEVMETHOD(gpio_pin_get, vf_gpio_pin_get),
369 DEVMETHOD(gpio_pin_toggle, vf_gpio_pin_toggle),
370 DEVMETHOD(gpio_pin_setflags, vf_gpio_pin_setflags),
371 DEVMETHOD(gpio_pin_set, vf_gpio_pin_set),
372 { 0, 0 }
373 };
374
375 static driver_t vf_gpio_driver = {
376 "gpio",
377 vf_gpio_methods,
378 sizeof(struct vf_gpio_softc),
379 };
380
381 DRIVER_MODULE(vf_gpio, simplebus, vf_gpio_driver, 0, 0);
382