1 From 4db59ee0d7224e0c8008534c9247480a83889034 Mon Sep 17 00:00:00 2001
2 From: Fugang Duan <fugang.duan@nxp.com>
3 Date: Wed, 11 Sep 2019 17:01:45 +0800
4 Subject: [PATCH] tty: serial: lpuart: enable wakeup source for lpuart
6 When use lpuart with DMA mode as wake up source, it still switch to
7 cpu mode in .suspend() that enable cpu interrupts RIE and ILIE as
8 wakeup source. Enable the wakeup irq bits in .suspend_noirq() and
9 disable the wakeup irq bits in .resume_noirq().
11 For DMA mode, after system resume back, it needs to setup DMA again,
12 if DMA setup is failed, it switchs to CPU mode. .resume() will share
13 the HW setup code with .startup(), so abstract the same code to the
14 api like lpuartx_hw_setup().
16 Signed-off-by: Fugang Duan <fugang.duan@nxp.com>
18 drivers/tty/serial/fsl_lpuart.c | 285 ++++++++++++++++++++++++++++------------
19 1 file changed, 198 insertions(+), 87 deletions(-)
21 --- a/drivers/tty/serial/fsl_lpuart.c
22 +++ b/drivers/tty/serial/fsl_lpuart.c
25 #include <linux/of_device.h>
26 #include <linux/of_dma.h>
27 +#include <linux/pinctrl/consumer.h>
28 #include <linux/pm_domain.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/reset.h>
31 @@ -1722,10 +1723,23 @@ static void lpuart_rx_dma_startup(struct
35 +static void lpuart_hw_setup(struct lpuart_port *sport)
37 + unsigned long flags;
39 + spin_lock_irqsave(&sport->port.lock, flags);
41 + lpuart_setup_watermark_enable(sport);
43 + lpuart_rx_dma_startup(sport);
44 + lpuart_tx_dma_startup(sport);
46 + spin_unlock_irqrestore(&sport->port.lock, flags);
49 static int lpuart_startup(struct uart_port *port)
51 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
52 - unsigned long flags;
55 /* determine FIFO size and enable FIFO mode */
56 @@ -1738,14 +1752,7 @@ static int lpuart_startup(struct uart_po
57 sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_RXSIZE_OFF) &
58 UARTPFIFO_FIFOSIZE_MASK);
60 - spin_lock_irqsave(&sport->port.lock, flags);
62 - lpuart_setup_watermark_enable(sport);
64 - lpuart_rx_dma_startup(sport);
65 - lpuart_tx_dma_startup(sport);
67 - spin_unlock_irqrestore(&sport->port.lock, flags);
68 + lpuart_hw_setup(sport);
72 @@ -1772,11 +1779,27 @@ static void lpuart32_configure(struct lp
73 lpuart32_write(&sport->port, temp, UARTCTRL);
76 +static void lpuart32_hw_setup(struct lpuart_port *sport)
78 + unsigned long flags;
80 + spin_lock_irqsave(&sport->port.lock, flags);
82 + lpuart32_hw_disable(sport);
84 + lpuart_rx_dma_startup(sport);
85 + lpuart_tx_dma_startup(sport);
87 + lpuart32_setup_watermark_enable(sport);
88 + lpuart32_configure(sport);
90 + spin_unlock_irqrestore(&sport->port.lock, flags);
93 static int lpuart32_startup(struct uart_port *port)
95 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
96 struct tty_port *tty_port = &sport->port.state->port;
97 - unsigned long flags;
101 @@ -1808,17 +1831,8 @@ static int lpuart32_startup(struct uart_
102 sport->port.fifosize = sport->txfifo_size;
105 - spin_lock_irqsave(&sport->port.lock, flags);
107 - lpuart32_hw_disable(sport);
109 - lpuart_rx_dma_startup(sport);
110 - lpuart_tx_dma_startup(sport);
112 - lpuart32_setup_watermark_enable(sport);
113 - lpuart32_configure(sport);
114 + lpuart32_hw_setup(sport);
116 - spin_unlock_irqrestore(&sport->port.lock, flags);
120 @@ -2876,108 +2890,205 @@ static int lpuart_runtime_resume(struct
121 return lpuart_enable_clks(sport);
124 -static int lpuart_suspend(struct device *dev)
125 +static void serial_lpuart_enable_wakeup(struct lpuart_port *sport, bool on)
127 - struct lpuart_port *sport = dev_get_drvdata(dev);
128 - unsigned long temp;
132 - ret = clk_prepare_enable(sport->ipg_clk);
137 if (lpuart_is_32(sport)) {
138 - /* disable Rx/Tx and interrupts */
139 - temp = lpuart32_read(&sport->port, UARTCTRL);
140 - temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
141 - lpuart32_write(&sport->port, temp, UARTCTRL);
142 + val = lpuart32_read(&sport->port, UARTCTRL);
144 + val |= (UARTCTRL_RIE | UARTCTRL_ILIE);
146 + val &= ~(UARTCTRL_RIE | UARTCTRL_ILIE);
147 + lpuart32_write(&sport->port, val, UARTCTRL);
149 - /* disable Rx/Tx and interrupts */
150 - temp = readb(sport->port.membase + UARTCR2);
151 - temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
152 - writeb(temp, sport->port.membase + UARTCR2);
153 + val = readb(sport->port.membase + UARTCR2);
155 + val |= UARTCR2_RIE;
157 + val &= ~UARTCR2_RIE;
158 + writeb(val, sport->port.membase + UARTCR2);
162 - clk_disable_unprepare(sport->ipg_clk);
163 +static bool lpuart_uport_is_active(struct lpuart_port *sport)
165 + struct tty_port *port = &sport->port.state->port;
166 + struct tty_struct *tty;
167 + struct device *tty_dev;
170 - uart_suspend_port(&lpuart_reg, &sport->port);
171 + tty = tty_port_tty_get(port);
173 + tty_dev = tty->dev;
174 + may_wake = device_may_wakeup(tty_dev);
178 - /* uart_suspend_port() might set wakeup flag */
179 - irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
180 - if (sport->port.suspended && !irq_wake)
182 + if ((tty_port_initialized(port) && may_wake) ||
183 + (!console_suspend_enabled && uart_console(&sport->port)))
186 - if (sport->lpuart_dma_rx_use) {
188 - * EDMA driver during suspend will forcefully release any
189 - * non-idle DMA channels. If port wakeup is enabled or if port
190 - * is console port or 'no_console_suspend' is set the Rx DMA
191 - * cannot resume as as expected, hence gracefully release the
192 - * Rx DMA path before suspend and start Rx DMA path on resume.
195 - lpuart_del_timer_sync(sport);
196 - lpuart_dma_rx_free(&sport->port);
201 - /* Disable Rx DMA to use UART port as wakeup source */
202 +static int lpuart_suspend_noirq(struct device *dev)
204 + struct lpuart_port *sport = dev_get_drvdata(dev);
205 + bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
207 + if (lpuart_uport_is_active(sport))
208 + serial_lpuart_enable_wakeup(sport, !!irq_wake);
210 + pinctrl_pm_select_sleep_state(dev);
215 +static int lpuart_resume_noirq(struct device *dev)
217 + struct lpuart_port *sport = dev_get_drvdata(dev);
220 + pinctrl_pm_select_default_state(dev);
222 + if (lpuart_uport_is_active(sport)) {
223 + serial_lpuart_enable_wakeup(sport, false);
225 + /* clear the wakeup flags */
226 if (lpuart_is_32(sport)) {
227 - temp = lpuart32_read(&sport->port, UARTBAUD);
228 - lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
231 - writeb(readb(sport->port.membase + UARTCR5) &
232 - ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
233 + val = lpuart32_read(&sport->port, UARTSTAT);
234 + lpuart32_write(&sport->port, val, UARTSTAT);
238 - if (sport->lpuart_dma_tx_use) {
239 - sport->dma_tx_in_progress = false;
240 - dmaengine_terminate_all(sport->dma_tx_chan);
246 -static int lpuart_resume(struct device *dev)
247 +static int lpuart_suspend(struct device *dev)
249 struct lpuart_port *sport = dev_get_drvdata(dev);
250 - bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
252 + unsigned long temp;
253 + unsigned long flags;
255 - ret = clk_prepare_enable(sport->ipg_clk);
258 + uart_suspend_port(&lpuart_reg, &sport->port);
260 - if (lpuart_is_32(sport))
261 - lpuart32_setup_watermark_enable(sport);
263 - lpuart_setup_watermark_enable(sport);
264 + if (lpuart_uport_is_active(sport)) {
265 + spin_lock_irqsave(&sport->port.lock, flags);
266 + if (lpuart_is_32(sport)) {
267 + temp = lpuart32_read(&sport->port, UARTCTRL);
268 + temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
269 + lpuart32_write(&sport->port, temp, UARTCTRL);
271 + temp = readb(sport->port.membase + UARTCR2);
272 + temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
273 + writeb(temp, sport->port.membase + UARTCR2);
275 + spin_unlock_irqrestore(&sport->port.lock, flags);
277 - if (sport->lpuart_dma_rx_use) {
279 - if (!lpuart_start_rx_dma(sport))
280 - rx_dma_timer_init(sport);
282 - sport->lpuart_dma_rx_use = false;
283 + if (sport->lpuart_dma_rx_use) {
285 + * EDMA driver during suspend will forcefully release any
286 + * non-idle DMA channels. If port wakeup is enabled or if port
287 + * is console port or 'no_console_suspend' is set the Rx DMA
288 + * cannot resume as as expected, hence gracefully release the
289 + * Rx DMA path before suspend and start Rx DMA path on resume.
291 + lpuart_del_timer_sync(sport);
292 + lpuart_dma_rx_free(&sport->port);
294 + /* Disable Rx DMA to use UART port as wakeup source */
295 + spin_lock_irqsave(&sport->port.lock, flags);
296 + if (lpuart_is_32(sport)) {
297 + temp = lpuart32_read(&sport->port, UARTBAUD);
298 + lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
301 + writeb(readb(sport->port.membase + UARTCR5) &
302 + ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
304 + spin_unlock_irqrestore(&sport->port.lock, flags);
307 + if (sport->lpuart_dma_tx_use) {
308 + spin_lock_irqsave(&sport->port.lock, flags);
309 + if (lpuart_is_32(sport)) {
310 + temp = lpuart32_read(&sport->port, UARTBAUD);
311 + temp &= ~UARTBAUD_TDMAE;
312 + lpuart32_write(&sport->port, temp, UARTBAUD);
314 + temp = readb(sport->port.membase + UARTCR5);
315 + temp &= ~UARTCR5_TDMAS;
316 + writeb(temp, sport->port.membase + UARTCR5);
318 + spin_unlock_irqrestore(&sport->port.lock, flags);
319 + sport->dma_tx_in_progress = false;
320 + dmaengine_terminate_all(sport->dma_tx_chan);
322 + } else if (pm_runtime_active(sport->port.dev)) {
323 + lpuart_disable_clks(sport);
324 + pm_runtime_disable(sport->port.dev);
325 + pm_runtime_set_suspended(sport->port.dev);
328 - lpuart_tx_dma_startup(sport);
332 - if (lpuart_is_32(sport))
333 - lpuart32_configure(sport);
334 +static void lpuart_console_fixup(struct lpuart_port *sport)
336 + struct tty_port *port = &sport->port.state->port;
337 + struct uart_port *uport = &sport->port;
338 + struct ktermios termios;
340 - clk_disable_unprepare(sport->ipg_clk);
341 + /* i.MX7ULP enter VLLS mode that lpuart module power off and registers
342 + * all lost no matter the port is wakeup source.
343 + * For console port, console baud rate setting lost and print messy
344 + * log when enable the console port as wakeup source. To avoid the
345 + * issue happen, user should not enable uart port as wakeup source
346 + * in VLLS mode, or restore console setting here.
348 + if (is_imx7ulp_lpuart(sport) && lpuart_uport_is_active(sport) &&
349 + console_suspend_enabled && uart_console(&sport->port)) {
351 + mutex_lock(&port->mutex);
352 + memset(&termios, 0, sizeof(struct ktermios));
353 + termios.c_cflag = uport->cons->cflag;
354 + if (port->tty && termios.c_cflag == 0)
355 + termios = port->tty->termios;
356 + uport->ops->set_termios(uport, &termios, NULL);
357 + mutex_unlock(&port->mutex);
361 +static int lpuart_resume(struct device *dev)
363 + struct lpuart_port *sport = dev_get_drvdata(dev);
366 + if (lpuart_uport_is_active(sport)) {
367 + if (lpuart_is_32(sport))
368 + lpuart32_hw_setup(sport);
370 + lpuart_hw_setup(sport);
371 + } else if (pm_runtime_active(sport->port.dev)) {
372 + ret = lpuart_enable_clks(sport);
375 + pm_runtime_set_active(sport->port.dev);
376 + pm_runtime_enable(sport->port.dev);
379 + lpuart_console_fixup(sport);
380 uart_resume_port(&lpuart_reg, &sport->port);
385 static const struct dev_pm_ops lpuart_pm_ops = {
386 SET_RUNTIME_PM_OPS(lpuart_runtime_suspend,
387 lpuart_runtime_resume, NULL)
388 + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(lpuart_suspend_noirq,
389 + lpuart_resume_noirq)
390 SET_SYSTEM_SLEEP_PM_OPS(lpuart_suspend, lpuart_resume)
392 #define SERIAL_LPUART_PM_OPS (&lpuart_pm_ops)