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