Patrick Delaunay | 10bccd0 | 2020-09-09 17:50:15 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2017-2020 STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_PINCTRL |
| 7 | |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 8 | #include <common.h> |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 9 | #include <dm.h> |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 10 | #include <hwspinlock.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 13 | #include <asm/gpio.h> |
| 14 | #include <asm/io.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Patrice Chotard | 7385826 | 2019-07-30 19:16:10 +0200 | [diff] [blame] | 16 | #include <dm/lists.h> |
| 17 | #include <dm/pinctrl.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 20 | #include <linux/libfdt.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 21 | #include <linux/printk.h> |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 22 | |
Patrick Delaunay | 56a368f | 2021-10-22 20:12:34 +0200 | [diff] [blame] | 23 | #include "../gpio/stm32_gpio_priv.h" |
| 24 | |
Vikas Manocha | 58fb3c8 | 2017-04-10 15:03:04 -0700 | [diff] [blame] | 25 | #define MAX_PINS_ONE_IP 70 |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 26 | #define MODE_BITS_MASK 3 |
| 27 | #define OSPEED_MASK 3 |
| 28 | #define PUPD_MASK 3 |
| 29 | #define OTYPE_MSK 1 |
| 30 | #define AFR_MASK 0xF |
| 31 | |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 32 | struct stm32_pinctrl_priv { |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 33 | struct hwspinlock hws; |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 34 | int pinctrl_ngpios; |
| 35 | struct list_head gpio_dev; |
| 36 | }; |
| 37 | |
| 38 | struct stm32_gpio_bank { |
| 39 | struct udevice *gpio_dev; |
| 40 | struct list_head list; |
| 41 | }; |
| 42 | |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 43 | #ifndef CONFIG_SPL_BUILD |
| 44 | |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 45 | static char pin_name[PINNAME_SIZE]; |
Patrice Chotard | 4382e55 | 2022-04-22 09:38:29 +0200 | [diff] [blame] | 46 | static const char * const pinmux_mode[GPIOF_COUNT] = { |
| 47 | [GPIOF_INPUT] = "gpio input", |
| 48 | [GPIOF_OUTPUT] = "gpio output", |
| 49 | [GPIOF_UNUSED] = "analog", |
| 50 | [GPIOF_UNKNOWN] = "unknown", |
| 51 | [GPIOF_FUNC] = "alt function", |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
Patrick Delaunay | b305dbc | 2020-10-28 10:49:07 +0100 | [diff] [blame] | 54 | static const char * const pinmux_bias[] = { |
| 55 | [STM32_GPIO_PUPD_NO] = "", |
| 56 | [STM32_GPIO_PUPD_UP] = "pull-up", |
| 57 | [STM32_GPIO_PUPD_DOWN] = "pull-down", |
Patrick Delaunay | da7a0bb | 2020-06-04 14:30:33 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Patrick Delaunay | 1da4269 | 2021-01-21 17:39:07 +0100 | [diff] [blame] | 60 | static const char * const pinmux_otype[] = { |
Patrick Delaunay | b305dbc | 2020-10-28 10:49:07 +0100 | [diff] [blame] | 61 | [STM32_GPIO_OTYPE_PP] = "push-pull", |
| 62 | [STM32_GPIO_OTYPE_OD] = "open-drain", |
Patrick Delaunay | da7a0bb | 2020-06-04 14:30:33 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Patrice Chotard | bff0d84 | 2023-03-27 09:46:41 +0200 | [diff] [blame] | 65 | static const char * const pinmux_speed[] = { |
| 66 | [STM32_GPIO_SPEED_2M] = "Low speed", |
| 67 | [STM32_GPIO_SPEED_25M] = "Medium speed", |
| 68 | [STM32_GPIO_SPEED_50M] = "High speed", |
| 69 | [STM32_GPIO_SPEED_100M] = "Very-high speed", |
| 70 | }; |
| 71 | |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 72 | static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset) |
| 73 | { |
| 74 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 75 | struct stm32_gpio_regs *regs = priv->regs; |
| 76 | u32 af; |
| 77 | u32 alt_shift = (offset % 8) * 4; |
| 78 | u32 alt_index = offset / 8; |
| 79 | |
| 80 | af = (readl(®s->afr[alt_index]) & |
| 81 | GENMASK(alt_shift + 3, alt_shift)) >> alt_shift; |
| 82 | |
| 83 | return af; |
| 84 | } |
| 85 | |
Patrice Chotard | 0435504 | 2018-12-03 10:52:50 +0100 | [diff] [blame] | 86 | static int stm32_populate_gpio_dev_list(struct udevice *dev) |
| 87 | { |
| 88 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 89 | struct udevice *gpio_dev; |
| 90 | struct udevice *child; |
| 91 | struct stm32_gpio_bank *gpio_bank; |
| 92 | int ret; |
| 93 | |
| 94 | /* |
| 95 | * parse pin-controller sub-nodes (ie gpio bank nodes) and fill |
| 96 | * a list with all gpio device reference which belongs to the |
| 97 | * current pin-controller. This list is used to find pin_name and |
| 98 | * pin muxing |
| 99 | */ |
| 100 | list_for_each_entry(child, &dev->child_head, sibling_node) { |
| 101 | ret = uclass_get_device_by_name(UCLASS_GPIO, child->name, |
| 102 | &gpio_dev); |
| 103 | if (ret < 0) |
| 104 | continue; |
| 105 | |
| 106 | gpio_bank = malloc(sizeof(*gpio_bank)); |
| 107 | if (!gpio_bank) { |
| 108 | dev_err(dev, "Not enough memory\n"); |
| 109 | return -ENOMEM; |
| 110 | } |
| 111 | |
| 112 | gpio_bank->gpio_dev = gpio_dev; |
| 113 | list_add_tail(&gpio_bank->list, &priv->gpio_dev); |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 119 | static int stm32_pinctrl_get_pins_count(struct udevice *dev) |
| 120 | { |
| 121 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 122 | struct gpio_dev_priv *uc_priv; |
| 123 | struct stm32_gpio_bank *gpio_bank; |
| 124 | |
| 125 | /* |
| 126 | * if get_pins_count has already been executed once on this |
| 127 | * pin-controller, no need to run it again |
| 128 | */ |
| 129 | if (priv->pinctrl_ngpios) |
| 130 | return priv->pinctrl_ngpios; |
| 131 | |
Patrice Chotard | 0435504 | 2018-12-03 10:52:50 +0100 | [diff] [blame] | 132 | if (list_empty(&priv->gpio_dev)) |
| 133 | stm32_populate_gpio_dev_list(dev); |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 134 | /* |
| 135 | * walk through all banks to retrieve the pin-controller |
| 136 | * pins number |
| 137 | */ |
| 138 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 139 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 140 | |
| 141 | priv->pinctrl_ngpios += uc_priv->gpio_count; |
| 142 | } |
| 143 | |
| 144 | return priv->pinctrl_ngpios; |
| 145 | } |
| 146 | |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 147 | static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev, |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 148 | unsigned int selector, |
| 149 | unsigned int *idx) |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 150 | { |
| 151 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 152 | struct stm32_gpio_bank *gpio_bank; |
| 153 | struct gpio_dev_priv *uc_priv; |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 154 | int pin_count = 0; |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 155 | |
Patrice Chotard | 0435504 | 2018-12-03 10:52:50 +0100 | [diff] [blame] | 156 | if (list_empty(&priv->gpio_dev)) |
| 157 | stm32_populate_gpio_dev_list(dev); |
| 158 | |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 159 | /* look up for the bank which owns the requested pin */ |
| 160 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 161 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 162 | |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 163 | if (selector < (pin_count + uc_priv->gpio_count)) { |
| 164 | /* |
| 165 | * we found the bank, convert pin selector to |
| 166 | * gpio bank index |
| 167 | */ |
Patrice Chotard | 427f452 | 2022-04-22 09:38:31 +0200 | [diff] [blame] | 168 | *idx = selector - pin_count; |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 169 | |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 170 | return gpio_bank->gpio_dev; |
| 171 | } |
| 172 | pin_count += uc_priv->gpio_count; |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | static const char *stm32_pinctrl_get_pin_name(struct udevice *dev, |
| 179 | unsigned int selector) |
| 180 | { |
| 181 | struct gpio_dev_priv *uc_priv; |
| 182 | struct udevice *gpio_dev; |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 183 | unsigned int gpio_idx; |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 184 | |
| 185 | /* look up for the bank which owns the requested pin */ |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 186 | gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 187 | if (!gpio_dev) { |
| 188 | snprintf(pin_name, PINNAME_SIZE, "Error"); |
| 189 | } else { |
| 190 | uc_priv = dev_get_uclass_priv(gpio_dev); |
| 191 | |
| 192 | snprintf(pin_name, PINNAME_SIZE, "%s%d", |
| 193 | uc_priv->bank_name, |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 194 | gpio_idx); |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return pin_name; |
| 198 | } |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 199 | |
| 200 | static int stm32_pinctrl_get_pin_muxing(struct udevice *dev, |
| 201 | unsigned int selector, |
| 202 | char *buf, |
| 203 | int size) |
| 204 | { |
| 205 | struct udevice *gpio_dev; |
Patrick Delaunay | da7a0bb | 2020-06-04 14:30:33 +0200 | [diff] [blame] | 206 | struct stm32_gpio_priv *priv; |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 207 | const char *label; |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 208 | int mode; |
| 209 | int af_num; |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 210 | unsigned int gpio_idx; |
Patrick Delaunay | da7a0bb | 2020-06-04 14:30:33 +0200 | [diff] [blame] | 211 | u32 pupd, otype; |
Patrice Chotard | bff0d84 | 2023-03-27 09:46:41 +0200 | [diff] [blame] | 212 | u8 speed; |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 213 | |
| 214 | /* look up for the bank which owns the requested pin */ |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 215 | gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 216 | |
| 217 | if (!gpio_dev) |
| 218 | return -ENODEV; |
| 219 | |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 220 | mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label); |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 221 | dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n", |
| 222 | selector, gpio_idx, mode); |
Patrick Delaunay | da7a0bb | 2020-06-04 14:30:33 +0200 | [diff] [blame] | 223 | priv = dev_get_priv(gpio_dev); |
Patrick Delaunay | b305dbc | 2020-10-28 10:49:07 +0100 | [diff] [blame] | 224 | pupd = (readl(&priv->regs->pupdr) >> (gpio_idx * 2)) & PUPD_MASK; |
Patrick Delaunay | 1da4269 | 2021-01-21 17:39:07 +0100 | [diff] [blame] | 225 | otype = (readl(&priv->regs->otyper) >> gpio_idx) & OTYPE_MSK; |
Patrice Chotard | bff0d84 | 2023-03-27 09:46:41 +0200 | [diff] [blame] | 226 | speed = (readl(&priv->regs->ospeedr) >> gpio_idx * 2) & OSPEED_MASK; |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 227 | |
| 228 | switch (mode) { |
| 229 | case GPIOF_UNKNOWN: |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 230 | case GPIOF_UNUSED: |
| 231 | snprintf(buf, size, "%s", pinmux_mode[mode]); |
| 232 | break; |
| 233 | case GPIOF_FUNC: |
Patrice Chotard | 530b63c | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 234 | af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx); |
Patrice Chotard | bff0d84 | 2023-03-27 09:46:41 +0200 | [diff] [blame] | 235 | snprintf(buf, size, "%s %d %s %s %s", pinmux_mode[mode], af_num, |
| 236 | pinmux_otype[otype], pinmux_bias[pupd], |
| 237 | pinmux_speed[speed]); |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 238 | break; |
| 239 | case GPIOF_OUTPUT: |
Patrice Chotard | bff0d84 | 2023-03-27 09:46:41 +0200 | [diff] [blame] | 240 | snprintf(buf, size, "%s %s %s %s %s", |
Patrick Delaunay | 1da4269 | 2021-01-21 17:39:07 +0100 | [diff] [blame] | 241 | pinmux_mode[mode], pinmux_otype[otype], |
Patrice Chotard | bff0d84 | 2023-03-27 09:46:41 +0200 | [diff] [blame] | 242 | pinmux_bias[pupd], label ? label : "", |
| 243 | pinmux_speed[speed]); |
Patrick Delaunay | da7a0bb | 2020-06-04 14:30:33 +0200 | [diff] [blame] | 244 | break; |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 245 | case GPIOF_INPUT: |
Patrick Delaunay | 1da4269 | 2021-01-21 17:39:07 +0100 | [diff] [blame] | 246 | snprintf(buf, size, "%s %s %s", pinmux_mode[mode], |
Patrick Delaunay | b305dbc | 2020-10-28 10:49:07 +0100 | [diff] [blame] | 247 | pinmux_bias[pupd], label ? label : ""); |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 254 | #endif |
| 255 | |
Patrick Delaunay | 91ca91e | 2019-06-21 15:26:52 +0200 | [diff] [blame] | 256 | static int stm32_pinctrl_probe(struct udevice *dev) |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 257 | { |
| 258 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 259 | int ret; |
| 260 | |
| 261 | INIT_LIST_HEAD(&priv->gpio_dev); |
| 262 | |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 263 | /* hwspinlock property is optional, just log the error */ |
| 264 | ret = hwspinlock_get_by_index(dev, 0, &priv->hws); |
| 265 | if (ret) |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 266 | dev_dbg(dev, "hwspinlock_get_by_index may have failed (%d)\n", |
| 267 | ret); |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 268 | |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 269 | return 0; |
| 270 | } |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 271 | |
Patrice Chotard | 2c38f7c | 2022-08-30 14:09:13 +0200 | [diff] [blame] | 272 | static int stm32_gpio_config(ofnode node, |
| 273 | struct gpio_desc *desc, |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 274 | const struct stm32_gpio_ctl *ctl) |
| 275 | { |
| 276 | struct stm32_gpio_priv *priv = dev_get_priv(desc->dev); |
Patrice Chotard | 2c38f7c | 2022-08-30 14:09:13 +0200 | [diff] [blame] | 277 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc->dev); |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 278 | struct stm32_gpio_regs *regs = priv->regs; |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 279 | struct stm32_pinctrl_priv *ctrl_priv; |
| 280 | int ret; |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 281 | u32 index; |
| 282 | |
| 283 | if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 || |
| 284 | ctl->pupd > 2 || ctl->speed > 3) |
| 285 | return -EINVAL; |
| 286 | |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 287 | ctrl_priv = dev_get_priv(dev_get_parent(desc->dev)); |
| 288 | ret = hwspinlock_lock_timeout(&ctrl_priv->hws, 10); |
| 289 | if (ret == -ETIME) { |
| 290 | dev_err(desc->dev, "HWSpinlock timeout\n"); |
| 291 | return ret; |
| 292 | } |
| 293 | |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 294 | index = (desc->offset & 0x07) * 4; |
| 295 | clrsetbits_le32(®s->afr[desc->offset >> 3], AFR_MASK << index, |
| 296 | ctl->af << index); |
| 297 | |
| 298 | index = desc->offset * 2; |
| 299 | clrsetbits_le32(®s->moder, MODE_BITS_MASK << index, |
| 300 | ctl->mode << index); |
| 301 | clrsetbits_le32(®s->ospeedr, OSPEED_MASK << index, |
| 302 | ctl->speed << index); |
| 303 | clrsetbits_le32(®s->pupdr, PUPD_MASK << index, ctl->pupd << index); |
| 304 | |
| 305 | index = desc->offset; |
| 306 | clrsetbits_le32(®s->otyper, OTYPE_MSK << index, ctl->otype << index); |
| 307 | |
Patrice Chotard | 2c38f7c | 2022-08-30 14:09:13 +0200 | [diff] [blame] | 308 | uc_priv->name[desc->offset] = strdup(ofnode_get_name(node)); |
| 309 | |
Benjamin Gaignard | 075b018 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 310 | hwspinlock_unlock(&ctrl_priv->hws); |
| 311 | |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 312 | return 0; |
| 313 | } |
Patrick Delaunay | 8aeba62 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 314 | |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 315 | static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin) |
| 316 | { |
Patrick Delaunay | 8aeba62 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 317 | gpio_dsc->port = (port_pin & 0x1F000) >> 12; |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 318 | gpio_dsc->pin = (port_pin & 0x0F00) >> 8; |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 319 | log_debug("GPIO:port= %d, pin= %d\n", gpio_dsc->port, gpio_dsc->pin); |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 324 | static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, |
| 325 | ofnode node) |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 326 | { |
| 327 | gpio_fn &= 0x00FF; |
Vikas Manocha | 7741710 | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 328 | gpio_ctl->af = 0; |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 329 | |
| 330 | switch (gpio_fn) { |
| 331 | case 0: |
| 332 | gpio_ctl->mode = STM32_GPIO_MODE_IN; |
| 333 | break; |
| 334 | case 1 ... 16: |
| 335 | gpio_ctl->mode = STM32_GPIO_MODE_AF; |
| 336 | gpio_ctl->af = gpio_fn - 1; |
| 337 | break; |
| 338 | case 17: |
| 339 | gpio_ctl->mode = STM32_GPIO_MODE_AN; |
| 340 | break; |
| 341 | default: |
| 342 | gpio_ctl->mode = STM32_GPIO_MODE_OUT; |
| 343 | break; |
| 344 | } |
| 345 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 346 | gpio_ctl->speed = ofnode_read_u32_default(node, "slew-rate", 0); |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 347 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 348 | if (ofnode_read_bool(node, "drive-open-drain")) |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 349 | gpio_ctl->otype = STM32_GPIO_OTYPE_OD; |
| 350 | else |
| 351 | gpio_ctl->otype = STM32_GPIO_OTYPE_PP; |
| 352 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 353 | if (ofnode_read_bool(node, "bias-pull-up")) |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 354 | gpio_ctl->pupd = STM32_GPIO_PUPD_UP; |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 355 | else if (ofnode_read_bool(node, "bias-pull-down")) |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 356 | gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN; |
| 357 | else |
| 358 | gpio_ctl->pupd = STM32_GPIO_PUPD_NO; |
| 359 | |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 360 | log_debug("gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n", |
| 361 | gpio_fn, gpio_ctl->speed, gpio_ctl->otype, |
| 362 | gpio_ctl->pupd); |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 367 | static int stm32_pinctrl_config(ofnode node) |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 368 | { |
Vikas Manocha | 58fb3c8 | 2017-04-10 15:03:04 -0700 | [diff] [blame] | 369 | u32 pin_mux[MAX_PINS_ONE_IP]; |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 370 | int rv, len; |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 371 | ofnode subnode; |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 372 | |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 373 | /* |
| 374 | * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for |
| 375 | * usart1) of pin controller phandle "pinctrl-0" |
| 376 | * */ |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 377 | ofnode_for_each_subnode(subnode, node) { |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 378 | struct stm32_gpio_dsc gpio_dsc; |
| 379 | struct stm32_gpio_ctl gpio_ctl; |
| 380 | int i; |
| 381 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 382 | rv = ofnode_read_size(subnode, "pinmux"); |
| 383 | if (rv < 0) |
| 384 | return rv; |
| 385 | len = rv / sizeof(pin_mux[0]); |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 386 | log_debug("No of pinmux entries= %d\n", len); |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 387 | if (len > MAX_PINS_ONE_IP) |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 388 | return -EINVAL; |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 389 | rv = ofnode_read_u32_array(subnode, "pinmux", pin_mux, len); |
| 390 | if (rv < 0) |
| 391 | return rv; |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 392 | for (i = 0; i < len; i++) { |
Vikas Manocha | 280057b | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 393 | struct gpio_desc desc; |
Patrick Delaunay | 8aeba62 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 394 | |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 395 | log_debug("pinmux = %x\n", *(pin_mux + i)); |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 396 | prep_gpio_dsc(&gpio_dsc, *(pin_mux + i)); |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 397 | prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), subnode); |
Vikas Manocha | 280057b | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 398 | rv = uclass_get_device_by_seq(UCLASS_GPIO, |
Patrick Delaunay | 8aeba62 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 399 | gpio_dsc.port, |
| 400 | &desc.dev); |
Vikas Manocha | 280057b | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 401 | if (rv) |
| 402 | return rv; |
| 403 | desc.offset = gpio_dsc.pin; |
Patrice Chotard | 2c38f7c | 2022-08-30 14:09:13 +0200 | [diff] [blame] | 404 | rv = stm32_gpio_config(node, &desc, &gpio_ctl); |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 405 | log_debug("rv = %d\n\n", rv); |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 406 | if (rv) |
| 407 | return rv; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Patrice Chotard | 158abbf | 2019-06-21 15:39:23 +0200 | [diff] [blame] | 414 | static int stm32_pinctrl_bind(struct udevice *dev) |
| 415 | { |
| 416 | ofnode node; |
| 417 | const char *name; |
| 418 | int ret; |
| 419 | |
| 420 | dev_for_each_subnode(node, dev) { |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 421 | dev_dbg(dev, "bind %s\n", ofnode_get_name(node)); |
Patrice Chotard | 158abbf | 2019-06-21 15:39:23 +0200 | [diff] [blame] | 422 | |
Patrick Delaunay | 4363aac | 2021-01-21 17:39:08 +0100 | [diff] [blame] | 423 | if (!ofnode_is_enabled(node)) |
| 424 | continue; |
| 425 | |
Patrice Chotard | 158abbf | 2019-06-21 15:39:23 +0200 | [diff] [blame] | 426 | ofnode_get_property(node, "gpio-controller", &ret); |
| 427 | if (ret < 0) |
| 428 | continue; |
| 429 | /* Get the name of each gpio node */ |
| 430 | name = ofnode_get_name(node); |
| 431 | if (!name) |
| 432 | return -EINVAL; |
| 433 | |
| 434 | /* Bind each gpio node */ |
| 435 | ret = device_bind_driver_to_node(dev, "gpio_stm32", |
| 436 | name, node, NULL); |
| 437 | if (ret) |
| 438 | return ret; |
| 439 | |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 440 | dev_dbg(dev, "bind %s\n", name); |
Patrice Chotard | 158abbf | 2019-06-21 15:39:23 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Christophe Kerello | bb44b96 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 446 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 447 | static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 448 | { |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 449 | return stm32_pinctrl_config(dev_ofnode(config)); |
Christophe Kerello | bb44b96 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 450 | } |
| 451 | #else /* PINCTRL_FULL */ |
Christophe Kerello | ad0376e | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 452 | static int stm32_pinctrl_set_state_simple(struct udevice *dev, |
| 453 | struct udevice *periph) |
| 454 | { |
Christophe Kerello | ad0376e | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 455 | const fdt32_t *list; |
| 456 | uint32_t phandle; |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 457 | ofnode config_node; |
Christophe Kerello | ad0376e | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 458 | int size, i, ret; |
| 459 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 460 | list = ofnode_get_property(dev_ofnode(periph), "pinctrl-0", &size); |
Christophe Kerello | ad0376e | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 461 | if (!list) |
| 462 | return -EINVAL; |
| 463 | |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 464 | dev_dbg(dev, "periph->name = %s\n", periph->name); |
Christophe Kerello | ad0376e | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 465 | |
| 466 | size /= sizeof(*list); |
| 467 | for (i = 0; i < size; i++) { |
| 468 | phandle = fdt32_to_cpu(*list++); |
| 469 | |
Patrick Delaunay | d3bfad2 | 2020-09-09 17:50:14 +0200 | [diff] [blame] | 470 | config_node = ofnode_get_by_phandle(phandle); |
| 471 | if (!ofnode_valid(config_node)) { |
Patrick Delaunay | 28b3e7b | 2020-11-06 19:01:32 +0100 | [diff] [blame] | 472 | dev_err(periph, |
| 473 | "prop pinctrl-0 index %d invalid phandle\n", i); |
Christophe Kerello | ad0376e | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 474 | return -EINVAL; |
| 475 | } |
| 476 | |
| 477 | ret = stm32_pinctrl_config(config_node); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | return 0; |
| 483 | } |
Christophe Kerello | bb44b96 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 484 | #endif /* PINCTRL_FULL */ |
Christophe Kerello | ad0376e | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 485 | |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 486 | static struct pinctrl_ops stm32_pinctrl_ops = { |
Christophe Kerello | bb44b96 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 487 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 488 | .set_state = stm32_pinctrl_set_state, |
| 489 | #else /* PINCTRL_FULL */ |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 490 | .set_state_simple = stm32_pinctrl_set_state_simple, |
Christophe Kerello | bb44b96 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 491 | #endif /* PINCTRL_FULL */ |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 492 | #ifndef CONFIG_SPL_BUILD |
Patrice Chotard | 4ff1c20 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 493 | .get_pin_name = stm32_pinctrl_get_pin_name, |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 494 | .get_pins_count = stm32_pinctrl_get_pins_count, |
Patrice Chotard | b42d938 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 495 | .get_pin_muxing = stm32_pinctrl_get_pin_muxing, |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 496 | #endif |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | static const struct udevice_id stm32_pinctrl_ids[] = { |
Patrice Chotard | 98693c2 | 2017-12-12 09:49:35 +0100 | [diff] [blame] | 500 | { .compatible = "st,stm32f429-pinctrl" }, |
| 501 | { .compatible = "st,stm32f469-pinctrl" }, |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 502 | { .compatible = "st,stm32f746-pinctrl" }, |
Patrice Chotard | dd18df4 | 2018-12-11 14:49:18 +0100 | [diff] [blame] | 503 | { .compatible = "st,stm32f769-pinctrl" }, |
Patrice Chotard | 092e72c | 2017-09-13 18:00:04 +0200 | [diff] [blame] | 504 | { .compatible = "st,stm32h743-pinctrl" }, |
Patrick Delaunay | 8aeba62 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 505 | { .compatible = "st,stm32mp157-pinctrl" }, |
| 506 | { .compatible = "st,stm32mp157-z-pinctrl" }, |
Patrick Delaunay | cf1d0fd | 2022-05-20 18:24:48 +0200 | [diff] [blame] | 507 | { .compatible = "st,stm32mp135-pinctrl" }, |
Patrice Chotard | 778f4ea | 2023-10-27 16:43:02 +0200 | [diff] [blame^] | 508 | { .compatible = "st,stm32mp257-pinctrl" }, |
| 509 | { .compatible = "st,stm32mp257-z-pinctrl" }, |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 510 | { } |
| 511 | }; |
| 512 | |
| 513 | U_BOOT_DRIVER(pinctrl_stm32) = { |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 514 | .name = "pinctrl_stm32", |
| 515 | .id = UCLASS_PINCTRL, |
| 516 | .of_match = stm32_pinctrl_ids, |
| 517 | .ops = &stm32_pinctrl_ops, |
Patrice Chotard | 158abbf | 2019-06-21 15:39:23 +0200 | [diff] [blame] | 518 | .bind = stm32_pinctrl_bind, |
Patrice Chotard | 8f651ca | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 519 | .probe = stm32_pinctrl_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 520 | .priv_auto = sizeof(struct stm32_pinctrl_priv), |
Vikas Manocha | 94d5308 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 521 | }; |