aboutsummaryrefslogtreecommitdiff
path: root/sys/riscv/sifive/sifive_gpio.c
blob: 47d03ca448d56f7c567b8376e50a08c5d5650957 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2021 Jessica Clarke <jrtc27@FreeBSD.org>
 *
 * 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 AUTHOR 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 AUTHOR 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.
 *
 */

/* TODO: Provide interrupt controller interface */

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

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/gpio.h>

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

#include <machine/bus.h>

/* Registers are 32-bit so can only fit 32 pins */
#define	SFGPIO_MAX_PINS		32

#define	SFGPIO_DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)

#define	SFGPIO_INPUT_VAL	0x0
#define	SFGPIO_INPUT_EN		0x4
#define	SFGPIO_OUTPUT_EN	0x8
#define	SFGPIO_OUTPUT_VAL	0xc
#define	SFGPIO_RISE_IE		0x18
#define	SFGPIO_RISE_IP		0x1c
#define	SFGPIO_FALL_IE		0x20
#define	SFGPIO_FALL_IP		0x24
#define	SFGPIO_HIGH_IE		0x28
#define	SFGPIO_HIGH_IP		0x2c
#define	SFGPIO_LOW_IE		0x30
#define	SFGPIO_LOW_IP		0x34

struct sfgpio_softc {
	device_t	dev;
	device_t	busdev;
	struct mtx	mtx;
	struct resource	*mem_res;
	int		mem_rid;
	struct resource	*irq_res;
	int		irq_rid;
	int		npins;
	struct gpio_pin	gpio_pins[SFGPIO_MAX_PINS];
};

#define	SFGPIO_LOCK(_sc)	mtx_lock(&(_sc)->mtx)
#define	SFGPIO_UNLOCK(_sc)	mtx_unlock(&(_sc)->mtx)

#define	SFGPIO_READ(_sc, _off)		\
    bus_read_4((_sc)->mem_res, (_off))
#define	SFGPIO_WRITE(_sc, _off, _val)	\
    bus_write_4((_sc)->mem_res, (_off), (_val))

static struct ofw_compat_data compat_data[] = {
	{ "sifive,gpio0",	1 },
	{ NULL,			0 },
};

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

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
		return (ENXIO);

	device_set_desc(dev, "SiFive GPIO Controller");

	return (BUS_PROBE_DEFAULT);
}

static int
sfgpio_attach(device_t dev)
{
	struct sfgpio_softc *sc;
	phandle_t node;
	int error, i;
	pcell_t npins;
	uint32_t input_en, output_en;

	sc = device_get_softc(dev);
	sc->dev = dev;

	node = ofw_bus_get_node(dev);

	mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);

	sc->mem_rid = 0;
	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
	    &sc->mem_rid, RF_ACTIVE);
	if (sc->mem_res == NULL) {
		device_printf(dev, "Cannot allocate memory resource\n");
		error = ENXIO;
		goto fail;
	}

	if (OF_getencprop(node, "ngpios", &npins, sizeof(npins)) <= 0) {
		/* Optional; defaults to 16 */
		npins = 16;
	} else if (npins > SFGPIO_MAX_PINS) {
		device_printf(dev, "Too many pins: %d\n", npins);
		error = ENXIO;
		goto fail;
	}
	sc->npins = npins;

	sc->irq_rid = 0;
	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
	    RF_ACTIVE);
	if (sc->irq_res == NULL) {
		device_printf(dev, "Cannot allocate IRQ resource\n");
		error = ENXIO;
		goto fail;
	}

	input_en = SFGPIO_READ(sc, SFGPIO_INPUT_EN);
	output_en = SFGPIO_READ(sc, SFGPIO_OUTPUT_EN);
	for (i = 0; i < sc->npins; ++i) {
		sc->gpio_pins[i].gp_pin = i;
		sc->gpio_pins[i].gp_caps = SFGPIO_DEFAULT_CAPS;
		sc->gpio_pins[i].gp_flags =
		    ((input_en & (1u << i)) ? GPIO_PIN_INPUT : 0) |
		    ((output_en & (1u << i)) ? GPIO_PIN_OUTPUT : 0);
		snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME, "GPIO%d", i);
		sc->gpio_pins[i].gp_name[GPIOMAXNAME - 1] = '\0';
	}

	sc->busdev = gpiobus_attach_bus(dev);
	if (sc->busdev == NULL) {
		device_printf(dev, "Cannot attach gpiobus\n");
		error = ENXIO;
		goto fail;
	}

	return (0);

fail:
	if (sc->busdev != NULL)
		gpiobus_detach_bus(dev);
	if (sc->irq_res != NULL)
		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
		    sc->irq_res);
	if (sc->mem_res != NULL)
		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
		    sc->mem_res);
	mtx_destroy(&sc->mtx);
	return (error);
}

static device_t
sfgpio_get_bus(device_t dev)
{
	struct sfgpio_softc *sc;

	sc = device_get_softc(dev);

	return (sc->busdev);
}

static int
sfgpio_pin_max(device_t dev, int *maxpin)
{
	struct sfgpio_softc *sc;

	sc = device_get_softc(dev);

	*maxpin = sc->npins - 1;

	return (0);
}

static int
sfgpio_pin_set(device_t dev, uint32_t pin, unsigned int val)
{
	struct sfgpio_softc *sc;
	uint32_t reg;

	sc = device_get_softc(dev);

	if (pin >= sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);
	reg = SFGPIO_READ(sc, SFGPIO_OUTPUT_VAL);
	if (val)
		reg |= (1u << pin);
	else
		reg &= ~(1u << pin);
	SFGPIO_WRITE(sc, SFGPIO_OUTPUT_VAL, reg);
	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
{
	struct sfgpio_softc *sc;
	uint32_t reg;

	sc = device_get_softc(dev);

	if (pin >= sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);
	if (sc->gpio_pins[pin].gp_flags & GPIO_PIN_OUTPUT)
		reg = SFGPIO_READ(sc, SFGPIO_OUTPUT_VAL);
	else
		reg = SFGPIO_READ(sc, SFGPIO_INPUT_VAL);
	*val = (reg & (1u << pin)) ? 1 : 0;
	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_toggle(device_t dev, uint32_t pin)
{
	struct sfgpio_softc *sc;
	uint32_t reg;

	sc = device_get_softc(dev);

	if (pin >= sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);
	reg = SFGPIO_READ(sc, SFGPIO_OUTPUT_VAL);
	reg ^= (1u << pin);
	SFGPIO_WRITE(sc, SFGPIO_OUTPUT_VAL, reg);
	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
{
	struct sfgpio_softc *sc;

	sc = device_get_softc(dev);

	if (pin >= sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);
	*caps = sc->gpio_pins[pin].gp_caps;
	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
{
	struct sfgpio_softc *sc;

	sc = device_get_softc(dev);

	if (pin >= sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);
	*flags = sc->gpio_pins[pin].gp_flags;
	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_getname(device_t dev, uint32_t pin, char *name)
{
	struct sfgpio_softc *sc;

	sc = device_get_softc(dev);

	if (pin >= sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);
	memcpy(name, sc->gpio_pins[pin].gp_name, GPIOMAXNAME);
	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
{
	struct sfgpio_softc *sc;
	uint32_t reg;

	sc = device_get_softc(dev);

	if (pin >= sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);

	reg = SFGPIO_READ(sc, SFGPIO_INPUT_EN);
	if (flags & GPIO_PIN_INPUT) {
		reg |= (1u << pin);
		sc->gpio_pins[pin].gp_flags |= GPIO_PIN_INPUT;
	} else {
		reg &= ~(1u << pin);
		sc->gpio_pins[pin].gp_flags &= ~GPIO_PIN_INPUT;
	}
	SFGPIO_WRITE(sc, SFGPIO_INPUT_EN, reg);

	reg = SFGPIO_READ(sc, SFGPIO_OUTPUT_EN);
	if (flags & GPIO_PIN_OUTPUT) {
		reg |= (1u << pin);
		sc->gpio_pins[pin].gp_flags |= GPIO_PIN_OUTPUT;
	} else {
		reg &= ~(1u << pin);
		sc->gpio_pins[pin].gp_flags &= ~GPIO_PIN_OUTPUT;
	}
	SFGPIO_WRITE(sc, SFGPIO_OUTPUT_EN, reg);

	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_access_32(device_t dev, uint32_t first_pin, uint32_t clear_pins,
    uint32_t change_pins, uint32_t *orig_pins)
{
	struct sfgpio_softc *sc;
	uint32_t reg;

	if (first_pin != 0)
		return (EINVAL);

	sc = device_get_softc(dev);

	SFGPIO_LOCK(sc);

	reg = SFGPIO_READ(sc, SFGPIO_OUTPUT_VAL);

	if (orig_pins != NULL)
		/* Only input_val is implicitly masked by input_en */
		*orig_pins = SFGPIO_READ(sc, SFGPIO_INPUT_VAL) |
		     (reg & SFGPIO_READ(sc, SFGPIO_OUTPUT_EN));

	if ((clear_pins | change_pins) != 0)
		SFGPIO_WRITE(sc, SFGPIO_OUTPUT_VAL,
		    (reg & ~clear_pins) ^ change_pins);

	SFGPIO_UNLOCK(sc);

	return (0);
}

static int
sfgpio_pin_config_32(device_t dev, uint32_t first_pin, uint32_t num_pins,
    uint32_t *pin_flags)
{
	struct sfgpio_softc *sc;
	uint32_t ireg, oreg;
	int i;

	sc = device_get_softc(dev);

	if (first_pin != 0 || num_pins > sc->npins)
		return (EINVAL);

	SFGPIO_LOCK(sc);

	ireg = SFGPIO_READ(sc, SFGPIO_INPUT_EN);
	oreg = SFGPIO_READ(sc, SFGPIO_OUTPUT_EN);
	for (i = 0; i < num_pins; ++i) {
		if (pin_flags[i] & GPIO_PIN_INPUT) {
			ireg |= (1u << i);
			oreg &= ~(1u << i);
			sc->gpio_pins[i].gp_flags |= GPIO_PIN_INPUT;
			sc->gpio_pins[i].gp_flags &= ~GPIO_PIN_OUTPUT;
		} else if (pin_flags[i] & GPIO_PIN_OUTPUT) {
			ireg &= ~(1u << i);
			oreg |= (1u << i);
			sc->gpio_pins[i].gp_flags &= ~GPIO_PIN_INPUT;
			sc->gpio_pins[i].gp_flags |= GPIO_PIN_OUTPUT;
		}
	}
	SFGPIO_WRITE(sc, SFGPIO_INPUT_EN, ireg);
	SFGPIO_WRITE(sc, SFGPIO_OUTPUT_EN, oreg);

	SFGPIO_UNLOCK(sc);

	return (0);
}

static phandle_t
sfgpio_get_node(device_t bus, device_t dev)
{
	return (ofw_bus_get_node(bus));
}

static device_method_t sfgpio_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		sfgpio_probe),
	DEVMETHOD(device_attach,	sfgpio_attach),

	/* GPIO protocol */
	DEVMETHOD(gpio_get_bus,		sfgpio_get_bus),
	DEVMETHOD(gpio_pin_max,		sfgpio_pin_max),
	DEVMETHOD(gpio_pin_set,		sfgpio_pin_set),
	DEVMETHOD(gpio_pin_get,		sfgpio_pin_get),
	DEVMETHOD(gpio_pin_toggle,	sfgpio_pin_toggle),
	DEVMETHOD(gpio_pin_getcaps,	sfgpio_pin_getcaps),
	DEVMETHOD(gpio_pin_getflags,	sfgpio_pin_getflags),
	DEVMETHOD(gpio_pin_getname,	sfgpio_pin_getname),
	DEVMETHOD(gpio_pin_setflags,	sfgpio_pin_setflags),
	DEVMETHOD(gpio_pin_access_32,	sfgpio_pin_access_32),
	DEVMETHOD(gpio_pin_config_32,	sfgpio_pin_config_32),

	/* ofw_bus interface */
	DEVMETHOD(ofw_bus_get_node,	sfgpio_get_node),

	DEVMETHOD_END
};

static devclass_t sfgpio_devclass;
DEFINE_CLASS_0(gpio, sfgpio_driver, sfgpio_methods,
    sizeof(struct sfgpio_softc));
EARLY_DRIVER_MODULE(gpio, simplebus, sfgpio_driver, sfgpio_devclass, 0, 0,
    BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
MODULE_DEPEND(sfgpio, gpiobus, 1, 1, 1);