gpio.c 6.6 KB

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