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