fsl_lpuart.c 72.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4
/*
 *  Freescale lpuart serial port driver
 *
5
 *  Copyright 2012-2014 Freescale Semiconductor, Inc.
6 7
 */

Y
Yuan Yao 已提交
8 9
#include <linux/clk.h>
#include <linux/console.h>
10
#include <linux/delay.h>
Y
Yuan Yao 已提交
11 12 13
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/dmapool.h>
14 15
#include <linux/io.h>
#include <linux/irq.h>
Y
Yuan Yao 已提交
16
#include <linux/module.h>
17 18
#include <linux/of.h>
#include <linux/of_device.h>
Y
Yuan Yao 已提交
19
#include <linux/of_dma.h>
20
#include <linux/serial_core.h>
Y
Yuan Yao 已提交
21
#include <linux/slab.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#include <linux/tty_flip.h>

/* All registers are 8-bit width */
#define UARTBDH			0x00
#define UARTBDL			0x01
#define UARTCR1			0x02
#define UARTCR2			0x03
#define UARTSR1			0x04
#define UARTCR3			0x06
#define UARTDR			0x07
#define UARTCR4			0x0a
#define UARTCR5			0x0b
#define UARTMODEM		0x0d
#define UARTPFIFO		0x10
#define UARTCFIFO		0x11
#define UARTSFIFO		0x12
#define UARTTWFIFO		0x13
#define UARTTCFIFO		0x14
#define UARTRWFIFO		0x15

#define UARTBDH_LBKDIE		0x80
#define UARTBDH_RXEDGIE		0x40
#define UARTBDH_SBR_MASK	0x1f

#define UARTCR1_LOOPS		0x80
#define UARTCR1_RSRC		0x20
#define UARTCR1_M		0x10
#define UARTCR1_WAKE		0x08
#define UARTCR1_ILT		0x04
#define UARTCR1_PE		0x02
#define UARTCR1_PT		0x01

#define UARTCR2_TIE		0x80
#define UARTCR2_TCIE		0x40
#define UARTCR2_RIE		0x20
#define UARTCR2_ILIE		0x10
#define UARTCR2_TE		0x08
#define UARTCR2_RE		0x04
#define UARTCR2_RWU		0x02
#define UARTCR2_SBK		0x01

#define UARTSR1_TDRE		0x80
#define UARTSR1_TC		0x40
#define UARTSR1_RDRF		0x20
#define UARTSR1_IDLE		0x10
#define UARTSR1_OR		0x08
#define UARTSR1_NF		0x04
#define UARTSR1_FE		0x02
#define UARTSR1_PE		0x01

#define UARTCR3_R8		0x80
#define UARTCR3_T8		0x40
#define UARTCR3_TXDIR		0x20
#define UARTCR3_TXINV		0x10
#define UARTCR3_ORIE		0x08
#define UARTCR3_NEIE		0x04
#define UARTCR3_FEIE		0x02
#define UARTCR3_PEIE		0x01

#define UARTCR4_MAEN1		0x80
#define UARTCR4_MAEN2		0x40
#define UARTCR4_M10		0x20
#define UARTCR4_BRFA_MASK	0x1f
#define UARTCR4_BRFA_OFF	0

#define UARTCR5_TDMAS		0x80
#define UARTCR5_RDMAS		0x20

#define UARTMODEM_RXRTSE	0x08
#define UARTMODEM_TXRTSPOL	0x04
#define UARTMODEM_TXRTSE	0x02
#define UARTMODEM_TXCTSE	0x01

#define UARTPFIFO_TXFE		0x80
#define UARTPFIFO_FIFOSIZE_MASK	0x7
#define UARTPFIFO_TXSIZE_OFF	4
#define UARTPFIFO_RXFE		0x08
#define UARTPFIFO_RXSIZE_OFF	0

#define UARTCFIFO_TXFLUSH	0x80
#define UARTCFIFO_RXFLUSH	0x40
#define UARTCFIFO_RXOFE		0x04
#define UARTCFIFO_TXOFE		0x02
#define UARTCFIFO_RXUFE		0x01

#define UARTSFIFO_TXEMPT	0x80
#define UARTSFIFO_RXEMPT	0x40
#define UARTSFIFO_RXOF		0x04
#define UARTSFIFO_TXOF		0x02
#define UARTSFIFO_RXUF		0x01

113 114 115 116 117
/* 32-bit global registers only for i.MX7ULP/i.MX8x
 * Used to reset all internal logic and registers, except the Global Register.
 */
#define UART_GLOBAL		0x8

118
/* 32-bit register definition */
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#define UARTBAUD		0x00
#define UARTSTAT		0x04
#define UARTCTRL		0x08
#define UARTDATA		0x0C
#define UARTMATCH		0x10
#define UARTMODIR		0x14
#define UARTFIFO		0x18
#define UARTWATER		0x1c

#define UARTBAUD_MAEN1		0x80000000
#define UARTBAUD_MAEN2		0x40000000
#define UARTBAUD_M10		0x20000000
#define UARTBAUD_TDMAE		0x00800000
#define UARTBAUD_RDMAE		0x00200000
#define UARTBAUD_MATCFG		0x00400000
#define UARTBAUD_BOTHEDGE	0x00020000
#define UARTBAUD_RESYNCDIS	0x00010000
#define UARTBAUD_LBKDIE		0x00008000
#define UARTBAUD_RXEDGIE	0x00004000
#define UARTBAUD_SBNS		0x00002000
#define UARTBAUD_SBR		0x00000000
#define UARTBAUD_SBR_MASK	0x1fff
141 142
#define UARTBAUD_OSR_MASK       0x1f
#define UARTBAUD_OSR_SHIFT      24
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

#define UARTSTAT_LBKDIF		0x80000000
#define UARTSTAT_RXEDGIF	0x40000000
#define UARTSTAT_MSBF		0x20000000
#define UARTSTAT_RXINV		0x10000000
#define UARTSTAT_RWUID		0x08000000
#define UARTSTAT_BRK13		0x04000000
#define UARTSTAT_LBKDE		0x02000000
#define UARTSTAT_RAF		0x01000000
#define UARTSTAT_TDRE		0x00800000
#define UARTSTAT_TC		0x00400000
#define UARTSTAT_RDRF		0x00200000
#define UARTSTAT_IDLE		0x00100000
#define UARTSTAT_OR		0x00080000
#define UARTSTAT_NF		0x00040000
#define UARTSTAT_FE		0x00020000
#define UARTSTAT_PE		0x00010000
#define UARTSTAT_MA1F		0x00008000
#define UARTSTAT_M21F		0x00004000

#define UARTCTRL_R8T9		0x80000000
#define UARTCTRL_R9T8		0x40000000
#define UARTCTRL_TXDIR		0x20000000
#define UARTCTRL_TXINV		0x10000000
#define UARTCTRL_ORIE		0x08000000
#define UARTCTRL_NEIE		0x04000000
#define UARTCTRL_FEIE		0x02000000
#define UARTCTRL_PEIE		0x01000000
#define UARTCTRL_TIE		0x00800000
#define UARTCTRL_TCIE		0x00400000
#define UARTCTRL_RIE		0x00200000
#define UARTCTRL_ILIE		0x00100000
#define UARTCTRL_TE		0x00080000
#define UARTCTRL_RE		0x00040000
#define UARTCTRL_RWU		0x00020000
#define UARTCTRL_SBK		0x00010000
#define UARTCTRL_MA1IE		0x00008000
#define UARTCTRL_MA2IE		0x00004000
#define UARTCTRL_IDLECFG	0x00000100
#define UARTCTRL_LOOPS		0x00000080
#define UARTCTRL_DOZEEN		0x00000040
#define UARTCTRL_RSRC		0x00000020
#define UARTCTRL_M		0x00000010
#define UARTCTRL_WAKE		0x00000008
#define UARTCTRL_ILT		0x00000004
#define UARTCTRL_PE		0x00000002
#define UARTCTRL_PT		0x00000001

#define UARTDATA_NOISY		0x00008000
#define UARTDATA_PARITYE	0x00004000
#define UARTDATA_FRETSC		0x00002000
#define UARTDATA_RXEMPT		0x00001000
#define UARTDATA_IDLINE		0x00000800
#define UARTDATA_MASK		0x3ff

#define UARTMODIR_IREN		0x00020000
#define UARTMODIR_TXCTSSRC	0x00000020
#define UARTMODIR_TXCTSC	0x00000010
#define UARTMODIR_RXRTSE	0x00000008
#define UARTMODIR_TXRTSPOL	0x00000004
#define UARTMODIR_TXRTSE	0x00000002
#define UARTMODIR_TXCTSE	0x00000001

#define UARTFIFO_TXEMPT		0x00800000
#define UARTFIFO_RXEMPT		0x00400000
#define UARTFIFO_TXOF		0x00020000
#define UARTFIFO_RXUF		0x00010000
#define UARTFIFO_TXFLUSH	0x00008000
#define UARTFIFO_RXFLUSH	0x00004000
#define UARTFIFO_TXOFE		0x00000200
#define UARTFIFO_RXUFE		0x00000100
#define UARTFIFO_TXFE		0x00000080
#define UARTFIFO_FIFOSIZE_MASK	0x7
#define UARTFIFO_TXSIZE_OFF	4
#define UARTFIFO_RXFE		0x00000008
#define UARTFIFO_RXSIZE_OFF	0
219
#define UARTFIFO_DEPTH(x)	(0x1 << ((x) ? ((x) + 1) : 0))
220 221 222 223 224 225 226 227

#define UARTWATER_COUNT_MASK	0xff
#define UARTWATER_TXCNT_OFF	8
#define UARTWATER_RXCNT_OFF	24
#define UARTWATER_WATER_MASK	0xff
#define UARTWATER_TXWATER_OFF	0
#define UARTWATER_RXWATER_OFF	16

228 229 230 231
#define UART_GLOBAL_RST	0x2
#define GLOBAL_RST_MIN_US	20
#define GLOBAL_RST_MAX_US	40

232 233
/* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
#define DMA_RX_TIMEOUT		(10)
Y
Yuan Yao 已提交
234

235 236 237 238
#define DRIVER_NAME	"fsl-lpuart"
#define DEV_NAME	"ttyLP"
#define UART_NR		6

239 240 241
/* IMX lpuart has four extra unused regs located at the beginning */
#define IMX_REG_OFF	0x10

242 243 244
enum lpuart_type {
	VF610_LPUART,
	LS1021A_LPUART,
245
	LS1028A_LPUART,
246 247
	IMX7ULP_LPUART,
	IMX8QXP_LPUART,
248
	IMXRT1050_LPUART,
249 250
};

251 252
struct lpuart_port {
	struct uart_port	port;
253 254 255
	enum lpuart_type	devtype;
	struct clk		*ipg_clk;
	struct clk		*baud_clk;
256 257
	unsigned int		txfifo_size;
	unsigned int		rxfifo_size;
Y
Yuan Yao 已提交
258

259 260
	bool			lpuart_dma_tx_use;
	bool			lpuart_dma_rx_use;
Y
Yuan Yao 已提交
261 262 263 264 265 266 267 268
	struct dma_chan		*dma_tx_chan;
	struct dma_chan		*dma_rx_chan;
	struct dma_async_tx_descriptor  *dma_tx_desc;
	struct dma_async_tx_descriptor  *dma_rx_desc;
	dma_cookie_t		dma_tx_cookie;
	dma_cookie_t		dma_rx_cookie;
	unsigned int		dma_tx_bytes;
	unsigned int		dma_rx_bytes;
269
	bool			dma_tx_in_progress;
Y
Yuan Yao 已提交
270 271
	unsigned int		dma_rx_timeout;
	struct timer_list	lpuart_timer;
272
	struct scatterlist	rx_sgl, tx_sgl[2];
273 274
	struct circ_buf		rx_ring;
	int			rx_dma_rng_buf_len;
275 276
	unsigned int		dma_tx_nents;
	wait_queue_head_t	dma_wait;
277 278
};

279
struct lpuart_soc_data {
280 281 282
	enum lpuart_type devtype;
	char iotype;
	u8 reg_off;
283 284 285
};

static const struct lpuart_soc_data vf_data = {
286
	.devtype = VF610_LPUART,
287 288 289
	.iotype = UPIO_MEM,
};

290
static const struct lpuart_soc_data ls1021a_data = {
291
	.devtype = LS1021A_LPUART,
292 293 294
	.iotype = UPIO_MEM32BE,
};

295 296 297 298 299
static const struct lpuart_soc_data ls1028a_data = {
	.devtype = LS1028A_LPUART,
	.iotype = UPIO_MEM32,
};

300 301 302 303 304 305 306 307
static struct lpuart_soc_data imx7ulp_data = {
	.devtype = IMX7ULP_LPUART,
	.iotype = UPIO_MEM32,
	.reg_off = IMX_REG_OFF,
};

static struct lpuart_soc_data imx8qxp_data = {
	.devtype = IMX8QXP_LPUART,
308 309 310
	.iotype = UPIO_MEM32,
	.reg_off = IMX_REG_OFF,
};
311 312 313 314 315
static struct lpuart_soc_data imxrt1050_data = {
	.devtype = IMXRT1050_LPUART,
	.iotype = UPIO_MEM32,
	.reg_off = IMX_REG_OFF,
};
316

317
static const struct of_device_id lpuart_dt_ids[] = {
318
	{ .compatible = "fsl,vf610-lpuart",	.data = &vf_data, },
319 320
	{ .compatible = "fsl,ls1021a-lpuart",	.data = &ls1021a_data, },
	{ .compatible = "fsl,ls1028a-lpuart",	.data = &ls1028a_data, },
321 322
	{ .compatible = "fsl,imx7ulp-lpuart",	.data = &imx7ulp_data, },
	{ .compatible = "fsl,imx8qxp-lpuart",	.data = &imx8qxp_data, },
323
	{ .compatible = "fsl,imxrt1050-lpuart",	.data = &imxrt1050_data},
324 325 326 327
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, lpuart_dt_ids);

Y
Yuan Yao 已提交
328 329 330
/* Forward declare this for the dma callbacks*/
static void lpuart_dma_tx_complete(void *arg);

331
static inline bool is_layerscape_lpuart(struct lpuart_port *sport)
332
{
333 334
	return (sport->devtype == LS1021A_LPUART ||
		sport->devtype == LS1028A_LPUART);
335 336
}

337 338 339 340 341
static inline bool is_imx7ulp_lpuart(struct lpuart_port *sport)
{
	return sport->devtype == IMX7ULP_LPUART;
}

342 343 344 345 346
static inline bool is_imx8qxp_lpuart(struct lpuart_port *sport)
{
	return sport->devtype == IMX8QXP_LPUART;
}

347 348 349 350 351 352 353 354 355 356
static inline u32 lpuart32_read(struct uart_port *port, u32 off)
{
	switch (port->iotype) {
	case UPIO_MEM32:
		return readl(port->membase + off);
	case UPIO_MEM32BE:
		return ioread32be(port->membase + off);
	default:
		return 0;
	}
357 358
}

359
static inline void lpuart32_write(struct uart_port *port, u32 val,
360 361 362 363 364 365 366 367 368 369
				  u32 off)
{
	switch (port->iotype) {
	case UPIO_MEM32:
		writel(val, port->membase + off);
		break;
	case UPIO_MEM32BE:
		iowrite32be(val, port->membase + off);
		break;
	}
370 371
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
static int __lpuart_enable_clks(struct lpuart_port *sport, bool is_en)
{
	int ret = 0;

	if (is_en) {
		ret = clk_prepare_enable(sport->ipg_clk);
		if (ret)
			return ret;

		ret = clk_prepare_enable(sport->baud_clk);
		if (ret) {
			clk_disable_unprepare(sport->ipg_clk);
			return ret;
		}
	} else {
		clk_disable_unprepare(sport->baud_clk);
		clk_disable_unprepare(sport->ipg_clk);
	}

	return 0;
}

static unsigned int lpuart_get_baud_clk_rate(struct lpuart_port *sport)
{
	if (is_imx8qxp_lpuart(sport))
		return clk_get_rate(sport->baud_clk);

	return clk_get_rate(sport->ipg_clk);
}

#define lpuart_enable_clks(x)	__lpuart_enable_clks(x, true)
#define lpuart_disable_clks(x)	__lpuart_enable_clks(x, false)

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
static int lpuart_global_reset(struct lpuart_port *sport)
{
	struct uart_port *port = &sport->port;
	void __iomem *global_addr;
	int ret;

	if (uart_console(port))
		return 0;

	ret = clk_prepare_enable(sport->ipg_clk);
	if (ret) {
		dev_err(sport->port.dev, "failed to enable uart ipg clk: %d\n", ret);
		return ret;
	}

	if (is_imx7ulp_lpuart(sport) || is_imx8qxp_lpuart(sport)) {
		global_addr = port->membase + UART_GLOBAL - IMX_REG_OFF;
		writel(UART_GLOBAL_RST, global_addr);
		usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
		writel(0, global_addr);
		usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
	}

	clk_disable_unprepare(sport->ipg_clk);
	return 0;
}

432 433 434 435 436 437 438 439 440
static void lpuart_stop_tx(struct uart_port *port)
{
	unsigned char temp;

	temp = readb(port->membase + UARTCR2);
	temp &= ~(UARTCR2_TIE | UARTCR2_TCIE);
	writeb(temp, port->membase + UARTCR2);
}

441 442 443 444
static void lpuart32_stop_tx(struct uart_port *port)
{
	unsigned long temp;

445
	temp = lpuart32_read(port, UARTCTRL);
446
	temp &= ~(UARTCTRL_TIE | UARTCTRL_TCIE);
447
	lpuart32_write(port, temp, UARTCTRL);
448 449
}

450 451 452 453 454 455 456 457
static void lpuart_stop_rx(struct uart_port *port)
{
	unsigned char temp;

	temp = readb(port->membase + UARTCR2);
	writeb(temp & ~UARTCR2_RE, port->membase + UARTCR2);
}

458 459 460 461
static void lpuart32_stop_rx(struct uart_port *port)
{
	unsigned long temp;

462 463
	temp = lpuart32_read(port, UARTCTRL);
	lpuart32_write(port, temp & ~UARTCTRL_RE, UARTCTRL);
464 465
}

466
static void lpuart_dma_tx(struct lpuart_port *sport)
Y
Yuan Yao 已提交
467 468
{
	struct circ_buf *xmit = &sport->port.state->xmit;
469 470
	struct scatterlist *sgl = sport->tx_sgl;
	struct device *dev = sport->port.dev;
471
	struct dma_chan *chan = sport->dma_tx_chan;
472
	int ret;
Y
Yuan Yao 已提交
473

474 475
	if (sport->dma_tx_in_progress)
		return;
Y
Yuan Yao 已提交
476

477
	sport->dma_tx_bytes = uart_circ_chars_pending(xmit);
Y
Yuan Yao 已提交
478

479
	if (xmit->tail < xmit->head || xmit->head == 0) {
480 481 482 483 484 485 486 487 488
		sport->dma_tx_nents = 1;
		sg_init_one(sgl, xmit->buf + xmit->tail, sport->dma_tx_bytes);
	} else {
		sport->dma_tx_nents = 2;
		sg_init_table(sgl, 2);
		sg_set_buf(sgl, xmit->buf + xmit->tail,
				UART_XMIT_SIZE - xmit->tail);
		sg_set_buf(sgl + 1, xmit->buf, xmit->head);
	}
Y
Yuan Yao 已提交
489

490 491
	ret = dma_map_sg(chan->device->dev, sgl, sport->dma_tx_nents,
			 DMA_TO_DEVICE);
492 493 494 495
	if (!ret) {
		dev_err(dev, "DMA mapping error for TX.\n");
		return;
	}
Y
Yuan Yao 已提交
496

497
	sport->dma_tx_desc = dmaengine_prep_slave_sg(chan, sgl,
498 499
					ret, DMA_MEM_TO_DEV,
					DMA_PREP_INTERRUPT);
Y
Yuan Yao 已提交
500
	if (!sport->dma_tx_desc) {
501 502
		dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
			      DMA_TO_DEVICE);
503 504
		dev_err(dev, "Cannot prepare TX slave DMA!\n");
		return;
Y
Yuan Yao 已提交
505 506 507 508
	}

	sport->dma_tx_desc->callback = lpuart_dma_tx_complete;
	sport->dma_tx_desc->callback_param = sport;
509
	sport->dma_tx_in_progress = true;
Y
Yuan Yao 已提交
510
	sport->dma_tx_cookie = dmaengine_submit(sport->dma_tx_desc);
511
	dma_async_issue_pending(chan);
Y
Yuan Yao 已提交
512 513
}

514 515 516 517 518
static bool lpuart_stopped_or_empty(struct uart_port *port)
{
	return uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port);
}

Y
Yuan Yao 已提交
519 520 521
static void lpuart_dma_tx_complete(void *arg)
{
	struct lpuart_port *sport = arg;
522
	struct scatterlist *sgl = &sport->tx_sgl[0];
Y
Yuan Yao 已提交
523
	struct circ_buf *xmit = &sport->port.state->xmit;
524
	struct dma_chan *chan = sport->dma_tx_chan;
Y
Yuan Yao 已提交
525 526 527
	unsigned long flags;

	spin_lock_irqsave(&sport->port.lock, flags);
528 529 530 531
	if (!sport->dma_tx_in_progress) {
		spin_unlock_irqrestore(&sport->port.lock, flags);
		return;
	}
Y
Yuan Yao 已提交
532

533 534
	dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
		     DMA_TO_DEVICE);
535

Y
Yuan Yao 已提交
536
	xmit->tail = (xmit->tail + sport->dma_tx_bytes) & (UART_XMIT_SIZE - 1);
537 538 539 540

	sport->port.icount.tx += sport->dma_tx_bytes;
	sport->dma_tx_in_progress = false;
	spin_unlock_irqrestore(&sport->port.lock, flags);
Y
Yuan Yao 已提交
541 542 543 544

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&sport->port);

545 546 547 548 549 550 551
	if (waitqueue_active(&sport->dma_wait)) {
		wake_up(&sport->dma_wait);
		return;
	}

	spin_lock_irqsave(&sport->port.lock, flags);

552
	if (!lpuart_stopped_or_empty(&sport->port))
553
		lpuart_dma_tx(sport);
Y
Yuan Yao 已提交
554 555 556 557

	spin_unlock_irqrestore(&sport->port.lock, flags);
}

558 559 560 561 562 563 564 565 566 567 568
static dma_addr_t lpuart_dma_datareg_addr(struct lpuart_port *sport)
{
	switch (sport->port.iotype) {
	case UPIO_MEM32:
		return sport->port.mapbase + UARTDATA;
	case UPIO_MEM32BE:
		return sport->port.mapbase + UARTDATA + sizeof(u32) - 1;
	}
	return sport->port.mapbase + UARTDR;
}

569 570 571 572 573 574 575
static int lpuart_dma_tx_request(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port,
					struct lpuart_port, port);
	struct dma_slave_config dma_tx_sconfig = {};
	int ret;

576
	dma_tx_sconfig.dst_addr = lpuart_dma_datareg_addr(sport);
577 578 579 580 581 582 583 584 585 586 587 588 589 590
	dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
	dma_tx_sconfig.dst_maxburst = 1;
	dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
	ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);

	if (ret) {
		dev_err(sport->port.dev,
				"DMA slave config failed, err = %d\n", ret);
		return ret;
	}

	return 0;
}

591 592 593 594 595 596
static bool lpuart_is_32(struct lpuart_port *sport)
{
	return sport->port.iotype == UPIO_MEM32 ||
	       sport->port.iotype ==  UPIO_MEM32BE;
}

597 598 599
static void lpuart_flush_buffer(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
600
	struct dma_chan *chan = sport->dma_tx_chan;
601
	u32 val;
602

603
	if (sport->lpuart_dma_tx_use) {
604
		if (sport->dma_tx_in_progress) {
605
			dma_unmap_sg(chan->device->dev, &sport->tx_sgl[0],
606 607 608
				sport->dma_tx_nents, DMA_TO_DEVICE);
			sport->dma_tx_in_progress = false;
		}
609
		dmaengine_terminate_all(chan);
610
	}
611 612 613 614 615 616

	if (lpuart_is_32(sport)) {
		val = lpuart32_read(&sport->port, UARTFIFO);
		val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
		lpuart32_write(&sport->port, val, UARTFIFO);
	} else {
617
		val = readb(sport->port.membase + UARTCFIFO);
618 619 620
		val |= UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH;
		writeb(val, sport->port.membase + UARTCFIFO);
	}
621 622
}

623 624 625 626
static void lpuart_wait_bit_set(struct uart_port *port, unsigned int offset,
				u8 bit)
{
	while (!(readb(port->membase + offset) & bit))
627
		cpu_relax();
628 629 630 631 632 633
}

static void lpuart32_wait_bit_set(struct uart_port *port, unsigned int offset,
				  u32 bit)
{
	while (!(lpuart32_read(port, offset) & bit))
634
		cpu_relax();
635 636
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
#if defined(CONFIG_CONSOLE_POLL)

static int lpuart_poll_init(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port,
					struct lpuart_port, port);
	unsigned long flags;
	unsigned char temp;

	sport->port.fifosize = 0;

	spin_lock_irqsave(&sport->port.lock, flags);
	/* Disable Rx & Tx */
	writeb(0, sport->port.membase + UARTCR2);

	temp = readb(sport->port.membase + UARTPFIFO);
	/* Enable Rx and Tx FIFO */
	writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
			sport->port.membase + UARTPFIFO);

	/* flush Tx and Rx FIFO */
	writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
			sport->port.membase + UARTCFIFO);

	/* explicitly clear RDRF */
	if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
		readb(sport->port.membase + UARTDR);
		writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
	}

	writeb(0, sport->port.membase + UARTTWFIFO);
	writeb(1, sport->port.membase + UARTRWFIFO);

	/* Enable Rx and Tx */
	writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
	spin_unlock_irqrestore(&sport->port.lock, flags);

	return 0;
}

static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
{
	/* drain */
680
	lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
681 682 683 684 685 686 687 688 689 690 691
	writeb(c, port->membase + UARTDR);
}

static int lpuart_poll_get_char(struct uart_port *port)
{
	if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
		return NO_POLL_CHAR;

	return readb(port->membase + UARTDR);
}

692 693 694 695 696 697 698 699 700 701 702
static int lpuart32_poll_init(struct uart_port *port)
{
	unsigned long flags;
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
	u32 temp;

	sport->port.fifosize = 0;

	spin_lock_irqsave(&sport->port.lock, flags);

	/* Disable Rx & Tx */
703
	lpuart32_write(&sport->port, 0, UARTCTRL);
704

705
	temp = lpuart32_read(&sport->port, UARTFIFO);
706 707

	/* Enable Rx and Tx FIFO */
708
	lpuart32_write(&sport->port, temp | UARTFIFO_RXFE | UARTFIFO_TXFE, UARTFIFO);
709 710

	/* flush Tx and Rx FIFO */
711
	lpuart32_write(&sport->port, UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH, UARTFIFO);
712 713

	/* explicitly clear RDRF */
714 715
	if (lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_RDRF) {
		lpuart32_read(&sport->port, UARTDATA);
716
		lpuart32_write(&sport->port, UARTFIFO_RXUF, UARTFIFO);
717 718 719
	}

	/* Enable Rx and Tx */
720
	lpuart32_write(&sport->port, UARTCTRL_RE | UARTCTRL_TE, UARTCTRL);
721 722 723 724 725 726 727
	spin_unlock_irqrestore(&sport->port.lock, flags);

	return 0;
}

static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
{
728
	lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
729
	lpuart32_write(port, c, UARTDATA);
730 731 732 733
}

static int lpuart32_poll_get_char(struct uart_port *port)
{
734
	if (!(lpuart32_read(port, UARTWATER) >> UARTWATER_RXCNT_OFF))
735 736
		return NO_POLL_CHAR;

737
	return lpuart32_read(port, UARTDATA);
738
}
739 740
#endif

741 742 743 744
static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
{
	struct circ_buf *xmit = &sport->port.state->xmit;

745 746 747 748 749 750 751
	if (sport->port.x_char) {
		writeb(sport->port.x_char, sport->port.membase + UARTDR);
		sport->port.icount.tx++;
		sport->port.x_char = 0;
		return;
	}

752
	if (lpuart_stopped_or_empty(&sport->port)) {
753 754 755 756
		lpuart_stop_tx(&sport->port);
		return;
	}

757 758 759 760 761 762 763 764 765 766 767 768 769 770
	while (!uart_circ_empty(xmit) &&
		(readb(sport->port.membase + UARTTCFIFO) < sport->txfifo_size)) {
		writeb(xmit->buf[xmit->tail], sport->port.membase + UARTDR);
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		sport->port.icount.tx++;
	}

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&sport->port);

	if (uart_circ_empty(xmit))
		lpuart_stop_tx(&sport->port);
}

771 772 773 774 775
static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
{
	struct circ_buf *xmit = &sport->port.state->xmit;
	unsigned long txcnt;

776 777 778 779 780 781 782
	if (sport->port.x_char) {
		lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
		sport->port.icount.tx++;
		sport->port.x_char = 0;
		return;
	}

783
	if (lpuart_stopped_or_empty(&sport->port)) {
784 785 786 787
		lpuart32_stop_tx(&sport->port);
		return;
	}

788
	txcnt = lpuart32_read(&sport->port, UARTWATER);
789 790 791
	txcnt = txcnt >> UARTWATER_TXCNT_OFF;
	txcnt &= UARTWATER_COUNT_MASK;
	while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
792
		lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
793 794
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		sport->port.icount.tx++;
795
		txcnt = lpuart32_read(&sport->port, UARTWATER);
796 797 798 799 800 801 802 803 804 805 806
		txcnt = txcnt >> UARTWATER_TXCNT_OFF;
		txcnt &= UARTWATER_COUNT_MASK;
	}

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&sport->port);

	if (uart_circ_empty(xmit))
		lpuart32_stop_tx(&sport->port);
}

807 808
static void lpuart_start_tx(struct uart_port *port)
{
Y
Yuan Yao 已提交
809 810
	struct lpuart_port *sport = container_of(port,
			struct lpuart_port, port);
811 812 813 814 815
	unsigned char temp;

	temp = readb(port->membase + UARTCR2);
	writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);

816
	if (sport->lpuart_dma_tx_use) {
817
		if (!lpuart_stopped_or_empty(port))
818
			lpuart_dma_tx(sport);
Y
Yuan Yao 已提交
819 820 821 822
	} else {
		if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
			lpuart_transmit_buffer(sport);
	}
823 824
}

825 826 827 828 829
static void lpuart32_start_tx(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
	unsigned long temp;

830
	if (sport->lpuart_dma_tx_use) {
831
		if (!lpuart_stopped_or_empty(port))
832 833 834 835
			lpuart_dma_tx(sport);
	} else {
		temp = lpuart32_read(port, UARTCTRL);
		lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
836

837 838 839
		if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
			lpuart32_transmit_buffer(sport);
	}
840 841
}

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
/* return TIOCSER_TEMT when transmitter is not busy */
static unsigned int lpuart_tx_empty(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port,
			struct lpuart_port, port);
	unsigned char sr1 = readb(port->membase + UARTSR1);
	unsigned char sfifo = readb(port->membase + UARTSFIFO);

	if (sport->dma_tx_in_progress)
		return 0;

	if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
		return TIOCSER_TEMT;

	return 0;
}

static unsigned int lpuart32_tx_empty(struct uart_port *port)
{
861 862 863 864 865 866 867 868 869 870 871 872
	struct lpuart_port *sport = container_of(port,
			struct lpuart_port, port);
	unsigned long stat = lpuart32_read(port, UARTSTAT);
	unsigned long sfifo = lpuart32_read(port, UARTFIFO);

	if (sport->dma_tx_in_progress)
		return 0;

	if (stat & UARTSTAT_TC && sfifo & UARTFIFO_TXEMPT)
		return TIOCSER_TEMT;

	return 0;
873 874
}

875
static void lpuart_txint(struct lpuart_port *sport)
876
{
877
	spin_lock(&sport->port.lock);
878
	lpuart_transmit_buffer(sport);
879
	spin_unlock(&sport->port.lock);
880 881
}

882
static void lpuart_rxint(struct lpuart_port *sport)
883
{
884
	unsigned int flg, ignored = 0, overrun = 0;
885 886 887
	struct tty_port *port = &sport->port.state->port;
	unsigned char rx, sr;

888
	spin_lock(&sport->port.lock);
889 890 891 892 893 894 895 896 897 898 899

	while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
		flg = TTY_NORMAL;
		sport->port.icount.rx++;
		/*
		 * to clear the FE, OR, NF, FE, PE flags,
		 * read SR1 then read DR
		 */
		sr = readb(sport->port.membase + UARTSR1);
		rx = readb(sport->port.membase + UARTDR);

900
		if (uart_prepare_sysrq_char(&sport->port, rx))
901 902 903 904 905 906 907 908 909
			continue;

		if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
			if (sr & UARTSR1_PE)
				sport->port.icount.parity++;
			else if (sr & UARTSR1_FE)
				sport->port.icount.frame++;

			if (sr & UARTSR1_OR)
910
				overrun++;
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

			if (sr & sport->port.ignore_status_mask) {
				if (++ignored > 100)
					goto out;
				continue;
			}

			sr &= sport->port.read_status_mask;

			if (sr & UARTSR1_PE)
				flg = TTY_PARITY;
			else if (sr & UARTSR1_FE)
				flg = TTY_FRAME;

			if (sr & UARTSR1_OR)
				flg = TTY_OVERRUN;

			sport->port.sysrq = 0;
		}

931 932
		if (tty_insert_flip_char(port, rx, flg) == 0)
			sport->port.icount.buf_overrun++;
933 934 935
	}

out:
936 937 938 939 940 941 942 943 944 945 946
	if (overrun) {
		sport->port.icount.overrun += overrun;

		/*
		 * Overruns cause FIFO pointers to become missaligned.
		 * Flushing the receive FIFO reinitializes the pointers.
		 */
		writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
		writeb(UARTSFIFO_RXOF, sport->port.membase + UARTSFIFO);
	}

947
	uart_unlock_and_check_sysrq(&sport->port);
948 949 950 951

	tty_flip_buffer_push(port);
}

952 953
static void lpuart32_txint(struct lpuart_port *sport)
{
954
	spin_lock(&sport->port.lock);
955
	lpuart32_transmit_buffer(sport);
956
	spin_unlock(&sport->port.lock);
957 958
}

959
static void lpuart32_rxint(struct lpuart_port *sport)
960 961 962 963
{
	unsigned int flg, ignored = 0;
	struct tty_port *port = &sport->port.state->port;
	unsigned long rx, sr;
964
	bool is_break;
965

966
	spin_lock(&sport->port.lock);
967

968
	while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
969 970 971 972 973 974
		flg = TTY_NORMAL;
		sport->port.icount.rx++;
		/*
		 * to clear the FE, OR, NF, FE, PE flags,
		 * read STAT then read DATA reg
		 */
975 976
		sr = lpuart32_read(&sport->port, UARTSTAT);
		rx = lpuart32_read(&sport->port, UARTDATA);
977
		rx &= UARTDATA_MASK;
978

979 980 981 982 983 984 985 986 987
		/*
		 * The LPUART can't distinguish between a break and a framing error,
		 * thus we assume it is a break if the received data is zero.
		 */
		is_break = (sr & UARTSTAT_FE) && !rx;

		if (is_break && uart_handle_break(&sport->port))
			continue;

988
		if (uart_prepare_sysrq_char(&sport->port, rx))
989 990 991
			continue;

		if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
992 993 994 995 996 997
			if (sr & UARTSTAT_PE) {
				if (is_break)
					sport->port.icount.brk++;
				else
					sport->port.icount.parity++;
			} else if (sr & UARTSTAT_FE) {
998
				sport->port.icount.frame++;
999
			}
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

			if (sr & UARTSTAT_OR)
				sport->port.icount.overrun++;

			if (sr & sport->port.ignore_status_mask) {
				if (++ignored > 100)
					goto out;
				continue;
			}

			sr &= sport->port.read_status_mask;

1012 1013 1014 1015 1016 1017
			if (sr & UARTSTAT_PE) {
				if (is_break)
					flg = TTY_BREAK;
				else
					flg = TTY_PARITY;
			} else if (sr & UARTSTAT_FE) {
1018
				flg = TTY_FRAME;
1019
			}
1020 1021 1022 1023 1024

			if (sr & UARTSTAT_OR)
				flg = TTY_OVERRUN;
		}

1025 1026
		if (tty_insert_flip_char(port, rx, flg) == 0)
			sport->port.icount.buf_overrun++;
1027 1028 1029
	}

out:
1030
	uart_unlock_and_check_sysrq(&sport->port);
1031 1032 1033 1034

	tty_flip_buffer_push(port);
}

1035 1036 1037
static irqreturn_t lpuart_int(int irq, void *dev_id)
{
	struct lpuart_port *sport = dev_id;
1038
	unsigned char sts;
1039 1040 1041

	sts = readb(sport->port.membase + UARTSR1);

1042 1043 1044 1045 1046 1047 1048 1049 1050
	/* SysRq, using dma, check for linebreak by framing err. */
	if (sts & UARTSR1_FE && sport->lpuart_dma_rx_use) {
		readb(sport->port.membase + UARTDR);
		uart_handle_break(&sport->port);
		/* linebreak produces some garbage, removing it */
		writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
		return IRQ_HANDLED;
	}

1051
	if (sts & UARTSR1_RDRF && !sport->lpuart_dma_rx_use)
1052
		lpuart_rxint(sport);
1053

1054
	if (sts & UARTSR1_TDRE && !sport->lpuart_dma_tx_use)
1055
		lpuart_txint(sport);
1056 1057 1058 1059

	return IRQ_HANDLED;
}

1060 1061 1062 1063 1064
static irqreturn_t lpuart32_int(int irq, void *dev_id)
{
	struct lpuart_port *sport = dev_id;
	unsigned long sts, rxcount;

1065 1066
	sts = lpuart32_read(&sport->port, UARTSTAT);
	rxcount = lpuart32_read(&sport->port, UARTWATER);
1067 1068
	rxcount = rxcount >> UARTWATER_RXCNT_OFF;

1069
	if ((sts & UARTSTAT_RDRF || rxcount > 0) && !sport->lpuart_dma_rx_use)
1070
		lpuart32_rxint(sport);
1071

1072
	if ((sts & UARTSTAT_TDRE) && !sport->lpuart_dma_tx_use)
1073
		lpuart32_txint(sport);
1074

1075
	lpuart32_write(&sport->port, sts, UARTSTAT);
1076 1077 1078
	return IRQ_HANDLED;
}

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

static inline void lpuart_handle_sysrq_chars(struct uart_port *port,
					     unsigned char *p, int count)
{
	while (count--) {
		if (*p && uart_handle_sysrq_char(port, *p))
			return;
		p++;
	}
}

static void lpuart_handle_sysrq(struct lpuart_port *sport)
{
	struct circ_buf *ring = &sport->rx_ring;
	int count;

	if (ring->head < ring->tail) {
		count = sport->rx_sgl.length - ring->tail;
		lpuart_handle_sysrq_chars(&sport->port,
					  ring->buf + ring->tail, count);
		ring->tail = 0;
	}

	if (ring->head > ring->tail) {
		count = ring->head - ring->tail;
		lpuart_handle_sysrq_chars(&sport->port,
					  ring->buf + ring->tail, count);
		ring->tail = ring->head;
	}
}

1110 1111 1112 1113 1114
static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
{
	struct tty_port *port = &sport->port.state->port;
	struct dma_tx_state state;
	enum dma_status dmastat;
1115
	struct dma_chan *chan = sport->dma_rx_chan;
1116 1117
	struct circ_buf *ring = &sport->rx_ring;
	unsigned long flags;
1118
	int count, copied;
1119

1120 1121
	if (lpuart_is_32(sport)) {
		unsigned long sr = lpuart32_read(&sport->port, UARTSTAT);
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
		if (sr & (UARTSTAT_PE | UARTSTAT_FE)) {
			/* Read DR to clear the error flags */
			lpuart32_read(&sport->port, UARTDATA);

			if (sr & UARTSTAT_PE)
				sport->port.icount.parity++;
			else if (sr & UARTSTAT_FE)
				sport->port.icount.frame++;
		}
	} else {
		unsigned char sr = readb(sport->port.membase + UARTSR1);

		if (sr & (UARTSR1_PE | UARTSR1_FE)) {
1136 1137 1138 1139 1140 1141 1142
			unsigned char cr2;

			/* Disable receiver during this operation... */
			cr2 = readb(sport->port.membase + UARTCR2);
			cr2 &= ~UARTCR2_RE;
			writeb(cr2, sport->port.membase + UARTCR2);

1143 1144
			/* Read DR to clear the error flags */
			readb(sport->port.membase + UARTDR);
1145

1146 1147 1148 1149
			if (sr & UARTSR1_PE)
				sport->port.icount.parity++;
			else if (sr & UARTSR1_FE)
				sport->port.icount.frame++;
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
			/*
			 * At this point parity/framing error is
			 * cleared However, since the DMA already read
			 * the data register and we had to read it
			 * again after reading the status register to
			 * properly clear the flags, the FIFO actually
			 * underflowed... This requires a clearing of
			 * the FIFO...
			 */
			if (readb(sport->port.membase + UARTSFIFO) &
			    UARTSFIFO_RXUF) {
				writeb(UARTSFIFO_RXUF,
				       sport->port.membase + UARTSFIFO);
				writeb(UARTCFIFO_RXFLUSH,
				       sport->port.membase + UARTCFIFO);
			}

			cr2 |= UARTCR2_RE;
			writeb(cr2, sport->port.membase + UARTCR2);
1169
		}
1170 1171 1172 1173 1174 1175
	}

	async_tx_ack(sport->dma_rx_desc);

	spin_lock_irqsave(&sport->port.lock, flags);

1176
	dmastat = dmaengine_tx_status(chan, sport->dma_rx_cookie, &state);
1177 1178 1179 1180 1181 1182 1183
	if (dmastat == DMA_ERROR) {
		dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
		spin_unlock_irqrestore(&sport->port.lock, flags);
		return;
	}

	/* CPU claims ownership of RX DMA buffer */
1184 1185
	dma_sync_sg_for_cpu(chan->device->dev, &sport->rx_sgl, 1,
			    DMA_FROM_DEVICE);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195

	/*
	 * ring->head points to the end of data already written by the DMA.
	 * ring->tail points to the beginning of data to be read by the
	 * framework.
	 * The current transfer size should not be larger than the dma buffer
	 * length.
	 */
	ring->head = sport->rx_sgl.length - state.residue;
	BUG_ON(ring->head > sport->rx_sgl.length);
1196 1197 1198 1199 1200 1201 1202 1203 1204

	/*
	 * Silent handling of keys pressed in the sysrq timeframe
	 */
	if (sport->port.sysrq) {
		lpuart_handle_sysrq(sport);
		goto exit;
	}

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	/*
	 * At this point ring->head may point to the first byte right after the
	 * last byte of the dma buffer:
	 * 0 <= ring->head <= sport->rx_sgl.length
	 *
	 * However ring->tail must always points inside the dma buffer:
	 * 0 <= ring->tail <= sport->rx_sgl.length - 1
	 *
	 * Since we use a ring buffer, we have to handle the case
	 * where head is lower than tail. In such a case, we first read from
	 * tail to the end of the buffer then reset tail.
	 */
	if (ring->head < ring->tail) {
		count = sport->rx_sgl.length - ring->tail;

1220 1221 1222
		copied = tty_insert_flip_string(port, ring->buf + ring->tail, count);
		if (copied != count)
			sport->port.icount.buf_overrun++;
1223
		ring->tail = 0;
1224
		sport->port.icount.rx += copied;
1225 1226 1227 1228 1229
	}

	/* Finally we read data from tail to head */
	if (ring->tail < ring->head) {
		count = ring->head - ring->tail;
1230 1231 1232
		copied = tty_insert_flip_string(port, ring->buf + ring->tail, count);
		if (copied != count)
			sport->port.icount.buf_overrun++;
1233 1234 1235 1236
		/* Wrap ring->head if needed */
		if (ring->head >= sport->rx_sgl.length)
			ring->head = 0;
		ring->tail = ring->head;
1237
		sport->port.icount.rx += copied;
1238 1239
	}

1240
exit:
1241
	dma_sync_sg_for_device(chan->device->dev, &sport->rx_sgl, 1,
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
			       DMA_FROM_DEVICE);

	spin_unlock_irqrestore(&sport->port.lock, flags);

	tty_flip_buffer_push(port);
	mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
}

static void lpuart_dma_rx_complete(void *arg)
{
	struct lpuart_port *sport = arg;

	lpuart_copy_rx_to_tty(sport);
}

1257
static void lpuart_timer_func(struct timer_list *t)
1258
{
1259
	struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

	lpuart_copy_rx_to_tty(sport);
}

static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
{
	struct dma_slave_config dma_rx_sconfig = {};
	struct circ_buf *ring = &sport->rx_ring;
	int ret, nent;
	int bits, baud;
1270 1271
	struct tty_port *port = &sport->port.state->port;
	struct tty_struct *tty = port->tty;
1272
	struct ktermios *termios = &tty->termios;
1273
	struct dma_chan *chan = sport->dma_rx_chan;
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

	baud = tty_get_baud_rate(tty);

	bits = (termios->c_cflag & CSIZE) == CS7 ? 9 : 10;
	if (termios->c_cflag & PARENB)
		bits++;

	/*
	 * Calculate length of one DMA buffer size to keep latency below
	 * 10ms at any baud rate.
	 */
	sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud /  bits / 1000) * 2;
	sport->rx_dma_rng_buf_len = (1 << (fls(sport->rx_dma_rng_buf_len) - 1));
	if (sport->rx_dma_rng_buf_len < 16)
		sport->rx_dma_rng_buf_len = 16;

1290
	ring->buf = kzalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
1291
	if (!ring->buf)
1292 1293 1294
		return -ENOMEM;

	sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1295 1296
	nent = dma_map_sg(chan->device->dev, &sport->rx_sgl, 1,
			  DMA_FROM_DEVICE);
1297 1298 1299 1300 1301 1302

	if (!nent) {
		dev_err(sport->port.dev, "DMA Rx mapping error\n");
		return -EINVAL;
	}

1303
	dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
1304 1305 1306
	dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
	dma_rx_sconfig.src_maxburst = 1;
	dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
1307
	ret = dmaengine_slave_config(chan, &dma_rx_sconfig);
1308 1309 1310 1311 1312 1313 1314

	if (ret < 0) {
		dev_err(sport->port.dev,
				"DMA Rx slave config failed, err = %d\n", ret);
		return ret;
	}

1315
	sport->dma_rx_desc = dmaengine_prep_dma_cyclic(chan,
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
				 sg_dma_address(&sport->rx_sgl),
				 sport->rx_sgl.length,
				 sport->rx_sgl.length / 2,
				 DMA_DEV_TO_MEM,
				 DMA_PREP_INTERRUPT);
	if (!sport->dma_rx_desc) {
		dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
		return -EFAULT;
	}

	sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
	sport->dma_rx_desc->callback_param = sport;
	sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
1329
	dma_async_issue_pending(chan);
1330

1331 1332 1333 1334 1335 1336 1337 1338
	if (lpuart_is_32(sport)) {
		unsigned long temp = lpuart32_read(&sport->port, UARTBAUD);

		lpuart32_write(&sport->port, temp | UARTBAUD_RDMAE, UARTBAUD);
	} else {
		writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
		       sport->port.membase + UARTCR5);
	}
1339 1340 1341 1342 1343 1344 1345 1346

	return 0;
}

static void lpuart_dma_rx_free(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port,
					struct lpuart_port, port);
1347
	struct dma_chan *chan = sport->dma_rx_chan;
1348

1349
	dmaengine_terminate_all(chan);
1350
	dma_unmap_sg(chan->device->dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1351 1352 1353 1354 1355 1356 1357
	kfree(sport->rx_ring.buf);
	sport->rx_ring.tail = 0;
	sport->rx_ring.head = 0;
	sport->dma_rx_desc = NULL;
	sport->dma_rx_cookie = -EINVAL;
}

1358
static int lpuart_config_rs485(struct uart_port *port, struct ktermios *termios,
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
			struct serial_rs485 *rs485)
{
	struct lpuart_port *sport = container_of(port,
			struct lpuart_port, port);

	u8 modem = readb(sport->port.membase + UARTMODEM) &
		~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
	writeb(modem, sport->port.membase + UARTMODEM);

	if (rs485->flags & SER_RS485_ENABLED) {
		/* Enable auto RS-485 RTS mode */
		modem |= UARTMODEM_TXRTSE;

		/*
		 * The hardware defaults to RTS logic HIGH while transfer.
		 * Switch polarity in case RTS shall be logic HIGH
		 * after transfer.
		 * Note: UART is assumed to be active high.
		 */
		if (rs485->flags & SER_RS485_RTS_ON_SEND)
			modem &= ~UARTMODEM_TXRTSPOL;
		else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
			modem |= UARTMODEM_TXRTSPOL;
	}

	writeb(modem, sport->port.membase + UARTMODEM);
	return 0;
}

1388
static int lpuart32_config_rs485(struct uart_port *port, struct ktermios *termios,
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
			struct serial_rs485 *rs485)
{
	struct lpuart_port *sport = container_of(port,
			struct lpuart_port, port);

	unsigned long modem = lpuart32_read(&sport->port, UARTMODIR)
				& ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
	lpuart32_write(&sport->port, modem, UARTMODIR);

	if (rs485->flags & SER_RS485_ENABLED) {
		/* Enable auto RS-485 RTS mode */
		modem |= UARTMODEM_TXRTSE;

		/*
		 * The hardware defaults to RTS logic HIGH while transfer.
		 * Switch polarity in case RTS shall be logic HIGH
		 * after transfer.
		 * Note: UART is assumed to be active high.
		 */
		if (rs485->flags & SER_RS485_RTS_ON_SEND)
			modem &= ~UARTMODEM_TXRTSPOL;
		else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
			modem |= UARTMODEM_TXRTSPOL;
	}

	lpuart32_write(&sport->port, modem, UARTMODIR);
	return 0;
}

1418 1419
static unsigned int lpuart_get_mctrl(struct uart_port *port)
{
1420 1421 1422 1423 1424 1425 1426 1427
	unsigned int mctrl = 0;
	u8 reg;

	reg = readb(port->membase + UARTCR1);
	if (reg & UARTCR1_LOOPS)
		mctrl |= TIOCM_LOOP;

	return mctrl;
1428 1429
}

1430 1431
static unsigned int lpuart32_get_mctrl(struct uart_port *port)
{
1432
	unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
1433 1434 1435 1436 1437 1438 1439
	u32 reg;

	reg = lpuart32_read(port, UARTCTRL);
	if (reg & UARTCTRL_LOOPS)
		mctrl |= TIOCM_LOOP;

	return mctrl;
1440 1441
}

1442 1443
static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
1444 1445 1446 1447 1448 1449 1450 1451
	u8 reg;

	reg = readb(port->membase + UARTCR1);

	/* for internal loopback we need LOOPS=1 and RSRC=0 */
	reg &= ~(UARTCR1_LOOPS | UARTCR1_RSRC);
	if (mctrl & TIOCM_LOOP)
		reg |= UARTCR1_LOOPS;
1452

1453
	writeb(reg, port->membase + UARTCR1);
1454 1455
}

1456 1457
static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
1458 1459 1460 1461 1462 1463 1464 1465
	u32 reg;

	reg = lpuart32_read(port, UARTCTRL);

	/* for internal loopback we need LOOPS=1 and RSRC=0 */
	reg &= ~(UARTCTRL_LOOPS | UARTCTRL_RSRC);
	if (mctrl & TIOCM_LOOP)
		reg |= UARTCTRL_LOOPS;
1466

1467
	lpuart32_write(port, reg, UARTCTRL);
1468 1469
}

1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
static void lpuart_break_ctl(struct uart_port *port, int break_state)
{
	unsigned char temp;

	temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;

	if (break_state != 0)
		temp |= UARTCR2_SBK;

	writeb(temp, port->membase + UARTCR2);
}

1482 1483 1484 1485
static void lpuart32_break_ctl(struct uart_port *port, int break_state)
{
	unsigned long temp;

1486
	temp = lpuart32_read(port, UARTCTRL) & ~UARTCTRL_SBK;
1487 1488 1489 1490

	if (break_state != 0)
		temp |= UARTCTRL_SBK;

1491
	lpuart32_write(port, temp, UARTCTRL);
1492 1493
}

1494 1495 1496
static void lpuart_setup_watermark(struct lpuart_port *sport)
{
	unsigned char val, cr2;
1497
	unsigned char cr2_saved;
1498 1499

	cr2 = readb(sport->port.membase + UARTCR2);
1500
	cr2_saved = cr2;
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
			UARTCR2_RIE | UARTCR2_RE);
	writeb(cr2, sport->port.membase + UARTCR2);

	val = readb(sport->port.membase + UARTPFIFO);
	writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
			sport->port.membase + UARTPFIFO);

	/* flush Tx and Rx FIFO */
	writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
			sport->port.membase + UARTCFIFO);

1513 1514 1515 1516 1517 1518
	/* explicitly clear RDRF */
	if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
		readb(sport->port.membase + UARTDR);
		writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
	}

Y
Yuan Yao 已提交
1519
	writeb(0, sport->port.membase + UARTTWFIFO);
1520
	writeb(1, sport->port.membase + UARTRWFIFO);
1521 1522 1523

	/* Restore cr2 */
	writeb(cr2_saved, sport->port.membase + UARTCR2);
1524 1525
}

1526 1527 1528 1529 1530 1531 1532
static void lpuart_setup_watermark_enable(struct lpuart_port *sport)
{
	unsigned char cr2;

	lpuart_setup_watermark(sport);

	cr2 = readb(sport->port.membase + UARTCR2);
1533
	cr2 |= UARTCR2_RIE | UARTCR2_RE | UARTCR2_TE;
1534 1535 1536
	writeb(cr2, sport->port.membase + UARTCR2);
}

1537 1538 1539 1540 1541
static void lpuart32_setup_watermark(struct lpuart_port *sport)
{
	unsigned long val, ctrl;
	unsigned long ctrl_saved;

1542
	ctrl = lpuart32_read(&sport->port, UARTCTRL);
1543 1544 1545
	ctrl_saved = ctrl;
	ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
			UARTCTRL_RIE | UARTCTRL_RE);
1546
	lpuart32_write(&sport->port, ctrl, UARTCTRL);
1547 1548

	/* enable FIFO mode */
1549
	val = lpuart32_read(&sport->port, UARTFIFO);
1550 1551
	val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
	val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
1552
	lpuart32_write(&sport->port, val, UARTFIFO);
1553 1554 1555

	/* set the watermark */
	val = (0x1 << UARTWATER_RXWATER_OFF) | (0x0 << UARTWATER_TXWATER_OFF);
1556
	lpuart32_write(&sport->port, val, UARTWATER);
1557 1558

	/* Restore cr2 */
1559
	lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
1560 1561
}

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
static void lpuart32_setup_watermark_enable(struct lpuart_port *sport)
{
	u32 temp;

	lpuart32_setup_watermark(sport);

	temp = lpuart32_read(&sport->port, UARTCTRL);
	temp |= UARTCTRL_RE | UARTCTRL_TE | UARTCTRL_ILIE;
	lpuart32_write(&sport->port, temp, UARTCTRL);
}

1573
static void rx_dma_timer_init(struct lpuart_port *sport)
Y
Yuan Yao 已提交
1574
{
1575 1576 1577
	timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
	sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
	add_timer(&sport->lpuart_timer);
Y
Yuan Yao 已提交
1578 1579
}

1580
static void lpuart_request_dma(struct lpuart_port *sport)
1581
{
1582 1583
	sport->dma_tx_chan = dma_request_chan(sport->port.dev, "tx");
	if (IS_ERR(sport->dma_tx_chan)) {
1584 1585 1586
		dev_dbg_once(sport->port.dev,
			     "DMA tx channel request failed, operating without tx DMA (%ld)\n",
			     PTR_ERR(sport->dma_tx_chan));
1587 1588 1589
		sport->dma_tx_chan = NULL;
	}

1590 1591
	sport->dma_rx_chan = dma_request_chan(sport->port.dev, "rx");
	if (IS_ERR(sport->dma_rx_chan)) {
1592 1593 1594
		dev_dbg_once(sport->port.dev,
			     "DMA rx channel request failed, operating without rx DMA (%ld)\n",
			     PTR_ERR(sport->dma_rx_chan));
1595 1596 1597 1598 1599 1600 1601 1602 1603
		sport->dma_rx_chan = NULL;
	}
}

static void lpuart_tx_dma_startup(struct lpuart_port *sport)
{
	u32 uartbaud;
	int ret;

1604 1605 1606
	if (uart_console(&sport->port))
		goto err;

1607 1608 1609
	if (!sport->dma_tx_chan)
		goto err;

1610
	ret = lpuart_dma_tx_request(&sport->port);
1611
	if (ret)
1612 1613 1614 1615 1616 1617 1618 1619
		goto err;

	init_waitqueue_head(&sport->dma_wait);
	sport->lpuart_dma_tx_use = true;
	if (lpuart_is_32(sport)) {
		uartbaud = lpuart32_read(&sport->port, UARTBAUD);
		lpuart32_write(&sport->port,
			       uartbaud | UARTBAUD_TDMAE, UARTBAUD);
1620
	} else {
1621 1622
		writeb(readb(sport->port.membase + UARTCR5) |
		       UARTCR5_TDMAS, sport->port.membase + UARTCR5);
1623
	}
1624 1625 1626 1627 1628

	return;

err:
	sport->lpuart_dma_tx_use = false;
1629 1630
}

1631 1632
static void lpuart_rx_dma_startup(struct lpuart_port *sport)
{
1633
	int ret;
1634
	unsigned char cr3;
1635

1636 1637 1638
	if (uart_console(&sport->port))
		goto err;

1639
	if (!sport->dma_rx_chan)
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
		goto err;

	ret = lpuart_start_rx_dma(sport);
	if (ret)
		goto err;

	/* set Rx DMA timeout */
	sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
	if (!sport->dma_rx_timeout)
		sport->dma_rx_timeout = 1;

	sport->lpuart_dma_rx_use = true;
	rx_dma_timer_init(sport);

1654
	if (sport->port.has_sysrq && !lpuart_is_32(sport)) {
1655 1656 1657 1658 1659
		cr3 = readb(sport->port.membase + UARTCR3);
		cr3 |= UARTCR3_FEIE;
		writeb(cr3, sport->port.membase + UARTCR3);
	}

1660 1661 1662 1663
	return;

err:
	sport->lpuart_dma_rx_use = false;
1664 1665
}

1666 1667 1668 1669 1670 1671
static int lpuart_startup(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
	unsigned long flags;
	unsigned char temp;

1672 1673 1674
	/* determine FIFO size and enable FIFO mode */
	temp = readb(sport->port.membase + UARTPFIFO);

1675 1676
	sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_TXSIZE_OFF) &
					    UARTPFIFO_FIFOSIZE_MASK);
1677 1678
	sport->port.fifosize = sport->txfifo_size;

1679 1680
	sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_RXSIZE_OFF) &
					    UARTPFIFO_FIFOSIZE_MASK);
1681

1682 1683
	lpuart_request_dma(sport);

1684 1685
	spin_lock_irqsave(&sport->port.lock, flags);

1686
	lpuart_setup_watermark_enable(sport);
1687

1688
	lpuart_rx_dma_startup(sport);
1689
	lpuart_tx_dma_startup(sport);
1690

1691
	spin_unlock_irqrestore(&sport->port.lock, flags);
1692

1693 1694 1695
	return 0;
}

1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
static void lpuart32_configure(struct lpuart_port *sport)
{
	unsigned long temp;

	if (sport->lpuart_dma_rx_use) {
		/* RXWATER must be 0 */
		temp = lpuart32_read(&sport->port, UARTWATER);
		temp &= ~(UARTWATER_WATER_MASK << UARTWATER_RXWATER_OFF);
		lpuart32_write(&sport->port, temp, UARTWATER);
	}
	temp = lpuart32_read(&sport->port, UARTCTRL);
	if (!sport->lpuart_dma_rx_use)
		temp |= UARTCTRL_RIE;
	if (!sport->lpuart_dma_tx_use)
		temp |= UARTCTRL_TIE;
	lpuart32_write(&sport->port, temp, UARTCTRL);
}

1714 1715 1716 1717 1718 1719 1720
static int lpuart32_startup(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
	unsigned long flags;
	unsigned long temp;

	/* determine FIFO size */
1721
	temp = lpuart32_read(&sport->port, UARTFIFO);
1722

1723 1724
	sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_TXSIZE_OFF) &
					    UARTFIFO_FIFOSIZE_MASK);
1725 1726
	sport->port.fifosize = sport->txfifo_size;

1727 1728
	sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_RXSIZE_OFF) &
					    UARTFIFO_FIFOSIZE_MASK);
1729

1730
	/*
1731 1732 1733
	 * The LS1021A and LS1028A have a fixed FIFO depth of 16 words.
	 * Although they support the RX/TXSIZE fields, their encoding is
	 * different. Eg the reference manual states 0b101 is 16 words.
1734
	 */
1735
	if (is_layerscape_lpuart(sport)) {
1736 1737 1738 1739 1740
		sport->rxfifo_size = 16;
		sport->txfifo_size = 16;
		sport->port.fifosize = sport->txfifo_size;
	}

1741 1742
	lpuart_request_dma(sport);

1743 1744
	spin_lock_irqsave(&sport->port.lock, flags);

1745
	lpuart32_setup_watermark_enable(sport);
1746

1747
	lpuart_rx_dma_startup(sport);
1748
	lpuart_tx_dma_startup(sport);
1749

1750
	lpuart32_configure(sport);
1751 1752 1753 1754 1755

	spin_unlock_irqrestore(&sport->port.lock, flags);
	return 0;
}

1756 1757 1758 1759 1760 1761 1762 1763
static void lpuart_dma_shutdown(struct lpuart_port *sport)
{
	if (sport->lpuart_dma_rx_use) {
		del_timer_sync(&sport->lpuart_timer);
		lpuart_dma_rx_free(&sport->port);
	}

	if (sport->lpuart_dma_tx_use) {
1764 1765
		if (wait_event_interruptible_timeout(sport->dma_wait,
			!sport->dma_tx_in_progress, msecs_to_jiffies(300)) <= 0) {
1766 1767 1768 1769
			sport->dma_tx_in_progress = false;
			dmaengine_terminate_all(sport->dma_tx_chan);
		}
	}
1770 1771 1772 1773 1774

	if (sport->dma_tx_chan)
		dma_release_channel(sport->dma_tx_chan);
	if (sport->dma_rx_chan)
		dma_release_channel(sport->dma_rx_chan);
1775 1776
}

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
static void lpuart_shutdown(struct uart_port *port)
{
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
	unsigned char temp;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);

	/* disable Rx/Tx and interrupts */
	temp = readb(port->membase + UARTCR2);
	temp &= ~(UARTCR2_TE | UARTCR2_RE |
			UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
	writeb(temp, port->membase + UARTCR2);

	spin_unlock_irqrestore(&port->lock, flags);

1793
	lpuart_dma_shutdown(sport);
1794 1795
}

1796 1797
static void lpuart32_shutdown(struct uart_port *port)
{
1798 1799
	struct lpuart_port *sport =
		container_of(port, struct lpuart_port, port);
1800 1801 1802 1803 1804 1805
	unsigned long temp;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);

	/* disable Rx/Tx and interrupts */
1806
	temp = lpuart32_read(port, UARTCTRL);
1807 1808
	temp &= ~(UARTCTRL_TE | UARTCTRL_RE |
			UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
1809
	lpuart32_write(port, temp, UARTCTRL);
1810 1811

	spin_unlock_irqrestore(&port->lock, flags);
1812

1813
	lpuart_dma_shutdown(sport);
1814 1815
}

1816 1817 1818 1819 1820 1821
static void
lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
		   struct ktermios *old)
{
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
	unsigned long flags;
1822
	unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
1823 1824 1825 1826 1827 1828
	unsigned int  baud;
	unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
	unsigned int sbr, brfa;

	cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
	old_cr2 = readb(sport->port.membase + UARTCR2);
1829
	cr3 = readb(sport->port.membase + UARTCR3);
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
	cr4 = readb(sport->port.membase + UARTCR4);
	bdh = readb(sport->port.membase + UARTBDH);
	modem = readb(sport->port.membase + UARTMODEM);
	/*
	 * only support CS8 and CS7, and for CS7 must enable PE.
	 * supported mode:
	 *  - (7,e/o,1)
	 *  - (8,n,1)
	 *  - (8,m/s,1)
	 *  - (8,e/o,1)
	 */
	while ((termios->c_cflag & CSIZE) != CS8 &&
		(termios->c_cflag & CSIZE) != CS7) {
		termios->c_cflag &= ~CSIZE;
		termios->c_cflag |= old_csize;
		old_csize = CS8;
	}

	if ((termios->c_cflag & CSIZE) == CS8 ||
		(termios->c_cflag & CSIZE) == CS7)
		cr1 = old_cr1 & ~UARTCR1_M;

	if (termios->c_cflag & CMSPAR) {
		if ((termios->c_cflag & CSIZE) != CS8) {
			termios->c_cflag &= ~CSIZE;
			termios->c_cflag |= CS8;
		}
		cr1 |= UARTCR1_M;
	}

1860 1861 1862 1863 1864 1865 1866
	/*
	 * When auto RS-485 RTS mode is enabled,
	 * hardware flow control need to be disabled.
	 */
	if (sport->port.rs485.flags & SER_RS485_ENABLED)
		termios->c_cflag &= ~CRTSCTS;

1867
	if (termios->c_cflag & CRTSCTS)
1868
		modem |= UARTMODEM_RXRTSE | UARTMODEM_TXCTSE;
1869
	else
1870 1871
		modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);

1872
	termios->c_cflag &= ~CSTOPB;
1873 1874 1875 1876 1877

	/* parity must be enabled when CS7 to match 8-bits format */
	if ((termios->c_cflag & CSIZE) == CS7)
		termios->c_cflag |= PARENB;

1878
	if (termios->c_cflag & PARENB) {
1879 1880
		if (termios->c_cflag & CMSPAR) {
			cr1 &= ~UARTCR1_PE;
1881 1882 1883 1884
			if (termios->c_cflag & PARODD)
				cr3 |= UARTCR3_T8;
			else
				cr3 &= ~UARTCR3_T8;
1885 1886 1887 1888 1889 1890 1891 1892 1893
		} else {
			cr1 |= UARTCR1_PE;
			if ((termios->c_cflag & CSIZE) == CS8)
				cr1 |= UARTCR1_M;
			if (termios->c_cflag & PARODD)
				cr1 |= UARTCR1_PT;
			else
				cr1 &= ~UARTCR1_PT;
		}
1894 1895
	} else {
		cr1 &= ~UARTCR1_PE;
1896 1897 1898 1899 1900
	}

	/* ask the core to calculate the divisor */
	baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);

1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
	/*
	 * Need to update the Ring buffer length according to the selected
	 * baud rate and restart Rx DMA path.
	 *
	 * Since timer function acqures sport->port.lock, need to stop before
	 * acquring same lock because otherwise del_timer_sync() can deadlock.
	 */
	if (old && sport->lpuart_dma_rx_use) {
		del_timer_sync(&sport->lpuart_timer);
		lpuart_dma_rx_free(&sport->port);
	}

1913 1914 1915 1916
	spin_lock_irqsave(&sport->port.lock, flags);

	sport->port.read_status_mask = 0;
	if (termios->c_iflag & INPCK)
1917
		sport->port.read_status_mask |= UARTSR1_FE | UARTSR1_PE;
P
Peter Hurley 已提交
1918
	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
		sport->port.read_status_mask |= UARTSR1_FE;

	/* characters to ignore */
	sport->port.ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR)
		sport->port.ignore_status_mask |= UARTSR1_PE;
	if (termios->c_iflag & IGNBRK) {
		sport->port.ignore_status_mask |= UARTSR1_FE;
		/*
		 * if we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
			sport->port.ignore_status_mask |= UARTSR1_OR;
	}

	/* update the per-port timeout */
	uart_update_timeout(port, termios->c_cflag, baud);

	/* wait transmit engin complete */
1939
	lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953

	/* disable transmit and receive */
	writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
			sport->port.membase + UARTCR2);

	sbr = sport->port.uartclk / (16 * baud);
	brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
	bdh &= ~UARTBDH_SBR_MASK;
	bdh |= (sbr >> 8) & 0x1F;
	cr4 &= ~UARTCR4_BRFA_MASK;
	brfa &= UARTCR4_BRFA_MASK;
	writeb(cr4 | brfa, sport->port.membase + UARTCR4);
	writeb(bdh, sport->port.membase + UARTBDH);
	writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
1954
	writeb(cr3, sport->port.membase + UARTCR3);
1955 1956 1957 1958 1959 1960
	writeb(cr1, sport->port.membase + UARTCR1);
	writeb(modem, sport->port.membase + UARTMODEM);

	/* restore control register */
	writeb(old_cr2, sport->port.membase + UARTCR2);

1961 1962
	if (old && sport->lpuart_dma_rx_use) {
		if (!lpuart_start_rx_dma(sport))
1963
			rx_dma_timer_init(sport);
1964
		else
1965 1966 1967
			sport->lpuart_dma_rx_use = false;
	}

1968 1969 1970
	spin_unlock_irqrestore(&sport->port.lock, flags);
}

1971 1972 1973
static void __lpuart32_serial_setbrg(struct uart_port *port,
				     unsigned int baudrate, bool use_rx_dma,
				     bool use_tx_dma)
1974 1975
{
	u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
1976
	u32 clk = port->uartclk;
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009

	/*
	 * The idea is to use the best OSR (over-sampling rate) possible.
	 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
	 * Loop to find the best OSR value possible, one that generates minimum
	 * baud_diff iterate through the rest of the supported values of OSR.
	 *
	 * Calculation Formula:
	 *  Baud Rate = baud clock / ((OSR+1) × SBR)
	 */
	baud_diff = baudrate;
	osr = 0;
	sbr = 0;

	for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
		/* calculate the temporary sbr value  */
		tmp_sbr = (clk / (baudrate * tmp_osr));
		if (tmp_sbr == 0)
			tmp_sbr = 1;

		/*
		 * calculate the baud rate difference based on the temporary
		 * osr and sbr values
		 */
		tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;

		/* select best values between sbr and sbr+1 */
		tmp = clk / (tmp_osr * (tmp_sbr + 1));
		if (tmp_diff > (baudrate - tmp)) {
			tmp_diff = baudrate - tmp;
			tmp_sbr++;
		}

2010 2011 2012
		if (tmp_sbr > UARTBAUD_SBR_MASK)
			continue;

2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
		if (tmp_diff <= baud_diff) {
			baud_diff = tmp_diff;
			osr = tmp_osr;
			sbr = tmp_sbr;

			if (!baud_diff)
				break;
		}
	}

	/* handle buadrate outside acceptable rate */
	if (baud_diff > ((baudrate / 100) * 3))
2025
		dev_warn(port->dev,
2026 2027
			 "unacceptable baud rate difference of more than 3%%\n");

2028
	tmp = lpuart32_read(port, UARTBAUD);
2029 2030 2031 2032 2033

	if ((osr > 3) && (osr < 8))
		tmp |= UARTBAUD_BOTHEDGE;

	tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
2034
	tmp |= ((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT;
2035 2036 2037 2038

	tmp &= ~UARTBAUD_SBR_MASK;
	tmp |= sbr & UARTBAUD_SBR_MASK;

2039
	if (!use_rx_dma)
2040
		tmp &= ~UARTBAUD_RDMAE;
2041
	if (!use_tx_dma)
2042
		tmp &= ~UARTBAUD_TDMAE;
2043

2044 2045 2046 2047 2048 2049 2050 2051 2052
	lpuart32_write(port, tmp, UARTBAUD);
}

static void lpuart32_serial_setbrg(struct lpuart_port *sport,
				   unsigned int baudrate)
{
	__lpuart32_serial_setbrg(&sport->port, baudrate,
				 sport->lpuart_dma_rx_use,
				 sport->lpuart_dma_tx_use);
2053 2054
}

2055

2056 2057 2058 2059 2060 2061
static void
lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
		   struct ktermios *old)
{
	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
	unsigned long flags;
2062
	unsigned long ctrl, old_ctrl, bd, modem;
2063 2064 2065
	unsigned int  baud;
	unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;

2066
	ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
2067
	bd = lpuart32_read(&sport->port, UARTBAUD);
2068
	modem = lpuart32_read(&sport->port, UARTMODIR);
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
	/*
	 * only support CS8 and CS7, and for CS7 must enable PE.
	 * supported mode:
	 *  - (7,e/o,1)
	 *  - (8,n,1)
	 *  - (8,m/s,1)
	 *  - (8,e/o,1)
	 */
	while ((termios->c_cflag & CSIZE) != CS8 &&
		(termios->c_cflag & CSIZE) != CS7) {
		termios->c_cflag &= ~CSIZE;
		termios->c_cflag |= old_csize;
		old_csize = CS8;
	}

	if ((termios->c_cflag & CSIZE) == CS8 ||
		(termios->c_cflag & CSIZE) == CS7)
		ctrl = old_ctrl & ~UARTCTRL_M;

	if (termios->c_cflag & CMSPAR) {
		if ((termios->c_cflag & CSIZE) != CS8) {
			termios->c_cflag &= ~CSIZE;
			termios->c_cflag |= CS8;
		}
		ctrl |= UARTCTRL_M;
	}

2096 2097 2098 2099 2100 2101 2102
	/*
	 * When auto RS-485 RTS mode is enabled,
	 * hardware flow control need to be disabled.
	 */
	if (sport->port.rs485.flags & SER_RS485_ENABLED)
		termios->c_cflag &= ~CRTSCTS;

2103 2104 2105
	if (termios->c_cflag & CRTSCTS)
		modem |= UARTMODIR_RXRTSE | UARTMODIR_TXCTSE;
	else
2106
		modem &= ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
2107 2108

	if (termios->c_cflag & CSTOPB)
2109 2110 2111
		bd |= UARTBAUD_SBNS;
	else
		bd &= ~UARTBAUD_SBNS;
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121

	/* parity must be enabled when CS7 to match 8-bits format */
	if ((termios->c_cflag & CSIZE) == CS7)
		termios->c_cflag |= PARENB;

	if ((termios->c_cflag & PARENB)) {
		if (termios->c_cflag & CMSPAR) {
			ctrl &= ~UARTCTRL_PE;
			ctrl |= UARTCTRL_M;
		} else {
2122
			ctrl |= UARTCTRL_PE;
2123 2124 2125 2126 2127 2128 2129
			if ((termios->c_cflag & CSIZE) == CS8)
				ctrl |= UARTCTRL_M;
			if (termios->c_cflag & PARODD)
				ctrl |= UARTCTRL_PT;
			else
				ctrl &= ~UARTCTRL_PT;
		}
2130 2131
	} else {
		ctrl &= ~UARTCTRL_PE;
2132 2133 2134
	}

	/* ask the core to calculate the divisor */
2135
	baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 4);
2136

2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
	/*
	 * Need to update the Ring buffer length according to the selected
	 * baud rate and restart Rx DMA path.
	 *
	 * Since timer function acqures sport->port.lock, need to stop before
	 * acquring same lock because otherwise del_timer_sync() can deadlock.
	 */
	if (old && sport->lpuart_dma_rx_use) {
		del_timer_sync(&sport->lpuart_timer);
		lpuart_dma_rx_free(&sport->port);
	}

2149 2150 2151 2152
	spin_lock_irqsave(&sport->port.lock, flags);

	sport->port.read_status_mask = 0;
	if (termios->c_iflag & INPCK)
2153
		sport->port.read_status_mask |= UARTSTAT_FE | UARTSTAT_PE;
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
		sport->port.read_status_mask |= UARTSTAT_FE;

	/* characters to ignore */
	sport->port.ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR)
		sport->port.ignore_status_mask |= UARTSTAT_PE;
	if (termios->c_iflag & IGNBRK) {
		sport->port.ignore_status_mask |= UARTSTAT_FE;
		/*
		 * if we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
			sport->port.ignore_status_mask |= UARTSTAT_OR;
	}

	/* update the per-port timeout */
	uart_update_timeout(port, termios->c_cflag, baud);

	/* wait transmit engin complete */
2175
	lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2176 2177

	/* disable transmit and receive */
2178 2179
	lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
		       UARTCTRL);
2180

2181
	lpuart32_write(&sport->port, bd, UARTBAUD);
2182
	lpuart32_serial_setbrg(sport, baud);
2183 2184
	lpuart32_write(&sport->port, modem, UARTMODIR);
	lpuart32_write(&sport->port, ctrl, UARTCTRL);
2185 2186
	/* restore control register */

2187 2188 2189 2190 2191 2192 2193
	if (old && sport->lpuart_dma_rx_use) {
		if (!lpuart_start_rx_dma(sport))
			rx_dma_timer_init(sport);
		else
			sport->lpuart_dma_rx_use = false;
	}

2194 2195 2196
	spin_unlock_irqrestore(&sport->port.lock, flags);
}

2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
static const char *lpuart_type(struct uart_port *port)
{
	return "FSL_LPUART";
}

static void lpuart_release_port(struct uart_port *port)
{
	/* nothing to do */
}

static int lpuart_request_port(struct uart_port *port)
{
	return  0;
}

/* configure/autoconfigure the port */
static void lpuart_config_port(struct uart_port *port, int flags)
{
	if (flags & UART_CONFIG_TYPE)
		port->type = PORT_LPUART;
}

static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	int ret = 0;

	if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
		ret = -EINVAL;
	if (port->irq != ser->irq)
		ret = -EINVAL;
	if (ser->io_type != UPIO_MEM)
		ret = -EINVAL;
	if (port->uartclk / 16 != ser->baud_base)
		ret = -EINVAL;
	if (port->iobase != ser->port)
		ret = -EINVAL;
	if (ser->hub6 != 0)
		ret = -EINVAL;
	return ret;
}

2238
static const struct uart_ops lpuart_pops = {
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
	.tx_empty	= lpuart_tx_empty,
	.set_mctrl	= lpuart_set_mctrl,
	.get_mctrl	= lpuart_get_mctrl,
	.stop_tx	= lpuart_stop_tx,
	.start_tx	= lpuart_start_tx,
	.stop_rx	= lpuart_stop_rx,
	.break_ctl	= lpuart_break_ctl,
	.startup	= lpuart_startup,
	.shutdown	= lpuart_shutdown,
	.set_termios	= lpuart_set_termios,
	.type		= lpuart_type,
	.request_port	= lpuart_request_port,
	.release_port	= lpuart_release_port,
	.config_port	= lpuart_config_port,
	.verify_port	= lpuart_verify_port,
2254
	.flush_buffer	= lpuart_flush_buffer,
2255 2256 2257 2258 2259
#if defined(CONFIG_CONSOLE_POLL)
	.poll_init	= lpuart_poll_init,
	.poll_get_char	= lpuart_poll_get_char,
	.poll_put_char	= lpuart_poll_put_char,
#endif
2260 2261
};

2262
static const struct uart_ops lpuart32_pops = {
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
	.tx_empty	= lpuart32_tx_empty,
	.set_mctrl	= lpuart32_set_mctrl,
	.get_mctrl	= lpuart32_get_mctrl,
	.stop_tx	= lpuart32_stop_tx,
	.start_tx	= lpuart32_start_tx,
	.stop_rx	= lpuart32_stop_rx,
	.break_ctl	= lpuart32_break_ctl,
	.startup	= lpuart32_startup,
	.shutdown	= lpuart32_shutdown,
	.set_termios	= lpuart32_set_termios,
	.type		= lpuart_type,
	.request_port	= lpuart_request_port,
	.release_port	= lpuart_release_port,
	.config_port	= lpuart_config_port,
	.verify_port	= lpuart_verify_port,
2278
	.flush_buffer	= lpuart_flush_buffer,
2279 2280 2281 2282 2283
#if defined(CONFIG_CONSOLE_POLL)
	.poll_init	= lpuart32_poll_init,
	.poll_get_char	= lpuart32_poll_get_char,
	.poll_put_char	= lpuart32_poll_put_char,
#endif
2284 2285
};

2286 2287 2288
static struct lpuart_port *lpuart_ports[UART_NR];

#ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
2289
static void lpuart_console_putchar(struct uart_port *port, unsigned char ch)
2290
{
2291
	lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
2292 2293 2294
	writeb(ch, port->membase + UARTDR);
}

2295
static void lpuart32_console_putchar(struct uart_port *port, unsigned char ch)
2296
{
2297
	lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
2298
	lpuart32_write(port, ch, UARTDATA);
2299 2300
}

2301 2302 2303 2304 2305
static void
lpuart_console_write(struct console *co, const char *s, unsigned int count)
{
	struct lpuart_port *sport = lpuart_ports[co->index];
	unsigned char  old_cr2, cr2;
2306 2307 2308
	unsigned long flags;
	int locked = 1;

2309
	if (oops_in_progress)
2310 2311 2312
		locked = spin_trylock_irqsave(&sport->port.lock, flags);
	else
		spin_lock_irqsave(&sport->port.lock, flags);
2313 2314 2315

	/* first save CR2 and then disable interrupts */
	cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
2316
	cr2 |= UARTCR2_TE | UARTCR2_RE;
2317 2318 2319 2320 2321 2322
	cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
	writeb(cr2, sport->port.membase + UARTCR2);

	uart_console_write(&sport->port, s, count, lpuart_console_putchar);

	/* wait for transmitter finish complete and restore CR2 */
2323
	lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
2324 2325

	writeb(old_cr2, sport->port.membase + UARTCR2);
2326 2327 2328

	if (locked)
		spin_unlock_irqrestore(&sport->port.lock, flags);
2329 2330
}

2331 2332 2333 2334 2335
static void
lpuart32_console_write(struct console *co, const char *s, unsigned int count)
{
	struct lpuart_port *sport = lpuart_ports[co->index];
	unsigned long  old_cr, cr;
2336 2337 2338
	unsigned long flags;
	int locked = 1;

2339
	if (oops_in_progress)
2340 2341 2342
		locked = spin_trylock_irqsave(&sport->port.lock, flags);
	else
		spin_lock_irqsave(&sport->port.lock, flags);
2343 2344

	/* first save CR2 and then disable interrupts */
2345
	cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
2346
	cr |= UARTCTRL_TE | UARTCTRL_RE;
2347
	cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
2348
	lpuart32_write(&sport->port, cr, UARTCTRL);
2349 2350 2351 2352

	uart_console_write(&sport->port, s, count, lpuart32_console_putchar);

	/* wait for transmitter finish complete and restore CR2 */
2353
	lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2354

2355
	lpuart32_write(&sport->port, old_cr, UARTCTRL);
2356 2357 2358

	if (locked)
		spin_unlock_irqrestore(&sport->port.lock, flags);
2359 2360
}

2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
/*
 * if the port was already initialised (eg, by a boot loader),
 * try to determine the current setup.
 */
static void __init
lpuart_console_get_options(struct lpuart_port *sport, int *baud,
			   int *parity, int *bits)
{
	unsigned char cr, bdh, bdl, brfa;
	unsigned int sbr, uartclk, baud_raw;

	cr = readb(sport->port.membase + UARTCR2);
	cr &= UARTCR2_TE | UARTCR2_RE;
	if (!cr)
		return;

	/* ok, the port was enabled */

	cr = readb(sport->port.membase + UARTCR1);

	*parity = 'n';
	if (cr & UARTCR1_PE) {
		if (cr & UARTCR1_PT)
			*parity = 'o';
		else
			*parity = 'e';
	}

	if (cr & UARTCR1_M)
		*bits = 9;
	else
		*bits = 8;

	bdh = readb(sport->port.membase + UARTBDH);
	bdh &= UARTBDH_SBR_MASK;
	bdl = readb(sport->port.membase + UARTBDL);
	sbr = bdh;
	sbr <<= 8;
	sbr |= bdl;
	brfa = readb(sport->port.membase + UARTCR4);
	brfa &= UARTCR4_BRFA_MASK;

2403
	uartclk = lpuart_get_baud_clk_rate(sport);
2404 2405 2406 2407 2408 2409
	/*
	 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
	 */
	baud_raw = uartclk / (16 * (sbr + brfa / 32));

	if (*baud != baud_raw)
2410
		dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2411 2412 2413
				"from %d to %d\n", baud_raw, *baud);
}

2414 2415 2416 2417 2418 2419 2420
static void __init
lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
			   int *parity, int *bits)
{
	unsigned long cr, bd;
	unsigned int sbr, uartclk, baud_raw;

2421
	cr = lpuart32_read(&sport->port, UARTCTRL);
2422 2423 2424 2425 2426 2427
	cr &= UARTCTRL_TE | UARTCTRL_RE;
	if (!cr)
		return;

	/* ok, the port was enabled */

2428
	cr = lpuart32_read(&sport->port, UARTCTRL);
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442

	*parity = 'n';
	if (cr & UARTCTRL_PE) {
		if (cr & UARTCTRL_PT)
			*parity = 'o';
		else
			*parity = 'e';
	}

	if (cr & UARTCTRL_M)
		*bits = 9;
	else
		*bits = 8;

2443
	bd = lpuart32_read(&sport->port, UARTBAUD);
2444
	bd &= UARTBAUD_SBR_MASK;
2445 2446 2447
	if (!bd)
		return;

2448
	sbr = bd;
2449
	uartclk = lpuart_get_baud_clk_rate(sport);
2450 2451 2452 2453 2454 2455
	/*
	 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
	 */
	baud_raw = uartclk / (16 * sbr);

	if (*baud != baud_raw)
2456
		dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2457 2458 2459
				"from %d to %d\n", baud_raw, *baud);
}

2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
static int __init lpuart_console_setup(struct console *co, char *options)
{
	struct lpuart_port *sport;
	int baud = 115200;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

	/*
	 * check whether an invalid uart number has been specified, and
	 * if so, search for the first available port that does have
	 * console support.
	 */
	if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
		co->index = 0;

	sport = lpuart_ports[co->index];
	if (sport == NULL)
		return -ENODEV;

	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);
	else
2483
		if (lpuart_is_32(sport))
2484 2485 2486
			lpuart32_console_get_options(sport, &baud, &parity, &bits);
		else
			lpuart_console_get_options(sport, &baud, &parity, &bits);
2487

2488
	if (lpuart_is_32(sport))
2489 2490 2491
		lpuart32_setup_watermark(sport);
	else
		lpuart_setup_watermark(sport);
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506

	return uart_set_options(&sport->port, co, baud, parity, bits, flow);
}

static struct uart_driver lpuart_reg;
static struct console lpuart_console = {
	.name		= DEV_NAME,
	.write		= lpuart_console_write,
	.device		= uart_console_device,
	.setup		= lpuart_console_setup,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
	.data		= &lpuart_reg,
};

2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
static struct console lpuart32_console = {
	.name		= DEV_NAME,
	.write		= lpuart32_console_write,
	.device		= uart_console_device,
	.setup		= lpuart_console_setup,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
	.data		= &lpuart_reg,
};

2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
static void lpuart_early_write(struct console *con, const char *s, unsigned n)
{
	struct earlycon_device *dev = con->data;

	uart_console_write(&dev->port, s, n, lpuart_console_putchar);
}

static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
{
	struct earlycon_device *dev = con->data;

	uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
}

static int __init lpuart_early_console_setup(struct earlycon_device *device,
					  const char *opt)
{
	if (!device->port.membase)
		return -ENODEV;

	device->con->write = lpuart_early_write;
	return 0;
}

static int __init lpuart32_early_console_setup(struct earlycon_device *device,
					  const char *opt)
{
	if (!device->port.membase)
		return -ENODEV;

2547 2548 2549
	if (device->port.iotype != UPIO_MEM32)
		device->port.iotype = UPIO_MEM32BE;

2550 2551 2552 2553
	device->con->write = lpuart32_early_write;
	return 0;
}

2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
static int __init ls1028a_early_console_setup(struct earlycon_device *device,
					      const char *opt)
{
	u32 cr;

	if (!device->port.membase)
		return -ENODEV;

	device->port.iotype = UPIO_MEM32;
	device->con->write = lpuart32_early_write;

	/* set the baudrate */
	if (device->port.uartclk && device->baud)
		__lpuart32_serial_setbrg(&device->port, device->baud,
					 false, false);

	/* enable transmitter */
	cr = lpuart32_read(&device->port, UARTCTRL);
	cr |= UARTCTRL_TE;
	lpuart32_write(&device->port, cr, UARTCTRL);

	return 0;
}

2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
						   const char *opt)
{
	if (!device->port.membase)
		return -ENODEV;

	device->port.iotype = UPIO_MEM32;
	device->port.membase += IMX_REG_OFF;
	device->con->write = lpuart32_early_write;

	return 0;
}
2590 2591
OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
2592
OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1028a-lpuart", ls1028a_early_console_setup);
2593
OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
2594
OF_EARLYCON_DECLARE(lpuart32, "fsl,imx8qxp-lpuart", lpuart32_imx_early_console_setup);
2595
OF_EARLYCON_DECLARE(lpuart32, "fsl,imxrt1050-lpuart", lpuart32_imx_early_console_setup);
2596 2597
EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
2598

2599
#define LPUART_CONSOLE	(&lpuart_console)
2600
#define LPUART32_CONSOLE	(&lpuart32_console)
2601 2602
#else
#define LPUART_CONSOLE	NULL
2603
#define LPUART32_CONSOLE	NULL
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
#endif

static struct uart_driver lpuart_reg = {
	.owner		= THIS_MODULE,
	.driver_name	= DRIVER_NAME,
	.dev_name	= DEV_NAME,
	.nr		= ARRAY_SIZE(lpuart_ports),
	.cons		= LPUART_CONSOLE,
};

2614 2615 2616 2617 2618
static const struct serial_rs485 lpuart_rs485_supported = {
	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
	/* delay_rts_* and RX_DURING_TX are not supported */
};

2619 2620
static int lpuart_probe(struct platform_device *pdev)
{
2621
	const struct lpuart_soc_data *sdata = of_device_get_match_data(&pdev->dev);
2622 2623 2624
	struct device_node *np = pdev->dev.of_node;
	struct lpuart_port *sport;
	struct resource *res;
2625
	irq_handler_t handler;
2626 2627 2628 2629 2630 2631
	int ret;

	sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
	if (!sport)
		return -ENOMEM;

2632
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2633 2634 2635 2636
	sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(sport->port.membase))
		return PTR_ERR(sport->port.membase);

2637
	sport->port.membase += sdata->reg_off;
2638
	sport->port.mapbase = res->start + sdata->reg_off;
2639 2640
	sport->port.dev = &pdev->dev;
	sport->port.type = PORT_LPUART;
2641
	sport->devtype = sdata->devtype;
2642
	ret = platform_get_irq(pdev, 0);
2643
	if (ret < 0)
2644 2645
		return ret;
	sport->port.irq = ret;
2646
	sport->port.iotype = sdata->iotype;
2647
	if (lpuart_is_32(sport))
2648 2649 2650
		sport->port.ops = &lpuart32_pops;
	else
		sport->port.ops = &lpuart_pops;
2651
	sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_FSL_LPUART_CONSOLE);
2652 2653
	sport->port.flags = UPF_BOOT_AUTOCONF;

2654 2655 2656 2657
	if (lpuart_is_32(sport))
		sport->port.rs485_config = lpuart32_config_rs485;
	else
		sport->port.rs485_config = lpuart_config_rs485;
2658
	sport->port.rs485_supported = lpuart_rs485_supported;
2659

2660 2661 2662 2663
	sport->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
	if (IS_ERR(sport->ipg_clk)) {
		ret = PTR_ERR(sport->ipg_clk);
		dev_err(&pdev->dev, "failed to get uart ipg clk: %d\n", ret);
2664 2665 2666
		return ret;
	}

2667 2668 2669 2670 2671 2672 2673 2674
	sport->baud_clk = NULL;
	if (is_imx8qxp_lpuart(sport)) {
		sport->baud_clk = devm_clk_get(&pdev->dev, "baud");
		if (IS_ERR(sport->baud_clk)) {
			ret = PTR_ERR(sport->baud_clk);
			dev_err(&pdev->dev, "failed to get uart baud clk: %d\n", ret);
			return ret;
		}
2675 2676
	}

2677 2678
	ret = of_alias_get_id(np, "serial");
	if (ret < 0) {
2679 2680
		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
		return ret;
2681 2682 2683
	}
	if (ret >= ARRAY_SIZE(lpuart_ports)) {
		dev_err(&pdev->dev, "serial%d out of range\n", ret);
2684
		return -EINVAL;
2685 2686 2687
	}
	sport->port.line = ret;

2688 2689
	ret = lpuart_enable_clks(sport);
	if (ret)
2690
		return ret;
2691
	sport->port.uartclk = lpuart_get_baud_clk_rate(sport);
2692 2693 2694 2695 2696

	lpuart_ports[sport->port.line] = sport;

	platform_set_drvdata(pdev, &sport->port);

2697
	if (lpuart_is_32(sport)) {
2698
		lpuart_reg.cons = LPUART32_CONSOLE;
2699
		handler = lpuart32_int;
2700
	} else {
2701
		lpuart_reg.cons = LPUART_CONSOLE;
2702
		handler = lpuart_int;
2703
	}
2704
	ret = uart_add_one_port(&lpuart_reg, &sport->port);
2705 2706
	if (ret)
		goto failed_attach_port;
2707

2708 2709 2710 2711
	ret = lpuart_global_reset(sport);
	if (ret)
		goto failed_reset;

2712 2713 2714
	ret = uart_get_rs485_mode(&sport->port);
	if (ret)
		goto failed_get_rs485;
2715

I
Ilpo Järvinen 已提交
2716
	uart_rs485_config(&sport->port);
2717

2718 2719 2720 2721 2722
	ret = devm_request_irq(&pdev->dev, sport->port.irq, handler, 0,
				DRIVER_NAME, sport);
	if (ret)
		goto failed_irq_request;

2723
	return 0;
2724

2725
failed_irq_request:
2726
failed_get_rs485:
2727 2728
failed_reset:
	uart_remove_one_port(&lpuart_reg, &sport->port);
2729
failed_attach_port:
2730
	lpuart_disable_clks(sport);
2731
	return ret;
2732 2733 2734 2735 2736 2737 2738 2739
}

static int lpuart_remove(struct platform_device *pdev)
{
	struct lpuart_port *sport = platform_get_drvdata(pdev);

	uart_remove_one_port(&lpuart_reg, &sport->port);

2740
	lpuart_disable_clks(sport);
2741

2742 2743 2744 2745 2746 2747
	if (sport->dma_tx_chan)
		dma_release_channel(sport->dma_tx_chan);

	if (sport->dma_rx_chan)
		dma_release_channel(sport->dma_rx_chan);

2748 2749 2750
	return 0;
}

2751
static int __maybe_unused lpuart_suspend(struct device *dev)
2752 2753
{
	struct lpuart_port *sport = dev_get_drvdata(dev);
2754
	unsigned long temp;
2755
	bool irq_wake;
2756

2757
	if (lpuart_is_32(sport)) {
2758
		/* disable Rx/Tx and interrupts */
2759
		temp = lpuart32_read(&sport->port, UARTCTRL);
2760
		temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
2761
		lpuart32_write(&sport->port, temp, UARTCTRL);
2762 2763 2764 2765 2766 2767
	} else {
		/* disable Rx/Tx and interrupts */
		temp = readb(sport->port.membase + UARTCR2);
		temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
		writeb(temp, sport->port.membase + UARTCR2);
	}
2768 2769

	uart_suspend_port(&lpuart_reg, &sport->port);
2770

2771 2772 2773
	/* uart_suspend_port() might set wakeup flag */
	irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));

2774 2775 2776 2777 2778 2779 2780 2781
	if (sport->lpuart_dma_rx_use) {
		/*
		 * EDMA driver during suspend will forcefully release any
		 * non-idle DMA channels. If port wakeup is enabled or if port
		 * is console port or 'no_console_suspend' is set the Rx DMA
		 * cannot resume as as expected, hence gracefully release the
		 * Rx DMA path before suspend and start Rx DMA path on resume.
		 */
2782
		if (irq_wake) {
2783 2784 2785 2786 2787
			del_timer_sync(&sport->lpuart_timer);
			lpuart_dma_rx_free(&sport->port);
		}

		/* Disable Rx DMA to use UART port as wakeup source */
2788 2789 2790 2791 2792 2793 2794 2795
		if (lpuart_is_32(sport)) {
			temp = lpuart32_read(&sport->port, UARTBAUD);
			lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
				       UARTBAUD);
		} else {
			writeb(readb(sport->port.membase + UARTCR5) &
			       ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
		}
2796 2797 2798 2799 2800 2801 2802
	}

	if (sport->lpuart_dma_tx_use) {
		sport->dma_tx_in_progress = false;
		dmaengine_terminate_all(sport->dma_tx_chan);
	}

2803
	if (sport->port.suspended && !irq_wake)
2804
		lpuart_disable_clks(sport);
2805 2806 2807 2808

	return 0;
}

2809
static int __maybe_unused lpuart_resume(struct device *dev)
2810 2811
{
	struct lpuart_port *sport = dev_get_drvdata(dev);
2812
	bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2813

2814
	if (sport->port.suspended && !irq_wake)
2815
		lpuart_enable_clks(sport);
2816

2817 2818 2819 2820
	if (lpuart_is_32(sport))
		lpuart32_setup_watermark_enable(sport);
	else
		lpuart_setup_watermark_enable(sport);
2821

2822
	if (sport->lpuart_dma_rx_use) {
2823
		if (irq_wake) {
2824
			if (!lpuart_start_rx_dma(sport))
2825
				rx_dma_timer_init(sport);
2826
			else
2827 2828 2829 2830
				sport->lpuart_dma_rx_use = false;
		}
	}

2831
	lpuart_tx_dma_startup(sport);
2832

2833 2834
	if (lpuart_is_32(sport))
		lpuart32_configure(sport);
2835

2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854
	uart_resume_port(&lpuart_reg, &sport->port);

	return 0;
}

static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);

static struct platform_driver lpuart_driver = {
	.probe		= lpuart_probe,
	.remove		= lpuart_remove,
	.driver		= {
		.name	= "fsl-lpuart",
		.of_match_table = lpuart_dt_ids,
		.pm	= &lpuart_pm_ops,
	},
};

static int __init lpuart_serial_init(void)
{
2855
	int ret = uart_register_driver(&lpuart_reg);
2856 2857 2858 2859 2860 2861 2862 2863

	if (ret)
		return ret;

	ret = platform_driver_register(&lpuart_driver);
	if (ret)
		uart_unregister_driver(&lpuart_reg);

2864
	return ret;
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
}

static void __exit lpuart_serial_exit(void)
{
	platform_driver_unregister(&lpuart_driver);
	uart_unregister_driver(&lpuart_reg);
}

module_init(lpuart_serial_init);
module_exit(lpuart_serial_exit);

MODULE_DESCRIPTION("Freescale lpuart serial port driver");
MODULE_LICENSE("GPL v2");