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