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