summaryrefslogtreecommitdiffstats
path: root/board/st-ericsson/u8500/gpio.c
blob: 96b2461b9c59329d6d7dbac8308f03ba317936dd (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
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (C) ST-Ericsson SA 2009
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <asm/arch/gpio.h>

static struct gpio_register *addr_gpio_register[] = {
	(void *)U8500_GPIO_0_BASE,
	(void *)U8500_GPIO_1_BASE,
	(void *)U8500_GPIO_2_BASE,
	(void *)U8500_GPIO_3_BASE,
	(void *)U8500_GPIO_4_BASE,
	(void *)U8500_GPIO_5_BASE,
	(void *)U8500_GPIO_6_BASE,
	(void *)U8500_GPIO_7_BASE,
	(void *)U8500_GPIO_8_BASE,
};

struct gpio_altfun_data altfun_table[] = {
	{
		.altfun = GPIO_ALT_I2C_0,
		.start = 147,
		.end = 148,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_I2C_1,
		.start = 16,
		.end = 17,
		.cont = 0,
		.type = GPIO_ALTF_B,
	},
	{
		.altfun = GPIO_ALT_I2C_2,
		.start = 10,
		.end = 11,
		.cont = 0,
		.type = GPIO_ALTF_B,
	},
	{
		.altfun = GPIO_ALT_I2C_3,
		.start = 229,
		.end = 230,
		.cont = 0,
		.type = GPIO_ALTF_C,
	},
	{
		.altfun = GPIO_ALT_UART_0_MODEM,
		.start = 0,
		.end = 3,
		.cont = 1,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_UART_0_MODEM,
		.start = 33,
		.end = 36,
		.cont = 0,
		.type = GPIO_ALTF_C,
	},
	{
		.altfun = GPIO_ALT_UART_1,
		.start = 4,
		.end = 7,
		.cont = 0,
		.type =
			GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_UART_2,
		.start = 18,
		.end = 19,
		.cont = 1,
		.type = GPIO_ALTF_B,
	},
	{
		.altfun = GPIO_ALT_UART_2,
		.start = 29,
		.end = 32,
		.cont = 0,
		.type = GPIO_ALTF_C,
	},
	{
		.altfun = GPIO_ALT_MSP_0,
		.start = 12,
		.end = 17,
		.cont = 1,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_MSP_0,
		.start = 21,
		.end = 21,
		.cont = 0,
		.type = GPIO_ALTF_B,
	},
	{
		.altfun = GPIO_ALT_MSP_1,
		.start = 33,
		.end = 36,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_MSP_2,
		.start = 192,
		.end = 196,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_LCD_PANEL,
		.start = 64,
		.end = 93,
		.cont = 1,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_LCD_PANEL,
		.start = 150,
		.end = 171,
		.cont = 0,
		.type = GPIO_ALTF_B,
	},
	{
		.altfun = GPIO_ALT_SD_CARD0,
		.start = 18,
		.end = 28,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_MM_CARD0,
		.start = 18,
		.end = 32,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_USB_OTG,
		.start = 256,
		.end = 267,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_EMMC,
		.start = 197,
		.end = 207,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
	{
		.altfun = GPIO_ALT_POP_EMMC,
		.start = 128,
		.end = 138,
		.cont = 0,
		.type = GPIO_ALTF_A,
	},
};

/*
 * Static Function declarations
 */
enum gpio_error gpio_setpinconfig(int pin_id, struct gpio_config *config)
{
	struct gpio_register *p_gpio_register =
	    addr_gpio_register[GPIO_BLOCK(pin_id)];
	u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);
	enum gpio_error error = GPIO_OK;
	u32 temp_reg;

	switch (config->mode) {
	case GPIO_ALTF_A:
		temp_reg = readl(&p_gpio_register->gpio_afsa);
		temp_reg |= mask;
		writel(temp_reg, &p_gpio_register->gpio_afsa);
		temp_reg = readl(&p_gpio_register->gpio_afsb);
		temp_reg &= ~mask;
		writel(temp_reg, &p_gpio_register->gpio_afsb);
		break;
	case GPIO_ALTF_B:
		temp_reg = readl(&p_gpio_register->gpio_afsa);
		temp_reg &= ~mask;
		writel(temp_reg, &p_gpio_register->gpio_afsa);
		temp_reg = readl(&p_gpio_register->gpio_afsb);
		temp_reg |= mask;
		writel(temp_reg, &p_gpio_register->gpio_afsb);
		break;
	case GPIO_ALTF_C:
		temp_reg = readl(&p_gpio_register->gpio_afsa);
		temp_reg |= mask;
		writel(temp_reg, &p_gpio_register->gpio_afsa);
		temp_reg = readl(&p_gpio_register->gpio_afsb);
		temp_reg |= mask;
		writel(temp_reg, &p_gpio_register->gpio_afsb);
		break;
	case GPIO_MODE_SOFTWARE:
		temp_reg = readl(&p_gpio_register->gpio_afsa);
		temp_reg &= ~mask;
		writel(temp_reg, &p_gpio_register->gpio_afsa);
		temp_reg = readl(&p_gpio_register->gpio_afsb);
		temp_reg &= ~mask;
		writel(temp_reg, &p_gpio_register->gpio_afsb);

		switch (config->direction) {
		case GPIO_DIR_INPUT:
			writel(mask, &p_gpio_register->gpio_dirc);
			break;
		case GPIO_DIR_OUTPUT:
			writel(mask, &p_gpio_register->gpio_dirs);
			break;
		case GPIO_DIR_LEAVE_UNCHANGED:
			break;
		default:
			return GPIO_INVALID_PARAMETER;
		}

		break;
	case GPIO_MODE_LEAVE_UNCHANGED:
		break;
	default:
		return GPIO_INVALID_PARAMETER;
	}
	return error;
}

enum gpio_error gpio_resetgpiopin(int pin_id, char *dev_name)
{
	struct gpio_register *p_gpio_register =
	    addr_gpio_register[GPIO_BLOCK(pin_id)];
	u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);
	enum gpio_error error = GPIO_OK;
	u32 temp_reg;

	temp_reg = readl(&p_gpio_register->gpio_afsa);
	temp_reg &= ~mask;
	writel(temp_reg, &p_gpio_register->gpio_afsa);
	temp_reg = readl(&p_gpio_register->gpio_afsb);
	temp_reg &= ~mask;
	writel(temp_reg, &p_gpio_register->gpio_afsb);
	writel(mask, &p_gpio_register->gpio_dirc);

	return error;
}

struct gpio_config altfun_pinconfig;
enum gpio_error gpio_altfunction(enum gpio_alt_function alt_func,
			    int which_altfunc, char *dev_name)
{
	int i, j, start, end;
	enum gpio_error error = -1;

	for (i = 0; i < ARRAY_SIZE(altfun_table); i++) {
		if (altfun_table[i].altfun != alt_func)
			continue;

		start = altfun_table[i].start;
		end = altfun_table[i].end;
		for (j = start; j <= end; j++) {
			if (which_altfunc == GPIO_ALTF_FIND)
				altfun_pinconfig.mode = altfun_table[i].type;
			else
				altfun_pinconfig.mode = which_altfunc;
			altfun_pinconfig.direction = GPIO_DIR_OUTPUT;
			altfun_pinconfig.dev_name = dev_name;

			if (which_altfunc != GPIO_ALTF_DISABLE)
				error = gpio_setpinconfig(j, &altfun_pinconfig);
			else
				error = gpio_resetgpiopin(j, dev_name);
			if (!error)
				continue;
			printf("GPIO %d configuration failure (nmdk_error:%d)",
				j, error);
			error = GPIO_INVALID_PARAMETER;
			return error;
		}

		if (!altfun_table[i].cont)
			break;
	}
	return error;
}

int gpio_writepin(int pin_id, enum gpio_data value, char *dev_name)
{
	struct gpio_register *p_gpio_register =
	    addr_gpio_register[GPIO_BLOCK(pin_id)];
	u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);

	switch (value) {
	case GPIO_DATA_HIGH:
		writel(mask, &p_gpio_register->gpio_dats);
		break;
	case GPIO_DATA_LOW:
		writel(mask, &p_gpio_register->gpio_datc);
		break;
	default:
		printf("Invalid value passed in %s", __FUNCTION__);
		return GPIO_INVALID_PARAMETER;
	}
	return GPIO_OK;
}

int gpio_readpin(int pin_id, enum gpio_data *rv)
{
	struct gpio_register *p_gpio_register =
	    addr_gpio_register[GPIO_BLOCK(pin_id)];
	u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);

	if ((readl(&p_gpio_register->gpio_dat) & mask) != 0)
		*rv = GPIO_DATA_HIGH;
	else
		*rv = GPIO_DATA_LOW;
	return GPIO_OK;
}

int gpio_altfuncenable(enum gpio_alt_function altfunc, char *dev_name)
{
	return (int)gpio_altfunction(altfunc, GPIO_ALTF_FIND, dev_name);
}

int gpio_altfuncdisable(enum gpio_alt_function altfunc, char *dev_name)
{
	return (int)gpio_altfunction(altfunc, GPIO_ALTF_DISABLE, dev_name);
}