mxs-auart.c 20 KB

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