gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "FreeRTOS.h"
  2. #include "chip.h"
  3. #define GPIO_SWPORTA_DR 0x00
  4. #define GPIO_SWPORTA_DDR 0x04
  5. #define GPIO_SWPORTA_CTL 0x08
  6. #define GPIO_SWPORTA_INTEN 0x30
  7. #define GPIO_SWPORTA_INTMASK 0x34
  8. #define GPIO_SWPORTA_INTTYPE_LEVEL 0x38
  9. #define GPIO_SWPORTA_INT_POLARITY 0x3c
  10. #define GPIO_SWPORTA_INTSTATUS 0x40
  11. #define GPIO_SWPORTA_RAW_INTSTATUS 0x44
  12. #define GPIO_SWPORTA_DEBOUNCE 0x48
  13. #define GPIO_SWPORTA_EOI 0x4c
  14. #define GPIO_SWPORTA_EXT_PORTA 0x50
  15. #define GPIO_SWPORTA_EXT_PORTB 0x54
  16. #define GPIO_SWPORTA_EXT_PORTC 0x58
  17. #define GPIO_SWPORTA_EXT_PORTD 0x5c
  18. #define GPIO_SWPORTA_LS_SYNC 0x60
  19. #define GPIO_SWPORTA_ID_CODE 0x64
  20. #define GPIO_SWPORTA_INT_BOTHEDGE 0x68
  21. #define GPIO_SWPORTA_VER_ID_CODE 0x6C
  22. #define GPIO_SWPORTA_CONFIG_REG2 0x70
  23. #define GPIO_SWPORTA_CONFIG_REG1 0x74
  24. #define GPIO_BANK 5
  25. #define GPIO_NUM 144
  26. typedef struct {
  27. ISRFunction_t handler;
  28. void *handler_param;
  29. int irq_type;
  30. } GpioIrqDesc_t;
  31. static GpioIrqDesc_t gpio_irq_descs[GPIO_NUM];
  32. static __INLINE uint32_t gpio_get_regbase(int gpio)
  33. {
  34. int gpiox = (gpio >> 5) & 0x7;
  35. return REGS_GPIO_BASE + 0x80 * gpiox;
  36. }
  37. /* static __INLINE int GPIO_BANK(unsigned gpio)
  38. {
  39. return gpio >> 5;
  40. } */
  41. static __INLINE int GPIO_OFFSET(unsigned gpio)
  42. {
  43. return gpio & 0x1F;
  44. }
  45. static __INLINE void *GPIO_MODREG(unsigned gpio)
  46. {
  47. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_DDR);
  48. }
  49. static __INLINE void *GPIO_WDATAREG(unsigned gpio)
  50. {
  51. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_DR);
  52. }
  53. static __INLINE void *GPIO_RDATAREG(unsigned gpio)
  54. {
  55. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_EXT_PORTA);
  56. }
  57. static __INLINE void *GPIO_INTENREG(unsigned gpio)
  58. {
  59. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_INTEN);
  60. }
  61. static __INLINE void *GPIO_INTMASKREG(unsigned gpio)
  62. {
  63. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_INTMASK);
  64. }
  65. static __INLINE void *GPIO_INTLVLREG(unsigned gpio)
  66. {
  67. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_INTTYPE_LEVEL);
  68. }
  69. static __INLINE void *GPIO_INTPOLREG(unsigned gpio)
  70. {
  71. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_INT_POLARITY);
  72. }
  73. static __INLINE void *GPIO_DEBOUNCEREG(unsigned gpio)
  74. {
  75. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_DEBOUNCE);
  76. }
  77. void gpio_request(unsigned gpio)
  78. {
  79. pinctrl_gpio_request(gpio);
  80. }
  81. void gpio_direction_output(unsigned gpio, int value)
  82. {
  83. configASSERT(gpio < GPIO_NUM);
  84. gpio_request(gpio);
  85. if ((gpio >= 111) && (gpio <= 126)) {
  86. writel(readl(GPIO_MODREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  87. } else {
  88. writel(readl(GPIO_MODREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  89. }
  90. if (value)
  91. writel(readl(GPIO_WDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  92. else
  93. writel(readl(GPIO_WDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  94. }
  95. void gpio_debouncereg_enable(unsigned gpio, int enable)
  96. {
  97. if(enable)
  98. writel(readl(GPIO_DEBOUNCEREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_DEBOUNCEREG(gpio));
  99. else
  100. writel(readl(GPIO_DEBOUNCEREG(gpio)) & (~(1 << GPIO_OFFSET(gpio))), GPIO_DEBOUNCEREG(gpio));
  101. }
  102. void gpio_direction_input(unsigned gpio)
  103. {
  104. configASSERT(gpio < GPIO_NUM);
  105. gpio_request(gpio);
  106. if ((gpio >= 111) && (gpio <= 126)) {
  107. writel(readl(GPIO_MODREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  108. writel(readl(REGS_SYSCTL_BASE + SYS_PAD_PUDCTL14) | (1 << (gpio - 111)), \
  109. REGS_SYSCTL_BASE + SYS_PAD_PUDCTL14);
  110. } else {
  111. writel(readl(GPIO_MODREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  112. }
  113. }
  114. void gpio_set_value(unsigned gpio, int value)
  115. {
  116. configASSERT(gpio < GPIO_NUM);
  117. if (value)
  118. writel(readl(GPIO_WDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  119. else
  120. writel(readl(GPIO_WDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  121. }
  122. int gpio_get_value(unsigned gpio)
  123. {
  124. configASSERT(gpio < GPIO_NUM);
  125. return !!(readl(GPIO_RDATAREG(gpio)) & (1 << GPIO_OFFSET(gpio)));
  126. }
  127. static void gpio_toggle_trigger(unsigned gpio)
  128. {
  129. u32 pol;
  130. pol = readl(GPIO_INTPOLREG(gpio));
  131. if (pol & (1 << GPIO_OFFSET(gpio)))
  132. pol &= ~(1 << GPIO_OFFSET(gpio));
  133. else
  134. pol |= (1 << GPIO_OFFSET(gpio));
  135. writel(pol, GPIO_INTPOLREG(gpio));
  136. }
  137. static void gpio_irq_enable(unsigned gpio)
  138. {
  139. writel(readl(GPIO_INTENREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_INTENREG(gpio));
  140. writel(readl(GPIO_INTMASKREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_INTMASKREG(gpio));
  141. }
  142. static void gpio_irq_disable(unsigned gpio)
  143. {
  144. writel(readl(GPIO_INTENREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_INTENREG(gpio));
  145. writel(readl(GPIO_INTMASKREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_INTMASKREG(gpio));
  146. }
  147. static int gpio_irq_set_irq_type(unsigned gpio, int type)
  148. {
  149. unsigned long level, polarity;
  150. level = readl(GPIO_INTLVLREG(gpio));
  151. polarity = readl(GPIO_INTPOLREG(gpio));
  152. switch (type) {
  153. case IRQ_TYPE_EDGE_BOTH:
  154. level |= (1 << GPIO_OFFSET(gpio));
  155. gpio_toggle_trigger(gpio);
  156. break;
  157. case IRQ_TYPE_EDGE_RISING:
  158. level |= (1 << GPIO_OFFSET(gpio));
  159. polarity |= (1 << GPIO_OFFSET(gpio));
  160. break;
  161. case IRQ_TYPE_EDGE_FALLING:
  162. level |= (1 << GPIO_OFFSET(gpio));
  163. polarity &= ~(1 << GPIO_OFFSET(gpio));
  164. break;
  165. case IRQ_TYPE_LEVEL_HIGH:
  166. level &= ~(1 << GPIO_OFFSET(gpio));
  167. polarity |= (1 << GPIO_OFFSET(gpio));
  168. break;
  169. case IRQ_TYPE_LEVEL_LOW:
  170. level &= ~(1 << GPIO_OFFSET(gpio));
  171. polarity &= ~(1 << GPIO_OFFSET(gpio));
  172. break;
  173. }
  174. writel(level, GPIO_INTLVLREG(gpio));
  175. if (type != IRQ_TYPE_EDGE_BOTH)
  176. writel(polarity, GPIO_INTPOLREG(gpio));
  177. return 0;
  178. }
  179. static void gpio_irq_handler(void *param)
  180. {
  181. u32 status;
  182. int i, j;
  183. for (i = 0; i < GPIO_BANK; i++) {
  184. status = readl(REGS_GPIO_BASE + 0x80 * i + GPIO_SWPORTA_INTSTATUS);
  185. for (j = 0; j < 32; j++) {
  186. if (status & (1 << j)) {
  187. u32 gpio = i * 32 + j;
  188. /* clear interrupt */
  189. writel(1 << j, REGS_GPIO_BASE + 0x80 * i + GPIO_SWPORTA_EOI);
  190. if (gpio_irq_descs[gpio].handler != NULL) {
  191. gpio_irq_descs[gpio].handler(gpio_irq_descs[gpio].handler_param);
  192. if (gpio_irq_descs[gpio].irq_type == IRQ_TYPE_EDGE_BOTH)
  193. gpio_toggle_trigger(gpio);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. int gpio_irq_request(unsigned gpio, int irq_type, ISRFunction_t irq_handler, void *param)
  200. {
  201. configASSERT(gpio < GPIO_NUM);
  202. portENTER_CRITICAL();
  203. gpio_request(gpio);
  204. gpio_irq_descs[gpio].handler = irq_handler;
  205. gpio_irq_descs[gpio].handler_param = param;
  206. gpio_irq_descs[gpio].irq_type = irq_type;
  207. gpio_irq_set_irq_type(gpio, irq_type);
  208. if(gpio >= 128)
  209. request_irq(GPIOE_IRQn, 0, gpio_irq_handler,NULL);
  210. else
  211. request_irq(GPIOA_IRQn + ((gpio >> 5) & 0x3), 0, gpio_irq_handler,NULL);
  212. gpio_irq_enable(gpio);
  213. portEXIT_CRITICAL();
  214. return 0;
  215. }
  216. int gpio_irq_free(unsigned gpio)
  217. {
  218. configASSERT(gpio < GPIO_NUM);
  219. portENTER_CRITICAL();
  220. gpio_irq_disable(gpio);
  221. gpio_irq_descs[gpio].handler = NULL;
  222. gpio_irq_descs[gpio].handler_param = NULL;
  223. portEXIT_CRITICAL();
  224. return 0;
  225. }