mcfgpio.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_M53xx) || defined(CONFIG_M54xx) || \
  94. defined(CONFIG_M5441x)
  95. /* These parts have GPIO organized by 8 bit ports */
  96. #define MCFGPIO_PORTTYPE u8
  97. #define MCFGPIO_PORTSIZE 8
  98. #define mcfgpio_read(port) __raw_readb(port)
  99. #define mcfgpio_write(data, port) __raw_writeb(data, port)
  100. #elif defined(CONFIG_M5307) || defined(CONFIG_M5407) || defined(CONFIG_M5272)
  101. /* These parts have GPIO organized by 16 bit ports */
  102. #define MCFGPIO_PORTTYPE u16
  103. #define MCFGPIO_PORTSIZE 16
  104. #define mcfgpio_read(port) __raw_readw(port)
  105. #define mcfgpio_write(data, port) __raw_writew(data, port)
  106. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  107. /* These parts have GPIO organized by 32 bit ports */
  108. #define MCFGPIO_PORTTYPE u32
  109. #define MCFGPIO_PORTSIZE 32
  110. #define mcfgpio_read(port) __raw_readl(port)
  111. #define mcfgpio_write(data, port) __raw_writel(data, port)
  112. #endif
  113. #define mcfgpio_bit(gpio) (1 << ((gpio) % MCFGPIO_PORTSIZE))
  114. #define mcfgpio_port(gpio) ((gpio) / MCFGPIO_PORTSIZE)
  115. #if defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  116. defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  117. defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
  118. defined(CONFIG_M5441x)
  119. /*
  120. * These parts have an 'Edge' Port module (external interrupt/GPIO) which uses
  121. * read-modify-write to change an output and a GPIO module which has separate
  122. * set/clr registers to directly change outputs with a single write access.
  123. */
  124. #if defined(CONFIG_M528x)
  125. /*
  126. * The 528x also has GPIOs in other modules (GPT, QADC) which use
  127. * read-modify-write as well as those controlled by the EPORT and GPIO modules.
  128. */
  129. #define MCFGPIO_SCR_START 40
  130. #elif defined(CONFIGM5441x)
  131. /* The m5441x EPORT doesn't have its own GPIO port, uses PORT C */
  132. #define MCFGPIO_SCR_START 0
  133. #else
  134. #define MCFGPIO_SCR_START 8
  135. #endif
  136. #define MCFGPIO_SETR_PORT(gpio) (MCFGPIO_SETR + \
  137. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  138. #define MCFGPIO_CLRR_PORT(gpio) (MCFGPIO_CLRR + \
  139. mcfgpio_port(gpio - MCFGPIO_SCR_START))
  140. #else
  141. #define MCFGPIO_SCR_START MCFGPIO_PIN_MAX
  142. /* with MCFGPIO_SCR == MCFGPIO_PIN_MAX, these will be optimized away */
  143. #define MCFGPIO_SETR_PORT(gpio) 0
  144. #define MCFGPIO_CLRR_PORT(gpio) 0
  145. #endif
  146. /*
  147. * Coldfire specific helper functions
  148. */
  149. /* return the port pin data register for a gpio */
  150. static inline u32 __mcfgpio_ppdr(unsigned gpio)
  151. {
  152. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  153. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  154. return MCFSIM_PADAT;
  155. #elif defined(CONFIG_M5272)
  156. if (gpio < 16)
  157. return MCFSIM_PADAT;
  158. else if (gpio < 32)
  159. return MCFSIM_PBDAT;
  160. else
  161. return MCFSIM_PCDAT;
  162. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  163. if (gpio < 32)
  164. return MCFSIM2_GPIOREAD;
  165. else
  166. return MCFSIM2_GPIO1READ;
  167. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  168. defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  169. defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
  170. defined(CONFIG_M5441x)
  171. #if !defined(CONFIG_M5441x)
  172. if (gpio < 8)
  173. return MCFEPORT_EPPDR;
  174. #if defined(CONFIG_M528x)
  175. else if (gpio < 16)
  176. return MCFGPTA_GPTPORT;
  177. else if (gpio < 24)
  178. return MCFGPTB_GPTPORT;
  179. else if (gpio < 32)
  180. return MCFQADC_PORTQA;
  181. else if (gpio < 40)
  182. return MCFQADC_PORTQB;
  183. #endif /* defined(CONFIG_M528x) */
  184. else
  185. #endif /* !defined(CONFIG_M5441x) */
  186. return MCFGPIO_PPDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  187. #else
  188. return 0;
  189. #endif
  190. }
  191. /* return the port output data register for a gpio */
  192. static inline u32 __mcfgpio_podr(unsigned gpio)
  193. {
  194. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  195. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  196. return MCFSIM_PADAT;
  197. #elif defined(CONFIG_M5272)
  198. if (gpio < 16)
  199. return MCFSIM_PADAT;
  200. else if (gpio < 32)
  201. return MCFSIM_PBDAT;
  202. else
  203. return MCFSIM_PCDAT;
  204. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  205. if (gpio < 32)
  206. return MCFSIM2_GPIOWRITE;
  207. else
  208. return MCFSIM2_GPIO1WRITE;
  209. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  210. defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  211. defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
  212. defined(CONFIG_M5441x)
  213. #if !defined(CONFIG_M5441x)
  214. if (gpio < 8)
  215. return MCFEPORT_EPDR;
  216. #if defined(CONFIG_M528x)
  217. else if (gpio < 16)
  218. return MCFGPTA_GPTPORT;
  219. else if (gpio < 24)
  220. return MCFGPTB_GPTPORT;
  221. else if (gpio < 32)
  222. return MCFQADC_PORTQA;
  223. else if (gpio < 40)
  224. return MCFQADC_PORTQB;
  225. #endif /* defined(CONFIG_M528x) */
  226. else
  227. #endif /* !defined(CONFIG_M5441x) */
  228. return MCFGPIO_PODR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  229. #else
  230. return 0;
  231. #endif
  232. }
  233. /* return the port direction data register for a gpio */
  234. static inline u32 __mcfgpio_pddr(unsigned gpio)
  235. {
  236. #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
  237. defined(CONFIG_M5307) || defined(CONFIG_M5407)
  238. return MCFSIM_PADDR;
  239. #elif defined(CONFIG_M5272)
  240. if (gpio < 16)
  241. return MCFSIM_PADDR;
  242. else if (gpio < 32)
  243. return MCFSIM_PBDDR;
  244. else
  245. return MCFSIM_PCDDR;
  246. #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
  247. if (gpio < 32)
  248. return MCFSIM2_GPIOENABLE;
  249. else
  250. return MCFSIM2_GPIO1ENABLE;
  251. #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
  252. defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  253. defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
  254. defined(CONFIG_M5441x)
  255. #if !defined(CONFIG_M5441x)
  256. if (gpio < 8)
  257. return MCFEPORT_EPDDR;
  258. #if defined(CONFIG_M528x)
  259. else if (gpio < 16)
  260. return MCFGPTA_GPTDDR;
  261. else if (gpio < 24)
  262. return MCFGPTB_GPTDDR;
  263. else if (gpio < 32)
  264. return MCFQADC_DDRQA;
  265. else if (gpio < 40)
  266. return MCFQADC_DDRQB;
  267. #endif /* defined(CONFIG_M528x) */
  268. else
  269. #endif /* !defined(CONFIG_M5441x) */
  270. return MCFGPIO_PDDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
  271. #else
  272. return 0;
  273. #endif
  274. }
  275. #endif /* mcfgpio_h */