stm32_gpio.c 3.8 KB

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