Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 |
| 3 | * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de> |
| 4 | * |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 5 | * Copyright (C) 2011 |
| 6 | * Stefano Babic, DENX Software Engineering, <sbabic@denx.de> |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 9 | */ |
| 10 | #include <common.h> |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <dm.h> |
| 13 | #include <malloc.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 14 | #include <asm/arch/imx-regs.h> |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 16 | #include <asm/io.h> |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 17 | |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 18 | enum mxc_gpio_direction { |
| 19 | MXC_GPIO_DIRECTION_IN, |
| 20 | MXC_GPIO_DIRECTION_OUT, |
| 21 | }; |
| 22 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 23 | #define GPIO_PER_BANK 32 |
| 24 | |
| 25 | struct mxc_gpio_plat { |
| 26 | struct gpio_regs *regs; |
| 27 | }; |
| 28 | |
| 29 | struct mxc_bank_info { |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 30 | struct gpio_regs *regs; |
| 31 | }; |
| 32 | |
| 33 | #ifndef CONFIG_DM_GPIO |
Vikram Narayanan | 8d28c21 | 2012-04-10 04:26:08 +0000 | [diff] [blame] | 34 | #define GPIO_TO_PORT(n) (n / 32) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 35 | |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 36 | /* GPIO port description */ |
| 37 | static unsigned long gpio_ports[] = { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 38 | [0] = GPIO1_BASE_ADDR, |
| 39 | [1] = GPIO2_BASE_ADDR, |
| 40 | [2] = GPIO3_BASE_ADDR, |
trem | e71c39d | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 41 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Troy Kisky | 5ea6d7c | 2012-10-23 10:57:47 +0000 | [diff] [blame] | 42 | defined(CONFIG_MX53) || defined(CONFIG_MX6) |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 43 | [3] = GPIO4_BASE_ADDR, |
| 44 | #endif |
Troy Kisky | 5ea6d7c | 2012-10-23 10:57:47 +0000 | [diff] [blame] | 45 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 46 | [4] = GPIO5_BASE_ADDR, |
| 47 | [5] = GPIO6_BASE_ADDR, |
trem | e71c39d | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 48 | #endif |
Troy Kisky | 5ea6d7c | 2012-10-23 10:57:47 +0000 | [diff] [blame] | 49 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 50 | [6] = GPIO7_BASE_ADDR, |
| 51 | #endif |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 54 | static int mxc_gpio_direction(unsigned int gpio, |
| 55 | enum mxc_gpio_direction direction) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 56 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 57 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 58 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 59 | u32 l; |
| 60 | |
| 61 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 62 | return -1; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 63 | |
| 64 | gpio &= 0x1f; |
| 65 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 66 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 67 | |
| 68 | l = readl(®s->gpio_dir); |
| 69 | |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 70 | switch (direction) { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 71 | case MXC_GPIO_DIRECTION_OUT: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 72 | l |= 1 << gpio; |
| 73 | break; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 74 | case MXC_GPIO_DIRECTION_IN: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 75 | l &= ~(1 << gpio); |
| 76 | } |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 77 | writel(l, ®s->gpio_dir); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 82 | int gpio_set_value(unsigned gpio, int value) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 83 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 84 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 85 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 86 | u32 l; |
| 87 | |
| 88 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 89 | return -1; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 90 | |
| 91 | gpio &= 0x1f; |
| 92 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 93 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 94 | |
| 95 | l = readl(®s->gpio_dr); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 96 | if (value) |
| 97 | l |= 1 << gpio; |
| 98 | else |
| 99 | l &= ~(1 << gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 100 | writel(l, ®s->gpio_dr); |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 101 | |
| 102 | return 0; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 103 | } |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 104 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 105 | int gpio_get_value(unsigned gpio) |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 106 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 107 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 108 | struct gpio_regs *regs; |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 109 | u32 val; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 110 | |
| 111 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 112 | return -1; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 113 | |
| 114 | gpio &= 0x1f; |
| 115 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 116 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 117 | |
Benoît Thébaudeau | 5dafa45 | 2012-08-20 10:55:41 +0000 | [diff] [blame] | 118 | val = (readl(®s->gpio_psr) >> gpio) & 0x01; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 119 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 120 | return val; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 121 | } |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 122 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 123 | int gpio_request(unsigned gpio, const char *label) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 124 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 125 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 126 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 127 | return -1; |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 131 | int gpio_free(unsigned gpio) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 132 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 133 | return 0; |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 136 | int gpio_direction_input(unsigned gpio) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 137 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 138 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 141 | int gpio_direction_output(unsigned gpio, int value) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 142 | { |
Dirk Behme | 04c79cb | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 143 | int ret = gpio_set_value(gpio, value); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 144 | |
| 145 | if (ret < 0) |
| 146 | return ret; |
| 147 | |
Dirk Behme | 04c79cb | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 148 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 149 | } |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 150 | #endif |
| 151 | |
| 152 | #ifdef CONFIG_DM_GPIO |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 153 | static int mxc_gpio_is_output(struct gpio_regs *regs, int offset) |
| 154 | { |
| 155 | u32 val; |
| 156 | |
| 157 | val = readl(®s->gpio_dir); |
| 158 | |
| 159 | return val & (1 << offset) ? 1 : 0; |
| 160 | } |
| 161 | |
| 162 | static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset, |
| 163 | enum mxc_gpio_direction direction) |
| 164 | { |
| 165 | u32 l; |
| 166 | |
| 167 | l = readl(®s->gpio_dir); |
| 168 | |
| 169 | switch (direction) { |
| 170 | case MXC_GPIO_DIRECTION_OUT: |
| 171 | l |= 1 << offset; |
| 172 | break; |
| 173 | case MXC_GPIO_DIRECTION_IN: |
| 174 | l &= ~(1 << offset); |
| 175 | } |
| 176 | writel(l, ®s->gpio_dir); |
| 177 | } |
| 178 | |
| 179 | static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset, |
| 180 | int value) |
| 181 | { |
| 182 | u32 l; |
| 183 | |
| 184 | l = readl(®s->gpio_dr); |
| 185 | if (value) |
| 186 | l |= 1 << offset; |
| 187 | else |
| 188 | l &= ~(1 << offset); |
| 189 | writel(l, ®s->gpio_dr); |
| 190 | } |
| 191 | |
| 192 | static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset) |
| 193 | { |
| 194 | return (readl(®s->gpio_psr) >> offset) & 0x01; |
| 195 | } |
| 196 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 197 | /* set GPIO pin 'gpio' as an input */ |
| 198 | static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 199 | { |
| 200 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 201 | |
| 202 | /* Configure GPIO direction as input. */ |
| 203 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 209 | static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 210 | int value) |
| 211 | { |
| 212 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 213 | |
| 214 | /* Configure GPIO output value. */ |
| 215 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 216 | |
| 217 | /* Configure GPIO direction as output. */ |
| 218 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* read GPIO IN value of pin 'gpio' */ |
| 224 | static int mxc_gpio_get_value(struct udevice *dev, unsigned offset) |
| 225 | { |
| 226 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 227 | |
| 228 | return mxc_gpio_bank_get_value(bank->regs, offset); |
| 229 | } |
| 230 | |
| 231 | /* write GPIO OUT value to pin 'gpio' */ |
| 232 | static int mxc_gpio_set_value(struct udevice *dev, unsigned offset, |
| 233 | int value) |
| 234 | { |
| 235 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 236 | |
| 237 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 242 | static int mxc_gpio_get_function(struct udevice *dev, unsigned offset) |
| 243 | { |
| 244 | struct mxc_bank_info *bank = dev_get_priv(dev); |
| 245 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 246 | /* GPIOF_FUNC is not implemented yet */ |
| 247 | if (mxc_gpio_is_output(bank->regs, offset)) |
| 248 | return GPIOF_OUTPUT; |
| 249 | else |
| 250 | return GPIOF_INPUT; |
| 251 | } |
| 252 | |
| 253 | static const struct dm_gpio_ops gpio_mxc_ops = { |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 254 | .direction_input = mxc_gpio_direction_input, |
| 255 | .direction_output = mxc_gpio_direction_output, |
| 256 | .get_value = mxc_gpio_get_value, |
| 257 | .set_value = mxc_gpio_set_value, |
| 258 | .get_function = mxc_gpio_get_function, |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | static const struct mxc_gpio_plat mxc_plat[] = { |
| 262 | { (struct gpio_regs *)GPIO1_BASE_ADDR }, |
| 263 | { (struct gpio_regs *)GPIO2_BASE_ADDR }, |
| 264 | { (struct gpio_regs *)GPIO3_BASE_ADDR }, |
| 265 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
| 266 | defined(CONFIG_MX53) || defined(CONFIG_MX6) |
| 267 | { (struct gpio_regs *)GPIO4_BASE_ADDR }, |
| 268 | #endif |
| 269 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) |
| 270 | { (struct gpio_regs *)GPIO5_BASE_ADDR }, |
| 271 | { (struct gpio_regs *)GPIO6_BASE_ADDR }, |
| 272 | #endif |
| 273 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) |
| 274 | { (struct gpio_regs *)GPIO7_BASE_ADDR }, |
| 275 | #endif |
| 276 | }; |
| 277 | |
| 278 | static int mxc_gpio_probe(struct udevice *dev) |
| 279 | { |
| 280 | struct mxc_bank_info *bank = dev_get_priv(dev); |
| 281 | struct mxc_gpio_plat *plat = dev_get_platdata(dev); |
| 282 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 283 | int banknum; |
| 284 | char name[18], *str; |
| 285 | |
| 286 | banknum = plat - mxc_plat; |
| 287 | sprintf(name, "GPIO%d_", banknum + 1); |
| 288 | str = strdup(name); |
| 289 | if (!str) |
| 290 | return -ENOMEM; |
| 291 | uc_priv->bank_name = str; |
| 292 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 293 | bank->regs = plat->regs; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | U_BOOT_DRIVER(gpio_mxc) = { |
| 299 | .name = "gpio_mxc", |
| 300 | .id = UCLASS_GPIO, |
| 301 | .ops = &gpio_mxc_ops, |
| 302 | .probe = mxc_gpio_probe, |
| 303 | .priv_auto_alloc_size = sizeof(struct mxc_bank_info), |
| 304 | }; |
| 305 | |
| 306 | U_BOOT_DEVICES(mxc_gpios) = { |
| 307 | { "gpio_mxc", &mxc_plat[0] }, |
| 308 | { "gpio_mxc", &mxc_plat[1] }, |
| 309 | { "gpio_mxc", &mxc_plat[2] }, |
| 310 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
| 311 | defined(CONFIG_MX53) || defined(CONFIG_MX6) |
| 312 | { "gpio_mxc", &mxc_plat[3] }, |
| 313 | #endif |
| 314 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) |
| 315 | { "gpio_mxc", &mxc_plat[4] }, |
| 316 | { "gpio_mxc", &mxc_plat[5] }, |
| 317 | #endif |
| 318 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) |
| 319 | { "gpio_mxc", &mxc_plat[6] }, |
| 320 | #endif |
| 321 | }; |
| 322 | #endif |