gpio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. if ((GetChipHWVeriosn() != CHIP_HVER_0) && (gpio != 111) && (gpio != 113)
  87. && (gpio != 118) && (gpio != 121)
  88. && (gpio != 124) && (gpio != 126)) {
  89. writel(readl(GPIO_MODREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  90. } else {
  91. writel(readl(GPIO_MODREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  92. }
  93. /* The GPIO related to ADC needs to be configured with IE. */
  94. writel(readl(REGS_SYSCTL_BASE + SYS_PAD_PUDCTL14) & ~(1 << (gpio - 111)), \
  95. REGS_SYSCTL_BASE + SYS_PAD_PUDCTL14);
  96. } else {
  97. writel(readl(GPIO_MODREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  98. }
  99. if (value)
  100. writel(readl(GPIO_WDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  101. else
  102. writel(readl(GPIO_WDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  103. }
  104. void gpio_debouncereg_enable(unsigned gpio, int enable)
  105. {
  106. if(enable)
  107. writel(readl(GPIO_DEBOUNCEREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_DEBOUNCEREG(gpio));
  108. else
  109. writel(readl(GPIO_DEBOUNCEREG(gpio)) & (~(1 << GPIO_OFFSET(gpio))), GPIO_DEBOUNCEREG(gpio));
  110. }
  111. void gpio_direction_input(unsigned gpio)
  112. {
  113. configASSERT(gpio < GPIO_NUM);
  114. gpio_request(gpio);
  115. if ((gpio >= 111) && (gpio <= 126)) {
  116. if ((GetChipHWVeriosn() != CHIP_HVER_0) && (gpio != 111) && (gpio != 113)
  117. && (gpio != 118) && (gpio != 121)
  118. && (gpio != 124) && (gpio != 126)) {
  119. writel(readl(GPIO_MODREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  120. } else {
  121. writel(readl(GPIO_MODREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  122. }
  123. /* The GPIO related to ADC needs to be configured with IE. */
  124. writel(readl(REGS_SYSCTL_BASE + SYS_PAD_PUDCTL14) | (1 << (gpio - 111)), \
  125. REGS_SYSCTL_BASE + SYS_PAD_PUDCTL14);
  126. } else {
  127. writel(readl(GPIO_MODREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  128. }
  129. }
  130. void gpio_set_value(unsigned gpio, int value)
  131. {
  132. configASSERT(gpio < GPIO_NUM);
  133. if (value)
  134. writel(readl(GPIO_WDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  135. else
  136. writel(readl(GPIO_WDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  137. }
  138. int gpio_get_value(unsigned gpio)
  139. {
  140. configASSERT(gpio < GPIO_NUM);
  141. return !!(readl(GPIO_RDATAREG(gpio)) & (1 << GPIO_OFFSET(gpio)));
  142. }
  143. static void gpio_toggle_trigger(unsigned gpio)
  144. {
  145. u32 pol;
  146. pol = readl(GPIO_INTPOLREG(gpio));
  147. if (pol & (1 << GPIO_OFFSET(gpio)))
  148. pol &= ~(1 << GPIO_OFFSET(gpio));
  149. else
  150. pol |= (1 << GPIO_OFFSET(gpio));
  151. writel(pol, GPIO_INTPOLREG(gpio));
  152. }
  153. static void gpio_irq_enable(unsigned gpio)
  154. {
  155. writel(readl(GPIO_INTENREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_INTENREG(gpio));
  156. writel(readl(GPIO_INTMASKREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_INTMASKREG(gpio));
  157. }
  158. static void gpio_irq_disable(unsigned gpio)
  159. {
  160. writel(readl(GPIO_INTENREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_INTENREG(gpio));
  161. writel(readl(GPIO_INTMASKREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_INTMASKREG(gpio));
  162. }
  163. static int gpio_irq_set_irq_type(unsigned gpio, int type)
  164. {
  165. unsigned long level, polarity;
  166. level = readl(GPIO_INTLVLREG(gpio));
  167. polarity = readl(GPIO_INTPOLREG(gpio));
  168. switch (type) {
  169. case IRQ_TYPE_EDGE_BOTH:
  170. level |= (1 << GPIO_OFFSET(gpio));
  171. gpio_toggle_trigger(gpio);
  172. break;
  173. case IRQ_TYPE_EDGE_RISING:
  174. level |= (1 << GPIO_OFFSET(gpio));
  175. polarity |= (1 << GPIO_OFFSET(gpio));
  176. break;
  177. case IRQ_TYPE_EDGE_FALLING:
  178. level |= (1 << GPIO_OFFSET(gpio));
  179. polarity &= ~(1 << GPIO_OFFSET(gpio));
  180. break;
  181. case IRQ_TYPE_LEVEL_HIGH:
  182. level &= ~(1 << GPIO_OFFSET(gpio));
  183. polarity |= (1 << GPIO_OFFSET(gpio));
  184. break;
  185. case IRQ_TYPE_LEVEL_LOW:
  186. level &= ~(1 << GPIO_OFFSET(gpio));
  187. polarity &= ~(1 << GPIO_OFFSET(gpio));
  188. break;
  189. }
  190. writel(level, GPIO_INTLVLREG(gpio));
  191. if (type != IRQ_TYPE_EDGE_BOTH)
  192. writel(polarity, GPIO_INTPOLREG(gpio));
  193. return 0;
  194. }
  195. static void gpio_irq_handler(void *param)
  196. {
  197. u32 status;
  198. int i, j;
  199. for (i = 0; i < GPIO_BANK; i++) {
  200. status = readl(REGS_GPIO_BASE + 0x80 * i + GPIO_SWPORTA_INTSTATUS);
  201. for (j = 0; j < 32; j++) {
  202. if (status & (1 << j)) {
  203. u32 gpio = i * 32 + j;
  204. /* clear interrupt */
  205. writel(1 << j, REGS_GPIO_BASE + 0x80 * i + GPIO_SWPORTA_EOI);
  206. if (gpio_irq_descs[gpio].handler != NULL) {
  207. gpio_irq_descs[gpio].handler(gpio_irq_descs[gpio].handler_param);
  208. if (gpio_irq_descs[gpio].irq_type == IRQ_TYPE_EDGE_BOTH)
  209. gpio_toggle_trigger(gpio);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. int gpio_irq_request(unsigned gpio, int irq_type, ISRFunction_t irq_handler, void *param)
  216. {
  217. configASSERT(gpio < GPIO_NUM);
  218. portENTER_CRITICAL();
  219. gpio_request(gpio);
  220. gpio_irq_descs[gpio].handler = irq_handler;
  221. gpio_irq_descs[gpio].handler_param = param;
  222. gpio_irq_descs[gpio].irq_type = irq_type;
  223. gpio_irq_set_irq_type(gpio, irq_type);
  224. if(gpio >= 128)
  225. request_irq(GPIOE_IRQn, 0, gpio_irq_handler,NULL);
  226. else
  227. request_irq(GPIOA_IRQn + ((gpio >> 5) & 0x3), 0, gpio_irq_handler,NULL);
  228. gpio_irq_enable(gpio);
  229. portEXIT_CRITICAL();
  230. return 0;
  231. }
  232. int gpio_irq_free(unsigned gpio)
  233. {
  234. configASSERT(gpio < GPIO_NUM);
  235. portENTER_CRITICAL();
  236. gpio_irq_disable(gpio);
  237. gpio_irq_descs[gpio].handler = NULL;
  238. gpio_irq_descs[gpio].handler_param = NULL;
  239. portEXIT_CRITICAL();
  240. return 0;
  241. }