819-uart-0010-tty-serial-lpuart-enable-wakeup-source-for-lpuart.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. From 4db59ee0d7224e0c8008534c9247480a83889034 Mon Sep 17 00:00:00 2001
  2. From: Fugang Duan <[email protected]>
  3. Date: Wed, 11 Sep 2019 17:01:45 +0800
  4. Subject: [PATCH] tty: serial: lpuart: enable wakeup source for lpuart
  5. When use lpuart with DMA mode as wake up source, it still switch to
  6. cpu mode in .suspend() that enable cpu interrupts RIE and ILIE as
  7. wakeup source. Enable the wakeup irq bits in .suspend_noirq() and
  8. disable the wakeup irq bits in .resume_noirq().
  9. For DMA mode, after system resume back, it needs to setup DMA again,
  10. if DMA setup is failed, it switchs to CPU mode. .resume() will share
  11. the HW setup code with .startup(), so abstract the same code to the
  12. api like lpuartx_hw_setup().
  13. Signed-off-by: Fugang Duan <[email protected]>
  14. ---
  15. drivers/tty/serial/fsl_lpuart.c | 285 ++++++++++++++++++++++++++++------------
  16. 1 file changed, 198 insertions(+), 87 deletions(-)
  17. --- a/drivers/tty/serial/fsl_lpuart.c
  18. +++ b/drivers/tty/serial/fsl_lpuart.c
  19. @@ -21,6 +21,7 @@
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_dma.h>
  23. +#include <linux/pinctrl/consumer.h>
  24. #include <linux/pm_domain.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/reset.h>
  27. @@ -1722,10 +1723,23 @@ static void lpuart_rx_dma_startup(struct
  28. }
  29. }
  30. +static void lpuart_hw_setup(struct lpuart_port *sport)
  31. +{
  32. + unsigned long flags;
  33. +
  34. + spin_lock_irqsave(&sport->port.lock, flags);
  35. +
  36. + lpuart_setup_watermark_enable(sport);
  37. +
  38. + lpuart_rx_dma_startup(sport);
  39. + lpuart_tx_dma_startup(sport);
  40. +
  41. + spin_unlock_irqrestore(&sport->port.lock, flags);
  42. +}
  43. +
  44. static int lpuart_startup(struct uart_port *port)
  45. {
  46. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  47. - unsigned long flags;
  48. unsigned char temp;
  49. /* determine FIFO size and enable FIFO mode */
  50. @@ -1738,14 +1752,7 @@ static int lpuart_startup(struct uart_po
  51. sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_RXSIZE_OFF) &
  52. UARTPFIFO_FIFOSIZE_MASK);
  53. - spin_lock_irqsave(&sport->port.lock, flags);
  54. -
  55. - lpuart_setup_watermark_enable(sport);
  56. -
  57. - lpuart_rx_dma_startup(sport);
  58. - lpuart_tx_dma_startup(sport);
  59. -
  60. - spin_unlock_irqrestore(&sport->port.lock, flags);
  61. + lpuart_hw_setup(sport);
  62. return 0;
  63. }
  64. @@ -1772,11 +1779,27 @@ static void lpuart32_configure(struct lp
  65. lpuart32_write(&sport->port, temp, UARTCTRL);
  66. }
  67. +static void lpuart32_hw_setup(struct lpuart_port *sport)
  68. +{
  69. + unsigned long flags;
  70. +
  71. + spin_lock_irqsave(&sport->port.lock, flags);
  72. +
  73. + lpuart32_hw_disable(sport);
  74. +
  75. + lpuart_rx_dma_startup(sport);
  76. + lpuart_tx_dma_startup(sport);
  77. +
  78. + lpuart32_setup_watermark_enable(sport);
  79. + lpuart32_configure(sport);
  80. +
  81. + spin_unlock_irqrestore(&sport->port.lock, flags);
  82. +}
  83. +
  84. static int lpuart32_startup(struct uart_port *port)
  85. {
  86. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  87. struct tty_port *tty_port = &sport->port.state->port;
  88. - unsigned long flags;
  89. unsigned long temp;
  90. int ret;
  91. @@ -1808,17 +1831,8 @@ static int lpuart32_startup(struct uart_
  92. sport->port.fifosize = sport->txfifo_size;
  93. }
  94. - spin_lock_irqsave(&sport->port.lock, flags);
  95. -
  96. - lpuart32_hw_disable(sport);
  97. -
  98. - lpuart_rx_dma_startup(sport);
  99. - lpuart_tx_dma_startup(sport);
  100. -
  101. - lpuart32_setup_watermark_enable(sport);
  102. - lpuart32_configure(sport);
  103. + lpuart32_hw_setup(sport);
  104. - spin_unlock_irqrestore(&sport->port.lock, flags);
  105. return 0;
  106. }
  107. @@ -2876,108 +2890,205 @@ static int lpuart_runtime_resume(struct
  108. return lpuart_enable_clks(sport);
  109. };
  110. -static int lpuart_suspend(struct device *dev)
  111. +static void serial_lpuart_enable_wakeup(struct lpuart_port *sport, bool on)
  112. {
  113. - struct lpuart_port *sport = dev_get_drvdata(dev);
  114. - unsigned long temp;
  115. - bool irq_wake;
  116. - int ret;
  117. -
  118. - ret = clk_prepare_enable(sport->ipg_clk);
  119. - if (ret)
  120. - return ret;
  121. + unsigned int val;
  122. if (lpuart_is_32(sport)) {
  123. - /* disable Rx/Tx and interrupts */
  124. - temp = lpuart32_read(&sport->port, UARTCTRL);
  125. - temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
  126. - lpuart32_write(&sport->port, temp, UARTCTRL);
  127. + val = lpuart32_read(&sport->port, UARTCTRL);
  128. + if (on)
  129. + val |= (UARTCTRL_RIE | UARTCTRL_ILIE);
  130. + else
  131. + val &= ~(UARTCTRL_RIE | UARTCTRL_ILIE);
  132. + lpuart32_write(&sport->port, val, UARTCTRL);
  133. } else {
  134. - /* disable Rx/Tx and interrupts */
  135. - temp = readb(sport->port.membase + UARTCR2);
  136. - temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
  137. - writeb(temp, sport->port.membase + UARTCR2);
  138. + val = readb(sport->port.membase + UARTCR2);
  139. + if (on)
  140. + val |= UARTCR2_RIE;
  141. + else
  142. + val &= ~UARTCR2_RIE;
  143. + writeb(val, sport->port.membase + UARTCR2);
  144. }
  145. +}
  146. - clk_disable_unprepare(sport->ipg_clk);
  147. +static bool lpuart_uport_is_active(struct lpuart_port *sport)
  148. +{
  149. + struct tty_port *port = &sport->port.state->port;
  150. + struct tty_struct *tty;
  151. + struct device *tty_dev;
  152. + int may_wake = 0;
  153. - uart_suspend_port(&lpuart_reg, &sport->port);
  154. + tty = tty_port_tty_get(port);
  155. + if (tty) {
  156. + tty_dev = tty->dev;
  157. + may_wake = device_may_wakeup(tty_dev);
  158. + tty_kref_put(tty);
  159. + }
  160. - /* uart_suspend_port() might set wakeup flag */
  161. - irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
  162. - if (sport->port.suspended && !irq_wake)
  163. - return 0;
  164. + if ((tty_port_initialized(port) && may_wake) ||
  165. + (!console_suspend_enabled && uart_console(&sport->port)))
  166. + return true;
  167. - if (sport->lpuart_dma_rx_use) {
  168. - /*
  169. - * EDMA driver during suspend will forcefully release any
  170. - * non-idle DMA channels. If port wakeup is enabled or if port
  171. - * is console port or 'no_console_suspend' is set the Rx DMA
  172. - * cannot resume as as expected, hence gracefully release the
  173. - * Rx DMA path before suspend and start Rx DMA path on resume.
  174. - */
  175. - if (irq_wake) {
  176. - lpuart_del_timer_sync(sport);
  177. - lpuart_dma_rx_free(&sport->port);
  178. - }
  179. + return false;
  180. +}
  181. - /* Disable Rx DMA to use UART port as wakeup source */
  182. +static int lpuart_suspend_noirq(struct device *dev)
  183. +{
  184. + struct lpuart_port *sport = dev_get_drvdata(dev);
  185. + bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
  186. +
  187. + if (lpuart_uport_is_active(sport))
  188. + serial_lpuart_enable_wakeup(sport, !!irq_wake);
  189. +
  190. + pinctrl_pm_select_sleep_state(dev);
  191. +
  192. + return 0;
  193. +}
  194. +
  195. +static int lpuart_resume_noirq(struct device *dev)
  196. +{
  197. + struct lpuart_port *sport = dev_get_drvdata(dev);
  198. + unsigned int val;
  199. +
  200. + pinctrl_pm_select_default_state(dev);
  201. +
  202. + if (lpuart_uport_is_active(sport)) {
  203. + serial_lpuart_enable_wakeup(sport, false);
  204. +
  205. + /* clear the wakeup flags */
  206. if (lpuart_is_32(sport)) {
  207. - temp = lpuart32_read(&sport->port, UARTBAUD);
  208. - lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
  209. - UARTBAUD);
  210. - } else {
  211. - writeb(readb(sport->port.membase + UARTCR5) &
  212. - ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
  213. + val = lpuart32_read(&sport->port, UARTSTAT);
  214. + lpuart32_write(&sport->port, val, UARTSTAT);
  215. }
  216. }
  217. - if (sport->lpuart_dma_tx_use) {
  218. - sport->dma_tx_in_progress = false;
  219. - dmaengine_terminate_all(sport->dma_tx_chan);
  220. - }
  221. -
  222. return 0;
  223. }
  224. -static int lpuart_resume(struct device *dev)
  225. +static int lpuart_suspend(struct device *dev)
  226. {
  227. struct lpuart_port *sport = dev_get_drvdata(dev);
  228. - bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
  229. - int ret;
  230. + unsigned long temp;
  231. + unsigned long flags;
  232. - ret = clk_prepare_enable(sport->ipg_clk);
  233. - if (ret)
  234. - return ret;
  235. + uart_suspend_port(&lpuart_reg, &sport->port);
  236. - if (lpuart_is_32(sport))
  237. - lpuart32_setup_watermark_enable(sport);
  238. - else
  239. - lpuart_setup_watermark_enable(sport);
  240. + if (lpuart_uport_is_active(sport)) {
  241. + spin_lock_irqsave(&sport->port.lock, flags);
  242. + if (lpuart_is_32(sport)) {
  243. + temp = lpuart32_read(&sport->port, UARTCTRL);
  244. + temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
  245. + lpuart32_write(&sport->port, temp, UARTCTRL);
  246. + } else {
  247. + temp = readb(sport->port.membase + UARTCR2);
  248. + temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
  249. + writeb(temp, sport->port.membase + UARTCR2);
  250. + }
  251. + spin_unlock_irqrestore(&sport->port.lock, flags);
  252. - if (sport->lpuart_dma_rx_use) {
  253. - if (irq_wake) {
  254. - if (!lpuart_start_rx_dma(sport))
  255. - rx_dma_timer_init(sport);
  256. - else
  257. - sport->lpuart_dma_rx_use = false;
  258. + if (sport->lpuart_dma_rx_use) {
  259. + /*
  260. + * EDMA driver during suspend will forcefully release any
  261. + * non-idle DMA channels. If port wakeup is enabled or if port
  262. + * is console port or 'no_console_suspend' is set the Rx DMA
  263. + * cannot resume as as expected, hence gracefully release the
  264. + * Rx DMA path before suspend and start Rx DMA path on resume.
  265. + */
  266. + lpuart_del_timer_sync(sport);
  267. + lpuart_dma_rx_free(&sport->port);
  268. +
  269. + /* Disable Rx DMA to use UART port as wakeup source */
  270. + spin_lock_irqsave(&sport->port.lock, flags);
  271. + if (lpuart_is_32(sport)) {
  272. + temp = lpuart32_read(&sport->port, UARTBAUD);
  273. + lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
  274. + UARTBAUD);
  275. + } else {
  276. + writeb(readb(sport->port.membase + UARTCR5) &
  277. + ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
  278. + }
  279. + spin_unlock_irqrestore(&sport->port.lock, flags);
  280. + }
  281. +
  282. + if (sport->lpuart_dma_tx_use) {
  283. + spin_lock_irqsave(&sport->port.lock, flags);
  284. + if (lpuart_is_32(sport)) {
  285. + temp = lpuart32_read(&sport->port, UARTBAUD);
  286. + temp &= ~UARTBAUD_TDMAE;
  287. + lpuart32_write(&sport->port, temp, UARTBAUD);
  288. + } else {
  289. + temp = readb(sport->port.membase + UARTCR5);
  290. + temp &= ~UARTCR5_TDMAS;
  291. + writeb(temp, sport->port.membase + UARTCR5);
  292. + }
  293. + spin_unlock_irqrestore(&sport->port.lock, flags);
  294. + sport->dma_tx_in_progress = false;
  295. + dmaengine_terminate_all(sport->dma_tx_chan);
  296. }
  297. + } else if (pm_runtime_active(sport->port.dev)) {
  298. + lpuart_disable_clks(sport);
  299. + pm_runtime_disable(sport->port.dev);
  300. + pm_runtime_set_suspended(sport->port.dev);
  301. }
  302. - lpuart_tx_dma_startup(sport);
  303. + return 0;
  304. +}
  305. - if (lpuart_is_32(sport))
  306. - lpuart32_configure(sport);
  307. +static void lpuart_console_fixup(struct lpuart_port *sport)
  308. +{
  309. + struct tty_port *port = &sport->port.state->port;
  310. + struct uart_port *uport = &sport->port;
  311. + struct ktermios termios;
  312. - clk_disable_unprepare(sport->ipg_clk);
  313. + /* i.MX7ULP enter VLLS mode that lpuart module power off and registers
  314. + * all lost no matter the port is wakeup source.
  315. + * For console port, console baud rate setting lost and print messy
  316. + * log when enable the console port as wakeup source. To avoid the
  317. + * issue happen, user should not enable uart port as wakeup source
  318. + * in VLLS mode, or restore console setting here.
  319. + */
  320. + if (is_imx7ulp_lpuart(sport) && lpuart_uport_is_active(sport) &&
  321. + console_suspend_enabled && uart_console(&sport->port)) {
  322. +
  323. + mutex_lock(&port->mutex);
  324. + memset(&termios, 0, sizeof(struct ktermios));
  325. + termios.c_cflag = uport->cons->cflag;
  326. + if (port->tty && termios.c_cflag == 0)
  327. + termios = port->tty->termios;
  328. + uport->ops->set_termios(uport, &termios, NULL);
  329. + mutex_unlock(&port->mutex);
  330. + }
  331. +}
  332. +
  333. +static int lpuart_resume(struct device *dev)
  334. +{
  335. + struct lpuart_port *sport = dev_get_drvdata(dev);
  336. + int ret;
  337. + if (lpuart_uport_is_active(sport)) {
  338. + if (lpuart_is_32(sport))
  339. + lpuart32_hw_setup(sport);
  340. + else
  341. + lpuart_hw_setup(sport);
  342. + } else if (pm_runtime_active(sport->port.dev)) {
  343. + ret = lpuart_enable_clks(sport);
  344. + if (ret)
  345. + return ret;
  346. + pm_runtime_set_active(sport->port.dev);
  347. + pm_runtime_enable(sport->port.dev);
  348. + }
  349. +
  350. + lpuart_console_fixup(sport);
  351. uart_resume_port(&lpuart_reg, &sport->port);
  352. return 0;
  353. }
  354. +
  355. static const struct dev_pm_ops lpuart_pm_ops = {
  356. SET_RUNTIME_PM_OPS(lpuart_runtime_suspend,
  357. lpuart_runtime_resume, NULL)
  358. + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(lpuart_suspend_noirq,
  359. + lpuart_resume_noirq)
  360. SET_SYSTEM_SLEEP_PM_OPS(lpuart_suspend, lpuart_resume)
  361. };
  362. #define SERIAL_LPUART_PM_OPS (&lpuart_pm_ops)