summaryrefslogtreecommitdiff
path: root/sys/arch/armv7/exynos/exgpio.c
blob: 678e76f6e81ebcec4f71ea5f7271f7e23c49dc37 (plain)
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
/* $OpenBSD: exgpio.c,v 1.8 2021/10/24 17:52:27 mpi Exp $ */
/*
 * Copyright (c) 2007,2009 Dale Rahn <drahn@openbsd.org>
 * Copyright (c) 2012-2013 Patrick Wildt <patrick@blueri.se>
 * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/fdt.h>

#define GPXCON(x)	((x) + 0x0000)
#define  GPXCON_INPUT	0
#define  GPXCON_OUTPUT	1
#define GPXDAT(x)	((x) + 0x0004)
#define GPXPUD(x)	((x) + 0x0008)
#define GPXDRV(x)	((x) + 0x000c)

#define GPX_NUM_PINS	8

#define HREAD4(sc, reg)							\
	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val)						\
	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))

struct exgpio_bank {
	const char name[8];
	bus_addr_t addr;
};

struct exgpio_controller {
	struct gpio_controller ec_gc;
	struct exgpio_bank *ec_bank;
	struct exgpio_softc *ec_sc;
};

struct exgpio_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;

	struct exgpio_bank	*sc_banks;
	int			sc_nbanks;
};

int exgpio_match(struct device *, void *, void *);
void exgpio_attach(struct device *, struct device *, void *);

const struct cfattach	exgpio_ca = {
	sizeof (struct exgpio_softc), exgpio_match, exgpio_attach
};

struct cfdriver exgpio_cd = {
	NULL, "exgpio", DV_DULL
};

/* Exynos 5420/5422 */
struct exgpio_bank exynos5420_banks[] = {
	/* Controller 0 */
	{ "gpy7", 0x0000 },
	{ "gpx0", 0x0c00 },
	{ "gpx1", 0x0c20 },
	{ "gpx2", 0x0c40 },
	{ "gpx3", 0x0c60 },

	/* Controller 1 */
	{ "gpc0", 0x0000 },
	{ "gpc1", 0x0020 },
	{ "gpc2", 0x0040 },
	{ "gpc3", 0x0060 },
	{ "gpc4", 0x0080 },
	{ "gpd1", 0x00a0 },
	{ "gpy0", 0x00c0 },
	{ "gpy1", 0x00e0 },
	{ "gpy2", 0x0100 },
	{ "gpy3", 0x0120 },
	{ "gpy4", 0x0140 },
	{ "gpy5", 0x0160 },
	{ "gpy6", 0x0180 },

	/* Controller 2 */
	{ "gpe0", 0x0000 },
	{ "gpe1", 0x0020 },
	{ "gpf0", 0x0040 },
	{ "gpf1", 0x0060 },
	{ "gpg0", 0x0080 },
	{ "gpg1", 0x00a0 },
	{ "gpg2", 0x00c0 },
	{ "gpj4", 0x00e0 },

	/* Controller 3 */
	{ "gpa0", 0x0000 },
	{ "gpa1", 0x0020 },
	{ "gpa2", 0x0040 },
	{ "gpb0", 0x0060 },
	{ "gpb1", 0x0080 },
	{ "gpb2", 0x00a0 },
	{ "gpb3", 0x00c0 },
	{ "gpb4", 0x00e0 },
	{ "gph0", 0x0100 },

	/* Controller 4 */
	{ "gpz", 0x0000 },
};

struct exgpio_bank *exgpio_bank(struct exgpio_softc *, const char *);
int	exgpio_pinctrl(uint32_t, void *);
void	exgpio_config_pin(void *, uint32_t *, int);
int	exgpio_get_pin(void *, uint32_t *);
void	exgpio_set_pin(void *, uint32_t *, int);

int
exgpio_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return OF_is_compatible(faa->fa_node, "samsung,exynos5420-pinctrl");
}

void
exgpio_attach(struct device *parent, struct device *self, void *aux)
{
	struct exgpio_softc *sc = (struct exgpio_softc *)self;
	struct fdt_attach_args *faa = aux;
	struct exgpio_controller *ec;
	struct exgpio_bank *bank;
	char name[8];
	int node;
	int len;

	sc->sc_iot = faa->fa_iot;

	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
		panic("%s: bus_space_map failed!", __func__);

	if (OF_is_compatible(faa->fa_node, "samsung,exynos5420-pinctrl")) {
		sc->sc_banks = exynos5420_banks;
		sc->sc_nbanks = nitems(exynos5420_banks);
	}

	KASSERT(sc->sc_banks);
	pinctrl_register(faa->fa_node, exgpio_pinctrl, sc);

	for (node = OF_child(faa->fa_node); node; node = OF_peer(node)) {
		if (OF_getproplen(node, "gpio-controller") < 0)
			continue;

		len = OF_getprop(node, "name", &name, sizeof(name));
		if (len <= 0 || len >= sizeof(name))
			continue;

		bank = exgpio_bank(sc, name);
		if (bank == NULL)
			continue;

		ec = malloc(sizeof(*ec), M_DEVBUF, M_WAITOK);
		ec->ec_bank = exgpio_bank(sc, name);
		ec->ec_sc = sc;
		ec->ec_gc.gc_node = node;
		ec->ec_gc.gc_cookie = ec;
		ec->ec_gc.gc_config_pin = exgpio_config_pin;
		ec->ec_gc.gc_get_pin = exgpio_get_pin;
		ec->ec_gc.gc_set_pin = exgpio_set_pin;
		gpio_controller_register(&ec->ec_gc);
	}

	printf("\n");
}

struct exgpio_bank *
exgpio_bank(struct exgpio_softc *sc, const char *name)
{
	int i;

	for (i = 0; i < sc->sc_nbanks; i++) {
		if (strcmp(name, sc->sc_banks[i].name) == 0)
			return &sc->sc_banks[i];
	}

	return NULL;
}

int
exgpio_pinctrl(uint32_t phandle, void *cookie)
{
	struct exgpio_softc *sc = cookie;
	char *pins, *bank_name, *pin_name;
	struct exgpio_bank *bank;
	uint32_t func, val, pud, drv;
	uint32_t reg;
	int node;
	int len;
	int pin;

	node = OF_getnodebyphandle(phandle);
	if (node == 0)
		return -1;

	len = OF_getproplen(node, "samsung,pins");
	if (len <= 0)
		return -1;

	pins = malloc(len, M_TEMP, M_WAITOK);
	OF_getprop(node, "samsung,pins", pins, len);

	func = OF_getpropint(node, "samsung,pin-function", 0);
	val = OF_getpropint(node, "samsung,pin-val", 0);
	pud = OF_getpropint(node, "samsung,pin-pud", 1);
	drv = OF_getpropint(node, "samsung,pin-drv", 0);

	bank_name = pins;
	while (bank_name < pins + len) {
		pin_name = strchr(bank_name, '-');
		if (pin_name == NULL)
			goto fail;
		*pin_name++ = 0;
		pin = *pin_name - '0';
		if (pin < 0 || pin >= GPX_NUM_PINS)
			goto fail;

		bank = exgpio_bank(sc, bank_name);
		if (bank == NULL)
			goto fail;

		reg = HREAD4(sc, GPXCON(bank->addr));
		reg &= ~(0xf << (pin * 4));
		reg |= (func << (pin * 4));
		HWRITE4(sc, GPXCON(bank->addr), reg);

		reg = HREAD4(sc, GPXDAT(bank->addr));
		if (val)
			reg |= (1 << pin);
		else
			reg &= ~(1 << pin);
		HWRITE4(sc, GPXDAT(bank->addr), reg);

		reg = HREAD4(sc, GPXPUD(bank->addr));
		reg &= ~(0x3 << (pin * 2));
		reg |= (pud << (pin * 2));
		HWRITE4(sc, GPXPUD(bank->addr), reg);

		reg = HREAD4(sc, GPXDRV(bank->addr));
		reg &= ~(0x3 << (pin * 2));
		reg |= (drv << (pin * 2));
		HWRITE4(sc, GPXDRV(bank->addr), reg);

		bank_name = pin_name + 2;
	}

	free(pins, M_TEMP, len);
	return 0;

fail:
	free(pins, M_TEMP, len);
	return -1;
}

void
exgpio_config_pin(void *cookie, uint32_t *cells, int config)
{
	struct exgpio_controller *ec = cookie;
	uint32_t pin = cells[0];
	uint32_t val;
	int func;

	if (pin >= GPX_NUM_PINS)
		return;

	func = (config & GPIO_CONFIG_OUTPUT) ? GPXCON_OUTPUT : GPXCON_INPUT;
	val = HREAD4(ec->ec_sc, GPXCON(ec->ec_bank->addr));
	val &= ~(0xf << (pin * 4));
	val |= (func << (pin * 4));
	HWRITE4(ec->ec_sc, GPXCON(ec->ec_bank->addr), val);
}

int
exgpio_get_pin(void *cookie, uint32_t *cells)
{
	struct exgpio_controller *ec = cookie;
	uint32_t pin = cells[0];
	uint32_t flags = cells[1];
	uint32_t reg;
	int val;

	if (pin >= GPX_NUM_PINS)
		return 0;

	reg = HREAD4(ec->ec_sc, GPXDAT(ec->ec_bank->addr));
	reg &= (1 << pin);
	val = (reg >> pin) & 1;
	if (flags & GPIO_ACTIVE_LOW)
		val = !val;
	return val;
}

void
exgpio_set_pin(void *cookie, uint32_t *cells, int val)
{
	struct exgpio_controller *ec = cookie;
	uint32_t pin = cells[0];
	uint32_t flags = cells[1];
	uint32_t reg;

	if (pin >= GPX_NUM_PINS)
		return;

	reg = HREAD4(ec->ec_sc, GPXDAT(ec->ec_bank->addr));
	if (flags & GPIO_ACTIVE_LOW)
		val = !val;
	if (val)
		reg |= (1 << pin);
	else
		reg &= ~(1 << pin);
	HWRITE4(ec->ec_sc, GPXDAT(ec->ec_bank->addr), reg);
}