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.

443 lines
12 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * pru/hal/uart/src/suart_utils.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_pru_regs.h"
  22. #include "omapl_suart_board.h"
  23. #include "suart_api.h"
  24. #include "suart_utils.h"
  25. #include "suart_err.h"
  26. #include "pru.h"
  27. #include "csl/soc_OMAPL138.h"
  28. #include "csl/cslr_dspintc.h"
  29. #include "csl/cslr_mcasp.h"
  30. #include "csl/cslr_psc_OMAPL138.h"
  31. #define SUART_TRX_DIV_CONF_SZ 4
  32. static short suart_mcasp_tx_baud_set(unsigned int txBaudValue,
  33. arm_pru_iomap * pru_arm_iomap);
  34. static short suart_mcasp_rx_baud_set(unsigned int rxBaudValue,
  35. unsigned int oversampling,
  36. arm_pru_iomap * pru_arm_iomap);
  37. /*
  38. * Lookup table for TX baud rate
  39. * The divisor value is calculated using the formula
  40. *
  41. * ACLKX = (AUXCLK)/(CLKXDIV * HCLKXDIV)
  42. *
  43. * Where
  44. * CLKXDIV takes values from 1-32
  45. * HCLKXDIV takes values from 1-4096
  46. * Here
  47. * AUXCLK = 24MHz
  48. */
  49. unsigned int lt_tx_baud_rate[][SUART_TRX_DIV_CONF_SZ] = {
  50. /*BaudRate, Divisor, CLKXDIV, HCLKXDIV */
  51. {300, 80000, 24, 3200},
  52. {600, 40000, 15, 2500},
  53. {1800, 13333, 10, 1212},
  54. {2400, 10000, 4, 2000},
  55. {4800, 5000, 1, 2500},
  56. {7200, 3333, 0, 3333},
  57. {9600, 2500, 0, 2500},
  58. {14400, 1666, 0, 1666},
  59. {19200, 1250, 0, 1250},
  60. {38400, 625, 0, 625},
  61. {57600, 416, 0, 416},
  62. {115200, 208, 0, 208},
  63. {230400, 104, 0, 104}
  64. };
  65. /*
  66. * Lookup table for RX baud rate for 8 bit oversampling
  67. * The divisor value is calculated using the formula
  68. *
  69. * ACLKR = (AUXCLK)/(CLKRDIV * HCLKRDIV) * Oversampling
  70. *
  71. * Where
  72. * CLKRDIV takes values from 1-32
  73. * HCLKRDIV takes values from 1-4096
  74. * Here
  75. * AUXCLK = 24MHz
  76. */
  77. unsigned int lt_rx_8x_baud_rate[][SUART_TRX_DIV_CONF_SZ] = {
  78. /*BaudRate, Divisor, CLKXDIV, HCLKXDIV */
  79. {300, 10000, 4, 2000},
  80. {600, 5000, 1, 2500},
  81. {1800, 1667, 0, 1667},
  82. {2400, 1250, 0, 1250},
  83. {7200, 417, 0, 417},
  84. {4800, 625, 0, 625},
  85. {9600, 312, 0, 312},
  86. {14400, 208, 0, 208},
  87. {19200, 156, 0, 156},
  88. {38400, 78, 0, 78},
  89. {57600, 52, 0, 52},
  90. {115200, 26, 0, 26},
  91. {230400, 13, 0, 13}
  92. };
  93. /*
  94. * Lookup table for RX baud rate for 16 bit oversampling
  95. * The divisor value is calculated using the formula
  96. *
  97. * ACLKR = (AUXCLK)/(CLKRDIV * HCLKRDIV) * Oversampling
  98. *
  99. * Where
  100. * CLKRDIV takes values from 1-32
  101. * HCLKRDIV takes values from 1-4096
  102. * Here
  103. * AUXCLK = 24MHz
  104. */
  105. unsigned int lt_rx_16x_baud_rate[][SUART_TRX_DIV_CONF_SZ] = {
  106. /*BaudRate, Divisor, CLKXDIV, HCLKXDIV */
  107. {300, 5000, 1, 2500},
  108. {600, 2500, 0, 2500},
  109. {1800, 833, 0, 833},
  110. {2400, 625, 0, 625},
  111. {4800, 312, 0, 312},
  112. {7200, 208, 0, 208},
  113. {9600, 156, 0, 156},
  114. {14400, 104, 0, 104},
  115. {19200, 78, 0, 78},
  116. {38400, 39, 0, 39},
  117. {57600, 26, 0, 26},
  118. {115200, 13, 0, 13},
  119. {230400, 6, 0, 6}
  120. };
  121. /*
  122. *====================
  123. * API implementations
  124. *====================
  125. */
  126. /*
  127. * McASP configuration routine
  128. */
  129. void suart_mcasp_config(unsigned int mcasp_addr,
  130. unsigned int txBaudValue,
  131. unsigned int rxBaudValue,
  132. unsigned int oversampling,
  133. arm_pru_iomap * pru_arm_iomap)
  134. {
  135. CSL_McaspRegsOvly mcasp0Regs = (CSL_McaspRegsOvly) mcasp_addr; //CSL_MCASP_0_CTRL_REGS;
  136. // reset mcasp.
  137. mcasp0Regs->GBLCTL = 0;
  138. mcasp0Regs->RGBLCTL = 0;
  139. mcasp0Regs->XGBLCTL = 0;
  140. // configure receive registers.
  141. if ((SUART_8X_OVRSMPL == oversampling) || (0 == oversampling)){
  142. mcasp0Regs->RMASK = 0x000000FF; //0x0000FFFF for 16
  143. mcasp0Regs->RFMT = 0x0000A038; //slot size 8 bits , RPAD = 1. 0x0000A078 for x16
  144. }
  145. if(SUART_16X_OVRSMPL == oversampling){
  146. mcasp0Regs->RMASK = 0x0000FFFF;
  147. mcasp0Regs->RFMT = 0x0000A078;
  148. }
  149. mcasp0Regs->AFSRCTL = 0x00000012; //burst mode
  150. mcasp0Regs->ACLKRCTL = 0x000000A0;
  151. mcasp0Regs->AHCLKRCTL = 0x00008000;
  152. suart_mcasp_rx_baud_set(rxBaudValue, oversampling, pru_arm_iomap);
  153. mcasp0Regs->RTDM = 0x00000003;
  154. mcasp0Regs->RINTCTL = 0x00000000;
  155. mcasp0Regs->RCLKCHK = 0x00FF0008;
  156. // configure transmit registers.
  157. mcasp0Regs->XMASK = 0x0000FFFF;
  158. mcasp0Regs->XFMT = 0x00002078;
  159. mcasp0Regs->AFSXCTL = 0x00000012;
  160. mcasp0Regs->ACLKXCTL = 0x000000E0;
  161. mcasp0Regs->AHCLKXCTL = 0x00008000;
  162. suart_mcasp_tx_baud_set(txBaudValue, pru_arm_iomap);
  163. mcasp0Regs->XTDM = 0x00000003;
  164. mcasp0Regs->XINTCTL = 0x00000000;
  165. mcasp0Regs->XCLKCHK = 0x00FF0008;
  166. //Serializer as a transmitter
  167. mcasp0Regs->SRCTL0 = 0x000c;
  168. mcasp0Regs->SRCTL1 = 0x000c;
  169. mcasp0Regs->SRCTL2 = 0x000c;
  170. mcasp0Regs->SRCTL3 = 0x000c;
  171. mcasp0Regs->SRCTL4 = 0x000c;
  172. mcasp0Regs->SRCTL5 = 0x000c;
  173. mcasp0Regs->SRCTL6 = 0x000c;
  174. mcasp0Regs->SRCTL7 = 0x000C;
  175. mcasp0Regs->SRCTL8 = 0x000C;
  176. mcasp0Regs->SRCTL9 = 0x000C;
  177. mcasp0Regs->SRCTL10 = 0x000C;
  178. mcasp0Regs->SRCTL11 = 0x000C;
  179. mcasp0Regs->SRCTL12 = 0x000C;
  180. mcasp0Regs->SRCTL13 = 0x000C;
  181. mcasp0Regs->SRCTL14 = 0x000C;
  182. mcasp0Regs->SRCTL15 = 0x000c;
  183. //Configure all AXR[n] as McASP pins
  184. mcasp0Regs->PFUNC = CSL_MCASP_PFUNC_RESETVAL;
  185. mcasp0Regs->PDOUT = 0xFFFF;
  186. //Configure PDIR for test purposes
  187. //set frame sync, clocks as output, AXR8, 10 & 14 as output, AXR7, 9 & 13
  188. mcasp0Regs->PDIR = MCASP_PDIR_VAL; // has receiver configuration as well
  189. mcasp0Regs->PDOUT = 0xFFFF;
  190. // config pin function and direction.
  191. mcasp0Regs->DITCTL = 0x00000000;
  192. mcasp0Regs->DLBCTL = 0x00000000;
  193. mcasp0Regs->AMUTE = 0x00000000;
  194. mcasp0Regs->XSTAT = 0x0000FFFF; // Clear all
  195. mcasp0Regs->RSTAT = 0x0000FFFF; // Clear all
  196. }
  197. void suart_mcasp_tx_serialzier_set(unsigned int serializerNum,
  198. arm_pru_iomap * pru_arm_iomap)
  199. {
  200. CSL_McaspRegsOvly mcasp0Regs =
  201. (CSL_McaspRegsOvly) pru_arm_iomap->mcasp_io_addr;
  202. mcasp0Regs->PFUNC |= (0x1 << serializerNum);
  203. }
  204. /*
  205. * mcasp TX buard rate setting routine
  206. */
  207. short suart_mcasp_tx_baud_set(unsigned int txBaudValue,
  208. arm_pru_iomap * pru_arm_iomap)
  209. {
  210. unsigned int clkDivVal;
  211. unsigned int loopCnt;
  212. short status = SUART_SUCCESS;
  213. short foundVal = SUART_FALSE;
  214. CSL_McaspRegsOvly mcasp0Regs =
  215. (CSL_McaspRegsOvly) pru_arm_iomap->mcasp_io_addr;
  216. /* Search the supported baud rate in the table */
  217. for (loopCnt = 0; loopCnt < SUART_NUM_OF_BAUDS_SUPPORTED; loopCnt++) {
  218. if (txBaudValue == lt_tx_baud_rate[loopCnt][0]) {
  219. foundVal = SUART_TRUE;
  220. break;
  221. }
  222. }
  223. if (foundVal == SUART_TRUE) {
  224. clkDivVal = lt_tx_baud_rate[loopCnt][2];
  225. mcasp0Regs->ACLKXCTL |=
  226. clkDivVal << CSL_MCASP_ACLKXCTL_CLKXDIV_SHIFT;
  227. clkDivVal = lt_tx_baud_rate[loopCnt][3]; /* starts from 0 */
  228. mcasp0Regs->AHCLKXCTL |=
  229. clkDivVal << CSL_MCASP_AHCLKXCTL_HCLKXDIV_SHIFT;
  230. } else {
  231. return SUART_INVALID_TX_BAUD;
  232. }
  233. return (status);
  234. }
  235. /*
  236. * mcasp RX buard rate setting routine
  237. */
  238. short suart_mcasp_rx_baud_set(unsigned int rxBaudValue,
  239. unsigned int oversampling,
  240. arm_pru_iomap * pru_arm_iomap)
  241. {
  242. unsigned int clkDivVal;
  243. unsigned int loopCnt;
  244. short status = SUART_SUCCESS;
  245. short foundVal = SUART_FALSE;
  246. CSL_McaspRegsOvly mcasp0Regs =
  247. (CSL_McaspRegsOvly) pru_arm_iomap->mcasp_io_addr;
  248. if (oversampling == SUART_8X_OVRSMPL) {
  249. for (loopCnt = 0; loopCnt < SUART_NUM_OF_BAUDS_SUPPORTED;
  250. loopCnt++) {
  251. if (rxBaudValue == lt_rx_8x_baud_rate[loopCnt][0]) {
  252. clkDivVal = lt_rx_8x_baud_rate[loopCnt][2];
  253. mcasp0Regs->ACLKRCTL |=
  254. clkDivVal <<
  255. CSL_MCASP_ACLKXCTL_CLKXDIV_SHIFT;
  256. clkDivVal = lt_rx_8x_baud_rate[loopCnt][3] - 1; /* starts from 0 */
  257. mcasp0Regs->AHCLKRCTL |=
  258. clkDivVal <<
  259. CSL_MCASP_AHCLKXCTL_HCLKXDIV_SHIFT;
  260. foundVal = SUART_TRUE;
  261. break;
  262. }
  263. }
  264. } else if (oversampling == SUART_16X_OVRSMPL) {
  265. for (loopCnt = 0; loopCnt < SUART_NUM_OF_BAUDS_SUPPORTED;
  266. loopCnt++) {
  267. if (rxBaudValue == lt_rx_16x_baud_rate[loopCnt][0]) {
  268. clkDivVal = lt_rx_16x_baud_rate[loopCnt][2];
  269. mcasp0Regs->ACLKRCTL |=
  270. clkDivVal <<
  271. CSL_MCASP_ACLKXCTL_CLKXDIV_SHIFT;
  272. clkDivVal = lt_rx_16x_baud_rate[loopCnt][3];
  273. mcasp0Regs->AHCLKRCTL |=
  274. clkDivVal <<
  275. CSL_MCASP_AHCLKXCTL_HCLKXDIV_SHIFT;
  276. foundVal = SUART_TRUE;
  277. break;
  278. }
  279. }
  280. } else if (oversampling == 0) {
  281. for (loopCnt = 0; loopCnt < SUART_NUM_OF_BAUDS_SUPPORTED;
  282. loopCnt++) {
  283. if (rxBaudValue == lt_tx_baud_rate[loopCnt][0]) {
  284. clkDivVal = lt_tx_baud_rate[loopCnt][2];
  285. mcasp0Regs->ACLKRCTL |=
  286. clkDivVal <<
  287. CSL_MCASP_ACLKXCTL_CLKXDIV_SHIFT;
  288. clkDivVal = lt_tx_baud_rate[loopCnt][3];
  289. mcasp0Regs->AHCLKRCTL |=
  290. clkDivVal <<
  291. CSL_MCASP_AHCLKXCTL_HCLKXDIV_SHIFT;
  292. foundVal = SUART_TRUE;
  293. break;
  294. }
  295. }
  296. } else {
  297. return SUART_INVALID_OVERSAMPLING;
  298. }
  299. if (foundVal != SUART_TRUE) {
  300. return SUART_INVALID_RX_BAUD;
  301. }
  302. return (status);
  303. }
  304. /*
  305. * mcasp buard rate setting routine
  306. */
  307. short suart_asp_baud_set(unsigned int txBaudValue,
  308. unsigned int rxBaudValue,
  309. unsigned int oversampling,
  310. arm_pru_iomap * pru_arm_iomap)
  311. {
  312. short status = SUART_SUCCESS;
  313. status = suart_mcasp_tx_baud_set(txBaudValue, pru_arm_iomap);
  314. status =
  315. suart_mcasp_rx_baud_set(rxBaudValue, oversampling, pru_arm_iomap);
  316. return (status);
  317. }
  318. /*
  319. * mcasp deactivate the selected serializer
  320. */
  321. short suart_asp_serializer_deactivate (unsigned short u16srNum,
  322. arm_pru_iomap * pru_arm_iomap)
  323. {
  324. short status = SUART_SUCCESS;
  325. CSL_McaspRegsOvly mcasp0Regs =
  326. (CSL_McaspRegsOvly) pru_arm_iomap->mcasp_io_addr;
  327. unsigned int * pu32SrCtlAddr = NULL;
  328. if (u16srNum > 15)
  329. {
  330. status = SUART_INVALID_SR_NUM ;
  331. }
  332. else
  333. {
  334. pu32SrCtlAddr = (unsigned int *)&(mcasp0Regs->SRCTL0);
  335. pu32SrCtlAddr += u16srNum;
  336. * (pu32SrCtlAddr) = 0x000C;
  337. }
  338. return (status);
  339. }
  340. /*
  341. * mcasp psc enable routine
  342. */
  343. void suart_mcasp_psc_enable(unsigned int psc1_addr)
  344. {
  345. CSL_PscRegsOvly PSC = (CSL_PscRegsOvly) psc1_addr; // CSL_PSC_1_REGS;
  346. // Wait for any outstanding transition to complete
  347. while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
  348. CSL_PSC_PTSTAT_GOSTAT0_IN_TRANSITION) ;
  349. // If we are already in that state, just return
  350. if (CSL_FEXT(PSC->MDSTAT[CSL_PSC_MCASP0], PSC_MDSTAT_STATE) ==
  351. CSL_PSC_MDSTAT_STATE_ENABLE)
  352. return;
  353. // Perform transition
  354. CSL_FINST(PSC->MDCTL[CSL_PSC_MCASP0], PSC_MDCTL_NEXT, ENABLE);
  355. CSL_FINST(PSC->PTCMD, PSC_PTCMD_GO0, SET);
  356. // Wait for transition to complete
  357. while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
  358. CSL_PSC_PTSTAT_GOSTAT0_IN_TRANSITION) ;
  359. // Wait and verify the state
  360. while (CSL_FEXT(PSC->MDSTAT[CSL_PSC_MCASP0], PSC_MDSTAT_STATE) !=
  361. CSL_PSC_MDSTAT_STATE_ENABLE) ;
  362. }
  363. /*
  364. * mcasp psc disable routine
  365. */
  366. void suart_mcasp_psc_disable(unsigned int psc1_addr)
  367. {
  368. CSL_PscRegsOvly PSC = (CSL_PscRegsOvly) psc1_addr; // CSL_PSC_1_REGS;
  369. // Wait for any outstanding transition to complete
  370. while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
  371. CSL_PSC_PTSTAT_GOSTAT0_IN_TRANSITION) ;
  372. // If we are already in that state, just return
  373. if (CSL_FEXT(PSC->MDSTAT[CSL_PSC_MCASP0], PSC_MDSTAT_STATE) ==
  374. CSL_PSC_MDSTAT_STATE_SYNCRST)
  375. return;
  376. // Perform transition
  377. CSL_FINST(PSC->MDCTL[CSL_PSC_MCASP0], PSC_MDCTL_NEXT, SYNCRST);
  378. CSL_FINST(PSC->PTCMD, PSC_PTCMD_GO0, SET);
  379. // Wait for transition to complete
  380. while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
  381. CSL_PSC_PTSTAT_GOSTAT0_IN_TRANSITION) ;
  382. // Wait and verify the state
  383. while (CSL_FEXT(PSC->MDSTAT[CSL_PSC_MCASP0], PSC_MDSTAT_STATE) !=
  384. CSL_PSC_MDSTAT_STATE_SYNCRST) ;
  385. }
  386. /* End of file */