blob: 211479aa34bb20d5375078afb103829faa5bfd84 [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>
Tony Lindgren2a0b9652013-10-22 06:49:48 -070042#include <linux/of_irq.h>
NeilBrown9574f362012-07-30 10:30:26 +100043#include <linux/gpio.h>
Mark Jackson4a0ac0f2013-08-14 11:29:38 +010044#include <linux/of_gpio.h>
Tony Lindgrend9ba5732012-12-14 09:09:11 -080045#include <linux/platform_data/serial-omap.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053046
Mark Jackson4a0ac0f2013-08-14 11:29:38 +010047#include <dt-bindings/gpio/gpio.h>
48
Nishanth Menon7af0ea52014-10-22 07:46:50 -050049#define OMAP_MAX_HSUART_PORTS 10
Russell Kingf91b55ab2012-10-06 10:50:58 +010050
Govindraj.R7c77c8d2012-04-03 19:12:34 +053051#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
52
53#define OMAP_UART_REV_42 0x0402
54#define OMAP_UART_REV_46 0x0406
55#define OMAP_UART_REV_52 0x0502
56#define OMAP_UART_REV_63 0x0603
57
Govindraj.Rf64ffda2013-07-05 18:25:59 +030058#define OMAP_UART_TX_WAKEUP_EN BIT(7)
59
60/* Feature flags */
61#define OMAP_UART_WER_HAS_TX_WAKEUP BIT(0)
62
Russell Kingf91b55ab2012-10-06 10:50:58 +010063#define UART_ERRATA_i202_MDR1_ACCESS BIT(0)
64#define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1)
65
Pavel Machekfbf7ebe2014-12-11 22:44:26 +010066#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz */
Rajendra Nayak8fe789d2011-12-14 17:25:44 +053067
Paul Walmsley0ba5f662012-01-25 19:50:36 -070068/* SCR register bitmasks */
69#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
Alexey Pelykh1776fd02013-02-04 12:19:46 -050070#define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
Russell Kingf91b55ab2012-10-06 10:50:58 +010071#define OMAP_UART_SCR_TX_EMPTY (1 << 3)
Paul Walmsley0ba5f662012-01-25 19:50:36 -070072
73/* FCR register bitmasks */
Paul Walmsley0ba5f662012-01-25 19:50:36 -070074#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
Felipe Balbi6721ab72012-09-06 15:45:40 +030075#define OMAP_UART_FCR_TX_FIFO_TRIG_MASK (0x3 << 4)
Paul Walmsley0ba5f662012-01-25 19:50:36 -070076
Govindraj.R7c77c8d2012-04-03 19:12:34 +053077/* MVR register bitmasks */
78#define OMAP_UART_MVR_SCHEME_SHIFT 30
79
80#define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
81#define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
82#define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
83
84#define OMAP_UART_MVR_MAJ_MASK 0x700
85#define OMAP_UART_MVR_MAJ_SHIFT 8
86#define OMAP_UART_MVR_MIN_MASK 0x3f
87
Russell Kingf91b55ab2012-10-06 10:50:58 +010088#define OMAP_UART_DMA_CH_FREE -1
89
90#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
91#define OMAP_MODE13X_SPEED 230400
92
93/* WER = 0x7F
94 * Enable module level wakeup in WER reg
95 */
Pavel Machekfbf7ebe2014-12-11 22:44:26 +010096#define OMAP_UART_WER_MOD_WKUP 0x7F
Russell Kingf91b55ab2012-10-06 10:50:58 +010097
98/* Enable XON/XOFF flow control on output */
Russell King3af08bd2012-10-05 13:32:08 +010099#define OMAP_UART_SW_TX 0x08
Russell Kingf91b55ab2012-10-06 10:50:58 +0100100
101/* Enable XON/XOFF flow control on input */
Russell King3af08bd2012-10-05 13:32:08 +0100102#define OMAP_UART_SW_RX 0x02
Russell Kingf91b55ab2012-10-06 10:50:58 +0100103
104#define OMAP_UART_SW_CLR 0xF0
105
106#define OMAP_UART_TCR_TRIG 0x0F
107
108struct uart_omap_dma {
109 u8 uart_dma_tx;
110 u8 uart_dma_rx;
111 int rx_dma_channel;
112 int tx_dma_channel;
113 dma_addr_t rx_buf_dma_phys;
114 dma_addr_t tx_buf_dma_phys;
115 unsigned int uart_base;
116 /*
Pavel Machekfbf7ebe2014-12-11 22:44:26 +0100117 * Buffer for rx dma. It is not required for tx because the buffer
Russell Kingf91b55ab2012-10-06 10:50:58 +0100118 * comes from port structure.
119 */
120 unsigned char *rx_buf;
121 unsigned int prev_rx_dma_pos;
122 int tx_buf_size;
123 int tx_dma_used;
124 int rx_dma_used;
125 spinlock_t tx_lock;
126 spinlock_t rx_lock;
127 /* timer to poll activity on rx dma */
128 struct timer_list rx_timer;
129 unsigned int rx_buf_size;
130 unsigned int rx_poll_rate;
131 unsigned int rx_timeout;
132};
133
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300134struct uart_omap_port {
135 struct uart_port port;
136 struct uart_omap_dma uart_dma;
137 struct device *dev;
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700138 int wakeirq;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300139
140 unsigned char ier;
141 unsigned char lcr;
142 unsigned char mcr;
143 unsigned char fcr;
144 unsigned char efr;
145 unsigned char dll;
146 unsigned char dlh;
147 unsigned char mdr1;
148 unsigned char scr;
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300149 unsigned char wer;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300150
151 int use_dma;
152 /*
153 * Some bits in registers are cleared on a read, so they must
Pavel Machekfbf7ebe2014-12-11 22:44:26 +0100154 * be saved whenever the register is read, but the bits will not
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300155 * be immediately processed.
156 */
157 unsigned int lsr_break_flag;
158 unsigned char msr_saved_flags;
159 char name[20];
160 unsigned long port_activity;
Shubhrajyoti D39aee512012-10-03 17:24:36 +0530161 int context_loss_cnt;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300162 u32 errata;
163 u8 wakeups_enabled;
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300164 u32 features;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300165
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100166 int rts_gpio;
167
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300168 struct pm_qos_request pm_qos_request;
169 u32 latency;
170 u32 calc_latency;
171 struct work_struct qos_work;
Sourav Poddarddd85e22013-05-15 21:05:38 +0530172 bool is_suspending;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300173};
174
Philippe Proulxe5f9bf72013-10-23 18:49:59 -0400175#define to_uart_omap_port(p) ((container_of((p), struct uart_omap_port, port)))
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300176
Govindraj.Rb6126332010-09-27 20:20:49 +0530177static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
178
179/* Forward declaration of functions */
Govindraj.R94734742011-11-07 19:00:33 +0530180static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +0530181
182static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
183{
184 offset <<= up->port.regshift;
185 return readw(up->port.membase + offset);
186}
187
188static inline void serial_out(struct uart_omap_port *up, int offset, int value)
189{
190 offset <<= up->port.regshift;
191 writew(value, up->port.membase + offset);
192}
193
194static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
195{
196 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
197 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
198 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
199 serial_out(up, UART_FCR, 0);
200}
201
Felipe Balbie5b57c02012-08-23 13:32:42 +0300202static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
203{
Jingoo Han574de552013-07-30 17:06:57 +0900204 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300205
Felipe Balbice2f08d2012-09-07 21:10:33 +0300206 if (!pdata || !pdata->get_context_loss_count)
Tony Lindgrena630fbf2013-06-10 07:39:09 -0700207 return -EINVAL;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300208
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300209 return pdata->get_context_loss_count(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300210}
211
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700212static inline void serial_omap_enable_wakeirq(struct uart_omap_port *up,
213 bool enable)
214{
215 if (!up->wakeirq)
216 return;
217
218 if (enable)
219 enable_irq(up->wakeirq);
220 else
Tony Lindgrend758c9c2014-03-25 11:48:47 -0700221 disable_irq_nosync(up->wakeirq);
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700222}
223
Felipe Balbie5b57c02012-08-23 13:32:42 +0300224static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
225{
Jingoo Han574de552013-07-30 17:06:57 +0900226 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300227
Tony Lindgrend758c9c2014-03-25 11:48:47 -0700228 if (enable == up->wakeups_enabled)
229 return;
230
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700231 serial_omap_enable_wakeirq(up, enable);
Tony Lindgrend758c9c2014-03-25 11:48:47 -0700232 up->wakeups_enabled = enable;
233
Felipe Balbice2f08d2012-09-07 21:10:33 +0300234 if (!pdata || !pdata->enable_wakeup)
235 return;
236
237 pdata->enable_wakeup(up->dev, enable);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300238}
239
Govindraj.Rb6126332010-09-27 20:20:49 +0530240/*
Frans Klaver13d6ceb2014-09-24 09:55:22 +0200241 * Calculate the absolute difference between the desired and actual baud
242 * rate for the given mode.
243 */
244static inline int calculate_baud_abs_diff(struct uart_port *port,
245 unsigned int baud, unsigned int mode)
246{
247 unsigned int n = port->uartclk / (mode * baud);
248 int abs_diff;
249
250 if (n == 0)
251 n = 1;
252
253 abs_diff = baud - (port->uartclk / (mode * n));
254 if (abs_diff < 0)
255 abs_diff = -abs_diff;
256
257 return abs_diff;
258}
259
260/*
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500261 * serial_omap_baud_is_mode16 - check if baud rate is MODE16X
262 * @port: uart port info
263 * @baud: baudrate for which mode needs to be determined
264 *
265 * Returns true if baud rate is MODE16X and false if MODE13X
266 * Original table in OMAP TRM named "UART Mode Baud Rates, Divisor Values,
267 * and Error Rates" determines modes not for all common baud rates.
268 * E.g. for 1000000 baud rate mode must be 16x, but according to that
269 * table it's determined as 13x.
270 */
271static bool
272serial_omap_baud_is_mode16(struct uart_port *port, unsigned int baud)
273{
Frans Klaver13d6ceb2014-09-24 09:55:22 +0200274 int abs_diff_13 = calculate_baud_abs_diff(port, baud, 13);
275 int abs_diff_16 = calculate_baud_abs_diff(port, baud, 16);
Frans Klaverdc318752014-09-25 11:19:51 +0200276
Frans Klaver13d6ceb2014-09-24 09:55:22 +0200277 return (abs_diff_13 >= abs_diff_16);
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500278}
279
280/*
Govindraj.Rb6126332010-09-27 20:20:49 +0530281 * serial_omap_get_divisor - calculate divisor value
282 * @port: uart port info
283 * @baud: baudrate for which divisor needs to be calculated.
Govindraj.Rb6126332010-09-27 20:20:49 +0530284 */
285static unsigned int
286serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
287{
Alexey Pelykh4250b5d2013-09-21 04:04:35 -0400288 unsigned int mode;
Govindraj.Rb6126332010-09-27 20:20:49 +0530289
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500290 if (!serial_omap_baud_is_mode16(port, baud))
Alexey Pelykh4250b5d2013-09-21 04:04:35 -0400291 mode = 13;
Govindraj.Rb6126332010-09-27 20:20:49 +0530292 else
Alexey Pelykh4250b5d2013-09-21 04:04:35 -0400293 mode = 16;
294 return port->uartclk/(mode * baud);
Govindraj.Rb6126332010-09-27 20:20:49 +0530295}
296
Govindraj.Rb6126332010-09-27 20:20:49 +0530297static void serial_omap_enable_ms(struct uart_port *port)
298{
Felipe Balbic990f352012-08-23 13:32:41 +0300299 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530300
Rajendra Nayakba774332011-12-14 17:25:43 +0530301 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530302
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300303 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530304 up->ier |= UART_IER_MSI;
305 serial_out(up, UART_IER, up->ier);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300306 pm_runtime_mark_last_busy(up->dev);
307 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530308}
309
310static void serial_omap_stop_tx(struct uart_port *port)
311{
Felipe Balbic990f352012-08-23 13:32:41 +0300312 struct uart_omap_port *up = to_uart_omap_port(port);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100313 int res;
Govindraj.Rb6126332010-09-27 20:20:49 +0530314
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300315 pm_runtime_get_sync(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100316
Philippe Proulx018e7442013-10-23 18:49:58 -0400317 /* Handle RS-485 */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100318 if (port->rs485.flags & SER_RS485_ENABLED) {
Philippe Proulx018e7442013-10-23 18:49:58 -0400319 if (up->scr & OMAP_UART_SCR_TX_EMPTY) {
320 /* THR interrupt is fired when both TX FIFO and TX
321 * shift register are empty. This means there's nothing
322 * left to transmit now, so make sure the THR interrupt
323 * is fired when TX FIFO is below the trigger level,
324 * disable THR interrupts and toggle the RS-485 GPIO
325 * data direction pin if needed.
326 */
327 up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
328 serial_out(up, UART_OMAP_SCR, up->scr);
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100329 res = (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) ?
330 1 : 0;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100331 if (gpio_get_value(up->rts_gpio) != res) {
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100332 if (port->rs485.delay_rts_after_send > 0)
333 mdelay(
334 port->rs485.delay_rts_after_send);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100335 gpio_set_value(up->rts_gpio, res);
336 }
Philippe Proulx018e7442013-10-23 18:49:58 -0400337 } else {
338 /* We're asked to stop, but there's still stuff in the
339 * UART FIFO, so make sure the THR interrupt is fired
340 * when both TX FIFO and TX shift register are empty.
341 * The next THR interrupt (if no transmission is started
342 * in the meantime) will indicate the end of a
343 * transmission. Therefore we _don't_ disable THR
344 * interrupts in this situation.
345 */
346 up->scr |= OMAP_UART_SCR_TX_EMPTY;
347 serial_out(up, UART_OMAP_SCR, up->scr);
348 return;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100349 }
350 }
351
Govindraj.Rb6126332010-09-27 20:20:49 +0530352 if (up->ier & UART_IER_THRI) {
353 up->ier &= ~UART_IER_THRI;
354 serial_out(up, UART_IER, up->ier);
355 }
Govindraj.Rfcdca752011-02-28 18:12:23 +0530356
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100357 if ((port->rs485.flags & SER_RS485_ENABLED) &&
358 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Dimitris Lampridis3a138842014-03-13 15:11:47 +0200359 /*
360 * Empty the RX FIFO, we are not interested in anything
361 * received during the half-duplex transmission.
362 */
363 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_RCVR);
364 /* Re-enable RX interrupts */
Dimitris Lampridiscab53dc2014-03-13 15:11:46 +0200365 up->ier |= UART_IER_RLSI | UART_IER_RDI;
366 up->port.read_status_mask |= UART_LSR_DR;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100367 serial_out(up, UART_IER, up->ier);
368 }
369
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300370 pm_runtime_mark_last_busy(up->dev);
371 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530372}
373
374static void serial_omap_stop_rx(struct uart_port *port)
375{
Felipe Balbic990f352012-08-23 13:32:41 +0300376 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530377
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300378 pm_runtime_get_sync(up->dev);
Dimitris Lampridiscab53dc2014-03-13 15:11:46 +0200379 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
Govindraj.Rb6126332010-09-27 20:20:49 +0530380 up->port.read_status_mask &= ~UART_LSR_DR;
381 serial_out(up, UART_IER, up->ier);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300382 pm_runtime_mark_last_busy(up->dev);
383 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530384}
385
Felipe Balbibf63a082012-09-06 15:45:25 +0300386static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
Govindraj.Rb6126332010-09-27 20:20:49 +0530387{
388 struct circ_buf *xmit = &up->port.state->xmit;
389 int count;
390
391 if (up->port.x_char) {
392 serial_out(up, UART_TX, up->port.x_char);
393 up->port.icount.tx++;
394 up->port.x_char = 0;
395 return;
396 }
397 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
398 serial_omap_stop_tx(&up->port);
399 return;
400 }
Greg Kroah-Hartman355fe562013-08-27 16:02:18 -0700401 count = up->port.fifosize / 4;
Govindraj.Rb6126332010-09-27 20:20:49 +0530402 do {
403 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
404 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
405 up->port.icount.tx++;
406 if (uart_circ_empty(xmit))
407 break;
408 } while (--count > 0);
409
Felipe Balbi6bf78962014-04-23 09:58:27 -0500410 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Govindraj.Rb6126332010-09-27 20:20:49 +0530411 uart_write_wakeup(&up->port);
412
413 if (uart_circ_empty(xmit))
414 serial_omap_stop_tx(&up->port);
415}
416
417static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
418{
419 if (!(up->ier & UART_IER_THRI)) {
420 up->ier |= UART_IER_THRI;
421 serial_out(up, UART_IER, up->ier);
422 }
423}
424
425static void serial_omap_start_tx(struct uart_port *port)
426{
Felipe Balbic990f352012-08-23 13:32:41 +0300427 struct uart_omap_port *up = to_uart_omap_port(port);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100428 int res;
Govindraj.Rb6126332010-09-27 20:20:49 +0530429
Felipe Balbi49457432012-09-06 15:45:21 +0300430 pm_runtime_get_sync(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100431
Philippe Proulx018e7442013-10-23 18:49:58 -0400432 /* Handle RS-485 */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100433 if (port->rs485.flags & SER_RS485_ENABLED) {
Philippe Proulx018e7442013-10-23 18:49:58 -0400434 /* Fire THR interrupts when FIFO is below trigger level */
435 up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
436 serial_out(up, UART_OMAP_SCR, up->scr);
437
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100438 /* if rts not already enabled */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100439 res = (port->rs485.flags & SER_RS485_RTS_ON_SEND) ? 1 : 0;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100440 if (gpio_get_value(up->rts_gpio) != res) {
441 gpio_set_value(up->rts_gpio, res);
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100442 if (port->rs485.delay_rts_before_send > 0)
443 mdelay(port->rs485.delay_rts_before_send);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100444 }
445 }
446
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100447 if ((port->rs485.flags & SER_RS485_ENABLED) &&
448 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100449 serial_omap_stop_rx(port);
450
Felipe Balbi49457432012-09-06 15:45:21 +0300451 serial_omap_enable_ier_thri(up);
Felipe Balbi49457432012-09-06 15:45:21 +0300452 pm_runtime_mark_last_busy(up->dev);
453 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530454}
455
Russell King3af08bd2012-10-05 13:32:08 +0100456static void serial_omap_throttle(struct uart_port *port)
457{
458 struct uart_omap_port *up = to_uart_omap_port(port);
459 unsigned long flags;
460
461 pm_runtime_get_sync(up->dev);
462 spin_lock_irqsave(&up->port.lock, flags);
463 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
464 serial_out(up, UART_IER, up->ier);
465 spin_unlock_irqrestore(&up->port.lock, flags);
466 pm_runtime_mark_last_busy(up->dev);
467 pm_runtime_put_autosuspend(up->dev);
468}
469
470static void serial_omap_unthrottle(struct uart_port *port)
471{
472 struct uart_omap_port *up = to_uart_omap_port(port);
473 unsigned long flags;
474
475 pm_runtime_get_sync(up->dev);
476 spin_lock_irqsave(&up->port.lock, flags);
477 up->ier |= UART_IER_RLSI | UART_IER_RDI;
478 serial_out(up, UART_IER, up->ier);
479 spin_unlock_irqrestore(&up->port.lock, flags);
480 pm_runtime_mark_last_busy(up->dev);
481 pm_runtime_put_autosuspend(up->dev);
482}
483
Govindraj.Rb6126332010-09-27 20:20:49 +0530484static unsigned int check_modem_status(struct uart_omap_port *up)
485{
486 unsigned int status;
487
488 status = serial_in(up, UART_MSR);
489 status |= up->msr_saved_flags;
490 up->msr_saved_flags = 0;
491 if ((status & UART_MSR_ANY_DELTA) == 0)
492 return status;
493
494 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
495 up->port.state != NULL) {
496 if (status & UART_MSR_TERI)
497 up->port.icount.rng++;
498 if (status & UART_MSR_DDSR)
499 up->port.icount.dsr++;
500 if (status & UART_MSR_DDCD)
501 uart_handle_dcd_change
502 (&up->port, status & UART_MSR_DCD);
503 if (status & UART_MSR_DCTS)
504 uart_handle_cts_change
505 (&up->port, status & UART_MSR_CTS);
506 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
507 }
508
509 return status;
510}
511
Felipe Balbi72256cb2012-09-06 15:45:24 +0300512static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr)
513{
514 unsigned int flag;
Shubhrajyoti D9a12fcf2012-09-21 20:07:19 +0530515 unsigned char ch = 0;
516
517 if (likely(lsr & UART_LSR_DR))
518 ch = serial_in(up, UART_RX);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300519
520 up->port.icount.rx++;
521 flag = TTY_NORMAL;
522
523 if (lsr & UART_LSR_BI) {
524 flag = TTY_BREAK;
525 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
526 up->port.icount.brk++;
527 /*
528 * We do the SysRQ and SAK checking
529 * here because otherwise the break
530 * may get masked by ignore_status_mask
531 * or read_status_mask.
532 */
533 if (uart_handle_break(&up->port))
534 return;
535
536 }
537
538 if (lsr & UART_LSR_PE) {
539 flag = TTY_PARITY;
540 up->port.icount.parity++;
541 }
542
543 if (lsr & UART_LSR_FE) {
544 flag = TTY_FRAME;
545 up->port.icount.frame++;
546 }
547
548 if (lsr & UART_LSR_OE)
549 up->port.icount.overrun++;
550
551#ifdef CONFIG_SERIAL_OMAP_CONSOLE
552 if (up->port.line == up->port.cons->index) {
553 /* Recover the break flag from console xmit */
554 lsr |= up->lsr_break_flag;
555 }
556#endif
557 uart_insert_char(&up->port, lsr, UART_LSR_OE, 0, flag);
558}
559
560static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr)
561{
562 unsigned char ch = 0;
563 unsigned int flag;
564
565 if (!(lsr & UART_LSR_DR))
566 return;
567
568 ch = serial_in(up, UART_RX);
569 flag = TTY_NORMAL;
570 up->port.icount.rx++;
571
572 if (uart_handle_sysrq_char(&up->port, ch))
573 return;
574
575 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
576}
577
Govindraj.Rb6126332010-09-27 20:20:49 +0530578/**
579 * serial_omap_irq() - This handles the interrupt from one port
580 * @irq: uart port irq number
581 * @dev_id: uart port info
582 */
Felipe Balbi52c55132012-09-06 15:45:33 +0300583static irqreturn_t serial_omap_irq(int irq, void *dev_id)
Govindraj.Rb6126332010-09-27 20:20:49 +0530584{
585 struct uart_omap_port *up = dev_id;
586 unsigned int iir, lsr;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300587 unsigned int type;
Greg Kroah-Hartman7b013e42013-08-27 15:59:53 -0700588 irqreturn_t ret = IRQ_NONE;
Felipe Balbi72256cb2012-09-06 15:45:24 +0300589 int max_count = 256;
Govindraj.Rb6126332010-09-27 20:20:49 +0530590
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300591 spin_lock(&up->port.lock);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300592 pm_runtime_get_sync(up->dev);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300593
Felipe Balbi72256cb2012-09-06 15:45:24 +0300594 do {
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300595 iir = serial_in(up, UART_IIR);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300596 if (iir & UART_IIR_NO_INT)
597 break;
Govindraj.Rb6126332010-09-27 20:20:49 +0530598
Greg Kroah-Hartman7b013e42013-08-27 15:59:53 -0700599 ret = IRQ_HANDLED;
Felipe Balbi72256cb2012-09-06 15:45:24 +0300600 lsr = serial_in(up, UART_LSR);
601
602 /* extract IRQ type from IIR register */
603 type = iir & 0x3e;
604
605 switch (type) {
606 case UART_IIR_MSI:
607 check_modem_status(up);
608 break;
609 case UART_IIR_THRI:
Felipe Balbibf63a082012-09-06 15:45:25 +0300610 transmit_chars(up, lsr);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300611 break;
612 case UART_IIR_RX_TIMEOUT:
613 /* FALLTHROUGH */
614 case UART_IIR_RDI:
615 serial_omap_rdi(up, lsr);
616 break;
617 case UART_IIR_RLSI:
618 serial_omap_rlsi(up, lsr);
619 break;
620 case UART_IIR_CTS_RTS_DSR:
621 /* simply try again */
622 break;
623 case UART_IIR_XOFF:
624 /* FALLTHROUGH */
625 default:
626 break;
627 }
628 } while (!(iir & UART_IIR_NO_INT) && max_count--);
629
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300630 spin_unlock(&up->port.lock);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300631
Jiri Slaby2e124b42013-01-03 15:53:06 +0100632 tty_flip_buffer_push(&up->port.state->port);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300633
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300634 pm_runtime_mark_last_busy(up->dev);
635 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530636 up->port_activity = jiffies;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300637
Greg Kroah-Hartman7b013e42013-08-27 15:59:53 -0700638 return ret;
Govindraj.Rb6126332010-09-27 20:20:49 +0530639}
640
641static unsigned int serial_omap_tx_empty(struct uart_port *port)
642{
Felipe Balbic990f352012-08-23 13:32:41 +0300643 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530644 unsigned long flags = 0;
645 unsigned int ret = 0;
646
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300647 pm_runtime_get_sync(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +0530648 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530649 spin_lock_irqsave(&up->port.lock, flags);
650 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
651 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300652 pm_runtime_mark_last_busy(up->dev);
653 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530654 return ret;
655}
656
657static unsigned int serial_omap_get_mctrl(struct uart_port *port)
658{
Felipe Balbic990f352012-08-23 13:32:41 +0300659 struct uart_omap_port *up = to_uart_omap_port(port);
Shubhrajyoti D514f31d2011-11-21 15:43:28 +0530660 unsigned int status;
Govindraj.Rb6126332010-09-27 20:20:49 +0530661 unsigned int ret = 0;
662
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300663 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530664 status = check_modem_status(up);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300665 pm_runtime_mark_last_busy(up->dev);
666 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530667
Rajendra Nayakba774332011-12-14 17:25:43 +0530668 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530669
670 if (status & UART_MSR_DCD)
671 ret |= TIOCM_CAR;
672 if (status & UART_MSR_RI)
673 ret |= TIOCM_RNG;
674 if (status & UART_MSR_DSR)
675 ret |= TIOCM_DSR;
676 if (status & UART_MSR_CTS)
677 ret |= TIOCM_CTS;
678 return ret;
679}
680
681static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
682{
Felipe Balbic990f352012-08-23 13:32:41 +0300683 struct uart_omap_port *up = to_uart_omap_port(port);
Peter Hurley348f9bb2015-01-25 14:44:53 -0500684 unsigned char mcr = 0, old_mcr, lcr;
Govindraj.Rb6126332010-09-27 20:20:49 +0530685
Rajendra Nayakba774332011-12-14 17:25:43 +0530686 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530687 if (mctrl & TIOCM_RTS)
688 mcr |= UART_MCR_RTS;
689 if (mctrl & TIOCM_DTR)
690 mcr |= UART_MCR_DTR;
691 if (mctrl & TIOCM_OUT1)
692 mcr |= UART_MCR_OUT1;
693 if (mctrl & TIOCM_OUT2)
694 mcr |= UART_MCR_OUT2;
695 if (mctrl & TIOCM_LOOP)
696 mcr |= UART_MCR_LOOP;
697
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300698 pm_runtime_get_sync(up->dev);
Russell King9363f8f2012-10-05 12:23:28 +0100699 old_mcr = serial_in(up, UART_MCR);
700 old_mcr &= ~(UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_OUT1 |
701 UART_MCR_DTR | UART_MCR_RTS);
702 up->mcr = old_mcr | mcr;
Govindraj.Rc538d202011-11-07 18:57:03 +0530703 serial_out(up, UART_MCR, up->mcr);
Peter Hurley348f9bb2015-01-25 14:44:53 -0500704
705 /* Turn off autoRTS if RTS is lowered; restore autoRTS if RTS raised */
706 lcr = serial_in(up, UART_LCR);
707 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
708 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
709 up->efr |= UART_EFR_RTS;
710 else
711 up->efr &= UART_EFR_RTS;
712 serial_out(up, UART_EFR, up->efr);
713 serial_out(up, UART_LCR, lcr);
714
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300715 pm_runtime_mark_last_busy(up->dev);
716 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530717}
718
719static void serial_omap_break_ctl(struct uart_port *port, int break_state)
720{
Felipe Balbic990f352012-08-23 13:32:41 +0300721 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530722 unsigned long flags = 0;
723
Rajendra Nayakba774332011-12-14 17:25:43 +0530724 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300725 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530726 spin_lock_irqsave(&up->port.lock, flags);
727 if (break_state == -1)
728 up->lcr |= UART_LCR_SBC;
729 else
730 up->lcr &= ~UART_LCR_SBC;
731 serial_out(up, UART_LCR, up->lcr);
732 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300733 pm_runtime_mark_last_busy(up->dev);
734 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530735}
736
737static int serial_omap_startup(struct uart_port *port)
738{
Felipe Balbic990f352012-08-23 13:32:41 +0300739 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530740 unsigned long flags = 0;
741 int retval;
742
743 /*
744 * Allocate the IRQ
745 */
746 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
747 up->name, up);
748 if (retval)
749 return retval;
750
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700751 /* Optional wake-up IRQ */
752 if (up->wakeirq) {
753 retval = request_irq(up->wakeirq, serial_omap_irq,
754 up->port.irqflags, up->name, up);
755 if (retval) {
756 free_irq(up->port.irq, up);
757 return retval;
758 }
759 disable_irq(up->wakeirq);
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700760 }
761
Rajendra Nayakba774332011-12-14 17:25:43 +0530762 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530763
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300764 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530765 /*
766 * Clear the FIFO buffers and disable them.
767 * (they will be reenabled in set_termios())
768 */
769 serial_omap_clear_fifos(up);
Govindraj.Rb6126332010-09-27 20:20:49 +0530770
771 /*
772 * Clear the interrupt registers.
773 */
774 (void) serial_in(up, UART_LSR);
775 if (serial_in(up, UART_LSR) & UART_LSR_DR)
776 (void) serial_in(up, UART_RX);
777 (void) serial_in(up, UART_IIR);
778 (void) serial_in(up, UART_MSR);
779
780 /*
781 * Now, initialize the UART
782 */
783 serial_out(up, UART_LCR, UART_LCR_WLEN8);
784 spin_lock_irqsave(&up->port.lock, flags);
785 /*
786 * Most PC uarts need OUT2 raised to enable interrupts.
787 */
788 up->port.mctrl |= TIOCM_OUT2;
789 serial_omap_set_mctrl(&up->port, up->port.mctrl);
790 spin_unlock_irqrestore(&up->port.lock, flags);
791
792 up->msr_saved_flags = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530793 /*
794 * Finally, enable interrupts. Note: Modem status interrupts
795 * are set via set_termios(), which will be occurring imminently
796 * anyway, so we don't enable them here.
797 */
798 up->ier = UART_IER_RLSI | UART_IER_RDI;
799 serial_out(up, UART_IER, up->ier);
800
Jarkko Nikula78841462011-01-24 17:51:22 +0200801 /* Enable module level wake up */
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300802 up->wer = OMAP_UART_WER_MOD_WKUP;
803 if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
804 up->wer |= OMAP_UART_TX_WAKEUP_EN;
805
806 serial_out(up, UART_OMAP_WER, up->wer);
Jarkko Nikula78841462011-01-24 17:51:22 +0200807
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300808 pm_runtime_mark_last_busy(up->dev);
809 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530810 up->port_activity = jiffies;
811 return 0;
812}
813
814static void serial_omap_shutdown(struct uart_port *port)
815{
Felipe Balbic990f352012-08-23 13:32:41 +0300816 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530817 unsigned long flags = 0;
818
Rajendra Nayakba774332011-12-14 17:25:43 +0530819 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530820
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300821 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530822 /*
823 * Disable interrupts from this port
824 */
825 up->ier = 0;
826 serial_out(up, UART_IER, 0);
827
828 spin_lock_irqsave(&up->port.lock, flags);
829 up->port.mctrl &= ~TIOCM_OUT2;
830 serial_omap_set_mctrl(&up->port, up->port.mctrl);
831 spin_unlock_irqrestore(&up->port.lock, flags);
832
833 /*
834 * Disable break condition and FIFOs
835 */
836 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
837 serial_omap_clear_fifos(up);
838
839 /*
840 * Read data port to reset things, and then free the irq
841 */
842 if (serial_in(up, UART_LSR) & UART_LSR_DR)
843 (void) serial_in(up, UART_RX);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530844
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300845 pm_runtime_mark_last_busy(up->dev);
846 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530847 free_irq(up->port.irq, up);
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700848 if (up->wakeirq)
849 free_irq(up->wakeirq, up);
Govindraj.Rb6126332010-09-27 20:20:49 +0530850}
851
Govindraj.R2fd14962011-11-09 17:41:21 +0530852static void serial_omap_uart_qos_work(struct work_struct *work)
853{
854 struct uart_omap_port *up = container_of(work, struct uart_omap_port,
855 qos_work);
856
857 pm_qos_update_request(&up->pm_qos_request, up->latency);
858}
859
Govindraj.Rb6126332010-09-27 20:20:49 +0530860static void
861serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
862 struct ktermios *old)
863{
Felipe Balbic990f352012-08-23 13:32:41 +0300864 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530865 unsigned char cval = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530866 unsigned long flags = 0;
867 unsigned int baud, quot;
868
869 switch (termios->c_cflag & CSIZE) {
870 case CS5:
871 cval = UART_LCR_WLEN5;
872 break;
873 case CS6:
874 cval = UART_LCR_WLEN6;
875 break;
876 case CS7:
877 cval = UART_LCR_WLEN7;
878 break;
879 default:
880 case CS8:
881 cval = UART_LCR_WLEN8;
882 break;
883 }
884
885 if (termios->c_cflag & CSTOPB)
886 cval |= UART_LCR_STOP;
887 if (termios->c_cflag & PARENB)
888 cval |= UART_LCR_PARITY;
889 if (!(termios->c_cflag & PARODD))
890 cval |= UART_LCR_EPAR;
Enric Balletbo i Serrafdbc7352012-12-06 09:45:04 +0100891 if (termios->c_cflag & CMSPAR)
892 cval |= UART_LCR_SPAR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530893
894 /*
895 * Ask the core to calculate the divisor for us.
896 */
897
898 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
899 quot = serial_omap_get_divisor(port, baud);
900
Govindraj.R2fd14962011-11-09 17:41:21 +0530901 /* calculate wakeup latency constraint */
Paul Walmsley19723452012-01-25 19:50:56 -0700902 up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
Govindraj.R2fd14962011-11-09 17:41:21 +0530903 up->latency = up->calc_latency;
904 schedule_work(&up->qos_work);
905
Govindraj.Rc538d202011-11-07 18:57:03 +0530906 up->dll = quot & 0xff;
907 up->dlh = quot >> 8;
908 up->mdr1 = UART_OMAP_MDR1_DISABLE;
909
Govindraj.Rb6126332010-09-27 20:20:49 +0530910 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
911 UART_FCR_ENABLE_FIFO;
Govindraj.Rb6126332010-09-27 20:20:49 +0530912
913 /*
914 * Ok, we're now changing the port state. Do it with
915 * interrupts disabled.
916 */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300917 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530918 spin_lock_irqsave(&up->port.lock, flags);
919
920 /*
921 * Update the per-port timeout.
922 */
923 uart_update_timeout(port, termios->c_cflag, baud);
924
925 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
926 if (termios->c_iflag & INPCK)
927 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
928 if (termios->c_iflag & (BRKINT | PARMRK))
929 up->port.read_status_mask |= UART_LSR_BI;
930
931 /*
932 * Characters to ignore
933 */
934 up->port.ignore_status_mask = 0;
935 if (termios->c_iflag & IGNPAR)
936 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
937 if (termios->c_iflag & IGNBRK) {
938 up->port.ignore_status_mask |= UART_LSR_BI;
939 /*
940 * If we're ignoring parity and break indicators,
941 * ignore overruns too (for real raw support).
942 */
943 if (termios->c_iflag & IGNPAR)
944 up->port.ignore_status_mask |= UART_LSR_OE;
945 }
946
947 /*
948 * ignore all characters if CREAD is not set
949 */
950 if ((termios->c_cflag & CREAD) == 0)
951 up->port.ignore_status_mask |= UART_LSR_DR;
952
953 /*
954 * Modem status interrupts
955 */
956 up->ier &= ~UART_IER_MSI;
957 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
958 up->ier |= UART_IER_MSI;
959 serial_out(up, UART_IER, up->ier);
960 serial_out(up, UART_LCR, cval); /* reset DLAB */
Govindraj.Rc538d202011-11-07 18:57:03 +0530961 up->lcr = cval;
Alexey Pelykh1776fd02013-02-04 12:19:46 -0500962 up->scr = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530963
964 /* FIFOs and DMA Settings */
965
966 /* FCR can be changed only when the
967 * baud clock is not running
968 * DLL_REG and DLH_REG set to 0.
969 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800970 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530971 serial_out(up, UART_DLL, 0);
972 serial_out(up, UART_DLM, 0);
973 serial_out(up, UART_LCR, 0);
974
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800975 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530976
Russell King08bd4902012-10-05 13:54:53 +0100977 up->efr = serial_in(up, UART_EFR) & ~UART_EFR_ECB;
Russell Kingd864c032012-10-06 00:51:17 +0100978 up->efr &= ~UART_EFR_SCD;
Govindraj.Rb6126332010-09-27 20:20:49 +0530979 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
980
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800981 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Russell King08bd4902012-10-05 13:54:53 +0100982 up->mcr = serial_in(up, UART_MCR) & ~UART_MCR_TCRTLR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530983 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
984 /* FIFO ENABLE, DMA MODE */
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700985
Alexey Pelykh1f663962013-04-03 14:31:46 -0400986 up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
987 /*
988 * NOTE: Setting OMAP_UART_SCR_RX_TRIG_GRANU1_MASK
989 * sets Enables the granularity of 1 for TRIGGER RX
990 * level. Along with setting RX FIFO trigger level
991 * to 1 (as noted below, 16 characters) and TLR[3:0]
992 * to zero this will result RX FIFO threshold level
993 * to 1 character, instead of 16 as noted in comment
994 * below.
995 */
996
Felipe Balbi6721ab72012-09-06 15:45:40 +0300997 /* Set receive FIFO threshold to 16 characters and
Philippe Proulx018e7442013-10-23 18:49:58 -0400998 * transmit FIFO threshold to 32 spaces
Felipe Balbi6721ab72012-09-06 15:45:40 +0300999 */
Felipe Balbi49457432012-09-06 15:45:21 +03001000 up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
Felipe Balbi6721ab72012-09-06 15:45:40 +03001001 up->fcr &= ~OMAP_UART_FCR_TX_FIFO_TRIG_MASK;
1002 up->fcr |= UART_FCR6_R_TRIGGER_16 | UART_FCR6_T_TRIGGER_24 |
1003 UART_FCR_ENABLE_FIFO;
Greg Kroah-Hartman8a74e9f2012-01-26 11:15:18 -08001004
Paul Walmsley0ba5f662012-01-25 19:50:36 -07001005 serial_out(up, UART_FCR, up->fcr);
1006 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1007
Govindraj.Rc538d202011-11-07 18:57:03 +05301008 serial_out(up, UART_OMAP_SCR, up->scr);
1009
Russell King08bd4902012-10-05 13:54:53 +01001010 /* Reset UART_MCR_TCRTLR: this must be done with the EFR_ECB bit set */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001011 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +05301012 serial_out(up, UART_MCR, up->mcr);
Russell King08bd4902012-10-05 13:54:53 +01001013 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1014 serial_out(up, UART_EFR, up->efr);
1015 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +05301016
1017 /* Protocol, Baud Rate, and Interrupt Settings */
1018
Govindraj.R94734742011-11-07 19:00:33 +05301019 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1020 serial_omap_mdr1_errataset(up, up->mdr1);
1021 else
1022 serial_out(up, UART_OMAP_MDR1, up->mdr1);
1023
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001024 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301025 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
1026
1027 serial_out(up, UART_LCR, 0);
1028 serial_out(up, UART_IER, 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001029 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301030
Govindraj.Rc538d202011-11-07 18:57:03 +05301031 serial_out(up, UART_DLL, up->dll); /* LS of divisor */
1032 serial_out(up, UART_DLM, up->dlh); /* MS of divisor */
Govindraj.Rb6126332010-09-27 20:20:49 +05301033
1034 serial_out(up, UART_LCR, 0);
1035 serial_out(up, UART_IER, up->ier);
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
1038 serial_out(up, UART_EFR, up->efr);
1039 serial_out(up, UART_LCR, cval);
1040
Alexey Pelykh5fe21232013-01-16 05:08:06 -05001041 if (!serial_omap_baud_is_mode16(port, baud))
Govindraj.Rc538d202011-11-07 18:57:03 +05301042 up->mdr1 = UART_OMAP_MDR1_13X_MODE;
Govindraj.Rb6126332010-09-27 20:20:49 +05301043 else
Govindraj.Rc538d202011-11-07 18:57:03 +05301044 up->mdr1 = UART_OMAP_MDR1_16X_MODE;
1045
Govindraj.R94734742011-11-07 19:00:33 +05301046 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1047 serial_omap_mdr1_errataset(up, up->mdr1);
1048 else
1049 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +05301050
Russell Kingc533e512012-10-06 09:34:36 +01001051 /* Configure flow control */
Russell Kingc7d059c2012-10-06 09:12:44 +01001052 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301053
Russell Kingc533e512012-10-06 09:34:36 +01001054 /* XON1/XOFF1 accessible mode B, TCRTLR=0, ECB=0 */
1055 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
1056 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
Govindraj.Rb6126332010-09-27 20:20:49 +05301057
Russell Kingc533e512012-10-06 09:34:36 +01001058 /* Enable access to TCR/TLR */
Russell Kingc7d059c2012-10-06 09:12:44 +01001059 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
1060 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1061 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
Govindraj.Rb6126332010-09-27 20:20:49 +05301062
Russell Kingc7d059c2012-10-06 09:12:44 +01001063 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
Govindraj.Rb6126332010-09-27 20:20:49 +05301064
Peter Hurley391f93f2015-01-25 14:44:51 -05001065 up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
1066
Russell King08bd4902012-10-05 13:54:53 +01001067 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
Peter Hurley348f9bb2015-01-25 14:44:53 -05001068 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
Peter Hurley391f93f2015-01-25 14:44:51 -05001069 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Peter Hurley348f9bb2015-01-25 14:44:53 -05001070 up->efr |= UART_EFR_CTS;
Russell King0d5b1662012-10-05 23:48:28 +01001071 } else {
1072 /* Disable AUTORTS and AUTOCTS */
1073 up->efr &= ~(UART_EFR_CTS | UART_EFR_RTS);
Govindraj.Rb6126332010-09-27 20:20:49 +05301074 }
1075
Russell King01d70bb2012-10-15 16:50:59 +01001076 if (up->port.flags & UPF_SOFT_FLOW) {
Russell King01d70bb2012-10-15 16:50:59 +01001077 /* clear SW control mode bits */
1078 up->efr &= OMAP_UART_SW_CLR;
1079
1080 /*
1081 * IXON Flag:
Russell King01d70bb2012-10-15 16:50:59 +01001082 * Enable XON/XOFF flow control on input.
1083 * Receiver compares XON1, XOFF1.
1084 */
Russell King3af08bd2012-10-05 13:32:08 +01001085 if (termios->c_iflag & IXON)
Russell King01d70bb2012-10-15 16:50:59 +01001086 up->efr |= OMAP_UART_SW_RX;
1087
Russell King01d70bb2012-10-15 16:50:59 +01001088 /*
Russell King3af08bd2012-10-05 13:32:08 +01001089 * IXOFF Flag:
1090 * Enable XON/XOFF flow control on output.
1091 * Transmit XON1, XOFF1
1092 */
Peter Hurley391f93f2015-01-25 14:44:51 -05001093 if (termios->c_iflag & IXOFF) {
1094 up->port.status |= UPSTAT_AUTOXOFF;
Russell King3af08bd2012-10-05 13:32:08 +01001095 up->efr |= OMAP_UART_SW_TX;
Peter Hurley391f93f2015-01-25 14:44:51 -05001096 }
Russell King3af08bd2012-10-05 13:32:08 +01001097
1098 /*
Russell King01d70bb2012-10-15 16:50:59 +01001099 * IXANY Flag:
1100 * Enable any character to restart output.
1101 * Operation resumes after receiving any
1102 * character after recognition of the XOFF character
1103 */
1104 if (termios->c_iflag & IXANY)
1105 up->mcr |= UART_MCR_XONANY;
1106 else
1107 up->mcr &= ~UART_MCR_XONANY;
Russell King01d70bb2012-10-15 16:50:59 +01001108 }
Russell Kingc7d059c2012-10-06 09:12:44 +01001109 serial_out(up, UART_MCR, up->mcr);
Russell King18f360f2012-10-06 09:08:20 +01001110 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1111 serial_out(up, UART_EFR, up->efr);
1112 serial_out(up, UART_LCR, up->lcr);
1113
Govindraj.Rb6126332010-09-27 20:20:49 +05301114 serial_omap_set_mctrl(&up->port, up->port.mctrl);
Govindraj.Rb6126332010-09-27 20:20:49 +05301115
1116 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001117 pm_runtime_mark_last_busy(up->dev);
1118 pm_runtime_put_autosuspend(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +05301119 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301120}
1121
1122static void
1123serial_omap_pm(struct uart_port *port, unsigned int state,
1124 unsigned int oldstate)
1125{
Felipe Balbic990f352012-08-23 13:32:41 +03001126 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301127 unsigned char efr;
1128
Rajendra Nayakba774332011-12-14 17:25:43 +05301129 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301130
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001131 pm_runtime_get_sync(up->dev);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001132 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301133 efr = serial_in(up, UART_EFR);
1134 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
1135 serial_out(up, UART_LCR, 0);
1136
1137 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001138 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301139 serial_out(up, UART_EFR, efr);
1140 serial_out(up, UART_LCR, 0);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301141
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001142 if (!device_may_wakeup(up->dev)) {
Govindraj.Rfcdca752011-02-28 18:12:23 +05301143 if (!state)
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001144 pm_runtime_forbid(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301145 else
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001146 pm_runtime_allow(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301147 }
1148
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001149 pm_runtime_mark_last_busy(up->dev);
1150 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301151}
1152
1153static void serial_omap_release_port(struct uart_port *port)
1154{
1155 dev_dbg(port->dev, "serial_omap_release_port+\n");
1156}
1157
1158static int serial_omap_request_port(struct uart_port *port)
1159{
1160 dev_dbg(port->dev, "serial_omap_request_port+\n");
1161 return 0;
1162}
1163
1164static void serial_omap_config_port(struct uart_port *port, int flags)
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 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
Rajendra Nayakba774332011-12-14 17:25:43 +05301169 up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301170 up->port.type = PORT_OMAP;
Russell King3af08bd2012-10-05 13:32:08 +01001171 up->port.flags |= UPF_SOFT_FLOW | UPF_HARD_FLOW;
Govindraj.Rb6126332010-09-27 20:20:49 +05301172}
1173
1174static int
1175serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
1176{
1177 /* we don't want the core code to modify any port params */
1178 dev_dbg(port->dev, "serial_omap_verify_port+\n");
1179 return -EINVAL;
1180}
1181
1182static const char *
1183serial_omap_type(struct uart_port *port)
1184{
Felipe Balbic990f352012-08-23 13:32:41 +03001185 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301186
Rajendra Nayakba774332011-12-14 17:25:43 +05301187 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301188 return up->name;
1189}
1190
Govindraj.Rb6126332010-09-27 20:20:49 +05301191#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1192
1193static inline void wait_for_xmitr(struct uart_omap_port *up)
1194{
1195 unsigned int status, tmout = 10000;
1196
1197 /* Wait up to 10ms for the character(s) to be sent. */
1198 do {
1199 status = serial_in(up, UART_LSR);
1200
1201 if (status & UART_LSR_BI)
1202 up->lsr_break_flag = UART_LSR_BI;
1203
1204 if (--tmout == 0)
1205 break;
1206 udelay(1);
1207 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1208
1209 /* Wait up to 1s for flow control if necessary */
1210 if (up->port.flags & UPF_CONS_FLOW) {
1211 tmout = 1000000;
1212 for (tmout = 1000000; tmout; tmout--) {
1213 unsigned int msr = serial_in(up, UART_MSR);
1214
1215 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1216 if (msr & UART_MSR_CTS)
1217 break;
1218
1219 udelay(1);
1220 }
1221 }
1222}
1223
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001224#ifdef CONFIG_CONSOLE_POLL
1225
1226static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
1227{
Felipe Balbic990f352012-08-23 13:32:41 +03001228 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301229
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001230 pm_runtime_get_sync(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001231 wait_for_xmitr(up);
1232 serial_out(up, UART_TX, ch);
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001233 pm_runtime_mark_last_busy(up->dev);
1234 pm_runtime_put_autosuspend(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001235}
1236
1237static int serial_omap_poll_get_char(struct uart_port *port)
1238{
Felipe Balbic990f352012-08-23 13:32:41 +03001239 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301240 unsigned int status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001241
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001242 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301243 status = serial_in(up, UART_LSR);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001244 if (!(status & UART_LSR_DR)) {
1245 status = NO_POLL_CHAR;
1246 goto out;
1247 }
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001248
Govindraj.Rfcdca752011-02-28 18:12:23 +05301249 status = serial_in(up, UART_RX);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001250
1251out:
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001252 pm_runtime_mark_last_busy(up->dev);
1253 pm_runtime_put_autosuspend(up->dev);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001254
Govindraj.Rfcdca752011-02-28 18:12:23 +05301255 return status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001256}
1257
1258#endif /* CONFIG_CONSOLE_POLL */
1259
1260#ifdef CONFIG_SERIAL_OMAP_CONSOLE
1261
Shubhrajyoti D40477d02012-10-03 17:24:38 +05301262static struct uart_omap_port *serial_omap_console_ports[OMAP_MAX_HSUART_PORTS];
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001263
1264static struct uart_driver serial_omap_reg;
1265
Govindraj.Rb6126332010-09-27 20:20:49 +05301266static void serial_omap_console_putchar(struct uart_port *port, int ch)
1267{
Felipe Balbic990f352012-08-23 13:32:41 +03001268 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301269
1270 wait_for_xmitr(up);
1271 serial_out(up, UART_TX, ch);
1272}
1273
1274static void
1275serial_omap_console_write(struct console *co, const char *s,
1276 unsigned int count)
1277{
1278 struct uart_omap_port *up = serial_omap_console_ports[co->index];
1279 unsigned long flags;
1280 unsigned int ier;
1281 int locked = 1;
1282
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001283 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301284
Govindraj.Rb6126332010-09-27 20:20:49 +05301285 local_irq_save(flags);
1286 if (up->port.sysrq)
1287 locked = 0;
1288 else if (oops_in_progress)
1289 locked = spin_trylock(&up->port.lock);
1290 else
1291 spin_lock(&up->port.lock);
1292
1293 /*
1294 * First save the IER then disable the interrupts
1295 */
1296 ier = serial_in(up, UART_IER);
1297 serial_out(up, UART_IER, 0);
1298
1299 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
1300
1301 /*
1302 * Finally, wait for transmitter to become empty
1303 * and restore the IER
1304 */
1305 wait_for_xmitr(up);
1306 serial_out(up, UART_IER, ier);
1307 /*
1308 * The receive handling will happen properly because the
1309 * receive ready bit will still be set; it is not cleared
1310 * on read. However, modem control will not, we must
1311 * call it if we have saved something in the saved flags
1312 * while processing with interrupts off.
1313 */
1314 if (up->msr_saved_flags)
1315 check_modem_status(up);
1316
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001317 pm_runtime_mark_last_busy(up->dev);
1318 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301319 if (locked)
1320 spin_unlock(&up->port.lock);
1321 local_irq_restore(flags);
1322}
1323
1324static int __init
1325serial_omap_console_setup(struct console *co, char *options)
1326{
1327 struct uart_omap_port *up;
1328 int baud = 115200;
1329 int bits = 8;
1330 int parity = 'n';
1331 int flow = 'n';
1332
1333 if (serial_omap_console_ports[co->index] == NULL)
1334 return -ENODEV;
1335 up = serial_omap_console_ports[co->index];
1336
1337 if (options)
1338 uart_parse_options(options, &baud, &parity, &bits, &flow);
1339
1340 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1341}
1342
1343static struct console serial_omap_console = {
1344 .name = OMAP_SERIAL_NAME,
1345 .write = serial_omap_console_write,
1346 .device = uart_console_device,
1347 .setup = serial_omap_console_setup,
1348 .flags = CON_PRINTBUFFER,
1349 .index = -1,
1350 .data = &serial_omap_reg,
1351};
1352
1353static void serial_omap_add_console_port(struct uart_omap_port *up)
1354{
Rajendra Nayakba774332011-12-14 17:25:43 +05301355 serial_omap_console_ports[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301356}
1357
1358#define OMAP_CONSOLE (&serial_omap_console)
1359
1360#else
1361
1362#define OMAP_CONSOLE NULL
1363
1364static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1365{}
1366
1367#endif
1368
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001369/* Enable or disable the rs485 support */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001370static int
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001371serial_omap_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
1372{
1373 struct uart_omap_port *up = to_uart_omap_port(port);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001374 unsigned int mode;
1375 int val;
1376
1377 pm_runtime_get_sync(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001378
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001379 /* Disable interrupts from this port */
1380 mode = up->ier;
1381 up->ier = 0;
1382 serial_out(up, UART_IER, 0);
1383
1384 /* store new config */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001385 port->rs485 = *rs485conf;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001386
1387 /*
1388 * Just as a precaution, only allow rs485
1389 * to be enabled if the gpio pin is valid
1390 */
1391 if (gpio_is_valid(up->rts_gpio)) {
1392 /* enable / disable rts */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001393 val = (port->rs485.flags & SER_RS485_ENABLED) ?
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001394 SER_RS485_RTS_AFTER_SEND : SER_RS485_RTS_ON_SEND;
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001395 val = (port->rs485.flags & val) ? 1 : 0;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001396 gpio_set_value(up->rts_gpio, val);
1397 } else
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001398 port->rs485.flags &= ~SER_RS485_ENABLED;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001399
1400 /* Enable interrupts */
1401 up->ier = mode;
1402 serial_out(up, UART_IER, up->ier);
1403
Philippe Proulx018e7442013-10-23 18:49:58 -04001404 /* If RS-485 is disabled, make sure the THR interrupt is fired when
1405 * TX FIFO is below the trigger level.
1406 */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001407 if (!(port->rs485.flags & SER_RS485_ENABLED) &&
Philippe Proulx018e7442013-10-23 18:49:58 -04001408 (up->scr & OMAP_UART_SCR_TX_EMPTY)) {
1409 up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
1410 serial_out(up, UART_OMAP_SCR, up->scr);
1411 }
1412
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001413 pm_runtime_mark_last_busy(up->dev);
1414 pm_runtime_put_autosuspend(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001415
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001416 return 0;
1417}
1418
Govindraj.Rb6126332010-09-27 20:20:49 +05301419static struct uart_ops serial_omap_pops = {
1420 .tx_empty = serial_omap_tx_empty,
1421 .set_mctrl = serial_omap_set_mctrl,
1422 .get_mctrl = serial_omap_get_mctrl,
1423 .stop_tx = serial_omap_stop_tx,
1424 .start_tx = serial_omap_start_tx,
Russell King3af08bd2012-10-05 13:32:08 +01001425 .throttle = serial_omap_throttle,
1426 .unthrottle = serial_omap_unthrottle,
Govindraj.Rb6126332010-09-27 20:20:49 +05301427 .stop_rx = serial_omap_stop_rx,
1428 .enable_ms = serial_omap_enable_ms,
1429 .break_ctl = serial_omap_break_ctl,
1430 .startup = serial_omap_startup,
1431 .shutdown = serial_omap_shutdown,
1432 .set_termios = serial_omap_set_termios,
1433 .pm = serial_omap_pm,
1434 .type = serial_omap_type,
1435 .release_port = serial_omap_release_port,
1436 .request_port = serial_omap_request_port,
1437 .config_port = serial_omap_config_port,
1438 .verify_port = serial_omap_verify_port,
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001439#ifdef CONFIG_CONSOLE_POLL
1440 .poll_put_char = serial_omap_poll_put_char,
1441 .poll_get_char = serial_omap_poll_get_char,
1442#endif
Govindraj.Rb6126332010-09-27 20:20:49 +05301443};
1444
1445static struct uart_driver serial_omap_reg = {
1446 .owner = THIS_MODULE,
1447 .driver_name = "OMAP-SERIAL",
1448 .dev_name = OMAP_SERIAL_NAME,
1449 .nr = OMAP_MAX_HSUART_PORTS,
1450 .cons = OMAP_CONSOLE,
1451};
1452
Shubhrajyoti D3bc4f0d2012-01-16 15:52:36 +05301453#ifdef CONFIG_PM_SLEEP
Sourav Poddarddd85e22013-05-15 21:05:38 +05301454static int serial_omap_prepare(struct device *dev)
1455{
1456 struct uart_omap_port *up = dev_get_drvdata(dev);
1457
1458 up->is_suspending = true;
1459
1460 return 0;
1461}
1462
1463static void serial_omap_complete(struct device *dev)
1464{
1465 struct uart_omap_port *up = dev_get_drvdata(dev);
1466
1467 up->is_suspending = false;
1468}
1469
Govindraj.Rfcdca752011-02-28 18:12:23 +05301470static int serial_omap_suspend(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301471{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301472 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301473
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301474 uart_suspend_port(&serial_omap_reg, &up->port);
Linus Torvalds033d9952012-10-02 09:54:49 -07001475 flush_work(&up->qos_work);
Govindraj.R2fd14962011-11-09 17:41:21 +05301476
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001477 if (device_may_wakeup(dev))
1478 serial_omap_enable_wakeup(up, true);
1479 else
1480 serial_omap_enable_wakeup(up, false);
1481
Govindraj.Rb6126332010-09-27 20:20:49 +05301482 return 0;
1483}
1484
Govindraj.Rfcdca752011-02-28 18:12:23 +05301485static int serial_omap_resume(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301486{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301487 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301488
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001489 if (device_may_wakeup(dev))
1490 serial_omap_enable_wakeup(up, false);
1491
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301492 uart_resume_port(&serial_omap_reg, &up->port);
1493
Govindraj.Rb6126332010-09-27 20:20:49 +05301494 return 0;
1495}
Sourav Poddarddd85e22013-05-15 21:05:38 +05301496#else
1497#define serial_omap_prepare NULL
Arnd Bergmann2cb5a2f2013-06-01 11:18:13 +02001498#define serial_omap_complete NULL
Sourav Poddarddd85e22013-05-15 21:05:38 +05301499#endif /* CONFIG_PM_SLEEP */
Govindraj.Rb6126332010-09-27 20:20:49 +05301500
Bill Pemberton9671f092012-11-19 13:21:50 -05001501static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301502{
1503 u32 mvr, scheme;
1504 u16 revision, major, minor;
1505
Ruchika Kharwar76bac192013-07-08 10:28:57 +03001506 mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301507
1508 /* Check revision register scheme */
1509 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
1510
1511 switch (scheme) {
1512 case 0: /* Legacy Scheme: OMAP2/3 */
1513 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
1514 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
1515 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
1516 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
1517 break;
1518 case 1:
1519 /* New Scheme: OMAP4+ */
1520 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
1521 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
1522 OMAP_UART_MVR_MAJ_SHIFT;
1523 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
1524 break;
1525 default:
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001526 dev_warn(up->dev,
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301527 "Unknown %s revision, defaulting to highest\n",
1528 up->name);
1529 /* highest possible revision */
1530 major = 0xff;
1531 minor = 0xff;
1532 }
1533
1534 /* normalize revision for the driver */
1535 revision = UART_BUILD_REVISION(major, minor);
1536
1537 switch (revision) {
1538 case OMAP_UART_REV_46:
1539 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1540 UART_ERRATA_i291_DMA_FORCEIDLE);
1541 break;
1542 case OMAP_UART_REV_52:
1543 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1544 UART_ERRATA_i291_DMA_FORCEIDLE);
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001545 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301546 break;
1547 case OMAP_UART_REV_63:
1548 up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001549 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301550 break;
1551 default:
1552 break;
1553 }
1554}
1555
Bill Pemberton9671f092012-11-19 13:21:50 -05001556static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301557{
1558 struct omap_uart_port_info *omap_up_info;
1559
1560 omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
1561 if (!omap_up_info)
1562 return NULL; /* out of memory */
1563
1564 of_property_read_u32(dev->of_node, "clock-frequency",
1565 &omap_up_info->uartclk);
1566 return omap_up_info;
1567}
1568
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001569static int serial_omap_probe_rs485(struct uart_omap_port *up,
1570 struct device_node *np)
1571{
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001572 struct serial_rs485 *rs485conf = &up->port.rs485;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001573 u32 rs485_delay[2];
1574 enum of_gpio_flags flags;
1575 int ret;
1576
1577 rs485conf->flags = 0;
1578 up->rts_gpio = -EINVAL;
1579
1580 if (!np)
1581 return 0;
1582
1583 if (of_property_read_bool(np, "rs485-rts-active-high"))
1584 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
1585 else
1586 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1587
1588 /* check for tx enable gpio */
1589 up->rts_gpio = of_get_named_gpio_flags(np, "rts-gpio", 0, &flags);
1590 if (gpio_is_valid(up->rts_gpio)) {
Felipe Balbi404dc572014-04-23 09:58:30 -05001591 ret = devm_gpio_request(up->dev, up->rts_gpio, "omap-serial");
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001592 if (ret < 0)
1593 return ret;
1594 ret = gpio_direction_output(up->rts_gpio,
1595 flags & SER_RS485_RTS_AFTER_SEND);
1596 if (ret < 0)
1597 return ret;
Michael Grzeschika64c1a12014-02-13 10:52:03 +01001598 } else if (up->rts_gpio == -EPROBE_DEFER) {
1599 return -EPROBE_DEFER;
1600 } else {
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001601 up->rts_gpio = -EINVAL;
Michael Grzeschika64c1a12014-02-13 10:52:03 +01001602 }
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001603
1604 if (of_property_read_u32_array(np, "rs485-rts-delay",
1605 rs485_delay, 2) == 0) {
1606 rs485conf->delay_rts_before_send = rs485_delay[0];
1607 rs485conf->delay_rts_after_send = rs485_delay[1];
1608 }
1609
1610 if (of_property_read_bool(np, "rs485-rx-during-tx"))
1611 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1612
1613 if (of_property_read_bool(np, "linux,rs485-enabled-at-boot-time"))
1614 rs485conf->flags |= SER_RS485_ENABLED;
1615
1616 return 0;
1617}
1618
Bill Pemberton9671f092012-11-19 13:21:50 -05001619static int serial_omap_probe(struct platform_device *pdev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301620{
Jingoo Han574de552013-07-30 17:06:57 +09001621 struct omap_uart_port_info *omap_up_info = dev_get_platdata(&pdev->dev);
Felipe Balbicc516382014-04-23 09:58:31 -05001622 struct uart_omap_port *up;
1623 struct resource *mem;
Felipe Balbid044d232014-04-23 09:58:33 -05001624 void __iomem *base;
Felipe Balbicc516382014-04-23 09:58:31 -05001625 int uartirq = 0;
1626 int wakeirq = 0;
1627 int ret;
Govindraj.Rb6126332010-09-27 20:20:49 +05301628
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001629 /* The optional wakeirq may be specified in the board dts file */
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001630 if (pdev->dev.of_node) {
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001631 uartirq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1632 if (!uartirq)
1633 return -EPROBE_DEFER;
1634 wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301635 omap_up_info = of_get_uart_port_info(&pdev->dev);
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001636 pdev->dev.platform_data = omap_up_info;
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001637 } else {
Felipe Balbi54af6922014-04-23 09:58:32 -05001638 uartirq = platform_get_irq(pdev, 0);
1639 if (uartirq < 0)
1640 return -EPROBE_DEFER;
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001641 }
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301642
Felipe Balbid044d232014-04-23 09:58:33 -05001643 up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
1644 if (!up)
1645 return -ENOMEM;
Govindraj.Rb6126332010-09-27 20:20:49 +05301646
Felipe Balbid044d232014-04-23 09:58:33 -05001647 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1648 base = devm_ioremap_resource(&pdev->dev, mem);
1649 if (IS_ERR(base))
1650 return PTR_ERR(base);
Govindraj.Rb6126332010-09-27 20:20:49 +05301651
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001652 up->dev = &pdev->dev;
Govindraj.Rb6126332010-09-27 20:20:49 +05301653 up->port.dev = &pdev->dev;
1654 up->port.type = PORT_OMAP;
1655 up->port.iotype = UPIO_MEM;
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001656 up->port.irq = uartirq;
Govindraj.Rb6126332010-09-27 20:20:49 +05301657 up->port.regshift = 2;
1658 up->port.fifosize = 64;
1659 up->port.ops = &serial_omap_pops;
Govindraj.Rb6126332010-09-27 20:20:49 +05301660
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301661 if (pdev->dev.of_node)
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001662 ret = of_alias_get_id(pdev->dev.of_node, "serial");
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301663 else
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001664 ret = pdev->id;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301665
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001666 if (ret < 0) {
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301667 dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001668 ret);
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301669 goto err_port_line;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301670 }
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001671 up->port.line = ret;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301672
Nishanth Menon7af0ea52014-10-22 07:46:50 -05001673 if (up->port.line >= OMAP_MAX_HSUART_PORTS) {
1674 dev_err(&pdev->dev, "uart ID %d > MAX %d.\n", up->port.line,
1675 OMAP_MAX_HSUART_PORTS);
1676 ret = -ENXIO;
1677 goto err_port_line;
1678 }
1679
Doug Kehn1cf94d32015-03-24 08:19:27 -05001680 up->wakeirq = wakeirq;
1681 if (!up->wakeirq)
1682 dev_info(up->port.dev, "no wakeirq for uart%d\n",
1683 up->port.line);
1684
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001685 ret = serial_omap_probe_rs485(up, pdev->dev.of_node);
1686 if (ret < 0)
1687 goto err_rs485;
1688
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301689 sprintf(up->name, "OMAP UART%d", up->port.line);
Govindraj.Redd70ad2011-10-11 14:55:41 +05301690 up->port.mapbase = mem->start;
Felipe Balbid044d232014-04-23 09:58:33 -05001691 up->port.membase = base;
Govindraj.Rb6126332010-09-27 20:20:49 +05301692 up->port.flags = omap_up_info->flags;
Govindraj.Rb6126332010-09-27 20:20:49 +05301693 up->port.uartclk = omap_up_info->uartclk;
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001694 up->port.rs485_config = serial_omap_config_rs485;
Rajendra Nayak8fe789d2011-12-14 17:25:44 +05301695 if (!up->port.uartclk) {
1696 up->port.uartclk = DEFAULT_CLK_SPEED;
Philippe Proulxe5f9bf72013-10-23 18:49:59 -04001697 dev_warn(&pdev->dev,
Philippe Proulx80d86112013-10-31 09:39:58 -04001698 "No clock speed specified: using default: %d\n",
Philippe Proulxe5f9bf72013-10-23 18:49:59 -04001699 DEFAULT_CLK_SPEED);
Rajendra Nayak8fe789d2011-12-14 17:25:44 +05301700 }
Govindraj.Rb6126332010-09-27 20:20:49 +05301701
Govindraj.R2fd14962011-11-09 17:41:21 +05301702 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1703 up->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1704 pm_qos_add_request(&up->pm_qos_request,
1705 PM_QOS_CPU_DMA_LATENCY, up->latency);
Govindraj.R2fd14962011-11-09 17:41:21 +05301706 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
1707
Felipe Balbi93220dc2012-09-06 15:45:27 +03001708 platform_set_drvdata(pdev, up);
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001709 if (omap_up_info->autosuspend_timeout == 0)
1710 omap_up_info->autosuspend_timeout = -1;
Felipe Balbi5b6acc72014-04-23 09:58:29 -05001711
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001712 device_init_wakeup(up->dev, true);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301713 pm_runtime_use_autosuspend(&pdev->dev);
1714 pm_runtime_set_autosuspend_delay(&pdev->dev,
Deepak Kc86845db2011-11-09 17:33:38 +05301715 omap_up_info->autosuspend_timeout);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301716
1717 pm_runtime_irq_safe(&pdev->dev);
Grygorii Strashko3026d142013-07-22 15:31:15 +05301718 pm_runtime_enable(&pdev->dev);
1719
Govindraj.Rfcdca752011-02-28 18:12:23 +05301720 pm_runtime_get_sync(&pdev->dev);
1721
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301722 omap_serial_fill_features_erratas(up);
1723
Rajendra Nayakba774332011-12-14 17:25:43 +05301724 ui[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301725 serial_omap_add_console_port(up);
1726
1727 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1728 if (ret != 0)
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301729 goto err_add_port;
Govindraj.Rb6126332010-09-27 20:20:49 +05301730
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001731 pm_runtime_mark_last_busy(up->dev);
1732 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301733 return 0;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301734
1735err_add_port:
1736 pm_runtime_put(&pdev->dev);
1737 pm_runtime_disable(&pdev->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001738err_rs485:
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301739err_port_line:
Govindraj.Rb6126332010-09-27 20:20:49 +05301740 return ret;
1741}
1742
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001743static int serial_omap_remove(struct platform_device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301744{
1745 struct uart_omap_port *up = platform_get_drvdata(dev);
1746
Felipe Balbi7e9c8e72012-09-06 15:45:29 +03001747 pm_runtime_put_sync(up->dev);
Felipe Balbi1b42c8b2012-09-06 15:45:28 +03001748 pm_runtime_disable(up->dev);
1749 uart_remove_one_port(&serial_omap_reg, &up->port);
1750 pm_qos_remove_request(&up->pm_qos_request);
Sanjay Singh Rawat93a2e472014-03-21 13:55:10 +05301751 device_init_wakeup(&dev->dev, false);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301752
Govindraj.Rb6126332010-09-27 20:20:49 +05301753 return 0;
1754}
1755
Govindraj.R94734742011-11-07 19:00:33 +05301756/*
1757 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
1758 * The access to uart register after MDR1 Access
1759 * causes UART to corrupt data.
1760 *
1761 * Need a delay =
1762 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
1763 * give 10 times as much
1764 */
1765static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
1766{
1767 u8 timeout = 255;
1768
1769 serial_out(up, UART_OMAP_MDR1, mdr1);
1770 udelay(2);
1771 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
1772 UART_FCR_CLEAR_RCVR);
1773 /*
1774 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
1775 * TX_FIFO_E bit is 1.
1776 */
1777 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
1778 (UART_LSR_THRE | UART_LSR_DR))) {
1779 timeout--;
1780 if (!timeout) {
1781 /* Should *never* happen. we warn and carry on */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001782 dev_crit(up->dev, "Errata i202: timedout %x\n",
Govindraj.R94734742011-11-07 19:00:33 +05301783 serial_in(up, UART_LSR));
1784 break;
1785 }
1786 udelay(1);
1787 }
1788}
1789
Rafael J. Wysockid39fe4e2014-12-13 00:41:36 +01001790#ifdef CONFIG_PM
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301791static void serial_omap_restore_context(struct uart_omap_port *up)
1792{
Govindraj.R94734742011-11-07 19:00:33 +05301793 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1794 serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
1795 else
1796 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
1797
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301798 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1799 serial_out(up, UART_EFR, UART_EFR_ECB);
1800 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1801 serial_out(up, UART_IER, 0x0);
1802 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301803 serial_out(up, UART_DLL, up->dll);
1804 serial_out(up, UART_DLM, up->dlh);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301805 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1806 serial_out(up, UART_IER, up->ier);
1807 serial_out(up, UART_FCR, up->fcr);
1808 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1809 serial_out(up, UART_MCR, up->mcr);
1810 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301811 serial_out(up, UART_OMAP_SCR, up->scr);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301812 serial_out(up, UART_EFR, up->efr);
1813 serial_out(up, UART_LCR, up->lcr);
Govindraj.R94734742011-11-07 19:00:33 +05301814 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1815 serial_omap_mdr1_errataset(up, up->mdr1);
1816 else
1817 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001818 serial_out(up, UART_OMAP_WER, up->wer);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301819}
1820
Govindraj.Rfcdca752011-02-28 18:12:23 +05301821static int serial_omap_runtime_suspend(struct device *dev)
1822{
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301823 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301824
Wei Yongjun7f253012013-06-05 10:04:49 +08001825 if (!up)
1826 return -EINVAL;
1827
Sourav Poddarddd85e22013-05-15 21:05:38 +05301828 /*
1829 * When using 'no_console_suspend', the console UART must not be
1830 * suspended. Since driver suspend is managed by runtime suspend,
1831 * preventing runtime suspend (by returning error) will keep device
1832 * active during suspend.
1833 */
1834 if (up->is_suspending && !console_suspend_enabled &&
1835 uart_console(&up->port))
1836 return -EBUSY;
1837
Felipe Balbie5b57c02012-08-23 13:32:42 +03001838 up->context_loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301839
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001840 serial_omap_enable_wakeup(up, true);
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301841
Govindraj.R2fd14962011-11-09 17:41:21 +05301842 up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1843 schedule_work(&up->qos_work);
1844
Govindraj.Rfcdca752011-02-28 18:12:23 +05301845 return 0;
1846}
1847
1848static int serial_omap_runtime_resume(struct device *dev)
1849{
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301850 struct uart_omap_port *up = dev_get_drvdata(dev);
1851
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301852 int loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301853
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001854 serial_omap_enable_wakeup(up, false);
1855
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301856 if (loss_cnt < 0) {
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001857 dev_dbg(dev, "serial_omap_get_context_loss_count failed : %d\n",
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301858 loss_cnt);
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301859 serial_omap_restore_context(up);
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301860 } else if (up->context_loss_cnt != loss_cnt) {
1861 serial_omap_restore_context(up);
1862 }
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301863 up->latency = up->calc_latency;
1864 schedule_work(&up->qos_work);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301865
Govindraj.Rfcdca752011-02-28 18:12:23 +05301866 return 0;
1867}
1868#endif
1869
1870static const struct dev_pm_ops serial_omap_dev_pm_ops = {
1871 SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
1872 SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
1873 serial_omap_runtime_resume, NULL)
Sourav Poddarddd85e22013-05-15 21:05:38 +05301874 .prepare = serial_omap_prepare,
1875 .complete = serial_omap_complete,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301876};
1877
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301878#if defined(CONFIG_OF)
1879static const struct of_device_id omap_serial_of_match[] = {
1880 { .compatible = "ti,omap2-uart" },
1881 { .compatible = "ti,omap3-uart" },
1882 { .compatible = "ti,omap4-uart" },
1883 {},
1884};
1885MODULE_DEVICE_TABLE(of, omap_serial_of_match);
1886#endif
1887
Govindraj.Rb6126332010-09-27 20:20:49 +05301888static struct platform_driver serial_omap_driver = {
1889 .probe = serial_omap_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001890 .remove = serial_omap_remove,
Govindraj.Rb6126332010-09-27 20:20:49 +05301891 .driver = {
1892 .name = DRIVER_NAME,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301893 .pm = &serial_omap_dev_pm_ops,
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301894 .of_match_table = of_match_ptr(omap_serial_of_match),
Govindraj.Rb6126332010-09-27 20:20:49 +05301895 },
1896};
1897
1898static int __init serial_omap_init(void)
1899{
1900 int ret;
1901
1902 ret = uart_register_driver(&serial_omap_reg);
1903 if (ret != 0)
1904 return ret;
1905 ret = platform_driver_register(&serial_omap_driver);
1906 if (ret != 0)
1907 uart_unregister_driver(&serial_omap_reg);
1908 return ret;
1909}
1910
1911static void __exit serial_omap_exit(void)
1912{
1913 platform_driver_unregister(&serial_omap_driver);
1914 uart_unregister_driver(&serial_omap_reg);
1915}
1916
1917module_init(serial_omap_init);
1918module_exit(serial_omap_exit);
1919
1920MODULE_DESCRIPTION("OMAP High Speed UART driver");
1921MODULE_LICENSE("GPL");
1922MODULE_AUTHOR("Texas Instruments Inc");