blob: 912577a5719135b5da22099ca828cfe1f43588ba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warrenb3878b82011-06-17 06:27:28 +00002/*
Allen Martin55d98a12012-08-31 08:30:00 +00003 * NVIDIA Tegra20 GPIO handling.
Stephen Warren20a91452015-09-25 10:44:08 -06004 * (C) Copyright 2010-2012,2015
Tom Warrenb3878b82011-06-17 06:27:28 +00005 * NVIDIA Corporation <www.nvidia.com>
Tom Warrenb3878b82011-06-17 06:27:28 +00006 */
7
8/*
9 * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
10 * Tom Warren (twarren@nvidia.com)
11 */
12
13#include <common.h>
Simon Glassb0461042014-09-03 17:37:03 -060014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glassb0461042014-09-03 17:37:03 -060016#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
Stephen Warren20a91452015-09-25 10:44:08 -060026static const int CONFIG_SFIO = 0;
27static const int CONFIG_GPIO = 1;
28static const int DIRECTION_INPUT = 0;
29static const int DIRECTION_OUTPUT = 1;
30
Simon Glassb0461042014-09-03 17:37:03 -060031struct tegra_gpio_platdata {
32 struct gpio_ctlr_bank *bank;
33 const char *port_name; /* Name of port, e.g. "B" */
34 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
35};
Tom Warrenb3878b82011-06-17 06:27:28 +000036
Simon Glassb0461042014-09-03 17:37:03 -060037/* Information about each port at run-time */
38struct tegra_port_info {
Simon Glassb0461042014-09-03 17:37:03 -060039 struct gpio_ctlr_bank *bank;
40 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
41};
Tom Warrenb3878b82011-06-17 06:27:28 +000042
Stephen Warren20a91452015-09-25 10:44:08 -060043/* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */
Joe Hershbergerf8928f12011-11-11 15:55:36 -060044static int get_config(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000045{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060046 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
47 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000048 u32 u;
49 int type;
50
Joe Hershbergerf8928f12011-11-11 15:55:36 -060051 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Stephen Warren20a91452015-09-25 10:44:08 -060052 type = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000053
54 debug("get_config: port = %d, bit = %d is %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060055 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000056
Stephen Warren20a91452015-09-25 10:44:08 -060057 return type ? CONFIG_GPIO : CONFIG_SFIO;
Tom Warrenb3878b82011-06-17 06:27:28 +000058}
59
Stephen Warren20a91452015-09-25 10:44:08 -060060/* Config pin 'gpio' as GPIO or SFIO, based on 'type' */
Joe Hershbergerf8928f12011-11-11 15:55:36 -060061static void set_config(unsigned gpio, int type)
Tom Warrenb3878b82011-06-17 06:27:28 +000062{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060063 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
64 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000065 u32 u;
66
67 debug("set_config: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060068 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000069
Joe Hershbergerf8928f12011-11-11 15:55:36 -060070 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Stephen Warren20a91452015-09-25 10:44:08 -060071 if (type != CONFIG_SFIO)
Joe Hershbergerf8928f12011-11-11 15:55:36 -060072 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +000073 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060074 u &= ~(1 << GPIO_BIT(gpio));
75 writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000076}
77
Joe Hershbergerf8928f12011-11-11 15:55:36 -060078/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
79static int get_direction(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000080{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060081 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
82 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000083 u32 u;
84 int dir;
85
Joe Hershbergerf8928f12011-11-11 15:55:36 -060086 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
87 dir = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000088
89 debug("get_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060090 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +000091
Stephen Warren20a91452015-09-25 10:44:08 -060092 return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT;
Tom Warrenb3878b82011-06-17 06:27:28 +000093}
94
Joe Hershbergerf8928f12011-11-11 15:55:36 -060095/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
96static void set_direction(unsigned gpio, int output)
Tom Warrenb3878b82011-06-17 06:27:28 +000097{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060098 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
99 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000100 u32 u;
101
102 debug("set_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600103 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +0000104
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600105 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
Stephen Warren20a91452015-09-25 10:44:08 -0600106 if (output != DIRECTION_INPUT)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600107 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000108 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600109 u &= ~(1 << GPIO_BIT(gpio));
110 writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000111}
112
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600113/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
114static void set_level(unsigned gpio, int high)
Tom Warrenb3878b82011-06-17 06:27:28 +0000115{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600116 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
117 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000118 u32 u;
119
120 debug("set_level: port = %d, bit %d == %d\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600121 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
Tom Warrenb3878b82011-06-17 06:27:28 +0000122
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600123 u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000124 if (high)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600125 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000126 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600127 u &= ~(1 << GPIO_BIT(gpio));
128 writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000129}
130
131/*
132 * Generic_GPIO primitives.
133 */
134
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600135/* set GPIO pin 'gpio' as an input */
Simon Glassb0461042014-09-03 17:37:03 -0600136static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000137{
Simon Glassb0461042014-09-03 17:37:03 -0600138 struct tegra_port_info *state = dev_get_priv(dev);
Tom Warrenb3878b82011-06-17 06:27:28 +0000139
140 /* Configure GPIO direction as input. */
Stephen Warren20a91452015-09-25 10:44:08 -0600141 set_direction(state->base_gpio + offset, DIRECTION_INPUT);
Tom Warrenb3878b82011-06-17 06:27:28 +0000142
Stephen Warren6904f6d2015-09-23 12:13:00 -0600143 /* Enable the pin as a GPIO */
144 set_config(state->base_gpio + offset, 1);
145
Tom Warrenb3878b82011-06-17 06:27:28 +0000146 return 0;
147}
148
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600149/* set GPIO pin 'gpio' as an output, with polarity 'value' */
Simon Glassb0461042014-09-03 17:37:03 -0600150static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
151 int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000152{
Simon Glassb0461042014-09-03 17:37:03 -0600153 struct tegra_port_info *state = dev_get_priv(dev);
154 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000155
156 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600157 set_level(gpio, value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000158
159 /* Configure GPIO direction as output. */
Stephen Warren20a91452015-09-25 10:44:08 -0600160 set_direction(gpio, DIRECTION_OUTPUT);
Tom Warrenb3878b82011-06-17 06:27:28 +0000161
Stephen Warren6904f6d2015-09-23 12:13:00 -0600162 /* Enable the pin as a GPIO */
163 set_config(state->base_gpio + offset, 1);
164
Tom Warrenb3878b82011-06-17 06:27:28 +0000165 return 0;
166}
167
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600168/* read GPIO IN value of pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600169static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000170{
Simon Glassb0461042014-09-03 17:37:03 -0600171 struct tegra_port_info *state = dev_get_priv(dev);
172 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000173 int val;
174
Simon Glassb0461042014-09-03 17:37:03 -0600175 debug("%s: pin = %d (port %d:bit %d)\n", __func__,
176 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
177
Simon Glass5f4de1f2016-01-30 16:37:45 -0700178 if (get_direction(gpio) == DIRECTION_INPUT)
179 val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
180 else
181 val = readl(&state->bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000182
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600183 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +0000184}
185
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600186/* write GPIO OUT value to pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600187static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000188{
Simon Glassb0461042014-09-03 17:37:03 -0600189 struct tegra_port_info *state = dev_get_priv(dev);
190 int gpio = state->base_gpio + offset;
Simon Glassb0461042014-09-03 17:37:03 -0600191
Tom Warrenb3878b82011-06-17 06:27:28 +0000192 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
Simon Glassb0461042014-09-03 17:37:03 -0600193 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000194
195 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600196 set_level(gpio, value);
197
198 return 0;
Tom Warrenb3878b82011-06-17 06:27:28 +0000199}
200
Stephen Warren4b27bf12014-04-22 14:37:53 -0600201void gpio_config_table(const struct tegra_gpio_config *config, int len)
202{
203 int i;
204
205 for (i = 0; i < len; i++) {
206 switch (config[i].init) {
207 case TEGRA_GPIO_INIT_IN:
Stephen Warren20a91452015-09-25 10:44:08 -0600208 set_direction(config[i].gpio, DIRECTION_INPUT);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600209 break;
210 case TEGRA_GPIO_INIT_OUT0:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600211 set_level(config[i].gpio, 0);
Stephen Warren20a91452015-09-25 10:44:08 -0600212 set_direction(config[i].gpio, DIRECTION_OUTPUT);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600213 break;
214 case TEGRA_GPIO_INIT_OUT1:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600215 set_level(config[i].gpio, 1);
Stephen Warren20a91452015-09-25 10:44:08 -0600216 set_direction(config[i].gpio, DIRECTION_OUTPUT);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600217 break;
218 }
Stephen Warren20a91452015-09-25 10:44:08 -0600219 set_config(config[i].gpio, CONFIG_GPIO);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600220 }
221}
222
Simon Glassb0461042014-09-03 17:37:03 -0600223static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
224{
225 struct tegra_port_info *state = dev_get_priv(dev);
226 int gpio = state->base_gpio + offset;
227
Simon Glassb0461042014-09-03 17:37:03 -0600228 if (!get_config(gpio))
229 return GPIOF_FUNC;
230 else if (get_direction(gpio))
231 return GPIOF_OUTPUT;
232 else
233 return GPIOF_INPUT;
234}
235
Simon Glassada3b752015-01-05 20:05:33 -0700236static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600237 struct ofnode_phandle_args *args)
Simon Glassada3b752015-01-05 20:05:33 -0700238{
239 int gpio, port, ret;
240
241 gpio = args->args[0];
242 port = gpio / TEGRA_GPIOS_PER_PORT;
243 ret = device_get_child(dev, port, &desc->dev);
244 if (ret)
245 return ret;
246 desc->offset = gpio % TEGRA_GPIOS_PER_PORT;
247 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
248
249 return 0;
250}
251
Simon Glassb0461042014-09-03 17:37:03 -0600252static const struct dm_gpio_ops gpio_tegra_ops = {
Simon Glassb0461042014-09-03 17:37:03 -0600253 .direction_input = tegra_gpio_direction_input,
254 .direction_output = tegra_gpio_direction_output,
255 .get_value = tegra_gpio_get_value,
256 .set_value = tegra_gpio_set_value,
257 .get_function = tegra_gpio_get_function,
Simon Glassada3b752015-01-05 20:05:33 -0700258 .xlate = tegra_gpio_xlate,
Simon Glassb0461042014-09-03 17:37:03 -0600259};
260
261/**
262 * Returns the name of a GPIO port
263 *
264 * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
265 *
266 * @base_port: Base port number (0, 1..n-1)
267 * @return allocated string containing the name
Tom Warrenb3878b82011-06-17 06:27:28 +0000268 */
Simon Glassb0461042014-09-03 17:37:03 -0600269static char *gpio_port_name(int base_port)
Tom Warrenb3878b82011-06-17 06:27:28 +0000270{
Simon Glassb0461042014-09-03 17:37:03 -0600271 char *name, *s;
Tom Warrenb3878b82011-06-17 06:27:28 +0000272
Simon Glassb0461042014-09-03 17:37:03 -0600273 name = malloc(3);
274 if (name) {
275 s = name;
276 *s++ = 'A' + (base_port % 26);
277 if (base_port >= 26)
278 *s++ = *name;
279 *s = '\0';
Tom Warrenb3878b82011-06-17 06:27:28 +0000280 }
Simon Glassb0461042014-09-03 17:37:03 -0600281
282 return name;
Tom Warrenb3878b82011-06-17 06:27:28 +0000283}
Simon Glassb0461042014-09-03 17:37:03 -0600284
285static const struct udevice_id tegra_gpio_ids[] = {
286 { .compatible = "nvidia,tegra30-gpio" },
287 { .compatible = "nvidia,tegra20-gpio" },
288 { }
289};
290
291static int gpio_tegra_probe(struct udevice *dev)
292{
Simon Glassde0977b2015-03-05 12:25:20 -0700293 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb0461042014-09-03 17:37:03 -0600294 struct tegra_port_info *priv = dev->priv;
295 struct tegra_gpio_platdata *plat = dev->platdata;
296
297 /* Only child devices have ports */
298 if (!plat)
299 return 0;
300
301 priv->bank = plat->bank;
302 priv->base_gpio = plat->base_gpio;
303
304 uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
305 uc_priv->bank_name = plat->port_name;
306
307 return 0;
308}
309
310/**
311 * We have a top-level GPIO device with no actual GPIOs. It has a child
312 * device for each Tegra port.
313 */
314static int gpio_tegra_bind(struct udevice *parent)
315{
316 struct tegra_gpio_platdata *plat = parent->platdata;
317 struct gpio_ctlr *ctlr;
318 int bank_count;
319 int bank;
320 int ret;
Simon Glassb0461042014-09-03 17:37:03 -0600321
322 /* If this is a child device, there is nothing to do here */
323 if (plat)
324 return 0;
325
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700326 /* TODO(sjg@chromium.org): Remove once SPL supports device tree */
327#ifdef CONFIG_SPL_BUILD
328 ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
329 bank_count = TEGRA_GPIO_BANKS;
330#else
331 {
332 int len;
333
Simon Glassb0461042014-09-03 17:37:03 -0600334 /*
335 * This driver does not make use of interrupts, other than to figure
336 * out the number of GPIO banks
337 */
Simon Glassb0ca8492017-07-25 08:30:03 -0600338 len = dev_read_size(parent, "interrupts");
339 if (len < 0)
340 return len;
Simon Glassb0461042014-09-03 17:37:03 -0600341 bank_count = len / 3 / sizeof(u32);
Simon Glassb0ca8492017-07-25 08:30:03 -0600342 ctlr = (struct gpio_ctlr *)dev_read_addr(parent);
343 if ((ulong)ctlr == FDT_ADDR_T_NONE)
344 return -EINVAL;
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700345 }
346#endif
Simon Glassb0461042014-09-03 17:37:03 -0600347 for (bank = 0; bank < bank_count; bank++) {
348 int port;
349
350 for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
351 struct tegra_gpio_platdata *plat;
352 struct udevice *dev;
353 int base_port;
354
355 plat = calloc(1, sizeof(*plat));
356 if (!plat)
357 return -ENOMEM;
358 plat->bank = &ctlr->gpio_bank[bank];
359 base_port = bank * TEGRA_PORTS_PER_BANK + port;
360 plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
361 plat->port_name = gpio_port_name(base_port);
362
363 ret = device_bind(parent, parent->driver,
364 plat->port_name, plat, -1, &dev);
365 if (ret)
366 return ret;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700367 dev_set_of_offset(dev, dev_of_offset(parent));
Simon Glassb0461042014-09-03 17:37:03 -0600368 }
369 }
370
371 return 0;
372}
373
374U_BOOT_DRIVER(gpio_tegra) = {
375 .name = "gpio_tegra",
376 .id = UCLASS_GPIO,
377 .of_match = tegra_gpio_ids,
378 .bind = gpio_tegra_bind,
379 .probe = gpio_tegra_probe,
380 .priv_auto_alloc_size = sizeof(struct tegra_port_info),
381 .ops = &gpio_tegra_ops,
382};