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