tegra_gpio.c 9.51 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
3
 * NVIDIA Tegra20 GPIO handling.
4
 *  (C) Copyright 2010-2012,2015
5
6
7
8
9
10
11
12
13
 *  NVIDIA Corporation <www.nvidia.com>
 */

/*
 * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
 * Tom Warren (twarren@nvidia.com)
 */

#include <common.h>
14
#include <dm.h>
15
#include <log.h>
16
17
18
#include <malloc.h>
#include <errno.h>
#include <fdtdec.h>
19
20
#include <asm/io.h>
#include <asm/bitops.h>
21
#include <asm/arch/tegra.h>
22
#include <asm/gpio.h>
23
#include <dm/device-internal.h>
24
#include <dt-bindings/gpio/gpio.h>
25

26
27
static const int CFG_SFIO = 0;
static const int CFG_GPIO = 1;
28
29
30
static const int DIRECTION_INPUT = 0;
static const int DIRECTION_OUTPUT = 1;

31
struct tegra_gpio_plat {
32
33
34
35
	struct gpio_ctlr_bank *bank;
	const char *port_name;	/* Name of port, e.g. "B" */
	int base_gpio;		/* Port number for this port (0, 1,.., n-1) */
};
36

37
38
39
40
41
/* Information about each port at run-time */
struct tegra_port_info {
	struct gpio_ctlr_bank *bank;
	int base_gpio;		/* Port number for this port (0, 1,.., n-1) */
};
42

43
/* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */
44
static int get_config(unsigned gpio)
45
{
46
47
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
48
49
50
	u32 u;
	int type;

51
	u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
52
	type = (u >> GPIO_BIT(gpio)) & 1;
53
54

	debug("get_config: port = %d, bit = %d is %s\n",
55
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
56

57
	return type ? CFG_GPIO : CFG_SFIO;
58
59
}

60
/* Config pin 'gpio' as GPIO or SFIO, based on 'type' */
61
static void set_config(unsigned gpio, int type)
62
{
63
64
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
65
66
67
	u32 u;

	debug("set_config: port = %d, bit = %d, %s\n",
68
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
69

70
	u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
71
	if (type != CFG_SFIO)
72
		u |= 1 << GPIO_BIT(gpio);
73
	else
74
75
		u &= ~(1 << GPIO_BIT(gpio));
	writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
76
77
}

78
79
/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
static int get_direction(unsigned gpio)
80
{
81
82
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
83
84
85
	u32 u;
	int dir;

86
87
	u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
	dir =  (u >> GPIO_BIT(gpio)) & 1;
88
89

	debug("get_direction: port = %d, bit = %d, %s\n",
90
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
91

92
	return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT;
93
94
}

95
96
/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
static void set_direction(unsigned gpio, int output)
97
{
98
99
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
100
101
102
	u32 u;

	debug("set_direction: port = %d, bit = %d, %s\n",
103
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
104

105
	u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
106
	if (output != DIRECTION_INPUT)
107
		u |= 1 << GPIO_BIT(gpio);
108
	else
109
110
		u &= ~(1 << GPIO_BIT(gpio));
	writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
111
112
}

113
114
/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
static void set_level(unsigned gpio, int high)
115
{
116
117
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
118
119
120
	u32 u;

	debug("set_level: port = %d, bit %d == %d\n",
121
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
122

123
	u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
124
	if (high)
125
		u |= 1 << GPIO_BIT(gpio);
126
	else
127
128
		u &= ~(1 << GPIO_BIT(gpio));
	writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
129
130
131
132
133
134
}

/*
 * Generic_GPIO primitives.
 */

135
/* set GPIO pin 'gpio' as an input */
136
static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
137
{
138
	struct tegra_port_info *state = dev_get_priv(dev);
139
140

	/* Configure GPIO direction as input. */
141
	set_direction(state->base_gpio + offset, DIRECTION_INPUT);
142

143
144
145
	/* Enable the pin as a GPIO */
	set_config(state->base_gpio + offset, 1);

146
147
148
	return 0;
}

149
/* set GPIO pin 'gpio' as an output, with polarity 'value' */
150
151
static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
				       int value)
152
{
153
154
	struct tegra_port_info *state = dev_get_priv(dev);
	int gpio = state->base_gpio + offset;
155
156

	/* Configure GPIO output value. */
157
	set_level(gpio, value);
158
159

	/* Configure GPIO direction as output. */
160
	set_direction(gpio, DIRECTION_OUTPUT);
161

162
163
164
	/* Enable the pin as a GPIO */
	set_config(state->base_gpio + offset, 1);

165
166
167
	return 0;
}

168
/* read GPIO IN value of pin 'gpio' */
169
static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
170
{
171
172
	struct tegra_port_info *state = dev_get_priv(dev);
	int gpio = state->base_gpio + offset;
173
174
	int val;

175
176
	debug("%s: pin = %d (port %d:bit %d)\n", __func__,
	      gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
177

178
179
180
181
	if (get_direction(gpio) == DIRECTION_INPUT)
		val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
	else
		val = readl(&state->bank->gpio_out[GPIO_PORT(gpio)]);
182

183
	return (val >> GPIO_BIT(gpio)) & 1;
184
185
}

186
/* write GPIO OUT value to pin 'gpio' */
187
static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
188
{
189
190
191
	struct tegra_port_info *state = dev_get_priv(dev);
	int gpio = state->base_gpio + offset;

192
	debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
193
	      gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
194
195

	/* Configure GPIO output value. */
196
197
198
	set_level(gpio, value);

	return 0;
199
200
}

201
202
203
204
205
206
207
void gpio_config_table(const struct tegra_gpio_config *config, int len)
{
	int i;

	for (i = 0; i < len; i++) {
		switch (config[i].init) {
		case TEGRA_GPIO_INIT_IN:
208
			set_direction(config[i].gpio, DIRECTION_INPUT);
209
210
			break;
		case TEGRA_GPIO_INIT_OUT0:
211
			set_level(config[i].gpio, 0);
212
			set_direction(config[i].gpio, DIRECTION_OUTPUT);
213
214
			break;
		case TEGRA_GPIO_INIT_OUT1:
215
			set_level(config[i].gpio, 1);
216
			set_direction(config[i].gpio, DIRECTION_OUTPUT);
217
218
			break;
		}
219
		set_config(config[i].gpio, CFG_GPIO);
220
221
222
	}
}

223
224
225
226
227
228
229
230
231
232
233
234
235
static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
{
	struct tegra_port_info *state = dev_get_priv(dev);
	int gpio = state->base_gpio + offset;

	if (!get_config(gpio))
		return GPIOF_FUNC;
	else if (get_direction(gpio))
		return GPIOF_OUTPUT;
	else
		return GPIOF_INPUT;
}

236
static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
237
			    struct ofnode_phandle_args *args)
238
239
240
241
242
243
244
245
246
247
248
249
250
251
{
	int gpio, port, ret;

	gpio = args->args[0];
	port = gpio / TEGRA_GPIOS_PER_PORT;
	ret = device_get_child(dev, port, &desc->dev);
	if (ret)
		return ret;
	desc->offset = gpio % TEGRA_GPIOS_PER_PORT;
	desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;

	return 0;
}

252
253
254
255
256
257
static const struct dm_gpio_ops gpio_tegra_ops = {
	.direction_input	= tegra_gpio_direction_input,
	.direction_output	= tegra_gpio_direction_output,
	.get_value		= tegra_gpio_get_value,
	.set_value		= tegra_gpio_set_value,
	.get_function		= tegra_gpio_get_function,
258
	.xlate			= tegra_gpio_xlate,
259
260
261
262
263
264
265
266
267
};

/**
 * Returns the name of a GPIO port
 *
 * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
 *
 * @base_port: Base port number (0, 1..n-1)
 * @return allocated string containing the name
268
 */
269
static char *gpio_port_name(int base_port)
270
{
271
272
273
274
275
276
277
278
279
280
	char *name, *s;

	name = malloc(3);
	if (name) {
		s = name;
		*s++ = 'A' + (base_port % 26);
		if (base_port >= 26)
			*s++ = *name;
		*s = '\0';
	}
281

282
283
284
285
286
287
288
289
290
291
292
	return name;
}

static const struct udevice_id tegra_gpio_ids[] = {
	{ .compatible = "nvidia,tegra30-gpio" },
	{ .compatible = "nvidia,tegra20-gpio" },
	{ }
};

static int gpio_tegra_probe(struct udevice *dev)
{
293
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
294
295
	struct tegra_port_info *priv = dev_get_priv(dev);
	struct tegra_gpio_plat *plat = dev_get_plat(dev);
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

	/* Only child devices have ports */
	if (!plat)
		return 0;

	priv->bank = plat->bank;
	priv->base_gpio = plat->base_gpio;

	uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
	uc_priv->bank_name = plat->port_name;

	return 0;
}

/**
 * We have a top-level GPIO device with no actual GPIOs. It has a child
 * device for each Tegra port.
 */
static int gpio_tegra_bind(struct udevice *parent)
{
316
	struct tegra_gpio_plat *plat = dev_get_plat(parent);
317
318
319
320
321
322
323
324
325
	struct gpio_ctlr *ctlr;
	int bank_count;
	int bank;
	int ret;

	/* If this is a child device, there is nothing to do here */
	if (plat)
		return 0;

326
327
328
329
330
331
332
333
	/* TODO(sjg@chromium.org): Remove once SPL supports device tree */
#ifdef CONFIG_SPL_BUILD
	ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	bank_count = TEGRA_GPIO_BANKS;
#else
	{
	int len;

334
335
336
337
	/*
	 * This driver does not make use of interrupts, other than to figure
	 * out the number of GPIO banks
	 */
338
339
340
	len = dev_read_size(parent, "interrupts");
	if (len < 0)
		return len;
341
	bank_count = len / 3 / sizeof(u32);
342
343
344
	ctlr = (struct gpio_ctlr *)dev_read_addr(parent);
	if ((ulong)ctlr == FDT_ADDR_T_NONE)
		return -EINVAL;
345
346
	}
#endif
347
348
349
350
	for (bank = 0; bank < bank_count; bank++) {
		int port;

		for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
351
			struct tegra_gpio_plat *plat;
352
353
354
355
356
357
358
359
360
361
362
			struct udevice *dev;
			int base_port;

			plat = calloc(1, sizeof(*plat));
			if (!plat)
				return -ENOMEM;
			plat->bank = &ctlr->gpio_bank[bank];
			base_port = bank * TEGRA_PORTS_PER_BANK + port;
			plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
			plat->port_name = gpio_port_name(base_port);

363
			ret = device_bind(parent, parent->driver,
364
365
					  plat->port_name, plat,
					  dev_ofnode(parent), &dev);
366
367
368
			if (ret)
				return ret;
		}
369
	}
370
371

	return 0;
372
}
373
374
375
376
377
378
379

U_BOOT_DRIVER(gpio_tegra) = {
	.name	= "gpio_tegra",
	.id	= UCLASS_GPIO,
	.of_match = tegra_gpio_ids,
	.bind	= gpio_tegra_bind,
	.probe = gpio_tegra_probe,
380
	.priv_auto	= sizeof(struct tegra_port_info),
381
382
	.ops	= &gpio_tegra_ops,
};