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 | */ |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <dm.h> |
| 11 | #include <malloc.h> |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 12 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 13 | #include <asm/gpio.h> |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Walter Lozano | e58c4f5 | 2020-07-29 12:31:18 -0300 | [diff] [blame] | 15 | #include <dt-structs.h> |
| 16 | #include <mapmem.h> |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 17 | |
Stefano Babic | 7faee91 | 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 | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 23 | #define GPIO_PER_BANK 32 |
| 24 | |
| 25 | struct mxc_gpio_plat { |
Walter Lozano | e58c4f5 | 2020-07-29 12:31:18 -0300 | [diff] [blame] | 26 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 27 | /* Put this first since driver model will copy the data here */ |
| 28 | struct dtd_gpio_mxc dtplat; |
| 29 | #endif |
Peng Fan | 86be426 | 2015-02-10 14:46:33 +0800 | [diff] [blame] | 30 | int bank_index; |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 31 | struct gpio_regs *regs; |
| 32 | }; |
| 33 | |
| 34 | struct mxc_bank_info { |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 35 | struct gpio_regs *regs; |
| 36 | }; |
| 37 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 38 | #if !CONFIG_IS_ENABLED(DM_GPIO) |
Lukasz Majewski | c00b093 | 2019-06-09 22:54:40 +0200 | [diff] [blame] | 39 | #define GPIO_TO_PORT(n) ((n) / 32) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 40 | |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 41 | /* GPIO port description */ |
| 42 | static unsigned long gpio_ports[] = { |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 43 | [0] = GPIO1_BASE_ADDR, |
| 44 | [1] = GPIO2_BASE_ADDR, |
| 45 | [2] = GPIO3_BASE_ADDR, |
Tom Rini | 31df83b | 2022-11-19 18:45:27 -0500 | [diff] [blame] | 46 | #if defined(CONFIG_MX51) || \ |
Adrian Alonso | 840d2e3 | 2015-08-11 11:19:51 -0500 | [diff] [blame] | 47 | 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) || \ |
Giulio Benetti | a82cd87 | 2020-01-10 15:47:03 +0100 | [diff] [blame] | 49 | defined(CONFIG_ARCH_IMX8) || defined(CONFIG_IMXRT1050) |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 50 | [3] = GPIO4_BASE_ADDR, |
| 51 | #endif |
Tom Rini | 31df83b | 2022-11-19 18:45:27 -0500 | [diff] [blame] | 52 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 53 | defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \ |
Giulio Benetti | a82cd87 | 2020-01-10 15:47:03 +0100 | [diff] [blame] | 54 | defined(CONFIG_ARCH_IMX8) || defined(CONFIG_IMXRT1050) |
Liu Hui-R64343 | a71164c | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 55 | [4] = GPIO5_BASE_ADDR, |
Giulio Benetti | a82cd87 | 2020-01-10 15:47:03 +0100 | [diff] [blame] | 56 | #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) || \ |
| 57 | defined(CONFIG_IMX8M) || defined(CONFIG_IMXRT1050)) |
Liu Hui-R64343 | a71164c | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 58 | [5] = GPIO6_BASE_ADDR, |
trem | cf233ed | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 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_MX53) || defined(CONFIG_MX6) || defined(CONFIG_MX7) || \ |
| 62 | defined(CONFIG_ARCH_IMX8) |
Fabio Estevam | 1b691df | 2018-01-03 12:33:05 -0200 | [diff] [blame] | 63 | #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)) |
Liu Hui-R64343 | a71164c | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 64 | [6] = GPIO7_BASE_ADDR, |
| 65 | #endif |
Peng Fan | f5dd87e | 2015-07-20 19:28:31 +0800 | [diff] [blame] | 66 | #endif |
Peng Fan | b2242e1 | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 67 | #if defined(CONFIG_ARCH_IMX8) |
| 68 | [7] = GPIO8_BASE_ADDR, |
| 69 | #endif |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 72 | static int mxc_gpio_direction(unsigned int gpio, |
| 73 | enum mxc_gpio_direction direction) |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 74 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 75 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 76 | struct gpio_regs *regs; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 77 | u32 l; |
| 78 | |
| 79 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 80 | return -1; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 81 | |
| 82 | gpio &= 0x1f; |
| 83 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 84 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 85 | |
| 86 | l = readl(®s->gpio_dir); |
| 87 | |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 88 | switch (direction) { |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 89 | case MXC_GPIO_DIRECTION_OUT: |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 90 | l |= 1 << gpio; |
| 91 | break; |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 92 | case MXC_GPIO_DIRECTION_IN: |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 93 | l &= ~(1 << gpio); |
| 94 | } |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 95 | writel(l, ®s->gpio_dir); |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 100 | int gpio_set_value(unsigned gpio, int value) |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 101 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 102 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 103 | struct gpio_regs *regs; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 104 | u32 l; |
| 105 | |
| 106 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 107 | return -1; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 108 | |
| 109 | gpio &= 0x1f; |
| 110 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 111 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 112 | |
| 113 | l = readl(®s->gpio_dr); |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 114 | if (value) |
| 115 | l |= 1 << gpio; |
| 116 | else |
| 117 | l &= ~(1 << gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 118 | writel(l, ®s->gpio_dr); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 119 | |
| 120 | return 0; |
Guennadi Liakhovetski | 7c6a856 | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 121 | } |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 122 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 123 | int gpio_get_value(unsigned gpio) |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 124 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 125 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 126 | struct gpio_regs *regs; |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 127 | u32 val; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 128 | |
| 129 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 130 | return -1; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 131 | |
| 132 | gpio &= 0x1f; |
| 133 | |
Stefano Babic | d77fe99 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 134 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 135 | |
Benoît Thébaudeau | daaf7a9 | 2012-08-20 10:55:41 +0000 | [diff] [blame] | 136 | val = (readl(®s->gpio_psr) >> gpio) & 0x01; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 137 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 138 | return val; |
Stefano Babic | a44d2a5 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 139 | } |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 140 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 141 | int gpio_request(unsigned gpio, const char *label) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 142 | { |
Vikram Narayanan | fbdf6bc | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 143 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 144 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 145 | return -1; |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 149 | int gpio_free(unsigned gpio) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 150 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 151 | return 0; |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 154 | int gpio_direction_input(unsigned gpio) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 155 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 156 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 159 | int gpio_direction_output(unsigned gpio, int value) |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 160 | { |
Dirk Behme | 1e0803f | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 161 | int ret = gpio_set_value(gpio, value); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 162 | |
| 163 | if (ret < 0) |
| 164 | return ret; |
| 165 | |
Dirk Behme | 1e0803f | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 166 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT); |
Stefano Babic | 7faee91 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 167 | } |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 168 | #endif |
| 169 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 170 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 171 | #include <fdtdec.h> |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 172 | static int mxc_gpio_is_output(struct gpio_regs *regs, int offset) |
| 173 | { |
| 174 | u32 val; |
| 175 | |
| 176 | val = readl(®s->gpio_dir); |
| 177 | |
| 178 | return val & (1 << offset) ? 1 : 0; |
| 179 | } |
| 180 | |
| 181 | static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset, |
| 182 | enum mxc_gpio_direction direction) |
| 183 | { |
| 184 | u32 l; |
| 185 | |
| 186 | l = readl(®s->gpio_dir); |
| 187 | |
| 188 | switch (direction) { |
| 189 | case MXC_GPIO_DIRECTION_OUT: |
| 190 | l |= 1 << offset; |
| 191 | break; |
| 192 | case MXC_GPIO_DIRECTION_IN: |
| 193 | l &= ~(1 << offset); |
| 194 | } |
| 195 | writel(l, ®s->gpio_dir); |
| 196 | } |
| 197 | |
| 198 | static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset, |
| 199 | int value) |
| 200 | { |
| 201 | u32 l; |
| 202 | |
| 203 | l = readl(®s->gpio_dr); |
| 204 | if (value) |
| 205 | l |= 1 << offset; |
| 206 | else |
| 207 | l &= ~(1 << offset); |
| 208 | writel(l, ®s->gpio_dr); |
| 209 | } |
| 210 | |
| 211 | static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset) |
| 212 | { |
| 213 | return (readl(®s->gpio_psr) >> offset) & 0x01; |
| 214 | } |
| 215 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 216 | /* set GPIO pin 'gpio' as an input */ |
| 217 | static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 218 | { |
| 219 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 220 | |
| 221 | /* Configure GPIO direction as input. */ |
| 222 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 228 | static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 229 | int value) |
| 230 | { |
| 231 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 232 | |
| 233 | /* Configure GPIO output value. */ |
| 234 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 235 | |
| 236 | /* Configure GPIO direction as output. */ |
| 237 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /* read GPIO IN value of pin 'gpio' */ |
| 243 | static int mxc_gpio_get_value(struct udevice *dev, unsigned offset) |
| 244 | { |
| 245 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 246 | |
| 247 | return mxc_gpio_bank_get_value(bank->regs, offset); |
| 248 | } |
| 249 | |
| 250 | /* write GPIO OUT value to pin 'gpio' */ |
| 251 | static int mxc_gpio_set_value(struct udevice *dev, unsigned offset, |
| 252 | int value) |
| 253 | { |
| 254 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 255 | |
| 256 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 261 | static int mxc_gpio_get_function(struct udevice *dev, unsigned offset) |
| 262 | { |
| 263 | struct mxc_bank_info *bank = dev_get_priv(dev); |
| 264 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 265 | /* GPIOF_FUNC is not implemented yet */ |
| 266 | if (mxc_gpio_is_output(bank->regs, offset)) |
| 267 | return GPIOF_OUTPUT; |
| 268 | else |
| 269 | return GPIOF_INPUT; |
| 270 | } |
| 271 | |
| 272 | static const struct dm_gpio_ops gpio_mxc_ops = { |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 273 | .direction_input = mxc_gpio_direction_input, |
| 274 | .direction_output = mxc_gpio_direction_output, |
| 275 | .get_value = mxc_gpio_get_value, |
| 276 | .set_value = mxc_gpio_set_value, |
| 277 | .get_function = mxc_gpio_get_function, |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 278 | }; |
| 279 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 280 | static int mxc_gpio_probe(struct udevice *dev) |
| 281 | { |
| 282 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 283 | struct mxc_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 284 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 285 | int banknum; |
| 286 | char name[18], *str; |
| 287 | |
Walter Lozano | e58c4f5 | 2020-07-29 12:31:18 -0300 | [diff] [blame] | 288 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 289 | struct dtd_gpio_mxc *dtplat = &plat->dtplat; |
| 290 | |
| 291 | plat->regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]); |
| 292 | #endif |
| 293 | |
Peng Fan | 86be426 | 2015-02-10 14:46:33 +0800 | [diff] [blame] | 294 | banknum = plat->bank_index; |
Ye Li | 81a4421 | 2020-06-09 20:28:02 -0700 | [diff] [blame] | 295 | if (IS_ENABLED(CONFIG_ARCH_IMX8)) |
| 296 | sprintf(name, "GPIO%d_", banknum); |
| 297 | else |
| 298 | sprintf(name, "GPIO%d_", banknum + 1); |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 299 | str = strdup(name); |
| 300 | if (!str) |
| 301 | return -ENOMEM; |
| 302 | uc_priv->bank_name = str; |
| 303 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 304 | bank->regs = plat->regs; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 309 | static int mxc_gpio_of_to_plat(struct udevice *dev) |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 310 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 311 | struct mxc_gpio_plat *plat = dev_get_plat(dev); |
Walter Lozano | e58c4f5 | 2020-07-29 12:31:18 -0300 | [diff] [blame] | 312 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 313 | fdt_addr_t addr; |
Tom Rini | 322ebe9 | 2020-08-04 11:11:02 -0400 | [diff] [blame] | 314 | addr = dev_read_addr(dev); |
Walter Lozano | e58c4f5 | 2020-07-29 12:31:18 -0300 | [diff] [blame] | 315 | if (addr == FDT_ADDR_T_NONE) |
| 316 | return -EINVAL; |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 317 | |
Walter Lozano | e58c4f5 | 2020-07-29 12:31:18 -0300 | [diff] [blame] | 318 | plat->regs = (struct gpio_regs *)addr; |
| 319 | } |
Simon Glass | b4db372 | 2020-12-16 21:20:24 -0700 | [diff] [blame] | 320 | plat->bank_index = dev_seq(dev); |
Ye Li | 755f3ee | 2020-06-09 20:29:51 -0700 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 324 | |
Ye Li | 755f3ee | 2020-06-09 20:29:51 -0700 | [diff] [blame] | 325 | static int mxc_gpio_bind(struct udevice *dev) |
| 326 | { |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static const struct udevice_id mxc_gpio_ids[] = { |
| 331 | { .compatible = "fsl,imx35-gpio" }, |
| 332 | { } |
| 333 | }; |
| 334 | |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 335 | U_BOOT_DRIVER(gpio_mxc) = { |
| 336 | .name = "gpio_mxc", |
| 337 | .id = UCLASS_GPIO, |
| 338 | .ops = &gpio_mxc_ops, |
| 339 | .probe = mxc_gpio_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 340 | .of_to_plat = mxc_gpio_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 341 | .plat_auto = sizeof(struct mxc_gpio_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 342 | .priv_auto = sizeof(struct mxc_bank_info), |
Peng Fan | 0ed2cb1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 343 | .of_match = mxc_gpio_ids, |
| 344 | .bind = mxc_gpio_bind, |
| 345 | }; |
| 346 | |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 347 | DM_DRIVER_ALIAS(gpio_mxc, fsl_imx6q_gpio) |
Walter Lozano | e58c4f5 | 2020-07-29 12:31:18 -0300 | [diff] [blame] | 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 }, |
Tom Rini | 31df83b | 2022-11-19 18:45:27 -0500 | [diff] [blame] | 354 | #if 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 |
Tom Rini | 31df83b | 2022-11-19 18:45:27 -0500 | [diff] [blame] | 359 | #if 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 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 374 | U_BOOT_DRVINFOS(mxc_gpios) = { |
Simon Glass | 2772b4d | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 375 | { "gpio_mxc", &mxc_plat[0] }, |
| 376 | { "gpio_mxc", &mxc_plat[1] }, |
| 377 | { "gpio_mxc", &mxc_plat[2] }, |
Tom Rini | 31df83b | 2022-11-19 18:45:27 -0500 | [diff] [blame] | 378 | #if 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 |
Tom Rini | 31df83b | 2022-11-19 18:45:27 -0500 | [diff] [blame] | 383 | #if 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 |