portmux-pio.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2006, 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_PIO_H__
  23. #define __AVR32_PORTMUX_PIO_H__
  24. #include <asm/io.h>
  25. /* PIO register offsets */
  26. #define PIO_PER 0x0000
  27. #define PIO_PDR 0x0004
  28. #define PIO_PSR 0x0008
  29. #define PIO_OER 0x0010
  30. #define PIO_ODR 0x0014
  31. #define PIO_OSR 0x0018
  32. #define PIO_IFER 0x0020
  33. #define PIO_IFDR 0x0024
  34. #define PIO_ISFR 0x0028
  35. #define PIO_SODR 0x0030
  36. #define PIO_CODR 0x0034
  37. #define PIO_ODSR 0x0038
  38. #define PIO_PDSR 0x003c
  39. #define PIO_IER 0x0040
  40. #define PIO_IDR 0x0044
  41. #define PIO_IMR 0x0048
  42. #define PIO_ISR 0x004c
  43. #define PIO_MDER 0x0050
  44. #define PIO_MDDR 0x0054
  45. #define PIO_MDSR 0x0058
  46. #define PIO_PUDR 0x0060
  47. #define PIO_PUER 0x0064
  48. #define PIO_PUSR 0x0068
  49. #define PIO_ASR 0x0070
  50. #define PIO_BSR 0x0074
  51. #define PIO_ABSR 0x0078
  52. #define PIO_OWER 0x00a0
  53. #define PIO_OWDR 0x00a4
  54. #define PIO_OWSR 0x00a8
  55. /* Hardware register access */
  56. #define pio_readl(base, reg) \
  57. __raw_readl((void *)base + PIO_##reg)
  58. #define pio_writel(base, reg, value) \
  59. __raw_writel((value), (void *)base + PIO_##reg)
  60. /* Portmux API starts here. See doc/README.AVR32-port-muxing */
  61. enum portmux_function {
  62. PORTMUX_FUNC_A,
  63. PORTMUX_FUNC_B,
  64. };
  65. /* Pull-down, buskeeper and drive strength are not supported */
  66. #define PORTMUX_DIR_INPUT (0 << 0)
  67. #define PORTMUX_DIR_OUTPUT (1 << 0)
  68. #define PORTMUX_INIT_LOW (0 << 1)
  69. #define PORTMUX_INIT_HIGH (1 << 1)
  70. #define PORTMUX_PULL_UP (1 << 2)
  71. #define PORTMUX_PULL_DOWN (0)
  72. #define PORTMUX_BUSKEEPER PORTMUX_PULL_UP
  73. #define PORTMUX_DRIVE_MIN (0)
  74. #define PORTMUX_DRIVE_LOW (0)
  75. #define PORTMUX_DRIVE_HIGH (0)
  76. #define PORTMUX_DRIVE_MAX (0)
  77. #define PORTMUX_OPEN_DRAIN (1 << 3)
  78. void portmux_select_peripheral(void *port, unsigned long pin_mask,
  79. enum portmux_function func, unsigned long flags);
  80. void portmux_select_gpio(void *port, unsigned long pin_mask,
  81. unsigned long flags);
  82. /* Internal helper functions */
  83. static inline void __pio_set_output_value(void *port, unsigned int pin,
  84. int value)
  85. {
  86. /*
  87. * value will usually be constant, but it's pretty cheap
  88. * either way.
  89. */
  90. if (value)
  91. pio_writel(port, SODR, 1 << pin);
  92. else
  93. pio_writel(port, CODR, 1 << pin);
  94. }
  95. static inline int __pio_get_input_value(void *port, unsigned int pin)
  96. {
  97. return (pio_readl(port, PDSR) >> pin) & 1;
  98. }
  99. void pio_set_output_value(unsigned int pin, int value);
  100. int pio_get_input_value(unsigned int pin);
  101. /* GPIO API starts here */
  102. /*
  103. * GCC doesn't realize that the constant case is extremely trivial,
  104. * so we need to help it make the right decision by using
  105. * always_inline.
  106. */
  107. __attribute__((always_inline))
  108. static inline void gpio_set_value(unsigned int pin, int value)
  109. {
  110. if (__builtin_constant_p(pin))
  111. __pio_set_output_value(pio_pin_to_port(pin), pin & 0x1f, value);
  112. else
  113. pio_set_output_value(pin, value);
  114. }
  115. __attribute__((always_inline))
  116. static inline int gpio_get_value(unsigned int pin)
  117. {
  118. if (__builtin_constant_p(pin))
  119. return __pio_get_input_value(pio_pin_to_port(pin), pin & 0x1f);
  120. else
  121. return pio_get_input_value(pin);
  122. }
  123. #endif /* __AVR32_PORTMUX_PIO_H__ */