summaryrefslogtreecommitdiff
path: root/sys/arch/armv7/exynos/exgpio.c
blob: f7677708c704fd4bdb4651e0e8c17d234d17114c (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
/* $OpenBSD: exgpio.c,v 1.2 2015/05/27 00:06:14 jsg Exp $ */
/*
 * Copyright (c) 2007,2009 Dale Rahn <drahn@openbsd.org>
 * Copyright (c) 2012-2013 Patrick Wildt <patrick@blueri.se>
 *
 * 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 "fdt.h"

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

#include <arm/cpufunc.h>

#include <machine/bus.h>
#if NFDT > 0
#include <machine/fdt.h>
#endif
#include <machine/intr.h>

#include <armv7/armv7/armv7var.h>
#include <armv7/exynos/exgpiovar.h>

/* Exynos5 registers */
#define GPIO_BANK_SIZE		0x20
#define GPIO_BANK(x)		(GPIO_BANK_SIZE * ((x) / 8))
#define GPIO_CON(x)		(GPIO_BANK(x) + 0x00)
#define GPIO_DAT(x)		(GPIO_BANK(x) + 0x04)
#define GPIO_PULL(x)		(GPIO_BANK(x) + 0x08)
#define GPIO_DRV(x)		(GPIO_BANK(x) + 0x0c)
#define GPIO_PDN_CON(x)		(GPIO_BANK(x) + 0x10)
#define GPIO_PDN_PULL(x)	(GPIO_BANK(x) + 0x14)

/* bits and bytes */
#define GPIO_PIN(x)		((x) % 8)
#define GPIO_CON_INPUT(x)	(0x0 << (GPIO_PIN(x) << 2))
#define GPIO_CON_OUTPUT(x)	(0x1 << (GPIO_PIN(x) << 2))
#define GPIO_CON_IRQ(x)		(0xf << (GPIO_PIN(x) << 2))
#define GPIO_CON_MASK(x)	(0xf << (GPIO_PIN(x) << 2))
#define GPIO_DAT_SET(x)		(0x1 << (GPIO_PIN(x) << 0))
#define GPIO_DAT_MASK(x)	(0x1 << (GPIO_PIN(x) << 0))
#define GPIO_PULL_NONE(x)	(0x0 << (GPIO_PIN(x) << 1))
#define GPIO_PULL_DOWN(x)	(0x1 << (GPIO_PIN(x) << 1))
#define GPIO_PULL_UP(x)		(0x3 << (GPIO_PIN(x) << 1))
#define GPIO_PULL_MASK(x)	(0x3 << (GPIO_PIN(x) << 1))
#define GPIO_DRV_1X(x)		(0x0 << (GPIO_PIN(x) << 1))
#define GPIO_DRV_2X(x)		(0x1 << (GPIO_PIN(x) << 1))
#define GPIO_DRV_3X(x)		(0x2 << (GPIO_PIN(x) << 1))
#define GPIO_DRV_4X(x)		(0x3 << (GPIO_PIN(x) << 1))
#define GPIO_DRV_MASK(x)	(0x3 << (GPIO_PIN(x) << 1))

#define GPIO_PINS_PER_BANK	8

struct exgpio_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	int			sc_ngpio;
	unsigned int (*sc_get_bit)(struct exgpio_softc *sc,
	    unsigned int gpio);
	void (*sc_set_bit)(struct exgpio_softc *sc,
	    unsigned int gpio);
	void (*sc_clear_bit)(struct exgpio_softc *sc,
	    unsigned int gpio);
	void (*sc_set_dir)(struct exgpio_softc *sc,
	    unsigned int gpio, unsigned int dir);
};

int exgpio_match(struct device *parent, void *v, void *aux);
void exgpio_attach(struct device *parent, struct device *self, void *args);

struct exgpio_softc *exgpio_pin_to_inst(unsigned int);
unsigned int exgpio_pin_to_offset(unsigned int);
unsigned int exgpio_v5_get_bit(struct exgpio_softc *, unsigned int);
void exgpio_v5_set_bit(struct exgpio_softc *, unsigned int);
void exgpio_v5_clear_bit(struct exgpio_softc *, unsigned int);
void exgpio_v5_set_dir(struct exgpio_softc *, unsigned int, unsigned int);
unsigned int exgpio_v5_get_dir(struct exgpio_softc *, unsigned int);


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

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

int
exgpio_match(struct device *parent, void *v, void *aux)
{
#if NFDT > 0
	struct armv7_attach_args *aa = aux;

	if (fdt_node_compatible("samsung,exynos5250-pinctrl", aa->aa_node))
		return 1;
#endif

	return 0;
}

void
exgpio_attach(struct device *parent, struct device *self, void *args)
{
	struct armv7_attach_args *aa = args;
	struct exgpio_softc *sc = (struct exgpio_softc *) self;
	struct armv7mem mem;

	sc->sc_iot = aa->aa_iot;
#if NFDT > 0
	if (aa->aa_node) {
		struct fdt_memory fdtmem;
		if (fdt_get_memory_address(aa->aa_node, 0, &fdtmem))
			panic("%s: could not extract memory data from FDT",
			    __func__);
		mem.addr = fdtmem.addr;
		mem.size = fdtmem.size;
	} else
#endif
	{
		mem.addr = aa->aa_dev->mem[0].addr;
		mem.size = aa->aa_dev->mem[0].size;
	}
	if (bus_space_map(sc->sc_iot, mem.addr, mem.size, 0, &sc->sc_ioh))
		panic("%s: bus_space_map failed!", __func__);

	sc->sc_ngpio = (mem.size / GPIO_BANK_SIZE) * GPIO_PINS_PER_BANK;

	sc->sc_get_bit  = exgpio_v5_get_bit;
	sc->sc_set_bit = exgpio_v5_set_bit;
	sc->sc_clear_bit = exgpio_v5_clear_bit;
	sc->sc_set_dir = exgpio_v5_set_dir;

	printf("\n");

	/* XXX - IRQ */
	/* XXX - SYSCONFIG */
	/* XXX - CTRL */
	/* XXX - DEBOUNCE */
}

struct exgpio_softc *
exgpio_pin_to_inst(unsigned int gpio)
{
	int i;

	for (i = 0; exgpio_cd.cd_devs[i] != NULL; i++)
	{
		struct exgpio_softc *sc = exgpio_cd.cd_devs[i];
		if (gpio < sc->sc_ngpio)
			return sc;
		else
			gpio -= sc->sc_ngpio;
	}

	return NULL;
}

unsigned int
exgpio_pin_to_offset(unsigned int gpio)
{
	int i;

	for (i = 0; exgpio_cd.cd_devs[i] != NULL; i++)
	{
		struct exgpio_softc *sc = exgpio_cd.cd_devs[i];
		if (gpio < sc->sc_ngpio)
			return gpio;
		else
			gpio -= sc->sc_ngpio;
	}

	return 0;
}

unsigned int
exgpio_get_bit(unsigned int gpio)
{
	struct exgpio_softc *sc = exgpio_pin_to_inst(gpio);

	return sc->sc_get_bit(sc, gpio);
}

void
exgpio_set_bit(unsigned int gpio)
{
	struct exgpio_softc *sc = exgpio_pin_to_inst(gpio);

	sc->sc_set_bit(sc, gpio);
}

void
exgpio_clear_bit(unsigned int gpio)
{
	struct exgpio_softc *sc = exgpio_pin_to_inst(gpio);

	sc->sc_clear_bit(sc, gpio);
}
void
exgpio_set_dir(unsigned int gpio, unsigned int dir)
{
	struct exgpio_softc *sc = exgpio_pin_to_inst(gpio);

	sc->sc_set_dir(sc, gpio, dir);
}

unsigned int
exgpio_v5_get_bit(struct exgpio_softc *sc, unsigned int gpio)
{
	u_int32_t val;

	gpio = exgpio_pin_to_offset(gpio);
	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPIO_DAT(gpio));

	return !!(val & GPIO_DAT_SET(gpio));
}

void
exgpio_v5_set_bit(struct exgpio_softc *sc, unsigned int gpio)
{
	u_int32_t val;

	gpio = exgpio_pin_to_offset(gpio);
	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPIO_DAT(gpio));

	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPIO_DAT(gpio),
		val | GPIO_DAT_SET(gpio));
}

void
exgpio_v5_clear_bit(struct exgpio_softc *sc, unsigned int gpio)
{
	u_int32_t val;

	gpio = exgpio_pin_to_offset(gpio);
	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPIO_DAT(gpio));

	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPIO_DAT(gpio),
		val & ~GPIO_DAT_MASK(gpio));
}

void
exgpio_v5_set_dir(struct exgpio_softc *sc, unsigned int gpio, unsigned int dir)
{
	int s;
	u_int32_t val;

	gpio = exgpio_pin_to_offset(gpio);
	s = splhigh();

	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPIO_CON(gpio));
	val &= ~GPIO_CON_OUTPUT(gpio);
	if (dir == EXGPIO_DIR_OUT)
		val |= GPIO_CON_OUTPUT(gpio);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPIO_CON(gpio), val);

	splx(s);
}

unsigned int
exgpio_v5_get_dir(struct exgpio_softc *sc, unsigned int gpio)
{
	int s;
	u_int32_t val;

	gpio = exgpio_pin_to_offset(gpio);
	s = splhigh();

	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPIO_CON(gpio));
	if (val & GPIO_CON_OUTPUT(gpio))
		val = EXGPIO_DIR_OUT;
	else
		val = EXGPIO_DIR_IN;

	splx(s);
	return val;
}