blob: 881b652220b486c4c80358d041e170c4745c6718 [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>
Felipe Balbid21e4002012-09-06 15:45:38 +030035#include <linux/platform_device.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053036#include <linux/io.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053037#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>
NeilBrown9574f362012-07-30 10:30:26 +100042#include <linux/gpio.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053043
Govindraj.Rb6126332010-09-27 20:20:49 +053044#include <plat/omap-serial.h>
45
Govindraj.R7c77c8d2012-04-03 19:12:34 +053046#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
47
48#define OMAP_UART_REV_42 0x0402
49#define OMAP_UART_REV_46 0x0406
50#define OMAP_UART_REV_52 0x0502
51#define OMAP_UART_REV_63 0x0603
52
Rajendra Nayak8fe789d2011-12-14 17:25:44 +053053#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz*/
54
Paul Walmsley0ba5f662012-01-25 19:50:36 -070055/* SCR register bitmasks */
56#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
57
58/* FCR register bitmasks */
59#define OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT 6
60#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
61
Govindraj.R7c77c8d2012-04-03 19:12:34 +053062/* MVR register bitmasks */
63#define OMAP_UART_MVR_SCHEME_SHIFT 30
64
65#define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
66#define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
67#define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
68
69#define OMAP_UART_MVR_MAJ_MASK 0x700
70#define OMAP_UART_MVR_MAJ_SHIFT 8
71#define OMAP_UART_MVR_MIN_MASK 0x3f
72
Govindraj.Rb6126332010-09-27 20:20:49 +053073static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
74
75/* Forward declaration of functions */
Govindraj.R94734742011-11-07 19:00:33 +053076static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +053077
Govindraj.R2fd14962011-11-09 17:41:21 +053078static struct workqueue_struct *serial_omap_uart_wq;
Govindraj.Rb6126332010-09-27 20:20:49 +053079
80static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
81{
82 offset <<= up->port.regshift;
83 return readw(up->port.membase + offset);
84}
85
86static inline void serial_out(struct uart_omap_port *up, int offset, int value)
87{
88 offset <<= up->port.regshift;
89 writew(value, up->port.membase + offset);
90}
91
92static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
93{
94 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
95 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
96 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
97 serial_out(up, UART_FCR, 0);
98}
99
Felipe Balbie5b57c02012-08-23 13:32:42 +0300100static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
101{
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300102 struct omap_uart_port_info *pdata = up->dev->platform_data;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300103
104 if (!pdata->get_context_loss_count)
105 return 0;
106
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300107 return pdata->get_context_loss_count(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300108}
109
110static void serial_omap_set_forceidle(struct uart_omap_port *up)
111{
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300112 struct omap_uart_port_info *pdata = up->dev->platform_data;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300113
114 if (pdata->set_forceidle)
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300115 pdata->set_forceidle(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300116}
117
118static void serial_omap_set_noidle(struct uart_omap_port *up)
119{
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300120 struct omap_uart_port_info *pdata = up->dev->platform_data;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300121
122 if (pdata->set_noidle)
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300123 pdata->set_noidle(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300124}
125
126static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
127{
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300128 struct omap_uart_port_info *pdata = up->dev->platform_data;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300129
130 if (pdata->enable_wakeup)
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300131 pdata->enable_wakeup(up->dev, enable);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300132}
133
Govindraj.Rb6126332010-09-27 20:20:49 +0530134/*
135 * serial_omap_get_divisor - calculate divisor value
136 * @port: uart port info
137 * @baud: baudrate for which divisor needs to be calculated.
138 *
139 * We have written our own function to get the divisor so as to support
140 * 13x mode. 3Mbps Baudrate as an different divisor.
141 * Reference OMAP TRM Chapter 17:
142 * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
143 * referring to oversampling - divisor value
144 * baudrate 460,800 to 3,686,400 all have divisor 13
145 * except 3,000,000 which has divisor value 16
146 */
147static unsigned int
148serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
149{
150 unsigned int divisor;
151
152 if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
153 divisor = 13;
154 else
155 divisor = 16;
156 return port->uartclk/(baud * divisor);
157}
158
Govindraj.Rb6126332010-09-27 20:20:49 +0530159static void serial_omap_enable_ms(struct uart_port *port)
160{
Felipe Balbic990f352012-08-23 13:32:41 +0300161 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530162
Rajendra Nayakba774332011-12-14 17:25:43 +0530163 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530164
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300165 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530166 up->ier |= UART_IER_MSI;
167 serial_out(up, UART_IER, up->ier);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300168 pm_runtime_mark_last_busy(up->dev);
169 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530170}
171
172static void serial_omap_stop_tx(struct uart_port *port)
173{
Felipe Balbic990f352012-08-23 13:32:41 +0300174 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530175
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300176 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530177 if (up->ier & UART_IER_THRI) {
178 up->ier &= ~UART_IER_THRI;
179 serial_out(up, UART_IER, up->ier);
180 }
Govindraj.Rfcdca752011-02-28 18:12:23 +0530181
Felipe Balbi49457432012-09-06 15:45:21 +0300182 serial_omap_set_forceidle(up);
Paul Walmsleybe4b0282012-01-25 19:50:52 -0700183
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300184 pm_runtime_mark_last_busy(up->dev);
185 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530186}
187
188static void serial_omap_stop_rx(struct uart_port *port)
189{
Felipe Balbic990f352012-08-23 13:32:41 +0300190 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530191
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300192 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530193 up->ier &= ~UART_IER_RLSI;
194 up->port.read_status_mask &= ~UART_LSR_DR;
195 serial_out(up, UART_IER, up->ier);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300196 pm_runtime_mark_last_busy(up->dev);
197 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530198}
199
Felipe Balbibf63a082012-09-06 15:45:25 +0300200static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
Govindraj.Rb6126332010-09-27 20:20:49 +0530201{
202 struct circ_buf *xmit = &up->port.state->xmit;
203 int count;
204
Felipe Balbibf63a082012-09-06 15:45:25 +0300205 if (!(lsr & UART_LSR_THRE))
206 return;
207
Govindraj.Rb6126332010-09-27 20:20:49 +0530208 if (up->port.x_char) {
209 serial_out(up, UART_TX, up->port.x_char);
210 up->port.icount.tx++;
211 up->port.x_char = 0;
212 return;
213 }
214 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
215 serial_omap_stop_tx(&up->port);
216 return;
217 }
Greg Kroah-Hartmanaf681ca2012-01-26 11:14:42 -0800218 count = up->port.fifosize / 4;
Govindraj.Rb6126332010-09-27 20:20:49 +0530219 do {
220 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
221 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
222 up->port.icount.tx++;
223 if (uart_circ_empty(xmit))
224 break;
225 } while (--count > 0);
226
Ruchika Kharwar0324a822012-09-06 15:45:34 +0300227 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
228 spin_unlock(&up->port.lock);
Govindraj.Rb6126332010-09-27 20:20:49 +0530229 uart_write_wakeup(&up->port);
Ruchika Kharwar0324a822012-09-06 15:45:34 +0300230 spin_lock(&up->port.lock);
231 }
Govindraj.Rb6126332010-09-27 20:20:49 +0530232
233 if (uart_circ_empty(xmit))
234 serial_omap_stop_tx(&up->port);
235}
236
237static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
238{
239 if (!(up->ier & UART_IER_THRI)) {
240 up->ier |= UART_IER_THRI;
241 serial_out(up, UART_IER, up->ier);
242 }
243}
244
245static void serial_omap_start_tx(struct uart_port *port)
246{
Felipe Balbic990f352012-08-23 13:32:41 +0300247 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530248
Felipe Balbi49457432012-09-06 15:45:21 +0300249 pm_runtime_get_sync(up->dev);
250 serial_omap_enable_ier_thri(up);
251 serial_omap_set_noidle(up);
252 pm_runtime_mark_last_busy(up->dev);
253 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530254}
255
256static unsigned int check_modem_status(struct uart_omap_port *up)
257{
258 unsigned int status;
259
260 status = serial_in(up, UART_MSR);
261 status |= up->msr_saved_flags;
262 up->msr_saved_flags = 0;
263 if ((status & UART_MSR_ANY_DELTA) == 0)
264 return status;
265
266 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
267 up->port.state != NULL) {
268 if (status & UART_MSR_TERI)
269 up->port.icount.rng++;
270 if (status & UART_MSR_DDSR)
271 up->port.icount.dsr++;
272 if (status & UART_MSR_DDCD)
273 uart_handle_dcd_change
274 (&up->port, status & UART_MSR_DCD);
275 if (status & UART_MSR_DCTS)
276 uart_handle_cts_change
277 (&up->port, status & UART_MSR_CTS);
278 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
279 }
280
281 return status;
282}
283
Felipe Balbi72256cb2012-09-06 15:45:24 +0300284static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr)
285{
286 unsigned int flag;
287
288 up->port.icount.rx++;
289 flag = TTY_NORMAL;
290
291 if (lsr & UART_LSR_BI) {
292 flag = TTY_BREAK;
293 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
294 up->port.icount.brk++;
295 /*
296 * We do the SysRQ and SAK checking
297 * here because otherwise the break
298 * may get masked by ignore_status_mask
299 * or read_status_mask.
300 */
301 if (uart_handle_break(&up->port))
302 return;
303
304 }
305
306 if (lsr & UART_LSR_PE) {
307 flag = TTY_PARITY;
308 up->port.icount.parity++;
309 }
310
311 if (lsr & UART_LSR_FE) {
312 flag = TTY_FRAME;
313 up->port.icount.frame++;
314 }
315
316 if (lsr & UART_LSR_OE)
317 up->port.icount.overrun++;
318
319#ifdef CONFIG_SERIAL_OMAP_CONSOLE
320 if (up->port.line == up->port.cons->index) {
321 /* Recover the break flag from console xmit */
322 lsr |= up->lsr_break_flag;
323 }
324#endif
325 uart_insert_char(&up->port, lsr, UART_LSR_OE, 0, flag);
326}
327
328static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr)
329{
330 unsigned char ch = 0;
331 unsigned int flag;
332
333 if (!(lsr & UART_LSR_DR))
334 return;
335
336 ch = serial_in(up, UART_RX);
337 flag = TTY_NORMAL;
338 up->port.icount.rx++;
339
340 if (uart_handle_sysrq_char(&up->port, ch))
341 return;
342
343 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
344}
345
Govindraj.Rb6126332010-09-27 20:20:49 +0530346/**
347 * serial_omap_irq() - This handles the interrupt from one port
348 * @irq: uart port irq number
349 * @dev_id: uart port info
350 */
Felipe Balbi52c55132012-09-06 15:45:33 +0300351static irqreturn_t serial_omap_irq(int irq, void *dev_id)
Govindraj.Rb6126332010-09-27 20:20:49 +0530352{
353 struct uart_omap_port *up = dev_id;
Felipe Balbi72256cb2012-09-06 15:45:24 +0300354 struct tty_struct *tty = up->port.state->port.tty;
Govindraj.Rb6126332010-09-27 20:20:49 +0530355 unsigned int iir, lsr;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300356 unsigned int type;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300357 irqreturn_t ret = IRQ_NONE;
Felipe Balbi72256cb2012-09-06 15:45:24 +0300358 int max_count = 256;
Govindraj.Rb6126332010-09-27 20:20:49 +0530359
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300360 spin_lock(&up->port.lock);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300361 pm_runtime_get_sync(up->dev);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300362
Felipe Balbi72256cb2012-09-06 15:45:24 +0300363 do {
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300364 iir = serial_in(up, UART_IIR);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300365 if (iir & UART_IIR_NO_INT)
366 break;
Govindraj.Rb6126332010-09-27 20:20:49 +0530367
Felipe Balbi72256cb2012-09-06 15:45:24 +0300368 ret = IRQ_HANDLED;
369 lsr = serial_in(up, UART_LSR);
370
371 /* extract IRQ type from IIR register */
372 type = iir & 0x3e;
373
374 switch (type) {
375 case UART_IIR_MSI:
376 check_modem_status(up);
377 break;
378 case UART_IIR_THRI:
Felipe Balbibf63a082012-09-06 15:45:25 +0300379 transmit_chars(up, lsr);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300380 break;
381 case UART_IIR_RX_TIMEOUT:
382 /* FALLTHROUGH */
383 case UART_IIR_RDI:
384 serial_omap_rdi(up, lsr);
385 break;
386 case UART_IIR_RLSI:
387 serial_omap_rlsi(up, lsr);
388 break;
389 case UART_IIR_CTS_RTS_DSR:
390 /* simply try again */
391 break;
392 case UART_IIR_XOFF:
393 /* FALLTHROUGH */
394 default:
395 break;
396 }
397 } while (!(iir & UART_IIR_NO_INT) && max_count--);
398
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300399 spin_unlock(&up->port.lock);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300400
401 tty_flip_buffer_push(tty);
402
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300403 pm_runtime_mark_last_busy(up->dev);
404 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530405 up->port_activity = jiffies;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300406
407 return ret;
Govindraj.Rb6126332010-09-27 20:20:49 +0530408}
409
410static unsigned int serial_omap_tx_empty(struct uart_port *port)
411{
Felipe Balbic990f352012-08-23 13:32:41 +0300412 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530413 unsigned long flags = 0;
414 unsigned int ret = 0;
415
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300416 pm_runtime_get_sync(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +0530417 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530418 spin_lock_irqsave(&up->port.lock, flags);
419 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
420 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300421 pm_runtime_mark_last_busy(up->dev);
422 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530423 return ret;
424}
425
426static unsigned int serial_omap_get_mctrl(struct uart_port *port)
427{
Felipe Balbic990f352012-08-23 13:32:41 +0300428 struct uart_omap_port *up = to_uart_omap_port(port);
Shubhrajyoti D514f31d2011-11-21 15:43:28 +0530429 unsigned int status;
Govindraj.Rb6126332010-09-27 20:20:49 +0530430 unsigned int ret = 0;
431
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300432 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530433 status = check_modem_status(up);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300434 pm_runtime_mark_last_busy(up->dev);
435 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530436
Rajendra Nayakba774332011-12-14 17:25:43 +0530437 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530438
439 if (status & UART_MSR_DCD)
440 ret |= TIOCM_CAR;
441 if (status & UART_MSR_RI)
442 ret |= TIOCM_RNG;
443 if (status & UART_MSR_DSR)
444 ret |= TIOCM_DSR;
445 if (status & UART_MSR_CTS)
446 ret |= TIOCM_CTS;
447 return ret;
448}
449
450static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
451{
Felipe Balbic990f352012-08-23 13:32:41 +0300452 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530453 unsigned char mcr = 0;
454
Rajendra Nayakba774332011-12-14 17:25:43 +0530455 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530456 if (mctrl & TIOCM_RTS)
457 mcr |= UART_MCR_RTS;
458 if (mctrl & TIOCM_DTR)
459 mcr |= UART_MCR_DTR;
460 if (mctrl & TIOCM_OUT1)
461 mcr |= UART_MCR_OUT1;
462 if (mctrl & TIOCM_OUT2)
463 mcr |= UART_MCR_OUT2;
464 if (mctrl & TIOCM_LOOP)
465 mcr |= UART_MCR_LOOP;
466
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300467 pm_runtime_get_sync(up->dev);
Govindraj.Rc538d202011-11-07 18:57:03 +0530468 up->mcr = serial_in(up, UART_MCR);
469 up->mcr |= mcr;
470 serial_out(up, UART_MCR, up->mcr);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300471 pm_runtime_mark_last_busy(up->dev);
472 pm_runtime_put_autosuspend(up->dev);
NeilBrown9574f362012-07-30 10:30:26 +1000473
474 if (gpio_is_valid(up->DTR_gpio) &&
475 !!(mctrl & TIOCM_DTR) != up->DTR_active) {
476 up->DTR_active = !up->DTR_active;
477 if (gpio_cansleep(up->DTR_gpio))
478 schedule_work(&up->qos_work);
479 else
480 gpio_set_value(up->DTR_gpio,
481 up->DTR_active != up->DTR_inverted);
482 }
Govindraj.Rb6126332010-09-27 20:20:49 +0530483}
484
485static void serial_omap_break_ctl(struct uart_port *port, int break_state)
486{
Felipe Balbic990f352012-08-23 13:32:41 +0300487 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530488 unsigned long flags = 0;
489
Rajendra Nayakba774332011-12-14 17:25:43 +0530490 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300491 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530492 spin_lock_irqsave(&up->port.lock, flags);
493 if (break_state == -1)
494 up->lcr |= UART_LCR_SBC;
495 else
496 up->lcr &= ~UART_LCR_SBC;
497 serial_out(up, UART_LCR, up->lcr);
498 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300499 pm_runtime_mark_last_busy(up->dev);
500 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530501}
502
503static int serial_omap_startup(struct uart_port *port)
504{
Felipe Balbic990f352012-08-23 13:32:41 +0300505 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530506 unsigned long flags = 0;
507 int retval;
508
509 /*
510 * Allocate the IRQ
511 */
512 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
513 up->name, up);
514 if (retval)
515 return retval;
516
Rajendra Nayakba774332011-12-14 17:25:43 +0530517 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530518
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300519 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530520 /*
521 * Clear the FIFO buffers and disable them.
522 * (they will be reenabled in set_termios())
523 */
524 serial_omap_clear_fifos(up);
525 /* For Hardware flow control */
526 serial_out(up, UART_MCR, UART_MCR_RTS);
527
528 /*
529 * Clear the interrupt registers.
530 */
531 (void) serial_in(up, UART_LSR);
532 if (serial_in(up, UART_LSR) & UART_LSR_DR)
533 (void) serial_in(up, UART_RX);
534 (void) serial_in(up, UART_IIR);
535 (void) serial_in(up, UART_MSR);
536
537 /*
538 * Now, initialize the UART
539 */
540 serial_out(up, UART_LCR, UART_LCR_WLEN8);
541 spin_lock_irqsave(&up->port.lock, flags);
542 /*
543 * Most PC uarts need OUT2 raised to enable interrupts.
544 */
545 up->port.mctrl |= TIOCM_OUT2;
546 serial_omap_set_mctrl(&up->port, up->port.mctrl);
547 spin_unlock_irqrestore(&up->port.lock, flags);
548
549 up->msr_saved_flags = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530550 /*
551 * Finally, enable interrupts. Note: Modem status interrupts
552 * are set via set_termios(), which will be occurring imminently
553 * anyway, so we don't enable them here.
554 */
555 up->ier = UART_IER_RLSI | UART_IER_RDI;
556 serial_out(up, UART_IER, up->ier);
557
Jarkko Nikula78841462011-01-24 17:51:22 +0200558 /* Enable module level wake up */
559 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
560
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300561 pm_runtime_mark_last_busy(up->dev);
562 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530563 up->port_activity = jiffies;
564 return 0;
565}
566
567static void serial_omap_shutdown(struct uart_port *port)
568{
Felipe Balbic990f352012-08-23 13:32:41 +0300569 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530570 unsigned long flags = 0;
571
Rajendra Nayakba774332011-12-14 17:25:43 +0530572 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530573
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300574 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530575 /*
576 * Disable interrupts from this port
577 */
578 up->ier = 0;
579 serial_out(up, UART_IER, 0);
580
581 spin_lock_irqsave(&up->port.lock, flags);
582 up->port.mctrl &= ~TIOCM_OUT2;
583 serial_omap_set_mctrl(&up->port, up->port.mctrl);
584 spin_unlock_irqrestore(&up->port.lock, flags);
585
586 /*
587 * Disable break condition and FIFOs
588 */
589 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
590 serial_omap_clear_fifos(up);
591
592 /*
593 * Read data port to reset things, and then free the irq
594 */
595 if (serial_in(up, UART_LSR) & UART_LSR_DR)
596 (void) serial_in(up, UART_RX);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530597
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300598 pm_runtime_mark_last_busy(up->dev);
599 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530600 free_irq(up->port.irq, up);
601}
602
603static inline void
604serial_omap_configure_xonxoff
605 (struct uart_omap_port *up, struct ktermios *termios)
606{
Govindraj.Rb6126332010-09-27 20:20:49 +0530607 up->lcr = serial_in(up, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800608 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530609 up->efr = serial_in(up, UART_EFR);
610 serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
611
612 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
613 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
614
615 /* clear SW control mode bits */
Govindraj.Rc538d202011-11-07 18:57:03 +0530616 up->efr &= OMAP_UART_SW_CLR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530617
618 /*
619 * IXON Flag:
Vikram Pandita957ee722012-09-06 15:45:37 +0300620 * Flow control for OMAP.TX
621 * OMAP.RX should listen for XON/XOFF
Govindraj.Rb6126332010-09-27 20:20:49 +0530622 */
623 if (termios->c_iflag & IXON)
Vikram Pandita957ee722012-09-06 15:45:37 +0300624 up->efr |= OMAP_UART_SW_RX;
Govindraj.Rb6126332010-09-27 20:20:49 +0530625
626 /*
627 * IXOFF Flag:
Vikram Pandita957ee722012-09-06 15:45:37 +0300628 * Flow control for OMAP.RX
629 * OMAP.TX should send XON/XOFF
Govindraj.Rb6126332010-09-27 20:20:49 +0530630 */
631 if (termios->c_iflag & IXOFF)
Vikram Pandita957ee722012-09-06 15:45:37 +0300632 up->efr |= OMAP_UART_SW_TX;
Govindraj.Rb6126332010-09-27 20:20:49 +0530633
634 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800635 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530636
637 up->mcr = serial_in(up, UART_MCR);
638
639 /*
640 * IXANY Flag:
641 * Enable any character to restart output.
642 * Operation resumes after receiving any
643 * character after recognition of the XOFF character
644 */
645 if (termios->c_iflag & IXANY)
646 up->mcr |= UART_MCR_XONANY;
647
648 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800649 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530650 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
651 /* Enable special char function UARTi.EFR_REG[5] and
652 * load the new software flow control mode IXON or IXOFF
653 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
654 */
Govindraj.Rc538d202011-11-07 18:57:03 +0530655 serial_out(up, UART_EFR, up->efr | UART_EFR_SCD);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800656 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530657
658 serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
659 serial_out(up, UART_LCR, up->lcr);
660}
661
Govindraj.R2fd14962011-11-09 17:41:21 +0530662static void serial_omap_uart_qos_work(struct work_struct *work)
663{
664 struct uart_omap_port *up = container_of(work, struct uart_omap_port,
665 qos_work);
666
667 pm_qos_update_request(&up->pm_qos_request, up->latency);
NeilBrown9574f362012-07-30 10:30:26 +1000668 if (gpio_is_valid(up->DTR_gpio))
669 gpio_set_value_cansleep(up->DTR_gpio,
670 up->DTR_active != up->DTR_inverted);
Govindraj.R2fd14962011-11-09 17:41:21 +0530671}
672
Govindraj.Rb6126332010-09-27 20:20:49 +0530673static void
674serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
675 struct ktermios *old)
676{
Felipe Balbic990f352012-08-23 13:32:41 +0300677 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530678 unsigned char cval = 0;
679 unsigned char efr = 0;
680 unsigned long flags = 0;
681 unsigned int baud, quot;
682
683 switch (termios->c_cflag & CSIZE) {
684 case CS5:
685 cval = UART_LCR_WLEN5;
686 break;
687 case CS6:
688 cval = UART_LCR_WLEN6;
689 break;
690 case CS7:
691 cval = UART_LCR_WLEN7;
692 break;
693 default:
694 case CS8:
695 cval = UART_LCR_WLEN8;
696 break;
697 }
698
699 if (termios->c_cflag & CSTOPB)
700 cval |= UART_LCR_STOP;
701 if (termios->c_cflag & PARENB)
702 cval |= UART_LCR_PARITY;
703 if (!(termios->c_cflag & PARODD))
704 cval |= UART_LCR_EPAR;
705
706 /*
707 * Ask the core to calculate the divisor for us.
708 */
709
710 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
711 quot = serial_omap_get_divisor(port, baud);
712
Govindraj.R2fd14962011-11-09 17:41:21 +0530713 /* calculate wakeup latency constraint */
Paul Walmsley19723452012-01-25 19:50:56 -0700714 up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
Govindraj.R2fd14962011-11-09 17:41:21 +0530715 up->latency = up->calc_latency;
716 schedule_work(&up->qos_work);
717
Govindraj.Rc538d202011-11-07 18:57:03 +0530718 up->dll = quot & 0xff;
719 up->dlh = quot >> 8;
720 up->mdr1 = UART_OMAP_MDR1_DISABLE;
721
Govindraj.Rb6126332010-09-27 20:20:49 +0530722 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
723 UART_FCR_ENABLE_FIFO;
Govindraj.Rb6126332010-09-27 20:20:49 +0530724
725 /*
726 * Ok, we're now changing the port state. Do it with
727 * interrupts disabled.
728 */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300729 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530730 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 */
Govindraj.Rc538d202011-11-07 18:57:03 +0530773 up->lcr = cval;
Govindraj.R32212892011-11-07 18:58:55 +0530774 up->scr = OMAP_UART_SCR_TX_EMPTY;
Govindraj.Rb6126332010-09-27 20:20:49 +0530775
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 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800782 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530783 serial_out(up, UART_DLL, 0);
784 serial_out(up, UART_DLM, 0);
785 serial_out(up, UART_LCR, 0);
786
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800787 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530788
789 up->efr = serial_in(up, UART_EFR);
790 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
791
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800792 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530793 up->mcr = serial_in(up, UART_MCR);
794 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
795 /* FIFO ENABLE, DMA MODE */
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700796
797 up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
Paul Walmsley0a697b22012-01-21 00:27:40 -0700798
Felipe Balbi49457432012-09-06 15:45:21 +0300799 /* Set receive FIFO threshold to 1 byte */
800 up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
801 up->fcr |= (0x1 << OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT);
Greg Kroah-Hartman8a74e9f2012-01-26 11:15:18 -0800802
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700803 serial_out(up, UART_FCR, up->fcr);
804 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
805
Govindraj.Rc538d202011-11-07 18:57:03 +0530806 serial_out(up, UART_OMAP_SCR, up->scr);
807
Govindraj.Rb6126332010-09-27 20:20:49 +0530808 serial_out(up, UART_EFR, up->efr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800809 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530810 serial_out(up, UART_MCR, up->mcr);
811
812 /* Protocol, Baud Rate, and Interrupt Settings */
813
Govindraj.R94734742011-11-07 19:00:33 +0530814 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
815 serial_omap_mdr1_errataset(up, up->mdr1);
816 else
817 serial_out(up, UART_OMAP_MDR1, up->mdr1);
818
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800819 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530820
821 up->efr = serial_in(up, UART_EFR);
822 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
823
824 serial_out(up, UART_LCR, 0);
825 serial_out(up, UART_IER, 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800826 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530827
Govindraj.Rc538d202011-11-07 18:57:03 +0530828 serial_out(up, UART_DLL, up->dll); /* LS of divisor */
829 serial_out(up, UART_DLM, up->dlh); /* MS of divisor */
Govindraj.Rb6126332010-09-27 20:20:49 +0530830
831 serial_out(up, UART_LCR, 0);
832 serial_out(up, UART_IER, up->ier);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800833 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530834
835 serial_out(up, UART_EFR, up->efr);
836 serial_out(up, UART_LCR, cval);
837
838 if (baud > 230400 && baud != 3000000)
Govindraj.Rc538d202011-11-07 18:57:03 +0530839 up->mdr1 = UART_OMAP_MDR1_13X_MODE;
Govindraj.Rb6126332010-09-27 20:20:49 +0530840 else
Govindraj.Rc538d202011-11-07 18:57:03 +0530841 up->mdr1 = UART_OMAP_MDR1_16X_MODE;
842
Govindraj.R94734742011-11-07 19:00:33 +0530843 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
844 serial_omap_mdr1_errataset(up, up->mdr1);
845 else
846 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +0530847
848 /* Hardware Flow Control Configuration */
849
850 if (termios->c_cflag & CRTSCTS) {
851 efr |= (UART_EFR_CTS | UART_EFR_RTS);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800852 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530853
854 up->mcr = serial_in(up, UART_MCR);
855 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
856
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800857 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530858 up->efr = serial_in(up, UART_EFR);
859 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
860
861 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
862 serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800863 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530864 serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
865 serial_out(up, UART_LCR, cval);
866 }
867
868 serial_omap_set_mctrl(&up->port, up->port.mctrl);
869 /* Software Flow Control Configuration */
Nick Pellyb280a972011-07-15 13:53:08 -0700870 serial_omap_configure_xonxoff(up, termios);
Govindraj.Rb6126332010-09-27 20:20:49 +0530871
872 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300873 pm_runtime_mark_last_busy(up->dev);
874 pm_runtime_put_autosuspend(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +0530875 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530876}
877
Felipe Balbi9727faf2012-09-06 15:45:35 +0300878static int serial_omap_set_wake(struct uart_port *port, unsigned int state)
879{
880 struct uart_omap_port *up = to_uart_omap_port(port);
881
882 serial_omap_enable_wakeup(up, state);
883
884 return 0;
885}
886
Govindraj.Rb6126332010-09-27 20:20:49 +0530887static void
888serial_omap_pm(struct uart_port *port, unsigned int state,
889 unsigned int oldstate)
890{
Felipe Balbic990f352012-08-23 13:32:41 +0300891 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530892 unsigned char efr;
893
Rajendra Nayakba774332011-12-14 17:25:43 +0530894 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530895
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300896 pm_runtime_get_sync(up->dev);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800897 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530898 efr = serial_in(up, UART_EFR);
899 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
900 serial_out(up, UART_LCR, 0);
901
902 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800903 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530904 serial_out(up, UART_EFR, efr);
905 serial_out(up, UART_LCR, 0);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530906
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300907 if (!device_may_wakeup(up->dev)) {
Govindraj.Rfcdca752011-02-28 18:12:23 +0530908 if (!state)
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300909 pm_runtime_forbid(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530910 else
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300911 pm_runtime_allow(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530912 }
913
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300914 pm_runtime_mark_last_busy(up->dev);
915 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530916}
917
918static void serial_omap_release_port(struct uart_port *port)
919{
920 dev_dbg(port->dev, "serial_omap_release_port+\n");
921}
922
923static int serial_omap_request_port(struct uart_port *port)
924{
925 dev_dbg(port->dev, "serial_omap_request_port+\n");
926 return 0;
927}
928
929static void serial_omap_config_port(struct uart_port *port, int flags)
930{
Felipe Balbic990f352012-08-23 13:32:41 +0300931 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530932
933 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
Rajendra Nayakba774332011-12-14 17:25:43 +0530934 up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530935 up->port.type = PORT_OMAP;
936}
937
938static int
939serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
940{
941 /* we don't want the core code to modify any port params */
942 dev_dbg(port->dev, "serial_omap_verify_port+\n");
943 return -EINVAL;
944}
945
946static const char *
947serial_omap_type(struct uart_port *port)
948{
Felipe Balbic990f352012-08-23 13:32:41 +0300949 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530950
Rajendra Nayakba774332011-12-14 17:25:43 +0530951 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530952 return up->name;
953}
954
Govindraj.Rb6126332010-09-27 20:20:49 +0530955#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
956
957static inline void wait_for_xmitr(struct uart_omap_port *up)
958{
959 unsigned int status, tmout = 10000;
960
961 /* Wait up to 10ms for the character(s) to be sent. */
962 do {
963 status = serial_in(up, UART_LSR);
964
965 if (status & UART_LSR_BI)
966 up->lsr_break_flag = UART_LSR_BI;
967
968 if (--tmout == 0)
969 break;
970 udelay(1);
971 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
972
973 /* Wait up to 1s for flow control if necessary */
974 if (up->port.flags & UPF_CONS_FLOW) {
975 tmout = 1000000;
976 for (tmout = 1000000; tmout; tmout--) {
977 unsigned int msr = serial_in(up, UART_MSR);
978
979 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
980 if (msr & UART_MSR_CTS)
981 break;
982
983 udelay(1);
984 }
985 }
986}
987
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +0100988#ifdef CONFIG_CONSOLE_POLL
989
990static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
991{
Felipe Balbic990f352012-08-23 13:32:41 +0300992 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530993
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300994 pm_runtime_get_sync(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +0100995 wait_for_xmitr(up);
996 serial_out(up, UART_TX, ch);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300997 pm_runtime_mark_last_busy(up->dev);
998 pm_runtime_put_autosuspend(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +0100999}
1000
1001static int serial_omap_poll_get_char(struct uart_port *port)
1002{
Felipe Balbic990f352012-08-23 13:32:41 +03001003 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301004 unsigned int status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001005
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001006 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301007 status = serial_in(up, UART_LSR);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001008 if (!(status & UART_LSR_DR)) {
1009 status = NO_POLL_CHAR;
1010 goto out;
1011 }
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001012
Govindraj.Rfcdca752011-02-28 18:12:23 +05301013 status = serial_in(up, UART_RX);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001014
1015out:
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001016 pm_runtime_mark_last_busy(up->dev);
1017 pm_runtime_put_autosuspend(up->dev);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001018
Govindraj.Rfcdca752011-02-28 18:12:23 +05301019 return status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001020}
1021
1022#endif /* CONFIG_CONSOLE_POLL */
1023
1024#ifdef CONFIG_SERIAL_OMAP_CONSOLE
1025
1026static struct uart_omap_port *serial_omap_console_ports[4];
1027
1028static struct uart_driver serial_omap_reg;
1029
Govindraj.Rb6126332010-09-27 20:20:49 +05301030static void serial_omap_console_putchar(struct uart_port *port, int ch)
1031{
Felipe Balbic990f352012-08-23 13:32:41 +03001032 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301033
1034 wait_for_xmitr(up);
1035 serial_out(up, UART_TX, ch);
1036}
1037
1038static void
1039serial_omap_console_write(struct console *co, const char *s,
1040 unsigned int count)
1041{
1042 struct uart_omap_port *up = serial_omap_console_ports[co->index];
1043 unsigned long flags;
1044 unsigned int ier;
1045 int locked = 1;
1046
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001047 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301048
Govindraj.Rb6126332010-09-27 20:20:49 +05301049 local_irq_save(flags);
1050 if (up->port.sysrq)
1051 locked = 0;
1052 else if (oops_in_progress)
1053 locked = spin_trylock(&up->port.lock);
1054 else
1055 spin_lock(&up->port.lock);
1056
1057 /*
1058 * First save the IER then disable the interrupts
1059 */
1060 ier = serial_in(up, UART_IER);
1061 serial_out(up, UART_IER, 0);
1062
1063 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
1064
1065 /*
1066 * Finally, wait for transmitter to become empty
1067 * and restore the IER
1068 */
1069 wait_for_xmitr(up);
1070 serial_out(up, UART_IER, ier);
1071 /*
1072 * The receive handling will happen properly because the
1073 * receive ready bit will still be set; it is not cleared
1074 * on read. However, modem control will not, we must
1075 * call it if we have saved something in the saved flags
1076 * while processing with interrupts off.
1077 */
1078 if (up->msr_saved_flags)
1079 check_modem_status(up);
1080
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001081 pm_runtime_mark_last_busy(up->dev);
1082 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301083 if (locked)
1084 spin_unlock(&up->port.lock);
1085 local_irq_restore(flags);
1086}
1087
1088static int __init
1089serial_omap_console_setup(struct console *co, char *options)
1090{
1091 struct uart_omap_port *up;
1092 int baud = 115200;
1093 int bits = 8;
1094 int parity = 'n';
1095 int flow = 'n';
1096
1097 if (serial_omap_console_ports[co->index] == NULL)
1098 return -ENODEV;
1099 up = serial_omap_console_ports[co->index];
1100
1101 if (options)
1102 uart_parse_options(options, &baud, &parity, &bits, &flow);
1103
1104 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1105}
1106
1107static struct console serial_omap_console = {
1108 .name = OMAP_SERIAL_NAME,
1109 .write = serial_omap_console_write,
1110 .device = uart_console_device,
1111 .setup = serial_omap_console_setup,
1112 .flags = CON_PRINTBUFFER,
1113 .index = -1,
1114 .data = &serial_omap_reg,
1115};
1116
1117static void serial_omap_add_console_port(struct uart_omap_port *up)
1118{
Rajendra Nayakba774332011-12-14 17:25:43 +05301119 serial_omap_console_ports[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301120}
1121
1122#define OMAP_CONSOLE (&serial_omap_console)
1123
1124#else
1125
1126#define OMAP_CONSOLE NULL
1127
1128static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1129{}
1130
1131#endif
1132
1133static struct uart_ops serial_omap_pops = {
1134 .tx_empty = serial_omap_tx_empty,
1135 .set_mctrl = serial_omap_set_mctrl,
1136 .get_mctrl = serial_omap_get_mctrl,
1137 .stop_tx = serial_omap_stop_tx,
1138 .start_tx = serial_omap_start_tx,
1139 .stop_rx = serial_omap_stop_rx,
1140 .enable_ms = serial_omap_enable_ms,
1141 .break_ctl = serial_omap_break_ctl,
1142 .startup = serial_omap_startup,
1143 .shutdown = serial_omap_shutdown,
1144 .set_termios = serial_omap_set_termios,
1145 .pm = serial_omap_pm,
Felipe Balbi9727faf2012-09-06 15:45:35 +03001146 .set_wake = serial_omap_set_wake,
Govindraj.Rb6126332010-09-27 20:20:49 +05301147 .type = serial_omap_type,
1148 .release_port = serial_omap_release_port,
1149 .request_port = serial_omap_request_port,
1150 .config_port = serial_omap_config_port,
1151 .verify_port = serial_omap_verify_port,
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001152#ifdef CONFIG_CONSOLE_POLL
1153 .poll_put_char = serial_omap_poll_put_char,
1154 .poll_get_char = serial_omap_poll_get_char,
1155#endif
Govindraj.Rb6126332010-09-27 20:20:49 +05301156};
1157
1158static struct uart_driver serial_omap_reg = {
1159 .owner = THIS_MODULE,
1160 .driver_name = "OMAP-SERIAL",
1161 .dev_name = OMAP_SERIAL_NAME,
1162 .nr = OMAP_MAX_HSUART_PORTS,
1163 .cons = OMAP_CONSOLE,
1164};
1165
Shubhrajyoti D3bc4f0d2012-01-16 15:52:36 +05301166#ifdef CONFIG_PM_SLEEP
Govindraj.Rfcdca752011-02-28 18:12:23 +05301167static int serial_omap_suspend(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301168{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301169 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301170
Govindraj.R2fd14962011-11-09 17:41:21 +05301171 if (up) {
Govindraj.Rb6126332010-09-27 20:20:49 +05301172 uart_suspend_port(&serial_omap_reg, &up->port);
Govindraj.R2fd14962011-11-09 17:41:21 +05301173 flush_work_sync(&up->qos_work);
1174 }
1175
Govindraj.Rb6126332010-09-27 20:20:49 +05301176 return 0;
1177}
1178
Govindraj.Rfcdca752011-02-28 18:12:23 +05301179static int serial_omap_resume(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301180{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301181 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301182
1183 if (up)
1184 uart_resume_port(&serial_omap_reg, &up->port);
1185 return 0;
1186}
Govindraj.Rfcdca752011-02-28 18:12:23 +05301187#endif
Govindraj.Rb6126332010-09-27 20:20:49 +05301188
Felipe Balbi6d608ef2012-09-06 15:45:32 +03001189static void __devinit omap_serial_fill_features_erratas(struct uart_omap_port *up)
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301190{
1191 u32 mvr, scheme;
1192 u16 revision, major, minor;
1193
1194 mvr = serial_in(up, UART_OMAP_MVER);
1195
1196 /* Check revision register scheme */
1197 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
1198
1199 switch (scheme) {
1200 case 0: /* Legacy Scheme: OMAP2/3 */
1201 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
1202 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
1203 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
1204 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
1205 break;
1206 case 1:
1207 /* New Scheme: OMAP4+ */
1208 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
1209 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
1210 OMAP_UART_MVR_MAJ_SHIFT;
1211 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
1212 break;
1213 default:
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001214 dev_warn(up->dev,
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301215 "Unknown %s revision, defaulting to highest\n",
1216 up->name);
1217 /* highest possible revision */
1218 major = 0xff;
1219 minor = 0xff;
1220 }
1221
1222 /* normalize revision for the driver */
1223 revision = UART_BUILD_REVISION(major, minor);
1224
1225 switch (revision) {
1226 case OMAP_UART_REV_46:
1227 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1228 UART_ERRATA_i291_DMA_FORCEIDLE);
1229 break;
1230 case OMAP_UART_REV_52:
1231 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1232 UART_ERRATA_i291_DMA_FORCEIDLE);
1233 break;
1234 case OMAP_UART_REV_63:
1235 up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
1236 break;
1237 default:
1238 break;
1239 }
1240}
1241
Felipe Balbi6d608ef2012-09-06 15:45:32 +03001242static __devinit struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301243{
1244 struct omap_uart_port_info *omap_up_info;
1245
1246 omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
1247 if (!omap_up_info)
1248 return NULL; /* out of memory */
1249
1250 of_property_read_u32(dev->of_node, "clock-frequency",
1251 &omap_up_info->uartclk);
1252 return omap_up_info;
1253}
1254
Felipe Balbi6d608ef2012-09-06 15:45:32 +03001255static int __devinit serial_omap_probe(struct platform_device *pdev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301256{
1257 struct uart_omap_port *up;
Felipe Balbi49457432012-09-06 15:45:21 +03001258 struct resource *mem, *irq;
Govindraj.Rb6126332010-09-27 20:20:49 +05301259 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
NeilBrown9574f362012-07-30 10:30:26 +10001260 int ret;
Govindraj.Rb6126332010-09-27 20:20:49 +05301261
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301262 if (pdev->dev.of_node)
1263 omap_up_info = of_get_uart_port_info(&pdev->dev);
1264
Govindraj.Rb6126332010-09-27 20:20:49 +05301265 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1266 if (!mem) {
1267 dev_err(&pdev->dev, "no mem resource?\n");
1268 return -ENODEV;
1269 }
1270
1271 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1272 if (!irq) {
1273 dev_err(&pdev->dev, "no irq resource?\n");
1274 return -ENODEV;
1275 }
1276
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301277 if (!devm_request_mem_region(&pdev->dev, mem->start, resource_size(mem),
Joe Perches28f65c112011-06-09 09:13:32 -07001278 pdev->dev.driver->name)) {
Govindraj.Rb6126332010-09-27 20:20:49 +05301279 dev_err(&pdev->dev, "memory region already claimed\n");
1280 return -EBUSY;
1281 }
1282
NeilBrown9574f362012-07-30 10:30:26 +10001283 if (gpio_is_valid(omap_up_info->DTR_gpio) &&
1284 omap_up_info->DTR_present) {
1285 ret = gpio_request(omap_up_info->DTR_gpio, "omap-serial");
1286 if (ret < 0)
1287 return ret;
1288 ret = gpio_direction_output(omap_up_info->DTR_gpio,
1289 omap_up_info->DTR_inverted);
1290 if (ret < 0)
1291 return ret;
1292 }
1293
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301294 up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
1295 if (!up)
1296 return -ENOMEM;
1297
NeilBrown9574f362012-07-30 10:30:26 +10001298 if (gpio_is_valid(omap_up_info->DTR_gpio) &&
1299 omap_up_info->DTR_present) {
1300 up->DTR_gpio = omap_up_info->DTR_gpio;
1301 up->DTR_inverted = omap_up_info->DTR_inverted;
1302 } else
1303 up->DTR_gpio = -EINVAL;
1304 up->DTR_active = 0;
1305
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001306 up->dev = &pdev->dev;
Govindraj.Rb6126332010-09-27 20:20:49 +05301307 up->port.dev = &pdev->dev;
1308 up->port.type = PORT_OMAP;
1309 up->port.iotype = UPIO_MEM;
1310 up->port.irq = irq->start;
1311
1312 up->port.regshift = 2;
1313 up->port.fifosize = 64;
1314 up->port.ops = &serial_omap_pops;
Govindraj.Rb6126332010-09-27 20:20:49 +05301315
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301316 if (pdev->dev.of_node)
1317 up->port.line = of_alias_get_id(pdev->dev.of_node, "serial");
1318 else
1319 up->port.line = pdev->id;
1320
1321 if (up->port.line < 0) {
1322 dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
1323 up->port.line);
1324 ret = -ENODEV;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301325 goto err_port_line;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301326 }
1327
1328 sprintf(up->name, "OMAP UART%d", up->port.line);
Govindraj.Redd70ad2011-10-11 14:55:41 +05301329 up->port.mapbase = mem->start;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301330 up->port.membase = devm_ioremap(&pdev->dev, mem->start,
1331 resource_size(mem));
Govindraj.Redd70ad2011-10-11 14:55:41 +05301332 if (!up->port.membase) {
1333 dev_err(&pdev->dev, "can't ioremap UART\n");
1334 ret = -ENOMEM;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301335 goto err_ioremap;
Govindraj.Redd70ad2011-10-11 14:55:41 +05301336 }
1337
Govindraj.Rb6126332010-09-27 20:20:49 +05301338 up->port.flags = omap_up_info->flags;
Govindraj.Rb6126332010-09-27 20:20:49 +05301339 up->port.uartclk = omap_up_info->uartclk;
Rajendra Nayak8fe789d2011-12-14 17:25:44 +05301340 if (!up->port.uartclk) {
1341 up->port.uartclk = DEFAULT_CLK_SPEED;
1342 dev_warn(&pdev->dev, "No clock speed specified: using default:"
1343 "%d\n", DEFAULT_CLK_SPEED);
1344 }
Govindraj.Rb6126332010-09-27 20:20:49 +05301345
Govindraj.R2fd14962011-11-09 17:41:21 +05301346 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1347 up->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1348 pm_qos_add_request(&up->pm_qos_request,
1349 PM_QOS_CPU_DMA_LATENCY, up->latency);
1350 serial_omap_uart_wq = create_singlethread_workqueue(up->name);
1351 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
1352
Felipe Balbi93220dc2012-09-06 15:45:27 +03001353 platform_set_drvdata(pdev, up);
Ruchika Kharwar856e35b2012-09-06 15:45:31 +03001354 pm_runtime_enable(&pdev->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301355 pm_runtime_use_autosuspend(&pdev->dev);
1356 pm_runtime_set_autosuspend_delay(&pdev->dev,
Deepak Kc86845db2011-11-09 17:33:38 +05301357 omap_up_info->autosuspend_timeout);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301358
1359 pm_runtime_irq_safe(&pdev->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301360 pm_runtime_get_sync(&pdev->dev);
1361
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301362 omap_serial_fill_features_erratas(up);
1363
Rajendra Nayakba774332011-12-14 17:25:43 +05301364 ui[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301365 serial_omap_add_console_port(up);
1366
1367 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1368 if (ret != 0)
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301369 goto err_add_port;
Govindraj.Rb6126332010-09-27 20:20:49 +05301370
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001371 pm_runtime_mark_last_busy(up->dev);
1372 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301373 return 0;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301374
1375err_add_port:
1376 pm_runtime_put(&pdev->dev);
1377 pm_runtime_disable(&pdev->dev);
1378err_ioremap:
1379err_port_line:
Govindraj.Rb6126332010-09-27 20:20:49 +05301380 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1381 pdev->id, __func__, ret);
Govindraj.Rb6126332010-09-27 20:20:49 +05301382 return ret;
1383}
1384
Felipe Balbi6d608ef2012-09-06 15:45:32 +03001385static int __devexit serial_omap_remove(struct platform_device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301386{
1387 struct uart_omap_port *up = platform_get_drvdata(dev);
1388
Felipe Balbi7e9c8e72012-09-06 15:45:29 +03001389 pm_runtime_put_sync(up->dev);
Felipe Balbi1b42c8b2012-09-06 15:45:28 +03001390 pm_runtime_disable(up->dev);
1391 uart_remove_one_port(&serial_omap_reg, &up->port);
1392 pm_qos_remove_request(&up->pm_qos_request);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301393
Govindraj.Rb6126332010-09-27 20:20:49 +05301394 return 0;
1395}
1396
Govindraj.R94734742011-11-07 19:00:33 +05301397/*
1398 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
1399 * The access to uart register after MDR1 Access
1400 * causes UART to corrupt data.
1401 *
1402 * Need a delay =
1403 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
1404 * give 10 times as much
1405 */
1406static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
1407{
1408 u8 timeout = 255;
1409
1410 serial_out(up, UART_OMAP_MDR1, mdr1);
1411 udelay(2);
1412 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
1413 UART_FCR_CLEAR_RCVR);
1414 /*
1415 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
1416 * TX_FIFO_E bit is 1.
1417 */
1418 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
1419 (UART_LSR_THRE | UART_LSR_DR))) {
1420 timeout--;
1421 if (!timeout) {
1422 /* Should *never* happen. we warn and carry on */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001423 dev_crit(up->dev, "Errata i202: timedout %x\n",
Govindraj.R94734742011-11-07 19:00:33 +05301424 serial_in(up, UART_LSR));
1425 break;
1426 }
1427 udelay(1);
1428 }
1429}
1430
Shubhrajyoti Db5148852012-01-16 15:52:37 +05301431#ifdef CONFIG_PM_RUNTIME
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301432static void serial_omap_restore_context(struct uart_omap_port *up)
1433{
Govindraj.R94734742011-11-07 19:00:33 +05301434 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1435 serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
1436 else
1437 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
1438
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301439 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1440 serial_out(up, UART_EFR, UART_EFR_ECB);
1441 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1442 serial_out(up, UART_IER, 0x0);
1443 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301444 serial_out(up, UART_DLL, up->dll);
1445 serial_out(up, UART_DLM, up->dlh);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301446 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1447 serial_out(up, UART_IER, up->ier);
1448 serial_out(up, UART_FCR, up->fcr);
1449 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1450 serial_out(up, UART_MCR, up->mcr);
1451 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301452 serial_out(up, UART_OMAP_SCR, up->scr);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301453 serial_out(up, UART_EFR, up->efr);
1454 serial_out(up, UART_LCR, up->lcr);
Govindraj.R94734742011-11-07 19:00:33 +05301455 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1456 serial_omap_mdr1_errataset(up, up->mdr1);
1457 else
1458 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301459}
1460
Govindraj.Rfcdca752011-02-28 18:12:23 +05301461static int serial_omap_runtime_suspend(struct device *dev)
1462{
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301463 struct uart_omap_port *up = dev_get_drvdata(dev);
1464 struct omap_uart_port_info *pdata = dev->platform_data;
1465
1466 if (!up)
1467 return -EINVAL;
1468
Felipe Balbie5b57c02012-08-23 13:32:42 +03001469 if (!pdata)
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301470 return 0;
1471
Felipe Balbie5b57c02012-08-23 13:32:42 +03001472 up->context_loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301473
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301474 if (device_may_wakeup(dev)) {
1475 if (!up->wakeups_enabled) {
Felipe Balbie5b57c02012-08-23 13:32:42 +03001476 serial_omap_enable_wakeup(up, true);
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301477 up->wakeups_enabled = true;
1478 }
1479 } else {
1480 if (up->wakeups_enabled) {
Felipe Balbie5b57c02012-08-23 13:32:42 +03001481 serial_omap_enable_wakeup(up, false);
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301482 up->wakeups_enabled = false;
1483 }
1484 }
1485
Govindraj.R2fd14962011-11-09 17:41:21 +05301486 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1487 schedule_work(&up->qos_work);
1488
Govindraj.Rfcdca752011-02-28 18:12:23 +05301489 return 0;
1490}
1491
1492static int serial_omap_runtime_resume(struct device *dev)
1493{
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301494 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301495 struct omap_uart_port_info *pdata = dev->platform_data;
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301496
Cousson, Benoita5f43132012-02-28 18:22:12 +01001497 if (up && pdata) {
Felipe Balbie5b57c02012-08-23 13:32:42 +03001498 u32 loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301499
1500 if (up->context_loss_cnt != loss_cnt)
1501 serial_omap_restore_context(up);
Govindraj.R94734742011-11-07 19:00:33 +05301502
Govindraj.R2fd14962011-11-09 17:41:21 +05301503 up->latency = up->calc_latency;
1504 schedule_work(&up->qos_work);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301505 }
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301506
Govindraj.Rfcdca752011-02-28 18:12:23 +05301507 return 0;
1508}
1509#endif
1510
1511static const struct dev_pm_ops serial_omap_dev_pm_ops = {
1512 SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
1513 SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
1514 serial_omap_runtime_resume, NULL)
1515};
1516
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301517#if defined(CONFIG_OF)
1518static const struct of_device_id omap_serial_of_match[] = {
1519 { .compatible = "ti,omap2-uart" },
1520 { .compatible = "ti,omap3-uart" },
1521 { .compatible = "ti,omap4-uart" },
1522 {},
1523};
1524MODULE_DEVICE_TABLE(of, omap_serial_of_match);
1525#endif
1526
Govindraj.Rb6126332010-09-27 20:20:49 +05301527static struct platform_driver serial_omap_driver = {
1528 .probe = serial_omap_probe,
Felipe Balbi6d608ef2012-09-06 15:45:32 +03001529 .remove = __devexit_p(serial_omap_remove),
Govindraj.Rb6126332010-09-27 20:20:49 +05301530 .driver = {
1531 .name = DRIVER_NAME,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301532 .pm = &serial_omap_dev_pm_ops,
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301533 .of_match_table = of_match_ptr(omap_serial_of_match),
Govindraj.Rb6126332010-09-27 20:20:49 +05301534 },
1535};
1536
1537static int __init serial_omap_init(void)
1538{
1539 int ret;
1540
1541 ret = uart_register_driver(&serial_omap_reg);
1542 if (ret != 0)
1543 return ret;
1544 ret = platform_driver_register(&serial_omap_driver);
1545 if (ret != 0)
1546 uart_unregister_driver(&serial_omap_reg);
1547 return ret;
1548}
1549
1550static void __exit serial_omap_exit(void)
1551{
1552 platform_driver_unregister(&serial_omap_driver);
1553 uart_unregister_driver(&serial_omap_reg);
1554}
1555
1556module_init(serial_omap_init);
1557module_exit(serial_omap_exit);
1558
1559MODULE_DESCRIPTION("OMAP High Speed UART driver");
1560MODULE_LICENSE("GPL");
1561MODULE_AUTHOR("Texas Instruments Inc");