portmux-gpio.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2008 Atmel Corporation
  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. #ifndef __AVR32_PORTMUX_GPIO_H__
  23. #define __AVR32_PORTMUX_GPIO_H__
  24. #include <asm/io.h>
  25. /* Register offsets */
  26. struct gpio_regs {
  27. u32 GPER;
  28. u32 GPERS;
  29. u32 GPERC;
  30. u32 GPERT;
  31. u32 PMR0;
  32. u32 PMR0S;
  33. u32 PMR0C;
  34. u32 PMR0T;
  35. u32 PMR1;
  36. u32 PMR1S;
  37. u32 PMR1C;
  38. u32 PMR1T;
  39. u32 __reserved0[4];
  40. u32 ODER;
  41. u32 ODERS;
  42. u32 ODERC;
  43. u32 ODERT;
  44. u32 OVR;
  45. u32 OVRS;
  46. u32 OVRC;
  47. u32 OVRT;
  48. u32 PVR;
  49. u32 __reserved_PVRS;
  50. u32 __reserved_PVRC;
  51. u32 __reserved_PVRT;
  52. u32 PUER;
  53. u32 PUERS;
  54. u32 PUERC;
  55. u32 PUERT;
  56. u32 PDER;
  57. u32 PDERS;
  58. u32 PDERC;
  59. u32 PDERT;
  60. u32 IER;
  61. u32 IERS;
  62. u32 IERC;
  63. u32 IERT;
  64. u32 IMR0;
  65. u32 IMR0S;
  66. u32 IMR0C;
  67. u32 IMR0T;
  68. u32 IMR1;
  69. u32 IMR1S;
  70. u32 IMR1C;
  71. u32 IMR1T;
  72. u32 GFER;
  73. u32 GFERS;
  74. u32 GFERC;
  75. u32 GFERT;
  76. u32 IFR;
  77. u32 __reserved_IFRS;
  78. u32 IFRC;
  79. u32 __reserved_IFRT;
  80. u32 ODMER;
  81. u32 ODMERS;
  82. u32 ODMERC;
  83. u32 ODMERT;
  84. u32 __reserved1[4];
  85. u32 ODCR0;
  86. u32 ODCR0S;
  87. u32 ODCR0C;
  88. u32 ODCR0T;
  89. u32 ODCR1;
  90. u32 ODCR1S;
  91. u32 ODCR1C;
  92. u32 ODCR1T;
  93. u32 __reserved2[4];
  94. u32 OSRR0;
  95. u32 OSRR0S;
  96. u32 OSRR0C;
  97. u32 OSRR0T;
  98. u32 __reserved3[8];
  99. u32 STER;
  100. u32 STERS;
  101. u32 STERC;
  102. u32 STERT;
  103. u32 __reserved4[35];
  104. u32 VERSION;
  105. };
  106. /* Register access macros */
  107. #define gpio_readl(port, reg) \
  108. __raw_readl(&((struct gpio_regs *)port)->reg)
  109. #define gpio_writel(gpio, reg, value) \
  110. __raw_writel(value, &((struct gpio_regs *)port)->reg)
  111. /* Portmux API starts here. See doc/README.AVR32-port-muxing */
  112. enum portmux_function {
  113. PORTMUX_FUNC_A,
  114. PORTMUX_FUNC_B,
  115. PORTMUX_FUNC_C,
  116. PORTMUX_FUNC_D,
  117. };
  118. #define PORTMUX_DIR_INPUT (0 << 0)
  119. #define PORTMUX_DIR_OUTPUT (1 << 0)
  120. #define PORTMUX_INIT_LOW (0 << 1)
  121. #define PORTMUX_INIT_HIGH (1 << 1)
  122. #define PORTMUX_PULL_UP (1 << 2)
  123. #define PORTMUX_PULL_DOWN (2 << 2)
  124. #define PORTMUX_BUSKEEPER (3 << 2)
  125. #define PORTMUX_DRIVE_MIN (0 << 4)
  126. #define PORTMUX_DRIVE_LOW (1 << 4)
  127. #define PORTMUX_DRIVE_HIGH (2 << 4)
  128. #define PORTMUX_DRIVE_MAX (3 << 4)
  129. #define PORTMUX_OPEN_DRAIN (1 << 6)
  130. void portmux_select_peripheral(void *port, unsigned long pin_mask,
  131. enum portmux_function func, unsigned long flags);
  132. void portmux_select_gpio(void *port, unsigned long pin_mask,
  133. unsigned long flags);
  134. /* Internal helper functions */
  135. static inline void *gpio_pin_to_port(unsigned int pin)
  136. {
  137. return (void *)GPIO_BASE + (pin >> 5) * 0x200;
  138. }
  139. static inline void __gpio_set_output_value(void *port, unsigned int pin,
  140. int value)
  141. {
  142. if (value)
  143. gpio_writel(port, OVRS, 1 << pin);
  144. else
  145. gpio_writel(port, OVRC, 1 << pin);
  146. }
  147. static inline int __gpio_get_input_value(void *port, unsigned int pin)
  148. {
  149. return (gpio_readl(port, PVR) >> pin) & 1;
  150. }
  151. void gpio_set_output_value(unsigned int pin, int value);
  152. int gpio_get_input_value(unsigned int pin);
  153. /* GPIO API starts here */
  154. /*
  155. * GCC doesn't realize that the constant case is extremely trivial,
  156. * so we need to help it make the right decision by using
  157. * always_inline.
  158. */
  159. __attribute__((always_inline))
  160. static inline void gpio_set_value(unsigned int pin, int value)
  161. {
  162. if (__builtin_constant_p(pin))
  163. __gpio_set_output_value(gpio_pin_to_port(pin),
  164. pin & 0x1f, value);
  165. else
  166. gpio_set_output_value(pin, value);
  167. }
  168. __attribute__((always_inline))
  169. static inline int gpio_get_value(unsigned int pin)
  170. {
  171. if (__builtin_constant_p(pin))
  172. return __gpio_get_input_value(gpio_pin_to_port(pin),
  173. pin & 0x1f);
  174. else
  175. return gpio_get_input_value(pin);
  176. }
  177. #endif /* __AVR32_PORTMUX_GPIO_H__ */