tegra_gpio.c 6.6 KB
Newer Older
1
/*
A
Allen Martin 已提交
2
 * NVIDIA Tegra20 GPIO handling.
3
 *  (C) Copyright 2010-2012
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *  NVIDIA Corporation <www.nvidia.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*
 * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
 * Tom Warren (twarren@nvidia.com)
 */

#include <common.h>
#include <asm/io.h>
#include <asm/bitops.h>
A
Allen Martin 已提交
33
#include <asm/arch/tegra20.h>
34 35 36
#include <asm/gpio.h>

enum {
37 38 39 40
	TEGRA_CMD_INFO,
	TEGRA_CMD_PORT,
	TEGRA_CMD_OUTPUT,
	TEGRA_CMD_INPUT,
41 42 43 44 45 46 47 48 49 50 51
};

static struct gpio_names {
	char name[GPIO_NAME_SIZE];
} gpio_names[MAX_NUM_GPIOS];

static char *get_name(int i)
{
	return *gpio_names[i].name ? gpio_names[i].name : "UNKNOWN";
}

52 53
/* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */
static int get_config(unsigned gpio)
54
{
55 56
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
57 58 59
	u32 u;
	int type;

60 61
	u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
	type =  (u >> GPIO_BIT(gpio)) & 1;
62 63

	debug("get_config: port = %d, bit = %d is %s\n",
64
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
65 66 67 68

	return type;
}

69 70
/* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */
static void set_config(unsigned gpio, int type)
71
{
72 73
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
74 75 76
	u32 u;

	debug("set_config: port = %d, bit = %d, %s\n",
77
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
78

79
	u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
80
	if (type)				/* GPIO */
81
		u |= 1 << GPIO_BIT(gpio);
82
	else
83 84
		u &= ~(1 << GPIO_BIT(gpio));
	writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
85 86
}

87 88
/* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
static int get_direction(unsigned gpio)
89
{
90 91
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
92 93 94
	u32 u;
	int dir;

95 96
	u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
	dir =  (u >> GPIO_BIT(gpio)) & 1;
97 98

	debug("get_direction: port = %d, bit = %d, %s\n",
99
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
100 101 102 103

	return dir;
}

104 105
/* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
static void set_direction(unsigned gpio, int output)
106
{
107 108
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
109 110 111
	u32 u;

	debug("set_direction: port = %d, bit = %d, %s\n",
112
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
113

114
	u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
115
	if (output)
116
		u |= 1 << GPIO_BIT(gpio);
117
	else
118 119
		u &= ~(1 << GPIO_BIT(gpio));
	writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
120 121
}

122 123
/* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
static void set_level(unsigned gpio, int high)
124
{
125 126
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
127 128 129
	u32 u;

	debug("set_level: port = %d, bit %d == %d\n",
130
		GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
131

132
	u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
133
	if (high)
134
		u |= 1 << GPIO_BIT(gpio);
135
	else
136 137
		u &= ~(1 << GPIO_BIT(gpio));
	writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
138 139 140 141 142 143
}

/*
 * Generic_GPIO primitives.
 */

144
int gpio_request(unsigned gpio, const char *label)
145
{
146
	if (gpio >= MAX_NUM_GPIOS)
147 148
		return -1;

149
	if (label != NULL) {
150 151
		strncpy(gpio_names[gpio].name, label, GPIO_NAME_SIZE);
		gpio_names[gpio].name[GPIO_NAME_SIZE - 1] = '\0';
152
	}
153 154

	/* Configure as a GPIO */
155
	set_config(gpio, 1);
156 157 158 159

	return 0;
}

160
int gpio_free(unsigned gpio)
161
{
162 163 164 165 166 167
	if (gpio >= MAX_NUM_GPIOS)
		return -1;

	gpio_names[gpio].name[0] = '\0';
	/* Do not configure as input or change pin mux here */
	return 0;
168 169
}

170 171
/* read GPIO OUT value of pin 'gpio' */
static int gpio_get_output_value(unsigned gpio)
172
{
173 174
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
175 176 177
	int val;

	debug("gpio_get_output_value: pin = %d (port %d:bit %d)\n",
178
		gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
179

180
	val = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
181

182
	return (val >> GPIO_BIT(gpio)) & 1;
183 184
}

185 186
/* set GPIO pin 'gpio' as an input */
int gpio_direction_input(unsigned gpio)
187 188
{
	debug("gpio_direction_input: pin = %d (port %d:bit %d)\n",
189
		gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
190 191

	/* Configure GPIO direction as input. */
192
	set_direction(gpio, 0);
193 194 195 196

	return 0;
}

197 198
/* set GPIO pin 'gpio' as an output, with polarity 'value' */
int gpio_direction_output(unsigned gpio, int value)
199 200
{
	debug("gpio_direction_output: pin = %d (port %d:bit %d) = %s\n",
201 202
		gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio),
		value ? "HIGH" : "LOW");
203 204

	/* Configure GPIO output value. */
205
	set_level(gpio, value);
206 207

	/* Configure GPIO direction as output. */
208
	set_direction(gpio, 1);
209 210 211 212

	return 0;
}

213 214
/* read GPIO IN value of pin 'gpio' */
int gpio_get_value(unsigned gpio)
215
{
216 217
	struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
	struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
218 219 220
	int val;

	debug("gpio_get_value: pin = %d (port %d:bit %d)\n",
221
		gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
222

223
	val = readl(&bank->gpio_in[GPIO_PORT(gpio)]);
224

225
	return (val >> GPIO_BIT(gpio)) & 1;
226 227
}

228 229
/* write GPIO OUT value to pin 'gpio' */
int gpio_set_value(unsigned gpio, int value)
230 231
{
	debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
232
		gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
233 234

	/* Configure GPIO output value. */
235 236 237
	set_level(gpio, value);

	return 0;
238 239 240 241 242 243 244
}

/*
 * Display Tegra GPIO information
 */
void gpio_info(void)
{
245 246
	unsigned c;
	int type;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

	for (c = 0; c < MAX_NUM_GPIOS; c++) {
		type = get_config(c);		/* GPIO, not SFPIO */
		if (type) {
			printf("GPIO_%d:\t%s is an %s, ", c,
				get_name(c),
				get_direction(c) ? "OUTPUT" : "INPUT");
			if (get_direction(c))
				printf("value = %d", gpio_get_output_value(c));
			else
				printf("value = %d", gpio_get_value(c));
			printf("\n");
		} else
			continue;
	}
}