Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 12 | #include <asm/arch/gpio.h> |
| 13 | #include <asm/arch/stm32.h> |
| 14 | #include <asm/gpio.h> |
| 15 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
| 19 | #include <linux/io.h> |
| 20 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 21 | #define MODE_BITS(gpio_pin) ((gpio_pin) * 2) |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 22 | #define MODE_BITS_MASK 3 |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 23 | #define BSRR_BIT(gpio_pin, value) BIT((gpio_pin) + (value ? 0 : 16)) |
| 24 | |
| 25 | #define PUPD_BITS(gpio_pin) ((gpio_pin) * 2) |
| 26 | #define PUPD_MASK 3 |
| 27 | |
| 28 | #define OTYPE_BITS(gpio_pin) (gpio_pin) |
| 29 | #define OTYPE_MSK 1 |
| 30 | |
| 31 | static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs, |
| 32 | int idx, |
| 33 | int mode) |
| 34 | { |
| 35 | int bits_index; |
| 36 | int mask; |
| 37 | |
| 38 | bits_index = MODE_BITS(idx); |
| 39 | mask = MODE_BITS_MASK << bits_index; |
| 40 | |
| 41 | clrsetbits_le32(®s->moder, mask, mode << bits_index); |
| 42 | } |
| 43 | |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 44 | static int stm32_gpio_get_moder(struct stm32_gpio_regs *regs, int idx) |
| 45 | { |
| 46 | return (readl(®s->moder) >> MODE_BITS(idx)) & MODE_BITS_MASK; |
| 47 | } |
| 48 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 49 | static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs, |
| 50 | int idx, |
| 51 | enum stm32_gpio_otype otype) |
| 52 | { |
| 53 | int bits; |
| 54 | |
| 55 | bits = OTYPE_BITS(idx); |
| 56 | clrsetbits_le32(®s->otyper, OTYPE_MSK << bits, otype << bits); |
| 57 | } |
| 58 | |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 59 | static enum stm32_gpio_otype stm32_gpio_get_otype(struct stm32_gpio_regs *regs, |
| 60 | int idx) |
| 61 | { |
| 62 | return (readl(®s->otyper) >> OTYPE_BITS(idx)) & OTYPE_MSK; |
| 63 | } |
| 64 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 65 | static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs, |
| 66 | int idx, |
| 67 | enum stm32_gpio_pupd pupd) |
| 68 | { |
| 69 | int bits; |
| 70 | |
| 71 | bits = PUPD_BITS(idx); |
| 72 | clrsetbits_le32(®s->pupdr, PUPD_MASK << bits, pupd << bits); |
| 73 | } |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 74 | |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 75 | static enum stm32_gpio_pupd stm32_gpio_get_pupd(struct stm32_gpio_regs *regs, |
| 76 | int idx) |
| 77 | { |
| 78 | return (readl(®s->pupdr) >> PUPD_BITS(idx)) & PUPD_MASK; |
| 79 | } |
| 80 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 81 | /* |
| 82 | * convert gpio offset to gpio index taking into account gpio holes |
| 83 | * into gpio bank |
| 84 | */ |
| 85 | int stm32_offset_to_index(struct udevice *dev, unsigned int offset) |
| 86 | { |
| 87 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
Patrick Delaunay | 2a6c7ad | 2019-06-21 15:26:46 +0200 | [diff] [blame] | 88 | unsigned int idx = 0; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 89 | int i; |
| 90 | |
| 91 | for (i = 0; i < STM32_GPIOS_PER_BANK; i++) { |
| 92 | if (priv->gpio_range & BIT(i)) { |
| 93 | if (idx == offset) |
| 94 | return idx; |
| 95 | idx++; |
| 96 | } |
| 97 | } |
| 98 | /* shouldn't happen */ |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 102 | static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 103 | { |
| 104 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 105 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 106 | int idx; |
| 107 | |
| 108 | idx = stm32_offset_to_index(dev, offset); |
| 109 | if (idx < 0) |
| 110 | return idx; |
| 111 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 112 | stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 118 | int value) |
| 119 | { |
| 120 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 121 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 122 | int idx; |
| 123 | |
| 124 | idx = stm32_offset_to_index(dev, offset); |
| 125 | if (idx < 0) |
| 126 | return idx; |
| 127 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 128 | stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT); |
Patrice Chotard | 4e91500 | 2018-08-09 11:57:57 +0200 | [diff] [blame] | 129 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 130 | writel(BSRR_BIT(idx, value), ®s->bsrr); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int stm32_gpio_get_value(struct udevice *dev, unsigned offset) |
| 136 | { |
| 137 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 138 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 139 | int idx; |
| 140 | |
| 141 | idx = stm32_offset_to_index(dev, offset); |
| 142 | if (idx < 0) |
| 143 | return idx; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 144 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 145 | return readl(®s->idr) & BIT(idx) ? 1 : 0; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
| 149 | { |
| 150 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 151 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 152 | int idx; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 153 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 154 | idx = stm32_offset_to_index(dev, offset); |
| 155 | if (idx < 0) |
| 156 | return idx; |
| 157 | |
| 158 | writel(BSRR_BIT(idx, value), ®s->bsrr); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 163 | static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset) |
| 164 | { |
| 165 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 166 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 167 | int bits_index; |
| 168 | int mask; |
| 169 | int idx; |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 170 | u32 mode; |
| 171 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 172 | idx = stm32_offset_to_index(dev, offset); |
| 173 | if (idx < 0) |
| 174 | return idx; |
| 175 | |
| 176 | bits_index = MODE_BITS(idx); |
| 177 | mask = MODE_BITS_MASK << bits_index; |
| 178 | |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 179 | mode = (readl(®s->moder) & mask) >> bits_index; |
| 180 | if (mode == STM32_GPIO_MODE_OUT) |
| 181 | return GPIOF_OUTPUT; |
| 182 | if (mode == STM32_GPIO_MODE_IN) |
| 183 | return GPIOF_INPUT; |
| 184 | if (mode == STM32_GPIO_MODE_AN) |
| 185 | return GPIOF_UNUSED; |
| 186 | |
| 187 | return GPIOF_FUNC; |
| 188 | } |
| 189 | |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 190 | static int stm32_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, |
| 191 | ulong flags) |
| 192 | { |
| 193 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 194 | struct stm32_gpio_regs *regs = priv->regs; |
| 195 | int idx; |
| 196 | |
| 197 | idx = stm32_offset_to_index(dev, offset); |
| 198 | if (idx < 0) |
| 199 | return idx; |
| 200 | |
| 201 | if (flags & GPIOD_IS_OUT) { |
| 202 | int value = GPIOD_FLAGS_OUTPUT(flags); |
| 203 | |
| 204 | if (flags & GPIOD_OPEN_DRAIN) |
| 205 | stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_OD); |
| 206 | else |
| 207 | stm32_gpio_set_otype(regs, idx, STM32_GPIO_OTYPE_PP); |
| 208 | stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_OUT); |
| 209 | writel(BSRR_BIT(idx, value), ®s->bsrr); |
| 210 | |
| 211 | } else if (flags & GPIOD_IS_IN) { |
| 212 | stm32_gpio_set_moder(regs, idx, STM32_GPIO_MODE_IN); |
| 213 | if (flags & GPIOD_PULL_UP) |
| 214 | stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_UP); |
| 215 | else if (flags & GPIOD_PULL_DOWN) |
| 216 | stm32_gpio_set_pupd(regs, idx, STM32_GPIO_PUPD_DOWN); |
| 217 | } |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int stm32_gpio_get_dir_flags(struct udevice *dev, unsigned int offset, |
| 223 | ulong *flags) |
| 224 | { |
| 225 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 226 | struct stm32_gpio_regs *regs = priv->regs; |
| 227 | int idx; |
| 228 | ulong dir_flags = 0; |
| 229 | |
| 230 | idx = stm32_offset_to_index(dev, offset); |
| 231 | if (idx < 0) |
| 232 | return idx; |
| 233 | |
| 234 | switch (stm32_gpio_get_moder(regs, idx)) { |
| 235 | case STM32_GPIO_MODE_OUT: |
| 236 | dir_flags |= GPIOD_IS_OUT; |
| 237 | if (stm32_gpio_get_otype(regs, idx) == STM32_GPIO_OTYPE_OD) |
| 238 | dir_flags |= GPIOD_OPEN_DRAIN; |
| 239 | if (readl(®s->idr) & BIT(idx)) |
| 240 | dir_flags |= GPIOD_IS_OUT_ACTIVE; |
| 241 | break; |
| 242 | case STM32_GPIO_MODE_IN: |
| 243 | dir_flags |= GPIOD_IS_IN; |
| 244 | switch (stm32_gpio_get_pupd(regs, idx)) { |
| 245 | case STM32_GPIO_PUPD_UP: |
| 246 | dir_flags |= GPIOD_PULL_UP; |
| 247 | break; |
| 248 | case STM32_GPIO_PUPD_DOWN: |
| 249 | dir_flags |= GPIOD_PULL_DOWN; |
| 250 | break; |
| 251 | default: |
| 252 | break; |
| 253 | } |
| 254 | break; |
| 255 | default: |
| 256 | break; |
| 257 | } |
| 258 | *flags = dir_flags; |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 263 | static const struct dm_gpio_ops gpio_stm32_ops = { |
| 264 | .direction_input = stm32_gpio_direction_input, |
| 265 | .direction_output = stm32_gpio_direction_output, |
| 266 | .get_value = stm32_gpio_get_value, |
| 267 | .set_value = stm32_gpio_set_value, |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 268 | .get_function = stm32_gpio_get_function, |
Patrick Delaunay | c6d455c | 2020-06-04 14:30:25 +0200 | [diff] [blame] | 269 | .set_dir_flags = stm32_gpio_set_dir_flags, |
Patrick Delaunay | b087cab | 2020-06-04 14:30:26 +0200 | [diff] [blame] | 270 | .get_dir_flags = stm32_gpio_get_dir_flags, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | static int gpio_stm32_probe(struct udevice *dev) |
| 274 | { |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 275 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
Patrice Chotard | 159d157 | 2018-12-03 10:52:53 +0100 | [diff] [blame] | 276 | struct clk clk; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 277 | fdt_addr_t addr; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 278 | int ret; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 279 | |
Patrick Delaunay | d78f968 | 2018-03-12 10:46:07 +0100 | [diff] [blame] | 280 | addr = dev_read_addr(dev); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 281 | if (addr == FDT_ADDR_T_NONE) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | priv->regs = (struct stm32_gpio_regs *)addr; |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 285 | |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 286 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 287 | struct ofnode_phandle_args args; |
| 288 | const char *name; |
| 289 | int i; |
| 290 | |
Patrick Delaunay | d78f968 | 2018-03-12 10:46:07 +0100 | [diff] [blame] | 291 | name = dev_read_string(dev, "st,bank-name"); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 292 | if (!name) |
| 293 | return -EINVAL; |
| 294 | uc_priv->bank_name = name; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 295 | |
| 296 | i = 0; |
| 297 | ret = dev_read_phandle_with_args(dev, "gpio-ranges", |
| 298 | NULL, 3, i, &args); |
| 299 | |
Patrice Chotard | 6225305 | 2019-01-04 10:55:05 +0100 | [diff] [blame] | 300 | if (ret == -ENOENT) { |
| 301 | uc_priv->gpio_count = STM32_GPIOS_PER_BANK; |
| 302 | priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0); |
| 303 | } |
| 304 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 305 | while (ret != -ENOENT) { |
| 306 | priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1, |
| 307 | args.args[0]); |
| 308 | |
| 309 | uc_priv->gpio_count += args.args[2]; |
| 310 | |
| 311 | ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3, |
| 312 | ++i, &args); |
| 313 | } |
| 314 | |
| 315 | dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n", |
| 316 | (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count, |
| 317 | priv->gpio_range); |
Patrick Delaunay | b1c6014 | 2020-04-22 14:29:17 +0200 | [diff] [blame] | 318 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 319 | ret = clk_get_by_index(dev, 0, &clk); |
| 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | |
| 323 | ret = clk_enable(&clk); |
| 324 | |
| 325 | if (ret) { |
| 326 | dev_err(dev, "failed to enable clock\n"); |
| 327 | return ret; |
| 328 | } |
| 329 | debug("clock enabled for device %s\n", dev->name); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 334 | U_BOOT_DRIVER(gpio_stm32) = { |
| 335 | .name = "gpio_stm32", |
| 336 | .id = UCLASS_GPIO, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 337 | .probe = gpio_stm32_probe, |
| 338 | .ops = &gpio_stm32_ops, |
Bin Meng | b508ee5 | 2018-10-24 06:36:30 -0700 | [diff] [blame] | 339 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 340 | .priv_auto_alloc_size = sizeof(struct stm32_gpio_priv), |
| 341 | }; |