summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/sys/arm/freescale/imx/imx_rtems_gpio.c
blob: c24732cc6274f2e36ee6248d62f4da325d34cebe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <machine/rtems-bsd-kernel-space.h>
/* SPDX-License-Identifier: BSD-2-Clause */

/*
 * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <bsp.h>
#if defined(LIBBSP_ARM_IMX_BSP_H)

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/gpio.h>

#include <dev/fdt/fdt_common.h>
#include <dev/gpio/gpiobusvar.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <bsp/imx-gpio.h>

#define MAXPIN		(32)

#define VALID_PIN(u)	((u) >= 0 && (u) <= MAXPIN)

struct imx_rtems_gpio_softc {
	device_t		dev;
	device_t		busdev;
	struct imx_gpio		*imx_gpio;
	struct {
		struct imx_gpio_pin	pin;
		bool			initialized;
		uint32_t		flags;
	} pin[MAXPIN];
};

static device_t
imx_rtems_gpio_get_bus(device_t dev)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);

	return (sc->busdev);
}

static int
imx_rtems_gpio_pin_max(device_t dev, int *maxpin)
{

	*maxpin = MAXPIN;
	return (0);
}

static int
imx_rtems_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
{

	if (!VALID_PIN(pin))
		return (EINVAL);

	*caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);

	return (0);
}

/* Get a specific pin's name. */
static int
imx_rtems_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);

	if (!VALID_PIN(pin))
		return (EINVAL);

	snprintf(name, GPIOMAXNAME, "%s_%d",
	    imx_gpio_get_name(sc->imx_gpio), pin);
	name[GPIOMAXNAME-1] = '\0';

	return (0);
}

static void
imx_rtems_gpio_pin_init(struct imx_rtems_gpio_softc *sc, uint32_t pin)
{
	imx_gpio_init(&sc->pin[pin].pin);
	sc->pin[pin].initialized = true;
}

static int
imx_rtems_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);

	if (!VALID_PIN(pin) || value > 1)
		return (EINVAL);

	if (!sc->pin[pin].initialized) {
		imx_rtems_gpio_pin_init(sc, pin);
	}

	imx_gpio_set_output(&sc->pin[pin].pin, value);

	return (0);
}

static int
imx_rtems_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);

	if (!VALID_PIN(pin))
		return (EINVAL);

	if (!sc->pin[pin].initialized) {
		imx_rtems_gpio_pin_init(sc, pin);
	}

	*value = imx_gpio_get_input(&sc->pin[pin].pin);

	return (0);
}

static int
imx_rtems_gpio_pin_toggle(device_t dev, uint32_t pin)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);

	if (!VALID_PIN(pin))
		return (EINVAL);

	if (!sc->pin[pin].initialized) {
		imx_rtems_gpio_pin_init(sc, pin);
	}

	imx_gpio_toggle_output(&sc->pin[pin].pin);

	return (0);
}

static int
imx_rtems_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);

	if (!VALID_PIN(pin))
		return (EINVAL);

	*flags = sc->pin[pin].flags;

	return (0);
}

static int
imx_rtems_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);

	if (!VALID_PIN(pin))
		return (EINVAL);

	if (flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) {
		if (flags & GPIO_PIN_PRESET_LOW) {
			imx_gpio_set_output(&sc->pin[pin].pin, 0);
		} else if (flags & GPIO_PIN_PRESET_HIGH) {
			imx_gpio_set_output(&sc->pin[pin].pin, 1);
		}
		if (flags & GPIO_PIN_OUTPUT) {
			sc->pin[pin].pin.mode = IMX_GPIO_MODE_OUTPUT;
		} else {
			sc->pin[pin].pin.mode = IMX_GPIO_MODE_INPUT;
		}
		imx_rtems_gpio_pin_init(sc, pin);
		sc->pin[pin].flags = flags;
	} else {
		return (EINVAL);
	}

	return (0);
}

static struct ofw_compat_data compat_data[] = {
	{"fsl,imx35-gpio",  1},
	{"fsl,imx7d-gpio",  1},
	{NULL,              0}
};

static int
imx_rtems_gpio_probe(device_t dev)
{
	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
		device_set_desc(dev, "Freescale i.MX GPIO Controller (RTEMS)");
		return (BUS_PROBE_DEFAULT);
	}
	
	return (ENXIO);
}

static int
imx_rtems_gpio_attach(device_t dev)
{
	struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
	u_long base, size;
	size_t i;

	sc->dev = dev;

	/* Get register address */
	if (fdt_regsize(ofw_bus_get_node(sc->dev), &base, &size) != 0) {
		device_printf(dev, "Can't get register address");
		return (EINVAL);
	}

	sc->imx_gpio = imx_gpio_get_by_register((void *)base);
	if (sc->imx_gpio == NULL) {
		device_printf(dev, "Can't get RTEMS imx_gpio handle");
		return (EINVAL);
	}

	memset(sc->pin, 0, sizeof(sc->pin));
	for (i = 0; i < MAXPIN; ++i) {
		sc->pin[i].pin.gpio = sc->imx_gpio;
		sc->pin[i].pin.mask = 1 << i;
		sc->pin[i].pin.shift = i;
		sc->pin[i].pin.mode = IMX_GPIO_MODE_INPUT;
	}
	/*
	 * Do _not_ initialize the pins here. Otherwise we would overwrite prior
	 * initializations done by other application parts.
	 */

	sc->busdev = gpiobus_attach_bus(dev);
	if (sc->busdev == NULL) {
		return (ENOMEM);
	}

	return (0);
}

static int
imx_rtems_gpio_detach(device_t dev)
{
	gpiobus_detach_bus(dev);

	return (0);
}

static device_method_t imx_rtems_gpio_methods[] = {
	/* device_if */
	DEVMETHOD(device_probe,		imx_rtems_gpio_probe),
	DEVMETHOD(device_attach,	imx_rtems_gpio_attach),
	DEVMETHOD(device_detach,	imx_rtems_gpio_detach),

	/* GPIO protocol */
	DEVMETHOD(gpio_get_bus,		imx_rtems_gpio_get_bus),
	DEVMETHOD(gpio_pin_max,		imx_rtems_gpio_pin_max),
	DEVMETHOD(gpio_pin_getname,	imx_rtems_gpio_pin_getname),
	DEVMETHOD(gpio_pin_getflags,	imx_rtems_gpio_pin_getflags),
	DEVMETHOD(gpio_pin_getcaps,	imx_rtems_gpio_pin_getcaps),
	DEVMETHOD(gpio_pin_get,		imx_rtems_gpio_pin_get),
	DEVMETHOD(gpio_pin_setflags,	imx_rtems_gpio_pin_setflags),
	DEVMETHOD(gpio_pin_set,		imx_rtems_gpio_pin_set),
	DEVMETHOD(gpio_pin_toggle,	imx_rtems_gpio_pin_toggle),

	DEVMETHOD_END
};

static driver_t imx_rtems_gpio_driver = {
	"gpio",
	imx_rtems_gpio_methods,
	sizeof(struct imx_rtems_gpio_softc),
};
static devclass_t imx_rtems_gpio_devclass;

EARLY_DRIVER_MODULE(imx_rtems_gpio, simplebus, imx_rtems_gpio_driver,
    imx_rtems_gpio_devclass, NULL, NULL,
    BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);

#endif /* LIBBSP_ARM_IMX_BSP_H */