gpio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * (C) Copyright 2007-2008
  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 const 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;
  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. int gpio_read_in_bit(int pin)
  107. {
  108. u32 offs = 0;
  109. if (pin >= GPIO_MAX) {
  110. offs = 0x100;
  111. pin -= GPIO_MAX;
  112. }
  113. return (in_be32((void *)GPIO0_IR + offs) & GPIO_VAL(pin) ? 1 : 0);
  114. }
  115. #if defined(CFG_4xx_GPIO_TABLE)
  116. void gpio_set_chip_configuration(void)
  117. {
  118. unsigned char i=0, j=0, offs=0, gpio_core;
  119. unsigned long reg, core_add;
  120. for (gpio_core=0; gpio_core<GPIO_GROUP_MAX; gpio_core++) {
  121. j = 0;
  122. offs = 0;
  123. /* GPIO config of the GPIOs 0 to 31 */
  124. for (i=0; i<GPIO_MAX; i++, j++) {
  125. if (i == GPIO_MAX/2) {
  126. offs = 4;
  127. j = i-16;
  128. }
  129. core_add = gpio_tab[gpio_core][i].add;
  130. if ((gpio_tab[gpio_core][i].in_out == GPIO_IN) ||
  131. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  132. switch (gpio_tab[gpio_core][i].alt_nb) {
  133. case GPIO_SEL:
  134. break;
  135. case GPIO_ALT1:
  136. reg = in_be32((void *)GPIO_IS1(core_add+offs))
  137. & ~(GPIO_MASK >> (j*2));
  138. reg = reg | (GPIO_IN_SEL >> (j*2));
  139. out_be32((void *)GPIO_IS1(core_add+offs), reg);
  140. break;
  141. case GPIO_ALT2:
  142. reg = in_be32((void *)GPIO_IS2(core_add+offs))
  143. & ~(GPIO_MASK >> (j*2));
  144. reg = reg | (GPIO_IN_SEL >> (j*2));
  145. out_be32((void *)GPIO_IS2(core_add+offs), reg);
  146. break;
  147. case GPIO_ALT3:
  148. reg = in_be32((void *)GPIO_IS3(core_add+offs))
  149. & ~(GPIO_MASK >> (j*2));
  150. reg = reg | (GPIO_IN_SEL >> (j*2));
  151. out_be32((void *)GPIO_IS3(core_add+offs), reg);
  152. break;
  153. }
  154. }
  155. if ((gpio_tab[gpio_core][i].in_out == GPIO_OUT) ||
  156. (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
  157. u32 gpio_alt_sel = 0;
  158. switch (gpio_tab[gpio_core][i].alt_nb) {
  159. case GPIO_SEL:
  160. /*
  161. * Setup output value
  162. * 1 -> high level
  163. * 0 -> low level
  164. * else -> don't touch
  165. */
  166. reg = in_be32((void *)GPIO_OR(core_add));
  167. if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_1)
  168. reg |= (0x80000000 >> (i));
  169. else if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_0)
  170. reg &= ~(0x80000000 >> (i));
  171. out_be32((void *)GPIO_OR(core_add), reg);
  172. reg = in_be32((void *)GPIO_TCR(core_add)) |
  173. (0x80000000 >> (i));
  174. out_be32((void *)GPIO_TCR(core_add), reg);
  175. reg = in_be32((void *)GPIO_OS(core_add+offs))
  176. & ~(GPIO_MASK >> (j*2));
  177. out_be32((void *)GPIO_OS(core_add+offs), reg);
  178. reg = in_be32((void *)GPIO_TS(core_add+offs))
  179. & ~(GPIO_MASK >> (j*2));
  180. out_be32((void *)GPIO_TS(core_add+offs), reg);
  181. break;
  182. case GPIO_ALT1:
  183. gpio_alt_sel = GPIO_ALT1_SEL;
  184. break;
  185. case GPIO_ALT2:
  186. gpio_alt_sel = GPIO_ALT2_SEL;
  187. break;
  188. case GPIO_ALT3:
  189. gpio_alt_sel = GPIO_ALT3_SEL;
  190. break;
  191. }
  192. if (0 != gpio_alt_sel) {
  193. reg = in_be32((void *)GPIO_OS(core_add+offs))
  194. & ~(GPIO_MASK >> (j*2));
  195. reg = reg | (gpio_alt_sel >> (j*2));
  196. out_be32((void *)GPIO_OS(core_add+offs), reg);
  197. if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_1) {
  198. reg = in_be32((void *)GPIO_TCR(core_add))
  199. | (0x80000000 >> (i));
  200. out_be32((void *)GPIO_TCR(core_add), reg);
  201. reg = in_be32((void *)GPIO_TS(core_add+offs))
  202. & ~(GPIO_MASK >> (j*2));
  203. out_be32((void *)GPIO_TS(core_add+offs), reg);
  204. } else {
  205. reg = in_be32((void *)GPIO_TCR(core_add))
  206. & ~(0x80000000 >> (i));
  207. out_be32((void *)GPIO_TCR(core_add), reg);
  208. reg = in_be32((void *)GPIO_TS(core_add+offs))
  209. & ~(GPIO_MASK >> (j*2));
  210. reg = reg | (gpio_alt_sel >> (j*2));
  211. out_be32((void *)GPIO_TS(core_add+offs), reg);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. #endif /* CFG_4xx_GPIO_TABLE */