mxs-auart.c 33 KB

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