dw_uart_obj.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "inc/embARC_toolchain.h"
  9. #include "inc/embARC_error.h"
  10. #include "inc/arc/arc_exception.h"
  11. #include "device/designware/uart/dw_uart.h"
  12. #include "dw_uart_obj.h"
  13. #include "emsk_hardware.h"
  14. #define DW_UART_FIFO_LEN 32
  15. /**
  16. * \name EMSK DesignWare UART 0 Object Instantiation
  17. * @{
  18. */
  19. #if (USE_DW_UART_0)
  20. static void dw_uart_0_isr(void *ptr);
  21. #define DW_UART_0_RELBASE (REL_REGBASE_UART0) /*!< designware uart 0 relative baseaddr */
  22. #define DW_UART_0_INTNO (INTNO_UART0) /*!< designware uart 0 interrupt number */
  23. DEV_UART dw_uart_0; /*!< designware uart object */
  24. DW_UART_CTRL dw_uart_0_ctrl = { /*!< designware uart 0 ctrl */
  25. 0, CLK_BUS_APB, DW_UART_0_INTNO, (INT_HANDLER)dw_uart_0_isr,
  26. DW_UART_FIFO_LEN, DW_UART_FIFO_LEN, 0
  27. };
  28. /** designware uart 0 open */
  29. static int32_t dw_uart_0_open (uint32_t baud)
  30. {
  31. return dw_uart_open(&dw_uart_0, baud);
  32. }
  33. /** designware uart 0 close */
  34. static int32_t dw_uart_0_close (void)
  35. {
  36. return dw_uart_close(&dw_uart_0);
  37. }
  38. /** designware uart 0 control */
  39. static int32_t dw_uart_0_control (uint32_t ctrl_cmd, void *param)
  40. {
  41. return dw_uart_control(&dw_uart_0, ctrl_cmd, param);
  42. }
  43. /** designware uart 0 write */
  44. static int32_t dw_uart_0_write (const void *data, uint32_t len)
  45. {
  46. return dw_uart_write(&dw_uart_0, data, len);
  47. }
  48. /** designware uart 0 close */
  49. static int32_t dw_uart_0_read (void *data, uint32_t len)
  50. {
  51. return dw_uart_read(&dw_uart_0, data, len);
  52. }
  53. /** designware uart 0 interrupt rountine */
  54. static void dw_uart_0_isr(void *ptr)
  55. {
  56. dw_uart_isr(&dw_uart_0, ptr);
  57. }
  58. /** install designware uart 0 to system */
  59. static void dw_uart_0_install(void)
  60. {
  61. uint32_t uart_abs_base = 0;
  62. DEV_UART *dw_uart_ptr = &dw_uart_0;
  63. DEV_UART_INFO *dw_uart_info_ptr = &(dw_uart_0.uart_info);
  64. DW_UART_CTRL *dw_uart_ctrl_ptr = &dw_uart_0_ctrl;
  65. /**
  66. * get absolute designware base address
  67. */
  68. uart_abs_base = (uint32_t)PERIPHERAL_BASE + DW_UART_0_RELBASE;
  69. dw_uart_ctrl_ptr->dw_uart_regbase = uart_abs_base;
  70. /** uart info init */
  71. dw_uart_info_ptr->uart_ctrl = (void *)dw_uart_ctrl_ptr;
  72. dw_uart_info_ptr->opn_cnt = 0;
  73. dw_uart_info_ptr->status = 0;
  74. dw_uart_info_ptr->baudrate = UART_BAUDRATE_115200; /* default 115200bps */
  75. /** uart dev init */
  76. dw_uart_ptr->uart_open = dw_uart_0_open;
  77. dw_uart_ptr->uart_close = dw_uart_0_close;
  78. dw_uart_ptr->uart_control = dw_uart_0_control;
  79. dw_uart_ptr->uart_write = dw_uart_0_write;
  80. dw_uart_ptr->uart_read = dw_uart_0_read;
  81. }
  82. #endif /* USE_DW_UART_0 */
  83. /** @} end of name */
  84. /**
  85. * \name EMSK DesignWare UART 1 Object Instantiation
  86. * @{
  87. */
  88. #if (USE_DW_UART_1)
  89. static void dw_uart_1_isr(void *ptr);
  90. #define DW_UART_1_RELBASE (REL_REGBASE_UART1) /*!< designware uart 1 relative baseaddr */
  91. #define DW_UART_1_INTNO (INTNO_UART1) /*!< designware uart 1 interrupt number */
  92. DEV_UART dw_uart_1; /*!< designware uart 1 object */
  93. DW_UART_CTRL dw_uart_1_ctrl = { /*!< designware uart 1 ctrl */
  94. 0, CLK_BUS_APB, DW_UART_1_INTNO, (INT_HANDLER)dw_uart_1_isr,
  95. DW_UART_FIFO_LEN, DW_UART_FIFO_LEN, 0
  96. };
  97. /** designware uart 1 open */
  98. static int32_t dw_uart_1_open (uint32_t baud)
  99. {
  100. return dw_uart_open(&dw_uart_1, baud);
  101. }
  102. /** designware uart 1 close */
  103. static int32_t dw_uart_1_close (void)
  104. {
  105. return dw_uart_close(&dw_uart_1);
  106. }
  107. /** designware uart 1 control */
  108. static int32_t dw_uart_1_control (uint32_t ctrl_cmd, void *param)
  109. {
  110. return dw_uart_control(&dw_uart_1, ctrl_cmd, param);
  111. }
  112. /** designware uart 1 write */
  113. static int32_t dw_uart_1_write (const void *data, uint32_t len)
  114. {
  115. return dw_uart_write(&dw_uart_1, data, len);
  116. }
  117. /** designware uart 1 close */
  118. static int32_t dw_uart_1_read (void *data, uint32_t len)
  119. {
  120. return dw_uart_read(&dw_uart_1, data, len);
  121. }
  122. /** designware uart 1 interrupt routine */
  123. static void dw_uart_1_isr(void *ptr)
  124. {
  125. dw_uart_isr(&dw_uart_1, ptr);
  126. }
  127. /** install designware uart 1 to system */
  128. static void dw_uart_1_install(void)
  129. {
  130. uint32_t uart_abs_base = 0;
  131. DEV_UART *dw_uart_ptr = &dw_uart_1;
  132. DEV_UART_INFO *dw_uart_info_ptr = &(dw_uart_1.uart_info);
  133. DW_UART_CTRL *dw_uart_ctrl_ptr = &dw_uart_1_ctrl;
  134. /**
  135. * get absolute designware base address
  136. */
  137. uart_abs_base = (uint32_t)PERIPHERAL_BASE + DW_UART_1_RELBASE;
  138. dw_uart_ctrl_ptr->dw_uart_regbase = uart_abs_base;
  139. /** uart info init */
  140. dw_uart_info_ptr->uart_ctrl = (void *)dw_uart_ctrl_ptr;
  141. dw_uart_info_ptr->opn_cnt = 0;
  142. dw_uart_info_ptr->status = 0;
  143. dw_uart_info_ptr->baudrate = UART_BAUDRATE_115200; /* default 115200bps */
  144. /** uart dev init */
  145. dw_uart_ptr->uart_open = dw_uart_1_open;
  146. dw_uart_ptr->uart_close = dw_uart_1_close;
  147. dw_uart_ptr->uart_control = dw_uart_1_control;
  148. dw_uart_ptr->uart_write = dw_uart_1_write;
  149. dw_uart_ptr->uart_read = dw_uart_1_read;
  150. }
  151. #endif /* USE_DW_UART_1 */
  152. /** @} end of name */
  153. /** get one designware device structure */
  154. DEV_UART_PTR uart_get_dev(int32_t uart_id)
  155. {
  156. static uint32_t install_flag = 0;
  157. /* intall device objects */
  158. if (install_flag == 0) {
  159. install_flag = 1;
  160. dw_uart_all_install();
  161. }
  162. switch (uart_id) {
  163. #if (USE_DW_UART_0)
  164. case DW_UART_0_ID:
  165. return &dw_uart_0;
  166. break;
  167. #endif
  168. #if (USE_DW_UART_1)
  169. case DW_UART_1_ID:
  170. return &dw_uart_1;
  171. break;
  172. #endif
  173. default:
  174. break;
  175. }
  176. return NULL;
  177. }
  178. /**
  179. * \brief install all uart objects
  180. * \note \b MUST be called during system init
  181. */
  182. void dw_uart_all_install(void)
  183. {
  184. #if (USE_DW_UART_0)
  185. dw_uart_0_install();
  186. #endif
  187. #if (USE_DW_UART_1)
  188. dw_uart_1_install();
  189. #endif
  190. }