gpio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * (C) Copyright 2007
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/processor.h>
  25. #include <asm/io.h>
  26. #include <asm/gpio.h>
  27. #if defined(CFG_440_GPIO_TABLE)
  28. gpio_param_s gpio_tab[GPIO_GROUP_MAX][GPIO_MAX] = CFG_440_GPIO_TABLE;
  29. #endif
  30. #if defined(GPIO0_OSRL)
  31. /* Only some 4xx variants support alternate funtions on the GPIO's */
  32. void gpio_config(int pin, int in_out, int gpio_alt, int out_val)
  33. {
  34. u32 mask;
  35. u32 mask2;
  36. u32 val;
  37. u32 offs = 0;
  38. u32 offs2 = 0;
  39. int pin2 = pin << 1;
  40. if (pin >= GPIO_MAX) {
  41. offs = 0x100;
  42. pin -= GPIO_MAX;
  43. }
  44. if (pin >= GPIO_MAX/2) {
  45. offs2 = 0x100;
  46. pin2 = (pin - GPIO_MAX/2) << 1;
  47. }
  48. mask = 0x80000000 >> pin;
  49. mask2 = 0xc0000000 >> (pin2 << 1);
  50. /* first set TCR to 0 */
  51. out32(GPIO0_TCR + offs, in32(GPIO0_TCR + offs) & ~mask);
  52. if (in_out == GPIO_OUT) {
  53. val = in32(GPIO0_OSRL + offs + offs2) & ~mask2;
  54. switch (gpio_alt) {
  55. case GPIO_ALT1:
  56. val |= GPIO_ALT1_SEL >> pin2;
  57. break;
  58. case GPIO_ALT2:
  59. val |= GPIO_ALT2_SEL >> pin2;
  60. break;
  61. case GPIO_ALT3:
  62. val |= GPIO_ALT3_SEL >> pin2;
  63. break;
  64. }
  65. out32(GPIO0_OSRL + offs + offs2, val);
  66. /* setup requested output value */
  67. if (out_val == GPIO_OUT_0)
  68. out32(GPIO0_OR + offs, in32(GPIO0_OR + offs) & ~mask);
  69. else if (out_val == GPIO_OUT_1)
  70. out32(GPIO0_OR + offs, in32(GPIO0_OR + offs) | mask);
  71. /* now configure TCR to drive output if selected */
  72. out32(GPIO0_TCR + offs, in32(GPIO0_TCR + offs) | mask);
  73. } else {
  74. val = in32(GPIO0_ISR1L + offs + offs2) & ~mask2;
  75. val |= GPIO_IN_SEL >> pin2;
  76. out32(GPIO0_ISR1L + offs + offs2, val);
  77. }
  78. }
  79. #endif /* GPIO_OSRL */
  80. void gpio_write_bit(int pin, int val)
  81. {
  82. u32 offs = 0;
  83. if (pin >= GPIO_MAX) {
  84. offs = 0x100;
  85. pin -= GPIO_MAX;
  86. }
  87. if (val)
  88. out32(GPIO0_OR + offs, in32(GPIO0_OR + offs) | GPIO_VAL(pin));
  89. else
  90. out32(GPIO0_OR + offs, in32(GPIO0_OR + offs) & ~GPIO_VAL(pin));
  91. }
  92. #if defined(CFG_440_GPIO_TABLE)
  93. void gpio_set_chip_configuration(void)
  94. {
  95. unsigned char i=0, j=0, offs=0, gpio_core;
  96. unsigned long reg, core_add;
  97. for (gpio_core=0; gpio_core<GPIO_GROUP_MAX; gpio_core++) {
  98. j = 0;
  99. offs = 0;
  100. /* GPIO config of the GPIOs 0 to 31 */
  101. for (i=0; i<GPIO_MAX; i++, j++) {
  102. if (i == GPIO_MAX/2) {
  103. offs = 4;
  104. j = i-16;
  105. }
  106. core_add = gpio_tab[gpio_core][i].add;
  107. if ((gpio_tab[gpio_core][i].in_out == GPIO_IN) ||
  108. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  109. switch (gpio_tab[gpio_core][i].alt_nb) {
  110. case GPIO_SEL:
  111. break;
  112. case GPIO_ALT1:
  113. reg = in32(GPIO_IS1(core_add+offs))
  114. & ~(GPIO_MASK >> (j*2));
  115. reg = reg | (GPIO_IN_SEL >> (j*2));
  116. out32(GPIO_IS1(core_add+offs), reg);
  117. break;
  118. case GPIO_ALT2:
  119. reg = in32(GPIO_IS2(core_add+offs))
  120. & ~(GPIO_MASK >> (j*2));
  121. reg = reg | (GPIO_IN_SEL >> (j*2));
  122. out32(GPIO_IS2(core_add+offs), reg);
  123. break;
  124. case GPIO_ALT3:
  125. reg = in32(GPIO_IS3(core_add+offs))
  126. & ~(GPIO_MASK >> (j*2));
  127. reg = reg | (GPIO_IN_SEL >> (j*2));
  128. out32(GPIO_IS3(core_add+offs), reg);
  129. break;
  130. }
  131. }
  132. if ((gpio_tab[gpio_core][i].in_out == GPIO_OUT) ||
  133. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  134. switch (gpio_tab[gpio_core][i].alt_nb) {
  135. case GPIO_SEL:
  136. if (gpio_core == GPIO0) {
  137. reg = in32(GPIO0_TCR) | (0x80000000 >> (j));
  138. out32(GPIO0_TCR, reg);
  139. }
  140. if (gpio_core == GPIO1) {
  141. reg = in32(GPIO1_TCR) | (0x80000000 >> (j));
  142. out32(GPIO1_TCR, reg);
  143. }
  144. reg = in32(GPIO_OS(core_add+offs))
  145. & ~(GPIO_MASK >> (j*2));
  146. out32(GPIO_OS(core_add+offs), reg);
  147. reg = in32(GPIO_TS(core_add+offs))
  148. & ~(GPIO_MASK >> (j*2));
  149. out32(GPIO_TS(core_add+offs), reg);
  150. break;
  151. case GPIO_ALT1:
  152. reg = in32(GPIO_OS(core_add+offs))
  153. & ~(GPIO_MASK >> (j*2));
  154. reg = reg | (GPIO_ALT1_SEL >> (j*2));
  155. out32(GPIO_OS(core_add+offs), reg);
  156. reg = in32(GPIO_TS(core_add+offs))
  157. & ~(GPIO_MASK >> (j*2));
  158. reg = reg | (GPIO_ALT1_SEL >> (j*2));
  159. out32(GPIO_TS(core_add+offs), reg);
  160. break;
  161. case GPIO_ALT2:
  162. reg = in32(GPIO_OS(core_add+offs))
  163. & ~(GPIO_MASK >> (j*2));
  164. reg = reg | (GPIO_ALT2_SEL >> (j*2));
  165. out32(GPIO_OS(core_add+offs), reg);
  166. reg = in32(GPIO_TS(core_add+offs))
  167. & ~(GPIO_MASK >> (j*2));
  168. reg = reg | (GPIO_ALT2_SEL >> (j*2));
  169. out32(GPIO_TS(core_add+offs), reg);
  170. break;
  171. case GPIO_ALT3:
  172. reg = in32(GPIO_OS(core_add+offs))
  173. & ~(GPIO_MASK >> (j*2));
  174. reg = reg | (GPIO_ALT3_SEL >> (j*2));
  175. out32(GPIO_OS(core_add+offs), reg);
  176. reg = in32(GPIO_TS(core_add+offs))
  177. & ~(GPIO_MASK >> (j*2));
  178. reg = reg | (GPIO_ALT3_SEL >> (j*2));
  179. out32(GPIO_TS(core_add+offs), reg);
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. #endif /* CFG_440_GPIO_TABLE */