mpc83xx_gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Freescale MPC83xx GPIO handling.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <mpc83xx.h>
  24. #include <asm/gpio.h>
  25. #include <asm/io.h>
  26. #ifndef CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION
  27. #define CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION 0
  28. #endif
  29. #ifndef CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION
  30. #define CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION 0
  31. #endif
  32. #ifndef CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN
  33. #define CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN 0
  34. #endif
  35. #ifndef CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN
  36. #define CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN 0
  37. #endif
  38. #ifndef CONFIG_MPC83XX_GPIO_0_INIT_VALUE
  39. #define CONFIG_MPC83XX_GPIO_0_INIT_VALUE 0
  40. #endif
  41. #ifndef CONFIG_MPC83XX_GPIO_1_INIT_VALUE
  42. #define CONFIG_MPC83XX_GPIO_1_INIT_VALUE 0
  43. #endif
  44. static unsigned int gpio_output_value[MPC83XX_GPIO_CTRLRS];
  45. /*
  46. * Generic_GPIO primitives.
  47. */
  48. int gpio_request(unsigned gpio, const char *label)
  49. {
  50. if (gpio >= MAX_NUM_GPIOS)
  51. return -1;
  52. return 0;
  53. }
  54. int gpio_free(unsigned gpio)
  55. {
  56. /* Do not set to input */
  57. return 0;
  58. }
  59. /* set GPIO pin 'gpio' as an input */
  60. int gpio_direction_input(unsigned gpio)
  61. {
  62. immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  63. unsigned int ctrlr;
  64. unsigned int line;
  65. unsigned int line_mask;
  66. /* 32-bits per controller */
  67. ctrlr = gpio >> 5;
  68. line = gpio & (0x1F);
  69. /* Big endian */
  70. line_mask = 1 << (31 - line);
  71. clrbits_be32(&im->gpio[ctrlr].dir, line_mask);
  72. return 0;
  73. }
  74. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  75. int gpio_direction_output(unsigned gpio, int value)
  76. {
  77. immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  78. unsigned int ctrlr;
  79. unsigned int line;
  80. unsigned int line_mask;
  81. if (value != 0 && value != 1) {
  82. printf("Error: Value parameter must be 0 or 1.\n");
  83. return -1;
  84. }
  85. gpio_set_value(gpio, value);
  86. /* 32-bits per controller */
  87. ctrlr = gpio >> 5;
  88. line = gpio & (0x1F);
  89. /* Big endian */
  90. line_mask = 1 << (31 - line);
  91. /* Make the line output */
  92. setbits_be32(&im->gpio[ctrlr].dir, line_mask);
  93. return 0;
  94. }
  95. /* read GPIO IN value of pin 'gpio' */
  96. int gpio_get_value(unsigned gpio)
  97. {
  98. immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  99. unsigned int ctrlr;
  100. unsigned int line;
  101. unsigned int line_mask;
  102. /* 32-bits per controller */
  103. ctrlr = gpio >> 5;
  104. line = gpio & (0x1F);
  105. /* Big endian */
  106. line_mask = 1 << (31 - line);
  107. /* Read the value and mask off the bit */
  108. return (in_be32(&im->gpio[ctrlr].dat) & line_mask) != 0;
  109. }
  110. /* write GPIO OUT value to pin 'gpio' */
  111. int gpio_set_value(unsigned gpio, int value)
  112. {
  113. immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  114. unsigned int ctrlr;
  115. unsigned int line;
  116. unsigned int line_mask;
  117. if (value != 0 && value != 1) {
  118. printf("Error: Value parameter must be 0 or 1.\n");
  119. return -1;
  120. }
  121. /* 32-bits per controller */
  122. ctrlr = gpio >> 5;
  123. line = gpio & (0x1F);
  124. /* Big endian */
  125. line_mask = 1 << (31 - line);
  126. /* Update the local output buffer soft copy */
  127. gpio_output_value[ctrlr] =
  128. (gpio_output_value[ctrlr] & ~line_mask) | \
  129. (value ? line_mask : 0);
  130. /* Write the output */
  131. out_be32(&im->gpio[ctrlr].dat, gpio_output_value[ctrlr]);
  132. return 0;
  133. }
  134. /* Configure GPIO registers early */
  135. void mpc83xx_gpio_init_f()
  136. {
  137. immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  138. #if MPC83XX_GPIO_CTRLRS >= 1
  139. out_be32(&im->gpio[0].dir, CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION);
  140. out_be32(&im->gpio[0].odr, CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN);
  141. out_be32(&im->gpio[0].dat, CONFIG_MPC83XX_GPIO_0_INIT_VALUE);
  142. out_be32(&im->gpio[0].ier, 0xFFFFFFFF); /* Clear all events */
  143. out_be32(&im->gpio[0].imr, 0);
  144. out_be32(&im->gpio[0].icr, 0);
  145. #endif
  146. #if MPC83XX_GPIO_CTRLRS >= 2
  147. out_be32(&im->gpio[1].dir, CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION);
  148. out_be32(&im->gpio[1].odr, CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN);
  149. out_be32(&im->gpio[1].dat, CONFIG_MPC83XX_GPIO_1_INIT_VALUE);
  150. out_be32(&im->gpio[1].ier, 0xFFFFFFFF); /* Clear all events */
  151. out_be32(&im->gpio[1].imr, 0);
  152. out_be32(&im->gpio[1].icr, 0);
  153. #endif
  154. }
  155. /* Initialize GPIO soft-copies */
  156. void mpc83xx_gpio_init_r()
  157. {
  158. #if MPC83XX_GPIO_CTRLRS >= 1
  159. gpio_output_value[0] = CONFIG_MPC83XX_GPIO_0_INIT_VALUE;
  160. #endif
  161. #if MPC83XX_GPIO_CTRLRS >= 2
  162. gpio_output_value[1] = CONFIG_MPC83XX_GPIO_1_INIT_VALUE;
  163. #endif
  164. }