pinctrl-sti.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Pinctrl driver for STMicroelectronics STi SoCs
  4. *
  5. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6. * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
  7. */
  8. #include <common.h>
  9. #include <bitfield.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <regmap.h>
  13. #include <syscon.h>
  14. #include <asm/io.h>
  15. #include <dm/pinctrl.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. #define MAX_STI_PINCONF_ENTRIES 7
  18. /* Output enable */
  19. #define OE (1 << 27)
  20. /* Pull Up */
  21. #define PU (1 << 26)
  22. /* Open Drain */
  23. #define OD (1 << 25)
  24. /* User-frendly defines for Pin Direction */
  25. /* oe = 0, pu = 0, od = 0 */
  26. #define IN (0)
  27. /* oe = 0, pu = 1, od = 0 */
  28. #define IN_PU (PU)
  29. /* oe = 1, pu = 0, od = 0 */
  30. #define OUT (OE)
  31. /* oe = 1, pu = 1, od = 0 */
  32. #define OUT_PU (OE | PU)
  33. /* oe = 1, pu = 0, od = 1 */
  34. #define BIDIR (OE | OD)
  35. /* oe = 1, pu = 1, od = 1 */
  36. #define BIDIR_PU (OE | PU | OD)
  37. struct sti_pinctrl_platdata {
  38. struct regmap *regmap;
  39. };
  40. struct sti_pin_desc {
  41. unsigned char bank;
  42. unsigned char pin;
  43. unsigned char alt;
  44. int dir;
  45. };
  46. /*
  47. * PIO alternative Function selector
  48. */
  49. void sti_alternate_select(struct udevice *dev, struct sti_pin_desc *pin_desc)
  50. {
  51. struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
  52. unsigned long sysconf, *sysconfreg;
  53. int alt = pin_desc->alt;
  54. int bank = pin_desc->bank;
  55. int pin = pin_desc->pin;
  56. sysconfreg = (unsigned long *)plat->regmap->ranges[0].start;
  57. switch (bank) {
  58. case 0 ... 5: /* in "SBC Bank" */
  59. sysconfreg += bank;
  60. break;
  61. case 10 ... 20: /* in "FRONT Bank" */
  62. sysconfreg += bank - 10;
  63. break;
  64. case 30 ... 35: /* in "REAR Bank" */
  65. sysconfreg += bank - 30;
  66. break;
  67. case 40 ... 42: /* in "FLASH Bank" */
  68. sysconfreg += bank - 40;
  69. break;
  70. default:
  71. BUG();
  72. return;
  73. }
  74. sysconf = readl(sysconfreg);
  75. sysconf = bitfield_replace(sysconf, pin * 4, 3, alt);
  76. writel(sysconf, sysconfreg);
  77. }
  78. /* pin configuration */
  79. void sti_pin_configure(struct udevice *dev, struct sti_pin_desc *pin_desc)
  80. {
  81. struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
  82. int bit;
  83. int oe = 0, pu = 0, od = 0;
  84. unsigned long *sysconfreg;
  85. int bank = pin_desc->bank;
  86. sysconfreg = (unsigned long *)plat->regmap->ranges[0].start + 40;
  87. /*
  88. * NOTE: The PIO configuration for the PIO pins in the
  89. * "FLASH Bank" are different from all the other banks!
  90. * Specifically, the output-enable pin control register
  91. * (SYS_CFG_3040) and the pull-up pin control register
  92. * (SYS_CFG_3050), are both classed as being "reserved".
  93. * Hence, we do not write to these registers to configure
  94. * the OE and PU features for PIOs in this bank. However,
  95. * the open-drain pin control register (SYS_CFG_3060)
  96. * follows the style of the other banks, and so we can
  97. * treat that register normally.
  98. *
  99. * Being pedantic, we should configure the PU and PD features
  100. * in the "FLASH Bank" explicitly instead using the four
  101. * SYS_CFG registers: 3080, 3081, 3085, and 3086. However, this
  102. * would necessitate passing in the alternate function number
  103. * to this function, and adding some horrible complexity here.
  104. * Alternatively, we could just perform 4 32-bit "pokes" to
  105. * these four SYS_CFG registers early in the initialization.
  106. * In practice, these four SYS_CFG registers are correct
  107. * after a reset, and U-Boot does not need to change them, so
  108. * we (cheat and) rely on these registers being correct.
  109. * WARNING: Please be aware of this (pragmatic) behaviour!
  110. */
  111. int flashss = 0; /* bool: PIO in the Flash Sub-System ? */
  112. switch (pin_desc->dir) {
  113. case IN:
  114. oe = 0; pu = 0; od = 0;
  115. break;
  116. case IN_PU:
  117. oe = 0; pu = 1; od = 0;
  118. break;
  119. case OUT:
  120. oe = 1; pu = 0; od = 0;
  121. break;
  122. case BIDIR:
  123. oe = 1; pu = 0; od = 1;
  124. break;
  125. case BIDIR_PU:
  126. oe = 1; pu = 1; od = 1;
  127. break;
  128. default:
  129. pr_err("%s invalid direction value: 0x%x\n",
  130. __func__, pin_desc->dir);
  131. BUG();
  132. break;
  133. }
  134. switch (bank) {
  135. case 0 ... 5: /* in "SBC Bank" */
  136. sysconfreg += bank / 4;
  137. break;
  138. case 10 ... 20: /* in "FRONT Bank" */
  139. bank -= 10;
  140. sysconfreg += bank / 4;
  141. break;
  142. case 30 ... 35: /* in "REAR Bank" */
  143. bank -= 30;
  144. sysconfreg += bank / 4;
  145. break;
  146. case 40 ... 42: /* in "FLASH Bank" */
  147. bank -= 40;
  148. sysconfreg += bank / 4;
  149. flashss = 1; /* pin is in the Flash Sub-System */
  150. break;
  151. default:
  152. BUG();
  153. return;
  154. }
  155. bit = ((bank * 8) + pin_desc->pin) % 32;
  156. /*
  157. * set the "Output Enable" pin control
  158. * but, do nothing if in the flashSS
  159. */
  160. if (!flashss) {
  161. if (oe)
  162. generic_set_bit(bit, sysconfreg);
  163. else
  164. generic_clear_bit(bit, sysconfreg);
  165. }
  166. sysconfreg += 10; /* skip to next set of syscfg registers */
  167. /*
  168. * set the "Pull Up" pin control
  169. * but, do nothing if in the FlashSS
  170. */
  171. if (!flashss) {
  172. if (pu)
  173. generic_set_bit(bit, sysconfreg);
  174. else
  175. generic_clear_bit(bit, sysconfreg);
  176. }
  177. sysconfreg += 10; /* skip to next set of syscfg registers */
  178. /* set the "Open Drain Enable" pin control */
  179. if (od)
  180. generic_set_bit(bit, sysconfreg);
  181. else
  182. generic_clear_bit(bit, sysconfreg);
  183. }
  184. static int sti_pinctrl_set_state(struct udevice *dev, struct udevice *config)
  185. {
  186. struct fdtdec_phandle_args args;
  187. const void *blob = gd->fdt_blob;
  188. const char *prop_name;
  189. int node = dev_of_offset(config);
  190. int property_offset, prop_len;
  191. int pinconf_node, ret, count;
  192. const char *bank_name;
  193. u32 cells[MAX_STI_PINCONF_ENTRIES];
  194. struct sti_pin_desc pin_desc;
  195. /* go to next node "st,pins" which contains the pins configuration */
  196. pinconf_node = fdt_subnode_offset(blob, node, "st,pins");
  197. /*
  198. * parse each pins configuration which looks like :
  199. * pin_name = <bank_phandle pin_nb alt dir rt_type rt_delay rt_clk>
  200. */
  201. fdt_for_each_property_offset(property_offset, blob, pinconf_node) {
  202. fdt_getprop_by_offset(blob, property_offset, &prop_name,
  203. &prop_len);
  204. /* extract the bank of the pin description */
  205. ret = fdtdec_parse_phandle_with_args(blob, pinconf_node,
  206. prop_name, "#gpio-cells",
  207. 0, 0, &args);
  208. if (ret < 0) {
  209. pr_err("Can't get the gpio bank phandle: %d\n", ret);
  210. return ret;
  211. }
  212. bank_name = fdt_getprop(blob, args.node, "st,bank-name",
  213. &count);
  214. if (count < 0) {
  215. pr_err("Can't find bank-name property %d\n", count);
  216. return -EINVAL;
  217. }
  218. pin_desc.bank = trailing_strtoln(bank_name, NULL);
  219. count = fdtdec_get_int_array_count(blob, pinconf_node,
  220. prop_name, cells,
  221. ARRAY_SIZE(cells));
  222. if (count < 0) {
  223. pr_err("Bad pin configuration array %d\n", count);
  224. return -EINVAL;
  225. }
  226. if (count > MAX_STI_PINCONF_ENTRIES) {
  227. pr_err("Unsupported pinconf array count %d\n", count);
  228. return -EINVAL;
  229. }
  230. pin_desc.pin = cells[1];
  231. pin_desc.alt = cells[2];
  232. pin_desc.dir = cells[3];
  233. sti_alternate_select(dev, &pin_desc);
  234. sti_pin_configure(dev, &pin_desc);
  235. };
  236. return 0;
  237. }
  238. static int sti_pinctrl_probe(struct udevice *dev)
  239. {
  240. struct sti_pinctrl_platdata *plat = dev_get_platdata(dev);
  241. struct udevice *syscon;
  242. int err;
  243. /* get corresponding syscon phandle */
  244. err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
  245. "st,syscfg", &syscon);
  246. if (err) {
  247. pr_err("unable to find syscon device\n");
  248. return err;
  249. }
  250. plat->regmap = syscon_get_regmap(syscon);
  251. if (!plat->regmap) {
  252. pr_err("unable to find regmap\n");
  253. return -ENODEV;
  254. }
  255. return 0;
  256. }
  257. static const struct udevice_id sti_pinctrl_ids[] = {
  258. { .compatible = "st,stih407-sbc-pinctrl" },
  259. { .compatible = "st,stih407-front-pinctrl" },
  260. { .compatible = "st,stih407-rear-pinctrl" },
  261. { .compatible = "st,stih407-flash-pinctrl" },
  262. { }
  263. };
  264. const struct pinctrl_ops sti_pinctrl_ops = {
  265. .set_state = sti_pinctrl_set_state,
  266. };
  267. U_BOOT_DRIVER(pinctrl_sti) = {
  268. .name = "pinctrl_sti",
  269. .id = UCLASS_PINCTRL,
  270. .of_match = sti_pinctrl_ids,
  271. .ops = &sti_pinctrl_ops,
  272. .probe = sti_pinctrl_probe,
  273. .platdata_auto_alloc_size = sizeof(struct sti_pinctrl_platdata),
  274. .ops = &sti_pinctrl_ops,
  275. };