uart_acts.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @brief UART Driver for Actions SoC
  8. */
  9. #include <kernel.h>
  10. #include <device.h>
  11. #include <string.h>
  12. #include <drivers/uart.h>
  13. #include <soc.h>
  14. #include <drivers/uart_dma.h>
  15. #include <board_cfg.h>
  16. #ifdef CONFIG_CFG_DRV
  17. #include <config.h>
  18. #include <drivers/cfg_drv/driver_config.h>
  19. #endif
  20. /* Device data config */
  21. struct acts_uart_config {
  22. uint32_t base;
  23. const char *dma_dev_name;
  24. uint8_t txdma_id;
  25. uint8_t txdma_chan;
  26. uint8_t rxdma_id;
  27. uint8_t rxdma_chan;
  28. uint8_t clock_id;
  29. uint8_t reset_id;
  30. uint8_t flag_use_txdma:1;
  31. uint8_t flag_use_rxdma:1;
  32. uint32_t baud_rate;
  33. uart_irq_config_func_t irq_config_func;
  34. };
  35. /* Device data structure */
  36. struct acts_uart_data {
  37. /* Baud rate */
  38. struct uart_config cfg;
  39. uint32_t baud_rate;
  40. #ifdef CONFIG_UART_INTERRUPT_DRIVEN
  41. /* IRQ Callback function pointer */
  42. uart_irq_callback_user_data_t cb;
  43. void *user_data;
  44. #endif
  45. #ifdef CONFIG_UART_DMA_DRIVEN
  46. struct device *dma_dev;
  47. uint8_t txdma_chan;
  48. uint8_t rxdma_chan;
  49. #endif
  50. };
  51. /* convenience defines */
  52. #define DEV_CFG(dev) \
  53. ((const struct acts_uart_config * const)(dev)->config)
  54. #define DEV_DATA(dev) \
  55. ((struct acts_uart_data * const)(dev)->data)
  56. #define UART_STRUCT(dev) \
  57. ((struct acts_uart_controller *)(DEV_CFG(dev))->base)
  58. /* UART registers struct */
  59. typedef struct acts_uart_controller {
  60. volatile uint32_t ctrl;
  61. volatile uint32_t rxdat;
  62. volatile uint32_t txdat;
  63. volatile uint32_t stat;
  64. volatile uint32_t br;
  65. } uart_register_t;
  66. /* bits */
  67. #define UART_CTL_RX_EN BIT(31)
  68. #define UART_CTL_TX_EN BIT(30)
  69. #define UART_CTL_TX_FIFO_EN BIT(29)
  70. #define UART_CTL_RX_FIFO_EN BIT(28)
  71. #define UART_CTL_TX_DST_SHIFT 26
  72. #define UART_FIFO_CPU 0
  73. #define UART_FIFO_DMA 1
  74. #define UART_FIFO_DSP 2
  75. #define UART_CTL_TX_DST(x) ((x) << UART_CTL_TX_DST_SHIFT)
  76. #define UART_CTL_TX_DST_MASK UART_CTL_TX_DST(0x3)
  77. #define UART_CTL_RX_SRC_SHIFT 24
  78. #define UART_CTL_RX_SRC(x) ((x) << UART_CTL_RX_SRC_SHIFT)
  79. #define UART_CTL_RX_SRC_MASK UART_CTL_RX_SRC(0x3)
  80. //#define UART_CTL_TX_DMA BIT(21)
  81. #define UART_CTL_LB_EN BIT(20)
  82. #define UART_CTL_TX_IE BIT(19)
  83. #define UART_CTL_RX_IE BIT(18)
  84. #define UART_CTL_TX_DE BIT(17)
  85. #define UART_CTL_RX_DE BIT(16)
  86. #define UART_CTL_EN BIT(15)
  87. //#define UART_CTL_RX_DMA BIT(14)
  88. #define UART_CTL_RTS_EN BIT(13)
  89. #define UART_CTL_AF_EN BIT(12)
  90. #define UART_CTL_RX_FIFO_THREHOLD_SHIFT 10
  91. #define UART_CTL_RX_FIFO_THREHOLD(x) ((x) << UART_CTL_RX_FIFO_THREHOLD_SHIFT)
  92. #define UART_CTL_RX_FIFO_THREHOLD_MASK UART_CTL_RX_FIFO_THREHOLD(0x3)
  93. #define UART_CTL_RX_FIFO_THREHOLD_1BYTE UART_CTL_RX_FIFO_THREHOLD(0x0)
  94. #define UART_CTL_RX_FIFO_THREHOLD_4BYTES UART_CTL_RX_FIFO_THREHOLD(0x1)
  95. #define UART_CTL_RX_FIFO_THREHOLD_8BYTES UART_CTL_RX_FIFO_THREHOLD(0x2)
  96. #define UART_CTL_RX_FIFO_THREHOLD_12BYTES UART_CTL_RX_FIFO_THREHOLD(0x3)
  97. #define UART_CTL_TX_FIFO_THREHOLD_SHIFT 8
  98. #define UART_CTL_TX_FIFO_THREHOLD(x) ((x) << UART_CTL_TX_FIFO_THREHOLD_SHIFT)
  99. #define UART_CTL_TX_FIFO_THREHOLD_MASK UART_CTL_TX_FIFO_THREHOLD(0x3)
  100. #define UART_CTL_TX_FIFO_THREHOLD_1BYTE UART_CTL_TX_FIFO_THREHOLD(0x0)
  101. #define UART_CTL_TX_FIFO_THREHOLD_4BYTES UART_CTL_TX_FIFO_THREHOLD(0x1)
  102. #define UART_CTL_TX_FIFO_THREHOLD_8BYTES UART_CTL_TX_FIFO_THREHOLD(0x2)
  103. #define UART_CTL_TX_FIFO_THREHOLD_12BYTES UART_CTL_TX_FIFO_THREHOLD(0x3)
  104. //#define UART_CTL_CTS_EN BIT(7)
  105. #define UART_CTL_PARITY_SHIFT 4
  106. #define UART_CTL_PARITY(x) ((x) << UART_CTL_PARITY_SHIFT)
  107. #define UART_CTL_PARITY_MASK UART_CTL_PARITY(0x7)
  108. #define UART_CTL_PARITY_NONE UART_CTL_PARITY(0x0)
  109. #define UART_CTL_PARITY_ODD UART_CTL_PARITY(0x4)
  110. #define UART_CTL_PARITY_LOGIC_1 UART_CTL_PARITY(0x5)
  111. #define UART_CTL_PARITY_EVEN UART_CTL_PARITY(0x6)
  112. #define UART_CTL_PARITY_LOGIC_0 UART_CTL_PARITY(0x7)
  113. #define UART_CTL_STOP_SHIFT 2
  114. #define UART_CTL_STOP(x) ((x) << UART_CTL_STOP_SHIFT)
  115. #define UART_CTL_STOP_MASK UART_CTL_STOP(0x1)
  116. #define UART_CTL_STOP_1BIT UART_CTL_STOP(0x0)
  117. #define UART_CTL_STOP_2BIT UART_CTL_STOP(0x1)
  118. #define UART_CTL_DATA_WIDTH_SHIFT 0
  119. #define UART_CTL_DATA_WIDTH(x) ((x) << UART_CTL_DATA_WIDTH_SHIFT)
  120. #define UART_CTL_DATA_WIDTH_MASK UART_CTL_DATA_WIDTH(0x3)
  121. #define UART_CTL_DATA_WIDTH_5BIT UART_CTL_DATA_WIDTH(0x0)
  122. #define UART_CTL_DATA_WIDTH_6BIT UART_CTL_DATA_WIDTH(0x1)
  123. #define UART_CTL_DATA_WIDTH_7BIT UART_CTL_DATA_WIDTH(0x2)
  124. #define UART_CTL_DATA_WIDTH_8BIT UART_CTL_DATA_WIDTH(0x3)
  125. #define UART_STA_PAER BIT(23)
  126. #define UART_STA_STER BIT(22)
  127. #define UART_STA_UTBB BIT(21)
  128. #define UART_STA_TXFL_SHIFT 16
  129. #define UART_STA_TXFL_MASK (0x1f << UART_STA_TXFL_SHIFT)
  130. #define UART_STA_RXFL_SHIFT 11
  131. #define UART_STA_RXFL_MASK (0x1f << UART_STA_RXFL_SHIFT)
  132. #define UART_STA_TFES BIT(10)
  133. #define UART_STA_RFFS BIT(9)
  134. #define UART_STA_RTSS BIT(8)
  135. #define UART_STA_CTSS BIT(7)
  136. #define UART_STA_TFFU BIT(6)
  137. #define UART_STA_RFEM BIT(5)
  138. #define UART_STA_RXST BIT(4)
  139. #define UART_STA_TFER BIT(3)
  140. #define UART_STA_RXER BIT(2)
  141. #define UART_STA_TIP BIT(1)
  142. #define UART_STA_RIP BIT(0)
  143. #define UART_STA_ERRS (UART_STA_RXER | UART_STA_TFER | \
  144. UART_STA_RXST | UART_STA_PAER | \
  145. UART_STA_STER)
  146. #define UART_BAUD_DIV(freq, baud_rate) (((freq) / (baud_rate))) + ((((freq) % (baud_rate)) + ((baud_rate) >> 1)) / baud_rate)
  147. static void uart_acts_init_bycfg(const struct device *dev)
  148. {
  149. struct acts_uart_data * dev_data = DEV_DATA(dev);
  150. struct acts_uart_controller *uart = UART_STRUCT(dev);
  151. struct uart_config *cfg = &dev_data->cfg;
  152. unsigned int div, ctl, ctl_mask;
  153. ctl = UART_CTL_DATA_WIDTH((cfg->data_bits&0x3));
  154. if(cfg->stop_bits== UART_CFG_STOP_BITS_2)
  155. ctl |= UART_CTL_STOP_2BIT;
  156. else
  157. ctl |= UART_CTL_STOP_1BIT;
  158. switch(cfg->parity){
  159. case UART_CFG_PARITY_NONE:
  160. ctl |= UART_CTL_PARITY_NONE;
  161. break;
  162. case UART_CFG_PARITY_ODD:
  163. ctl |= UART_CTL_PARITY_ODD;
  164. break;
  165. case UART_CFG_PARITY_EVEN:
  166. ctl |= UART_CTL_PARITY_EVEN;
  167. break;
  168. case UART_CFG_PARITY_MARK:
  169. ctl |= UART_CTL_PARITY_LOGIC_1;
  170. break;
  171. case UART_CFG_PARITY_SPACE:
  172. ctl |= UART_CTL_PARITY_LOGIC_0;
  173. break;
  174. }
  175. ctl_mask = UART_CTL_PARITY_MASK | UART_CTL_STOP_MASK |UART_CTL_DATA_WIDTH_MASK;
  176. uart->ctrl = (uart->ctrl&(~ctl_mask)) | ctl;
  177. uart->ctrl &= ~UART_CTL_EN; /*fix set 2M buad error*/
  178. div = UART_BAUD_DIV(CONFIG_HOSC_CLK_MHZ * 1000000,cfg->baudrate);
  179. uart->br = (div | (div << 16));
  180. uart->ctrl |= UART_CTL_EN;
  181. /* clear error status */
  182. uart->stat = UART_STA_ERRS;
  183. }
  184. static int uart_acts_set_config(const struct device *dev, const struct uart_config *cfg)
  185. {
  186. struct acts_uart_data * dev_data = DEV_DATA(dev);
  187. struct uart_config *dcfg = &dev_data->cfg;
  188. memcpy(dcfg, cfg, sizeof(struct uart_config));
  189. uart_acts_init_bycfg(dev);
  190. return 0;
  191. }
  192. static int uart_acts_get_config(const struct device *dev, struct uart_config *cfg)
  193. {
  194. struct acts_uart_data * dev_data = DEV_DATA(dev);
  195. struct uart_config *dcfg = &dev_data->cfg;
  196. memcpy(cfg , dcfg, sizeof(struct uart_config));
  197. return 0;
  198. }
  199. /**
  200. * @brief Poll the device for input.
  201. *
  202. * @param dev UART device struct
  203. * @param c Pointer to character
  204. *
  205. * @return 0 if a character arrived, -1 if the input buffer if empty.
  206. */
  207. static int uart_acts_poll_in(const struct device *dev, unsigned char *c)
  208. {
  209. struct acts_uart_controller *uart = UART_STRUCT(dev);
  210. /* Wait for transmitter to be ready */
  211. while (uart->stat & UART_STA_RFEM)
  212. ;
  213. /* got a character */
  214. *c = (unsigned char)uart->rxdat;
  215. return 0;
  216. }
  217. /**
  218. * @brief Output a character in polled mode.
  219. *
  220. * Checks if the transmitter is empty. If empty, a character is written to
  221. * the data register.
  222. *
  223. * @param dev UART device struct
  224. * @param c Character to send
  225. *
  226. * @return Sent character
  227. */
  228. static void uart_acts_poll_out(const struct device *dev,
  229. unsigned char c)
  230. {
  231. struct acts_uart_controller *uart = UART_STRUCT(dev);
  232. /* Wait for transmitter to be ready */
  233. while (uart->stat & UART_STA_TFFU);
  234. /* send a character */
  235. uart->txdat = (uint32_t)c;
  236. }
  237. #ifdef CONFIG_UART_INTERRUPT_DRIVEN
  238. static int uart_acts_err_check(const struct device *dev)
  239. {
  240. struct acts_uart_controller *uart = UART_STRUCT(dev);
  241. uint32_t error = 0;
  242. if (uart->stat & UART_STA_ERRS) {
  243. if (uart->stat & (UART_STA_TFER | UART_STA_TFER))
  244. error |= UART_ERROR_OVERRUN;
  245. if (uart->stat & (UART_STA_STER | UART_STA_RXER))
  246. error |= UART_ERROR_FRAMING;
  247. if (uart->stat & UART_STA_PAER)
  248. error |= UART_ERROR_PARITY;
  249. /* clear error status */
  250. uart->stat = UART_STA_ERRS;
  251. }
  252. return error;
  253. }
  254. /** Interrupt driven FIFO fill function */
  255. static int uart_acts_fifo_fill(const struct device *dev, const uint8_t *tx_data, int len)
  256. {
  257. struct acts_uart_controller *uart = UART_STRUCT(dev);
  258. uint8_t num_tx = 0;
  259. while ((len - num_tx > 0) && !(uart->stat & UART_STA_TFFU)) {
  260. /* Send a character */
  261. uart->txdat = (uint8_t)tx_data[num_tx++];
  262. }
  263. /* Clear the interrupt */
  264. uart->stat = UART_STA_TIP;
  265. return (int)num_tx;
  266. }
  267. /** Interrupt driven FIFO read function */
  268. static int uart_acts_fifo_read(const struct device *dev, uint8_t *rx_data, const int size)
  269. {
  270. struct acts_uart_controller *uart = UART_STRUCT(dev);
  271. uint8_t num_rx = 0;
  272. while ((size - num_rx > 0) && !(uart->stat & UART_STA_RFEM)) {
  273. /* Receive a character */
  274. rx_data[num_rx++] = (uint8_t)uart->rxdat;
  275. }
  276. /* Clear the interrupt */
  277. uart->stat = UART_STA_RIP;
  278. return num_rx;
  279. }
  280. /** Interrupt driven transfer enabling function */
  281. static void uart_acts_irq_tx_enable(const struct device *dev)
  282. {
  283. struct acts_uart_controller *uart = UART_STRUCT(dev);
  284. uart->ctrl |= UART_CTL_TX_IE;
  285. }
  286. /** Interrupt driven transfer disabling function */
  287. static void uart_acts_irq_tx_disable(const struct device *dev)
  288. {
  289. struct acts_uart_controller *uart = UART_STRUCT(dev);
  290. uart->ctrl &= ~UART_CTL_TX_IE;
  291. }
  292. /** Interrupt driven transfer ready function */
  293. static int uart_acts_irq_tx_ready(const struct device *dev)
  294. {
  295. struct acts_uart_controller *uart = UART_STRUCT(dev);
  296. return !(uart->stat & UART_STA_TFFU);
  297. }
  298. /** Interrupt driven receiver enabling function */
  299. static void uart_acts_irq_rx_enable(const struct device *dev)
  300. {
  301. struct acts_uart_controller *uart = UART_STRUCT(dev);
  302. uart->ctrl |= UART_CTL_RX_IE;
  303. }
  304. /** Interrupt driven receiver disabling function */
  305. static void uart_acts_irq_rx_disable(const struct device *dev)
  306. {
  307. struct acts_uart_controller *uart = UART_STRUCT(dev);
  308. uart->ctrl &= ~UART_CTL_RX_IE;
  309. }
  310. /** Interrupt driven transfer empty function */
  311. static int uart_acts_irq_tx_complete(const struct device *dev)
  312. {
  313. struct acts_uart_controller *uart = UART_STRUCT(dev);
  314. return (uart->stat & UART_STA_TFES);
  315. }
  316. /** Interrupt driven receiver ready function */
  317. static int uart_acts_irq_rx_ready(const struct device *dev)
  318. {
  319. struct acts_uart_controller *uart = UART_STRUCT(dev);
  320. return !(uart->stat & UART_STA_RFEM);
  321. }
  322. /** Interrupt driven pending status function */
  323. static int uart_acts_irq_is_pending(const struct device *dev)
  324. {
  325. struct acts_uart_controller *uart = UART_STRUCT(dev);
  326. int tx_pending, rx_pending;
  327. tx_pending = (uart->ctrl & UART_CTL_TX_IE) && (uart->stat & UART_STA_TIP);
  328. rx_pending = (uart->ctrl & UART_CTL_RX_IE) && (uart->stat & UART_STA_RIP);
  329. return (tx_pending || rx_pending);
  330. }
  331. /** Interrupt driven interrupt update function */
  332. static int uart_acts_irq_update(const struct device *dev)
  333. {
  334. return 1;
  335. }
  336. /** Set the callback function */
  337. static void uart_acts_irq_callback_set(const struct device *dev, uart_irq_callback_user_data_t cb, void *user_data)
  338. {
  339. struct acts_uart_data * const dev_data = DEV_DATA(dev);
  340. dev_data->cb = cb;
  341. dev_data->user_data = user_data;
  342. }
  343. /**
  344. * @brief Interrupt service routine.
  345. *
  346. * This simply calls the callback function, if one exists.
  347. *
  348. * @param arg Argument to ISR.
  349. *
  350. * @return N/A
  351. */
  352. void uart_acts_isr(void *arg)
  353. {
  354. struct device *dev = arg;
  355. struct acts_uart_data * const dev_data = DEV_DATA(dev);
  356. struct acts_uart_controller *uart = UART_STRUCT(dev);
  357. /* clear error status */
  358. uart->stat = UART_STA_ERRS;
  359. if (dev_data->cb) {
  360. dev_data->cb(dev, dev_data->user_data);
  361. }
  362. }
  363. #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
  364. #ifdef CONFIG_UART_DMA_DRIVEN
  365. #include <drivers/dma.h>
  366. int uart_acts_fifo_switch(struct device *dev, uint32_t is_tx, uint32_t fifo_type)
  367. {
  368. int tmp;
  369. struct acts_uart_controller *uart = UART_STRUCT(dev);
  370. tmp = uart->ctrl;
  371. if(is_tx){
  372. if(fifo_type > UART_FIFO_DMA){
  373. return -EINVAL;
  374. }
  375. while(!uart_irq_tx_complete(dev)){}
  376. //disable uart tx dma, tx threshold
  377. tmp &= (~(UART_CTL_TX_DST_MASK | UART_CTL_TX_FIFO_THREHOLD_MASK | UART_CTL_TX_DE | UART_CTL_TX_IE));
  378. tmp |= UART_CTL_TX_DST(fifo_type);
  379. if(fifo_type == UART_FIFO_DMA){
  380. tmp |= UART_CTL_TX_FIFO_THREHOLD_8BYTES | UART_CTL_TX_DE;
  381. }else{
  382. tmp |= UART_CTL_TX_FIFO_THREHOLD_1BYTE;
  383. }
  384. }else{
  385. if(fifo_type > UART_FIFO_DMA){
  386. return -EINVAL;
  387. }
  388. //disable uart rx dma, rx threshold
  389. tmp &= (~(UART_CTL_RX_SRC_MASK | UART_CTL_RX_FIFO_THREHOLD_MASK | UART_CTL_RX_DE | UART_CTL_RX_IE));
  390. tmp |= UART_CTL_RX_SRC(fifo_type);
  391. if(fifo_type == UART_FIFO_DMA){
  392. //dma
  393. tmp |= UART_CTL_RX_FIFO_THREHOLD_8BYTES | UART_CTL_RX_DE;
  394. }else{
  395. tmp |= UART_CTL_RX_FIFO_THREHOLD_1BYTE;
  396. }
  397. }
  398. uart->ctrl = tmp;
  399. return 0;
  400. }
  401. int uart_acts_dma_send_init(struct device *dev, dma_callback_t callback, void *arg)
  402. {
  403. struct dma_config dma_cfg;
  404. struct dma_block_config head_block;
  405. struct acts_uart_data *uart_data = DEV_DATA(dev);
  406. struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  407. if(!uart_config->flag_use_txdma){
  408. printk("err:dma not config by dts\n");
  409. return -1;
  410. }
  411. if(!uart_data->dma_dev){
  412. uart_data->dma_dev = (struct device *)device_get_binding(uart_config->dma_dev_name);
  413. if (!uart_data->dma_dev){
  414. printk("err:get dma(%s) dev fail\n", uart_config->dma_dev_name);
  415. return -1;
  416. }
  417. uart_data->txdma_chan = dma_request(uart_data->dma_dev, uart_config->txdma_chan);
  418. printk("uart use txdma chan=%d\n", uart_data->txdma_chan);
  419. }
  420. memset(&dma_cfg, 0, sizeof(dma_cfg));
  421. memset(&head_block, 0, sizeof(head_block));
  422. if (callback) {
  423. dma_cfg.dma_callback = callback;
  424. dma_cfg.user_data = arg;
  425. dma_cfg.complete_callback_en = 1;
  426. }
  427. head_block.source_address = (unsigned int)NULL;
  428. head_block.dest_address = (unsigned int)NULL;
  429. head_block.block_size = 0;
  430. head_block.source_reload_en = 0;
  431. dma_cfg.block_count = 1;
  432. dma_cfg.head_block = &head_block;
  433. head_block.block_size = 0;
  434. dma_cfg.dma_slot = uart_config->txdma_id;
  435. dma_cfg.channel_direction = MEMORY_TO_PERIPHERAL;
  436. dma_cfg.dest_data_size = 1;
  437. dma_config(uart_data->dma_dev, uart_data->txdma_chan, &dma_cfg);
  438. return 0;
  439. }
  440. int uart_acts_dma_send(struct device *dev, char *s, int len)
  441. {
  442. struct acts_uart_data *uart_data = DEV_DATA(dev);
  443. // struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  444. struct acts_uart_controller *uart = UART_STRUCT(dev);
  445. __ASSERT(uart_data->dma_dev, "uart dma dev not config\n");
  446. dma_reload(uart_data->dma_dev, uart_data->txdma_chan, (unsigned int) s, (unsigned int)(&uart->txdat), len);
  447. dma_start(uart_data->dma_dev, uart_data->txdma_chan);
  448. return 0;
  449. }
  450. int uart_acts_dma_send_complete(struct device *dev)
  451. {
  452. struct acts_uart_data *uart_data = DEV_DATA(dev);
  453. //struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  454. struct dma_status stat;
  455. __ASSERT(uart_data->dma_dev, "uart dma dev not config\n");
  456. dma_get_status(uart_data->dma_dev, uart_data->txdma_chan, &stat);
  457. if(stat.pending_length==0)
  458. return 1;
  459. return 0 ;
  460. }
  461. int uart_acts_dma_send_stop(struct device *dev)
  462. {
  463. struct acts_uart_data *uart_data = DEV_DATA(dev);
  464. // struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  465. dma_stop(uart_data->dma_dev, uart_data->txdma_chan);
  466. return 0;
  467. }
  468. int uart_acts_dma_receive_init(struct device *dev, dma_callback_t callback, void *arg)
  469. {
  470. struct dma_config dma_cfg;
  471. struct dma_block_config head_block;
  472. struct acts_uart_data *uart_data = DEV_DATA(dev);
  473. struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  474. if(!uart_config->flag_use_rxdma){
  475. printk("err:dma rx not config by dts\n");
  476. return -1;
  477. }
  478. if(!uart_data->dma_dev){
  479. uart_data->dma_dev = (struct device *)device_get_binding(uart_config->dma_dev_name);
  480. if (!uart_data->dma_dev){
  481. printk("err:get dma(%s) dev fail\n", uart_config->dma_dev_name);
  482. return -1;
  483. }
  484. }
  485. uart_data->rxdma_chan = dma_request(uart_data->dma_dev, uart_config->rxdma_chan);
  486. printk("uart use rxdma chan=%d\n", uart_data->rxdma_chan);
  487. memset(&dma_cfg, 0, sizeof(dma_cfg));
  488. memset(&head_block, 0, sizeof(head_block));
  489. if (callback) {
  490. dma_cfg.dma_callback = callback;
  491. dma_cfg.user_data = arg;
  492. dma_cfg.complete_callback_en = 1;
  493. }
  494. head_block.source_address = (unsigned int)NULL;
  495. head_block.dest_address = (unsigned int)NULL;
  496. head_block.block_size = 0;
  497. head_block.source_reload_en = 0;
  498. dma_cfg.block_count = 200;
  499. dma_cfg.head_block = &head_block;
  500. head_block.block_size = 0;
  501. dma_cfg.dma_slot = uart_config->rxdma_id;
  502. dma_cfg.channel_direction = PERIPHERAL_TO_MEMORY;
  503. dma_cfg.dest_data_size = 200;
  504. dma_config(uart_data->dma_dev, uart_data->rxdma_chan, &dma_cfg);
  505. return 0;
  506. }
  507. int uart_acts_dma_receive(struct device *dev, char *d, int len)
  508. {
  509. struct acts_uart_data *uart_data = DEV_DATA(dev);
  510. // struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  511. struct acts_uart_controller *uart = UART_STRUCT(dev);
  512. __ASSERT(uart_data->dma_dev, "uart dma dev not config\n");
  513. dma_reload(uart_data->dma_dev, uart_data->rxdma_chan, (unsigned int)(&uart->rxdat), (unsigned int)d, len);
  514. dma_start(uart_data->dma_dev, uart_data->rxdma_chan);
  515. return 0;
  516. }
  517. int uart_acts_dma_receive_complete(struct device *dev)
  518. {
  519. struct acts_uart_data *uart_data = DEV_DATA(dev);
  520. //struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  521. struct dma_status stat;
  522. __ASSERT(uart_data->dma_dev, "uart dma dev not config\n");
  523. dma_get_status(uart_data->dma_dev, uart_data->rxdma_chan, &stat);
  524. if(stat.pending_length==0)
  525. return 1;
  526. return 0 ;
  527. }
  528. int uart_acts_dma_receive_stop(struct device *dev)
  529. {
  530. struct acts_uart_data *uart_data = DEV_DATA(dev);
  531. struct dma_status stat;
  532. // struct acts_uart_config *uart_config = (struct acts_uart_config *)DEV_CFG(dev);
  533. dma_get_status(uart_data->dma_dev, uart_data->rxdma_chan, &stat);
  534. dma_stop(uart_data->dma_dev, uart_data->rxdma_chan);
  535. return stat.pending_length;
  536. }
  537. int uart_acts_dma_receive_drq_switch(struct device *dev, bool drq_enable)
  538. {
  539. struct acts_uart_controller *uart = UART_STRUCT(dev);
  540. if (drq_enable)
  541. {
  542. uart->ctrl |= UART_CTL_RX_DE;
  543. }
  544. else
  545. {
  546. uart->ctrl &= ~UART_CTL_RX_DE;
  547. }
  548. return 0;
  549. }
  550. #endif
  551. static const struct uart_driver_api uart_acts_driver_api = {
  552. .poll_in = uart_acts_poll_in,
  553. .poll_out = uart_acts_poll_out,
  554. .configure = uart_acts_set_config,
  555. .config_get = uart_acts_get_config,
  556. #ifdef CONFIG_UART_INTERRUPT_DRIVEN
  557. .err_check = uart_acts_err_check,
  558. .fifo_fill = uart_acts_fifo_fill,
  559. .fifo_read = uart_acts_fifo_read,
  560. .irq_tx_enable = uart_acts_irq_tx_enable,
  561. .irq_tx_disable = uart_acts_irq_tx_disable,
  562. .irq_tx_ready = uart_acts_irq_tx_ready,
  563. .irq_rx_enable = uart_acts_irq_rx_enable,
  564. .irq_rx_disable = uart_acts_irq_rx_disable,
  565. .irq_tx_complete = uart_acts_irq_tx_complete,
  566. .irq_rx_ready = uart_acts_irq_rx_ready,
  567. .irq_is_pending = uart_acts_irq_is_pending,
  568. .irq_update = uart_acts_irq_update,
  569. .irq_callback_set = uart_acts_irq_callback_set,
  570. #endif
  571. };
  572. #ifdef CONFIG_CFG_DRV
  573. static void acts_pinmux_setup_pins_dynamic_uart(void)
  574. {
  575. struct acts_pin_config pinconf[2];
  576. unsigned short pin_val;
  577. #if (CONFIG_UART_CONSOLE_ON_DEV_NAME == CONFIG_UART_0_NAME)
  578. pinconf[0] = UART0_MFP_CFG;
  579. pinconf[1] = UART0_MFP_CFG;
  580. #endif
  581. #if (CONFIG_UART_CONSOLE_ON_DEV_NAME == CONFIG_UART_1_NAME)
  582. pinconf[0] = UART1_MFP_CFG;
  583. pinconf[1] = UART1_MFP_CFG;
  584. #endif
  585. pin_val = GPIO_NONE;
  586. if(cfg_get_by_key(ITEM_UART_TX_PIN, &pin_val, 2) && (pin_val != GPIO_NONE)){
  587. pinconf[0].pin_num = pin_val&0xff;
  588. pinconf[0].mode = (pinconf[0].mode & (~GPIO_CTL_MFP_MASK)) | ((pin_val>>8) & GPIO_CTL_MFP_MASK);
  589. }
  590. pin_val = GPIO_NONE;
  591. if(cfg_get_by_key(ITEM_UART_RX_PIN, &pin_val, 2) && (pin_val != GPIO_NONE) ){
  592. pinconf[1].pin_num = pin_val&0xff;
  593. pinconf[1].mode = (pinconf[0].mode & (~GPIO_CTL_MFP_MASK)) | ((pin_val>>8)&GPIO_CTL_MFP_MASK);
  594. }
  595. acts_pinmux_setup_pins(pinconf, 2);
  596. }
  597. #endif
  598. /**
  599. * @brief Initialize UART channel
  600. *
  601. * This routine is called to reset the chip in a quiescent state.
  602. * It is assumed that this function is called only once per UART.
  603. *
  604. * @param dev UART device struct
  605. *
  606. * @return 0
  607. */
  608. static int uart_acts_init(const struct device *dev)
  609. {
  610. const struct acts_uart_config *dev_cfg = DEV_CFG(dev);
  611. struct acts_uart_data * dev_data = DEV_DATA(dev);
  612. struct acts_uart_controller *uart = UART_STRUCT(dev);
  613. struct uart_config *cfg = &dev_data->cfg;
  614. #ifdef CONFIG_CFG_DRV
  615. struct acts_pin_config pinconf[2];
  616. unsigned short pin_val;
  617. #endif
  618. /* enable uart clock */
  619. acts_clock_peripheral_enable(dev_cfg->clock_id);
  620. /* clear reset */
  621. acts_reset_peripheral_deassert(dev_cfg->reset_id);
  622. /* wait busy if uart enabled */
  623. if (uart->ctrl & UART_CTL_EN) {
  624. while (uart->stat & UART_STA_UTBB);
  625. }
  626. /* enable rx/tx, 8/1/n */
  627. uart->ctrl = UART_CTL_EN |
  628. UART_CTL_RX_EN | UART_CTL_RX_FIFO_EN | UART_CTL_RX_FIFO_THREHOLD_1BYTE |
  629. UART_CTL_TX_EN | UART_CTL_TX_FIFO_EN | UART_CTL_TX_FIFO_THREHOLD_1BYTE |
  630. UART_CTL_DATA_WIDTH_8BIT | UART_CTL_STOP_1BIT | UART_CTL_PARITY_NONE;
  631. #if 0
  632. //#ifdef CONFIG_CFG_DRV
  633. acts_pinmux_setup_pins_dynamic_uart();
  634. if(!cfg_get_by_key(ITEM_UART_BAUDRATE, &cfg->baudrate, 4)){
  635. cfg->baudrate = dev_cfg->baud_rate;
  636. }
  637. printk("drv cfg: console,baud=%d\n", dev_cfg->baud_rate);
  638. //#else
  639. #endif
  640. cfg->baudrate = dev_cfg->baud_rate;
  641. //#endif
  642. cfg->parity = UART_CFG_PARITY_NONE;
  643. cfg->stop_bits = UART_CFG_STOP_BITS_1;
  644. cfg->data_bits = UART_CFG_DATA_BITS_8;
  645. cfg->flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
  646. uart_acts_init_bycfg(dev);
  647. /* clear error status */
  648. //uart->stat = UART_STA_ERRS;
  649. #ifdef CONFIG_UART_INTERRUPT_DRIVEN
  650. DEV_CFG(dev)->irq_config_func(dev);
  651. #endif
  652. return 0;
  653. }
  654. /*
  655. .rxdma_id =DT_INST_DMAS_CELL_BY_NAME(n, rx, devid),\
  656. .rxdma_chan = DT_INST_DMAS_CELL_BY_NAME(n, rx, channel),\
  657. */
  658. #define dma_use(n) (\
  659. .dma_dev_name = CONFIG_DMA_0_NAME, \
  660. .txdma_id = CONFIG_UART_##n##_TX_DMA_ID,\
  661. .txdma_chan = CONFIG_UART_##n##_TX_DMA_CHAN,\
  662. .rxdma_id = CONFIG_UART_##n##_RX_DMA_ID,\
  663. .rxdma_chan = CONFIG_UART_##n##_RX_DMA_CHAN,\
  664. .flag_use_txdma = 1, \
  665. .flag_use_rxdma = 1, \
  666. )
  667. #define dma_not(n) (\
  668. .flag_use_txdma = 0, \
  669. .flag_use_rxdma = 0, \
  670. )
  671. #define UART_ACTS_DEFINE_CONFIG(n) \
  672. static const struct device DEVICE_NAME_GET(uart_acts_##n); \
  673. \
  674. static void uart##n##_acts_irq_config(const struct device *port) \
  675. { \
  676. IRQ_CONNECT(IRQ_ID_UART##n, CONFIG_UART_##n##_IRQ_PRI, \
  677. uart_acts_isr, \
  678. DEVICE_GET(uart_acts_##n), 0); \
  679. irq_enable(IRQ_ID_UART##n); \
  680. } \
  681. static const struct acts_uart_config uart_acts_config_##n = { \
  682. .base = UART##n##_REG_BASE,\
  683. .clock_id = CLOCK_ID_UART##n,\
  684. .reset_id = RESET_ID_UART##n,\
  685. .baud_rate = CONFIG_UART_##n##_SPEED, \
  686. COND_CODE_1(CONFIG_UART_##n##_USE_TX_DMA,dma_use(n), dma_not(n))\
  687. .irq_config_func = uart##n##_acts_irq_config, \
  688. }
  689. #define UART_ACTS_DEVICE_INIT(n) \
  690. UART_ACTS_DEFINE_CONFIG(n); \
  691. static struct acts_uart_data uart_acts_dev_data_##n ;\
  692. DEVICE_DEFINE(uart_acts_##n, \
  693. CONFIG_UART_##n##_NAME, \
  694. &uart_acts_init, NULL, &uart_acts_dev_data_##n, \
  695. &uart_acts_config_##n, PRE_KERNEL_1, \
  696. 0, &uart_acts_driver_api);
  697. #if IS_ENABLED(CONFIG_UART_0)
  698. UART_ACTS_DEVICE_INIT(0)
  699. #endif
  700. #if IS_ENABLED(CONFIG_UART_1)
  701. UART_ACTS_DEVICE_INIT(1)
  702. #endif
  703. #if IS_ENABLED(CONFIG_UART_2)
  704. UART_ACTS_DEVICE_INIT(2)
  705. #endif
  706. #if IS_ENABLED(CONFIG_UART_3)
  707. UART_ACTS_DEVICE_INIT(3)
  708. #endif
  709. #if IS_ENABLED(CONFIG_UART_4)
  710. UART_ACTS_DEVICE_INIT(4)
  711. #endif