mxs-auart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 <asm/cacheflush.h>
  37. #define MXS_AUART_PORTS 5
  38. #define AUART_CTRL0 0x00000000
  39. #define AUART_CTRL0_SET 0x00000004
  40. #define AUART_CTRL0_CLR 0x00000008
  41. #define AUART_CTRL0_TOG 0x0000000c
  42. #define AUART_CTRL1 0x00000010
  43. #define AUART_CTRL1_SET 0x00000014
  44. #define AUART_CTRL1_CLR 0x00000018
  45. #define AUART_CTRL1_TOG 0x0000001c
  46. #define AUART_CTRL2 0x00000020
  47. #define AUART_CTRL2_SET 0x00000024
  48. #define AUART_CTRL2_CLR 0x00000028
  49. #define AUART_CTRL2_TOG 0x0000002c
  50. #define AUART_LINECTRL 0x00000030
  51. #define AUART_LINECTRL_SET 0x00000034
  52. #define AUART_LINECTRL_CLR 0x00000038
  53. #define AUART_LINECTRL_TOG 0x0000003c
  54. #define AUART_LINECTRL2 0x00000040
  55. #define AUART_LINECTRL2_SET 0x00000044
  56. #define AUART_LINECTRL2_CLR 0x00000048
  57. #define AUART_LINECTRL2_TOG 0x0000004c
  58. #define AUART_INTR 0x00000050
  59. #define AUART_INTR_SET 0x00000054
  60. #define AUART_INTR_CLR 0x00000058
  61. #define AUART_INTR_TOG 0x0000005c
  62. #define AUART_DATA 0x00000060
  63. #define AUART_STAT 0x00000070
  64. #define AUART_DEBUG 0x00000080
  65. #define AUART_VERSION 0x00000090
  66. #define AUART_AUTOBAUD 0x000000a0
  67. #define AUART_CTRL0_SFTRST (1 << 31)
  68. #define AUART_CTRL0_CLKGATE (1 << 30)
  69. #define AUART_CTRL2_CTSEN (1 << 15)
  70. #define AUART_CTRL2_RTSEN (1 << 14)
  71. #define AUART_CTRL2_RTS (1 << 11)
  72. #define AUART_CTRL2_RXE (1 << 9)
  73. #define AUART_CTRL2_TXE (1 << 8)
  74. #define AUART_CTRL2_UARTEN (1 << 0)
  75. #define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
  76. #define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
  77. #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
  78. #define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
  79. #define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
  80. #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
  81. #define AUART_LINECTRL_WLEN_MASK 0x00000060
  82. #define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
  83. #define AUART_LINECTRL_FEN (1 << 4)
  84. #define AUART_LINECTRL_STP2 (1 << 3)
  85. #define AUART_LINECTRL_EPS (1 << 2)
  86. #define AUART_LINECTRL_PEN (1 << 1)
  87. #define AUART_LINECTRL_BRK (1 << 0)
  88. #define AUART_INTR_RTIEN (1 << 22)
  89. #define AUART_INTR_TXIEN (1 << 21)
  90. #define AUART_INTR_RXIEN (1 << 20)
  91. #define AUART_INTR_CTSMIEN (1 << 17)
  92. #define AUART_INTR_RTIS (1 << 6)
  93. #define AUART_INTR_TXIS (1 << 5)
  94. #define AUART_INTR_RXIS (1 << 4)
  95. #define AUART_INTR_CTSMIS (1 << 1)
  96. #define AUART_STAT_BUSY (1 << 29)
  97. #define AUART_STAT_CTS (1 << 28)
  98. #define AUART_STAT_TXFE (1 << 27)
  99. #define AUART_STAT_TXFF (1 << 25)
  100. #define AUART_STAT_RXFE (1 << 24)
  101. #define AUART_STAT_OERR (1 << 19)
  102. #define AUART_STAT_BERR (1 << 18)
  103. #define AUART_STAT_PERR (1 << 17)
  104. #define AUART_STAT_FERR (1 << 16)
  105. static struct uart_driver auart_driver;
  106. enum mxs_auart_type {
  107. IMX23_AUART,
  108. IMX28_AUART,
  109. };
  110. struct mxs_auart_port {
  111. struct uart_port port;
  112. unsigned int flags;
  113. unsigned int ctrl;
  114. enum mxs_auart_type devtype;
  115. unsigned int irq;
  116. struct clk *clk;
  117. struct device *dev;
  118. };
  119. static struct platform_device_id mxs_auart_devtype[] = {
  120. { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
  121. { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
  122. { /* sentinel */ }
  123. };
  124. MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
  125. static struct of_device_id mxs_auart_dt_ids[] = {
  126. {
  127. .compatible = "fsl,imx28-auart",
  128. .data = &mxs_auart_devtype[IMX28_AUART]
  129. }, {
  130. .compatible = "fsl,imx23-auart",
  131. .data = &mxs_auart_devtype[IMX23_AUART]
  132. }, { /* sentinel */ }
  133. };
  134. MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
  135. static inline int is_imx28_auart(struct mxs_auart_port *s)
  136. {
  137. return s->devtype == IMX28_AUART;
  138. }
  139. static void mxs_auart_stop_tx(struct uart_port *u);
  140. #define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
  141. static inline void mxs_auart_tx_chars(struct mxs_auart_port *s)
  142. {
  143. struct circ_buf *xmit = &s->port.state->xmit;
  144. while (!(readl(s->port.membase + AUART_STAT) &
  145. AUART_STAT_TXFF)) {
  146. if (s->port.x_char) {
  147. s->port.icount.tx++;
  148. writel(s->port.x_char,
  149. s->port.membase + AUART_DATA);
  150. s->port.x_char = 0;
  151. continue;
  152. }
  153. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  154. s->port.icount.tx++;
  155. writel(xmit->buf[xmit->tail],
  156. s->port.membase + AUART_DATA);
  157. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  158. } else
  159. break;
  160. }
  161. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  162. uart_write_wakeup(&s->port);
  163. if (uart_circ_empty(&(s->port.state->xmit)))
  164. writel(AUART_INTR_TXIEN,
  165. s->port.membase + AUART_INTR_CLR);
  166. else
  167. writel(AUART_INTR_TXIEN,
  168. s->port.membase + AUART_INTR_SET);
  169. if (uart_tx_stopped(&s->port))
  170. mxs_auart_stop_tx(&s->port);
  171. }
  172. static void mxs_auart_rx_char(struct mxs_auart_port *s)
  173. {
  174. int flag;
  175. u32 stat;
  176. u8 c;
  177. c = readl(s->port.membase + AUART_DATA);
  178. stat = readl(s->port.membase + AUART_STAT);
  179. flag = TTY_NORMAL;
  180. s->port.icount.rx++;
  181. if (stat & AUART_STAT_BERR) {
  182. s->port.icount.brk++;
  183. if (uart_handle_break(&s->port))
  184. goto out;
  185. } else if (stat & AUART_STAT_PERR) {
  186. s->port.icount.parity++;
  187. } else if (stat & AUART_STAT_FERR) {
  188. s->port.icount.frame++;
  189. }
  190. /*
  191. * Mask off conditions which should be ingored.
  192. */
  193. stat &= s->port.read_status_mask;
  194. if (stat & AUART_STAT_BERR) {
  195. flag = TTY_BREAK;
  196. } else if (stat & AUART_STAT_PERR)
  197. flag = TTY_PARITY;
  198. else if (stat & AUART_STAT_FERR)
  199. flag = TTY_FRAME;
  200. if (stat & AUART_STAT_OERR)
  201. s->port.icount.overrun++;
  202. if (uart_handle_sysrq_char(&s->port, c))
  203. goto out;
  204. uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
  205. out:
  206. writel(stat, s->port.membase + AUART_STAT);
  207. }
  208. static void mxs_auart_rx_chars(struct mxs_auart_port *s)
  209. {
  210. struct tty_struct *tty = s->port.state->port.tty;
  211. u32 stat = 0;
  212. for (;;) {
  213. stat = readl(s->port.membase + AUART_STAT);
  214. if (stat & AUART_STAT_RXFE)
  215. break;
  216. mxs_auart_rx_char(s);
  217. }
  218. writel(stat, s->port.membase + AUART_STAT);
  219. tty_flip_buffer_push(tty);
  220. }
  221. static int mxs_auart_request_port(struct uart_port *u)
  222. {
  223. return 0;
  224. }
  225. static int mxs_auart_verify_port(struct uart_port *u,
  226. struct serial_struct *ser)
  227. {
  228. if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
  229. return -EINVAL;
  230. return 0;
  231. }
  232. static void mxs_auart_config_port(struct uart_port *u, int flags)
  233. {
  234. }
  235. static const char *mxs_auart_type(struct uart_port *u)
  236. {
  237. struct mxs_auart_port *s = to_auart_port(u);
  238. return dev_name(s->dev);
  239. }
  240. static void mxs_auart_release_port(struct uart_port *u)
  241. {
  242. }
  243. static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
  244. {
  245. struct mxs_auart_port *s = to_auart_port(u);
  246. u32 ctrl = readl(u->membase + AUART_CTRL2);
  247. ctrl &= ~AUART_CTRL2_RTSEN;
  248. if (mctrl & TIOCM_RTS) {
  249. if (tty_port_cts_enabled(&u->state->port))
  250. ctrl |= AUART_CTRL2_RTSEN;
  251. }
  252. s->ctrl = mctrl;
  253. writel(ctrl, u->membase + AUART_CTRL2);
  254. }
  255. static u32 mxs_auart_get_mctrl(struct uart_port *u)
  256. {
  257. struct mxs_auart_port *s = to_auart_port(u);
  258. u32 stat = readl(u->membase + AUART_STAT);
  259. int ctrl2 = readl(u->membase + AUART_CTRL2);
  260. u32 mctrl = s->ctrl;
  261. mctrl &= ~TIOCM_CTS;
  262. if (stat & AUART_STAT_CTS)
  263. mctrl |= TIOCM_CTS;
  264. if (ctrl2 & AUART_CTRL2_RTS)
  265. mctrl |= TIOCM_RTS;
  266. return mctrl;
  267. }
  268. static void mxs_auart_settermios(struct uart_port *u,
  269. struct ktermios *termios,
  270. struct ktermios *old)
  271. {
  272. u32 bm, ctrl, ctrl2, div;
  273. unsigned int cflag, baud;
  274. cflag = termios->c_cflag;
  275. ctrl = AUART_LINECTRL_FEN;
  276. ctrl2 = readl(u->membase + AUART_CTRL2);
  277. /* byte size */
  278. switch (cflag & CSIZE) {
  279. case CS5:
  280. bm = 0;
  281. break;
  282. case CS6:
  283. bm = 1;
  284. break;
  285. case CS7:
  286. bm = 2;
  287. break;
  288. case CS8:
  289. bm = 3;
  290. break;
  291. default:
  292. return;
  293. }
  294. ctrl |= AUART_LINECTRL_WLEN(bm);
  295. /* parity */
  296. if (cflag & PARENB) {
  297. ctrl |= AUART_LINECTRL_PEN;
  298. if ((cflag & PARODD) == 0)
  299. ctrl |= AUART_LINECTRL_EPS;
  300. }
  301. u->read_status_mask = 0;
  302. if (termios->c_iflag & INPCK)
  303. u->read_status_mask |= AUART_STAT_PERR;
  304. if (termios->c_iflag & (BRKINT | PARMRK))
  305. u->read_status_mask |= AUART_STAT_BERR;
  306. /*
  307. * Characters to ignore
  308. */
  309. u->ignore_status_mask = 0;
  310. if (termios->c_iflag & IGNPAR)
  311. u->ignore_status_mask |= AUART_STAT_PERR;
  312. if (termios->c_iflag & IGNBRK) {
  313. u->ignore_status_mask |= AUART_STAT_BERR;
  314. /*
  315. * If we're ignoring parity and break indicators,
  316. * ignore overruns too (for real raw support).
  317. */
  318. if (termios->c_iflag & IGNPAR)
  319. u->ignore_status_mask |= AUART_STAT_OERR;
  320. }
  321. /*
  322. * ignore all characters if CREAD is not set
  323. */
  324. if (cflag & CREAD)
  325. ctrl2 |= AUART_CTRL2_RXE;
  326. else
  327. ctrl2 &= ~AUART_CTRL2_RXE;
  328. /* figure out the stop bits requested */
  329. if (cflag & CSTOPB)
  330. ctrl |= AUART_LINECTRL_STP2;
  331. /* figure out the hardware flow control settings */
  332. if (cflag & CRTSCTS)
  333. ctrl2 |= AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN;
  334. else
  335. ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
  336. /* set baud rate */
  337. baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
  338. div = u->uartclk * 32 / baud;
  339. ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
  340. ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
  341. writel(ctrl, u->membase + AUART_LINECTRL);
  342. writel(ctrl2, u->membase + AUART_CTRL2);
  343. uart_update_timeout(u, termios->c_cflag, baud);
  344. }
  345. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  346. {
  347. u32 istatus, istat;
  348. struct mxs_auart_port *s = context;
  349. u32 stat = readl(s->port.membase + AUART_STAT);
  350. istatus = istat = readl(s->port.membase + AUART_INTR);
  351. if (istat & AUART_INTR_CTSMIS) {
  352. uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
  353. writel(AUART_INTR_CTSMIS,
  354. s->port.membase + AUART_INTR_CLR);
  355. istat &= ~AUART_INTR_CTSMIS;
  356. }
  357. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  358. mxs_auart_rx_chars(s);
  359. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  360. }
  361. if (istat & AUART_INTR_TXIS) {
  362. mxs_auart_tx_chars(s);
  363. istat &= ~AUART_INTR_TXIS;
  364. }
  365. writel(istatus & (AUART_INTR_RTIS
  366. | AUART_INTR_TXIS
  367. | AUART_INTR_RXIS
  368. | AUART_INTR_CTSMIS),
  369. s->port.membase + AUART_INTR_CLR);
  370. return IRQ_HANDLED;
  371. }
  372. static void mxs_auart_reset(struct uart_port *u)
  373. {
  374. int i;
  375. unsigned int reg;
  376. writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
  377. for (i = 0; i < 10000; i++) {
  378. reg = readl(u->membase + AUART_CTRL0);
  379. if (!(reg & AUART_CTRL0_SFTRST))
  380. break;
  381. udelay(3);
  382. }
  383. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  384. }
  385. static int mxs_auart_startup(struct uart_port *u)
  386. {
  387. struct mxs_auart_port *s = to_auart_port(u);
  388. clk_prepare_enable(s->clk);
  389. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  390. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
  391. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  392. u->membase + AUART_INTR);
  393. /*
  394. * Enable fifo so all four bytes of a DMA word are written to
  395. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  396. */
  397. writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
  398. return 0;
  399. }
  400. static void mxs_auart_shutdown(struct uart_port *u)
  401. {
  402. struct mxs_auart_port *s = to_auart_port(u);
  403. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
  404. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  405. u->membase + AUART_INTR_CLR);
  406. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
  407. clk_disable_unprepare(s->clk);
  408. }
  409. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  410. {
  411. if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
  412. return TIOCSER_TEMT;
  413. else
  414. return 0;
  415. }
  416. static void mxs_auart_start_tx(struct uart_port *u)
  417. {
  418. struct mxs_auart_port *s = to_auart_port(u);
  419. /* enable transmitter */
  420. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
  421. mxs_auart_tx_chars(s);
  422. }
  423. static void mxs_auart_stop_tx(struct uart_port *u)
  424. {
  425. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
  426. }
  427. static void mxs_auart_stop_rx(struct uart_port *u)
  428. {
  429. writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
  430. }
  431. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  432. {
  433. if (ctl)
  434. writel(AUART_LINECTRL_BRK,
  435. u->membase + AUART_LINECTRL_SET);
  436. else
  437. writel(AUART_LINECTRL_BRK,
  438. u->membase + AUART_LINECTRL_CLR);
  439. }
  440. static void mxs_auart_enable_ms(struct uart_port *port)
  441. {
  442. /* just empty */
  443. }
  444. static struct uart_ops mxs_auart_ops = {
  445. .tx_empty = mxs_auart_tx_empty,
  446. .start_tx = mxs_auart_start_tx,
  447. .stop_tx = mxs_auart_stop_tx,
  448. .stop_rx = mxs_auart_stop_rx,
  449. .enable_ms = mxs_auart_enable_ms,
  450. .break_ctl = mxs_auart_break_ctl,
  451. .set_mctrl = mxs_auart_set_mctrl,
  452. .get_mctrl = mxs_auart_get_mctrl,
  453. .startup = mxs_auart_startup,
  454. .shutdown = mxs_auart_shutdown,
  455. .set_termios = mxs_auart_settermios,
  456. .type = mxs_auart_type,
  457. .release_port = mxs_auart_release_port,
  458. .request_port = mxs_auart_request_port,
  459. .config_port = mxs_auart_config_port,
  460. .verify_port = mxs_auart_verify_port,
  461. };
  462. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  463. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  464. static void mxs_auart_console_putchar(struct uart_port *port, int ch)
  465. {
  466. unsigned int to = 1000;
  467. while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
  468. if (!to--)
  469. break;
  470. udelay(1);
  471. }
  472. writel(ch, port->membase + AUART_DATA);
  473. }
  474. static void
  475. auart_console_write(struct console *co, const char *str, unsigned int count)
  476. {
  477. struct mxs_auart_port *s;
  478. struct uart_port *port;
  479. unsigned int old_ctrl0, old_ctrl2;
  480. unsigned int to = 1000;
  481. if (co->index > MXS_AUART_PORTS || co->index < 0)
  482. return;
  483. s = auart_port[co->index];
  484. port = &s->port;
  485. clk_enable(s->clk);
  486. /* First save the CR then disable the interrupts */
  487. old_ctrl2 = readl(port->membase + AUART_CTRL2);
  488. old_ctrl0 = readl(port->membase + AUART_CTRL0);
  489. writel(AUART_CTRL0_CLKGATE,
  490. port->membase + AUART_CTRL0_CLR);
  491. writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
  492. port->membase + AUART_CTRL2_SET);
  493. uart_console_write(port, str, count, mxs_auart_console_putchar);
  494. /*
  495. * Finally, wait for transmitter to become empty
  496. * and restore the TCR
  497. */
  498. while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
  499. if (!to--)
  500. break;
  501. udelay(1);
  502. }
  503. writel(old_ctrl0, port->membase + AUART_CTRL0);
  504. writel(old_ctrl2, port->membase + AUART_CTRL2);
  505. clk_disable(s->clk);
  506. }
  507. static void __init
  508. auart_console_get_options(struct uart_port *port, int *baud,
  509. int *parity, int *bits)
  510. {
  511. unsigned int lcr_h, quot;
  512. if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
  513. return;
  514. lcr_h = readl(port->membase + AUART_LINECTRL);
  515. *parity = 'n';
  516. if (lcr_h & AUART_LINECTRL_PEN) {
  517. if (lcr_h & AUART_LINECTRL_EPS)
  518. *parity = 'e';
  519. else
  520. *parity = 'o';
  521. }
  522. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
  523. *bits = 7;
  524. else
  525. *bits = 8;
  526. quot = ((readl(port->membase + AUART_LINECTRL)
  527. & AUART_LINECTRL_BAUD_DIVINT_MASK))
  528. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  529. quot |= ((readl(port->membase + AUART_LINECTRL)
  530. & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  531. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  532. if (quot == 0)
  533. quot = 1;
  534. *baud = (port->uartclk << 2) / quot;
  535. }
  536. static int __init
  537. auart_console_setup(struct console *co, char *options)
  538. {
  539. struct mxs_auart_port *s;
  540. int baud = 9600;
  541. int bits = 8;
  542. int parity = 'n';
  543. int flow = 'n';
  544. int ret;
  545. /*
  546. * Check whether an invalid uart number has been specified, and
  547. * if so, search for the first available port that does have
  548. * console support.
  549. */
  550. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  551. co->index = 0;
  552. s = auart_port[co->index];
  553. if (!s)
  554. return -ENODEV;
  555. clk_prepare_enable(s->clk);
  556. if (options)
  557. uart_parse_options(options, &baud, &parity, &bits, &flow);
  558. else
  559. auart_console_get_options(&s->port, &baud, &parity, &bits);
  560. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  561. clk_disable_unprepare(s->clk);
  562. return ret;
  563. }
  564. static struct console auart_console = {
  565. .name = "ttyAPP",
  566. .write = auart_console_write,
  567. .device = uart_console_device,
  568. .setup = auart_console_setup,
  569. .flags = CON_PRINTBUFFER,
  570. .index = -1,
  571. .data = &auart_driver,
  572. };
  573. #endif
  574. static struct uart_driver auart_driver = {
  575. .owner = THIS_MODULE,
  576. .driver_name = "ttyAPP",
  577. .dev_name = "ttyAPP",
  578. .major = 0,
  579. .minor = 0,
  580. .nr = MXS_AUART_PORTS,
  581. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  582. .cons = &auart_console,
  583. #endif
  584. };
  585. /*
  586. * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
  587. * could successfully get all information from dt or a negative errno.
  588. */
  589. static int serial_mxs_probe_dt(struct mxs_auart_port *s,
  590. struct platform_device *pdev)
  591. {
  592. struct device_node *np = pdev->dev.of_node;
  593. int ret;
  594. if (!np)
  595. /* no device tree device */
  596. return 1;
  597. ret = of_alias_get_id(np, "serial");
  598. if (ret < 0) {
  599. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  600. return ret;
  601. }
  602. s->port.line = ret;
  603. return 0;
  604. }
  605. static int __devinit mxs_auart_probe(struct platform_device *pdev)
  606. {
  607. const struct of_device_id *of_id =
  608. of_match_device(mxs_auart_dt_ids, &pdev->dev);
  609. struct mxs_auart_port *s;
  610. u32 version;
  611. int ret = 0;
  612. struct resource *r;
  613. struct pinctrl *pinctrl;
  614. s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
  615. if (!s) {
  616. ret = -ENOMEM;
  617. goto out;
  618. }
  619. ret = serial_mxs_probe_dt(s, pdev);
  620. if (ret > 0)
  621. s->port.line = pdev->id < 0 ? 0 : pdev->id;
  622. else if (ret < 0)
  623. goto out_free;
  624. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  625. if (IS_ERR(pinctrl)) {
  626. ret = PTR_ERR(pinctrl);
  627. goto out_free;
  628. }
  629. if (of_id) {
  630. pdev->id_entry = of_id->data;
  631. s->devtype = pdev->id_entry->driver_data;
  632. }
  633. s->clk = clk_get(&pdev->dev, NULL);
  634. if (IS_ERR(s->clk)) {
  635. ret = PTR_ERR(s->clk);
  636. goto out_free;
  637. }
  638. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  639. if (!r) {
  640. ret = -ENXIO;
  641. goto out_free_clk;
  642. }
  643. s->port.mapbase = r->start;
  644. s->port.membase = ioremap(r->start, resource_size(r));
  645. s->port.ops = &mxs_auart_ops;
  646. s->port.iotype = UPIO_MEM;
  647. s->port.fifosize = 16;
  648. s->port.uartclk = clk_get_rate(s->clk);
  649. s->port.type = PORT_IMX;
  650. s->port.dev = s->dev = get_device(&pdev->dev);
  651. s->flags = 0;
  652. s->ctrl = 0;
  653. s->irq = platform_get_irq(pdev, 0);
  654. s->port.irq = s->irq;
  655. ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
  656. if (ret)
  657. goto out_free_clk;
  658. platform_set_drvdata(pdev, s);
  659. auart_port[s->port.line] = s;
  660. mxs_auart_reset(&s->port);
  661. ret = uart_add_one_port(&auart_driver, &s->port);
  662. if (ret)
  663. goto out_free_irq;
  664. version = readl(s->port.membase + AUART_VERSION);
  665. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  666. (version >> 24) & 0xff,
  667. (version >> 16) & 0xff, version & 0xffff);
  668. return 0;
  669. out_free_irq:
  670. auart_port[pdev->id] = NULL;
  671. free_irq(s->irq, s);
  672. out_free_clk:
  673. put_device(s->dev);
  674. clk_put(s->clk);
  675. out_free:
  676. kfree(s);
  677. out:
  678. return ret;
  679. }
  680. static int __devexit mxs_auart_remove(struct platform_device *pdev)
  681. {
  682. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  683. uart_remove_one_port(&auart_driver, &s->port);
  684. auart_port[pdev->id] = NULL;
  685. put_device(s->dev);
  686. clk_put(s->clk);
  687. free_irq(s->irq, s);
  688. kfree(s);
  689. return 0;
  690. }
  691. static struct platform_driver mxs_auart_driver = {
  692. .probe = mxs_auart_probe,
  693. .remove = __devexit_p(mxs_auart_remove),
  694. .driver = {
  695. .name = "mxs-auart",
  696. .owner = THIS_MODULE,
  697. .of_match_table = mxs_auart_dt_ids,
  698. },
  699. };
  700. static int __init mxs_auart_init(void)
  701. {
  702. int r;
  703. r = uart_register_driver(&auart_driver);
  704. if (r)
  705. goto out;
  706. r = platform_driver_register(&mxs_auart_driver);
  707. if (r)
  708. goto out_err;
  709. return 0;
  710. out_err:
  711. uart_unregister_driver(&auart_driver);
  712. out:
  713. return r;
  714. }
  715. static void __exit mxs_auart_exit(void)
  716. {
  717. platform_driver_unregister(&mxs_auart_driver);
  718. uart_unregister_driver(&auart_driver);
  719. }
  720. module_init(mxs_auart_init);
  721. module_exit(mxs_auart_exit);
  722. MODULE_LICENSE("GPL");
  723. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  724. MODULE_ALIAS("platform:mxs-auart");