mxs-auart.c 19 KB

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