Tom Rix | 0c872ec | 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 | * |
Wolfgang Denk | bcd4d4e | 2013-07-28 22:12:44 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0 |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 6 | * |
| 7 | * This work is derived from the linux 2.6.27 kernel source |
| 8 | * To fetch, use the kernel repository |
| 9 | * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git |
| 10 | * Use the v2.6.27 tag. |
| 11 | * |
| 12 | * Below is the original's header including its copyright |
| 13 | * |
| 14 | * linux/arch/arm/plat-omap/gpio.c |
| 15 | * |
| 16 | * Support functions for OMAP GPIO |
| 17 | * |
| 18 | * Copyright (C) 2003-2005 Nokia Corporation |
| 19 | * Written by Juha Yrjölä <juha.yrjola@nokia.com> |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 20 | */ |
| 21 | #include <common.h> |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 22 | #include <asm/gpio.h> |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 23 | #include <asm/io.h> |
| 24 | #include <asm/errno.h> |
| 25 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 26 | #define OMAP_GPIO_DIR_OUT 0 |
| 27 | #define OMAP_GPIO_DIR_IN 1 |
| 28 | |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 29 | static inline int get_gpio_index(int gpio) |
| 30 | { |
| 31 | return gpio & 0x1f; |
| 32 | } |
| 33 | |
Nikita Kiryanov | dcee1ab | 2012-11-27 22:40:57 +0000 | [diff] [blame] | 34 | int gpio_is_valid(int gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 35 | { |
Axel Lin | 87bd05d | 2013-06-21 18:54:25 +0800 | [diff] [blame] | 36 | return (gpio >= 0) && (gpio < OMAP_MAX_GPIO); |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 39 | static void _set_gpio_direction(const struct gpio_bank *bank, int gpio, |
| 40 | int is_input) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 41 | { |
| 42 | void *reg = bank->base; |
| 43 | u32 l; |
| 44 | |
| 45 | switch (bank->method) { |
| 46 | case METHOD_GPIO_24XX: |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 47 | reg += OMAP_GPIO_OE; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 48 | break; |
| 49 | default: |
| 50 | return; |
| 51 | } |
| 52 | l = __raw_readl(reg); |
| 53 | if (is_input) |
| 54 | l |= 1 << gpio; |
| 55 | else |
| 56 | l &= ~(1 << gpio); |
| 57 | __raw_writel(l, reg); |
| 58 | } |
| 59 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Get the direction of the GPIO by reading the GPIO_OE register |
| 62 | * corresponding to the specified bank. |
| 63 | */ |
| 64 | static int _get_gpio_direction(const struct gpio_bank *bank, int gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 65 | { |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 66 | void *reg = bank->base; |
| 67 | u32 v; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 68 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 69 | switch (bank->method) { |
| 70 | case METHOD_GPIO_24XX: |
| 71 | reg += OMAP_GPIO_OE; |
| 72 | break; |
| 73 | default: |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 74 | return -1; |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | v = __raw_readl(reg); |
| 78 | |
| 79 | if (v & (1 << gpio)) |
| 80 | return OMAP_GPIO_DIR_IN; |
| 81 | else |
| 82 | return OMAP_GPIO_DIR_OUT; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 85 | static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, |
| 86 | int enable) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 87 | { |
| 88 | void *reg = bank->base; |
| 89 | u32 l = 0; |
| 90 | |
| 91 | switch (bank->method) { |
| 92 | case METHOD_GPIO_24XX: |
| 93 | if (enable) |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 94 | reg += OMAP_GPIO_SETDATAOUT; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 95 | else |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 96 | reg += OMAP_GPIO_CLEARDATAOUT; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 97 | l = 1 << gpio; |
| 98 | break; |
| 99 | default: |
| 100 | printf("omap3-gpio unknown bank method %s %d\n", |
| 101 | __FILE__, __LINE__); |
| 102 | return; |
| 103 | } |
| 104 | __raw_writel(l, reg); |
| 105 | } |
| 106 | |
Simon Glass | d57b611 | 2014-10-22 21:37:08 -0600 | [diff] [blame^] | 107 | static int _get_gpio_value(const struct gpio_bank *bank, int gpio) |
| 108 | { |
| 109 | void *reg = bank->base; |
| 110 | int input; |
| 111 | |
| 112 | switch (bank->method) { |
| 113 | case METHOD_GPIO_24XX: |
| 114 | input = _get_gpio_direction(bank, gpio); |
| 115 | switch (input) { |
| 116 | case OMAP_GPIO_DIR_IN: |
| 117 | reg += OMAP_GPIO_DATAIN; |
| 118 | break; |
| 119 | case OMAP_GPIO_DIR_OUT: |
| 120 | reg += OMAP_GPIO_DATAOUT; |
| 121 | break; |
| 122 | default: |
| 123 | return -1; |
| 124 | } |
| 125 | break; |
| 126 | default: |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | return (__raw_readl(reg) & (1 << gpio)) != 0; |
| 131 | } |
| 132 | |
| 133 | static inline const struct gpio_bank *get_gpio_bank(int gpio) |
| 134 | { |
| 135 | return &omap_gpio_bank[gpio >> 5]; |
| 136 | } |
| 137 | |
| 138 | static int check_gpio(int gpio) |
| 139 | { |
| 140 | if (!gpio_is_valid(gpio)) { |
| 141 | printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); |
| 142 | return -1; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 147 | /** |
| 148 | * Set value of the specified gpio |
| 149 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 150 | int gpio_set_value(unsigned gpio, int value) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 151 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 152 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 153 | |
| 154 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 155 | return -1; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 156 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 157 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 158 | |
| 159 | return 0; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 162 | /** |
| 163 | * Get value of the specified gpio |
| 164 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 165 | int gpio_get_value(unsigned gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 166 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 167 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 168 | |
| 169 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 170 | return -1; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 171 | bank = get_gpio_bank(gpio); |
Simon Glass | d57b611 | 2014-10-22 21:37:08 -0600 | [diff] [blame^] | 172 | |
| 173 | return _get_gpio_value(bank, get_gpio_index(gpio)); |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 176 | /** |
| 177 | * Set gpio direction as input |
| 178 | */ |
| 179 | int gpio_direction_input(unsigned gpio) |
Joel A Fernandes | 569919d | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 180 | { |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 181 | const struct gpio_bank *bank; |
Joel A Fernandes | 569919d | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 182 | |
| 183 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 184 | return -1; |
Joel A Fernandes | 569919d | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 185 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 186 | bank = get_gpio_bank(gpio); |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 187 | _set_gpio_direction(bank, get_gpio_index(gpio), 1); |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 188 | |
| 189 | return 0; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 192 | /** |
| 193 | * Set gpio direction as output |
| 194 | */ |
| 195 | int gpio_direction_output(unsigned gpio, int value) |
| 196 | { |
| 197 | const struct gpio_bank *bank; |
| 198 | |
| 199 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 200 | return -1; |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 201 | |
| 202 | bank = get_gpio_bank(gpio); |
| 203 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
| 204 | _set_gpio_direction(bank, get_gpio_index(gpio), 0); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Request a gpio before using it. |
| 211 | * |
| 212 | * NOTE: Argument 'label' is unused. |
| 213 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 214 | int gpio_request(unsigned gpio, const char *label) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 215 | { |
| 216 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 217 | return -1; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 222 | /** |
| 223 | * Reset and free the gpio after using it. |
| 224 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 225 | int gpio_free(unsigned gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 226 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 227 | return 0; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 228 | } |