blob: f9c06fe88b71d289752dd0c82ec68761241c3c1a [file] [log] [blame]
Tom Warrenb3878b82011-06-17 06:27:28 +00001/*
Allen Martin55d98a12012-08-31 08:30:00 +00002 * NVIDIA Tegra20 GPIO handling.
Tom Warrenc570d7a2012-05-22 12:19:25 +00003 * (C) Copyright 2010-2012
Tom Warrenb3878b82011-06-17 06:27:28 +00004 * NVIDIA Corporation <www.nvidia.com>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Tom Warrenb3878b82011-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 Glassb0461042014-09-03 17:37:03 -060015#include <dm.h>
16#include <malloc.h>
17#include <errno.h>
18#include <fdtdec.h>
Tom Warrenb3878b82011-06-17 06:27:28 +000019#include <asm/io.h>
20#include <asm/bitops.h>
Tom Warrenab371962012-09-19 15:50:56 -070021#include <asm/arch/tegra.h>
Tom Warrenb3878b82011-06-17 06:27:28 +000022#include <asm/gpio.h>
Simon Glassb0461042014-09-03 17:37:03 -060023#include <dm/device-internal.h>
Simon Glassada3b752015-01-05 20:05:33 -070024#include <dt-bindings/gpio/gpio.h>
Simon Glassb0461042014-09-03 17:37:03 -060025
26DECLARE_GLOBAL_DATA_PTR;
Tom Warrenb3878b82011-06-17 06:27:28 +000027
Simon Glassb0461042014-09-03 17:37:03 -060028struct tegra_gpio_platdata {
29 struct gpio_ctlr_bank *bank;
30 const char *port_name; /* Name of port, e.g. "B" */
31 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
32};
Tom Warrenb3878b82011-06-17 06:27:28 +000033
Simon Glassb0461042014-09-03 17:37:03 -060034/* Information about each port at run-time */
35struct tegra_port_info {
Simon Glassb0461042014-09-03 17:37:03 -060036 struct gpio_ctlr_bank *bank;
37 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
38};
Tom Warrenb3878b82011-06-17 06:27:28 +000039
Joe Hershbergerf8928f12011-11-11 15:55:36 -060040/* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */
41static int get_config(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000042{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060043 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
44 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000045 u32 u;
46 int type;
47
Joe Hershbergerf8928f12011-11-11 15:55:36 -060048 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
49 type = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000050
51 debug("get_config: port = %d, bit = %d is %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060052 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000053
54 return type;
55}
56
Joe Hershbergerf8928f12011-11-11 15:55:36 -060057/* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */
58static void set_config(unsigned gpio, int type)
Tom Warrenb3878b82011-06-17 06:27:28 +000059{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060060 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
61 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000062 u32 u;
63
64 debug("set_config: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060065 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000066
Joe Hershbergerf8928f12011-11-11 15:55:36 -060067 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000068 if (type) /* GPIO */
Joe Hershbergerf8928f12011-11-11 15:55:36 -060069 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +000070 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060071 u &= ~(1 << GPIO_BIT(gpio));
72 writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000073}
74
Joe Hershbergerf8928f12011-11-11 15:55:36 -060075/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
76static int get_direction(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000077{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060078 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
79 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000080 u32 u;
81 int dir;
82
Joe Hershbergerf8928f12011-11-11 15:55:36 -060083 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
84 dir = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000085
86 debug("get_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060087 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +000088
89 return dir;
90}
91
Joe Hershbergerf8928f12011-11-11 15:55:36 -060092/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
93static void set_direction(unsigned gpio, int output)
Tom Warrenb3878b82011-06-17 06:27:28 +000094{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060095 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
96 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000097 u32 u;
98
99 debug("set_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600100 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +0000101
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600102 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000103 if (output)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600104 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000105 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600106 u &= ~(1 << GPIO_BIT(gpio));
107 writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000108}
109
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600110/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
111static void set_level(unsigned gpio, int high)
Tom Warrenb3878b82011-06-17 06:27:28 +0000112{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600113 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
114 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000115 u32 u;
116
117 debug("set_level: port = %d, bit %d == %d\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600118 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
Tom Warrenb3878b82011-06-17 06:27:28 +0000119
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600120 u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000121 if (high)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600122 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000123 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600124 u &= ~(1 << GPIO_BIT(gpio));
125 writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000126}
127
128/*
129 * Generic_GPIO primitives.
130 */
131
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600132/* set GPIO pin 'gpio' as an input */
Simon Glassb0461042014-09-03 17:37:03 -0600133static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000134{
Simon Glassb0461042014-09-03 17:37:03 -0600135 struct tegra_port_info *state = dev_get_priv(dev);
Tom Warrenb3878b82011-06-17 06:27:28 +0000136
137 /* Configure GPIO direction as input. */
Simon Glassb0461042014-09-03 17:37:03 -0600138 set_direction(state->base_gpio + offset, 0);
Tom Warrenb3878b82011-06-17 06:27:28 +0000139
Stephen Warren6904f6d2015-09-23 12:13:00 -0600140 /* Enable the pin as a GPIO */
141 set_config(state->base_gpio + offset, 1);
142
Tom Warrenb3878b82011-06-17 06:27:28 +0000143 return 0;
144}
145
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600146/* set GPIO pin 'gpio' as an output, with polarity 'value' */
Simon Glassb0461042014-09-03 17:37:03 -0600147static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
148 int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000149{
Simon Glassb0461042014-09-03 17:37:03 -0600150 struct tegra_port_info *state = dev_get_priv(dev);
151 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000152
153 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600154 set_level(gpio, value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000155
156 /* Configure GPIO direction as output. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600157 set_direction(gpio, 1);
Tom Warrenb3878b82011-06-17 06:27:28 +0000158
Stephen Warren6904f6d2015-09-23 12:13:00 -0600159 /* Enable the pin as a GPIO */
160 set_config(state->base_gpio + offset, 1);
161
Tom Warrenb3878b82011-06-17 06:27:28 +0000162 return 0;
163}
164
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600165/* read GPIO IN value of pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600166static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000167{
Simon Glassb0461042014-09-03 17:37:03 -0600168 struct tegra_port_info *state = dev_get_priv(dev);
169 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000170 int val;
171
Simon Glassb0461042014-09-03 17:37:03 -0600172 debug("%s: pin = %d (port %d:bit %d)\n", __func__,
173 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
174
175 val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000176
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600177 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +0000178}
179
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600180/* write GPIO OUT value to pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600181static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000182{
Simon Glassb0461042014-09-03 17:37:03 -0600183 struct tegra_port_info *state = dev_get_priv(dev);
184 int gpio = state->base_gpio + offset;
Simon Glassb0461042014-09-03 17:37:03 -0600185
Tom Warrenb3878b82011-06-17 06:27:28 +0000186 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
Simon Glassb0461042014-09-03 17:37:03 -0600187 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000188
189 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600190 set_level(gpio, value);
191
192 return 0;
Tom Warrenb3878b82011-06-17 06:27:28 +0000193}
194
Stephen Warren4b27bf12014-04-22 14:37:53 -0600195void gpio_config_table(const struct tegra_gpio_config *config, int len)
196{
197 int i;
198
199 for (i = 0; i < len; i++) {
200 switch (config[i].init) {
201 case TEGRA_GPIO_INIT_IN:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600202 set_direction(config[i].gpio, 0);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600203 break;
204 case TEGRA_GPIO_INIT_OUT0:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600205 set_level(config[i].gpio, 0);
206 set_direction(config[i].gpio, 1);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600207 break;
208 case TEGRA_GPIO_INIT_OUT1:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600209 set_level(config[i].gpio, 1);
210 set_direction(config[i].gpio, 1);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600211 break;
212 }
213 set_config(config[i].gpio, 1);
214 }
215}
216
Simon Glassb0461042014-09-03 17:37:03 -0600217static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
218{
219 struct tegra_port_info *state = dev_get_priv(dev);
220 int gpio = state->base_gpio + offset;
221
Simon Glassb0461042014-09-03 17:37:03 -0600222 if (!get_config(gpio))
223 return GPIOF_FUNC;
224 else if (get_direction(gpio))
225 return GPIOF_OUTPUT;
226 else
227 return GPIOF_INPUT;
228}
229
Simon Glassada3b752015-01-05 20:05:33 -0700230static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
231 struct fdtdec_phandle_args *args)
232{
233 int gpio, port, ret;
234
235 gpio = args->args[0];
236 port = gpio / TEGRA_GPIOS_PER_PORT;
237 ret = device_get_child(dev, port, &desc->dev);
238 if (ret)
239 return ret;
240 desc->offset = gpio % TEGRA_GPIOS_PER_PORT;
241 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
242
243 return 0;
244}
245
Simon Glassb0461042014-09-03 17:37:03 -0600246static const struct dm_gpio_ops gpio_tegra_ops = {
Simon Glassb0461042014-09-03 17:37:03 -0600247 .direction_input = tegra_gpio_direction_input,
248 .direction_output = tegra_gpio_direction_output,
249 .get_value = tegra_gpio_get_value,
250 .set_value = tegra_gpio_set_value,
251 .get_function = tegra_gpio_get_function,
Simon Glassada3b752015-01-05 20:05:33 -0700252 .xlate = tegra_gpio_xlate,
Simon Glassb0461042014-09-03 17:37:03 -0600253};
254
255/**
256 * Returns the name of a GPIO port
257 *
258 * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
259 *
260 * @base_port: Base port number (0, 1..n-1)
261 * @return allocated string containing the name
Tom Warrenb3878b82011-06-17 06:27:28 +0000262 */
Simon Glassb0461042014-09-03 17:37:03 -0600263static char *gpio_port_name(int base_port)
Tom Warrenb3878b82011-06-17 06:27:28 +0000264{
Simon Glassb0461042014-09-03 17:37:03 -0600265 char *name, *s;
Tom Warrenb3878b82011-06-17 06:27:28 +0000266
Simon Glassb0461042014-09-03 17:37:03 -0600267 name = malloc(3);
268 if (name) {
269 s = name;
270 *s++ = 'A' + (base_port % 26);
271 if (base_port >= 26)
272 *s++ = *name;
273 *s = '\0';
Tom Warrenb3878b82011-06-17 06:27:28 +0000274 }
Simon Glassb0461042014-09-03 17:37:03 -0600275
276 return name;
Tom Warrenb3878b82011-06-17 06:27:28 +0000277}
Simon Glassb0461042014-09-03 17:37:03 -0600278
279static const struct udevice_id tegra_gpio_ids[] = {
280 { .compatible = "nvidia,tegra30-gpio" },
281 { .compatible = "nvidia,tegra20-gpio" },
282 { }
283};
284
285static int gpio_tegra_probe(struct udevice *dev)
286{
Simon Glassde0977b2015-03-05 12:25:20 -0700287 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb0461042014-09-03 17:37:03 -0600288 struct tegra_port_info *priv = dev->priv;
289 struct tegra_gpio_platdata *plat = dev->platdata;
290
291 /* Only child devices have ports */
292 if (!plat)
293 return 0;
294
295 priv->bank = plat->bank;
296 priv->base_gpio = plat->base_gpio;
297
298 uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
299 uc_priv->bank_name = plat->port_name;
300
301 return 0;
302}
303
304/**
305 * We have a top-level GPIO device with no actual GPIOs. It has a child
306 * device for each Tegra port.
307 */
308static int gpio_tegra_bind(struct udevice *parent)
309{
310 struct tegra_gpio_platdata *plat = parent->platdata;
311 struct gpio_ctlr *ctlr;
312 int bank_count;
313 int bank;
314 int ret;
Simon Glassb0461042014-09-03 17:37:03 -0600315
316 /* If this is a child device, there is nothing to do here */
317 if (plat)
318 return 0;
319
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700320 /* TODO(sjg@chromium.org): Remove once SPL supports device tree */
321#ifdef CONFIG_SPL_BUILD
322 ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
323 bank_count = TEGRA_GPIO_BANKS;
324#else
325 {
326 int len;
327
Simon Glassb0461042014-09-03 17:37:03 -0600328 /*
329 * This driver does not make use of interrupts, other than to figure
330 * out the number of GPIO banks
331 */
332 if (!fdt_getprop(gd->fdt_blob, parent->of_offset, "interrupts", &len))
333 return -EINVAL;
334 bank_count = len / 3 / sizeof(u32);
Simon Glass09717782015-08-11 08:33:29 -0600335 ctlr = (struct gpio_ctlr *)dev_get_addr(parent);
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700336 }
337#endif
Simon Glassb0461042014-09-03 17:37:03 -0600338 for (bank = 0; bank < bank_count; bank++) {
339 int port;
340
341 for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
342 struct tegra_gpio_platdata *plat;
343 struct udevice *dev;
344 int base_port;
345
346 plat = calloc(1, sizeof(*plat));
347 if (!plat)
348 return -ENOMEM;
349 plat->bank = &ctlr->gpio_bank[bank];
350 base_port = bank * TEGRA_PORTS_PER_BANK + port;
351 plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
352 plat->port_name = gpio_port_name(base_port);
353
354 ret = device_bind(parent, parent->driver,
355 plat->port_name, plat, -1, &dev);
356 if (ret)
357 return ret;
358 dev->of_offset = parent->of_offset;
359 }
360 }
361
362 return 0;
363}
364
365U_BOOT_DRIVER(gpio_tegra) = {
366 .name = "gpio_tegra",
367 .id = UCLASS_GPIO,
368 .of_match = tegra_gpio_ids,
369 .bind = gpio_tegra_bind,
370 .probe = gpio_tegra_probe,
371 .priv_auto_alloc_size = sizeof(struct tegra_port_info),
372 .ops = &gpio_tegra_ops,
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700373 .flags = DM_FLAG_PRE_RELOC,
Simon Glassb0461042014-09-03 17:37:03 -0600374};