mxs-auart.c 27 KB

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