stm32_gpio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (c) 2016-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdbool.h>
  9. #include <common/bl_common.h>
  10. #include <common/debug.h>
  11. #include <drivers/clk.h>
  12. #include <drivers/st/stm32_gpio.h>
  13. #include <drivers/st/stm32mp_clkfunc.h>
  14. #include <lib/mmio.h>
  15. #include <lib/utils_def.h>
  16. #include <libfdt.h>
  17. #include <platform_def.h>
  18. #define DT_GPIO_BANK_SHIFT 12
  19. #define DT_GPIO_BANK_MASK GENMASK(16, 12)
  20. #define DT_GPIO_PIN_SHIFT 8
  21. #define DT_GPIO_PIN_MASK GENMASK(11, 8)
  22. #define DT_GPIO_MODE_MASK GENMASK(7, 0)
  23. static void set_gpio(uint32_t bank, uint32_t pin, uint32_t mode, uint32_t type,
  24. uint32_t speed, uint32_t pull, uint32_t od,
  25. uint32_t alternate, uint8_t status);
  26. /*******************************************************************************
  27. * This function gets GPIO bank node in DT.
  28. * Returns node offset if status is okay in DT, else return 0
  29. ******************************************************************************/
  30. static int ckeck_gpio_bank(void *fdt, uint32_t bank, int pinctrl_node)
  31. {
  32. int pinctrl_subnode;
  33. uint32_t bank_offset = stm32_get_gpio_bank_offset(bank);
  34. fdt_for_each_subnode(pinctrl_subnode, fdt, pinctrl_node) {
  35. const fdt32_t *cuint;
  36. if (fdt_getprop(fdt, pinctrl_subnode,
  37. "gpio-controller", NULL) == NULL) {
  38. continue;
  39. }
  40. cuint = fdt_getprop(fdt, pinctrl_subnode, "reg", NULL);
  41. if (cuint == NULL) {
  42. continue;
  43. }
  44. if ((fdt32_to_cpu(*cuint) == bank_offset) &&
  45. (fdt_get_status(pinctrl_subnode) != DT_DISABLED)) {
  46. return pinctrl_subnode;
  47. }
  48. }
  49. return 0;
  50. }
  51. /*******************************************************************************
  52. * This function gets the pin settings from DT information.
  53. * When analyze and parsing is done, set the GPIO registers.
  54. * Returns 0 on success and a negative FDT error code on failure.
  55. ******************************************************************************/
  56. static int dt_set_gpio_config(void *fdt, int node, uint8_t status)
  57. {
  58. const fdt32_t *cuint, *slewrate;
  59. int len;
  60. int pinctrl_node;
  61. uint32_t i;
  62. uint32_t speed = GPIO_SPEED_LOW;
  63. uint32_t pull = GPIO_NO_PULL;
  64. cuint = fdt_getprop(fdt, node, "pinmux", &len);
  65. if (cuint == NULL) {
  66. return -FDT_ERR_NOTFOUND;
  67. }
  68. pinctrl_node = fdt_parent_offset(fdt, fdt_parent_offset(fdt, node));
  69. if (pinctrl_node < 0) {
  70. return -FDT_ERR_NOTFOUND;
  71. }
  72. slewrate = fdt_getprop(fdt, node, "slew-rate", NULL);
  73. if (slewrate != NULL) {
  74. speed = fdt32_to_cpu(*slewrate);
  75. }
  76. if (fdt_getprop(fdt, node, "bias-pull-up", NULL) != NULL) {
  77. pull = GPIO_PULL_UP;
  78. } else if (fdt_getprop(fdt, node, "bias-pull-down", NULL) != NULL) {
  79. pull = GPIO_PULL_DOWN;
  80. } else {
  81. VERBOSE("No bias configured in node %d\n", node);
  82. }
  83. for (i = 0U; i < ((uint32_t)len / sizeof(uint32_t)); i++) {
  84. uint32_t pincfg;
  85. uint32_t bank;
  86. uint32_t pin;
  87. uint32_t mode;
  88. uint32_t alternate = GPIO_ALTERNATE_(0);
  89. uint32_t type;
  90. uint32_t od = GPIO_OD_OUTPUT_LOW;
  91. int bank_node;
  92. int clk;
  93. pincfg = fdt32_to_cpu(*cuint);
  94. cuint++;
  95. bank = (pincfg & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT;
  96. pin = (pincfg & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT;
  97. mode = pincfg & DT_GPIO_MODE_MASK;
  98. switch (mode) {
  99. case 0:
  100. mode = GPIO_MODE_INPUT;
  101. break;
  102. case 1 ... 16:
  103. alternate = mode - 1U;
  104. mode = GPIO_MODE_ALTERNATE;
  105. break;
  106. case 17:
  107. mode = GPIO_MODE_ANALOG;
  108. break;
  109. default:
  110. mode = GPIO_MODE_OUTPUT;
  111. break;
  112. }
  113. if (fdt_getprop(fdt, node, "drive-open-drain", NULL) != NULL) {
  114. type = GPIO_TYPE_OPEN_DRAIN;
  115. } else {
  116. type = GPIO_TYPE_PUSH_PULL;
  117. }
  118. if (fdt_getprop(fdt, node, "output-high", NULL) != NULL) {
  119. if (mode == GPIO_MODE_INPUT) {
  120. mode = GPIO_MODE_OUTPUT;
  121. od = GPIO_OD_OUTPUT_HIGH;
  122. }
  123. }
  124. if (fdt_getprop(fdt, node, "output-low", NULL) != NULL) {
  125. if (mode == GPIO_MODE_INPUT) {
  126. mode = GPIO_MODE_OUTPUT;
  127. od = GPIO_OD_OUTPUT_LOW;
  128. }
  129. }
  130. bank_node = ckeck_gpio_bank(fdt, bank, pinctrl_node);
  131. if (bank_node == 0) {
  132. ERROR("PINCTRL inconsistent in DT\n");
  133. panic();
  134. }
  135. clk = fdt_get_clock_id(bank_node);
  136. if (clk < 0) {
  137. return -FDT_ERR_NOTFOUND;
  138. }
  139. /* Platform knows the clock: assert it is okay */
  140. assert((unsigned long)clk == stm32_get_gpio_bank_clock(bank));
  141. set_gpio(bank, pin, mode, type, speed, pull, od, alternate, status);
  142. }
  143. return 0;
  144. }
  145. /*******************************************************************************
  146. * This function gets the pin settings from DT information.
  147. * When analyze and parsing is done, set the GPIO registers.
  148. * Returns 0 on success and a negative FDT/ERRNO error code on failure.
  149. ******************************************************************************/
  150. int dt_set_pinctrl_config(int node)
  151. {
  152. const fdt32_t *cuint;
  153. int lenp;
  154. uint32_t i;
  155. uint8_t status;
  156. void *fdt;
  157. if (fdt_get_address(&fdt) == 0) {
  158. return -FDT_ERR_NOTFOUND;
  159. }
  160. status = fdt_get_status(node);
  161. if (status == DT_DISABLED) {
  162. return -FDT_ERR_NOTFOUND;
  163. }
  164. cuint = fdt_getprop(fdt, node, "pinctrl-0", &lenp);
  165. if (cuint == NULL) {
  166. return -FDT_ERR_NOTFOUND;
  167. }
  168. for (i = 0; i < ((uint32_t)lenp / 4U); i++) {
  169. int p_node, p_subnode;
  170. p_node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
  171. if (p_node < 0) {
  172. return -FDT_ERR_NOTFOUND;
  173. }
  174. fdt_for_each_subnode(p_subnode, fdt, p_node) {
  175. int ret = dt_set_gpio_config(fdt, p_subnode, status);
  176. if (ret < 0) {
  177. return ret;
  178. }
  179. }
  180. cuint++;
  181. }
  182. return 0;
  183. }
  184. static void set_gpio(uint32_t bank, uint32_t pin, uint32_t mode, uint32_t type,
  185. uint32_t speed, uint32_t pull, uint32_t od,
  186. uint32_t alternate, uint8_t status)
  187. {
  188. uintptr_t base = stm32_get_gpio_bank_base(bank);
  189. unsigned long clock = stm32_get_gpio_bank_clock(bank);
  190. assert(pin <= GPIO_PIN_MAX);
  191. clk_enable(clock);
  192. mmio_clrsetbits_32(base + GPIO_MODE_OFFSET,
  193. (uint32_t)GPIO_MODE_MASK << (pin << 1U),
  194. mode << (pin << 1U));
  195. mmio_clrsetbits_32(base + GPIO_TYPE_OFFSET,
  196. (uint32_t)GPIO_TYPE_MASK << pin,
  197. type << pin);
  198. mmio_clrsetbits_32(base + GPIO_SPEED_OFFSET,
  199. (uint32_t)GPIO_SPEED_MASK << (pin << 1U),
  200. speed << (pin << 1U));
  201. mmio_clrsetbits_32(base + GPIO_PUPD_OFFSET,
  202. (uint32_t)GPIO_PULL_MASK << (pin << 1U),
  203. pull << (pin << 1U));
  204. if (pin < GPIO_ALT_LOWER_LIMIT) {
  205. mmio_clrsetbits_32(base + GPIO_AFRL_OFFSET,
  206. (uint32_t)GPIO_ALTERNATE_MASK << (pin << 2U),
  207. alternate << (pin << 2U));
  208. } else {
  209. uint32_t shift = (pin - GPIO_ALT_LOWER_LIMIT) << 2U;
  210. mmio_clrsetbits_32(base + GPIO_AFRH_OFFSET,
  211. (uint32_t)GPIO_ALTERNATE_MASK << shift,
  212. alternate << shift);
  213. }
  214. mmio_clrsetbits_32(base + GPIO_OD_OFFSET,
  215. (uint32_t)GPIO_OD_MASK << pin,
  216. od << pin);
  217. VERBOSE("GPIO %u mode set to 0x%x\n", bank,
  218. mmio_read_32(base + GPIO_MODE_OFFSET));
  219. VERBOSE("GPIO %u type set to 0x%x\n", bank,
  220. mmio_read_32(base + GPIO_TYPE_OFFSET));
  221. VERBOSE("GPIO %u speed set to 0x%x\n", bank,
  222. mmio_read_32(base + GPIO_SPEED_OFFSET));
  223. VERBOSE("GPIO %u mode pull to 0x%x\n", bank,
  224. mmio_read_32(base + GPIO_PUPD_OFFSET));
  225. VERBOSE("GPIO %u mode alternate low to 0x%x\n", bank,
  226. mmio_read_32(base + GPIO_AFRL_OFFSET));
  227. VERBOSE("GPIO %u mode alternate high to 0x%x\n", bank,
  228. mmio_read_32(base + GPIO_AFRH_OFFSET));
  229. VERBOSE("GPIO %u output data set to 0x%x\n", bank,
  230. mmio_read_32(base + GPIO_OD_OFFSET));
  231. clk_disable(clock);
  232. #if STM32MP13 || STM32MP15
  233. if (status == DT_SECURE) {
  234. stm32mp_register_secure_gpio(bank, pin);
  235. #if !IMAGE_BL2
  236. set_gpio_secure_cfg(bank, pin, true);
  237. #endif
  238. } else {
  239. stm32mp_register_non_secure_gpio(bank, pin);
  240. #if !IMAGE_BL2
  241. set_gpio_secure_cfg(bank, pin, false);
  242. #endif
  243. }
  244. #else /* !STM32MP13 && !STM32MP15 */
  245. set_gpio_secure_cfg(bank, pin, true);
  246. #endif /* STM32MP13 || STM32MP15 */
  247. }
  248. void set_gpio_secure_cfg(uint32_t bank, uint32_t pin, bool secure)
  249. {
  250. uintptr_t base = stm32_get_gpio_bank_base(bank);
  251. unsigned long clock = stm32_get_gpio_bank_clock(bank);
  252. assert(pin <= GPIO_PIN_MAX);
  253. clk_enable(clock);
  254. if (secure) {
  255. mmio_setbits_32(base + GPIO_SECR_OFFSET, BIT(pin));
  256. } else {
  257. mmio_clrbits_32(base + GPIO_SECR_OFFSET, BIT(pin));
  258. }
  259. clk_disable(clock);
  260. }
  261. void set_gpio_reset_cfg(uint32_t bank, uint32_t pin)
  262. {
  263. set_gpio(bank, pin, GPIO_MODE_ANALOG, GPIO_TYPE_PUSH_PULL,
  264. GPIO_SPEED_LOW, GPIO_NO_PULL, GPIO_OD_OUTPUT_LOW,
  265. GPIO_ALTERNATE_(0), DT_DISABLED);
  266. set_gpio_secure_cfg(bank, pin, stm32_gpio_is_secure_at_reset(bank));
  267. }
  268. void set_gpio_level(uint32_t bank, uint32_t pin, enum gpio_level level)
  269. {
  270. uintptr_t base = stm32_get_gpio_bank_base(bank);
  271. unsigned long clock = stm32_get_gpio_bank_clock(bank);
  272. assert(pin <= GPIO_PIN_MAX);
  273. clk_enable(clock);
  274. if (level == GPIO_LEVEL_HIGH) {
  275. mmio_write_32(base + GPIO_BSRR_OFFSET, BIT(pin));
  276. } else {
  277. mmio_write_32(base + GPIO_BSRR_OFFSET, BIT(pin + 16U));
  278. }
  279. VERBOSE("GPIO %u level set to 0x%x\n", bank,
  280. mmio_read_32(base + GPIO_IDR_OFFSET));
  281. clk_disable(clock);
  282. }
  283. enum gpio_level get_gpio_level(uint32_t bank, uint32_t pin)
  284. {
  285. uintptr_t base = stm32_get_gpio_bank_base(bank);
  286. unsigned long clock = stm32_get_gpio_bank_clock(bank);
  287. enum gpio_level level = GPIO_LEVEL_LOW;
  288. assert(pin <= GPIO_PIN_MAX);
  289. clk_enable(clock);
  290. if (mmio_read_32(base + GPIO_IDR_OFFSET) & BIT(pin)) {
  291. level = GPIO_LEVEL_HIGH;
  292. }
  293. VERBOSE("GPIO %u get level 0x%x\n", bank,
  294. mmio_read_32(base + GPIO_IDR_OFFSET));
  295. clk_disable(clock);
  296. return level;
  297. }
  298. void set_gpio_config(uint32_t bank, uint32_t pin, uint32_t config, uint8_t status)
  299. {
  300. uint32_t mode = GPIO_MODE_OUTPUT;
  301. uint32_t od = 0U;
  302. uint32_t pull = GPIO_NO_PULL;
  303. VERBOSE("GPIO %u:%u set config to 0x%x\n", bank, pin, config);
  304. if (config & GPIOF_DIR_IN) {
  305. mode = GPIO_MODE_INPUT;
  306. }
  307. if (config & GPIOF_OUT_INIT_HIGH) {
  308. od = 1U;
  309. }
  310. if (config & GPIOF_PULL_UP) {
  311. pull |= GPIO_PULL_UP;
  312. }
  313. if (config & GPIOF_PULL_DOWN) {
  314. pull |= GPIO_PULL_DOWN;
  315. }
  316. set_gpio(bank, pin, mode, GPIO_TYPE_PUSH_PULL, GPIO_SPEED_LOW,
  317. pull, od, GPIO_ALTERNATE_(0), status);
  318. }