mxs-auart.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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. #if defined(CONFIG_SERIAL_MXS_AUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  17. #define SUPPORT_SYSRQ
  18. #endif
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/init.h>
  22. #include <linux/console.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/wait.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial.h>
  31. #include <linux/serial_core.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/device.h>
  34. #include <linux/clk.h>
  35. #include <linux/delay.h>
  36. #include <linux/io.h>
  37. #include <linux/of_device.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/dmaengine.h>
  40. #include <asm/cacheflush.h>
  41. #include <linux/gpio.h>
  42. #include <linux/gpio/consumer.h>
  43. #include <linux/err.h>
  44. #include <linux/irq.h>
  45. #include "serial_mctrl_gpio.h"
  46. #define MXS_AUART_PORTS 5
  47. #define MXS_AUART_FIFO_SIZE 16
  48. #define AUART_CTRL0 0x00000000
  49. #define AUART_CTRL0_SET 0x00000004
  50. #define AUART_CTRL0_CLR 0x00000008
  51. #define AUART_CTRL0_TOG 0x0000000c
  52. #define AUART_CTRL1 0x00000010
  53. #define AUART_CTRL1_SET 0x00000014
  54. #define AUART_CTRL1_CLR 0x00000018
  55. #define AUART_CTRL1_TOG 0x0000001c
  56. #define AUART_CTRL2 0x00000020
  57. #define AUART_CTRL2_SET 0x00000024
  58. #define AUART_CTRL2_CLR 0x00000028
  59. #define AUART_CTRL2_TOG 0x0000002c
  60. #define AUART_LINECTRL 0x00000030
  61. #define AUART_LINECTRL_SET 0x00000034
  62. #define AUART_LINECTRL_CLR 0x00000038
  63. #define AUART_LINECTRL_TOG 0x0000003c
  64. #define AUART_LINECTRL2 0x00000040
  65. #define AUART_LINECTRL2_SET 0x00000044
  66. #define AUART_LINECTRL2_CLR 0x00000048
  67. #define AUART_LINECTRL2_TOG 0x0000004c
  68. #define AUART_INTR 0x00000050
  69. #define AUART_INTR_SET 0x00000054
  70. #define AUART_INTR_CLR 0x00000058
  71. #define AUART_INTR_TOG 0x0000005c
  72. #define AUART_DATA 0x00000060
  73. #define AUART_STAT 0x00000070
  74. #define AUART_DEBUG 0x00000080
  75. #define AUART_VERSION 0x00000090
  76. #define AUART_AUTOBAUD 0x000000a0
  77. #define AUART_CTRL0_SFTRST (1 << 31)
  78. #define AUART_CTRL0_CLKGATE (1 << 30)
  79. #define AUART_CTRL0_RXTO_ENABLE (1 << 27)
  80. #define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
  81. #define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
  82. #define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
  83. #define AUART_CTRL2_DMAONERR (1 << 26)
  84. #define AUART_CTRL2_TXDMAE (1 << 25)
  85. #define AUART_CTRL2_RXDMAE (1 << 24)
  86. #define AUART_CTRL2_CTSEN (1 << 15)
  87. #define AUART_CTRL2_RTSEN (1 << 14)
  88. #define AUART_CTRL2_RTS (1 << 11)
  89. #define AUART_CTRL2_RXE (1 << 9)
  90. #define AUART_CTRL2_TXE (1 << 8)
  91. #define AUART_CTRL2_UARTEN (1 << 0)
  92. #define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
  93. #define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
  94. #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
  95. #define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
  96. #define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
  97. #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
  98. #define AUART_LINECTRL_WLEN_MASK 0x00000060
  99. #define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
  100. #define AUART_LINECTRL_FEN (1 << 4)
  101. #define AUART_LINECTRL_STP2 (1 << 3)
  102. #define AUART_LINECTRL_EPS (1 << 2)
  103. #define AUART_LINECTRL_PEN (1 << 1)
  104. #define AUART_LINECTRL_BRK (1 << 0)
  105. #define AUART_INTR_RTIEN (1 << 22)
  106. #define AUART_INTR_TXIEN (1 << 21)
  107. #define AUART_INTR_RXIEN (1 << 20)
  108. #define AUART_INTR_CTSMIEN (1 << 17)
  109. #define AUART_INTR_RTIS (1 << 6)
  110. #define AUART_INTR_TXIS (1 << 5)
  111. #define AUART_INTR_RXIS (1 << 4)
  112. #define AUART_INTR_CTSMIS (1 << 1)
  113. #define AUART_STAT_BUSY (1 << 29)
  114. #define AUART_STAT_CTS (1 << 28)
  115. #define AUART_STAT_TXFE (1 << 27)
  116. #define AUART_STAT_TXFF (1 << 25)
  117. #define AUART_STAT_RXFE (1 << 24)
  118. #define AUART_STAT_OERR (1 << 19)
  119. #define AUART_STAT_BERR (1 << 18)
  120. #define AUART_STAT_PERR (1 << 17)
  121. #define AUART_STAT_FERR (1 << 16)
  122. #define AUART_STAT_RXCOUNT_MASK 0xffff
  123. static struct uart_driver auart_driver;
  124. enum mxs_auart_type {
  125. IMX23_AUART,
  126. IMX28_AUART,
  127. };
  128. struct mxs_auart_port {
  129. struct uart_port port;
  130. #define MXS_AUART_DMA_ENABLED 0x2
  131. #define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
  132. #define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
  133. #define MXS_AUART_RTSCTS 4 /* bit 4 */
  134. unsigned long flags;
  135. unsigned int mctrl_prev;
  136. enum mxs_auart_type devtype;
  137. struct clk *clk;
  138. struct device *dev;
  139. /* for DMA */
  140. struct scatterlist tx_sgl;
  141. struct dma_chan *tx_dma_chan;
  142. void *tx_dma_buf;
  143. struct scatterlist rx_sgl;
  144. struct dma_chan *rx_dma_chan;
  145. void *rx_dma_buf;
  146. struct mctrl_gpios *gpios;
  147. int gpio_irq[UART_GPIO_MAX];
  148. bool ms_irq_enabled;
  149. };
  150. static struct platform_device_id mxs_auart_devtype[] = {
  151. { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
  152. { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
  153. { /* sentinel */ }
  154. };
  155. MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
  156. static const struct of_device_id mxs_auart_dt_ids[] = {
  157. {
  158. .compatible = "fsl,imx28-auart",
  159. .data = &mxs_auart_devtype[IMX28_AUART]
  160. }, {
  161. .compatible = "fsl,imx23-auart",
  162. .data = &mxs_auart_devtype[IMX23_AUART]
  163. }, { /* sentinel */ }
  164. };
  165. MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
  166. static inline int is_imx28_auart(struct mxs_auart_port *s)
  167. {
  168. return s->devtype == IMX28_AUART;
  169. }
  170. static inline bool auart_dma_enabled(struct mxs_auart_port *s)
  171. {
  172. return s->flags & MXS_AUART_DMA_ENABLED;
  173. }
  174. static void mxs_auart_stop_tx(struct uart_port *u);
  175. #define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
  176. static void mxs_auart_tx_chars(struct mxs_auart_port *s);
  177. static void dma_tx_callback(void *param)
  178. {
  179. struct mxs_auart_port *s = param;
  180. struct circ_buf *xmit = &s->port.state->xmit;
  181. dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
  182. /* clear the bit used to serialize the DMA tx. */
  183. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  184. smp_mb__after_atomic();
  185. /* wake up the possible processes. */
  186. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  187. uart_write_wakeup(&s->port);
  188. mxs_auart_tx_chars(s);
  189. }
  190. static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
  191. {
  192. struct dma_async_tx_descriptor *desc;
  193. struct scatterlist *sgl = &s->tx_sgl;
  194. struct dma_chan *channel = s->tx_dma_chan;
  195. u32 pio;
  196. /* [1] : send PIO. Note, the first pio word is CTRL1. */
  197. pio = AUART_CTRL1_XFER_COUNT(size);
  198. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
  199. 1, DMA_TRANS_NONE, 0);
  200. if (!desc) {
  201. dev_err(s->dev, "step 1 error\n");
  202. return -EINVAL;
  203. }
  204. /* [2] : set DMA buffer. */
  205. sg_init_one(sgl, s->tx_dma_buf, size);
  206. dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
  207. desc = dmaengine_prep_slave_sg(channel, sgl,
  208. 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  209. if (!desc) {
  210. dev_err(s->dev, "step 2 error\n");
  211. return -EINVAL;
  212. }
  213. /* [3] : submit the DMA */
  214. desc->callback = dma_tx_callback;
  215. desc->callback_param = s;
  216. dmaengine_submit(desc);
  217. dma_async_issue_pending(channel);
  218. return 0;
  219. }
  220. static void mxs_auart_tx_chars(struct mxs_auart_port *s)
  221. {
  222. struct circ_buf *xmit = &s->port.state->xmit;
  223. if (auart_dma_enabled(s)) {
  224. u32 i = 0;
  225. int size;
  226. void *buffer = s->tx_dma_buf;
  227. if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
  228. return;
  229. while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  230. size = min_t(u32, UART_XMIT_SIZE - i,
  231. CIRC_CNT_TO_END(xmit->head,
  232. xmit->tail,
  233. UART_XMIT_SIZE));
  234. memcpy(buffer + i, xmit->buf + xmit->tail, size);
  235. xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
  236. i += size;
  237. if (i >= UART_XMIT_SIZE)
  238. break;
  239. }
  240. if (uart_tx_stopped(&s->port))
  241. mxs_auart_stop_tx(&s->port);
  242. if (i) {
  243. mxs_auart_dma_tx(s, i);
  244. } else {
  245. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  246. smp_mb__after_atomic();
  247. }
  248. return;
  249. }
  250. while (!(readl(s->port.membase + AUART_STAT) &
  251. AUART_STAT_TXFF)) {
  252. if (s->port.x_char) {
  253. s->port.icount.tx++;
  254. writel(s->port.x_char,
  255. s->port.membase + AUART_DATA);
  256. s->port.x_char = 0;
  257. continue;
  258. }
  259. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  260. s->port.icount.tx++;
  261. writel(xmit->buf[xmit->tail],
  262. s->port.membase + AUART_DATA);
  263. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  264. } else
  265. break;
  266. }
  267. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  268. uart_write_wakeup(&s->port);
  269. if (uart_circ_empty(&(s->port.state->xmit)))
  270. writel(AUART_INTR_TXIEN,
  271. s->port.membase + AUART_INTR_CLR);
  272. else
  273. writel(AUART_INTR_TXIEN,
  274. s->port.membase + AUART_INTR_SET);
  275. if (uart_tx_stopped(&s->port))
  276. mxs_auart_stop_tx(&s->port);
  277. }
  278. static void mxs_auart_rx_char(struct mxs_auart_port *s)
  279. {
  280. int flag;
  281. u32 stat;
  282. u8 c;
  283. c = readl(s->port.membase + AUART_DATA);
  284. stat = readl(s->port.membase + AUART_STAT);
  285. flag = TTY_NORMAL;
  286. s->port.icount.rx++;
  287. if (stat & AUART_STAT_BERR) {
  288. s->port.icount.brk++;
  289. if (uart_handle_break(&s->port))
  290. goto out;
  291. } else if (stat & AUART_STAT_PERR) {
  292. s->port.icount.parity++;
  293. } else if (stat & AUART_STAT_FERR) {
  294. s->port.icount.frame++;
  295. }
  296. /*
  297. * Mask off conditions which should be ingored.
  298. */
  299. stat &= s->port.read_status_mask;
  300. if (stat & AUART_STAT_BERR) {
  301. flag = TTY_BREAK;
  302. } else if (stat & AUART_STAT_PERR)
  303. flag = TTY_PARITY;
  304. else if (stat & AUART_STAT_FERR)
  305. flag = TTY_FRAME;
  306. if (stat & AUART_STAT_OERR)
  307. s->port.icount.overrun++;
  308. if (uart_handle_sysrq_char(&s->port, c))
  309. goto out;
  310. uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
  311. out:
  312. writel(stat, s->port.membase + AUART_STAT);
  313. }
  314. static void mxs_auart_rx_chars(struct mxs_auart_port *s)
  315. {
  316. u32 stat = 0;
  317. for (;;) {
  318. stat = readl(s->port.membase + AUART_STAT);
  319. if (stat & AUART_STAT_RXFE)
  320. break;
  321. mxs_auart_rx_char(s);
  322. }
  323. writel(stat, s->port.membase + AUART_STAT);
  324. tty_flip_buffer_push(&s->port.state->port);
  325. }
  326. static int mxs_auart_request_port(struct uart_port *u)
  327. {
  328. return 0;
  329. }
  330. static int mxs_auart_verify_port(struct uart_port *u,
  331. struct serial_struct *ser)
  332. {
  333. if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
  334. return -EINVAL;
  335. return 0;
  336. }
  337. static void mxs_auart_config_port(struct uart_port *u, int flags)
  338. {
  339. }
  340. static const char *mxs_auart_type(struct uart_port *u)
  341. {
  342. struct mxs_auart_port *s = to_auart_port(u);
  343. return dev_name(s->dev);
  344. }
  345. static void mxs_auart_release_port(struct uart_port *u)
  346. {
  347. }
  348. static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
  349. {
  350. struct mxs_auart_port *s = to_auart_port(u);
  351. u32 ctrl = readl(u->membase + AUART_CTRL2);
  352. ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
  353. if (mctrl & TIOCM_RTS) {
  354. if (uart_cts_enabled(u))
  355. ctrl |= AUART_CTRL2_RTSEN;
  356. else
  357. ctrl |= AUART_CTRL2_RTS;
  358. }
  359. writel(ctrl, u->membase + AUART_CTRL2);
  360. mctrl_gpio_set(s->gpios, mctrl);
  361. }
  362. #define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
  363. static u32 mxs_auart_modem_status(struct mxs_auart_port *s, u32 mctrl)
  364. {
  365. u32 mctrl_diff;
  366. mctrl_diff = mctrl ^ s->mctrl_prev;
  367. s->mctrl_prev = mctrl;
  368. if (mctrl_diff & MCTRL_ANY_DELTA && s->ms_irq_enabled &&
  369. s->port.state != NULL) {
  370. if (mctrl_diff & TIOCM_RI)
  371. s->port.icount.rng++;
  372. if (mctrl_diff & TIOCM_DSR)
  373. s->port.icount.dsr++;
  374. if (mctrl_diff & TIOCM_CD)
  375. uart_handle_dcd_change(&s->port, mctrl & TIOCM_CD);
  376. if (mctrl_diff & TIOCM_CTS)
  377. uart_handle_cts_change(&s->port, mctrl & TIOCM_CTS);
  378. wake_up_interruptible(&s->port.state->port.delta_msr_wait);
  379. }
  380. return mctrl;
  381. }
  382. static u32 mxs_auart_get_mctrl(struct uart_port *u)
  383. {
  384. struct mxs_auart_port *s = to_auart_port(u);
  385. u32 stat = readl(u->membase + AUART_STAT);
  386. u32 mctrl = 0;
  387. if (stat & AUART_STAT_CTS)
  388. mctrl |= TIOCM_CTS;
  389. return mctrl_gpio_get(s->gpios, &mctrl);
  390. }
  391. /*
  392. * Enable modem status interrupts
  393. */
  394. static void mxs_auart_enable_ms(struct uart_port *port)
  395. {
  396. struct mxs_auart_port *s = to_auart_port(port);
  397. /*
  398. * Interrupt should not be enabled twice
  399. */
  400. if (s->ms_irq_enabled)
  401. return;
  402. s->ms_irq_enabled = true;
  403. if (s->gpio_irq[UART_GPIO_CTS] >= 0)
  404. enable_irq(s->gpio_irq[UART_GPIO_CTS]);
  405. /* TODO: enable AUART_INTR_CTSMIEN otherwise */
  406. if (s->gpio_irq[UART_GPIO_DSR] >= 0)
  407. enable_irq(s->gpio_irq[UART_GPIO_DSR]);
  408. if (s->gpio_irq[UART_GPIO_RI] >= 0)
  409. enable_irq(s->gpio_irq[UART_GPIO_RI]);
  410. if (s->gpio_irq[UART_GPIO_DCD] >= 0)
  411. enable_irq(s->gpio_irq[UART_GPIO_DCD]);
  412. }
  413. /*
  414. * Disable modem status interrupts
  415. */
  416. static void mxs_auart_disable_ms(struct uart_port *port)
  417. {
  418. struct mxs_auart_port *s = to_auart_port(port);
  419. /*
  420. * Interrupt should not be disabled twice
  421. */
  422. if (!s->ms_irq_enabled)
  423. return;
  424. s->ms_irq_enabled = false;
  425. if (s->gpio_irq[UART_GPIO_CTS] >= 0)
  426. disable_irq(s->gpio_irq[UART_GPIO_CTS]);
  427. /* TODO: disable AUART_INTR_CTSMIEN otherwise */
  428. if (s->gpio_irq[UART_GPIO_DSR] >= 0)
  429. disable_irq(s->gpio_irq[UART_GPIO_DSR]);
  430. if (s->gpio_irq[UART_GPIO_RI] >= 0)
  431. disable_irq(s->gpio_irq[UART_GPIO_RI]);
  432. if (s->gpio_irq[UART_GPIO_DCD] >= 0)
  433. disable_irq(s->gpio_irq[UART_GPIO_DCD]);
  434. }
  435. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
  436. static void dma_rx_callback(void *arg)
  437. {
  438. struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
  439. struct tty_port *port = &s->port.state->port;
  440. int count;
  441. u32 stat;
  442. dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
  443. stat = readl(s->port.membase + AUART_STAT);
  444. stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
  445. AUART_STAT_PERR | AUART_STAT_FERR);
  446. count = stat & AUART_STAT_RXCOUNT_MASK;
  447. tty_insert_flip_string(port, s->rx_dma_buf, count);
  448. writel(stat, s->port.membase + AUART_STAT);
  449. tty_flip_buffer_push(port);
  450. /* start the next DMA for RX. */
  451. mxs_auart_dma_prep_rx(s);
  452. }
  453. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
  454. {
  455. struct dma_async_tx_descriptor *desc;
  456. struct scatterlist *sgl = &s->rx_sgl;
  457. struct dma_chan *channel = s->rx_dma_chan;
  458. u32 pio[1];
  459. /* [1] : send PIO */
  460. pio[0] = AUART_CTRL0_RXTO_ENABLE
  461. | AUART_CTRL0_RXTIMEOUT(0x80)
  462. | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
  463. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  464. 1, DMA_TRANS_NONE, 0);
  465. if (!desc) {
  466. dev_err(s->dev, "step 1 error\n");
  467. return -EINVAL;
  468. }
  469. /* [2] : send DMA request */
  470. sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
  471. dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
  472. desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
  473. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  474. if (!desc) {
  475. dev_err(s->dev, "step 2 error\n");
  476. return -1;
  477. }
  478. /* [3] : submit the DMA, but do not issue it. */
  479. desc->callback = dma_rx_callback;
  480. desc->callback_param = s;
  481. dmaengine_submit(desc);
  482. dma_async_issue_pending(channel);
  483. return 0;
  484. }
  485. static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
  486. {
  487. if (s->tx_dma_chan) {
  488. dma_release_channel(s->tx_dma_chan);
  489. s->tx_dma_chan = NULL;
  490. }
  491. if (s->rx_dma_chan) {
  492. dma_release_channel(s->rx_dma_chan);
  493. s->rx_dma_chan = NULL;
  494. }
  495. kfree(s->tx_dma_buf);
  496. kfree(s->rx_dma_buf);
  497. s->tx_dma_buf = NULL;
  498. s->rx_dma_buf = NULL;
  499. }
  500. static void mxs_auart_dma_exit(struct mxs_auart_port *s)
  501. {
  502. writel(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
  503. s->port.membase + AUART_CTRL2_CLR);
  504. mxs_auart_dma_exit_channel(s);
  505. s->flags &= ~MXS_AUART_DMA_ENABLED;
  506. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  507. clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
  508. }
  509. static int mxs_auart_dma_init(struct mxs_auart_port *s)
  510. {
  511. if (auart_dma_enabled(s))
  512. return 0;
  513. /* init for RX */
  514. s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
  515. if (!s->rx_dma_chan)
  516. goto err_out;
  517. s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  518. if (!s->rx_dma_buf)
  519. goto err_out;
  520. /* init for TX */
  521. s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
  522. if (!s->tx_dma_chan)
  523. goto err_out;
  524. s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  525. if (!s->tx_dma_buf)
  526. goto err_out;
  527. /* set the flags */
  528. s->flags |= MXS_AUART_DMA_ENABLED;
  529. dev_dbg(s->dev, "enabled the DMA support.");
  530. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  531. s->port.fifosize = UART_XMIT_SIZE;
  532. return 0;
  533. err_out:
  534. mxs_auart_dma_exit_channel(s);
  535. return -EINVAL;
  536. }
  537. #define RTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
  538. UART_GPIO_RTS))
  539. #define CTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
  540. UART_GPIO_CTS))
  541. static void mxs_auart_settermios(struct uart_port *u,
  542. struct ktermios *termios,
  543. struct ktermios *old)
  544. {
  545. struct mxs_auart_port *s = to_auart_port(u);
  546. u32 bm, ctrl, ctrl2, div;
  547. unsigned int cflag, baud;
  548. cflag = termios->c_cflag;
  549. ctrl = AUART_LINECTRL_FEN;
  550. ctrl2 = readl(u->membase + AUART_CTRL2);
  551. /* byte size */
  552. switch (cflag & CSIZE) {
  553. case CS5:
  554. bm = 0;
  555. break;
  556. case CS6:
  557. bm = 1;
  558. break;
  559. case CS7:
  560. bm = 2;
  561. break;
  562. case CS8:
  563. bm = 3;
  564. break;
  565. default:
  566. return;
  567. }
  568. ctrl |= AUART_LINECTRL_WLEN(bm);
  569. /* parity */
  570. if (cflag & PARENB) {
  571. ctrl |= AUART_LINECTRL_PEN;
  572. if ((cflag & PARODD) == 0)
  573. ctrl |= AUART_LINECTRL_EPS;
  574. }
  575. u->read_status_mask = 0;
  576. if (termios->c_iflag & INPCK)
  577. u->read_status_mask |= AUART_STAT_PERR;
  578. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  579. u->read_status_mask |= AUART_STAT_BERR;
  580. /*
  581. * Characters to ignore
  582. */
  583. u->ignore_status_mask = 0;
  584. if (termios->c_iflag & IGNPAR)
  585. u->ignore_status_mask |= AUART_STAT_PERR;
  586. if (termios->c_iflag & IGNBRK) {
  587. u->ignore_status_mask |= AUART_STAT_BERR;
  588. /*
  589. * If we're ignoring parity and break indicators,
  590. * ignore overruns too (for real raw support).
  591. */
  592. if (termios->c_iflag & IGNPAR)
  593. u->ignore_status_mask |= AUART_STAT_OERR;
  594. }
  595. /*
  596. * ignore all characters if CREAD is not set
  597. */
  598. if (cflag & CREAD)
  599. ctrl2 |= AUART_CTRL2_RXE;
  600. else
  601. ctrl2 &= ~AUART_CTRL2_RXE;
  602. /* figure out the stop bits requested */
  603. if (cflag & CSTOPB)
  604. ctrl |= AUART_LINECTRL_STP2;
  605. /* figure out the hardware flow control settings */
  606. ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
  607. if (cflag & CRTSCTS) {
  608. /*
  609. * The DMA has a bug(see errata:2836) in mx23.
  610. * So we can not implement the DMA for auart in mx23,
  611. * we can only implement the DMA support for auart
  612. * in mx28.
  613. */
  614. if (is_imx28_auart(s)
  615. && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
  616. if (!mxs_auart_dma_init(s))
  617. /* enable DMA tranfer */
  618. ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
  619. | AUART_CTRL2_DMAONERR;
  620. }
  621. /* Even if RTS is GPIO line RTSEN can be enabled because
  622. * the pinctrl configuration decides about RTS pin function */
  623. ctrl2 |= AUART_CTRL2_RTSEN;
  624. if (CTS_AT_AUART())
  625. ctrl2 |= AUART_CTRL2_CTSEN;
  626. }
  627. /* set baud rate */
  628. baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
  629. div = u->uartclk * 32 / baud;
  630. ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
  631. ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
  632. writel(ctrl, u->membase + AUART_LINECTRL);
  633. writel(ctrl2, u->membase + AUART_CTRL2);
  634. uart_update_timeout(u, termios->c_cflag, baud);
  635. /* prepare for the DMA RX. */
  636. if (auart_dma_enabled(s) &&
  637. !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
  638. if (!mxs_auart_dma_prep_rx(s)) {
  639. /* Disable the normal RX interrupt. */
  640. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
  641. u->membase + AUART_INTR_CLR);
  642. } else {
  643. mxs_auart_dma_exit(s);
  644. dev_err(s->dev, "We can not start up the DMA.\n");
  645. }
  646. }
  647. /* CTS flow-control and modem-status interrupts */
  648. if (UART_ENABLE_MS(u, termios->c_cflag))
  649. mxs_auart_enable_ms(u);
  650. else
  651. mxs_auart_disable_ms(u);
  652. }
  653. static void mxs_auart_set_ldisc(struct uart_port *port,
  654. struct ktermios *termios)
  655. {
  656. if (termios->c_line == N_PPS) {
  657. port->flags |= UPF_HARDPPS_CD;
  658. mxs_auart_enable_ms(port);
  659. } else {
  660. port->flags &= ~UPF_HARDPPS_CD;
  661. }
  662. }
  663. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  664. {
  665. u32 istat;
  666. struct mxs_auart_port *s = context;
  667. u32 mctrl_temp = s->mctrl_prev;
  668. u32 stat = readl(s->port.membase + AUART_STAT);
  669. istat = readl(s->port.membase + AUART_INTR);
  670. /* ack irq */
  671. writel(istat & (AUART_INTR_RTIS
  672. | AUART_INTR_TXIS
  673. | AUART_INTR_RXIS
  674. | AUART_INTR_CTSMIS),
  675. s->port.membase + AUART_INTR_CLR);
  676. /*
  677. * Dealing with GPIO interrupt
  678. */
  679. if (irq == s->gpio_irq[UART_GPIO_CTS] ||
  680. irq == s->gpio_irq[UART_GPIO_DCD] ||
  681. irq == s->gpio_irq[UART_GPIO_DSR] ||
  682. irq == s->gpio_irq[UART_GPIO_RI])
  683. mxs_auart_modem_status(s,
  684. mctrl_gpio_get(s->gpios, &mctrl_temp));
  685. if (istat & AUART_INTR_CTSMIS) {
  686. if (CTS_AT_AUART() && s->ms_irq_enabled)
  687. uart_handle_cts_change(&s->port,
  688. stat & AUART_STAT_CTS);
  689. writel(AUART_INTR_CTSMIS,
  690. s->port.membase + AUART_INTR_CLR);
  691. istat &= ~AUART_INTR_CTSMIS;
  692. }
  693. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  694. if (!auart_dma_enabled(s))
  695. mxs_auart_rx_chars(s);
  696. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  697. }
  698. if (istat & AUART_INTR_TXIS) {
  699. mxs_auart_tx_chars(s);
  700. istat &= ~AUART_INTR_TXIS;
  701. }
  702. return IRQ_HANDLED;
  703. }
  704. static void mxs_auart_reset(struct uart_port *u)
  705. {
  706. int i;
  707. unsigned int reg;
  708. writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
  709. for (i = 0; i < 10000; i++) {
  710. reg = readl(u->membase + AUART_CTRL0);
  711. if (!(reg & AUART_CTRL0_SFTRST))
  712. break;
  713. udelay(3);
  714. }
  715. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  716. }
  717. static int mxs_auart_startup(struct uart_port *u)
  718. {
  719. int ret;
  720. struct mxs_auart_port *s = to_auart_port(u);
  721. ret = clk_prepare_enable(s->clk);
  722. if (ret)
  723. return ret;
  724. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  725. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
  726. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  727. u->membase + AUART_INTR);
  728. /* Reset FIFO size (it could have changed if DMA was enabled) */
  729. u->fifosize = MXS_AUART_FIFO_SIZE;
  730. /*
  731. * Enable fifo so all four bytes of a DMA word are written to
  732. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  733. */
  734. writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
  735. /* get initial status of modem lines */
  736. mctrl_gpio_get(s->gpios, &s->mctrl_prev);
  737. s->ms_irq_enabled = false;
  738. return 0;
  739. }
  740. static void mxs_auart_shutdown(struct uart_port *u)
  741. {
  742. struct mxs_auart_port *s = to_auart_port(u);
  743. mxs_auart_disable_ms(u);
  744. if (auart_dma_enabled(s))
  745. mxs_auart_dma_exit(s);
  746. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
  747. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  748. u->membase + AUART_INTR_CLR);
  749. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
  750. clk_disable_unprepare(s->clk);
  751. }
  752. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  753. {
  754. if ((readl(u->membase + AUART_STAT) &
  755. (AUART_STAT_TXFE | AUART_STAT_BUSY)) == AUART_STAT_TXFE)
  756. return TIOCSER_TEMT;
  757. return 0;
  758. }
  759. static void mxs_auart_start_tx(struct uart_port *u)
  760. {
  761. struct mxs_auart_port *s = to_auart_port(u);
  762. /* enable transmitter */
  763. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
  764. mxs_auart_tx_chars(s);
  765. }
  766. static void mxs_auart_stop_tx(struct uart_port *u)
  767. {
  768. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
  769. }
  770. static void mxs_auart_stop_rx(struct uart_port *u)
  771. {
  772. writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
  773. }
  774. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  775. {
  776. if (ctl)
  777. writel(AUART_LINECTRL_BRK,
  778. u->membase + AUART_LINECTRL_SET);
  779. else
  780. writel(AUART_LINECTRL_BRK,
  781. u->membase + AUART_LINECTRL_CLR);
  782. }
  783. static struct uart_ops mxs_auart_ops = {
  784. .tx_empty = mxs_auart_tx_empty,
  785. .start_tx = mxs_auart_start_tx,
  786. .stop_tx = mxs_auart_stop_tx,
  787. .stop_rx = mxs_auart_stop_rx,
  788. .enable_ms = mxs_auart_enable_ms,
  789. .break_ctl = mxs_auart_break_ctl,
  790. .set_mctrl = mxs_auart_set_mctrl,
  791. .get_mctrl = mxs_auart_get_mctrl,
  792. .startup = mxs_auart_startup,
  793. .shutdown = mxs_auart_shutdown,
  794. .set_termios = mxs_auart_settermios,
  795. .set_ldisc = mxs_auart_set_ldisc,
  796. .type = mxs_auart_type,
  797. .release_port = mxs_auart_release_port,
  798. .request_port = mxs_auart_request_port,
  799. .config_port = mxs_auart_config_port,
  800. .verify_port = mxs_auart_verify_port,
  801. };
  802. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  803. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  804. static void mxs_auart_console_putchar(struct uart_port *port, int ch)
  805. {
  806. unsigned int to = 1000;
  807. while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
  808. if (!to--)
  809. break;
  810. udelay(1);
  811. }
  812. writel(ch, port->membase + AUART_DATA);
  813. }
  814. static void
  815. auart_console_write(struct console *co, const char *str, unsigned int count)
  816. {
  817. struct mxs_auart_port *s;
  818. struct uart_port *port;
  819. unsigned int old_ctrl0, old_ctrl2;
  820. unsigned int to = 20000;
  821. if (co->index >= MXS_AUART_PORTS || co->index < 0)
  822. return;
  823. s = auart_port[co->index];
  824. port = &s->port;
  825. clk_enable(s->clk);
  826. /* First save the CR then disable the interrupts */
  827. old_ctrl2 = readl(port->membase + AUART_CTRL2);
  828. old_ctrl0 = readl(port->membase + AUART_CTRL0);
  829. writel(AUART_CTRL0_CLKGATE,
  830. port->membase + AUART_CTRL0_CLR);
  831. writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
  832. port->membase + AUART_CTRL2_SET);
  833. uart_console_write(port, str, count, mxs_auart_console_putchar);
  834. /* Finally, wait for transmitter to become empty ... */
  835. while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
  836. udelay(1);
  837. if (!to--)
  838. break;
  839. }
  840. /*
  841. * ... and restore the TCR if we waited long enough for the transmitter
  842. * to be idle. This might keep the transmitter enabled although it is
  843. * unused, but that is better than to disable it while it is still
  844. * transmitting.
  845. */
  846. if (!(readl(port->membase + AUART_STAT) & AUART_STAT_BUSY)) {
  847. writel(old_ctrl0, port->membase + AUART_CTRL0);
  848. writel(old_ctrl2, port->membase + AUART_CTRL2);
  849. }
  850. clk_disable(s->clk);
  851. }
  852. static void __init
  853. auart_console_get_options(struct uart_port *port, int *baud,
  854. int *parity, int *bits)
  855. {
  856. unsigned int lcr_h, quot;
  857. if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
  858. return;
  859. lcr_h = readl(port->membase + AUART_LINECTRL);
  860. *parity = 'n';
  861. if (lcr_h & AUART_LINECTRL_PEN) {
  862. if (lcr_h & AUART_LINECTRL_EPS)
  863. *parity = 'e';
  864. else
  865. *parity = 'o';
  866. }
  867. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
  868. *bits = 7;
  869. else
  870. *bits = 8;
  871. quot = ((readl(port->membase + AUART_LINECTRL)
  872. & AUART_LINECTRL_BAUD_DIVINT_MASK))
  873. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  874. quot |= ((readl(port->membase + AUART_LINECTRL)
  875. & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  876. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  877. if (quot == 0)
  878. quot = 1;
  879. *baud = (port->uartclk << 2) / quot;
  880. }
  881. static int __init
  882. auart_console_setup(struct console *co, char *options)
  883. {
  884. struct mxs_auart_port *s;
  885. int baud = 9600;
  886. int bits = 8;
  887. int parity = 'n';
  888. int flow = 'n';
  889. int ret;
  890. /*
  891. * Check whether an invalid uart number has been specified, and
  892. * if so, search for the first available port that does have
  893. * console support.
  894. */
  895. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  896. co->index = 0;
  897. s = auart_port[co->index];
  898. if (!s)
  899. return -ENODEV;
  900. ret = clk_prepare_enable(s->clk);
  901. if (ret)
  902. return ret;
  903. if (options)
  904. uart_parse_options(options, &baud, &parity, &bits, &flow);
  905. else
  906. auart_console_get_options(&s->port, &baud, &parity, &bits);
  907. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  908. clk_disable_unprepare(s->clk);
  909. return ret;
  910. }
  911. static struct console auart_console = {
  912. .name = "ttyAPP",
  913. .write = auart_console_write,
  914. .device = uart_console_device,
  915. .setup = auart_console_setup,
  916. .flags = CON_PRINTBUFFER,
  917. .index = -1,
  918. .data = &auart_driver,
  919. };
  920. #endif
  921. static struct uart_driver auart_driver = {
  922. .owner = THIS_MODULE,
  923. .driver_name = "ttyAPP",
  924. .dev_name = "ttyAPP",
  925. .major = 0,
  926. .minor = 0,
  927. .nr = MXS_AUART_PORTS,
  928. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  929. .cons = &auart_console,
  930. #endif
  931. };
  932. /*
  933. * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
  934. * could successfully get all information from dt or a negative errno.
  935. */
  936. static int serial_mxs_probe_dt(struct mxs_auart_port *s,
  937. struct platform_device *pdev)
  938. {
  939. struct device_node *np = pdev->dev.of_node;
  940. int ret;
  941. if (!np)
  942. /* no device tree device */
  943. return 1;
  944. ret = of_alias_get_id(np, "serial");
  945. if (ret < 0) {
  946. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  947. return ret;
  948. }
  949. s->port.line = ret;
  950. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  951. set_bit(MXS_AUART_RTSCTS, &s->flags);
  952. return 0;
  953. }
  954. static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
  955. {
  956. enum mctrl_gpio_idx i;
  957. struct gpio_desc *gpiod;
  958. s->gpios = mctrl_gpio_init(dev, 0);
  959. if (IS_ERR(s->gpios))
  960. return PTR_ERR(s->gpios);
  961. /* Block (enabled before) DMA option if RTS or CTS is GPIO line */
  962. if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
  963. if (test_bit(MXS_AUART_RTSCTS, &s->flags))
  964. dev_warn(dev,
  965. "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
  966. clear_bit(MXS_AUART_RTSCTS, &s->flags);
  967. }
  968. for (i = 0; i < UART_GPIO_MAX; i++) {
  969. gpiod = mctrl_gpio_to_gpiod(s->gpios, i);
  970. if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
  971. s->gpio_irq[i] = gpiod_to_irq(gpiod);
  972. else
  973. s->gpio_irq[i] = -EINVAL;
  974. }
  975. return 0;
  976. }
  977. static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
  978. {
  979. enum mctrl_gpio_idx i;
  980. for (i = 0; i < UART_GPIO_MAX; i++)
  981. if (s->gpio_irq[i] >= 0)
  982. free_irq(s->gpio_irq[i], s);
  983. }
  984. static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)
  985. {
  986. int *irq = s->gpio_irq;
  987. enum mctrl_gpio_idx i;
  988. int err = 0;
  989. for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
  990. if (irq[i] < 0)
  991. continue;
  992. irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
  993. err = request_irq(irq[i], mxs_auart_irq_handle,
  994. IRQ_TYPE_EDGE_BOTH, dev_name(s->dev), s);
  995. if (err)
  996. dev_err(s->dev, "%s - Can't get %d irq\n",
  997. __func__, irq[i]);
  998. }
  999. /*
  1000. * If something went wrong, rollback.
  1001. */
  1002. while (err && (--i >= 0))
  1003. if (irq[i] >= 0)
  1004. free_irq(irq[i], s);
  1005. return err;
  1006. }
  1007. static int mxs_auart_probe(struct platform_device *pdev)
  1008. {
  1009. const struct of_device_id *of_id =
  1010. of_match_device(mxs_auart_dt_ids, &pdev->dev);
  1011. struct mxs_auart_port *s;
  1012. u32 version;
  1013. int ret, irq;
  1014. struct resource *r;
  1015. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  1016. if (!s)
  1017. return -ENOMEM;
  1018. ret = serial_mxs_probe_dt(s, pdev);
  1019. if (ret > 0)
  1020. s->port.line = pdev->id < 0 ? 0 : pdev->id;
  1021. else if (ret < 0)
  1022. return ret;
  1023. if (of_id) {
  1024. pdev->id_entry = of_id->data;
  1025. s->devtype = pdev->id_entry->driver_data;
  1026. }
  1027. s->clk = devm_clk_get(&pdev->dev, NULL);
  1028. if (IS_ERR(s->clk))
  1029. return PTR_ERR(s->clk);
  1030. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1031. if (!r)
  1032. return -ENXIO;
  1033. s->port.mapbase = r->start;
  1034. s->port.membase = ioremap(r->start, resource_size(r));
  1035. s->port.ops = &mxs_auart_ops;
  1036. s->port.iotype = UPIO_MEM;
  1037. s->port.fifosize = MXS_AUART_FIFO_SIZE;
  1038. s->port.uartclk = clk_get_rate(s->clk);
  1039. s->port.type = PORT_IMX;
  1040. s->port.dev = s->dev = &pdev->dev;
  1041. s->mctrl_prev = 0;
  1042. irq = platform_get_irq(pdev, 0);
  1043. if (irq < 0)
  1044. return irq;
  1045. s->port.irq = irq;
  1046. ret = devm_request_irq(&pdev->dev, irq, mxs_auart_irq_handle, 0,
  1047. dev_name(&pdev->dev), s);
  1048. if (ret)
  1049. return ret;
  1050. platform_set_drvdata(pdev, s);
  1051. ret = mxs_auart_init_gpios(s, &pdev->dev);
  1052. if (ret) {
  1053. dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
  1054. return ret;
  1055. }
  1056. /*
  1057. * Get the GPIO lines IRQ
  1058. */
  1059. ret = mxs_auart_request_gpio_irq(s);
  1060. if (ret)
  1061. return ret;
  1062. auart_port[s->port.line] = s;
  1063. mxs_auart_reset(&s->port);
  1064. ret = uart_add_one_port(&auart_driver, &s->port);
  1065. if (ret)
  1066. goto out_free_gpio_irq;
  1067. version = readl(s->port.membase + AUART_VERSION);
  1068. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  1069. (version >> 24) & 0xff,
  1070. (version >> 16) & 0xff, version & 0xffff);
  1071. return 0;
  1072. out_free_gpio_irq:
  1073. mxs_auart_free_gpio_irq(s);
  1074. auart_port[pdev->id] = NULL;
  1075. return ret;
  1076. }
  1077. static int mxs_auart_remove(struct platform_device *pdev)
  1078. {
  1079. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  1080. uart_remove_one_port(&auart_driver, &s->port);
  1081. auart_port[pdev->id] = NULL;
  1082. mxs_auart_free_gpio_irq(s);
  1083. return 0;
  1084. }
  1085. static struct platform_driver mxs_auart_driver = {
  1086. .probe = mxs_auart_probe,
  1087. .remove = mxs_auart_remove,
  1088. .driver = {
  1089. .name = "mxs-auart",
  1090. .of_match_table = mxs_auart_dt_ids,
  1091. },
  1092. };
  1093. static int __init mxs_auart_init(void)
  1094. {
  1095. int r;
  1096. r = uart_register_driver(&auart_driver);
  1097. if (r)
  1098. goto out;
  1099. r = platform_driver_register(&mxs_auart_driver);
  1100. if (r)
  1101. goto out_err;
  1102. return 0;
  1103. out_err:
  1104. uart_unregister_driver(&auart_driver);
  1105. out:
  1106. return r;
  1107. }
  1108. static void __exit mxs_auart_exit(void)
  1109. {
  1110. platform_driver_unregister(&mxs_auart_driver);
  1111. uart_unregister_driver(&auart_driver);
  1112. }
  1113. module_init(mxs_auart_init);
  1114. module_exit(mxs_auart_exit);
  1115. MODULE_LICENSE("GPL");
  1116. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  1117. MODULE_ALIAS("platform:mxs-auart");