mxs-auart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. }
  315. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  316. {
  317. u32 istatus, istat;
  318. struct mxs_auart_port *s = context;
  319. u32 stat = readl(s->port.membase + AUART_STAT);
  320. istatus = istat = readl(s->port.membase + AUART_INTR);
  321. if (istat & AUART_INTR_CTSMIS) {
  322. uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
  323. writel(AUART_INTR_CTSMIS,
  324. s->port.membase + AUART_INTR_CLR);
  325. istat &= ~AUART_INTR_CTSMIS;
  326. }
  327. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  328. mxs_auart_rx_chars(s);
  329. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  330. }
  331. if (istat & AUART_INTR_TXIS) {
  332. mxs_auart_tx_chars(s);
  333. istat &= ~AUART_INTR_TXIS;
  334. }
  335. writel(istatus & (AUART_INTR_RTIS
  336. | AUART_INTR_TXIS
  337. | AUART_INTR_RXIS
  338. | AUART_INTR_CTSMIS),
  339. s->port.membase + AUART_INTR_CLR);
  340. return IRQ_HANDLED;
  341. }
  342. static void mxs_auart_reset(struct uart_port *u)
  343. {
  344. int i;
  345. unsigned int reg;
  346. writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
  347. for (i = 0; i < 10000; i++) {
  348. reg = readl(u->membase + AUART_CTRL0);
  349. if (!(reg & AUART_CTRL0_SFTRST))
  350. break;
  351. udelay(3);
  352. }
  353. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  354. }
  355. static int mxs_auart_startup(struct uart_port *u)
  356. {
  357. struct mxs_auart_port *s = to_auart_port(u);
  358. clk_prepare_enable(s->clk);
  359. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  360. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
  361. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  362. u->membase + AUART_INTR);
  363. /*
  364. * Enable fifo so all four bytes of a DMA word are written to
  365. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  366. */
  367. writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
  368. return 0;
  369. }
  370. static void mxs_auart_shutdown(struct uart_port *u)
  371. {
  372. struct mxs_auart_port *s = to_auart_port(u);
  373. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
  374. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
  375. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  376. u->membase + AUART_INTR_CLR);
  377. clk_disable_unprepare(s->clk);
  378. }
  379. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  380. {
  381. if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
  382. return TIOCSER_TEMT;
  383. else
  384. return 0;
  385. }
  386. static void mxs_auart_start_tx(struct uart_port *u)
  387. {
  388. struct mxs_auart_port *s = to_auart_port(u);
  389. /* enable transmitter */
  390. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
  391. mxs_auart_tx_chars(s);
  392. }
  393. static void mxs_auart_stop_tx(struct uart_port *u)
  394. {
  395. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
  396. }
  397. static void mxs_auart_stop_rx(struct uart_port *u)
  398. {
  399. writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
  400. }
  401. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  402. {
  403. if (ctl)
  404. writel(AUART_LINECTRL_BRK,
  405. u->membase + AUART_LINECTRL_SET);
  406. else
  407. writel(AUART_LINECTRL_BRK,
  408. u->membase + AUART_LINECTRL_CLR);
  409. }
  410. static void mxs_auart_enable_ms(struct uart_port *port)
  411. {
  412. /* just empty */
  413. }
  414. static struct uart_ops mxs_auart_ops = {
  415. .tx_empty = mxs_auart_tx_empty,
  416. .start_tx = mxs_auart_start_tx,
  417. .stop_tx = mxs_auart_stop_tx,
  418. .stop_rx = mxs_auart_stop_rx,
  419. .enable_ms = mxs_auart_enable_ms,
  420. .break_ctl = mxs_auart_break_ctl,
  421. .set_mctrl = mxs_auart_set_mctrl,
  422. .get_mctrl = mxs_auart_get_mctrl,
  423. .startup = mxs_auart_startup,
  424. .shutdown = mxs_auart_shutdown,
  425. .set_termios = mxs_auart_settermios,
  426. .type = mxs_auart_type,
  427. .release_port = mxs_auart_release_port,
  428. .request_port = mxs_auart_request_port,
  429. .config_port = mxs_auart_config_port,
  430. .verify_port = mxs_auart_verify_port,
  431. };
  432. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  433. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  434. static void mxs_auart_console_putchar(struct uart_port *port, int ch)
  435. {
  436. unsigned int to = 1000;
  437. while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
  438. if (!to--)
  439. break;
  440. udelay(1);
  441. }
  442. writel(ch, port->membase + AUART_DATA);
  443. }
  444. static void
  445. auart_console_write(struct console *co, const char *str, unsigned int count)
  446. {
  447. struct mxs_auart_port *s;
  448. struct uart_port *port;
  449. unsigned int old_ctrl0, old_ctrl2;
  450. unsigned int to = 1000;
  451. if (co->index > MXS_AUART_PORTS || co->index < 0)
  452. return;
  453. s = auart_port[co->index];
  454. port = &s->port;
  455. clk_enable(s->clk);
  456. /* First save the CR then disable the interrupts */
  457. old_ctrl2 = readl(port->membase + AUART_CTRL2);
  458. old_ctrl0 = readl(port->membase + AUART_CTRL0);
  459. writel(AUART_CTRL0_CLKGATE,
  460. port->membase + AUART_CTRL0_CLR);
  461. writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
  462. port->membase + AUART_CTRL2_SET);
  463. uart_console_write(port, str, count, mxs_auart_console_putchar);
  464. /*
  465. * Finally, wait for transmitter to become empty
  466. * and restore the TCR
  467. */
  468. while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
  469. if (!to--)
  470. break;
  471. udelay(1);
  472. }
  473. writel(old_ctrl0, port->membase + AUART_CTRL0);
  474. writel(old_ctrl2, port->membase + AUART_CTRL2);
  475. clk_disable(s->clk);
  476. }
  477. static void __init
  478. auart_console_get_options(struct uart_port *port, int *baud,
  479. int *parity, int *bits)
  480. {
  481. unsigned int lcr_h, quot;
  482. if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
  483. return;
  484. lcr_h = readl(port->membase + AUART_LINECTRL);
  485. *parity = 'n';
  486. if (lcr_h & AUART_LINECTRL_PEN) {
  487. if (lcr_h & AUART_LINECTRL_EPS)
  488. *parity = 'e';
  489. else
  490. *parity = 'o';
  491. }
  492. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
  493. *bits = 7;
  494. else
  495. *bits = 8;
  496. quot = ((readl(port->membase + AUART_LINECTRL)
  497. & AUART_LINECTRL_BAUD_DIVINT_MASK))
  498. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  499. quot |= ((readl(port->membase + AUART_LINECTRL)
  500. & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  501. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  502. if (quot == 0)
  503. quot = 1;
  504. *baud = (port->uartclk << 2) / quot;
  505. }
  506. static int __init
  507. auart_console_setup(struct console *co, char *options)
  508. {
  509. struct mxs_auart_port *s;
  510. int baud = 9600;
  511. int bits = 8;
  512. int parity = 'n';
  513. int flow = 'n';
  514. int ret;
  515. /*
  516. * Check whether an invalid uart number has been specified, and
  517. * if so, search for the first available port that does have
  518. * console support.
  519. */
  520. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  521. co->index = 0;
  522. s = auart_port[co->index];
  523. if (!s)
  524. return -ENODEV;
  525. clk_prepare_enable(s->clk);
  526. if (options)
  527. uart_parse_options(options, &baud, &parity, &bits, &flow);
  528. else
  529. auart_console_get_options(&s->port, &baud, &parity, &bits);
  530. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  531. clk_disable_unprepare(s->clk);
  532. return ret;
  533. }
  534. static struct console auart_console = {
  535. .name = "ttyAPP",
  536. .write = auart_console_write,
  537. .device = uart_console_device,
  538. .setup = auart_console_setup,
  539. .flags = CON_PRINTBUFFER,
  540. .index = -1,
  541. .data = &auart_driver,
  542. };
  543. #endif
  544. static struct uart_driver auart_driver = {
  545. .owner = THIS_MODULE,
  546. .driver_name = "ttyAPP",
  547. .dev_name = "ttyAPP",
  548. .major = 0,
  549. .minor = 0,
  550. .nr = MXS_AUART_PORTS,
  551. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  552. .cons = &auart_console,
  553. #endif
  554. };
  555. static int __devinit mxs_auart_probe(struct platform_device *pdev)
  556. {
  557. struct mxs_auart_port *s;
  558. u32 version;
  559. int ret = 0;
  560. struct resource *r;
  561. struct pinctrl *pinctrl;
  562. s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
  563. if (!s) {
  564. ret = -ENOMEM;
  565. goto out;
  566. }
  567. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  568. if (IS_ERR(pinctrl)) {
  569. ret = PTR_ERR(pinctrl);
  570. goto out_free;
  571. }
  572. s->clk = clk_get(&pdev->dev, NULL);
  573. if (IS_ERR(s->clk)) {
  574. ret = PTR_ERR(s->clk);
  575. goto out_free;
  576. }
  577. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  578. if (!r) {
  579. ret = -ENXIO;
  580. goto out_free_clk;
  581. }
  582. s->port.mapbase = r->start;
  583. s->port.membase = ioremap(r->start, resource_size(r));
  584. s->port.ops = &mxs_auart_ops;
  585. s->port.iotype = UPIO_MEM;
  586. s->port.line = pdev->id < 0 ? 0 : pdev->id;
  587. s->port.fifosize = 16;
  588. s->port.uartclk = clk_get_rate(s->clk);
  589. s->port.type = PORT_IMX;
  590. s->port.dev = s->dev = get_device(&pdev->dev);
  591. s->flags = 0;
  592. s->ctrl = 0;
  593. s->irq = platform_get_irq(pdev, 0);
  594. s->port.irq = s->irq;
  595. ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
  596. if (ret)
  597. goto out_free_clk;
  598. platform_set_drvdata(pdev, s);
  599. auart_port[pdev->id] = s;
  600. mxs_auart_reset(&s->port);
  601. ret = uart_add_one_port(&auart_driver, &s->port);
  602. if (ret)
  603. goto out_free_irq;
  604. version = readl(s->port.membase + AUART_VERSION);
  605. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  606. (version >> 24) & 0xff,
  607. (version >> 16) & 0xff, version & 0xffff);
  608. return 0;
  609. out_free_irq:
  610. auart_port[pdev->id] = NULL;
  611. free_irq(s->irq, s);
  612. out_free_clk:
  613. clk_put(s->clk);
  614. out_free:
  615. kfree(s);
  616. out:
  617. return ret;
  618. }
  619. static int __devexit mxs_auart_remove(struct platform_device *pdev)
  620. {
  621. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  622. uart_remove_one_port(&auart_driver, &s->port);
  623. auart_port[pdev->id] = NULL;
  624. clk_put(s->clk);
  625. free_irq(s->irq, s);
  626. kfree(s);
  627. return 0;
  628. }
  629. static struct platform_driver mxs_auart_driver = {
  630. .probe = mxs_auart_probe,
  631. .remove = __devexit_p(mxs_auart_remove),
  632. .driver = {
  633. .name = "mxs-auart",
  634. .owner = THIS_MODULE,
  635. },
  636. };
  637. static int __init mxs_auart_init(void)
  638. {
  639. int r;
  640. r = uart_register_driver(&auart_driver);
  641. if (r)
  642. goto out;
  643. r = platform_driver_register(&mxs_auart_driver);
  644. if (r)
  645. goto out_err;
  646. return 0;
  647. out_err:
  648. uart_unregister_driver(&auart_driver);
  649. out:
  650. return r;
  651. }
  652. static void __exit mxs_auart_exit(void)
  653. {
  654. platform_driver_unregister(&mxs_auart_driver);
  655. uart_unregister_driver(&auart_driver);
  656. }
  657. module_init(mxs_auart_init);
  658. module_exit(mxs_auart_exit);
  659. MODULE_LICENSE("GPL");
  660. MODULE_DESCRIPTION("Freescale MXS application uart driver");