at91_gpio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Memory Setup stuff - taken from blob memsetup.S
  3. *
  4. * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
  5. *
  6. * Copyright (C) 2005 HP Labs
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <config.h>
  27. #include <common.h>
  28. #include <asm/sizes.h>
  29. #include <asm/arch/hardware.h>
  30. #include <asm/arch/io.h>
  31. #include <asm/arch/at91_pio.h>
  32. int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
  33. {
  34. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  35. u32 mask;
  36. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  37. mask = 1 << pin;
  38. if (use_pullup)
  39. writel(1 << pin, &pio->port[port].puer);
  40. else
  41. writel(1 << pin, &pio->port[port].pudr);
  42. writel(mask, &pio->port[port].per);
  43. }
  44. return 0;
  45. }
  46. /*
  47. * mux the pin to the "GPIO" peripheral role.
  48. */
  49. int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
  50. {
  51. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  52. u32 mask;
  53. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  54. mask = 1 << pin;
  55. writel(mask, &pio->port[port].idr);
  56. at91_set_pio_pullup(port, pin, use_pullup);
  57. writel(mask, &pio->port[port].per);
  58. }
  59. return 0;
  60. }
  61. /*
  62. * mux the pin to the "A" internal peripheral role.
  63. */
  64. int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
  65. {
  66. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  67. u32 mask;
  68. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  69. mask = 1 << pin;
  70. writel(mask, &pio->port[port].idr);
  71. at91_set_pio_pullup(port, pin, use_pullup);
  72. writel(mask, &pio->port[port].asr);
  73. writel(mask, &pio->port[port].pdr);
  74. }
  75. return 0;
  76. }
  77. /*
  78. * mux the pin to the "B" internal peripheral role.
  79. */
  80. int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
  81. {
  82. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  83. u32 mask;
  84. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  85. mask = 1 << pin;
  86. writel(mask, &pio->port[port].idr);
  87. at91_set_pio_pullup(port, pin, use_pullup);
  88. writel(mask, &pio->port[port].bsr);
  89. writel(mask, &pio->port[port].pdr);
  90. }
  91. return 0;
  92. }
  93. /*
  94. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  95. * configure it for an input.
  96. */
  97. int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
  98. {
  99. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  100. u32 mask;
  101. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  102. mask = 1 << pin;
  103. writel(mask, &pio->port[port].idr);
  104. at91_set_pio_pullup(port, pin, use_pullup);
  105. writel(mask, &pio->port[port].odr);
  106. writel(mask, &pio->port[port].per);
  107. }
  108. return 0;
  109. }
  110. /*
  111. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  112. * and configure it for an output.
  113. */
  114. int at91_set_pio_output(unsigned port, u32 pin, int value)
  115. {
  116. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  117. u32 mask;
  118. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  119. mask = 1 << pin;
  120. writel(mask, &pio->port[port].idr);
  121. writel(mask, &pio->port[port].pudr);
  122. if (value)
  123. writel(mask, &pio->port[port].sodr);
  124. else
  125. writel(mask, &pio->port[port].codr);
  126. writel(mask, &pio->port[port].oer);
  127. writel(mask, &pio->port[port].per);
  128. }
  129. return 0;
  130. }
  131. /*
  132. * enable/disable the glitch filter. mostly used with IRQ handling.
  133. */
  134. int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
  135. {
  136. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  137. u32 mask;
  138. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  139. mask = 1 << pin;
  140. if (is_on)
  141. writel(mask, &pio->port[port].ifer);
  142. else
  143. writel(mask, &pio->port[port].ifdr);
  144. }
  145. return 0;
  146. }
  147. /*
  148. * enable/disable the multi-driver. This is only valid for output and
  149. * allows the output pin to run as an open collector output.
  150. */
  151. int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
  152. {
  153. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  154. u32 mask;
  155. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  156. mask = 1 << pin;
  157. if (is_on)
  158. writel(mask, &pio->port[port].mder);
  159. else
  160. writel(mask, &pio->port[port].mddr);
  161. }
  162. return 0;
  163. }
  164. /*
  165. * assuming the pin is muxed as a gpio output, set its value.
  166. */
  167. int at91_set_pio_value(unsigned port, unsigned pin, int value)
  168. {
  169. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  170. u32 mask;
  171. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  172. mask = 1 << pin;
  173. if (value)
  174. writel(mask, &pio->port[port].sodr);
  175. else
  176. writel(mask, &pio->port[port].codr);
  177. }
  178. return 0;
  179. }
  180. /*
  181. * read the pin's value (works even if it's not muxed as a gpio).
  182. */
  183. int at91_get_pio_value(unsigned port, unsigned pin)
  184. {
  185. u32 pdsr = 0;
  186. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  187. u32 mask;
  188. if ((port < AT91_PIO_PORTS) && (pin < 32)) {
  189. mask = 1 << pin;
  190. pdsr = readl(&pio->port[port].pdsr) & mask;
  191. }
  192. return pdsr != 0;
  193. }