]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/mpc85xx/qoriq_gpio.c
Update svn-1.9.7 to 1.10.0.
[FreeBSD/FreeBSD.git] / sys / powerpc / mpc85xx / qoriq_gpio.c
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  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <sys/gpio.h>
41
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <machine/stdarg.h>
45
46 #include <dev/gpio/gpiobusvar.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49
50 #include "gpio_if.h"
51
52 #define MAXPIN          (31)
53
54 #define VALID_PIN(u)    ((u) >= 0 && (u) <= MAXPIN)
55
56 #define GPIO_LOCK(sc)                   mtx_lock(&(sc)->sc_mtx)
57 #define GPIO_UNLOCK(sc)         mtx_unlock(&(sc)->sc_mtx)
58 #define GPIO_LOCK_INIT(sc) \
59         mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
60             "gpio", MTX_DEF)
61 #define GPIO_LOCK_DESTROY(_sc)  mtx_destroy(&_sc->sc_mtx);
62
63 #define GPIO_GPDIR      0x0
64 #define GPIO_GPODR      0x4
65 #define GPIO_GPDAT      0x8
66 #define GPIO_GPIER      0xc
67 #define GPIO_GPIMR      0x10
68 #define GPIO_GPICR      0x14
69
70
71 struct qoriq_gpio_softc {
72         device_t        dev;
73         device_t        busdev;
74         struct mtx      sc_mtx;
75         struct resource *sc_mem;        /* Memory resource */
76 };
77
78 static device_t
79 qoriq_gpio_get_bus(device_t dev)
80 {
81         struct qoriq_gpio_softc *sc;
82
83         sc = device_get_softc(dev);
84
85         return (sc->busdev);
86 }
87
88 static int
89 qoriq_gpio_pin_max(device_t dev, int *maxpin)
90 {
91
92         *maxpin = MAXPIN;
93         return (0);
94 }
95
96 /* Get a specific pin's capabilities. */
97 static int
98 qoriq_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
99 {
100
101         if (!VALID_PIN(pin))
102                 return (EINVAL);
103
104         *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN);
105
106         return (0);
107 }
108
109 /* Get a specific pin's name. */
110 static int
111 qoriq_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
112 {
113
114         if (!VALID_PIN(pin))
115                 return (EINVAL);
116
117         snprintf(name, GPIOMAXNAME, "qoriq_gpio%d.%d",
118             device_get_unit(dev), pin);
119         name[GPIOMAXNAME-1] = '\0';
120
121         return (0);
122 }
123
124 /* Set flags for the pin. */
125 static int
126 qoriq_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
127 {
128         struct qoriq_gpio_softc *sc = device_get_softc(dev);
129         uint32_t reg;
130
131         if (!VALID_PIN(pin))
132                 return (EINVAL);
133
134         if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) ==
135             (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT))
136                 return (EINVAL);
137
138         GPIO_LOCK(sc);
139         if (flags & GPIO_PIN_INPUT) {
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         }
144         else if (flags & GPIO_PIN_OUTPUT) {
145                 reg = bus_read_4(sc->sc_mem, GPIO_GPDIR);
146                 reg |= (1 << (31 - pin));
147                 bus_write_4(sc->sc_mem, GPIO_GPDIR, reg);
148                 reg = bus_read_4(sc->sc_mem, GPIO_GPODR);
149                 if (flags & GPIO_PIN_OPENDRAIN)
150                         reg |= (1 << (31 - pin));
151                 else
152                         reg &= ~(1 << (31 - pin));
153                 bus_write_4(sc->sc_mem, GPIO_GPODR, reg);
154         }
155         GPIO_UNLOCK(sc);
156         return (0);
157 }
158
159 /* Set a specific output pin's value. */
160 static int
161 qoriq_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
162 {
163         struct qoriq_gpio_softc *sc = device_get_softc(dev);
164         uint32_t outvals;
165         uint8_t pinbit;
166
167         if (!VALID_PIN(pin) || value > 1)
168                 return (EINVAL);
169
170         GPIO_LOCK(sc);
171         pinbit = 31 - pin;
172
173         outvals = bus_read_4(sc->sc_mem, GPIO_GPDAT);
174         outvals &= ~(1 << pinbit);
175         outvals |= (value << pinbit);
176         bus_write_4(sc->sc_mem, GPIO_GPDAT, outvals);
177
178         GPIO_UNLOCK(sc);
179
180         return (0);
181 }
182
183 /* Get a specific pin's input value. */
184 static int
185 qoriq_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
186 {
187         struct qoriq_gpio_softc *sc = device_get_softc(dev);
188
189         if (!VALID_PIN(pin))
190                 return (EINVAL);
191
192         *value = (bus_read_4(sc->sc_mem, GPIO_GPDAT) >> (31 - pin)) & 1;
193
194         return (0);
195 }
196
197 /* Toggle a pin's output value. */
198 static int
199 qoriq_gpio_pin_toggle(device_t dev, uint32_t pin)
200 {
201         struct qoriq_gpio_softc *sc = device_get_softc(dev);
202         uint32_t val;
203
204         if (!VALID_PIN(pin))
205                 return (EINVAL);
206
207         GPIO_LOCK(sc);
208
209         val = bus_read_4(sc->sc_mem, GPIO_GPDAT);
210         val ^= (1 << (31 - pin));
211         bus_write_4(sc->sc_mem, GPIO_GPDAT, val);
212         
213         GPIO_UNLOCK(sc);
214
215         return (0);
216 }
217
218 static struct ofw_compat_data gpio_matches[] = {
219     {"fsl,qoriq-gpio", 1},
220     {"fsl,pq3-gpio", 1},
221     {"fsl,mpc8572-gpio", 1},
222     {0, 0}
223 };
224
225 static int
226 qoriq_gpio_probe(device_t dev)
227 {
228
229         if (ofw_bus_search_compatible(dev, gpio_matches)->ocd_data == 0)
230                 return (ENXIO);
231
232         device_set_desc(dev, "Freescale QorIQ GPIO driver");
233
234         return (0);
235 }
236
237 static int qoriq_gpio_detach(device_t dev);
238
239 static int
240 qoriq_gpio_attach(device_t dev)
241 {
242         struct qoriq_gpio_softc *sc = device_get_softc(dev);
243         int rid;
244
245         sc->dev = dev;
246
247         GPIO_LOCK_INIT(sc);
248
249         /* Allocate memory. */
250         rid = 0;
251         sc->sc_mem = bus_alloc_resource_any(dev,
252                      SYS_RES_MEMORY, &rid, RF_ACTIVE);
253         if (sc->sc_mem == NULL) {
254                 device_printf(dev, "Can't allocate memory for device output port");
255                 qoriq_gpio_detach(dev);
256                 return (ENOMEM);
257         }
258
259         sc->busdev = gpiobus_attach_bus(dev);
260         if (sc->busdev == NULL) {
261                 qoriq_gpio_detach(dev);
262                 return (ENOMEM);
263         }
264
265         OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
266
267         return (0);
268 }
269
270 static int
271 qoriq_gpio_detach(device_t dev)
272 {
273         struct qoriq_gpio_softc *sc = device_get_softc(dev);
274
275         gpiobus_detach_bus(dev);
276
277         if (sc->sc_mem != NULL) {
278                 /* Release output port resource. */
279                 bus_release_resource(dev, SYS_RES_MEMORY,
280                                      rman_get_rid(sc->sc_mem), sc->sc_mem);
281         }
282
283         GPIO_LOCK_DESTROY(sc);
284
285         return (0);
286 }
287
288 static device_method_t qoriq_gpio_methods[] = {
289         /* device_if */
290         DEVMETHOD(device_probe,         qoriq_gpio_probe),
291         DEVMETHOD(device_attach,        qoriq_gpio_attach),
292         DEVMETHOD(device_detach,        qoriq_gpio_detach),
293
294         /* GPIO protocol */
295         DEVMETHOD(gpio_get_bus,         qoriq_gpio_get_bus),
296         DEVMETHOD(gpio_pin_max,         qoriq_gpio_pin_max),
297         DEVMETHOD(gpio_pin_getname,     qoriq_gpio_pin_getname),
298         DEVMETHOD(gpio_pin_getcaps,     qoriq_gpio_pin_getcaps),
299         DEVMETHOD(gpio_pin_get,         qoriq_gpio_pin_get),
300         DEVMETHOD(gpio_pin_set,         qoriq_gpio_pin_set),
301         DEVMETHOD(gpio_pin_setflags,    qoriq_gpio_pin_setflags),
302         DEVMETHOD(gpio_pin_toggle,      qoriq_gpio_pin_toggle),
303
304         DEVMETHOD_END
305 };
306
307 static driver_t qoriq_gpio_driver = {
308         "gpio",
309         qoriq_gpio_methods,
310         sizeof(struct qoriq_gpio_softc),
311 };
312 static devclass_t qoriq_gpio_devclass;
313
314 EARLY_DRIVER_MODULE(qoriq_gpio, simplebus, qoriq_gpio_driver,
315     qoriq_gpio_devclass, NULL, NULL,
316     BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);