mxs-auart.c 19 KB

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