Skip to content

Commit c49436b

Browse files
Tim Krygergregkh
authored andcommitted
serial: 8250_dw: Improve unwritable LCR workaround
When configured with UART_16550_COMPATIBLE=NO or in versions prior to the introduction of this option, the Designware UART will ignore writes to the LCR if the UART is busy. The current workaround saves a copy of the last written LCR and re-writes it in the ISR for a special interrupt that is raised when a write was ignored. Unfortunately, interrupts are typically disabled prior to performing a sequence of register writes that include the LCR so the point at which the retry occurs is too late. An example is serial8250_do_set_termios() where an ignored LCR write results in the baud divisor not being set and instead a garbage character is sent out the transmitter. Furthermore, since serial_port_out() offers no way to indicate failure, a serious effort must be made to ensure that the LCR is actually updated before returning back to the caller. This is difficult, however, as a UART that was busy during the first attempt is likely to still be busy when a subsequent attempt is made unless some extra action is taken. This updated workaround reads back the LCR after each write to confirm that the new value was accepted by the hardware. Should the hardware ignore a write, the TX/RX FIFOs are cleared and the receive buffer read before attempting to rewrite the LCR out of the hope that doing so will force the UART into an idle state. While this may seem unnecessarily aggressive, writes to the LCR are used to change the baud rate, parity, stop bit, or data length so the data that may be lost is likely not important. Admittedly, this is far from ideal but it seems to be the best that can be done given the hardware limitations. Lastly, the revised workaround doesn't touch the LCR in the ISR, so it avoids the possibility of a "serial8250: too much work for irq" lock up. This problem is rare in real situations but can be reproduced easily by wiring up two UARTs and running the following commands. # stty -F /dev/ttyS1 echo # stty -F /dev/ttyS2 echo # cat /dev/ttyS1 & [1] 375 # echo asdf > /dev/ttyS1 asdf [ 27.700000] serial8250: too much work for irq96 [ 27.700000] serial8250: too much work for irq96 [ 27.710000] serial8250: too much work for irq96 [ 27.710000] serial8250: too much work for irq96 [ 27.720000] serial8250: too much work for irq96 [ 27.720000] serial8250: too much work for irq96 [ 27.730000] serial8250: too much work for irq96 [ 27.730000] serial8250: too much work for irq96 [ 27.740000] serial8250: too much work for irq96 Signed-off-by: Tim Kryger <[email protected]> Reviewed-by: Matt Porter <[email protected]> Reviewed-by: Markus Mayer <[email protected]> Reviewed-by: Heikki Krogerus <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent af8c5b8 commit c49436b

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

drivers/tty/serial/8250/8250_dw.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757

5858
struct dw8250_data {
5959
u8 usr_reg;
60-
int last_lcr;
6160
int last_mcr;
6261
int line;
6362
struct clk *clk;
@@ -77,17 +76,33 @@ static inline int dw8250_modify_msr(struct uart_port *p, int offset, int value)
7776
return value;
7877
}
7978

79+
static void dw8250_force_idle(struct uart_port *p)
80+
{
81+
serial8250_clear_and_reinit_fifos(container_of
82+
(p, struct uart_8250_port, port));
83+
(void)p->serial_in(p, UART_RX);
84+
}
85+
8086
static void dw8250_serial_out(struct uart_port *p, int offset, int value)
8187
{
8288
struct dw8250_data *d = p->private_data;
8389

84-
if (offset == UART_LCR)
85-
d->last_lcr = value;
86-
8790
if (offset == UART_MCR)
8891
d->last_mcr = value;
8992

9093
writeb(value, p->membase + (offset << p->regshift));
94+
95+
/* Make sure LCR write wasn't ignored */
96+
if (offset == UART_LCR) {
97+
int tries = 1000;
98+
while (tries--) {
99+
if (value == p->serial_in(p, UART_LCR))
100+
return;
101+
dw8250_force_idle(p);
102+
writeb(value, p->membase + (UART_LCR << p->regshift));
103+
}
104+
dev_err(p->dev, "Couldn't set LCR to %d\n", value);
105+
}
91106
}
92107

93108
static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
@@ -108,13 +123,22 @@ static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
108123
{
109124
struct dw8250_data *d = p->private_data;
110125

111-
if (offset == UART_LCR)
112-
d->last_lcr = value;
113-
114126
if (offset == UART_MCR)
115127
d->last_mcr = value;
116128

117129
writel(value, p->membase + (offset << p->regshift));
130+
131+
/* Make sure LCR write wasn't ignored */
132+
if (offset == UART_LCR) {
133+
int tries = 1000;
134+
while (tries--) {
135+
if (value == p->serial_in(p, UART_LCR))
136+
return;
137+
dw8250_force_idle(p);
138+
writel(value, p->membase + (UART_LCR << p->regshift));
139+
}
140+
dev_err(p->dev, "Couldn't set LCR to %d\n", value);
141+
}
118142
}
119143

120144
static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
@@ -132,9 +156,8 @@ static int dw8250_handle_irq(struct uart_port *p)
132156
if (serial8250_handle_irq(p, iir)) {
133157
return 1;
134158
} else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
135-
/* Clear the USR and write the LCR again. */
159+
/* Clear the USR */
136160
(void)p->serial_in(p, d->usr_reg);
137-
p->serial_out(p, UART_LCR, d->last_lcr);
138161

139162
return 1;
140163
}

0 commit comments

Comments
 (0)