mcfgpio.h 8.1 KB

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