Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 1 | /* |
Allen Martin | 00a2749 | 2012-08-31 08:30:00 +0000 | [diff] [blame] | 2 | * NVIDIA Tegra20 GPIO handling. |
Tom Warren | 52a8b82 | 2012-05-22 12:19:25 +0000 | [diff] [blame] | 3 | * (C) Copyright 2010-2012 |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 4 | * NVIDIA Corporation <www.nvidia.com> |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 7 | */ |
| 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 Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 17 | #include <asm/arch/tegra.h> |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 18 | #include <asm/gpio.h> |
| 19 | |
| 20 | enum { |
Tom Warren | 29f3e3f | 2012-09-04 17:00:24 -0700 | [diff] [blame] | 21 | TEGRA_CMD_INFO, |
| 22 | TEGRA_CMD_PORT, |
| 23 | TEGRA_CMD_OUTPUT, |
| 24 | TEGRA_CMD_INPUT, |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | static struct gpio_names { |
| 28 | char name[GPIO_NAME_SIZE]; |
| 29 | } gpio_names[MAX_NUM_GPIOS]; |
| 30 | |
| 31 | static char *get_name(int i) |
| 32 | { |
| 33 | return *gpio_names[i].name ? gpio_names[i].name : "UNKNOWN"; |
| 34 | } |
| 35 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 36 | /* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */ |
| 37 | static int get_config(unsigned gpio) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 38 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 39 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 40 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 41 | u32 u; |
| 42 | int type; |
| 43 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 44 | u = readl(&bank->gpio_config[GPIO_PORT(gpio)]); |
| 45 | type = (u >> GPIO_BIT(gpio)) & 1; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 46 | |
| 47 | debug("get_config: port = %d, bit = %d is %s\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 48 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO"); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 49 | |
| 50 | return type; |
| 51 | } |
| 52 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 53 | /* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */ |
| 54 | static void set_config(unsigned gpio, int type) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 55 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 56 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 57 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 58 | u32 u; |
| 59 | |
| 60 | debug("set_config: port = %d, bit = %d, %s\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 61 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO"); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 62 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 63 | u = readl(&bank->gpio_config[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 64 | if (type) /* GPIO */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 65 | u |= 1 << GPIO_BIT(gpio); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 66 | else |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 67 | u &= ~(1 << GPIO_BIT(gpio)); |
| 68 | writel(u, &bank->gpio_config[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 71 | /* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */ |
| 72 | static int get_direction(unsigned gpio) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 73 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 74 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 75 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 76 | u32 u; |
| 77 | int dir; |
| 78 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 79 | u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]); |
| 80 | dir = (u >> GPIO_BIT(gpio)) & 1; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 81 | |
| 82 | debug("get_direction: port = %d, bit = %d, %s\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 83 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN"); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 84 | |
| 85 | return dir; |
| 86 | } |
| 87 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 88 | /* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */ |
| 89 | static void set_direction(unsigned gpio, int output) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 90 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 91 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 92 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 93 | u32 u; |
| 94 | |
| 95 | debug("set_direction: port = %d, bit = %d, %s\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 96 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN"); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 97 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 98 | u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 99 | if (output) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 100 | u |= 1 << GPIO_BIT(gpio); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 101 | else |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 102 | u &= ~(1 << GPIO_BIT(gpio)); |
| 103 | writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 106 | /* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */ |
| 107 | static void set_level(unsigned gpio, int high) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 108 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 109 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 110 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 111 | u32 u; |
| 112 | |
| 113 | debug("set_level: port = %d, bit %d == %d\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 114 | GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 115 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 116 | u = readl(&bank->gpio_out[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 117 | if (high) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 118 | u |= 1 << GPIO_BIT(gpio); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 119 | else |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 120 | u &= ~(1 << GPIO_BIT(gpio)); |
| 121 | writel(u, &bank->gpio_out[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Generic_GPIO primitives. |
| 126 | */ |
| 127 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 128 | int gpio_request(unsigned gpio, const char *label) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 129 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 130 | if (gpio >= MAX_NUM_GPIOS) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 131 | return -1; |
| 132 | |
Stephen Warren | 5fac236 | 2011-10-06 12:52:22 +0000 | [diff] [blame] | 133 | if (label != NULL) { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 134 | strncpy(gpio_names[gpio].name, label, GPIO_NAME_SIZE); |
| 135 | gpio_names[gpio].name[GPIO_NAME_SIZE - 1] = '\0'; |
Stephen Warren | 5fac236 | 2011-10-06 12:52:22 +0000 | [diff] [blame] | 136 | } |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 137 | |
| 138 | /* Configure as a GPIO */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 139 | set_config(gpio, 1); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 144 | int gpio_free(unsigned gpio) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 145 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 146 | 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 Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 154 | /* read GPIO OUT value of pin 'gpio' */ |
| 155 | static int gpio_get_output_value(unsigned gpio) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 156 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 157 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 158 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 159 | int val; |
| 160 | |
| 161 | debug("gpio_get_output_value: pin = %d (port %d:bit %d)\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 162 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio)); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 163 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 164 | val = readl(&bank->gpio_out[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 165 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 166 | return (val >> GPIO_BIT(gpio)) & 1; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 169 | /* set GPIO pin 'gpio' as an input */ |
| 170 | int gpio_direction_input(unsigned gpio) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 171 | { |
| 172 | debug("gpio_direction_input: pin = %d (port %d:bit %d)\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 173 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio)); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 174 | |
| 175 | /* Configure GPIO direction as input. */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 176 | set_direction(gpio, 0); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 181 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 182 | int gpio_direction_output(unsigned gpio, int value) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 183 | { |
| 184 | debug("gpio_direction_output: pin = %d (port %d:bit %d) = %s\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 185 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), |
| 186 | value ? "HIGH" : "LOW"); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 187 | |
| 188 | /* Configure GPIO output value. */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 189 | set_level(gpio, value); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 190 | |
| 191 | /* Configure GPIO direction as output. */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 192 | set_direction(gpio, 1); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 197 | /* read GPIO IN value of pin 'gpio' */ |
| 198 | int gpio_get_value(unsigned gpio) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 199 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 200 | struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE; |
| 201 | struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)]; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 202 | int val; |
| 203 | |
| 204 | debug("gpio_get_value: pin = %d (port %d:bit %d)\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 205 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio)); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 206 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 207 | val = readl(&bank->gpio_in[GPIO_PORT(gpio)]); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 208 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 209 | return (val >> GPIO_BIT(gpio)) & 1; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 212 | /* write GPIO OUT value to pin 'gpio' */ |
| 213 | int gpio_set_value(unsigned gpio, int value) |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 214 | { |
| 215 | debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 216 | gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value); |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 217 | |
| 218 | /* Configure GPIO output value. */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 219 | set_level(gpio, value); |
| 220 | |
| 221 | return 0; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Display Tegra GPIO information |
| 226 | */ |
| 227 | void gpio_info(void) |
| 228 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 229 | unsigned c; |
| 230 | int type; |
Tom Warren | 4e5ae09 | 2011-06-17 06:27:28 +0000 | [diff] [blame] | 231 | |
| 232 | for (c = 0; c < MAX_NUM_GPIOS; c++) { |
| 233 | type = get_config(c); /* GPIO, not SFPIO */ |
| 234 | if (type) { |
| 235 | printf("GPIO_%d:\t%s is an %s, ", c, |
| 236 | get_name(c), |
| 237 | get_direction(c) ? "OUTPUT" : "INPUT"); |
| 238 | if (get_direction(c)) |
| 239 | printf("value = %d", gpio_get_output_value(c)); |
| 240 | else |
| 241 | printf("value = %d", gpio_get_value(c)); |
| 242 | printf("\n"); |
| 243 | } else |
| 244 | continue; |
| 245 | } |
| 246 | } |