gpio.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 accomodate 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) || defined(CONFIG_M532x)
  37. /* These parts have GPIO organized by 8 bit ports */
  38. #define MCFGPIO_PORTTYPE u8
  39. #define MCFGPIO_PORTSIZE 8
  40. #define mcfgpio_read(port) __raw_readb(port)
  41. #define mcfgpio_write(data, port) __raw_writeb(data, port)
  42. #elif defined(CONFIG_M5307) || defined(CONFIG_M5407) || defined(CONFIG_M5272)
  43. /* These parts have GPIO organized by 16 bit ports */
  44. #define MCFGPIO_PORTTYPE u16
  45. #define MCFGPIO_PORTSIZE 16
  46. #define mcfgpio_read(port) __raw_readw(port)
  47. #define mcfgpio_write(data, port) __raw_writew(data, port)
  48. #elif defined(CONFIG_M5249)
  49. /* These parts have GPIO organized by 32 bit ports */
  50. #define MCFGPIO_PORTTYPE u32
  51. #define MCFGPIO_PORTSIZE 32
  52. #define mcfgpio_read(port) __raw_readl(port)
  53. #define mcfgpio_write(data, port) __raw_writel(data, port)
  54. #endif
  55. #define mcfgpio_bit(gpio) (1 << ((gpio) % MCFGPIO_PORTSIZE))
  56. #define mcfgpio_port(gpio) ((gpio) / MCFGPIO_PORTSIZE)
  57. #if defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  58. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  59. /*
  60. * These parts have an 'Edge' Port module (external interrupt/GPIO) which uses
  61. * read-modify-write to change an output and a GPIO module which has separate
  62. * set/clr registers to directly change outputs with a single write access.
  63. */
  64. #if defined(CONFIG_M528x)
  65. /*
  66. * The 528x also has GPIOs in other modules (GPT, QADC) which use
  67. * read-modify-write as well as those controlled by the EPORT and GPIO modules.
  68. */
  69. #define MCFGPIO_SCR_START 40
  70. #else
  71. #define MCFGPIO_SCR_START 8
  72. #endif
  73. #define MCFGPIO_SETR_PORT(gpio) (MCFGPIO_SETR + \
  74. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  75. #define MCFGPIO_CLRR_PORT(gpio) (MCFGPIO_CLRR + \
  76. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  77. #else
  78. #define MCFGPIO_SCR_START MCFGPIO_PIN_MAX
  79. /* with MCFGPIO_SCR == MCFGPIO_PIN_MAX, these will be optimized away */
  80. #define MCFGPIO_SETR_PORT(gpio) 0
  81. #define MCFGPIO_CLRR_PORT(gpio) 0
  82. #endif
  83. /*
  84. * Coldfire specific helper functions
  85. */
  86. /* return the port pin data register for a gpio */
  87. static inline u32 __mcf_gpio_ppdr(unsigned gpio)
  88. {
  89. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  90. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  91. return MCFSIM_PADAT;
  92. #elif defined(CONFIG_M5272)
  93. if (gpio < 16)
  94. return MCFSIM_PADAT;
  95. else if (gpio < 32)
  96. return MCFSIM_PBDAT;
  97. else
  98. return MCFSIM_PCDAT;
  99. #elif defined(CONFIG_M5249)
  100. if (gpio < 32)
  101. return MCFSIM2_GPIOREAD;
  102. else
  103. return MCFSIM2_GPIO1READ;
  104. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  105. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  106. if (gpio < 8)
  107. return MCFEPORT_EPPDR;
  108. #if defined(CONFIG_M528x)
  109. else if (gpio < 16)
  110. return MCFGPTA_GPTPORT;
  111. else if (gpio < 24)
  112. return MCFGPTB_GPTPORT;
  113. else if (gpio < 32)
  114. return MCFQADC_PORTQA;
  115. else if (gpio < 40)
  116. return MCFQADC_PORTQB;
  117. #endif
  118. else
  119. return MCFGPIO_PPDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  120. #endif
  121. }
  122. /* return the port output data register for a gpio */
  123. static inline u32 __mcf_gpio_podr(unsigned gpio)
  124. {
  125. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  126. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  127. return MCFSIM_PADAT;
  128. #elif defined(CONFIG_M5272)
  129. if (gpio < 16)
  130. return MCFSIM_PADAT;
  131. else if (gpio < 32)
  132. return MCFSIM_PBDAT;
  133. else
  134. return MCFSIM_PCDAT;
  135. #elif defined(CONFIG_M5249)
  136. if (gpio < 32)
  137. return MCFSIM2_GPIOWRITE;
  138. else
  139. return MCFSIM2_GPIO1WRITE;
  140. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  141. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  142. if (gpio < 8)
  143. return MCFEPORT_EPDR;
  144. #if defined(CONFIG_M528x)
  145. else if (gpio < 16)
  146. return MCFGPTA_GPTPORT;
  147. else if (gpio < 24)
  148. return MCFGPTB_GPTPORT;
  149. else if (gpio < 32)
  150. return MCFQADC_PORTQA;
  151. else if (gpio < 40)
  152. return MCFQADC_PORTQB;
  153. #endif
  154. else
  155. return MCFGPIO_PODR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  156. #endif
  157. }
  158. /*
  159. * The Generic GPIO functions
  160. *
  161. * If the gpio is a compile time constant and is one of the Coldfire gpios,
  162. * use the inline version, otherwise dispatch thru gpiolib.
  163. */
  164. static inline int gpio_get_value(unsigned gpio)
  165. {
  166. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX)
  167. return mcfgpio_read(__mcf_gpio_ppdr(gpio)) & mcfgpio_bit(gpio);
  168. else
  169. return __gpio_get_value(gpio);
  170. }
  171. static inline void gpio_set_value(unsigned gpio, int value)
  172. {
  173. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX) {
  174. if (gpio < MCFGPIO_SCR_START) {
  175. unsigned long flags;
  176. MCFGPIO_PORTTYPE data;
  177. local_irq_save(flags);
  178. data = mcfgpio_read(__mcf_gpio_podr(gpio));
  179. if (value)
  180. data |= mcfgpio_bit(gpio);
  181. else
  182. data &= ~mcfgpio_bit(gpio);
  183. mcfgpio_write(data, __mcf_gpio_podr(gpio));
  184. local_irq_restore(flags);
  185. } else {
  186. if (value)
  187. mcfgpio_write(mcfgpio_bit(gpio),
  188. MCFGPIO_SETR_PORT(gpio));
  189. else
  190. mcfgpio_write(~mcfgpio_bit(gpio),
  191. MCFGPIO_CLRR_PORT(gpio));
  192. }
  193. } else
  194. __gpio_set_value(gpio, value);
  195. }
  196. static inline int gpio_to_irq(unsigned gpio)
  197. {
  198. return (gpio < MCFGPIO_IRQ_MAX) ? gpio + MCFGPIO_IRQ_VECBASE : -EINVAL;
  199. }
  200. static inline int irq_to_gpio(unsigned irq)
  201. {
  202. return (irq >= MCFGPIO_IRQ_VECBASE &&
  203. irq < (MCFGPIO_IRQ_VECBASE + MCFGPIO_IRQ_MAX)) ?
  204. irq - MCFGPIO_IRQ_VECBASE : -ENXIO;
  205. }
  206. static inline int gpio_cansleep(unsigned gpio)
  207. {
  208. return gpio < MCFGPIO_PIN_MAX ? 0 : __gpio_cansleep(gpio);
  209. }
  210. #endif