gpio.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/processor.h>
  9. #include <asm/io.h>
  10. #include <asm/ppc4xx-gpio.h>
  11. /* Only compile this file for boards with GPIO support */
  12. #if defined(GPIO0_BASE)
  13. #if defined(CONFIG_SYS_4xx_GPIO_TABLE)
  14. gpio_param_s const gpio_tab[GPIO_GROUP_MAX][GPIO_MAX] = CONFIG_SYS_4xx_GPIO_TABLE;
  15. #endif
  16. #if defined(GPIO0_OSRL)
  17. /* Only some 4xx variants support alternate funtions on the GPIO's */
  18. void gpio_config(int pin, int in_out, int gpio_alt, int out_val)
  19. {
  20. u32 mask;
  21. u32 mask2;
  22. u32 val;
  23. u32 offs = 0;
  24. u32 offs2 = 0;
  25. int pin2 = pin << 1;
  26. if (pin >= GPIO_MAX) {
  27. offs = 0x100;
  28. pin -= GPIO_MAX;
  29. }
  30. if (pin >= GPIO_MAX/2) {
  31. offs2 = 0x4;
  32. pin2 = (pin - GPIO_MAX/2) << 1;
  33. }
  34. mask = 0x80000000 >> pin;
  35. mask2 = 0xc0000000 >> pin2;
  36. /* first set TCR to 0 */
  37. out_be32((void *)GPIO0_TCR + offs, in_be32((void *)GPIO0_TCR + offs) & ~mask);
  38. if (in_out == GPIO_OUT) {
  39. val = in_be32((void *)GPIO0_OSRL + offs + offs2) & ~mask2;
  40. switch (gpio_alt) {
  41. case GPIO_ALT1:
  42. val |= GPIO_ALT1_SEL >> pin2;
  43. break;
  44. case GPIO_ALT2:
  45. val |= GPIO_ALT2_SEL >> pin2;
  46. break;
  47. case GPIO_ALT3:
  48. val |= GPIO_ALT3_SEL >> pin2;
  49. break;
  50. }
  51. out_be32((void *)GPIO0_OSRL + offs + offs2, val);
  52. /* setup requested output value */
  53. if (out_val == GPIO_OUT_0)
  54. out_be32((void *)GPIO0_OR + offs,
  55. in_be32((void *)GPIO0_OR + offs) & ~mask);
  56. else if (out_val == GPIO_OUT_1)
  57. out_be32((void *)GPIO0_OR + offs,
  58. in_be32((void *)GPIO0_OR + offs) | mask);
  59. /* now configure TCR to drive output if selected */
  60. out_be32((void *)GPIO0_TCR + offs,
  61. in_be32((void *)GPIO0_TCR + offs) | mask);
  62. } else {
  63. val = in_be32((void *)GPIO0_ISR1L + offs + offs2) & ~mask2;
  64. val |= GPIO_IN_SEL >> pin2;
  65. out_be32((void *)GPIO0_ISR1L + offs + offs2, val);
  66. }
  67. }
  68. #endif /* GPIO_OSRL */
  69. void gpio_write_bit(int pin, int val)
  70. {
  71. u32 offs = 0;
  72. if (pin >= GPIO_MAX) {
  73. offs = 0x100;
  74. pin -= GPIO_MAX;
  75. }
  76. if (val)
  77. out_be32((void *)GPIO0_OR + offs,
  78. in_be32((void *)GPIO0_OR + offs) | GPIO_VAL(pin));
  79. else
  80. out_be32((void *)GPIO0_OR + offs,
  81. in_be32((void *)GPIO0_OR + offs) & ~GPIO_VAL(pin));
  82. }
  83. int gpio_read_out_bit(int pin)
  84. {
  85. u32 offs = 0;
  86. if (pin >= GPIO_MAX) {
  87. offs = 0x100;
  88. pin -= GPIO_MAX;
  89. }
  90. return (in_be32((void *)GPIO0_OR + offs) & GPIO_VAL(pin) ? 1 : 0);
  91. }
  92. int gpio_read_in_bit(int pin)
  93. {
  94. u32 offs = 0;
  95. if (pin >= GPIO_MAX) {
  96. offs = 0x100;
  97. pin -= GPIO_MAX;
  98. }
  99. return (in_be32((void *)GPIO0_IR + offs) & GPIO_VAL(pin) ? 1 : 0);
  100. }
  101. #if defined(CONFIG_SYS_4xx_GPIO_TABLE)
  102. void gpio_set_chip_configuration(void)
  103. {
  104. unsigned char i=0, j=0, offs=0, gpio_core;
  105. unsigned long reg, core_add;
  106. for (gpio_core=0; gpio_core<GPIO_GROUP_MAX; gpio_core++) {
  107. j = 0;
  108. offs = 0;
  109. /* GPIO config of the GPIOs 0 to 31 */
  110. for (i=0; i<GPIO_MAX; i++, j++) {
  111. if (i == GPIO_MAX/2) {
  112. offs = 4;
  113. j = i-16;
  114. }
  115. core_add = gpio_tab[gpio_core][i].add;
  116. if ((gpio_tab[gpio_core][i].in_out == GPIO_IN) ||
  117. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  118. switch (gpio_tab[gpio_core][i].alt_nb) {
  119. case GPIO_SEL:
  120. break;
  121. case GPIO_ALT1:
  122. reg = in_be32((void *)GPIO_IS1(core_add+offs))
  123. & ~(GPIO_MASK >> (j*2));
  124. reg = reg | (GPIO_IN_SEL >> (j*2));
  125. out_be32((void *)GPIO_IS1(core_add+offs), reg);
  126. break;
  127. case GPIO_ALT2:
  128. reg = in_be32((void *)GPIO_IS2(core_add+offs))
  129. & ~(GPIO_MASK >> (j*2));
  130. reg = reg | (GPIO_IN_SEL >> (j*2));
  131. out_be32((void *)GPIO_IS2(core_add+offs), reg);
  132. break;
  133. case GPIO_ALT3:
  134. reg = in_be32((void *)GPIO_IS3(core_add+offs))
  135. & ~(GPIO_MASK >> (j*2));
  136. reg = reg | (GPIO_IN_SEL >> (j*2));
  137. out_be32((void *)GPIO_IS3(core_add+offs), reg);
  138. break;
  139. }
  140. }
  141. if ((gpio_tab[gpio_core][i].in_out == GPIO_OUT) ||
  142. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  143. u32 gpio_alt_sel = 0;
  144. switch (gpio_tab[gpio_core][i].alt_nb) {
  145. case GPIO_SEL:
  146. /*
  147. * Setup output value
  148. * 1 -> high level
  149. * 0 -> low level
  150. * else -> don't touch
  151. */
  152. reg = in_be32((void *)GPIO_OR(core_add));
  153. if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_1)
  154. reg |= (0x80000000 >> (i));
  155. else if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_0)
  156. reg &= ~(0x80000000 >> (i));
  157. out_be32((void *)GPIO_OR(core_add), reg);
  158. reg = in_be32((void *)GPIO_TCR(core_add)) |
  159. (0x80000000 >> (i));
  160. out_be32((void *)GPIO_TCR(core_add), reg);
  161. reg = in_be32((void *)GPIO_OS(core_add+offs))
  162. & ~(GPIO_MASK >> (j*2));
  163. out_be32((void *)GPIO_OS(core_add+offs), reg);
  164. reg = in_be32((void *)GPIO_TS(core_add+offs))
  165. & ~(GPIO_MASK >> (j*2));
  166. out_be32((void *)GPIO_TS(core_add+offs), reg);
  167. break;
  168. case GPIO_ALT1:
  169. gpio_alt_sel = GPIO_ALT1_SEL;
  170. break;
  171. case GPIO_ALT2:
  172. gpio_alt_sel = GPIO_ALT2_SEL;
  173. break;
  174. case GPIO_ALT3:
  175. gpio_alt_sel = GPIO_ALT3_SEL;
  176. break;
  177. }
  178. if (0 != gpio_alt_sel) {
  179. reg = in_be32((void *)GPIO_OS(core_add+offs))
  180. & ~(GPIO_MASK >> (j*2));
  181. reg = reg | (gpio_alt_sel >> (j*2));
  182. out_be32((void *)GPIO_OS(core_add+offs), reg);
  183. if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_1) {
  184. reg = in_be32((void *)GPIO_TCR(core_add))
  185. | (0x80000000 >> (i));
  186. out_be32((void *)GPIO_TCR(core_add), reg);
  187. reg = in_be32((void *)GPIO_TS(core_add+offs))
  188. & ~(GPIO_MASK >> (j*2));
  189. out_be32((void *)GPIO_TS(core_add+offs), reg);
  190. } else {
  191. reg = in_be32((void *)GPIO_TCR(core_add))
  192. & ~(0x80000000 >> (i));
  193. out_be32((void *)GPIO_TCR(core_add), reg);
  194. reg = in_be32((void *)GPIO_TS(core_add+offs))
  195. & ~(GPIO_MASK >> (j*2));
  196. reg = reg | (gpio_alt_sel >> (j*2));
  197. out_be32((void *)GPIO_TS(core_add+offs), reg);
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. #endif /* GPIO0_BASE */
  205. #endif /* CONFIG_SYS_4xx_GPIO_TABLE */