mcfgpio.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 mcfgpio_h
  16. #define mcfgpio_h
  17. #ifdef CONFIG_GPIOLIB
  18. #include <asm-generic/gpio.h>
  19. #else
  20. int __mcfgpio_get_value(unsigned gpio);
  21. void __mcfgpio_set_value(unsigned gpio, int value);
  22. int __mcfgpio_direction_input(unsigned gpio);
  23. int __mcfgpio_direction_output(unsigned gpio, int value);
  24. int __mcfgpio_request(unsigned gpio);
  25. void __mcfgpio_free(unsigned gpio);
  26. /* our alternate 'gpiolib' functions */
  27. static inline int __gpio_get_value(unsigned gpio)
  28. {
  29. if (gpio < MCFGPIO_PIN_MAX)
  30. return __mcfgpio_get_value(gpio);
  31. else
  32. return -EINVAL;
  33. }
  34. static inline void __gpio_set_value(unsigned gpio, int value)
  35. {
  36. if (gpio < MCFGPIO_PIN_MAX)
  37. __mcfgpio_set_value(gpio, value);
  38. }
  39. static inline int __gpio_cansleep(unsigned gpio)
  40. {
  41. if (gpio < MCFGPIO_PIN_MAX)
  42. return 0;
  43. else
  44. return -EINVAL;
  45. }
  46. static inline int __gpio_to_irq(unsigned gpio)
  47. {
  48. return -EINVAL;
  49. }
  50. static inline int gpio_direction_input(unsigned gpio)
  51. {
  52. if (gpio < MCFGPIO_PIN_MAX)
  53. return __mcfgpio_direction_input(gpio);
  54. else
  55. return -EINVAL;
  56. }
  57. static inline int gpio_direction_output(unsigned gpio, int value)
  58. {
  59. if (gpio < MCFGPIO_PIN_MAX)
  60. return __mcfgpio_direction_output(gpio, value);
  61. else
  62. return -EINVAL;
  63. }
  64. static inline int gpio_request(unsigned gpio, const char *label)
  65. {
  66. if (gpio < MCFGPIO_PIN_MAX)
  67. return __mcfgpio_request(gpio);
  68. else
  69. return -EINVAL;
  70. }
  71. static inline void gpio_free(unsigned gpio)
  72. {
  73. if (gpio < MCFGPIO_PIN_MAX)
  74. __mcfgpio_free(gpio);
  75. }
  76. #endif /* CONFIG_GPIOLIB */
  77. /*
  78. * The Freescale Coldfire family is quite varied in how they implement GPIO.
  79. * Some parts have 8 bit ports, some have 16bit and some have 32bit; some have
  80. * only one port, others have multiple ports; some have a single data latch
  81. * for both input and output, others have a separate pin data register to read
  82. * input; some require a read-modify-write access to change an output, others
  83. * have set and clear registers for some of the outputs; Some have all the
  84. * GPIOs in a single control area, others have some GPIOs implemented in
  85. * different modules.
  86. *
  87. * This implementation attempts accommodate the differences while presenting
  88. * a generic interface that will optimize to as few instructions as possible.
  89. */
  90. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  91. defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  92. defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  93. defined(CONFIG_M532x) || defined(CONFIG_M54xx)
  94. /* These parts have GPIO organized by 8 bit ports */
  95. #define MCFGPIO_PORTTYPE u8
  96. #define MCFGPIO_PORTSIZE 8
  97. #define mcfgpio_read(port) __raw_readb(port)
  98. #define mcfgpio_write(data, port) __raw_writeb(data, port)
  99. #elif defined(CONFIG_M5307) || defined(CONFIG_M5407) || defined(CONFIG_M5272)
  100. /* These parts have GPIO organized by 16 bit ports */
  101. #define MCFGPIO_PORTTYPE u16
  102. #define MCFGPIO_PORTSIZE 16
  103. #define mcfgpio_read(port) __raw_readw(port)
  104. #define mcfgpio_write(data, port) __raw_writew(data, port)
  105. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  106. /* These parts have GPIO organized by 32 bit ports */
  107. #define MCFGPIO_PORTTYPE u32
  108. #define MCFGPIO_PORTSIZE 32
  109. #define mcfgpio_read(port) __raw_readl(port)
  110. #define mcfgpio_write(data, port) __raw_writel(data, port)
  111. #endif
  112. #define mcfgpio_bit(gpio) (1 << ((gpio) % MCFGPIO_PORTSIZE))
  113. #define mcfgpio_port(gpio) ((gpio) / MCFGPIO_PORTSIZE)
  114. #if defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  115. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  116. /*
  117. * These parts have an 'Edge' Port module (external interrupt/GPIO) which uses
  118. * read-modify-write to change an output and a GPIO module which has separate
  119. * set/clr registers to directly change outputs with a single write access.
  120. */
  121. #if defined(CONFIG_M528x)
  122. /*
  123. * The 528x also has GPIOs in other modules (GPT, QADC) which use
  124. * read-modify-write as well as those controlled by the EPORT and GPIO modules.
  125. */
  126. #define MCFGPIO_SCR_START 40
  127. #else
  128. #define MCFGPIO_SCR_START 8
  129. #endif
  130. #define MCFGPIO_SETR_PORT(gpio) (MCFGPIO_SETR + \
  131. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  132. #define MCFGPIO_CLRR_PORT(gpio) (MCFGPIO_CLRR + \
  133. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  134. #else
  135. #define MCFGPIO_SCR_START MCFGPIO_PIN_MAX
  136. /* with MCFGPIO_SCR == MCFGPIO_PIN_MAX, these will be optimized away */
  137. #define MCFGPIO_SETR_PORT(gpio) 0
  138. #define MCFGPIO_CLRR_PORT(gpio) 0
  139. #endif
  140. /*
  141. * Coldfire specific helper functions
  142. */
  143. /* return the port pin data register for a gpio */
  144. static inline u32 __mcfgpio_ppdr(unsigned gpio)
  145. {
  146. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  147. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  148. return MCFSIM_PADAT;
  149. #elif defined(CONFIG_M5272)
  150. if (gpio < 16)
  151. return MCFSIM_PADAT;
  152. else if (gpio < 32)
  153. return MCFSIM_PBDAT;
  154. else
  155. return MCFSIM_PCDAT;
  156. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  157. if (gpio < 32)
  158. return MCFSIM2_GPIOREAD;
  159. else
  160. return MCFSIM2_GPIO1READ;
  161. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  162. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  163. if (gpio < 8)
  164. return MCFEPORT_EPPDR;
  165. #if defined(CONFIG_M528x)
  166. else if (gpio < 16)
  167. return MCFGPTA_GPTPORT;
  168. else if (gpio < 24)
  169. return MCFGPTB_GPTPORT;
  170. else if (gpio < 32)
  171. return MCFQADC_PORTQA;
  172. else if (gpio < 40)
  173. return MCFQADC_PORTQB;
  174. #endif
  175. else
  176. return MCFGPIO_PPDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  177. #else
  178. return 0;
  179. #endif
  180. }
  181. /* return the port output data register for a gpio */
  182. static inline u32 __mcfgpio_podr(unsigned gpio)
  183. {
  184. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  185. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  186. return MCFSIM_PADAT;
  187. #elif defined(CONFIG_M5272)
  188. if (gpio < 16)
  189. return MCFSIM_PADAT;
  190. else if (gpio < 32)
  191. return MCFSIM_PBDAT;
  192. else
  193. return MCFSIM_PCDAT;
  194. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  195. if (gpio < 32)
  196. return MCFSIM2_GPIOWRITE;
  197. else
  198. return MCFSIM2_GPIO1WRITE;
  199. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  200. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  201. if (gpio < 8)
  202. return MCFEPORT_EPDR;
  203. #if defined(CONFIG_M528x)
  204. else if (gpio < 16)
  205. return MCFGPTA_GPTPORT;
  206. else if (gpio < 24)
  207. return MCFGPTB_GPTPORT;
  208. else if (gpio < 32)
  209. return MCFQADC_PORTQA;
  210. else if (gpio < 40)
  211. return MCFQADC_PORTQB;
  212. #endif
  213. else
  214. return MCFGPIO_PODR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  215. #else
  216. return 0;
  217. #endif
  218. }
  219. /* return the port direction data register for a gpio */
  220. static inline u32 __mcfgpio_pddr(unsigned gpio)
  221. {
  222. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  223. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  224. return MCFSIM_PADDR;
  225. #elif defined(CONFIG_M5272)
  226. if (gpio < 16)
  227. return MCFSIM_PADDR;
  228. else if (gpio < 32)
  229. return MCFSIM_PBDDR;
  230. else
  231. return MCFSIM_PCDDR;
  232. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  233. if (gpio < 32)
  234. return MCFSIM2_GPIOENABLE;
  235. else
  236. return MCFSIM2_GPIO1ENABLE;
  237. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  238. defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
  239. if (gpio < 8)
  240. return MCFEPORT_EPDDR;
  241. #if defined(CONFIG_M528x)
  242. else if (gpio < 16)
  243. return MCFGPTA_GPTDDR;
  244. else if (gpio < 24)
  245. return MCFGPTB_GPTDDR;
  246. else if (gpio < 32)
  247. return MCFQADC_DDRQA;
  248. else if (gpio < 40)
  249. return MCFQADC_DDRQB;
  250. #endif
  251. else
  252. return MCFGPIO_PDDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  253. #else
  254. return 0;
  255. #endif
  256. }
  257. #endif /* mcfgpio_h */