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