1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017-2020 STMicroelectronics - All Rights Reserved
6 #define LOG_CATEGORY UCLASS_PINCTRL
10 #include <hwspinlock.h>
15 #include <dm/device_compat.h>
17 #include <dm/pinctrl.h>
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 #include <linux/libfdt.h>
22 #include "../gpio/stm32_gpio_priv.h"
24 #define MAX_PINS_ONE_IP 70
25 #define MODE_BITS_MASK 3
31 struct stm32_pinctrl_priv {
32 struct hwspinlock hws;
34 struct list_head gpio_dev;
37 struct stm32_gpio_bank {
38 struct udevice *gpio_dev;
39 struct list_head list;
42 #ifndef CONFIG_SPL_BUILD
44 static char pin_name[PINNAME_SIZE];
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",
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",
59 static const char * const pinmux_otype[] = {
60 [STM32_GPIO_OTYPE_PP] = "push-pull",
61 [STM32_GPIO_OTYPE_OD] = "open-drain",
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",
71 static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
73 struct stm32_gpio_priv *priv = dev_get_priv(dev);
74 struct stm32_gpio_regs *regs = priv->regs;
76 u32 alt_shift = (offset % 8) * 4;
77 u32 alt_index = offset / 8;
79 af = (readl(®s->afr[alt_index]) &
80 GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
85 static int stm32_populate_gpio_dev_list(struct udevice *dev)
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;
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
99 list_for_each_entry(child, &dev->child_head, sibling_node) {
100 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
105 gpio_bank = malloc(sizeof(*gpio_bank));
107 dev_err(dev, "Not enough memory\n");
111 gpio_bank->gpio_dev = gpio_dev;
112 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
118 static int stm32_pinctrl_get_pins_count(struct udevice *dev)
120 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
121 struct gpio_dev_priv *uc_priv;
122 struct stm32_gpio_bank *gpio_bank;
125 * if get_pins_count has already been executed once on this
126 * pin-controller, no need to run it again
128 if (priv->pinctrl_ngpios)
129 return priv->pinctrl_ngpios;
131 if (list_empty(&priv->gpio_dev))
132 stm32_populate_gpio_dev_list(dev);
134 * walk through all banks to retrieve the pin-controller
137 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
138 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
140 priv->pinctrl_ngpios += uc_priv->gpio_count;
143 return priv->pinctrl_ngpios;
146 static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
147 unsigned int selector,
150 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
151 struct stm32_gpio_bank *gpio_bank;
152 struct gpio_dev_priv *uc_priv;
155 if (list_empty(&priv->gpio_dev))
156 stm32_populate_gpio_dev_list(dev);
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);
162 if (selector < (pin_count + uc_priv->gpio_count)) {
164 * we found the bank, convert pin selector to
167 *idx = selector - pin_count;
169 return gpio_bank->gpio_dev;
171 pin_count += uc_priv->gpio_count;
177 static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
178 unsigned int selector)
180 struct gpio_dev_priv *uc_priv;
181 struct udevice *gpio_dev;
182 unsigned int gpio_idx;
184 /* look up for the bank which owns the requested pin */
185 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
187 snprintf(pin_name, PINNAME_SIZE, "Error");
189 uc_priv = dev_get_uclass_priv(gpio_dev);
191 snprintf(pin_name, PINNAME_SIZE, "%s%d",
199 static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
200 unsigned int selector,
204 struct udevice *gpio_dev;
205 struct stm32_gpio_priv *priv;
209 unsigned int gpio_idx;
213 /* look up for the bank which owns the requested pin */
214 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
219 mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label);
220 dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n",
221 selector, gpio_idx, mode);
222 priv = dev_get_priv(gpio_dev);
223 pupd = (readl(&priv->regs->pupdr) >> (gpio_idx * 2)) & PUPD_MASK;
224 otype = (readl(&priv->regs->otyper) >> gpio_idx) & OTYPE_MSK;
225 speed = (readl(&priv->regs->ospeedr) >> gpio_idx * 2) & OSPEED_MASK;
230 snprintf(buf, size, "%s", pinmux_mode[mode]);
233 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx);
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]);
239 snprintf(buf, size, "%s %s %s %s %s",
240 pinmux_mode[mode], pinmux_otype[otype],
241 pinmux_bias[pupd], label ? label : "",
242 pinmux_speed[speed]);
245 snprintf(buf, size, "%s %s %s", pinmux_mode[mode],
246 pinmux_bias[pupd], label ? label : "");
255 static int stm32_pinctrl_probe(struct udevice *dev)
257 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
260 INIT_LIST_HEAD(&priv->gpio_dev);
262 /* hwspinlock property is optional, just log the error */
263 ret = hwspinlock_get_by_index(dev, 0, &priv->hws);
265 dev_dbg(dev, "hwspinlock_get_by_index may have failed (%d)\n",
271 static int stm32_gpio_config(ofnode node,
272 struct gpio_desc *desc,
273 const struct stm32_gpio_ctl *ctl)
275 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
276 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc->dev);
277 struct stm32_gpio_regs *regs = priv->regs;
278 struct stm32_pinctrl_priv *ctrl_priv;
282 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
283 ctl->pupd > 2 || ctl->speed > 3)
286 ctrl_priv = dev_get_priv(dev_get_parent(desc->dev));
287 ret = hwspinlock_lock_timeout(&ctrl_priv->hws, 10);
289 dev_err(desc->dev, "HWSpinlock timeout\n");
293 index = (desc->offset & 0x07) * 4;
294 clrsetbits_le32(®s->afr[desc->offset >> 3], AFR_MASK << index,
297 index = desc->offset * 2;
298 clrsetbits_le32(®s->moder, MODE_BITS_MASK << index,
300 clrsetbits_le32(®s->ospeedr, OSPEED_MASK << index,
301 ctl->speed << index);
302 clrsetbits_le32(®s->pupdr, PUPD_MASK << index, ctl->pupd << index);
304 index = desc->offset;
305 clrsetbits_le32(®s->otyper, OTYPE_MSK << index, ctl->otype << index);
307 uc_priv->name[desc->offset] = strdup(ofnode_get_name(node));
309 hwspinlock_unlock(&ctrl_priv->hws);
314 static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
316 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
317 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
318 log_debug("GPIO:port= %d, pin= %d\n", gpio_dsc->port, gpio_dsc->pin);
323 static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn,
331 gpio_ctl->mode = STM32_GPIO_MODE_IN;
334 gpio_ctl->mode = STM32_GPIO_MODE_AF;
335 gpio_ctl->af = gpio_fn - 1;
338 gpio_ctl->mode = STM32_GPIO_MODE_AN;
341 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
345 gpio_ctl->speed = ofnode_read_u32_default(node, "slew-rate", 0);
347 if (ofnode_read_bool(node, "drive-open-drain"))
348 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
350 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
352 if (ofnode_read_bool(node, "bias-pull-up"))
353 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
354 else if (ofnode_read_bool(node, "bias-pull-down"))
355 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
357 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
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,
366 static int stm32_pinctrl_config(ofnode node)
368 u32 pin_mux[MAX_PINS_ONE_IP];
373 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
374 * usart1) of pin controller phandle "pinctrl-0"
376 ofnode_for_each_subnode(subnode, node) {
377 struct stm32_gpio_dsc gpio_dsc;
378 struct stm32_gpio_ctl gpio_ctl;
381 rv = ofnode_read_size(subnode, "pinmux");
384 len = rv / sizeof(pin_mux[0]);
385 log_debug("No of pinmux entries= %d\n", len);
386 if (len > MAX_PINS_ONE_IP)
388 rv = ofnode_read_u32_array(subnode, "pinmux", pin_mux, len);
391 for (i = 0; i < len; i++) {
392 struct gpio_desc desc;
394 log_debug("pinmux = %x\n", *(pin_mux + i));
395 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
396 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), subnode);
397 rv = uclass_get_device_by_seq(UCLASS_GPIO,
402 desc.offset = gpio_dsc.pin;
403 rv = stm32_gpio_config(node, &desc, &gpio_ctl);
404 log_debug("rv = %d\n\n", rv);
413 static int stm32_pinctrl_bind(struct udevice *dev)
419 dev_for_each_subnode(node, dev) {
420 dev_dbg(dev, "bind %s\n", ofnode_get_name(node));
422 if (!ofnode_is_enabled(node))
425 ofnode_get_property(node, "gpio-controller", &ret);
428 /* Get the name of each gpio node */
429 name = ofnode_get_name(node);
433 /* Bind each gpio node */
434 ret = device_bind_driver_to_node(dev, "gpio_stm32",
439 dev_dbg(dev, "bind %s\n", name);
445 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
446 static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
448 return stm32_pinctrl_config(dev_ofnode(config));
450 #else /* PINCTRL_FULL */
451 static int stm32_pinctrl_set_state_simple(struct udevice *dev,
452 struct udevice *periph)
459 list = ofnode_get_property(dev_ofnode(periph), "pinctrl-0", &size);
463 dev_dbg(dev, "periph->name = %s\n", periph->name);
465 size /= sizeof(*list);
466 for (i = 0; i < size; i++) {
467 phandle = fdt32_to_cpu(*list++);
469 config_node = ofnode_get_by_phandle(phandle);
470 if (!ofnode_valid(config_node)) {
472 "prop pinctrl-0 index %d invalid phandle\n", i);
476 ret = stm32_pinctrl_config(config_node);
483 #endif /* PINCTRL_FULL */
485 static struct pinctrl_ops stm32_pinctrl_ops = {
486 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
487 .set_state = stm32_pinctrl_set_state,
488 #else /* PINCTRL_FULL */
489 .set_state_simple = stm32_pinctrl_set_state_simple,
490 #endif /* PINCTRL_FULL */
491 #ifndef CONFIG_SPL_BUILD
492 .get_pin_name = stm32_pinctrl_get_pin_name,
493 .get_pins_count = stm32_pinctrl_get_pins_count,
494 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
498 static const struct udevice_id stm32_pinctrl_ids[] = {
499 { .compatible = "st,stm32f429-pinctrl" },
500 { .compatible = "st,stm32f469-pinctrl" },
501 { .compatible = "st,stm32f746-pinctrl" },
502 { .compatible = "st,stm32f769-pinctrl" },
503 { .compatible = "st,stm32h743-pinctrl" },
504 { .compatible = "st,stm32mp157-pinctrl" },
505 { .compatible = "st,stm32mp157-z-pinctrl" },
506 { .compatible = "st,stm32mp135-pinctrl" },
510 U_BOOT_DRIVER(pinctrl_stm32) = {
511 .name = "pinctrl_stm32",
512 .id = UCLASS_PINCTRL,
513 .of_match = stm32_pinctrl_ids,
514 .ops = &stm32_pinctrl_ops,
515 .bind = stm32_pinctrl_bind,
516 .probe = stm32_pinctrl_probe,
517 .priv_auto = sizeof(struct stm32_pinctrl_priv),