xref: /freebsd/sys/powerpc/mpc85xx/mpc85xx_gpio.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/resource.h>
40 #include <sys/rman.h>
41 #include <sys/gpio.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
46 
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/gpio/gpiobusvar.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include "gpio_if.h"
53 
54 #define MAXPIN		(7)
55 
56 #define VALID_PIN(u)	((u) >= 0 && (u) <= MAXPIN)
57 
58 #define GPIO_LOCK(sc)			mtx_lock(&(sc)->sc_mtx)
59 #define	GPIO_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
60 #define GPIO_LOCK_INIT(sc) \
61 	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev),	\
62 	    "gpio", MTX_DEF)
63 #define GPIO_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
64 
65 struct mpc85xx_gpio_softc {
66 	device_t	dev;
67 	device_t	busdev;
68 	struct mtx	sc_mtx;
69 	struct resource *out_res;	/* Memory resource */
70 	struct resource *in_res;
71 };
72 
73 static device_t
74 mpc85xx_gpio_get_bus(device_t dev)
75 {
76 	struct mpc85xx_gpio_softc *sc;
77 
78 	sc = device_get_softc(dev);
79 
80 	return (sc->busdev);
81 }
82 
83 static int
84 mpc85xx_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
93 mpc85xx_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);
100 
101 	return (0);
102 }
103 
104 /* Get a specific pin's name. */
105 static int
106 mpc85xx_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, "GPIO%d", pin);
113 	name[GPIOMAXNAME-1] = '\0';
114 
115 	return (0);
116 }
117 
118 /* Set a specific output pin's value. */
119 static int
120 mpc85xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
121 {
122 	struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
123 	uint32_t outvals;
124 	uint8_t pinbit;
125 
126 	if (!VALID_PIN(pin) || value > 1)
127 		return (EINVAL);
128 
129 	GPIO_LOCK(sc);
130 	pinbit = 31 - pin;
131 
132 	outvals = bus_read_4(sc->out_res, 0);
133 	outvals &= ~(1 << pinbit);
134 	outvals |= (value << pinbit);
135 	bus_write_4(sc->out_res, 0, outvals);
136 
137 	GPIO_UNLOCK(sc);
138 
139 	return (0);
140 }
141 
142 /* Get a specific pin's input value. */
143 static int
144 mpc85xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
145 {
146 	struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
147 
148 	if (!VALID_PIN(pin))
149 		return (EINVAL);
150 
151 	*value = (bus_read_4(sc->in_res, 0) >> (31 - pin)) & 1;
152 
153 	return (0);
154 }
155 
156 /* Toggle a pin's output value. */
157 static int
158 mpc85xx_gpio_pin_toggle(device_t dev, uint32_t pin)
159 {
160 	struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
161 	uint32_t val;
162 
163 	if (!VALID_PIN(pin))
164 		return (EINVAL);
165 
166 	GPIO_LOCK(sc);
167 
168 	val = bus_read_4(sc->out_res, 0);
169 	val ^= (1 << (31 - pin));
170 	bus_write_4(sc->out_res, 0, val);
171 
172 	GPIO_UNLOCK(sc);
173 
174 	return (0);
175 }
176 
177 static int
178 mpc85xx_gpio_probe(device_t dev)
179 {
180 	uint32_t svr;
181 
182 	if (!ofw_bus_status_okay(dev))
183 		return (ENXIO);
184 
185 	if (!ofw_bus_is_compatible(dev, "gpio"))
186 		return (ENXIO);
187 
188 	svr = mfspr(SPR_SVR);
189 	switch (SVR_VER(svr)) {
190 	case SVR_MPC8533:
191 	case SVR_MPC8533E:
192 		break;
193 	default:
194 		return (ENXIO);
195 	}
196 
197 	device_set_desc(dev, "MPC85xx GPIO driver");
198 	return (0);
199 }
200 
201 static int mpc85xx_gpio_detach(device_t dev);
202 
203 static int
204 mpc85xx_gpio_attach(device_t dev)
205 {
206 	struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
207 	int rid;
208 
209 	sc->dev = dev;
210 
211 	GPIO_LOCK_INIT(sc);
212 
213 	/* Allocate memory. */
214 	rid = 0;
215 	sc->out_res = bus_alloc_resource_any(dev,
216 		     SYS_RES_MEMORY, &rid, RF_ACTIVE);
217 	if (sc->out_res == NULL) {
218 		device_printf(dev, "Can't allocate memory for device output port");
219 		mpc85xx_gpio_detach(dev);
220 		return (ENOMEM);
221 	}
222 
223 	rid = 1;
224 	sc->in_res = bus_alloc_resource_any(dev,
225 		     SYS_RES_MEMORY, &rid, RF_ACTIVE);
226 	if (sc->in_res == NULL) {
227 		device_printf(dev, "Can't allocate memory for device input port");
228 		mpc85xx_gpio_detach(dev);
229 		return (ENOMEM);
230 	}
231 
232 	sc->busdev = gpiobus_attach_bus(dev);
233 	if (sc->busdev == NULL) {
234 		mpc85xx_gpio_detach(dev);
235 		return (ENOMEM);
236 	}
237 
238 	OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
239 
240 	return (0);
241 }
242 
243 static int
244 mpc85xx_gpio_detach(device_t dev)
245 {
246 	struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
247 
248 	gpiobus_detach_bus(dev);
249 
250 	if (sc->out_res != NULL) {
251 		/* Release output port resource. */
252 		bus_release_resource(dev, SYS_RES_MEMORY,
253 				     rman_get_rid(sc->out_res), sc->out_res);
254 	}
255 
256 	if (sc->in_res != NULL) {
257 		/* Release input port resource. */
258 		bus_release_resource(dev, SYS_RES_MEMORY,
259 				     rman_get_rid(sc->in_res), sc->in_res);
260 	}
261 
262 	GPIO_LOCK_DESTROY(sc);
263 
264 	return (0);
265 }
266 
267 static device_method_t mpc85xx_gpio_methods[] = {
268 	/* device_if */
269 	DEVMETHOD(device_probe, 	mpc85xx_gpio_probe),
270 	DEVMETHOD(device_attach, 	mpc85xx_gpio_attach),
271 	DEVMETHOD(device_detach, 	mpc85xx_gpio_detach),
272 
273 	/* GPIO protocol */
274 	DEVMETHOD(gpio_get_bus, 	mpc85xx_gpio_get_bus),
275 	DEVMETHOD(gpio_pin_max, 	mpc85xx_gpio_pin_max),
276 	DEVMETHOD(gpio_pin_getname, 	mpc85xx_gpio_pin_getname),
277 	DEVMETHOD(gpio_pin_getcaps, 	mpc85xx_gpio_pin_getcaps),
278 	DEVMETHOD(gpio_pin_get, 	mpc85xx_gpio_pin_get),
279 	DEVMETHOD(gpio_pin_set, 	mpc85xx_gpio_pin_set),
280 	DEVMETHOD(gpio_pin_toggle, 	mpc85xx_gpio_pin_toggle),
281 
282 	DEVMETHOD_END
283 };
284 
285 static driver_t mpc85xx_gpio_driver = {
286 	"gpio",
287 	mpc85xx_gpio_methods,
288 	sizeof(struct mpc85xx_gpio_softc),
289 };
290 
291 EARLY_DRIVER_MODULE(mpc85xx_gpio, simplebus, mpc85xx_gpio_driver, NULL, NULL,
292     BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
293