at91_gpio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /*
  27. * WARNING:
  28. *
  29. * As the code is right now, it expects all PIO ports A,B,C,...
  30. * to be evenly spaced in the memory map:
  31. * ATMEL_BASE_PIOA + port * sizeof at91pio_t
  32. * This might not necessaryly be true in future Atmel SoCs.
  33. * This code should be fixed to use a pointer array to the ports.
  34. */
  35. #include <config.h>
  36. #include <common.h>
  37. #include <asm/sizes.h>
  38. #include <asm/arch/hardware.h>
  39. #include <asm/arch/io.h>
  40. #include <asm/arch/at91_pio.h>
  41. int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
  42. {
  43. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  44. u32 mask;
  45. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  46. mask = 1 << pin;
  47. if (use_pullup)
  48. writel(1 << pin, &pio->port[port].puer);
  49. else
  50. writel(1 << pin, &pio->port[port].pudr);
  51. writel(mask, &pio->port[port].per);
  52. }
  53. return 0;
  54. }
  55. /*
  56. * mux the pin to the "GPIO" peripheral role.
  57. */
  58. int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
  59. {
  60. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  61. u32 mask;
  62. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  63. mask = 1 << pin;
  64. writel(mask, &pio->port[port].idr);
  65. at91_set_pio_pullup(port, pin, use_pullup);
  66. writel(mask, &pio->port[port].per);
  67. }
  68. return 0;
  69. }
  70. /*
  71. * mux the pin to the "A" internal peripheral role.
  72. */
  73. int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
  74. {
  75. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  76. u32 mask;
  77. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  78. mask = 1 << pin;
  79. writel(mask, &pio->port[port].idr);
  80. at91_set_pio_pullup(port, pin, use_pullup);
  81. writel(mask, &pio->port[port].asr);
  82. writel(mask, &pio->port[port].pdr);
  83. }
  84. return 0;
  85. }
  86. /*
  87. * mux the pin to the "B" internal peripheral role.
  88. */
  89. int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
  90. {
  91. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  92. u32 mask;
  93. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  94. mask = 1 << pin;
  95. writel(mask, &pio->port[port].idr);
  96. at91_set_pio_pullup(port, pin, use_pullup);
  97. writel(mask, &pio->port[port].bsr);
  98. writel(mask, &pio->port[port].pdr);
  99. }
  100. return 0;
  101. }
  102. /*
  103. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  104. * configure it for an input.
  105. */
  106. int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
  107. {
  108. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  109. u32 mask;
  110. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  111. mask = 1 << pin;
  112. writel(mask, &pio->port[port].idr);
  113. at91_set_pio_pullup(port, pin, use_pullup);
  114. writel(mask, &pio->port[port].odr);
  115. writel(mask, &pio->port[port].per);
  116. }
  117. return 0;
  118. }
  119. /*
  120. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  121. * and configure it for an output.
  122. */
  123. int at91_set_pio_output(unsigned port, u32 pin, int value)
  124. {
  125. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  126. u32 mask;
  127. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  128. mask = 1 << pin;
  129. writel(mask, &pio->port[port].idr);
  130. writel(mask, &pio->port[port].pudr);
  131. if (value)
  132. writel(mask, &pio->port[port].sodr);
  133. else
  134. writel(mask, &pio->port[port].codr);
  135. writel(mask, &pio->port[port].oer);
  136. writel(mask, &pio->port[port].per);
  137. }
  138. return 0;
  139. }
  140. /*
  141. * enable/disable the glitch filter. mostly used with IRQ handling.
  142. */
  143. int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
  144. {
  145. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  146. u32 mask;
  147. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  148. mask = 1 << pin;
  149. if (is_on)
  150. writel(mask, &pio->port[port].ifer);
  151. else
  152. writel(mask, &pio->port[port].ifdr);
  153. }
  154. return 0;
  155. }
  156. /*
  157. * enable/disable the multi-driver. This is only valid for output and
  158. * allows the output pin to run as an open collector output.
  159. */
  160. int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
  161. {
  162. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  163. u32 mask;
  164. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  165. mask = 1 << pin;
  166. if (is_on)
  167. writel(mask, &pio->port[port].mder);
  168. else
  169. writel(mask, &pio->port[port].mddr);
  170. }
  171. return 0;
  172. }
  173. /*
  174. * assuming the pin is muxed as a gpio output, set its value.
  175. */
  176. int at91_set_pio_value(unsigned port, unsigned pin, int value)
  177. {
  178. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  179. u32 mask;
  180. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  181. mask = 1 << pin;
  182. if (value)
  183. writel(mask, &pio->port[port].sodr);
  184. else
  185. writel(mask, &pio->port[port].codr);
  186. }
  187. return 0;
  188. }
  189. /*
  190. * read the pin's value (works even if it's not muxed as a gpio).
  191. */
  192. int at91_get_pio_value(unsigned port, unsigned pin)
  193. {
  194. u32 pdsr = 0;
  195. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  196. u32 mask;
  197. if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
  198. mask = 1 << pin;
  199. pdsr = readl(&pio->port[port].pdsr) & mask;
  200. }
  201. return pdsr != 0;
  202. }