1 /*-
2 * Copyright (c) 2015 Justin Hibbits
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/conf.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 #include <sys/rman.h>
36 #include <sys/gpio.h>
37
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <machine/stdarg.h>
41
42 #include <dev/gpio/gpiobusvar.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45
46 #include "gpio_if.h"
47
48 #define MAXPIN (31)
49
50 #define VALID_PIN(u) ((u) >= 0 && (u) <= MAXPIN)
51
52 #define GPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
53 #define GPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
54 #define GPIO_LOCK_INIT(sc) \
55 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
56 "gpio", MTX_DEF)
57 #define GPIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
58
59 #define GPIO_GPDIR 0x0
60 #define GPIO_GPODR 0x4
61 #define GPIO_GPDAT 0x8
62 #define GPIO_GPIER 0xc
63 #define GPIO_GPIMR 0x10
64 #define GPIO_GPICR 0x14
65
66 struct qoriq_gpio_softc {
67 device_t dev;
68 device_t busdev;
69 struct mtx sc_mtx;
70 struct resource *sc_mem; /* Memory resource */
71 };
72
73 static device_t
qoriq_gpio_get_bus(device_t dev)74 qoriq_gpio_get_bus(device_t dev)
75 {
76 struct qoriq_gpio_softc *sc;
77
78 sc = device_get_softc(dev);
79
80 return (sc->busdev);
81 }
82
83 static int
qoriq_gpio_pin_max(device_t dev,int * maxpin)84 qoriq_gpio_pin_max(device_t dev, int *maxpin)
85 {
86
87 *maxpin = MAXPIN;
88 return (0);
89 }
90
91 /* Get a specific pin's capabilities. */
92 static int
qoriq_gpio_pin_getcaps(device_t dev,uint32_t pin,uint32_t * caps)93 qoriq_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
94 {
95
96 if (!VALID_PIN(pin))
97 return (EINVAL);
98
99 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN);
100
101 return (0);
102 }
103
104 /* Get a specific pin's name. */
105 static int
qoriq_gpio_pin_getname(device_t dev,uint32_t pin,char * name)106 qoriq_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
107 {
108
109 if (!VALID_PIN(pin))
110 return (EINVAL);
111
112 snprintf(name, GPIOMAXNAME, "qoriq_gpio%d.%d",
113 device_get_unit(dev), pin);
114 name[GPIOMAXNAME-1] = '\0';
115
116 return (0);
117 }
118
119 /* Set flags for the pin. */
120 static int
qoriq_gpio_pin_setflags(device_t dev,uint32_t pin,uint32_t flags)121 qoriq_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
122 {
123 struct qoriq_gpio_softc *sc = device_get_softc(dev);
124 uint32_t reg;
125
126 if (!VALID_PIN(pin))
127 return (EINVAL);
128
129 if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) ==
130 (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT))
131 return (EINVAL);
132
133 GPIO_LOCK(sc);
134 if (flags & GPIO_PIN_INPUT) {
135 reg = bus_read_4(sc->sc_mem, GPIO_GPDIR);
136 reg &= ~(1 << (31 - pin));
137 bus_write_4(sc->sc_mem, GPIO_GPDIR, reg);
138 }
139 else if (flags & GPIO_PIN_OUTPUT) {
140 reg = bus_read_4(sc->sc_mem, GPIO_GPDIR);
141 reg |= (1 << (31 - pin));
142 bus_write_4(sc->sc_mem, GPIO_GPDIR, reg);
143 reg = bus_read_4(sc->sc_mem, GPIO_GPODR);
144 if (flags & GPIO_PIN_OPENDRAIN)
145 reg |= (1 << (31 - pin));
146 else
147 reg &= ~(1 << (31 - pin));
148 bus_write_4(sc->sc_mem, GPIO_GPODR, reg);
149 }
150 GPIO_UNLOCK(sc);
151 return (0);
152 }
153
154 /* Set a specific output pin's value. */
155 static int
qoriq_gpio_pin_set(device_t dev,uint32_t pin,unsigned int value)156 qoriq_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
157 {
158 struct qoriq_gpio_softc *sc = device_get_softc(dev);
159 uint32_t outvals;
160 uint8_t pinbit;
161
162 if (!VALID_PIN(pin) || value > 1)
163 return (EINVAL);
164
165 GPIO_LOCK(sc);
166 pinbit = 31 - pin;
167
168 outvals = bus_read_4(sc->sc_mem, GPIO_GPDAT);
169 outvals &= ~(1 << pinbit);
170 outvals |= (value << pinbit);
171 bus_write_4(sc->sc_mem, GPIO_GPDAT, outvals);
172
173 GPIO_UNLOCK(sc);
174
175 return (0);
176 }
177
178 /* Get a specific pin's input value. */
179 static int
qoriq_gpio_pin_get(device_t dev,uint32_t pin,unsigned int * value)180 qoriq_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
181 {
182 struct qoriq_gpio_softc *sc = device_get_softc(dev);
183
184 if (!VALID_PIN(pin))
185 return (EINVAL);
186
187 *value = (bus_read_4(sc->sc_mem, GPIO_GPDAT) >> (31 - pin)) & 1;
188
189 return (0);
190 }
191
192 /* Toggle a pin's output value. */
193 static int
qoriq_gpio_pin_toggle(device_t dev,uint32_t pin)194 qoriq_gpio_pin_toggle(device_t dev, uint32_t pin)
195 {
196 struct qoriq_gpio_softc *sc = device_get_softc(dev);
197 uint32_t val;
198
199 if (!VALID_PIN(pin))
200 return (EINVAL);
201
202 GPIO_LOCK(sc);
203
204 val = bus_read_4(sc->sc_mem, GPIO_GPDAT);
205 val ^= (1 << (31 - pin));
206 bus_write_4(sc->sc_mem, GPIO_GPDAT, val);
207
208 GPIO_UNLOCK(sc);
209
210 return (0);
211 }
212
213 static struct ofw_compat_data gpio_matches[] = {
214 {"fsl,qoriq-gpio", 1},
215 {"fsl,pq3-gpio", 1},
216 {"fsl,mpc8572-gpio", 1},
217 {0, 0}
218 };
219
220 static int
qoriq_gpio_probe(device_t dev)221 qoriq_gpio_probe(device_t dev)
222 {
223
224 if (ofw_bus_search_compatible(dev, gpio_matches)->ocd_data == 0)
225 return (ENXIO);
226
227 device_set_desc(dev, "Freescale QorIQ GPIO driver");
228
229 return (0);
230 }
231
232 static int qoriq_gpio_detach(device_t dev);
233
234 static int
qoriq_gpio_attach(device_t dev)235 qoriq_gpio_attach(device_t dev)
236 {
237 struct qoriq_gpio_softc *sc = device_get_softc(dev);
238 int rid;
239
240 sc->dev = dev;
241
242 GPIO_LOCK_INIT(sc);
243
244 /* Allocate memory. */
245 rid = 0;
246 sc->sc_mem = bus_alloc_resource_any(dev,
247 SYS_RES_MEMORY, &rid, RF_ACTIVE);
248 if (sc->sc_mem == NULL) {
249 device_printf(dev, "Can't allocate memory for device output port");
250 qoriq_gpio_detach(dev);
251 return (ENOMEM);
252 }
253
254 sc->busdev = gpiobus_attach_bus(dev);
255 if (sc->busdev == NULL) {
256 qoriq_gpio_detach(dev);
257 return (ENOMEM);
258 }
259
260 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
261
262 return (0);
263 }
264
265 static int
qoriq_gpio_detach(device_t dev)266 qoriq_gpio_detach(device_t dev)
267 {
268 struct qoriq_gpio_softc *sc = device_get_softc(dev);
269
270 gpiobus_detach_bus(dev);
271
272 if (sc->sc_mem != NULL) {
273 /* Release output port resource. */
274 bus_release_resource(dev, SYS_RES_MEMORY,
275 rman_get_rid(sc->sc_mem), sc->sc_mem);
276 }
277
278 GPIO_LOCK_DESTROY(sc);
279
280 return (0);
281 }
282
283 static device_method_t qoriq_gpio_methods[] = {
284 /* device_if */
285 DEVMETHOD(device_probe, qoriq_gpio_probe),
286 DEVMETHOD(device_attach, qoriq_gpio_attach),
287 DEVMETHOD(device_detach, qoriq_gpio_detach),
288
289 /* GPIO protocol */
290 DEVMETHOD(gpio_get_bus, qoriq_gpio_get_bus),
291 DEVMETHOD(gpio_pin_max, qoriq_gpio_pin_max),
292 DEVMETHOD(gpio_pin_getname, qoriq_gpio_pin_getname),
293 DEVMETHOD(gpio_pin_getcaps, qoriq_gpio_pin_getcaps),
294 DEVMETHOD(gpio_pin_get, qoriq_gpio_pin_get),
295 DEVMETHOD(gpio_pin_set, qoriq_gpio_pin_set),
296 DEVMETHOD(gpio_pin_setflags, qoriq_gpio_pin_setflags),
297 DEVMETHOD(gpio_pin_toggle, qoriq_gpio_pin_toggle),
298
299 DEVMETHOD_END
300 };
301
302 static driver_t qoriq_gpio_driver = {
303 "gpio",
304 qoriq_gpio_methods,
305 sizeof(struct qoriq_gpio_softc),
306 };
307 static devclass_t qoriq_gpio_devclass;
308
309 EARLY_DRIVER_MODULE(qoriq_gpio, simplebus, qoriq_gpio_driver,
310 qoriq_gpio_devclass, NULL, NULL,
311 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
312