blob: 72bb690829afea9c1b2e6f261a8f807aeb4a4c49 [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>
Tony Lindgrend9ba5732012-12-14 09:09:11 -080043#include <linux/platform_data/serial-omap.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053044
Russell Kingf91b55a2012-10-06 10:50:58 +010045#define OMAP_MAX_HSUART_PORTS 6
46
Govindraj.R7c77c8d2012-04-03 19:12:34 +053047#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
48
49#define OMAP_UART_REV_42 0x0402
50#define OMAP_UART_REV_46 0x0406
51#define OMAP_UART_REV_52 0x0502
52#define OMAP_UART_REV_63 0x0603
53
Govindraj.Rf64ffda2013-07-05 18:25:59 +030054#define OMAP_UART_TX_WAKEUP_EN BIT(7)
55
56/* Feature flags */
57#define OMAP_UART_WER_HAS_TX_WAKEUP BIT(0)
58
Russell Kingf91b55a2012-10-06 10:50:58 +010059#define UART_ERRATA_i202_MDR1_ACCESS BIT(0)
60#define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1)
61
Rajendra Nayak8fe789d2011-12-14 17:25:44 +053062#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz*/
63
Paul Walmsley0ba5f662012-01-25 19:50:36 -070064/* SCR register bitmasks */
65#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
Alexey Pelykh1776fd02013-02-04 12:19:46 -050066#define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
Russell Kingf91b55a2012-10-06 10:50:58 +010067#define OMAP_UART_SCR_TX_EMPTY (1 << 3)
Paul Walmsley0ba5f662012-01-25 19:50:36 -070068
69/* FCR register bitmasks */
Paul Walmsley0ba5f662012-01-25 19:50:36 -070070#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
Felipe Balbi6721ab72012-09-06 15:45:40 +030071#define OMAP_UART_FCR_TX_FIFO_TRIG_MASK (0x3 << 4)
Paul Walmsley0ba5f662012-01-25 19:50:36 -070072
Govindraj.R7c77c8d2012-04-03 19:12:34 +053073/* MVR register bitmasks */
74#define OMAP_UART_MVR_SCHEME_SHIFT 30
75
76#define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
77#define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
78#define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
79
80#define OMAP_UART_MVR_MAJ_MASK 0x700
81#define OMAP_UART_MVR_MAJ_SHIFT 8
82#define OMAP_UART_MVR_MIN_MASK 0x3f
83
Russell Kingf91b55a2012-10-06 10:50:58 +010084#define OMAP_UART_DMA_CH_FREE -1
85
86#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
87#define OMAP_MODE13X_SPEED 230400
88
89/* WER = 0x7F
90 * Enable module level wakeup in WER reg
91 */
92#define OMAP_UART_WER_MOD_WKUP 0X7F
93
94/* Enable XON/XOFF flow control on output */
Russell King3af08bd2012-10-05 13:32:08 +010095#define OMAP_UART_SW_TX 0x08
Russell Kingf91b55a2012-10-06 10:50:58 +010096
97/* Enable XON/XOFF flow control on input */
Russell King3af08bd2012-10-05 13:32:08 +010098#define OMAP_UART_SW_RX 0x02
Russell Kingf91b55a2012-10-06 10:50:58 +010099
100#define OMAP_UART_SW_CLR 0xF0
101
102#define OMAP_UART_TCR_TRIG 0x0F
103
104struct uart_omap_dma {
105 u8 uart_dma_tx;
106 u8 uart_dma_rx;
107 int rx_dma_channel;
108 int tx_dma_channel;
109 dma_addr_t rx_buf_dma_phys;
110 dma_addr_t tx_buf_dma_phys;
111 unsigned int uart_base;
112 /*
113 * Buffer for rx dma.It is not required for tx because the buffer
114 * comes from port structure.
115 */
116 unsigned char *rx_buf;
117 unsigned int prev_rx_dma_pos;
118 int tx_buf_size;
119 int tx_dma_used;
120 int rx_dma_used;
121 spinlock_t tx_lock;
122 spinlock_t rx_lock;
123 /* timer to poll activity on rx dma */
124 struct timer_list rx_timer;
125 unsigned int rx_buf_size;
126 unsigned int rx_poll_rate;
127 unsigned int rx_timeout;
128};
129
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300130struct uart_omap_port {
131 struct uart_port port;
132 struct uart_omap_dma uart_dma;
133 struct device *dev;
134
135 unsigned char ier;
136 unsigned char lcr;
137 unsigned char mcr;
138 unsigned char fcr;
139 unsigned char efr;
140 unsigned char dll;
141 unsigned char dlh;
142 unsigned char mdr1;
143 unsigned char scr;
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300144 unsigned char wer;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300145
146 int use_dma;
147 /*
148 * Some bits in registers are cleared on a read, so they must
149 * be saved whenever the register is read but the bits will not
150 * be immediately processed.
151 */
152 unsigned int lsr_break_flag;
153 unsigned char msr_saved_flags;
154 char name[20];
155 unsigned long port_activity;
Shubhrajyoti D39aee512012-10-03 17:24:36 +0530156 int context_loss_cnt;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300157 u32 errata;
158 u8 wakeups_enabled;
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300159 u32 features;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300160
Felipe Balbie36851d2012-09-07 18:34:19 +0300161 int DTR_gpio;
162 int DTR_inverted;
163 int DTR_active;
164
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300165 struct pm_qos_request pm_qos_request;
166 u32 latency;
167 u32 calc_latency;
168 struct work_struct qos_work;
Sourav Poddarddd85e22013-05-15 21:05:38 +0530169 bool is_suspending;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300170};
171
172#define to_uart_omap_port(p) ((container_of((p), struct uart_omap_port, port)))
173
Govindraj.Rb6126332010-09-27 20:20:49 +0530174static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
175
176/* Forward declaration of functions */
Govindraj.R94734742011-11-07 19:00:33 +0530177static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +0530178
Govindraj.R2fd14962011-11-09 17:41:21 +0530179static struct workqueue_struct *serial_omap_uart_wq;
Govindraj.Rb6126332010-09-27 20:20:49 +0530180
181static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
182{
183 offset <<= up->port.regshift;
184 return readw(up->port.membase + offset);
185}
186
187static inline void serial_out(struct uart_omap_port *up, int offset, int value)
188{
189 offset <<= up->port.regshift;
190 writew(value, up->port.membase + offset);
191}
192
193static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
194{
195 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
196 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
197 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
198 serial_out(up, UART_FCR, 0);
199}
200
Felipe Balbie5b57c02012-08-23 13:32:42 +0300201static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
202{
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300203 struct omap_uart_port_info *pdata = up->dev->platform_data;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300204
Felipe Balbice2f08d2012-09-07 21:10:33 +0300205 if (!pdata || !pdata->get_context_loss_count)
Tony Lindgrena630fbf2013-06-10 07:39:09 -0700206 return -EINVAL;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300207
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300208 return pdata->get_context_loss_count(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300209}
210
Felipe Balbie5b57c02012-08-23 13:32:42 +0300211static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
212{
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300213 struct omap_uart_port_info *pdata = up->dev->platform_data;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300214
Felipe Balbice2f08d2012-09-07 21:10:33 +0300215 if (!pdata || !pdata->enable_wakeup)
216 return;
217
218 pdata->enable_wakeup(up->dev, enable);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300219}
220
Govindraj.Rb6126332010-09-27 20:20:49 +0530221/*
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500222 * serial_omap_baud_is_mode16 - check if baud rate is MODE16X
223 * @port: uart port info
224 * @baud: baudrate for which mode needs to be determined
225 *
226 * Returns true if baud rate is MODE16X and false if MODE13X
227 * Original table in OMAP TRM named "UART Mode Baud Rates, Divisor Values,
228 * and Error Rates" determines modes not for all common baud rates.
229 * E.g. for 1000000 baud rate mode must be 16x, but according to that
230 * table it's determined as 13x.
231 */
232static bool
233serial_omap_baud_is_mode16(struct uart_port *port, unsigned int baud)
234{
235 unsigned int n13 = port->uartclk / (13 * baud);
236 unsigned int n16 = port->uartclk / (16 * baud);
237 int baudAbsDiff13 = baud - (port->uartclk / (13 * n13));
238 int baudAbsDiff16 = baud - (port->uartclk / (16 * n16));
239 if(baudAbsDiff13 < 0)
240 baudAbsDiff13 = -baudAbsDiff13;
241 if(baudAbsDiff16 < 0)
242 baudAbsDiff16 = -baudAbsDiff16;
243
244 return (baudAbsDiff13 > baudAbsDiff16);
245}
246
247/*
Govindraj.Rb6126332010-09-27 20:20:49 +0530248 * serial_omap_get_divisor - calculate divisor value
249 * @port: uart port info
250 * @baud: baudrate for which divisor needs to be calculated.
Govindraj.Rb6126332010-09-27 20:20:49 +0530251 */
252static unsigned int
253serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
254{
255 unsigned int divisor;
256
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500257 if (!serial_omap_baud_is_mode16(port, baud))
Govindraj.Rb6126332010-09-27 20:20:49 +0530258 divisor = 13;
259 else
260 divisor = 16;
261 return port->uartclk/(baud * divisor);
262}
263
Govindraj.Rb6126332010-09-27 20:20:49 +0530264static void serial_omap_enable_ms(struct uart_port *port)
265{
Felipe Balbic990f352012-08-23 13:32:41 +0300266 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530267
Rajendra Nayakba774332011-12-14 17:25:43 +0530268 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530269
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300270 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530271 up->ier |= UART_IER_MSI;
272 serial_out(up, UART_IER, up->ier);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300273 pm_runtime_mark_last_busy(up->dev);
274 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530275}
276
277static void serial_omap_stop_tx(struct uart_port *port)
278{
Felipe Balbic990f352012-08-23 13:32:41 +0300279 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530280
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300281 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530282 if (up->ier & UART_IER_THRI) {
283 up->ier &= ~UART_IER_THRI;
284 serial_out(up, UART_IER, up->ier);
285 }
Govindraj.Rfcdca752011-02-28 18:12:23 +0530286
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300287 pm_runtime_mark_last_busy(up->dev);
288 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530289}
290
291static void serial_omap_stop_rx(struct uart_port *port)
292{
Felipe Balbic990f352012-08-23 13:32:41 +0300293 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530294
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300295 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530296 up->ier &= ~UART_IER_RLSI;
297 up->port.read_status_mask &= ~UART_LSR_DR;
298 serial_out(up, UART_IER, up->ier);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300299 pm_runtime_mark_last_busy(up->dev);
300 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530301}
302
Felipe Balbibf63a082012-09-06 15:45:25 +0300303static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
Govindraj.Rb6126332010-09-27 20:20:49 +0530304{
305 struct circ_buf *xmit = &up->port.state->xmit;
306 int count;
307
308 if (up->port.x_char) {
309 serial_out(up, UART_TX, up->port.x_char);
310 up->port.icount.tx++;
311 up->port.x_char = 0;
312 return;
313 }
314 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
315 serial_omap_stop_tx(&up->port);
316 return;
317 }
Dmitry Finkc4415082013-07-08 13:04:44 +0300318 count = up->port.fifosize -
319 (serial_in(up, UART_OMAP_TXFIFO_LVL) & 0xFF);
Govindraj.Rb6126332010-09-27 20:20:49 +0530320 do {
321 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
322 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
323 up->port.icount.tx++;
324 if (uart_circ_empty(xmit))
325 break;
326 } while (--count > 0);
327
Ruchika Kharwar0324a822012-09-06 15:45:34 +0300328 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
329 spin_unlock(&up->port.lock);
Govindraj.Rb6126332010-09-27 20:20:49 +0530330 uart_write_wakeup(&up->port);
Ruchika Kharwar0324a822012-09-06 15:45:34 +0300331 spin_lock(&up->port.lock);
332 }
Govindraj.Rb6126332010-09-27 20:20:49 +0530333
334 if (uart_circ_empty(xmit))
335 serial_omap_stop_tx(&up->port);
336}
337
338static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
339{
340 if (!(up->ier & UART_IER_THRI)) {
341 up->ier |= UART_IER_THRI;
342 serial_out(up, UART_IER, up->ier);
343 }
344}
345
346static void serial_omap_start_tx(struct uart_port *port)
347{
Felipe Balbic990f352012-08-23 13:32:41 +0300348 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530349
Felipe Balbi49457432012-09-06 15:45:21 +0300350 pm_runtime_get_sync(up->dev);
351 serial_omap_enable_ier_thri(up);
Felipe Balbi49457432012-09-06 15:45:21 +0300352 pm_runtime_mark_last_busy(up->dev);
353 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530354}
355
Russell King3af08bd2012-10-05 13:32:08 +0100356static void serial_omap_throttle(struct uart_port *port)
357{
358 struct uart_omap_port *up = to_uart_omap_port(port);
359 unsigned long flags;
360
361 pm_runtime_get_sync(up->dev);
362 spin_lock_irqsave(&up->port.lock, flags);
363 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
364 serial_out(up, UART_IER, up->ier);
365 spin_unlock_irqrestore(&up->port.lock, flags);
366 pm_runtime_mark_last_busy(up->dev);
367 pm_runtime_put_autosuspend(up->dev);
368}
369
370static void serial_omap_unthrottle(struct uart_port *port)
371{
372 struct uart_omap_port *up = to_uart_omap_port(port);
373 unsigned long flags;
374
375 pm_runtime_get_sync(up->dev);
376 spin_lock_irqsave(&up->port.lock, flags);
377 up->ier |= UART_IER_RLSI | UART_IER_RDI;
378 serial_out(up, UART_IER, up->ier);
379 spin_unlock_irqrestore(&up->port.lock, flags);
380 pm_runtime_mark_last_busy(up->dev);
381 pm_runtime_put_autosuspend(up->dev);
382}
383
Govindraj.Rb6126332010-09-27 20:20:49 +0530384static unsigned int check_modem_status(struct uart_omap_port *up)
385{
386 unsigned int status;
387
388 status = serial_in(up, UART_MSR);
389 status |= up->msr_saved_flags;
390 up->msr_saved_flags = 0;
391 if ((status & UART_MSR_ANY_DELTA) == 0)
392 return status;
393
394 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
395 up->port.state != NULL) {
396 if (status & UART_MSR_TERI)
397 up->port.icount.rng++;
398 if (status & UART_MSR_DDSR)
399 up->port.icount.dsr++;
400 if (status & UART_MSR_DDCD)
401 uart_handle_dcd_change
402 (&up->port, status & UART_MSR_DCD);
403 if (status & UART_MSR_DCTS)
404 uart_handle_cts_change
405 (&up->port, status & UART_MSR_CTS);
406 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
407 }
408
409 return status;
410}
411
Felipe Balbi72256cb2012-09-06 15:45:24 +0300412static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr)
413{
414 unsigned int flag;
Shubhrajyoti D9a12fcf2012-09-21 20:07:19 +0530415 unsigned char ch = 0;
416
417 if (likely(lsr & UART_LSR_DR))
418 ch = serial_in(up, UART_RX);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300419
420 up->port.icount.rx++;
421 flag = TTY_NORMAL;
422
423 if (lsr & UART_LSR_BI) {
424 flag = TTY_BREAK;
425 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
426 up->port.icount.brk++;
427 /*
428 * We do the SysRQ and SAK checking
429 * here because otherwise the break
430 * may get masked by ignore_status_mask
431 * or read_status_mask.
432 */
433 if (uart_handle_break(&up->port))
434 return;
435
436 }
437
438 if (lsr & UART_LSR_PE) {
439 flag = TTY_PARITY;
440 up->port.icount.parity++;
441 }
442
443 if (lsr & UART_LSR_FE) {
444 flag = TTY_FRAME;
445 up->port.icount.frame++;
446 }
447
448 if (lsr & UART_LSR_OE)
449 up->port.icount.overrun++;
450
451#ifdef CONFIG_SERIAL_OMAP_CONSOLE
452 if (up->port.line == up->port.cons->index) {
453 /* Recover the break flag from console xmit */
454 lsr |= up->lsr_break_flag;
455 }
456#endif
457 uart_insert_char(&up->port, lsr, UART_LSR_OE, 0, flag);
458}
459
460static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr)
461{
462 unsigned char ch = 0;
463 unsigned int flag;
464
465 if (!(lsr & UART_LSR_DR))
466 return;
467
468 ch = serial_in(up, UART_RX);
469 flag = TTY_NORMAL;
470 up->port.icount.rx++;
471
472 if (uart_handle_sysrq_char(&up->port, ch))
473 return;
474
475 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
476}
477
Govindraj.Rb6126332010-09-27 20:20:49 +0530478/**
479 * serial_omap_irq() - This handles the interrupt from one port
480 * @irq: uart port irq number
481 * @dev_id: uart port info
482 */
Felipe Balbi52c55132012-09-06 15:45:33 +0300483static irqreturn_t serial_omap_irq(int irq, void *dev_id)
Govindraj.Rb6126332010-09-27 20:20:49 +0530484{
485 struct uart_omap_port *up = dev_id;
486 unsigned int iir, lsr;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300487 unsigned int type;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300488 irqreturn_t ret = IRQ_NONE;
Felipe Balbi72256cb2012-09-06 15:45:24 +0300489 int max_count = 256;
Govindraj.Rb6126332010-09-27 20:20:49 +0530490
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300491 spin_lock(&up->port.lock);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300492 pm_runtime_get_sync(up->dev);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300493
Felipe Balbi72256cb2012-09-06 15:45:24 +0300494 do {
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300495 iir = serial_in(up, UART_IIR);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300496 if (iir & UART_IIR_NO_INT)
497 break;
Govindraj.Rb6126332010-09-27 20:20:49 +0530498
Felipe Balbi72256cb2012-09-06 15:45:24 +0300499 ret = IRQ_HANDLED;
500 lsr = serial_in(up, UART_LSR);
501
502 /* extract IRQ type from IIR register */
503 type = iir & 0x3e;
504
505 switch (type) {
506 case UART_IIR_MSI:
507 check_modem_status(up);
508 break;
509 case UART_IIR_THRI:
Felipe Balbibf63a082012-09-06 15:45:25 +0300510 transmit_chars(up, lsr);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300511 break;
512 case UART_IIR_RX_TIMEOUT:
513 /* FALLTHROUGH */
514 case UART_IIR_RDI:
515 serial_omap_rdi(up, lsr);
516 break;
517 case UART_IIR_RLSI:
518 serial_omap_rlsi(up, lsr);
519 break;
520 case UART_IIR_CTS_RTS_DSR:
521 /* simply try again */
522 break;
523 case UART_IIR_XOFF:
524 /* FALLTHROUGH */
525 default:
526 break;
527 }
528 } while (!(iir & UART_IIR_NO_INT) && max_count--);
529
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300530 spin_unlock(&up->port.lock);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300531
Jiri Slaby2e124b42013-01-03 15:53:06 +0100532 tty_flip_buffer_push(&up->port.state->port);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300533
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300534 pm_runtime_mark_last_busy(up->dev);
535 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530536 up->port_activity = jiffies;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300537
538 return ret;
Govindraj.Rb6126332010-09-27 20:20:49 +0530539}
540
541static unsigned int serial_omap_tx_empty(struct uart_port *port)
542{
Felipe Balbic990f352012-08-23 13:32:41 +0300543 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530544 unsigned long flags = 0;
545 unsigned int ret = 0;
546
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300547 pm_runtime_get_sync(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +0530548 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530549 spin_lock_irqsave(&up->port.lock, flags);
550 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
551 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300552 pm_runtime_mark_last_busy(up->dev);
553 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530554 return ret;
555}
556
557static unsigned int serial_omap_get_mctrl(struct uart_port *port)
558{
Felipe Balbic990f352012-08-23 13:32:41 +0300559 struct uart_omap_port *up = to_uart_omap_port(port);
Shubhrajyoti D514f31d2011-11-21 15:43:28 +0530560 unsigned int status;
Govindraj.Rb6126332010-09-27 20:20:49 +0530561 unsigned int ret = 0;
562
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300563 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530564 status = check_modem_status(up);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300565 pm_runtime_mark_last_busy(up->dev);
566 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530567
Rajendra Nayakba774332011-12-14 17:25:43 +0530568 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530569
570 if (status & UART_MSR_DCD)
571 ret |= TIOCM_CAR;
572 if (status & UART_MSR_RI)
573 ret |= TIOCM_RNG;
574 if (status & UART_MSR_DSR)
575 ret |= TIOCM_DSR;
576 if (status & UART_MSR_CTS)
577 ret |= TIOCM_CTS;
578 return ret;
579}
580
581static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
582{
Felipe Balbic990f352012-08-23 13:32:41 +0300583 struct uart_omap_port *up = to_uart_omap_port(port);
Russell King9363f8f2012-10-05 12:23:28 +0100584 unsigned char mcr = 0, old_mcr;
Govindraj.Rb6126332010-09-27 20:20:49 +0530585
Rajendra Nayakba774332011-12-14 17:25:43 +0530586 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530587 if (mctrl & TIOCM_RTS)
588 mcr |= UART_MCR_RTS;
589 if (mctrl & TIOCM_DTR)
590 mcr |= UART_MCR_DTR;
591 if (mctrl & TIOCM_OUT1)
592 mcr |= UART_MCR_OUT1;
593 if (mctrl & TIOCM_OUT2)
594 mcr |= UART_MCR_OUT2;
595 if (mctrl & TIOCM_LOOP)
596 mcr |= UART_MCR_LOOP;
597
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300598 pm_runtime_get_sync(up->dev);
Russell King9363f8f2012-10-05 12:23:28 +0100599 old_mcr = serial_in(up, UART_MCR);
600 old_mcr &= ~(UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_OUT1 |
601 UART_MCR_DTR | UART_MCR_RTS);
602 up->mcr = old_mcr | mcr;
Govindraj.Rc538d202011-11-07 18:57:03 +0530603 serial_out(up, UART_MCR, up->mcr);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300604 pm_runtime_mark_last_busy(up->dev);
605 pm_runtime_put_autosuspend(up->dev);
NeilBrown9574f362012-07-30 10:30:26 +1000606
607 if (gpio_is_valid(up->DTR_gpio) &&
608 !!(mctrl & TIOCM_DTR) != up->DTR_active) {
609 up->DTR_active = !up->DTR_active;
610 if (gpio_cansleep(up->DTR_gpio))
611 schedule_work(&up->qos_work);
612 else
613 gpio_set_value(up->DTR_gpio,
614 up->DTR_active != up->DTR_inverted);
615 }
Govindraj.Rb6126332010-09-27 20:20:49 +0530616}
617
618static void serial_omap_break_ctl(struct uart_port *port, int break_state)
619{
Felipe Balbic990f352012-08-23 13:32:41 +0300620 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530621 unsigned long flags = 0;
622
Rajendra Nayakba774332011-12-14 17:25:43 +0530623 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300624 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530625 spin_lock_irqsave(&up->port.lock, flags);
626 if (break_state == -1)
627 up->lcr |= UART_LCR_SBC;
628 else
629 up->lcr &= ~UART_LCR_SBC;
630 serial_out(up, UART_LCR, up->lcr);
631 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300632 pm_runtime_mark_last_busy(up->dev);
633 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530634}
635
636static int serial_omap_startup(struct uart_port *port)
637{
Felipe Balbic990f352012-08-23 13:32:41 +0300638 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530639 unsigned long flags = 0;
640 int retval;
641
642 /*
643 * Allocate the IRQ
644 */
645 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
646 up->name, up);
647 if (retval)
648 return retval;
649
Rajendra Nayakba774332011-12-14 17:25:43 +0530650 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530651
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300652 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530653 /*
654 * Clear the FIFO buffers and disable them.
655 * (they will be reenabled in set_termios())
656 */
657 serial_omap_clear_fifos(up);
658 /* For Hardware flow control */
659 serial_out(up, UART_MCR, UART_MCR_RTS);
660
661 /*
662 * Clear the interrupt registers.
663 */
664 (void) serial_in(up, UART_LSR);
665 if (serial_in(up, UART_LSR) & UART_LSR_DR)
666 (void) serial_in(up, UART_RX);
667 (void) serial_in(up, UART_IIR);
668 (void) serial_in(up, UART_MSR);
669
670 /*
671 * Now, initialize the UART
672 */
673 serial_out(up, UART_LCR, UART_LCR_WLEN8);
674 spin_lock_irqsave(&up->port.lock, flags);
675 /*
676 * Most PC uarts need OUT2 raised to enable interrupts.
677 */
678 up->port.mctrl |= TIOCM_OUT2;
679 serial_omap_set_mctrl(&up->port, up->port.mctrl);
680 spin_unlock_irqrestore(&up->port.lock, flags);
681
682 up->msr_saved_flags = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530683 /*
684 * Finally, enable interrupts. Note: Modem status interrupts
685 * are set via set_termios(), which will be occurring imminently
686 * anyway, so we don't enable them here.
687 */
688 up->ier = UART_IER_RLSI | UART_IER_RDI;
689 serial_out(up, UART_IER, up->ier);
690
Jarkko Nikula78841462011-01-24 17:51:22 +0200691 /* Enable module level wake up */
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300692 up->wer = OMAP_UART_WER_MOD_WKUP;
693 if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
694 up->wer |= OMAP_UART_TX_WAKEUP_EN;
695
696 serial_out(up, UART_OMAP_WER, up->wer);
Jarkko Nikula78841462011-01-24 17:51:22 +0200697
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300698 pm_runtime_mark_last_busy(up->dev);
699 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530700 up->port_activity = jiffies;
701 return 0;
702}
703
704static void serial_omap_shutdown(struct uart_port *port)
705{
Felipe Balbic990f352012-08-23 13:32:41 +0300706 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530707 unsigned long flags = 0;
708
Rajendra Nayakba774332011-12-14 17:25:43 +0530709 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530710
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300711 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530712 /*
713 * Disable interrupts from this port
714 */
715 up->ier = 0;
716 serial_out(up, UART_IER, 0);
717
718 spin_lock_irqsave(&up->port.lock, flags);
719 up->port.mctrl &= ~TIOCM_OUT2;
720 serial_omap_set_mctrl(&up->port, up->port.mctrl);
721 spin_unlock_irqrestore(&up->port.lock, flags);
722
723 /*
724 * Disable break condition and FIFOs
725 */
726 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
727 serial_omap_clear_fifos(up);
728
729 /*
730 * Read data port to reset things, and then free the irq
731 */
732 if (serial_in(up, UART_LSR) & UART_LSR_DR)
733 (void) serial_in(up, UART_RX);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530734
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300735 pm_runtime_mark_last_busy(up->dev);
736 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530737 free_irq(up->port.irq, up);
738}
739
Govindraj.R2fd14962011-11-09 17:41:21 +0530740static void serial_omap_uart_qos_work(struct work_struct *work)
741{
742 struct uart_omap_port *up = container_of(work, struct uart_omap_port,
743 qos_work);
744
745 pm_qos_update_request(&up->pm_qos_request, up->latency);
NeilBrown9574f362012-07-30 10:30:26 +1000746 if (gpio_is_valid(up->DTR_gpio))
747 gpio_set_value_cansleep(up->DTR_gpio,
748 up->DTR_active != up->DTR_inverted);
Govindraj.R2fd14962011-11-09 17:41:21 +0530749}
750
Govindraj.Rb6126332010-09-27 20:20:49 +0530751static void
752serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
753 struct ktermios *old)
754{
Felipe Balbic990f352012-08-23 13:32:41 +0300755 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530756 unsigned char cval = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530757 unsigned long flags = 0;
758 unsigned int baud, quot;
759
760 switch (termios->c_cflag & CSIZE) {
761 case CS5:
762 cval = UART_LCR_WLEN5;
763 break;
764 case CS6:
765 cval = UART_LCR_WLEN6;
766 break;
767 case CS7:
768 cval = UART_LCR_WLEN7;
769 break;
770 default:
771 case CS8:
772 cval = UART_LCR_WLEN8;
773 break;
774 }
775
776 if (termios->c_cflag & CSTOPB)
777 cval |= UART_LCR_STOP;
778 if (termios->c_cflag & PARENB)
779 cval |= UART_LCR_PARITY;
780 if (!(termios->c_cflag & PARODD))
781 cval |= UART_LCR_EPAR;
Enric Balletbo i Serrafdbc7352012-12-06 09:45:04 +0100782 if (termios->c_cflag & CMSPAR)
783 cval |= UART_LCR_SPAR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530784
785 /*
786 * Ask the core to calculate the divisor for us.
787 */
788
789 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
790 quot = serial_omap_get_divisor(port, baud);
791
Govindraj.R2fd14962011-11-09 17:41:21 +0530792 /* calculate wakeup latency constraint */
Paul Walmsley19723452012-01-25 19:50:56 -0700793 up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
Govindraj.R2fd14962011-11-09 17:41:21 +0530794 up->latency = up->calc_latency;
795 schedule_work(&up->qos_work);
796
Govindraj.Rc538d202011-11-07 18:57:03 +0530797 up->dll = quot & 0xff;
798 up->dlh = quot >> 8;
799 up->mdr1 = UART_OMAP_MDR1_DISABLE;
800
Govindraj.Rb6126332010-09-27 20:20:49 +0530801 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
802 UART_FCR_ENABLE_FIFO;
Govindraj.Rb6126332010-09-27 20:20:49 +0530803
804 /*
805 * Ok, we're now changing the port state. Do it with
806 * interrupts disabled.
807 */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300808 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530809 spin_lock_irqsave(&up->port.lock, flags);
810
811 /*
812 * Update the per-port timeout.
813 */
814 uart_update_timeout(port, termios->c_cflag, baud);
815
816 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
817 if (termios->c_iflag & INPCK)
818 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
819 if (termios->c_iflag & (BRKINT | PARMRK))
820 up->port.read_status_mask |= UART_LSR_BI;
821
822 /*
823 * Characters to ignore
824 */
825 up->port.ignore_status_mask = 0;
826 if (termios->c_iflag & IGNPAR)
827 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
828 if (termios->c_iflag & IGNBRK) {
829 up->port.ignore_status_mask |= UART_LSR_BI;
830 /*
831 * If we're ignoring parity and break indicators,
832 * ignore overruns too (for real raw support).
833 */
834 if (termios->c_iflag & IGNPAR)
835 up->port.ignore_status_mask |= UART_LSR_OE;
836 }
837
838 /*
839 * ignore all characters if CREAD is not set
840 */
841 if ((termios->c_cflag & CREAD) == 0)
842 up->port.ignore_status_mask |= UART_LSR_DR;
843
844 /*
845 * Modem status interrupts
846 */
847 up->ier &= ~UART_IER_MSI;
848 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
849 up->ier |= UART_IER_MSI;
850 serial_out(up, UART_IER, up->ier);
851 serial_out(up, UART_LCR, cval); /* reset DLAB */
Govindraj.Rc538d202011-11-07 18:57:03 +0530852 up->lcr = cval;
Alexey Pelykh1776fd02013-02-04 12:19:46 -0500853 up->scr = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530854
855 /* FIFOs and DMA Settings */
856
857 /* FCR can be changed only when the
858 * baud clock is not running
859 * DLL_REG and DLH_REG set to 0.
860 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800861 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530862 serial_out(up, UART_DLL, 0);
863 serial_out(up, UART_DLM, 0);
864 serial_out(up, UART_LCR, 0);
865
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800866 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530867
Russell King08bd4902012-10-05 13:54:53 +0100868 up->efr = serial_in(up, UART_EFR) & ~UART_EFR_ECB;
Russell Kingd864c032012-10-06 00:51:17 +0100869 up->efr &= ~UART_EFR_SCD;
Govindraj.Rb6126332010-09-27 20:20:49 +0530870 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
871
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800872 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Russell King08bd4902012-10-05 13:54:53 +0100873 up->mcr = serial_in(up, UART_MCR) & ~UART_MCR_TCRTLR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530874 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
875 /* FIFO ENABLE, DMA MODE */
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700876
Alexey Pelykh1f663962013-04-03 14:31:46 -0400877 up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
878 /*
879 * NOTE: Setting OMAP_UART_SCR_RX_TRIG_GRANU1_MASK
880 * sets Enables the granularity of 1 for TRIGGER RX
881 * level. Along with setting RX FIFO trigger level
882 * to 1 (as noted below, 16 characters) and TLR[3:0]
883 * to zero this will result RX FIFO threshold level
884 * to 1 character, instead of 16 as noted in comment
885 * below.
886 */
887
Felipe Balbi6721ab72012-09-06 15:45:40 +0300888 /* Set receive FIFO threshold to 16 characters and
889 * transmit FIFO threshold to 16 spaces
890 */
Felipe Balbi49457432012-09-06 15:45:21 +0300891 up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
Felipe Balbi6721ab72012-09-06 15:45:40 +0300892 up->fcr &= ~OMAP_UART_FCR_TX_FIFO_TRIG_MASK;
893 up->fcr |= UART_FCR6_R_TRIGGER_16 | UART_FCR6_T_TRIGGER_24 |
894 UART_FCR_ENABLE_FIFO;
Greg Kroah-Hartman8a74e9f2012-01-26 11:15:18 -0800895
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700896 serial_out(up, UART_FCR, up->fcr);
897 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
898
Govindraj.Rc538d202011-11-07 18:57:03 +0530899 serial_out(up, UART_OMAP_SCR, up->scr);
900
Russell King08bd4902012-10-05 13:54:53 +0100901 /* Reset UART_MCR_TCRTLR: this must be done with the EFR_ECB bit set */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800902 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530903 serial_out(up, UART_MCR, up->mcr);
Russell King08bd4902012-10-05 13:54:53 +0100904 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
905 serial_out(up, UART_EFR, up->efr);
906 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530907
908 /* Protocol, Baud Rate, and Interrupt Settings */
909
Govindraj.R94734742011-11-07 19:00:33 +0530910 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
911 serial_omap_mdr1_errataset(up, up->mdr1);
912 else
913 serial_out(up, UART_OMAP_MDR1, up->mdr1);
914
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800915 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530916 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
917
918 serial_out(up, UART_LCR, 0);
919 serial_out(up, UART_IER, 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800920 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530921
Govindraj.Rc538d202011-11-07 18:57:03 +0530922 serial_out(up, UART_DLL, up->dll); /* LS of divisor */
923 serial_out(up, UART_DLM, up->dlh); /* MS of divisor */
Govindraj.Rb6126332010-09-27 20:20:49 +0530924
925 serial_out(up, UART_LCR, 0);
926 serial_out(up, UART_IER, up->ier);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800927 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530928
929 serial_out(up, UART_EFR, up->efr);
930 serial_out(up, UART_LCR, cval);
931
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500932 if (!serial_omap_baud_is_mode16(port, baud))
Govindraj.Rc538d202011-11-07 18:57:03 +0530933 up->mdr1 = UART_OMAP_MDR1_13X_MODE;
Govindraj.Rb6126332010-09-27 20:20:49 +0530934 else
Govindraj.Rc538d202011-11-07 18:57:03 +0530935 up->mdr1 = UART_OMAP_MDR1_16X_MODE;
936
Govindraj.R94734742011-11-07 19:00:33 +0530937 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
938 serial_omap_mdr1_errataset(up, up->mdr1);
939 else
940 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +0530941
Russell Kingc533e512012-10-06 09:34:36 +0100942 /* Configure flow control */
Russell Kingc7d059c2012-10-06 09:12:44 +0100943 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530944
Russell Kingc533e512012-10-06 09:34:36 +0100945 /* XON1/XOFF1 accessible mode B, TCRTLR=0, ECB=0 */
946 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
947 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
Govindraj.Rb6126332010-09-27 20:20:49 +0530948
Russell Kingc533e512012-10-06 09:34:36 +0100949 /* Enable access to TCR/TLR */
Russell Kingc7d059c2012-10-06 09:12:44 +0100950 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
951 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
952 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
Govindraj.Rb6126332010-09-27 20:20:49 +0530953
Russell Kingc7d059c2012-10-06 09:12:44 +0100954 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
Govindraj.Rb6126332010-09-27 20:20:49 +0530955
Russell King08bd4902012-10-05 13:54:53 +0100956 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
Russell King08bd4902012-10-05 13:54:53 +0100957 /* Enable AUTORTS and AUTOCTS */
958 up->efr |= UART_EFR_CTS | UART_EFR_RTS;
959
Russell King1fe8aa82012-10-06 09:04:03 +0100960 /* Ensure MCR RTS is asserted */
961 up->mcr |= UART_MCR_RTS;
Russell King0d5b1662012-10-05 23:48:28 +0100962 } else {
963 /* Disable AUTORTS and AUTOCTS */
964 up->efr &= ~(UART_EFR_CTS | UART_EFR_RTS);
Govindraj.Rb6126332010-09-27 20:20:49 +0530965 }
966
Russell King01d70bb2012-10-15 16:50:59 +0100967 if (up->port.flags & UPF_SOFT_FLOW) {
Russell King01d70bb2012-10-15 16:50:59 +0100968 /* clear SW control mode bits */
969 up->efr &= OMAP_UART_SW_CLR;
970
971 /*
972 * IXON Flag:
Russell King01d70bb2012-10-15 16:50:59 +0100973 * Enable XON/XOFF flow control on input.
974 * Receiver compares XON1, XOFF1.
975 */
Russell King3af08bd2012-10-05 13:32:08 +0100976 if (termios->c_iflag & IXON)
Russell King01d70bb2012-10-15 16:50:59 +0100977 up->efr |= OMAP_UART_SW_RX;
978
Russell King01d70bb2012-10-15 16:50:59 +0100979 /*
Russell King3af08bd2012-10-05 13:32:08 +0100980 * IXOFF Flag:
981 * Enable XON/XOFF flow control on output.
982 * Transmit XON1, XOFF1
983 */
984 if (termios->c_iflag & IXOFF)
985 up->efr |= OMAP_UART_SW_TX;
986
987 /*
Russell King01d70bb2012-10-15 16:50:59 +0100988 * IXANY Flag:
989 * Enable any character to restart output.
990 * Operation resumes after receiving any
991 * character after recognition of the XOFF character
992 */
993 if (termios->c_iflag & IXANY)
994 up->mcr |= UART_MCR_XONANY;
995 else
996 up->mcr &= ~UART_MCR_XONANY;
Russell King01d70bb2012-10-15 16:50:59 +0100997 }
Russell Kingc7d059c2012-10-06 09:12:44 +0100998 serial_out(up, UART_MCR, up->mcr);
Russell King18f360f2012-10-06 09:08:20 +0100999 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1000 serial_out(up, UART_EFR, up->efr);
1001 serial_out(up, UART_LCR, up->lcr);
1002
Govindraj.Rb6126332010-09-27 20:20:49 +05301003 serial_omap_set_mctrl(&up->port, up->port.mctrl);
Govindraj.Rb6126332010-09-27 20:20:49 +05301004
1005 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001006 pm_runtime_mark_last_busy(up->dev);
1007 pm_runtime_put_autosuspend(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +05301008 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301009}
1010
Felipe Balbi9727faf2012-09-06 15:45:35 +03001011static int serial_omap_set_wake(struct uart_port *port, unsigned int state)
1012{
1013 struct uart_omap_port *up = to_uart_omap_port(port);
1014
1015 serial_omap_enable_wakeup(up, state);
1016
1017 return 0;
1018}
1019
Govindraj.Rb6126332010-09-27 20:20:49 +05301020static void
1021serial_omap_pm(struct uart_port *port, unsigned int state,
1022 unsigned int oldstate)
1023{
Felipe Balbic990f352012-08-23 13:32:41 +03001024 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301025 unsigned char efr;
1026
Rajendra Nayakba774332011-12-14 17:25:43 +05301027 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301028
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001029 pm_runtime_get_sync(up->dev);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001030 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301031 efr = serial_in(up, UART_EFR);
1032 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
1033 serial_out(up, UART_LCR, 0);
1034
1035 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001036 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301037 serial_out(up, UART_EFR, efr);
1038 serial_out(up, UART_LCR, 0);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301039
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001040 if (!device_may_wakeup(up->dev)) {
Govindraj.Rfcdca752011-02-28 18:12:23 +05301041 if (!state)
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001042 pm_runtime_forbid(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301043 else
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001044 pm_runtime_allow(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301045 }
1046
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001047 pm_runtime_mark_last_busy(up->dev);
1048 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301049}
1050
1051static void serial_omap_release_port(struct uart_port *port)
1052{
1053 dev_dbg(port->dev, "serial_omap_release_port+\n");
1054}
1055
1056static int serial_omap_request_port(struct uart_port *port)
1057{
1058 dev_dbg(port->dev, "serial_omap_request_port+\n");
1059 return 0;
1060}
1061
1062static void serial_omap_config_port(struct uart_port *port, int flags)
1063{
Felipe Balbic990f352012-08-23 13:32:41 +03001064 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301065
1066 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
Rajendra Nayakba774332011-12-14 17:25:43 +05301067 up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301068 up->port.type = PORT_OMAP;
Russell King3af08bd2012-10-05 13:32:08 +01001069 up->port.flags |= UPF_SOFT_FLOW | UPF_HARD_FLOW;
Govindraj.Rb6126332010-09-27 20:20:49 +05301070}
1071
1072static int
1073serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
1074{
1075 /* we don't want the core code to modify any port params */
1076 dev_dbg(port->dev, "serial_omap_verify_port+\n");
1077 return -EINVAL;
1078}
1079
1080static const char *
1081serial_omap_type(struct uart_port *port)
1082{
Felipe Balbic990f352012-08-23 13:32:41 +03001083 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301084
Rajendra Nayakba774332011-12-14 17:25:43 +05301085 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301086 return up->name;
1087}
1088
Govindraj.Rb6126332010-09-27 20:20:49 +05301089#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1090
1091static inline void wait_for_xmitr(struct uart_omap_port *up)
1092{
1093 unsigned int status, tmout = 10000;
1094
1095 /* Wait up to 10ms for the character(s) to be sent. */
1096 do {
1097 status = serial_in(up, UART_LSR);
1098
1099 if (status & UART_LSR_BI)
1100 up->lsr_break_flag = UART_LSR_BI;
1101
1102 if (--tmout == 0)
1103 break;
1104 udelay(1);
1105 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1106
1107 /* Wait up to 1s for flow control if necessary */
1108 if (up->port.flags & UPF_CONS_FLOW) {
1109 tmout = 1000000;
1110 for (tmout = 1000000; tmout; tmout--) {
1111 unsigned int msr = serial_in(up, UART_MSR);
1112
1113 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1114 if (msr & UART_MSR_CTS)
1115 break;
1116
1117 udelay(1);
1118 }
1119 }
1120}
1121
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001122#ifdef CONFIG_CONSOLE_POLL
1123
1124static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
1125{
Felipe Balbic990f352012-08-23 13:32:41 +03001126 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301127
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001128 pm_runtime_get_sync(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001129 wait_for_xmitr(up);
1130 serial_out(up, UART_TX, ch);
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001131 pm_runtime_mark_last_busy(up->dev);
1132 pm_runtime_put_autosuspend(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001133}
1134
1135static int serial_omap_poll_get_char(struct uart_port *port)
1136{
Felipe Balbic990f352012-08-23 13:32:41 +03001137 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301138 unsigned int status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001139
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001140 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301141 status = serial_in(up, UART_LSR);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001142 if (!(status & UART_LSR_DR)) {
1143 status = NO_POLL_CHAR;
1144 goto out;
1145 }
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001146
Govindraj.Rfcdca752011-02-28 18:12:23 +05301147 status = serial_in(up, UART_RX);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001148
1149out:
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001150 pm_runtime_mark_last_busy(up->dev);
1151 pm_runtime_put_autosuspend(up->dev);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001152
Govindraj.Rfcdca752011-02-28 18:12:23 +05301153 return status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001154}
1155
1156#endif /* CONFIG_CONSOLE_POLL */
1157
1158#ifdef CONFIG_SERIAL_OMAP_CONSOLE
1159
Shubhrajyoti D40477d02012-10-03 17:24:38 +05301160static struct uart_omap_port *serial_omap_console_ports[OMAP_MAX_HSUART_PORTS];
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001161
1162static struct uart_driver serial_omap_reg;
1163
Govindraj.Rb6126332010-09-27 20:20:49 +05301164static void serial_omap_console_putchar(struct uart_port *port, int ch)
1165{
Felipe Balbic990f352012-08-23 13:32:41 +03001166 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301167
1168 wait_for_xmitr(up);
1169 serial_out(up, UART_TX, ch);
1170}
1171
1172static void
1173serial_omap_console_write(struct console *co, const char *s,
1174 unsigned int count)
1175{
1176 struct uart_omap_port *up = serial_omap_console_ports[co->index];
1177 unsigned long flags;
1178 unsigned int ier;
1179 int locked = 1;
1180
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001181 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301182
Govindraj.Rb6126332010-09-27 20:20:49 +05301183 local_irq_save(flags);
1184 if (up->port.sysrq)
1185 locked = 0;
1186 else if (oops_in_progress)
1187 locked = spin_trylock(&up->port.lock);
1188 else
1189 spin_lock(&up->port.lock);
1190
1191 /*
1192 * First save the IER then disable the interrupts
1193 */
1194 ier = serial_in(up, UART_IER);
1195 serial_out(up, UART_IER, 0);
1196
1197 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
1198
1199 /*
1200 * Finally, wait for transmitter to become empty
1201 * and restore the IER
1202 */
1203 wait_for_xmitr(up);
1204 serial_out(up, UART_IER, ier);
1205 /*
1206 * The receive handling will happen properly because the
1207 * receive ready bit will still be set; it is not cleared
1208 * on read. However, modem control will not, we must
1209 * call it if we have saved something in the saved flags
1210 * while processing with interrupts off.
1211 */
1212 if (up->msr_saved_flags)
1213 check_modem_status(up);
1214
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001215 pm_runtime_mark_last_busy(up->dev);
1216 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301217 if (locked)
1218 spin_unlock(&up->port.lock);
1219 local_irq_restore(flags);
1220}
1221
1222static int __init
1223serial_omap_console_setup(struct console *co, char *options)
1224{
1225 struct uart_omap_port *up;
1226 int baud = 115200;
1227 int bits = 8;
1228 int parity = 'n';
1229 int flow = 'n';
1230
1231 if (serial_omap_console_ports[co->index] == NULL)
1232 return -ENODEV;
1233 up = serial_omap_console_ports[co->index];
1234
1235 if (options)
1236 uart_parse_options(options, &baud, &parity, &bits, &flow);
1237
1238 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1239}
1240
1241static struct console serial_omap_console = {
1242 .name = OMAP_SERIAL_NAME,
1243 .write = serial_omap_console_write,
1244 .device = uart_console_device,
1245 .setup = serial_omap_console_setup,
1246 .flags = CON_PRINTBUFFER,
1247 .index = -1,
1248 .data = &serial_omap_reg,
1249};
1250
1251static void serial_omap_add_console_port(struct uart_omap_port *up)
1252{
Rajendra Nayakba774332011-12-14 17:25:43 +05301253 serial_omap_console_ports[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301254}
1255
1256#define OMAP_CONSOLE (&serial_omap_console)
1257
1258#else
1259
1260#define OMAP_CONSOLE NULL
1261
1262static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1263{}
1264
1265#endif
1266
1267static struct uart_ops serial_omap_pops = {
1268 .tx_empty = serial_omap_tx_empty,
1269 .set_mctrl = serial_omap_set_mctrl,
1270 .get_mctrl = serial_omap_get_mctrl,
1271 .stop_tx = serial_omap_stop_tx,
1272 .start_tx = serial_omap_start_tx,
Russell King3af08bd2012-10-05 13:32:08 +01001273 .throttle = serial_omap_throttle,
1274 .unthrottle = serial_omap_unthrottle,
Govindraj.Rb6126332010-09-27 20:20:49 +05301275 .stop_rx = serial_omap_stop_rx,
1276 .enable_ms = serial_omap_enable_ms,
1277 .break_ctl = serial_omap_break_ctl,
1278 .startup = serial_omap_startup,
1279 .shutdown = serial_omap_shutdown,
1280 .set_termios = serial_omap_set_termios,
1281 .pm = serial_omap_pm,
Felipe Balbi9727faf2012-09-06 15:45:35 +03001282 .set_wake = serial_omap_set_wake,
Govindraj.Rb6126332010-09-27 20:20:49 +05301283 .type = serial_omap_type,
1284 .release_port = serial_omap_release_port,
1285 .request_port = serial_omap_request_port,
1286 .config_port = serial_omap_config_port,
1287 .verify_port = serial_omap_verify_port,
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001288#ifdef CONFIG_CONSOLE_POLL
1289 .poll_put_char = serial_omap_poll_put_char,
1290 .poll_get_char = serial_omap_poll_get_char,
1291#endif
Govindraj.Rb6126332010-09-27 20:20:49 +05301292};
1293
1294static struct uart_driver serial_omap_reg = {
1295 .owner = THIS_MODULE,
1296 .driver_name = "OMAP-SERIAL",
1297 .dev_name = OMAP_SERIAL_NAME,
1298 .nr = OMAP_MAX_HSUART_PORTS,
1299 .cons = OMAP_CONSOLE,
1300};
1301
Shubhrajyoti D3bc4f0d2012-01-16 15:52:36 +05301302#ifdef CONFIG_PM_SLEEP
Sourav Poddarddd85e22013-05-15 21:05:38 +05301303static int serial_omap_prepare(struct device *dev)
1304{
1305 struct uart_omap_port *up = dev_get_drvdata(dev);
1306
1307 up->is_suspending = true;
1308
1309 return 0;
1310}
1311
1312static void serial_omap_complete(struct device *dev)
1313{
1314 struct uart_omap_port *up = dev_get_drvdata(dev);
1315
1316 up->is_suspending = false;
1317}
1318
Govindraj.Rfcdca752011-02-28 18:12:23 +05301319static int serial_omap_suspend(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301320{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301321 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301322
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301323 uart_suspend_port(&serial_omap_reg, &up->port);
Linus Torvalds033d9952012-10-02 09:54:49 -07001324 flush_work(&up->qos_work);
Govindraj.R2fd14962011-11-09 17:41:21 +05301325
Govindraj.Rb6126332010-09-27 20:20:49 +05301326 return 0;
1327}
1328
Govindraj.Rfcdca752011-02-28 18:12:23 +05301329static int serial_omap_resume(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301330{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301331 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301332
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301333 uart_resume_port(&serial_omap_reg, &up->port);
1334
Govindraj.Rb6126332010-09-27 20:20:49 +05301335 return 0;
1336}
Sourav Poddarddd85e22013-05-15 21:05:38 +05301337#else
1338#define serial_omap_prepare NULL
Arnd Bergmann2cb5a2f2013-06-01 11:18:13 +02001339#define serial_omap_complete NULL
Sourav Poddarddd85e22013-05-15 21:05:38 +05301340#endif /* CONFIG_PM_SLEEP */
Govindraj.Rb6126332010-09-27 20:20:49 +05301341
Bill Pemberton9671f092012-11-19 13:21:50 -05001342static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301343{
1344 u32 mvr, scheme;
1345 u16 revision, major, minor;
1346
Ruchika Kharwar76bac192013-07-08 10:28:57 +03001347 mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301348
1349 /* Check revision register scheme */
1350 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
1351
1352 switch (scheme) {
1353 case 0: /* Legacy Scheme: OMAP2/3 */
1354 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
1355 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
1356 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
1357 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
1358 break;
1359 case 1:
1360 /* New Scheme: OMAP4+ */
1361 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
1362 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
1363 OMAP_UART_MVR_MAJ_SHIFT;
1364 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
1365 break;
1366 default:
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001367 dev_warn(up->dev,
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301368 "Unknown %s revision, defaulting to highest\n",
1369 up->name);
1370 /* highest possible revision */
1371 major = 0xff;
1372 minor = 0xff;
1373 }
1374
1375 /* normalize revision for the driver */
1376 revision = UART_BUILD_REVISION(major, minor);
1377
1378 switch (revision) {
1379 case OMAP_UART_REV_46:
1380 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1381 UART_ERRATA_i291_DMA_FORCEIDLE);
1382 break;
1383 case OMAP_UART_REV_52:
1384 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1385 UART_ERRATA_i291_DMA_FORCEIDLE);
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001386 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301387 break;
1388 case OMAP_UART_REV_63:
1389 up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001390 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301391 break;
1392 default:
1393 break;
1394 }
1395}
1396
Bill Pemberton9671f092012-11-19 13:21:50 -05001397static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301398{
1399 struct omap_uart_port_info *omap_up_info;
1400
1401 omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
1402 if (!omap_up_info)
1403 return NULL; /* out of memory */
1404
1405 of_property_read_u32(dev->of_node, "clock-frequency",
1406 &omap_up_info->uartclk);
1407 return omap_up_info;
1408}
1409
Bill Pemberton9671f092012-11-19 13:21:50 -05001410static int serial_omap_probe(struct platform_device *pdev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301411{
1412 struct uart_omap_port *up;
Felipe Balbi49457432012-09-06 15:45:21 +03001413 struct resource *mem, *irq;
Govindraj.Rb6126332010-09-27 20:20:49 +05301414 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
NeilBrown9574f362012-07-30 10:30:26 +10001415 int ret;
Govindraj.Rb6126332010-09-27 20:20:49 +05301416
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001417 if (pdev->dev.of_node) {
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301418 omap_up_info = of_get_uart_port_info(&pdev->dev);
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001419 pdev->dev.platform_data = omap_up_info;
1420 }
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301421
Govindraj.Rb6126332010-09-27 20:20:49 +05301422 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1423 if (!mem) {
1424 dev_err(&pdev->dev, "no mem resource?\n");
1425 return -ENODEV;
1426 }
1427
1428 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1429 if (!irq) {
1430 dev_err(&pdev->dev, "no irq resource?\n");
1431 return -ENODEV;
1432 }
1433
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301434 if (!devm_request_mem_region(&pdev->dev, mem->start, resource_size(mem),
Joe Perches28f65c112011-06-09 09:13:32 -07001435 pdev->dev.driver->name)) {
Govindraj.Rb6126332010-09-27 20:20:49 +05301436 dev_err(&pdev->dev, "memory region already claimed\n");
1437 return -EBUSY;
1438 }
1439
NeilBrown9574f362012-07-30 10:30:26 +10001440 if (gpio_is_valid(omap_up_info->DTR_gpio) &&
1441 omap_up_info->DTR_present) {
1442 ret = gpio_request(omap_up_info->DTR_gpio, "omap-serial");
1443 if (ret < 0)
1444 return ret;
1445 ret = gpio_direction_output(omap_up_info->DTR_gpio,
1446 omap_up_info->DTR_inverted);
1447 if (ret < 0)
1448 return ret;
1449 }
1450
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301451 up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
1452 if (!up)
1453 return -ENOMEM;
1454
NeilBrown9574f362012-07-30 10:30:26 +10001455 if (gpio_is_valid(omap_up_info->DTR_gpio) &&
1456 omap_up_info->DTR_present) {
1457 up->DTR_gpio = omap_up_info->DTR_gpio;
1458 up->DTR_inverted = omap_up_info->DTR_inverted;
1459 } else
1460 up->DTR_gpio = -EINVAL;
1461 up->DTR_active = 0;
1462
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001463 up->dev = &pdev->dev;
Govindraj.Rb6126332010-09-27 20:20:49 +05301464 up->port.dev = &pdev->dev;
1465 up->port.type = PORT_OMAP;
1466 up->port.iotype = UPIO_MEM;
1467 up->port.irq = irq->start;
1468
1469 up->port.regshift = 2;
1470 up->port.fifosize = 64;
1471 up->port.ops = &serial_omap_pops;
Govindraj.Rb6126332010-09-27 20:20:49 +05301472
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301473 if (pdev->dev.of_node)
1474 up->port.line = of_alias_get_id(pdev->dev.of_node, "serial");
1475 else
1476 up->port.line = pdev->id;
1477
1478 if (up->port.line < 0) {
1479 dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
1480 up->port.line);
1481 ret = -ENODEV;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301482 goto err_port_line;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301483 }
1484
1485 sprintf(up->name, "OMAP UART%d", up->port.line);
Govindraj.Redd70ad2011-10-11 14:55:41 +05301486 up->port.mapbase = mem->start;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301487 up->port.membase = devm_ioremap(&pdev->dev, mem->start,
1488 resource_size(mem));
Govindraj.Redd70ad2011-10-11 14:55:41 +05301489 if (!up->port.membase) {
1490 dev_err(&pdev->dev, "can't ioremap UART\n");
1491 ret = -ENOMEM;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301492 goto err_ioremap;
Govindraj.Redd70ad2011-10-11 14:55:41 +05301493 }
1494
Govindraj.Rb6126332010-09-27 20:20:49 +05301495 up->port.flags = omap_up_info->flags;
Govindraj.Rb6126332010-09-27 20:20:49 +05301496 up->port.uartclk = omap_up_info->uartclk;
Rajendra Nayak8fe789d2011-12-14 17:25:44 +05301497 if (!up->port.uartclk) {
1498 up->port.uartclk = DEFAULT_CLK_SPEED;
1499 dev_warn(&pdev->dev, "No clock speed specified: using default:"
1500 "%d\n", DEFAULT_CLK_SPEED);
1501 }
Govindraj.Rb6126332010-09-27 20:20:49 +05301502
Govindraj.R2fd14962011-11-09 17:41:21 +05301503 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1504 up->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1505 pm_qos_add_request(&up->pm_qos_request,
1506 PM_QOS_CPU_DMA_LATENCY, up->latency);
1507 serial_omap_uart_wq = create_singlethread_workqueue(up->name);
1508 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
1509
Felipe Balbi93220dc2012-09-06 15:45:27 +03001510 platform_set_drvdata(pdev, up);
Ruchika Kharwar856e35b2012-09-06 15:45:31 +03001511 pm_runtime_enable(&pdev->dev);
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001512 if (omap_up_info->autosuspend_timeout == 0)
1513 omap_up_info->autosuspend_timeout = -1;
1514 device_init_wakeup(up->dev, true);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301515 pm_runtime_use_autosuspend(&pdev->dev);
1516 pm_runtime_set_autosuspend_delay(&pdev->dev,
Deepak Kc86845db2011-11-09 17:33:38 +05301517 omap_up_info->autosuspend_timeout);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301518
1519 pm_runtime_irq_safe(&pdev->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301520 pm_runtime_get_sync(&pdev->dev);
1521
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301522 omap_serial_fill_features_erratas(up);
1523
Rajendra Nayakba774332011-12-14 17:25:43 +05301524 ui[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301525 serial_omap_add_console_port(up);
1526
1527 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1528 if (ret != 0)
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301529 goto err_add_port;
Govindraj.Rb6126332010-09-27 20:20:49 +05301530
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001531 pm_runtime_mark_last_busy(up->dev);
1532 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301533 return 0;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301534
1535err_add_port:
1536 pm_runtime_put(&pdev->dev);
1537 pm_runtime_disable(&pdev->dev);
1538err_ioremap:
1539err_port_line:
Govindraj.Rb6126332010-09-27 20:20:49 +05301540 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1541 pdev->id, __func__, ret);
Govindraj.Rb6126332010-09-27 20:20:49 +05301542 return ret;
1543}
1544
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001545static int serial_omap_remove(struct platform_device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301546{
1547 struct uart_omap_port *up = platform_get_drvdata(dev);
1548
Felipe Balbi7e9c8e72012-09-06 15:45:29 +03001549 pm_runtime_put_sync(up->dev);
Felipe Balbi1b42c8b2012-09-06 15:45:28 +03001550 pm_runtime_disable(up->dev);
1551 uart_remove_one_port(&serial_omap_reg, &up->port);
1552 pm_qos_remove_request(&up->pm_qos_request);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301553
Govindraj.Rb6126332010-09-27 20:20:49 +05301554 return 0;
1555}
1556
Govindraj.R94734742011-11-07 19:00:33 +05301557/*
1558 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
1559 * The access to uart register after MDR1 Access
1560 * causes UART to corrupt data.
1561 *
1562 * Need a delay =
1563 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
1564 * give 10 times as much
1565 */
1566static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
1567{
1568 u8 timeout = 255;
1569
1570 serial_out(up, UART_OMAP_MDR1, mdr1);
1571 udelay(2);
1572 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
1573 UART_FCR_CLEAR_RCVR);
1574 /*
1575 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
1576 * TX_FIFO_E bit is 1.
1577 */
1578 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
1579 (UART_LSR_THRE | UART_LSR_DR))) {
1580 timeout--;
1581 if (!timeout) {
1582 /* Should *never* happen. we warn and carry on */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001583 dev_crit(up->dev, "Errata i202: timedout %x\n",
Govindraj.R94734742011-11-07 19:00:33 +05301584 serial_in(up, UART_LSR));
1585 break;
1586 }
1587 udelay(1);
1588 }
1589}
1590
Shubhrajyoti Db5148852012-01-16 15:52:37 +05301591#ifdef CONFIG_PM_RUNTIME
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301592static void serial_omap_restore_context(struct uart_omap_port *up)
1593{
Govindraj.R94734742011-11-07 19:00:33 +05301594 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1595 serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
1596 else
1597 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
1598
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301599 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1600 serial_out(up, UART_EFR, UART_EFR_ECB);
1601 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1602 serial_out(up, UART_IER, 0x0);
1603 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301604 serial_out(up, UART_DLL, up->dll);
1605 serial_out(up, UART_DLM, up->dlh);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301606 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1607 serial_out(up, UART_IER, up->ier);
1608 serial_out(up, UART_FCR, up->fcr);
1609 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1610 serial_out(up, UART_MCR, up->mcr);
1611 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301612 serial_out(up, UART_OMAP_SCR, up->scr);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301613 serial_out(up, UART_EFR, up->efr);
1614 serial_out(up, UART_LCR, up->lcr);
Govindraj.R94734742011-11-07 19:00:33 +05301615 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1616 serial_omap_mdr1_errataset(up, up->mdr1);
1617 else
1618 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001619 serial_out(up, UART_OMAP_WER, up->wer);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301620}
1621
Govindraj.Rfcdca752011-02-28 18:12:23 +05301622static int serial_omap_runtime_suspend(struct device *dev)
1623{
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301624 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301625
Wei Yongjun7f253012013-06-05 10:04:49 +08001626 if (!up)
1627 return -EINVAL;
1628
Sourav Poddarddd85e22013-05-15 21:05:38 +05301629 /*
1630 * When using 'no_console_suspend', the console UART must not be
1631 * suspended. Since driver suspend is managed by runtime suspend,
1632 * preventing runtime suspend (by returning error) will keep device
1633 * active during suspend.
1634 */
1635 if (up->is_suspending && !console_suspend_enabled &&
1636 uart_console(&up->port))
1637 return -EBUSY;
1638
Felipe Balbie5b57c02012-08-23 13:32:42 +03001639 up->context_loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301640
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301641 if (device_may_wakeup(dev)) {
1642 if (!up->wakeups_enabled) {
Felipe Balbie5b57c02012-08-23 13:32:42 +03001643 serial_omap_enable_wakeup(up, true);
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301644 up->wakeups_enabled = true;
1645 }
1646 } else {
1647 if (up->wakeups_enabled) {
Felipe Balbie5b57c02012-08-23 13:32:42 +03001648 serial_omap_enable_wakeup(up, false);
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301649 up->wakeups_enabled = false;
1650 }
1651 }
1652
Govindraj.R2fd14962011-11-09 17:41:21 +05301653 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1654 schedule_work(&up->qos_work);
1655
Govindraj.Rfcdca752011-02-28 18:12:23 +05301656 return 0;
1657}
1658
1659static int serial_omap_runtime_resume(struct device *dev)
1660{
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301661 struct uart_omap_port *up = dev_get_drvdata(dev);
1662
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301663 int loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301664
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301665 if (loss_cnt < 0) {
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001666 dev_dbg(dev, "serial_omap_get_context_loss_count failed : %d\n",
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301667 loss_cnt);
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301668 serial_omap_restore_context(up);
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301669 } else if (up->context_loss_cnt != loss_cnt) {
1670 serial_omap_restore_context(up);
1671 }
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301672 up->latency = up->calc_latency;
1673 schedule_work(&up->qos_work);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301674
Govindraj.Rfcdca752011-02-28 18:12:23 +05301675 return 0;
1676}
1677#endif
1678
1679static const struct dev_pm_ops serial_omap_dev_pm_ops = {
1680 SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
1681 SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
1682 serial_omap_runtime_resume, NULL)
Sourav Poddarddd85e22013-05-15 21:05:38 +05301683 .prepare = serial_omap_prepare,
1684 .complete = serial_omap_complete,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301685};
1686
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301687#if defined(CONFIG_OF)
1688static const struct of_device_id omap_serial_of_match[] = {
1689 { .compatible = "ti,omap2-uart" },
1690 { .compatible = "ti,omap3-uart" },
1691 { .compatible = "ti,omap4-uart" },
1692 {},
1693};
1694MODULE_DEVICE_TABLE(of, omap_serial_of_match);
1695#endif
1696
Govindraj.Rb6126332010-09-27 20:20:49 +05301697static struct platform_driver serial_omap_driver = {
1698 .probe = serial_omap_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001699 .remove = serial_omap_remove,
Govindraj.Rb6126332010-09-27 20:20:49 +05301700 .driver = {
1701 .name = DRIVER_NAME,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301702 .pm = &serial_omap_dev_pm_ops,
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301703 .of_match_table = of_match_ptr(omap_serial_of_match),
Govindraj.Rb6126332010-09-27 20:20:49 +05301704 },
1705};
1706
1707static int __init serial_omap_init(void)
1708{
1709 int ret;
1710
1711 ret = uart_register_driver(&serial_omap_reg);
1712 if (ret != 0)
1713 return ret;
1714 ret = platform_driver_register(&serial_omap_driver);
1715 if (ret != 0)
1716 uart_unregister_driver(&serial_omap_reg);
1717 return ret;
1718}
1719
1720static void __exit serial_omap_exit(void)
1721{
1722 platform_driver_unregister(&serial_omap_driver);
1723 uart_unregister_driver(&serial_omap_reg);
1724}
1725
1726module_init(serial_omap_init);
1727module_exit(serial_omap_exit);
1728
1729MODULE_DESCRIPTION("OMAP High Speed UART driver");
1730MODULE_LICENSE("GPL");
1731MODULE_AUTHOR("Texas Instruments Inc");