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