blob: c0ae7719e2c9b97c2952ea50ca01383543570bab [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
28enum {
Tom Warren22562a42012-09-04 17:00:24 -070029 TEGRA_CMD_INFO,
30 TEGRA_CMD_PORT,
31 TEGRA_CMD_OUTPUT,
32 TEGRA_CMD_INPUT,
Tom Warrenb3878b82011-06-17 06:27:28 +000033};
34
Simon Glassb0461042014-09-03 17:37:03 -060035struct tegra_gpio_platdata {
36 struct gpio_ctlr_bank *bank;
37 const char *port_name; /* Name of port, e.g. "B" */
38 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
39};
Tom Warrenb3878b82011-06-17 06:27:28 +000040
Simon Glassb0461042014-09-03 17:37:03 -060041/* Information about each port at run-time */
42struct tegra_port_info {
Simon Glassb0461042014-09-03 17:37:03 -060043 struct gpio_ctlr_bank *bank;
44 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
45};
Tom Warrenb3878b82011-06-17 06:27:28 +000046
Joe Hershbergerf8928f12011-11-11 15:55:36 -060047/* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */
48static int get_config(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000049{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060050 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
51 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000052 u32 u;
53 int type;
54
Joe Hershbergerf8928f12011-11-11 15:55:36 -060055 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
56 type = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000057
58 debug("get_config: port = %d, bit = %d is %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060059 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000060
61 return type;
62}
63
Joe Hershbergerf8928f12011-11-11 15:55:36 -060064/* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */
65static void set_config(unsigned gpio, int type)
Tom Warrenb3878b82011-06-17 06:27:28 +000066{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060067 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
68 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000069 u32 u;
70
71 debug("set_config: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060072 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000073
Joe Hershbergerf8928f12011-11-11 15:55:36 -060074 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000075 if (type) /* GPIO */
Joe Hershbergerf8928f12011-11-11 15:55:36 -060076 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +000077 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060078 u &= ~(1 << GPIO_BIT(gpio));
79 writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000080}
81
Joe Hershbergerf8928f12011-11-11 15:55:36 -060082/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
83static int get_direction(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000084{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060085 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
86 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000087 u32 u;
88 int dir;
89
Joe Hershbergerf8928f12011-11-11 15:55:36 -060090 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
91 dir = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000092
93 debug("get_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060094 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +000095
96 return dir;
97}
98
Joe Hershbergerf8928f12011-11-11 15:55:36 -060099/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
100static void set_direction(unsigned gpio, int output)
Tom Warrenb3878b82011-06-17 06:27:28 +0000101{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600102 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
103 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000104 u32 u;
105
106 debug("set_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600107 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +0000108
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600109 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000110 if (output)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600111 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000112 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600113 u &= ~(1 << GPIO_BIT(gpio));
114 writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000115}
116
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600117/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
118static void set_level(unsigned gpio, int high)
Tom Warrenb3878b82011-06-17 06:27:28 +0000119{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600120 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
121 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000122 u32 u;
123
124 debug("set_level: port = %d, bit %d == %d\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600125 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
Tom Warrenb3878b82011-06-17 06:27:28 +0000126
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600127 u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000128 if (high)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600129 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000130 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600131 u &= ~(1 << GPIO_BIT(gpio));
132 writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000133}
134
135/*
136 * Generic_GPIO primitives.
137 */
138
Simon Glassb0461042014-09-03 17:37:03 -0600139static int tegra_gpio_request(struct udevice *dev, unsigned offset,
140 const char *label)
Tom Warrenb3878b82011-06-17 06:27:28 +0000141{
Simon Glassb0461042014-09-03 17:37:03 -0600142 struct tegra_port_info *state = dev_get_priv(dev);
Tom Warrenb3878b82011-06-17 06:27:28 +0000143
Tom Warrenb3878b82011-06-17 06:27:28 +0000144 /* Configure as a GPIO */
Simon Glassb0461042014-09-03 17:37:03 -0600145 set_config(state->base_gpio + offset, 1);
Tom Warrenb3878b82011-06-17 06:27:28 +0000146
147 return 0;
148}
149
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600150/* set GPIO pin 'gpio' as an input */
Simon Glassb0461042014-09-03 17:37:03 -0600151static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
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);
Tom Warrenb3878b82011-06-17 06:27:28 +0000154
155 /* Configure GPIO direction as input. */
Simon Glassb0461042014-09-03 17:37:03 -0600156 set_direction(state->base_gpio + offset, 0);
Tom Warrenb3878b82011-06-17 06:27:28 +0000157
158 return 0;
159}
160
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600161/* set GPIO pin 'gpio' as an output, with polarity 'value' */
Simon Glassb0461042014-09-03 17:37:03 -0600162static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
163 int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000164{
Simon Glassb0461042014-09-03 17:37:03 -0600165 struct tegra_port_info *state = dev_get_priv(dev);
166 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000167
168 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600169 set_level(gpio, value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000170
171 /* Configure GPIO direction as output. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600172 set_direction(gpio, 1);
Tom Warrenb3878b82011-06-17 06:27:28 +0000173
174 return 0;
175}
176
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600177/* read GPIO IN value of pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600178static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000179{
Simon Glassb0461042014-09-03 17:37:03 -0600180 struct tegra_port_info *state = dev_get_priv(dev);
181 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000182 int val;
183
Simon Glassb0461042014-09-03 17:37:03 -0600184 debug("%s: pin = %d (port %d:bit %d)\n", __func__,
185 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
186
187 val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000188
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600189 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +0000190}
191
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600192/* write GPIO OUT value to pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600193static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000194{
Simon Glassb0461042014-09-03 17:37:03 -0600195 struct tegra_port_info *state = dev_get_priv(dev);
196 int gpio = state->base_gpio + offset;
Simon Glassb0461042014-09-03 17:37:03 -0600197
Tom Warrenb3878b82011-06-17 06:27:28 +0000198 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
Simon Glassb0461042014-09-03 17:37:03 -0600199 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000200
201 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600202 set_level(gpio, value);
203
204 return 0;
Tom Warrenb3878b82011-06-17 06:27:28 +0000205}
206
Stephen Warren4b27bf12014-04-22 14:37:53 -0600207void gpio_config_table(const struct tegra_gpio_config *config, int len)
208{
209 int i;
210
211 for (i = 0; i < len; i++) {
212 switch (config[i].init) {
213 case TEGRA_GPIO_INIT_IN:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600214 set_direction(config[i].gpio, 0);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600215 break;
216 case TEGRA_GPIO_INIT_OUT0:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600217 set_level(config[i].gpio, 0);
218 set_direction(config[i].gpio, 1);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600219 break;
220 case TEGRA_GPIO_INIT_OUT1:
Stephen Warrenc6f54022015-09-23 12:12:59 -0600221 set_level(config[i].gpio, 1);
222 set_direction(config[i].gpio, 1);
Stephen Warren4b27bf12014-04-22 14:37:53 -0600223 break;
224 }
225 set_config(config[i].gpio, 1);
226 }
227}
228
Simon Glassb0461042014-09-03 17:37:03 -0600229static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
230{
231 struct tegra_port_info *state = dev_get_priv(dev);
232 int gpio = state->base_gpio + offset;
233
Simon Glassb0461042014-09-03 17:37:03 -0600234 if (!get_config(gpio))
235 return GPIOF_FUNC;
236 else if (get_direction(gpio))
237 return GPIOF_OUTPUT;
238 else
239 return GPIOF_INPUT;
240}
241
Simon Glassada3b752015-01-05 20:05:33 -0700242static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
243 struct fdtdec_phandle_args *args)
244{
245 int gpio, port, ret;
246
247 gpio = args->args[0];
248 port = gpio / TEGRA_GPIOS_PER_PORT;
249 ret = device_get_child(dev, port, &desc->dev);
250 if (ret)
251 return ret;
252 desc->offset = gpio % TEGRA_GPIOS_PER_PORT;
253 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
254
255 return 0;
256}
257
Simon Glassb0461042014-09-03 17:37:03 -0600258static const struct dm_gpio_ops gpio_tegra_ops = {
259 .request = tegra_gpio_request,
Simon Glassb0461042014-09-03 17:37:03 -0600260 .direction_input = tegra_gpio_direction_input,
261 .direction_output = tegra_gpio_direction_output,
262 .get_value = tegra_gpio_get_value,
263 .set_value = tegra_gpio_set_value,
264 .get_function = tegra_gpio_get_function,
Simon Glassada3b752015-01-05 20:05:33 -0700265 .xlate = tegra_gpio_xlate,
Simon Glassb0461042014-09-03 17:37:03 -0600266};
267
268/**
269 * Returns the name of a GPIO port
270 *
271 * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
272 *
273 * @base_port: Base port number (0, 1..n-1)
274 * @return allocated string containing the name
Tom Warrenb3878b82011-06-17 06:27:28 +0000275 */
Simon Glassb0461042014-09-03 17:37:03 -0600276static char *gpio_port_name(int base_port)
Tom Warrenb3878b82011-06-17 06:27:28 +0000277{
Simon Glassb0461042014-09-03 17:37:03 -0600278 char *name, *s;
Tom Warrenb3878b82011-06-17 06:27:28 +0000279
Simon Glassb0461042014-09-03 17:37:03 -0600280 name = malloc(3);
281 if (name) {
282 s = name;
283 *s++ = 'A' + (base_port % 26);
284 if (base_port >= 26)
285 *s++ = *name;
286 *s = '\0';
Tom Warrenb3878b82011-06-17 06:27:28 +0000287 }
Simon Glassb0461042014-09-03 17:37:03 -0600288
289 return name;
Tom Warrenb3878b82011-06-17 06:27:28 +0000290}
Simon Glassb0461042014-09-03 17:37:03 -0600291
292static const struct udevice_id tegra_gpio_ids[] = {
293 { .compatible = "nvidia,tegra30-gpio" },
294 { .compatible = "nvidia,tegra20-gpio" },
295 { }
296};
297
298static int gpio_tegra_probe(struct udevice *dev)
299{
Simon Glassde0977b2015-03-05 12:25:20 -0700300 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb0461042014-09-03 17:37:03 -0600301 struct tegra_port_info *priv = dev->priv;
302 struct tegra_gpio_platdata *plat = dev->platdata;
303
304 /* Only child devices have ports */
305 if (!plat)
306 return 0;
307
308 priv->bank = plat->bank;
309 priv->base_gpio = plat->base_gpio;
310
311 uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
312 uc_priv->bank_name = plat->port_name;
313
314 return 0;
315}
316
317/**
318 * We have a top-level GPIO device with no actual GPIOs. It has a child
319 * device for each Tegra port.
320 */
321static int gpio_tegra_bind(struct udevice *parent)
322{
323 struct tegra_gpio_platdata *plat = parent->platdata;
324 struct gpio_ctlr *ctlr;
325 int bank_count;
326 int bank;
327 int ret;
Simon Glassb0461042014-09-03 17:37:03 -0600328
329 /* If this is a child device, there is nothing to do here */
330 if (plat)
331 return 0;
332
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700333 /* TODO(sjg@chromium.org): Remove once SPL supports device tree */
334#ifdef CONFIG_SPL_BUILD
335 ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
336 bank_count = TEGRA_GPIO_BANKS;
337#else
338 {
339 int len;
340
Simon Glassb0461042014-09-03 17:37:03 -0600341 /*
342 * This driver does not make use of interrupts, other than to figure
343 * out the number of GPIO banks
344 */
345 if (!fdt_getprop(gd->fdt_blob, parent->of_offset, "interrupts", &len))
346 return -EINVAL;
347 bank_count = len / 3 / sizeof(u32);
Simon Glass09717782015-08-11 08:33:29 -0600348 ctlr = (struct gpio_ctlr *)dev_get_addr(parent);
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700349 }
350#endif
Simon Glassb0461042014-09-03 17:37:03 -0600351 for (bank = 0; bank < bank_count; bank++) {
352 int port;
353
354 for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
355 struct tegra_gpio_platdata *plat;
356 struct udevice *dev;
357 int base_port;
358
359 plat = calloc(1, sizeof(*plat));
360 if (!plat)
361 return -ENOMEM;
362 plat->bank = &ctlr->gpio_bank[bank];
363 base_port = bank * TEGRA_PORTS_PER_BANK + port;
364 plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
365 plat->port_name = gpio_port_name(base_port);
366
367 ret = device_bind(parent, parent->driver,
368 plat->port_name, plat, -1, &dev);
369 if (ret)
370 return ret;
371 dev->of_offset = parent->of_offset;
372 }
373 }
374
375 return 0;
376}
377
378U_BOOT_DRIVER(gpio_tegra) = {
379 .name = "gpio_tegra",
380 .id = UCLASS_GPIO,
381 .of_match = tegra_gpio_ids,
382 .bind = gpio_tegra_bind,
383 .probe = gpio_tegra_probe,
384 .priv_auto_alloc_size = sizeof(struct tegra_port_info),
385 .ops = &gpio_tegra_ops,
Simon Glasse3e5a9f2015-03-03 08:02:59 -0700386 .flags = DM_FLAG_PRE_RELOC,
Simon Glassb0461042014-09-03 17:37:03 -0600387};