drv_gpio.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023/10/19 xiunian first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. #include "drv_ioremap.h"
  15. #ifdef RT_USING_PIN
  16. #include "drv_gpio.h"
  17. #define GPIO_SWPORTA_DR 0x00
  18. #define GPIO_SWPORTA_DDR 0x04
  19. #define GPIO_INTEN 0x30
  20. #define GPIO_INTTYPE_LEVEL 0x38
  21. #define GPIO_INT_POLARITY 0x3c
  22. #define GPIO_INTSTATUS 0x40
  23. #define GPIO_PORTA_EOI 0x4c
  24. #define GPIO_EXT_PORTA 0x50
  25. #define DWAPB_GPIOA_BASE 0x03020000
  26. #define DWAPB_GPIOE_BASE 0x05021000
  27. #define DWAPB_GPIO_SIZE 0x1000
  28. #define DWAPB_GPIO_PORT_NR 5
  29. #define DWAPB_GPIO_NR 32
  30. #define PIN_NUM(port, no) (((((port) & 0xFu) << 8) | ((no) & 0xFFu)))
  31. #define PIN_PORT(pin) ((uint8_t)(((pin) >> 8) & 0xFu))
  32. #define PIN_NO(pin) ((uint8_t)((pin) & 0xFFu))
  33. #define BIT(x) (1UL << (x))
  34. rt_inline rt_uint32_t dwapb_read32(rt_ubase_t addr)
  35. {
  36. return HWREG32(addr);
  37. }
  38. rt_inline void dwapb_write32(rt_ubase_t addr, rt_uint32_t value)
  39. {
  40. HWREG32(addr) = value;
  41. }
  42. static rt_ubase_t dwapb_gpio_base = DWAPB_GPIOA_BASE;
  43. static rt_ubase_t dwapb_gpio_base_e = DWAPB_GPIOE_BASE;
  44. static struct dwapb_event
  45. {
  46. void (*(hdr[DWAPB_GPIO_NR]))(void *args);
  47. void *args[DWAPB_GPIO_NR];
  48. rt_uint8_t is_both_edge[DWAPB_GPIO_NR];
  49. } _dwapb_events[DWAPB_GPIO_PORT_NR];
  50. static void dwapb_toggle_trigger(rt_uint8_t port, rt_uint8_t bit)
  51. {
  52. rt_uint8_t val;
  53. rt_ubase_t base_addr;
  54. rt_uint32_t pol;
  55. base_addr = (port == 4 ? dwapb_gpio_base_e : (dwapb_gpio_base + DWAPB_GPIO_SIZE * port));
  56. pol = dwapb_read32(base_addr + GPIO_INT_POLARITY);
  57. /* Just read the current value right out of the data register */
  58. val = (rt_uint8_t)((dwapb_read32(base_addr + GPIO_EXT_PORTA) >> (bit)) & 1);
  59. if (val)
  60. pol &= ~BIT(bit);
  61. else
  62. pol |= BIT(bit);
  63. dwapb_write32(base_addr + GPIO_INT_POLARITY, pol);
  64. }
  65. static void dwapb_pin_mode(struct rt_device *device, rt_base_t pin, rt_uint8_t mode)
  66. {
  67. rt_uint8_t bit, port;
  68. rt_ubase_t base_addr;
  69. rt_uint32_t reg_val;
  70. bit = PIN_NO(pin);
  71. port = PIN_PORT(pin);
  72. base_addr = (port == 4 ? dwapb_gpio_base_e : (dwapb_gpio_base + DWAPB_GPIO_SIZE * port));
  73. reg_val = dwapb_read32(base_addr + GPIO_SWPORTA_DDR);
  74. switch (mode)
  75. {
  76. case PIN_MODE_OUTPUT:
  77. reg_val |= BIT(bit);
  78. break;
  79. case PIN_MODE_INPUT:
  80. reg_val &= ~BIT(bit);
  81. break;
  82. }
  83. dwapb_write32(base_addr + GPIO_SWPORTA_DDR, reg_val);
  84. }
  85. static void dwapb_pin_write(struct rt_device *device, rt_base_t pin, rt_uint8_t value)
  86. {
  87. rt_uint8_t bit, port;
  88. rt_ubase_t base_addr;
  89. rt_uint32_t reg_val;
  90. bit = PIN_NO(pin);
  91. port = PIN_PORT(pin);
  92. base_addr = (port == 4 ? dwapb_gpio_base_e : (dwapb_gpio_base + DWAPB_GPIO_SIZE * port));
  93. reg_val = dwapb_read32(base_addr + GPIO_SWPORTA_DR);
  94. reg_val = (value ? (reg_val | BIT(bit)) : (reg_val & (~BIT(bit))));
  95. dwapb_write32(base_addr + GPIO_SWPORTA_DR, reg_val);
  96. }
  97. static rt_ssize_t dwapb_pin_read(struct rt_device *device, rt_base_t pin)
  98. {
  99. rt_uint8_t bit, port;
  100. rt_ubase_t base_addr;
  101. bit = PIN_NO(pin);
  102. port = PIN_PORT(pin);
  103. base_addr = (port == 4 ? dwapb_gpio_base_e : (dwapb_gpio_base + DWAPB_GPIO_SIZE * port));
  104. rt_uint32_t reg_val = dwapb_read32(GPIO_EXT_PORTA + base_addr);
  105. return ((reg_val >> (bit)) & 1);
  106. }
  107. static rt_base_t dwapb_pin_get(const char *name)
  108. {
  109. rt_base_t pin = 0;
  110. int port_num, pin_num = 0;
  111. int i, name_len;
  112. name_len = rt_strlen(name);
  113. if ((name_len < 2) || (name_len > 3))
  114. {
  115. goto out;
  116. }
  117. if ((name[0] >= 'A') && (name[0] <= 'E'))
  118. {
  119. port_num = (int)(name[0] - 'A');
  120. }
  121. else
  122. {
  123. goto out;
  124. }
  125. for (i = 1; i < name_len; i++)
  126. {
  127. pin_num *= 10;
  128. pin_num += name[i] - '0';
  129. }
  130. pin = PIN_NUM(port_num, pin_num);
  131. return pin;
  132. out:
  133. rt_kprintf("xy x:A~E y:0~31, e.g. C24\n");
  134. return -RT_EINVAL;
  135. }
  136. static rt_err_t dwapb_pin_attach_irq(struct rt_device *device, rt_base_t pin, rt_uint8_t mode, void (*hdr)(void *args), void *args)
  137. {
  138. rt_uint8_t bit, port;
  139. rt_ubase_t base_addr;
  140. rt_uint32_t it_val, ip_val;
  141. bit = PIN_NO(pin);
  142. port = PIN_PORT(pin);
  143. base_addr = (port == 4 ? dwapb_gpio_base_e : (dwapb_gpio_base + DWAPB_GPIO_SIZE * port));
  144. it_val = dwapb_read32(base_addr + GPIO_INTTYPE_LEVEL);
  145. ip_val = dwapb_read32(base_addr + GPIO_INT_POLARITY);
  146. if (mode == PIN_IRQ_MODE_HIGH_LEVEL || mode == PIN_IRQ_MODE_LOW_LEVEL)
  147. {
  148. rt_bool_t polarity = (mode == PIN_IRQ_MODE_HIGH_LEVEL);
  149. /* Enable level detection */
  150. it_val = (it_val & (~BIT(bit)));
  151. /* Select polarity */
  152. ip_val = (polarity ? (ip_val | BIT(bit)) : (ip_val & (~BIT(bit))));
  153. }
  154. else if (mode == PIN_IRQ_MODE_RISING_FALLING)
  155. {
  156. /* Disable level detection */
  157. it_val = (it_val | BIT(bit));
  158. /* Select both edges */
  159. dwapb_toggle_trigger(port, bit);
  160. }
  161. else if (mode == PIN_IRQ_MODE_RISING || mode == PIN_IRQ_MODE_FALLING)
  162. {
  163. rt_bool_t rising = (mode == PIN_IRQ_MODE_RISING);
  164. /* Disable level detection */
  165. it_val = (it_val | BIT(bit));
  166. /* Select edge */
  167. ip_val = (rising ? (ip_val | BIT(bit)) : (ip_val & (~BIT(bit))));
  168. }
  169. else
  170. {
  171. /* No trigger: disable everything */
  172. it_val = (it_val & (~BIT(bit)));
  173. ip_val = (ip_val & (~BIT(bit)));
  174. }
  175. dwapb_write32(base_addr + GPIO_INTTYPE_LEVEL, it_val);
  176. if (mode != PIN_IRQ_MODE_RISING_FALLING)
  177. dwapb_write32(base_addr + GPIO_INT_POLARITY, ip_val);
  178. _dwapb_events[PIN_PORT(pin)].hdr[PIN_NO(pin)] = hdr;
  179. _dwapb_events[PIN_PORT(pin)].args[PIN_NO(pin)] = args;
  180. _dwapb_events[PIN_PORT(pin)].is_both_edge[PIN_NO(pin)] = (mode == PIN_IRQ_MODE_RISING_FALLING);
  181. return RT_EOK;
  182. }
  183. static rt_err_t dwapb_pin_detach_irq(struct rt_device *device, rt_base_t pin)
  184. {
  185. _dwapb_events[PIN_PORT(pin)].hdr[PIN_NO(pin)] = RT_NULL;
  186. _dwapb_events[PIN_PORT(pin)].args[PIN_NO(pin)] = RT_NULL;
  187. _dwapb_events[PIN_PORT(pin)].is_both_edge[PIN_NO(pin)] = 0;
  188. return RT_EOK;
  189. }
  190. static rt_err_t dwapb_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint8_t enabled)
  191. {
  192. rt_uint8_t bit, port;
  193. rt_ubase_t base_addr;
  194. bit = PIN_NO(pin);
  195. port = PIN_PORT(pin);
  196. base_addr = (port == 4 ? dwapb_gpio_base_e : (dwapb_gpio_base + DWAPB_GPIO_SIZE * port));
  197. rt_uint32_t reg_val = dwapb_read32(base_addr + GPIO_INTEN);
  198. reg_val = (enabled ? (reg_val | BIT(bit)) : (reg_val & (~BIT(bit))));
  199. dwapb_write32(base_addr + GPIO_INTEN, reg_val);
  200. return RT_EOK;
  201. }
  202. static const struct rt_pin_ops _dwapb_ops =
  203. {
  204. dwapb_pin_mode,
  205. dwapb_pin_write,
  206. dwapb_pin_read,
  207. dwapb_pin_attach_irq,
  208. dwapb_pin_detach_irq,
  209. dwapb_pin_irq_enable,
  210. dwapb_pin_get,
  211. };
  212. static void rt_hw_gpio_isr(int irqno, void *param)
  213. {
  214. rt_uint8_t port;
  215. rt_ubase_t base_addr;
  216. rt_uint32_t pending, mask;
  217. mask = 0;
  218. port = (irqno == BSP_SYS_GPIO_IRQ_BASE ? 4 : (irqno - BSP_GPIO_IRQ_BASE));
  219. base_addr = (port == 4 ? dwapb_gpio_base_e : (dwapb_gpio_base + DWAPB_GPIO_SIZE * port));
  220. pending = dwapb_read32(base_addr + GPIO_INTSTATUS);
  221. if (pending)
  222. {
  223. rt_base_t bit;
  224. for (bit = 0; bit < DWAPB_GPIO_NR; ++bit)
  225. {
  226. if (pending & BIT(bit))
  227. {
  228. mask = (mask | (BIT(bit)));
  229. if (_dwapb_events[port].hdr[bit] != RT_NULL)
  230. {
  231. _dwapb_events[port].hdr[bit](_dwapb_events[port].args[bit]);
  232. }
  233. if (_dwapb_events[port].is_both_edge[bit]) {
  234. dwapb_toggle_trigger(port, bit);
  235. }
  236. }
  237. }
  238. }
  239. dwapb_write32(base_addr + GPIO_PORTA_EOI, mask);
  240. }
  241. int rt_hw_gpio_init(void)
  242. {
  243. dwapb_gpio_base = (rt_ubase_t)DRV_IOREMAP((void *)dwapb_gpio_base, 0x1000);
  244. dwapb_gpio_base_e = (rt_ubase_t)DRV_IOREMAP((void *)dwapb_gpio_base_e, 0x1000);
  245. rt_device_pin_register("gpio", &_dwapb_ops, RT_NULL);
  246. #define INT_INSTALL_GPIO_DEVICE(no) \
  247. rt_hw_interrupt_install(BSP_GPIO_IRQ_BASE + (no), rt_hw_gpio_isr, RT_NULL, "gpio"); \
  248. rt_hw_interrupt_umask(BSP_GPIO_IRQ_BASE + (no));
  249. INT_INSTALL_GPIO_DEVICE(0);
  250. INT_INSTALL_GPIO_DEVICE(1);
  251. INT_INSTALL_GPIO_DEVICE(2);
  252. INT_INSTALL_GPIO_DEVICE(3);
  253. rt_hw_interrupt_install(BSP_SYS_GPIO_IRQ_BASE, rt_hw_gpio_isr, RT_NULL, "gpio");
  254. rt_hw_interrupt_umask(BSP_SYS_GPIO_IRQ_BASE);
  255. return 0;
  256. }
  257. INIT_DEVICE_EXPORT(rt_hw_gpio_init);
  258. #endif /* RT_USING_PIN */