blob: fea9d17f8e628b1a98fdbd2c921396ba78f3d644 [file] [log] [blame]
Tom Warren4e5ae092011-06-17 06:27:28 +00001/*
Allen Martin00a27492012-08-31 08:30:00 +00002 * NVIDIA Tegra20 GPIO handling.
Tom Warren52a8b822012-05-22 12:19:25 +00003 * (C) Copyright 2010-2012
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>
15#include <asm/io.h>
16#include <asm/bitops.h>
Tom Warren150c2492012-09-19 15:50:56 -070017#include <asm/arch/tegra.h>
Tom Warren4e5ae092011-06-17 06:27:28 +000018#include <asm/gpio.h>
19
20enum {
Tom Warren29f3e3f2012-09-04 17:00:24 -070021 TEGRA_CMD_INFO,
22 TEGRA_CMD_PORT,
23 TEGRA_CMD_OUTPUT,
24 TEGRA_CMD_INPUT,
Tom Warren4e5ae092011-06-17 06:27:28 +000025};
26
27static struct gpio_names {
28 char name[GPIO_NAME_SIZE];
29} gpio_names[MAX_NUM_GPIOS];
30
31static char *get_name(int i)
32{
33 return *gpio_names[i].name ? gpio_names[i].name : "UNKNOWN";
34}
35
Joe Hershberger365d6072011-11-11 15:55:36 -060036/* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */
37static int get_config(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +000038{
Joe Hershberger365d6072011-11-11 15:55:36 -060039 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
40 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +000041 u32 u;
42 int type;
43
Joe Hershberger365d6072011-11-11 15:55:36 -060044 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
45 type = (u >> GPIO_BIT(gpio)) & 1;
Tom Warren4e5ae092011-06-17 06:27:28 +000046
47 debug("get_config: port = %d, bit = %d is %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -060048 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warren4e5ae092011-06-17 06:27:28 +000049
50 return type;
51}
52
Joe Hershberger365d6072011-11-11 15:55:36 -060053/* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */
54static void set_config(unsigned gpio, int type)
Tom Warren4e5ae092011-06-17 06:27:28 +000055{
Joe Hershberger365d6072011-11-11 15:55:36 -060056 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
57 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +000058 u32 u;
59
60 debug("set_config: port = %d, bit = %d, %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -060061 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
Tom Warren4e5ae092011-06-17 06:27:28 +000062
Joe Hershberger365d6072011-11-11 15:55:36 -060063 u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +000064 if (type) /* GPIO */
Joe Hershberger365d6072011-11-11 15:55:36 -060065 u |= 1 << GPIO_BIT(gpio);
Tom Warren4e5ae092011-06-17 06:27:28 +000066 else
Joe Hershberger365d6072011-11-11 15:55:36 -060067 u &= ~(1 << GPIO_BIT(gpio));
68 writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +000069}
70
Joe Hershberger365d6072011-11-11 15:55:36 -060071/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
72static int get_direction(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +000073{
Joe Hershberger365d6072011-11-11 15:55:36 -060074 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
75 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +000076 u32 u;
77 int dir;
78
Joe Hershberger365d6072011-11-11 15:55:36 -060079 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
80 dir = (u >> GPIO_BIT(gpio)) & 1;
Tom Warren4e5ae092011-06-17 06:27:28 +000081
82 debug("get_direction: port = %d, bit = %d, %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -060083 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
Tom Warren4e5ae092011-06-17 06:27:28 +000084
85 return dir;
86}
87
Joe Hershberger365d6072011-11-11 15:55:36 -060088/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
89static void set_direction(unsigned gpio, int output)
Tom Warren4e5ae092011-06-17 06:27:28 +000090{
Joe Hershberger365d6072011-11-11 15:55:36 -060091 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
92 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +000093 u32 u;
94
95 debug("set_direction: port = %d, bit = %d, %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -060096 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
Tom Warren4e5ae092011-06-17 06:27:28 +000097
Joe Hershberger365d6072011-11-11 15:55:36 -060098 u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +000099 if (output)
Joe Hershberger365d6072011-11-11 15:55:36 -0600100 u |= 1 << GPIO_BIT(gpio);
Tom Warren4e5ae092011-06-17 06:27:28 +0000101 else
Joe Hershberger365d6072011-11-11 15:55:36 -0600102 u &= ~(1 << GPIO_BIT(gpio));
103 writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000104}
105
Joe Hershberger365d6072011-11-11 15:55:36 -0600106/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
107static void set_level(unsigned gpio, int high)
Tom Warren4e5ae092011-06-17 06:27:28 +0000108{
Joe Hershberger365d6072011-11-11 15:55:36 -0600109 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
110 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +0000111 u32 u;
112
113 debug("set_level: port = %d, bit %d == %d\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600114 GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
Tom Warren4e5ae092011-06-17 06:27:28 +0000115
Joe Hershberger365d6072011-11-11 15:55:36 -0600116 u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000117 if (high)
Joe Hershberger365d6072011-11-11 15:55:36 -0600118 u |= 1 << GPIO_BIT(gpio);
Tom Warren4e5ae092011-06-17 06:27:28 +0000119 else
Joe Hershberger365d6072011-11-11 15:55:36 -0600120 u &= ~(1 << GPIO_BIT(gpio));
121 writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000122}
123
124/*
125 * Generic_GPIO primitives.
126 */
127
Joe Hershberger365d6072011-11-11 15:55:36 -0600128int gpio_request(unsigned gpio, const char *label)
Tom Warren4e5ae092011-06-17 06:27:28 +0000129{
Joe Hershberger365d6072011-11-11 15:55:36 -0600130 if (gpio >= MAX_NUM_GPIOS)
Tom Warren4e5ae092011-06-17 06:27:28 +0000131 return -1;
132
Stephen Warren5fac2362011-10-06 12:52:22 +0000133 if (label != NULL) {
Joe Hershberger365d6072011-11-11 15:55:36 -0600134 strncpy(gpio_names[gpio].name, label, GPIO_NAME_SIZE);
135 gpio_names[gpio].name[GPIO_NAME_SIZE - 1] = '\0';
Stephen Warren5fac2362011-10-06 12:52:22 +0000136 }
Tom Warren4e5ae092011-06-17 06:27:28 +0000137
138 /* Configure as a GPIO */
Joe Hershberger365d6072011-11-11 15:55:36 -0600139 set_config(gpio, 1);
Tom Warren4e5ae092011-06-17 06:27:28 +0000140
141 return 0;
142}
143
Joe Hershberger365d6072011-11-11 15:55:36 -0600144int gpio_free(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +0000145{
Joe Hershberger365d6072011-11-11 15:55:36 -0600146 if (gpio >= MAX_NUM_GPIOS)
147 return -1;
148
149 gpio_names[gpio].name[0] = '\0';
150 /* Do not configure as input or change pin mux here */
151 return 0;
Tom Warren4e5ae092011-06-17 06:27:28 +0000152}
153
Joe Hershberger365d6072011-11-11 15:55:36 -0600154/* read GPIO OUT value of pin 'gpio' */
155static int gpio_get_output_value(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +0000156{
Joe Hershberger365d6072011-11-11 15:55:36 -0600157 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
158 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +0000159 int val;
160
161 debug("gpio_get_output_value: pin = %d (port %d:bit %d)\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600162 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
Tom Warren4e5ae092011-06-17 06:27:28 +0000163
Joe Hershberger365d6072011-11-11 15:55:36 -0600164 val = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000165
Joe Hershberger365d6072011-11-11 15:55:36 -0600166 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warren4e5ae092011-06-17 06:27:28 +0000167}
168
Joe Hershberger365d6072011-11-11 15:55:36 -0600169/* set GPIO pin 'gpio' as an input */
170int gpio_direction_input(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +0000171{
172 debug("gpio_direction_input: pin = %d (port %d:bit %d)\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600173 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
Tom Warren4e5ae092011-06-17 06:27:28 +0000174
175 /* Configure GPIO direction as input. */
Joe Hershberger365d6072011-11-11 15:55:36 -0600176 set_direction(gpio, 0);
Tom Warren4e5ae092011-06-17 06:27:28 +0000177
178 return 0;
179}
180
Joe Hershberger365d6072011-11-11 15:55:36 -0600181/* set GPIO pin 'gpio' as an output, with polarity 'value' */
182int gpio_direction_output(unsigned gpio, int value)
Tom Warren4e5ae092011-06-17 06:27:28 +0000183{
184 debug("gpio_direction_output: pin = %d (port %d:bit %d) = %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600185 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio),
186 value ? "HIGH" : "LOW");
Tom Warren4e5ae092011-06-17 06:27:28 +0000187
188 /* Configure GPIO output value. */
Joe Hershberger365d6072011-11-11 15:55:36 -0600189 set_level(gpio, value);
Tom Warren4e5ae092011-06-17 06:27:28 +0000190
191 /* Configure GPIO direction as output. */
Joe Hershberger365d6072011-11-11 15:55:36 -0600192 set_direction(gpio, 1);
Tom Warren4e5ae092011-06-17 06:27:28 +0000193
194 return 0;
195}
196
Joe Hershberger365d6072011-11-11 15:55:36 -0600197/* read GPIO IN value of pin 'gpio' */
198int gpio_get_value(unsigned gpio)
Tom Warren4e5ae092011-06-17 06:27:28 +0000199{
Joe Hershberger365d6072011-11-11 15:55:36 -0600200 struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
201 struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
Tom Warren4e5ae092011-06-17 06:27:28 +0000202 int val;
203
204 debug("gpio_get_value: pin = %d (port %d:bit %d)\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600205 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
Tom Warren4e5ae092011-06-17 06:27:28 +0000206
Joe Hershberger365d6072011-11-11 15:55:36 -0600207 val = readl(&bank->gpio_in[GPIO_PORT(gpio)]);
Tom Warren4e5ae092011-06-17 06:27:28 +0000208
Joe Hershberger365d6072011-11-11 15:55:36 -0600209 return (val >> GPIO_BIT(gpio)) & 1;
Tom Warren4e5ae092011-06-17 06:27:28 +0000210}
211
Joe Hershberger365d6072011-11-11 15:55:36 -0600212/* write GPIO OUT value to pin 'gpio' */
213int gpio_set_value(unsigned gpio, int value)
Tom Warren4e5ae092011-06-17 06:27:28 +0000214{
215 debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600216 gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
Tom Warren4e5ae092011-06-17 06:27:28 +0000217
218 /* Configure GPIO output value. */
Joe Hershberger365d6072011-11-11 15:55:36 -0600219 set_level(gpio, value);
220
221 return 0;
Tom Warren4e5ae092011-06-17 06:27:28 +0000222}
223
Stephen Warreneceb3f22014-04-22 14:37:53 -0600224void gpio_config_table(const struct tegra_gpio_config *config, int len)
225{
226 int i;
227
228 for (i = 0; i < len; i++) {
229 switch (config[i].init) {
230 case TEGRA_GPIO_INIT_IN:
231 gpio_direction_input(config[i].gpio);
232 break;
233 case TEGRA_GPIO_INIT_OUT0:
234 gpio_direction_output(config[i].gpio, 0);
235 break;
236 case TEGRA_GPIO_INIT_OUT1:
237 gpio_direction_output(config[i].gpio, 1);
238 break;
239 }
240 set_config(config[i].gpio, 1);
241 }
242}
243
Tom Warren4e5ae092011-06-17 06:27:28 +0000244/*
245 * Display Tegra GPIO information
246 */
247void gpio_info(void)
248{
Joe Hershberger365d6072011-11-11 15:55:36 -0600249 unsigned c;
250 int type;
Tom Warren4e5ae092011-06-17 06:27:28 +0000251
252 for (c = 0; c < MAX_NUM_GPIOS; c++) {
253 type = get_config(c); /* GPIO, not SFPIO */
254 if (type) {
255 printf("GPIO_%d:\t%s is an %s, ", c,
256 get_name(c),
257 get_direction(c) ? "OUTPUT" : "INPUT");
258 if (get_direction(c))
259 printf("value = %d", gpio_get_output_value(c));
260 else
261 printf("value = %d", gpio_get_value(c));
262 printf("\n");
263 } else
264 continue;
265 }
266}