xref: /linux/drivers/tty/serial/omap-serial.c (revision 62f3ec5fbd5fddce62b7a71adc04723a5eb903da)
1 /*
2  * Driver for OMAP-UART controller.
3  * Based on drivers/serial/8250.c
4  *
5  * Copyright (C) 2010 Texas Instruments.
6  *
7  * Authors:
8  *	Govindraj R	<govindraj.raja@ti.com>
9  *	Thara Gopinath	<thara@ti.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * Note: This driver is made separate from 8250 driver as we cannot
17  * over load 8250 driver with omap platform specific configuration for
18  * features like DMA, it makes easier to implement features like DMA and
19  * hardware flow control and software flow control configuration with
20  * this driver as required for the omap-platform.
21  */
22 
23 #if defined(CONFIG_SERIAL_OMAP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24 #define SUPPORT_SYSRQ
25 #endif
26 
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/serial_reg.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/io.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/clk.h>
38 #include <linux/serial_core.h>
39 #include <linux/irq.h>
40 #include <linux/pm_runtime.h>
41 
42 #include <plat/dma.h>
43 #include <plat/dmtimer.h>
44 #include <plat/omap-serial.h>
45 
46 #define OMAP_UART_AUTOSUSPEND_DELAY -1
47 
48 static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
49 
50 /* Forward declaration of functions */
51 static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
52 static void serial_omap_rx_timeout(unsigned long uart_no);
53 static int serial_omap_start_rxdma(struct uart_omap_port *up);
54 static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
55 
56 static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
57 {
58 	offset <<= up->port.regshift;
59 	return readw(up->port.membase + offset);
60 }
61 
62 static inline void serial_out(struct uart_omap_port *up, int offset, int value)
63 {
64 	offset <<= up->port.regshift;
65 	writew(value, up->port.membase + offset);
66 }
67 
68 static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
69 {
70 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
71 	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
72 		       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
73 	serial_out(up, UART_FCR, 0);
74 }
75 
76 /*
77  * serial_omap_get_divisor - calculate divisor value
78  * @port: uart port info
79  * @baud: baudrate for which divisor needs to be calculated.
80  *
81  * We have written our own function to get the divisor so as to support
82  * 13x mode. 3Mbps Baudrate as an different divisor.
83  * Reference OMAP TRM Chapter 17:
84  * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
85  * referring to oversampling - divisor value
86  * baudrate 460,800 to 3,686,400 all have divisor 13
87  * except 3,000,000 which has divisor value 16
88  */
89 static unsigned int
90 serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
91 {
92 	unsigned int divisor;
93 
94 	if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
95 		divisor = 13;
96 	else
97 		divisor = 16;
98 	return port->uartclk/(baud * divisor);
99 }
100 
101 static void serial_omap_stop_rxdma(struct uart_omap_port *up)
102 {
103 	if (up->uart_dma.rx_dma_used) {
104 		del_timer(&up->uart_dma.rx_timer);
105 		omap_stop_dma(up->uart_dma.rx_dma_channel);
106 		omap_free_dma(up->uart_dma.rx_dma_channel);
107 		up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
108 		up->uart_dma.rx_dma_used = false;
109 		pm_runtime_mark_last_busy(&up->pdev->dev);
110 		pm_runtime_put_autosuspend(&up->pdev->dev);
111 	}
112 }
113 
114 static void serial_omap_enable_ms(struct uart_port *port)
115 {
116 	struct uart_omap_port *up = (struct uart_omap_port *)port;
117 
118 	dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->pdev->id);
119 
120 	pm_runtime_get_sync(&up->pdev->dev);
121 	up->ier |= UART_IER_MSI;
122 	serial_out(up, UART_IER, up->ier);
123 	pm_runtime_put(&up->pdev->dev);
124 }
125 
126 static void serial_omap_stop_tx(struct uart_port *port)
127 {
128 	struct uart_omap_port *up = (struct uart_omap_port *)port;
129 
130 	if (up->use_dma &&
131 		up->uart_dma.tx_dma_channel != OMAP_UART_DMA_CH_FREE) {
132 		/*
133 		 * Check if dma is still active. If yes do nothing,
134 		 * return. Else stop dma
135 		 */
136 		if (omap_get_dma_active_status(up->uart_dma.tx_dma_channel))
137 			return;
138 		omap_stop_dma(up->uart_dma.tx_dma_channel);
139 		omap_free_dma(up->uart_dma.tx_dma_channel);
140 		up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
141 		pm_runtime_mark_last_busy(&up->pdev->dev);
142 		pm_runtime_put_autosuspend(&up->pdev->dev);
143 	}
144 
145 	pm_runtime_get_sync(&up->pdev->dev);
146 	if (up->ier & UART_IER_THRI) {
147 		up->ier &= ~UART_IER_THRI;
148 		serial_out(up, UART_IER, up->ier);
149 	}
150 
151 	pm_runtime_mark_last_busy(&up->pdev->dev);
152 	pm_runtime_put_autosuspend(&up->pdev->dev);
153 }
154 
155 static void serial_omap_stop_rx(struct uart_port *port)
156 {
157 	struct uart_omap_port *up = (struct uart_omap_port *)port;
158 
159 	pm_runtime_get_sync(&up->pdev->dev);
160 	if (up->use_dma)
161 		serial_omap_stop_rxdma(up);
162 	up->ier &= ~UART_IER_RLSI;
163 	up->port.read_status_mask &= ~UART_LSR_DR;
164 	serial_out(up, UART_IER, up->ier);
165 	pm_runtime_mark_last_busy(&up->pdev->dev);
166 	pm_runtime_put_autosuspend(&up->pdev->dev);
167 }
168 
169 static inline void receive_chars(struct uart_omap_port *up, int *status)
170 {
171 	struct tty_struct *tty = up->port.state->port.tty;
172 	unsigned int flag;
173 	unsigned char ch, lsr = *status;
174 	int max_count = 256;
175 
176 	do {
177 		if (likely(lsr & UART_LSR_DR))
178 			ch = serial_in(up, UART_RX);
179 		flag = TTY_NORMAL;
180 		up->port.icount.rx++;
181 
182 		if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
183 			/*
184 			 * For statistics only
185 			 */
186 			if (lsr & UART_LSR_BI) {
187 				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
188 				up->port.icount.brk++;
189 				/*
190 				 * We do the SysRQ and SAK checking
191 				 * here because otherwise the break
192 				 * may get masked by ignore_status_mask
193 				 * or read_status_mask.
194 				 */
195 				if (uart_handle_break(&up->port))
196 					goto ignore_char;
197 			} else if (lsr & UART_LSR_PE) {
198 				up->port.icount.parity++;
199 			} else if (lsr & UART_LSR_FE) {
200 				up->port.icount.frame++;
201 			}
202 
203 			if (lsr & UART_LSR_OE)
204 				up->port.icount.overrun++;
205 
206 			/*
207 			 * Mask off conditions which should be ignored.
208 			 */
209 			lsr &= up->port.read_status_mask;
210 
211 #ifdef CONFIG_SERIAL_OMAP_CONSOLE
212 			if (up->port.line == up->port.cons->index) {
213 				/* Recover the break flag from console xmit */
214 				lsr |= up->lsr_break_flag;
215 			}
216 #endif
217 			if (lsr & UART_LSR_BI)
218 				flag = TTY_BREAK;
219 			else if (lsr & UART_LSR_PE)
220 				flag = TTY_PARITY;
221 			else if (lsr & UART_LSR_FE)
222 				flag = TTY_FRAME;
223 		}
224 
225 		if (uart_handle_sysrq_char(&up->port, ch))
226 			goto ignore_char;
227 		uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
228 ignore_char:
229 		lsr = serial_in(up, UART_LSR);
230 	} while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
231 	spin_unlock(&up->port.lock);
232 	tty_flip_buffer_push(tty);
233 	spin_lock(&up->port.lock);
234 }
235 
236 static void transmit_chars(struct uart_omap_port *up)
237 {
238 	struct circ_buf *xmit = &up->port.state->xmit;
239 	int count;
240 
241 	if (up->port.x_char) {
242 		serial_out(up, UART_TX, up->port.x_char);
243 		up->port.icount.tx++;
244 		up->port.x_char = 0;
245 		return;
246 	}
247 	if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
248 		serial_omap_stop_tx(&up->port);
249 		return;
250 	}
251 	count = up->port.fifosize / 4;
252 	do {
253 		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
254 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
255 		up->port.icount.tx++;
256 		if (uart_circ_empty(xmit))
257 			break;
258 	} while (--count > 0);
259 
260 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
261 		uart_write_wakeup(&up->port);
262 
263 	if (uart_circ_empty(xmit))
264 		serial_omap_stop_tx(&up->port);
265 }
266 
267 static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
268 {
269 	if (!(up->ier & UART_IER_THRI)) {
270 		up->ier |= UART_IER_THRI;
271 		serial_out(up, UART_IER, up->ier);
272 	}
273 }
274 
275 static void serial_omap_start_tx(struct uart_port *port)
276 {
277 	struct uart_omap_port *up = (struct uart_omap_port *)port;
278 	struct circ_buf *xmit;
279 	unsigned int start;
280 	int ret = 0;
281 
282 	if (!up->use_dma) {
283 		pm_runtime_get_sync(&up->pdev->dev);
284 		serial_omap_enable_ier_thri(up);
285 		pm_runtime_mark_last_busy(&up->pdev->dev);
286 		pm_runtime_put_autosuspend(&up->pdev->dev);
287 		return;
288 	}
289 
290 	if (up->uart_dma.tx_dma_used)
291 		return;
292 
293 	xmit = &up->port.state->xmit;
294 
295 	if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
296 		pm_runtime_get_sync(&up->pdev->dev);
297 		ret = omap_request_dma(up->uart_dma.uart_dma_tx,
298 				"UART Tx DMA",
299 				(void *)uart_tx_dma_callback, up,
300 				&(up->uart_dma.tx_dma_channel));
301 
302 		if (ret < 0) {
303 			serial_omap_enable_ier_thri(up);
304 			return;
305 		}
306 	}
307 	spin_lock(&(up->uart_dma.tx_lock));
308 	up->uart_dma.tx_dma_used = true;
309 	spin_unlock(&(up->uart_dma.tx_lock));
310 
311 	start = up->uart_dma.tx_buf_dma_phys +
312 				(xmit->tail & (UART_XMIT_SIZE - 1));
313 
314 	up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
315 	/*
316 	 * It is a circular buffer. See if the buffer has wounded back.
317 	 * If yes it will have to be transferred in two separate dma
318 	 * transfers
319 	 */
320 	if (start + up->uart_dma.tx_buf_size >=
321 			up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
322 		up->uart_dma.tx_buf_size =
323 			(up->uart_dma.tx_buf_dma_phys +
324 			UART_XMIT_SIZE) - start;
325 
326 	omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
327 				OMAP_DMA_AMODE_CONSTANT,
328 				up->uart_dma.uart_base, 0, 0);
329 	omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
330 				OMAP_DMA_AMODE_POST_INC, start, 0, 0);
331 	omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
332 				OMAP_DMA_DATA_TYPE_S8,
333 				up->uart_dma.tx_buf_size, 1,
334 				OMAP_DMA_SYNC_ELEMENT,
335 				up->uart_dma.uart_dma_tx, 0);
336 	/* FIXME: Cache maintenance needed here? */
337 	omap_start_dma(up->uart_dma.tx_dma_channel);
338 }
339 
340 static unsigned int check_modem_status(struct uart_omap_port *up)
341 {
342 	unsigned int status;
343 
344 	status = serial_in(up, UART_MSR);
345 	status |= up->msr_saved_flags;
346 	up->msr_saved_flags = 0;
347 	if ((status & UART_MSR_ANY_DELTA) == 0)
348 		return status;
349 
350 	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
351 	    up->port.state != NULL) {
352 		if (status & UART_MSR_TERI)
353 			up->port.icount.rng++;
354 		if (status & UART_MSR_DDSR)
355 			up->port.icount.dsr++;
356 		if (status & UART_MSR_DDCD)
357 			uart_handle_dcd_change
358 				(&up->port, status & UART_MSR_DCD);
359 		if (status & UART_MSR_DCTS)
360 			uart_handle_cts_change
361 				(&up->port, status & UART_MSR_CTS);
362 		wake_up_interruptible(&up->port.state->port.delta_msr_wait);
363 	}
364 
365 	return status;
366 }
367 
368 /**
369  * serial_omap_irq() - This handles the interrupt from one port
370  * @irq: uart port irq number
371  * @dev_id: uart port info
372  */
373 static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
374 {
375 	struct uart_omap_port *up = dev_id;
376 	unsigned int iir, lsr;
377 	unsigned long flags;
378 
379 	pm_runtime_get_sync(&up->pdev->dev);
380 	iir = serial_in(up, UART_IIR);
381 	if (iir & UART_IIR_NO_INT) {
382 		pm_runtime_mark_last_busy(&up->pdev->dev);
383 		pm_runtime_put_autosuspend(&up->pdev->dev);
384 		return IRQ_NONE;
385 	}
386 
387 	spin_lock_irqsave(&up->port.lock, flags);
388 	lsr = serial_in(up, UART_LSR);
389 	if (iir & UART_IIR_RLSI) {
390 		if (!up->use_dma) {
391 			if (lsr & UART_LSR_DR)
392 				receive_chars(up, &lsr);
393 		} else {
394 			up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
395 			serial_out(up, UART_IER, up->ier);
396 			if ((serial_omap_start_rxdma(up) != 0) &&
397 					(lsr & UART_LSR_DR))
398 				receive_chars(up, &lsr);
399 		}
400 	}
401 
402 	check_modem_status(up);
403 	if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
404 		transmit_chars(up);
405 
406 	spin_unlock_irqrestore(&up->port.lock, flags);
407 	pm_runtime_mark_last_busy(&up->pdev->dev);
408 	pm_runtime_put_autosuspend(&up->pdev->dev);
409 
410 	up->port_activity = jiffies;
411 	return IRQ_HANDLED;
412 }
413 
414 static unsigned int serial_omap_tx_empty(struct uart_port *port)
415 {
416 	struct uart_omap_port *up = (struct uart_omap_port *)port;
417 	unsigned long flags = 0;
418 	unsigned int ret = 0;
419 
420 	pm_runtime_get_sync(&up->pdev->dev);
421 	dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->pdev->id);
422 	spin_lock_irqsave(&up->port.lock, flags);
423 	ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
424 	spin_unlock_irqrestore(&up->port.lock, flags);
425 	pm_runtime_put(&up->pdev->dev);
426 	return ret;
427 }
428 
429 static unsigned int serial_omap_get_mctrl(struct uart_port *port)
430 {
431 	struct uart_omap_port *up = (struct uart_omap_port *)port;
432 	unsigned char status;
433 	unsigned int ret = 0;
434 
435 	pm_runtime_get_sync(&up->pdev->dev);
436 	status = check_modem_status(up);
437 	pm_runtime_put(&up->pdev->dev);
438 
439 	dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->pdev->id);
440 
441 	if (status & UART_MSR_DCD)
442 		ret |= TIOCM_CAR;
443 	if (status & UART_MSR_RI)
444 		ret |= TIOCM_RNG;
445 	if (status & UART_MSR_DSR)
446 		ret |= TIOCM_DSR;
447 	if (status & UART_MSR_CTS)
448 		ret |= TIOCM_CTS;
449 	return ret;
450 }
451 
452 static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
453 {
454 	struct uart_omap_port *up = (struct uart_omap_port *)port;
455 	unsigned char mcr = 0;
456 
457 	dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->pdev->id);
458 	if (mctrl & TIOCM_RTS)
459 		mcr |= UART_MCR_RTS;
460 	if (mctrl & TIOCM_DTR)
461 		mcr |= UART_MCR_DTR;
462 	if (mctrl & TIOCM_OUT1)
463 		mcr |= UART_MCR_OUT1;
464 	if (mctrl & TIOCM_OUT2)
465 		mcr |= UART_MCR_OUT2;
466 	if (mctrl & TIOCM_LOOP)
467 		mcr |= UART_MCR_LOOP;
468 
469 	pm_runtime_get_sync(&up->pdev->dev);
470 	up->mcr = serial_in(up, UART_MCR);
471 	up->mcr |= mcr;
472 	serial_out(up, UART_MCR, up->mcr);
473 	pm_runtime_put(&up->pdev->dev);
474 }
475 
476 static void serial_omap_break_ctl(struct uart_port *port, int break_state)
477 {
478 	struct uart_omap_port *up = (struct uart_omap_port *)port;
479 	unsigned long flags = 0;
480 
481 	dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
482 	pm_runtime_get_sync(&up->pdev->dev);
483 	spin_lock_irqsave(&up->port.lock, flags);
484 	if (break_state == -1)
485 		up->lcr |= UART_LCR_SBC;
486 	else
487 		up->lcr &= ~UART_LCR_SBC;
488 	serial_out(up, UART_LCR, up->lcr);
489 	spin_unlock_irqrestore(&up->port.lock, flags);
490 	pm_runtime_put(&up->pdev->dev);
491 }
492 
493 static int serial_omap_startup(struct uart_port *port)
494 {
495 	struct uart_omap_port *up = (struct uart_omap_port *)port;
496 	unsigned long flags = 0;
497 	int retval;
498 
499 	/*
500 	 * Allocate the IRQ
501 	 */
502 	retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
503 				up->name, up);
504 	if (retval)
505 		return retval;
506 
507 	dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
508 
509 	pm_runtime_get_sync(&up->pdev->dev);
510 	/*
511 	 * Clear the FIFO buffers and disable them.
512 	 * (they will be reenabled in set_termios())
513 	 */
514 	serial_omap_clear_fifos(up);
515 	/* For Hardware flow control */
516 	serial_out(up, UART_MCR, UART_MCR_RTS);
517 
518 	/*
519 	 * Clear the interrupt registers.
520 	 */
521 	(void) serial_in(up, UART_LSR);
522 	if (serial_in(up, UART_LSR) & UART_LSR_DR)
523 		(void) serial_in(up, UART_RX);
524 	(void) serial_in(up, UART_IIR);
525 	(void) serial_in(up, UART_MSR);
526 
527 	/*
528 	 * Now, initialize the UART
529 	 */
530 	serial_out(up, UART_LCR, UART_LCR_WLEN8);
531 	spin_lock_irqsave(&up->port.lock, flags);
532 	/*
533 	 * Most PC uarts need OUT2 raised to enable interrupts.
534 	 */
535 	up->port.mctrl |= TIOCM_OUT2;
536 	serial_omap_set_mctrl(&up->port, up->port.mctrl);
537 	spin_unlock_irqrestore(&up->port.lock, flags);
538 
539 	up->msr_saved_flags = 0;
540 	if (up->use_dma) {
541 		free_page((unsigned long)up->port.state->xmit.buf);
542 		up->port.state->xmit.buf = dma_alloc_coherent(NULL,
543 			UART_XMIT_SIZE,
544 			(dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
545 			0);
546 		init_timer(&(up->uart_dma.rx_timer));
547 		up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
548 		up->uart_dma.rx_timer.data = up->pdev->id;
549 		/* Currently the buffer size is 4KB. Can increase it */
550 		up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
551 			up->uart_dma.rx_buf_size,
552 			(dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
553 	}
554 	/*
555 	 * Finally, enable interrupts. Note: Modem status interrupts
556 	 * are set via set_termios(), which will be occurring imminently
557 	 * anyway, so we don't enable them here.
558 	 */
559 	up->ier = UART_IER_RLSI | UART_IER_RDI;
560 	serial_out(up, UART_IER, up->ier);
561 
562 	/* Enable module level wake up */
563 	serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
564 
565 	pm_runtime_mark_last_busy(&up->pdev->dev);
566 	pm_runtime_put_autosuspend(&up->pdev->dev);
567 	up->port_activity = jiffies;
568 	return 0;
569 }
570 
571 static void serial_omap_shutdown(struct uart_port *port)
572 {
573 	struct uart_omap_port *up = (struct uart_omap_port *)port;
574 	unsigned long flags = 0;
575 
576 	dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
577 
578 	pm_runtime_get_sync(&up->pdev->dev);
579 	/*
580 	 * Disable interrupts from this port
581 	 */
582 	up->ier = 0;
583 	serial_out(up, UART_IER, 0);
584 
585 	spin_lock_irqsave(&up->port.lock, flags);
586 	up->port.mctrl &= ~TIOCM_OUT2;
587 	serial_omap_set_mctrl(&up->port, up->port.mctrl);
588 	spin_unlock_irqrestore(&up->port.lock, flags);
589 
590 	/*
591 	 * Disable break condition and FIFOs
592 	 */
593 	serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
594 	serial_omap_clear_fifos(up);
595 
596 	/*
597 	 * Read data port to reset things, and then free the irq
598 	 */
599 	if (serial_in(up, UART_LSR) & UART_LSR_DR)
600 		(void) serial_in(up, UART_RX);
601 	if (up->use_dma) {
602 		dma_free_coherent(up->port.dev,
603 			UART_XMIT_SIZE,	up->port.state->xmit.buf,
604 			up->uart_dma.tx_buf_dma_phys);
605 		up->port.state->xmit.buf = NULL;
606 		serial_omap_stop_rx(port);
607 		dma_free_coherent(up->port.dev,
608 			up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
609 			up->uart_dma.rx_buf_dma_phys);
610 		up->uart_dma.rx_buf = NULL;
611 	}
612 
613 	pm_runtime_put(&up->pdev->dev);
614 	free_irq(up->port.irq, up);
615 }
616 
617 static inline void
618 serial_omap_configure_xonxoff
619 		(struct uart_omap_port *up, struct ktermios *termios)
620 {
621 	up->lcr = serial_in(up, UART_LCR);
622 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
623 	up->efr = serial_in(up, UART_EFR);
624 	serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
625 
626 	serial_out(up, UART_XON1, termios->c_cc[VSTART]);
627 	serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
628 
629 	/* clear SW control mode bits */
630 	up->efr &= OMAP_UART_SW_CLR;
631 
632 	/*
633 	 * IXON Flag:
634 	 * Enable XON/XOFF flow control on output.
635 	 * Transmit XON1, XOFF1
636 	 */
637 	if (termios->c_iflag & IXON)
638 		up->efr |= OMAP_UART_SW_TX;
639 
640 	/*
641 	 * IXOFF Flag:
642 	 * Enable XON/XOFF flow control on input.
643 	 * Receiver compares XON1, XOFF1.
644 	 */
645 	if (termios->c_iflag & IXOFF)
646 		up->efr |= OMAP_UART_SW_RX;
647 
648 	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
649 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
650 
651 	up->mcr = serial_in(up, UART_MCR);
652 
653 	/*
654 	 * IXANY Flag:
655 	 * Enable any character to restart output.
656 	 * Operation resumes after receiving any
657 	 * character after recognition of the XOFF character
658 	 */
659 	if (termios->c_iflag & IXANY)
660 		up->mcr |= UART_MCR_XONANY;
661 
662 	serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
663 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
664 	serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
665 	/* Enable special char function UARTi.EFR_REG[5] and
666 	 * load the new software flow control mode IXON or IXOFF
667 	 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
668 	 */
669 	serial_out(up, UART_EFR, up->efr | UART_EFR_SCD);
670 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
671 
672 	serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
673 	serial_out(up, UART_LCR, up->lcr);
674 }
675 
676 static void
677 serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
678 			struct ktermios *old)
679 {
680 	struct uart_omap_port *up = (struct uart_omap_port *)port;
681 	unsigned char cval = 0;
682 	unsigned char efr = 0;
683 	unsigned long flags = 0;
684 	unsigned int baud, quot;
685 
686 	switch (termios->c_cflag & CSIZE) {
687 	case CS5:
688 		cval = UART_LCR_WLEN5;
689 		break;
690 	case CS6:
691 		cval = UART_LCR_WLEN6;
692 		break;
693 	case CS7:
694 		cval = UART_LCR_WLEN7;
695 		break;
696 	default:
697 	case CS8:
698 		cval = UART_LCR_WLEN8;
699 		break;
700 	}
701 
702 	if (termios->c_cflag & CSTOPB)
703 		cval |= UART_LCR_STOP;
704 	if (termios->c_cflag & PARENB)
705 		cval |= UART_LCR_PARITY;
706 	if (!(termios->c_cflag & PARODD))
707 		cval |= UART_LCR_EPAR;
708 
709 	/*
710 	 * Ask the core to calculate the divisor for us.
711 	 */
712 
713 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
714 	quot = serial_omap_get_divisor(port, baud);
715 
716 	up->dll = quot & 0xff;
717 	up->dlh = quot >> 8;
718 	up->mdr1 = UART_OMAP_MDR1_DISABLE;
719 
720 	up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
721 			UART_FCR_ENABLE_FIFO;
722 	if (up->use_dma)
723 		up->fcr |= UART_FCR_DMA_SELECT;
724 
725 	/*
726 	 * Ok, we're now changing the port state. Do it with
727 	 * interrupts disabled.
728 	 */
729 	pm_runtime_get_sync(&up->pdev->dev);
730 	spin_lock_irqsave(&up->port.lock, flags);
731 
732 	/*
733 	 * Update the per-port timeout.
734 	 */
735 	uart_update_timeout(port, termios->c_cflag, baud);
736 
737 	up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
738 	if (termios->c_iflag & INPCK)
739 		up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
740 	if (termios->c_iflag & (BRKINT | PARMRK))
741 		up->port.read_status_mask |= UART_LSR_BI;
742 
743 	/*
744 	 * Characters to ignore
745 	 */
746 	up->port.ignore_status_mask = 0;
747 	if (termios->c_iflag & IGNPAR)
748 		up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
749 	if (termios->c_iflag & IGNBRK) {
750 		up->port.ignore_status_mask |= UART_LSR_BI;
751 		/*
752 		 * If we're ignoring parity and break indicators,
753 		 * ignore overruns too (for real raw support).
754 		 */
755 		if (termios->c_iflag & IGNPAR)
756 			up->port.ignore_status_mask |= UART_LSR_OE;
757 	}
758 
759 	/*
760 	 * ignore all characters if CREAD is not set
761 	 */
762 	if ((termios->c_cflag & CREAD) == 0)
763 		up->port.ignore_status_mask |= UART_LSR_DR;
764 
765 	/*
766 	 * Modem status interrupts
767 	 */
768 	up->ier &= ~UART_IER_MSI;
769 	if (UART_ENABLE_MS(&up->port, termios->c_cflag))
770 		up->ier |= UART_IER_MSI;
771 	serial_out(up, UART_IER, up->ier);
772 	serial_out(up, UART_LCR, cval);		/* reset DLAB */
773 	up->lcr = cval;
774 	up->scr = OMAP_UART_SCR_TX_EMPTY;
775 
776 	/* FIFOs and DMA Settings */
777 
778 	/* FCR can be changed only when the
779 	 * baud clock is not running
780 	 * DLL_REG and DLH_REG set to 0.
781 	 */
782 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
783 	serial_out(up, UART_DLL, 0);
784 	serial_out(up, UART_DLM, 0);
785 	serial_out(up, UART_LCR, 0);
786 
787 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
788 
789 	up->efr = serial_in(up, UART_EFR);
790 	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
791 
792 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
793 	up->mcr = serial_in(up, UART_MCR);
794 	serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
795 	/* FIFO ENABLE, DMA MODE */
796 	serial_out(up, UART_FCR, up->fcr);
797 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
798 
799 	if (up->use_dma) {
800 		serial_out(up, UART_TI752_TLR, 0);
801 		up->scr |= (UART_FCR_TRIGGER_4 | UART_FCR_TRIGGER_8);
802 	}
803 
804 	serial_out(up, UART_OMAP_SCR, up->scr);
805 
806 	serial_out(up, UART_EFR, up->efr);
807 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
808 	serial_out(up, UART_MCR, up->mcr);
809 
810 	/* Protocol, Baud Rate, and Interrupt Settings */
811 
812 	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
813 		serial_omap_mdr1_errataset(up, up->mdr1);
814 	else
815 		serial_out(up, UART_OMAP_MDR1, up->mdr1);
816 
817 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
818 
819 	up->efr = serial_in(up, UART_EFR);
820 	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
821 
822 	serial_out(up, UART_LCR, 0);
823 	serial_out(up, UART_IER, 0);
824 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
825 
826 	serial_out(up, UART_DLL, up->dll);	/* LS of divisor */
827 	serial_out(up, UART_DLM, up->dlh);	/* MS of divisor */
828 
829 	serial_out(up, UART_LCR, 0);
830 	serial_out(up, UART_IER, up->ier);
831 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
832 
833 	serial_out(up, UART_EFR, up->efr);
834 	serial_out(up, UART_LCR, cval);
835 
836 	if (baud > 230400 && baud != 3000000)
837 		up->mdr1 = UART_OMAP_MDR1_13X_MODE;
838 	else
839 		up->mdr1 = UART_OMAP_MDR1_16X_MODE;
840 
841 	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
842 		serial_omap_mdr1_errataset(up, up->mdr1);
843 	else
844 		serial_out(up, UART_OMAP_MDR1, up->mdr1);
845 
846 	/* Hardware Flow Control Configuration */
847 
848 	if (termios->c_cflag & CRTSCTS) {
849 		efr |= (UART_EFR_CTS | UART_EFR_RTS);
850 		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
851 
852 		up->mcr = serial_in(up, UART_MCR);
853 		serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
854 
855 		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
856 		up->efr = serial_in(up, UART_EFR);
857 		serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
858 
859 		serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
860 		serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
861 		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
862 		serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
863 		serial_out(up, UART_LCR, cval);
864 	}
865 
866 	serial_omap_set_mctrl(&up->port, up->port.mctrl);
867 	/* Software Flow Control Configuration */
868 	serial_omap_configure_xonxoff(up, termios);
869 
870 	spin_unlock_irqrestore(&up->port.lock, flags);
871 	pm_runtime_put(&up->pdev->dev);
872 	dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
873 }
874 
875 static void
876 serial_omap_pm(struct uart_port *port, unsigned int state,
877 	       unsigned int oldstate)
878 {
879 	struct uart_omap_port *up = (struct uart_omap_port *)port;
880 	unsigned char efr;
881 
882 	dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
883 
884 	pm_runtime_get_sync(&up->pdev->dev);
885 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
886 	efr = serial_in(up, UART_EFR);
887 	serial_out(up, UART_EFR, efr | UART_EFR_ECB);
888 	serial_out(up, UART_LCR, 0);
889 
890 	serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
891 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
892 	serial_out(up, UART_EFR, efr);
893 	serial_out(up, UART_LCR, 0);
894 
895 	if (!device_may_wakeup(&up->pdev->dev)) {
896 		if (!state)
897 			pm_runtime_forbid(&up->pdev->dev);
898 		else
899 			pm_runtime_allow(&up->pdev->dev);
900 	}
901 
902 	pm_runtime_put(&up->pdev->dev);
903 }
904 
905 static void serial_omap_release_port(struct uart_port *port)
906 {
907 	dev_dbg(port->dev, "serial_omap_release_port+\n");
908 }
909 
910 static int serial_omap_request_port(struct uart_port *port)
911 {
912 	dev_dbg(port->dev, "serial_omap_request_port+\n");
913 	return 0;
914 }
915 
916 static void serial_omap_config_port(struct uart_port *port, int flags)
917 {
918 	struct uart_omap_port *up = (struct uart_omap_port *)port;
919 
920 	dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
921 							up->pdev->id);
922 	up->port.type = PORT_OMAP;
923 }
924 
925 static int
926 serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
927 {
928 	/* we don't want the core code to modify any port params */
929 	dev_dbg(port->dev, "serial_omap_verify_port+\n");
930 	return -EINVAL;
931 }
932 
933 static const char *
934 serial_omap_type(struct uart_port *port)
935 {
936 	struct uart_omap_port *up = (struct uart_omap_port *)port;
937 
938 	dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
939 	return up->name;
940 }
941 
942 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
943 
944 static inline void wait_for_xmitr(struct uart_omap_port *up)
945 {
946 	unsigned int status, tmout = 10000;
947 
948 	/* Wait up to 10ms for the character(s) to be sent. */
949 	do {
950 		status = serial_in(up, UART_LSR);
951 
952 		if (status & UART_LSR_BI)
953 			up->lsr_break_flag = UART_LSR_BI;
954 
955 		if (--tmout == 0)
956 			break;
957 		udelay(1);
958 	} while ((status & BOTH_EMPTY) != BOTH_EMPTY);
959 
960 	/* Wait up to 1s for flow control if necessary */
961 	if (up->port.flags & UPF_CONS_FLOW) {
962 		tmout = 1000000;
963 		for (tmout = 1000000; tmout; tmout--) {
964 			unsigned int msr = serial_in(up, UART_MSR);
965 
966 			up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
967 			if (msr & UART_MSR_CTS)
968 				break;
969 
970 			udelay(1);
971 		}
972 	}
973 }
974 
975 #ifdef CONFIG_CONSOLE_POLL
976 
977 static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
978 {
979 	struct uart_omap_port *up = (struct uart_omap_port *)port;
980 
981 	pm_runtime_get_sync(&up->pdev->dev);
982 	wait_for_xmitr(up);
983 	serial_out(up, UART_TX, ch);
984 	pm_runtime_put(&up->pdev->dev);
985 }
986 
987 static int serial_omap_poll_get_char(struct uart_port *port)
988 {
989 	struct uart_omap_port *up = (struct uart_omap_port *)port;
990 	unsigned int status;
991 
992 	pm_runtime_get_sync(&up->pdev->dev);
993 	status = serial_in(up, UART_LSR);
994 	if (!(status & UART_LSR_DR))
995 		return NO_POLL_CHAR;
996 
997 	status = serial_in(up, UART_RX);
998 	pm_runtime_put(&up->pdev->dev);
999 	return status;
1000 }
1001 
1002 #endif /* CONFIG_CONSOLE_POLL */
1003 
1004 #ifdef CONFIG_SERIAL_OMAP_CONSOLE
1005 
1006 static struct uart_omap_port *serial_omap_console_ports[4];
1007 
1008 static struct uart_driver serial_omap_reg;
1009 
1010 static void serial_omap_console_putchar(struct uart_port *port, int ch)
1011 {
1012 	struct uart_omap_port *up = (struct uart_omap_port *)port;
1013 
1014 	wait_for_xmitr(up);
1015 	serial_out(up, UART_TX, ch);
1016 }
1017 
1018 static void
1019 serial_omap_console_write(struct console *co, const char *s,
1020 		unsigned int count)
1021 {
1022 	struct uart_omap_port *up = serial_omap_console_ports[co->index];
1023 	unsigned long flags;
1024 	unsigned int ier;
1025 	int locked = 1;
1026 
1027 	pm_runtime_get_sync(&up->pdev->dev);
1028 
1029 	local_irq_save(flags);
1030 	if (up->port.sysrq)
1031 		locked = 0;
1032 	else if (oops_in_progress)
1033 		locked = spin_trylock(&up->port.lock);
1034 	else
1035 		spin_lock(&up->port.lock);
1036 
1037 	/*
1038 	 * First save the IER then disable the interrupts
1039 	 */
1040 	ier = serial_in(up, UART_IER);
1041 	serial_out(up, UART_IER, 0);
1042 
1043 	uart_console_write(&up->port, s, count, serial_omap_console_putchar);
1044 
1045 	/*
1046 	 * Finally, wait for transmitter to become empty
1047 	 * and restore the IER
1048 	 */
1049 	wait_for_xmitr(up);
1050 	serial_out(up, UART_IER, ier);
1051 	/*
1052 	 * The receive handling will happen properly because the
1053 	 * receive ready bit will still be set; it is not cleared
1054 	 * on read.  However, modem control will not, we must
1055 	 * call it if we have saved something in the saved flags
1056 	 * while processing with interrupts off.
1057 	 */
1058 	if (up->msr_saved_flags)
1059 		check_modem_status(up);
1060 
1061 	pm_runtime_mark_last_busy(&up->pdev->dev);
1062 	pm_runtime_put_autosuspend(&up->pdev->dev);
1063 	if (locked)
1064 		spin_unlock(&up->port.lock);
1065 	local_irq_restore(flags);
1066 }
1067 
1068 static int __init
1069 serial_omap_console_setup(struct console *co, char *options)
1070 {
1071 	struct uart_omap_port *up;
1072 	int baud = 115200;
1073 	int bits = 8;
1074 	int parity = 'n';
1075 	int flow = 'n';
1076 
1077 	if (serial_omap_console_ports[co->index] == NULL)
1078 		return -ENODEV;
1079 	up = serial_omap_console_ports[co->index];
1080 
1081 	if (options)
1082 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1083 
1084 	return uart_set_options(&up->port, co, baud, parity, bits, flow);
1085 }
1086 
1087 static struct console serial_omap_console = {
1088 	.name		= OMAP_SERIAL_NAME,
1089 	.write		= serial_omap_console_write,
1090 	.device		= uart_console_device,
1091 	.setup		= serial_omap_console_setup,
1092 	.flags		= CON_PRINTBUFFER,
1093 	.index		= -1,
1094 	.data		= &serial_omap_reg,
1095 };
1096 
1097 static void serial_omap_add_console_port(struct uart_omap_port *up)
1098 {
1099 	serial_omap_console_ports[up->pdev->id] = up;
1100 }
1101 
1102 #define OMAP_CONSOLE	(&serial_omap_console)
1103 
1104 #else
1105 
1106 #define OMAP_CONSOLE	NULL
1107 
1108 static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1109 {}
1110 
1111 #endif
1112 
1113 static struct uart_ops serial_omap_pops = {
1114 	.tx_empty	= serial_omap_tx_empty,
1115 	.set_mctrl	= serial_omap_set_mctrl,
1116 	.get_mctrl	= serial_omap_get_mctrl,
1117 	.stop_tx	= serial_omap_stop_tx,
1118 	.start_tx	= serial_omap_start_tx,
1119 	.stop_rx	= serial_omap_stop_rx,
1120 	.enable_ms	= serial_omap_enable_ms,
1121 	.break_ctl	= serial_omap_break_ctl,
1122 	.startup	= serial_omap_startup,
1123 	.shutdown	= serial_omap_shutdown,
1124 	.set_termios	= serial_omap_set_termios,
1125 	.pm		= serial_omap_pm,
1126 	.type		= serial_omap_type,
1127 	.release_port	= serial_omap_release_port,
1128 	.request_port	= serial_omap_request_port,
1129 	.config_port	= serial_omap_config_port,
1130 	.verify_port	= serial_omap_verify_port,
1131 #ifdef CONFIG_CONSOLE_POLL
1132 	.poll_put_char  = serial_omap_poll_put_char,
1133 	.poll_get_char  = serial_omap_poll_get_char,
1134 #endif
1135 };
1136 
1137 static struct uart_driver serial_omap_reg = {
1138 	.owner		= THIS_MODULE,
1139 	.driver_name	= "OMAP-SERIAL",
1140 	.dev_name	= OMAP_SERIAL_NAME,
1141 	.nr		= OMAP_MAX_HSUART_PORTS,
1142 	.cons		= OMAP_CONSOLE,
1143 };
1144 
1145 #ifdef CONFIG_SUSPEND
1146 static int serial_omap_suspend(struct device *dev)
1147 {
1148 	struct uart_omap_port *up = dev_get_drvdata(dev);
1149 
1150 	if (up)
1151 		uart_suspend_port(&serial_omap_reg, &up->port);
1152 	return 0;
1153 }
1154 
1155 static int serial_omap_resume(struct device *dev)
1156 {
1157 	struct uart_omap_port *up = dev_get_drvdata(dev);
1158 
1159 	if (up)
1160 		uart_resume_port(&serial_omap_reg, &up->port);
1161 	return 0;
1162 }
1163 #endif
1164 
1165 static void serial_omap_rx_timeout(unsigned long uart_no)
1166 {
1167 	struct uart_omap_port *up = ui[uart_no];
1168 	unsigned int curr_dma_pos, curr_transmitted_size;
1169 	int ret = 0;
1170 
1171 	curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
1172 	if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
1173 			     (curr_dma_pos == 0)) {
1174 		if (jiffies_to_msecs(jiffies - up->port_activity) <
1175 							RX_TIMEOUT) {
1176 			mod_timer(&up->uart_dma.rx_timer, jiffies +
1177 				usecs_to_jiffies(up->uart_dma.rx_timeout));
1178 		} else {
1179 			serial_omap_stop_rxdma(up);
1180 			up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1181 			serial_out(up, UART_IER, up->ier);
1182 		}
1183 		return;
1184 	}
1185 
1186 	curr_transmitted_size = curr_dma_pos -
1187 					up->uart_dma.prev_rx_dma_pos;
1188 	up->port.icount.rx += curr_transmitted_size;
1189 	tty_insert_flip_string(up->port.state->port.tty,
1190 			up->uart_dma.rx_buf +
1191 			(up->uart_dma.prev_rx_dma_pos -
1192 			up->uart_dma.rx_buf_dma_phys),
1193 			curr_transmitted_size);
1194 	tty_flip_buffer_push(up->port.state->port.tty);
1195 	up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
1196 	if (up->uart_dma.rx_buf_size +
1197 			up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
1198 		ret = serial_omap_start_rxdma(up);
1199 		if (ret < 0) {
1200 			serial_omap_stop_rxdma(up);
1201 			up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1202 			serial_out(up, UART_IER, up->ier);
1203 		}
1204 	} else  {
1205 		mod_timer(&up->uart_dma.rx_timer, jiffies +
1206 			usecs_to_jiffies(up->uart_dma.rx_timeout));
1207 	}
1208 	up->port_activity = jiffies;
1209 }
1210 
1211 static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
1212 {
1213 	return;
1214 }
1215 
1216 static int serial_omap_start_rxdma(struct uart_omap_port *up)
1217 {
1218 	int ret = 0;
1219 
1220 	if (up->uart_dma.rx_dma_channel == -1) {
1221 		pm_runtime_get_sync(&up->pdev->dev);
1222 		ret = omap_request_dma(up->uart_dma.uart_dma_rx,
1223 				"UART Rx DMA",
1224 				(void *)uart_rx_dma_callback, up,
1225 				&(up->uart_dma.rx_dma_channel));
1226 		if (ret < 0)
1227 			return ret;
1228 
1229 		omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
1230 				OMAP_DMA_AMODE_CONSTANT,
1231 				up->uart_dma.uart_base, 0, 0);
1232 		omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
1233 				OMAP_DMA_AMODE_POST_INC,
1234 				up->uart_dma.rx_buf_dma_phys, 0, 0);
1235 		omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
1236 				OMAP_DMA_DATA_TYPE_S8,
1237 				up->uart_dma.rx_buf_size, 1,
1238 				OMAP_DMA_SYNC_ELEMENT,
1239 				up->uart_dma.uart_dma_rx, 0);
1240 	}
1241 	up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
1242 	/* FIXME: Cache maintenance needed here? */
1243 	omap_start_dma(up->uart_dma.rx_dma_channel);
1244 	mod_timer(&up->uart_dma.rx_timer, jiffies +
1245 				usecs_to_jiffies(up->uart_dma.rx_timeout));
1246 	up->uart_dma.rx_dma_used = true;
1247 	return ret;
1248 }
1249 
1250 static void serial_omap_continue_tx(struct uart_omap_port *up)
1251 {
1252 	struct circ_buf *xmit = &up->port.state->xmit;
1253 	unsigned int start = up->uart_dma.tx_buf_dma_phys
1254 			+ (xmit->tail & (UART_XMIT_SIZE - 1));
1255 
1256 	if (uart_circ_empty(xmit))
1257 		return;
1258 
1259 	up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
1260 	/*
1261 	 * It is a circular buffer. See if the buffer has wounded back.
1262 	 * If yes it will have to be transferred in two separate dma
1263 	 * transfers
1264 	 */
1265 	if (start + up->uart_dma.tx_buf_size >=
1266 			up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
1267 		up->uart_dma.tx_buf_size =
1268 			(up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
1269 	omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
1270 				OMAP_DMA_AMODE_CONSTANT,
1271 				up->uart_dma.uart_base, 0, 0);
1272 	omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
1273 				OMAP_DMA_AMODE_POST_INC, start, 0, 0);
1274 	omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
1275 				OMAP_DMA_DATA_TYPE_S8,
1276 				up->uart_dma.tx_buf_size, 1,
1277 				OMAP_DMA_SYNC_ELEMENT,
1278 				up->uart_dma.uart_dma_tx, 0);
1279 	/* FIXME: Cache maintenance needed here? */
1280 	omap_start_dma(up->uart_dma.tx_dma_channel);
1281 }
1282 
1283 static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1284 {
1285 	struct uart_omap_port *up = (struct uart_omap_port *)data;
1286 	struct circ_buf *xmit = &up->port.state->xmit;
1287 
1288 	xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
1289 			(UART_XMIT_SIZE - 1);
1290 	up->port.icount.tx += up->uart_dma.tx_buf_size;
1291 
1292 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1293 		uart_write_wakeup(&up->port);
1294 
1295 	if (uart_circ_empty(xmit)) {
1296 		spin_lock(&(up->uart_dma.tx_lock));
1297 		serial_omap_stop_tx(&up->port);
1298 		up->uart_dma.tx_dma_used = false;
1299 		spin_unlock(&(up->uart_dma.tx_lock));
1300 	} else {
1301 		omap_stop_dma(up->uart_dma.tx_dma_channel);
1302 		serial_omap_continue_tx(up);
1303 	}
1304 	up->port_activity = jiffies;
1305 	return;
1306 }
1307 
1308 static int serial_omap_probe(struct platform_device *pdev)
1309 {
1310 	struct uart_omap_port	*up;
1311 	struct resource		*mem, *irq, *dma_tx, *dma_rx;
1312 	struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1313 	int ret = -ENOSPC;
1314 
1315 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1316 	if (!mem) {
1317 		dev_err(&pdev->dev, "no mem resource?\n");
1318 		return -ENODEV;
1319 	}
1320 
1321 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1322 	if (!irq) {
1323 		dev_err(&pdev->dev, "no irq resource?\n");
1324 		return -ENODEV;
1325 	}
1326 
1327 	if (!request_mem_region(mem->start, resource_size(mem),
1328 				pdev->dev.driver->name)) {
1329 		dev_err(&pdev->dev, "memory region already claimed\n");
1330 		return -EBUSY;
1331 	}
1332 
1333 	dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1334 	if (!dma_rx) {
1335 		ret = -EINVAL;
1336 		goto err;
1337 	}
1338 
1339 	dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1340 	if (!dma_tx) {
1341 		ret = -EINVAL;
1342 		goto err;
1343 	}
1344 
1345 	up = kzalloc(sizeof(*up), GFP_KERNEL);
1346 	if (up == NULL) {
1347 		ret = -ENOMEM;
1348 		goto do_release_region;
1349 	}
1350 	sprintf(up->name, "OMAP UART%d", pdev->id);
1351 	up->pdev = pdev;
1352 	up->port.dev = &pdev->dev;
1353 	up->port.type = PORT_OMAP;
1354 	up->port.iotype = UPIO_MEM;
1355 	up->port.irq = irq->start;
1356 
1357 	up->port.regshift = 2;
1358 	up->port.fifosize = 64;
1359 	up->port.ops = &serial_omap_pops;
1360 	up->port.line = pdev->id;
1361 
1362 	up->port.mapbase = mem->start;
1363 	up->port.membase = ioremap(mem->start, resource_size(mem));
1364 	if (!up->port.membase) {
1365 		dev_err(&pdev->dev, "can't ioremap UART\n");
1366 		ret = -ENOMEM;
1367 		goto err;
1368 	}
1369 
1370 	up->port.flags = omap_up_info->flags;
1371 	up->port.uartclk = omap_up_info->uartclk;
1372 	up->uart_dma.uart_base = mem->start;
1373 	up->errata = omap_up_info->errata;
1374 
1375 	if (omap_up_info->dma_enabled) {
1376 		up->uart_dma.uart_dma_tx = dma_tx->start;
1377 		up->uart_dma.uart_dma_rx = dma_rx->start;
1378 		up->use_dma = 1;
1379 		up->uart_dma.rx_buf_size = 4096;
1380 		up->uart_dma.rx_timeout = 2;
1381 		spin_lock_init(&(up->uart_dma.tx_lock));
1382 		spin_lock_init(&(up->uart_dma.rx_lock));
1383 		up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
1384 		up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
1385 	}
1386 
1387 	pm_runtime_use_autosuspend(&pdev->dev);
1388 	pm_runtime_set_autosuspend_delay(&pdev->dev,
1389 			OMAP_UART_AUTOSUSPEND_DELAY);
1390 
1391 	pm_runtime_irq_safe(&pdev->dev);
1392 	pm_runtime_enable(&pdev->dev);
1393 	pm_runtime_get_sync(&pdev->dev);
1394 
1395 	ui[pdev->id] = up;
1396 	serial_omap_add_console_port(up);
1397 
1398 	ret = uart_add_one_port(&serial_omap_reg, &up->port);
1399 	if (ret != 0)
1400 		goto do_release_region;
1401 
1402 	pm_runtime_put(&pdev->dev);
1403 	platform_set_drvdata(pdev, up);
1404 	return 0;
1405 err:
1406 	dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1407 				pdev->id, __func__, ret);
1408 do_release_region:
1409 	release_mem_region(mem->start, resource_size(mem));
1410 	return ret;
1411 }
1412 
1413 static int serial_omap_remove(struct platform_device *dev)
1414 {
1415 	struct uart_omap_port *up = platform_get_drvdata(dev);
1416 
1417 	if (up) {
1418 		pm_runtime_disable(&up->pdev->dev);
1419 		uart_remove_one_port(&serial_omap_reg, &up->port);
1420 		kfree(up);
1421 	}
1422 
1423 	platform_set_drvdata(dev, NULL);
1424 	return 0;
1425 }
1426 
1427 /*
1428  * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
1429  * The access to uart register after MDR1 Access
1430  * causes UART to corrupt data.
1431  *
1432  * Need a delay =
1433  * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
1434  * give 10 times as much
1435  */
1436 static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
1437 {
1438 	u8 timeout = 255;
1439 
1440 	serial_out(up, UART_OMAP_MDR1, mdr1);
1441 	udelay(2);
1442 	serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
1443 			UART_FCR_CLEAR_RCVR);
1444 	/*
1445 	 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
1446 	 * TX_FIFO_E bit is 1.
1447 	 */
1448 	while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
1449 				(UART_LSR_THRE | UART_LSR_DR))) {
1450 		timeout--;
1451 		if (!timeout) {
1452 			/* Should *never* happen. we warn and carry on */
1453 			dev_crit(&up->pdev->dev, "Errata i202: timedout %x\n",
1454 						serial_in(up, UART_LSR));
1455 			break;
1456 		}
1457 		udelay(1);
1458 	}
1459 }
1460 
1461 static void serial_omap_restore_context(struct uart_omap_port *up)
1462 {
1463 	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1464 		serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
1465 	else
1466 		serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
1467 
1468 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1469 	serial_out(up, UART_EFR, UART_EFR_ECB);
1470 	serial_out(up, UART_LCR, 0x0); /* Operational mode */
1471 	serial_out(up, UART_IER, 0x0);
1472 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1473 	serial_out(up, UART_DLL, up->dll);
1474 	serial_out(up, UART_DLM, up->dlh);
1475 	serial_out(up, UART_LCR, 0x0); /* Operational mode */
1476 	serial_out(up, UART_IER, up->ier);
1477 	serial_out(up, UART_FCR, up->fcr);
1478 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1479 	serial_out(up, UART_MCR, up->mcr);
1480 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1481 	serial_out(up, UART_OMAP_SCR, up->scr);
1482 	serial_out(up, UART_EFR, up->efr);
1483 	serial_out(up, UART_LCR, up->lcr);
1484 	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1485 		serial_omap_mdr1_errataset(up, up->mdr1);
1486 	else
1487 		serial_out(up, UART_OMAP_MDR1, up->mdr1);
1488 }
1489 
1490 #ifdef CONFIG_PM_RUNTIME
1491 static int serial_omap_runtime_suspend(struct device *dev)
1492 {
1493 	struct uart_omap_port *up = dev_get_drvdata(dev);
1494 	struct omap_uart_port_info *pdata = dev->platform_data;
1495 
1496 	if (!up)
1497 		return -EINVAL;
1498 
1499 	if (!pdata->enable_wakeup)
1500 		return 0;
1501 
1502 	if (pdata->get_context_loss_count)
1503 		up->context_loss_cnt = pdata->get_context_loss_count(dev);
1504 
1505 	if (device_may_wakeup(dev)) {
1506 		if (!up->wakeups_enabled) {
1507 			pdata->enable_wakeup(up->pdev, true);
1508 			up->wakeups_enabled = true;
1509 		}
1510 	} else {
1511 		if (up->wakeups_enabled) {
1512 			pdata->enable_wakeup(up->pdev, false);
1513 			up->wakeups_enabled = false;
1514 		}
1515 	}
1516 
1517 	/* Errata i291 */
1518 	if (up->use_dma && pdata->set_forceidle &&
1519 			(up->errata & UART_ERRATA_i291_DMA_FORCEIDLE))
1520 		pdata->set_forceidle(up->pdev);
1521 
1522 	return 0;
1523 }
1524 
1525 static int serial_omap_runtime_resume(struct device *dev)
1526 {
1527 	struct uart_omap_port *up = dev_get_drvdata(dev);
1528 	struct omap_uart_port_info *pdata = dev->platform_data;
1529 
1530 	if (up) {
1531 		if (pdata->get_context_loss_count) {
1532 			u32 loss_cnt = pdata->get_context_loss_count(dev);
1533 
1534 			if (up->context_loss_cnt != loss_cnt)
1535 				serial_omap_restore_context(up);
1536 		}
1537 
1538 		/* Errata i291 */
1539 		if (up->use_dma && pdata->set_noidle &&
1540 				(up->errata & UART_ERRATA_i291_DMA_FORCEIDLE))
1541 			pdata->set_noidle(up->pdev);
1542 	}
1543 
1544 	return 0;
1545 }
1546 #endif
1547 
1548 static const struct dev_pm_ops serial_omap_dev_pm_ops = {
1549 	SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
1550 	SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
1551 				serial_omap_runtime_resume, NULL)
1552 };
1553 
1554 static struct platform_driver serial_omap_driver = {
1555 	.probe          = serial_omap_probe,
1556 	.remove         = serial_omap_remove,
1557 	.driver		= {
1558 		.name	= DRIVER_NAME,
1559 		.pm	= &serial_omap_dev_pm_ops,
1560 	},
1561 };
1562 
1563 static int __init serial_omap_init(void)
1564 {
1565 	int ret;
1566 
1567 	ret = uart_register_driver(&serial_omap_reg);
1568 	if (ret != 0)
1569 		return ret;
1570 	ret = platform_driver_register(&serial_omap_driver);
1571 	if (ret != 0)
1572 		uart_unregister_driver(&serial_omap_reg);
1573 	return ret;
1574 }
1575 
1576 static void __exit serial_omap_exit(void)
1577 {
1578 	platform_driver_unregister(&serial_omap_driver);
1579 	uart_unregister_driver(&serial_omap_reg);
1580 }
1581 
1582 module_init(serial_omap_init);
1583 module_exit(serial_omap_exit);
1584 
1585 MODULE_DESCRIPTION("OMAP High Speed UART driver");
1586 MODULE_LICENSE("GPL");
1587 MODULE_AUTHOR("Texas Instruments Inc");
1588