blob: 8573fc9cb0cdf58abe4e3dc59e6c8a72845d375d [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Govindraj.Rb6126332010-09-27 20:20:49 +05302/*
3 * Driver for OMAP-UART controller.
4 * Based on drivers/serial/8250.c
5 *
6 * Copyright (C) 2010 Texas Instruments.
7 *
8 * Authors:
9 * Govindraj R <govindraj.raja@ti.com>
10 * Thara Gopinath <thara@ti.com>
11 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012 * Note: This driver is made separate from 8250 driver as we cannot
Govindraj.Rb6126332010-09-27 20:20:49 +053013 * over load 8250 driver with omap platform specific configuration for
14 * features like DMA, it makes easier to implement features like DMA and
15 * hardware flow control and software flow control configuration with
16 * this driver as required for the omap-platform.
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/console.h>
22#include <linux/serial_reg.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/tty.h>
26#include <linux/tty_flip.h>
Felipe Balbid21e4002012-09-06 15:45:38 +030027#include <linux/platform_device.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053028#include <linux/io.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053029#include <linux/clk.h>
30#include <linux/serial_core.h>
31#include <linux/irq.h>
Govindraj.Rfcdca752011-02-28 18:12:23 +053032#include <linux/pm_runtime.h>
Tony Lindgrenee83bd32015-06-09 23:35:00 -070033#include <linux/pm_wakeirq.h>
Rajendra Nayakd92b0df2011-12-14 17:25:45 +053034#include <linux/of.h>
Tony Lindgren2a0b9652013-10-22 06:49:48 -070035#include <linux/of_irq.h>
Linus Walleij5745fd02020-04-15 20:39:27 +020036#include <linux/gpio/consumer.h>
Tony Lindgrend9ba5732012-12-14 09:09:11 -080037#include <linux/platform_data/serial-omap.h>
Govindraj.Rb6126332010-09-27 20:20:49 +053038
Nishanth Menon7af0ea52014-10-22 07:46:50 -050039#define OMAP_MAX_HSUART_PORTS 10
Russell Kingf91b55a2012-10-06 10:50:58 +010040
Govindraj.R7c77c8d2012-04-03 19:12:34 +053041#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
42
43#define OMAP_UART_REV_42 0x0402
44#define OMAP_UART_REV_46 0x0406
45#define OMAP_UART_REV_52 0x0502
46#define OMAP_UART_REV_63 0x0603
47
Govindraj.Rf64ffda2013-07-05 18:25:59 +030048#define OMAP_UART_TX_WAKEUP_EN BIT(7)
49
50/* Feature flags */
51#define OMAP_UART_WER_HAS_TX_WAKEUP BIT(0)
52
Russell Kingf91b55a2012-10-06 10:50:58 +010053#define UART_ERRATA_i202_MDR1_ACCESS BIT(0)
54#define UART_ERRATA_i291_DMA_FORCEIDLE BIT(1)
55
Pavel Machekfbf7ebe2014-12-11 22:44:26 +010056#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz */
Rajendra Nayak8fe789d2011-12-14 17:25:44 +053057
Paul Walmsley0ba5f662012-01-25 19:50:36 -070058/* SCR register bitmasks */
59#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
Alexey Pelykh1776fd02013-02-04 12:19:46 -050060#define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
Russell Kingf91b55a2012-10-06 10:50:58 +010061#define OMAP_UART_SCR_TX_EMPTY (1 << 3)
Paul Walmsley0ba5f662012-01-25 19:50:36 -070062
63/* FCR register bitmasks */
Paul Walmsley0ba5f662012-01-25 19:50:36 -070064#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
Felipe Balbi6721ab72012-09-06 15:45:40 +030065#define OMAP_UART_FCR_TX_FIFO_TRIG_MASK (0x3 << 4)
Paul Walmsley0ba5f662012-01-25 19:50:36 -070066
Govindraj.R7c77c8d2012-04-03 19:12:34 +053067/* MVR register bitmasks */
68#define OMAP_UART_MVR_SCHEME_SHIFT 30
69
70#define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
71#define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
72#define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
73
74#define OMAP_UART_MVR_MAJ_MASK 0x700
75#define OMAP_UART_MVR_MAJ_SHIFT 8
76#define OMAP_UART_MVR_MIN_MASK 0x3f
77
Russell Kingf91b55a2012-10-06 10:50:58 +010078#define OMAP_UART_DMA_CH_FREE -1
79
80#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
81#define OMAP_MODE13X_SPEED 230400
82
83/* WER = 0x7F
84 * Enable module level wakeup in WER reg
85 */
Pavel Machekfbf7ebe2014-12-11 22:44:26 +010086#define OMAP_UART_WER_MOD_WKUP 0x7F
Russell Kingf91b55a2012-10-06 10:50:58 +010087
88/* Enable XON/XOFF flow control on output */
Russell King3af08bd2012-10-05 13:32:08 +010089#define OMAP_UART_SW_TX 0x08
Russell Kingf91b55a2012-10-06 10:50:58 +010090
91/* Enable XON/XOFF flow control on input */
Russell King3af08bd2012-10-05 13:32:08 +010092#define OMAP_UART_SW_RX 0x02
Russell Kingf91b55a2012-10-06 10:50:58 +010093
94#define OMAP_UART_SW_CLR 0xF0
95
96#define OMAP_UART_TCR_TRIG 0x0F
97
98struct uart_omap_dma {
99 u8 uart_dma_tx;
100 u8 uart_dma_rx;
101 int rx_dma_channel;
102 int tx_dma_channel;
103 dma_addr_t rx_buf_dma_phys;
104 dma_addr_t tx_buf_dma_phys;
105 unsigned int uart_base;
106 /*
Pavel Machekfbf7ebe2014-12-11 22:44:26 +0100107 * Buffer for rx dma. It is not required for tx because the buffer
Russell Kingf91b55a2012-10-06 10:50:58 +0100108 * comes from port structure.
109 */
110 unsigned char *rx_buf;
111 unsigned int prev_rx_dma_pos;
112 int tx_buf_size;
113 int tx_dma_used;
114 int rx_dma_used;
115 spinlock_t tx_lock;
116 spinlock_t rx_lock;
117 /* timer to poll activity on rx dma */
118 struct timer_list rx_timer;
119 unsigned int rx_buf_size;
120 unsigned int rx_poll_rate;
121 unsigned int rx_timeout;
122};
123
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300124struct uart_omap_port {
125 struct uart_port port;
126 struct uart_omap_dma uart_dma;
127 struct device *dev;
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700128 int wakeirq;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300129
130 unsigned char ier;
131 unsigned char lcr;
132 unsigned char mcr;
133 unsigned char fcr;
134 unsigned char efr;
135 unsigned char dll;
136 unsigned char dlh;
137 unsigned char mdr1;
138 unsigned char scr;
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300139 unsigned char wer;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300140
141 int use_dma;
142 /*
143 * Some bits in registers are cleared on a read, so they must
Pavel Machekfbf7ebe2014-12-11 22:44:26 +0100144 * be saved whenever the register is read, but the bits will not
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300145 * be immediately processed.
146 */
147 unsigned int lsr_break_flag;
148 unsigned char msr_saved_flags;
149 char name[20];
150 unsigned long port_activity;
Shubhrajyoti D39aee512012-10-03 17:24:36 +0530151 int context_loss_cnt;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300152 u32 errata;
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300153 u32 features;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300154
Linus Walleij5745fd02020-04-15 20:39:27 +0200155 struct gpio_desc *rts_gpiod;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100156
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300157 struct pm_qos_request pm_qos_request;
158 u32 latency;
159 u32 calc_latency;
160 struct work_struct qos_work;
Sourav Poddarddd85e22013-05-15 21:05:38 +0530161 bool is_suspending;
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300162};
163
Philippe Proulxe5f9bf72013-10-23 18:49:59 -0400164#define to_uart_omap_port(p) ((container_of((p), struct uart_omap_port, port)))
Felipe Balbid37c6ce2012-09-06 15:45:39 +0300165
Govindraj.Rb6126332010-09-27 20:20:49 +0530166static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
167
168/* Forward declaration of functions */
Govindraj.R94734742011-11-07 19:00:33 +0530169static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +0530170
171static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
172{
173 offset <<= up->port.regshift;
174 return readw(up->port.membase + offset);
175}
176
177static inline void serial_out(struct uart_omap_port *up, int offset, int value)
178{
179 offset <<= up->port.regshift;
180 writew(value, up->port.membase + offset);
181}
182
183static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
184{
185 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
186 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
187 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
188 serial_out(up, UART_FCR, 0);
189}
190
Ezequiel Garciaadfb9232015-10-03 16:45:35 -0300191#ifdef CONFIG_PM
Felipe Balbie5b57c02012-08-23 13:32:42 +0300192static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
193{
Jingoo Han574de552013-07-30 17:06:57 +0900194 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300195
Felipe Balbice2f08d2012-09-07 21:10:33 +0300196 if (!pdata || !pdata->get_context_loss_count)
Tony Lindgrena630fbf2013-06-10 07:39:09 -0700197 return -EINVAL;
Felipe Balbie5b57c02012-08-23 13:32:42 +0300198
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300199 return pdata->get_context_loss_count(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300200}
201
Tony Lindgrenee83bd32015-06-09 23:35:00 -0700202/* REVISIT: Remove this when omap3 boots in device tree only mode */
Felipe Balbie5b57c02012-08-23 13:32:42 +0300203static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
204{
Jingoo Han574de552013-07-30 17:06:57 +0900205 struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300206
Felipe Balbice2f08d2012-09-07 21:10:33 +0300207 if (!pdata || !pdata->enable_wakeup)
208 return;
209
210 pdata->enable_wakeup(up->dev, enable);
Felipe Balbie5b57c02012-08-23 13:32:42 +0300211}
Ezequiel Garciaadfb9232015-10-03 16:45:35 -0300212#endif /* CONFIG_PM */
Felipe Balbie5b57c02012-08-23 13:32:42 +0300213
Govindraj.Rb6126332010-09-27 20:20:49 +0530214/*
Frans Klaver13d6ceb2014-09-24 09:55:22 +0200215 * Calculate the absolute difference between the desired and actual baud
216 * rate for the given mode.
217 */
218static inline int calculate_baud_abs_diff(struct uart_port *port,
219 unsigned int baud, unsigned int mode)
220{
221 unsigned int n = port->uartclk / (mode * baud);
222 int abs_diff;
223
224 if (n == 0)
225 n = 1;
226
227 abs_diff = baud - (port->uartclk / (mode * n));
228 if (abs_diff < 0)
229 abs_diff = -abs_diff;
230
231 return abs_diff;
232}
233
234/*
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500235 * serial_omap_baud_is_mode16 - check if baud rate is MODE16X
236 * @port: uart port info
237 * @baud: baudrate for which mode needs to be determined
238 *
239 * Returns true if baud rate is MODE16X and false if MODE13X
240 * Original table in OMAP TRM named "UART Mode Baud Rates, Divisor Values,
241 * and Error Rates" determines modes not for all common baud rates.
242 * E.g. for 1000000 baud rate mode must be 16x, but according to that
243 * table it's determined as 13x.
244 */
245static bool
246serial_omap_baud_is_mode16(struct uart_port *port, unsigned int baud)
247{
Frans Klaver13d6ceb2014-09-24 09:55:22 +0200248 int abs_diff_13 = calculate_baud_abs_diff(port, baud, 13);
249 int abs_diff_16 = calculate_baud_abs_diff(port, baud, 16);
Frans Klaverdc318752014-09-25 11:19:51 +0200250
Frans Klaver13d6ceb2014-09-24 09:55:22 +0200251 return (abs_diff_13 >= abs_diff_16);
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500252}
253
254/*
Govindraj.Rb6126332010-09-27 20:20:49 +0530255 * serial_omap_get_divisor - calculate divisor value
256 * @port: uart port info
257 * @baud: baudrate for which divisor needs to be calculated.
Govindraj.Rb6126332010-09-27 20:20:49 +0530258 */
259static unsigned int
260serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
261{
Alexey Pelykh4250b5d2013-09-21 04:04:35 -0400262 unsigned int mode;
Govindraj.Rb6126332010-09-27 20:20:49 +0530263
Alexey Pelykh5fe21232013-01-16 05:08:06 -0500264 if (!serial_omap_baud_is_mode16(port, baud))
Alexey Pelykh4250b5d2013-09-21 04:04:35 -0400265 mode = 13;
Govindraj.Rb6126332010-09-27 20:20:49 +0530266 else
Alexey Pelykh4250b5d2013-09-21 04:04:35 -0400267 mode = 16;
268 return port->uartclk/(mode * baud);
Govindraj.Rb6126332010-09-27 20:20:49 +0530269}
270
Govindraj.Rb6126332010-09-27 20:20:49 +0530271static void serial_omap_enable_ms(struct uart_port *port)
272{
Felipe Balbic990f352012-08-23 13:32:41 +0300273 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530274
Rajendra Nayakba774332011-12-14 17:25:43 +0530275 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530276
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300277 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530278 up->ier |= UART_IER_MSI;
279 serial_out(up, UART_IER, up->ier);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300280 pm_runtime_mark_last_busy(up->dev);
281 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530282}
283
284static void serial_omap_stop_tx(struct uart_port *port)
285{
Felipe Balbic990f352012-08-23 13:32:41 +0300286 struct uart_omap_port *up = to_uart_omap_port(port);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100287 int res;
Govindraj.Rb6126332010-09-27 20:20:49 +0530288
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300289 pm_runtime_get_sync(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100290
Philippe Proulx018e7442013-10-23 18:49:58 -0400291 /* Handle RS-485 */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100292 if (port->rs485.flags & SER_RS485_ENABLED) {
Philippe Proulx018e7442013-10-23 18:49:58 -0400293 if (up->scr & OMAP_UART_SCR_TX_EMPTY) {
294 /* THR interrupt is fired when both TX FIFO and TX
295 * shift register are empty. This means there's nothing
296 * left to transmit now, so make sure the THR interrupt
297 * is fired when TX FIFO is below the trigger level,
298 * disable THR interrupts and toggle the RS-485 GPIO
299 * data direction pin if needed.
300 */
301 up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
302 serial_out(up, UART_OMAP_SCR, up->scr);
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100303 res = (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) ?
304 1 : 0;
Linus Walleij5745fd02020-04-15 20:39:27 +0200305 if (gpiod_get_value(up->rts_gpiod) != res) {
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100306 if (port->rs485.delay_rts_after_send > 0)
307 mdelay(
308 port->rs485.delay_rts_after_send);
Linus Walleij5745fd02020-04-15 20:39:27 +0200309 gpiod_set_value(up->rts_gpiod, res);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100310 }
Philippe Proulx018e7442013-10-23 18:49:58 -0400311 } else {
312 /* We're asked to stop, but there's still stuff in the
313 * UART FIFO, so make sure the THR interrupt is fired
314 * when both TX FIFO and TX shift register are empty.
315 * The next THR interrupt (if no transmission is started
316 * in the meantime) will indicate the end of a
317 * transmission. Therefore we _don't_ disable THR
318 * interrupts in this situation.
319 */
320 up->scr |= OMAP_UART_SCR_TX_EMPTY;
321 serial_out(up, UART_OMAP_SCR, up->scr);
322 return;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100323 }
324 }
325
Govindraj.Rb6126332010-09-27 20:20:49 +0530326 if (up->ier & UART_IER_THRI) {
327 up->ier &= ~UART_IER_THRI;
328 serial_out(up, UART_IER, up->ier);
329 }
Govindraj.Rfcdca752011-02-28 18:12:23 +0530330
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100331 if ((port->rs485.flags & SER_RS485_ENABLED) &&
332 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Dimitris Lampridis3a138842014-03-13 15:11:47 +0200333 /*
334 * Empty the RX FIFO, we are not interested in anything
335 * received during the half-duplex transmission.
336 */
337 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_RCVR);
338 /* Re-enable RX interrupts */
Dimitris Lampridiscab53dc2014-03-13 15:11:46 +0200339 up->ier |= UART_IER_RLSI | UART_IER_RDI;
340 up->port.read_status_mask |= UART_LSR_DR;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100341 serial_out(up, UART_IER, up->ier);
342 }
343
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300344 pm_runtime_mark_last_busy(up->dev);
345 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530346}
347
348static void serial_omap_stop_rx(struct uart_port *port)
349{
Felipe Balbic990f352012-08-23 13:32:41 +0300350 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530351
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300352 pm_runtime_get_sync(up->dev);
Dimitris Lampridiscab53dc2014-03-13 15:11:46 +0200353 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
Govindraj.Rb6126332010-09-27 20:20:49 +0530354 up->port.read_status_mask &= ~UART_LSR_DR;
355 serial_out(up, UART_IER, up->ier);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300356 pm_runtime_mark_last_busy(up->dev);
357 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530358}
359
Felipe Balbibf63a082012-09-06 15:45:25 +0300360static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
Govindraj.Rb6126332010-09-27 20:20:49 +0530361{
362 struct circ_buf *xmit = &up->port.state->xmit;
363 int count;
364
365 if (up->port.x_char) {
366 serial_out(up, UART_TX, up->port.x_char);
367 up->port.icount.tx++;
368 up->port.x_char = 0;
369 return;
370 }
371 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
372 serial_omap_stop_tx(&up->port);
373 return;
374 }
Greg Kroah-Hartman355fe562013-08-27 16:02:18 -0700375 count = up->port.fifosize / 4;
Govindraj.Rb6126332010-09-27 20:20:49 +0530376 do {
377 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
378 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
379 up->port.icount.tx++;
380 if (uart_circ_empty(xmit))
381 break;
382 } while (--count > 0);
383
Felipe Balbi6bf78962014-04-23 09:58:27 -0500384 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Govindraj.Rb6126332010-09-27 20:20:49 +0530385 uart_write_wakeup(&up->port);
386
387 if (uart_circ_empty(xmit))
388 serial_omap_stop_tx(&up->port);
389}
390
391static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
392{
393 if (!(up->ier & UART_IER_THRI)) {
394 up->ier |= UART_IER_THRI;
395 serial_out(up, UART_IER, up->ier);
396 }
397}
398
399static void serial_omap_start_tx(struct uart_port *port)
400{
Felipe Balbic990f352012-08-23 13:32:41 +0300401 struct uart_omap_port *up = to_uart_omap_port(port);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100402 int res;
Govindraj.Rb6126332010-09-27 20:20:49 +0530403
Felipe Balbi49457432012-09-06 15:45:21 +0300404 pm_runtime_get_sync(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100405
Philippe Proulx018e7442013-10-23 18:49:58 -0400406 /* Handle RS-485 */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100407 if (port->rs485.flags & SER_RS485_ENABLED) {
Philippe Proulx018e7442013-10-23 18:49:58 -0400408 /* Fire THR interrupts when FIFO is below trigger level */
409 up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
410 serial_out(up, UART_OMAP_SCR, up->scr);
411
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100412 /* if rts not already enabled */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100413 res = (port->rs485.flags & SER_RS485_RTS_ON_SEND) ? 1 : 0;
Linus Walleij5745fd02020-04-15 20:39:27 +0200414 if (gpiod_get_value(up->rts_gpiod) != res) {
415 gpiod_set_value(up->rts_gpiod, res);
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100416 if (port->rs485.delay_rts_before_send > 0)
417 mdelay(port->rs485.delay_rts_before_send);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100418 }
419 }
420
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +0100421 if ((port->rs485.flags & SER_RS485_ENABLED) &&
422 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Mark Jackson4a0ac0f2013-08-14 11:29:38 +0100423 serial_omap_stop_rx(port);
424
Felipe Balbi49457432012-09-06 15:45:21 +0300425 serial_omap_enable_ier_thri(up);
Felipe Balbi49457432012-09-06 15:45:21 +0300426 pm_runtime_mark_last_busy(up->dev);
427 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530428}
429
Russell King3af08bd2012-10-05 13:32:08 +0100430static void serial_omap_throttle(struct uart_port *port)
431{
432 struct uart_omap_port *up = to_uart_omap_port(port);
433 unsigned long flags;
434
435 pm_runtime_get_sync(up->dev);
436 spin_lock_irqsave(&up->port.lock, flags);
437 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
438 serial_out(up, UART_IER, up->ier);
439 spin_unlock_irqrestore(&up->port.lock, flags);
440 pm_runtime_mark_last_busy(up->dev);
441 pm_runtime_put_autosuspend(up->dev);
442}
443
444static void serial_omap_unthrottle(struct uart_port *port)
445{
446 struct uart_omap_port *up = to_uart_omap_port(port);
447 unsigned long flags;
448
449 pm_runtime_get_sync(up->dev);
450 spin_lock_irqsave(&up->port.lock, flags);
451 up->ier |= UART_IER_RLSI | UART_IER_RDI;
452 serial_out(up, UART_IER, up->ier);
453 spin_unlock_irqrestore(&up->port.lock, flags);
454 pm_runtime_mark_last_busy(up->dev);
455 pm_runtime_put_autosuspend(up->dev);
456}
457
Govindraj.Rb6126332010-09-27 20:20:49 +0530458static unsigned int check_modem_status(struct uart_omap_port *up)
459{
460 unsigned int status;
461
462 status = serial_in(up, UART_MSR);
463 status |= up->msr_saved_flags;
464 up->msr_saved_flags = 0;
465 if ((status & UART_MSR_ANY_DELTA) == 0)
466 return status;
467
468 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
469 up->port.state != NULL) {
470 if (status & UART_MSR_TERI)
471 up->port.icount.rng++;
472 if (status & UART_MSR_DDSR)
473 up->port.icount.dsr++;
474 if (status & UART_MSR_DDCD)
475 uart_handle_dcd_change
476 (&up->port, status & UART_MSR_DCD);
477 if (status & UART_MSR_DCTS)
478 uart_handle_cts_change
479 (&up->port, status & UART_MSR_CTS);
480 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
481 }
482
483 return status;
484}
485
Felipe Balbi72256cb2012-09-06 15:45:24 +0300486static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr)
487{
488 unsigned int flag;
Shubhrajyoti D9a12fcf2012-09-21 20:07:19 +0530489
Xiongfeng Wange83c6582019-12-06 15:37:43 +0800490 /*
491 * Read one data character out to avoid stalling the receiver according
492 * to the table 23-246 of the omap4 TRM.
493 */
Shubhrajyoti D9a12fcf2012-09-21 20:07:19 +0530494 if (likely(lsr & UART_LSR_DR))
Xiongfeng Wange83c6582019-12-06 15:37:43 +0800495 serial_in(up, UART_RX);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300496
497 up->port.icount.rx++;
498 flag = TTY_NORMAL;
499
500 if (lsr & UART_LSR_BI) {
501 flag = TTY_BREAK;
502 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
503 up->port.icount.brk++;
504 /*
505 * We do the SysRQ and SAK checking
506 * here because otherwise the break
507 * may get masked by ignore_status_mask
508 * or read_status_mask.
509 */
510 if (uart_handle_break(&up->port))
511 return;
512
513 }
514
515 if (lsr & UART_LSR_PE) {
516 flag = TTY_PARITY;
517 up->port.icount.parity++;
518 }
519
520 if (lsr & UART_LSR_FE) {
521 flag = TTY_FRAME;
522 up->port.icount.frame++;
523 }
524
525 if (lsr & UART_LSR_OE)
526 up->port.icount.overrun++;
527
528#ifdef CONFIG_SERIAL_OMAP_CONSOLE
529 if (up->port.line == up->port.cons->index) {
530 /* Recover the break flag from console xmit */
531 lsr |= up->lsr_break_flag;
532 }
533#endif
534 uart_insert_char(&up->port, lsr, UART_LSR_OE, 0, flag);
535}
536
537static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr)
538{
539 unsigned char ch = 0;
540 unsigned int flag;
541
542 if (!(lsr & UART_LSR_DR))
543 return;
544
545 ch = serial_in(up, UART_RX);
546 flag = TTY_NORMAL;
547 up->port.icount.rx++;
548
549 if (uart_handle_sysrq_char(&up->port, ch))
550 return;
551
552 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
553}
554
Govindraj.Rb6126332010-09-27 20:20:49 +0530555/**
556 * serial_omap_irq() - This handles the interrupt from one port
557 * @irq: uart port irq number
558 * @dev_id: uart port info
559 */
Felipe Balbi52c55132012-09-06 15:45:33 +0300560static irqreturn_t serial_omap_irq(int irq, void *dev_id)
Govindraj.Rb6126332010-09-27 20:20:49 +0530561{
562 struct uart_omap_port *up = dev_id;
563 unsigned int iir, lsr;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300564 unsigned int type;
Greg Kroah-Hartman7b013e42013-08-27 15:59:53 -0700565 irqreturn_t ret = IRQ_NONE;
Felipe Balbi72256cb2012-09-06 15:45:24 +0300566 int max_count = 256;
Govindraj.Rb6126332010-09-27 20:20:49 +0530567
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300568 spin_lock(&up->port.lock);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300569 pm_runtime_get_sync(up->dev);
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300570
Felipe Balbi72256cb2012-09-06 15:45:24 +0300571 do {
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300572 iir = serial_in(up, UART_IIR);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300573 if (iir & UART_IIR_NO_INT)
574 break;
Govindraj.Rb6126332010-09-27 20:20:49 +0530575
Greg Kroah-Hartman7b013e42013-08-27 15:59:53 -0700576 ret = IRQ_HANDLED;
Felipe Balbi72256cb2012-09-06 15:45:24 +0300577 lsr = serial_in(up, UART_LSR);
578
579 /* extract IRQ type from IIR register */
580 type = iir & 0x3e;
581
582 switch (type) {
583 case UART_IIR_MSI:
584 check_modem_status(up);
585 break;
586 case UART_IIR_THRI:
Felipe Balbibf63a082012-09-06 15:45:25 +0300587 transmit_chars(up, lsr);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300588 break;
589 case UART_IIR_RX_TIMEOUT:
590 /* FALLTHROUGH */
591 case UART_IIR_RDI:
592 serial_omap_rdi(up, lsr);
593 break;
594 case UART_IIR_RLSI:
595 serial_omap_rlsi(up, lsr);
596 break;
597 case UART_IIR_CTS_RTS_DSR:
598 /* simply try again */
599 break;
600 case UART_IIR_XOFF:
601 /* FALLTHROUGH */
602 default:
603 break;
604 }
Martin Townsende60f9fd2017-10-20 22:17:52 +0100605 } while (max_count--);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300606
Felipe Balbi6c3a30c2012-09-06 15:45:30 +0300607 spin_unlock(&up->port.lock);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300608
Jiri Slaby2e124b42013-01-03 15:53:06 +0100609 tty_flip_buffer_push(&up->port.state->port);
Felipe Balbi72256cb2012-09-06 15:45:24 +0300610
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300611 pm_runtime_mark_last_busy(up->dev);
612 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530613 up->port_activity = jiffies;
Felipe Balbi81b75ae2012-09-06 15:45:23 +0300614
Greg Kroah-Hartman7b013e42013-08-27 15:59:53 -0700615 return ret;
Govindraj.Rb6126332010-09-27 20:20:49 +0530616}
617
618static unsigned int serial_omap_tx_empty(struct uart_port *port)
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 unsigned int ret = 0;
623
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300624 pm_runtime_get_sync(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +0530625 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530626 spin_lock_irqsave(&up->port.lock, flags);
627 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
628 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300629 pm_runtime_mark_last_busy(up->dev);
630 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530631 return ret;
632}
633
634static unsigned int serial_omap_get_mctrl(struct uart_port *port)
635{
Felipe Balbic990f352012-08-23 13:32:41 +0300636 struct uart_omap_port *up = to_uart_omap_port(port);
Shubhrajyoti D514f31d2011-11-21 15:43:28 +0530637 unsigned int status;
Govindraj.Rb6126332010-09-27 20:20:49 +0530638 unsigned int ret = 0;
639
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300640 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530641 status = check_modem_status(up);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300642 pm_runtime_mark_last_busy(up->dev);
643 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530644
Rajendra Nayakba774332011-12-14 17:25:43 +0530645 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530646
647 if (status & UART_MSR_DCD)
648 ret |= TIOCM_CAR;
649 if (status & UART_MSR_RI)
650 ret |= TIOCM_RNG;
651 if (status & UART_MSR_DSR)
652 ret |= TIOCM_DSR;
653 if (status & UART_MSR_CTS)
654 ret |= TIOCM_CTS;
655 return ret;
656}
657
658static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
659{
Felipe Balbic990f352012-08-23 13:32:41 +0300660 struct uart_omap_port *up = to_uart_omap_port(port);
Peter Hurley348f9bb2015-01-25 14:44:53 -0500661 unsigned char mcr = 0, old_mcr, lcr;
Govindraj.Rb6126332010-09-27 20:20:49 +0530662
Rajendra Nayakba774332011-12-14 17:25:43 +0530663 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530664 if (mctrl & TIOCM_RTS)
665 mcr |= UART_MCR_RTS;
666 if (mctrl & TIOCM_DTR)
667 mcr |= UART_MCR_DTR;
668 if (mctrl & TIOCM_OUT1)
669 mcr |= UART_MCR_OUT1;
670 if (mctrl & TIOCM_OUT2)
671 mcr |= UART_MCR_OUT2;
672 if (mctrl & TIOCM_LOOP)
673 mcr |= UART_MCR_LOOP;
674
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300675 pm_runtime_get_sync(up->dev);
Russell King9363f8f2012-10-05 12:23:28 +0100676 old_mcr = serial_in(up, UART_MCR);
677 old_mcr &= ~(UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_OUT1 |
678 UART_MCR_DTR | UART_MCR_RTS);
679 up->mcr = old_mcr | mcr;
Govindraj.Rc538d202011-11-07 18:57:03 +0530680 serial_out(up, UART_MCR, up->mcr);
Peter Hurley348f9bb2015-01-25 14:44:53 -0500681
682 /* Turn off autoRTS if RTS is lowered; restore autoRTS if RTS raised */
683 lcr = serial_in(up, UART_LCR);
684 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
685 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
686 up->efr |= UART_EFR_RTS;
687 else
Lukas Wunner2a71de22017-10-21 10:50:18 +0200688 up->efr &= ~UART_EFR_RTS;
Peter Hurley348f9bb2015-01-25 14:44:53 -0500689 serial_out(up, UART_EFR, up->efr);
690 serial_out(up, UART_LCR, lcr);
691
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300692 pm_runtime_mark_last_busy(up->dev);
693 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530694}
695
696static void serial_omap_break_ctl(struct uart_port *port, int break_state)
697{
Felipe Balbic990f352012-08-23 13:32:41 +0300698 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530699 unsigned long flags = 0;
700
Rajendra Nayakba774332011-12-14 17:25:43 +0530701 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300702 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530703 spin_lock_irqsave(&up->port.lock, flags);
704 if (break_state == -1)
705 up->lcr |= UART_LCR_SBC;
706 else
707 up->lcr &= ~UART_LCR_SBC;
708 serial_out(up, UART_LCR, up->lcr);
709 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300710 pm_runtime_mark_last_busy(up->dev);
711 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530712}
713
714static int serial_omap_startup(struct uart_port *port)
715{
Felipe Balbic990f352012-08-23 13:32:41 +0300716 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530717 unsigned long flags = 0;
718 int retval;
719
720 /*
721 * Allocate the IRQ
722 */
723 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
724 up->name, up);
725 if (retval)
726 return retval;
727
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700728 /* Optional wake-up IRQ */
729 if (up->wakeirq) {
Tony Lindgrenee83bd32015-06-09 23:35:00 -0700730 retval = dev_pm_set_dedicated_wake_irq(up->dev, up->wakeirq);
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700731 if (retval) {
732 free_irq(up->port.irq, up);
733 return retval;
734 }
Tony Lindgren2a0b9652013-10-22 06:49:48 -0700735 }
736
Rajendra Nayakba774332011-12-14 17:25:43 +0530737 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +0530738
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300739 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530740 /*
741 * Clear the FIFO buffers and disable them.
742 * (they will be reenabled in set_termios())
743 */
744 serial_omap_clear_fifos(up);
Govindraj.Rb6126332010-09-27 20:20:49 +0530745
746 /*
747 * Clear the interrupt registers.
748 */
749 (void) serial_in(up, UART_LSR);
750 if (serial_in(up, UART_LSR) & UART_LSR_DR)
751 (void) serial_in(up, UART_RX);
752 (void) serial_in(up, UART_IIR);
753 (void) serial_in(up, UART_MSR);
754
755 /*
756 * Now, initialize the UART
757 */
758 serial_out(up, UART_LCR, UART_LCR_WLEN8);
759 spin_lock_irqsave(&up->port.lock, flags);
760 /*
761 * Most PC uarts need OUT2 raised to enable interrupts.
762 */
763 up->port.mctrl |= TIOCM_OUT2;
764 serial_omap_set_mctrl(&up->port, up->port.mctrl);
765 spin_unlock_irqrestore(&up->port.lock, flags);
766
767 up->msr_saved_flags = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530768 /*
769 * Finally, enable interrupts. Note: Modem status interrupts
770 * are set via set_termios(), which will be occurring imminently
771 * anyway, so we don't enable them here.
772 */
773 up->ier = UART_IER_RLSI | UART_IER_RDI;
774 serial_out(up, UART_IER, up->ier);
775
Jarkko Nikula78841462011-01-24 17:51:22 +0200776 /* Enable module level wake up */
Govindraj.Rf64ffda2013-07-05 18:25:59 +0300777 up->wer = OMAP_UART_WER_MOD_WKUP;
778 if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
779 up->wer |= OMAP_UART_TX_WAKEUP_EN;
780
781 serial_out(up, UART_OMAP_WER, up->wer);
Jarkko Nikula78841462011-01-24 17:51:22 +0200782
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300783 pm_runtime_mark_last_busy(up->dev);
784 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530785 up->port_activity = jiffies;
786 return 0;
787}
788
789static void serial_omap_shutdown(struct uart_port *port)
790{
Felipe Balbic990f352012-08-23 13:32:41 +0300791 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530792 unsigned long flags = 0;
793
Rajendra Nayakba774332011-12-14 17:25:43 +0530794 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530795
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300796 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530797 /*
798 * Disable interrupts from this port
799 */
800 up->ier = 0;
801 serial_out(up, UART_IER, 0);
802
803 spin_lock_irqsave(&up->port.lock, flags);
804 up->port.mctrl &= ~TIOCM_OUT2;
805 serial_omap_set_mctrl(&up->port, up->port.mctrl);
806 spin_unlock_irqrestore(&up->port.lock, flags);
807
808 /*
809 * Disable break condition and FIFOs
810 */
811 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
812 serial_omap_clear_fifos(up);
813
814 /*
815 * Read data port to reset things, and then free the irq
816 */
817 if (serial_in(up, UART_LSR) & UART_LSR_DR)
818 (void) serial_in(up, UART_RX);
Govindraj.Rfcdca752011-02-28 18:12:23 +0530819
Felipe Balbi660ac5f2012-09-06 15:45:26 +0300820 pm_runtime_mark_last_busy(up->dev);
821 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530822 free_irq(up->port.irq, up);
Tony Lindgrenee83bd32015-06-09 23:35:00 -0700823 dev_pm_clear_wake_irq(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530824}
825
Govindraj.R2fd14962011-11-09 17:41:21 +0530826static void serial_omap_uart_qos_work(struct work_struct *work)
827{
828 struct uart_omap_port *up = container_of(work, struct uart_omap_port,
829 qos_work);
830
Rafael J. Wysocki01d2b182020-02-12 00:27:04 +0100831 cpu_latency_qos_update_request(&up->pm_qos_request, up->latency);
Govindraj.R2fd14962011-11-09 17:41:21 +0530832}
833
Govindraj.Rb6126332010-09-27 20:20:49 +0530834static void
835serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
836 struct ktermios *old)
837{
Felipe Balbic990f352012-08-23 13:32:41 +0300838 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +0530839 unsigned char cval = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530840 unsigned long flags = 0;
841 unsigned int baud, quot;
842
843 switch (termios->c_cflag & CSIZE) {
844 case CS5:
845 cval = UART_LCR_WLEN5;
846 break;
847 case CS6:
848 cval = UART_LCR_WLEN6;
849 break;
850 case CS7:
851 cval = UART_LCR_WLEN7;
852 break;
853 default:
854 case CS8:
855 cval = UART_LCR_WLEN8;
856 break;
857 }
858
859 if (termios->c_cflag & CSTOPB)
860 cval |= UART_LCR_STOP;
861 if (termios->c_cflag & PARENB)
862 cval |= UART_LCR_PARITY;
863 if (!(termios->c_cflag & PARODD))
864 cval |= UART_LCR_EPAR;
Enric Balletbo i Serrafdbc7352012-12-06 09:45:04 +0100865 if (termios->c_cflag & CMSPAR)
866 cval |= UART_LCR_SPAR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530867
868 /*
869 * Ask the core to calculate the divisor for us.
870 */
871
872 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
873 quot = serial_omap_get_divisor(port, baud);
874
Govindraj.R2fd14962011-11-09 17:41:21 +0530875 /* calculate wakeup latency constraint */
Paul Walmsley19723452012-01-25 19:50:56 -0700876 up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
Govindraj.R2fd14962011-11-09 17:41:21 +0530877 up->latency = up->calc_latency;
878 schedule_work(&up->qos_work);
879
Govindraj.Rc538d202011-11-07 18:57:03 +0530880 up->dll = quot & 0xff;
881 up->dlh = quot >> 8;
882 up->mdr1 = UART_OMAP_MDR1_DISABLE;
883
Govindraj.Rb6126332010-09-27 20:20:49 +0530884 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
885 UART_FCR_ENABLE_FIFO;
Govindraj.Rb6126332010-09-27 20:20:49 +0530886
887 /*
888 * Ok, we're now changing the port state. Do it with
889 * interrupts disabled.
890 */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +0300891 pm_runtime_get_sync(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +0530892 spin_lock_irqsave(&up->port.lock, flags);
893
894 /*
895 * Update the per-port timeout.
896 */
897 uart_update_timeout(port, termios->c_cflag, baud);
898
899 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
900 if (termios->c_iflag & INPCK)
901 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
902 if (termios->c_iflag & (BRKINT | PARMRK))
903 up->port.read_status_mask |= UART_LSR_BI;
904
905 /*
906 * Characters to ignore
907 */
908 up->port.ignore_status_mask = 0;
909 if (termios->c_iflag & IGNPAR)
910 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
911 if (termios->c_iflag & IGNBRK) {
912 up->port.ignore_status_mask |= UART_LSR_BI;
913 /*
914 * If we're ignoring parity and break indicators,
915 * ignore overruns too (for real raw support).
916 */
917 if (termios->c_iflag & IGNPAR)
918 up->port.ignore_status_mask |= UART_LSR_OE;
919 }
920
921 /*
922 * ignore all characters if CREAD is not set
923 */
924 if ((termios->c_cflag & CREAD) == 0)
925 up->port.ignore_status_mask |= UART_LSR_DR;
926
927 /*
928 * Modem status interrupts
929 */
930 up->ier &= ~UART_IER_MSI;
931 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
932 up->ier |= UART_IER_MSI;
933 serial_out(up, UART_IER, up->ier);
934 serial_out(up, UART_LCR, cval); /* reset DLAB */
Govindraj.Rc538d202011-11-07 18:57:03 +0530935 up->lcr = cval;
Alexey Pelykh1776fd02013-02-04 12:19:46 -0500936 up->scr = 0;
Govindraj.Rb6126332010-09-27 20:20:49 +0530937
938 /* FIFOs and DMA Settings */
939
940 /* FCR can be changed only when the
941 * baud clock is not running
942 * DLL_REG and DLH_REG set to 0.
943 */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800944 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530945 serial_out(up, UART_DLL, 0);
946 serial_out(up, UART_DLM, 0);
947 serial_out(up, UART_LCR, 0);
948
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800949 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530950
Russell King08bd4902012-10-05 13:54:53 +0100951 up->efr = serial_in(up, UART_EFR) & ~UART_EFR_ECB;
Russell Kingd864c032012-10-06 00:51:17 +0100952 up->efr &= ~UART_EFR_SCD;
Govindraj.Rb6126332010-09-27 20:20:49 +0530953 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
954
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800955 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Russell King08bd4902012-10-05 13:54:53 +0100956 up->mcr = serial_in(up, UART_MCR) & ~UART_MCR_TCRTLR;
Govindraj.Rb6126332010-09-27 20:20:49 +0530957 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
958 /* FIFO ENABLE, DMA MODE */
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700959
Alexey Pelykh1f663962013-04-03 14:31:46 -0400960 up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
961 /*
962 * NOTE: Setting OMAP_UART_SCR_RX_TRIG_GRANU1_MASK
963 * sets Enables the granularity of 1 for TRIGGER RX
964 * level. Along with setting RX FIFO trigger level
965 * to 1 (as noted below, 16 characters) and TLR[3:0]
966 * to zero this will result RX FIFO threshold level
967 * to 1 character, instead of 16 as noted in comment
968 * below.
969 */
970
Felipe Balbi6721ab72012-09-06 15:45:40 +0300971 /* Set receive FIFO threshold to 16 characters and
Philippe Proulx018e7442013-10-23 18:49:58 -0400972 * transmit FIFO threshold to 32 spaces
Felipe Balbi6721ab72012-09-06 15:45:40 +0300973 */
Felipe Balbi49457432012-09-06 15:45:21 +0300974 up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
Felipe Balbi6721ab72012-09-06 15:45:40 +0300975 up->fcr &= ~OMAP_UART_FCR_TX_FIFO_TRIG_MASK;
976 up->fcr |= UART_FCR6_R_TRIGGER_16 | UART_FCR6_T_TRIGGER_24 |
977 UART_FCR_ENABLE_FIFO;
Greg Kroah-Hartman8a74e9f2012-01-26 11:15:18 -0800978
Paul Walmsley0ba5f662012-01-25 19:50:36 -0700979 serial_out(up, UART_FCR, up->fcr);
980 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
981
Govindraj.Rc538d202011-11-07 18:57:03 +0530982 serial_out(up, UART_OMAP_SCR, up->scr);
983
Russell King08bd4902012-10-05 13:54:53 +0100984 /* Reset UART_MCR_TCRTLR: this must be done with the EFR_ECB bit set */
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800985 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530986 serial_out(up, UART_MCR, up->mcr);
Russell King08bd4902012-10-05 13:54:53 +0100987 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
988 serial_out(up, UART_EFR, up->efr);
989 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Govindraj.Rb6126332010-09-27 20:20:49 +0530990
991 /* Protocol, Baud Rate, and Interrupt Settings */
992
Govindraj.R94734742011-11-07 19:00:33 +0530993 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
994 serial_omap_mdr1_errataset(up, up->mdr1);
995 else
996 serial_out(up, UART_OMAP_MDR1, up->mdr1);
997
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800998 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +0530999 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
1000
1001 serial_out(up, UART_LCR, 0);
1002 serial_out(up, UART_IER, 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001003 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301004
Govindraj.Rc538d202011-11-07 18:57:03 +05301005 serial_out(up, UART_DLL, up->dll); /* LS of divisor */
1006 serial_out(up, UART_DLM, up->dlh); /* MS of divisor */
Govindraj.Rb6126332010-09-27 20:20:49 +05301007
1008 serial_out(up, UART_LCR, 0);
1009 serial_out(up, UART_IER, up->ier);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001010 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301011
1012 serial_out(up, UART_EFR, up->efr);
1013 serial_out(up, UART_LCR, cval);
1014
Alexey Pelykh5fe21232013-01-16 05:08:06 -05001015 if (!serial_omap_baud_is_mode16(port, baud))
Govindraj.Rc538d202011-11-07 18:57:03 +05301016 up->mdr1 = UART_OMAP_MDR1_13X_MODE;
Govindraj.Rb6126332010-09-27 20:20:49 +05301017 else
Govindraj.Rc538d202011-11-07 18:57:03 +05301018 up->mdr1 = UART_OMAP_MDR1_16X_MODE;
1019
Govindraj.R94734742011-11-07 19:00:33 +05301020 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1021 serial_omap_mdr1_errataset(up, up->mdr1);
1022 else
1023 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rb6126332010-09-27 20:20:49 +05301024
Russell Kingc533e512012-10-06 09:34:36 +01001025 /* Configure flow control */
Russell Kingc7d059c2012-10-06 09:12:44 +01001026 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301027
Russell Kingc533e512012-10-06 09:34:36 +01001028 /* XON1/XOFF1 accessible mode B, TCRTLR=0, ECB=0 */
1029 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
1030 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
Govindraj.Rb6126332010-09-27 20:20:49 +05301031
Russell Kingc533e512012-10-06 09:34:36 +01001032 /* Enable access to TCR/TLR */
Russell Kingc7d059c2012-10-06 09:12:44 +01001033 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
1034 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1035 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
Govindraj.Rb6126332010-09-27 20:20:49 +05301036
Russell Kingc7d059c2012-10-06 09:12:44 +01001037 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
Govindraj.Rb6126332010-09-27 20:20:49 +05301038
Peter Hurley391f93f2015-01-25 14:44:51 -05001039 up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
1040
Russell King08bd4902012-10-05 13:54:53 +01001041 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
Peter Hurley348f9bb2015-01-25 14:44:53 -05001042 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
Peter Hurley391f93f2015-01-25 14:44:51 -05001043 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Peter Hurley348f9bb2015-01-25 14:44:53 -05001044 up->efr |= UART_EFR_CTS;
Russell King0d5b1662012-10-05 23:48:28 +01001045 } else {
1046 /* Disable AUTORTS and AUTOCTS */
1047 up->efr &= ~(UART_EFR_CTS | UART_EFR_RTS);
Govindraj.Rb6126332010-09-27 20:20:49 +05301048 }
1049
Russell King01d70bb2012-10-15 16:50:59 +01001050 if (up->port.flags & UPF_SOFT_FLOW) {
Russell King01d70bb2012-10-15 16:50:59 +01001051 /* clear SW control mode bits */
1052 up->efr &= OMAP_UART_SW_CLR;
1053
1054 /*
1055 * IXON Flag:
Russell King01d70bb2012-10-15 16:50:59 +01001056 * Enable XON/XOFF flow control on input.
1057 * Receiver compares XON1, XOFF1.
1058 */
Russell King3af08bd2012-10-05 13:32:08 +01001059 if (termios->c_iflag & IXON)
Russell King01d70bb2012-10-15 16:50:59 +01001060 up->efr |= OMAP_UART_SW_RX;
1061
Russell King01d70bb2012-10-15 16:50:59 +01001062 /*
Russell King3af08bd2012-10-05 13:32:08 +01001063 * IXOFF Flag:
1064 * Enable XON/XOFF flow control on output.
1065 * Transmit XON1, XOFF1
1066 */
Peter Hurley391f93f2015-01-25 14:44:51 -05001067 if (termios->c_iflag & IXOFF) {
1068 up->port.status |= UPSTAT_AUTOXOFF;
Russell King3af08bd2012-10-05 13:32:08 +01001069 up->efr |= OMAP_UART_SW_TX;
Peter Hurley391f93f2015-01-25 14:44:51 -05001070 }
Russell King3af08bd2012-10-05 13:32:08 +01001071
1072 /*
Russell King01d70bb2012-10-15 16:50:59 +01001073 * IXANY Flag:
1074 * Enable any character to restart output.
1075 * Operation resumes after receiving any
1076 * character after recognition of the XOFF character
1077 */
1078 if (termios->c_iflag & IXANY)
1079 up->mcr |= UART_MCR_XONANY;
1080 else
1081 up->mcr &= ~UART_MCR_XONANY;
Russell King01d70bb2012-10-15 16:50:59 +01001082 }
Russell Kingc7d059c2012-10-06 09:12:44 +01001083 serial_out(up, UART_MCR, up->mcr);
Russell King18f360f2012-10-06 09:08:20 +01001084 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1085 serial_out(up, UART_EFR, up->efr);
1086 serial_out(up, UART_LCR, up->lcr);
1087
Govindraj.Rb6126332010-09-27 20:20:49 +05301088 serial_omap_set_mctrl(&up->port, up->port.mctrl);
Govindraj.Rb6126332010-09-27 20:20:49 +05301089
1090 spin_unlock_irqrestore(&up->port.lock, flags);
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001091 pm_runtime_mark_last_busy(up->dev);
1092 pm_runtime_put_autosuspend(up->dev);
Rajendra Nayakba774332011-12-14 17:25:43 +05301093 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301094}
1095
1096static void
1097serial_omap_pm(struct uart_port *port, unsigned int state,
1098 unsigned int oldstate)
1099{
Felipe Balbic990f352012-08-23 13:32:41 +03001100 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301101 unsigned char efr;
1102
Rajendra Nayakba774332011-12-14 17:25:43 +05301103 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301104
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001105 pm_runtime_get_sync(up->dev);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001106 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301107 efr = serial_in(up, UART_EFR);
1108 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
1109 serial_out(up, UART_LCR, 0);
1110
1111 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -08001112 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Govindraj.Rb6126332010-09-27 20:20:49 +05301113 serial_out(up, UART_EFR, efr);
1114 serial_out(up, UART_LCR, 0);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301115
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001116 pm_runtime_mark_last_busy(up->dev);
1117 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301118}
1119
1120static void serial_omap_release_port(struct uart_port *port)
1121{
1122 dev_dbg(port->dev, "serial_omap_release_port+\n");
1123}
1124
1125static int serial_omap_request_port(struct uart_port *port)
1126{
1127 dev_dbg(port->dev, "serial_omap_request_port+\n");
1128 return 0;
1129}
1130
1131static void serial_omap_config_port(struct uart_port *port, int flags)
1132{
Felipe Balbic990f352012-08-23 13:32:41 +03001133 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301134
1135 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
Rajendra Nayakba774332011-12-14 17:25:43 +05301136 up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301137 up->port.type = PORT_OMAP;
Russell King3af08bd2012-10-05 13:32:08 +01001138 up->port.flags |= UPF_SOFT_FLOW | UPF_HARD_FLOW;
Govindraj.Rb6126332010-09-27 20:20:49 +05301139}
1140
1141static int
1142serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
1143{
1144 /* we don't want the core code to modify any port params */
1145 dev_dbg(port->dev, "serial_omap_verify_port+\n");
1146 return -EINVAL;
1147}
1148
1149static const char *
1150serial_omap_type(struct uart_port *port)
1151{
Felipe Balbic990f352012-08-23 13:32:41 +03001152 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301153
Rajendra Nayakba774332011-12-14 17:25:43 +05301154 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
Govindraj.Rb6126332010-09-27 20:20:49 +05301155 return up->name;
1156}
1157
Govindraj.Rb6126332010-09-27 20:20:49 +05301158#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1159
Arnd Bergmannb4a512b2016-01-13 21:59:23 +01001160static void __maybe_unused wait_for_xmitr(struct uart_omap_port *up)
Govindraj.Rb6126332010-09-27 20:20:49 +05301161{
1162 unsigned int status, tmout = 10000;
1163
1164 /* Wait up to 10ms for the character(s) to be sent. */
1165 do {
1166 status = serial_in(up, UART_LSR);
1167
1168 if (status & UART_LSR_BI)
1169 up->lsr_break_flag = UART_LSR_BI;
1170
1171 if (--tmout == 0)
1172 break;
1173 udelay(1);
1174 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1175
1176 /* Wait up to 1s for flow control if necessary */
1177 if (up->port.flags & UPF_CONS_FLOW) {
1178 tmout = 1000000;
1179 for (tmout = 1000000; tmout; tmout--) {
1180 unsigned int msr = serial_in(up, UART_MSR);
1181
1182 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1183 if (msr & UART_MSR_CTS)
1184 break;
1185
1186 udelay(1);
1187 }
1188 }
1189}
1190
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001191#ifdef CONFIG_CONSOLE_POLL
1192
1193static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
1194{
Felipe Balbic990f352012-08-23 13:32:41 +03001195 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301196
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001197 pm_runtime_get_sync(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001198 wait_for_xmitr(up);
1199 serial_out(up, UART_TX, ch);
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001200 pm_runtime_mark_last_busy(up->dev);
1201 pm_runtime_put_autosuspend(up->dev);
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001202}
1203
1204static int serial_omap_poll_get_char(struct uart_port *port)
1205{
Felipe Balbic990f352012-08-23 13:32:41 +03001206 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301207 unsigned int status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001208
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001209 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301210 status = serial_in(up, UART_LSR);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001211 if (!(status & UART_LSR_DR)) {
1212 status = NO_POLL_CHAR;
1213 goto out;
1214 }
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001215
Govindraj.Rfcdca752011-02-28 18:12:23 +05301216 status = serial_in(up, UART_RX);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001217
1218out:
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001219 pm_runtime_mark_last_busy(up->dev);
1220 pm_runtime_put_autosuspend(up->dev);
Felipe Balbia6b19c32012-09-06 15:45:36 +03001221
Govindraj.Rfcdca752011-02-28 18:12:23 +05301222 return status;
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001223}
1224
1225#endif /* CONFIG_CONSOLE_POLL */
1226
1227#ifdef CONFIG_SERIAL_OMAP_CONSOLE
1228
Lokesh Vutla28ec9572017-01-19 15:29:38 +05301229#ifdef CONFIG_SERIAL_EARLYCON
Jeffy Chenb38dd0e2017-07-18 14:02:55 +08001230static unsigned int omap_serial_early_in(struct uart_port *port, int offset)
Lokesh Vutla28ec9572017-01-19 15:29:38 +05301231{
1232 offset <<= port->regshift;
1233 return readw(port->membase + offset);
1234}
1235
Jeffy Chenb38dd0e2017-07-18 14:02:55 +08001236static void omap_serial_early_out(struct uart_port *port, int offset,
1237 int value)
Lokesh Vutla28ec9572017-01-19 15:29:38 +05301238{
1239 offset <<= port->regshift;
1240 writew(value, port->membase + offset);
1241}
1242
Jeffy Chenb38dd0e2017-07-18 14:02:55 +08001243static void omap_serial_early_putc(struct uart_port *port, int c)
Lokesh Vutla28ec9572017-01-19 15:29:38 +05301244{
1245 unsigned int status;
1246
1247 for (;;) {
1248 status = omap_serial_early_in(port, UART_LSR);
1249 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
1250 break;
1251 cpu_relax();
1252 }
1253 omap_serial_early_out(port, UART_TX, c);
1254}
1255
Jeffy Chenb38dd0e2017-07-18 14:02:55 +08001256static void early_omap_serial_write(struct console *console, const char *s,
1257 unsigned int count)
Lokesh Vutla28ec9572017-01-19 15:29:38 +05301258{
1259 struct earlycon_device *device = console->data;
1260 struct uart_port *port = &device->port;
1261
1262 uart_console_write(port, s, count, omap_serial_early_putc);
1263}
1264
1265static int __init early_omap_serial_setup(struct earlycon_device *device,
1266 const char *options)
1267{
1268 struct uart_port *port = &device->port;
1269
1270 if (!(device->port.membase || device->port.iobase))
1271 return -ENODEV;
1272
1273 port->regshift = 2;
1274 device->con->write = early_omap_serial_write;
1275 return 0;
1276}
1277
1278OF_EARLYCON_DECLARE(omapserial, "ti,omap2-uart", early_omap_serial_setup);
1279OF_EARLYCON_DECLARE(omapserial, "ti,omap3-uart", early_omap_serial_setup);
1280OF_EARLYCON_DECLARE(omapserial, "ti,omap4-uart", early_omap_serial_setup);
1281#endif /* CONFIG_SERIAL_EARLYCON */
1282
Shubhrajyoti D40477d02012-10-03 17:24:38 +05301283static struct uart_omap_port *serial_omap_console_ports[OMAP_MAX_HSUART_PORTS];
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001284
1285static struct uart_driver serial_omap_reg;
1286
Govindraj.Rb6126332010-09-27 20:20:49 +05301287static void serial_omap_console_putchar(struct uart_port *port, int ch)
1288{
Felipe Balbic990f352012-08-23 13:32:41 +03001289 struct uart_omap_port *up = to_uart_omap_port(port);
Govindraj.Rb6126332010-09-27 20:20:49 +05301290
1291 wait_for_xmitr(up);
1292 serial_out(up, UART_TX, ch);
1293}
1294
1295static void
1296serial_omap_console_write(struct console *co, const char *s,
1297 unsigned int count)
1298{
1299 struct uart_omap_port *up = serial_omap_console_ports[co->index];
1300 unsigned long flags;
1301 unsigned int ier;
1302 int locked = 1;
1303
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001304 pm_runtime_get_sync(up->dev);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301305
Govindraj.Rb6126332010-09-27 20:20:49 +05301306 local_irq_save(flags);
1307 if (up->port.sysrq)
1308 locked = 0;
1309 else if (oops_in_progress)
1310 locked = spin_trylock(&up->port.lock);
1311 else
1312 spin_lock(&up->port.lock);
1313
1314 /*
1315 * First save the IER then disable the interrupts
1316 */
1317 ier = serial_in(up, UART_IER);
1318 serial_out(up, UART_IER, 0);
1319
1320 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
1321
1322 /*
1323 * Finally, wait for transmitter to become empty
1324 * and restore the IER
1325 */
1326 wait_for_xmitr(up);
1327 serial_out(up, UART_IER, ier);
1328 /*
1329 * The receive handling will happen properly because the
1330 * receive ready bit will still be set; it is not cleared
1331 * on read. However, modem control will not, we must
1332 * call it if we have saved something in the saved flags
1333 * while processing with interrupts off.
1334 */
1335 if (up->msr_saved_flags)
1336 check_modem_status(up);
1337
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001338 pm_runtime_mark_last_busy(up->dev);
1339 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301340 if (locked)
1341 spin_unlock(&up->port.lock);
1342 local_irq_restore(flags);
1343}
1344
1345static int __init
1346serial_omap_console_setup(struct console *co, char *options)
1347{
1348 struct uart_omap_port *up;
1349 int baud = 115200;
1350 int bits = 8;
1351 int parity = 'n';
1352 int flow = 'n';
1353
1354 if (serial_omap_console_ports[co->index] == NULL)
1355 return -ENODEV;
1356 up = serial_omap_console_ports[co->index];
1357
1358 if (options)
1359 uart_parse_options(options, &baud, &parity, &bits, &flow);
1360
1361 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1362}
1363
1364static struct console serial_omap_console = {
1365 .name = OMAP_SERIAL_NAME,
1366 .write = serial_omap_console_write,
1367 .device = uart_console_device,
1368 .setup = serial_omap_console_setup,
1369 .flags = CON_PRINTBUFFER,
1370 .index = -1,
1371 .data = &serial_omap_reg,
1372};
1373
1374static void serial_omap_add_console_port(struct uart_omap_port *up)
1375{
Rajendra Nayakba774332011-12-14 17:25:43 +05301376 serial_omap_console_ports[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301377}
1378
1379#define OMAP_CONSOLE (&serial_omap_console)
1380
1381#else
1382
1383#define OMAP_CONSOLE NULL
1384
1385static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1386{}
1387
1388#endif
1389
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001390/* Enable or disable the rs485 support */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001391static int
Peter Hurley308bbc92016-01-12 15:14:46 -08001392serial_omap_config_rs485(struct uart_port *port, struct serial_rs485 *rs485)
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001393{
1394 struct uart_omap_port *up = to_uart_omap_port(port);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001395 unsigned int mode;
1396 int val;
1397
1398 pm_runtime_get_sync(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001399
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001400 /* Disable interrupts from this port */
1401 mode = up->ier;
1402 up->ier = 0;
1403 serial_out(up, UART_IER, 0);
1404
Peter Hurley308bbc92016-01-12 15:14:46 -08001405 /* Clamp the delays to [0, 100ms] */
1406 rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
1407 rs485->delay_rts_after_send = min(rs485->delay_rts_after_send, 100U);
1408
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001409 /* store new config */
Peter Hurley308bbc92016-01-12 15:14:46 -08001410 port->rs485 = *rs485;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001411
1412 /*
1413 * Just as a precaution, only allow rs485
1414 * to be enabled if the gpio pin is valid
1415 */
Linus Walleij5745fd02020-04-15 20:39:27 +02001416 if (up->rts_gpiod) {
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001417 /* enable / disable rts */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001418 val = (port->rs485.flags & SER_RS485_ENABLED) ?
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001419 SER_RS485_RTS_AFTER_SEND : SER_RS485_RTS_ON_SEND;
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001420 val = (port->rs485.flags & val) ? 1 : 0;
Linus Walleij5745fd02020-04-15 20:39:27 +02001421 gpiod_set_value(up->rts_gpiod, val);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001422 } else
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001423 port->rs485.flags &= ~SER_RS485_ENABLED;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001424
1425 /* Enable interrupts */
1426 up->ier = mode;
1427 serial_out(up, UART_IER, up->ier);
1428
Philippe Proulx018e7442013-10-23 18:49:58 -04001429 /* If RS-485 is disabled, make sure the THR interrupt is fired when
1430 * TX FIFO is below the trigger level.
1431 */
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001432 if (!(port->rs485.flags & SER_RS485_ENABLED) &&
Philippe Proulx018e7442013-10-23 18:49:58 -04001433 (up->scr & OMAP_UART_SCR_TX_EMPTY)) {
1434 up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
1435 serial_out(up, UART_OMAP_SCR, up->scr);
1436 }
1437
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001438 pm_runtime_mark_last_busy(up->dev);
1439 pm_runtime_put_autosuspend(up->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001440
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001441 return 0;
1442}
1443
Bhumika Goyal2331e062017-01-25 23:18:52 +05301444static const struct uart_ops serial_omap_pops = {
Govindraj.Rb6126332010-09-27 20:20:49 +05301445 .tx_empty = serial_omap_tx_empty,
1446 .set_mctrl = serial_omap_set_mctrl,
1447 .get_mctrl = serial_omap_get_mctrl,
1448 .stop_tx = serial_omap_stop_tx,
1449 .start_tx = serial_omap_start_tx,
Russell King3af08bd2012-10-05 13:32:08 +01001450 .throttle = serial_omap_throttle,
1451 .unthrottle = serial_omap_unthrottle,
Govindraj.Rb6126332010-09-27 20:20:49 +05301452 .stop_rx = serial_omap_stop_rx,
1453 .enable_ms = serial_omap_enable_ms,
1454 .break_ctl = serial_omap_break_ctl,
1455 .startup = serial_omap_startup,
1456 .shutdown = serial_omap_shutdown,
1457 .set_termios = serial_omap_set_termios,
1458 .pm = serial_omap_pm,
1459 .type = serial_omap_type,
1460 .release_port = serial_omap_release_port,
1461 .request_port = serial_omap_request_port,
1462 .config_port = serial_omap_config_port,
1463 .verify_port = serial_omap_verify_port,
Cosmin Cojocar1b41dbc2010-12-05 16:15:10 +01001464#ifdef CONFIG_CONSOLE_POLL
1465 .poll_put_char = serial_omap_poll_put_char,
1466 .poll_get_char = serial_omap_poll_get_char,
1467#endif
Govindraj.Rb6126332010-09-27 20:20:49 +05301468};
1469
1470static struct uart_driver serial_omap_reg = {
1471 .owner = THIS_MODULE,
1472 .driver_name = "OMAP-SERIAL",
1473 .dev_name = OMAP_SERIAL_NAME,
1474 .nr = OMAP_MAX_HSUART_PORTS,
1475 .cons = OMAP_CONSOLE,
1476};
1477
Shubhrajyoti D3bc4f0d2012-01-16 15:52:36 +05301478#ifdef CONFIG_PM_SLEEP
Sourav Poddarddd85e22013-05-15 21:05:38 +05301479static int serial_omap_prepare(struct device *dev)
1480{
1481 struct uart_omap_port *up = dev_get_drvdata(dev);
1482
1483 up->is_suspending = true;
1484
1485 return 0;
1486}
1487
1488static void serial_omap_complete(struct device *dev)
1489{
1490 struct uart_omap_port *up = dev_get_drvdata(dev);
1491
1492 up->is_suspending = false;
1493}
1494
Govindraj.Rfcdca752011-02-28 18:12:23 +05301495static int serial_omap_suspend(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301496{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301497 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301498
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301499 uart_suspend_port(&serial_omap_reg, &up->port);
Linus Torvalds033d9952012-10-02 09:54:49 -07001500 flush_work(&up->qos_work);
Govindraj.R2fd14962011-11-09 17:41:21 +05301501
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001502 if (device_may_wakeup(dev))
1503 serial_omap_enable_wakeup(up, true);
1504 else
1505 serial_omap_enable_wakeup(up, false);
1506
Govindraj.Rb6126332010-09-27 20:20:49 +05301507 return 0;
1508}
1509
Govindraj.Rfcdca752011-02-28 18:12:23 +05301510static int serial_omap_resume(struct device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301511{
Govindraj.Rfcdca752011-02-28 18:12:23 +05301512 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301513
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001514 if (device_may_wakeup(dev))
1515 serial_omap_enable_wakeup(up, false);
1516
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301517 uart_resume_port(&serial_omap_reg, &up->port);
1518
Govindraj.Rb6126332010-09-27 20:20:49 +05301519 return 0;
1520}
Sourav Poddarddd85e22013-05-15 21:05:38 +05301521#else
1522#define serial_omap_prepare NULL
Arnd Bergmann2cb5a2f2013-06-01 11:18:13 +02001523#define serial_omap_complete NULL
Sourav Poddarddd85e22013-05-15 21:05:38 +05301524#endif /* CONFIG_PM_SLEEP */
Govindraj.Rb6126332010-09-27 20:20:49 +05301525
Bill Pemberton9671f092012-11-19 13:21:50 -05001526static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301527{
1528 u32 mvr, scheme;
1529 u16 revision, major, minor;
1530
Ruchika Kharwar76bac192013-07-08 10:28:57 +03001531 mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301532
1533 /* Check revision register scheme */
1534 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
1535
1536 switch (scheme) {
1537 case 0: /* Legacy Scheme: OMAP2/3 */
1538 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
1539 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
1540 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
1541 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
1542 break;
1543 case 1:
1544 /* New Scheme: OMAP4+ */
1545 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
1546 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
1547 OMAP_UART_MVR_MAJ_SHIFT;
1548 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
1549 break;
1550 default:
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001551 dev_warn(up->dev,
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301552 "Unknown %s revision, defaulting to highest\n",
1553 up->name);
1554 /* highest possible revision */
1555 major = 0xff;
1556 minor = 0xff;
1557 }
1558
1559 /* normalize revision for the driver */
1560 revision = UART_BUILD_REVISION(major, minor);
1561
1562 switch (revision) {
1563 case OMAP_UART_REV_46:
1564 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1565 UART_ERRATA_i291_DMA_FORCEIDLE);
1566 break;
1567 case OMAP_UART_REV_52:
1568 up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
1569 UART_ERRATA_i291_DMA_FORCEIDLE);
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001570 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301571 break;
1572 case OMAP_UART_REV_63:
1573 up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001574 up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301575 break;
1576 default:
1577 break;
1578 }
1579}
1580
Bill Pemberton9671f092012-11-19 13:21:50 -05001581static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301582{
1583 struct omap_uart_port_info *omap_up_info;
1584
1585 omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
1586 if (!omap_up_info)
1587 return NULL; /* out of memory */
1588
1589 of_property_read_u32(dev->of_node, "clock-frequency",
1590 &omap_up_info->uartclk);
Sebastian Reichel1b775de2017-03-28 17:59:30 +02001591
1592 omap_up_info->flags = UPF_BOOT_AUTOCONF;
1593
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301594 return omap_up_info;
1595}
1596
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001597static int serial_omap_probe_rs485(struct uart_omap_port *up,
Linus Walleij5745fd02020-04-15 20:39:27 +02001598 struct device *dev)
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001599{
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001600 struct serial_rs485 *rs485conf = &up->port.rs485;
Linus Walleij5745fd02020-04-15 20:39:27 +02001601 struct device_node *np = dev->of_node;
1602 enum gpiod_flags gflags;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001603 int ret;
1604
1605 rs485conf->flags = 0;
Linus Walleij5745fd02020-04-15 20:39:27 +02001606 up->rts_gpiod = NULL;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001607
1608 if (!np)
1609 return 0;
1610
Lukas Wunnerc150c0f2020-05-12 14:40:02 +02001611 ret = uart_get_rs485_mode(&up->port);
1612 if (ret)
1613 return ret;
Lukas Wunner743f93f2017-11-24 23:26:40 +01001614
Lukas Wunnerf1e5b612017-11-24 23:26:40 +01001615 if (of_property_read_bool(np, "rs485-rts-active-high")) {
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001616 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
Lukas Wunnerf1e5b612017-11-24 23:26:40 +01001617 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
1618 } else {
1619 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001620 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
Lukas Wunnerf1e5b612017-11-24 23:26:40 +01001621 }
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001622
1623 /* check for tx enable gpio */
Linus Walleij5745fd02020-04-15 20:39:27 +02001624 gflags = rs485conf->flags & SER_RS485_RTS_AFTER_SEND ?
1625 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
1626 up->rts_gpiod = devm_gpiod_get_optional(dev, "rts", gflags);
1627 if (IS_ERR(up->rts_gpiod)) {
1628 ret = PTR_ERR(up->rts_gpiod);
1629 if (ret == -EPROBE_DEFER)
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001630 return ret;
Linus Walleij5745fd02020-04-15 20:39:27 +02001631 /*
1632 * FIXME: the code historically ignored any other error than
1633 * -EPROBE_DEFER and just went on without GPIO.
1634 */
1635 up->rts_gpiod = NULL;
Michael Grzeschika64c1a12014-02-13 10:52:03 +01001636 } else {
Linus Walleij5745fd02020-04-15 20:39:27 +02001637 gpiod_set_consumer_name(up->rts_gpiod, "omap-serial");
Michael Grzeschika64c1a12014-02-13 10:52:03 +01001638 }
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001639
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001640 return 0;
1641}
1642
Bill Pemberton9671f092012-11-19 13:21:50 -05001643static int serial_omap_probe(struct platform_device *pdev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301644{
Jingoo Han574de552013-07-30 17:06:57 +09001645 struct omap_uart_port_info *omap_up_info = dev_get_platdata(&pdev->dev);
Felipe Balbicc516382014-04-23 09:58:31 -05001646 struct uart_omap_port *up;
1647 struct resource *mem;
Felipe Balbid044d232014-04-23 09:58:33 -05001648 void __iomem *base;
Felipe Balbicc516382014-04-23 09:58:31 -05001649 int uartirq = 0;
1650 int wakeirq = 0;
1651 int ret;
Govindraj.Rb6126332010-09-27 20:20:49 +05301652
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001653 /* The optional wakeirq may be specified in the board dts file */
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001654 if (pdev->dev.of_node) {
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001655 uartirq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1656 if (!uartirq)
1657 return -EPROBE_DEFER;
1658 wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301659 omap_up_info = of_get_uart_port_info(&pdev->dev);
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001660 pdev->dev.platform_data = omap_up_info;
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001661 } else {
Felipe Balbi54af6922014-04-23 09:58:32 -05001662 uartirq = platform_get_irq(pdev, 0);
1663 if (uartirq < 0)
1664 return -EPROBE_DEFER;
Vikram Panditaa0a490f2013-07-08 10:25:43 +03001665 }
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301666
Felipe Balbid044d232014-04-23 09:58:33 -05001667 up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
1668 if (!up)
1669 return -ENOMEM;
Govindraj.Rb6126332010-09-27 20:20:49 +05301670
Felipe Balbid044d232014-04-23 09:58:33 -05001671 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1672 base = devm_ioremap_resource(&pdev->dev, mem);
1673 if (IS_ERR(base))
1674 return PTR_ERR(base);
Govindraj.Rb6126332010-09-27 20:20:49 +05301675
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001676 up->dev = &pdev->dev;
Govindraj.Rb6126332010-09-27 20:20:49 +05301677 up->port.dev = &pdev->dev;
1678 up->port.type = PORT_OMAP;
1679 up->port.iotype = UPIO_MEM;
Tony Lindgren2a0b9652013-10-22 06:49:48 -07001680 up->port.irq = uartirq;
Govindraj.Rb6126332010-09-27 20:20:49 +05301681 up->port.regshift = 2;
1682 up->port.fifosize = 64;
1683 up->port.ops = &serial_omap_pops;
Dmitry Safonovb062e4a2019-12-13 00:06:27 +00001684 up->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_OMAP_CONSOLE);
Govindraj.Rb6126332010-09-27 20:20:49 +05301685
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301686 if (pdev->dev.of_node)
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001687 ret = of_alias_get_id(pdev->dev.of_node, "serial");
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301688 else
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001689 ret = pdev->id;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301690
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001691 if (ret < 0) {
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301692 dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001693 ret);
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301694 goto err_port_line;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301695 }
Sebastian Andrzej Siewior3c599582014-11-12 10:28:34 +01001696 up->port.line = ret;
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301697
Nishanth Menon7af0ea52014-10-22 07:46:50 -05001698 if (up->port.line >= OMAP_MAX_HSUART_PORTS) {
1699 dev_err(&pdev->dev, "uart ID %d > MAX %d.\n", up->port.line,
1700 OMAP_MAX_HSUART_PORTS);
1701 ret = -ENXIO;
1702 goto err_port_line;
1703 }
1704
Doug Kehn1cf94d32015-03-24 08:19:27 -05001705 up->wakeirq = wakeirq;
1706 if (!up->wakeirq)
1707 dev_info(up->port.dev, "no wakeirq for uart%d\n",
1708 up->port.line);
1709
Linus Walleij5745fd02020-04-15 20:39:27 +02001710 ret = serial_omap_probe_rs485(up, &pdev->dev);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001711 if (ret < 0)
1712 goto err_rs485;
1713
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301714 sprintf(up->name, "OMAP UART%d", up->port.line);
Govindraj.Redd70ad2011-10-11 14:55:41 +05301715 up->port.mapbase = mem->start;
Felipe Balbid044d232014-04-23 09:58:33 -05001716 up->port.membase = base;
Govindraj.Rb6126332010-09-27 20:20:49 +05301717 up->port.flags = omap_up_info->flags;
Govindraj.Rb6126332010-09-27 20:20:49 +05301718 up->port.uartclk = omap_up_info->uartclk;
Ricardo Ribalda Delgadodadd7ec2014-11-06 22:46:14 +01001719 up->port.rs485_config = serial_omap_config_rs485;
Rajendra Nayak8fe789d2011-12-14 17:25:44 +05301720 if (!up->port.uartclk) {
1721 up->port.uartclk = DEFAULT_CLK_SPEED;
Philippe Proulxe5f9bf72013-10-23 18:49:59 -04001722 dev_warn(&pdev->dev,
Philippe Proulx80d86112013-10-31 09:39:58 -04001723 "No clock speed specified: using default: %d\n",
Philippe Proulxe5f9bf72013-10-23 18:49:59 -04001724 DEFAULT_CLK_SPEED);
Rajendra Nayak8fe789d2011-12-14 17:25:44 +05301725 }
Govindraj.Rb6126332010-09-27 20:20:49 +05301726
Rafael J. Wysocki2552d352020-02-12 00:04:31 +01001727 up->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1728 up->calc_latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
Rafael J. Wysocki01d2b182020-02-12 00:27:04 +01001729 cpu_latency_qos_add_request(&up->pm_qos_request, up->latency);
Govindraj.R2fd14962011-11-09 17:41:21 +05301730 INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);
1731
Felipe Balbi93220dc2012-09-06 15:45:27 +03001732 platform_set_drvdata(pdev, up);
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001733 if (omap_up_info->autosuspend_timeout == 0)
1734 omap_up_info->autosuspend_timeout = -1;
Felipe Balbi5b6acc72014-04-23 09:58:29 -05001735
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001736 device_init_wakeup(up->dev, true);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301737 pm_runtime_use_autosuspend(&pdev->dev);
1738 pm_runtime_set_autosuspend_delay(&pdev->dev,
Deepak Kc86845db2011-11-09 17:33:38 +05301739 omap_up_info->autosuspend_timeout);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301740
1741 pm_runtime_irq_safe(&pdev->dev);
Grygorii Strashko3026d142013-07-22 15:31:15 +05301742 pm_runtime_enable(&pdev->dev);
1743
Govindraj.Rfcdca752011-02-28 18:12:23 +05301744 pm_runtime_get_sync(&pdev->dev);
1745
Govindraj.R7c77c8d2012-04-03 19:12:34 +05301746 omap_serial_fill_features_erratas(up);
1747
Rajendra Nayakba774332011-12-14 17:25:43 +05301748 ui[up->port.line] = up;
Govindraj.Rb6126332010-09-27 20:20:49 +05301749 serial_omap_add_console_port(up);
1750
1751 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1752 if (ret != 0)
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301753 goto err_add_port;
Govindraj.Rb6126332010-09-27 20:20:49 +05301754
Felipe Balbi660ac5f2012-09-06 15:45:26 +03001755 pm_runtime_mark_last_busy(up->dev);
1756 pm_runtime_put_autosuspend(up->dev);
Govindraj.Rb6126332010-09-27 20:20:49 +05301757 return 0;
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301758
1759err_add_port:
Johan Hovold77e6fe72017-04-10 11:21:39 +02001760 pm_runtime_dont_use_autosuspend(&pdev->dev);
1761 pm_runtime_put_sync(&pdev->dev);
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301762 pm_runtime_disable(&pdev->dev);
Rafael J. Wysocki01d2b182020-02-12 00:27:04 +01001763 cpu_latency_qos_remove_request(&up->pm_qos_request);
Semen Protsenko66cf1d82015-04-30 18:35:27 +03001764 device_init_wakeup(up->dev, false);
Mark Jackson4a0ac0f2013-08-14 11:29:38 +01001765err_rs485:
Shubhrajyoti D388bc262012-03-21 17:22:22 +05301766err_port_line:
Govindraj.Rb6126332010-09-27 20:20:49 +05301767 return ret;
1768}
1769
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001770static int serial_omap_remove(struct platform_device *dev)
Govindraj.Rb6126332010-09-27 20:20:49 +05301771{
1772 struct uart_omap_port *up = platform_get_drvdata(dev);
1773
Johan Hovold099bd732017-04-10 11:21:38 +02001774 pm_runtime_get_sync(up->dev);
1775
1776 uart_remove_one_port(&serial_omap_reg, &up->port);
1777
1778 pm_runtime_dont_use_autosuspend(up->dev);
Felipe Balbi7e9c8e72012-09-06 15:45:29 +03001779 pm_runtime_put_sync(up->dev);
Felipe Balbi1b42c8b2012-09-06 15:45:28 +03001780 pm_runtime_disable(up->dev);
Rafael J. Wysocki01d2b182020-02-12 00:27:04 +01001781 cpu_latency_qos_remove_request(&up->pm_qos_request);
Sanjay Singh Rawat93a2e472014-03-21 13:55:10 +05301782 device_init_wakeup(&dev->dev, false);
Govindraj.Rfcdca752011-02-28 18:12:23 +05301783
Govindraj.Rb6126332010-09-27 20:20:49 +05301784 return 0;
1785}
1786
Govindraj.R94734742011-11-07 19:00:33 +05301787/*
1788 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
1789 * The access to uart register after MDR1 Access
1790 * causes UART to corrupt data.
1791 *
1792 * Need a delay =
1793 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
1794 * give 10 times as much
1795 */
1796static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
1797{
1798 u8 timeout = 255;
1799
1800 serial_out(up, UART_OMAP_MDR1, mdr1);
1801 udelay(2);
1802 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
1803 UART_FCR_CLEAR_RCVR);
1804 /*
1805 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
1806 * TX_FIFO_E bit is 1.
1807 */
1808 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
1809 (UART_LSR_THRE | UART_LSR_DR))) {
1810 timeout--;
1811 if (!timeout) {
1812 /* Should *never* happen. we warn and carry on */
Felipe Balbid8ee4ea2012-09-06 15:45:20 +03001813 dev_crit(up->dev, "Errata i202: timedout %x\n",
Govindraj.R94734742011-11-07 19:00:33 +05301814 serial_in(up, UART_LSR));
1815 break;
1816 }
1817 udelay(1);
1818 }
1819}
1820
Rafael J. Wysockid39fe4e2014-12-13 00:41:36 +01001821#ifdef CONFIG_PM
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301822static void serial_omap_restore_context(struct uart_omap_port *up)
1823{
Govindraj.R94734742011-11-07 19:00:33 +05301824 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1825 serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
1826 else
1827 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
1828
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301829 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1830 serial_out(up, UART_EFR, UART_EFR_ECB);
1831 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1832 serial_out(up, UART_IER, 0x0);
1833 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301834 serial_out(up, UART_DLL, up->dll);
1835 serial_out(up, UART_DLM, up->dlh);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301836 serial_out(up, UART_LCR, 0x0); /* Operational mode */
1837 serial_out(up, UART_IER, up->ier);
1838 serial_out(up, UART_FCR, up->fcr);
1839 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1840 serial_out(up, UART_MCR, up->mcr);
1841 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
Govindraj.Rc538d202011-11-07 18:57:03 +05301842 serial_out(up, UART_OMAP_SCR, up->scr);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301843 serial_out(up, UART_EFR, up->efr);
1844 serial_out(up, UART_LCR, up->lcr);
Govindraj.R94734742011-11-07 19:00:33 +05301845 if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
1846 serial_omap_mdr1_errataset(up, up->mdr1);
1847 else
1848 serial_out(up, UART_OMAP_MDR1, up->mdr1);
Govindraj.Rf64ffda2013-07-05 18:25:59 +03001849 serial_out(up, UART_OMAP_WER, up->wer);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301850}
1851
Govindraj.Rfcdca752011-02-28 18:12:23 +05301852static int serial_omap_runtime_suspend(struct device *dev)
1853{
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301854 struct uart_omap_port *up = dev_get_drvdata(dev);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301855
Wei Yongjun7f253012013-06-05 10:04:49 +08001856 if (!up)
1857 return -EINVAL;
1858
Sourav Poddarddd85e22013-05-15 21:05:38 +05301859 /*
1860 * When using 'no_console_suspend', the console UART must not be
1861 * suspended. Since driver suspend is managed by runtime suspend,
1862 * preventing runtime suspend (by returning error) will keep device
1863 * active during suspend.
1864 */
1865 if (up->is_suspending && !console_suspend_enabled &&
1866 uart_console(&up->port))
1867 return -EBUSY;
1868
Felipe Balbie5b57c02012-08-23 13:32:42 +03001869 up->context_loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301870
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001871 serial_omap_enable_wakeup(up, true);
Govindraj.R62f3ec5f2011-10-13 14:11:09 +05301872
Rafael J. Wysocki2552d352020-02-12 00:04:31 +01001873 up->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
Govindraj.R2fd14962011-11-09 17:41:21 +05301874 schedule_work(&up->qos_work);
1875
Govindraj.Rfcdca752011-02-28 18:12:23 +05301876 return 0;
1877}
1878
1879static int serial_omap_runtime_resume(struct device *dev)
1880{
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301881 struct uart_omap_port *up = dev_get_drvdata(dev);
1882
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301883 int loss_cnt = serial_omap_get_context_loss_count(up);
Govindraj.Rec3bebc2011-10-11 19:11:27 +05301884
Tony Lindgrend758c9c2014-03-25 11:48:47 -07001885 serial_omap_enable_wakeup(up, false);
1886
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301887 if (loss_cnt < 0) {
Tony Lindgrena630fbf2013-06-10 07:39:09 -07001888 dev_dbg(dev, "serial_omap_get_context_loss_count failed : %d\n",
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301889 loss_cnt);
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301890 serial_omap_restore_context(up);
Shubhrajyoti D39aee512012-10-03 17:24:36 +05301891 } else if (up->context_loss_cnt != loss_cnt) {
1892 serial_omap_restore_context(up);
1893 }
Sourav Poddarac57e7f2012-09-18 17:05:54 +05301894 up->latency = up->calc_latency;
1895 schedule_work(&up->qos_work);
Govindraj.R9f9ac1e2011-11-07 18:56:12 +05301896
Govindraj.Rfcdca752011-02-28 18:12:23 +05301897 return 0;
1898}
1899#endif
1900
1901static const struct dev_pm_ops serial_omap_dev_pm_ops = {
1902 SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
1903 SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
1904 serial_omap_runtime_resume, NULL)
Sourav Poddarddd85e22013-05-15 21:05:38 +05301905 .prepare = serial_omap_prepare,
1906 .complete = serial_omap_complete,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301907};
1908
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301909#if defined(CONFIG_OF)
1910static const struct of_device_id omap_serial_of_match[] = {
1911 { .compatible = "ti,omap2-uart" },
1912 { .compatible = "ti,omap3-uart" },
1913 { .compatible = "ti,omap4-uart" },
1914 {},
1915};
1916MODULE_DEVICE_TABLE(of, omap_serial_of_match);
1917#endif
1918
Govindraj.Rb6126332010-09-27 20:20:49 +05301919static struct platform_driver serial_omap_driver = {
1920 .probe = serial_omap_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001921 .remove = serial_omap_remove,
Govindraj.Rb6126332010-09-27 20:20:49 +05301922 .driver = {
Jean Delvare1349ba02016-01-21 09:46:12 +01001923 .name = OMAP_SERIAL_DRIVER_NAME,
Govindraj.Rfcdca752011-02-28 18:12:23 +05301924 .pm = &serial_omap_dev_pm_ops,
Rajendra Nayakd92b0df2011-12-14 17:25:45 +05301925 .of_match_table = of_match_ptr(omap_serial_of_match),
Govindraj.Rb6126332010-09-27 20:20:49 +05301926 },
1927};
1928
1929static int __init serial_omap_init(void)
1930{
1931 int ret;
1932
1933 ret = uart_register_driver(&serial_omap_reg);
1934 if (ret != 0)
1935 return ret;
1936 ret = platform_driver_register(&serial_omap_driver);
1937 if (ret != 0)
1938 uart_unregister_driver(&serial_omap_reg);
1939 return ret;
1940}
1941
1942static void __exit serial_omap_exit(void)
1943{
1944 platform_driver_unregister(&serial_omap_driver);
1945 uart_unregister_driver(&serial_omap_reg);
1946}
1947
1948module_init(serial_omap_init);
1949module_exit(serial_omap_exit);
1950
1951MODULE_DESCRIPTION("OMAP High Speed UART driver");
1952MODULE_LICENSE("GPL");
1953MODULE_AUTHOR("Texas Instruments Inc");