mxs-auart.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /*
  2. * Freescale STMP37XX/STMP378X Application UART driver
  3. *
  4. * Author: dmitry pervushin <dimka@embeddedalley.com>
  5. *
  6. * Copyright 2008-2010 Freescale Semiconductor, Inc.
  7. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  8. *
  9. * The code contained herein is licensed under the GNU General Public
  10. * License. You may obtain a copy of the GNU General Public License
  11. * Version 2 or later at the following locations:
  12. *
  13. * http://www.opensource.org/licenses/gpl-license.html
  14. * http://www.gnu.org/copyleft/gpl.html
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/console.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/wait.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_driver.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/serial.h>
  28. #include <linux/serial_core.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/device.h>
  31. #include <linux/clk.h>
  32. #include <linux/delay.h>
  33. #include <linux/io.h>
  34. #include <linux/of_device.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/dmaengine.h>
  37. #include <asm/cacheflush.h>
  38. #define MXS_AUART_PORTS 5
  39. #define MXS_AUART_FIFO_SIZE 16
  40. #define AUART_CTRL0 0x00000000
  41. #define AUART_CTRL0_SET 0x00000004
  42. #define AUART_CTRL0_CLR 0x00000008
  43. #define AUART_CTRL0_TOG 0x0000000c
  44. #define AUART_CTRL1 0x00000010
  45. #define AUART_CTRL1_SET 0x00000014
  46. #define AUART_CTRL1_CLR 0x00000018
  47. #define AUART_CTRL1_TOG 0x0000001c
  48. #define AUART_CTRL2 0x00000020
  49. #define AUART_CTRL2_SET 0x00000024
  50. #define AUART_CTRL2_CLR 0x00000028
  51. #define AUART_CTRL2_TOG 0x0000002c
  52. #define AUART_LINECTRL 0x00000030
  53. #define AUART_LINECTRL_SET 0x00000034
  54. #define AUART_LINECTRL_CLR 0x00000038
  55. #define AUART_LINECTRL_TOG 0x0000003c
  56. #define AUART_LINECTRL2 0x00000040
  57. #define AUART_LINECTRL2_SET 0x00000044
  58. #define AUART_LINECTRL2_CLR 0x00000048
  59. #define AUART_LINECTRL2_TOG 0x0000004c
  60. #define AUART_INTR 0x00000050
  61. #define AUART_INTR_SET 0x00000054
  62. #define AUART_INTR_CLR 0x00000058
  63. #define AUART_INTR_TOG 0x0000005c
  64. #define AUART_DATA 0x00000060
  65. #define AUART_STAT 0x00000070
  66. #define AUART_DEBUG 0x00000080
  67. #define AUART_VERSION 0x00000090
  68. #define AUART_AUTOBAUD 0x000000a0
  69. #define AUART_CTRL0_SFTRST (1 << 31)
  70. #define AUART_CTRL0_CLKGATE (1 << 30)
  71. #define AUART_CTRL0_RXTO_ENABLE (1 << 27)
  72. #define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
  73. #define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
  74. #define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
  75. #define AUART_CTRL2_DMAONERR (1 << 26)
  76. #define AUART_CTRL2_TXDMAE (1 << 25)
  77. #define AUART_CTRL2_RXDMAE (1 << 24)
  78. #define AUART_CTRL2_CTSEN (1 << 15)
  79. #define AUART_CTRL2_RTSEN (1 << 14)
  80. #define AUART_CTRL2_RTS (1 << 11)
  81. #define AUART_CTRL2_RXE (1 << 9)
  82. #define AUART_CTRL2_TXE (1 << 8)
  83. #define AUART_CTRL2_UARTEN (1 << 0)
  84. #define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
  85. #define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
  86. #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
  87. #define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
  88. #define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
  89. #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
  90. #define AUART_LINECTRL_WLEN_MASK 0x00000060
  91. #define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
  92. #define AUART_LINECTRL_FEN (1 << 4)
  93. #define AUART_LINECTRL_STP2 (1 << 3)
  94. #define AUART_LINECTRL_EPS (1 << 2)
  95. #define AUART_LINECTRL_PEN (1 << 1)
  96. #define AUART_LINECTRL_BRK (1 << 0)
  97. #define AUART_INTR_RTIEN (1 << 22)
  98. #define AUART_INTR_TXIEN (1 << 21)
  99. #define AUART_INTR_RXIEN (1 << 20)
  100. #define AUART_INTR_CTSMIEN (1 << 17)
  101. #define AUART_INTR_RTIS (1 << 6)
  102. #define AUART_INTR_TXIS (1 << 5)
  103. #define AUART_INTR_RXIS (1 << 4)
  104. #define AUART_INTR_CTSMIS (1 << 1)
  105. #define AUART_STAT_BUSY (1 << 29)
  106. #define AUART_STAT_CTS (1 << 28)
  107. #define AUART_STAT_TXFE (1 << 27)
  108. #define AUART_STAT_TXFF (1 << 25)
  109. #define AUART_STAT_RXFE (1 << 24)
  110. #define AUART_STAT_OERR (1 << 19)
  111. #define AUART_STAT_BERR (1 << 18)
  112. #define AUART_STAT_PERR (1 << 17)
  113. #define AUART_STAT_FERR (1 << 16)
  114. #define AUART_STAT_RXCOUNT_MASK 0xffff
  115. static struct uart_driver auart_driver;
  116. enum mxs_auart_type {
  117. IMX23_AUART,
  118. IMX28_AUART,
  119. };
  120. struct mxs_auart_port {
  121. struct uart_port port;
  122. #define MXS_AUART_DMA_ENABLED 0x2
  123. #define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
  124. #define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
  125. #define MXS_AUART_RTSCTS 4 /* bit 4 */
  126. unsigned long flags;
  127. unsigned int ctrl;
  128. enum mxs_auart_type devtype;
  129. unsigned int irq;
  130. struct clk *clk;
  131. struct device *dev;
  132. /* for DMA */
  133. struct scatterlist tx_sgl;
  134. struct dma_chan *tx_dma_chan;
  135. void *tx_dma_buf;
  136. struct scatterlist rx_sgl;
  137. struct dma_chan *rx_dma_chan;
  138. void *rx_dma_buf;
  139. };
  140. static struct platform_device_id mxs_auart_devtype[] = {
  141. { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
  142. { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
  143. { /* sentinel */ }
  144. };
  145. MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
  146. static struct of_device_id mxs_auart_dt_ids[] = {
  147. {
  148. .compatible = "fsl,imx28-auart",
  149. .data = &mxs_auart_devtype[IMX28_AUART]
  150. }, {
  151. .compatible = "fsl,imx23-auart",
  152. .data = &mxs_auart_devtype[IMX23_AUART]
  153. }, { /* sentinel */ }
  154. };
  155. MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
  156. static inline int is_imx28_auart(struct mxs_auart_port *s)
  157. {
  158. return s->devtype == IMX28_AUART;
  159. }
  160. static inline bool auart_dma_enabled(struct mxs_auart_port *s)
  161. {
  162. return s->flags & MXS_AUART_DMA_ENABLED;
  163. }
  164. static void mxs_auart_stop_tx(struct uart_port *u);
  165. #define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
  166. static void mxs_auart_tx_chars(struct mxs_auart_port *s);
  167. static void dma_tx_callback(void *param)
  168. {
  169. struct mxs_auart_port *s = param;
  170. struct circ_buf *xmit = &s->port.state->xmit;
  171. dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
  172. /* clear the bit used to serialize the DMA tx. */
  173. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  174. smp_mb__after_clear_bit();
  175. /* wake up the possible processes. */
  176. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  177. uart_write_wakeup(&s->port);
  178. mxs_auart_tx_chars(s);
  179. }
  180. static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
  181. {
  182. struct dma_async_tx_descriptor *desc;
  183. struct scatterlist *sgl = &s->tx_sgl;
  184. struct dma_chan *channel = s->tx_dma_chan;
  185. u32 pio;
  186. /* [1] : send PIO. Note, the first pio word is CTRL1. */
  187. pio = AUART_CTRL1_XFER_COUNT(size);
  188. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
  189. 1, DMA_TRANS_NONE, 0);
  190. if (!desc) {
  191. dev_err(s->dev, "step 1 error\n");
  192. return -EINVAL;
  193. }
  194. /* [2] : set DMA buffer. */
  195. sg_init_one(sgl, s->tx_dma_buf, size);
  196. dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
  197. desc = dmaengine_prep_slave_sg(channel, sgl,
  198. 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  199. if (!desc) {
  200. dev_err(s->dev, "step 2 error\n");
  201. return -EINVAL;
  202. }
  203. /* [3] : submit the DMA */
  204. desc->callback = dma_tx_callback;
  205. desc->callback_param = s;
  206. dmaengine_submit(desc);
  207. dma_async_issue_pending(channel);
  208. return 0;
  209. }
  210. static void mxs_auart_tx_chars(struct mxs_auart_port *s)
  211. {
  212. struct circ_buf *xmit = &s->port.state->xmit;
  213. if (auart_dma_enabled(s)) {
  214. u32 i = 0;
  215. int size;
  216. void *buffer = s->tx_dma_buf;
  217. if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
  218. return;
  219. while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  220. size = min_t(u32, UART_XMIT_SIZE - i,
  221. CIRC_CNT_TO_END(xmit->head,
  222. xmit->tail,
  223. UART_XMIT_SIZE));
  224. memcpy(buffer + i, xmit->buf + xmit->tail, size);
  225. xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
  226. i += size;
  227. if (i >= UART_XMIT_SIZE)
  228. break;
  229. }
  230. if (uart_tx_stopped(&s->port))
  231. mxs_auart_stop_tx(&s->port);
  232. if (i) {
  233. mxs_auart_dma_tx(s, i);
  234. } else {
  235. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  236. smp_mb__after_clear_bit();
  237. }
  238. return;
  239. }
  240. while (!(readl(s->port.membase + AUART_STAT) &
  241. AUART_STAT_TXFF)) {
  242. if (s->port.x_char) {
  243. s->port.icount.tx++;
  244. writel(s->port.x_char,
  245. s->port.membase + AUART_DATA);
  246. s->port.x_char = 0;
  247. continue;
  248. }
  249. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  250. s->port.icount.tx++;
  251. writel(xmit->buf[xmit->tail],
  252. s->port.membase + AUART_DATA);
  253. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  254. } else
  255. break;
  256. }
  257. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  258. uart_write_wakeup(&s->port);
  259. if (uart_circ_empty(&(s->port.state->xmit)))
  260. writel(AUART_INTR_TXIEN,
  261. s->port.membase + AUART_INTR_CLR);
  262. else
  263. writel(AUART_INTR_TXIEN,
  264. s->port.membase + AUART_INTR_SET);
  265. if (uart_tx_stopped(&s->port))
  266. mxs_auart_stop_tx(&s->port);
  267. }
  268. static void mxs_auart_rx_char(struct mxs_auart_port *s)
  269. {
  270. int flag;
  271. u32 stat;
  272. u8 c;
  273. c = readl(s->port.membase + AUART_DATA);
  274. stat = readl(s->port.membase + AUART_STAT);
  275. flag = TTY_NORMAL;
  276. s->port.icount.rx++;
  277. if (stat & AUART_STAT_BERR) {
  278. s->port.icount.brk++;
  279. if (uart_handle_break(&s->port))
  280. goto out;
  281. } else if (stat & AUART_STAT_PERR) {
  282. s->port.icount.parity++;
  283. } else if (stat & AUART_STAT_FERR) {
  284. s->port.icount.frame++;
  285. }
  286. /*
  287. * Mask off conditions which should be ingored.
  288. */
  289. stat &= s->port.read_status_mask;
  290. if (stat & AUART_STAT_BERR) {
  291. flag = TTY_BREAK;
  292. } else if (stat & AUART_STAT_PERR)
  293. flag = TTY_PARITY;
  294. else if (stat & AUART_STAT_FERR)
  295. flag = TTY_FRAME;
  296. if (stat & AUART_STAT_OERR)
  297. s->port.icount.overrun++;
  298. if (uart_handle_sysrq_char(&s->port, c))
  299. goto out;
  300. uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
  301. out:
  302. writel(stat, s->port.membase + AUART_STAT);
  303. }
  304. static void mxs_auart_rx_chars(struct mxs_auart_port *s)
  305. {
  306. u32 stat = 0;
  307. for (;;) {
  308. stat = readl(s->port.membase + AUART_STAT);
  309. if (stat & AUART_STAT_RXFE)
  310. break;
  311. mxs_auart_rx_char(s);
  312. }
  313. writel(stat, s->port.membase + AUART_STAT);
  314. tty_flip_buffer_push(&s->port.state->port);
  315. }
  316. static int mxs_auart_request_port(struct uart_port *u)
  317. {
  318. return 0;
  319. }
  320. static int mxs_auart_verify_port(struct uart_port *u,
  321. struct serial_struct *ser)
  322. {
  323. if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
  324. return -EINVAL;
  325. return 0;
  326. }
  327. static void mxs_auart_config_port(struct uart_port *u, int flags)
  328. {
  329. }
  330. static const char *mxs_auart_type(struct uart_port *u)
  331. {
  332. struct mxs_auart_port *s = to_auart_port(u);
  333. return dev_name(s->dev);
  334. }
  335. static void mxs_auart_release_port(struct uart_port *u)
  336. {
  337. }
  338. static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
  339. {
  340. struct mxs_auart_port *s = to_auart_port(u);
  341. u32 ctrl = readl(u->membase + AUART_CTRL2);
  342. ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
  343. if (mctrl & TIOCM_RTS) {
  344. if (tty_port_cts_enabled(&u->state->port))
  345. ctrl |= AUART_CTRL2_RTSEN;
  346. else
  347. ctrl |= AUART_CTRL2_RTS;
  348. }
  349. s->ctrl = mctrl;
  350. writel(ctrl, u->membase + AUART_CTRL2);
  351. }
  352. static u32 mxs_auart_get_mctrl(struct uart_port *u)
  353. {
  354. struct mxs_auart_port *s = to_auart_port(u);
  355. u32 stat = readl(u->membase + AUART_STAT);
  356. int ctrl2 = readl(u->membase + AUART_CTRL2);
  357. u32 mctrl = s->ctrl;
  358. mctrl &= ~TIOCM_CTS;
  359. if (stat & AUART_STAT_CTS)
  360. mctrl |= TIOCM_CTS;
  361. if (ctrl2 & AUART_CTRL2_RTS)
  362. mctrl |= TIOCM_RTS;
  363. return mctrl;
  364. }
  365. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
  366. static void dma_rx_callback(void *arg)
  367. {
  368. struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
  369. struct tty_port *port = &s->port.state->port;
  370. int count;
  371. u32 stat;
  372. dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
  373. stat = readl(s->port.membase + AUART_STAT);
  374. stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
  375. AUART_STAT_PERR | AUART_STAT_FERR);
  376. count = stat & AUART_STAT_RXCOUNT_MASK;
  377. tty_insert_flip_string(port, s->rx_dma_buf, count);
  378. writel(stat, s->port.membase + AUART_STAT);
  379. tty_flip_buffer_push(port);
  380. /* start the next DMA for RX. */
  381. mxs_auart_dma_prep_rx(s);
  382. }
  383. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
  384. {
  385. struct dma_async_tx_descriptor *desc;
  386. struct scatterlist *sgl = &s->rx_sgl;
  387. struct dma_chan *channel = s->rx_dma_chan;
  388. u32 pio[1];
  389. /* [1] : send PIO */
  390. pio[0] = AUART_CTRL0_RXTO_ENABLE
  391. | AUART_CTRL0_RXTIMEOUT(0x80)
  392. | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
  393. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  394. 1, DMA_TRANS_NONE, 0);
  395. if (!desc) {
  396. dev_err(s->dev, "step 1 error\n");
  397. return -EINVAL;
  398. }
  399. /* [2] : send DMA request */
  400. sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
  401. dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
  402. desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
  403. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  404. if (!desc) {
  405. dev_err(s->dev, "step 2 error\n");
  406. return -1;
  407. }
  408. /* [3] : submit the DMA, but do not issue it. */
  409. desc->callback = dma_rx_callback;
  410. desc->callback_param = s;
  411. dmaengine_submit(desc);
  412. dma_async_issue_pending(channel);
  413. return 0;
  414. }
  415. static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
  416. {
  417. if (s->tx_dma_chan) {
  418. dma_release_channel(s->tx_dma_chan);
  419. s->tx_dma_chan = NULL;
  420. }
  421. if (s->rx_dma_chan) {
  422. dma_release_channel(s->rx_dma_chan);
  423. s->rx_dma_chan = NULL;
  424. }
  425. kfree(s->tx_dma_buf);
  426. kfree(s->rx_dma_buf);
  427. s->tx_dma_buf = NULL;
  428. s->rx_dma_buf = NULL;
  429. }
  430. static void mxs_auart_dma_exit(struct mxs_auart_port *s)
  431. {
  432. writel(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
  433. s->port.membase + AUART_CTRL2_CLR);
  434. mxs_auart_dma_exit_channel(s);
  435. s->flags &= ~MXS_AUART_DMA_ENABLED;
  436. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  437. clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
  438. }
  439. static int mxs_auart_dma_init(struct mxs_auart_port *s)
  440. {
  441. if (auart_dma_enabled(s))
  442. return 0;
  443. /* init for RX */
  444. s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
  445. if (!s->rx_dma_chan)
  446. goto err_out;
  447. s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  448. if (!s->rx_dma_buf)
  449. goto err_out;
  450. /* init for TX */
  451. s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
  452. if (!s->tx_dma_chan)
  453. goto err_out;
  454. s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  455. if (!s->tx_dma_buf)
  456. goto err_out;
  457. /* set the flags */
  458. s->flags |= MXS_AUART_DMA_ENABLED;
  459. dev_dbg(s->dev, "enabled the DMA support.");
  460. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  461. s->port.fifosize = UART_XMIT_SIZE;
  462. return 0;
  463. err_out:
  464. mxs_auart_dma_exit_channel(s);
  465. return -EINVAL;
  466. }
  467. static void mxs_auart_settermios(struct uart_port *u,
  468. struct ktermios *termios,
  469. struct ktermios *old)
  470. {
  471. struct mxs_auart_port *s = to_auart_port(u);
  472. u32 bm, ctrl, ctrl2, div;
  473. unsigned int cflag, baud;
  474. cflag = termios->c_cflag;
  475. ctrl = AUART_LINECTRL_FEN;
  476. ctrl2 = readl(u->membase + AUART_CTRL2);
  477. /* byte size */
  478. switch (cflag & CSIZE) {
  479. case CS5:
  480. bm = 0;
  481. break;
  482. case CS6:
  483. bm = 1;
  484. break;
  485. case CS7:
  486. bm = 2;
  487. break;
  488. case CS8:
  489. bm = 3;
  490. break;
  491. default:
  492. return;
  493. }
  494. ctrl |= AUART_LINECTRL_WLEN(bm);
  495. /* parity */
  496. if (cflag & PARENB) {
  497. ctrl |= AUART_LINECTRL_PEN;
  498. if ((cflag & PARODD) == 0)
  499. ctrl |= AUART_LINECTRL_EPS;
  500. }
  501. u->read_status_mask = 0;
  502. if (termios->c_iflag & INPCK)
  503. u->read_status_mask |= AUART_STAT_PERR;
  504. if (termios->c_iflag & (BRKINT | PARMRK))
  505. u->read_status_mask |= AUART_STAT_BERR;
  506. /*
  507. * Characters to ignore
  508. */
  509. u->ignore_status_mask = 0;
  510. if (termios->c_iflag & IGNPAR)
  511. u->ignore_status_mask |= AUART_STAT_PERR;
  512. if (termios->c_iflag & IGNBRK) {
  513. u->ignore_status_mask |= AUART_STAT_BERR;
  514. /*
  515. * If we're ignoring parity and break indicators,
  516. * ignore overruns too (for real raw support).
  517. */
  518. if (termios->c_iflag & IGNPAR)
  519. u->ignore_status_mask |= AUART_STAT_OERR;
  520. }
  521. /*
  522. * ignore all characters if CREAD is not set
  523. */
  524. if (cflag & CREAD)
  525. ctrl2 |= AUART_CTRL2_RXE;
  526. else
  527. ctrl2 &= ~AUART_CTRL2_RXE;
  528. /* figure out the stop bits requested */
  529. if (cflag & CSTOPB)
  530. ctrl |= AUART_LINECTRL_STP2;
  531. /* figure out the hardware flow control settings */
  532. if (cflag & CRTSCTS) {
  533. /*
  534. * The DMA has a bug(see errata:2836) in mx23.
  535. * So we can not implement the DMA for auart in mx23,
  536. * we can only implement the DMA support for auart
  537. * in mx28.
  538. */
  539. if (is_imx28_auart(s)
  540. && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
  541. if (!mxs_auart_dma_init(s))
  542. /* enable DMA tranfer */
  543. ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
  544. | AUART_CTRL2_DMAONERR;
  545. }
  546. ctrl2 |= AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN;
  547. } else {
  548. ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
  549. }
  550. /* set baud rate */
  551. baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
  552. div = u->uartclk * 32 / baud;
  553. ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
  554. ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
  555. writel(ctrl, u->membase + AUART_LINECTRL);
  556. writel(ctrl2, u->membase + AUART_CTRL2);
  557. uart_update_timeout(u, termios->c_cflag, baud);
  558. /* prepare for the DMA RX. */
  559. if (auart_dma_enabled(s) &&
  560. !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
  561. if (!mxs_auart_dma_prep_rx(s)) {
  562. /* Disable the normal RX interrupt. */
  563. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
  564. u->membase + AUART_INTR_CLR);
  565. } else {
  566. mxs_auart_dma_exit(s);
  567. dev_err(s->dev, "We can not start up the DMA.\n");
  568. }
  569. }
  570. }
  571. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  572. {
  573. u32 istat;
  574. struct mxs_auart_port *s = context;
  575. u32 stat = readl(s->port.membase + AUART_STAT);
  576. istat = readl(s->port.membase + AUART_INTR);
  577. /* ack irq */
  578. writel(istat & (AUART_INTR_RTIS
  579. | AUART_INTR_TXIS
  580. | AUART_INTR_RXIS
  581. | AUART_INTR_CTSMIS),
  582. s->port.membase + AUART_INTR_CLR);
  583. if (istat & AUART_INTR_CTSMIS) {
  584. uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
  585. writel(AUART_INTR_CTSMIS,
  586. s->port.membase + AUART_INTR_CLR);
  587. istat &= ~AUART_INTR_CTSMIS;
  588. }
  589. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  590. if (!auart_dma_enabled(s))
  591. mxs_auart_rx_chars(s);
  592. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  593. }
  594. if (istat & AUART_INTR_TXIS) {
  595. mxs_auart_tx_chars(s);
  596. istat &= ~AUART_INTR_TXIS;
  597. }
  598. return IRQ_HANDLED;
  599. }
  600. static void mxs_auart_reset(struct uart_port *u)
  601. {
  602. int i;
  603. unsigned int reg;
  604. writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
  605. for (i = 0; i < 10000; i++) {
  606. reg = readl(u->membase + AUART_CTRL0);
  607. if (!(reg & AUART_CTRL0_SFTRST))
  608. break;
  609. udelay(3);
  610. }
  611. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  612. }
  613. static int mxs_auart_startup(struct uart_port *u)
  614. {
  615. struct mxs_auart_port *s = to_auart_port(u);
  616. clk_prepare_enable(s->clk);
  617. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  618. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
  619. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  620. u->membase + AUART_INTR);
  621. /* Reset FIFO size (it could have changed if DMA was enabled) */
  622. u->fifosize = MXS_AUART_FIFO_SIZE;
  623. /*
  624. * Enable fifo so all four bytes of a DMA word are written to
  625. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  626. */
  627. writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
  628. return 0;
  629. }
  630. static void mxs_auart_shutdown(struct uart_port *u)
  631. {
  632. struct mxs_auart_port *s = to_auart_port(u);
  633. if (auart_dma_enabled(s))
  634. mxs_auart_dma_exit(s);
  635. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
  636. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  637. u->membase + AUART_INTR_CLR);
  638. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
  639. clk_disable_unprepare(s->clk);
  640. }
  641. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  642. {
  643. if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
  644. return TIOCSER_TEMT;
  645. else
  646. return 0;
  647. }
  648. static void mxs_auart_start_tx(struct uart_port *u)
  649. {
  650. struct mxs_auart_port *s = to_auart_port(u);
  651. /* enable transmitter */
  652. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
  653. mxs_auart_tx_chars(s);
  654. }
  655. static void mxs_auart_stop_tx(struct uart_port *u)
  656. {
  657. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
  658. }
  659. static void mxs_auart_stop_rx(struct uart_port *u)
  660. {
  661. writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
  662. }
  663. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  664. {
  665. if (ctl)
  666. writel(AUART_LINECTRL_BRK,
  667. u->membase + AUART_LINECTRL_SET);
  668. else
  669. writel(AUART_LINECTRL_BRK,
  670. u->membase + AUART_LINECTRL_CLR);
  671. }
  672. static void mxs_auart_enable_ms(struct uart_port *port)
  673. {
  674. /* just empty */
  675. }
  676. static struct uart_ops mxs_auart_ops = {
  677. .tx_empty = mxs_auart_tx_empty,
  678. .start_tx = mxs_auart_start_tx,
  679. .stop_tx = mxs_auart_stop_tx,
  680. .stop_rx = mxs_auart_stop_rx,
  681. .enable_ms = mxs_auart_enable_ms,
  682. .break_ctl = mxs_auart_break_ctl,
  683. .set_mctrl = mxs_auart_set_mctrl,
  684. .get_mctrl = mxs_auart_get_mctrl,
  685. .startup = mxs_auart_startup,
  686. .shutdown = mxs_auart_shutdown,
  687. .set_termios = mxs_auart_settermios,
  688. .type = mxs_auart_type,
  689. .release_port = mxs_auart_release_port,
  690. .request_port = mxs_auart_request_port,
  691. .config_port = mxs_auart_config_port,
  692. .verify_port = mxs_auart_verify_port,
  693. };
  694. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  695. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  696. static void mxs_auart_console_putchar(struct uart_port *port, int ch)
  697. {
  698. unsigned int to = 1000;
  699. while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
  700. if (!to--)
  701. break;
  702. udelay(1);
  703. }
  704. writel(ch, port->membase + AUART_DATA);
  705. }
  706. static void
  707. auart_console_write(struct console *co, const char *str, unsigned int count)
  708. {
  709. struct mxs_auart_port *s;
  710. struct uart_port *port;
  711. unsigned int old_ctrl0, old_ctrl2;
  712. unsigned int to = 20000;
  713. if (co->index >= MXS_AUART_PORTS || co->index < 0)
  714. return;
  715. s = auart_port[co->index];
  716. port = &s->port;
  717. clk_enable(s->clk);
  718. /* First save the CR then disable the interrupts */
  719. old_ctrl2 = readl(port->membase + AUART_CTRL2);
  720. old_ctrl0 = readl(port->membase + AUART_CTRL0);
  721. writel(AUART_CTRL0_CLKGATE,
  722. port->membase + AUART_CTRL0_CLR);
  723. writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
  724. port->membase + AUART_CTRL2_SET);
  725. uart_console_write(port, str, count, mxs_auart_console_putchar);
  726. /* Finally, wait for transmitter to become empty ... */
  727. while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
  728. udelay(1);
  729. if (!to--)
  730. break;
  731. }
  732. /*
  733. * ... and restore the TCR if we waited long enough for the transmitter
  734. * to be idle. This might keep the transmitter enabled although it is
  735. * unused, but that is better than to disable it while it is still
  736. * transmitting.
  737. */
  738. if (!(readl(port->membase + AUART_STAT) & AUART_STAT_BUSY)) {
  739. writel(old_ctrl0, port->membase + AUART_CTRL0);
  740. writel(old_ctrl2, port->membase + AUART_CTRL2);
  741. }
  742. clk_disable(s->clk);
  743. }
  744. static void __init
  745. auart_console_get_options(struct uart_port *port, int *baud,
  746. int *parity, int *bits)
  747. {
  748. unsigned int lcr_h, quot;
  749. if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
  750. return;
  751. lcr_h = readl(port->membase + AUART_LINECTRL);
  752. *parity = 'n';
  753. if (lcr_h & AUART_LINECTRL_PEN) {
  754. if (lcr_h & AUART_LINECTRL_EPS)
  755. *parity = 'e';
  756. else
  757. *parity = 'o';
  758. }
  759. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
  760. *bits = 7;
  761. else
  762. *bits = 8;
  763. quot = ((readl(port->membase + AUART_LINECTRL)
  764. & AUART_LINECTRL_BAUD_DIVINT_MASK))
  765. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  766. quot |= ((readl(port->membase + AUART_LINECTRL)
  767. & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  768. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  769. if (quot == 0)
  770. quot = 1;
  771. *baud = (port->uartclk << 2) / quot;
  772. }
  773. static int __init
  774. auart_console_setup(struct console *co, char *options)
  775. {
  776. struct mxs_auart_port *s;
  777. int baud = 9600;
  778. int bits = 8;
  779. int parity = 'n';
  780. int flow = 'n';
  781. int ret;
  782. /*
  783. * Check whether an invalid uart number has been specified, and
  784. * if so, search for the first available port that does have
  785. * console support.
  786. */
  787. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  788. co->index = 0;
  789. s = auart_port[co->index];
  790. if (!s)
  791. return -ENODEV;
  792. clk_prepare_enable(s->clk);
  793. if (options)
  794. uart_parse_options(options, &baud, &parity, &bits, &flow);
  795. else
  796. auart_console_get_options(&s->port, &baud, &parity, &bits);
  797. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  798. clk_disable_unprepare(s->clk);
  799. return ret;
  800. }
  801. static struct console auart_console = {
  802. .name = "ttyAPP",
  803. .write = auart_console_write,
  804. .device = uart_console_device,
  805. .setup = auart_console_setup,
  806. .flags = CON_PRINTBUFFER,
  807. .index = -1,
  808. .data = &auart_driver,
  809. };
  810. #endif
  811. static struct uart_driver auart_driver = {
  812. .owner = THIS_MODULE,
  813. .driver_name = "ttyAPP",
  814. .dev_name = "ttyAPP",
  815. .major = 0,
  816. .minor = 0,
  817. .nr = MXS_AUART_PORTS,
  818. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  819. .cons = &auart_console,
  820. #endif
  821. };
  822. /*
  823. * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
  824. * could successfully get all information from dt or a negative errno.
  825. */
  826. static int serial_mxs_probe_dt(struct mxs_auart_port *s,
  827. struct platform_device *pdev)
  828. {
  829. struct device_node *np = pdev->dev.of_node;
  830. int ret;
  831. if (!np)
  832. /* no device tree device */
  833. return 1;
  834. ret = of_alias_get_id(np, "serial");
  835. if (ret < 0) {
  836. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  837. return ret;
  838. }
  839. s->port.line = ret;
  840. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  841. set_bit(MXS_AUART_RTSCTS, &s->flags);
  842. return 0;
  843. }
  844. static int mxs_auart_probe(struct platform_device *pdev)
  845. {
  846. const struct of_device_id *of_id =
  847. of_match_device(mxs_auart_dt_ids, &pdev->dev);
  848. struct mxs_auart_port *s;
  849. u32 version;
  850. int ret = 0;
  851. struct resource *r;
  852. s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
  853. if (!s) {
  854. ret = -ENOMEM;
  855. goto out;
  856. }
  857. ret = serial_mxs_probe_dt(s, pdev);
  858. if (ret > 0)
  859. s->port.line = pdev->id < 0 ? 0 : pdev->id;
  860. else if (ret < 0)
  861. goto out_free;
  862. if (of_id) {
  863. pdev->id_entry = of_id->data;
  864. s->devtype = pdev->id_entry->driver_data;
  865. }
  866. s->clk = clk_get(&pdev->dev, NULL);
  867. if (IS_ERR(s->clk)) {
  868. ret = PTR_ERR(s->clk);
  869. goto out_free;
  870. }
  871. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  872. if (!r) {
  873. ret = -ENXIO;
  874. goto out_free_clk;
  875. }
  876. s->port.mapbase = r->start;
  877. s->port.membase = ioremap(r->start, resource_size(r));
  878. s->port.ops = &mxs_auart_ops;
  879. s->port.iotype = UPIO_MEM;
  880. s->port.fifosize = MXS_AUART_FIFO_SIZE;
  881. s->port.uartclk = clk_get_rate(s->clk);
  882. s->port.type = PORT_IMX;
  883. s->port.dev = s->dev = &pdev->dev;
  884. s->ctrl = 0;
  885. s->irq = platform_get_irq(pdev, 0);
  886. s->port.irq = s->irq;
  887. ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
  888. if (ret)
  889. goto out_free_clk;
  890. platform_set_drvdata(pdev, s);
  891. auart_port[s->port.line] = s;
  892. mxs_auart_reset(&s->port);
  893. ret = uart_add_one_port(&auart_driver, &s->port);
  894. if (ret)
  895. goto out_free_irq;
  896. version = readl(s->port.membase + AUART_VERSION);
  897. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  898. (version >> 24) & 0xff,
  899. (version >> 16) & 0xff, version & 0xffff);
  900. return 0;
  901. out_free_irq:
  902. auart_port[pdev->id] = NULL;
  903. free_irq(s->irq, s);
  904. out_free_clk:
  905. clk_put(s->clk);
  906. out_free:
  907. kfree(s);
  908. out:
  909. return ret;
  910. }
  911. static int mxs_auart_remove(struct platform_device *pdev)
  912. {
  913. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  914. uart_remove_one_port(&auart_driver, &s->port);
  915. auart_port[pdev->id] = NULL;
  916. clk_put(s->clk);
  917. free_irq(s->irq, s);
  918. kfree(s);
  919. return 0;
  920. }
  921. static struct platform_driver mxs_auart_driver = {
  922. .probe = mxs_auart_probe,
  923. .remove = mxs_auart_remove,
  924. .driver = {
  925. .name = "mxs-auart",
  926. .owner = THIS_MODULE,
  927. .of_match_table = mxs_auart_dt_ids,
  928. },
  929. };
  930. static int __init mxs_auart_init(void)
  931. {
  932. int r;
  933. r = uart_register_driver(&auart_driver);
  934. if (r)
  935. goto out;
  936. r = platform_driver_register(&mxs_auart_driver);
  937. if (r)
  938. goto out_err;
  939. return 0;
  940. out_err:
  941. uart_unregister_driver(&auart_driver);
  942. out:
  943. return r;
  944. }
  945. static void __exit mxs_auart_exit(void)
  946. {
  947. platform_driver_unregister(&mxs_auart_driver);
  948. uart_unregister_driver(&auart_driver);
  949. }
  950. module_init(mxs_auart_init);
  951. module_exit(mxs_auart_exit);
  952. MODULE_LICENSE("GPL");
  953. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  954. MODULE_ALIAS("platform:mxs-auart");