blob: 88f7ef5bf04d9abce9605cd7e8e161a11b135a8d [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>
24
25DECLARE_GLOBAL_DATA_PTR;
Tom Warrenb3878b82011-06-17 06:27:28 +000026
27enum {
Tom Warren22562a42012-09-04 17:00:24 -070028 TEGRA_CMD_INFO,
29 TEGRA_CMD_PORT,
30 TEGRA_CMD_OUTPUT,
31 TEGRA_CMD_INPUT,
Tom Warrenb3878b82011-06-17 06:27:28 +000032};
33
Simon Glassb0461042014-09-03 17:37:03 -060034struct tegra_gpio_platdata {
35 struct gpio_ctlr_bank *bank;
36 const char *port_name; /* Name of port, e.g. "B" */
37 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
38};
Tom Warrenb3878b82011-06-17 06:27:28 +000039
Simon Glassb0461042014-09-03 17:37:03 -060040/* Information about each port at run-time */
41struct tegra_port_info {
Simon Glassb0461042014-09-03 17:37:03 -060042 struct gpio_ctlr_bank *bank;
43 int base_gpio; /* Port number for this port (0, 1,.., n-1) */
44};
Tom Warrenb3878b82011-06-17 06:27:28 +000045
Joe Hershbergerf8928f12011-11-11 15:55:36 -060046/* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */
47static int get_config(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000048{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060049 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
50 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000051 u32 u;
52 int type;
53
Joe Hershbergerf8928f12011-11-11 15:55:36 -060054 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
55 type = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000056
57 debug("get_config: port = %d, bit = %d is %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060058 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000059
60 return type;
61}
62
Joe Hershbergerf8928f12011-11-11 15:55:36 -060063/* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */
64static void set_config(unsigned gpio, int type)
Tom Warrenb3878b82011-06-17 06:27:28 +000065{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060066 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
67 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000068 u32 u;
69
70 debug("set_config: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060071 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warrenb3878b82011-06-17 06:27:28 +000072
Joe Hershbergerf8928f12011-11-11 15:55:36 -060073 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000074 if (type) /* GPIO */
Joe Hershbergerf8928f12011-11-11 15:55:36 -060075 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +000076 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060077 u &= ~(1 << GPIO_BIT(gpio));
78 writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +000079}
80
Joe Hershbergerf8928f12011-11-11 15:55:36 -060081/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
82static int get_direction(unsigned gpio)
Tom Warrenb3878b82011-06-17 06:27:28 +000083{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060084 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
85 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +000086 u32 u;
87 int dir;
88
Joe Hershbergerf8928f12011-11-11 15:55:36 -060089 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
90 dir = (u >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +000091
92 debug("get_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -060093 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +000094
95 return dir;
96}
97
Joe Hershbergerf8928f12011-11-11 15:55:36 -060098/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
99static void set_direction(unsigned gpio, int output)
Tom Warrenb3878b82011-06-17 06:27:28 +0000100{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600101 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
102 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000103 u32 u;
104
105 debug("set_direction: port = %d, bit = %d, %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600106 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
Tom Warrenb3878b82011-06-17 06:27:28 +0000107
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600108 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000109 if (output)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600110 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000111 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600112 u &= ~(1 << GPIO_BIT(gpio));
113 writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000114}
115
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600116/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
117static void set_level(unsigned gpio, int high)
Tom Warrenb3878b82011-06-17 06:27:28 +0000118{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600119 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
120 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warrenb3878b82011-06-17 06:27:28 +0000121 u32 u;
122
123 debug("set_level: port = %d, bit %d == %d\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600124 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
Tom Warrenb3878b82011-06-17 06:27:28 +0000125
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600126 u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000127 if (high)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600128 u |= 1 << GPIO_BIT(gpio);
Tom Warrenb3878b82011-06-17 06:27:28 +0000129 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600130 u &= ~(1 << GPIO_BIT(gpio));
131 writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000132}
133
Simon Glassb0461042014-09-03 17:37:03 -0600134/* set GPIO pin 'gpio' as an output, with polarity 'value' */
135int tegra_spl_gpio_direction_output(int gpio, int value)
136{
137 /* Configure as a GPIO */
138 set_config(gpio, 1);
139
140 /* Configure GPIO output value. */
141 set_level(gpio, value);
142
143 /* Configure GPIO direction as output. */
144 set_direction(gpio, 1);
145
146 return 0;
147}
148
Tom Warrenb3878b82011-06-17 06:27:28 +0000149/*
150 * Generic_GPIO primitives.
151 */
152
Simon Glassb0461042014-09-03 17:37:03 -0600153static int tegra_gpio_request(struct udevice *dev, unsigned offset,
154 const char *label)
Tom Warrenb3878b82011-06-17 06:27:28 +0000155{
Simon Glassb0461042014-09-03 17:37:03 -0600156 struct tegra_port_info *state = dev_get_priv(dev);
Tom Warrenb3878b82011-06-17 06:27:28 +0000157
Tom Warrenb3878b82011-06-17 06:27:28 +0000158 /* Configure as a GPIO */
Simon Glassb0461042014-09-03 17:37:03 -0600159 set_config(state->base_gpio + offset, 1);
Tom Warrenb3878b82011-06-17 06:27:28 +0000160
161 return 0;
162}
163
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600164/* set GPIO pin 'gpio' as an input */
Simon Glassb0461042014-09-03 17:37:03 -0600165static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000166{
Simon Glassb0461042014-09-03 17:37:03 -0600167 struct tegra_port_info *state = dev_get_priv(dev);
Tom Warrenb3878b82011-06-17 06:27:28 +0000168
169 /* Configure GPIO direction as input. */
Simon Glassb0461042014-09-03 17:37:03 -0600170 set_direction(state->base_gpio + offset, 0);
Tom Warrenb3878b82011-06-17 06:27:28 +0000171
172 return 0;
173}
174
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600175/* set GPIO pin 'gpio' as an output, with polarity 'value' */
Simon Glassb0461042014-09-03 17:37:03 -0600176static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
177 int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000178{
Simon Glassb0461042014-09-03 17:37:03 -0600179 struct tegra_port_info *state = dev_get_priv(dev);
180 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000181
182 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600183 set_level(gpio, value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000184
185 /* Configure GPIO direction as output. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600186 set_direction(gpio, 1);
Tom Warrenb3878b82011-06-17 06:27:28 +0000187
188 return 0;
189}
190
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600191/* read GPIO IN value of pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600192static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
Tom Warrenb3878b82011-06-17 06:27:28 +0000193{
Simon Glassb0461042014-09-03 17:37:03 -0600194 struct tegra_port_info *state = dev_get_priv(dev);
195 int gpio = state->base_gpio + offset;
Tom Warrenb3878b82011-06-17 06:27:28 +0000196 int val;
197
Simon Glassb0461042014-09-03 17:37:03 -0600198 debug("%s: pin = %d (port %d:bit %d)\n", __func__,
199 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
200
201 val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
Tom Warrenb3878b82011-06-17 06:27:28 +0000202
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600203 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warrenb3878b82011-06-17 06:27:28 +0000204}
205
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600206/* write GPIO OUT value to pin 'gpio' */
Simon Glassb0461042014-09-03 17:37:03 -0600207static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Tom Warrenb3878b82011-06-17 06:27:28 +0000208{
Simon Glassb0461042014-09-03 17:37:03 -0600209 struct tegra_port_info *state = dev_get_priv(dev);
210 int gpio = state->base_gpio + offset;
Simon Glassb0461042014-09-03 17:37:03 -0600211
Tom Warrenb3878b82011-06-17 06:27:28 +0000212 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
Simon Glassb0461042014-09-03 17:37:03 -0600213 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
Tom Warrenb3878b82011-06-17 06:27:28 +0000214
215 /* Configure GPIO output value. */
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600216 set_level(gpio, value);
217
218 return 0;
Tom Warrenb3878b82011-06-17 06:27:28 +0000219}
220
Stephen Warren4b27bf12014-04-22 14:37:53 -0600221void gpio_config_table(const struct tegra_gpio_config *config, int len)
222{
223 int i;
224
225 for (i = 0; i < len; i++) {
226 switch (config[i].init) {
227 case TEGRA_GPIO_INIT_IN:
228 gpio_direction_input(config[i].gpio);
229 break;
230 case TEGRA_GPIO_INIT_OUT0:
231 gpio_direction_output(config[i].gpio, 0);
232 break;
233 case TEGRA_GPIO_INIT_OUT1:
234 gpio_direction_output(config[i].gpio, 1);
235 break;
236 }
237 set_config(config[i].gpio, 1);
238 }
239}
240
Simon Glassb0461042014-09-03 17:37:03 -0600241static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
242{
243 struct tegra_port_info *state = dev_get_priv(dev);
244 int gpio = state->base_gpio + offset;
245
Simon Glassb0461042014-09-03 17:37:03 -0600246 if (!get_config(gpio))
247 return GPIOF_FUNC;
248 else if (get_direction(gpio))
249 return GPIOF_OUTPUT;
250 else
251 return GPIOF_INPUT;
252}
253
Simon Glassb0461042014-09-03 17:37:03 -0600254static const struct dm_gpio_ops gpio_tegra_ops = {
255 .request = tegra_gpio_request,
Simon Glassb0461042014-09-03 17:37:03 -0600256 .direction_input = tegra_gpio_direction_input,
257 .direction_output = tegra_gpio_direction_output,
258 .get_value = tegra_gpio_get_value,
259 .set_value = tegra_gpio_set_value,
260 .get_function = tegra_gpio_get_function,
Simon Glassb0461042014-09-03 17:37:03 -0600261};
262
263/**
264 * Returns the name of a GPIO port
265 *
266 * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
267 *
268 * @base_port: Base port number (0, 1..n-1)
269 * @return allocated string containing the name
Tom Warrenb3878b82011-06-17 06:27:28 +0000270 */
Simon Glassb0461042014-09-03 17:37:03 -0600271static char *gpio_port_name(int base_port)
Tom Warrenb3878b82011-06-17 06:27:28 +0000272{
Simon Glassb0461042014-09-03 17:37:03 -0600273 char *name, *s;
Tom Warrenb3878b82011-06-17 06:27:28 +0000274
Simon Glassb0461042014-09-03 17:37:03 -0600275 name = malloc(3);
276 if (name) {
277 s = name;
278 *s++ = 'A' + (base_port % 26);
279 if (base_port >= 26)
280 *s++ = *name;
281 *s = '\0';
Tom Warrenb3878b82011-06-17 06:27:28 +0000282 }
Simon Glassb0461042014-09-03 17:37:03 -0600283
284 return name;
Tom Warrenb3878b82011-06-17 06:27:28 +0000285}
Simon Glassb0461042014-09-03 17:37:03 -0600286
287static const struct udevice_id tegra_gpio_ids[] = {
288 { .compatible = "nvidia,tegra30-gpio" },
289 { .compatible = "nvidia,tegra20-gpio" },
290 { }
291};
292
293static int gpio_tegra_probe(struct udevice *dev)
294{
295 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
296 struct tegra_port_info *priv = dev->priv;
297 struct tegra_gpio_platdata *plat = dev->platdata;
298
299 /* Only child devices have ports */
300 if (!plat)
301 return 0;
302
303 priv->bank = plat->bank;
304 priv->base_gpio = plat->base_gpio;
305
306 uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
307 uc_priv->bank_name = plat->port_name;
308
309 return 0;
310}
311
312/**
313 * We have a top-level GPIO device with no actual GPIOs. It has a child
314 * device for each Tegra port.
315 */
316static int gpio_tegra_bind(struct udevice *parent)
317{
318 struct tegra_gpio_platdata *plat = parent->platdata;
319 struct gpio_ctlr *ctlr;
320 int bank_count;
321 int bank;
322 int ret;
323 int len;
324
325 /* If this is a child device, there is nothing to do here */
326 if (plat)
327 return 0;
328
329 /*
330 * This driver does not make use of interrupts, other than to figure
331 * out the number of GPIO banks
332 */
333 if (!fdt_getprop(gd->fdt_blob, parent->of_offset, "interrupts", &len))
334 return -EINVAL;
335 bank_count = len / 3 / sizeof(u32);
336 ctlr = (struct gpio_ctlr *)fdtdec_get_addr(gd->fdt_blob,
337 parent->of_offset, "reg");
338 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,
373};