You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1936 lines
53 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * pru/hal/uart/src/suart_api.c
  3. *
  4. * Copyright (C) 2010 Texas Instruments Incorporated
  5. * Author: Jitendra Kumar <jitendra@mistralsolutions.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation version 2.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  12. * whether express or implied; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. /*
  17. *====================
  18. * Includes
  19. *====================
  20. */
  21. #include "suart_api.h"
  22. #include "suart_pru_regs.h"
  23. #include "pru.h"
  24. #include "omapl_suart_board.h"
  25. #include "suart_utils.h"
  26. #include "suart_err.h"
  27. static unsigned char gUartStatuTable[8];
  28. static arm_pru_iomap pru_arm_iomap;
  29. static int suart_set_pru_id (unsigned int pru_no);
  30. /*
  31. * suart Initialization routine
  32. */
  33. short pru_softuart_init(unsigned int txBaudValue,
  34. unsigned int rxBaudValue,
  35. unsigned int oversampling,
  36. unsigned char *pru_suart_emu_code,
  37. unsigned int fw_size, arm_pru_iomap * arm_iomap_pru)
  38. {
  39. unsigned int omapl_addr;
  40. short status = PRU_SUART_SUCCESS;
  41. short idx;
  42. short retval;
  43. pru_arm_iomap.pru_io_addr = arm_iomap_pru->pru_io_addr;
  44. pru_arm_iomap.mcasp_io_addr = arm_iomap_pru->mcasp_io_addr;
  45. pru_arm_iomap.psc0_io_addr = arm_iomap_pru->psc0_io_addr;
  46. pru_arm_iomap.psc1_io_addr = arm_iomap_pru->psc1_io_addr;
  47. pru_arm_iomap.syscfg_io_addr = arm_iomap_pru->syscfg_io_addr;
  48. pru_arm_iomap.pFifoBufferPhysBase = arm_iomap_pru->pFifoBufferPhysBase;
  49. pru_arm_iomap.pFifoBufferVirtBase = arm_iomap_pru->pFifoBufferVirtBase;
  50. omapl_addr = (unsigned int)arm_iomap_pru->syscfg_io_addr;
  51. omapl_addr = (unsigned int)arm_iomap_pru->psc1_io_addr;
  52. suart_mcasp_psc_enable(omapl_addr);
  53. omapl_addr = (unsigned int)arm_iomap_pru->mcasp_io_addr;
  54. // Configure McASP0
  55. suart_mcasp_config(omapl_addr, txBaudValue, rxBaudValue, oversampling,
  56. arm_iomap_pru);
  57. retval = arm_to_pru_intr_init();
  58. if (-1 == retval) {
  59. return status;
  60. }
  61. pru_enable(0, arm_iomap_pru);
  62. pru_enable(1, arm_iomap_pru);
  63. pru_load(PRU_NUM0, (unsigned int *)pru_suart_emu_code,
  64. (fw_size / sizeof(unsigned int)), arm_iomap_pru);
  65. pru_load(PRU_NUM1, (unsigned int *)pru_suart_emu_code,
  66. (fw_size / sizeof(unsigned int)), arm_iomap_pru);
  67. suart_set_pru_id(0);
  68. suart_set_pru_id(1);
  69. pru_run(PRU_NUM0, arm_iomap_pru);
  70. // pru_run(PRU_NUM1, arm_iomap_pru);
  71. /* Initialize gUartStatuTable */
  72. for (idx = 0; idx < 8; idx++) {
  73. gUartStatuTable[idx] = ePRU_SUART_UART_FREE;
  74. }
  75. return status;
  76. }
  77. void pru_set_fifo_timeout(Uint32 timeout)
  78. {
  79. /* PRU 0 */
  80. pru_ram_write_data(PRU_SUART_PRU0_IDLE_TIMEOUT_OFFSET,
  81. (Uint8 *)&timeout, 2, &pru_arm_iomap);
  82. /* PRU 1 */
  83. pru_ram_write_data(PRU_SUART_PRU1_IDLE_TIMEOUT_OFFSET,
  84. (Uint8 *)&timeout, 2, &pru_arm_iomap);
  85. }
  86. short pru_softuart_deinit(void)
  87. {
  88. unsigned int offset;
  89. short s16retval = 0;
  90. unsigned int u32value = 0;
  91. offset =
  92. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_STATCLRINT1 &
  93. 0xFFFF);
  94. u32value = 0xFFFFFFFF;
  95. s16retval =
  96. pru_ram_write_data_4byte(offset, (unsigned int *)&u32value, 1);
  97. if (-1 == s16retval) {
  98. return -1;
  99. }
  100. offset =
  101. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_STATCLRINT0 &
  102. 0xFFFF);
  103. u32value = 0xFFFFFFFF;
  104. s16retval =
  105. pru_ram_write_data_4byte(offset, (unsigned int *)&u32value, 1);
  106. if (-1 == s16retval) {
  107. return -1;
  108. }
  109. pru_disable(&pru_arm_iomap);
  110. return PRU_SUART_SUCCESS;
  111. }
  112. /*
  113. * suart Instance open routine
  114. */
  115. short pru_softuart_open(suart_handle hSuart)
  116. {
  117. short status = PRU_SUART_SUCCESS;
  118. switch (hSuart->uartNum) {
  119. /* ************ PRU 0 ************** */
  120. case PRU_SUART_UART1:
  121. if (gUartStatuTable[PRU_SUART_UART1 - 1] ==
  122. ePRU_SUART_UART_IN_USE) {
  123. status = SUART_UART_IN_USE;
  124. return status;
  125. } else {
  126. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  127. hSuart->uartType = PRU_SUART1_CONFIG_DUPLEX;
  128. hSuart->uartTxChannel = PRU_SUART1_CONFIG_TX_SER;
  129. hSuart->uartRxChannel = PRU_SUART1_CONFIG_RX_SER;
  130. gUartStatuTable[PRU_SUART_UART1 - 1] =
  131. ePRU_SUART_UART_IN_USE;
  132. }
  133. break;
  134. case PRU_SUART_UART2:
  135. if (gUartStatuTable[PRU_SUART_UART2 - 1] ==
  136. ePRU_SUART_UART_IN_USE) {
  137. status = SUART_UART_IN_USE;
  138. return status;
  139. } else {
  140. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  141. hSuart->uartType = PRU_SUART2_CONFIG_DUPLEX;
  142. hSuart->uartTxChannel = PRU_SUART2_CONFIG_TX_SER;
  143. hSuart->uartRxChannel = PRU_SUART2_CONFIG_RX_SER;
  144. gUartStatuTable[PRU_SUART_UART2 - 1] =
  145. ePRU_SUART_UART_IN_USE;
  146. }
  147. break;
  148. case PRU_SUART_UART3:
  149. if (gUartStatuTable[PRU_SUART_UART3 - 1] ==
  150. ePRU_SUART_UART_IN_USE) {
  151. status = SUART_UART_IN_USE;
  152. return status;
  153. } else {
  154. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  155. hSuart->uartType = PRU_SUART3_CONFIG_DUPLEX;
  156. hSuart->uartTxChannel = PRU_SUART3_CONFIG_TX_SER;
  157. hSuart->uartRxChannel = PRU_SUART3_CONFIG_RX_SER;
  158. gUartStatuTable[PRU_SUART_UART3 - 1] =
  159. ePRU_SUART_UART_IN_USE;
  160. }
  161. break;
  162. case PRU_SUART_UART4:
  163. if (gUartStatuTable[PRU_SUART_UART4 - 1] ==
  164. ePRU_SUART_UART_IN_USE) {
  165. status = SUART_UART_IN_USE;
  166. return status;
  167. } else {
  168. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  169. hSuart->uartType = PRU_SUART4_CONFIG_DUPLEX;
  170. hSuart->uartTxChannel = PRU_SUART4_CONFIG_TX_SER;
  171. hSuart->uartRxChannel = PRU_SUART4_CONFIG_RX_SER;
  172. gUartStatuTable[PRU_SUART_UART4 - 1] =
  173. ePRU_SUART_UART_IN_USE;
  174. }
  175. break;
  176. /* ************ PRU 1 ************** */
  177. case PRU_SUART_UART5:
  178. if (gUartStatuTable[PRU_SUART_UART5 - 1] ==
  179. ePRU_SUART_UART_IN_USE) {
  180. status = SUART_UART_IN_USE;
  181. return status;
  182. } else {
  183. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  184. hSuart->uartType = PRU_SUART5_CONFIG_DUPLEX;
  185. hSuart->uartTxChannel = PRU_SUART5_CONFIG_TX_SER;
  186. hSuart->uartRxChannel = PRU_SUART5_CONFIG_RX_SER;
  187. gUartStatuTable[PRU_SUART_UART5 - 1] =
  188. ePRU_SUART_UART_IN_USE;
  189. }
  190. break;
  191. case PRU_SUART_UART6:
  192. if (gUartStatuTable[PRU_SUART_UART6 - 1] ==
  193. ePRU_SUART_UART_IN_USE) {
  194. status = SUART_UART_IN_USE;
  195. return status;
  196. } else {
  197. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  198. hSuart->uartType = PRU_SUART6_CONFIG_DUPLEX;
  199. hSuart->uartTxChannel = PRU_SUART6_CONFIG_TX_SER;
  200. hSuart->uartRxChannel = PRU_SUART6_CONFIG_RX_SER;
  201. gUartStatuTable[PRU_SUART_UART6 - 1] =
  202. ePRU_SUART_UART_IN_USE;
  203. }
  204. break;
  205. case PRU_SUART_UART7:
  206. if (gUartStatuTable[PRU_SUART_UART7 - 1] ==
  207. ePRU_SUART_UART_IN_USE) {
  208. status = SUART_UART_IN_USE;
  209. return status;
  210. } else {
  211. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  212. hSuart->uartType = PRU_SUART7_CONFIG_DUPLEX;
  213. hSuart->uartTxChannel = PRU_SUART7_CONFIG_TX_SER;
  214. hSuart->uartRxChannel = PRU_SUART7_CONFIG_RX_SER;
  215. gUartStatuTable[PRU_SUART_UART7 - 1] =
  216. ePRU_SUART_UART_IN_USE;
  217. }
  218. break;
  219. case PRU_SUART_UART8:
  220. if (gUartStatuTable[PRU_SUART_UART8 - 1] ==
  221. ePRU_SUART_UART_IN_USE) {
  222. status = SUART_UART_IN_USE;
  223. return status;
  224. } else {
  225. hSuart->uartStatus = ePRU_SUART_UART_IN_USE;
  226. hSuart->uartType = PRU_SUART8_CONFIG_DUPLEX;
  227. hSuart->uartTxChannel = PRU_SUART8_CONFIG_TX_SER;
  228. hSuart->uartRxChannel = PRU_SUART8_CONFIG_RX_SER;
  229. gUartStatuTable[PRU_SUART_UART8 - 1] =
  230. ePRU_SUART_UART_IN_USE;
  231. }
  232. break;
  233. default:
  234. /* return invalid UART */
  235. status = SUART_INVALID_UART_NUM;
  236. break;
  237. }
  238. return (status);
  239. }
  240. /*
  241. * suart instance close routine
  242. */
  243. short pru_softuart_close(suart_handle hUart)
  244. {
  245. short status = SUART_SUCCESS;
  246. if (hUart == NULL) {
  247. return (PRU_SUART_ERR_HANDLE_INVALID);
  248. } else {
  249. gUartStatuTable[hUart->uartNum - 1] = ePRU_SUART_UART_FREE;
  250. /* Reset the Instance to Invalid */
  251. hUart->uartNum = PRU_SUART_UARTx_INVALID;
  252. hUart->uartStatus = ePRU_SUART_UART_FREE;
  253. }
  254. return (status);
  255. }
  256. /*
  257. * suart routine for setting relative baud rate
  258. */
  259. short pru_softuart_setbaud
  260. (suart_handle hUart,
  261. unsigned short txClkDivisor,
  262. unsigned short rxClkDivisor)
  263. {
  264. unsigned int offset;
  265. unsigned int pruOffset;
  266. short status = SUART_SUCCESS;
  267. unsigned short chNum;
  268. unsigned short regval = 0;
  269. if (hUart == NULL)
  270. {
  271. return (PRU_SUART_ERR_HANDLE_INVALID);
  272. }
  273. /* Set the clock divisor value into the McASP */
  274. if ((txClkDivisor > 385) || (txClkDivisor == 0))
  275. {
  276. return SUART_INVALID_CLKDIVISOR;
  277. }
  278. if ((rxClkDivisor > 385) || (rxClkDivisor == 0))
  279. {
  280. return SUART_INVALID_CLKDIVISOR;
  281. }
  282. /* channel starts from 0 and uart instance starts from 1 */
  283. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  284. if (hUart->uartNum <= 4)
  285. {
  286. /* PRU0 */
  287. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  288. } else {
  289. /* PRU1 */
  290. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  291. /* First 8 channel corresponds to PRU0 */
  292. chNum -= 8;
  293. }
  294. // /* channel starts from 0 and uart instance starts from 1 */
  295. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  296. if (txClkDivisor != 0)
  297. {
  298. offset =
  299. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  300. PRU_SUART_CH_CONFIG1_OFFSET;
  301. pru_ram_read_data(offset, (Uint8 *) &regval, 2,
  302. &pru_arm_iomap);
  303. regval &= (~0x3FF);
  304. regval |= txClkDivisor;
  305. pru_ram_write_data(offset, (Uint8 *) &regval, 2,
  306. &pru_arm_iomap);
  307. }
  308. chNum++;
  309. regval = 0;
  310. if (rxClkDivisor != 0)
  311. {
  312. offset =
  313. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  314. PRU_SUART_CH_CONFIG1_OFFSET;
  315. pru_ram_read_data(offset, (Uint8 *) &regval, 2,
  316. &pru_arm_iomap);
  317. regval &= (~0x3FF);
  318. regval |= txClkDivisor;
  319. pru_ram_write_data(offset, (Uint8 *) &regval, 2,
  320. &pru_arm_iomap);
  321. }
  322. return status;
  323. }
  324. /*
  325. * suart routine for setting number of bits per character for a specific uart
  326. */
  327. short pru_softuart_setdatabits
  328. (suart_handle hUart, unsigned short txDataBits, unsigned short rxDataBits) {
  329. unsigned int offset;
  330. unsigned int pruOffset;
  331. short status = SUART_SUCCESS;
  332. unsigned short chNum;
  333. unsigned int reg_val;
  334. if (hUart == NULL) {
  335. return (PRU_SUART_ERR_HANDLE_INVALID);
  336. }
  337. /*
  338. * NOTE:
  339. * The supported data bits are 6, 7, 8, 9, 10, 11 and 12 bits per character
  340. */
  341. if ((txDataBits < ePRU_SUART_DATA_BITS6) || (txDataBits > ePRU_SUART_DATA_BITS12)) {
  342. return PRU_SUART_ERR_PARAMETER_INVALID;
  343. }
  344. if ((rxDataBits < ePRU_SUART_DATA_BITS6) || (rxDataBits > ePRU_SUART_DATA_BITS12)) {
  345. return PRU_SUART_ERR_PARAMETER_INVALID;
  346. }
  347. /* channel starts from 0 and uart instance starts from 1 */
  348. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  349. if (hUart->uartNum <= 4) {
  350. /* PRU0 */
  351. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  352. } else {
  353. /* PRU1 */
  354. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  355. /* First 8 channel corresponds to PRU0 */
  356. chNum -= 8;
  357. }
  358. // /* channel starts from 0 and uart instance starts from 1 */
  359. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  360. if (txDataBits != 0) {
  361. offset =
  362. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  363. PRU_SUART_CH_CONFIG2_OFFSET;
  364. pru_ram_read_data(offset, (Uint8 *) &reg_val, 1,
  365. &pru_arm_iomap);
  366. reg_val &= ~(0xF);
  367. reg_val |= txDataBits;
  368. pru_ram_write_data(offset, (Uint8 *) &reg_val, 1,
  369. &pru_arm_iomap);
  370. }
  371. chNum++;
  372. if (rxDataBits != 0) {
  373. offset =
  374. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  375. PRU_SUART_CH_CONFIG2_OFFSET;
  376. pru_ram_read_data(offset, (Uint8 *) &reg_val, 1,
  377. &pru_arm_iomap);
  378. reg_val &= ~(0xF);
  379. reg_val |= rxDataBits;
  380. pru_ram_write_data(offset, (Uint8 *) & rxDataBits, 1,
  381. &pru_arm_iomap);
  382. }
  383. return (status);
  384. }
  385. /*
  386. * suart routine to configure specific uart
  387. */
  388. short pru_softuart_setconfig(suart_handle hUart, suart_config * configUart)
  389. {
  390. unsigned int offset;
  391. unsigned int pruOffset;
  392. short status = SUART_SUCCESS;
  393. unsigned short chNum;
  394. unsigned short regVal = 0;
  395. if (hUart == NULL) {
  396. return (PRU_SUART_ERR_HANDLE_INVALID);
  397. }
  398. /*
  399. * NOTE:
  400. * Dependent baud rate for the given UART, the value MUST BE LESS THAN OR
  401. * EQUAL TO 64, preScalarValue <= 64
  402. */
  403. /* Validate the value of relative buad rate */
  404. if ((configUart->txClkDivisor > 384) || (configUart->rxClkDivisor > 384)) {
  405. return SUART_INVALID_CLKDIVISOR;
  406. }
  407. /* Validate the bits per character */
  408. if ((configUart->txBitsPerChar < 8) || (configUart->txBitsPerChar > 14)) {
  409. return PRU_SUART_ERR_PARAMETER_INVALID;
  410. }
  411. if ((configUart->rxBitsPerChar < 8) || (configUart->rxBitsPerChar > 14)) {
  412. return PRU_SUART_ERR_PARAMETER_INVALID;
  413. }
  414. /* channel starts from 0 and uart instance starts from 1 */
  415. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  416. if (hUart->uartNum <= 4) {
  417. /* PRU0 */
  418. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  419. } else {
  420. /* PRU1 */
  421. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  422. chNum -= 8;
  423. }
  424. // /* channel starts from 0 and uart instance starts from 1 */
  425. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  426. /* Configuring the Transmit part of the given UART */
  427. if (configUart->TXSerializer != PRU_SUART_SERIALIZER_NONE) {
  428. suart_mcasp_tx_serialzier_set (configUart->TXSerializer, &pru_arm_iomap);
  429. /* Configuring TX serializer */
  430. offset =
  431. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  432. PRU_SUART_CH_CTRL_OFFSET;
  433. pru_ram_read_data(offset, (Uint8 *) & regVal, 2,
  434. &pru_arm_iomap);
  435. regVal =
  436. regVal | (configUart->TXSerializer <<
  437. PRU_SUART_CH_CTRL_SR_SHIFT);
  438. pru_ram_write_data(offset, (Uint8 *) & regVal, 2,
  439. &pru_arm_iomap);
  440. /* Configuring the Transmit part of the given UART */
  441. /* Configuring TX prescalar value */
  442. offset =
  443. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  444. PRU_SUART_CH_CONFIG1_OFFSET;
  445. pru_ram_read_data(offset, (Uint8 *) & regVal, 2,
  446. &pru_arm_iomap);
  447. regVal =
  448. regVal | (configUart->txClkDivisor <<
  449. PRU_SUART_CH_CONFIG1_DIVISOR_SHIFT);
  450. pru_ram_write_data(offset, (Uint8 *) & regVal, 2,
  451. &pru_arm_iomap);
  452. /* Configuring TX bits per character value */
  453. offset =
  454. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  455. PRU_SUART_CH_CONFIG2_OFFSET;
  456. pru_ram_read_data(offset, (Uint8 *) & regVal, 2,
  457. &pru_arm_iomap);
  458. regVal =
  459. regVal | (configUart->txBitsPerChar <<
  460. PRU_SUART_CH_CONFIG2_BITPERCHAR_SHIFT);
  461. pru_ram_write_data(offset, (Uint8 *) & regVal, 2,
  462. &pru_arm_iomap);
  463. }
  464. chNum++;
  465. /* Configuring the Transmit part of the given UART */
  466. if (configUart->RXSerializer != PRU_SUART_SERIALIZER_NONE) {
  467. /* Configuring RX serializer */
  468. offset =
  469. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  470. PRU_SUART_CH_CTRL_OFFSET;
  471. pru_ram_read_data(offset, (Uint8 *) & regVal, 2,
  472. &pru_arm_iomap);
  473. regVal =
  474. pruOffset | (configUart->RXSerializer <<
  475. PRU_SUART_CH_CTRL_SR_SHIFT);
  476. pru_ram_write_data(offset, (Uint8 *) & regVal, 2,
  477. &pru_arm_iomap);
  478. /* Configuring RX prescalar value and Oversampling */
  479. offset =
  480. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  481. PRU_SUART_CH_CONFIG1_OFFSET;
  482. pru_ram_read_data(offset, (Uint8 *) & regVal, 2,
  483. &pru_arm_iomap);
  484. regVal =
  485. regVal | (configUart->rxClkDivisor <<
  486. PRU_SUART_CH_CONFIG1_DIVISOR_SHIFT) |
  487. (configUart->Oversampling <<
  488. PRU_SUART_CH_CONFIG1_OVS_SHIFT);
  489. pru_ram_write_data(offset, (Uint8 *) & regVal, 2,
  490. &pru_arm_iomap);
  491. /* Configuring RX bits per character value */
  492. offset =
  493. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  494. PRU_SUART_CH_CONFIG2_OFFSET;
  495. pru_ram_read_data(offset, (Uint8 *) & regVal, 2,
  496. &pru_arm_iomap);
  497. regVal =
  498. regVal | (configUart->rxBitsPerChar <<
  499. PRU_SUART_CH_CONFIG1_DIVISOR_SHIFT);
  500. pru_ram_write_data(offset, (Uint8 *) & regVal, 2,
  501. &pru_arm_iomap);
  502. }
  503. return (status);
  504. }
  505. /*
  506. * suart routine for getting the number of bytes transfered
  507. */
  508. short pru_softuart_getTxDataLen(suart_handle hUart)
  509. {
  510. unsigned int offset;
  511. unsigned int pruOffset;
  512. unsigned short chNum;
  513. unsigned short u16ReadValue = 0;
  514. if (hUart == NULL) {
  515. return (PRU_SUART_ERR_HANDLE_INVALID);
  516. }
  517. /* channel starts from 0 and uart instance starts from 1 */
  518. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  519. if (hUart->uartNum <= 4) {
  520. /* PRU0 */
  521. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  522. } else {
  523. /* PRU1 */
  524. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  525. /* First 8 channel corresponds to PRU0 */
  526. chNum -= 8;
  527. }
  528. // /* channel starts from 0 and uart instance starts from 1 */
  529. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  530. /* Transmit channel number is (UartNum * 2) - 2 */
  531. offset =
  532. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  533. PRU_SUART_CH_CONFIG2_OFFSET;
  534. pru_ram_read_data(offset, (Uint8 *) & u16ReadValue, 2, &pru_arm_iomap);
  535. u16ReadValue = ((u16ReadValue & PRU_SUART_CH_CONFIG1_DIVISOR_MASK) >>
  536. PRU_SUART_CH_CONFIG2_DATALEN_SHIFT);
  537. return (u16ReadValue);
  538. }
  539. /*
  540. * suart routine for getting the number of bytes received
  541. */
  542. short pru_softuart_getRxDataLen(suart_handle hUart)
  543. {
  544. unsigned int offset;
  545. unsigned int pruOffset;
  546. unsigned short chNum;
  547. unsigned short u16ReadValue = 0;
  548. if (hUart == NULL) {
  549. return (PRU_SUART_ERR_HANDLE_INVALID);
  550. }
  551. /* channel starts from 0 and uart instance starts from 1 */
  552. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  553. if (hUart->uartNum <= 4) {
  554. /* PRU0 */
  555. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  556. } else {
  557. /* PRU1 */
  558. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  559. /* First 8 channel corresponds to PRU0 */
  560. chNum -= 8;
  561. }
  562. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 1;
  563. chNum++;
  564. // /* Transmit channel number is (UartNum * 2) - 1 */
  565. offset =
  566. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  567. PRU_SUART_CH_CONFIG2_OFFSET;
  568. pru_ram_read_data(offset, (Uint8 *) & u16ReadValue, 2, &pru_arm_iomap);
  569. u16ReadValue = ((u16ReadValue & PRU_SUART_CH_CONFIG1_DIVISOR_MASK) >>
  570. PRU_SUART_CH_CONFIG2_DATALEN_SHIFT);
  571. return (u16ReadValue);
  572. }
  573. /*
  574. * suart routine to get the configuration information from a specific uart
  575. */
  576. short pru_softuart_getconfig(suart_handle hUart, suart_config * configUart)
  577. {
  578. unsigned int offset;
  579. unsigned int pruOffset;
  580. unsigned short chNum;
  581. unsigned short regVal = 0;
  582. short status = SUART_SUCCESS;
  583. if (hUart == NULL) {
  584. return (PRU_SUART_ERR_HANDLE_INVALID);
  585. }
  586. /*
  587. * NOTE:
  588. * Dependent baud rate for the given UART, the value MUST BE LESS THAN OR
  589. * EQUAL TO 64, preScalarValue <= 64
  590. */
  591. /* channel starts from 0 and uart instance starts from 1 */
  592. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  593. if (hUart->uartNum <= 4) {
  594. /* PRU0 */
  595. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  596. } else {
  597. /* PRU1 */
  598. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  599. /* First 8 channel corresponds to PRU0 */
  600. chNum -= 8;
  601. }
  602. /* channel starts from 0 and uart instance starts from 1 */
  603. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  604. /* Configuring the Transmit part of the given UART */
  605. /* Configuring TX serializer */
  606. offset =
  607. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  608. PRU_SUART_CH_CTRL_OFFSET;
  609. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  610. configUart->TXSerializer =
  611. ((regVal & PRU_SUART_CH_CTRL_SR_MASK) >>
  612. PRU_SUART_CH_CTRL_SR_SHIFT);
  613. /* Configuring TX prescalar value */
  614. offset =
  615. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  616. PRU_SUART_CH_CONFIG1_OFFSET;
  617. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  618. configUart->txClkDivisor =
  619. ((regVal & PRU_SUART_CH_CONFIG1_DIVISOR_MASK) >>
  620. PRU_SUART_CH_CONFIG1_DIVISOR_SHIFT);
  621. /* Configuring TX bits per character value */
  622. offset =
  623. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  624. PRU_SUART_CH_CONFIG2_OFFSET;
  625. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  626. configUart->txBitsPerChar =
  627. ((regVal & PRU_SUART_CH_CONFIG1_DIVISOR_MASK) >>
  628. PRU_SUART_CH_CONFIG1_DIVISOR_SHIFT);
  629. chNum++;
  630. /* Configuring the Transmit part of the given UART */
  631. /* Configuring RX serializer */
  632. offset =
  633. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  634. PRU_SUART_CH_CTRL_OFFSET;
  635. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  636. configUart->RXSerializer =
  637. ((regVal & PRU_SUART_CH_CTRL_SR_MASK) >>
  638. PRU_SUART_CH_CTRL_SR_SHIFT);
  639. /* Configuring RX prescalar value and Oversampling */
  640. offset =
  641. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  642. PRU_SUART_CH_CONFIG1_OFFSET;
  643. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  644. configUart->rxClkDivisor =
  645. ((regVal & PRU_SUART_CH_CONFIG1_DIVISOR_MASK) >>
  646. PRU_SUART_CH_CONFIG1_DIVISOR_SHIFT);
  647. configUart->Oversampling =
  648. ((regVal & PRU_SUART_CH_CONFIG1_OVS_MASK) >>
  649. PRU_SUART_CH_CONFIG1_OVS_SHIFT);
  650. /* Configuring RX bits per character value */
  651. offset =
  652. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  653. PRU_SUART_CH_CONFIG2_OFFSET;
  654. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  655. configUart->rxBitsPerChar =
  656. ((regVal & PRU_SUART_CH_CONFIG1_DIVISOR_MASK) >>
  657. PRU_SUART_CH_CONFIG1_DIVISOR_SHIFT);
  658. return (status);
  659. }
  660. /*
  661. * suart data transmit routine
  662. */
  663. short pru_softuart_write
  664. (suart_handle hUart, unsigned int *ptTxDataBuf, unsigned short dataLen) {
  665. unsigned int offset = 0;
  666. unsigned int pruOffset;
  667. short status = SUART_SUCCESS;
  668. unsigned short chNum;
  669. unsigned short regVal = 0;
  670. if (hUart == NULL) {
  671. return (PRU_SUART_ERR_HANDLE_INVALID);
  672. }
  673. /* channel starts from 0 and uart instance starts from 1 */
  674. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  675. if (hUart->uartNum <= 4) {
  676. /* PRU0 */
  677. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  678. } else {
  679. /* PRU1 */
  680. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  681. /* First 8 channel corresponds to PRU0 */
  682. chNum -= 8;
  683. }
  684. // /* channel starts from 0 and uart instance starts from 1 */
  685. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  686. /* Writing data length to SUART channel register */
  687. offset =
  688. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  689. PRU_SUART_CH_CONFIG2_OFFSET;
  690. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  691. regVal &= ~PRU_SUART_CH_CONFIG2_DATALEN_MASK;
  692. regVal = regVal | (dataLen << PRU_SUART_CH_CONFIG2_DATALEN_SHIFT);
  693. pru_ram_write_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  694. /* Writing the data pointer to channel TX data pointer */
  695. offset =
  696. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  697. PRU_SUART_CH_TXRXDATA_OFFSET;
  698. pru_ram_write_data(offset, (Uint8 *) ptTxDataBuf, 4, &pru_arm_iomap);
  699. /* Service Request to PRU */
  700. offset =
  701. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  702. PRU_SUART_CH_CTRL_OFFSET;
  703. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  704. regVal &= ~(PRU_SUART_CH_CTRL_MODE_MASK |PRU_SUART_CH_CTRL_SREQ_MASK);
  705. regVal |= (PRU_SUART_CH_CTRL_TX_MODE << PRU_SUART_CH_CTRL_MODE_SHIFT) |
  706. (PRU_SUART_CH_CTRL_SREQ << PRU_SUART_CH_CTRL_SREQ_SHIFT);
  707. pru_ram_write_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  708. /* generate ARM->PRU event */
  709. suart_arm_to_pru_intr(hUart->uartNum);
  710. return (status);
  711. }
  712. /*
  713. * suart data receive routine
  714. */
  715. short pru_softuart_read
  716. (suart_handle hUart, unsigned int *ptDataBuf, unsigned short dataLen) {
  717. unsigned int offset = 0;
  718. unsigned int pruOffset;
  719. short status = SUART_SUCCESS;
  720. unsigned short chNum;
  721. unsigned short regVal = 0;
  722. if (hUart == NULL) {
  723. return (PRU_SUART_ERR_HANDLE_INVALID);
  724. }
  725. /* channel starts from 0 and uart instance starts from 1 */
  726. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  727. if (hUart->uartNum <= 4) {
  728. /* PRU0 */
  729. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  730. } else {
  731. /* PRU1 */
  732. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  733. /* First 8 channel corresponds to PRU0 */
  734. chNum -= 8;
  735. }
  736. // /* channel starts from 0 and uart instance starts from 1 */
  737. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  738. chNum++;
  739. /* Writing data length to SUART channel register */
  740. offset =
  741. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  742. PRU_SUART_CH_CONFIG2_OFFSET;
  743. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  744. regVal &= ~PRU_SUART_CH_CONFIG2_DATALEN_MASK;
  745. regVal = regVal | (dataLen << PRU_SUART_CH_CONFIG2_DATALEN_SHIFT);
  746. pru_ram_write_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  747. /* Writing the data pointer to channel RX data pointer */
  748. offset =
  749. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  750. PRU_SUART_CH_TXRXDATA_OFFSET;
  751. pru_ram_write_data(offset, (Uint8 *) ptDataBuf, 4, &pru_arm_iomap);
  752. /* Service Request to PRU */
  753. offset =
  754. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  755. PRU_SUART_CH_CTRL_OFFSET;
  756. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  757. regVal &= ~(PRU_SUART_CH_CTRL_MODE_MASK |PRU_SUART_CH_CTRL_SREQ_MASK);
  758. regVal |= ( PRU_SUART_CH_CTRL_RX_MODE << PRU_SUART_CH_CTRL_MODE_SHIFT) |
  759. (PRU_SUART_CH_CTRL_SREQ << PRU_SUART_CH_CTRL_SREQ_SHIFT);
  760. pru_ram_write_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  761. /* enable the timeout interrupt */
  762. suart_intr_setmask (hUart->uartNum, PRU_RX_INTR, CHN_TXRX_IE_MASK_TIMEOUT);
  763. /* generate ARM->PRU event */
  764. suart_arm_to_pru_intr(hUart->uartNum);
  765. return (status);
  766. }
  767. /*
  768. * suart routine to read the data from the RX FIFO
  769. */
  770. short pru_softuart_read_data (
  771. suart_handle hUart,
  772. Uint8 * pDataBuffer,
  773. Int32 s32MaxLen,
  774. Uint32 * pu32DataRead)
  775. {
  776. short retVal = PRU_SUART_SUCCESS;
  777. Uint8 * pu8SrcAddr = NULL;
  778. Uint32 u32DataRead = 0;
  779. Uint32 u32DataLen = 0;
  780. Uint32 u32CharLen = 0;
  781. unsigned int offset = 0;
  782. unsigned int pruOffset;
  783. unsigned short chNum;
  784. unsigned short u16Status = 0;
  785. if (hUart == NULL) {
  786. return (PRU_SUART_ERR_HANDLE_INVALID);
  787. }
  788. /* channel starts from 0 and uart instance starts from 1 */
  789. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  790. if (hUart->uartNum <= 4) {
  791. /* PRU0 */
  792. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  793. } else {
  794. /* PRU1 */
  795. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  796. /* First 8 channel corresponds to PRU0 */
  797. chNum -= 8;
  798. }
  799. // /* channel starts from 0 and uart instance starts from 1 */
  800. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  801. chNum++;
  802. /* Get the data pointer from channel RX data pointer */
  803. offset =
  804. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  805. PRU_SUART_CH_TXRXDATA_OFFSET;
  806. pru_ram_read_data(offset, (Uint8 *) &pu8SrcAddr, 4, &pru_arm_iomap);
  807. /* Reading data length from SUART channel register */
  808. offset = pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  809. PRU_SUART_CH_CONFIG2_OFFSET;
  810. pru_ram_read_data(offset, (Uint8 *) & u32DataLen, 2, &pru_arm_iomap);
  811. /* read the character length */
  812. u32CharLen = u32DataLen & PRU_SUART_CH_CONFIG2_BITPERCHAR_MASK;
  813. u32CharLen -= 2; /* remove the START & STOP bit */
  814. u32DataLen &= PRU_SUART_CH_CONFIG2_DATALEN_MASK;
  815. u32DataLen = u32DataLen >> PRU_SUART_CH_CONFIG2_DATALEN_SHIFT;
  816. u32DataLen ++;
  817. /* if the character length is greater than 8, then the size doubles */
  818. if (u32CharLen > 8)
  819. {
  820. u32DataLen *= 2;
  821. }
  822. /* Check if the time-out had occured. If, yes, then we need to find the
  823. * number of bytes read from PRU. Else, we need to read the requested bytes
  824. */
  825. offset = pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  826. PRU_SUART_CH_TXRXSTATUS_OFFSET;
  827. pru_ram_read_data(offset, (Uint8 *) & u16Status, 1, &pru_arm_iomap);
  828. if (CHN_TXRX_STATUS_TIMEOUT == (u16Status & CHN_TXRX_STATUS_TIMEOUT))
  829. {
  830. /* determine the number of bytes read into the FIFO */
  831. offset = pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  832. PRU_SUART_CH_BYTESDONECNTR_OFFSET;
  833. pru_ram_read_data(offset, (Uint8 *) & u32DataRead, 1, &pru_arm_iomap);
  834. /* if the character length is greater than 8, then the size doubles */
  835. if (u32CharLen > 8)
  836. {
  837. u32DataRead *= 2;
  838. }
  839. /* the data corresponding is loaded in second half during the timeout */
  840. if (u32DataRead > u32DataLen)
  841. {
  842. u32DataRead -= u32DataLen;
  843. pu8SrcAddr += (u32DataLen + 1);
  844. }
  845. pru_softuart_clrRxFifo (hUart);
  846. }
  847. else
  848. {
  849. u32DataRead = u32DataLen;
  850. /* Determine the buffer index by reading the FIFO_OddEven flag*/
  851. if (u16Status & CHN_TXRX_STATUS_CMPLT)
  852. {
  853. /* if the bit is set, the data is in the first half of the FIFO else
  854. * the data is in the second half
  855. */
  856. pu8SrcAddr += u32DataLen;
  857. }
  858. }
  859. /* we should be copying only max len given by the application */
  860. if (u32DataRead > s32MaxLen)
  861. {
  862. u32DataRead = s32MaxLen;
  863. }
  864. /* evaluate the virtual address of the FIFO address based on the physical addr */
  865. pu8SrcAddr = (Uint8 *) ((Uint32) pu8SrcAddr - (Uint32) pru_arm_iomap.pFifoBufferPhysBase +
  866. (Uint32) pru_arm_iomap.pFifoBufferVirtBase);
  867. /* Now we have both the data length and the source address. copy */
  868. for (offset = 0; offset < u32DataRead; offset++)
  869. {
  870. * pDataBuffer++ = * pu8SrcAddr++;
  871. }
  872. * pu32DataRead = u32DataRead;
  873. retVal = PRU_SUART_SUCCESS;
  874. return (retVal);
  875. }
  876. /*
  877. * suart routine to disable the receive functionality. This routine stops the PRU
  878. * from receiving on selected UART and also disables the McASP serializer corresponding
  879. * to this UART Rx line.
  880. */
  881. short pru_softuart_stopReceive (suart_handle hUart)
  882. {
  883. unsigned short status = SUART_SUCCESS;
  884. unsigned int offset;
  885. unsigned int pruOffset;
  886. unsigned short chNum;
  887. unsigned short u16Status;
  888. if (hUart == NULL) {
  889. return (PRU_SUART_ERR_HANDLE_INVALID);
  890. }
  891. /* channel starts from 0 and uart instance starts from 1 */
  892. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  893. if (hUart->uartNum <= 4) {
  894. /* PRU0 */
  895. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  896. } else {
  897. /* PRU1 */
  898. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  899. /* First 8 channel corresponds to PRU0 */
  900. chNum -= 8;
  901. }
  902. /* channel starts from 0 and uart instance starts from 1 */
  903. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  904. chNum++;
  905. /* read the existing value of status flag */
  906. offset = pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  907. PRU_SUART_CH_TXRXSTATUS_OFFSET;
  908. pru_ram_read_data(offset, (Uint8 *) &u16Status, 2, &pru_arm_iomap);
  909. /* we need to clear the busy bit corresponding to this receive channel */
  910. u16Status &= ~(CHN_TXRX_STATUS_RDY);
  911. pru_ram_write_data(offset, (Uint8 *) & u16Status, 2, &pru_arm_iomap);
  912. /* get the serizlizer number being used for this Rx channel */
  913. offset = pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  914. PRU_SUART_CH_CTRL_OFFSET;
  915. pru_ram_read_data(offset, (Uint8 *) &u16Status, 2, &pru_arm_iomap);
  916. u16Status &= PRU_SUART_CH_CTRL_SR_MASK;
  917. u16Status = u16Status >> PRU_SUART_CH_CTRL_SR_SHIFT;
  918. /* we need to de-activate the serializer corresponding to this receive */
  919. status = suart_asp_serializer_deactivate (u16Status, &pru_arm_iomap);
  920. return (status);
  921. }
  922. /*
  923. * suart routine to get the tx status for a specific uart
  924. */
  925. short pru_softuart_getTxStatus(suart_handle hUart)
  926. {
  927. unsigned int offset;
  928. unsigned int pruOffset;
  929. unsigned short status = SUART_SUCCESS;
  930. unsigned short chNum;
  931. if (hUart == NULL) {
  932. return (PRU_SUART_ERR_HANDLE_INVALID);
  933. }
  934. /* channel starts from 0 and uart instance starts from 1 */
  935. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  936. if (hUart->uartNum <= 4) {
  937. /* PRU0 */
  938. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  939. } else {
  940. /* PRU1 */
  941. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  942. /* First 8 channel corresponds to PRU0 */
  943. chNum -= 8;
  944. }
  945. // /* channel starts from 0 and uart instance starts from 1 */
  946. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  947. offset =
  948. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  949. PRU_SUART_CH_TXRXSTATUS_OFFSET;
  950. pru_ram_read_data(offset, (Uint8 *) & status, 1, &pru_arm_iomap);
  951. return (status);
  952. }
  953. short pru_softuart_clrTxStatus(suart_handle hUart)
  954. {
  955. unsigned int offset;
  956. unsigned int pruOffset;
  957. unsigned short status = SUART_SUCCESS;
  958. unsigned short chNum;
  959. if (hUart == NULL) {
  960. return (PRU_SUART_ERR_HANDLE_INVALID);
  961. }
  962. /* channel starts from 0 and uart instance starts from 1 */
  963. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  964. if (hUart->uartNum <= 4) {
  965. /* PRU0 */
  966. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  967. } else {
  968. /* PRU1 */
  969. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  970. /* First 8 channel corresponds to PRU0 */
  971. chNum -= 8;
  972. }
  973. // /* channel starts from 0 and uart instance starts from 1 */
  974. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  975. offset =
  976. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  977. PRU_SUART_CH_TXRXSTATUS_OFFSET;
  978. pru_ram_read_data(offset, (Uint8 *) & status, 2, &pru_arm_iomap);
  979. status &= ~(0x2);
  980. pru_ram_write_data(offset, (Uint8 *) & status, 2, &pru_arm_iomap);
  981. return (status);
  982. }
  983. /*
  984. * suart routine to get the rx status for a specific uart
  985. */
  986. short pru_softuart_getRxStatus(suart_handle hUart)
  987. {
  988. unsigned int offset;
  989. unsigned int pruOffset;
  990. unsigned short status = SUART_SUCCESS;
  991. unsigned short chNum;
  992. if (hUart == NULL) {
  993. return (PRU_SUART_ERR_HANDLE_INVALID);
  994. }
  995. /* channel starts from 0 and uart instance starts from 1 */
  996. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  997. if (hUart->uartNum <= 4) {
  998. /* PRU0 */
  999. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  1000. } else {
  1001. /* PRU1 */
  1002. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  1003. /* First 8 channel corresponds to PRU0 */
  1004. chNum -= 8;
  1005. }
  1006. // /* channel starts from 0 and uart instance starts from 1 */
  1007. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1008. chNum++;
  1009. offset =
  1010. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1011. PRU_SUART_CH_TXRXSTATUS_OFFSET;
  1012. pru_ram_read_data(offset, (Uint8 *) &status, 2, &pru_arm_iomap);
  1013. return (status);
  1014. }
  1015. short pru_softuart_clrRxFifo(suart_handle hUart)
  1016. {
  1017. unsigned int offset;
  1018. unsigned int pruOffset;
  1019. unsigned short status = SUART_SUCCESS;
  1020. unsigned short chNum;
  1021. unsigned short regVal;
  1022. if (hUart == NULL) {
  1023. return (PRU_SUART_ERR_HANDLE_INVALID);
  1024. }
  1025. /* channel starts from 0 and uart instance starts from 1 */
  1026. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1027. if (hUart->uartNum <= 4) {
  1028. /* PRU0 */
  1029. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  1030. } else {
  1031. /* PRU1 */
  1032. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  1033. /* First 8 channel corresponds to PRU0 */
  1034. chNum -= 8;
  1035. }
  1036. // /* channel starts from 0 and uart instance starts from 1 */
  1037. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1038. chNum++;
  1039. /* Service Request to PRU */
  1040. offset = pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1041. PRU_SUART_CH_CTRL_OFFSET;
  1042. pru_ram_read_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  1043. regVal &= ~(PRU_SUART_CH_CTRL_MODE_MASK |PRU_SUART_CH_CTRL_SREQ_MASK);
  1044. regVal |= ( PRU_SUART_CH_CTRL_RX_MODE << PRU_SUART_CH_CTRL_MODE_SHIFT) |
  1045. (PRU_SUART_CH_CTRL_SREQ << PRU_SUART_CH_CTRL_SREQ_SHIFT);
  1046. pru_ram_write_data(offset, (Uint8 *) & regVal, 2, &pru_arm_iomap);
  1047. suart_intr_setmask (hUart->uartNum, PRU_RX_INTR, CHN_TXRX_IE_MASK_TIMEOUT);
  1048. /* generate ARM->PRU event */
  1049. suart_arm_to_pru_intr(hUart->uartNum);
  1050. return (status);
  1051. }
  1052. short pru_softuart_clrRxStatus(suart_handle hUart)
  1053. {
  1054. unsigned int offset;
  1055. unsigned int pruOffset;
  1056. unsigned short status = SUART_SUCCESS;
  1057. unsigned short chNum;
  1058. if (hUart == NULL) {
  1059. return (PRU_SUART_ERR_HANDLE_INVALID);
  1060. }
  1061. /* channel starts from 0 and uart instance starts from 1 */
  1062. chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1063. if (hUart->uartNum <= 4) {
  1064. /* PRU0 */
  1065. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  1066. } else {
  1067. /* PRU1 */
  1068. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  1069. /* First 8 channel corresponds to PRU0 */
  1070. chNum -= 8;
  1071. }
  1072. // /* channel starts from 0 and uart instance starts from 1 */
  1073. // chNum = (hUart->uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1074. chNum++;
  1075. offset =
  1076. pruOffset + (chNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1077. PRU_SUART_CH_TXRXSTATUS_OFFSET;
  1078. pru_ram_read_data(offset, (Uint8 *) & status, 2, &pru_arm_iomap);
  1079. status &= ~(0x3C);
  1080. pru_ram_write_data(offset, (Uint8 *) & status, 2, &pru_arm_iomap);
  1081. return (status);
  1082. }
  1083. /*
  1084. * suart_intr_status_read: Gets the Global Interrupt status register
  1085. * for the specified SUART.
  1086. * uartNum < 1 to 6 >
  1087. * txrxFlag < Indicates TX or RX interrupt for the uart >
  1088. */
  1089. short pru_softuart_get_isrstatus(unsigned short uartNum,
  1090. unsigned short *txrxFlag)
  1091. {
  1092. unsigned int offset;
  1093. unsigned int chNum;
  1094. unsigned int regVal = 0;
  1095. unsigned int u32RegVal = 0;
  1096. unsigned int u32ISRValue = 0;
  1097. unsigned int u32AckRegVal = 0;
  1098. /* initialize the status & Flag to known value */
  1099. *txrxFlag = 0;
  1100. /* Read PRU Interrupt Status Register from PRU */
  1101. offset =
  1102. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_STATCLRINT1 &
  1103. 0xFFFF);
  1104. pru_ram_read_data_4byte(offset, (unsigned int *)&u32ISRValue, 1);
  1105. /* determine if the interrupt occured in the current UART context */
  1106. u32RegVal = (PRU_SUART0_TX_EVT_BIT | PRU_SUART0_RX_EVT_BIT) << ((uartNum -1) * 2);
  1107. /* channel starts from 0 and uart instance starts from 1 */
  1108. chNum = uartNum * 2 - 2;
  1109. if (u32ISRValue & u32RegVal)
  1110. {
  1111. /* Check if the interrupt occured for Tx */
  1112. u32RegVal = PRU_SUART0_TX_EVT_BIT << ((uartNum - 1)* 2);
  1113. if (u32ISRValue & u32RegVal)
  1114. {
  1115. /* interupt occured for TX */
  1116. *txrxFlag |= PRU_TX_INTR;
  1117. /* acknowledge the interrupt */
  1118. u32AckRegVal = chNum + PRU_SUART0_TX_EVT;
  1119. offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_STATIDXCLR &
  1120. 0xFFFF);
  1121. pru_ram_write_data_4byte(offset, (unsigned int *)&u32AckRegVal, 1);
  1122. }
  1123. /* Check if the interrupt occured for Rx */
  1124. u32RegVal = PRU_SUART0_RX_EVT_BIT << ((uartNum - 1)* 2);
  1125. if (u32ISRValue & u32RegVal)
  1126. {
  1127. chNum += 1;
  1128. /* interupt occured for RX */
  1129. *txrxFlag |= PRU_RX_INTR;
  1130. /* acknowledge the interrupt */
  1131. u32AckRegVal = chNum + PRU_SUART0_TX_EVT;
  1132. offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_STATIDXCLR &
  1133. 0xFFFF);
  1134. pru_ram_write_data_4byte(offset, (unsigned int *)&u32AckRegVal, 1);
  1135. }
  1136. regVal = SUART_SUCCESS;
  1137. }
  1138. return regVal;
  1139. }
  1140. int pru_intr_clr_isrstatus(unsigned short uartNum, unsigned int txrxmode)
  1141. {
  1142. unsigned int offset;
  1143. unsigned short txrxFlag = 0;
  1144. unsigned short chnNum;
  1145. /* channel starts from 0 and uart instance starts from 1 */
  1146. chnNum = (uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1147. if ((uartNum > 0) && (uartNum <= 4))
  1148. {
  1149. offset = PRU_SUART_PRU0_ISR_OFFSET + 1;
  1150. }
  1151. else if ((uartNum > 4) && (uartNum <= 8))
  1152. {
  1153. /* PRU1 */
  1154. offset = PRU_SUART_PRU1_ISR_OFFSET + 1;
  1155. /* First 8 channel corresponds to PRU0 */
  1156. chnNum -= 8;
  1157. } else
  1158. {
  1159. return SUART_INVALID_UART_NUM;
  1160. }
  1161. // /* determine the channel number from UART number and direction */
  1162. // chnNum = uartNum * 2 - 2;
  1163. if (2 == txrxmode)
  1164. chnNum++;
  1165. pru_ram_read_data(offset, (Uint8 *) & txrxFlag, 1, &pru_arm_iomap);
  1166. txrxFlag &= ~(0x2);
  1167. pru_ram_write_data(offset, (Uint8 *) & txrxFlag, 1, &pru_arm_iomap);
  1168. return 0;
  1169. }
  1170. short suart_arm_to_pru_intr(unsigned short uartNum)
  1171. {
  1172. unsigned int u32offset;
  1173. unsigned int u32value;
  1174. short s16retval;
  1175. if ((uartNum > 0) && (uartNum <= 4)) {
  1176. /* PRU0 SYS_EVT32 */
  1177. u32value = 0x1;
  1178. } else if ((uartNum > 4) && (uartNum <= 8)) {
  1179. /* PRU1 SYS_EVT32 */
  1180. u32value = 0x1;
  1181. } else {
  1182. return SUART_INVALID_UART_NUM;
  1183. }
  1184. u32value = 0x20;
  1185. u32offset =
  1186. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_STATIDXSET &
  1187. 0xFFFF);
  1188. s16retval =
  1189. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1190. if (s16retval == -1) {
  1191. return -1;
  1192. }
  1193. return 0;
  1194. }
  1195. short arm_to_pru_intr_init(void)
  1196. {
  1197. unsigned int u32offset;
  1198. unsigned int u32value;
  1199. unsigned int intOffset;
  1200. short s16retval = -1;
  1201. /* Clear all the host interrupts */
  1202. for (intOffset = 0; intOffset < PRU_INTC_HOSTINTLVL_MAX; intOffset++)
  1203. {
  1204. u32offset =
  1205. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_HSTINTENIDXCLR&
  1206. 0xFFFF);
  1207. u32value = intOffset;
  1208. s16retval =
  1209. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1210. if (s16retval == -1) {
  1211. return -1;
  1212. }
  1213. }
  1214. /* Enable the global interrupt */
  1215. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_GLBLEN &
  1216. 0xFFFF);
  1217. u32value = 0x1;
  1218. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1219. if (s16retval == -1) {
  1220. return -1;
  1221. }
  1222. /* Enable the Host interrupts for all host channels */
  1223. for (intOffset = 0; intOffset < PRU_INTC_HOSTINTLVL_MAX; intOffset++)
  1224. {
  1225. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_HSTINTENIDXSET &
  1226. 0xFFFF);
  1227. u32value = intOffset;
  1228. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1229. if (s16retval == -1) {
  1230. return -1;
  1231. }
  1232. }
  1233. /* host to channel mapping : Setting the host interrupt for channels 0,1,2,3 */
  1234. u32offset =
  1235. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_HOSTMAP0 &
  1236. 0xFFFF);
  1237. u32value = 0x03020100;
  1238. s16retval =
  1239. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1240. if (-1 == s16retval) {
  1241. return -1;
  1242. }
  1243. /* host to channel mapping : Setting the host interrupt for channels 4,5,6,7 */
  1244. u32offset =
  1245. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_HOSTMAP1 &
  1246. 0xFFFF);
  1247. u32value = 0x07060504;
  1248. s16retval =
  1249. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1250. if (-1 == s16retval) {
  1251. return -1;
  1252. }
  1253. /* host to channel mapping : Setting the host interrupt for channels 8,9 */
  1254. u32offset =
  1255. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_HOSTMAP2 &
  1256. 0xFFFF);
  1257. u32value = 0x00000908;
  1258. s16retval =
  1259. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1260. if (-1 == s16retval) {
  1261. return -1;
  1262. }
  1263. /* Sets the channel for the system interrupt
  1264. * MAP channel 0 to SYS_EVT32
  1265. * MAP channel 1 to SYS_EVT33
  1266. * MAP channel 2 to SYS_EVT34 SUART0-Tx
  1267. * MAP channel 2 to SYS_EVT35 SUART0-Rx
  1268. */
  1269. u32offset =
  1270. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_CHANMAP8 &
  1271. 0xFFFF);
  1272. u32value = 0x02020100;
  1273. s16retval =
  1274. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1275. if (-1 == s16retval) {
  1276. return -1;
  1277. }
  1278. /* Sets the channel for the system interrupt
  1279. * MAP channel 3 to SYS_EVT36 SUART1-Tx
  1280. * MAP channel 3 to SYS_EVT37 SUART1-Rx
  1281. * MAP channel 4 to SYS_EVT38 SUART2-Tx
  1282. * MAP channel 4 to SYS_EVT39 SUART2-Rx
  1283. */
  1284. u32offset =
  1285. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_CHANMAP9 &
  1286. 0xFFFF);
  1287. u32value = 0x04040303;
  1288. s16retval =
  1289. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1290. if (-1 == s16retval) {
  1291. return -1;
  1292. }
  1293. /* Sets the channel for the system interrupt
  1294. * MAP channel 5 to SYS_EVT40 SUART3-Tx
  1295. * MAP channel 5 to SYS_EVT41 SUART3-Rx
  1296. * MAP channel 6 to SYS_EVT42 SUART4-Tx
  1297. * MAP channel 6 to SYS_EVT43 SUART4-Rx
  1298. */
  1299. u32offset =
  1300. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_CHANMAP10 &
  1301. 0xFFFF);
  1302. u32value = 0x06060505;
  1303. s16retval =
  1304. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1305. if (-1 == s16retval) {
  1306. return -1;
  1307. }
  1308. /* Sets the channel for the system interrupt
  1309. * MAP channel 7 to SYS_EVT44 SUART5-Tx
  1310. * MAP channel 7 to SYS_EVT45 SUART5-Rx
  1311. * MAP channel 8 to SYS_EVT46 SUART6-Tx
  1312. * MAP channel 8 to SYS_EVT47 SUART6-Rx
  1313. */
  1314. u32offset =
  1315. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_CHANMAP11 &
  1316. 0xFFFF);
  1317. u32value = 0x08080707;
  1318. s16retval =
  1319. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1320. if (-1 == s16retval) {
  1321. return -1;
  1322. }
  1323. /* Sets the channel for the system interrupt
  1324. * MAP channel 9 to SYS_EVT48 SUART7-Tx
  1325. * MAP channel 9 to SYS_EVT49 SUART7-Rx
  1326. */
  1327. u32offset =
  1328. (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_CHANMAP11 &
  1329. 0xFFFF);
  1330. u32value = 0x00000909;
  1331. s16retval =
  1332. pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1333. if (-1 == s16retval) {
  1334. return -1;
  1335. }
  1336. /* Clear required set of system events and enable them using indexed register */
  1337. for (intOffset = 0; intOffset < 18; intOffset++)
  1338. {
  1339. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_STATIDXCLR & 0xFFFF);
  1340. u32value = 32 + intOffset;
  1341. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int *) &u32value, 1);
  1342. if (s16retval == -1) {
  1343. return -1;
  1344. }
  1345. }
  1346. /* enable only the HOST to PRU interrupts and let the PRU to Host events be
  1347. * enabled by the separate API on demand basis.
  1348. */
  1349. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_ENIDXSET & 0xFFFF);
  1350. u32value = 32;
  1351. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int*) &u32value, 1);
  1352. if (s16retval == -1) {
  1353. return -1;
  1354. }
  1355. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_ENIDXSET & 0xFFFF);
  1356. u32value = 33;
  1357. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int*) &u32value, 1);
  1358. if (s16retval == -1) {
  1359. return -1;
  1360. }
  1361. /* Enable the global interrupt */
  1362. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_GLBLEN &
  1363. 0xFFFF);
  1364. u32value = 0x1;
  1365. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1366. if (s16retval == -1) {
  1367. return -1;
  1368. }
  1369. /* Enable the Host interrupts for all host channels */
  1370. for (intOffset = 0; intOffset < PRU_INTC_HOSTINTLVL_MAX; intOffset++)
  1371. {
  1372. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_HSTINTENIDXSET &
  1373. 0xFFFF);
  1374. u32value = intOffset;
  1375. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int *)&u32value, 1);
  1376. if (s16retval == -1) {
  1377. return -1;
  1378. }
  1379. }
  1380. return 0;
  1381. }
  1382. int suart_pru_to_host_intr_enable (unsigned short uartNum,
  1383. unsigned int txrxmode, int s32Flag)
  1384. {
  1385. int retVal = 0;
  1386. unsigned int u32offset;
  1387. unsigned int chnNum;
  1388. unsigned int u32value;
  1389. short s16retval = 0;
  1390. if (uartNum > 8) {
  1391. return SUART_INVALID_UART_NUM;
  1392. }
  1393. chnNum = (uartNum * 2) - 2;
  1394. if (2 == txrxmode) { /* Rx mode */
  1395. chnNum++;
  1396. }
  1397. if (TRUE == s32Flag)
  1398. {
  1399. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_ENIDXSET & 0xFFFF);
  1400. u32value = 34 + chnNum;
  1401. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int*) &u32value, 1);
  1402. if (s16retval == -1) {
  1403. return -1;
  1404. }
  1405. }
  1406. else
  1407. {
  1408. u32offset = (unsigned int)pru_arm_iomap.pru_io_addr | (PRU_INTC_ENIDXCLR & 0xFFFF);
  1409. u32value = 34 + chnNum;
  1410. s16retval = pru_ram_write_data_4byte(u32offset, (unsigned int*) &u32value, 1);
  1411. if (s16retval == -1) {
  1412. return -1;
  1413. }
  1414. }
  1415. return (retVal);
  1416. }
  1417. int suart_intr_setmask(unsigned short uartNum,
  1418. unsigned int txrxmode, unsigned int intrmask)
  1419. {
  1420. unsigned int offset;
  1421. unsigned int pruOffset;
  1422. unsigned int txrxFlag;
  1423. unsigned int regval = 0;
  1424. unsigned int chnNum;
  1425. /* channel starts from 0 and uart instance starts from 1 */
  1426. chnNum = (uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1427. if ((uartNum > 0) && (uartNum <= 4)) {
  1428. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  1429. offset = PRU_SUART_PRU0_IMR_OFFSET;
  1430. } else if ((uartNum > 4) && (uartNum <= 8)) {
  1431. /* PRU1 */
  1432. offset = PRU_SUART_PRU1_IMR_OFFSET;
  1433. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  1434. /* First 8 channel corresponds to PRU0 */
  1435. chnNum -= 8;
  1436. } else {
  1437. return SUART_INVALID_UART_NUM;
  1438. }
  1439. // chnNum = (uartNum * 2) - 2;
  1440. if (1 == txrxmode) { /* tx mode */
  1441. regval = 1 << chnNum;
  1442. } else if (2 == txrxmode) { /* rx mode */
  1443. chnNum++;
  1444. regval = 1 << chnNum;
  1445. }
  1446. if (CHN_TXRX_IE_MASK_CMPLT == (intrmask & CHN_TXRX_IE_MASK_CMPLT))
  1447. {
  1448. pru_ram_read_data(offset, (Uint8 *) & txrxFlag, 2,
  1449. &pru_arm_iomap);
  1450. txrxFlag &= ~(regval);
  1451. txrxFlag |= regval;
  1452. pru_ram_write_data(offset, (Uint8 *) & txrxFlag, 2,
  1453. &pru_arm_iomap);
  1454. }
  1455. if ((intrmask & SUART_GBL_INTR_ERR_MASK) == SUART_GBL_INTR_ERR_MASK) {
  1456. regval = 0;
  1457. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1458. &pru_arm_iomap);
  1459. regval &= ~(SUART_GBL_INTR_ERR_MASK);
  1460. regval |= (SUART_GBL_INTR_ERR_MASK);
  1461. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1462. &pru_arm_iomap);
  1463. }
  1464. // Break Indicator Interrupt Masked
  1465. if ((intrmask & CHN_TXRX_IE_MASK_FE) == CHN_TXRX_IE_MASK_FE) {
  1466. regval = 0;
  1467. offset =
  1468. pruOffset + (chnNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1469. PRU_SUART_CH_CONFIG1_OFFSET;
  1470. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1471. &pru_arm_iomap);
  1472. regval &= ~(CHN_TXRX_IE_MASK_FE);
  1473. regval |= CHN_TXRX_IE_MASK_FE;
  1474. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1475. &pru_arm_iomap);
  1476. }
  1477. //Framing Error Interrupt Masked
  1478. if (CHN_TXRX_IE_MASK_BI == (intrmask & CHN_TXRX_IE_MASK_BI)) {
  1479. regval = 0;
  1480. offset =
  1481. pruOffset + (chnNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1482. PRU_SUART_CH_CONFIG1_OFFSET;
  1483. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1484. &pru_arm_iomap);
  1485. regval &= ~(CHN_TXRX_IE_MASK_BI);
  1486. regval |= CHN_TXRX_IE_MASK_BI;
  1487. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1488. &pru_arm_iomap);
  1489. }
  1490. //Timeout error Interrupt Masked
  1491. if (CHN_TXRX_IE_MASK_TIMEOUT == (intrmask & CHN_TXRX_IE_MASK_TIMEOUT)) {
  1492. regval = 0;
  1493. offset =
  1494. pruOffset + (chnNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1495. PRU_SUART_CH_CONFIG1_OFFSET;
  1496. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1497. &pru_arm_iomap);
  1498. regval &= ~(CHN_TXRX_IE_MASK_TIMEOUT);
  1499. regval |= CHN_TXRX_IE_MASK_TIMEOUT;
  1500. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1501. &pru_arm_iomap);
  1502. }
  1503. return 0;
  1504. }
  1505. int suart_intr_clrmask(unsigned short uartNum,
  1506. unsigned int txrxmode, unsigned int intrmask)
  1507. {
  1508. unsigned int offset;
  1509. unsigned int pruOffset;
  1510. unsigned short txrxFlag;
  1511. unsigned short regval = 0;
  1512. unsigned short chnNum;
  1513. /* channel starts from 0 and uart instance starts from 1 */
  1514. chnNum = (uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1515. if ((uartNum > 0) && (uartNum <= 4)) {
  1516. pruOffset = PRU_SUART_PRU0_CH0_OFFSET;
  1517. offset = PRU_SUART_PRU0_IMR_OFFSET;
  1518. } else if ((uartNum > 4) && (uartNum <= 8)) {
  1519. /* PRU1 */
  1520. offset = PRU_SUART_PRU1_IMR_OFFSET;
  1521. pruOffset = PRU_SUART_PRU1_CH0_OFFSET;
  1522. /* First 8 channel corresponds to PRU0 */
  1523. chnNum -= 8;
  1524. } else {
  1525. return SUART_INVALID_UART_NUM;
  1526. }
  1527. // chnNum = uartNum * 2 - 2;
  1528. if (1 == txrxmode) { /* tx mode */
  1529. regval = 1 << chnNum;
  1530. } else if (2 == txrxmode) { /* rx mode */
  1531. chnNum++;
  1532. regval = 1 << chnNum;
  1533. }
  1534. if (CHN_TXRX_IE_MASK_CMPLT == (intrmask & CHN_TXRX_IE_MASK_CMPLT)) {
  1535. pru_ram_read_data(offset, (Uint8 *) & txrxFlag, 2,
  1536. &pru_arm_iomap);
  1537. txrxFlag &= ~(regval);
  1538. pru_ram_write_data(offset, (Uint8 *) & txrxFlag, 2,
  1539. &pru_arm_iomap);
  1540. }
  1541. if ((intrmask & SUART_GBL_INTR_ERR_MASK) == SUART_GBL_INTR_ERR_MASK) {
  1542. regval = 0;
  1543. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1544. &pru_arm_iomap);
  1545. regval &= ~(SUART_GBL_INTR_ERR_MASK);
  1546. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1547. &pru_arm_iomap);
  1548. }
  1549. // Break Indicator Interrupt Masked
  1550. if ((intrmask & CHN_TXRX_IE_MASK_FE) == CHN_TXRX_IE_MASK_FE) {
  1551. regval = 0;
  1552. offset =
  1553. pruOffset + (chnNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1554. PRU_SUART_CH_CONFIG1_OFFSET;
  1555. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1556. &pru_arm_iomap);
  1557. regval &= ~(CHN_TXRX_IE_MASK_FE);
  1558. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1559. &pru_arm_iomap);
  1560. }
  1561. //Framing Error Interrupt Masked
  1562. if (CHN_TXRX_IE_MASK_BI == (intrmask & CHN_TXRX_IE_MASK_BI)) {
  1563. regval = 0;
  1564. offset =
  1565. pruOffset + (chnNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1566. PRU_SUART_CH_CONFIG1_OFFSET;
  1567. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1568. &pru_arm_iomap);
  1569. regval &= ~(CHN_TXRX_IE_MASK_BI);
  1570. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1571. &pru_arm_iomap);
  1572. }
  1573. //Timeout error Interrupt Masked
  1574. if (CHN_TXRX_IE_MASK_TIMEOUT == (intrmask & CHN_TXRX_IE_MASK_TIMEOUT)) {
  1575. regval = 0;
  1576. offset =
  1577. pruOffset + (chnNum * SUART_NUM_OF_BYTES_PER_CHANNEL) +
  1578. PRU_SUART_CH_CONFIG1_OFFSET;
  1579. pru_ram_read_data(offset, (Uint8 *) & regval, 2,
  1580. &pru_arm_iomap);
  1581. regval &= ~(CHN_TXRX_IE_MASK_TIMEOUT);
  1582. pru_ram_write_data(offset, (Uint8 *) & regval, 2,
  1583. &pru_arm_iomap);
  1584. }
  1585. return 0;
  1586. }
  1587. int suart_intr_getmask(unsigned short uartNum,
  1588. unsigned int txrxmode, unsigned int intrmask)
  1589. {
  1590. unsigned short chnNum;
  1591. unsigned int offset;
  1592. unsigned short txrxFlag;
  1593. unsigned short regval = 1;
  1594. /* channel starts from 0 and uart instance starts from 1 */
  1595. chnNum = (uartNum * SUART_NUM_OF_CHANNELS_PER_SUART) - 2;
  1596. if ((uartNum > 0) && (uartNum <= 4)) {
  1597. offset = PRU_SUART_PRU0_IMR_OFFSET;
  1598. } else if ((uartNum > 4) && (uartNum <= 8)) {
  1599. /* PRU1 */
  1600. offset = PRU_SUART_PRU1_ISR_OFFSET;
  1601. /* First 8 channel corresponds to PRU0 */
  1602. chnNum -= 8;
  1603. } else {
  1604. return SUART_INVALID_UART_NUM;
  1605. }
  1606. // chnNum = uartNum * 2 - 2;
  1607. if (1 == txrxmode) { /* tx mode */
  1608. regval = regval << chnNum;
  1609. } else if (2 == txrxmode) { /* rx mode */
  1610. chnNum++;
  1611. regval = regval << chnNum;
  1612. }
  1613. pru_ram_read_data(offset, (Uint8 *) & txrxFlag, 2, &pru_arm_iomap);
  1614. txrxFlag &= regval;
  1615. if (0 == intrmask) {
  1616. if (txrxFlag == 0)
  1617. return 1;
  1618. }
  1619. if (1 == intrmask) {
  1620. if (txrxFlag == regval)
  1621. return 1;
  1622. }
  1623. return 0;
  1624. }
  1625. static int suart_set_pru_id (unsigned int pru_no)
  1626. {
  1627. unsigned int offset;
  1628. unsigned short reg_val = 0;
  1629. if (0 == pru_no)
  1630. {
  1631. offset = PRU_SUART_PRU0_ID_ADDR;
  1632. }
  1633. else if (1 == pru_no)
  1634. {
  1635. offset = PRU_SUART_PRU1_ID_ADDR;
  1636. }
  1637. else
  1638. {
  1639. return PRU_SUART_FAILURE;
  1640. }
  1641. pru_ram_read_data(offset, (Uint8 *) & reg_val, 1, &pru_arm_iomap);
  1642. reg_val &=~SUART_PRU_ID_MASK;
  1643. reg_val = pru_no;
  1644. pru_ram_write_data(offset, (Uint8 *) & reg_val, 1, &pru_arm_iomap);
  1645. return PRU_SUART_SUCCESS;
  1646. }
  1647. /* End of file */