mxs-auart.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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, u->membase + AUART_INTR_CLR);
  584. } else {
  585. mxs_auart_dma_exit(s);
  586. dev_err(s->dev, "We can not start up the DMA.\n");
  587. }
  588. }
  589. }
  590. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  591. {
  592. u32 istatus, istat;
  593. struct mxs_auart_port *s = context;
  594. u32 stat = readl(s->port.membase + AUART_STAT);
  595. istatus = istat = readl(s->port.membase + AUART_INTR);
  596. if (istat & AUART_INTR_CTSMIS) {
  597. uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
  598. writel(AUART_INTR_CTSMIS,
  599. s->port.membase + AUART_INTR_CLR);
  600. istat &= ~AUART_INTR_CTSMIS;
  601. }
  602. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  603. mxs_auart_rx_chars(s);
  604. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  605. }
  606. if (istat & AUART_INTR_TXIS) {
  607. mxs_auart_tx_chars(s);
  608. istat &= ~AUART_INTR_TXIS;
  609. }
  610. writel(istatus & (AUART_INTR_RTIS
  611. | AUART_INTR_TXIS
  612. | AUART_INTR_RXIS
  613. | AUART_INTR_CTSMIS),
  614. s->port.membase + AUART_INTR_CLR);
  615. return IRQ_HANDLED;
  616. }
  617. static void mxs_auart_reset(struct uart_port *u)
  618. {
  619. int i;
  620. unsigned int reg;
  621. writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
  622. for (i = 0; i < 10000; i++) {
  623. reg = readl(u->membase + AUART_CTRL0);
  624. if (!(reg & AUART_CTRL0_SFTRST))
  625. break;
  626. udelay(3);
  627. }
  628. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  629. }
  630. static int mxs_auart_startup(struct uart_port *u)
  631. {
  632. struct mxs_auart_port *s = to_auart_port(u);
  633. clk_prepare_enable(s->clk);
  634. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  635. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
  636. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  637. u->membase + AUART_INTR);
  638. /*
  639. * Enable fifo so all four bytes of a DMA word are written to
  640. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  641. */
  642. writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
  643. return 0;
  644. }
  645. static void mxs_auart_shutdown(struct uart_port *u)
  646. {
  647. struct mxs_auart_port *s = to_auart_port(u);
  648. if (auart_dma_enabled(s))
  649. mxs_auart_dma_exit(s);
  650. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
  651. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  652. u->membase + AUART_INTR_CLR);
  653. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
  654. clk_disable_unprepare(s->clk);
  655. }
  656. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  657. {
  658. if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
  659. return TIOCSER_TEMT;
  660. else
  661. return 0;
  662. }
  663. static void mxs_auart_start_tx(struct uart_port *u)
  664. {
  665. struct mxs_auart_port *s = to_auart_port(u);
  666. /* enable transmitter */
  667. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
  668. mxs_auart_tx_chars(s);
  669. }
  670. static void mxs_auart_stop_tx(struct uart_port *u)
  671. {
  672. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
  673. }
  674. static void mxs_auart_stop_rx(struct uart_port *u)
  675. {
  676. writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
  677. }
  678. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  679. {
  680. if (ctl)
  681. writel(AUART_LINECTRL_BRK,
  682. u->membase + AUART_LINECTRL_SET);
  683. else
  684. writel(AUART_LINECTRL_BRK,
  685. u->membase + AUART_LINECTRL_CLR);
  686. }
  687. static void mxs_auart_enable_ms(struct uart_port *port)
  688. {
  689. /* just empty */
  690. }
  691. static struct uart_ops mxs_auart_ops = {
  692. .tx_empty = mxs_auart_tx_empty,
  693. .start_tx = mxs_auart_start_tx,
  694. .stop_tx = mxs_auart_stop_tx,
  695. .stop_rx = mxs_auart_stop_rx,
  696. .enable_ms = mxs_auart_enable_ms,
  697. .break_ctl = mxs_auart_break_ctl,
  698. .set_mctrl = mxs_auart_set_mctrl,
  699. .get_mctrl = mxs_auart_get_mctrl,
  700. .startup = mxs_auart_startup,
  701. .shutdown = mxs_auart_shutdown,
  702. .set_termios = mxs_auart_settermios,
  703. .type = mxs_auart_type,
  704. .release_port = mxs_auart_release_port,
  705. .request_port = mxs_auart_request_port,
  706. .config_port = mxs_auart_config_port,
  707. .verify_port = mxs_auart_verify_port,
  708. };
  709. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  710. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  711. static void mxs_auart_console_putchar(struct uart_port *port, int ch)
  712. {
  713. unsigned int to = 1000;
  714. while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
  715. if (!to--)
  716. break;
  717. udelay(1);
  718. }
  719. writel(ch, port->membase + AUART_DATA);
  720. }
  721. static void
  722. auart_console_write(struct console *co, const char *str, unsigned int count)
  723. {
  724. struct mxs_auart_port *s;
  725. struct uart_port *port;
  726. unsigned int old_ctrl0, old_ctrl2;
  727. unsigned int to = 1000;
  728. if (co->index > MXS_AUART_PORTS || co->index < 0)
  729. return;
  730. s = auart_port[co->index];
  731. port = &s->port;
  732. clk_enable(s->clk);
  733. /* First save the CR then disable the interrupts */
  734. old_ctrl2 = readl(port->membase + AUART_CTRL2);
  735. old_ctrl0 = readl(port->membase + AUART_CTRL0);
  736. writel(AUART_CTRL0_CLKGATE,
  737. port->membase + AUART_CTRL0_CLR);
  738. writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
  739. port->membase + AUART_CTRL2_SET);
  740. uart_console_write(port, str, count, mxs_auart_console_putchar);
  741. /*
  742. * Finally, wait for transmitter to become empty
  743. * and restore the TCR
  744. */
  745. while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
  746. if (!to--)
  747. break;
  748. udelay(1);
  749. }
  750. writel(old_ctrl0, port->membase + AUART_CTRL0);
  751. writel(old_ctrl2, port->membase + AUART_CTRL2);
  752. clk_disable(s->clk);
  753. }
  754. static void __init
  755. auart_console_get_options(struct uart_port *port, int *baud,
  756. int *parity, int *bits)
  757. {
  758. unsigned int lcr_h, quot;
  759. if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
  760. return;
  761. lcr_h = readl(port->membase + AUART_LINECTRL);
  762. *parity = 'n';
  763. if (lcr_h & AUART_LINECTRL_PEN) {
  764. if (lcr_h & AUART_LINECTRL_EPS)
  765. *parity = 'e';
  766. else
  767. *parity = 'o';
  768. }
  769. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
  770. *bits = 7;
  771. else
  772. *bits = 8;
  773. quot = ((readl(port->membase + AUART_LINECTRL)
  774. & AUART_LINECTRL_BAUD_DIVINT_MASK))
  775. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  776. quot |= ((readl(port->membase + AUART_LINECTRL)
  777. & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  778. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  779. if (quot == 0)
  780. quot = 1;
  781. *baud = (port->uartclk << 2) / quot;
  782. }
  783. static int __init
  784. auart_console_setup(struct console *co, char *options)
  785. {
  786. struct mxs_auart_port *s;
  787. int baud = 9600;
  788. int bits = 8;
  789. int parity = 'n';
  790. int flow = 'n';
  791. int ret;
  792. /*
  793. * Check whether an invalid uart number has been specified, and
  794. * if so, search for the first available port that does have
  795. * console support.
  796. */
  797. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  798. co->index = 0;
  799. s = auart_port[co->index];
  800. if (!s)
  801. return -ENODEV;
  802. clk_prepare_enable(s->clk);
  803. if (options)
  804. uart_parse_options(options, &baud, &parity, &bits, &flow);
  805. else
  806. auart_console_get_options(&s->port, &baud, &parity, &bits);
  807. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  808. clk_disable_unprepare(s->clk);
  809. return ret;
  810. }
  811. static struct console auart_console = {
  812. .name = "ttyAPP",
  813. .write = auart_console_write,
  814. .device = uart_console_device,
  815. .setup = auart_console_setup,
  816. .flags = CON_PRINTBUFFER,
  817. .index = -1,
  818. .data = &auart_driver,
  819. };
  820. #endif
  821. static struct uart_driver auart_driver = {
  822. .owner = THIS_MODULE,
  823. .driver_name = "ttyAPP",
  824. .dev_name = "ttyAPP",
  825. .major = 0,
  826. .minor = 0,
  827. .nr = MXS_AUART_PORTS,
  828. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  829. .cons = &auart_console,
  830. #endif
  831. };
  832. /*
  833. * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
  834. * could successfully get all information from dt or a negative errno.
  835. */
  836. static int serial_mxs_probe_dt(struct mxs_auart_port *s,
  837. struct platform_device *pdev)
  838. {
  839. struct device_node *np = pdev->dev.of_node;
  840. u32 dma_channel[2];
  841. int ret;
  842. if (!np)
  843. /* no device tree device */
  844. return 1;
  845. ret = of_alias_get_id(np, "serial");
  846. if (ret < 0) {
  847. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  848. return ret;
  849. }
  850. s->port.line = ret;
  851. s->dma_irq_rx = platform_get_irq(pdev, 1);
  852. s->dma_irq_tx = platform_get_irq(pdev, 2);
  853. ret = of_property_read_u32_array(np, "fsl,auart-dma-channel",
  854. dma_channel, 2);
  855. if (ret == 0) {
  856. s->dma_channel_rx = dma_channel[0];
  857. s->dma_channel_tx = dma_channel[1];
  858. s->flags |= MXS_AUART_DMA_CONFIG;
  859. } else {
  860. s->dma_channel_rx = -1;
  861. s->dma_channel_tx = -1;
  862. }
  863. return 0;
  864. }
  865. static int __devinit mxs_auart_probe(struct platform_device *pdev)
  866. {
  867. const struct of_device_id *of_id =
  868. of_match_device(mxs_auart_dt_ids, &pdev->dev);
  869. struct mxs_auart_port *s;
  870. u32 version;
  871. int ret = 0;
  872. struct resource *r;
  873. struct pinctrl *pinctrl;
  874. s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
  875. if (!s) {
  876. ret = -ENOMEM;
  877. goto out;
  878. }
  879. ret = serial_mxs_probe_dt(s, pdev);
  880. if (ret > 0)
  881. s->port.line = pdev->id < 0 ? 0 : pdev->id;
  882. else if (ret < 0)
  883. goto out_free;
  884. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  885. if (IS_ERR(pinctrl)) {
  886. ret = PTR_ERR(pinctrl);
  887. goto out_free;
  888. }
  889. if (of_id) {
  890. pdev->id_entry = of_id->data;
  891. s->devtype = pdev->id_entry->driver_data;
  892. }
  893. s->clk = clk_get(&pdev->dev, NULL);
  894. if (IS_ERR(s->clk)) {
  895. ret = PTR_ERR(s->clk);
  896. goto out_free;
  897. }
  898. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  899. if (!r) {
  900. ret = -ENXIO;
  901. goto out_free_clk;
  902. }
  903. s->port.mapbase = r->start;
  904. s->port.membase = ioremap(r->start, resource_size(r));
  905. s->port.ops = &mxs_auart_ops;
  906. s->port.iotype = UPIO_MEM;
  907. s->port.fifosize = 16;
  908. s->port.uartclk = clk_get_rate(s->clk);
  909. s->port.type = PORT_IMX;
  910. s->port.dev = s->dev = get_device(&pdev->dev);
  911. s->ctrl = 0;
  912. s->irq = platform_get_irq(pdev, 0);
  913. s->port.irq = s->irq;
  914. ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
  915. if (ret)
  916. goto out_free_clk;
  917. platform_set_drvdata(pdev, s);
  918. auart_port[s->port.line] = s;
  919. mxs_auart_reset(&s->port);
  920. ret = uart_add_one_port(&auart_driver, &s->port);
  921. if (ret)
  922. goto out_free_irq;
  923. version = readl(s->port.membase + AUART_VERSION);
  924. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  925. (version >> 24) & 0xff,
  926. (version >> 16) & 0xff, version & 0xffff);
  927. return 0;
  928. out_free_irq:
  929. auart_port[pdev->id] = NULL;
  930. free_irq(s->irq, s);
  931. out_free_clk:
  932. put_device(s->dev);
  933. clk_put(s->clk);
  934. out_free:
  935. kfree(s);
  936. out:
  937. return ret;
  938. }
  939. static int __devexit mxs_auart_remove(struct platform_device *pdev)
  940. {
  941. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  942. uart_remove_one_port(&auart_driver, &s->port);
  943. auart_port[pdev->id] = NULL;
  944. put_device(s->dev);
  945. clk_put(s->clk);
  946. free_irq(s->irq, s);
  947. kfree(s);
  948. return 0;
  949. }
  950. static struct platform_driver mxs_auart_driver = {
  951. .probe = mxs_auart_probe,
  952. .remove = __devexit_p(mxs_auart_remove),
  953. .driver = {
  954. .name = "mxs-auart",
  955. .owner = THIS_MODULE,
  956. .of_match_table = mxs_auart_dt_ids,
  957. },
  958. };
  959. static int __init mxs_auart_init(void)
  960. {
  961. int r;
  962. r = uart_register_driver(&auart_driver);
  963. if (r)
  964. goto out;
  965. r = platform_driver_register(&mxs_auart_driver);
  966. if (r)
  967. goto out_err;
  968. return 0;
  969. out_err:
  970. uart_unregister_driver(&auart_driver);
  971. out:
  972. return r;
  973. }
  974. static void __exit mxs_auart_exit(void)
  975. {
  976. platform_driver_unregister(&mxs_auart_driver);
  977. uart_unregister_driver(&auart_driver);
  978. }
  979. module_init(mxs_auart_init);
  980. module_exit(mxs_auart_exit);
  981. MODULE_LICENSE("GPL");
  982. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  983. MODULE_ALIAS("platform:mxs-auart");