stm32_gpio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * (C) Copyright 2011
  3. * Yuri Tikhonov, Emcraft Systems, yur@emcraft.com
  4. *
  5. * (C) Copyright 2015
  6. * Kamil Lulko, <rev13@wp.pl>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <asm/io.h>
  12. #include <asm/errno.h>
  13. #include <asm/arch/stm32.h>
  14. #include <asm/arch/gpio.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #define STM32_GPIOA_BASE (STM32_AHB1PERIPH_BASE + 0x0000)
  17. #define STM32_GPIOB_BASE (STM32_AHB1PERIPH_BASE + 0x0400)
  18. #define STM32_GPIOC_BASE (STM32_AHB1PERIPH_BASE + 0x0800)
  19. #define STM32_GPIOD_BASE (STM32_AHB1PERIPH_BASE + 0x0C00)
  20. #define STM32_GPIOE_BASE (STM32_AHB1PERIPH_BASE + 0x1000)
  21. #define STM32_GPIOF_BASE (STM32_AHB1PERIPH_BASE + 0x1400)
  22. #define STM32_GPIOG_BASE (STM32_AHB1PERIPH_BASE + 0x1800)
  23. #define STM32_GPIOH_BASE (STM32_AHB1PERIPH_BASE + 0x1C00)
  24. #define STM32_GPIOI_BASE (STM32_AHB1PERIPH_BASE + 0x2000)
  25. static const unsigned long io_base[] = {
  26. STM32_GPIOA_BASE, STM32_GPIOB_BASE, STM32_GPIOC_BASE,
  27. STM32_GPIOD_BASE, STM32_GPIOE_BASE, STM32_GPIOF_BASE,
  28. STM32_GPIOG_BASE, STM32_GPIOH_BASE, STM32_GPIOI_BASE
  29. };
  30. struct stm32_gpio_regs {
  31. u32 moder; /* GPIO port mode */
  32. u32 otyper; /* GPIO port output type */
  33. u32 ospeedr; /* GPIO port output speed */
  34. u32 pupdr; /* GPIO port pull-up/pull-down */
  35. u32 idr; /* GPIO port input data */
  36. u32 odr; /* GPIO port output data */
  37. u32 bsrr; /* GPIO port bit set/reset */
  38. u32 lckr; /* GPIO port configuration lock */
  39. u32 afr[2]; /* GPIO alternate function */
  40. };
  41. #define CHECK_DSC(x) (!x || x->port > 8 || x->pin > 15)
  42. #define CHECK_CTL(x) (!x || x->af > 15 || x->mode > 3 || x->otype > 1 || \
  43. x->pupd > 2 || x->speed > 3)
  44. int stm32_gpio_config(const struct stm32_gpio_dsc *dsc,
  45. const struct stm32_gpio_ctl *ctl)
  46. {
  47. struct stm32_gpio_regs *gpio_regs;
  48. u32 i;
  49. int rv;
  50. if (CHECK_DSC(dsc)) {
  51. rv = -EINVAL;
  52. goto out;
  53. }
  54. if (CHECK_CTL(ctl)) {
  55. rv = -EINVAL;
  56. goto out;
  57. }
  58. gpio_regs = (struct stm32_gpio_regs *)io_base[dsc->port];
  59. setbits_le32(&STM32_RCC->ahb1enr, 1 << dsc->port);
  60. i = (dsc->pin & 0x07) * 4;
  61. clrbits_le32(&gpio_regs->afr[dsc->pin >> 3], (0xF << i));
  62. setbits_le32(&gpio_regs->afr[dsc->pin >> 3], ctl->af << i);
  63. i = dsc->pin * 2;
  64. clrbits_le32(&gpio_regs->moder, (0x3 << i));
  65. setbits_le32(&gpio_regs->moder, ctl->mode << i);
  66. clrbits_le32(&gpio_regs->otyper, (0x3 << i));
  67. setbits_le32(&gpio_regs->otyper, ctl->otype << i);
  68. clrbits_le32(&gpio_regs->ospeedr, (0x3 << i));
  69. setbits_le32(&gpio_regs->ospeedr, ctl->speed << i);
  70. clrbits_le32(&gpio_regs->pupdr, (0x3 << i));
  71. setbits_le32(&gpio_regs->pupdr, ctl->pupd << i);
  72. rv = 0;
  73. out:
  74. return rv;
  75. }
  76. int stm32_gpout_set(const struct stm32_gpio_dsc *dsc, int state)
  77. {
  78. struct stm32_gpio_regs *gpio_regs;
  79. int rv;
  80. if (CHECK_DSC(dsc)) {
  81. rv = -EINVAL;
  82. goto out;
  83. }
  84. gpio_regs = (struct stm32_gpio_regs *)io_base[dsc->port];
  85. if (state)
  86. writel(1 << dsc->pin, &gpio_regs->bsrr);
  87. else
  88. writel(1 << (dsc->pin + 16), &gpio_regs->bsrr);
  89. rv = 0;
  90. out:
  91. return rv;
  92. }
  93. int stm32_gpin_get(const struct stm32_gpio_dsc *dsc)
  94. {
  95. struct stm32_gpio_regs *gpio_regs;
  96. int rv;
  97. if (CHECK_DSC(dsc)) {
  98. rv = -EINVAL;
  99. goto out;
  100. }
  101. gpio_regs = (struct stm32_gpio_regs *)io_base[dsc->port];
  102. rv = readl(&gpio_regs->idr) & (1 << dsc->pin);
  103. out:
  104. return rv;
  105. }
  106. /* Common GPIO API */
  107. int gpio_request(unsigned gpio, const char *label)
  108. {
  109. return 0;
  110. }
  111. int gpio_free(unsigned gpio)
  112. {
  113. return 0;
  114. }
  115. int gpio_direction_input(unsigned gpio)
  116. {
  117. struct stm32_gpio_dsc dsc;
  118. struct stm32_gpio_ctl ctl;
  119. dsc.port = stm32_gpio_to_port(gpio);
  120. dsc.pin = stm32_gpio_to_pin(gpio);
  121. ctl.af = STM32_GPIO_AF0;
  122. ctl.mode = STM32_GPIO_MODE_IN;
  123. ctl.pupd = STM32_GPIO_PUPD_NO;
  124. ctl.speed = STM32_GPIO_SPEED_50M;
  125. return stm32_gpio_config(&dsc, &ctl);
  126. }
  127. int gpio_direction_output(unsigned gpio, int value)
  128. {
  129. struct stm32_gpio_dsc dsc;
  130. struct stm32_gpio_ctl ctl;
  131. int res;
  132. dsc.port = stm32_gpio_to_port(gpio);
  133. dsc.pin = stm32_gpio_to_pin(gpio);
  134. ctl.af = STM32_GPIO_AF0;
  135. ctl.mode = STM32_GPIO_MODE_OUT;
  136. ctl.otype = STM32_GPIO_OTYPE_PP;
  137. ctl.pupd = STM32_GPIO_PUPD_NO;
  138. ctl.speed = STM32_GPIO_SPEED_50M;
  139. res = stm32_gpio_config(&dsc, &ctl);
  140. if (res < 0)
  141. goto out;
  142. res = stm32_gpout_set(&dsc, value);
  143. out:
  144. return res;
  145. }
  146. int gpio_get_value(unsigned gpio)
  147. {
  148. struct stm32_gpio_dsc dsc;
  149. dsc.port = stm32_gpio_to_port(gpio);
  150. dsc.pin = stm32_gpio_to_pin(gpio);
  151. return stm32_gpin_get(&dsc);
  152. }
  153. int gpio_set_value(unsigned gpio, int value)
  154. {
  155. struct stm32_gpio_dsc dsc;
  156. dsc.port = stm32_gpio_to_port(gpio);
  157. dsc.pin = stm32_gpio_to_pin(gpio);
  158. return stm32_gpout_set(&dsc, value);
  159. }