blob: 8e880e276f0a9915a22095b043820996e3a32c81 [file] [log] [blame]
Tom Warren4e5ae092011-06-17 06:27:28 +00001/*
Allen Martin00a27492012-08-31 08:30:00 +00002 * NVIDIA Tegra20 GPIO handling.
Stephen Warrenfe828572015-09-25 10:44:08 -06003 * (C) Copyright 2010-2012,2015
Tom Warren4e5ae092011-06-17 06:27:28 +00004 * NVIDIA Corporation <www.nvidia.com>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Tom Warren4e5ae092011-06-17 06:27:28 +00007 */
8
9/*
10 * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
11 * Tom Warren (twarren@nvidia.com)
12 */
13
14#include <common.h>
Simon Glass2fccd2d2014-09-03 17:37:03 -060015#include <dm.h>
16#include <malloc.h>
17#include <errno.h>
18#include <fdtdec.h>
Tom Warren4e5ae092011-06-17 06:27:28 +000019#include <asm/io.h>
20#include <asm/bitops.h>
Tom Warren150c2492012-09-19 15:50:56 -070021#include <asm/arch/tegra.h>
Tom Warren4e5ae092011-06-17 06:27:28 +000022#include <asm/gpio.h>
Simon Glass2fccd2d2014-09-03 17:37:03 -060023#include <dm/device-internal.h>
Simon Glass838aa5c2015-01-05 20:05:33 -070024#include <dt-bindings/gpio/gpio.h>
Simon Glass2fccd2d2014-09-03 17:37:03 -060025
26DECLARE_GLOBAL_DATA_PTR;
Tom Warren4e5ae092011-06-17 06:27:28 +000027
Stephen Warrenfe828572015-09-25 10:44:08 -060028static const int CONFIG_SFIO = 0;
29static const int CONFIG_GPIO = 1;
30static const int DIRECTION_INPUT = 0;
31static const int DIRECTION_OUTPUT = 1;
32
Simon Glass2fccd2d2014-09-03 17:37:03 -060033struct tegra_gpio_platdata {
34 struct gpio_ctlr_bank *bank;
35 const char *port_name; /* Name of port, e.g. "B" */
36 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
37};
Tom Warren4e5ae092011-06-17 06:27:28 +000038
Simon Glass2fccd2d2014-09-03 17:37:03 -060039/* Information about each port at run-time */
40struct tegra_port_info {
Simon Glass2fccd2d2014-09-03 17:37:03 -060041 struct gpio_ctlr_bank *bank;
42 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
43};
Tom Warren4e5ae092011-06-17 06:27:28 +000044
Stephen Warrenfe828572015-09-25 10:44:08 -060045/* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */
Joe Hershberger365d6072011-11-11 15:55:36 -060046static int get_config(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +000047{
Joe Hershberger365d6072011-11-11 15:55:36 -060048 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
49 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +000050 u32 u;
51 int type;
52
Joe Hershberger365d6072011-11-11 15:55:36 -060053 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Stephen Warrenfe828572015-09-25 10:44:08 -060054 type = (u >> GPIO_BIT(gpio)) & 1;
Tom Warren4e5ae092011-06-17 06:27:28 +000055
56 debug("get_config: port = %d, bit = %d is %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -060057 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warren4e5ae092011-06-17 06:27:28 +000058
Stephen Warrenfe828572015-09-25 10:44:08 -060059 return type ? CONFIG_GPIO : CONFIG_SFIO;
Tom Warren4e5ae092011-06-17 06:27:28 +000060}
61
Stephen Warrenfe828572015-09-25 10:44:08 -060062/* Config pin 'gpio' as GPIO or SFIO, based on 'type' */
Joe Hershberger365d6072011-11-11 15:55:36 -060063static void set_config(unsigned gpio, int type)
Tom Warren4e5ae092011-06-17 06:27:28 +000064{
Joe Hershberger365d6072011-11-11 15:55:36 -060065 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
66 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +000067 u32 u;
68
69 debug("set_config: port = %d, bit = %d, %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -060070 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warren4e5ae092011-06-17 06:27:28 +000071
Joe Hershberger365d6072011-11-11 15:55:36 -060072 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Stephen Warrenfe828572015-09-25 10:44:08 -060073 if (type != CONFIG_SFIO)
Joe Hershberger365d6072011-11-11 15:55:36 -060074 u |= 1 << GPIO_BIT(gpio);
Tom Warren4e5ae092011-06-17 06:27:28 +000075 else
Joe Hershberger365d6072011-11-11 15:55:36 -060076 u &= ~(1 << GPIO_BIT(gpio));
77 writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +000078}
79
Joe Hershberger365d6072011-11-11 15:55:36 -060080/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
81static int get_direction(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +000082{
Joe Hershberger365d6072011-11-11 15:55:36 -060083 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
84 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +000085 u32 u;
86 int dir;
87
Joe Hershberger365d6072011-11-11 15:55:36 -060088 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
89 dir = (u >> GPIO_BIT(gpio)) & 1;
Tom Warren4e5ae092011-06-17 06:27:28 +000090
91 debug("get_direction: port = %d, bit = %d, %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -060092 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
Tom Warren4e5ae092011-06-17 06:27:28 +000093
Stephen Warrenfe828572015-09-25 10:44:08 -060094 return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT;
Tom Warren4e5ae092011-06-17 06:27:28 +000095}
96
Joe Hershberger365d6072011-11-11 15:55:36 -060097/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
98static void set_direction(unsigned gpio, int output)
Tom Warren4e5ae092011-06-17 06:27:28 +000099{
Joe Hershberger365d6072011-11-11 15:55:36 -0600100 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
101 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +0000102 u32 u;
103
104 debug("set_direction: port = %d, bit = %d, %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600105 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
Tom Warren4e5ae092011-06-17 06:27:28 +0000106
Joe Hershberger365d6072011-11-11 15:55:36 -0600107 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
Stephen Warrenfe828572015-09-25 10:44:08 -0600108 if (output != DIRECTION_INPUT)
Joe Hershberger365d6072011-11-11 15:55:36 -0600109 u |= 1 << GPIO_BIT(gpio);
Tom Warren4e5ae092011-06-17 06:27:28 +0000110 else
Joe Hershberger365d6072011-11-11 15:55:36 -0600111 u &= ~(1 << GPIO_BIT(gpio));
112 writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000113}
114
Joe Hershberger365d6072011-11-11 15:55:36 -0600115/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
116static void set_level(unsigned gpio, int high)
Tom Warren4e5ae092011-06-17 06:27:28 +0000117{
Joe Hershberger365d6072011-11-11 15:55:36 -0600118 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
119 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +0000120 u32 u;
121
122 debug("set_level: port = %d, bit %d == %d\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600123 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
Tom Warren4e5ae092011-06-17 06:27:28 +0000124
Joe Hershberger365d6072011-11-11 15:55:36 -0600125 u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000126 if (high)
Joe Hershberger365d6072011-11-11 15:55:36 -0600127 u |= 1 << GPIO_BIT(gpio);
Tom Warren4e5ae092011-06-17 06:27:28 +0000128 else
Joe Hershberger365d6072011-11-11 15:55:36 -0600129 u &= ~(1 << GPIO_BIT(gpio));
130 writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000131}
132
133/*
134 * Generic_GPIO primitives.
135 */
136
Joe Hershberger365d6072011-11-11 15:55:36 -0600137/* set GPIO pin 'gpio' as an input */
Simon Glass2fccd2d2014-09-03 17:37:03 -0600138static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
Tom Warren4e5ae092011-06-17 06:27:28 +0000139{
Simon Glass2fccd2d2014-09-03 17:37:03 -0600140 struct tegra_port_info *state = dev_get_priv(dev);
Tom Warren4e5ae092011-06-17 06:27:28 +0000141
142 /* Configure GPIO direction as input. */
Stephen Warrenfe828572015-09-25 10:44:08 -0600143 set_direction(state->base_gpio + offset, DIRECTION_INPUT);
Tom Warren4e5ae092011-06-17 06:27:28 +0000144
Stephen Warren0c35e3a2015-09-23 12:13:00 -0600145 /* Enable the pin as a GPIO */
146 set_config(state->base_gpio + offset, 1);
147
Tom Warren4e5ae092011-06-17 06:27:28 +0000148 return 0;
149}
150
Joe Hershberger365d6072011-11-11 15:55:36 -0600151/* set GPIO pin 'gpio' as an output, with polarity 'value' */
Simon Glass2fccd2d2014-09-03 17:37:03 -0600152static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
153 int value)
Tom Warren4e5ae092011-06-17 06:27:28 +0000154{
Simon Glass2fccd2d2014-09-03 17:37:03 -0600155 struct tegra_port_info *state = dev_get_priv(dev);
156 int gpio = state->base_gpio + offset;
Tom Warren4e5ae092011-06-17 06:27:28 +0000157
158 /* Configure GPIO output value. */
Joe Hershberger365d6072011-11-11 15:55:36 -0600159 set_level(gpio, value);
Tom Warren4e5ae092011-06-17 06:27:28 +0000160
161 /* Configure GPIO direction as output. */
Stephen Warrenfe828572015-09-25 10:44:08 -0600162 set_direction(gpio, DIRECTION_OUTPUT);
Tom Warren4e5ae092011-06-17 06:27:28 +0000163
Stephen Warren0c35e3a2015-09-23 12:13:00 -0600164 /* Enable the pin as a GPIO */
165 set_config(state->base_gpio + offset, 1);
166
Tom Warren4e5ae092011-06-17 06:27:28 +0000167 return 0;
168}
169
Joe Hershberger365d6072011-11-11 15:55:36 -0600170/* read GPIO IN value of pin 'gpio' */
Simon Glass2fccd2d2014-09-03 17:37:03 -0600171static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
Tom Warren4e5ae092011-06-17 06:27:28 +0000172{
Simon Glass2fccd2d2014-09-03 17:37:03 -0600173 struct tegra_port_info *state = dev_get_priv(dev);
174 int gpio = state->base_gpio + offset;
Tom Warren4e5ae092011-06-17 06:27:28 +0000175 int val;
176
Simon Glass2fccd2d2014-09-03 17:37:03 -0600177 debug("%s: pin = %d (port %d:bit %d)\n", __func__,
178 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
179
180 val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000181
Joe Hershberger365d6072011-11-11 15:55:36 -0600182 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warren4e5ae092011-06-17 06:27:28 +0000183}
184
Joe Hershberger365d6072011-11-11 15:55:36 -0600185/* write GPIO OUT value to pin 'gpio' */
Simon Glass2fccd2d2014-09-03 17:37:03 -0600186static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Tom Warren4e5ae092011-06-17 06:27:28 +0000187{
Simon Glass2fccd2d2014-09-03 17:37:03 -0600188 struct tegra_port_info *state = dev_get_priv(dev);
189 int gpio = state->base_gpio + offset;
Simon Glass2fccd2d2014-09-03 17:37:03 -0600190
Tom Warren4e5ae092011-06-17 06:27:28 +0000191 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
Simon Glass2fccd2d2014-09-03 17:37:03 -0600192 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
Tom Warren4e5ae092011-06-17 06:27:28 +0000193
194 /* Configure GPIO output value. */
Joe Hershberger365d6072011-11-11 15:55:36 -0600195 set_level(gpio, value);
196
197 return 0;
Tom Warren4e5ae092011-06-17 06:27:28 +0000198}
199
Stephen Warreneceb3f22014-04-22 14:37:53 -0600200void gpio_config_table(const struct tegra_gpio_config *config, int len)
201{
202 int i;
203
204 for (i = 0; i < len; i++) {
205 switch (config[i].init) {
206 case TEGRA_GPIO_INIT_IN:
Stephen Warrenfe828572015-09-25 10:44:08 -0600207 set_direction(config[i].gpio, DIRECTION_INPUT);
Stephen Warreneceb3f22014-04-22 14:37:53 -0600208 break;
209 case TEGRA_GPIO_INIT_OUT0:
Stephen Warrenf9d3cab2015-09-23 12:12:59 -0600210 set_level(config[i].gpio, 0);
Stephen Warrenfe828572015-09-25 10:44:08 -0600211 set_direction(config[i].gpio, DIRECTION_OUTPUT);
Stephen Warreneceb3f22014-04-22 14:37:53 -0600212 break;
213 case TEGRA_GPIO_INIT_OUT1:
Stephen Warrenf9d3cab2015-09-23 12:12:59 -0600214 set_level(config[i].gpio, 1);
Stephen Warrenfe828572015-09-25 10:44:08 -0600215 set_direction(config[i].gpio, DIRECTION_OUTPUT);
Stephen Warreneceb3f22014-04-22 14:37:53 -0600216 break;
217 }
Stephen Warrenfe828572015-09-25 10:44:08 -0600218 set_config(config[i].gpio, CONFIG_GPIO);
Stephen Warreneceb3f22014-04-22 14:37:53 -0600219 }
220}
221
Simon Glass2fccd2d2014-09-03 17:37:03 -0600222static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
Tom Warren4e5ae092011-06-17 06:27:28 +0000223{
Simon Glass2fccd2d2014-09-03 17:37:03 -0600224 struct tegra_port_info *state = dev_get_priv(dev);
225 int gpio = state->base_gpio + offset;
Tom Warren4e5ae092011-06-17 06:27:28 +0000226
Simon Glass2fccd2d2014-09-03 17:37:03 -0600227 if (!get_config(gpio))
228 return GPIOF_FUNC;
229 else if (get_direction(gpio))
230 return GPIOF_OUTPUT;
231 else
232 return GPIOF_INPUT;
Tom Warren4e5ae092011-06-17 06:27:28 +0000233}
Simon Glass2fccd2d2014-09-03 17:37:03 -0600234
Simon Glass838aa5c2015-01-05 20:05:33 -0700235static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
236 struct fdtdec_phandle_args *args)
237{
238 int gpio, port, ret;
239
240 gpio = args->args[0];
241 port = gpio / TEGRA_GPIOS_PER_PORT;
242 ret = device_get_child(dev, port, &desc->dev);
243 if (ret)
244 return ret;
245 desc->offset = gpio % TEGRA_GPIOS_PER_PORT;
246 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
247
248 return 0;
249}
250
Simon Glass2fccd2d2014-09-03 17:37:03 -0600251static const struct dm_gpio_ops gpio_tegra_ops = {
Simon Glass2fccd2d2014-09-03 17:37:03 -0600252 .direction_input = tegra_gpio_direction_input,
253 .direction_output = tegra_gpio_direction_output,
254 .get_value = tegra_gpio_get_value,
255 .set_value = tegra_gpio_set_value,
256 .get_function = tegra_gpio_get_function,
Simon Glass838aa5c2015-01-05 20:05:33 -0700257 .xlate = tegra_gpio_xlate,
Simon Glass2fccd2d2014-09-03 17:37:03 -0600258};
259
260/**
261 * Returns the name of a GPIO port
262 *
263 * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
264 *
265 * @base_port: Base port number (0, 1..n-1)
266 * @return allocated string containing the name
267 */
268static char *gpio_port_name(int base_port)
269{
270 char *name, *s;
271
272 name = malloc(3);
273 if (name) {
274 s = name;
275 *s++ = 'A' + (base_port % 26);
276 if (base_port >= 26)
277 *s++ = *name;
278 *s = '\0';
279 }
280
281 return name;
282}
283
284static const struct udevice_id tegra_gpio_ids[] = {
285 { .compatible = "nvidia,tegra30-gpio" },
286 { .compatible = "nvidia,tegra20-gpio" },
287 { }
288};
289
290static int gpio_tegra_probe(struct udevice *dev)
291{
Simon Glasse564f052015-03-05 12:25:20 -0700292 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass2fccd2d2014-09-03 17:37:03 -0600293 struct tegra_port_info *priv = dev->priv;
294 struct tegra_gpio_platdata *plat = dev->platdata;
295
296 /* Only child devices have ports */
297 if (!plat)
298 return 0;
299
300 priv->bank = plat->bank;
301 priv->base_gpio = plat->base_gpio;
302
303 uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
304 uc_priv->bank_name = plat->port_name;
305
306 return 0;
307}
308
309/**
310 * We have a top-level GPIO device with no actual GPIOs. It has a child
311 * device for each Tegra port.
312 */
313static int gpio_tegra_bind(struct udevice *parent)
314{
315 struct tegra_gpio_platdata *plat = parent->platdata;
316 struct gpio_ctlr *ctlr;
317 int bank_count;
318 int bank;
319 int ret;
Simon Glass2fccd2d2014-09-03 17:37:03 -0600320
321 /* If this is a child device, there is nothing to do here */
322 if (plat)
323 return 0;
324
Simon Glassbdfb3412015-03-03 08:02:59 -0700325 /* TODO(sjg@chromium.org): Remove once SPL supports device tree */
326#ifdef CONFIG_SPL_BUILD
327 ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
328 bank_count = TEGRA_GPIO_BANKS;
329#else
330 {
331 int len;
332
Simon Glass2fccd2d2014-09-03 17:37:03 -0600333 /*
334 * This driver does not make use of interrupts, other than to figure
335 * out the number of GPIO banks
336 */
337 if (!fdt_getprop(gd->fdt_blob, parent->of_offset, "interrupts", &len))
338 return -EINVAL;
339 bank_count = len / 3 / sizeof(u32);
Simon Glass4e9838c2015-08-11 08:33:29 -0600340 ctlr = (struct gpio_ctlr *)dev_get_addr(parent);
Simon Glassbdfb3412015-03-03 08:02:59 -0700341 }
342#endif
Simon Glass2fccd2d2014-09-03 17:37:03 -0600343 for (bank = 0; bank < bank_count; bank++) {
344 int port;
345
346 for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
347 struct tegra_gpio_platdata *plat;
348 struct udevice *dev;
349 int base_port;
350
351 plat = calloc(1, sizeof(*plat));
352 if (!plat)
353 return -ENOMEM;
354 plat->bank = &ctlr->gpio_bank[bank];
355 base_port = bank * TEGRA_PORTS_PER_BANK + port;
356 plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
357 plat->port_name = gpio_port_name(base_port);
358
359 ret = device_bind(parent, parent->driver,
360 plat->port_name, plat, -1, &dev);
361 if (ret)
362 return ret;
363 dev->of_offset = parent->of_offset;
364 }
365 }
366
367 return 0;
368}
369
370U_BOOT_DRIVER(gpio_tegra) = {
371 .name = "gpio_tegra",
372 .id = UCLASS_GPIO,
373 .of_match = tegra_gpio_ids,
374 .bind = gpio_tegra_bind,
375 .probe = gpio_tegra_probe,
376 .priv_auto_alloc_size = sizeof(struct tegra_port_info),
377 .ops = &gpio_tegra_ops,
Simon Glassbdfb3412015-03-03 08:02:59 -0700378 .flags = DM_FLAG_PRE_RELOC,
Simon Glass2fccd2d2014-09-03 17:37:03 -0600379};