mxs-auart.c 28 KB

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