mcfgpio.h 8.3 KB

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