summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-samsung/gpio-s3c64xx.c
blob: d70a8716c92595634d7e7cc48604e5061a3a6457 (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
/*
 * Copyright (C) 2012 Juergen Beisert
 *
 * This code bases partially on code from the Linux kernel:
 *
 * Copyright 2008 Openmoko, Inc.
 * Copyright 2008 Simtec Electronics
 *	http://armlinux.simtec.co.uk/
 *	Ben Dooks <ben@simtec.co.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <common.h>
#include <errno.h>
#include <io.h>
#include <gpio.h>
#include <mach/iomux.h>
#include <mach/s3c-iomap.h>

#define S3C_GPACON (S3C_GPIO_BASE)
#define S3C_GPADAT (S3C_GPIO_BASE + 0x04)
#define S3C_GPAPUD (S3C_GPIO_BASE + 0x08)

static const unsigned short group_offset[] = {
	0x000,	/* GPA */ /* 8 pins, 4 bit each */
	0x020,	/* GPB */ /* 7 pins, 4 bit each */
	0x040,	/* GPC */ /* 8 pins, 4 bit each */
	0x060,	/* GPD */ /* 5 pins, 4 bit each */
	0x080,	/* GPE */ /* 5 pins, 4 bit each */
	0x0a0,	/* GPF */ /* 16 pins, 2 bit each */
	0x0c0,	/* GPG */ /* 7 pins, 4 bit each */
	0x0e0,	/* GPH */ /* two registers, 8 + 2 pins, 4 bit each */
	0x100,	/* GPI */ /* 16 pins, 2 bit each */
	0x120,	/* GPJ */ /* 12 pins, 2 bit each */
	0x800,	/* GPK */ /* two registers, 8 + 8 pins, 4 bit each */
	0x810,	/* GPL */ /* two registers, 8 + 8 pins, 4 bit each */
	0x820,	/* GPM */ /* 6 pins, 4 bit each */
	0x830,	/* GPN */ /* 16 pins, 2 bit each */
	0x140,	/* GPO */ /* 16 pins, 2 bit each */
	0x160,	/* GPP */ /* 15 pins, 2 bit each */
	0x180,	/* GPQ */ /* 9 pins, 2 bit each */
};

void gpio_set_value(unsigned gpio, int value)
{
	unsigned group = GET_GROUP(gpio);
	unsigned bit = GET_BIT(gpio);
	unsigned offset;
	uint32_t reg;

	offset = group_offset[group];

	switch (group) {
	case 7:		/* GPH */
	case 10:	/* GPK */
	case 11:	/* GPL */
		offset += 4;
		break;
	}

	reg = readl(S3C_GPADAT + offset);
	reg &= ~(1 << bit);
	reg |= (!!value) << bit;
	writel(reg, S3C_GPADAT + offset);
}

int gpio_get_value(unsigned gpio)
{
	unsigned group = GET_GROUP(gpio);
	unsigned bit = GET_BIT(gpio);
	unsigned offset;
	uint32_t reg;

	offset = group_offset[group];

	switch (group) {
	case 7:		/* GPH */
	case 10:	/* GPK */
	case 11:	/* GPL */
		offset += 4;
		break;
	}

	/* value */
	reg = readl(S3C_GPADAT + offset);

	return !!(reg & (1 << bit));
}

static void gpio_direction_input_4b(unsigned offset, unsigned bit)
{
	uint32_t reg;

	if (bit > 31) {
		offset += 4;
		bit -= 32;
	}

	reg = readl(S3C_GPACON + offset) & ~(0xf << bit);
	writel(reg, S3C_GPACON + offset); /* b0000 means 'GPIO input' */
}

static void gpio_direction_input_2b(unsigned offset, unsigned bit)
{
	uint32_t reg;

	reg = readl(S3C_GPACON + offset) & ~(0x3 << bit);
	writel(reg, S3C_GPACON + offset); /* b00 means 'GPIO input' */
}

int gpio_direction_input(unsigned gpio)
{
	unsigned group = GET_GROUP(gpio);
	unsigned bit = GET_BIT(gpio);
	unsigned offset;

	offset = group_offset[group];

	switch (group) {
	case 5:		/* GPF */
	case 8:		/* GPI */
	case 9:		/* GPJ */
	case 13:	/* GPN */
	case 14:	/* GPO */
	case 15:	/* GPP */
	case 16:	/* GPQ */
		gpio_direction_input_2b(offset, bit << 1);
		break;
	default:
		gpio_direction_input_4b(offset, bit << 2);
	}

	return 0;
}

static void gpio_direction_output_4b(unsigned offset, unsigned bit)
{
	uint32_t reg;

	if (bit > 31) {
		offset += 4;
		bit -= 32;
	}

	reg = readl(S3C_GPACON + offset) & ~(0xf << bit);
	reg |= 0x1 << bit;
	writel(reg, S3C_GPACON + offset); /* b0001 means 'GPIO output' */
}

static void gpio_direction_output_2b(unsigned offset, unsigned bit)
{
	uint32_t reg;

	/* direction */
	reg = readl(S3C_GPACON + offset) & ~(0x3 << bit);
	reg |= 0x1 << bit;
	writel(reg, S3C_GPACON + offset);
}

int gpio_direction_output(unsigned gpio, int value)
{
	unsigned group = GET_GROUP(gpio);
	unsigned bit = GET_BIT(gpio);
	unsigned offset;

	gpio_set_value(gpio, value);

	offset = group_offset[group];
	switch (group) {
	case 5:		/* GPF */
	case 8:		/* GPI */
	case 9:		/* GPJ */
	case 13:	/* GPN */
	case 14:	/* GPO */
	case 15:	/* GPP */
	case 16:	/* GPQ */
		gpio_direction_output_2b(offset, bit << 1);
		break;
	default:
		gpio_direction_output_4b(offset, bit << 2);
	}

	return 0;
}

/* one register, 2 bits per function -> GPF, GPI, GPJ, GPN, GPO, GPP, GPQ */
static void s3c_d2pins(unsigned offset, unsigned pin_mode)
{
	unsigned bit = GET_BIT(pin_mode);
	unsigned func = GET_FUNC(pin_mode);
	unsigned reg;

	if (PUD_PRESENT(pin_mode)) {
		reg = readl(S3C_GPAPUD + offset);
		reg &= ~(PUD_MASK << bit);
		reg |= GET_PUD(pin_mode) << bit;
		writel(reg, S3C_GPAPUD + offset);
	}

	/* in the case of pin's function is GPIO it also sets up the direction */
	reg = readl(S3C_GPACON + offset) & ~(0x3 << bit);
	writel(reg | (func << bit), S3C_GPACON + offset);

	if (func == 1) { /* output? if yes, also set the initial value */
		reg = readl(S3C_GPADAT + offset) & ~(1 << (bit >> 1));
		reg |= GET_GPIOVAL(pin_mode) << (bit >> 1);
		writel(reg, S3C_GPADAT + offset);
	}
}

/* one register, 4 bits per function -> GPA, GPB, GPC, GPD, GPE, GPG, GPM */
static void s3c_d4pins(unsigned offset, unsigned pin_mode)
{
	unsigned bit = GET_BIT(pin_mode);
	unsigned func = GET_FUNC(pin_mode);
	unsigned reg;

	if (PUD_PRESENT(pin_mode)) {
		reg = readl(S3C_GPAPUD + offset);
		reg &= ~(PUD_MASK << (bit >> 1));
		reg |= GET_PUD(pin_mode) << (bit >> 1);
		writel(reg, S3C_GPAPUD + offset);
	}

	/* in the case of pin's function is GPIO it also sets up the direction */
	reg = readl(S3C_GPACON + offset) & ~(0xf << bit);
	writel(reg | (func << bit), S3C_GPACON + offset);

	if (func == 1) { /* output? if yes, also set the initial value */
		reg = readl(S3C_GPADAT + offset) & ~(1 << (bit >> 2));
		reg |= GET_GPIOVAL(pin_mode) << (bit >> 2);
		writel(reg, S3C_GPADAT + offset);
	}
}

/* two registers, 4 bits per pin -> GPH, GPK, GPL */
static void s3c_d42pins(unsigned offset, unsigned pin_mode)
{
	unsigned bit = GET_BIT(pin_mode);
	unsigned func = GET_FUNC(pin_mode);
	uint32_t reg;
	unsigned reg_offs = 0;

	if (PUD_PRESENT(pin_mode)) {
		reg = readl(S3C_GPAPUD + 4 + offset);
		reg &= ~(PUD_MASK << (bit >> 1));
		reg |= GET_PUD(pin_mode) << (bit >> 1);
		writel(reg, S3C_GPACON + 4 + offset);
	}

	if (bit > 31) {
		reg_offs = 4;
		bit -= 32;
	}

	/* in the case of pin's function is GPIO it also sets up the direction */
	reg = readl(S3C_GPACON + offset + reg_offs) & ~(0xf << bit);
	writel(reg | (func << bit), S3C_GPACON + offset + reg_offs);

	if (func == 1) { /* output? if yes, also set the initial value */
		reg = readl(S3C_GPADAT + 4 + offset) & ~(1 << (bit >> 2));
		reg |= GET_GPIOVAL(pin_mode) << (bit >> 2);
		writel(reg, S3C_GPADAT + 4 + offset);
	}
}

/* 'gpio_mode' must be one of the 'GP?_*' macros */
void s3c_gpio_mode(unsigned gpio_mode)
{
	unsigned group, offset;

	group = GET_GROUP(gpio_mode);
	offset = group_offset[group];

	switch (group) {
	case 5:		/* GPF */
	case 8:		/* GPI */
	case 9:		/* GPJ */
	case 13:	/* GPN */
	case 14:	/* GPO */
	case 15:	/* GPP */
	case 16:	/* GPQ */
		s3c_d2pins(offset, gpio_mode);
		break;
	case 7:		/* GPH */
	case 10:	/* GPK */
	case 11:	/* GPL */
		s3c_d42pins(offset, gpio_mode);
		break;
	default:
		s3c_d4pins(offset, gpio_mode);
	}
}