blob: df8a0c8b8b29b076af2982d31b355f614d5776c6 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Uwe Kleine-Königf890cef2015-02-24 11:17:08 +01003 * Driver for Motorola/Freescale IMX serial ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Uwe Kleine-Königf890cef2015-02-24 11:17:08 +01005 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Uwe Kleine-Königf890cef2015-02-24 11:17:08 +01007 * Author: Sascha Hauer <sascha@saschahauer.de>
8 * Copyright (C) 2004 Pengutronix
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/ioport.h>
13#include <linux/init.h>
14#include <linux/console.h>
15#include <linux/sysrq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010016#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/tty.h>
18#include <linux/tty_flip.h>
19#include <linux/serial_core.h>
20#include <linux/serial.h>
Sascha Hauer38a41fd2008-07-05 10:02:46 +020021#include <linux/clk.h>
Fabian Godehardtb6e49132009-06-11 14:53:18 +010022#include <linux/delay.h>
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +020023#include <linux/ktime.h>
Anson Huangfcfed1be2018-09-05 09:24:27 +080024#include <linux/pinctrl/consumer.h>
Oskar Schirmer534fca02009-06-11 14:52:23 +010025#include <linux/rational.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Shawn Guo22698aa2011-06-25 02:04:34 +080027#include <linux/of.h>
28#include <linux/of_device.h>
Sachin Kamate32a9f82013-01-07 10:25:03 +053029#include <linux/io.h>
Huang Shijieb4cdc8f2013-07-08 17:14:18 +080030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/irq.h>
Huang Shijieb4cdc8f2013-07-08 17:14:18 +080033#include <linux/platform_data/dma-imx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Uwe Kleine-König58362d52015-12-13 11:30:03 +010035#include "serial_mctrl_gpio.h"
36
Sascha Hauerff4bfb22007-04-26 08:26:13 +010037/* Register definitions */
38#define URXD0 0x0 /* Receiver Register */
39#define URTX0 0x40 /* Transmitter Register */
40#define UCR1 0x80 /* Control Register 1 */
41#define UCR2 0x84 /* Control Register 2 */
42#define UCR3 0x88 /* Control Register 3 */
43#define UCR4 0x8c /* Control Register 4 */
44#define UFCR 0x90 /* FIFO Control Register */
45#define USR1 0x94 /* Status Register 1 */
46#define USR2 0x98 /* Status Register 2 */
47#define UESC 0x9c /* Escape Character Register */
48#define UTIM 0xa0 /* Escape Timer Register */
49#define UBIR 0xa4 /* BRM Incremental Register */
50#define UBMR 0xa8 /* BRM Modulator Register */
51#define UBRC 0xac /* Baud Rate Count Register */
Shawn Guofe6b5402011-06-25 02:04:33 +080052#define IMX21_ONEMS 0xb0 /* One Millisecond register */
53#define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
54#define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
Sascha Hauerff4bfb22007-04-26 08:26:13 +010055
56/* UART Control Register Bit Fields.*/
Jiada Wang55d86932014-12-09 18:11:22 +090057#define URXD_DUMMY_READ (1<<16)
Sachin Kamat82313e62013-01-07 10:25:02 +053058#define URXD_CHARRDY (1<<15)
59#define URXD_ERR (1<<14)
60#define URXD_OVRRUN (1<<13)
61#define URXD_FRMERR (1<<12)
62#define URXD_BRK (1<<11)
63#define URXD_PRERR (1<<10)
Dirk Behme26c47412014-09-03 12:33:53 +010064#define URXD_RX_DATA (0xFF<<0)
Sachin Kamat82313e62013-01-07 10:25:02 +053065#define UCR1_ADEN (1<<15) /* Auto detect interrupt */
66#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
67#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
68#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
Huang Shijieb4cdc8f2013-07-08 17:14:18 +080069#define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
Sachin Kamat82313e62013-01-07 10:25:02 +053070#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
Uwe Kleine-König302e8dc2018-02-27 22:44:55 +010071#define UCR1_RXDMAEN (1<<8) /* Recv ready DMA enable */
Sachin Kamat82313e62013-01-07 10:25:02 +053072#define UCR1_IREN (1<<7) /* Infrared interface enable */
73#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
74#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
75#define UCR1_SNDBRK (1<<4) /* Send break */
Uwe Kleine-König302e8dc2018-02-27 22:44:55 +010076#define UCR1_TXDMAEN (1<<3) /* Transmitter ready DMA enable */
Sachin Kamat82313e62013-01-07 10:25:02 +053077#define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
Huang Shijieb4cdc8f2013-07-08 17:14:18 +080078#define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
Sachin Kamat82313e62013-01-07 10:25:02 +053079#define UCR1_DOZE (1<<1) /* Doze */
80#define UCR1_UARTEN (1<<0) /* UART enabled */
81#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
82#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
83#define UCR2_CTSC (1<<13) /* CTS pin control */
84#define UCR2_CTS (1<<12) /* Clear to send */
85#define UCR2_ESCEN (1<<11) /* Escape enable */
86#define UCR2_PREN (1<<8) /* Parity enable */
87#define UCR2_PROE (1<<7) /* Parity odd/even */
88#define UCR2_STPB (1<<6) /* Stop */
89#define UCR2_WS (1<<5) /* Word size */
90#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
91#define UCR2_ATEN (1<<3) /* Aging Timer Enable */
92#define UCR2_TXEN (1<<2) /* Transmitter enabled */
93#define UCR2_RXEN (1<<1) /* Receiver enabled */
94#define UCR2_SRST (1<<0) /* SW reset */
95#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
96#define UCR3_PARERREN (1<<12) /* Parity enable */
97#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
98#define UCR3_DSR (1<<10) /* Data set ready */
99#define UCR3_DCD (1<<9) /* Data carrier detect */
100#define UCR3_RI (1<<8) /* Ring indicator */
Fabio Estevamb38cb7d2014-05-14 15:55:03 -0300101#define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */
Sachin Kamat82313e62013-01-07 10:25:02 +0530102#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
103#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
104#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
Uwe Kleine-König27e16502016-03-24 14:24:25 +0100105#define UCR3_DTRDEN (1<<3) /* Data Terminal Ready Delta Enable. */
Sachin Kamat82313e62013-01-07 10:25:02 +0530106#define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
107#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
108#define UCR3_BPEN (1<<0) /* Preset registers enable */
109#define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
110#define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
111#define UCR4_INVR (1<<9) /* Inverted infrared reception */
112#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
113#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
114#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800115#define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
Sachin Kamat82313e62013-01-07 10:25:02 +0530116#define UCR4_IRSC (1<<5) /* IR special case */
117#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
118#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
119#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
120#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
121#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
122#define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
123#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
124#define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
125#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
126#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
127#define USR1_RTSS (1<<14) /* RTS pin status */
128#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
129#define USR1_RTSD (1<<12) /* RTS delta */
130#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
131#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
132#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
Lucas Stach86a04ba2015-09-04 17:52:38 +0200133#define USR1_AGTIM (1<<8) /* Ageing timer interrupt flag */
Uwe Kleine-König27e16502016-03-24 14:24:25 +0100134#define USR1_DTRD (1<<7) /* DTR Delta */
Sachin Kamat82313e62013-01-07 10:25:02 +0530135#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
136#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
137#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
138#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
139#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
140#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
141#define USR2_IDLE (1<<12) /* Idle condition */
Uwe Kleine-König90ebc482015-10-18 21:34:46 +0200142#define USR2_RIDELT (1<<10) /* Ring Interrupt Delta */
143#define USR2_RIIN (1<<9) /* Ring Indicator Input */
Sachin Kamat82313e62013-01-07 10:25:02 +0530144#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
145#define USR2_WAKE (1<<7) /* Wake */
Uwe Kleine-König90ebc482015-10-18 21:34:46 +0200146#define USR2_DCDIN (1<<5) /* Data Carrier Detect Input */
Sachin Kamat82313e62013-01-07 10:25:02 +0530147#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
148#define USR2_TXDC (1<<3) /* Transmitter complete */
149#define USR2_BRCD (1<<2) /* Break condition */
150#define USR2_ORE (1<<1) /* Overrun error */
151#define USR2_RDR (1<<0) /* Recv data ready */
152#define UTS_FRCPERR (1<<13) /* Force parity error */
153#define UTS_LOOP (1<<12) /* Loop tx and rx */
154#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
155#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
156#define UTS_TXFULL (1<<4) /* TxFIFO full */
157#define UTS_RXFULL (1<<3) /* RxFIFO full */
158#define UTS_SOFTRST (1<<0) /* Software reset */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* We've been assigned a range on the "Low-density serial ports" major */
Sachin Kamat82313e62013-01-07 10:25:02 +0530161#define SERIAL_IMX_MAJOR 207
162#define MINOR_START 16
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200163#define DEV_NAME "ttymxc"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * This determines how often we check the modem status signals
167 * for any change. They generally aren't connected to an IRQ
168 * so we have to poll them. We also check immediately before
169 * filling the TX fifo incase CTS has been dropped.
170 */
171#define MCTRL_TIMEOUT (250*HZ/1000)
172
173#define DRIVER_NAME "IMX-uart"
174
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200175#define UART_NR 8
176
Uwe Kleine-Königf95661b2015-02-24 11:17:09 +0100177/* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
Shawn Guofe6b5402011-06-25 02:04:33 +0800178enum imx_uart_type {
179 IMX1_UART,
180 IMX21_UART,
Martyn Welch1c06bde62016-09-01 11:30:46 +0200181 IMX53_UART,
Huang Shijiea496e622013-07-08 17:14:17 +0800182 IMX6Q_UART,
Shawn Guofe6b5402011-06-25 02:04:33 +0800183};
184
185/* device type dependent stuff */
186struct imx_uart_data {
187 unsigned uts_reg;
188 enum imx_uart_type devtype;
189};
190
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200191enum imx_tx_state {
192 OFF,
193 WAIT_AFTER_RTS,
194 SEND,
195 WAIT_AFTER_SEND,
196};
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198struct imx_port {
199 struct uart_port port;
200 struct timer_list timer;
201 unsigned int old_status;
Daniel Glöckner26bbb3f2009-06-11 14:36:29 +0100202 unsigned int have_rtscts:1;
Fabio Estevam7b7e8e82017-01-07 19:29:13 -0200203 unsigned int have_rtsgpio:1;
Huang Shijie20ff2fe2013-05-30 14:07:12 +0800204 unsigned int dte_mode:1;
George Hilliard5a08a482020-02-26 16:23:19 -0600205 unsigned int inverted_tx:1;
206 unsigned int inverted_rx:1;
Sascha Hauer3a9465f2012-03-07 09:31:43 +0100207 struct clk *clk_ipg;
208 struct clk *clk_per;
Uwe Kleine-König7d0b0662012-05-21 21:57:39 +0200209 const struct imx_uart_data *devdata;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800210
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100211 struct mctrl_gpios *gpios;
212
Uwe Kleine-König3a0ab622018-03-02 11:07:20 +0100213 /* shadow registers */
214 unsigned int ucr1;
215 unsigned int ucr2;
216 unsigned int ucr3;
217 unsigned int ucr4;
218 unsigned int ufcr;
219
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800220 /* DMA fields */
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800221 unsigned int dma_is_enabled:1;
222 unsigned int dma_is_rxing:1;
223 unsigned int dma_is_txing:1;
224 struct dma_chan *dma_chan_rx, *dma_chan_tx;
225 struct scatterlist rx_sgl, tx_sgl[2];
226 void *rx_buf;
Nandor Han9d297232016-08-08 15:38:27 +0300227 struct circ_buf rx_ring;
Fabien Lahouderedb0a1962021-04-30 19:50:37 +0200228 unsigned int rx_buf_size;
229 unsigned int rx_period_length;
Nandor Han9d297232016-08-08 15:38:27 +0300230 unsigned int rx_periods;
231 dma_cookie_t rx_cookie;
Huang Shijie7cb92fd2013-10-15 15:23:40 +0800232 unsigned int tx_bytes;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800233 unsigned int dma_tx_nents;
Shenwei Wang90bb6bd2015-07-30 10:32:36 -0500234 unsigned int saved_reg[10];
Eduardo Valentinc868cbb2015-08-11 10:21:23 -0700235 bool context_saved;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200236
237 enum imx_tx_state tx_state;
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200238 struct hrtimer trigger_start_tx;
239 struct hrtimer trigger_stop_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
Dirk Behme0ad5a812011-12-22 09:57:52 +0100242struct imx_port_ucrs {
243 unsigned int ucr1;
244 unsigned int ucr2;
245 unsigned int ucr3;
246};
247
Shawn Guofe6b5402011-06-25 02:04:33 +0800248static struct imx_uart_data imx_uart_devdata[] = {
249 [IMX1_UART] = {
250 .uts_reg = IMX1_UTS,
251 .devtype = IMX1_UART,
252 },
253 [IMX21_UART] = {
254 .uts_reg = IMX21_UTS,
255 .devtype = IMX21_UART,
256 },
Martyn Welch1c06bde62016-09-01 11:30:46 +0200257 [IMX53_UART] = {
258 .uts_reg = IMX21_UTS,
259 .devtype = IMX53_UART,
260 },
Huang Shijiea496e622013-07-08 17:14:17 +0800261 [IMX6Q_UART] = {
262 .uts_reg = IMX21_UTS,
263 .devtype = IMX6Q_UART,
264 },
Shawn Guofe6b5402011-06-25 02:04:33 +0800265};
266
Sanjeev Sharmaad3d4fd2015-02-03 16:16:06 +0530267static const struct of_device_id imx_uart_dt_ids[] = {
Huang Shijiea496e622013-07-08 17:14:17 +0800268 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
Martyn Welch1c06bde62016-09-01 11:30:46 +0200269 { .compatible = "fsl,imx53-uart", .data = &imx_uart_devdata[IMX53_UART], },
Shawn Guo22698aa2011-06-25 02:04:34 +0800270 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
271 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
272 { /* sentinel */ }
273};
274MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
275
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100276static void imx_uart_writel(struct imx_port *sport, u32 val, u32 offset)
277{
Uwe Kleine-König3a0ab622018-03-02 11:07:20 +0100278 switch (offset) {
279 case UCR1:
280 sport->ucr1 = val;
281 break;
282 case UCR2:
283 sport->ucr2 = val;
284 break;
285 case UCR3:
286 sport->ucr3 = val;
287 break;
288 case UCR4:
289 sport->ucr4 = val;
290 break;
291 case UFCR:
292 sport->ufcr = val;
293 break;
294 default:
295 break;
296 }
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100297 writel(val, sport->port.membase + offset);
298}
299
300static u32 imx_uart_readl(struct imx_port *sport, u32 offset)
301{
Uwe Kleine-König3a0ab622018-03-02 11:07:20 +0100302 switch (offset) {
303 case UCR1:
304 return sport->ucr1;
305 break;
306 case UCR2:
307 /*
308 * UCR2_SRST is the only bit in the cached registers that might
309 * differ from the value that was last written. As it only
Uwe Kleine-König728e74a2018-06-12 11:58:37 +0200310 * automatically becomes one after being cleared, reread
311 * conditionally.
Uwe Kleine-König3a0ab622018-03-02 11:07:20 +0100312 */
Stefan Agner0aa821d2018-04-20 14:44:07 +0200313 if (!(sport->ucr2 & UCR2_SRST))
Uwe Kleine-König3a0ab622018-03-02 11:07:20 +0100314 sport->ucr2 = readl(sport->port.membase + offset);
315 return sport->ucr2;
316 break;
317 case UCR3:
318 return sport->ucr3;
319 break;
320 case UCR4:
321 return sport->ucr4;
322 break;
323 case UFCR:
324 return sport->ufcr;
325 break;
326 default:
327 return readl(sport->port.membase + offset);
328 }
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100329}
330
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100331static inline unsigned imx_uart_uts_reg(struct imx_port *sport)
Shawn Guofe6b5402011-06-25 02:04:33 +0800332{
333 return sport->devdata->uts_reg;
334}
335
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100336static inline int imx_uart_is_imx1(struct imx_port *sport)
Shawn Guofe6b5402011-06-25 02:04:33 +0800337{
338 return sport->devdata->devtype == IMX1_UART;
339}
340
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100341static inline int imx_uart_is_imx21(struct imx_port *sport)
Shawn Guofe6b5402011-06-25 02:04:33 +0800342{
343 return sport->devdata->devtype == IMX21_UART;
344}
345
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100346static inline int imx_uart_is_imx53(struct imx_port *sport)
Martyn Welch1c06bde62016-09-01 11:30:46 +0200347{
348 return sport->devdata->devtype == IMX53_UART;
349}
350
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100351static inline int imx_uart_is_imx6q(struct imx_port *sport)
Huang Shijiea496e622013-07-08 17:14:17 +0800352{
353 return sport->devdata->devtype == IMX6Q_UART;
354}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*
fabio.estevam@freescale.com44a75412013-02-06 19:00:02 -0200356 * Save and restore functions for UCR1, UCR2 and UCR3 registers
357 */
Fugang Duan0db4f9b2020-07-24 15:08:14 +0800358#if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100359static void imx_uart_ucrs_save(struct imx_port *sport,
fabio.estevam@freescale.com44a75412013-02-06 19:00:02 -0200360 struct imx_port_ucrs *ucr)
361{
362 /* save control registers */
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100363 ucr->ucr1 = imx_uart_readl(sport, UCR1);
364 ucr->ucr2 = imx_uart_readl(sport, UCR2);
365 ucr->ucr3 = imx_uart_readl(sport, UCR3);
fabio.estevam@freescale.com44a75412013-02-06 19:00:02 -0200366}
367
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100368static void imx_uart_ucrs_restore(struct imx_port *sport,
fabio.estevam@freescale.com44a75412013-02-06 19:00:02 -0200369 struct imx_port_ucrs *ucr)
370{
371 /* restore control registers */
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100372 imx_uart_writel(sport, ucr->ucr1, UCR1);
373 imx_uart_writel(sport, ucr->ucr2, UCR2);
374 imx_uart_writel(sport, ucr->ucr3, UCR3);
fabio.estevam@freescale.com44a75412013-02-06 19:00:02 -0200375}
Fabio Estevame8bfa762013-06-05 00:58:46 -0300376#endif
fabio.estevam@freescale.com44a75412013-02-06 19:00:02 -0200377
Sergey Organov4e828c32019-06-11 15:05:24 +0300378/* called with port.lock taken and irqs caller dependent */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100379static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2)
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100380{
Fabio Estevambc2be232017-01-30 09:12:12 -0200381 *ucr2 &= ~(UCR2_CTSC | UCR2_CTS);
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100382
Ian Jamisona0983c72017-09-21 10:13:12 +0200383 sport->port.mctrl |= TIOCM_RTS;
384 mctrl_gpio_set(sport->gpios, sport->port.mctrl);
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100385}
386
Sergey Organov4e828c32019-06-11 15:05:24 +0300387/* called with port.lock taken and irqs caller dependent */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100388static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2)
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100389{
Fabio Estevambc2be232017-01-30 09:12:12 -0200390 *ucr2 &= ~UCR2_CTSC;
391 *ucr2 |= UCR2_CTS;
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100392
Ian Jamisona0983c72017-09-21 10:13:12 +0200393 sport->port.mctrl &= ~TIOCM_RTS;
394 mctrl_gpio_set(sport->gpios, sport->port.mctrl);
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100395}
396
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200397static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
398{
Jiri Slabyf751ae12021-03-02 07:21:40 +0100399 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200400}
401
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +0100402/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100403static void imx_uart_start_rx(struct uart_port *port)
Uwe Kleine-König76821e22018-03-02 11:07:26 +0100404{
405 struct imx_port *sport = (struct imx_port *)port;
406 unsigned int ucr1, ucr2;
407
408 ucr1 = imx_uart_readl(sport, UCR1);
409 ucr2 = imx_uart_readl(sport, UCR2);
410
411 ucr2 |= UCR2_RXEN;
412
413 if (sport->dma_is_enabled) {
414 ucr1 |= UCR1_RXDMAEN | UCR1_ATDMAEN;
415 } else {
416 ucr1 |= UCR1_RRDYEN;
Uwe Kleine-König81ca8e82018-03-02 11:07:27 +0100417 ucr2 |= UCR2_ATEN;
Uwe Kleine-König76821e22018-03-02 11:07:26 +0100418 }
419
420 /* Write UCR2 first as it includes RXEN */
421 imx_uart_writel(sport, ucr2, UCR2);
422 imx_uart_writel(sport, ucr1, UCR1);
423}
424
425/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100426static void imx_uart_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct imx_port *sport = (struct imx_port *)port;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200429 u32 ucr1, ucr4, usr2;
430
431 if (sport->tx_state == OFF)
432 return;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100433
Greg Kroah-Hartman9ce4f8f2014-05-29 19:30:54 -0700434 /*
435 * We are maybe in the SMP context, so if the DMA TX thread is running
436 * on other cpu, we have to wait for it to finish.
437 */
Uwe Kleine-König686351f2018-03-02 11:07:21 +0100438 if (sport->dma_is_txing)
Greg Kroah-Hartman9ce4f8f2014-05-29 19:30:54 -0700439 return;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800440
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100441 ucr1 = imx_uart_readl(sport, UCR1);
Sergey Organovc514a6f2019-08-28 21:37:55 +0300442 imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +0100443
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200444 usr2 = imx_uart_readl(sport, USR2);
445 if (!(usr2 & USR2_TXDC)) {
446 /* The shifter is still busy, so retry once TC triggers */
447 return;
448 }
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +0100449
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200450 ucr4 = imx_uart_readl(sport, UCR4);
451 ucr4 &= ~UCR4_TCEN;
452 imx_uart_writel(sport, ucr4, UCR4);
Uwe Kleine-König76821e22018-03-02 11:07:26 +0100453
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200454 /* in rs485 mode disable transmitter */
455 if (port->rs485.flags & SER_RS485_ENABLED) {
456 if (sport->tx_state == SEND) {
457 sport->tx_state = WAIT_AFTER_SEND;
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200458 start_hrtimer_ms(&sport->trigger_stop_tx,
459 port->rs485.delay_rts_after_send);
460 return;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200461 }
462
463 if (sport->tx_state == WAIT_AFTER_RTS ||
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200464 sport->tx_state == WAIT_AFTER_SEND) {
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200465 u32 ucr2;
466
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200467 hrtimer_try_to_cancel(&sport->trigger_start_tx);
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200468
469 ucr2 = imx_uart_readl(sport, UCR2);
470 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
471 imx_uart_rts_active(sport, &ucr2);
472 else
473 imx_uart_rts_inactive(sport, &ucr2);
474 imx_uart_writel(sport, ucr2, UCR2);
475
476 imx_uart_start_rx(port);
477
478 sport->tx_state = OFF;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200479 }
480 } else {
481 sport->tx_state = OFF;
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +0100482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +0100485/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100486static void imx_uart_stop_rx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct imx_port *sport = (struct imx_port *)port;
Fugang Duan028e0832021-11-25 10:03:49 +0800489 u32 ucr1, ucr2, ucr4;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100490
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100491 ucr1 = imx_uart_readl(sport, UCR1);
Uwe Kleine-König76821e22018-03-02 11:07:26 +0100492 ucr2 = imx_uart_readl(sport, UCR2);
Fugang Duan028e0832021-11-25 10:03:49 +0800493 ucr4 = imx_uart_readl(sport, UCR4);
Uwe Kleine-König76821e22018-03-02 11:07:26 +0100494
495 if (sport->dma_is_enabled) {
496 ucr1 &= ~(UCR1_RXDMAEN | UCR1_ATDMAEN);
497 } else {
498 ucr1 &= ~UCR1_RRDYEN;
Uwe Kleine-König81ca8e82018-03-02 11:07:27 +0100499 ucr2 &= ~UCR2_ATEN;
Fugang Duan028e0832021-11-25 10:03:49 +0800500 ucr4 &= ~UCR4_OREN;
Uwe Kleine-König76821e22018-03-02 11:07:26 +0100501 }
502 imx_uart_writel(sport, ucr1, UCR1);
Fugang Duan028e0832021-11-25 10:03:49 +0800503 imx_uart_writel(sport, ucr4, UCR4);
Uwe Kleine-König76821e22018-03-02 11:07:26 +0100504
505 ucr2 &= ~UCR2_RXEN;
506 imx_uart_writel(sport, ucr2, UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +0100509/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100510static void imx_uart_enable_ms(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct imx_port *sport = (struct imx_port *)port;
513
514 mod_timer(&sport->timer, jiffies);
Uwe Kleine-König58362d52015-12-13 11:30:03 +0100515
516 mctrl_gpio_enable_ms(sport->gpios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100519static void imx_uart_dma_tx(struct imx_port *sport);
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +0100520
521/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100522static inline void imx_uart_transmit_buffer(struct imx_port *sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700524 struct circ_buf *xmit = &sport->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Peter Hurley5e42e9a2014-09-02 17:39:12 -0400526 if (sport->port.x_char) {
527 /* Send next char */
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100528 imx_uart_writel(sport, sport->port.x_char, URTX0);
Jiada Wang7e2fb5a2014-12-09 18:11:35 +0900529 sport->port.icount.tx++;
530 sport->port.x_char = 0;
Peter Hurley5e42e9a2014-09-02 17:39:12 -0400531 return;
532 }
533
534 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100535 imx_uart_stop_tx(&sport->port);
Peter Hurley5e42e9a2014-09-02 17:39:12 -0400536 return;
537 }
538
Jiada Wang91a1a902014-12-09 18:11:36 +0900539 if (sport->dma_is_enabled) {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100540 u32 ucr1;
Jiada Wang91a1a902014-12-09 18:11:36 +0900541 /*
542 * We've just sent a X-char Ensure the TX DMA is enabled
543 * and the TX IRQ is disabled.
544 **/
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100545 ucr1 = imx_uart_readl(sport, UCR1);
Sergey Organovc514a6f2019-08-28 21:37:55 +0300546 ucr1 &= ~UCR1_TRDYEN;
Jiada Wang91a1a902014-12-09 18:11:36 +0900547 if (sport->dma_is_txing) {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100548 ucr1 |= UCR1_TXDMAEN;
549 imx_uart_writel(sport, ucr1, UCR1);
Jiada Wang91a1a902014-12-09 18:11:36 +0900550 } else {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100551 imx_uart_writel(sport, ucr1, UCR1);
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100552 imx_uart_dma_tx(sport);
Jiada Wang91a1a902014-12-09 18:11:36 +0900553 }
Jiada Wang91a1a902014-12-09 18:11:36 +0900554
Ian Jamison5aabd3b2017-08-28 09:02:29 +0100555 return;
Uwe Kleine-König0c549222018-03-02 11:07:22 +0100556 }
Ian Jamison5aabd3b2017-08-28 09:02:29 +0100557
558 while (!uart_circ_empty(xmit) &&
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100559 !(imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* send xmit->buf[xmit->tail]
561 * out the port here */
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100562 imx_uart_writel(sport, xmit->buf[xmit->tail], URTX0);
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100563 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 sport->port.icount.tx++;
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Fabian Godehardt977757312009-06-11 14:37:19 +0100567 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
568 uart_write_wakeup(&sport->port);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (uart_circ_empty(xmit))
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100571 imx_uart_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100574static void imx_uart_dma_tx_callback(void *data)
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800575{
576 struct imx_port *sport = data;
577 struct scatterlist *sgl = &sport->tx_sgl[0];
578 struct circ_buf *xmit = &sport->port.state->xmit;
579 unsigned long flags;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100580 u32 ucr1;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800581
Dirk Behme42f752b2014-12-09 18:11:28 +0900582 spin_lock_irqsave(&sport->port.lock, flags);
583
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800584 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
585
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100586 ucr1 = imx_uart_readl(sport, UCR1);
587 ucr1 &= ~UCR1_TXDMAEN;
588 imx_uart_writel(sport, ucr1, UCR1);
Dirk Behmea2c718c2014-12-09 18:11:31 +0900589
Dirk Behme42f752b2014-12-09 18:11:28 +0900590 /* update the stat */
591 xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
592 sport->port.icount.tx += sport->tx_bytes;
593
594 dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
595
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800596 sport->dma_is_txing = 0;
597
Jiada Wangd64b8602014-12-09 18:11:29 +0900598 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
599 uart_write_wakeup(&sport->port);
Greg Kroah-Hartman9ce4f8f2014-05-29 19:30:54 -0700600
Jiada Wang0bbc9b82014-12-09 18:11:30 +0900601 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100602 imx_uart_dma_tx(sport);
Uwe Kleine-König18665412018-03-02 11:07:28 +0100603 else if (sport->port.rs485.flags & SER_RS485_ENABLED) {
604 u32 ucr4 = imx_uart_readl(sport, UCR4);
605 ucr4 |= UCR4_TCEN;
606 imx_uart_writel(sport, ucr4, UCR4);
607 }
Uwe Kleine-König64432a82017-07-18 14:01:52 +0200608
Jiada Wang0bbc9b82014-12-09 18:11:30 +0900609 spin_unlock_irqrestore(&sport->port.lock, flags);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800610}
611
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +0100612/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100613static void imx_uart_dma_tx(struct imx_port *sport)
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800614{
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800615 struct circ_buf *xmit = &sport->port.state->xmit;
616 struct scatterlist *sgl = sport->tx_sgl;
617 struct dma_async_tx_descriptor *desc;
618 struct dma_chan *chan = sport->dma_chan_tx;
619 struct device *dev = sport->port.dev;
Uwe Kleine-König18665412018-03-02 11:07:28 +0100620 u32 ucr1, ucr4;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800621 int ret;
622
Dirk Behme42f752b2014-12-09 18:11:28 +0900623 if (sport->dma_is_txing)
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800624 return;
625
Uwe Kleine-König18665412018-03-02 11:07:28 +0100626 ucr4 = imx_uart_readl(sport, UCR4);
627 ucr4 &= ~UCR4_TCEN;
628 imx_uart_writel(sport, ucr4, UCR4);
629
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800630 sport->tx_bytes = uart_circ_chars_pending(xmit);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800631
Fugang Duanf7670782020-02-11 14:16:01 +0800632 if (xmit->tail < xmit->head || xmit->head == 0) {
Dirk Behme7942f852014-12-09 18:11:25 +0900633 sport->dma_tx_nents = 1;
634 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
635 } else {
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800636 sport->dma_tx_nents = 2;
637 sg_init_table(sgl, 2);
638 sg_set_buf(sgl, xmit->buf + xmit->tail,
639 UART_XMIT_SIZE - xmit->tail);
640 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800641 }
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800642
643 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
644 if (ret == 0) {
645 dev_err(dev, "DMA mapping error for TX.\n");
646 return;
647 }
Peng Fan596fd8d2019-11-07 06:42:53 +0000648 desc = dmaengine_prep_slave_sg(chan, sgl, ret,
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800649 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
650 if (!desc) {
Dirk Behme24649822014-12-09 18:11:26 +0900651 dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
652 DMA_TO_DEVICE);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800653 dev_err(dev, "We cannot prepare for the TX slave dma!\n");
654 return;
655 }
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100656 desc->callback = imx_uart_dma_tx_callback;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800657 desc->callback_param = sport;
658
659 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
660 uart_circ_chars_pending(xmit));
Dirk Behmea2c718c2014-12-09 18:11:31 +0900661
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100662 ucr1 = imx_uart_readl(sport, UCR1);
663 ucr1 |= UCR1_TXDMAEN;
664 imx_uart_writel(sport, ucr1, UCR1);
Dirk Behmea2c718c2014-12-09 18:11:31 +0900665
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800666 /* fire it */
667 sport->dma_is_txing = 1;
668 dmaengine_submit(desc);
669 dma_async_issue_pending(chan);
670 return;
671}
672
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +0100673/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100674static void imx_uart_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct imx_port *sport = (struct imx_port *)port;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100677 u32 ucr1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Uwe Kleine-König48669b62018-03-02 11:07:29 +0100679 if (!sport->port.x_char && uart_circ_empty(&port->state->xmit))
680 return;
681
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200682 /*
683 * We cannot simply do nothing here if sport->tx_state == SEND already
684 * because UCR1_TXMPTYEN might already have been cleared in
685 * imx_uart_stop_tx(), but tx_state is still SEND.
686 */
687
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +0100688 if (port->rs485.flags & SER_RS485_ENABLED) {
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200689 if (sport->tx_state == OFF) {
690 u32 ucr2 = imx_uart_readl(sport, UCR2);
691 if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
692 imx_uart_rts_active(sport, &ucr2);
693 else
694 imx_uart_rts_inactive(sport, &ucr2);
695 imx_uart_writel(sport, ucr2, UCR2);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100696
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200697 if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
698 imx_uart_stop_rx(port);
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +0100699
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200700 sport->tx_state = WAIT_AFTER_RTS;
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200701 start_hrtimer_ms(&sport->trigger_start_tx,
702 port->rs485.delay_rts_before_send);
703 return;
Uwe Kleine-König18665412018-03-02 11:07:28 +0100704 }
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200705
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200706 if (sport->tx_state == WAIT_AFTER_SEND
707 || sport->tx_state == WAIT_AFTER_RTS) {
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200708
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +0200709 hrtimer_try_to_cancel(&sport->trigger_stop_tx);
710
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200711 /*
712 * Enable transmitter and shifter empty irq only if DMA
713 * is off. In the DMA case this is done in the
714 * tx-callback.
715 */
716 if (!sport->dma_is_enabled) {
717 u32 ucr4 = imx_uart_readl(sport, UCR4);
718 ucr4 |= UCR4_TCEN;
719 imx_uart_writel(sport, ucr4, UCR4);
720 }
721
722 sport->tx_state = SEND;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +0200723 }
724 } else {
725 sport->tx_state = SEND;
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +0100726 }
727
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800728 if (!sport->dma_is_enabled) {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100729 ucr1 = imx_uart_readl(sport, UCR1);
Sergey Organovc514a6f2019-08-28 21:37:55 +0300730 imx_uart_writel(sport, ucr1 | UCR1_TRDYEN, UCR1);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800733 if (sport->dma_is_enabled) {
Jiada Wang91a1a902014-12-09 18:11:36 +0900734 if (sport->port.x_char) {
735 /* We have X-char to send, so enable TX IRQ and
736 * disable TX DMA to let TX interrupt to send X-char */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100737 ucr1 = imx_uart_readl(sport, UCR1);
738 ucr1 &= ~UCR1_TXDMAEN;
Sergey Organovc514a6f2019-08-28 21:37:55 +0300739 ucr1 |= UCR1_TRDYEN;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100740 imx_uart_writel(sport, ucr1, UCR1);
Jiada Wang91a1a902014-12-09 18:11:36 +0900741 return;
742 }
743
Peter Hurley5e42e9a2014-09-02 17:39:12 -0400744 if (!uart_circ_empty(&port->state->xmit) &&
745 !uart_tx_stopped(port))
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100746 imx_uart_dma_tx(sport);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800747 return;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100751static irqreturn_t __imx_uart_rtsint(int irq, void *dev_id)
Sascha Hauerceca6292005-10-12 19:58:08 +0100752{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800753 struct imx_port *sport = dev_id;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100754 u32 usr1;
Sascha Hauerceca6292005-10-12 19:58:08 +0100755
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100756 imx_uart_writel(sport, USR1_RTSD, USR1);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100757 usr1 = imx_uart_readl(sport, USR1) & USR1_RTSS;
758 uart_handle_cts_change(&sport->port, !!usr1);
Alan Coxbdc04e32009-09-19 13:13:31 -0700759 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
Sascha Hauerceca6292005-10-12 19:58:08 +0100760
Sascha Hauerceca6292005-10-12 19:58:08 +0100761 return IRQ_HANDLED;
762}
763
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100764static irqreturn_t imx_uart_rtsint(int irq, void *dev_id)
765{
766 struct imx_port *sport = dev_id;
767 irqreturn_t ret;
768
769 spin_lock(&sport->port.lock);
770
771 ret = __imx_uart_rtsint(irq, dev_id);
772
773 spin_unlock(&sport->port.lock);
774
775 return ret;
776}
777
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100778static irqreturn_t imx_uart_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800780 struct imx_port *sport = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
jun qianc9749912018-08-27 07:49:04 -0700782 spin_lock(&sport->port.lock);
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100783 imx_uart_transmit_buffer(sport);
jun qianc9749912018-08-27 07:49:04 -0700784 spin_unlock(&sport->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return IRQ_HANDLED;
786}
787
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100788static irqreturn_t __imx_uart_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct imx_port *sport = dev_id;
Sachin Kamat82313e62013-01-07 10:25:02 +0530791 unsigned int rx, flg, ignored = 0;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100792 struct tty_port *port = &sport->port.state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100794 while (imx_uart_readl(sport, USR2) & USR2_RDR) {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100795 u32 usr2;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 flg = TTY_NORMAL;
798 sport->port.icount.rx++;
799
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100800 rx = imx_uart_readl(sport, URXD0);
Sascha Hauer0d3c3932008-04-17 08:43:14 +0100801
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +0100802 usr2 = imx_uart_readl(sport, USR2);
803 if (usr2 & USR2_BRCD) {
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100804 imx_uart_writel(sport, USR2_BRCD, USR2);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100805 if (uart_handle_break(&sport->port))
806 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100809 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
Sascha Hauer864eeed2008-04-17 08:39:22 +0100810 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Hui Wang019dc9e2011-08-24 17:41:47 +0800812 if (unlikely(rx & URXD_ERR)) {
813 if (rx & URXD_BRK)
814 sport->port.icount.brk++;
815 else if (rx & URXD_PRERR)
Sascha Hauer864eeed2008-04-17 08:39:22 +0100816 sport->port.icount.parity++;
817 else if (rx & URXD_FRMERR)
818 sport->port.icount.frame++;
819 if (rx & URXD_OVRRUN)
820 sport->port.icount.overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Sascha Hauer864eeed2008-04-17 08:39:22 +0100822 if (rx & sport->port.ignore_status_mask) {
823 if (++ignored > 100)
824 goto out;
825 continue;
826 }
827
Eric Nelson8d267fd2014-12-18 12:37:13 -0700828 rx &= (sport->port.read_status_mask | 0xFF);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100829
Hui Wang019dc9e2011-08-24 17:41:47 +0800830 if (rx & URXD_BRK)
831 flg = TTY_BREAK;
832 else if (rx & URXD_PRERR)
Sascha Hauer864eeed2008-04-17 08:39:22 +0100833 flg = TTY_PARITY;
834 else if (rx & URXD_FRMERR)
835 flg = TTY_FRAME;
836 if (rx & URXD_OVRRUN)
837 flg = TTY_OVERRUN;
838
Sascha Hauer864eeed2008-04-17 08:39:22 +0100839 sport->port.sysrq = 0;
Sascha Hauer864eeed2008-04-17 08:39:22 +0100840 }
841
Jiada Wang55d86932014-12-09 18:11:22 +0900842 if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
843 goto out;
844
Manfred Schlaegl9b289932015-06-20 19:25:35 +0200845 if (tty_insert_flip_char(port, rx, flg) == 0)
846 sport->port.icount.buf_overrun++;
Sascha Hauer864eeed2008-04-17 08:39:22 +0100847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849out:
Jiri Slaby2e124b42013-01-03 15:53:06 +0100850 tty_flip_buffer_push(port);
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100855static irqreturn_t imx_uart_rxint(int irq, void *dev_id)
856{
857 struct imx_port *sport = dev_id;
858 irqreturn_t ret;
859
860 spin_lock(&sport->port.lock);
861
862 ret = __imx_uart_rxint(irq, dev_id);
863
864 spin_unlock(&sport->port.lock);
865
866 return ret;
867}
868
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100869static void imx_uart_clear_rx_errors(struct imx_port *sport);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800870
Uwe Kleine-König66f95882016-03-24 14:24:24 +0100871/*
872 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
873 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100874static unsigned int imx_uart_get_hwmctrl(struct imx_port *sport)
Uwe Kleine-König66f95882016-03-24 14:24:24 +0100875{
876 unsigned int tmp = TIOCM_DSR;
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100877 unsigned usr1 = imx_uart_readl(sport, USR1);
878 unsigned usr2 = imx_uart_readl(sport, USR2);
Uwe Kleine-König66f95882016-03-24 14:24:24 +0100879
880 if (usr1 & USR1_RTSS)
881 tmp |= TIOCM_CTS;
882
883 /* in DCE mode DCDIN is always 0 */
Sascha Hauer4b75f802016-09-26 15:55:31 +0200884 if (!(usr2 & USR2_DCDIN))
Uwe Kleine-König66f95882016-03-24 14:24:24 +0100885 tmp |= TIOCM_CAR;
886
887 if (sport->dte_mode)
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100888 if (!(imx_uart_readl(sport, USR2) & USR2_RIIN))
Uwe Kleine-König66f95882016-03-24 14:24:24 +0100889 tmp |= TIOCM_RI;
890
891 return tmp;
892}
893
894/*
895 * Handle any change of modem status signal since we were last called.
896 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100897static void imx_uart_mctrl_check(struct imx_port *sport)
Uwe Kleine-König66f95882016-03-24 14:24:24 +0100898{
899 unsigned int status, changed;
900
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100901 status = imx_uart_get_hwmctrl(sport);
Uwe Kleine-König66f95882016-03-24 14:24:24 +0100902 changed = status ^ sport->old_status;
903
904 if (changed == 0)
905 return;
906
907 sport->old_status = status;
908
909 if (changed & TIOCM_RI && status & TIOCM_RI)
910 sport->port.icount.rng++;
911 if (changed & TIOCM_DSR)
912 sport->port.icount.dsr++;
913 if (changed & TIOCM_CAR)
914 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
915 if (changed & TIOCM_CTS)
916 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
917
918 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
919}
920
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100921static irqreturn_t imx_uart_int(int irq, void *dev_id)
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200922{
923 struct imx_port *sport = dev_id;
Uwe Kleine-König43776892018-02-18 22:02:44 +0100924 unsigned int usr1, usr2, ucr1, ucr2, ucr3, ucr4;
Uwe Kleine-König4d845a62016-03-24 14:24:21 +0100925 irqreturn_t ret = IRQ_NONE;
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200926
Johan Hovold9baedb72021-03-22 12:10:36 +0100927 spin_lock(&sport->port.lock);
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100928
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100929 usr1 = imx_uart_readl(sport, USR1);
930 usr2 = imx_uart_readl(sport, USR2);
931 ucr1 = imx_uart_readl(sport, UCR1);
932 ucr2 = imx_uart_readl(sport, UCR2);
933 ucr3 = imx_uart_readl(sport, UCR3);
934 ucr4 = imx_uart_readl(sport, UCR4);
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200935
Uwe Kleine-König43776892018-02-18 22:02:44 +0100936 /*
937 * Even if a condition is true that can trigger an irq only handle it if
938 * the respective irq source is enabled. This prevents some undesired
939 * actions, for example if a character that sits in the RX FIFO and that
940 * should be fetched via DMA is tried to be fetched using PIO. Or the
941 * receiver is currently off and so reading from URXD0 results in an
942 * exception. So just mask the (raw) status bits for disabled irqs.
943 */
944 if ((ucr1 & UCR1_RRDYEN) == 0)
945 usr1 &= ~USR1_RRDY;
946 if ((ucr2 & UCR2_ATEN) == 0)
947 usr1 &= ~USR1_AGTIM;
Sergey Organovc514a6f2019-08-28 21:37:55 +0300948 if ((ucr1 & UCR1_TRDYEN) == 0)
Uwe Kleine-König43776892018-02-18 22:02:44 +0100949 usr1 &= ~USR1_TRDY;
950 if ((ucr4 & UCR4_TCEN) == 0)
951 usr2 &= ~USR2_TXDC;
952 if ((ucr3 & UCR3_DTRDEN) == 0)
953 usr1 &= ~USR1_DTRD;
954 if ((ucr1 & UCR1_RTSDEN) == 0)
955 usr1 &= ~USR1_RTSD;
956 if ((ucr3 & UCR3_AWAKEN) == 0)
957 usr1 &= ~USR1_AWAKE;
958 if ((ucr4 & UCR4_OREN) == 0)
959 usr2 &= ~USR2_ORE;
960
961 if (usr1 & (USR1_RRDY | USR1_AGTIM)) {
Matthias Schifferd1d996a2020-05-28 17:47:47 +0200962 imx_uart_writel(sport, USR1_AGTIM, USR1);
963
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100964 __imx_uart_rxint(irq, dev_id);
Uwe Kleine-König4d845a62016-03-24 14:24:21 +0100965 ret = IRQ_HANDLED;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +0800966 }
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200967
Uwe Kleine-König43776892018-02-18 22:02:44 +0100968 if ((usr1 & USR1_TRDY) || (usr2 & USR2_TXDC)) {
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100969 imx_uart_transmit_buffer(sport);
Uwe Kleine-König4d845a62016-03-24 14:24:21 +0100970 ret = IRQ_HANDLED;
971 }
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200972
Uwe Kleine-König0399fd62018-02-18 22:02:43 +0100973 if (usr1 & USR1_DTRD) {
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100974 imx_uart_writel(sport, USR1_DTRD, USR1);
Uwe Kleine-König27e16502016-03-24 14:24:25 +0100975
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +0100976 imx_uart_mctrl_check(sport);
Uwe Kleine-König27e16502016-03-24 14:24:25 +0100977
978 ret = IRQ_HANDLED;
979 }
980
Uwe Kleine-König0399fd62018-02-18 22:02:43 +0100981 if (usr1 & USR1_RTSD) {
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100982 __imx_uart_rtsint(irq, dev_id);
Uwe Kleine-König4d845a62016-03-24 14:24:21 +0100983 ret = IRQ_HANDLED;
984 }
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200985
Uwe Kleine-König0399fd62018-02-18 22:02:43 +0100986 if (usr1 & USR1_AWAKE) {
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100987 imx_uart_writel(sport, USR1_AWAKE, USR1);
Uwe Kleine-König4d845a62016-03-24 14:24:21 +0100988 ret = IRQ_HANDLED;
989 }
Fabio Estevamdb1a9b52011-12-13 01:23:48 -0200990
Uwe Kleine-König0399fd62018-02-18 22:02:43 +0100991 if (usr2 & USR2_ORE) {
Alexander Steinf1f836e2013-05-14 17:06:07 +0200992 sport->port.icount.overrun++;
Uwe Kleine-König27c84422018-03-02 11:07:19 +0100993 imx_uart_writel(sport, USR2_ORE, USR2);
Uwe Kleine-König4d845a62016-03-24 14:24:21 +0100994 ret = IRQ_HANDLED;
Alexander Steinf1f836e2013-05-14 17:06:07 +0200995 }
996
Johan Hovold9baedb72021-03-22 12:10:36 +0100997 spin_unlock(&sport->port.lock);
Uwe Kleine-König101aa462020-01-21 08:17:02 +0100998
Uwe Kleine-König4d845a62016-03-24 14:24:21 +0100999 return ret;
Sascha Hauere3d13ff2008-07-05 10:02:48 +02001000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/*
1003 * Return TIOCSER_TEMT when transmitter is not busy.
1004 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001005static unsigned int imx_uart_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct imx_port *sport = (struct imx_port *)port;
Huang Shijie1ce43e52013-10-11 18:30:59 +08001008 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001010 ret = (imx_uart_readl(sport, USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
Huang Shijie1ce43e52013-10-11 18:30:59 +08001011
1012 /* If the TX DMA is working, return 0. */
Uwe Kleine-König686351f2018-03-02 11:07:21 +01001013 if (sport->dma_is_txing)
Huang Shijie1ce43e52013-10-11 18:30:59 +08001014 ret = 0;
1015
1016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +01001019/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001020static unsigned int imx_uart_get_mctrl(struct uart_port *port)
Uwe Kleine-König58362d52015-12-13 11:30:03 +01001021{
1022 struct imx_port *sport = (struct imx_port *)port;
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001023 unsigned int ret = imx_uart_get_hwmctrl(sport);
Uwe Kleine-König58362d52015-12-13 11:30:03 +01001024
1025 mctrl_gpio_get(sport->gpios, &ret);
1026
1027 return ret;
1028}
1029
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +01001030/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001031static void imx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001033 struct imx_port *sport = (struct imx_port *)port;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001034 u32 ucr3, uts;
Sascha Hauerff4bfb22007-04-26 08:26:13 +01001035
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001036 if (!(port->rs485.flags & SER_RS485_ENABLED)) {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001037 u32 ucr2;
1038
Sergey Organov197540d2019-07-26 21:52:40 +03001039 /*
1040 * Turn off autoRTS if RTS is lowered and restore autoRTS
1041 * setting if RTS is raised.
1042 */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001043 ucr2 = imx_uart_readl(sport, UCR2);
1044 ucr2 &= ~(UCR2_CTS | UCR2_CTSC);
Sergey Organov197540d2019-07-26 21:52:40 +03001045 if (mctrl & TIOCM_RTS) {
1046 ucr2 |= UCR2_CTS;
1047 /*
1048 * UCR2_IRTS is unset if and only if the port is
1049 * configured for CRTSCTS, so we use inverted UCR2_IRTS
1050 * to get the state to restore to.
1051 */
1052 if (!(ucr2 & UCR2_IRTS))
1053 ucr2 |= UCR2_CTSC;
1054 }
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001055 imx_uart_writel(sport, ucr2, UCR2);
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001056 }
Huang Shijie6b471a92013-11-29 17:29:24 +08001057
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001058 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_DSR;
Uwe Kleine-König90ebc482015-10-18 21:34:46 +02001059 if (!(mctrl & TIOCM_DTR))
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001060 ucr3 |= UCR3_DSR;
1061 imx_uart_writel(sport, ucr3, UCR3);
Uwe Kleine-König90ebc482015-10-18 21:34:46 +02001062
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001063 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)) & ~UTS_LOOP;
Huang Shijie6b471a92013-11-29 17:29:24 +08001064 if (mctrl & TIOCM_LOOP)
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001065 uts |= UTS_LOOP;
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001066 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
Uwe Kleine-König58362d52015-12-13 11:30:03 +01001067
1068 mctrl_gpio_set(sport->gpios, mctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071/*
1072 * Interrupts always disabled.
1073 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001074static void imx_uart_break_ctl(struct uart_port *port, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 struct imx_port *sport = (struct imx_port *)port;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001077 unsigned long flags;
1078 u32 ucr1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 spin_lock_irqsave(&sport->port.lock, flags);
1081
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001082 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_SNDBRK;
Sascha Hauerff4bfb22007-04-26 08:26:13 +01001083
Sachin Kamat82313e62013-01-07 10:25:02 +05301084 if (break_state != 0)
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001085 ucr1 |= UCR1_SNDBRK;
Sascha Hauerff4bfb22007-04-26 08:26:13 +01001086
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001087 imx_uart_writel(sport, ucr1, UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 spin_unlock_irqrestore(&sport->port.lock, flags);
1090}
1091
Uwe Kleine-Königcc568842015-10-18 21:34:47 +02001092/*
Uwe Kleine-Königcc568842015-10-18 21:34:47 +02001093 * This is our per-port timeout handler, for checking the
1094 * modem status signals.
1095 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001096static void imx_uart_timeout(struct timer_list *t)
Uwe Kleine-Königcc568842015-10-18 21:34:47 +02001097{
Kees Cooke99e88a2017-10-16 14:43:17 -07001098 struct imx_port *sport = from_timer(sport, t, timer);
Uwe Kleine-Königcc568842015-10-18 21:34:47 +02001099 unsigned long flags;
1100
1101 if (sport->port.state) {
1102 spin_lock_irqsave(&sport->port.lock, flags);
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001103 imx_uart_mctrl_check(sport);
Uwe Kleine-Königcc568842015-10-18 21:34:47 +02001104 spin_unlock_irqrestore(&sport->port.lock, flags);
1105
1106 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
1107 }
1108}
1109
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001110/*
Lucas Stach905c0de2015-09-04 17:52:41 +02001111 * There are two kinds of RX DMA interrupts(such as in the MX6Q):
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001112 * [1] the RX DMA buffer is full.
Lucas Stach905c0de2015-09-04 17:52:41 +02001113 * [2] the aging timer expires
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001114 *
Lucas Stach905c0de2015-09-04 17:52:41 +02001115 * Condition [2] is triggered when a character has been sitting in the FIFO
1116 * for at least 8 byte durations.
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001117 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001118static void imx_uart_dma_rx_callback(void *data)
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001119{
1120 struct imx_port *sport = data;
1121 struct dma_chan *chan = sport->dma_chan_rx;
1122 struct scatterlist *sgl = &sport->rx_sgl;
Huang Shijie7cb92fd2013-10-15 15:23:40 +08001123 struct tty_port *port = &sport->port.state->port;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001124 struct dma_tx_state state;
Nandor Han9d297232016-08-08 15:38:27 +03001125 struct circ_buf *rx_ring = &sport->rx_ring;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001126 enum dma_status status;
Nandor Han9d297232016-08-08 15:38:27 +03001127 unsigned int w_bytes = 0;
1128 unsigned int r_bytes;
1129 unsigned int bd_size;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001130
Robin Gongfb7f1bf2018-06-20 00:56:58 +08001131 status = dmaengine_tx_status(chan, sport->rx_cookie, &state);
Philipp Zabel392bceed2015-05-19 10:54:09 +02001132
Nandor Han9d297232016-08-08 15:38:27 +03001133 if (status == DMA_ERROR) {
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001134 imx_uart_clear_rx_errors(sport);
Nandor Han9d297232016-08-08 15:38:27 +03001135 return;
Robin Gongee5e7c12014-12-09 18:11:33 +09001136 }
Lucas Stach976b39c2015-09-04 17:52:39 +02001137
Nandor Han9d297232016-08-08 15:38:27 +03001138 if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) {
1139
1140 /*
1141 * The state-residue variable represents the empty space
1142 * relative to the entire buffer. Taking this in consideration
1143 * the head is always calculated base on the buffer total
1144 * length - DMA transaction residue. The UART script from the
1145 * SDMA firmware will jump to the next buffer descriptor,
1146 * once a DMA transaction if finalized (IMX53 RM - A.4.1.2.4).
1147 * Taking this in consideration the tail is always at the
1148 * beginning of the buffer descriptor that contains the head.
1149 */
1150
1151 /* Calculate the head */
1152 rx_ring->head = sg_dma_len(sgl) - state.residue;
1153
1154 /* Calculate the tail. */
1155 bd_size = sg_dma_len(sgl) / sport->rx_periods;
1156 rx_ring->tail = ((rx_ring->head-1) / bd_size) * bd_size;
1157
1158 if (rx_ring->head <= sg_dma_len(sgl) &&
1159 rx_ring->head > rx_ring->tail) {
1160
1161 /* Move data from tail to head */
1162 r_bytes = rx_ring->head - rx_ring->tail;
1163
1164 /* CPU claims ownership of RX DMA buffer */
1165 dma_sync_sg_for_cpu(sport->port.dev, sgl, 1,
1166 DMA_FROM_DEVICE);
1167
1168 w_bytes = tty_insert_flip_string(port,
1169 sport->rx_buf + rx_ring->tail, r_bytes);
1170
1171 /* UART retrieves ownership of RX DMA buffer */
1172 dma_sync_sg_for_device(sport->port.dev, sgl, 1,
1173 DMA_FROM_DEVICE);
1174
1175 if (w_bytes != r_bytes)
1176 sport->port.icount.buf_overrun++;
1177
1178 sport->port.icount.rx += w_bytes;
1179 } else {
1180 WARN_ON(rx_ring->head > sg_dma_len(sgl));
1181 WARN_ON(rx_ring->head <= rx_ring->tail);
1182 }
1183 }
1184
1185 if (w_bytes) {
1186 tty_flip_buffer_push(port);
1187 dev_dbg(sport->port.dev, "We get %d bytes.\n", w_bytes);
1188 }
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001189}
1190
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001191static int imx_uart_start_rx_dma(struct imx_port *sport)
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001192{
1193 struct scatterlist *sgl = &sport->rx_sgl;
1194 struct dma_chan *chan = sport->dma_chan_rx;
1195 struct device *dev = sport->port.dev;
1196 struct dma_async_tx_descriptor *desc;
1197 int ret;
1198
Nandor Han9d297232016-08-08 15:38:27 +03001199 sport->rx_ring.head = 0;
1200 sport->rx_ring.tail = 0;
Nandor Han9d297232016-08-08 15:38:27 +03001201
Fabien Lahouderedb0a1962021-04-30 19:50:37 +02001202 sg_init_one(sgl, sport->rx_buf, sport->rx_buf_size);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001203 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1204 if (ret == 0) {
1205 dev_err(dev, "DMA mapping error for RX.\n");
1206 return -EINVAL;
1207 }
Nandor Han9d297232016-08-08 15:38:27 +03001208
1209 desc = dmaengine_prep_dma_cyclic(chan, sg_dma_address(sgl),
1210 sg_dma_len(sgl), sg_dma_len(sgl) / sport->rx_periods,
1211 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
1212
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001213 if (!desc) {
Dirk Behme24649822014-12-09 18:11:26 +09001214 dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001215 dev_err(dev, "We cannot prepare for the RX slave dma!\n");
1216 return -EINVAL;
1217 }
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001218 desc->callback = imx_uart_dma_rx_callback;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001219 desc->callback_param = sport;
1220
1221 dev_dbg(dev, "RX: prepare for the DMA.\n");
Romain Perier4139fd72017-09-28 11:03:49 +01001222 sport->dma_is_rxing = 1;
Nandor Han9d297232016-08-08 15:38:27 +03001223 sport->rx_cookie = dmaengine_submit(desc);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001224 dma_async_issue_pending(chan);
1225 return 0;
1226}
1227
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001228static void imx_uart_clear_rx_errors(struct imx_port *sport)
Nandor Han41d98b52016-08-08 15:38:28 +03001229{
Troy Kisky45ca6732018-02-23 18:27:50 -08001230 struct tty_port *port = &sport->port.state->port;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001231 u32 usr1, usr2;
Nandor Han41d98b52016-08-08 15:38:28 +03001232
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001233 usr1 = imx_uart_readl(sport, USR1);
1234 usr2 = imx_uart_readl(sport, USR2);
Nandor Han41d98b52016-08-08 15:38:28 +03001235
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001236 if (usr2 & USR2_BRCD) {
Nandor Han41d98b52016-08-08 15:38:28 +03001237 sport->port.icount.brk++;
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001238 imx_uart_writel(sport, USR2_BRCD, USR2);
Troy Kisky45ca6732018-02-23 18:27:50 -08001239 uart_handle_break(&sport->port);
1240 if (tty_insert_flip_char(port, 0, TTY_BREAK) == 0)
1241 sport->port.icount.buf_overrun++;
1242 tty_flip_buffer_push(port);
1243 } else {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001244 if (usr1 & USR1_FRAMERR) {
Troy Kisky45ca6732018-02-23 18:27:50 -08001245 sport->port.icount.frame++;
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001246 imx_uart_writel(sport, USR1_FRAMERR, USR1);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001247 } else if (usr1 & USR1_PARITYERR) {
Troy Kisky45ca6732018-02-23 18:27:50 -08001248 sport->port.icount.parity++;
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001249 imx_uart_writel(sport, USR1_PARITYERR, USR1);
Troy Kisky45ca6732018-02-23 18:27:50 -08001250 }
Nandor Han41d98b52016-08-08 15:38:28 +03001251 }
1252
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001253 if (usr2 & USR2_ORE) {
Nandor Han41d98b52016-08-08 15:38:28 +03001254 sport->port.icount.overrun++;
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001255 imx_uart_writel(sport, USR2_ORE, USR2);
Nandor Han41d98b52016-08-08 15:38:28 +03001256 }
1257
1258}
1259
Lucas Stachcc323822015-09-04 17:52:37 +02001260#define TXTL_DEFAULT 2 /* reset default */
1261#define RXTL_DEFAULT 1 /* reset default */
Lucas Stach184bd702015-09-04 17:52:40 +02001262#define TXTL_DMA 8 /* DMA burst setting */
1263#define RXTL_DMA 9 /* DMA burst setting */
Lucas Stachcc323822015-09-04 17:52:37 +02001264
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001265static void imx_uart_setup_ufcr(struct imx_port *sport,
1266 unsigned char txwl, unsigned char rxwl)
Lucas Stachcc323822015-09-04 17:52:37 +02001267{
1268 unsigned int val;
1269
1270 /* set receiver / transmitter trigger level */
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001271 val = imx_uart_readl(sport, UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
Lucas Stachcc323822015-09-04 17:52:37 +02001272 val |= txwl << UFCR_TXTL_SHF | rxwl;
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001273 imx_uart_writel(sport, val, UFCR);
Lucas Stachcc323822015-09-04 17:52:37 +02001274}
1275
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001276static void imx_uart_dma_exit(struct imx_port *sport)
1277{
1278 if (sport->dma_chan_rx) {
Fabien Lahouderee5e89602016-09-13 10:17:05 +02001279 dmaengine_terminate_sync(sport->dma_chan_rx);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001280 dma_release_channel(sport->dma_chan_rx);
1281 sport->dma_chan_rx = NULL;
Nandor Han9d297232016-08-08 15:38:27 +03001282 sport->rx_cookie = -EINVAL;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001283 kfree(sport->rx_buf);
1284 sport->rx_buf = NULL;
1285 }
1286
1287 if (sport->dma_chan_tx) {
Fabien Lahouderee5e89602016-09-13 10:17:05 +02001288 dmaengine_terminate_sync(sport->dma_chan_tx);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001289 dma_release_channel(sport->dma_chan_tx);
1290 sport->dma_chan_tx = NULL;
1291 }
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001292}
1293
1294static int imx_uart_dma_init(struct imx_port *sport)
1295{
Huang Shijieb09c74a2013-08-29 16:29:25 +08001296 struct dma_slave_config slave_config = {};
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001297 struct device *dev = sport->port.dev;
1298 int ret;
1299
1300 /* Prepare for RX : */
1301 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1302 if (!sport->dma_chan_rx) {
1303 dev_dbg(dev, "cannot get the DMA channel.\n");
1304 ret = -EINVAL;
1305 goto err;
1306 }
1307
1308 slave_config.direction = DMA_DEV_TO_MEM;
1309 slave_config.src_addr = sport->port.mapbase + URXD0;
1310 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
Lucas Stach184bd702015-09-04 17:52:40 +02001311 /* one byte less than the watermark level to enable the aging timer */
1312 slave_config.src_maxburst = RXTL_DMA - 1;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001313 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
1314 if (ret) {
1315 dev_err(dev, "error in RX dma configuration.\n");
1316 goto err;
1317 }
1318
Fabien Lahouderedb0a1962021-04-30 19:50:37 +02001319 sport->rx_buf_size = sport->rx_period_length * sport->rx_periods;
1320 sport->rx_buf = kzalloc(sport->rx_buf_size, GFP_KERNEL);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001321 if (!sport->rx_buf) {
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001322 ret = -ENOMEM;
1323 goto err;
1324 }
Nandor Han9d297232016-08-08 15:38:27 +03001325 sport->rx_ring.buf = sport->rx_buf;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001326
1327 /* Prepare for TX : */
1328 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1329 if (!sport->dma_chan_tx) {
1330 dev_err(dev, "cannot get the TX DMA channel!\n");
1331 ret = -EINVAL;
1332 goto err;
1333 }
1334
1335 slave_config.direction = DMA_MEM_TO_DEV;
1336 slave_config.dst_addr = sport->port.mapbase + URTX0;
1337 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
Lucas Stach184bd702015-09-04 17:52:40 +02001338 slave_config.dst_maxburst = TXTL_DMA;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001339 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1340 if (ret) {
1341 dev_err(dev, "error in TX dma configuration.");
1342 goto err;
1343 }
1344
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001345 return 0;
1346err:
1347 imx_uart_dma_exit(sport);
1348 return ret;
1349}
1350
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001351static void imx_uart_enable_dma(struct imx_port *sport)
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001352{
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001353 u32 ucr1;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001354
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001355 imx_uart_setup_ufcr(sport, TXTL_DMA, RXTL_DMA);
Uwe Kleine-König02b0abd32018-03-02 11:07:24 +01001356
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001357 /* set UCR1 */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001358 ucr1 = imx_uart_readl(sport, UCR1);
1359 ucr1 |= UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN;
1360 imx_uart_writel(sport, ucr1, UCR1);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001361
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001362 sport->dma_is_enabled = 1;
1363}
1364
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001365static void imx_uart_disable_dma(struct imx_port *sport)
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001366{
Sebastian Reichel676a31d2018-05-07 23:36:09 +02001367 u32 ucr1;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001368
1369 /* clear UCR1 */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001370 ucr1 = imx_uart_readl(sport, UCR1);
1371 ucr1 &= ~(UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN);
1372 imx_uart_writel(sport, ucr1, UCR1);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001373
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001374 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
Lucas Stach184bd702015-09-04 17:52:40 +02001375
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001376 sport->dma_is_enabled = 0;
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001377}
1378
Valentin Longchamp1c5250d2010-05-05 11:47:07 +02001379/* half the RX buffer size */
1380#define CTSTL 16
1381
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001382static int imx_uart_startup(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct imx_port *sport = (struct imx_port *)port;
Fabio Estevam458e2c82015-07-27 15:15:59 -03001385 int retval, i;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001386 unsigned long flags;
Uwe Kleine-König4238c002018-02-18 22:02:45 +01001387 int dma_is_inited = 0;
George Hilliard5a08a482020-02-26 16:23:19 -06001388 u32 ucr1, ucr2, ucr3, ucr4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Huang Shijie1cf93e02013-06-28 13:39:42 +08001390 retval = clk_prepare_enable(sport->clk_per);
1391 if (retval)
Fabio Estevamcb0f0a52014-10-27 14:49:38 -02001392 return retval;
Huang Shijie1cf93e02013-06-28 13:39:42 +08001393 retval = clk_prepare_enable(sport->clk_ipg);
1394 if (retval) {
1395 clk_disable_unprepare(sport->clk_per);
Fabio Estevamcb0f0a52014-10-27 14:49:38 -02001396 return retval;
Huang Shijie0c375502013-06-09 10:01:19 +08001397 }
Huang Shijie28eb4272013-06-04 09:59:33 +08001398
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001399 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /* disable the DREN bit (Data Ready interrupt enable) before
1402 * requesting IRQs
1403 */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001404 ucr4 = imx_uart_readl(sport, UCR4);
Fabian Godehardtb6e49132009-06-11 14:53:18 +01001405
Valentin Longchamp1c5250d2010-05-05 11:47:07 +02001406 /* set the trigger level for CTS */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001407 ucr4 &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
1408 ucr4 |= CTSTL << UCR4_CTSTL_SHF;
Valentin Longchamp1c5250d2010-05-05 11:47:07 +02001409
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001410 imx_uart_writel(sport, ucr4 & ~UCR4_DREN, UCR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Lucas Stach7e115772015-09-04 17:52:42 +02001412 /* Can we enable the DMA support? */
Uwe Kleine-König4238c002018-02-18 22:02:45 +01001413 if (!uart_console(port) && imx_uart_dma_init(sport) == 0)
1414 dma_is_inited = 1;
Lucas Stach7e115772015-09-04 17:52:42 +02001415
Jiada Wang53794182015-04-13 18:31:43 +09001416 spin_lock_irqsave(&sport->port.lock, flags);
Huang Shijie772f8992014-05-21 08:56:28 +08001417 /* Reset fifo's and state machines */
Fabio Estevam458e2c82015-07-27 15:15:59 -03001418 i = 100;
1419
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001420 ucr2 = imx_uart_readl(sport, UCR2);
1421 ucr2 &= ~UCR2_SRST;
1422 imx_uart_writel(sport, ucr2, UCR2);
Fabio Estevam458e2c82015-07-27 15:15:59 -03001423
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001424 while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0))
Fabio Estevam458e2c82015-07-27 15:15:59 -03001425 udelay(1);
Fabian Godehardtb6e49132009-06-11 14:53:18 +01001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * Finally, clear and enable interrupts
1429 */
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001430 imx_uart_writel(sport, USR1_RTSD | USR1_DTRD, USR1);
1431 imx_uart_writel(sport, USR2_ORE, USR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001433 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_RRDYEN;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001434 ucr1 |= UCR1_UARTEN;
Nandor Han6376cd32017-06-28 15:59:36 +02001435 if (sport->have_rtscts)
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001436 ucr1 |= UCR1_RTSDEN;
Fabian Godehardtb6e49132009-06-11 14:53:18 +01001437
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001438 imx_uart_writel(sport, ucr1, UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
George Hilliard5a08a482020-02-26 16:23:19 -06001440 ucr4 = imx_uart_readl(sport, UCR4) & ~(UCR4_OREN | UCR4_INVR);
Troy Kisky1f043572017-11-16 11:14:53 -07001441 if (!sport->dma_is_enabled)
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001442 ucr4 |= UCR4_OREN;
George Hilliard5a08a482020-02-26 16:23:19 -06001443 if (sport->inverted_rx)
1444 ucr4 |= UCR4_INVR;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001445 imx_uart_writel(sport, ucr4, UCR4);
Jiada Wang6f026d6b2014-12-09 18:11:34 +09001446
George Hilliard5a08a482020-02-26 16:23:19 -06001447 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_INVT;
1448 /*
1449 * configure tx polarity before enabling tx
1450 */
1451 if (sport->inverted_tx)
1452 ucr3 |= UCR3_INVT;
1453
1454 if (!imx_uart_is_imx1(sport)) {
1455 ucr3 |= UCR3_DTRDEN | UCR3_RI | UCR3_DCD;
1456
1457 if (sport->dte_mode)
1458 /* disable broken interrupts */
1459 ucr3 &= ~(UCR3_RI | UCR3_DCD);
1460 }
1461 imx_uart_writel(sport, ucr3, UCR3);
1462
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001463 ucr2 = imx_uart_readl(sport, UCR2) & ~UCR2_ATEN;
1464 ucr2 |= (UCR2_RXEN | UCR2_TXEN);
Lucas Stachbff09b02013-05-30 15:47:04 +02001465 if (!sport->have_rtscts)
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001466 ucr2 |= UCR2_IRTS;
Uwe Kleine-König16804d62016-03-24 14:24:22 +01001467 /*
1468 * make sure the edge sensitive RTS-irq is disabled,
1469 * we're using RTSD instead.
1470 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001471 if (!imx_uart_is_imx1(sport))
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001472 ucr2 &= ~UCR2_RTSEN;
1473 imx_uart_writel(sport, ucr2, UCR2);
Sascha Hauerff4bfb22007-04-26 08:26:13 +01001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /*
1476 * Enable modem status interrupts
1477 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001478 imx_uart_enable_ms(&sport->port);
Peter Senna Tschudin18a42082017-04-07 11:45:24 +02001479
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001480 if (dma_is_inited) {
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001481 imx_uart_enable_dma(sport);
1482 imx_uart_start_rx_dma(sport);
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001483 } else {
1484 ucr1 = imx_uart_readl(sport, UCR1);
1485 ucr1 |= UCR1_RRDYEN;
1486 imx_uart_writel(sport, ucr1, UCR1);
Uwe Kleine-König81ca8e82018-03-02 11:07:27 +01001487
1488 ucr2 = imx_uart_readl(sport, UCR2);
1489 ucr2 |= UCR2_ATEN;
1490 imx_uart_writel(sport, ucr2, UCR2);
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001491 }
Peter Senna Tschudin18a42082017-04-07 11:45:24 +02001492
Sachin Kamat82313e62013-01-07 10:25:02 +05301493 spin_unlock_irqrestore(&sport->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001498static void imx_uart_shutdown(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct imx_port *sport = (struct imx_port *)port;
Xinyu Chen9ec18822012-08-27 09:36:51 +02001501 unsigned long flags;
Sebastian Reichel339c7a82018-05-24 19:30:24 +02001502 u32 ucr1, ucr2, ucr4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001504 if (sport->dma_is_enabled) {
Fabien Lahouderee5e89602016-09-13 10:17:05 +02001505 dmaengine_terminate_sync(sport->dma_chan_tx);
Sebastian Reichel7722c242018-05-07 23:36:10 +02001506 if (sport->dma_is_txing) {
1507 dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
1508 sport->dma_tx_nents, DMA_TO_DEVICE);
1509 sport->dma_is_txing = 0;
1510 }
Fabien Lahouderee5e89602016-09-13 10:17:05 +02001511 dmaengine_terminate_sync(sport->dma_chan_rx);
Sebastian Reichel7722c242018-05-07 23:36:10 +02001512 if (sport->dma_is_rxing) {
1513 dma_unmap_sg(sport->port.dev, &sport->rx_sgl,
1514 1, DMA_FROM_DEVICE);
1515 sport->dma_is_rxing = 0;
1516 }
Huang Shijiea4688bc2014-09-19 15:42:57 +08001517
Jiada Wang73631812014-12-09 18:11:23 +09001518 spin_lock_irqsave(&sport->port.lock, flags);
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001519 imx_uart_stop_tx(port);
1520 imx_uart_stop_rx(port);
1521 imx_uart_disable_dma(sport);
Jiada Wang73631812014-12-09 18:11:23 +09001522 spin_unlock_irqrestore(&sport->port.lock, flags);
Huang Shijieb4cdc8f2013-07-08 17:14:18 +08001523 imx_uart_dma_exit(sport);
1524 }
1525
Uwe Kleine-König58362d52015-12-13 11:30:03 +01001526 mctrl_gpio_disable_ms(sport->gpios);
1527
Xinyu Chen9ec18822012-08-27 09:36:51 +02001528 spin_lock_irqsave(&sport->port.lock, flags);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001529 ucr2 = imx_uart_readl(sport, UCR2);
Sebastian Reichel0fdf1782018-05-24 19:30:23 +02001530 ucr2 &= ~(UCR2_TXEN | UCR2_ATEN);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001531 imx_uart_writel(sport, ucr2, UCR2);
Xinyu Chen9ec18822012-08-27 09:36:51 +02001532 spin_unlock_irqrestore(&sport->port.lock, flags);
Fabian Godehardt2e146392009-06-11 14:38:38 +01001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /*
1535 * Stop our timer.
1536 */
1537 del_timer_sync(&sport->timer);
1538
1539 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * Disable all interrupts, port and break condition.
1541 */
1542
Xinyu Chen9ec18822012-08-27 09:36:51 +02001543 spin_lock_irqsave(&sport->port.lock, flags);
Matthias Schifferedd64f32020-09-25 10:24:12 +02001544
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001545 ucr1 = imx_uart_readl(sport, UCR1);
Sergey Organovc514a6f2019-08-28 21:37:55 +03001546 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN | UCR1_RXDMAEN | UCR1_ATDMAEN);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001547 imx_uart_writel(sport, ucr1, UCR1);
Matthias Schifferedd64f32020-09-25 10:24:12 +02001548
1549 ucr4 = imx_uart_readl(sport, UCR4);
Fugang Duan028e0832021-11-25 10:03:49 +08001550 ucr4 &= ~UCR4_TCEN;
Matthias Schifferedd64f32020-09-25 10:24:12 +02001551 imx_uart_writel(sport, ucr4, UCR4);
1552
Xinyu Chen9ec18822012-08-27 09:36:51 +02001553 spin_unlock_irqrestore(&sport->port.lock, flags);
Huang Shijie28eb4272013-06-04 09:59:33 +08001554
Huang Shijie1cf93e02013-06-28 13:39:42 +08001555 clk_disable_unprepare(sport->clk_per);
1556 clk_disable_unprepare(sport->clk_ipg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +01001559/* called with port.lock taken and irqs off */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001560static void imx_uart_flush_buffer(struct uart_port *port)
Huang Shijieeb56b7e2013-10-11 18:30:58 +08001561{
1562 struct imx_port *sport = (struct imx_port *)port;
Dirk Behme82e86ae2014-12-09 18:11:27 +09001563 struct scatterlist *sgl = &sport->tx_sgl[0];
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001564 u32 ucr2;
Fabio Estevam4f86a952015-02-07 15:46:41 -02001565 int i = 100, ubir, ubmr, uts;
Huang Shijieeb56b7e2013-10-11 18:30:58 +08001566
Dirk Behme82e86ae2014-12-09 18:11:27 +09001567 if (!sport->dma_chan_tx)
1568 return;
1569
1570 sport->tx_bytes = 0;
1571 dmaengine_terminate_all(sport->dma_chan_tx);
1572 if (sport->dma_is_txing) {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001573 u32 ucr1;
1574
Dirk Behme82e86ae2014-12-09 18:11:27 +09001575 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
1576 DMA_TO_DEVICE);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001577 ucr1 = imx_uart_readl(sport, UCR1);
1578 ucr1 &= ~UCR1_TXDMAEN;
1579 imx_uart_writel(sport, ucr1, UCR1);
Martyn Welch0f7bdbd2017-09-28 11:38:51 +01001580 sport->dma_is_txing = 0;
Huang Shijieeb56b7e2013-10-11 18:30:58 +08001581 }
Fabio Estevam934084a2015-01-13 10:00:26 -02001582
1583 /*
1584 * According to the Reference Manual description of the UART SRST bit:
Martyn Welch263763c2017-10-04 17:13:27 +01001585 *
Fabio Estevam934084a2015-01-13 10:00:26 -02001586 * "Reset the transmit and receive state machines,
1587 * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
Martyn Welch263763c2017-10-04 17:13:27 +01001588 * and UTS[6-3]".
1589 *
1590 * We don't need to restore the old values from USR1, USR2, URXD and
1591 * UTXD. UBRC is read only, so only save/restore the other three
1592 * registers.
Fabio Estevam934084a2015-01-13 10:00:26 -02001593 */
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001594 ubir = imx_uart_readl(sport, UBIR);
1595 ubmr = imx_uart_readl(sport, UBMR);
1596 uts = imx_uart_readl(sport, IMX21_UTS);
Fabio Estevam934084a2015-01-13 10:00:26 -02001597
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001598 ucr2 = imx_uart_readl(sport, UCR2);
1599 ucr2 &= ~UCR2_SRST;
1600 imx_uart_writel(sport, ucr2, UCR2);
Fabio Estevam934084a2015-01-13 10:00:26 -02001601
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001602 while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0))
Fabio Estevam934084a2015-01-13 10:00:26 -02001603 udelay(1);
1604
1605 /* Restore the registers */
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001606 imx_uart_writel(sport, ubir, UBIR);
1607 imx_uart_writel(sport, ubmr, UBMR);
1608 imx_uart_writel(sport, uts, IMX21_UTS);
Huang Shijieeb56b7e2013-10-11 18:30:58 +08001609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611static void
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001612imx_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1613 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 struct imx_port *sport = (struct imx_port *)port;
1616 unsigned long flags;
Sergey Organov85f30fb2019-08-28 21:37:53 +03001617 u32 ucr2, old_ucr2, ufcr;
Uwe Kleine-König58362d52015-12-13 11:30:03 +01001618 unsigned int baud, quot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001620 unsigned long div;
Sergey Organovd47bcb42019-08-28 21:37:54 +03001621 unsigned long num, denom, old_ubir, old_ubmr;
Oskar Schirmerd7f8d432009-06-11 14:55:22 +01001622 uint64_t tdiv64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * We only support CS7 and CS8.
1626 */
1627 while ((termios->c_cflag & CSIZE) != CS7 &&
1628 (termios->c_cflag & CSIZE) != CS8) {
1629 termios->c_cflag &= ~CSIZE;
1630 termios->c_cflag |= old_csize;
1631 old_csize = CS8;
1632 }
1633
Sergey Organov4e828c32019-06-11 15:05:24 +03001634 del_timer_sync(&sport->timer);
1635
1636 /*
1637 * Ask the core to calculate the divisor for us.
1638 */
1639 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1640 quot = uart_get_divisor(port, baud);
1641
1642 spin_lock_irqsave(&sport->port.lock, flags);
1643
Sergey Organov011bd052019-06-26 17:11:30 +03001644 /*
1645 * Read current UCR2 and save it for future use, then clear all the bits
1646 * except those we will or may need to preserve.
1647 */
1648 old_ucr2 = imx_uart_readl(sport, UCR2);
1649 ucr2 = old_ucr2 & (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN | UCR2_CTS);
1650
1651 ucr2 |= UCR2_SRST | UCR2_IRTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if ((termios->c_cflag & CSIZE) == CS8)
Sergey Organov41ffa482019-06-26 17:11:28 +03001653 ucr2 |= UCR2_WS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Sergey Organovddf89e72019-06-26 17:11:29 +03001655 if (!sport->have_rtscts)
1656 termios->c_cflag &= ~CRTSCTS;
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001657
Sergey Organovddf89e72019-06-26 17:11:29 +03001658 if (port->rs485.flags & SER_RS485_ENABLED) {
1659 /*
1660 * RTS is mandatory for rs485 operation, so keep
1661 * it under manual control and keep transmitter
1662 * disabled.
1663 */
Uwe Kleine-König58362d52015-12-13 11:30:03 +01001664 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001665 imx_uart_rts_active(sport, &ucr2);
Fabio Estevam1a613622017-01-30 09:12:11 -02001666 else
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001667 imx_uart_rts_inactive(sport, &ucr2);
Uwe Kleine-König58362d52015-12-13 11:30:03 +01001668
Sergey Organovb777b5d2019-07-26 21:52:41 +03001669 } else if (termios->c_cflag & CRTSCTS) {
1670 /*
1671 * Only let receiver control RTS output if we were not requested
1672 * to have RTS inactive (which then should take precedence).
1673 */
1674 if (ucr2 & UCR2_CTS)
1675 ucr2 |= UCR2_CTSC;
1676 }
Sergey Organovddf89e72019-06-26 17:11:29 +03001677
1678 if (termios->c_cflag & CRTSCTS)
1679 ucr2 &= ~UCR2_IRTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (termios->c_cflag & CSTOPB)
1681 ucr2 |= UCR2_STPB;
1682 if (termios->c_cflag & PARENB) {
1683 ucr2 |= UCR2_PREN;
Matt Reimer3261e362006-01-13 20:51:44 +00001684 if (termios->c_cflag & PARODD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 ucr2 |= UCR2_PROE;
1686 }
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 sport->port.read_status_mask = 0;
1689 if (termios->c_iflag & INPCK)
1690 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
1691 if (termios->c_iflag & (BRKINT | PARMRK))
1692 sport->port.read_status_mask |= URXD_BRK;
1693
1694 /*
1695 * Characters to ignore
1696 */
1697 sport->port.ignore_status_mask = 0;
1698 if (termios->c_iflag & IGNPAR)
Eric Nelson865cea82014-12-18 12:37:14 -07001699 sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (termios->c_iflag & IGNBRK) {
1701 sport->port.ignore_status_mask |= URXD_BRK;
1702 /*
1703 * If we're ignoring parity and break indicators,
1704 * ignore overruns too (for real raw support).
1705 */
1706 if (termios->c_iflag & IGNPAR)
1707 sport->port.ignore_status_mask |= URXD_OVRRUN;
1708 }
1709
Jiada Wang55d86932014-12-09 18:11:22 +09001710 if ((termios->c_cflag & CREAD) == 0)
1711 sport->port.ignore_status_mask |= URXD_DUMMY_READ;
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
1714 * Update the per-port timeout.
1715 */
1716 uart_update_timeout(port, termios->c_cflag, baud);
1717
Uwe Kleine-Königafe9cbb2015-02-24 11:17:10 +01001718 /* custom-baudrate handling */
1719 div = sport->port.uartclk / (baud * 16);
1720 if (baud == 38400 && quot != div)
1721 baud = sport->port.uartclk / (quot * 16);
Hubert Feurstein09bd00f2013-07-18 18:52:49 +02001722
Uwe Kleine-Königafe9cbb2015-02-24 11:17:10 +01001723 div = sport->port.uartclk / (baud * 16);
1724 if (div > 7)
1725 div = 7;
1726 if (!div)
1727 div = 1;
Sascha Hauer036bb152008-07-05 10:02:44 +02001728
Oskar Schirmer534fca02009-06-11 14:52:23 +01001729 rational_best_approximation(16 * div * baud, sport->port.uartclk,
1730 1 << 16, 1 << 16, &num, &denom);
Sascha Hauer036bb152008-07-05 10:02:44 +02001731
Alan Coxeab4f5a2010-06-01 22:52:52 +02001732 tdiv64 = sport->port.uartclk;
1733 tdiv64 *= num;
1734 do_div(tdiv64, denom * 16 * div);
1735 tty_termios_encode_baud_rate(termios,
Sascha Hauer1a2c4b32009-06-16 17:02:15 +01001736 (speed_t)tdiv64, (speed_t)tdiv64);
Oskar Schirmerd7f8d432009-06-11 14:55:22 +01001737
Oskar Schirmer534fca02009-06-11 14:52:23 +01001738 num -= 1;
1739 denom -= 1;
Sascha Hauer036bb152008-07-05 10:02:44 +02001740
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001741 ufcr = imx_uart_readl(sport, UFCR);
Fabian Godehardtb6e49132009-06-11 14:53:18 +01001742 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001743 imx_uart_writel(sport, ufcr, UFCR);
Sascha Hauer036bb152008-07-05 10:02:44 +02001744
Sergey Organovd47bcb42019-08-28 21:37:54 +03001745 /*
1746 * Two registers below should always be written both and in this
1747 * particular order. One consequence is that we need to check if any of
1748 * them changes and then update both. We do need the check for change
1749 * as even writing the same values seem to "restart"
1750 * transmission/receiving logic in the hardware, that leads to data
1751 * breakage even when rate doesn't in fact change. E.g., user switches
1752 * RTS/CTS handshake and suddenly gets broken bytes.
1753 */
1754 old_ubir = imx_uart_readl(sport, UBIR);
1755 old_ubmr = imx_uart_readl(sport, UBMR);
1756 if (old_ubir != num || old_ubmr != denom) {
1757 imx_uart_writel(sport, num, UBIR);
1758 imx_uart_writel(sport, denom, UBMR);
1759 }
Oskar Schirmer534fca02009-06-11 14:52:23 +01001760
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001761 if (!imx_uart_is_imx1(sport))
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001762 imx_uart_writel(sport, sport->port.uartclk / div / 1000,
1763 IMX21_ONEMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Sergey Organov011bd052019-06-26 17:11:30 +03001765 imx_uart_writel(sport, ucr2, UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001768 imx_uart_enable_ms(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 spin_unlock_irqrestore(&sport->port.lock, flags);
1771}
1772
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001773static const char *imx_uart_type(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct imx_port *sport = (struct imx_port *)port;
1776
1777 return sport->port.type == PORT_IMX ? "IMX" : NULL;
1778}
1779
1780/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 * Configure/autoconfigure the port.
1782 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001783static void imx_uart_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct imx_port *sport = (struct imx_port *)port;
1786
Alexander Shiyanda82f992014-02-22 16:01:33 +04001787 if (flags & UART_CONFIG_TYPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 sport->port.type = PORT_IMX;
1789}
1790
1791/*
1792 * Verify the new serial_struct (for TIOCSSERIAL).
1793 * The only change we allow are to the flags and type, and
1794 * even then only between PORT_IMX and PORT_UNKNOWN
1795 */
1796static int
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001797imx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 struct imx_port *sport = (struct imx_port *)port;
1800 int ret = 0;
1801
1802 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1803 ret = -EINVAL;
1804 if (sport->port.irq != ser->irq)
1805 ret = -EINVAL;
1806 if (ser->io_type != UPIO_MEM)
1807 ret = -EINVAL;
1808 if (sport->port.uartclk / 16 != ser->baud_base)
1809 ret = -EINVAL;
Olof Johanssona50c44c2013-09-11 21:27:53 -07001810 if (sport->port.mapbase != (unsigned long)ser->iomem_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 ret = -EINVAL;
1812 if (sport->port.iobase != ser->port)
1813 ret = -EINVAL;
1814 if (ser->hub6 != 0)
1815 ret = -EINVAL;
1816 return ret;
1817}
1818
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001819#if defined(CONFIG_CONSOLE_POLL)
Daniel Thompson6b8bdad2014-10-28 09:28:08 +01001820
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001821static int imx_uart_poll_init(struct uart_port *port)
Daniel Thompson6b8bdad2014-10-28 09:28:08 +01001822{
1823 struct imx_port *sport = (struct imx_port *)port;
1824 unsigned long flags;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001825 u32 ucr1, ucr2;
Daniel Thompson6b8bdad2014-10-28 09:28:08 +01001826 int retval;
1827
1828 retval = clk_prepare_enable(sport->clk_ipg);
1829 if (retval)
1830 return retval;
1831 retval = clk_prepare_enable(sport->clk_per);
1832 if (retval)
1833 clk_disable_unprepare(sport->clk_ipg);
1834
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001835 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
Daniel Thompson6b8bdad2014-10-28 09:28:08 +01001836
1837 spin_lock_irqsave(&sport->port.lock, flags);
1838
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001839 /*
1840 * Be careful about the order of enabling bits here. First enable the
1841 * receiver (UARTEN + RXEN) and only then the corresponding irqs.
1842 * This prevents that a character that already sits in the RX fifo is
1843 * triggering an irq but the try to fetch it from there results in an
1844 * exception because UARTEN or RXEN is still off.
1845 */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001846 ucr1 = imx_uart_readl(sport, UCR1);
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001847 ucr2 = imx_uart_readl(sport, UCR2);
1848
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001849 if (imx_uart_is_imx1(sport))
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001850 ucr1 |= IMX1_UCR1_UARTCLKEN;
Daniel Thompson6b8bdad2014-10-28 09:28:08 +01001851
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001852 ucr1 |= UCR1_UARTEN;
Sergey Organovc514a6f2019-08-28 21:37:55 +03001853 ucr1 &= ~(UCR1_TRDYEN | UCR1_RTSDEN | UCR1_RRDYEN);
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001854
Mingrui Renaef1b6a2020-12-02 15:25:43 +08001855 ucr2 |= UCR2_RXEN | UCR2_TXEN;
Uwe Kleine-König81ca8e82018-03-02 11:07:27 +01001856 ucr2 &= ~UCR2_ATEN;
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001857
1858 imx_uart_writel(sport, ucr1, UCR1);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001859 imx_uart_writel(sport, ucr2, UCR2);
Daniel Thompson6b8bdad2014-10-28 09:28:08 +01001860
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001861 /* now enable irqs */
1862 imx_uart_writel(sport, ucr1 | UCR1_RRDYEN, UCR1);
Uwe Kleine-König81ca8e82018-03-02 11:07:27 +01001863 imx_uart_writel(sport, ucr2 | UCR2_ATEN, UCR2);
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001864
Daniel Thompson6b8bdad2014-10-28 09:28:08 +01001865 spin_unlock_irqrestore(&sport->port.lock, flags);
1866
1867 return 0;
1868}
1869
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001870static int imx_uart_poll_get_char(struct uart_port *port)
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001871{
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001872 struct imx_port *sport = (struct imx_port *)port;
1873 if (!(imx_uart_readl(sport, USR2) & USR2_RDR))
Dirk Behme26c47412014-09-03 12:33:53 +01001874 return NO_POLL_CHAR;
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001875
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001876 return imx_uart_readl(sport, URXD0) & URXD_RX_DATA;
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001877}
1878
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001879static void imx_uart_poll_put_char(struct uart_port *port, unsigned char c)
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001880{
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001881 struct imx_port *sport = (struct imx_port *)port;
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001882 unsigned int status;
1883
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001884 /* drain */
1885 do {
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001886 status = imx_uart_readl(sport, USR1);
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001887 } while (~status & USR1_TRDY);
1888
1889 /* write */
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001890 imx_uart_writel(sport, c, URTX0);
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001891
1892 /* flush */
1893 do {
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001894 status = imx_uart_readl(sport, USR2);
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001895 } while (~status & USR2_TXDC);
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001896}
1897#endif
1898
Uwe Kleine-König6aed2a82018-02-27 22:44:56 +01001899/* called with port.lock taken and irqs off or from .probe without locking */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001900static int imx_uart_rs485_config(struct uart_port *port,
1901 struct serial_rs485 *rs485conf)
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001902{
1903 struct imx_port *sport = (struct imx_port *)port;
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001904 u32 ucr2;
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001905
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001906 /* RTS is required to control the transmitter */
Fabio Estevam7b7e8e82017-01-07 19:29:13 -02001907 if (!sport->have_rtscts && !sport->have_rtsgpio)
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001908 rs485conf->flags &= ~SER_RS485_ENABLED;
1909
1910 if (rs485conf->flags & SER_RS485_ENABLED) {
Stefan Agner6d215f82018-04-19 17:39:16 +02001911 /* Enable receiver if low-active RTS signal is requested */
1912 if (sport->have_rtscts && !sport->have_rtsgpio &&
1913 !(rs485conf->flags & SER_RS485_RTS_ON_SEND))
1914 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1915
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001916 /* disable transmitter */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001917 ucr2 = imx_uart_readl(sport, UCR2);
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001918 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001919 imx_uart_rts_active(sport, &ucr2);
Fabio Estevam1a613622017-01-30 09:12:11 -02001920 else
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001921 imx_uart_rts_inactive(sport, &ucr2);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01001922 imx_uart_writel(sport, ucr2, UCR2);
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001923 }
1924
Baruch Siach7d1cadc2016-02-29 14:34:10 +02001925 /* Make sure Rx is enabled in case Tx is active with Rx disabled */
1926 if (!(rs485conf->flags & SER_RS485_ENABLED) ||
Uwe Kleine-König76821e22018-03-02 11:07:26 +01001927 rs485conf->flags & SER_RS485_RX_DURING_TX)
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001928 imx_uart_start_rx(port);
Baruch Siach7d1cadc2016-02-29 14:34:10 +02001929
Uwe Kleine-König17b8f2a2015-02-24 11:17:11 +01001930 port->rs485 = *rs485conf;
1931
1932 return 0;
1933}
1934
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001935static const struct uart_ops imx_uart_pops = {
1936 .tx_empty = imx_uart_tx_empty,
1937 .set_mctrl = imx_uart_set_mctrl,
1938 .get_mctrl = imx_uart_get_mctrl,
1939 .stop_tx = imx_uart_stop_tx,
1940 .start_tx = imx_uart_start_tx,
1941 .stop_rx = imx_uart_stop_rx,
1942 .enable_ms = imx_uart_enable_ms,
1943 .break_ctl = imx_uart_break_ctl,
1944 .startup = imx_uart_startup,
1945 .shutdown = imx_uart_shutdown,
1946 .flush_buffer = imx_uart_flush_buffer,
1947 .set_termios = imx_uart_set_termios,
1948 .type = imx_uart_type,
1949 .config_port = imx_uart_config_port,
1950 .verify_port = imx_uart_verify_port,
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001951#if defined(CONFIG_CONSOLE_POLL)
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001952 .poll_init = imx_uart_poll_init,
1953 .poll_get_char = imx_uart_poll_get_char,
1954 .poll_put_char = imx_uart_poll_put_char,
Saleem Abdulrasool01f56ab2011-12-22 09:57:53 +01001955#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956};
1957
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001958static struct imx_port *imx_uart_ports[UART_NR];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Fugang Duan0db4f9b2020-07-24 15:08:14 +08001960#if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001961static void imx_uart_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00001962{
1963 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +01001964
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001965 while (imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)
Russell Kingd3587882006-03-20 20:00:09 +00001966 barrier();
Sascha Hauerff4bfb22007-04-26 08:26:13 +01001967
Uwe Kleine-König27c84422018-03-02 11:07:19 +01001968 imx_uart_writel(sport, ch, URTX0);
Russell Kingd3587882006-03-20 20:00:09 +00001969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971/*
1972 * Interrupts are disabled on entering
1973 */
1974static void
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001975imx_uart_console_write(struct console *co, const char *s, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001977 struct imx_port *sport = imx_uart_ports[co->index];
Dirk Behme0ad5a812011-12-22 09:57:52 +01001978 struct imx_port_ucrs old_ucr;
Johan Hovold18ee37e2021-05-19 11:25:41 +02001979 unsigned long flags;
Dirk Behme0ad5a812011-12-22 09:57:52 +01001980 unsigned int ucr1;
Thomas Gleixner677fe552013-02-14 21:01:06 +01001981 int locked = 1;
Xinyu Chen9ec18822012-08-27 09:36:51 +02001982
Thomas Gleixner677fe552013-02-14 21:01:06 +01001983 if (sport->port.sysrq)
1984 locked = 0;
1985 else if (oops_in_progress)
1986 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1987 else
1988 spin_lock_irqsave(&sport->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
Dirk Behme0ad5a812011-12-22 09:57:52 +01001991 * First, save UCR1/2/3 and then disable interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001993 imx_uart_ucrs_save(sport, &old_ucr);
Dirk Behme0ad5a812011-12-22 09:57:52 +01001994 ucr1 = old_ucr.ucr1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01001996 if (imx_uart_is_imx1(sport))
Shawn Guofe6b5402011-06-25 02:04:33 +08001997 ucr1 |= IMX1_UCR1_UARTCLKEN;
Sascha Hauer37d6fb62009-05-27 18:23:48 +02001998 ucr1 |= UCR1_UARTEN;
Sergey Organovc514a6f2019-08-28 21:37:55 +03001999 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN);
Sascha Hauer37d6fb62009-05-27 18:23:48 +02002000
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002001 imx_uart_writel(sport, ucr1, UCR1);
Sascha Hauerff4bfb22007-04-26 08:26:13 +01002002
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002003 imx_uart_writel(sport, old_ucr.ucr2 | UCR2_TXEN, UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002005 uart_console_write(&sport->port, s, count, imx_uart_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 /*
2008 * Finally, wait for transmitter to become empty
Dirk Behme0ad5a812011-12-22 09:57:52 +01002009 * and restore UCR1/2/3
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 */
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002011 while (!(imx_uart_readl(sport, USR2) & USR2_TXDC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002013 imx_uart_ucrs_restore(sport, &old_ucr);
Xinyu Chen9ec18822012-08-27 09:36:51 +02002014
Thomas Gleixner677fe552013-02-14 21:01:06 +01002015 if (locked)
2016 spin_unlock_irqrestore(&sport->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
2019/*
2020 * If the port was already initialised (eg, by a boot loader),
2021 * try to determine the current setup.
2022 */
Stefan Agner6d0d1b52021-10-20 21:26:42 +02002023static void
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002024imx_uart_console_get_options(struct imx_port *sport, int *baud,
2025 int *parity, int *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Sascha Hauer587897f2005-04-29 22:46:40 +01002027
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002028 if (imx_uart_readl(sport, UCR1) & UCR1_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /* ok, the port was enabled */
Sachin Kamat82313e62013-01-07 10:25:02 +05302030 unsigned int ucr2, ubir, ubmr, uartclk;
Sascha Hauer587897f2005-04-29 22:46:40 +01002031 unsigned int baud_raw;
2032 unsigned int ucfr_rfdiv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002034 ucr2 = imx_uart_readl(sport, UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 *parity = 'n';
2037 if (ucr2 & UCR2_PREN) {
2038 if (ucr2 & UCR2_PROE)
2039 *parity = 'o';
2040 else
2041 *parity = 'e';
2042 }
2043
2044 if (ucr2 & UCR2_WS)
2045 *bits = 8;
2046 else
2047 *bits = 7;
2048
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002049 ubir = imx_uart_readl(sport, UBIR) & 0xffff;
2050 ubmr = imx_uart_readl(sport, UBMR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002052 ucfr_rfdiv = (imx_uart_readl(sport, UFCR) & UFCR_RFDIV) >> 7;
Sascha Hauer587897f2005-04-29 22:46:40 +01002053 if (ucfr_rfdiv == 6)
2054 ucfr_rfdiv = 7;
2055 else
2056 ucfr_rfdiv = 6 - ucfr_rfdiv;
2057
Sascha Hauer3a9465f2012-03-07 09:31:43 +01002058 uartclk = clk_get_rate(sport->clk_per);
Sascha Hauer587897f2005-04-29 22:46:40 +01002059 uartclk /= ucfr_rfdiv;
2060
2061 { /*
2062 * The next code provides exact computation of
2063 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
2064 * without need of float support or long long division,
2065 * which would be required to prevent 32bit arithmetic overflow
2066 */
2067 unsigned int mul = ubir + 1;
2068 unsigned int div = 16 * (ubmr + 1);
2069 unsigned int rem = uartclk % div;
2070
2071 baud_raw = (uartclk / div) * mul;
2072 baud_raw += (rem * mul + div / 2) / div;
2073 *baud = (baud_raw + 50) / 100 * 100;
2074 }
2075
Sachin Kamat82313e62013-01-07 10:25:02 +05302076 if (*baud != baud_raw)
Fabio Estevamf5a9e5f2019-06-04 00:31:39 -03002077 dev_info(sport->port.dev, "Console IMX rounded baud rate from %d to %d\n",
Sascha Hauer587897f2005-04-29 22:46:40 +01002078 baud_raw, *baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080}
2081
Stefan Agner6d0d1b52021-10-20 21:26:42 +02002082static int
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002083imx_uart_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct imx_port *sport;
2086 int baud = 9600;
2087 int bits = 8;
2088 int parity = 'n';
2089 int flow = 'n';
Huang Shijie1cf93e02013-06-28 13:39:42 +08002090 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 /*
2093 * Check whether an invalid uart number has been specified, and
2094 * if so, search for the first available port that does have
2095 * console support.
2096 */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002097 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_uart_ports))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 co->index = 0;
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002099 sport = imx_uart_ports[co->index];
Sachin Kamat82313e62013-01-07 10:25:02 +05302100 if (sport == NULL)
Eric Lammertse76afc42009-05-19 20:53:20 -04002101 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Huang Shijie1cf93e02013-06-28 13:39:42 +08002103 /* For setting the registers, we only need to enable the ipg clock. */
2104 retval = clk_prepare_enable(sport->clk_ipg);
2105 if (retval)
2106 goto error_console;
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (options)
2109 uart_parse_options(options, &baud, &parity, &bits, &flow);
2110 else
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002111 imx_uart_console_get_options(sport, &baud, &parity, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002113 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
Sascha Hauer587897f2005-04-29 22:46:40 +01002114
Huang Shijie1cf93e02013-06-28 13:39:42 +08002115 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
2116
Fabio Estevam0c727a42015-08-18 12:43:12 -03002117 if (retval) {
Fugang Duane67c1392020-11-11 10:51:36 +08002118 clk_disable_unprepare(sport->clk_ipg);
Fabio Estevam0c727a42015-08-18 12:43:12 -03002119 goto error_console;
2120 }
2121
Fugang Duane67c1392020-11-11 10:51:36 +08002122 retval = clk_prepare_enable(sport->clk_per);
Fabio Estevam0c727a42015-08-18 12:43:12 -03002123 if (retval)
Fugang Duane67c1392020-11-11 10:51:36 +08002124 clk_disable_unprepare(sport->clk_ipg);
Huang Shijie1cf93e02013-06-28 13:39:42 +08002125
2126error_console:
2127 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Francesco Dolcini9768a372021-10-20 21:26:43 +02002130static int
2131imx_uart_console_exit(struct console *co)
2132{
2133 struct imx_port *sport = imx_uart_ports[co->index];
2134
2135 clk_disable_unprepare(sport->clk_per);
2136 clk_disable_unprepare(sport->clk_ipg);
2137
2138 return 0;
2139}
2140
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002141static struct uart_driver imx_uart_uart_driver;
2142static struct console imx_uart_console = {
Sascha Hauere3d13ff2008-07-05 10:02:48 +02002143 .name = DEV_NAME,
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002144 .write = imx_uart_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 .device = uart_console_device,
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002146 .setup = imx_uart_console_setup,
Francesco Dolcini9768a372021-10-20 21:26:43 +02002147 .exit = imx_uart_console_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 .flags = CON_PRINTBUFFER,
2149 .index = -1,
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002150 .data = &imx_uart_uart_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151};
2152
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002153#define IMX_CONSOLE &imx_uart_console
Lucas Stach913c6c02015-08-28 11:56:19 +02002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155#else
2156#define IMX_CONSOLE NULL
2157#endif
2158
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002159static struct uart_driver imx_uart_uart_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 .owner = THIS_MODULE,
2161 .driver_name = DRIVER_NAME,
Sascha Hauere3d13ff2008-07-05 10:02:48 +02002162 .dev_name = DEV_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 .major = SERIAL_IMX_MAJOR,
2164 .minor = MINOR_START,
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002165 .nr = ARRAY_SIZE(imx_uart_ports),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 .cons = IMX_CONSOLE,
2167};
2168
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +02002169static enum hrtimer_restart imx_trigger_start_tx(struct hrtimer *t)
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +02002170{
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +02002171 struct imx_port *sport = container_of(t, struct imx_port, trigger_start_tx);
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +02002172 unsigned long flags;
2173
2174 spin_lock_irqsave(&sport->port.lock, flags);
2175 if (sport->tx_state == WAIT_AFTER_RTS)
2176 imx_uart_start_tx(&sport->port);
2177 spin_unlock_irqrestore(&sport->port.lock, flags);
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +02002178
2179 return HRTIMER_NORESTART;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +02002180}
2181
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +02002182static enum hrtimer_restart imx_trigger_stop_tx(struct hrtimer *t)
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +02002183{
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +02002184 struct imx_port *sport = container_of(t, struct imx_port, trigger_stop_tx);
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +02002185 unsigned long flags;
2186
2187 spin_lock_irqsave(&sport->port.lock, flags);
2188 if (sport->tx_state == WAIT_AFTER_SEND)
2189 imx_uart_stop_tx(&sport->port);
2190 spin_unlock_irqrestore(&sport->port.lock, flags);
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +02002191
2192 return HRTIMER_NORESTART;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +02002193}
2194
Fabien Lahouderedb0a1962021-04-30 19:50:37 +02002195/* Default RX DMA buffer configuration */
2196#define RX_DMA_PERIODS 16
2197#define RX_DMA_PERIOD_LEN (PAGE_SIZE / 4)
2198
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002199static int imx_uart_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Fabio Estevam4661f462020-12-09 18:47:12 -03002201 struct device_node *np = pdev->dev.of_node;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002202 struct imx_port *sport;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002203 void __iomem *base;
Fabien Lahouderedb0a1962021-04-30 19:50:37 +02002204 u32 dma_buf_conf[2];
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002205 int ret = 0;
2206 u32 ucr1;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002207 struct resource *res;
Uwe Kleine-König842633b2015-02-24 11:17:07 +01002208 int txirq, rxirq, rtsirq;
Sascha Hauer5b802342006-05-04 14:07:42 +01002209
Sachin Kamat42d34192013-01-07 10:25:06 +05302210 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002211 if (!sport)
2212 return -ENOMEM;
2213
Fabio Estevam4661f462020-12-09 18:47:12 -03002214 sport->devdata = of_device_get_match_data(&pdev->dev);
2215
2216 ret = of_alias_get_id(np, "serial");
2217 if (ret < 0) {
2218 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
Sachin Kamat42d34192013-01-07 10:25:06 +05302219 return ret;
Fabio Estevam4661f462020-12-09 18:47:12 -03002220 }
2221 sport->port.line = ret;
2222
2223 if (of_get_property(np, "uart-has-rtscts", NULL) ||
2224 of_get_property(np, "fsl,uart-has-rtscts", NULL) /* deprecated */)
2225 sport->have_rtscts = 1;
2226
2227 if (of_get_property(np, "fsl,dte-mode", NULL))
2228 sport->dte_mode = 1;
2229
2230 if (of_get_property(np, "rts-gpios", NULL))
2231 sport->have_rtsgpio = 1;
2232
2233 if (of_get_property(np, "fsl,inverted-tx", NULL))
2234 sport->inverted_tx = 1;
2235
2236 if (of_get_property(np, "fsl,inverted-rx", NULL))
2237 sport->inverted_rx = 1;
Shawn Guo22698aa2011-06-25 02:04:34 +08002238
Fabien Lahouderedb0a1962021-04-30 19:50:37 +02002239 if (!of_property_read_u32_array(np, "fsl,dma-info", dma_buf_conf, 2)) {
2240 sport->rx_period_length = dma_buf_conf[0];
2241 sport->rx_periods = dma_buf_conf[1];
2242 } else {
2243 sport->rx_period_length = RX_DMA_PERIOD_LEN;
2244 sport->rx_periods = RX_DMA_PERIODS;
2245 }
2246
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002247 if (sport->port.line >= ARRAY_SIZE(imx_uart_ports)) {
Geert Uytterhoeven56734442018-02-23 14:38:31 +01002248 dev_err(&pdev->dev, "serial%d out of range\n",
2249 sport->port.line);
2250 return -EINVAL;
2251 }
2252
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Alexander Shiyanda82f992014-02-22 16:01:33 +04002254 base = devm_ioremap_resource(&pdev->dev, res);
2255 if (IS_ERR(base))
2256 return PTR_ERR(base);
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002257
Uwe Kleine-König842633b2015-02-24 11:17:07 +01002258 rxirq = platform_get_irq(pdev, 0);
Anson Huangaa49d8e2020-05-11 15:09:56 +08002259 if (rxirq < 0)
2260 return rxirq;
Anson Huang31a8d8f2019-10-09 17:49:19 +08002261 txirq = platform_get_irq_optional(pdev, 1);
2262 rtsirq = platform_get_irq_optional(pdev, 2);
Uwe Kleine-König842633b2015-02-24 11:17:07 +01002263
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002264 sport->port.dev = &pdev->dev;
2265 sport->port.mapbase = res->start;
2266 sport->port.membase = base;
Zheng Yongjun5b109562020-12-14 21:37:19 +08002267 sport->port.type = PORT_IMX;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002268 sport->port.iotype = UPIO_MEM;
Uwe Kleine-König842633b2015-02-24 11:17:07 +01002269 sport->port.irq = rxirq;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002270 sport->port.fifosize = 32;
Dmitry Safonovaa3479d2019-12-13 00:06:18 +00002271 sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE);
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002272 sport->port.ops = &imx_uart_pops;
2273 sport->port.rs485_config = imx_uart_rs485_config;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002274 sport->port.flags = UPF_BOOT_AUTOCONF;
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002275 timer_setup(&sport->timer, imx_uart_timeout, 0);
Sascha Hauer38a41fd2008-07-05 10:02:46 +02002276
Uwe Kleine-König58362d52015-12-13 11:30:03 +01002277 sport->gpios = mctrl_gpio_init(&sport->port, 0);
2278 if (IS_ERR(sport->gpios))
2279 return PTR_ERR(sport->gpios);
2280
Sascha Hauer3a9465f2012-03-07 09:31:43 +01002281 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2282 if (IS_ERR(sport->clk_ipg)) {
2283 ret = PTR_ERR(sport->clk_ipg);
Uwe Kleine-König833462e2012-08-20 09:57:04 +02002284 dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
Sachin Kamat42d34192013-01-07 10:25:06 +05302285 return ret;
Sascha Hauer38a41fd2008-07-05 10:02:46 +02002286 }
Sascha Hauer38a41fd2008-07-05 10:02:46 +02002287
Sascha Hauer3a9465f2012-03-07 09:31:43 +01002288 sport->clk_per = devm_clk_get(&pdev->dev, "per");
2289 if (IS_ERR(sport->clk_per)) {
2290 ret = PTR_ERR(sport->clk_per);
Uwe Kleine-König833462e2012-08-20 09:57:04 +02002291 dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
Sachin Kamat42d34192013-01-07 10:25:06 +05302292 return ret;
Sascha Hauer3a9465f2012-03-07 09:31:43 +01002293 }
2294
Sascha Hauer3a9465f2012-03-07 09:31:43 +01002295 sport->port.uartclk = clk_get_rate(sport->clk_per);
Sascha Hauerdbff4e92008-07-05 10:02:45 +02002296
Fabio Estevam8a61f0c2015-06-17 17:35:43 -03002297 /* For register access, we only need to enable the ipg clock. */
2298 ret = clk_prepare_enable(sport->clk_ipg);
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002299 if (ret) {
2300 dev_err(&pdev->dev, "failed to enable per clk: %d\n", ret);
Fabio Estevam8a61f0c2015-06-17 17:35:43 -03002301 return ret;
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002302 }
Fabio Estevam8a61f0c2015-06-17 17:35:43 -03002303
Uwe Kleine-König3a0ab622018-03-02 11:07:20 +01002304 /* initialize shadow register values */
2305 sport->ucr1 = readl(sport->port.membase + UCR1);
2306 sport->ucr2 = readl(sport->port.membase + UCR2);
2307 sport->ucr3 = readl(sport->port.membase + UCR3);
2308 sport->ucr4 = readl(sport->port.membase + UCR4);
2309 sport->ufcr = readl(sport->port.membase + UFCR);
2310
Lukas Wunnerc150c0f2020-05-12 14:40:02 +02002311 ret = uart_get_rs485_mode(&sport->port);
2312 if (ret) {
2313 clk_disable_unprepare(sport->clk_ipg);
2314 return ret;
2315 }
Lukas Wunner743f93f2017-11-24 23:26:40 +01002316
Lukas Wunnerb8f3bff2017-11-24 23:26:40 +01002317 if (sport->port.rs485.flags & SER_RS485_ENABLED &&
phil eichinger5d7f77e2018-02-19 10:24:15 +01002318 (!sport->have_rtscts && !sport->have_rtsgpio))
Lukas Wunnerb8f3bff2017-11-24 23:26:40 +01002319 dev_err(&pdev->dev, "no RTS control, disabling rs485\n");
2320
Stefan Agner6d215f82018-04-19 17:39:16 +02002321 /*
2322 * If using the i.MX UART RTS/CTS control then the RTS (CTS_B)
2323 * signal cannot be set low during transmission in case the
2324 * receiver is off (limitation of the i.MX UART IP).
2325 */
2326 if (sport->port.rs485.flags & SER_RS485_ENABLED &&
2327 sport->have_rtscts && !sport->have_rtsgpio &&
2328 (!(sport->port.rs485.flags & SER_RS485_RTS_ON_SEND) &&
2329 !(sport->port.rs485.flags & SER_RS485_RX_DURING_TX)))
2330 dev_err(&pdev->dev,
2331 "low-active RTS not possible when receiver is off, enabling receiver\n");
2332
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002333 imx_uart_rs485_config(&sport->port, &sport->port.rs485);
Lukas Wunnerb8f3bff2017-11-24 23:26:40 +01002334
Fabio Estevam8a61f0c2015-06-17 17:35:43 -03002335 /* Disable interrupts before requesting them */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002336 ucr1 = imx_uart_readl(sport, UCR1);
Ye Bin5f0e7082020-09-03 14:24:01 +08002337 ucr1 &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN | UCR1_RTSDEN);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002338 imx_uart_writel(sport, ucr1, UCR1);
Fabio Estevam8a61f0c2015-06-17 17:35:43 -03002339
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002340 if (!imx_uart_is_imx1(sport) && sport->dte_mode) {
Uwe Kleine-Könige61c38d2017-04-04 11:18:51 +02002341 /*
2342 * The DCEDTE bit changes the direction of DSR, DCD, DTR and RI
2343 * and influences if UCR3_RI and UCR3_DCD changes the level of RI
2344 * and DCD (when they are outputs) or enables the respective
2345 * irqs. So set this bit early, i.e. before requesting irqs.
2346 */
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002347 u32 ufcr = imx_uart_readl(sport, UFCR);
2348 if (!(ufcr & UFCR_DCEDTE))
2349 imx_uart_writel(sport, ufcr | UFCR_DCEDTE, UFCR);
Uwe Kleine-Könige61c38d2017-04-04 11:18:51 +02002350
2351 /*
2352 * Disable UCR3_RI and UCR3_DCD irqs. They are also not
2353 * enabled later because they cannot be cleared
2354 * (confirmed on i.MX25) which makes them unusable.
2355 */
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002356 imx_uart_writel(sport,
2357 IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP | UCR3_DSR,
2358 UCR3);
Uwe Kleine-Könige61c38d2017-04-04 11:18:51 +02002359
2360 } else {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002361 u32 ucr3 = UCR3_DSR;
2362 u32 ufcr = imx_uart_readl(sport, UFCR);
2363 if (ufcr & UFCR_DCEDTE)
2364 imx_uart_writel(sport, ufcr & ~UFCR_DCEDTE, UFCR);
Uwe Kleine-König6df765d2017-05-24 21:38:46 +02002365
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002366 if (!imx_uart_is_imx1(sport))
Uwe Kleine-König6df765d2017-05-24 21:38:46 +02002367 ucr3 |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002368 imx_uart_writel(sport, ucr3, UCR3);
Uwe Kleine-Könige61c38d2017-04-04 11:18:51 +02002369 }
2370
Fabio Estevam8a61f0c2015-06-17 17:35:43 -03002371 clk_disable_unprepare(sport->clk_ipg);
2372
Ahmad Fatoumbd78ecd2020-07-14 11:30:12 +02002373 hrtimer_init(&sport->trigger_start_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2374 hrtimer_init(&sport->trigger_stop_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2375 sport->trigger_start_tx.function = imx_trigger_start_tx;
2376 sport->trigger_stop_tx.function = imx_trigger_stop_tx;
Uwe Kleine-Königcb1a6092020-07-14 11:30:11 +02002377
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002378 /*
2379 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
2380 * chips only have one interrupt.
2381 */
Uwe Kleine-König842633b2015-02-24 11:17:07 +01002382 if (txirq > 0) {
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002383 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_rxint, 0,
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002384 dev_name(&pdev->dev), sport);
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002385 if (ret) {
2386 dev_err(&pdev->dev, "failed to request rx irq: %d\n",
2387 ret);
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002388 return ret;
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002389 }
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002390
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002391 ret = devm_request_irq(&pdev->dev, txirq, imx_uart_txint, 0,
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002392 dev_name(&pdev->dev), sport);
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002393 if (ret) {
2394 dev_err(&pdev->dev, "failed to request tx irq: %d\n",
2395 ret);
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002396 return ret;
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002397 }
Uwe Kleine-König7e620982018-09-20 14:11:17 +02002398
2399 ret = devm_request_irq(&pdev->dev, rtsirq, imx_uart_rtsint, 0,
2400 dev_name(&pdev->dev), sport);
2401 if (ret) {
2402 dev_err(&pdev->dev, "failed to request rts irq: %d\n",
2403 ret);
2404 return ret;
2405 }
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002406 } else {
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002407 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_int, 0,
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002408 dev_name(&pdev->dev), sport);
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002409 if (ret) {
2410 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002411 return ret;
Uwe Kleine-König1e512d42016-09-08 14:27:53 +02002412 }
Fabio Estevamc0d1c6b2014-10-27 14:49:37 -02002413 }
2414
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002415 imx_uart_ports[sport->port.line] = sport;
Sascha Hauer5b802342006-05-04 14:07:42 +01002416
Richard Zhao0a86a862012-09-18 16:14:58 +08002417 platform_set_drvdata(pdev, sport);
Sascha Hauer2582d8c2008-07-05 10:02:45 +02002418
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002419 return uart_add_one_port(&imx_uart_uart_driver, &sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002422static int imx_uart_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Sascha Hauer2582d8c2008-07-05 10:02:45 +02002424 struct imx_port *sport = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002426 return uart_remove_one_port(&imx_uart_uart_driver, &sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002429static void imx_uart_restore_context(struct imx_port *sport)
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002430{
Anson Huang07b5e162018-09-05 09:24:26 +08002431 unsigned long flags;
2432
2433 spin_lock_irqsave(&sport->port.lock, flags);
2434 if (!sport->context_saved) {
2435 spin_unlock_irqrestore(&sport->port.lock, flags);
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002436 return;
Anson Huang07b5e162018-09-05 09:24:26 +08002437 }
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002438
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002439 imx_uart_writel(sport, sport->saved_reg[4], UFCR);
2440 imx_uart_writel(sport, sport->saved_reg[5], UESC);
2441 imx_uart_writel(sport, sport->saved_reg[6], UTIM);
2442 imx_uart_writel(sport, sport->saved_reg[7], UBIR);
2443 imx_uart_writel(sport, sport->saved_reg[8], UBMR);
2444 imx_uart_writel(sport, sport->saved_reg[9], IMX21_UTS);
2445 imx_uart_writel(sport, sport->saved_reg[0], UCR1);
2446 imx_uart_writel(sport, sport->saved_reg[1] | UCR2_SRST, UCR2);
2447 imx_uart_writel(sport, sport->saved_reg[2], UCR3);
2448 imx_uart_writel(sport, sport->saved_reg[3], UCR4);
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002449 sport->context_saved = false;
Anson Huang07b5e162018-09-05 09:24:26 +08002450 spin_unlock_irqrestore(&sport->port.lock, flags);
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002451}
2452
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002453static void imx_uart_save_context(struct imx_port *sport)
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002454{
Anson Huang07b5e162018-09-05 09:24:26 +08002455 unsigned long flags;
2456
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002457 /* Save necessary regs */
Anson Huang07b5e162018-09-05 09:24:26 +08002458 spin_lock_irqsave(&sport->port.lock, flags);
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002459 sport->saved_reg[0] = imx_uart_readl(sport, UCR1);
2460 sport->saved_reg[1] = imx_uart_readl(sport, UCR2);
2461 sport->saved_reg[2] = imx_uart_readl(sport, UCR3);
2462 sport->saved_reg[3] = imx_uart_readl(sport, UCR4);
2463 sport->saved_reg[4] = imx_uart_readl(sport, UFCR);
2464 sport->saved_reg[5] = imx_uart_readl(sport, UESC);
2465 sport->saved_reg[6] = imx_uart_readl(sport, UTIM);
2466 sport->saved_reg[7] = imx_uart_readl(sport, UBIR);
2467 sport->saved_reg[8] = imx_uart_readl(sport, UBMR);
2468 sport->saved_reg[9] = imx_uart_readl(sport, IMX21_UTS);
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002469 sport->context_saved = true;
Anson Huang07b5e162018-09-05 09:24:26 +08002470 spin_unlock_irqrestore(&sport->port.lock, flags);
Eduardo Valentinc868cbb2015-08-11 10:21:23 -07002471}
2472
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002473static void imx_uart_enable_wakeup(struct imx_port *sport, bool on)
Eduardo Valentin189550b2015-08-11 10:21:21 -07002474{
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002475 u32 ucr3;
Eduardo Valentin189550b2015-08-11 10:21:21 -07002476
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002477 ucr3 = imx_uart_readl(sport, UCR3);
Martin Kaiser09df0b32018-01-05 17:46:43 +01002478 if (on) {
Uwe Kleine-König27c84422018-03-02 11:07:19 +01002479 imx_uart_writel(sport, USR1_AWAKE, USR1);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002480 ucr3 |= UCR3_AWAKEN;
2481 } else {
2482 ucr3 &= ~UCR3_AWAKEN;
Martin Kaiser09df0b32018-01-05 17:46:43 +01002483 }
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002484 imx_uart_writel(sport, ucr3, UCR3);
Eduardo Valentinbc857342015-08-11 10:21:22 -07002485
Fabio Estevam38b1f0f2018-01-04 15:58:34 -02002486 if (sport->have_rtscts) {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002487 u32 ucr1 = imx_uart_readl(sport, UCR1);
Fugang Duanc67643b2021-11-25 09:43:06 +08002488 if (on) {
2489 imx_uart_writel(sport, USR1_RTSD, USR1);
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002490 ucr1 |= UCR1_RTSDEN;
Fugang Duanc67643b2021-11-25 09:43:06 +08002491 } else {
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002492 ucr1 &= ~UCR1_RTSDEN;
Fugang Duanc67643b2021-11-25 09:43:06 +08002493 }
Uwe Kleine-König4444dcf2018-03-02 11:07:23 +01002494 imx_uart_writel(sport, ucr1, UCR1);
Fabio Estevam38b1f0f2018-01-04 15:58:34 -02002495 }
Eduardo Valentin189550b2015-08-11 10:21:21 -07002496}
2497
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002498static int imx_uart_suspend_noirq(struct device *dev)
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002499{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02002500 struct imx_port *sport = dev_get_drvdata(dev);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002501
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002502 imx_uart_save_context(sport);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002503
2504 clk_disable(sport->clk_ipg);
2505
Anson Huangfcfed1be2018-09-05 09:24:27 +08002506 pinctrl_pm_select_sleep_state(dev);
2507
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002508 return 0;
2509}
2510
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002511static int imx_uart_resume_noirq(struct device *dev)
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002512{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02002513 struct imx_port *sport = dev_get_drvdata(dev);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002514 int ret;
2515
Anson Huangfcfed1be2018-09-05 09:24:27 +08002516 pinctrl_pm_select_default_state(dev);
2517
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002518 ret = clk_enable(sport->clk_ipg);
2519 if (ret)
2520 return ret;
2521
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002522 imx_uart_restore_context(sport);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002523
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002524 return 0;
2525}
2526
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002527static int imx_uart_suspend(struct device *dev)
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002528{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02002529 struct imx_port *sport = dev_get_drvdata(dev);
Martin Kaiser09df0b32018-01-05 17:46:43 +01002530 int ret;
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002531
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002532 uart_suspend_port(&imx_uart_uart_driver, &sport->port);
Maxim Yu. Osipov81b289c2017-08-14 16:27:49 +02002533 disable_irq(sport->port.irq);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002534
Martin Kaiser09df0b32018-01-05 17:46:43 +01002535 ret = clk_prepare_enable(sport->clk_ipg);
2536 if (ret)
2537 return ret;
2538
2539 /* enable wakeup from i.MX UART */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002540 imx_uart_enable_wakeup(sport, true);
Martin Kaiser09df0b32018-01-05 17:46:43 +01002541
2542 return 0;
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002543}
2544
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002545static int imx_uart_resume(struct device *dev)
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002546{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02002547 struct imx_port *sport = dev_get_drvdata(dev);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002548
2549 /* disable wakeup from i.MX UART */
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002550 imx_uart_enable_wakeup(sport, false);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002551
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002552 uart_resume_port(&imx_uart_uart_driver, &sport->port);
Maxim Yu. Osipov81b289c2017-08-14 16:27:49 +02002553 enable_irq(sport->port.irq);
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002554
Martin Kaiser09df0b32018-01-05 17:46:43 +01002555 clk_disable_unprepare(sport->clk_ipg);
Martin Fuzzey29add682016-01-05 16:53:31 +01002556
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002557 return 0;
2558}
2559
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002560static int imx_uart_freeze(struct device *dev)
Philipp Zabel94be6d72017-11-01 13:51:41 +01002561{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02002562 struct imx_port *sport = dev_get_drvdata(dev);
Philipp Zabel94be6d72017-11-01 13:51:41 +01002563
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002564 uart_suspend_port(&imx_uart_uart_driver, &sport->port);
Philipp Zabel94be6d72017-11-01 13:51:41 +01002565
Martin Kaiser09df0b32018-01-05 17:46:43 +01002566 return clk_prepare_enable(sport->clk_ipg);
Philipp Zabel94be6d72017-11-01 13:51:41 +01002567}
2568
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002569static int imx_uart_thaw(struct device *dev)
Philipp Zabel94be6d72017-11-01 13:51:41 +01002570{
Wolfram Sanga406c4b2018-04-19 16:06:23 +02002571 struct imx_port *sport = dev_get_drvdata(dev);
Philipp Zabel94be6d72017-11-01 13:51:41 +01002572
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002573 uart_resume_port(&imx_uart_uart_driver, &sport->port);
Philipp Zabel94be6d72017-11-01 13:51:41 +01002574
Martin Kaiser09df0b32018-01-05 17:46:43 +01002575 clk_disable_unprepare(sport->clk_ipg);
Philipp Zabel94be6d72017-11-01 13:51:41 +01002576
2577 return 0;
2578}
2579
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002580static const struct dev_pm_ops imx_uart_pm_ops = {
2581 .suspend_noirq = imx_uart_suspend_noirq,
2582 .resume_noirq = imx_uart_resume_noirq,
2583 .freeze_noirq = imx_uart_suspend_noirq,
2584 .restore_noirq = imx_uart_resume_noirq,
2585 .suspend = imx_uart_suspend,
2586 .resume = imx_uart_resume,
2587 .freeze = imx_uart_freeze,
2588 .thaw = imx_uart_thaw,
2589 .restore = imx_uart_thaw,
Shenwei Wang90bb6bd2015-07-30 10:32:36 -05002590};
2591
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002592static struct platform_driver imx_uart_platform_driver = {
2593 .probe = imx_uart_probe,
2594 .remove = imx_uart_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002596 .driver = {
2597 .name = "imx-uart",
Shawn Guo22698aa2011-06-25 02:04:34 +08002598 .of_match_table = imx_uart_dt_ids,
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002599 .pm = &imx_uart_pm_ops,
Russell King3ae5eae2005-11-09 22:32:44 +00002600 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601};
2602
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002603static int __init imx_uart_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002605 int ret = uart_register_driver(&imx_uart_uart_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (ret)
2608 return ret;
2609
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002610 ret = platform_driver_register(&imx_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (ret != 0)
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002612 uart_unregister_driver(&imx_uart_uart_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Uwe Kleine-Königf2278242011-11-22 14:22:55 +01002614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002617static void __exit imx_uart_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002619 platform_driver_unregister(&imx_uart_platform_driver);
2620 uart_unregister_driver(&imx_uart_uart_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Uwe Kleine-König9d1a50a2018-03-02 11:07:30 +01002623module_init(imx_uart_init);
2624module_exit(imx_uart_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626MODULE_AUTHOR("Sascha Hauer");
2627MODULE_DESCRIPTION("IMX generic serial port driver");
2628MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002629MODULE_ALIAS("platform:imx-uart");