gpio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_4xx_GPIO_TABLE)
  28. gpio_param_s gpio_tab[GPIO_GROUP_MAX][GPIO_MAX] = CFG_4xx_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 = 0x4;
  46. pin2 = (pin - GPIO_MAX/2) << 1;
  47. }
  48. mask = 0x80000000 >> pin;
  49. mask2 = 0xc0000000 >> (pin2 << 1);
  50. /* first set TCR to 0 */
  51. out_be32((void *)GPIO0_TCR + offs, in_be32((void *)GPIO0_TCR + offs) & ~mask);
  52. if (in_out == GPIO_OUT) {
  53. val = in_be32((void *)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. out_be32((void *)GPIO0_OSRL + offs + offs2, val);
  66. /* setup requested output value */
  67. if (out_val == GPIO_OUT_0)
  68. out_be32((void *)GPIO0_OR + offs,
  69. in_be32((void *)GPIO0_OR + offs) & ~mask);
  70. else if (out_val == GPIO_OUT_1)
  71. out_be32((void *)GPIO0_OR + offs,
  72. in_be32((void *)GPIO0_OR + offs) | mask);
  73. /* now configure TCR to drive output if selected */
  74. out_be32((void *)GPIO0_TCR + offs,
  75. in_be32((void *)GPIO0_TCR + offs) | mask);
  76. } else {
  77. val = in_be32((void *)GPIO0_ISR1L + offs + offs2) & ~mask2;
  78. val |= GPIO_IN_SEL >> pin2;
  79. out_be32((void *)GPIO0_ISR1L + offs + offs2, val);
  80. }
  81. }
  82. #endif /* GPIO_OSRL */
  83. void gpio_write_bit(int pin, int val)
  84. {
  85. u32 offs = 0;
  86. if (pin >= GPIO_MAX) {
  87. offs = 0x100;
  88. pin -= GPIO_MAX;
  89. }
  90. if (val)
  91. out_be32((void *)GPIO0_OR + offs,
  92. in_be32((void *)GPIO0_OR + offs) | GPIO_VAL(pin));
  93. else
  94. out_be32((void *)GPIO0_OR + offs,
  95. in_be32((void *)GPIO0_OR + offs) & ~GPIO_VAL(pin));
  96. }
  97. int gpio_read_out_bit(int pin)
  98. {
  99. u32 offs = 0;
  100. if (pin >= GPIO_MAX) {
  101. offs = 0x100;
  102. pin -= GPIO_MAX;
  103. }
  104. return (in_be32((void *)GPIO0_OR + offs) & GPIO_VAL(pin) ? 1 : 0);
  105. }
  106. #if defined(CFG_4xx_GPIO_TABLE)
  107. void gpio_set_chip_configuration(void)
  108. {
  109. unsigned char i=0, j=0, offs=0, gpio_core;
  110. unsigned long reg, core_add;
  111. for (gpio_core=0; gpio_core<GPIO_GROUP_MAX; gpio_core++) {
  112. j = 0;
  113. offs = 0;
  114. /* GPIO config of the GPIOs 0 to 31 */
  115. for (i=0; i<GPIO_MAX; i++, j++) {
  116. if (i == GPIO_MAX/2) {
  117. offs = 4;
  118. j = i-16;
  119. }
  120. core_add = gpio_tab[gpio_core][i].add;
  121. if ((gpio_tab[gpio_core][i].in_out == GPIO_IN) ||
  122. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  123. switch (gpio_tab[gpio_core][i].alt_nb) {
  124. case GPIO_SEL:
  125. break;
  126. case GPIO_ALT1:
  127. reg = in_be32((void *)GPIO_IS1(core_add+offs))
  128. & ~(GPIO_MASK >> (j*2));
  129. reg = reg | (GPIO_IN_SEL >> (j*2));
  130. out_be32((void *)GPIO_IS1(core_add+offs), reg);
  131. break;
  132. case GPIO_ALT2:
  133. reg = in_be32((void *)GPIO_IS2(core_add+offs))
  134. & ~(GPIO_MASK >> (j*2));
  135. reg = reg | (GPIO_IN_SEL >> (j*2));
  136. out_be32((void *)GPIO_IS2(core_add+offs), reg);
  137. break;
  138. case GPIO_ALT3:
  139. reg = in_be32((void *)GPIO_IS3(core_add+offs))
  140. & ~(GPIO_MASK >> (j*2));
  141. reg = reg | (GPIO_IN_SEL >> (j*2));
  142. out_be32((void *)GPIO_IS3(core_add+offs), reg);
  143. break;
  144. }
  145. }
  146. if ((gpio_tab[gpio_core][i].in_out == GPIO_OUT) ||
  147. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  148. switch (gpio_tab[gpio_core][i].alt_nb) {
  149. case GPIO_SEL:
  150. /*
  151. * Setup output value
  152. * 1 -> high level
  153. * 0 -> low level
  154. * else -> don't touch
  155. */
  156. reg = in_be32((void *)GPIO_OR(core_add));
  157. if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_1)
  158. reg |= (0x80000000 >> (i));
  159. else if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_0)
  160. reg &= ~(0x80000000 >> (i));
  161. out_be32((void *)GPIO_OR(core_add), reg);
  162. reg = in_be32((void *)GPIO_TCR(core_add)) |
  163. (0x80000000 >> (i));
  164. out_be32((void *)GPIO_TCR(core_add), reg);
  165. reg = in_be32((void *)GPIO_OS(core_add+offs))
  166. & ~(GPIO_MASK >> (j*2));
  167. out_be32((void *)GPIO_OS(core_add+offs), reg);
  168. reg = in_be32((void *)GPIO_TS(core_add+offs))
  169. & ~(GPIO_MASK >> (j*2));
  170. out_be32((void *)GPIO_TS(core_add+offs), reg);
  171. break;
  172. case GPIO_ALT1:
  173. reg = in_be32((void *)GPIO_OS(core_add+offs))
  174. & ~(GPIO_MASK >> (j*2));
  175. reg = reg | (GPIO_ALT1_SEL >> (j*2));
  176. out_be32((void *)GPIO_OS(core_add+offs), reg);
  177. reg = in_be32((void *)GPIO_TS(core_add+offs))
  178. & ~(GPIO_MASK >> (j*2));
  179. reg = reg | (GPIO_ALT1_SEL >> (j*2));
  180. out_be32((void *)GPIO_TS(core_add+offs), reg);
  181. break;
  182. case GPIO_ALT2:
  183. reg = in_be32((void *)GPIO_OS(core_add+offs))
  184. & ~(GPIO_MASK >> (j*2));
  185. reg = reg | (GPIO_ALT2_SEL >> (j*2));
  186. out_be32((void *)GPIO_OS(core_add+offs), reg);
  187. reg = in_be32((void *)GPIO_TS(core_add+offs))
  188. & ~(GPIO_MASK >> (j*2));
  189. reg = reg | (GPIO_ALT2_SEL >> (j*2));
  190. out_be32((void *)GPIO_TS(core_add+offs), reg);
  191. break;
  192. case GPIO_ALT3:
  193. reg = in_be32((void *)GPIO_OS(core_add+offs))
  194. & ~(GPIO_MASK >> (j*2));
  195. reg = reg | (GPIO_ALT3_SEL >> (j*2));
  196. out_be32((void *)GPIO_OS(core_add+offs), reg);
  197. reg = in_be32((void *)GPIO_TS(core_add+offs))
  198. & ~(GPIO_MASK >> (j*2));
  199. reg = reg | (GPIO_ALT3_SEL >> (j*2));
  200. out_be32((void *)GPIO_TS(core_add+offs), reg);
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. #endif /* CFG_4xx_GPIO_TABLE */