Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009 |
| 4 | * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de> |
| 5 | * |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 6 | * Copyright (C) 2011 |
| 7 | * Stefano Babic, DENX Software Engineering, <sbabic@denx.de> |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <dm.h> |
| 12 | #include <malloc.h> |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 13 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 16 | |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 17 | enum mxc_gpio_direction { |
| 18 | MXC_GPIO_DIRECTION_IN, |
| 19 | MXC_GPIO_DIRECTION_OUT, |
| 20 | }; |
| 21 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 22 | #define GPIO_PER_BANK 32 |
| 23 | |
| 24 | struct mxc_gpio_plat { |
Peng Fan | 86be426 | 2015-02-10 14:46:33 +0800 | [diff] [blame] | 25 | int bank_index; |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 26 | struct gpio_regs *regs; |
| 27 | }; |
| 28 | |
| 29 | struct mxc_bank_info { |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 30 | struct gpio_regs *regs; |
| 31 | }; |
| 32 | |
| 33 | #ifndef CONFIG_DM_GPIO |
Lukasz Majewski | c00b093 | 2019-06-09 22:54:40 +0200 | [diff] [blame] | 34 | #define GPIO_TO_PORT(n) ((n) / 32) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 35 | |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 36 | /* GPIO port description */ |
| 37 | static unsigned long gpio_ports[] = { |
Stefano Babic | d77fe99 | 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 | cf233ed | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 41 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Adrian Alonso | 840d2e3 | 2015-08-11 11:19:51 -0500 | [diff] [blame] | 42 | defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 43 | defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \ |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 44 | defined(CONFIG_ARCH_IMX8) |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 45 | [3] = GPIO4_BASE_ADDR, |
| 46 | #endif |
Adrian Alonso | 840d2e3 | 2015-08-11 11:19:51 -0500 | [diff] [blame] | 47 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 48 | defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \ |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 49 | defined(CONFIG_ARCH_IMX8) |
Liu Hui-R64343 | a71164c | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 50 | [4] = GPIO5_BASE_ADDR, |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 51 | #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) || defined(CONFIG_IMX8M)) |
Liu Hui-R64343 | a71164c | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 52 | [5] = GPIO6_BASE_ADDR, |
trem | cf233ed | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 53 | #endif |
Peng Fan | f5dd87e | 2015-07-20 19:28:31 +0800 | [diff] [blame] | 54 | #endif |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 55 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_MX7) || \ |
| 56 | defined(CONFIG_ARCH_IMX8) |
Fabio Estevam | 1b691df | 2018-01-03 12:33:05 -0200 | [diff] [blame] | 57 | #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)) |
Liu Hui-R64343 | a71164c | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 58 | [6] = GPIO7_BASE_ADDR, |
| 59 | #endif |
Peng Fan | f5dd87e | 2015-07-20 19:28:31 +0800 | [diff] [blame] | 60 | #endif |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 61 | #if defined(CONFIG_ARCH_IMX8) |
| 62 | [7] = GPIO8_BASE_ADDR, |
| 63 | #endif |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 66 | static int mxc_gpio_direction(unsigned int gpio, |
| 67 | enum mxc_gpio_direction direction) |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 68 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 69 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 70 | struct gpio_regs *regs; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 71 | u32 l; |
| 72 | |
| 73 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 74 | return -1; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 75 | |
| 76 | gpio &= 0x1f; |
| 77 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 78 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 79 | |
| 80 | l = readl(®s->gpio_dir); |
| 81 | |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 82 | switch (direction) { |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 83 | case MXC_GPIO_DIRECTION_OUT: |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 84 | l |= 1 << gpio; |
| 85 | break; |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 86 | case MXC_GPIO_DIRECTION_IN: |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 87 | l &= ~(1 << gpio); |
| 88 | } |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 89 | writel(l, ®s->gpio_dir); |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 94 | int gpio_set_value(unsigned gpio, int value) |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 95 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 96 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 97 | struct gpio_regs *regs; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 98 | u32 l; |
| 99 | |
| 100 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 101 | return -1; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 102 | |
| 103 | gpio &= 0x1f; |
| 104 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 105 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 106 | |
| 107 | l = readl(®s->gpio_dr); |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 108 | if (value) |
| 109 | l |= 1 << gpio; |
| 110 | else |
| 111 | l &= ~(1 << gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 112 | writel(l, ®s->gpio_dr); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 113 | |
| 114 | return 0; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 115 | } |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 116 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 117 | int gpio_get_value(unsigned gpio) |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 118 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 119 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 120 | struct gpio_regs *regs; |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 121 | u32 val; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 122 | |
| 123 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 124 | return -1; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 125 | |
| 126 | gpio &= 0x1f; |
| 127 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 128 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 129 | |
Benoît Thébaudeau | daaf7a9 | 2012-08-20 10:55:41 +0000 | [diff] [blame] | 130 | val = (readl(®s->gpio_psr) >> gpio) & 0x01; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 131 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 132 | return val; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 133 | } |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 134 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 135 | int gpio_request(unsigned gpio, const char *label) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 136 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 137 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 138 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 139 | return -1; |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 143 | int gpio_free(unsigned gpio) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 144 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 145 | return 0; |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 148 | int gpio_direction_input(unsigned gpio) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 149 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 150 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 153 | int gpio_direction_output(unsigned gpio, int value) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 154 | { |
Dirk Behme | 1e0803f | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 155 | int ret = gpio_set_value(gpio, value); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 156 | |
| 157 | if (ret < 0) |
| 158 | return ret; |
| 159 | |
Dirk Behme | 1e0803f | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 160 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 161 | } |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 162 | #endif |
| 163 | |
| 164 | #ifdef CONFIG_DM_GPIO |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 165 | #include <fdtdec.h> |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 166 | static int mxc_gpio_is_output(struct gpio_regs *regs, int offset) |
| 167 | { |
| 168 | u32 val; |
| 169 | |
| 170 | val = readl(®s->gpio_dir); |
| 171 | |
| 172 | return val & (1 << offset) ? 1 : 0; |
| 173 | } |
| 174 | |
| 175 | static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset, |
| 176 | enum mxc_gpio_direction direction) |
| 177 | { |
| 178 | u32 l; |
| 179 | |
| 180 | l = readl(®s->gpio_dir); |
| 181 | |
| 182 | switch (direction) { |
| 183 | case MXC_GPIO_DIRECTION_OUT: |
| 184 | l |= 1 << offset; |
| 185 | break; |
| 186 | case MXC_GPIO_DIRECTION_IN: |
| 187 | l &= ~(1 << offset); |
| 188 | } |
| 189 | writel(l, ®s->gpio_dir); |
| 190 | } |
| 191 | |
| 192 | static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset, |
| 193 | int value) |
| 194 | { |
| 195 | u32 l; |
| 196 | |
| 197 | l = readl(®s->gpio_dr); |
| 198 | if (value) |
| 199 | l |= 1 << offset; |
| 200 | else |
| 201 | l &= ~(1 << offset); |
| 202 | writel(l, ®s->gpio_dr); |
| 203 | } |
| 204 | |
| 205 | static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset) |
| 206 | { |
| 207 | return (readl(®s->gpio_psr) >> offset) & 0x01; |
| 208 | } |
| 209 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 210 | /* set GPIO pin 'gpio' as an input */ |
| 211 | static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 212 | { |
| 213 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 214 | |
| 215 | /* Configure GPIO direction as input. */ |
| 216 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 222 | static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 223 | int value) |
| 224 | { |
| 225 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 226 | |
| 227 | /* Configure GPIO output value. */ |
| 228 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 229 | |
| 230 | /* Configure GPIO direction as output. */ |
| 231 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* read GPIO IN value of pin 'gpio' */ |
| 237 | static int mxc_gpio_get_value(struct udevice *dev, unsigned offset) |
| 238 | { |
| 239 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 240 | |
| 241 | return mxc_gpio_bank_get_value(bank->regs, offset); |
| 242 | } |
| 243 | |
| 244 | /* write GPIO OUT value to pin 'gpio' */ |
| 245 | static int mxc_gpio_set_value(struct udevice *dev, unsigned offset, |
| 246 | int value) |
| 247 | { |
| 248 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 249 | |
| 250 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 255 | static int mxc_gpio_get_function(struct udevice *dev, unsigned offset) |
| 256 | { |
| 257 | struct mxc_bank_info *bank = dev_get_priv(dev); |
| 258 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 259 | /* GPIOF_FUNC is not implemented yet */ |
| 260 | if (mxc_gpio_is_output(bank->regs, offset)) |
| 261 | return GPIOF_OUTPUT; |
| 262 | else |
| 263 | return GPIOF_INPUT; |
| 264 | } |
| 265 | |
| 266 | static const struct dm_gpio_ops gpio_mxc_ops = { |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 267 | .direction_input = mxc_gpio_direction_input, |
| 268 | .direction_output = mxc_gpio_direction_output, |
| 269 | .get_value = mxc_gpio_get_value, |
| 270 | .set_value = mxc_gpio_set_value, |
| 271 | .get_function = mxc_gpio_get_function, |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 272 | }; |
| 273 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 274 | static int mxc_gpio_probe(struct udevice *dev) |
| 275 | { |
| 276 | struct mxc_bank_info *bank = dev_get_priv(dev); |
| 277 | struct mxc_gpio_plat *plat = dev_get_platdata(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 278 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 279 | int banknum; |
| 280 | char name[18], *str; |
| 281 | |
Peng Fan | 86be426 | 2015-02-10 14:46:33 +0800 | [diff] [blame] | 282 | banknum = plat->bank_index; |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 283 | sprintf(name, "GPIO%d_", banknum + 1); |
| 284 | str = strdup(name); |
| 285 | if (!str) |
| 286 | return -ENOMEM; |
| 287 | uc_priv->bank_name = str; |
| 288 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 289 | bank->regs = plat->regs; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 294 | static int mxc_gpio_bind(struct udevice *dev) |
| 295 | { |
| 296 | struct mxc_gpio_plat *plat = dev->platdata; |
| 297 | fdt_addr_t addr; |
| 298 | |
| 299 | /* |
| 300 | * If platdata already exsits, directly return. |
| 301 | * Actually only when DT is not supported, platdata |
| 302 | * is statically initialized in U_BOOT_DEVICES.Here |
| 303 | * will return. |
| 304 | */ |
| 305 | if (plat) |
| 306 | return 0; |
| 307 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 308 | addr = devfdt_get_addr(dev); |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 309 | if (addr == FDT_ADDR_T_NONE) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 310 | return -EINVAL; |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * TODO: |
| 314 | * When every board is converted to driver model and DT is supported, |
| 315 | * this can be done by auto-alloc feature, but not using calloc |
| 316 | * to alloc memory for platdata. |
Simon Glass | 87f4cb7 | 2017-09-17 16:54:52 -0600 | [diff] [blame] | 317 | * |
| 318 | * For example mxc_plat below uses platform data rather than device |
| 319 | * tree. |
| 320 | * |
| 321 | * NOTE: DO NOT COPY this code if you are using device tree. |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 322 | */ |
| 323 | plat = calloc(1, sizeof(*plat)); |
| 324 | if (!plat) |
| 325 | return -ENOMEM; |
| 326 | |
| 327 | plat->regs = (struct gpio_regs *)addr; |
| 328 | plat->bank_index = dev->req_seq; |
| 329 | dev->platdata = plat; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static const struct udevice_id mxc_gpio_ids[] = { |
| 335 | { .compatible = "fsl,imx35-gpio" }, |
| 336 | { } |
| 337 | }; |
| 338 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 339 | U_BOOT_DRIVER(gpio_mxc) = { |
| 340 | .name = "gpio_mxc", |
| 341 | .id = UCLASS_GPIO, |
| 342 | .ops = &gpio_mxc_ops, |
| 343 | .probe = mxc_gpio_probe, |
| 344 | .priv_auto_alloc_size = sizeof(struct mxc_bank_info), |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 345 | .of_match = mxc_gpio_ids, |
| 346 | .bind = mxc_gpio_bind, |
| 347 | }; |
| 348 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 349 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 350 | static const struct mxc_gpio_plat mxc_plat[] = { |
| 351 | { 0, (struct gpio_regs *)GPIO1_BASE_ADDR }, |
| 352 | { 1, (struct gpio_regs *)GPIO2_BASE_ADDR }, |
| 353 | { 2, (struct gpio_regs *)GPIO3_BASE_ADDR }, |
| 354 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Peng Fan | fa94fb4 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 355 | defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 356 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 357 | { 3, (struct gpio_regs *)GPIO4_BASE_ADDR }, |
| 358 | #endif |
Peng Fan | fa94fb4 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 359 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 360 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 361 | { 4, (struct gpio_regs *)GPIO5_BASE_ADDR }, |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 362 | #ifndef CONFIG_IMX8M |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 363 | { 5, (struct gpio_regs *)GPIO6_BASE_ADDR }, |
| 364 | #endif |
Peng Fan | fa94fb4 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 365 | #endif |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 366 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8) |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 367 | { 6, (struct gpio_regs *)GPIO7_BASE_ADDR }, |
| 368 | #endif |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 369 | #if defined(CONFIG_ARCH_IMX8) |
| 370 | { 7, (struct gpio_regs *)GPIO8_BASE_ADDR }, |
| 371 | #endif |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | U_BOOT_DEVICES(mxc_gpios) = { |
| 375 | { "gpio_mxc", &mxc_plat[0] }, |
| 376 | { "gpio_mxc", &mxc_plat[1] }, |
| 377 | { "gpio_mxc", &mxc_plat[2] }, |
| 378 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Peng Fan | fa94fb4 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 379 | defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 380 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 381 | { "gpio_mxc", &mxc_plat[3] }, |
| 382 | #endif |
Peng Fan | fa94fb4 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 383 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 384 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 385 | { "gpio_mxc", &mxc_plat[4] }, |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 386 | #ifndef CONFIG_IMX8M |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 387 | { "gpio_mxc", &mxc_plat[5] }, |
| 388 | #endif |
Peng Fan | fa94fb4 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 389 | #endif |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 390 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8) |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 391 | { "gpio_mxc", &mxc_plat[6] }, |
| 392 | #endif |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 393 | #if defined(CONFIG_ARCH_IMX8) |
| 394 | { "gpio_mxc", &mxc_plat[7] }, |
| 395 | #endif |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 396 | }; |
| 397 | #endif |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 398 | #endif |