vf_gpio.c revision 261410
1/*-
2 * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
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/*
28 * Vybrid Family General-Purpose Input/Output (GPIO)
29 * Chapter 7, Vybrid Reference Manual, Rev. 5, 07/2013
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/arm/freescale/vybrid/vf_gpio.c 261410 2014-02-02 19:17:28Z ian $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/bus.h>
38#include <sys/kernel.h>
39#include <sys/module.h>
40#include <sys/malloc.h>
41#include <sys/rman.h>
42#include <sys/timeet.h>
43#include <sys/timetc.h>
44#include <sys/watchdog.h>
45#include <sys/mutex.h>
46#include <sys/gpio.h>
47
48#include <dev/fdt/fdt_common.h>
49#include <dev/ofw/openfirm.h>
50#include <dev/ofw/ofw_bus.h>
51#include <dev/ofw/ofw_bus_subr.h>
52
53#include <machine/bus.h>
54#include <machine/fdt.h>
55#include <machine/cpu.h>
56#include <machine/intr.h>
57
58#include "gpio_if.h"
59
60#include <arm/freescale/vybrid/vf_common.h>
61
62#define	GPIO_PDOR(n)	(0x00 + 0x40 * (n >> 5))
63#define	GPIO_PSOR(n)	(0x04 + 0x40 * (n >> 5))
64#define	GPIO_PCOR(n)	(0x08 + 0x40 * (n >> 5))
65#define	GPIO_PTOR(n)	(0x0C + 0x40 * (n >> 5))
66#define	GPIO_PDIR(n)	(0x10 + 0x40 * (n >> 5))
67
68#define	GPIO_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
69#define	GPIO_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
70
71#define	NPORTS		5
72#define	NGPIO		(NPORTS * 32)
73#define	DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
74
75/*
76 * GPIO interface
77 */
78static int vf_gpio_pin_max(device_t, int *);
79static int vf_gpio_pin_getcaps(device_t, uint32_t, uint32_t *);
80static int vf_gpio_pin_getname(device_t, uint32_t, char *);
81static int vf_gpio_pin_getflags(device_t, uint32_t, uint32_t *);
82static int vf_gpio_pin_setflags(device_t, uint32_t, uint32_t);
83static int vf_gpio_pin_set(device_t, uint32_t, unsigned int);
84static int vf_gpio_pin_get(device_t, uint32_t, unsigned int *);
85static int vf_gpio_pin_toggle(device_t, uint32_t pin);
86
87struct vf_gpio_softc {
88	struct resource		*res[6];
89	bus_space_tag_t		bst;
90	bus_space_handle_t	bsh;
91
92	struct mtx		sc_mtx;
93	int			gpio_npins;
94	struct gpio_pin		gpio_pins[NGPIO];
95	void			*gpio_ih[NPORTS];
96};
97
98struct vf_gpio_softc *gpio_sc;
99
100static struct resource_spec vf_gpio_spec[] = {
101	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
102	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
103	{ SYS_RES_IRQ,		1,	RF_ACTIVE },
104	{ SYS_RES_IRQ,		2,	RF_ACTIVE },
105	{ SYS_RES_IRQ,		3,	RF_ACTIVE },
106	{ SYS_RES_IRQ,		4,	RF_ACTIVE },
107	{ -1, 0 }
108};
109
110static int
111vf_gpio_intr(void *arg)
112{
113	struct vf_gpio_softc *sc;
114	sc = arg;
115
116	/* TODO: interrupt handling */
117
118	return (FILTER_HANDLED);
119}
120
121
122static int
123vf_gpio_probe(device_t dev)
124{
125
126	if (!ofw_bus_status_okay(dev))
127		return (ENXIO);
128
129	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-gpio"))
130		return (ENXIO);
131
132	device_set_desc(dev, "Vybrid Family GPIO Unit");
133	return (BUS_PROBE_DEFAULT);
134}
135
136static int
137vf_gpio_attach(device_t dev)
138{
139	struct vf_gpio_softc *sc;
140	int irq, i;
141
142	sc = device_get_softc(dev);
143	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
144
145	if (bus_alloc_resources(dev, vf_gpio_spec, sc->res)) {
146		device_printf(dev, "could not allocate resources\n");
147		return (ENXIO);
148	}
149
150	gpio_sc = sc;
151
152	/* Memory interface */
153	sc->bst = rman_get_bustag(sc->res[0]);
154	sc->bsh = rman_get_bushandle(sc->res[0]);
155
156	sc->gpio_npins = NGPIO;
157
158	for (irq = 0; irq < NPORTS; irq ++) {
159		if ((bus_setup_intr(dev, sc->res[1 + irq], INTR_TYPE_MISC,
160		    vf_gpio_intr, NULL, sc, &sc->gpio_ih[irq]))) {
161			device_printf(dev,
162			    "WARNING: unable to register interrupt handler\n");
163			return (ENXIO);
164		}
165	}
166
167	for (i = 0; i < sc->gpio_npins; i++) {
168		sc->gpio_pins[i].gp_pin = i;
169		sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
170		sc->gpio_pins[i].gp_flags =
171		    (READ4(sc, GPIO_PDOR(i)) & (1 << (i % 32))) ?
172		    GPIO_PIN_OUTPUT: GPIO_PIN_INPUT;
173		snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME,
174		    "vf_gpio%d.%d", device_get_unit(dev), i);
175	}
176
177	device_add_child(dev, "gpioc", device_get_unit(dev));
178	device_add_child(dev, "gpiobus", device_get_unit(dev));
179
180	return (bus_generic_attach(dev));
181}
182
183static int
184vf_gpio_pin_max(device_t dev, int *maxpin)
185{
186
187	*maxpin = NGPIO - 1;
188	return (0);
189}
190
191static int
192vf_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
193{
194	struct vf_gpio_softc *sc;
195	int i;
196
197	sc = device_get_softc(dev);
198	for (i = 0; i < sc->gpio_npins; i++) {
199		if (sc->gpio_pins[i].gp_pin == pin)
200			break;
201	}
202
203	if (i >= sc->gpio_npins)
204		return (EINVAL);
205
206	GPIO_LOCK(sc);
207	memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
208	GPIO_UNLOCK(sc);
209
210	return (0);
211}
212
213static int
214vf_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
215{
216	struct vf_gpio_softc *sc;
217	int i;
218
219	sc = device_get_softc(dev);
220	for (i = 0; i < sc->gpio_npins; i++) {
221		if (sc->gpio_pins[i].gp_pin == pin)
222			break;
223	}
224
225	if (i >= sc->gpio_npins)
226		return (EINVAL);
227
228	GPIO_LOCK(sc);
229	*caps = sc->gpio_pins[i].gp_caps;
230	GPIO_UNLOCK(sc);
231
232	return (0);
233}
234
235static int
236vf_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
237{
238	struct vf_gpio_softc *sc;
239	int i;
240
241	sc = device_get_softc(dev);
242	for (i = 0; i < sc->gpio_npins; i++) {
243		if (sc->gpio_pins[i].gp_pin == pin)
244			break;
245	}
246
247	if (i >= sc->gpio_npins)
248		return (EINVAL);
249
250	GPIO_LOCK(sc);
251	*flags = sc->gpio_pins[i].gp_flags;
252	GPIO_UNLOCK(sc);
253
254	return (0);
255}
256
257static int
258vf_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
259{
260	struct vf_gpio_softc *sc;
261	int i;
262
263	sc = device_get_softc(dev);
264	for (i = 0; i < sc->gpio_npins; i++) {
265		if (sc->gpio_pins[i].gp_pin == pin)
266			break;
267	}
268
269	if (i >= sc->gpio_npins)
270		return (EINVAL);
271
272	GPIO_LOCK(sc);
273	*val = (READ4(sc, GPIO_PDOR(i)) & (1 << (i % 32)));
274	GPIO_UNLOCK(sc);
275
276	return (0);
277}
278
279static int
280vf_gpio_pin_toggle(device_t dev, uint32_t pin)
281{
282	struct vf_gpio_softc *sc;
283	int i;
284
285	sc = device_get_softc(dev);
286	for (i = 0; i < sc->gpio_npins; i++) {
287		if (sc->gpio_pins[i].gp_pin == pin)
288			break;
289	}
290
291	if (i >= sc->gpio_npins)
292		return (EINVAL);
293
294	GPIO_LOCK(sc);
295	WRITE4(sc, GPIO_PTOR(i), (1 << (i % 32)));
296	GPIO_UNLOCK(sc);
297
298	return (0);
299}
300
301
302static void
303vf_gpio_pin_configure(struct vf_gpio_softc *sc, struct gpio_pin *pin,
304    unsigned int flags)
305{
306
307	GPIO_LOCK(sc);
308
309	/*
310	 * Manage input/output
311	 */
312	if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
313		pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
314		if (flags & GPIO_PIN_OUTPUT) {
315			pin->gp_flags |= GPIO_PIN_OUTPUT;
316
317		} else {
318			pin->gp_flags |= GPIO_PIN_INPUT;
319			WRITE4(sc, GPIO_PCOR(pin->gp_pin),
320			    (1 << (pin->gp_pin % 32)));
321		}
322	}
323
324	GPIO_UNLOCK(sc);
325}
326
327
328static int
329vf_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
330{
331	struct vf_gpio_softc *sc;
332	int i;
333
334	sc = device_get_softc(dev);
335	for (i = 0; i < sc->gpio_npins; i++) {
336		if (sc->gpio_pins[i].gp_pin == pin)
337			break;
338	}
339
340	if (i >= sc->gpio_npins)
341		return (EINVAL);
342
343	/* Check for unwanted flags. */
344	if ((flags & sc->gpio_pins[i].gp_caps) != flags)
345		return (EINVAL);
346
347	/* Can't mix input/output together */
348	if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) ==
349	    (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT))
350		return (EINVAL);
351
352	vf_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
353
354	return (0);
355}
356
357static int
358vf_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
359{
360	struct vf_gpio_softc *sc;
361	int i;
362
363	sc = device_get_softc(dev);
364	for (i = 0; i < sc->gpio_npins; i++) {
365		if (sc->gpio_pins[i].gp_pin == pin)
366			break;
367	}
368
369	if (i >= sc->gpio_npins)
370		return (EINVAL);
371
372	GPIO_LOCK(sc);
373	if (value)
374		WRITE4(sc, GPIO_PSOR(i), (1 << (i % 32)));
375	else
376		WRITE4(sc, GPIO_PCOR(i), (1 << (i % 32)));
377	GPIO_UNLOCK(sc);
378
379	return (0);
380}
381
382static device_method_t vf_gpio_methods[] = {
383	DEVMETHOD(device_probe,		vf_gpio_probe),
384	DEVMETHOD(device_attach,	vf_gpio_attach),
385
386	/* GPIO protocol */
387	DEVMETHOD(gpio_pin_max,		vf_gpio_pin_max),
388	DEVMETHOD(gpio_pin_getname,	vf_gpio_pin_getname),
389	DEVMETHOD(gpio_pin_getcaps,	vf_gpio_pin_getcaps),
390	DEVMETHOD(gpio_pin_getflags,	vf_gpio_pin_getflags),
391	DEVMETHOD(gpio_pin_get,		vf_gpio_pin_get),
392	DEVMETHOD(gpio_pin_toggle,	vf_gpio_pin_toggle),
393	DEVMETHOD(gpio_pin_setflags,	vf_gpio_pin_setflags),
394	DEVMETHOD(gpio_pin_set,		vf_gpio_pin_set),
395	{ 0, 0 }
396};
397
398static driver_t vf_gpio_driver = {
399	"gpio",
400	vf_gpio_methods,
401	sizeof(struct vf_gpio_softc),
402};
403
404static devclass_t vf_gpio_devclass;
405
406DRIVER_MODULE(vf_gpio, simplebus, vf_gpio_driver, vf_gpio_devclass, 0, 0);
407