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 | * |
Wolfgang Denk | 5064ca4 | 2013-07-28 22:12:44 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0 |
Tom Rix | dd2d1cc | 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 | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 20 | */ |
| 21 | #include <common.h> |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 22 | #include <asm/gpio.h> |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 23 | #include <asm/io.h> |
| 24 | #include <asm/errno.h> |
| 25 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 26 | #define OMAP_GPIO_DIR_OUT 0 |
| 27 | #define OMAP_GPIO_DIR_IN 1 |
| 28 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 29 | static inline const struct gpio_bank *get_gpio_bank(int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 30 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 31 | return &omap_gpio_bank[gpio >> 5]; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static inline int get_gpio_index(int gpio) |
| 35 | { |
| 36 | return gpio & 0x1f; |
| 37 | } |
| 38 | |
Nikita Kiryanov | e3cb9c1 | 2012-11-27 22:40:57 +0000 | [diff] [blame] | 39 | int gpio_is_valid(int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 40 | { |
Axel Lin | 01a461f | 2013-06-21 18:54:25 +0800 | [diff] [blame] | 41 | return (gpio >= 0) && (gpio < OMAP_MAX_GPIO); |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static int check_gpio(int gpio) |
| 45 | { |
Nikita Kiryanov | e3cb9c1 | 2012-11-27 22:40:57 +0000 | [diff] [blame] | 46 | if (!gpio_is_valid(gpio)) { |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 47 | printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 48 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 53 | static void _set_gpio_direction(const struct gpio_bank *bank, int gpio, |
| 54 | int is_input) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 55 | { |
| 56 | void *reg = bank->base; |
| 57 | u32 l; |
| 58 | |
| 59 | switch (bank->method) { |
| 60 | case METHOD_GPIO_24XX: |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 61 | reg += OMAP_GPIO_OE; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 62 | break; |
| 63 | default: |
| 64 | return; |
| 65 | } |
| 66 | l = __raw_readl(reg); |
| 67 | if (is_input) |
| 68 | l |= 1 << gpio; |
| 69 | else |
| 70 | l &= ~(1 << gpio); |
| 71 | __raw_writel(l, reg); |
| 72 | } |
| 73 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 74 | /** |
| 75 | * Get the direction of the GPIO by reading the GPIO_OE register |
| 76 | * corresponding to the specified bank. |
| 77 | */ |
| 78 | static int _get_gpio_direction(const struct gpio_bank *bank, int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 79 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 80 | void *reg = bank->base; |
| 81 | u32 v; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 82 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 83 | switch (bank->method) { |
| 84 | case METHOD_GPIO_24XX: |
| 85 | reg += OMAP_GPIO_OE; |
| 86 | break; |
| 87 | default: |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 88 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | v = __raw_readl(reg); |
| 92 | |
| 93 | if (v & (1 << gpio)) |
| 94 | return OMAP_GPIO_DIR_IN; |
| 95 | else |
| 96 | return OMAP_GPIO_DIR_OUT; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 99 | static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, |
| 100 | int enable) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 101 | { |
| 102 | void *reg = bank->base; |
| 103 | u32 l = 0; |
| 104 | |
| 105 | switch (bank->method) { |
| 106 | case METHOD_GPIO_24XX: |
| 107 | if (enable) |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 108 | reg += OMAP_GPIO_SETDATAOUT; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 109 | else |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 110 | reg += OMAP_GPIO_CLEARDATAOUT; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 111 | l = 1 << gpio; |
| 112 | break; |
| 113 | default: |
| 114 | printf("omap3-gpio unknown bank method %s %d\n", |
| 115 | __FILE__, __LINE__); |
| 116 | return; |
| 117 | } |
| 118 | __raw_writel(l, reg); |
| 119 | } |
| 120 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 121 | /** |
| 122 | * Set value of the specified gpio |
| 123 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 124 | int gpio_set_value(unsigned gpio, int value) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 125 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 126 | const struct gpio_bank *bank; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 127 | |
| 128 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 129 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 130 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 131 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 132 | |
| 133 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 136 | /** |
| 137 | * Get value of the specified gpio |
| 138 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 139 | int gpio_get_value(unsigned gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 140 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 141 | const struct gpio_bank *bank; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 142 | void *reg; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 143 | int input; |
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); |
| 148 | reg = bank->base; |
| 149 | switch (bank->method) { |
| 150 | case METHOD_GPIO_24XX: |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 151 | input = _get_gpio_direction(bank, get_gpio_index(gpio)); |
| 152 | switch (input) { |
| 153 | case OMAP_GPIO_DIR_IN: |
| 154 | reg += OMAP_GPIO_DATAIN; |
| 155 | break; |
| 156 | case OMAP_GPIO_DIR_OUT: |
| 157 | reg += OMAP_GPIO_DATAOUT; |
| 158 | break; |
| 159 | default: |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 160 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 161 | } |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 162 | break; |
| 163 | default: |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 164 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 165 | } |
| 166 | return (__raw_readl(reg) |
| 167 | & (1 << get_gpio_index(gpio))) != 0; |
| 168 | } |
| 169 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 170 | /** |
| 171 | * Set gpio direction as input |
| 172 | */ |
| 173 | int gpio_direction_input(unsigned gpio) |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 174 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 175 | const struct gpio_bank *bank; |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 176 | |
| 177 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 178 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 179 | |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 180 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 181 | _set_gpio_direction(bank, get_gpio_index(gpio), 1); |
| 182 | |
| 183 | return 0; |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 186 | /** |
| 187 | * Set gpio direction as output |
| 188 | */ |
| 189 | int gpio_direction_output(unsigned gpio, int value) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 190 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 191 | const struct gpio_bank *bank; |
| 192 | |
| 193 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 194 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 195 | |
| 196 | bank = get_gpio_bank(gpio); |
| 197 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
| 198 | _set_gpio_direction(bank, get_gpio_index(gpio), 0); |
| 199 | |
| 200 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 203 | /** |
| 204 | * Request a gpio before using it. |
| 205 | * |
| 206 | * NOTE: Argument 'label' is unused. |
| 207 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 208 | int gpio_request(unsigned gpio, const char *label) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 209 | { |
| 210 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 211 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 216 | /** |
| 217 | * Reset and free the gpio after using it. |
| 218 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 219 | int gpio_free(unsigned gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 220 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 221 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 222 | } |