dw_gpio_obj.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2018, Synopsys, Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "inc/arc/arc.h"
  7. #include "inc/arc/arc_builtin.h"
  8. #include "dw_gpio_obj.h"
  9. #include "emsk_hardware.h"
  10. /*
  11. * Uncomment this to enable default
  12. * gpio bit handler output message
  13. * by uart
  14. */
  15. #ifdef ARC_FEATURE_DMP_PERIPHERAL
  16. #define PERIPHERAL_BASE ARC_FEATURE_DMP_PERIPHERAL
  17. #else
  18. #define PERIPHERAL_BASE _arc_aux_read(AUX_DMP_PERIPHERAL)
  19. #endif
  20. #if (USE_DW_GPIO_PORT_A)
  21. static DEV_GPIO port_a;
  22. static DW_GPIO_PORT dw_gpio_port_a;
  23. static DEV_GPIO_HANDLER dw_gpio_bit_handler_a[EMSK_GPIO_A_INT_MAX_COUNT];
  24. static DW_GPIO_BIT_ISR dw_gpio_bit_isr_a = {
  25. EMSK_GPIO_A_INT_MAX_COUNT, dw_gpio_bit_handler_a
  26. };
  27. static int32_t porta_open(uint32_t dir)
  28. {
  29. return dw_gpio_open(&port_a, dir);
  30. }
  31. static int32_t porta_close(void)
  32. {
  33. return dw_gpio_close(&port_a);
  34. }
  35. static int32_t porta_control(uint32_t ctrl_cmd, void *param)
  36. {
  37. return dw_gpio_control(&port_a, ctrl_cmd, param);
  38. }
  39. static int32_t porta_write(uint32_t val, uint32_t mask)
  40. {
  41. return dw_gpio_write(&port_a, val, mask);
  42. }
  43. static int32_t porta_read(uint32_t *val, uint32_t mask)
  44. {
  45. return dw_gpio_read(&port_a, val, mask);
  46. }
  47. static void porta_isr(void *ptr)
  48. {
  49. dw_gpio_isr_handler(&port_a, ptr);
  50. }
  51. static void porta_install(void)
  52. {
  53. uint32_t i;
  54. DEV_GPIO_PTR port_ptr = &port_a;
  55. DEV_GPIO_INFO_PTR info_ptr = &(port_a.gpio_info);
  56. DW_GPIO_PORT_PTR dw_port_ptr = &(dw_gpio_port_a);
  57. info_ptr->gpio_ctrl = (void *)dw_port_ptr;
  58. info_ptr->opn_cnt = 0;
  59. info_ptr->method = 0;
  60. info_ptr->direction = 0;
  61. info_ptr->extra = 0;
  62. dw_port_ptr->no = DW_GPIO_PORT_A;
  63. dw_port_ptr->regs = (DW_GPIO_REG_PTR)(PERIPHERAL_BASE|REL_REGBASE_GPIO0);
  64. dw_port_ptr->valid_bit_mask = EMSK_GPIO_A_VALID_MASK;
  65. dw_port_ptr->intno = INTNO_GPIO;
  66. dw_port_ptr->int_handler = porta_isr;
  67. for (i=0; i<dw_gpio_bit_isr_a.int_bit_max_cnt; i++) {
  68. dw_gpio_bit_isr_a.int_bit_handler_ptr[i] = NULL;
  69. }
  70. dw_port_ptr->gpio_bit_isr = &dw_gpio_bit_isr_a;
  71. port_ptr->gpio_open = porta_open;
  72. port_ptr->gpio_close = porta_close;
  73. port_ptr->gpio_control = porta_control;
  74. port_ptr->gpio_write = porta_write;
  75. port_ptr->gpio_read = porta_read;
  76. }
  77. #endif
  78. #if (USE_DW_GPIO_PORT_B)
  79. static DEV_GPIO port_b;
  80. static DW_GPIO_PORT dw_gpio_port_b;
  81. static int32_t portb_open(uint32_t dir)
  82. {
  83. return dw_gpio_open(&port_b, dir);
  84. }
  85. static int32_t portb_close(void)
  86. {
  87. return dw_gpio_close(&port_b);
  88. }
  89. static int32_t portb_control(uint32_t ctrl_cmd, void *param)
  90. {
  91. return dw_gpio_control(&port_b, ctrl_cmd, param);
  92. }
  93. static int32_t portb_write(uint32_t val, uint32_t mask)
  94. {
  95. return dw_gpio_write(&port_b, val, mask);
  96. }
  97. static int32_t portb_read(uint32_t *val, uint32_t mask)
  98. {
  99. return dw_gpio_read(&port_b, val, mask);
  100. }
  101. static void portb_isr(void *ptr)
  102. {
  103. dw_gpio_isr_handler(&port_b, ptr);
  104. }
  105. static void portb_install(void)
  106. {
  107. DEV_GPIO_PTR port_ptr = &port_b;
  108. DEV_GPIO_INFO_PTR info_ptr = &(port_b.gpio_info);
  109. DW_GPIO_PORT_PTR dw_port_ptr = &(dw_gpio_port_b);
  110. info_ptr->gpio_ctrl = (void *)dw_port_ptr;
  111. info_ptr->opn_cnt = 0;
  112. info_ptr->method = 0;
  113. info_ptr->direction = 0;
  114. info_ptr->extra = 0;
  115. dw_port_ptr->no = DW_GPIO_PORT_B;
  116. dw_port_ptr->regs = (DW_GPIO_REG_PTR)(PERIPHERAL_BASE|REL_REGBASE_GPIO0);
  117. dw_port_ptr->valid_bit_mask = EMSK_GPIO_B_VALID_MASK;
  118. dw_port_ptr->intno = INTNO_GPIO;
  119. dw_port_ptr->int_handler = portb_isr;
  120. dw_port_ptr->gpio_bit_isr = NULL;
  121. port_ptr->gpio_open = portb_open;
  122. port_ptr->gpio_close = portb_close;
  123. port_ptr->gpio_control = portb_control;
  124. port_ptr->gpio_write = portb_write;
  125. port_ptr->gpio_read = portb_read;
  126. }
  127. #endif
  128. #if (USE_DW_GPIO_PORT_C)
  129. static DEV_GPIO port_c;
  130. static DW_GPIO_PORT dw_gpio_port_c;
  131. static int32_t portc_open(uint32_t dir)
  132. {
  133. return dw_gpio_open(&port_c, dir);
  134. }
  135. static int32_t portc_close(void)
  136. {
  137. return dw_gpio_close(&port_c);
  138. }
  139. static int32_t portc_control(uint32_t ctrl_cmd, void *param)
  140. {
  141. return dw_gpio_control(&port_c, ctrl_cmd, param);
  142. }
  143. static int32_t portc_write(uint32_t val, uint32_t mask)
  144. {
  145. return dw_gpio_write(&port_c, val, mask);
  146. }
  147. static int32_t portc_read(uint32_t *val, uint32_t mask)
  148. {
  149. return dw_gpio_read(&port_c, val, mask);
  150. }
  151. static void portc_isr(void *ptr)
  152. {
  153. dw_gpio_isr_handler(&port_c, ptr);
  154. }
  155. static void portc_install(void)
  156. {
  157. DEV_GPIO_PTR port_ptr = &port_c;
  158. DEV_GPIO_INFO_PTR info_ptr = &(port_c.gpio_info);
  159. DW_GPIO_PORT_PTR dw_port_ptr = &(dw_gpio_port_c);
  160. info_ptr->gpio_ctrl = (void *)dw_port_ptr;
  161. info_ptr->opn_cnt = 0;
  162. info_ptr->method = 0;
  163. info_ptr->direction = 0;
  164. info_ptr->extra = 0;
  165. dw_port_ptr->no = DW_GPIO_PORT_C;
  166. dw_port_ptr->regs = (DW_GPIO_REG_PTR)(PERIPHERAL_BASE|REL_REGBASE_GPIO0);
  167. dw_port_ptr->valid_bit_mask = EMSK_GPIO_C_VALID_MASK;
  168. dw_port_ptr->intno = INTNO_GPIO;
  169. dw_port_ptr->int_handler = portc_isr;
  170. dw_port_ptr->gpio_bit_isr = NULL;
  171. port_ptr->gpio_open = portc_open;
  172. port_ptr->gpio_close = portc_close;
  173. port_ptr->gpio_control = portc_control;
  174. port_ptr->gpio_write = portc_write;
  175. port_ptr->gpio_read = portc_read;
  176. }
  177. #endif
  178. #if (USE_DW_GPIO_PORT_D)
  179. static DEV_GPIO port_d;
  180. static DW_GPIO_PORT dw_gpio_port_d;
  181. static int32_t portd_open(uint32_t dir)
  182. {
  183. return dw_gpio_open(&port_d, dir);
  184. }
  185. static int32_t portd_close(void)
  186. {
  187. return dw_gpio_close(&port_d);
  188. }
  189. static int32_t portd_control(uint32_t ctrl_cmd, void *param)
  190. {
  191. return dw_gpio_control(&port_d, ctrl_cmd, param);
  192. }
  193. static int32_t portd_write(uint32_t val, uint32_t mask)
  194. {
  195. return dw_gpio_write(&port_d, val, mask);
  196. }
  197. static int32_t portd_read(uint32_t *val, uint32_t mask)
  198. {
  199. return dw_gpio_read(&port_d, val, mask);
  200. }
  201. static void portd_isr(void *ptr)
  202. {
  203. dw_gpio_isr_handler(&port_d, ptr);
  204. }
  205. static void portd_install(void)
  206. {
  207. DEV_GPIO_PTR port_ptr = &port_d;
  208. DEV_GPIO_INFO_PTR info_ptr = &(port_d.gpio_info);
  209. DW_GPIO_PORT_PTR dw_port_ptr = &(dw_gpio_port_d);
  210. info_ptr->gpio_ctrl = (void *)dw_port_ptr;
  211. info_ptr->opn_cnt = 0;
  212. info_ptr->method = 0;
  213. info_ptr->direction = 0;
  214. info_ptr->extra = 0;
  215. dw_port_ptr->no = DW_GPIO_PORT_D;
  216. dw_port_ptr->regs = (DW_GPIO_REG_PTR)(PERIPHERAL_BASE|REL_REGBASE_GPIO0);
  217. dw_port_ptr->valid_bit_mask = EMSK_GPIO_D_VALID_MASK;
  218. dw_port_ptr->intno = INTNO_GPIO;
  219. dw_port_ptr->int_handler = portd_isr;
  220. dw_port_ptr->gpio_bit_isr = NULL;
  221. port_ptr->gpio_open = portd_open;
  222. port_ptr->gpio_close = portd_close;
  223. port_ptr->gpio_control = portd_control;
  224. port_ptr->gpio_write = portd_write;
  225. port_ptr->gpio_read = portd_read;
  226. }
  227. #endif
  228. DEV_GPIO_PTR gpio_get_dev(int32_t gpio_id)
  229. {
  230. static uint32_t install_flag = 0;
  231. /* intall device objects */
  232. if (install_flag == 0) {
  233. install_flag = 1;
  234. dw_gpio_all_install();
  235. }
  236. switch (gpio_id) {
  237. #if (USE_DW_GPIO_PORT_A)
  238. case DW_GPIO_PORT_A: return &port_a;
  239. #endif
  240. #if (USE_DW_GPIO_PORT_B)
  241. case DW_GPIO_PORT_B: return &port_b;
  242. #endif
  243. #if (USE_DW_GPIO_PORT_C)
  244. case DW_GPIO_PORT_C: return &port_c;
  245. #endif
  246. #if (USE_DW_GPIO_PORT_D)
  247. case DW_GPIO_PORT_D: return &port_d;
  248. #endif
  249. default:
  250. break;
  251. }
  252. return NULL;
  253. }
  254. void dw_gpio_all_install(void)
  255. {
  256. #if (USE_DW_GPIO_PORT_A)
  257. porta_install();
  258. #endif
  259. #if (USE_DW_GPIO_PORT_B)
  260. portb_install();
  261. #endif
  262. #if (USE_DW_GPIO_PORT_C)
  263. portc_install();
  264. #endif
  265. #if (USE_DW_GPIO_PORT_D)
  266. portd_install();
  267. #endif
  268. }