Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 1 | #include <common.h> |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 2 | #include <dm.h> |
| 3 | #include <dm/pinctrl.h> |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 4 | #include <asm/arch/gpio.h> |
| 5 | #include <asm/gpio.h> |
| 6 | #include <asm/io.h> |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 7 | |
| 8 | DECLARE_GLOBAL_DATA_PTR; |
| 9 | |
Vikas Manocha | 40ddb3a | 2017-04-10 15:03:04 -0700 | [diff] [blame] | 10 | #define MAX_PINS_ONE_IP 70 |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 11 | #define MODE_BITS_MASK 3 |
| 12 | #define OSPEED_MASK 3 |
| 13 | #define PUPD_MASK 3 |
| 14 | #define OTYPE_MSK 1 |
| 15 | #define AFR_MASK 0xF |
| 16 | |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame^] | 17 | #ifndef CONFIG_SPL_BUILD |
| 18 | struct stm32_pinctrl_priv { |
| 19 | int pinctrl_ngpios; |
| 20 | struct list_head gpio_dev; |
| 21 | }; |
| 22 | |
| 23 | struct stm32_gpio_bank { |
| 24 | struct udevice *gpio_dev; |
| 25 | struct list_head list; |
| 26 | }; |
| 27 | |
| 28 | static int stm32_pinctrl_get_pins_count(struct udevice *dev) |
| 29 | { |
| 30 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 31 | struct gpio_dev_priv *uc_priv; |
| 32 | struct stm32_gpio_bank *gpio_bank; |
| 33 | |
| 34 | /* |
| 35 | * if get_pins_count has already been executed once on this |
| 36 | * pin-controller, no need to run it again |
| 37 | */ |
| 38 | if (priv->pinctrl_ngpios) |
| 39 | return priv->pinctrl_ngpios; |
| 40 | |
| 41 | /* |
| 42 | * walk through all banks to retrieve the pin-controller |
| 43 | * pins number |
| 44 | */ |
| 45 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 46 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 47 | |
| 48 | priv->pinctrl_ngpios += uc_priv->gpio_count; |
| 49 | } |
| 50 | |
| 51 | return priv->pinctrl_ngpios; |
| 52 | } |
| 53 | |
| 54 | int stm32_pinctrl_probe(struct udevice *dev) |
| 55 | { |
| 56 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 57 | struct udevice *gpio_dev; |
| 58 | struct udevice *child; |
| 59 | struct stm32_gpio_bank *gpio_bank; |
| 60 | int ret; |
| 61 | |
| 62 | INIT_LIST_HEAD(&priv->gpio_dev); |
| 63 | |
| 64 | /* |
| 65 | * parse pin-controller sub-nodes (ie gpio bank nodes) and fill |
| 66 | * a list with all gpio device reference which belongs to the |
| 67 | * current pin-controller. This list is used to find pin_name and |
| 68 | * pin muxing |
| 69 | */ |
| 70 | list_for_each_entry(child, &dev->child_head, sibling_node) { |
| 71 | ret = uclass_get_device_by_name(UCLASS_GPIO, child->name, |
| 72 | &gpio_dev); |
| 73 | if (ret < 0) |
| 74 | continue; |
| 75 | |
| 76 | gpio_bank = malloc(sizeof(*gpio_bank)); |
| 77 | if (!gpio_bank) { |
| 78 | dev_err(dev, "Not enough memory\n"); |
| 79 | return -ENOMEM; |
| 80 | } |
| 81 | |
| 82 | gpio_bank->gpio_dev = gpio_dev; |
| 83 | list_add_tail(&gpio_bank->list, &priv->gpio_dev); |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | #endif |
| 89 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 90 | static int stm32_gpio_config(struct gpio_desc *desc, |
| 91 | const struct stm32_gpio_ctl *ctl) |
| 92 | { |
| 93 | struct stm32_gpio_priv *priv = dev_get_priv(desc->dev); |
| 94 | struct stm32_gpio_regs *regs = priv->regs; |
| 95 | u32 index; |
| 96 | |
| 97 | if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 || |
| 98 | ctl->pupd > 2 || ctl->speed > 3) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | index = (desc->offset & 0x07) * 4; |
| 102 | clrsetbits_le32(®s->afr[desc->offset >> 3], AFR_MASK << index, |
| 103 | ctl->af << index); |
| 104 | |
| 105 | index = desc->offset * 2; |
| 106 | clrsetbits_le32(®s->moder, MODE_BITS_MASK << index, |
| 107 | ctl->mode << index); |
| 108 | clrsetbits_le32(®s->ospeedr, OSPEED_MASK << index, |
| 109 | ctl->speed << index); |
| 110 | clrsetbits_le32(®s->pupdr, PUPD_MASK << index, ctl->pupd << index); |
| 111 | |
| 112 | index = desc->offset; |
| 113 | clrsetbits_le32(®s->otyper, OTYPE_MSK << index, ctl->otype << index); |
| 114 | |
| 115 | return 0; |
| 116 | } |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 117 | |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 118 | static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin) |
| 119 | { |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 120 | gpio_dsc->port = (port_pin & 0x1F000) >> 12; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 121 | gpio_dsc->pin = (port_pin & 0x0F00) >> 8; |
| 122 | debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port, |
| 123 | gpio_dsc->pin); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node) |
| 129 | { |
| 130 | gpio_fn &= 0x00FF; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 131 | gpio_ctl->af = 0; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 132 | |
| 133 | switch (gpio_fn) { |
| 134 | case 0: |
| 135 | gpio_ctl->mode = STM32_GPIO_MODE_IN; |
| 136 | break; |
| 137 | case 1 ... 16: |
| 138 | gpio_ctl->mode = STM32_GPIO_MODE_AF; |
| 139 | gpio_ctl->af = gpio_fn - 1; |
| 140 | break; |
| 141 | case 17: |
| 142 | gpio_ctl->mode = STM32_GPIO_MODE_AN; |
| 143 | break; |
| 144 | default: |
| 145 | gpio_ctl->mode = STM32_GPIO_MODE_OUT; |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0); |
| 150 | |
| 151 | if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain")) |
| 152 | gpio_ctl->otype = STM32_GPIO_OTYPE_OD; |
| 153 | else |
| 154 | gpio_ctl->otype = STM32_GPIO_OTYPE_PP; |
| 155 | |
| 156 | if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up")) |
| 157 | gpio_ctl->pupd = STM32_GPIO_PUPD_UP; |
| 158 | else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down")) |
| 159 | gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN; |
| 160 | else |
| 161 | gpio_ctl->pupd = STM32_GPIO_PUPD_NO; |
| 162 | |
| 163 | debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n", |
| 164 | __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype, |
| 165 | gpio_ctl->pupd); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 170 | static int stm32_pinctrl_config(int offset) |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 171 | { |
Vikas Manocha | 40ddb3a | 2017-04-10 15:03:04 -0700 | [diff] [blame] | 172 | u32 pin_mux[MAX_PINS_ONE_IP]; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 173 | int rv, len; |
| 174 | |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 175 | /* |
| 176 | * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for |
| 177 | * usart1) of pin controller phandle "pinctrl-0" |
| 178 | * */ |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 179 | fdt_for_each_subnode(offset, gd->fdt_blob, offset) { |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 180 | struct stm32_gpio_dsc gpio_dsc; |
| 181 | struct stm32_gpio_ctl gpio_ctl; |
| 182 | int i; |
| 183 | |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 184 | len = fdtdec_get_int_array_count(gd->fdt_blob, offset, |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 185 | "pinmux", pin_mux, |
| 186 | ARRAY_SIZE(pin_mux)); |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 187 | debug("%s: no of pinmux entries= %d\n", __func__, len); |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 188 | if (len < 0) |
| 189 | return -EINVAL; |
| 190 | for (i = 0; i < len; i++) { |
Vikas Manocha | 1a8fde7 | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 191 | struct gpio_desc desc; |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 192 | |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 193 | debug("%s: pinmux = %x\n", __func__, *(pin_mux + i)); |
| 194 | prep_gpio_dsc(&gpio_dsc, *(pin_mux + i)); |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 195 | prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset); |
Vikas Manocha | 1a8fde7 | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 196 | rv = uclass_get_device_by_seq(UCLASS_GPIO, |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 197 | gpio_dsc.port, |
| 198 | &desc.dev); |
Vikas Manocha | 1a8fde7 | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 199 | if (rv) |
| 200 | return rv; |
| 201 | desc.offset = gpio_dsc.pin; |
| 202 | rv = stm32_gpio_config(&desc, &gpio_ctl); |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 203 | debug("%s: rv = %d\n\n", __func__, rv); |
| 204 | if (rv) |
| 205 | return rv; |
| 206 | } |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 212 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 213 | static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 214 | { |
| 215 | return stm32_pinctrl_config(dev_of_offset(config)); |
| 216 | } |
| 217 | #else /* PINCTRL_FULL */ |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 218 | static int stm32_pinctrl_set_state_simple(struct udevice *dev, |
| 219 | struct udevice *periph) |
| 220 | { |
| 221 | const void *fdt = gd->fdt_blob; |
| 222 | const fdt32_t *list; |
| 223 | uint32_t phandle; |
| 224 | int config_node; |
| 225 | int size, i, ret; |
| 226 | |
| 227 | list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size); |
| 228 | if (!list) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | debug("%s: periph->name = %s\n", __func__, periph->name); |
| 232 | |
| 233 | size /= sizeof(*list); |
| 234 | for (i = 0; i < size; i++) { |
| 235 | phandle = fdt32_to_cpu(*list++); |
| 236 | |
| 237 | config_node = fdt_node_offset_by_phandle(fdt, phandle); |
| 238 | if (config_node < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 239 | pr_err("prop pinctrl-0 index %d invalid phandle\n", i); |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | ret = stm32_pinctrl_config(config_node); |
| 244 | if (ret) |
| 245 | return ret; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 250 | #endif /* PINCTRL_FULL */ |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 251 | |
| 252 | static struct pinctrl_ops stm32_pinctrl_ops = { |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 253 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 254 | .set_state = stm32_pinctrl_set_state, |
| 255 | #else /* PINCTRL_FULL */ |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 256 | .set_state_simple = stm32_pinctrl_set_state_simple, |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 257 | #endif /* PINCTRL_FULL */ |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame^] | 258 | #ifndef CONFIG_SPL_BUILD |
| 259 | .get_pins_count = stm32_pinctrl_get_pins_count, |
| 260 | #endif |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | static const struct udevice_id stm32_pinctrl_ids[] = { |
Patrice Chotard | b5652b7 | 2017-12-12 09:49:35 +0100 | [diff] [blame] | 264 | { .compatible = "st,stm32f429-pinctrl" }, |
| 265 | { .compatible = "st,stm32f469-pinctrl" }, |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 266 | { .compatible = "st,stm32f746-pinctrl" }, |
Patrice Chotard | 6502c47 | 2017-09-13 18:00:04 +0200 | [diff] [blame] | 267 | { .compatible = "st,stm32h743-pinctrl" }, |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 268 | { .compatible = "st,stm32mp157-pinctrl" }, |
| 269 | { .compatible = "st,stm32mp157-z-pinctrl" }, |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 270 | { } |
| 271 | }; |
| 272 | |
| 273 | U_BOOT_DRIVER(pinctrl_stm32) = { |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame^] | 274 | .name = "pinctrl_stm32", |
| 275 | .id = UCLASS_PINCTRL, |
| 276 | .of_match = stm32_pinctrl_ids, |
| 277 | .ops = &stm32_pinctrl_ops, |
| 278 | .bind = dm_scan_fdt_dev, |
| 279 | #ifndef CONFIG_SPL_BUILD |
| 280 | .probe = stm32_pinctrl_probe, |
| 281 | .priv_auto_alloc_size = sizeof(struct stm32_pinctrl_priv), |
| 282 | #endif |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 283 | }; |