uart_acts.c 27 KB

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