pinctrl_stm32.c 8.1 KB

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