mxs-auart.c 28 KB

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