iproc_gpio.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2019-2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <drivers/gpio.h>
  8. #include <lib/mmio.h>
  9. #include <plat/common/platform.h>
  10. #include <iproc_gpio.h>
  11. #include <platform_def.h>
  12. #define IPROC_GPIO_DATA_IN_OFFSET 0x00
  13. #define IPROC_GPIO_DATA_OUT_OFFSET 0x04
  14. #define IPROC_GPIO_OUT_EN_OFFSET 0x08
  15. #define IPROC_GPIO_PAD_RES_OFFSET 0x34
  16. #define IPROC_GPIO_RES_EN_OFFSET 0x38
  17. #define PINMUX_OFFSET(gpio) ((gpio) * 4)
  18. #define PINCONF_OFFSET(gpio) ((gpio) * 4)
  19. #define PINCONF_PULL_UP BIT(4)
  20. #define PINCONF_PULL_DOWN BIT(5)
  21. /*
  22. * iProc GPIO bank is always 0x200 per bank,
  23. * with each bank supporting 32 GPIOs.
  24. */
  25. #define GPIO_BANK_SIZE 0x200
  26. #define NGPIOS_PER_BANK 32
  27. #define GPIO_BANK(pin) ((pin) / NGPIOS_PER_BANK)
  28. #define IPROC_GPIO_REG(pin, reg) (GPIO_BANK(pin) * GPIO_BANK_SIZE + (reg))
  29. #define IPROC_GPIO_SHIFT(pin) ((pin) % NGPIOS_PER_BANK)
  30. #define MUX_GPIO_MODE 0x3
  31. /*
  32. * @base: base address of the gpio controller
  33. * @pinconf_base: base address of the pinconf
  34. * @pinmux_base: base address of the mux controller
  35. * @nr_gpios: maxinum number of GPIOs
  36. */
  37. struct iproc_gpio {
  38. uintptr_t base;
  39. uintptr_t pinconf_base;
  40. uintptr_t pinmux_base;
  41. int nr_gpios;
  42. };
  43. static struct iproc_gpio iproc_gpio;
  44. static void gpio_set_bit(uintptr_t base, unsigned int reg, int gpio, bool set)
  45. {
  46. unsigned int offset = IPROC_GPIO_REG(gpio, reg);
  47. unsigned int shift = IPROC_GPIO_SHIFT(gpio);
  48. uint32_t val;
  49. val = mmio_read_32(base + offset);
  50. if (set)
  51. val |= BIT(shift);
  52. else
  53. val &= ~BIT(shift);
  54. mmio_write_32(base + offset, val);
  55. }
  56. static bool gpio_get_bit(uintptr_t base, unsigned int reg, int gpio)
  57. {
  58. unsigned int offset = IPROC_GPIO_REG(gpio, reg);
  59. unsigned int shift = IPROC_GPIO_SHIFT(gpio);
  60. return !!(mmio_read_32(base + offset) & BIT(shift));
  61. }
  62. static void mux_to_gpio(struct iproc_gpio *g, int gpio)
  63. {
  64. /* mux pad to GPIO if IOPAD configuration is mandatory */
  65. if (g->pinmux_base)
  66. mmio_write_32(g->pinmux_base + PINMUX_OFFSET(gpio),
  67. MUX_GPIO_MODE);
  68. }
  69. static void set_direction(int gpio, int direction)
  70. {
  71. struct iproc_gpio *g = &iproc_gpio;
  72. bool dir = (direction == GPIO_DIR_OUT) ? true : false;
  73. assert(gpio < g->nr_gpios);
  74. mux_to_gpio(g, gpio);
  75. gpio_set_bit(g->base, IPROC_GPIO_OUT_EN_OFFSET, gpio, dir);
  76. }
  77. static int get_direction(int gpio)
  78. {
  79. struct iproc_gpio *g = &iproc_gpio;
  80. int dir;
  81. assert(gpio < g->nr_gpios);
  82. mux_to_gpio(g, gpio);
  83. dir = gpio_get_bit(g->base, IPROC_GPIO_OUT_EN_OFFSET, gpio) ?
  84. GPIO_DIR_OUT : GPIO_DIR_IN;
  85. return dir;
  86. }
  87. static int get_value(int gpio)
  88. {
  89. struct iproc_gpio *g = &iproc_gpio;
  90. unsigned int offset;
  91. assert(gpio < g->nr_gpios);
  92. mux_to_gpio(g, gpio);
  93. /*
  94. * If GPIO is configured as output, read from the GPIO_OUT register;
  95. * otherwise, read from the GPIO_IN register
  96. */
  97. offset = gpio_get_bit(g->base, IPROC_GPIO_OUT_EN_OFFSET, gpio) ?
  98. IPROC_GPIO_DATA_OUT_OFFSET : IPROC_GPIO_DATA_IN_OFFSET;
  99. return gpio_get_bit(g->base, offset, gpio);
  100. }
  101. static void set_value(int gpio, int val)
  102. {
  103. struct iproc_gpio *g = &iproc_gpio;
  104. assert(gpio < g->nr_gpios);
  105. mux_to_gpio(g, gpio);
  106. /* make sure GPIO is configured to output, and then set the value */
  107. gpio_set_bit(g->base, IPROC_GPIO_OUT_EN_OFFSET, gpio, true);
  108. gpio_set_bit(g->base, IPROC_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
  109. }
  110. static int get_pull(int gpio)
  111. {
  112. struct iproc_gpio *g = &iproc_gpio;
  113. uint32_t val;
  114. assert(gpio < g->nr_gpios);
  115. mux_to_gpio(g, gpio);
  116. /* when there's a valid pinconf_base, use it */
  117. if (g->pinconf_base) {
  118. val = mmio_read_32(g->pinconf_base + PINCONF_OFFSET(gpio));
  119. if (val & PINCONF_PULL_UP)
  120. return GPIO_PULL_UP;
  121. else if (val & PINCONF_PULL_DOWN)
  122. return GPIO_PULL_DOWN;
  123. else
  124. return GPIO_PULL_NONE;
  125. }
  126. /* no pinconf_base. fall back to GPIO internal pull control */
  127. if (!gpio_get_bit(g->base, IPROC_GPIO_RES_EN_OFFSET, gpio))
  128. return GPIO_PULL_NONE;
  129. return gpio_get_bit(g->base, IPROC_GPIO_PAD_RES_OFFSET, gpio) ?
  130. GPIO_PULL_UP : GPIO_PULL_DOWN;
  131. }
  132. static void set_pull(int gpio, int pull)
  133. {
  134. struct iproc_gpio *g = &iproc_gpio;
  135. uint32_t val;
  136. assert(gpio < g->nr_gpios);
  137. mux_to_gpio(g, gpio);
  138. /* when there's a valid pinconf_base, use it */
  139. if (g->pinconf_base) {
  140. val = mmio_read_32(g->pinconf_base + PINCONF_OFFSET(gpio));
  141. if (pull == GPIO_PULL_NONE) {
  142. val &= ~(PINCONF_PULL_UP | PINCONF_PULL_DOWN);
  143. } else if (pull == GPIO_PULL_UP) {
  144. val |= PINCONF_PULL_UP;
  145. val &= ~PINCONF_PULL_DOWN;
  146. } else if (pull == GPIO_PULL_DOWN) {
  147. val |= PINCONF_PULL_DOWN;
  148. val &= ~PINCONF_PULL_UP;
  149. } else {
  150. return;
  151. }
  152. mmio_write_32(g->pinconf_base + PINCONF_OFFSET(gpio), val);
  153. }
  154. /* no pinconf_base. fall back to GPIO internal pull control */
  155. if (pull == GPIO_PULL_NONE) {
  156. gpio_set_bit(g->base, IPROC_GPIO_RES_EN_OFFSET, gpio, false);
  157. return;
  158. }
  159. /* enable pad register and pull up or down */
  160. gpio_set_bit(g->base, IPROC_GPIO_RES_EN_OFFSET, gpio, true);
  161. gpio_set_bit(g->base, IPROC_GPIO_PAD_RES_OFFSET, gpio,
  162. !!(pull == GPIO_PULL_UP));
  163. }
  164. const gpio_ops_t iproc_gpio_ops = {
  165. .get_direction = get_direction,
  166. .set_direction = set_direction,
  167. .get_value = get_value,
  168. .set_value = set_value,
  169. .get_pull = get_pull,
  170. .set_pull = set_pull,
  171. };
  172. void iproc_gpio_init(uintptr_t base, int nr_gpios, uintptr_t pinmux_base,
  173. uintptr_t pinconf_base)
  174. {
  175. iproc_gpio.base = base;
  176. iproc_gpio.nr_gpios = nr_gpios;
  177. /* pinmux/pinconf base is optional for some SoCs */
  178. if (pinmux_base)
  179. iproc_gpio.pinmux_base = pinmux_base;
  180. if (pinconf_base)
  181. iproc_gpio.pinconf_base = pinconf_base;
  182. gpio_init(&iproc_gpio_ops);
  183. }