Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 Wind River Systems, Inc. |
| 3 | * Tom Rix <Tom.Rix@windriver.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 18 | * MA 02111-1307 USA |
| 19 | * |
| 20 | * This work is derived from the linux 2.6.27 kernel source |
| 21 | * To fetch, use the kernel repository |
| 22 | * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git |
| 23 | * Use the v2.6.27 tag. |
| 24 | * |
| 25 | * Below is the original's header including its copyright |
| 26 | * |
| 27 | * linux/arch/arm/plat-omap/gpio.c |
| 28 | * |
| 29 | * Support functions for OMAP GPIO |
| 30 | * |
| 31 | * Copyright (C) 2003-2005 Nokia Corporation |
| 32 | * Written by Juha Yrjölä <juha.yrjola@nokia.com> |
| 33 | * |
| 34 | * This program is free software; you can redistribute it and/or modify |
| 35 | * it under the terms of the GNU General Public License version 2 as |
| 36 | * published by the Free Software Foundation. |
| 37 | */ |
| 38 | #include <common.h> |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 39 | #include <asm/gpio.h> |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 40 | #include <asm/io.h> |
| 41 | #include <asm/errno.h> |
| 42 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 43 | #define OMAP_GPIO_DIR_OUT 0 |
| 44 | #define OMAP_GPIO_DIR_IN 1 |
| 45 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 46 | static inline const struct gpio_bank *get_gpio_bank(int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 47 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 48 | return &omap_gpio_bank[gpio >> 5]; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static inline int get_gpio_index(int gpio) |
| 52 | { |
| 53 | return gpio & 0x1f; |
| 54 | } |
| 55 | |
Nikita Kiryanov | e3cb9c1 | 2012-11-27 22:40:57 +0000 | [diff] [blame] | 56 | int gpio_is_valid(int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 57 | { |
Axel Lin | 01a461f | 2013-06-21 18:54:25 +0800 | [diff] [blame] | 58 | return (gpio >= 0) && (gpio < OMAP_MAX_GPIO); |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int check_gpio(int gpio) |
| 62 | { |
Nikita Kiryanov | e3cb9c1 | 2012-11-27 22:40:57 +0000 | [diff] [blame] | 63 | if (!gpio_is_valid(gpio)) { |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 64 | printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 65 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 70 | static void _set_gpio_direction(const struct gpio_bank *bank, int gpio, |
| 71 | int is_input) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 72 | { |
| 73 | void *reg = bank->base; |
| 74 | u32 l; |
| 75 | |
| 76 | switch (bank->method) { |
| 77 | case METHOD_GPIO_24XX: |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 78 | reg += OMAP_GPIO_OE; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | return; |
| 82 | } |
| 83 | l = __raw_readl(reg); |
| 84 | if (is_input) |
| 85 | l |= 1 << gpio; |
| 86 | else |
| 87 | l &= ~(1 << gpio); |
| 88 | __raw_writel(l, reg); |
| 89 | } |
| 90 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 91 | /** |
| 92 | * Get the direction of the GPIO by reading the GPIO_OE register |
| 93 | * corresponding to the specified bank. |
| 94 | */ |
| 95 | static int _get_gpio_direction(const struct gpio_bank *bank, int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 96 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 97 | void *reg = bank->base; |
| 98 | u32 v; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 99 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 100 | switch (bank->method) { |
| 101 | case METHOD_GPIO_24XX: |
| 102 | reg += OMAP_GPIO_OE; |
| 103 | break; |
| 104 | default: |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 105 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | v = __raw_readl(reg); |
| 109 | |
| 110 | if (v & (1 << gpio)) |
| 111 | return OMAP_GPIO_DIR_IN; |
| 112 | else |
| 113 | return OMAP_GPIO_DIR_OUT; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 116 | static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, |
| 117 | int enable) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 118 | { |
| 119 | void *reg = bank->base; |
| 120 | u32 l = 0; |
| 121 | |
| 122 | switch (bank->method) { |
| 123 | case METHOD_GPIO_24XX: |
| 124 | if (enable) |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 125 | reg += OMAP_GPIO_SETDATAOUT; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 126 | else |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 127 | reg += OMAP_GPIO_CLEARDATAOUT; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 128 | l = 1 << gpio; |
| 129 | break; |
| 130 | default: |
| 131 | printf("omap3-gpio unknown bank method %s %d\n", |
| 132 | __FILE__, __LINE__); |
| 133 | return; |
| 134 | } |
| 135 | __raw_writel(l, reg); |
| 136 | } |
| 137 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 138 | /** |
| 139 | * Set value of the specified gpio |
| 140 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 141 | int gpio_set_value(unsigned gpio, int value) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 142 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 143 | const struct gpio_bank *bank; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 144 | |
| 145 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 146 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 147 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 148 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 149 | |
| 150 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 153 | /** |
| 154 | * Get value of the specified gpio |
| 155 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 156 | int gpio_get_value(unsigned gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 157 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 158 | const struct gpio_bank *bank; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 159 | void *reg; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 160 | int input; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 161 | |
| 162 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 163 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 164 | bank = get_gpio_bank(gpio); |
| 165 | reg = bank->base; |
| 166 | switch (bank->method) { |
| 167 | case METHOD_GPIO_24XX: |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 168 | input = _get_gpio_direction(bank, get_gpio_index(gpio)); |
| 169 | switch (input) { |
| 170 | case OMAP_GPIO_DIR_IN: |
| 171 | reg += OMAP_GPIO_DATAIN; |
| 172 | break; |
| 173 | case OMAP_GPIO_DIR_OUT: |
| 174 | reg += OMAP_GPIO_DATAOUT; |
| 175 | break; |
| 176 | default: |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 177 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 178 | } |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 179 | break; |
| 180 | default: |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 181 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 182 | } |
| 183 | return (__raw_readl(reg) |
| 184 | & (1 << get_gpio_index(gpio))) != 0; |
| 185 | } |
| 186 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 187 | /** |
| 188 | * Set gpio direction as input |
| 189 | */ |
| 190 | int gpio_direction_input(unsigned gpio) |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 191 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 192 | const struct gpio_bank *bank; |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 193 | |
| 194 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 195 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 196 | |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 197 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 198 | _set_gpio_direction(bank, get_gpio_index(gpio), 1); |
| 199 | |
| 200 | return 0; |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 203 | /** |
| 204 | * Set gpio direction as output |
| 205 | */ |
| 206 | int gpio_direction_output(unsigned gpio, int value) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 207 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 208 | const struct gpio_bank *bank; |
| 209 | |
| 210 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 211 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 212 | |
| 213 | bank = get_gpio_bank(gpio); |
| 214 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
| 215 | _set_gpio_direction(bank, get_gpio_index(gpio), 0); |
| 216 | |
| 217 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 220 | /** |
| 221 | * Request a gpio before using it. |
| 222 | * |
| 223 | * NOTE: Argument 'label' is unused. |
| 224 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 225 | int gpio_request(unsigned gpio, const char *label) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 226 | { |
| 227 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 228 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 233 | /** |
| 234 | * Reset and free the gpio after using it. |
| 235 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 236 | int gpio_free(unsigned gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 237 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 238 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 239 | } |