gpio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. int gpio_read_out_bit(int pin)
  93. {
  94. u32 offs = 0;
  95. if (pin >= GPIO_MAX) {
  96. offs = 0x100;
  97. pin -= GPIO_MAX;
  98. }
  99. return (in32(GPIO0_OR + offs) & GPIO_VAL(pin) ? 1 : 0);
  100. }
  101. #if defined(CFG_440_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 = in32(GPIO_IS1(core_add+offs))
  123. & ~(GPIO_MASK >> (j*2));
  124. reg = reg | (GPIO_IN_SEL >> (j*2));
  125. out32(GPIO_IS1(core_add+offs), reg);
  126. break;
  127. case GPIO_ALT2:
  128. reg = in32(GPIO_IS2(core_add+offs))
  129. & ~(GPIO_MASK >> (j*2));
  130. reg = reg | (GPIO_IN_SEL >> (j*2));
  131. out32(GPIO_IS2(core_add+offs), reg);
  132. break;
  133. case GPIO_ALT3:
  134. reg = in32(GPIO_IS3(core_add+offs))
  135. & ~(GPIO_MASK >> (j*2));
  136. reg = reg | (GPIO_IN_SEL >> (j*2));
  137. out32(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. switch (gpio_tab[gpio_core][i].alt_nb) {
  144. case GPIO_SEL:
  145. if (gpio_core == GPIO0) {
  146. /*
  147. * Setup output value
  148. * 1 -> high level
  149. * 0 -> low level
  150. * else -> don't touch
  151. */
  152. reg = in32(GPIO0_OR);
  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. out32(GPIO0_OR, reg);
  158. reg = in32(GPIO0_TCR) | (0x80000000 >> (i));
  159. out32(GPIO0_TCR, reg);
  160. }
  161. #ifdef GPIO1
  162. if (gpio_core == GPIO1) {
  163. /*
  164. * Setup output value
  165. * 1 -> high level
  166. * 0 -> low level
  167. * else -> don't touch
  168. */
  169. reg = in32(GPIO1_OR);
  170. if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_1)
  171. reg |= (0x80000000 >> (i));
  172. else if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_0)
  173. reg &= ~(0x80000000 >> (i));
  174. out32(GPIO1_OR, reg);
  175. reg = in32(GPIO1_TCR) | (0x80000000 >> (i));
  176. out32(GPIO1_TCR, reg);
  177. }
  178. #endif /* GPIO1 */
  179. reg = in32(GPIO_OS(core_add+offs))
  180. & ~(GPIO_MASK >> (j*2));
  181. out32(GPIO_OS(core_add+offs), reg);
  182. reg = in32(GPIO_TS(core_add+offs))
  183. & ~(GPIO_MASK >> (j*2));
  184. out32(GPIO_TS(core_add+offs), reg);
  185. break;
  186. case GPIO_ALT1:
  187. reg = in32(GPIO_OS(core_add+offs))
  188. & ~(GPIO_MASK >> (j*2));
  189. reg = reg | (GPIO_ALT1_SEL >> (j*2));
  190. out32(GPIO_OS(core_add+offs), reg);
  191. reg = in32(GPIO_TS(core_add+offs))
  192. & ~(GPIO_MASK >> (j*2));
  193. reg = reg | (GPIO_ALT1_SEL >> (j*2));
  194. out32(GPIO_TS(core_add+offs), reg);
  195. break;
  196. case GPIO_ALT2:
  197. reg = in32(GPIO_OS(core_add+offs))
  198. & ~(GPIO_MASK >> (j*2));
  199. reg = reg | (GPIO_ALT2_SEL >> (j*2));
  200. out32(GPIO_OS(core_add+offs), reg);
  201. reg = in32(GPIO_TS(core_add+offs))
  202. & ~(GPIO_MASK >> (j*2));
  203. reg = reg | (GPIO_ALT2_SEL >> (j*2));
  204. out32(GPIO_TS(core_add+offs), reg);
  205. break;
  206. case GPIO_ALT3:
  207. reg = in32(GPIO_OS(core_add+offs))
  208. & ~(GPIO_MASK >> (j*2));
  209. reg = reg | (GPIO_ALT3_SEL >> (j*2));
  210. out32(GPIO_OS(core_add+offs), reg);
  211. reg = in32(GPIO_TS(core_add+offs))
  212. & ~(GPIO_MASK >> (j*2));
  213. reg = reg | (GPIO_ALT3_SEL >> (j*2));
  214. out32(GPIO_TS(core_add+offs), reg);
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. #endif /* CFG_440_GPIO_TABLE */