blob: a3f5ea46f3455239ed5ff587441e6d7cae0e7847 [file] [log] [blame]
Govindraj.Rb6126332010-09-27 20:20:49 +05301/*
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 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016 * Note: This driver is made separate from 8250 driver as we cannot
Govindraj.Rb6126332010-09-27 20:20:49 +053017 * 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
Thomas Weber364a6ec2011-02-01 08:30:41 +010023#if defined(CONFIG_SERIAL_OMAP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
Govindraj.Rb6126332010-09-27 20:20:49 +053027#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>
Govindraj.Rfcdca752011-02-28 18:12:23 +053040#include <linux/pm_runtime.h>
Rajendra Nayakd92b0df2011-12-14 17:25:45 +053041#include <linux/of.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053042
43#include <plat/dma.h>
44#include <plat/dmtimer.h>
45#include <plat/omap-serial.h>
46
Rajendra Nayak8fe789d2011-12-14 17:25:44 +053047#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz*/
48
Paul Walmsley0ba5f662012-01-25 19:50:36 -070049/* SCR register bitmasks */
50#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
51
52/* FCR register bitmasks */
53#define OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT 6
54#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
55
Govindraj.Rb6126332010-09-27 20:20:49 +053056static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
57
58/* Forward declaration of functions */
59static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
Jon Huntera9e210e2011-11-09 17:34:49 +053060static void serial_omap_rxdma_poll(unsigned long uart_no);
Govindraj.Rb6126332010-09-27 20:20:49 +053061static int serial_omap_start_rxdma(struct uart_omap_port *up);
Govindraj.R94734742011-11-07 19:00:33 +053062static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +053063
Govindraj.R2fd14962011-11-09 17:41:21 +053064static struct workqueue_struct *serial_omap_uart_wq;
Govindraj.Rb6126332010-09-27 20:20:49 +053065
66static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
67{
68 offset <<= up->port.regshift;
69 return readw(up->port.membase + offset);
70}
71
72static inline void serial_out(struct uart_omap_port *up, int offset, int value)
73{
74 offset <<= up->port.regshift;
75 writew(value, up->port.membase + offset);
76}
77
78static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
79{
80 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
81 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
82 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
83 serial_out(up, UART_FCR, 0);
84}
85
86/*
87 * serial_omap_get_divisor - calculate divisor value
88 * @port: uart port info
89 * @baud: baudrate for which divisor needs to be calculated.
90 *
91 * We have written our own function to get the divisor so as to support
92 * 13x mode. 3Mbps Baudrate as an different divisor.
93 * Reference OMAP TRM Chapter 17:
94 * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
95 * referring to oversampling - divisor value
96 * baudrate 460,800 to 3,686,400 all have divisor 13
97 * except 3,000,000 which has divisor value 16
98 */
99static unsigned int
100serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
101{
102 unsigned int divisor;
103
104 if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
105 divisor = 13;
106 else
107 divisor = 16;
108 return port->uartclk/(baud * divisor);
109}
110
111static void serial_omap_stop_rxdma(struct uart_omap_port *up)
112{
113 if (up->uart_dma.rx_dma_used) {
114 del_timer(&up->uart_dma.rx_timer);
115 omap_stop_dma(up->uart_dma.rx_dma_channel);
116 omap_free_dma(up->uart_dma.rx_dma_channel);
117 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
118 up->uart_dma.rx_dma_used = false;
Govindraj.Rfcdca752011-02-28 18:12:23 +0530119 pm_runtime_mark_last_busy(&up->pdev->dev);
120 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530121 }
122}
123
124static void serial_omap_enable_ms(struct uart_port *port)
125{
126 struct uart_omap_port *up = (struct uart_omap_port *)port;
127
Rajendra Nayakba774332011-12-14 17:25:43 +0530128 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530129
130 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530131 up->ier |= UART_IER_MSI;
132 serial_out(up, UART_IER, up->ier);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530133 pm_runtime_put(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530134}
135
136static void serial_omap_stop_tx(struct uart_port *port)
137{
138 struct uart_omap_port *up = (struct uart_omap_port *)port;
139
140 if (up->use_dma &&
141 up->uart_dma.tx_dma_channel != OMAP_UART_DMA_CH_FREE) {
142 /*
143 * Check if dma is still active. If yes do nothing,
144 * return. Else stop dma
145 */
146 if (omap_get_dma_active_status(up->uart_dma.tx_dma_channel))
147 return;
148 omap_stop_dma(up->uart_dma.tx_dma_channel);
149 omap_free_dma(up->uart_dma.tx_dma_channel);
150 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
Govindraj.Rfcdca752011-02-28 18:12:23 +0530151 pm_runtime_mark_last_busy(&up->pdev->dev);
152 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530153 }
154
Govindraj.Rfcdca752011-02-28 18:12:23 +0530155 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530156 if (up->ier & UART_IER_THRI) {
157 up->ier &= ~UART_IER_THRI;
158 serial_out(up, UART_IER, up->ier);
159 }
Govindraj.Rfcdca752011-02-28 18:12:23 +0530160
161 pm_runtime_mark_last_busy(&up->pdev->dev);
162 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530163}
164
165static void serial_omap_stop_rx(struct uart_port *port)
166{
167 struct uart_omap_port *up = (struct uart_omap_port *)port;
168
Govindraj.Rfcdca752011-02-28 18:12:23 +0530169 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530170 if (up->use_dma)
171 serial_omap_stop_rxdma(up);
172 up->ier &= ~UART_IER_RLSI;
173 up->port.read_status_mask &= ~UART_LSR_DR;
174 serial_out(up, UART_IER, up->ier);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530175 pm_runtime_mark_last_busy(&up->pdev->dev);
176 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530177}
178
Govindraj.Rda274682011-12-14 21:24:11 +0530179static inline void receive_chars(struct uart_omap_port *up,
180 unsigned int *status)
Govindraj.Rb6126332010-09-27 20:20:49 +0530181{
182 struct tty_struct *tty = up->port.state->port.tty;
Govindraj.Rda274682011-12-14 21:24:11 +0530183 unsigned int flag, lsr = *status;
184 unsigned char ch = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530185 int max_count = 256;
186
187 do {
188 if (likely(lsr & UART_LSR_DR))
189 ch = serial_in(up, UART_RX);
190 flag = TTY_NORMAL;
191 up->port.icount.rx++;
192
193 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
194 /*
195 * For statistics only
196 */
197 if (lsr & UART_LSR_BI) {
198 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
199 up->port.icount.brk++;
200 /*
201 * We do the SysRQ and SAK checking
202 * here because otherwise the break
203 * may get masked by ignore_status_mask
204 * or read_status_mask.
205 */
206 if (uart_handle_break(&up->port))
207 goto ignore_char;
208 } else if (lsr & UART_LSR_PE) {
209 up->port.icount.parity++;
210 } else if (lsr & UART_LSR_FE) {
211 up->port.icount.frame++;
212 }
213
214 if (lsr & UART_LSR_OE)
215 up->port.icount.overrun++;
216
217 /*
218 * Mask off conditions which should be ignored.
219 */
220 lsr &= up->port.read_status_mask;
221
222#ifdef CONFIG_SERIAL_OMAP_CONSOLE
223 if (up->port.line == up->port.cons->index) {
224 /* Recover the break flag from console xmit */
225 lsr |= up->lsr_break_flag;
Govindraj.Rb6126332010-09-27 20:20:49 +0530226 }
227#endif
228 if (lsr & UART_LSR_BI)
229 flag = TTY_BREAK;
230 else if (lsr & UART_LSR_PE)
231 flag = TTY_PARITY;
232 else if (lsr & UART_LSR_FE)
233 flag = TTY_FRAME;
234 }
235
236 if (uart_handle_sysrq_char(&up->port, ch))
237 goto ignore_char;
238 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
239ignore_char:
240 lsr = serial_in(up, UART_LSR);
241 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
242 spin_unlock(&up->port.lock);
243 tty_flip_buffer_push(tty);
244 spin_lock(&up->port.lock);
245}
246
247static void transmit_chars(struct uart_omap_port *up)
248{
249 struct circ_buf *xmit = &up->port.state->xmit;
250 int count;
251
252 if (up->port.x_char) {
253 serial_out(up, UART_TX, up->port.x_char);
254 up->port.icount.tx++;
255 up->port.x_char = 0;
256 return;
257 }
258 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
259 serial_omap_stop_tx(&up->port);
260 return;
261 }
Greg Kroah-Hartmanaf681ca2012-01-26 11:14:42 -0800262 count = up->port.fifosize / 4;
Govindraj.Rb6126332010-09-27 20:20:49 +0530263 do {
264 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
265 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
266 up->port.icount.tx++;
267 if (uart_circ_empty(xmit))
268 break;
269 } while (--count > 0);
270
271 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
272 uart_write_wakeup(&up->port);
273
274 if (uart_circ_empty(xmit))
275 serial_omap_stop_tx(&up->port);
276}
277
278static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
279{
280 if (!(up->ier & UART_IER_THRI)) {
281 up->ier |= UART_IER_THRI;
282 serial_out(up, UART_IER, up->ier);
283 }
284}
285
286static void serial_omap_start_tx(struct uart_port *port)
287{
288 struct uart_omap_port *up = (struct uart_omap_port *)port;
289 struct circ_buf *xmit;
290 unsigned int start;
291 int ret = 0;
292
293 if (!up->use_dma) {
Govindraj.Rfcdca752011-02-28 18:12:23 +0530294 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530295 serial_omap_enable_ier_thri(up);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530296 pm_runtime_mark_last_busy(&up->pdev->dev);
297 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530298 return;
299 }
300
301 if (up->uart_dma.tx_dma_used)
302 return;
303
304 xmit = &up->port.state->xmit;
305
306 if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
Govindraj.Rfcdca752011-02-28 18:12:23 +0530307 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530308 ret = omap_request_dma(up->uart_dma.uart_dma_tx,
309 "UART Tx DMA",
310 (void *)uart_tx_dma_callback, up,
311 &(up->uart_dma.tx_dma_channel));
312
313 if (ret < 0) {
314 serial_omap_enable_ier_thri(up);
315 return;
316 }
317 }
318 spin_lock(&(up->uart_dma.tx_lock));
319 up->uart_dma.tx_dma_used = true;
320 spin_unlock(&(up->uart_dma.tx_lock));
321
322 start = up->uart_dma.tx_buf_dma_phys +
323 (xmit->tail & (UART_XMIT_SIZE - 1));
324
325 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
326 /*
327 * It is a circular buffer. See if the buffer has wounded back.
328 * If yes it will have to be transferred in two separate dma
329 * transfers
330 */
331 if (start + up->uart_dma.tx_buf_size >=
332 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
333 up->uart_dma.tx_buf_size =
334 (up->uart_dma.tx_buf_dma_phys +
335 UART_XMIT_SIZE) - start;
336
337 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
338 OMAP_DMA_AMODE_CONSTANT,
339 up->uart_dma.uart_base, 0, 0);
340 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
341 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
342 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
343 OMAP_DMA_DATA_TYPE_S8,
344 up->uart_dma.tx_buf_size, 1,
345 OMAP_DMA_SYNC_ELEMENT,
346 up->uart_dma.uart_dma_tx, 0);
347 /* FIXME: Cache maintenance needed here? */
348 omap_start_dma(up->uart_dma.tx_dma_channel);
349}
350
351static unsigned int check_modem_status(struct uart_omap_port *up)
352{
353 unsigned int status;
354
355 status = serial_in(up, UART_MSR);
356 status |= up->msr_saved_flags;
357 up->msr_saved_flags = 0;
358 if ((status & UART_MSR_ANY_DELTA) == 0)
359 return status;
360
361 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
362 up->port.state != NULL) {
363 if (status & UART_MSR_TERI)
364 up->port.icount.rng++;
365 if (status & UART_MSR_DDSR)
366 up->port.icount.dsr++;
367 if (status & UART_MSR_DDCD)
368 uart_handle_dcd_change
369 (&up->port, status & UART_MSR_DCD);
370 if (status & UART_MSR_DCTS)
371 uart_handle_cts_change
372 (&up->port, status & UART_MSR_CTS);
373 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
374 }
375
376 return status;
377}
378
379/**
380 * serial_omap_irq() - This handles the interrupt from one port
381 * @irq: uart port irq number
382 * @dev_id: uart port info
383 */
384static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
385{
386 struct uart_omap_port *up = dev_id;
387 unsigned int iir, lsr;
388 unsigned long flags;
389
Govindraj.Rfcdca752011-02-28 18:12:23 +0530390 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530391 iir = serial_in(up, UART_IIR);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530392 if (iir & UART_IIR_NO_INT) {
393 pm_runtime_mark_last_busy(&up->pdev->dev);
394 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530395 return IRQ_NONE;
Govindraj.Rfcdca752011-02-28 18:12:23 +0530396 }
Govindraj.Rb6126332010-09-27 20:20:49 +0530397
398 spin_lock_irqsave(&up->port.lock, flags);
399 lsr = serial_in(up, UART_LSR);
400 if (iir & UART_IIR_RLSI) {
401 if (!up->use_dma) {
402 if (lsr & UART_LSR_DR)
403 receive_chars(up, &lsr);
404 } else {
405 up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
406 serial_out(up, UART_IER, up->ier);
407 if ((serial_omap_start_rxdma(up) != 0) &&
408 (lsr & UART_LSR_DR))
409 receive_chars(up, &lsr);
410 }
411 }
412
413 check_modem_status(up);
414 if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
415 transmit_chars(up);
416
417 spin_unlock_irqrestore(&up->port.lock, flags);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530418 pm_runtime_mark_last_busy(&up->pdev->dev);
419 pm_runtime_put_autosuspend(&up->pdev->dev);
420
Govindraj.Rb6126332010-09-27 20:20:49 +0530421 up->port_activity = jiffies;
422 return IRQ_HANDLED;
423}
424
425static unsigned int serial_omap_tx_empty(struct uart_port *port)
426{
427 struct uart_omap_port *up = (struct uart_omap_port *)port;
428 unsigned long flags = 0;
429 unsigned int ret = 0;
430
Govindraj.Rfcdca752011-02-28 18:12:23 +0530431 pm_runtime_get_sync(&up->pdev->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +0530432 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530433 spin_lock_irqsave(&up->port.lock, flags);
434 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
435 spin_unlock_irqrestore(&up->port.lock, flags);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530436 pm_runtime_put(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530437 return ret;
438}
439
440static unsigned int serial_omap_get_mctrl(struct uart_port *port)
441{
442 struct uart_omap_port *up = (struct uart_omap_port *)port;
Shubhrajyoti D514f31d2011-11-21 15:43:28 +0530443 unsigned int status;
Govindraj.Rb6126332010-09-27 20:20:49 +0530444 unsigned int ret = 0;
445
Govindraj.Rfcdca752011-02-28 18:12:23 +0530446 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530447 status = check_modem_status(up);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530448 pm_runtime_put(&up->pdev->dev);
449
Rajendra Nayakba774332011-12-14 17:25:43 +0530450 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530451
452 if (status & UART_MSR_DCD)
453 ret |= TIOCM_CAR;
454 if (status & UART_MSR_RI)
455 ret |= TIOCM_RNG;
456 if (status & UART_MSR_DSR)
457 ret |= TIOCM_DSR;
458 if (status & UART_MSR_CTS)
459 ret |= TIOCM_CTS;
460 return ret;
461}
462
463static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
464{
465 struct uart_omap_port *up = (struct uart_omap_port *)port;
466 unsigned char mcr = 0;
467
Rajendra Nayakba774332011-12-14 17:25:43 +0530468 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530469 if (mctrl & TIOCM_RTS)
470 mcr |= UART_MCR_RTS;
471 if (mctrl & TIOCM_DTR)
472 mcr |= UART_MCR_DTR;
473 if (mctrl & TIOCM_OUT1)
474 mcr |= UART_MCR_OUT1;
475 if (mctrl & TIOCM_OUT2)
476 mcr |= UART_MCR_OUT2;
477 if (mctrl & TIOCM_LOOP)
478 mcr |= UART_MCR_LOOP;
479
Govindraj.Rfcdca752011-02-28 18:12:23 +0530480 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rc538d202011-11-07 18:57:03 +0530481 up->mcr = serial_in(up, UART_MCR);
482 up->mcr |= mcr;
483 serial_out(up, UART_MCR, up->mcr);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530484 pm_runtime_put(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530485}
486
487static void serial_omap_break_ctl(struct uart_port *port, int break_state)
488{
489 struct uart_omap_port *up = (struct uart_omap_port *)port;
490 unsigned long flags = 0;
491
Rajendra Nayakba774332011-12-14 17:25:43 +0530492 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530493 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530494 spin_lock_irqsave(&up->port.lock, flags);
495 if (break_state == -1)
496 up->lcr |= UART_LCR_SBC;
497 else
498 up->lcr &= ~UART_LCR_SBC;
499 serial_out(up, UART_LCR, up->lcr);
500 spin_unlock_irqrestore(&up->port.lock, flags);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530501 pm_runtime_put(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530502}
503
504static int serial_omap_startup(struct uart_port *port)
505{
506 struct uart_omap_port *up = (struct uart_omap_port *)port;
507 unsigned long flags = 0;
508 int retval;
509
510 /*
511 * Allocate the IRQ
512 */
513 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
514 up->name, up);
515 if (retval)
516 return retval;
517
Rajendra Nayakba774332011-12-14 17:25:43 +0530518 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530519
Govindraj.Rfcdca752011-02-28 18:12:23 +0530520 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530521 /*
522 * Clear the FIFO buffers and disable them.
523 * (they will be reenabled in set_termios())
524 */
525 serial_omap_clear_fifos(up);
526 /* For Hardware flow control */
527 serial_out(up, UART_MCR, UART_MCR_RTS);
528
529 /*
530 * Clear the interrupt registers.
531 */
532 (void) serial_in(up, UART_LSR);
533 if (serial_in(up, UART_LSR) & UART_LSR_DR)
534 (void) serial_in(up, UART_RX);
535 (void) serial_in(up, UART_IIR);
536 (void) serial_in(up, UART_MSR);
537
538 /*
539 * Now, initialize the UART
540 */
541 serial_out(up, UART_LCR, UART_LCR_WLEN8);
542 spin_lock_irqsave(&up->port.lock, flags);
543 /*
544 * Most PC uarts need OUT2 raised to enable interrupts.
545 */
546 up->port.mctrl |= TIOCM_OUT2;
547 serial_omap_set_mctrl(&up->port, up->port.mctrl);
548 spin_unlock_irqrestore(&up->port.lock, flags);
549
550 up->msr_saved_flags = 0;
551 if (up->use_dma) {
552 free_page((unsigned long)up->port.state->xmit.buf);
553 up->port.state->xmit.buf = dma_alloc_coherent(NULL,
554 UART_XMIT_SIZE,
555 (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
556 0);
557 init_timer(&(up->uart_dma.rx_timer));
Jon Huntera9e210e2011-11-09 17:34:49 +0530558 up->uart_dma.rx_timer.function = serial_omap_rxdma_poll;
Rajendra Nayakba774332011-12-14 17:25:43 +0530559 up->uart_dma.rx_timer.data = up->port.line;
Govindraj.Rb6126332010-09-27 20:20:49 +0530560 /* Currently the buffer size is 4KB. Can increase it */
561 up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
562 up->uart_dma.rx_buf_size,
563 (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
564 }
565 /*
566 * Finally, enable interrupts. Note: Modem status interrupts
567 * are set via set_termios(), which will be occurring imminently
568 * anyway, so we don't enable them here.
569 */
570 up->ier = UART_IER_RLSI | UART_IER_RDI;
571 serial_out(up, UART_IER, up->ier);
572
Jarkko Nikula78841462011-01-24 17:51:22 +0200573 /* Enable module level wake up */
574 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
575
Govindraj.Rfcdca752011-02-28 18:12:23 +0530576 pm_runtime_mark_last_busy(&up->pdev->dev);
577 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530578 up->port_activity = jiffies;
579 return 0;
580}
581
582static void serial_omap_shutdown(struct uart_port *port)
583{
584 struct uart_omap_port *up = (struct uart_omap_port *)port;
585 unsigned long flags = 0;
586
Rajendra Nayakba774332011-12-14 17:25:43 +0530587 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530588
589 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530590 /*
591 * Disable interrupts from this port
592 */
593 up->ier = 0;
594 serial_out(up, UART_IER, 0);
595
596 spin_lock_irqsave(&up->port.lock, flags);
597 up->port.mctrl &= ~TIOCM_OUT2;
598 serial_omap_set_mctrl(&up->port, up->port.mctrl);
599 spin_unlock_irqrestore(&up->port.lock, flags);
600
601 /*
602 * Disable break condition and FIFOs
603 */
604 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
605 serial_omap_clear_fifos(up);
606
607 /*
608 * Read data port to reset things, and then free the irq
609 */
610 if (serial_in(up, UART_LSR) & UART_LSR_DR)
611 (void) serial_in(up, UART_RX);
612 if (up->use_dma) {
613 dma_free_coherent(up->port.dev,
614 UART_XMIT_SIZE, up->port.state->xmit.buf,
615 up->uart_dma.tx_buf_dma_phys);
616 up->port.state->xmit.buf = NULL;
617 serial_omap_stop_rx(port);
618 dma_free_coherent(up->port.dev,
619 up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
620 up->uart_dma.rx_buf_dma_phys);
621 up->uart_dma.rx_buf = NULL;
622 }
Govindraj.Rfcdca752011-02-28 18:12:23 +0530623
624 pm_runtime_put(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530625 free_irq(up->port.irq, up);
626}
627
628static inline void
629serial_omap_configure_xonxoff
630 (struct uart_omap_port *up, struct ktermios *termios)
631{
Govindraj.Rb6126332010-09-27 20:20:49 +0530632 up->lcr = serial_in(up, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800633 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530634 up->efr = serial_in(up, UART_EFR);
635 serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
636
637 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
638 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
639
640 /* clear SW control mode bits */
Govindraj.Rc538d202011-11-07 18:57:03 +0530641 up->efr &= OMAP_UART_SW_CLR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530642
643 /*
644 * IXON Flag:
645 * Enable XON/XOFF flow control on output.
646 * Transmit XON1, XOFF1
647 */
648 if (termios->c_iflag & IXON)
Govindraj.Rc538d202011-11-07 18:57:03 +0530649 up->efr |= OMAP_UART_SW_TX;
Govindraj.Rb6126332010-09-27 20:20:49 +0530650
651 /*
652 * IXOFF Flag:
653 * Enable XON/XOFF flow control on input.
654 * Receiver compares XON1, XOFF1.
655 */
656 if (termios->c_iflag & IXOFF)
Govindraj.Rc538d202011-11-07 18:57:03 +0530657 up->efr |= OMAP_UART_SW_RX;
Govindraj.Rb6126332010-09-27 20:20:49 +0530658
659 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800660 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530661
662 up->mcr = serial_in(up, UART_MCR);
663
664 /*
665 * IXANY Flag:
666 * Enable any character to restart output.
667 * Operation resumes after receiving any
668 * character after recognition of the XOFF character
669 */
670 if (termios->c_iflag & IXANY)
671 up->mcr |= UART_MCR_XONANY;
672
673 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800674 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530675 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
676 /* Enable special char function UARTi.EFR_REG[5] and
677 * load the new software flow control mode IXON or IXOFF
678 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
679 */
Govindraj.Rc538d202011-11-07 18:57:03 +0530680 serial_out(up, UART_EFR, up->efr | UART_EFR_SCD);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800681 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530682
683 serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
684 serial_out(up, UART_LCR, up->lcr);
685}
686
Govindraj.R2fd14962011-11-09 17:41:21 +0530687static void serial_omap_uart_qos_work(struct work_struct *work)
688{
689 struct uart_omap_port *up = container_of(work, struct uart_omap_port,
690 qos_work);
691
692 pm_qos_update_request(&up->pm_qos_request, up->latency);
693}
694
Govindraj.Rb6126332010-09-27 20:20:49 +0530695static void
696serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
697 struct ktermios *old)
698{
699 struct uart_omap_port *up = (struct uart_omap_port *)port;
700 unsigned char cval = 0;
701 unsigned char efr = 0;
702 unsigned long flags = 0;
703 unsigned int baud, quot;
704
705 switch (termios->c_cflag & CSIZE) {
706 case CS5:
707 cval = UART_LCR_WLEN5;
708 break;
709 case CS6:
710 cval = UART_LCR_WLEN6;
711 break;
712 case CS7:
713 cval = UART_LCR_WLEN7;
714 break;
715 default:
716 case CS8:
717 cval = UART_LCR_WLEN8;
718 break;
719 }
720
721 if (termios->c_cflag & CSTOPB)
722 cval |= UART_LCR_STOP;
723 if (termios->c_cflag & PARENB)
724 cval |= UART_LCR_PARITY;
725 if (!(termios->c_cflag & PARODD))
726 cval |= UART_LCR_EPAR;
727
728 /*
729 * Ask the core to calculate the divisor for us.
730 */
731
732 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
733 quot = serial_omap_get_divisor(port, baud);
734
Govindraj.R2fd14962011-11-09 17:41:21 +0530735 /* calculate wakeup latency constraint */
736 up->calc_latency = (1000000 * up->port.fifosize) /
737 (1000 * baud / 8);
738 up->latency = up->calc_latency;
739 schedule_work(&up->qos_work);
740
Govindraj.Rc538d202011-11-07 18:57:03 +0530741 up->dll = quot & 0xff;
742 up->dlh = quot >> 8;
743 up->mdr1 = UART_OMAP_MDR1_DISABLE;
744
Govindraj.Rb6126332010-09-27 20:20:49 +0530745 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
746 UART_FCR_ENABLE_FIFO;
747 if (up->use_dma)
748 up->fcr |= UART_FCR_DMA_SELECT;
749
750 /*
751 * Ok, we're now changing the port state. Do it with
752 * interrupts disabled.
753 */
Govindraj.Rfcdca752011-02-28 18:12:23 +0530754 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530755 spin_lock_irqsave(&up->port.lock, flags);
756
757 /*
758 * Update the per-port timeout.
759 */
760 uart_update_timeout(port, termios->c_cflag, baud);
761
762 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
763 if (termios->c_iflag & INPCK)
764 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
765 if (termios->c_iflag & (BRKINT | PARMRK))
766 up->port.read_status_mask |= UART_LSR_BI;
767
768 /*
769 * Characters to ignore
770 */
771 up->port.ignore_status_mask = 0;
772 if (termios->c_iflag & IGNPAR)
773 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
774 if (termios->c_iflag & IGNBRK) {
775 up->port.ignore_status_mask |= UART_LSR_BI;
776 /*
777 * If we're ignoring parity and break indicators,
778 * ignore overruns too (for real raw support).
779 */
780 if (termios->c_iflag & IGNPAR)
781 up->port.ignore_status_mask |= UART_LSR_OE;
782 }
783
784 /*
785 * ignore all characters if CREAD is not set
786 */
787 if ((termios->c_cflag & CREAD) == 0)
788 up->port.ignore_status_mask |= UART_LSR_DR;
789
790 /*
791 * Modem status interrupts
792 */
793 up->ier &= ~UART_IER_MSI;
794 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
795 up->ier |= UART_IER_MSI;
796 serial_out(up, UART_IER, up->ier);
797 serial_out(up, UART_LCR, cval); /* reset DLAB */
Govindraj.Rc538d202011-11-07 18:57:03 +0530798 up->lcr = cval;
Govindraj.R32212892011-11-07 18:58:55 +0530799 up->scr = OMAP_UART_SCR_TX_EMPTY;
Govindraj.Rb6126332010-09-27 20:20:49 +0530800
801 /* FIFOs and DMA Settings */
802
803 /* FCR can be changed only when the
804 * baud clock is not running
805 * DLL_REG and DLH_REG set to 0.
806 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800807 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530808 serial_out(up, UART_DLL, 0);
809 serial_out(up, UART_DLM, 0);
810 serial_out(up, UART_LCR, 0);
811
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800812 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530813
814 up->efr = serial_in(up, UART_EFR);
815 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
816
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800817 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530818 up->mcr = serial_in(up, UART_MCR);
819 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
820 /* FIFO ENABLE, DMA MODE */
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700821
822 up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
Paul Walmsley0a697b22012-01-21 00:27:40 -0700823
Greg Kroah-Hartman8a74e9f2012-01-26 11:15:18 -0800824 if (up->use_dma) {
825 serial_out(up, UART_TI752_TLR, 0);
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700826 up->scr |= UART_FCR_TRIGGER_4;
827 } else {
828 /* Set receive FIFO threshold to 1 byte */
829 up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
830 up->fcr |= (0x1 << OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT);
Greg Kroah-Hartman8a74e9f2012-01-26 11:15:18 -0800831 }
832
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700833 serial_out(up, UART_FCR, up->fcr);
834 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
835
Govindraj.Rc538d202011-11-07 18:57:03 +0530836 serial_out(up, UART_OMAP_SCR, up->scr);
837
Govindraj.Rb6126332010-09-27 20:20:49 +0530838 serial_out(up, UART_EFR, up->efr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800839 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530840 serial_out(up, UART_MCR, up->mcr);
841
842 /* Protocol, Baud Rate, and Interrupt Settings */
843
Govindraj.R94734742011-11-07 19:00:33 +0530844 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
845 serial_omap_mdr1_errataset(up, up->mdr1);
846 else
847 serial_out(up, UART_OMAP_MDR1, up->mdr1);
848
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800849 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530850
851 up->efr = serial_in(up, UART_EFR);
852 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
853
854 serial_out(up, UART_LCR, 0);
855 serial_out(up, UART_IER, 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800856 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530857
Govindraj.Rc538d202011-11-07 18:57:03 +0530858 serial_out(up, UART_DLL, up->dll); /* LS of divisor */
859 serial_out(up, UART_DLM, up->dlh); /* MS of divisor */
Govindraj.Rb6126332010-09-27 20:20:49 +0530860
861 serial_out(up, UART_LCR, 0);
862 serial_out(up, UART_IER, up->ier);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800863 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530864
865 serial_out(up, UART_EFR, up->efr);
866 serial_out(up, UART_LCR, cval);
867
868 if (baud > 230400 && baud != 3000000)
Govindraj.Rc538d202011-11-07 18:57:03 +0530869 up->mdr1 = UART_OMAP_MDR1_13X_MODE;
Govindraj.Rb6126332010-09-27 20:20:49 +0530870 else
Govindraj.Rc538d202011-11-07 18:57:03 +0530871 up->mdr1 = UART_OMAP_MDR1_16X_MODE;
872
Govindraj.R94734742011-11-07 19:00:33 +0530873 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
874 serial_omap_mdr1_errataset(up, up->mdr1);
875 else
876 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +0530877
878 /* Hardware Flow Control Configuration */
879
880 if (termios->c_cflag & CRTSCTS) {
881 efr |= (UART_EFR_CTS | UART_EFR_RTS);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800882 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530883
884 up->mcr = serial_in(up, UART_MCR);
885 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
886
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800887 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530888 up->efr = serial_in(up, UART_EFR);
889 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
890
891 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
892 serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800893 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530894 serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
895 serial_out(up, UART_LCR, cval);
896 }
897
898 serial_omap_set_mctrl(&up->port, up->port.mctrl);
899 /* Software Flow Control Configuration */
Nick Pellyb280a972011-07-15 13:53:08 -0700900 serial_omap_configure_xonxoff(up, termios);
Govindraj.Rb6126332010-09-27 20:20:49 +0530901
902 spin_unlock_irqrestore(&up->port.lock, flags);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530903 pm_runtime_put(&up->pdev->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +0530904 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530905}
906
907static void
908serial_omap_pm(struct uart_port *port, unsigned int state,
909 unsigned int oldstate)
910{
911 struct uart_omap_port *up = (struct uart_omap_port *)port;
912 unsigned char efr;
913
Rajendra Nayakba774332011-12-14 17:25:43 +0530914 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530915
916 pm_runtime_get_sync(&up->pdev->dev);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800917 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530918 efr = serial_in(up, UART_EFR);
919 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
920 serial_out(up, UART_LCR, 0);
921
922 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800923 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530924 serial_out(up, UART_EFR, efr);
925 serial_out(up, UART_LCR, 0);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530926
927 if (!device_may_wakeup(&up->pdev->dev)) {
928 if (!state)
929 pm_runtime_forbid(&up->pdev->dev);
930 else
931 pm_runtime_allow(&up->pdev->dev);
932 }
933
934 pm_runtime_put(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530935}
936
937static void serial_omap_release_port(struct uart_port *port)
938{
939 dev_dbg(port->dev, "serial_omap_release_port+\n");
940}
941
942static int serial_omap_request_port(struct uart_port *port)
943{
944 dev_dbg(port->dev, "serial_omap_request_port+\n");
945 return 0;
946}
947
948static void serial_omap_config_port(struct uart_port *port, int flags)
949{
950 struct uart_omap_port *up = (struct uart_omap_port *)port;
951
952 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
Rajendra Nayakba774332011-12-14 17:25:43 +0530953 up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530954 up->port.type = PORT_OMAP;
955}
956
957static int
958serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
959{
960 /* we don't want the core code to modify any port params */
961 dev_dbg(port->dev, "serial_omap_verify_port+\n");
962 return -EINVAL;
963}
964
965static const char *
966serial_omap_type(struct uart_port *port)
967{
968 struct uart_omap_port *up = (struct uart_omap_port *)port;
969
Rajendra Nayakba774332011-12-14 17:25:43 +0530970 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530971 return up->name;
972}
973
Govindraj.Rb6126332010-09-27 20:20:49 +0530974#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
975
976static inline void wait_for_xmitr(struct uart_omap_port *up)
977{
978 unsigned int status, tmout = 10000;
979
980 /* Wait up to 10ms for the character(s) to be sent. */
981 do {
982 status = serial_in(up, UART_LSR);
983
984 if (status & UART_LSR_BI)
985 up->lsr_break_flag = UART_LSR_BI;
986
987 if (--tmout == 0)
988 break;
989 udelay(1);
990 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
991
992 /* Wait up to 1s for flow control if necessary */
993 if (up->port.flags & UPF_CONS_FLOW) {
994 tmout = 1000000;
995 for (tmout = 1000000; tmout; tmout--) {
996 unsigned int msr = serial_in(up, UART_MSR);
997
998 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
999 if (msr & UART_MSR_CTS)
1000 break;
1001
1002 udelay(1);
1003 }
1004 }
1005}
1006
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001007#ifdef CONFIG_CONSOLE_POLL
1008
1009static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
1010{
1011 struct uart_omap_port *up = (struct uart_omap_port *)port;
Govindraj.Rfcdca752011-02-28 18:12:23 +05301012
1013 pm_runtime_get_sync(&up->pdev->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001014 wait_for_xmitr(up);
1015 serial_out(up, UART_TX, ch);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301016 pm_runtime_put(&up->pdev->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001017}
1018
1019static int serial_omap_poll_get_char(struct uart_port *port)
1020{
1021 struct uart_omap_port *up = (struct uart_omap_port *)port;
Govindraj.Rfcdca752011-02-28 18:12:23 +05301022 unsigned int status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001023
Govindraj.Rfcdca752011-02-28 18:12:23 +05301024 pm_runtime_get_sync(&up->pdev->dev);
1025 status = serial_in(up, UART_LSR);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001026 if (!(status & UART_LSR_DR))
1027 return NO_POLL_CHAR;
1028
Govindraj.Rfcdca752011-02-28 18:12:23 +05301029 status = serial_in(up, UART_RX);
1030 pm_runtime_put(&up->pdev->dev);
1031 return status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001032}
1033
1034#endif /* CONFIG_CONSOLE_POLL */
1035
1036#ifdef CONFIG_SERIAL_OMAP_CONSOLE
1037
1038static struct uart_omap_port *serial_omap_console_ports[4];
1039
1040static struct uart_driver serial_omap_reg;
1041
Govindraj.Rb6126332010-09-27 20:20:49 +05301042static void serial_omap_console_putchar(struct uart_port *port, int ch)
1043{
1044 struct uart_omap_port *up = (struct uart_omap_port *)port;
1045
1046 wait_for_xmitr(up);
1047 serial_out(up, UART_TX, ch);
1048}
1049
1050static void
1051serial_omap_console_write(struct console *co, const char *s,
1052 unsigned int count)
1053{
1054 struct uart_omap_port *up = serial_omap_console_ports[co->index];
1055 unsigned long flags;
1056 unsigned int ier;
1057 int locked = 1;
1058
Govindraj.Rfcdca752011-02-28 18:12:23 +05301059 pm_runtime_get_sync(&up->pdev->dev);
1060
Govindraj.Rb6126332010-09-27 20:20:49 +05301061 local_irq_save(flags);
1062 if (up->port.sysrq)
1063 locked = 0;
1064 else if (oops_in_progress)
1065 locked = spin_trylock(&up->port.lock);
1066 else
1067 spin_lock(&up->port.lock);
1068
1069 /*
1070 * First save the IER then disable the interrupts
1071 */
1072 ier = serial_in(up, UART_IER);
1073 serial_out(up, UART_IER, 0);
1074
1075 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
1076
1077 /*
1078 * Finally, wait for transmitter to become empty
1079 * and restore the IER
1080 */
1081 wait_for_xmitr(up);
1082 serial_out(up, UART_IER, ier);
1083 /*
1084 * The receive handling will happen properly because the
1085 * receive ready bit will still be set; it is not cleared
1086 * on read. However, modem control will not, we must
1087 * call it if we have saved something in the saved flags
1088 * while processing with interrupts off.
1089 */
1090 if (up->msr_saved_flags)
1091 check_modem_status(up);
1092
Govindraj.Rfcdca752011-02-28 18:12:23 +05301093 pm_runtime_mark_last_busy(&up->pdev->dev);
1094 pm_runtime_put_autosuspend(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301095 if (locked)
1096 spin_unlock(&up->port.lock);
1097 local_irq_restore(flags);
1098}
1099
1100static int __init
1101serial_omap_console_setup(struct console *co, char *options)
1102{
1103 struct uart_omap_port *up;
1104 int baud = 115200;
1105 int bits = 8;
1106 int parity = 'n';
1107 int flow = 'n';
1108
1109 if (serial_omap_console_ports[co->index] == NULL)
1110 return -ENODEV;
1111 up = serial_omap_console_ports[co->index];
1112
1113 if (options)
1114 uart_parse_options(options, &baud, &parity, &bits, &flow);
1115
1116 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1117}
1118
1119static struct console serial_omap_console = {
1120 .name = OMAP_SERIAL_NAME,
1121 .write = serial_omap_console_write,
1122 .device = uart_console_device,
1123 .setup = serial_omap_console_setup,
1124 .flags = CON_PRINTBUFFER,
1125 .index = -1,
1126 .data = &serial_omap_reg,
1127};
1128
1129static void serial_omap_add_console_port(struct uart_omap_port *up)
1130{
Rajendra Nayakba774332011-12-14 17:25:43 +05301131 serial_omap_console_ports[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301132}
1133
1134#define OMAP_CONSOLE (&serial_omap_console)
1135
1136#else
1137
1138#define OMAP_CONSOLE NULL
1139
1140static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1141{}
1142
1143#endif
1144
1145static struct uart_ops serial_omap_pops = {
1146 .tx_empty = serial_omap_tx_empty,
1147 .set_mctrl = serial_omap_set_mctrl,
1148 .get_mctrl = serial_omap_get_mctrl,
1149 .stop_tx = serial_omap_stop_tx,
1150 .start_tx = serial_omap_start_tx,
1151 .stop_rx = serial_omap_stop_rx,
1152 .enable_ms = serial_omap_enable_ms,
1153 .break_ctl = serial_omap_break_ctl,
1154 .startup = serial_omap_startup,
1155 .shutdown = serial_omap_shutdown,
1156 .set_termios = serial_omap_set_termios,
1157 .pm = serial_omap_pm,
1158 .type = serial_omap_type,
1159 .release_port = serial_omap_release_port,
1160 .request_port = serial_omap_request_port,
1161 .config_port = serial_omap_config_port,
1162 .verify_port = serial_omap_verify_port,
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001163#ifdef CONFIG_CONSOLE_POLL
1164 .poll_put_char = serial_omap_poll_put_char,
1165 .poll_get_char = serial_omap_poll_get_char,
1166#endif
Govindraj.Rb6126332010-09-27 20:20:49 +05301167};
1168
1169static struct uart_driver serial_omap_reg = {
1170 .owner = THIS_MODULE,
1171 .driver_name = "OMAP-SERIAL",
1172 .dev_name = OMAP_SERIAL_NAME,
1173 .nr = OMAP_MAX_HSUART_PORTS,
1174 .cons = OMAP_CONSOLE,
1175};
1176
Shubhrajyoti D3bc4f0d2012-01-16 15:52:36 +05301177#ifdef CONFIG_PM_SLEEP
Govindraj.Rfcdca752011-02-28 18:12:23 +05301178static int serial_omap_suspend(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301179{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301180 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301181
Govindraj.R2fd14962011-11-09 17:41:21 +05301182 if (up) {
Govindraj.Rb6126332010-09-27 20:20:49 +05301183 uart_suspend_port(&serial_omap_reg, &up->port);
Govindraj.R2fd14962011-11-09 17:41:21 +05301184 flush_work_sync(&up->qos_work);
1185 }
1186
Govindraj.Rb6126332010-09-27 20:20:49 +05301187 return 0;
1188}
1189
Govindraj.Rfcdca752011-02-28 18:12:23 +05301190static int serial_omap_resume(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301191{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301192 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301193
1194 if (up)
1195 uart_resume_port(&serial_omap_reg, &up->port);
1196 return 0;
1197}
Govindraj.Rfcdca752011-02-28 18:12:23 +05301198#endif
Govindraj.Rb6126332010-09-27 20:20:49 +05301199
Jon Huntera9e210e2011-11-09 17:34:49 +05301200static void serial_omap_rxdma_poll(unsigned long uart_no)
Govindraj.Rb6126332010-09-27 20:20:49 +05301201{
1202 struct uart_omap_port *up = ui[uart_no];
1203 unsigned int curr_dma_pos, curr_transmitted_size;
Vasiliy Kulikov79fc3e22010-10-10 21:28:35 +04001204 int ret = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +05301205
1206 curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
1207 if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
1208 (curr_dma_pos == 0)) {
1209 if (jiffies_to_msecs(jiffies - up->port_activity) <
Jon Huntera9e210e2011-11-09 17:34:49 +05301210 up->uart_dma.rx_timeout) {
Govindraj.Rb6126332010-09-27 20:20:49 +05301211 mod_timer(&up->uart_dma.rx_timer, jiffies +
Jon Huntera9e210e2011-11-09 17:34:49 +05301212 usecs_to_jiffies(up->uart_dma.rx_poll_rate));
Govindraj.Rb6126332010-09-27 20:20:49 +05301213 } else {
1214 serial_omap_stop_rxdma(up);
1215 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1216 serial_out(up, UART_IER, up->ier);
1217 }
1218 return;
1219 }
1220
1221 curr_transmitted_size = curr_dma_pos -
1222 up->uart_dma.prev_rx_dma_pos;
1223 up->port.icount.rx += curr_transmitted_size;
1224 tty_insert_flip_string(up->port.state->port.tty,
1225 up->uart_dma.rx_buf +
1226 (up->uart_dma.prev_rx_dma_pos -
1227 up->uart_dma.rx_buf_dma_phys),
1228 curr_transmitted_size);
1229 tty_flip_buffer_push(up->port.state->port.tty);
1230 up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
1231 if (up->uart_dma.rx_buf_size +
1232 up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
1233 ret = serial_omap_start_rxdma(up);
1234 if (ret < 0) {
1235 serial_omap_stop_rxdma(up);
1236 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1237 serial_out(up, UART_IER, up->ier);
1238 }
1239 } else {
1240 mod_timer(&up->uart_dma.rx_timer, jiffies +
Jon Huntera9e210e2011-11-09 17:34:49 +05301241 usecs_to_jiffies(up->uart_dma.rx_poll_rate));
Govindraj.Rb6126332010-09-27 20:20:49 +05301242 }
1243 up->port_activity = jiffies;
1244}
1245
1246static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
1247{
1248 return;
1249}
1250
1251static int serial_omap_start_rxdma(struct uart_omap_port *up)
1252{
1253 int ret = 0;
1254
1255 if (up->uart_dma.rx_dma_channel == -1) {
Govindraj.Rfcdca752011-02-28 18:12:23 +05301256 pm_runtime_get_sync(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301257 ret = omap_request_dma(up->uart_dma.uart_dma_rx,
1258 "UART Rx DMA",
1259 (void *)uart_rx_dma_callback, up,
1260 &(up->uart_dma.rx_dma_channel));
1261 if (ret < 0)
1262 return ret;
1263
1264 omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
1265 OMAP_DMA_AMODE_CONSTANT,
1266 up->uart_dma.uart_base, 0, 0);
1267 omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
1268 OMAP_DMA_AMODE_POST_INC,
1269 up->uart_dma.rx_buf_dma_phys, 0, 0);
1270 omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
1271 OMAP_DMA_DATA_TYPE_S8,
1272 up->uart_dma.rx_buf_size, 1,
1273 OMAP_DMA_SYNC_ELEMENT,
1274 up->uart_dma.uart_dma_rx, 0);
1275 }
1276 up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
1277 /* FIXME: Cache maintenance needed here? */
1278 omap_start_dma(up->uart_dma.rx_dma_channel);
1279 mod_timer(&up->uart_dma.rx_timer, jiffies +
Jon Huntera9e210e2011-11-09 17:34:49 +05301280 usecs_to_jiffies(up->uart_dma.rx_poll_rate));
Govindraj.Rb6126332010-09-27 20:20:49 +05301281 up->uart_dma.rx_dma_used = true;
1282 return ret;
1283}
1284
1285static void serial_omap_continue_tx(struct uart_omap_port *up)
1286{
1287 struct circ_buf *xmit = &up->port.state->xmit;
1288 unsigned int start = up->uart_dma.tx_buf_dma_phys
1289 + (xmit->tail & (UART_XMIT_SIZE - 1));
1290
1291 if (uart_circ_empty(xmit))
1292 return;
1293
1294 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
1295 /*
1296 * It is a circular buffer. See if the buffer has wounded back.
1297 * If yes it will have to be transferred in two separate dma
1298 * transfers
1299 */
1300 if (start + up->uart_dma.tx_buf_size >=
1301 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
1302 up->uart_dma.tx_buf_size =
1303 (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
1304 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
1305 OMAP_DMA_AMODE_CONSTANT,
1306 up->uart_dma.uart_base, 0, 0);
1307 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
1308 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
1309 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
1310 OMAP_DMA_DATA_TYPE_S8,
1311 up->uart_dma.tx_buf_size, 1,
1312 OMAP_DMA_SYNC_ELEMENT,
1313 up->uart_dma.uart_dma_tx, 0);
1314 /* FIXME: Cache maintenance needed here? */
1315 omap_start_dma(up->uart_dma.tx_dma_channel);
1316}
1317
1318static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1319{
1320 struct uart_omap_port *up = (struct uart_omap_port *)data;
1321 struct circ_buf *xmit = &up->port.state->xmit;
1322
1323 xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
1324 (UART_XMIT_SIZE - 1);
1325 up->port.icount.tx += up->uart_dma.tx_buf_size;
1326
1327 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1328 uart_write_wakeup(&up->port);
1329
1330 if (uart_circ_empty(xmit)) {
1331 spin_lock(&(up->uart_dma.tx_lock));
1332 serial_omap_stop_tx(&up->port);
1333 up->uart_dma.tx_dma_used = false;
1334 spin_unlock(&(up->uart_dma.tx_lock));
1335 } else {
1336 omap_stop_dma(up->uart_dma.tx_dma_channel);
1337 serial_omap_continue_tx(up);
1338 }
1339 up->port_activity = jiffies;
1340 return;
1341}
1342
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301343static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
1344{
1345 struct omap_uart_port_info *omap_up_info;
1346
1347 omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
1348 if (!omap_up_info)
1349 return NULL; /* out of memory */
1350
1351 of_property_read_u32(dev->of_node, "clock-frequency",
1352 &omap_up_info->uartclk);
1353 return omap_up_info;
1354}
1355
Govindraj.Rb6126332010-09-27 20:20:49 +05301356static int serial_omap_probe(struct platform_device *pdev)
1357{
1358 struct uart_omap_port *up;
1359 struct resource *mem, *irq, *dma_tx, *dma_rx;
1360 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1361 int ret = -ENOSPC;
1362
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301363 if (pdev->dev.of_node)
1364 omap_up_info = of_get_uart_port_info(&pdev->dev);
1365
Govindraj.Rb6126332010-09-27 20:20:49 +05301366 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1367 if (!mem) {
1368 dev_err(&pdev->dev, "no mem resource?\n");
1369 return -ENODEV;
1370 }
1371
1372 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1373 if (!irq) {
1374 dev_err(&pdev->dev, "no irq resource?\n");
1375 return -ENODEV;
1376 }
1377
Joe Perches28f65c112011-06-09 09:13:32 -07001378 if (!request_mem_region(mem->start, resource_size(mem),
1379 pdev->dev.driver->name)) {
Govindraj.Rb6126332010-09-27 20:20:49 +05301380 dev_err(&pdev->dev, "memory region already claimed\n");
1381 return -EBUSY;
1382 }
1383
1384 dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1385 if (!dma_rx) {
1386 ret = -EINVAL;
1387 goto err;
1388 }
1389
1390 dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1391 if (!dma_tx) {
1392 ret = -EINVAL;
1393 goto err;
1394 }
1395
1396 up = kzalloc(sizeof(*up), GFP_KERNEL);
1397 if (up == NULL) {
1398 ret = -ENOMEM;
1399 goto do_release_region;
1400 }
Govindraj.Rb6126332010-09-27 20:20:49 +05301401 up->pdev = pdev;
1402 up->port.dev = &pdev->dev;
1403 up->port.type = PORT_OMAP;
1404 up->port.iotype = UPIO_MEM;
1405 up->port.irq = irq->start;
1406
1407 up->port.regshift = 2;
1408 up->port.fifosize = 64;
1409 up->port.ops = &serial_omap_pops;
Govindraj.Rb6126332010-09-27 20:20:49 +05301410
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301411 if (pdev->dev.of_node)
1412 up->port.line = of_alias_get_id(pdev->dev.of_node, "serial");
1413 else
1414 up->port.line = pdev->id;
1415
1416 if (up->port.line < 0) {
1417 dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
1418 up->port.line);
1419 ret = -ENODEV;
1420 goto err;
1421 }
1422
1423 sprintf(up->name, "OMAP UART%d", up->port.line);
Govindraj.Redd70ad2011-10-11 14:55:41 +05301424 up->port.mapbase = mem->start;
1425 up->port.membase = ioremap(mem->start, resource_size(mem));
1426 if (!up->port.membase) {
1427 dev_err(&pdev->dev, "can't ioremap UART\n");
1428 ret = -ENOMEM;
1429 goto err;
1430 }
1431
Govindraj.Rb6126332010-09-27 20:20:49 +05301432 up->port.flags = omap_up_info->flags;
Govindraj.Rb6126332010-09-27 20:20:49 +05301433 up->port.uartclk = omap_up_info->uartclk;
Rajendra Nayak8fe789d2011-12-14 17:25:44 +05301434 if (!up->port.uartclk) {
1435 up->port.uartclk = DEFAULT_CLK_SPEED;
1436 dev_warn(&pdev->dev, "No clock speed specified: using default:"
1437 "%d\n", DEFAULT_CLK_SPEED);
1438 }
Govindraj.Rb6126332010-09-27 20:20:49 +05301439 up->uart_dma.uart_base = mem->start;
Govindraj.R94734742011-11-07 19:00:33 +05301440 up->errata = omap_up_info->errata;
Govindraj.Rb6126332010-09-27 20:20:49 +05301441
1442 if (omap_up_info->dma_enabled) {
1443 up->uart_dma.uart_dma_tx = dma_tx->start;
1444 up->uart_dma.uart_dma_rx = dma_rx->start;
1445 up->use_dma = 1;
Deepak Kc86845db2011-11-09 17:33:38 +05301446 up->uart_dma.rx_buf_size = omap_up_info->dma_rx_buf_size;
1447 up->uart_dma.rx_timeout = omap_up_info->dma_rx_timeout;
Jon Huntera9e210e2011-11-09 17:34:49 +05301448 up->uart_dma.rx_poll_rate = omap_up_info->dma_rx_poll_rate;
Govindraj.Rb6126332010-09-27 20:20:49 +05301449 spin_lock_init(&(up->uart_dma.tx_lock));
1450 spin_lock_init(&(up->uart_dma.rx_lock));
1451 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
1452 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
1453 }
1454
Govindraj.R2fd14962011-11-09 17:41:21 +05301455 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1456 up->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1457 pm_qos_add_request(&up->pm_qos_request,
1458 PM_QOS_CPU_DMA_LATENCY, up->latency);
1459 serial_omap_uart_wq = create_singlethread_workqueue(up->name);
1460 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
1461
Govindraj.Rfcdca752011-02-28 18:12:23 +05301462 pm_runtime_use_autosuspend(&pdev->dev);
1463 pm_runtime_set_autosuspend_delay(&pdev->dev,
Deepak Kc86845db2011-11-09 17:33:38 +05301464 omap_up_info->autosuspend_timeout);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301465
1466 pm_runtime_irq_safe(&pdev->dev);
1467 pm_runtime_enable(&pdev->dev);
1468 pm_runtime_get_sync(&pdev->dev);
1469
Rajendra Nayakba774332011-12-14 17:25:43 +05301470 ui[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301471 serial_omap_add_console_port(up);
1472
1473 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1474 if (ret != 0)
1475 goto do_release_region;
1476
Govindraj.Rfcdca752011-02-28 18:12:23 +05301477 pm_runtime_put(&pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301478 platform_set_drvdata(pdev, up);
1479 return 0;
1480err:
1481 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1482 pdev->id, __func__, ret);
1483do_release_region:
Joe Perches28f65c112011-06-09 09:13:32 -07001484 release_mem_region(mem->start, resource_size(mem));
Govindraj.Rb6126332010-09-27 20:20:49 +05301485 return ret;
1486}
1487
1488static int serial_omap_remove(struct platform_device *dev)
1489{
1490 struct uart_omap_port *up = platform_get_drvdata(dev);
1491
Govindraj.Rb6126332010-09-27 20:20:49 +05301492 if (up) {
Govindraj.Rfcdca752011-02-28 18:12:23 +05301493 pm_runtime_disable(&up->pdev->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301494 uart_remove_one_port(&serial_omap_reg, &up->port);
Govindraj.R2fd14962011-11-09 17:41:21 +05301495 pm_qos_remove_request(&up->pm_qos_request);
1496
Govindraj.Rb6126332010-09-27 20:20:49 +05301497 kfree(up);
1498 }
Govindraj.Rfcdca752011-02-28 18:12:23 +05301499
1500 platform_set_drvdata(dev, NULL);
Govindraj.Rb6126332010-09-27 20:20:49 +05301501 return 0;
1502}
1503
Govindraj.R94734742011-11-07 19:00:33 +05301504/*
1505 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
1506 * The access to uart register after MDR1 Access
1507 * causes UART to corrupt data.
1508 *
1509 * Need a delay =
1510 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
1511 * give 10 times as much
1512 */
1513static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
1514{
1515 u8 timeout = 255;
1516
1517 serial_out(up, UART_OMAP_MDR1, mdr1);
1518 udelay(2);
1519 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
1520 UART_FCR_CLEAR_RCVR);
1521 /*
1522 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
1523 * TX_FIFO_E bit is 1.
1524 */
1525 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
1526 (UART_LSR_THRE | UART_LSR_DR))) {
1527 timeout--;
1528 if (!timeout) {
1529 /* Should *never* happen. we warn and carry on */
1530 dev_crit(&up->pdev->dev, "Errata i202: timedout %x\n",
1531 serial_in(up, UART_LSR));
1532 break;
1533 }
1534 udelay(1);
1535 }
1536}
1537
Shubhrajyoti Db5148852012-01-16 15:52:37 +05301538#ifdef CONFIG_PM_RUNTIME
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301539static void serial_omap_restore_context(struct uart_omap_port *up)
1540{
Govindraj.R94734742011-11-07 19:00:33 +05301541 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1542 serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
1543 else
1544 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
1545
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301546 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1547 serial_out(up, UART_EFR, UART_EFR_ECB);
1548 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1549 serial_out(up, UART_IER, 0x0);
1550 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301551 serial_out(up, UART_DLL, up->dll);
1552 serial_out(up, UART_DLM, up->dlh);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301553 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1554 serial_out(up, UART_IER, up->ier);
1555 serial_out(up, UART_FCR, up->fcr);
1556 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1557 serial_out(up, UART_MCR, up->mcr);
1558 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301559 serial_out(up, UART_OMAP_SCR, up->scr);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301560 serial_out(up, UART_EFR, up->efr);
1561 serial_out(up, UART_LCR, up->lcr);
Govindraj.R94734742011-11-07 19:00:33 +05301562 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1563 serial_omap_mdr1_errataset(up, up->mdr1);
1564 else
1565 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301566}
1567
Govindraj.Rfcdca752011-02-28 18:12:23 +05301568static int serial_omap_runtime_suspend(struct device *dev)
1569{
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301570 struct uart_omap_port *up = dev_get_drvdata(dev);
1571 struct omap_uart_port_info *pdata = dev->platform_data;
1572
1573 if (!up)
1574 return -EINVAL;
1575
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301576 if (!pdata || !pdata->enable_wakeup)
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301577 return 0;
1578
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301579 if (pdata->get_context_loss_count)
1580 up->context_loss_cnt = pdata->get_context_loss_count(dev);
1581
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301582 if (device_may_wakeup(dev)) {
1583 if (!up->wakeups_enabled) {
1584 pdata->enable_wakeup(up->pdev, true);
1585 up->wakeups_enabled = true;
1586 }
1587 } else {
1588 if (up->wakeups_enabled) {
1589 pdata->enable_wakeup(up->pdev, false);
1590 up->wakeups_enabled = false;
1591 }
1592 }
1593
Govindraj.R94734742011-11-07 19:00:33 +05301594 /* Errata i291 */
1595 if (up->use_dma && pdata->set_forceidle &&
1596 (up->errata & UART_ERRATA_i291_DMA_FORCEIDLE))
1597 pdata->set_forceidle(up->pdev);
1598
Govindraj.R2fd14962011-11-09 17:41:21 +05301599 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1600 schedule_work(&up->qos_work);
1601
Govindraj.Rfcdca752011-02-28 18:12:23 +05301602 return 0;
1603}
1604
1605static int serial_omap_runtime_resume(struct device *dev)
1606{
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301607 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301608 struct omap_uart_port_info *pdata = dev->platform_data;
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301609
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301610 if (up) {
1611 if (pdata->get_context_loss_count) {
1612 u32 loss_cnt = pdata->get_context_loss_count(dev);
1613
1614 if (up->context_loss_cnt != loss_cnt)
1615 serial_omap_restore_context(up);
1616 }
Govindraj.R94734742011-11-07 19:00:33 +05301617
1618 /* Errata i291 */
1619 if (up->use_dma && pdata->set_noidle &&
1620 (up->errata & UART_ERRATA_i291_DMA_FORCEIDLE))
1621 pdata->set_noidle(up->pdev);
Govindraj.R2fd14962011-11-09 17:41:21 +05301622
1623 up->latency = up->calc_latency;
1624 schedule_work(&up->qos_work);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301625 }
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301626
Govindraj.Rfcdca752011-02-28 18:12:23 +05301627 return 0;
1628}
1629#endif
1630
1631static const struct dev_pm_ops serial_omap_dev_pm_ops = {
1632 SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
1633 SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
1634 serial_omap_runtime_resume, NULL)
1635};
1636
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301637#if defined(CONFIG_OF)
1638static const struct of_device_id omap_serial_of_match[] = {
1639 { .compatible = "ti,omap2-uart" },
1640 { .compatible = "ti,omap3-uart" },
1641 { .compatible = "ti,omap4-uart" },
1642 {},
1643};
1644MODULE_DEVICE_TABLE(of, omap_serial_of_match);
1645#endif
1646
Govindraj.Rb6126332010-09-27 20:20:49 +05301647static struct platform_driver serial_omap_driver = {
1648 .probe = serial_omap_probe,
1649 .remove = serial_omap_remove,
Govindraj.Rb6126332010-09-27 20:20:49 +05301650 .driver = {
1651 .name = DRIVER_NAME,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301652 .pm = &serial_omap_dev_pm_ops,
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301653 .of_match_table = of_match_ptr(omap_serial_of_match),
Govindraj.Rb6126332010-09-27 20:20:49 +05301654 },
1655};
1656
1657static int __init serial_omap_init(void)
1658{
1659 int ret;
1660
1661 ret = uart_register_driver(&serial_omap_reg);
1662 if (ret != 0)
1663 return ret;
1664 ret = platform_driver_register(&serial_omap_driver);
1665 if (ret != 0)
1666 uart_unregister_driver(&serial_omap_reg);
1667 return ret;
1668}
1669
1670static void __exit serial_omap_exit(void)
1671{
1672 platform_driver_unregister(&serial_omap_driver);
1673 uart_unregister_driver(&serial_omap_reg);
1674}
1675
1676module_init(serial_omap_init);
1677module_exit(serial_omap_exit);
1678
1679MODULE_DESCRIPTION("OMAP High Speed UART driver");
1680MODULE_LICENSE("GPL");
1681MODULE_AUTHOR("Texas Instruments Inc");