mxs-auart.c 18 KB

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