pinctrl_stm32.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #define PINMUX_MODE_COUNT 5
  26. static const char * const pinmux_mode[PINMUX_MODE_COUNT] = {
  27. "gpio input",
  28. "gpio output",
  29. "analog",
  30. "unknown",
  31. "alt function",
  32. };
  33. static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
  34. {
  35. struct stm32_gpio_priv *priv = dev_get_priv(dev);
  36. struct stm32_gpio_regs *regs = priv->regs;
  37. u32 af;
  38. u32 alt_shift = (offset % 8) * 4;
  39. u32 alt_index = offset / 8;
  40. af = (readl(&regs->afr[alt_index]) &
  41. GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
  42. return af;
  43. }
  44. static int stm32_pinctrl_get_pins_count(struct udevice *dev)
  45. {
  46. struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
  47. struct gpio_dev_priv *uc_priv;
  48. struct stm32_gpio_bank *gpio_bank;
  49. /*
  50. * if get_pins_count has already been executed once on this
  51. * pin-controller, no need to run it again
  52. */
  53. if (priv->pinctrl_ngpios)
  54. return priv->pinctrl_ngpios;
  55. /*
  56. * walk through all banks to retrieve the pin-controller
  57. * pins number
  58. */
  59. list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
  60. uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
  61. priv->pinctrl_ngpios += uc_priv->gpio_count;
  62. }
  63. return priv->pinctrl_ngpios;
  64. }
  65. static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
  66. unsigned int selector)
  67. {
  68. struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
  69. struct stm32_gpio_bank *gpio_bank;
  70. struct gpio_dev_priv *uc_priv;
  71. int first_pin = 0;
  72. /* look up for the bank which owns the requested pin */
  73. list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
  74. uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
  75. if (selector < (first_pin + uc_priv->gpio_count))
  76. /* we found the bank */
  77. return gpio_bank->gpio_dev;
  78. first_pin += uc_priv->gpio_count;
  79. }
  80. return NULL;
  81. }
  82. static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
  83. unsigned int selector)
  84. {
  85. struct gpio_dev_priv *uc_priv;
  86. struct udevice *gpio_dev;
  87. /* look up for the bank which owns the requested pin */
  88. gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector);
  89. if (!gpio_dev) {
  90. snprintf(pin_name, PINNAME_SIZE, "Error");
  91. } else {
  92. uc_priv = dev_get_uclass_priv(gpio_dev);
  93. snprintf(pin_name, PINNAME_SIZE, "%s%d",
  94. uc_priv->bank_name,
  95. selector % MAX_PIN_PER_BANK);
  96. }
  97. return pin_name;
  98. }
  99. static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
  100. unsigned int selector,
  101. char *buf,
  102. int size)
  103. {
  104. struct udevice *gpio_dev;
  105. const char *label;
  106. int gpio_pin;
  107. int mode;
  108. int af_num;
  109. /* look up for the bank which owns the requested pin */
  110. gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector);
  111. if (!gpio_dev)
  112. return -ENODEV;
  113. /* translate pin-controller pin number to gpio pin number */
  114. gpio_pin = selector % MAX_PIN_PER_BANK;
  115. mode = gpio_get_raw_function(gpio_dev, gpio_pin, &label);
  116. dev_dbg(dev, "selector = %d gpio_pin = %d mode = %d\n",
  117. selector, gpio_pin, mode);
  118. switch (mode) {
  119. case GPIOF_UNKNOWN:
  120. /* should never happen */
  121. return -EINVAL;
  122. case GPIOF_UNUSED:
  123. snprintf(buf, size, "%s", pinmux_mode[mode]);
  124. break;
  125. case GPIOF_FUNC:
  126. af_num = stm32_pinctrl_get_af(gpio_dev, gpio_pin);
  127. snprintf(buf, size, "%s %d", pinmux_mode[mode], af_num);
  128. break;
  129. case GPIOF_OUTPUT:
  130. case GPIOF_INPUT:
  131. snprintf(buf, size, "%s %s",
  132. pinmux_mode[mode], label ? label : "");
  133. break;
  134. }
  135. return 0;
  136. }
  137. int stm32_pinctrl_probe(struct udevice *dev)
  138. {
  139. struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
  140. struct udevice *gpio_dev;
  141. struct udevice *child;
  142. struct stm32_gpio_bank *gpio_bank;
  143. int ret;
  144. INIT_LIST_HEAD(&priv->gpio_dev);
  145. /*
  146. * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
  147. * a list with all gpio device reference which belongs to the
  148. * current pin-controller. This list is used to find pin_name and
  149. * pin muxing
  150. */
  151. list_for_each_entry(child, &dev->child_head, sibling_node) {
  152. ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
  153. &gpio_dev);
  154. if (ret < 0)
  155. continue;
  156. gpio_bank = malloc(sizeof(*gpio_bank));
  157. if (!gpio_bank) {
  158. dev_err(dev, "Not enough memory\n");
  159. return -ENOMEM;
  160. }
  161. gpio_bank->gpio_dev = gpio_dev;
  162. list_add_tail(&gpio_bank->list, &priv->gpio_dev);
  163. }
  164. return 0;
  165. }
  166. #endif
  167. static int stm32_gpio_config(struct gpio_desc *desc,
  168. const struct stm32_gpio_ctl *ctl)
  169. {
  170. struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
  171. struct stm32_gpio_regs *regs = priv->regs;
  172. u32 index;
  173. if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
  174. ctl->pupd > 2 || ctl->speed > 3)
  175. return -EINVAL;
  176. index = (desc->offset & 0x07) * 4;
  177. clrsetbits_le32(&regs->afr[desc->offset >> 3], AFR_MASK << index,
  178. ctl->af << index);
  179. index = desc->offset * 2;
  180. clrsetbits_le32(&regs->moder, MODE_BITS_MASK << index,
  181. ctl->mode << index);
  182. clrsetbits_le32(&regs->ospeedr, OSPEED_MASK << index,
  183. ctl->speed << index);
  184. clrsetbits_le32(&regs->pupdr, PUPD_MASK << index, ctl->pupd << index);
  185. index = desc->offset;
  186. clrsetbits_le32(&regs->otyper, OTYPE_MSK << index, ctl->otype << index);
  187. return 0;
  188. }
  189. static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
  190. {
  191. gpio_dsc->port = (port_pin & 0x1F000) >> 12;
  192. gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
  193. debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
  194. gpio_dsc->pin);
  195. return 0;
  196. }
  197. static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node)
  198. {
  199. gpio_fn &= 0x00FF;
  200. gpio_ctl->af = 0;
  201. switch (gpio_fn) {
  202. case 0:
  203. gpio_ctl->mode = STM32_GPIO_MODE_IN;
  204. break;
  205. case 1 ... 16:
  206. gpio_ctl->mode = STM32_GPIO_MODE_AF;
  207. gpio_ctl->af = gpio_fn - 1;
  208. break;
  209. case 17:
  210. gpio_ctl->mode = STM32_GPIO_MODE_AN;
  211. break;
  212. default:
  213. gpio_ctl->mode = STM32_GPIO_MODE_OUT;
  214. break;
  215. }
  216. gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0);
  217. if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain"))
  218. gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
  219. else
  220. gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
  221. if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up"))
  222. gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
  223. else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down"))
  224. gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
  225. else
  226. gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
  227. debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
  228. __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
  229. gpio_ctl->pupd);
  230. return 0;
  231. }
  232. static int stm32_pinctrl_config(int offset)
  233. {
  234. u32 pin_mux[MAX_PINS_ONE_IP];
  235. int rv, len;
  236. /*
  237. * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
  238. * usart1) of pin controller phandle "pinctrl-0"
  239. * */
  240. fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
  241. struct stm32_gpio_dsc gpio_dsc;
  242. struct stm32_gpio_ctl gpio_ctl;
  243. int i;
  244. len = fdtdec_get_int_array_count(gd->fdt_blob, offset,
  245. "pinmux", pin_mux,
  246. ARRAY_SIZE(pin_mux));
  247. debug("%s: no of pinmux entries= %d\n", __func__, len);
  248. if (len < 0)
  249. return -EINVAL;
  250. for (i = 0; i < len; i++) {
  251. struct gpio_desc desc;
  252. debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
  253. prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
  254. prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset);
  255. rv = uclass_get_device_by_seq(UCLASS_GPIO,
  256. gpio_dsc.port,
  257. &desc.dev);
  258. if (rv)
  259. return rv;
  260. desc.offset = gpio_dsc.pin;
  261. rv = stm32_gpio_config(&desc, &gpio_ctl);
  262. debug("%s: rv = %d\n\n", __func__, rv);
  263. if (rv)
  264. return rv;
  265. }
  266. }
  267. return 0;
  268. }
  269. #if CONFIG_IS_ENABLED(PINCTRL_FULL)
  270. static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
  271. {
  272. return stm32_pinctrl_config(dev_of_offset(config));
  273. }
  274. #else /* PINCTRL_FULL */
  275. static int stm32_pinctrl_set_state_simple(struct udevice *dev,
  276. struct udevice *periph)
  277. {
  278. const void *fdt = gd->fdt_blob;
  279. const fdt32_t *list;
  280. uint32_t phandle;
  281. int config_node;
  282. int size, i, ret;
  283. list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size);
  284. if (!list)
  285. return -EINVAL;
  286. debug("%s: periph->name = %s\n", __func__, periph->name);
  287. size /= sizeof(*list);
  288. for (i = 0; i < size; i++) {
  289. phandle = fdt32_to_cpu(*list++);
  290. config_node = fdt_node_offset_by_phandle(fdt, phandle);
  291. if (config_node < 0) {
  292. pr_err("prop pinctrl-0 index %d invalid phandle\n", i);
  293. return -EINVAL;
  294. }
  295. ret = stm32_pinctrl_config(config_node);
  296. if (ret)
  297. return ret;
  298. }
  299. return 0;
  300. }
  301. #endif /* PINCTRL_FULL */
  302. static struct pinctrl_ops stm32_pinctrl_ops = {
  303. #if CONFIG_IS_ENABLED(PINCTRL_FULL)
  304. .set_state = stm32_pinctrl_set_state,
  305. #else /* PINCTRL_FULL */
  306. .set_state_simple = stm32_pinctrl_set_state_simple,
  307. #endif /* PINCTRL_FULL */
  308. #ifndef CONFIG_SPL_BUILD
  309. .get_pin_name = stm32_pinctrl_get_pin_name,
  310. .get_pins_count = stm32_pinctrl_get_pins_count,
  311. .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
  312. #endif
  313. };
  314. static const struct udevice_id stm32_pinctrl_ids[] = {
  315. { .compatible = "st,stm32f429-pinctrl" },
  316. { .compatible = "st,stm32f469-pinctrl" },
  317. { .compatible = "st,stm32f746-pinctrl" },
  318. { .compatible = "st,stm32h743-pinctrl" },
  319. { .compatible = "st,stm32mp157-pinctrl" },
  320. { .compatible = "st,stm32mp157-z-pinctrl" },
  321. { }
  322. };
  323. U_BOOT_DRIVER(pinctrl_stm32) = {
  324. .name = "pinctrl_stm32",
  325. .id = UCLASS_PINCTRL,
  326. .of_match = stm32_pinctrl_ids,
  327. .ops = &stm32_pinctrl_ops,
  328. .bind = dm_scan_fdt_dev,
  329. #ifndef CONFIG_SPL_BUILD
  330. .probe = stm32_pinctrl_probe,
  331. .priv_auto_alloc_size = sizeof(struct stm32_pinctrl_priv),
  332. #endif
  333. };