1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017-2020 STMicroelectronics - All Rights Reserved
8 #include <hwspinlock.h>
11 #include <asm/arch/gpio.h>
14 #include <dm/device_compat.h>
16 #include <dm/pinctrl.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/libfdt.h>
21 #define MAX_PINS_ONE_IP 70
22 #define MODE_BITS_MASK 3
28 struct stm32_pinctrl_priv {
29 struct hwspinlock hws;
31 struct list_head gpio_dev;
34 struct stm32_gpio_bank {
35 struct udevice *gpio_dev;
36 struct list_head list;
39 #ifndef CONFIG_SPL_BUILD
41 static char pin_name[PINNAME_SIZE];
42 #define PINMUX_MODE_COUNT 5
43 static const char * const pinmux_mode[PINMUX_MODE_COUNT] = {
51 static const char * const pinmux_output[] = {
52 [STM32_GPIO_PUPD_NO] = "bias-disable",
53 [STM32_GPIO_PUPD_UP] = "bias-pull-up",
54 [STM32_GPIO_PUPD_DOWN] = "bias-pull-down",
57 static const char * const pinmux_input[] = {
58 [STM32_GPIO_OTYPE_PP] = "drive-push-pull",
59 [STM32_GPIO_OTYPE_OD] = "drive-open-drain",
62 static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
64 struct stm32_gpio_priv *priv = dev_get_priv(dev);
65 struct stm32_gpio_regs *regs = priv->regs;
67 u32 alt_shift = (offset % 8) * 4;
68 u32 alt_index = offset / 8;
70 af = (readl(®s->afr[alt_index]) &
71 GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
76 static int stm32_populate_gpio_dev_list(struct udevice *dev)
78 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
79 struct udevice *gpio_dev;
80 struct udevice *child;
81 struct stm32_gpio_bank *gpio_bank;
85 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
86 * a list with all gpio device reference which belongs to the
87 * current pin-controller. This list is used to find pin_name and
90 list_for_each_entry(child, &dev->child_head, sibling_node) {
91 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
96 gpio_bank = malloc(sizeof(*gpio_bank));
98 dev_err(dev, "Not enough memory\n");
102 gpio_bank->gpio_dev = gpio_dev;
103 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
109 static int stm32_pinctrl_get_pins_count(struct udevice *dev)
111 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
112 struct gpio_dev_priv *uc_priv;
113 struct stm32_gpio_bank *gpio_bank;
116 * if get_pins_count has already been executed once on this
117 * pin-controller, no need to run it again
119 if (priv->pinctrl_ngpios)
120 return priv->pinctrl_ngpios;
122 if (list_empty(&priv->gpio_dev))
123 stm32_populate_gpio_dev_list(dev);
125 * walk through all banks to retrieve the pin-controller
128 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
129 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
131 priv->pinctrl_ngpios += uc_priv->gpio_count;
134 return priv->pinctrl_ngpios;
137 static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
138 unsigned int selector,
141 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
142 struct stm32_gpio_bank *gpio_bank;
143 struct gpio_dev_priv *uc_priv;
146 if (list_empty(&priv->gpio_dev))
147 stm32_populate_gpio_dev_list(dev);
149 /* look up for the bank which owns the requested pin */
150 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
151 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
153 if (selector < (pin_count + uc_priv->gpio_count)) {
155 * we found the bank, convert pin selector to
158 *idx = stm32_offset_to_index(gpio_bank->gpio_dev,
159 selector - pin_count);
160 if (IS_ERR_VALUE(*idx))
163 return gpio_bank->gpio_dev;
165 pin_count += uc_priv->gpio_count;
171 static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
172 unsigned int selector)
174 struct gpio_dev_priv *uc_priv;
175 struct udevice *gpio_dev;
176 unsigned int gpio_idx;
178 /* look up for the bank which owns the requested pin */
179 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
181 snprintf(pin_name, PINNAME_SIZE, "Error");
183 uc_priv = dev_get_uclass_priv(gpio_dev);
185 snprintf(pin_name, PINNAME_SIZE, "%s%d",
193 static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
194 unsigned int selector,
198 struct udevice *gpio_dev;
199 struct stm32_gpio_priv *priv;
203 unsigned int gpio_idx;
206 /* look up for the bank which owns the requested pin */
207 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
212 mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label);
213 dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n",
214 selector, gpio_idx, mode);
215 priv = dev_get_priv(gpio_dev);
220 /* should never happen */
223 snprintf(buf, size, "%s", pinmux_mode[mode]);
226 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx);
227 snprintf(buf, size, "%s %d", pinmux_mode[mode], af_num);
230 pupd = (readl(&priv->regs->pupdr) >> (gpio_idx * 2)) &
232 snprintf(buf, size, "%s %s %s",
233 pinmux_mode[mode], pinmux_output[pupd],
237 otype = (readl(&priv->regs->otyper) >> gpio_idx) & OTYPE_MSK;
238 snprintf(buf, size, "%s %s %s",
239 pinmux_mode[mode], pinmux_input[otype],
249 static int stm32_pinctrl_probe(struct udevice *dev)
251 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
254 INIT_LIST_HEAD(&priv->gpio_dev);
256 /* hwspinlock property is optional, just log the error */
257 ret = hwspinlock_get_by_index(dev, 0, &priv->hws);
259 debug("%s: hwspinlock_get_by_index may have failed (%d)\n",
265 static int stm32_gpio_config(struct gpio_desc *desc,
266 const struct stm32_gpio_ctl *ctl)
268 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
269 struct stm32_gpio_regs *regs = priv->regs;
270 struct stm32_pinctrl_priv *ctrl_priv;
274 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
275 ctl->pupd > 2 || ctl->speed > 3)
278 ctrl_priv = dev_get_priv(dev_get_parent(desc->dev));
279 ret = hwspinlock_lock_timeout(&ctrl_priv->hws, 10);
281 dev_err(desc->dev, "HWSpinlock timeout\n");
285 index = (desc->offset & 0x07) * 4;
286 clrsetbits_le32(®s->afr[desc->offset >> 3], AFR_MASK << index,
289 index = desc->offset * 2;
290 clrsetbits_le32(®s->moder, MODE_BITS_MASK << index,
292 clrsetbits_le32(®s->ospeedr, OSPEED_MASK << index,
293 ctl->speed << index);
294 clrsetbits_le32(®s->pupdr, PUPD_MASK << index, ctl->pupd << index);
296 index = desc->offset;
297 clrsetbits_le32(®s->otyper, OTYPE_MSK << index, ctl->otype << index);
299 hwspinlock_unlock(&ctrl_priv->hws);
304 static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
306 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
307 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
308 debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
314 static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn,
322 gpio_ctl->mode = STM32_GPIO_MODE_IN;
325 gpio_ctl->mode = STM32_GPIO_MODE_AF;
326 gpio_ctl->af = gpio_fn - 1;
329 gpio_ctl->mode = STM32_GPIO_MODE_AN;
332 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
336 gpio_ctl->speed = ofnode_read_u32_default(node, "slew-rate", 0);
338 if (ofnode_read_bool(node, "drive-open-drain"))
339 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
341 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
343 if (ofnode_read_bool(node, "bias-pull-up"))
344 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
345 else if (ofnode_read_bool(node, "bias-pull-down"))
346 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
348 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
350 debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
351 __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
357 static int stm32_pinctrl_config(ofnode node)
359 u32 pin_mux[MAX_PINS_ONE_IP];
364 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
365 * usart1) of pin controller phandle "pinctrl-0"
367 ofnode_for_each_subnode(subnode, node) {
368 struct stm32_gpio_dsc gpio_dsc;
369 struct stm32_gpio_ctl gpio_ctl;
372 rv = ofnode_read_size(subnode, "pinmux");
375 len = rv / sizeof(pin_mux[0]);
376 debug("%s: no of pinmux entries= %d\n", __func__, len);
377 if (len > MAX_PINS_ONE_IP)
379 rv = ofnode_read_u32_array(subnode, "pinmux", pin_mux, len);
382 for (i = 0; i < len; i++) {
383 struct gpio_desc desc;
385 debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
386 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
387 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), subnode);
388 rv = uclass_get_device_by_seq(UCLASS_GPIO,
393 desc.offset = gpio_dsc.pin;
394 rv = stm32_gpio_config(&desc, &gpio_ctl);
395 debug("%s: rv = %d\n\n", __func__, rv);
404 static int stm32_pinctrl_bind(struct udevice *dev)
410 dev_for_each_subnode(node, dev) {
411 debug("%s: bind %s\n", __func__, ofnode_get_name(node));
413 ofnode_get_property(node, "gpio-controller", &ret);
416 /* Get the name of each gpio node */
417 name = ofnode_get_name(node);
421 /* Bind each gpio node */
422 ret = device_bind_driver_to_node(dev, "gpio_stm32",
427 debug("%s: bind %s\n", __func__, name);
433 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
434 static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
436 return stm32_pinctrl_config(dev_ofnode(config));
438 #else /* PINCTRL_FULL */
439 static int stm32_pinctrl_set_state_simple(struct udevice *dev,
440 struct udevice *periph)
447 list = ofnode_get_property(dev_ofnode(periph), "pinctrl-0", &size);
451 debug("%s: periph->name = %s\n", __func__, periph->name);
453 size /= sizeof(*list);
454 for (i = 0; i < size; i++) {
455 phandle = fdt32_to_cpu(*list++);
457 config_node = ofnode_get_by_phandle(phandle);
458 if (!ofnode_valid(config_node)) {
459 pr_err("prop pinctrl-0 index %d invalid phandle\n", i);
463 ret = stm32_pinctrl_config(config_node);
470 #endif /* PINCTRL_FULL */
472 static struct pinctrl_ops stm32_pinctrl_ops = {
473 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
474 .set_state = stm32_pinctrl_set_state,
475 #else /* PINCTRL_FULL */
476 .set_state_simple = stm32_pinctrl_set_state_simple,
477 #endif /* PINCTRL_FULL */
478 #ifndef CONFIG_SPL_BUILD
479 .get_pin_name = stm32_pinctrl_get_pin_name,
480 .get_pins_count = stm32_pinctrl_get_pins_count,
481 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
485 static const struct udevice_id stm32_pinctrl_ids[] = {
486 { .compatible = "st,stm32f429-pinctrl" },
487 { .compatible = "st,stm32f469-pinctrl" },
488 { .compatible = "st,stm32f746-pinctrl" },
489 { .compatible = "st,stm32f769-pinctrl" },
490 { .compatible = "st,stm32h743-pinctrl" },
491 { .compatible = "st,stm32mp157-pinctrl" },
492 { .compatible = "st,stm32mp157-z-pinctrl" },
496 U_BOOT_DRIVER(pinctrl_stm32) = {
497 .name = "pinctrl_stm32",
498 .id = UCLASS_PINCTRL,
499 .of_match = stm32_pinctrl_ids,
500 .ops = &stm32_pinctrl_ops,
501 .bind = stm32_pinctrl_bind,
502 .probe = stm32_pinctrl_probe,
503 .priv_auto_alloc_size = sizeof(struct stm32_pinctrl_priv),