mxs-auart.c 28 KB

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