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