3 #include <dm/pinctrl.h>
4 #include <hwspinlock.h>
5 #include <asm/arch/gpio.h>
9 DECLARE_GLOBAL_DATA_PTR;
11 #define MAX_PINS_ONE_IP 70
12 #define MODE_BITS_MASK 3
18 struct stm32_pinctrl_priv {
19 struct hwspinlock hws;
21 struct list_head gpio_dev;
24 struct stm32_gpio_bank {
25 struct udevice *gpio_dev;
26 struct list_head list;
29 #ifndef CONFIG_SPL_BUILD
31 static char pin_name[PINNAME_SIZE];
32 #define PINMUX_MODE_COUNT 5
33 static const char * const pinmux_mode[PINMUX_MODE_COUNT] = {
41 static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
43 struct stm32_gpio_priv *priv = dev_get_priv(dev);
44 struct stm32_gpio_regs *regs = priv->regs;
46 u32 alt_shift = (offset % 8) * 4;
47 u32 alt_index = offset / 8;
49 af = (readl(®s->afr[alt_index]) &
50 GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
55 static int stm32_populate_gpio_dev_list(struct udevice *dev)
57 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
58 struct udevice *gpio_dev;
59 struct udevice *child;
60 struct stm32_gpio_bank *gpio_bank;
64 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
65 * a list with all gpio device reference which belongs to the
66 * current pin-controller. This list is used to find pin_name and
69 list_for_each_entry(child, &dev->child_head, sibling_node) {
70 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
75 gpio_bank = malloc(sizeof(*gpio_bank));
77 dev_err(dev, "Not enough memory\n");
81 gpio_bank->gpio_dev = gpio_dev;
82 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
88 static int stm32_pinctrl_get_pins_count(struct udevice *dev)
90 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
91 struct gpio_dev_priv *uc_priv;
92 struct stm32_gpio_bank *gpio_bank;
95 * if get_pins_count has already been executed once on this
96 * pin-controller, no need to run it again
98 if (priv->pinctrl_ngpios)
99 return priv->pinctrl_ngpios;
101 if (list_empty(&priv->gpio_dev))
102 stm32_populate_gpio_dev_list(dev);
104 * walk through all banks to retrieve the pin-controller
107 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
108 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
110 priv->pinctrl_ngpios += uc_priv->gpio_count;
113 return priv->pinctrl_ngpios;
116 static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
117 unsigned int selector,
120 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
121 struct stm32_gpio_bank *gpio_bank;
122 struct gpio_dev_priv *uc_priv;
125 if (list_empty(&priv->gpio_dev))
126 stm32_populate_gpio_dev_list(dev);
128 /* look up for the bank which owns the requested pin */
129 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
130 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
132 if (selector < (pin_count + uc_priv->gpio_count)) {
134 * we found the bank, convert pin selector to
137 *idx = stm32_offset_to_index(gpio_bank->gpio_dev,
138 selector - pin_count);
142 return gpio_bank->gpio_dev;
144 pin_count += uc_priv->gpio_count;
150 static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
151 unsigned int selector)
153 struct gpio_dev_priv *uc_priv;
154 struct udevice *gpio_dev;
155 unsigned int gpio_idx;
157 /* look up for the bank which owns the requested pin */
158 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
160 snprintf(pin_name, PINNAME_SIZE, "Error");
162 uc_priv = dev_get_uclass_priv(gpio_dev);
164 snprintf(pin_name, PINNAME_SIZE, "%s%d",
172 static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
173 unsigned int selector,
177 struct udevice *gpio_dev;
181 unsigned int gpio_idx;
183 /* look up for the bank which owns the requested pin */
184 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
189 mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label);
191 dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n",
192 selector, gpio_idx, mode);
197 /* should never happen */
200 snprintf(buf, size, "%s", pinmux_mode[mode]);
203 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx);
204 snprintf(buf, size, "%s %d", pinmux_mode[mode], af_num);
208 snprintf(buf, size, "%s %s",
209 pinmux_mode[mode], label ? label : "");
218 int stm32_pinctrl_probe(struct udevice *dev)
220 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
223 INIT_LIST_HEAD(&priv->gpio_dev);
225 /* hwspinlock property is optional, just log the error */
226 ret = hwspinlock_get_by_index(dev, 0, &priv->hws);
228 debug("%s: hwspinlock_get_by_index may have failed (%d)\n",
234 static int stm32_gpio_config(struct gpio_desc *desc,
235 const struct stm32_gpio_ctl *ctl)
237 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
238 struct stm32_gpio_regs *regs = priv->regs;
239 struct stm32_pinctrl_priv *ctrl_priv;
243 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
244 ctl->pupd > 2 || ctl->speed > 3)
247 ctrl_priv = dev_get_priv(dev_get_parent(desc->dev));
248 ret = hwspinlock_lock_timeout(&ctrl_priv->hws, 10);
250 dev_err(desc->dev, "HWSpinlock timeout\n");
254 index = (desc->offset & 0x07) * 4;
255 clrsetbits_le32(®s->afr[desc->offset >> 3], AFR_MASK << index,
258 index = desc->offset * 2;
259 clrsetbits_le32(®s->moder, MODE_BITS_MASK << index,
261 clrsetbits_le32(®s->ospeedr, OSPEED_MASK << index,
262 ctl->speed << index);
263 clrsetbits_le32(®s->pupdr, PUPD_MASK << index, ctl->pupd << index);
265 index = desc->offset;
266 clrsetbits_le32(®s->otyper, OTYPE_MSK << index, ctl->otype << index);
268 hwspinlock_unlock(&ctrl_priv->hws);
273 static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
275 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
276 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
277 debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
283 static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node)
290 gpio_ctl->mode = STM32_GPIO_MODE_IN;
293 gpio_ctl->mode = STM32_GPIO_MODE_AF;
294 gpio_ctl->af = gpio_fn - 1;
297 gpio_ctl->mode = STM32_GPIO_MODE_AN;
300 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
304 gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0);
306 if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain"))
307 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
309 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
311 if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up"))
312 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
313 else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down"))
314 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
316 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
318 debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
319 __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
325 static int stm32_pinctrl_config(int offset)
327 u32 pin_mux[MAX_PINS_ONE_IP];
331 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
332 * usart1) of pin controller phandle "pinctrl-0"
334 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
335 struct stm32_gpio_dsc gpio_dsc;
336 struct stm32_gpio_ctl gpio_ctl;
339 len = fdtdec_get_int_array_count(gd->fdt_blob, offset,
341 ARRAY_SIZE(pin_mux));
342 debug("%s: no of pinmux entries= %d\n", __func__, len);
345 for (i = 0; i < len; i++) {
346 struct gpio_desc desc;
348 debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
349 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
350 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset);
351 rv = uclass_get_device_by_seq(UCLASS_GPIO,
356 desc.offset = gpio_dsc.pin;
357 rv = stm32_gpio_config(&desc, &gpio_ctl);
358 debug("%s: rv = %d\n\n", __func__, rv);
367 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
368 static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
370 return stm32_pinctrl_config(dev_of_offset(config));
372 #else /* PINCTRL_FULL */
373 static int stm32_pinctrl_set_state_simple(struct udevice *dev,
374 struct udevice *periph)
376 const void *fdt = gd->fdt_blob;
382 list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size);
386 debug("%s: periph->name = %s\n", __func__, periph->name);
388 size /= sizeof(*list);
389 for (i = 0; i < size; i++) {
390 phandle = fdt32_to_cpu(*list++);
392 config_node = fdt_node_offset_by_phandle(fdt, phandle);
393 if (config_node < 0) {
394 pr_err("prop pinctrl-0 index %d invalid phandle\n", i);
398 ret = stm32_pinctrl_config(config_node);
405 #endif /* PINCTRL_FULL */
407 static struct pinctrl_ops stm32_pinctrl_ops = {
408 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
409 .set_state = stm32_pinctrl_set_state,
410 #else /* PINCTRL_FULL */
411 .set_state_simple = stm32_pinctrl_set_state_simple,
412 #endif /* PINCTRL_FULL */
413 #ifndef CONFIG_SPL_BUILD
414 .get_pin_name = stm32_pinctrl_get_pin_name,
415 .get_pins_count = stm32_pinctrl_get_pins_count,
416 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
420 static const struct udevice_id stm32_pinctrl_ids[] = {
421 { .compatible = "st,stm32f429-pinctrl" },
422 { .compatible = "st,stm32f469-pinctrl" },
423 { .compatible = "st,stm32f746-pinctrl" },
424 { .compatible = "st,stm32h743-pinctrl" },
425 { .compatible = "st,stm32mp157-pinctrl" },
426 { .compatible = "st,stm32mp157-z-pinctrl" },
430 U_BOOT_DRIVER(pinctrl_stm32) = {
431 .name = "pinctrl_stm32",
432 .id = UCLASS_PINCTRL,
433 .of_match = stm32_pinctrl_ids,
434 .ops = &stm32_pinctrl_ops,
435 .bind = dm_scan_fdt_dev,
436 .probe = stm32_pinctrl_probe,
437 .priv_auto_alloc_size = sizeof(struct stm32_pinctrl_priv),