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> |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 22 | #include <dm.h> |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 23 | #include <fdtdec.h> |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 24 | #include <asm/gpio.h> |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 25 | #include <asm/io.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 26 | #include <linux/errno.h> |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 27 | #include <malloc.h> |
| 28 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 30 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 31 | #define OMAP_GPIO_DIR_OUT 0 |
| 32 | #define OMAP_GPIO_DIR_IN 1 |
| 33 | |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 34 | #ifdef CONFIG_DM_GPIO |
| 35 | |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 36 | #define GPIO_PER_BANK 32 |
| 37 | |
| 38 | struct gpio_bank { |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 39 | /* TODO(sjg@chromium.org): Can we use a struct here? */ |
| 40 | void *base; /* address of registers in physical memory */ |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | #endif |
| 44 | |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 45 | static inline int get_gpio_index(int gpio) |
| 46 | { |
| 47 | return gpio & 0x1f; |
| 48 | } |
| 49 | |
Nikita Kiryanov | dcee1ab | 2012-11-27 22:40:57 +0000 | [diff] [blame] | 50 | int gpio_is_valid(int gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 51 | { |
Axel Lin | 87bd05d | 2013-06-21 18:54:25 +0800 | [diff] [blame] | 52 | return (gpio >= 0) && (gpio < OMAP_MAX_GPIO); |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 55 | static void _set_gpio_direction(const struct gpio_bank *bank, int gpio, |
| 56 | int is_input) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 57 | { |
| 58 | void *reg = bank->base; |
| 59 | u32 l; |
| 60 | |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 61 | reg += OMAP_GPIO_OE; |
| 62 | |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 63 | l = __raw_readl(reg); |
| 64 | if (is_input) |
| 65 | l |= 1 << gpio; |
| 66 | else |
| 67 | l &= ~(1 << gpio); |
| 68 | __raw_writel(l, reg); |
| 69 | } |
| 70 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 71 | /** |
| 72 | * Get the direction of the GPIO by reading the GPIO_OE register |
| 73 | * corresponding to the specified bank. |
| 74 | */ |
| 75 | static int _get_gpio_direction(const struct gpio_bank *bank, int gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 76 | { |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 77 | void *reg = bank->base; |
| 78 | u32 v; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 79 | |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 80 | reg += OMAP_GPIO_OE; |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 81 | |
| 82 | v = __raw_readl(reg); |
| 83 | |
| 84 | if (v & (1 << gpio)) |
| 85 | return OMAP_GPIO_DIR_IN; |
| 86 | else |
| 87 | return OMAP_GPIO_DIR_OUT; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 90 | static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, |
| 91 | int enable) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 92 | { |
| 93 | void *reg = bank->base; |
| 94 | u32 l = 0; |
| 95 | |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 96 | if (enable) |
| 97 | reg += OMAP_GPIO_SETDATAOUT; |
| 98 | else |
| 99 | reg += OMAP_GPIO_CLEARDATAOUT; |
| 100 | |
| 101 | l = 1 << gpio; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 102 | __raw_writel(l, reg); |
| 103 | } |
| 104 | |
Simon Glass | d57b611 | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 105 | static int _get_gpio_value(const struct gpio_bank *bank, int gpio) |
| 106 | { |
| 107 | void *reg = bank->base; |
| 108 | int input; |
| 109 | |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 110 | input = _get_gpio_direction(bank, gpio); |
| 111 | switch (input) { |
| 112 | case OMAP_GPIO_DIR_IN: |
| 113 | reg += OMAP_GPIO_DATAIN; |
| 114 | break; |
| 115 | case OMAP_GPIO_DIR_OUT: |
| 116 | reg += OMAP_GPIO_DATAOUT; |
Simon Glass | d57b611 | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 117 | break; |
| 118 | default: |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | return (__raw_readl(reg) & (1 << gpio)) != 0; |
| 123 | } |
| 124 | |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 125 | #ifndef CONFIG_DM_GPIO |
| 126 | |
Simon Glass | d57b611 | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 127 | static inline const struct gpio_bank *get_gpio_bank(int gpio) |
| 128 | { |
| 129 | return &omap_gpio_bank[gpio >> 5]; |
| 130 | } |
| 131 | |
| 132 | static int check_gpio(int gpio) |
| 133 | { |
| 134 | if (!gpio_is_valid(gpio)) { |
| 135 | printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); |
| 136 | return -1; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 141 | /** |
| 142 | * Set value of the specified gpio |
| 143 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 144 | int gpio_set_value(unsigned gpio, int value) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 145 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 146 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 147 | |
| 148 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 149 | return -1; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 150 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 151 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 152 | |
| 153 | return 0; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 156 | /** |
| 157 | * Get value of the specified gpio |
| 158 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 159 | int gpio_get_value(unsigned gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 160 | { |
Aneesh V | 25223a6 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 161 | const struct gpio_bank *bank; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 162 | |
| 163 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 164 | return -1; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 165 | bank = get_gpio_bank(gpio); |
Simon Glass | d57b611 | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 166 | |
| 167 | return _get_gpio_value(bank, get_gpio_index(gpio)); |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Sanjeev Premi | 81bdc15 | 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 | 569919d | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 174 | { |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 175 | const struct gpio_bank *bank; |
Joel A Fernandes | 569919d | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 176 | |
| 177 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 178 | return -1; |
Joel A Fernandes | 569919d | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 179 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 180 | bank = get_gpio_bank(gpio); |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 181 | _set_gpio_direction(bank, get_gpio_index(gpio), 1); |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 182 | |
| 183 | return 0; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Sanjeev Premi | 81bdc15 | 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) |
| 190 | { |
| 191 | const struct gpio_bank *bank; |
| 192 | |
| 193 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 194 | return -1; |
Sanjeev Premi | 81bdc15 | 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; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Request a gpio before using it. |
| 205 | * |
| 206 | * NOTE: Argument 'label' is unused. |
| 207 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 208 | int gpio_request(unsigned gpio, const char *label) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 209 | { |
| 210 | if (check_gpio(gpio) < 0) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 211 | return -1; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Sanjeev Premi | 81bdc15 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 216 | /** |
| 217 | * Reset and free the gpio after using it. |
| 218 | */ |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 219 | int gpio_free(unsigned gpio) |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 220 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 221 | return 0; |
Tom Rix | 0c872ec | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 222 | } |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 223 | |
| 224 | #else /* new driver model interface CONFIG_DM_GPIO */ |
| 225 | |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 226 | /* set GPIO pin 'gpio' as an input */ |
| 227 | static int omap_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 228 | { |
| 229 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 230 | |
| 231 | /* Configure GPIO direction as input. */ |
| 232 | _set_gpio_direction(bank, offset, 1); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 238 | static int omap_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 239 | int value) |
| 240 | { |
| 241 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 242 | |
| 243 | _set_gpio_dataout(bank, offset, value); |
| 244 | _set_gpio_direction(bank, offset, 0); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* read GPIO IN value of pin 'gpio' */ |
| 250 | static int omap_gpio_get_value(struct udevice *dev, unsigned offset) |
| 251 | { |
| 252 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 253 | |
| 254 | return _get_gpio_value(bank, offset); |
| 255 | } |
| 256 | |
| 257 | /* write GPIO OUT value to pin 'gpio' */ |
| 258 | static int omap_gpio_set_value(struct udevice *dev, unsigned offset, |
| 259 | int value) |
| 260 | { |
| 261 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 262 | |
| 263 | _set_gpio_dataout(bank, offset, value); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 268 | static int omap_gpio_get_function(struct udevice *dev, unsigned offset) |
| 269 | { |
| 270 | struct gpio_bank *bank = dev_get_priv(dev); |
| 271 | |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 272 | /* GPIOF_FUNC is not implemented yet */ |
Axel Lin | 26c0472 | 2015-01-31 22:23:38 +0800 | [diff] [blame] | 273 | if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT) |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 274 | return GPIOF_OUTPUT; |
| 275 | else |
| 276 | return GPIOF_INPUT; |
| 277 | } |
| 278 | |
| 279 | static const struct dm_gpio_ops gpio_omap_ops = { |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 280 | .direction_input = omap_gpio_direction_input, |
| 281 | .direction_output = omap_gpio_direction_output, |
| 282 | .get_value = omap_gpio_get_value, |
| 283 | .set_value = omap_gpio_set_value, |
| 284 | .get_function = omap_gpio_get_function, |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | static int omap_gpio_probe(struct udevice *dev) |
| 288 | { |
| 289 | struct gpio_bank *bank = dev_get_priv(dev); |
| 290 | struct omap_gpio_platdata *plat = dev_get_platdata(dev); |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 291 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 292 | |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 293 | uc_priv->bank_name = plat->port_name; |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 294 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 295 | bank->base = (void *)plat->base; |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 300 | static int omap_gpio_bind(struct udevice *dev) |
| 301 | { |
| 302 | struct omap_gpio_platdata *plat = dev->platdata; |
| 303 | fdt_addr_t base_addr; |
| 304 | |
| 305 | if (plat) |
| 306 | return 0; |
| 307 | |
| 308 | base_addr = dev_get_addr(dev); |
| 309 | if (base_addr == FDT_ADDR_T_NONE) |
| 310 | return -ENODEV; |
| 311 | |
| 312 | /* |
| 313 | * TODO: |
| 314 | * When every board is converted to driver model and DT is |
| 315 | * supported, this can be done by auto-alloc feature, but |
| 316 | * not using calloc to alloc memory for platdata. |
| 317 | */ |
| 318 | plat = calloc(1, sizeof(*plat)); |
| 319 | if (!plat) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | plat->base = base_addr; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 323 | plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL); |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 324 | dev->platdata = plat; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static const struct udevice_id omap_gpio_ids[] = { |
| 330 | { .compatible = "ti,omap3-gpio" }, |
| 331 | { .compatible = "ti,omap4-gpio" }, |
| 332 | { .compatible = "ti,am4372-gpio" }, |
| 333 | { } |
| 334 | }; |
| 335 | |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 336 | U_BOOT_DRIVER(gpio_omap) = { |
| 337 | .name = "gpio_omap", |
| 338 | .id = UCLASS_GPIO, |
| 339 | .ops = &gpio_omap_ops, |
Tom Rini | 0a9e340 | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 340 | .of_match = omap_gpio_ids, |
| 341 | .bind = omap_gpio_bind, |
Simon Glass | 5915a2a | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 342 | .probe = omap_gpio_probe, |
| 343 | .priv_auto_alloc_size = sizeof(struct gpio_bank), |
| 344 | }; |
| 345 | |
| 346 | #endif /* CONFIG_DM_GPIO */ |