gpio.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Coldfire generic GPIO support
  3. *
  4. * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #ifndef coldfire_gpio_h
  16. #define coldfire_gpio_h
  17. #include <linux/io.h>
  18. #include <asm-generic/gpio.h>
  19. #include <asm/coldfire.h>
  20. #include <asm/mcfsim.h>
  21. /*
  22. * The Freescale Coldfire family is quite varied in how they implement GPIO.
  23. * Some parts have 8 bit ports, some have 16bit and some have 32bit; some have
  24. * only one port, others have multiple ports; some have a single data latch
  25. * for both input and output, others have a separate pin data register to read
  26. * input; some require a read-modify-write access to change an output, others
  27. * have set and clear registers for some of the outputs; Some have all the
  28. * GPIOs in a single control area, others have some GPIOs implemented in
  29. * different modules.
  30. *
  31. * This implementation attempts accommodate the differences while presenting
  32. * a generic interface that will optimize to as few instructions as possible.
  33. */
  34. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  35. defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  36. defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  37. defined(CONFIG_M532x) || defined(CONFIG_M54xx)
  38. /* These parts have GPIO organized by 8 bit ports */
  39. #define MCFGPIO_PORTTYPE u8
  40. #define MCFGPIO_PORTSIZE 8
  41. #define mcfgpio_read(port) __raw_readb(port)
  42. #define mcfgpio_write(data, port) __raw_writeb(data, port)
  43. #elif defined(CONFIG_M5307) || defined(CONFIG_M5407) || defined(CONFIG_M5272)
  44. /* These parts have GPIO organized by 16 bit ports */
  45. #define MCFGPIO_PORTTYPE u16
  46. #define MCFGPIO_PORTSIZE 16
  47. #define mcfgpio_read(port) __raw_readw(port)
  48. #define mcfgpio_write(data, port) __raw_writew(data, port)
  49. #elif defined(CONFIG_M5249)
  50. /* These parts have GPIO organized by 32 bit ports */
  51. #define MCFGPIO_PORTTYPE u32
  52. #define MCFGPIO_PORTSIZE 32
  53. #define mcfgpio_read(port) __raw_readl(port)
  54. #define mcfgpio_write(data, port) __raw_writel(data, port)
  55. #endif
  56. #define mcfgpio_bit(gpio) (1 << ((gpio) % MCFGPIO_PORTSIZE))
  57. #define mcfgpio_port(gpio) ((gpio) / MCFGPIO_PORTSIZE)
  58. #if defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  59. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  60. /*
  61. * These parts have an 'Edge' Port module (external interrupt/GPIO) which uses
  62. * read-modify-write to change an output and a GPIO module which has separate
  63. * set/clr registers to directly change outputs with a single write access.
  64. */
  65. #if defined(CONFIG_M528x)
  66. /*
  67. * The 528x also has GPIOs in other modules (GPT, QADC) which use
  68. * read-modify-write as well as those controlled by the EPORT and GPIO modules.
  69. */
  70. #define MCFGPIO_SCR_START 40
  71. #else
  72. #define MCFGPIO_SCR_START 8
  73. #endif
  74. #define MCFGPIO_SETR_PORT(gpio) (MCFGPIO_SETR + \
  75. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  76. #define MCFGPIO_CLRR_PORT(gpio) (MCFGPIO_CLRR + \
  77. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  78. #else
  79. #define MCFGPIO_SCR_START MCFGPIO_PIN_MAX
  80. /* with MCFGPIO_SCR == MCFGPIO_PIN_MAX, these will be optimized away */
  81. #define MCFGPIO_SETR_PORT(gpio) 0
  82. #define MCFGPIO_CLRR_PORT(gpio) 0
  83. #endif
  84. /*
  85. * Coldfire specific helper functions
  86. */
  87. /* return the port pin data register for a gpio */
  88. static inline u32 __mcf_gpio_ppdr(unsigned gpio)
  89. {
  90. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  91. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  92. return MCFSIM_PADAT;
  93. #elif defined(CONFIG_M5272)
  94. if (gpio < 16)
  95. return MCFSIM_PADAT;
  96. else if (gpio < 32)
  97. return MCFSIM_PBDAT;
  98. else
  99. return MCFSIM_PCDAT;
  100. #elif defined(CONFIG_M5249)
  101. if (gpio < 32)
  102. return MCFSIM2_GPIOREAD;
  103. else
  104. return MCFSIM2_GPIO1READ;
  105. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  106. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  107. if (gpio < 8)
  108. return MCFEPORT_EPPDR;
  109. #if defined(CONFIG_M528x)
  110. else if (gpio < 16)
  111. return MCFGPTA_GPTPORT;
  112. else if (gpio < 24)
  113. return MCFGPTB_GPTPORT;
  114. else if (gpio < 32)
  115. return MCFQADC_PORTQA;
  116. else if (gpio < 40)
  117. return MCFQADC_PORTQB;
  118. #endif
  119. else
  120. return MCFGPIO_PPDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  121. #else
  122. return 0;
  123. #endif
  124. }
  125. /* return the port output data register for a gpio */
  126. static inline u32 __mcf_gpio_podr(unsigned gpio)
  127. {
  128. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  129. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  130. return MCFSIM_PADAT;
  131. #elif defined(CONFIG_M5272)
  132. if (gpio < 16)
  133. return MCFSIM_PADAT;
  134. else if (gpio < 32)
  135. return MCFSIM_PBDAT;
  136. else
  137. return MCFSIM_PCDAT;
  138. #elif defined(CONFIG_M5249)
  139. if (gpio < 32)
  140. return MCFSIM2_GPIOWRITE;
  141. else
  142. return MCFSIM2_GPIO1WRITE;
  143. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  144. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  145. if (gpio < 8)
  146. return MCFEPORT_EPDR;
  147. #if defined(CONFIG_M528x)
  148. else if (gpio < 16)
  149. return MCFGPTA_GPTPORT;
  150. else if (gpio < 24)
  151. return MCFGPTB_GPTPORT;
  152. else if (gpio < 32)
  153. return MCFQADC_PORTQA;
  154. else if (gpio < 40)
  155. return MCFQADC_PORTQB;
  156. #endif
  157. else
  158. return MCFGPIO_PODR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  159. #else
  160. return 0;
  161. #endif
  162. }
  163. /*
  164. * The Generic GPIO functions
  165. *
  166. * If the gpio is a compile time constant and is one of the Coldfire gpios,
  167. * use the inline version, otherwise dispatch thru gpiolib.
  168. */
  169. static inline int gpio_get_value(unsigned gpio)
  170. {
  171. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX)
  172. return mcfgpio_read(__mcf_gpio_ppdr(gpio)) & mcfgpio_bit(gpio);
  173. else
  174. return __gpio_get_value(gpio);
  175. }
  176. static inline void gpio_set_value(unsigned gpio, int value)
  177. {
  178. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX) {
  179. if (gpio < MCFGPIO_SCR_START) {
  180. unsigned long flags;
  181. MCFGPIO_PORTTYPE data;
  182. local_irq_save(flags);
  183. data = mcfgpio_read(__mcf_gpio_podr(gpio));
  184. if (value)
  185. data |= mcfgpio_bit(gpio);
  186. else
  187. data &= ~mcfgpio_bit(gpio);
  188. mcfgpio_write(data, __mcf_gpio_podr(gpio));
  189. local_irq_restore(flags);
  190. } else {
  191. if (value)
  192. mcfgpio_write(mcfgpio_bit(gpio),
  193. MCFGPIO_SETR_PORT(gpio));
  194. else
  195. mcfgpio_write(~mcfgpio_bit(gpio),
  196. MCFGPIO_CLRR_PORT(gpio));
  197. }
  198. } else
  199. __gpio_set_value(gpio, value);
  200. }
  201. static inline int gpio_to_irq(unsigned gpio)
  202. {
  203. return (gpio < MCFGPIO_IRQ_MAX) ? gpio + MCFGPIO_IRQ_VECBASE : -EINVAL;
  204. }
  205. static inline int irq_to_gpio(unsigned irq)
  206. {
  207. return (irq >= MCFGPIO_IRQ_VECBASE &&
  208. irq < (MCFGPIO_IRQ_VECBASE + MCFGPIO_IRQ_MAX)) ?
  209. irq - MCFGPIO_IRQ_VECBASE : -ENXIO;
  210. }
  211. static inline int gpio_cansleep(unsigned gpio)
  212. {
  213. return gpio < MCFGPIO_PIN_MAX ? 0 : __gpio_cansleep(gpio);
  214. }
  215. #endif