xilinx_uartps.c 44.1 KB
Newer Older
1
/*
2
 * Cadence UART driver (found in Xilinx Zynq)
3
 *
S
Soren Brinkmann 已提交
4
 * 2011 - 2014 (C) Xilinx Inc.
5 6 7 8 9 10
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any
 * later version.
11 12 13 14
 *
 * This driver has originally been pushed by Xilinx using a Zynq-branding. This
 * still shows in the naming of this file, the kconfig symbols and some symbols
 * in the code.
15 16
 */

17 18 19 20
#if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif

21
#include <linux/platform_device.h>
22
#include <linux/serial.h>
23
#include <linux/console.h>
24
#include <linux/serial_core.h>
25
#include <linux/slab.h>
26 27
#include <linux/tty.h>
#include <linux/tty_flip.h>
28
#include <linux/clk.h>
29 30 31
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/of.h>
32
#include <linux/module.h>
33

34 35 36 37 38 39
#define CDNS_UART_TTY_NAME	"ttyPS"
#define CDNS_UART_NAME		"xuartps"
#define CDNS_UART_MAJOR		0	/* use dynamic node allocation */
#define CDNS_UART_MINOR		0	/* works best with devtmpfs */
#define CDNS_UART_NR_PORTS	2
#define CDNS_UART_FIFO_SIZE	64	/* FIFO size */
40
#define CDNS_UART_REGISTER_SPACE	0x1000
41

S
Suneel 已提交
42 43 44 45 46 47 48 49 50 51
/* Rx Trigger level */
static int rx_trigger_level = 56;
module_param(rx_trigger_level, uint, S_IRUGO);
MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");

/* Rx Timeout */
static int rx_timeout = 10;
module_param(rx_timeout, uint, S_IRUGO);
MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");

S
Soren Brinkmann 已提交
52
/* Register offsets for the UART. */
53 54 55 56 57 58 59
#define CDNS_UART_CR		0x00  /* Control Register */
#define CDNS_UART_MR		0x04  /* Mode Register */
#define CDNS_UART_IER		0x08  /* Interrupt Enable */
#define CDNS_UART_IDR		0x0C  /* Interrupt Disable */
#define CDNS_UART_IMR		0x10  /* Interrupt Mask */
#define CDNS_UART_ISR		0x14  /* Interrupt Status */
#define CDNS_UART_BAUDGEN	0x18  /* Baud Rate Generator */
60
#define CDNS_UART_RXTOUT	0x1C  /* RX Timeout */
61 62 63 64 65 66 67 68 69 70
#define CDNS_UART_RXWM		0x20  /* RX FIFO Trigger Level */
#define CDNS_UART_MODEMCR	0x24  /* Modem Control */
#define CDNS_UART_MODEMSR	0x28  /* Modem Status */
#define CDNS_UART_SR		0x2C  /* Channel Status */
#define CDNS_UART_FIFO		0x30  /* FIFO */
#define CDNS_UART_BAUDDIV	0x34  /* Baud Rate Divider */
#define CDNS_UART_FLOWDEL	0x38  /* Flow Delay */
#define CDNS_UART_IRRX_PWIDTH	0x3C  /* IR Min Received Pulse Width */
#define CDNS_UART_IRTX_PWIDTH	0x40  /* IR Transmitted pulse Width */
#define CDNS_UART_TXWM		0x44  /* TX FIFO Trigger Level */
71
#define CDNS_UART_RXBS		0x48  /* RX FIFO byte status register */
S
Soren Brinkmann 已提交
72 73

/* Control Register Bit Definitions */
74 75 76 77 78 79 80 81 82
#define CDNS_UART_CR_STOPBRK	0x00000100  /* Stop TX break */
#define CDNS_UART_CR_STARTBRK	0x00000080  /* Set TX break */
#define CDNS_UART_CR_TX_DIS	0x00000020  /* TX disabled. */
#define CDNS_UART_CR_TX_EN	0x00000010  /* TX enabled */
#define CDNS_UART_CR_RX_DIS	0x00000008  /* RX disabled. */
#define CDNS_UART_CR_RX_EN	0x00000004  /* RX enabled */
#define CDNS_UART_CR_TXRST	0x00000002  /* TX logic reset */
#define CDNS_UART_CR_RXRST	0x00000001  /* RX logic reset */
#define CDNS_UART_CR_RST_TO	0x00000040  /* Restart Timeout Counter */
83 84 85
#define CDNS_UART_RXBS_PARITY    0x00000001 /* Parity error status */
#define CDNS_UART_RXBS_FRAMING   0x00000002 /* Framing error status */
#define CDNS_UART_RXBS_BRK       0x00000004 /* Overrun error status */
86

S
Soren Brinkmann 已提交
87 88
/*
 * Mode Register:
89 90 91 92
 * The mode register (MR) defines the mode of transfer as well as the data
 * format. If this register is modified during transmission or reception,
 * data validity cannot be guaranteed.
 */
93 94 95
#define CDNS_UART_MR_CLKSEL		0x00000001  /* Pre-scalar selection */
#define CDNS_UART_MR_CHMODE_L_LOOP	0x00000200  /* Local loop back mode */
#define CDNS_UART_MR_CHMODE_NORM	0x00000000  /* Normal mode */
96

97 98
#define CDNS_UART_MR_STOPMODE_2_BIT	0x00000080  /* 2 stop bits */
#define CDNS_UART_MR_STOPMODE_1_BIT	0x00000000  /* 1 stop bit */
99

100 101 102 103 104
#define CDNS_UART_MR_PARITY_NONE	0x00000020  /* No parity mode */
#define CDNS_UART_MR_PARITY_MARK	0x00000018  /* Mark parity mode */
#define CDNS_UART_MR_PARITY_SPACE	0x00000010  /* Space parity mode */
#define CDNS_UART_MR_PARITY_ODD		0x00000008  /* Odd parity mode */
#define CDNS_UART_MR_PARITY_EVEN	0x00000000  /* Even parity mode */
105

106 107 108
#define CDNS_UART_MR_CHARLEN_6_BIT	0x00000006  /* 6 bits data */
#define CDNS_UART_MR_CHARLEN_7_BIT	0x00000004  /* 7 bits data */
#define CDNS_UART_MR_CHARLEN_8_BIT	0x00000000  /* 8 bits data */
109

S
Soren Brinkmann 已提交
110 111
/*
 * Interrupt Registers:
112 113 114 115 116 117 118 119 120
 * Interrupt control logic uses the interrupt enable register (IER) and the
 * interrupt disable register (IDR) to set the value of the bits in the
 * interrupt mask register (IMR). The IMR determines whether to pass an
 * interrupt to the interrupt status register (ISR).
 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
 * Reading either IER or IDR returns 0x00.
 * All four registers have the same bit definitions.
 */
121 122 123 124 125 126 127 128 129 130 131
#define CDNS_UART_IXR_TOUT	0x00000100 /* RX Timeout error interrupt */
#define CDNS_UART_IXR_PARITY	0x00000080 /* Parity error interrupt */
#define CDNS_UART_IXR_FRAMING	0x00000040 /* Framing error interrupt */
#define CDNS_UART_IXR_OVERRUN	0x00000020 /* Overrun error interrupt */
#define CDNS_UART_IXR_TXFULL	0x00000010 /* TX FIFO Full interrupt */
#define CDNS_UART_IXR_TXEMPTY	0x00000008 /* TX FIFO empty interrupt */
#define CDNS_UART_ISR_RXEMPTY	0x00000002 /* RX FIFO empty interrupt */
#define CDNS_UART_IXR_RXTRIG	0x00000001 /* RX FIFO trigger interrupt */
#define CDNS_UART_IXR_RXFULL	0x00000004 /* RX FIFO full interrupt. */
#define CDNS_UART_IXR_RXEMPTY	0x00000002 /* RX FIFO empty interrupt. */
#define CDNS_UART_IXR_MASK	0x00001FFF /* Valid bit mask */
132

133 134 135 136
#define CDNS_UART_RX_IRQS	(CDNS_UART_IXR_PARITY | CDNS_UART_IXR_FRAMING | \
				 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_RXTRIG | \
				 CDNS_UART_IXR_TOUT)

137
/* Goes in read_status_mask for break detection as the HW doesn't do it*/
138
#define CDNS_UART_IXR_BRK	0x00002000
139

140
#define CDNS_UART_RXBS_SUPPORT BIT(1)
141 142 143 144 145 146 147 148 149
/*
 * Modem Control register:
 * The read/write Modem Control register controls the interface with the modem
 * or data set, or a peripheral device emulating a modem.
 */
#define CDNS_UART_MODEMCR_FCM	0x00000020 /* Automatic flow control mode */
#define CDNS_UART_MODEMCR_RTS	0x00000002 /* Request to send output control */
#define CDNS_UART_MODEMCR_DTR	0x00000001 /* Data Terminal Ready */

S
Soren Brinkmann 已提交
150 151
/*
 * Channel Status Register:
152 153 154 155
 * The channel status register (CSR) is provided to enable the control logic
 * to monitor the status of bits in the channel interrupt status register,
 * even if these are masked out by the interrupt mask register.
 */
156 157 158 159
#define CDNS_UART_SR_RXEMPTY	0x00000002 /* RX FIFO empty */
#define CDNS_UART_SR_TXEMPTY	0x00000008 /* TX FIFO empty */
#define CDNS_UART_SR_TXFULL	0x00000010 /* TX FIFO full */
#define CDNS_UART_SR_RXTRIG	0x00000001 /* Rx Trigger */
160

161
/* baud dividers min/max values */
162 163 164
#define CDNS_UART_BDIV_MIN	4
#define CDNS_UART_BDIV_MAX	255
#define CDNS_UART_CD_MAX	65535
165

166
/**
167
 * struct cdns_uart - device data
168
 * @port:		Pointer to the UART port
169 170
 * @uartclk:		Reference clock
 * @pclk:		APB clock
171 172
 * @baud:		Current baud rate
 * @clk_rate_change_nb:	Notifier block for clock changes
173
 */
174
struct cdns_uart {
175
	struct uart_port	*port;
176 177
	struct clk		*uartclk;
	struct clk		*pclk;
178 179
	unsigned int		baud;
	struct notifier_block	clk_rate_change_nb;
180 181 182 183
	u32			quirks;
};
struct cdns_platform_data {
	u32 quirks;
184
};
185 186
#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
		clk_rate_change_nb);
187

188
static void cdns_uart_handle_rx(struct uart_port *port, unsigned int isrstatus)
189
{
190 191 192 193 194 195 196
	struct cdns_uart *cdns_uart = port->private_data;
	bool is_rxbs_support;
	unsigned int rxbs_status = 0;
	unsigned int status_mask;

	is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;

197 198 199 200 201
	/*
	 * There is no hardware break detection, so we interpret framing
	 * error with all-zeros data as a break sequence. Most of the time,
	 * there's another non-zero byte at the end of the sequence.
	 */
202
	if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
203
		while (!(readl(port->membase + CDNS_UART_SR) &
204
					CDNS_UART_SR_RXEMPTY)) {
205
			if (!readl(port->membase + CDNS_UART_FIFO)) {
206 207
				port->read_status_mask |= CDNS_UART_IXR_BRK;
				isrstatus &= ~CDNS_UART_IXR_FRAMING;
208 209
			}
		}
210
		writel(CDNS_UART_IXR_FRAMING, port->membase + CDNS_UART_ISR);
211 212
	}

213
	/* drop byte with parity error if IGNPAR specified */
214 215
	if (isrstatus & port->ignore_status_mask & CDNS_UART_IXR_PARITY)
		isrstatus &= ~(CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT);
216 217 218

	isrstatus &= port->read_status_mask;
	isrstatus &= ~port->ignore_status_mask;
219 220
	status_mask = port->read_status_mask;
	status_mask &= ~port->ignore_status_mask;
221

222 223 224
	if (!(isrstatus & (CDNS_UART_IXR_TOUT | CDNS_UART_IXR_RXTRIG)))
		return;

225
	while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)) {
226 227 228
		u32 data;
		char status = TTY_NORMAL;

229 230 231
		if (is_rxbs_support)
			rxbs_status = readl(port->membase + CDNS_UART_RXBS);

232
		data = readl(port->membase + CDNS_UART_FIFO);
233 234 235 236 237 238 239 240

		/* Non-NULL byte after BREAK is garbage (99%) */
		if (data && (port->read_status_mask & CDNS_UART_IXR_BRK)) {
			port->read_status_mask &= ~CDNS_UART_IXR_BRK;
			port->icount.brk++;
			if (uart_handle_break(port))
				continue;
		}
241 242 243 244 245 246
		if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
			port->icount.brk++;
			status = TTY_BREAK;
			if (uart_handle_break(port))
				continue;
		}
247

248 249
		if (uart_handle_sysrq_char(port, data))
			continue;
250

251
		port->icount.rx++;
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
		if (is_rxbs_support) {
			if ((rxbs_status & CDNS_UART_RXBS_PARITY)
				&& (status_mask & CDNS_UART_IXR_PARITY)) {
				port->icount.parity++;
				status = TTY_PARITY;
			}
			if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
				&& (status_mask & CDNS_UART_IXR_PARITY)) {
				port->icount.frame++;
				status = TTY_FRAME;
			}
		} else {
			if (isrstatus & CDNS_UART_IXR_PARITY) {
				port->icount.parity++;
				status = TTY_PARITY;
			}
			if (isrstatus & CDNS_UART_IXR_FRAMING) {
				port->icount.frame++;
				status = TTY_FRAME;
			}
273
		}
274 275
		if (isrstatus & CDNS_UART_IXR_OVERRUN)
			port->icount.overrun++;
276 277 278

		uart_insert_char(port, isrstatus, CDNS_UART_IXR_OVERRUN,
				 data, status);
279
	}
280
	tty_flip_buffer_push(&port->state->port);
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
static void cdns_uart_handle_tx(struct uart_port *port)
{
	unsigned int numbytes;

	if (uart_circ_empty(&port->state->xmit)) {
		writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
		return;
	}

	numbytes = port->fifosize;
	while (numbytes && !uart_circ_empty(&port->state->xmit) &&
	       !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)) {
		/*
		 * Get the data from the UART circular buffer
		 * and write it to the cdns_uart's TX_FIFO
		 * register.
		 */
		writel(port->state->xmit.buf[port->state->xmit.tail],
			port->membase + CDNS_UART_FIFO);
		port->icount.tx++;

		/*
		 * Adjust the tail of the UART buffer and wrap
		 * the buffer if it reaches limit.
		 */
		port->state->xmit.tail =
			(port->state->xmit.tail + 1) & (UART_XMIT_SIZE - 1);

		numbytes--;
	}

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

318 319 320 321 322 323 324 325 326 327 328
/**
 * cdns_uart_isr - Interrupt handler
 * @irq: Irq number
 * @dev_id: Id of the port
 *
 * Return: IRQHANDLED
 */
static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
{
	struct uart_port *port = (struct uart_port *)dev_id;
	unsigned long flags;
329
	unsigned int isrstatus;
330 331 332 333 334 335

	spin_lock_irqsave(&port->lock, flags);

	/* Read the interrupt status register to determine which
	 * interrupt(s) is/are active.
	 */
336
	isrstatus = readl(port->membase + CDNS_UART_ISR);
337

338 339
	if (isrstatus & CDNS_UART_RX_IRQS)
		cdns_uart_handle_rx(port, isrstatus);
340

341 342
	if ((isrstatus & CDNS_UART_IXR_TXEMPTY) == CDNS_UART_IXR_TXEMPTY)
		cdns_uart_handle_tx(port);
343

344
	writel(isrstatus, port->membase + CDNS_UART_ISR);
345 346 347 348 349 350 351 352

	/* be sure to release the lock and tty before leaving */
	spin_unlock_irqrestore(&port->lock, flags);

	return IRQ_HANDLED;
}

/**
353
 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
354 355 356 357 358
 * @clk: UART module input clock
 * @baud: Desired baud rate
 * @rbdiv: BDIV value (return value)
 * @rcd: CD value (return value)
 * @div8: Value for clk_sel bit in mod (return value)
359
 * Return: baud rate, requested baud when possible, or actual baud when there
360 361 362 363 364 365 366 367 368 369 370 371
 *	was too much error, zero if no valid divisors are found.
 *
 * Formula to obtain baud rate is
 *	baud_tx/rx rate = clk/CD * (BDIV + 1)
 *	input_clk = (Uart User Defined Clock or Apb Clock)
 *		depends on UCLKEN in MR Reg
 *	clk = input_clk or input_clk/8;
 *		depends on CLKS in MR reg
 *	CD and BDIV depends on values in
 *			baud rate generate register
 *			baud rate clock divisor register
 */
372 373
static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
		unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
374
{
375 376 377
	u32 cd, bdiv;
	unsigned int calc_baud;
	unsigned int bestbaud = 0;
378
	unsigned int bauderror;
379
	unsigned int besterror = ~0;
380

381
	if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
382 383 384 385 386
		*div8 = 1;
		clk /= 8;
	} else {
		*div8 = 0;
	}
387

388
	for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
389
		cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
390
		if (cd < 1 || cd > CDNS_UART_CD_MAX)
391 392
			continue;

393
		calc_baud = clk / (cd * (bdiv + 1));
394 395 396 397 398 399

		if (baud > calc_baud)
			bauderror = baud - calc_baud;
		else
			bauderror = calc_baud - baud;

400 401 402 403 404
		if (besterror > bauderror) {
			*rbdiv = bdiv;
			*rcd = cd;
			bestbaud = calc_baud;
			besterror = bauderror;
405 406
		}
	}
407 408 409 410 411 412
	/* use the values when percent error is acceptable */
	if (((besterror * 100) / baud) < 3)
		bestbaud = baud;

	return bestbaud;
}
413

414
/**
415
 * cdns_uart_set_baud_rate - Calculate and set the baud rate
416 417
 * @port: Handle to the uart port structure
 * @baud: Baud rate to set
418
 * Return: baud rate, requested baud when possible, or actual baud when there
419 420
 *	   was too much error, zero if no valid divisors are found.
 */
421
static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
422 423 424
		unsigned int baud)
{
	unsigned int calc_baud;
425
	u32 cd = 0, bdiv = 0;
426 427
	u32 mreg;
	int div8;
428
	struct cdns_uart *cdns_uart = port->private_data;
429

430
	calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
431 432 433
			&div8);

	/* Write new divisors to hardware */
434
	mreg = readl(port->membase + CDNS_UART_MR);
435
	if (div8)
436
		mreg |= CDNS_UART_MR_CLKSEL;
437
	else
438
		mreg &= ~CDNS_UART_MR_CLKSEL;
439 440 441
	writel(mreg, port->membase + CDNS_UART_MR);
	writel(cd, port->membase + CDNS_UART_BAUDGEN);
	writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
442
	cdns_uart->baud = baud;
443 444 445 446

	return calc_baud;
}

447
#ifdef CONFIG_COMMON_CLK
448
/**
449
 * cdns_uart_clk_notitifer_cb - Clock notifier callback
450 451 452
 * @nb:		Notifier block
 * @event:	Notify event
 * @data:	Notifier data
S
Soren Brinkmann 已提交
453
 * Return:	NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
454
 */
455
static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
456 457 458 459 460 461 462
		unsigned long event, void *data)
{
	u32 ctrl_reg;
	struct uart_port *port;
	int locked = 0;
	struct clk_notifier_data *ndata = data;
	unsigned long flags = 0;
463
	struct cdns_uart *cdns_uart = to_cdns_uart(nb);
464

465
	port = cdns_uart->port;
466 467 468 469 470 471
	if (port->suspended)
		return NOTIFY_OK;

	switch (event) {
	case PRE_RATE_CHANGE:
	{
S
Soren Brinkmann 已提交
472
		u32 bdiv, cd;
473 474 475 476 477 478
		int div8;

		/*
		 * Find out if current baud-rate can be achieved with new clock
		 * frequency.
		 */
479
		if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
480 481
					&bdiv, &cd, &div8)) {
			dev_warn(port->dev, "clock rate change rejected\n");
482
			return NOTIFY_BAD;
483
		}
484

485
		spin_lock_irqsave(&cdns_uart->port->lock, flags);
486 487

		/* Disable the TX and RX to set baud rate */
488
		ctrl_reg = readl(port->membase + CDNS_UART_CR);
489
		ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
490
		writel(ctrl_reg, port->membase + CDNS_UART_CR);
491

492
		spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
493 494 495 496 497 498 499 500 501

		return NOTIFY_OK;
	}
	case POST_RATE_CHANGE:
		/*
		 * Set clk dividers to generate correct baud with new clock
		 * frequency.
		 */

502
		spin_lock_irqsave(&cdns_uart->port->lock, flags);
503 504 505 506

		locked = 1;
		port->uartclk = ndata->new_rate;

507 508
		cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
				cdns_uart->baud);
509 510 511
		/* fall through */
	case ABORT_RATE_CHANGE:
		if (!locked)
512
			spin_lock_irqsave(&cdns_uart->port->lock, flags);
513 514

		/* Set TX/RX Reset */
515
		ctrl_reg = readl(port->membase + CDNS_UART_CR);
516
		ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
517
		writel(ctrl_reg, port->membase + CDNS_UART_CR);
518

519
		while (readl(port->membase + CDNS_UART_CR) &
520
				(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
521 522 523 524 525 526 527
			cpu_relax();

		/*
		 * Clear the RX disable and TX disable bits and then set the TX
		 * enable bit and RX enable bit to enable the transmitter and
		 * receiver.
		 */
528 529
		writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
		ctrl_reg = readl(port->membase + CDNS_UART_CR);
530 531
		ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
		ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
532
		writel(ctrl_reg, port->membase + CDNS_UART_CR);
533

534
		spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
535 536 537 538 539 540

		return NOTIFY_OK;
	default:
		return NOTIFY_DONE;
	}
}
541
#endif
542

543
/**
544
 * cdns_uart_start_tx -  Start transmitting bytes
545
 * @port: Handle to the uart port structure
546
 */
547
static void cdns_uart_start_tx(struct uart_port *port)
548
{
549
	unsigned int status;
550

551
	if (uart_tx_stopped(port))
552 553
		return;

554 555
	/*
	 * Set the TX enable bit and clear the TX disable bit to enable the
556 557
	 * transmitter.
	 */
558
	status = readl(port->membase + CDNS_UART_CR);
559 560
	status &= ~CDNS_UART_CR_TX_DIS;
	status |= CDNS_UART_CR_TX_EN;
561
	writel(status, port->membase + CDNS_UART_CR);
562

563 564 565
	if (uart_circ_empty(&port->state->xmit))
		return;

566
	cdns_uart_handle_tx(port);
567

568
	writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
569
	/* Enable the TX Empty interrupt */
570
	writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
571 572 573
}

/**
574
 * cdns_uart_stop_tx - Stop TX
575
 * @port: Handle to the uart port structure
576
 */
577
static void cdns_uart_stop_tx(struct uart_port *port)
578 579 580
{
	unsigned int regval;

581
	regval = readl(port->membase + CDNS_UART_CR);
582
	regval |= CDNS_UART_CR_TX_DIS;
583
	/* Disable the transmitter */
584
	writel(regval, port->membase + CDNS_UART_CR);
585 586 587
}

/**
588
 * cdns_uart_stop_rx - Stop RX
589
 * @port: Handle to the uart port structure
590
 */
591
static void cdns_uart_stop_rx(struct uart_port *port)
592 593 594
{
	unsigned int regval;

595
	/* Disable RX IRQs */
596
	writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
597 598

	/* Disable the receiver */
599
	regval = readl(port->membase + CDNS_UART_CR);
600
	regval |= CDNS_UART_CR_RX_DIS;
601
	writel(regval, port->membase + CDNS_UART_CR);
602 603 604
}

/**
605
 * cdns_uart_tx_empty -  Check whether TX is empty
606 607
 * @port: Handle to the uart port structure
 *
608 609
 * Return: TIOCSER_TEMT on success, 0 otherwise
 */
610
static unsigned int cdns_uart_tx_empty(struct uart_port *port)
611 612 613
{
	unsigned int status;

614
	status = readl(port->membase + CDNS_UART_SR) &
615
				CDNS_UART_SR_TXEMPTY;
616 617 618 619
	return status ? TIOCSER_TEMT : 0;
}

/**
620
 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
621 622 623
 *			transmitting char breaks
 * @port: Handle to the uart port structure
 * @ctl: Value based on which start or stop decision is taken
624
 */
625
static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
626 627 628 629 630 631
{
	unsigned int status;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);

632
	status = readl(port->membase + CDNS_UART_CR);
633 634

	if (ctl == -1)
635
		writel(CDNS_UART_CR_STARTBRK | status,
636
				port->membase + CDNS_UART_CR);
637
	else {
638
		if ((status & CDNS_UART_CR_STOPBRK) == 0)
639
			writel(CDNS_UART_CR_STOPBRK | status,
640
					port->membase + CDNS_UART_CR);
641 642 643 644 645
	}
	spin_unlock_irqrestore(&port->lock, flags);
}

/**
646
 * cdns_uart_set_termios - termios operations, handling data length, parity,
647 648 649 650
 *				stop bits, flow control, baud rate
 * @port: Handle to the uart port structure
 * @termios: Handle to the input termios structure
 * @old: Values of the previously saved termios structure
651
 */
652
static void cdns_uart_set_termios(struct uart_port *port,
653 654 655
				struct ktermios *termios, struct ktermios *old)
{
	unsigned int cval = 0;
656
	unsigned int baud, minbaud, maxbaud;
657 658 659 660 661
	unsigned long flags;
	unsigned int ctrl_reg, mode_reg;

	spin_lock_irqsave(&port->lock, flags);

662
	/* Wait for the transmit FIFO to empty before making changes */
663
	if (!(readl(port->membase + CDNS_UART_CR) &
664
				CDNS_UART_CR_TX_DIS)) {
665
		while (!(readl(port->membase + CDNS_UART_SR) &
666 667 668
				CDNS_UART_SR_TXEMPTY)) {
			cpu_relax();
		}
669 670 671
	}

	/* Disable the TX and RX to set baud rate */
672
	ctrl_reg = readl(port->membase + CDNS_UART_CR);
673
	ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
674
	writel(ctrl_reg, port->membase + CDNS_UART_CR);
675

676 677 678 679 680
	/*
	 * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
	 * min and max baud should be calculated here based on port->uartclk.
	 * this way we get a valid baud and can safely call set_baud()
	 */
681 682 683
	minbaud = port->uartclk /
			((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
	maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
684
	baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
685
	baud = cdns_uart_set_baud_rate(port, baud);
686 687 688
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);

S
Soren Brinkmann 已提交
689
	/* Update the per-port timeout. */
690 691 692
	uart_update_timeout(port, termios->c_cflag, baud);

	/* Set TX/RX Reset */
693
	ctrl_reg = readl(port->membase + CDNS_UART_CR);
694
	ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
695
	writel(ctrl_reg, port->membase + CDNS_UART_CR);
696

S
Soren Brinkmann 已提交
697 698
	/*
	 * Clear the RX disable and TX disable bits and then set the TX enable
699 700
	 * bit and RX enable bit to enable the transmitter and receiver.
	 */
701
	ctrl_reg = readl(port->membase + CDNS_UART_CR);
702 703
	ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
	ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
704
	writel(ctrl_reg, port->membase + CDNS_UART_CR);
705

706
	writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
707

708 709
	port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
			CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
710 711 712
	port->ignore_status_mask = 0;

	if (termios->c_iflag & INPCK)
713 714
		port->read_status_mask |= CDNS_UART_IXR_PARITY |
		CDNS_UART_IXR_FRAMING;
715 716

	if (termios->c_iflag & IGNPAR)
717 718
		port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
			CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
719 720 721

	/* ignore all characters if CREAD is not set */
	if ((termios->c_cflag & CREAD) == 0)
722 723 724
		port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
			CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
			CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
725

726
	mode_reg = readl(port->membase + CDNS_UART_MR);
727 728 729 730

	/* Handling Data Size */
	switch (termios->c_cflag & CSIZE) {
	case CS6:
731
		cval |= CDNS_UART_MR_CHARLEN_6_BIT;
732 733
		break;
	case CS7:
734
		cval |= CDNS_UART_MR_CHARLEN_7_BIT;
735 736 737
		break;
	default:
	case CS8:
738
		cval |= CDNS_UART_MR_CHARLEN_8_BIT;
739 740 741 742 743 744 745
		termios->c_cflag &= ~CSIZE;
		termios->c_cflag |= CS8;
		break;
	}

	/* Handling Parity and Stop Bits length */
	if (termios->c_cflag & CSTOPB)
746
		cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
747
	else
748
		cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
749 750 751 752 753

	if (termios->c_cflag & PARENB) {
		/* Mark or Space parity */
		if (termios->c_cflag & CMSPAR) {
			if (termios->c_cflag & PARODD)
754
				cval |= CDNS_UART_MR_PARITY_MARK;
755
			else
756
				cval |= CDNS_UART_MR_PARITY_SPACE;
757 758
		} else {
			if (termios->c_cflag & PARODD)
759
				cval |= CDNS_UART_MR_PARITY_ODD;
760
			else
761
				cval |= CDNS_UART_MR_PARITY_EVEN;
762 763
		}
	} else {
764
		cval |= CDNS_UART_MR_PARITY_NONE;
765 766
	}
	cval |= mode_reg & 1;
767
	writel(cval, port->membase + CDNS_UART_MR);
768 769 770 771 772

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

/**
773
 * cdns_uart_startup - Called when an application opens a cdns_uart port
774 775
 * @port: Handle to the uart port structure
 *
S
Soren Brinkmann 已提交
776
 * Return: 0 on success, negative errno otherwise
777
 */
778
static int cdns_uart_startup(struct uart_port *port)
779
{
780 781
	struct cdns_uart *cdns_uart = port->private_data;
	bool is_brk_support;
782
	int ret;
783
	unsigned long flags;
784
	unsigned int status = 0;
785

786 787
	is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;

788 789
	spin_lock_irqsave(&port->lock, flags);

790
	/* Disable the TX and RX */
791
	writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
792
			port->membase + CDNS_UART_CR);
793 794 795 796

	/* Set the Control Register with TX/RX Enable, TX/RX Reset,
	 * no break chars.
	 */
797
	writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
798
			port->membase + CDNS_UART_CR);
799

800 801 802
	/*
	 * Clear the RX disable bit and then set the RX enable bit to enable
	 * the receiver.
803
	 */
804
	status = readl(port->membase + CDNS_UART_CR);
805 806
	status &= CDNS_UART_CR_RX_DIS;
	status |= CDNS_UART_CR_RX_EN;
807
	writel(status, port->membase + CDNS_UART_CR);
808 809 810 811

	/* Set the Mode Register with normal mode,8 data bits,1 stop bit,
	 * no parity.
	 */
812
	writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
813
		| CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
814
		port->membase + CDNS_UART_MR);
815

S
Suneel 已提交
816 817 818 819
	/*
	 * Set the RX FIFO Trigger level to use most of the FIFO, but it
	 * can be tuned with a module parameter
	 */
820
	writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
821

S
Suneel 已提交
822 823 824 825
	/*
	 * Receive Timeout register is enabled but it
	 * can be tuned with a module parameter
	 */
826
	writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
827

828
	/* Clear out any pending interrupts before enabling them */
829 830
	writel(readl(port->membase + CDNS_UART_ISR),
			port->membase + CDNS_UART_ISR);
831

832 833 834 835 836 837 838 839 840
	spin_unlock_irqrestore(&port->lock, flags);

	ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
	if (ret) {
		dev_err(port->dev, "request_irq '%d' failed with %d\n",
			port->irq, ret);
		return ret;
	}

841
	/* Set the Interrupt Registers with desired interrupts */
842 843 844 845 846
	if (is_brk_support)
		writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
					port->membase + CDNS_UART_IER);
	else
		writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
847

848
	return 0;
849 850 851
}

/**
852
 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
853
 * @port: Handle to the uart port structure
854
 */
855
static void cdns_uart_shutdown(struct uart_port *port)
856 857
{
	int status;
858 859 860
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
861 862

	/* Disable interrupts */
863 864 865
	status = readl(port->membase + CDNS_UART_IMR);
	writel(status, port->membase + CDNS_UART_IDR);
	writel(0xffffffff, port->membase + CDNS_UART_ISR);
866 867

	/* Disable the TX and RX */
868
	writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
869
			port->membase + CDNS_UART_CR);
870 871 872

	spin_unlock_irqrestore(&port->lock, flags);

873 874 875 876
	free_irq(port->irq, port);
}

/**
877
 * cdns_uart_type - Set UART type to cdns_uart port
878 879
 * @port: Handle to the uart port structure
 *
880 881
 * Return: string on success, NULL otherwise
 */
882
static const char *cdns_uart_type(struct uart_port *port)
883
{
884
	return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
885 886 887
}

/**
888
 * cdns_uart_verify_port - Verify the port params
889 890 891
 * @port: Handle to the uart port structure
 * @ser: Handle to the structure whose members are compared
 *
S
Soren Brinkmann 已提交
892
 * Return: 0 on success, negative errno otherwise.
893
 */
894
static int cdns_uart_verify_port(struct uart_port *port,
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
					struct serial_struct *ser)
{
	if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
		return -EINVAL;
	if (port->irq != ser->irq)
		return -EINVAL;
	if (ser->io_type != UPIO_MEM)
		return -EINVAL;
	if (port->iobase != ser->port)
		return -EINVAL;
	if (ser->hub6 != 0)
		return -EINVAL;
	return 0;
}

/**
911 912
 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
 *				called when the driver adds a cdns_uart port via
913 914 915
 *				uart_add_one_port()
 * @port: Handle to the uart port structure
 *
S
Soren Brinkmann 已提交
916
 * Return: 0 on success, negative errno otherwise.
917
 */
918
static int cdns_uart_request_port(struct uart_port *port)
919
{
920 921
	if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
					 CDNS_UART_NAME)) {
922 923 924
		return -ENOMEM;
	}

925
	port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
926 927
	if (!port->membase) {
		dev_err(port->dev, "Unable to map registers\n");
928
		release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
929 930 931 932 933 934
		return -ENOMEM;
	}
	return 0;
}

/**
935
 * cdns_uart_release_port - Release UART port
936
 * @port: Handle to the uart port structure
S
Soren Brinkmann 已提交
937
 *
938 939
 * Release the memory region attached to a cdns_uart port. Called when the
 * driver removes a cdns_uart port via uart_remove_one_port().
940
 */
941
static void cdns_uart_release_port(struct uart_port *port)
942
{
943
	release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
944 945 946 947 948
	iounmap(port->membase);
	port->membase = NULL;
}

/**
949
 * cdns_uart_config_port - Configure UART port
950 951
 * @port: Handle to the uart port structure
 * @flags: If any
952
 */
953
static void cdns_uart_config_port(struct uart_port *port, int flags)
954
{
955
	if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
956 957 958 959
		port->type = PORT_XUARTPS;
}

/**
960
 * cdns_uart_get_mctrl - Get the modem control state
961 962
 * @port: Handle to the uart port structure
 *
963 964
 * Return: the modem control state
 */
965
static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
966 967 968 969
{
	return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
}

970
static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
971
{
972 973
	u32 val;

974
	val = readl(port->membase + CDNS_UART_MODEMCR);
975 976 977 978 979 980 981 982

	val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);

	if (mctrl & TIOCM_RTS)
		val |= CDNS_UART_MODEMCR_RTS;
	if (mctrl & TIOCM_DTR)
		val |= CDNS_UART_MODEMCR_DTR;

983
	writel(val, port->membase + CDNS_UART_MODEMCR);
984 985
}

986
#ifdef CONFIG_CONSOLE_POLL
987
static int cdns_uart_poll_get_char(struct uart_port *port)
988 989
{
	int c;
990
	unsigned long flags;
991

992
	spin_lock_irqsave(&port->lock, flags);
993 994

	/* Check if FIFO is empty */
995
	if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
996 997
		c = NO_POLL_CHAR;
	else /* Read a character */
998
		c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
999

1000
	spin_unlock_irqrestore(&port->lock, flags);
1001 1002 1003 1004

	return c;
}

1005
static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
1006
{
1007
	unsigned long flags;
1008

1009
	spin_lock_irqsave(&port->lock, flags);
1010 1011

	/* Wait until FIFO is empty */
1012
	while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1013 1014 1015
		cpu_relax();

	/* Write a character */
1016
	writel(c, port->membase + CDNS_UART_FIFO);
1017 1018

	/* Wait until FIFO is empty */
1019
	while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1020 1021
		cpu_relax();

1022
	spin_unlock_irqrestore(&port->lock, flags);
1023 1024 1025 1026 1027

	return;
}
#endif

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
static void cdns_uart_pm(struct uart_port *port, unsigned int state,
		   unsigned int oldstate)
{
	struct cdns_uart *cdns_uart = port->private_data;

	switch (state) {
	case UART_PM_STATE_OFF:
		clk_disable(cdns_uart->uartclk);
		clk_disable(cdns_uart->pclk);
		break;
	default:
		clk_enable(cdns_uart->pclk);
		clk_enable(cdns_uart->uartclk);
		break;
	}
}

1045
static const struct uart_ops cdns_uart_ops = {
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	.set_mctrl	= cdns_uart_set_mctrl,
	.get_mctrl	= cdns_uart_get_mctrl,
	.start_tx	= cdns_uart_start_tx,
	.stop_tx	= cdns_uart_stop_tx,
	.stop_rx	= cdns_uart_stop_rx,
	.tx_empty	= cdns_uart_tx_empty,
	.break_ctl	= cdns_uart_break_ctl,
	.set_termios	= cdns_uart_set_termios,
	.startup	= cdns_uart_startup,
	.shutdown	= cdns_uart_shutdown,
1056
	.pm		= cdns_uart_pm,
1057 1058 1059 1060 1061
	.type		= cdns_uart_type,
	.verify_port	= cdns_uart_verify_port,
	.request_port	= cdns_uart_request_port,
	.release_port	= cdns_uart_release_port,
	.config_port	= cdns_uart_config_port,
1062
#ifdef CONFIG_CONSOLE_POLL
1063 1064
	.poll_get_char	= cdns_uart_poll_get_char,
	.poll_put_char	= cdns_uart_poll_put_char,
1065
#endif
1066 1067
};

1068
static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
1069 1070

/**
1071
 * cdns_uart_get_port - Configure the port from platform device resource info
1072 1073
 * @id: Port id
 *
1074 1075
 * Return: a pointer to a uart_port or NULL for failure
 */
1076
static struct uart_port *cdns_uart_get_port(int id)
1077 1078 1079
{
	struct uart_port *port;

1080
	/* Try the given port id if failed use default method */
1081
	if (cdns_uart_port[id].mapbase != 0) {
1082
		/* Find the next unused port */
1083 1084
		for (id = 0; id < CDNS_UART_NR_PORTS; id++)
			if (cdns_uart_port[id].mapbase == 0)
1085 1086
				break;
	}
1087

1088
	if (id >= CDNS_UART_NR_PORTS)
1089 1090
		return NULL;

1091
	port = &cdns_uart_port[id];
1092 1093 1094 1095 1096 1097 1098 1099

	/* At this point, we've got an empty uart_port struct, initialize it */
	spin_lock_init(&port->lock);
	port->membase	= NULL;
	port->irq	= 0;
	port->type	= PORT_UNKNOWN;
	port->iotype	= UPIO_MEM32;
	port->flags	= UPF_BOOT_AUTOCONF;
1100 1101
	port->ops	= &cdns_uart_ops;
	port->fifosize	= CDNS_UART_FIFO_SIZE;
1102 1103 1104 1105 1106 1107 1108
	port->line	= id;
	port->dev	= NULL;
	return port;
}

#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
/**
1109
 * cdns_uart_console_wait_tx - Wait for the TX to be full
1110
 * @port: Handle to the uart port structure
1111
 */
1112
static void cdns_uart_console_wait_tx(struct uart_port *port)
1113
{
1114
	while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
1115 1116 1117 1118
		barrier();
}

/**
1119
 * cdns_uart_console_putchar - write the character to the FIFO buffer
1120 1121
 * @port: Handle to the uart port structure
 * @ch: Character to be written
1122
 */
1123
static void cdns_uart_console_putchar(struct uart_port *port, int ch)
1124
{
1125
	cdns_uart_console_wait_tx(port);
1126
	writel(ch, port->membase + CDNS_UART_FIFO);
1127 1128
}

1129 1130
static void __init cdns_early_write(struct console *con, const char *s,
				    unsigned n)
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
{
	struct earlycon_device *dev = con->data;

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

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

	device->con->write = cdns_early_write;

	return 0;
}
1147 1148 1149
OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
1150

1151
/**
1152
 * cdns_uart_console_write - perform write operation
1153
 * @co: Console handle
1154 1155
 * @s: Pointer to character array
 * @count: No of characters
1156
 */
1157
static void cdns_uart_console_write(struct console *co, const char *s,
1158 1159
				unsigned int count)
{
1160
	struct uart_port *port = &cdns_uart_port[co->index];
1161
	unsigned long flags;
1162
	unsigned int imr, ctrl;
1163 1164
	int locked = 1;

1165 1166 1167
	if (port->sysrq)
		locked = 0;
	else if (oops_in_progress)
1168 1169 1170 1171 1172
		locked = spin_trylock_irqsave(&port->lock, flags);
	else
		spin_lock_irqsave(&port->lock, flags);

	/* save and disable interrupt */
1173 1174
	imr = readl(port->membase + CDNS_UART_IMR);
	writel(imr, port->membase + CDNS_UART_IDR);
1175

1176 1177 1178 1179
	/*
	 * Make sure that the tx part is enabled. Set the TX enable bit and
	 * clear the TX disable bit to enable the transmitter.
	 */
1180
	ctrl = readl(port->membase + CDNS_UART_CR);
1181 1182
	ctrl &= ~CDNS_UART_CR_TX_DIS;
	ctrl |= CDNS_UART_CR_TX_EN;
1183
	writel(ctrl, port->membase + CDNS_UART_CR);
1184

1185 1186
	uart_console_write(port, s, count, cdns_uart_console_putchar);
	cdns_uart_console_wait_tx(port);
1187

1188
	writel(ctrl, port->membase + CDNS_UART_CR);
1189

1190
	/* restore interrupt state */
1191
	writel(imr, port->membase + CDNS_UART_IER);
1192 1193 1194 1195 1196 1197

	if (locked)
		spin_unlock_irqrestore(&port->lock, flags);
}

/**
1198
 * cdns_uart_console_setup - Initialize the uart to default config
1199 1200 1201
 * @co: Console handle
 * @options: Initial settings of uart
 *
S
Soren Brinkmann 已提交
1202
 * Return: 0 on success, negative errno otherwise.
1203
 */
1204
static int __init cdns_uart_console_setup(struct console *co, char *options)
1205
{
1206
	struct uart_port *port = &cdns_uart_port[co->index];
1207 1208 1209 1210 1211
	int baud = 9600;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

1212
	if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
1213 1214
		return -EINVAL;

1215
	if (!port->membase) {
1216 1217
		pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
			 co->index);
1218 1219 1220 1221 1222 1223 1224 1225 1226
		return -ENODEV;
	}

	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);

	return uart_set_options(port, co, baud, parity, bits, flow);
}

1227
static struct uart_driver cdns_uart_uart_driver;
1228

1229 1230 1231
static struct console cdns_uart_console = {
	.name	= CDNS_UART_TTY_NAME,
	.write	= cdns_uart_console_write,
1232
	.device	= uart_console_device,
1233
	.setup	= cdns_uart_console_setup,
1234 1235
	.flags	= CON_PRINTBUFFER,
	.index	= -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
1236
	.data	= &cdns_uart_uart_driver,
1237 1238 1239
};

/**
1240
 * cdns_uart_console_init - Initialization call
1241
 *
S
Soren Brinkmann 已提交
1242
 * Return: 0 on success, negative errno otherwise
1243
 */
1244
static int __init cdns_uart_console_init(void)
1245
{
1246
	register_console(&cdns_uart_console);
1247 1248 1249
	return 0;
}

1250
console_initcall(cdns_uart_console_init);
1251 1252 1253

#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */

1254
static struct uart_driver cdns_uart_uart_driver = {
S
Soren Brinkmann 已提交
1255
	.owner		= THIS_MODULE,
1256 1257 1258 1259 1260
	.driver_name	= CDNS_UART_NAME,
	.dev_name	= CDNS_UART_TTY_NAME,
	.major		= CDNS_UART_MAJOR,
	.minor		= CDNS_UART_MINOR,
	.nr		= CDNS_UART_NR_PORTS,
1261
#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1262
	.cons		= &cdns_uart_console,
1263 1264 1265
#endif
};

1266 1267
#ifdef CONFIG_PM_SLEEP
/**
1268
 * cdns_uart_suspend - suspend event
1269 1270
 * @device: Pointer to the device structure
 *
1271
 * Return: 0
1272
 */
1273
static int cdns_uart_suspend(struct device *device)
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
{
	struct uart_port *port = dev_get_drvdata(device);
	struct tty_struct *tty;
	struct device *tty_dev;
	int may_wake = 0;

	/* Get the tty which could be NULL so don't assume it's valid */
	tty = tty_port_tty_get(&port->state->port);
	if (tty) {
		tty_dev = tty->dev;
		may_wake = device_may_wakeup(tty_dev);
		tty_kref_put(tty);
	}

	/*
	 * Call the API provided in serial_core.c file which handles
	 * the suspend.
	 */
1292
	uart_suspend_port(&cdns_uart_uart_driver, port);
1293
	if (console_suspend_enabled && !may_wake) {
1294
		struct cdns_uart *cdns_uart = port->private_data;
1295

1296 1297
		clk_disable(cdns_uart->uartclk);
		clk_disable(cdns_uart->pclk);
1298 1299 1300 1301 1302
	} else {
		unsigned long flags = 0;

		spin_lock_irqsave(&port->lock, flags);
		/* Empty the receive FIFO 1st before making changes */
1303
		while (!(readl(port->membase + CDNS_UART_SR) &
1304
					CDNS_UART_SR_RXEMPTY))
1305
			readl(port->membase + CDNS_UART_FIFO);
1306
		/* set RX trigger level to 1 */
1307
		writel(1, port->membase + CDNS_UART_RXWM);
1308
		/* disable RX timeout interrups */
1309
		writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
1310 1311 1312 1313 1314 1315 1316
		spin_unlock_irqrestore(&port->lock, flags);
	}

	return 0;
}

/**
1317
 * cdns_uart_resume - Resume after a previous suspend
1318 1319
 * @device: Pointer to the device structure
 *
1320
 * Return: 0
1321
 */
1322
static int cdns_uart_resume(struct device *device)
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
{
	struct uart_port *port = dev_get_drvdata(device);
	unsigned long flags = 0;
	u32 ctrl_reg;
	struct tty_struct *tty;
	struct device *tty_dev;
	int may_wake = 0;

	/* Get the tty which could be NULL so don't assume it's valid */
	tty = tty_port_tty_get(&port->state->port);
	if (tty) {
		tty_dev = tty->dev;
		may_wake = device_may_wakeup(tty_dev);
		tty_kref_put(tty);
	}

	if (console_suspend_enabled && !may_wake) {
1340
		struct cdns_uart *cdns_uart = port->private_data;
1341

1342 1343
		clk_enable(cdns_uart->pclk);
		clk_enable(cdns_uart->uartclk);
1344 1345 1346 1347

		spin_lock_irqsave(&port->lock, flags);

		/* Set TX/RX Reset */
1348
		ctrl_reg = readl(port->membase + CDNS_UART_CR);
1349
		ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1350 1351
		writel(ctrl_reg, port->membase + CDNS_UART_CR);
		while (readl(port->membase + CDNS_UART_CR) &
1352
				(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
1353 1354 1355
			cpu_relax();

		/* restore rx timeout value */
1356
		writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
1357
		/* Enable Tx/Rx */
1358
		ctrl_reg = readl(port->membase + CDNS_UART_CR);
1359 1360
		ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
		ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1361
		writel(ctrl_reg, port->membase + CDNS_UART_CR);
1362 1363 1364 1365 1366

		spin_unlock_irqrestore(&port->lock, flags);
	} else {
		spin_lock_irqsave(&port->lock, flags);
		/* restore original rx trigger level */
1367
		writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
1368
		/* enable RX timeout interrupt */
1369
		writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
1370 1371 1372
		spin_unlock_irqrestore(&port->lock, flags);
	}

1373
	return uart_resume_port(&cdns_uart_uart_driver, port);
1374 1375 1376
}
#endif /* ! CONFIG_PM_SLEEP */

1377 1378
static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
		cdns_uart_resume);
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
static const struct cdns_platform_data zynqmp_uart_def = {
				.quirks = CDNS_UART_RXBS_SUPPORT, };

/* Match table for of_platform binding */
static const struct of_device_id cdns_uart_of_match[] = {
	{ .compatible = "xlnx,xuartps", },
	{ .compatible = "cdns,uart-r1p8", },
	{ .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
	{}
};
MODULE_DEVICE_TABLE(of, cdns_uart_of_match);

1392
/**
1393
 * cdns_uart_probe - Platform driver probe
1394 1395
 * @pdev: Pointer to the platform device structure
 *
S
Soren Brinkmann 已提交
1396
 * Return: 0 on success, negative errno otherwise
1397
 */
1398
static int cdns_uart_probe(struct platform_device *pdev)
1399
{
1400
	int rc, id, irq;
1401
	struct uart_port *port;
1402
	struct resource *res;
1403
	struct cdns_uart *cdns_uart_data;
1404
	const struct of_device_id *match;
1405

1406
	cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
S
Soren Brinkmann 已提交
1407
			GFP_KERNEL);
1408
	if (!cdns_uart_data)
1409 1410
		return -ENOMEM;

1411 1412 1413 1414 1415 1416 1417
	match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
	if (match && match->data) {
		const struct cdns_platform_data *data = match->data;

		cdns_uart_data->quirks = data->quirks;
	}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
	if (IS_ERR(cdns_uart_data->pclk)) {
		cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
		if (!IS_ERR(cdns_uart_data->pclk))
			dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
	}
	if (IS_ERR(cdns_uart_data->pclk)) {
		dev_err(&pdev->dev, "pclk clock not found.\n");
		return PTR_ERR(cdns_uart_data->pclk);
	}

	cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
	if (IS_ERR(cdns_uart_data->uartclk)) {
		cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
		if (!IS_ERR(cdns_uart_data->uartclk))
			dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1434
	}
1435 1436 1437
	if (IS_ERR(cdns_uart_data->uartclk)) {
		dev_err(&pdev->dev, "uart_clk clock not found.\n");
		return PTR_ERR(cdns_uart_data->uartclk);
1438 1439
	}

1440
	rc = clk_prepare(cdns_uart_data->pclk);
1441
	if (rc) {
1442
		dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
S
Soren Brinkmann 已提交
1443
		return rc;
1444
	}
1445
	rc = clk_prepare(cdns_uart_data->uartclk);
1446
	if (rc) {
1447
		dev_err(&pdev->dev, "Unable to enable device clock.\n");
1448
		goto err_out_clk_dis_pclk;
1449 1450 1451
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1452 1453 1454 1455
	if (!res) {
		rc = -ENODEV;
		goto err_out_clk_disable;
	}
1456

1457 1458 1459
	irq = platform_get_irq(pdev, 0);
	if (irq <= 0) {
		rc = -ENXIO;
1460 1461
		goto err_out_clk_disable;
	}
1462

1463
#ifdef CONFIG_COMMON_CLK
1464 1465 1466 1467
	cdns_uart_data->clk_rate_change_nb.notifier_call =
			cdns_uart_clk_notifier_cb;
	if (clk_notifier_register(cdns_uart_data->uartclk,
				&cdns_uart_data->clk_rate_change_nb))
1468
		dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1469
#endif
1470 1471 1472 1473
	/* Look for a serialN alias */
	id = of_alias_get_id(pdev->dev.of_node, "serial");
	if (id < 0)
		id = 0;
1474

1475
	/* Initialize the port structure */
1476
	port = cdns_uart_get_port(id);
1477 1478 1479

	if (!port) {
		dev_err(&pdev->dev, "Cannot get uart_port structure\n");
1480
		rc = -ENODEV;
1481
		goto err_out_notif_unreg;
1482
	}
1483

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
	/*
	 * Register the port.
	 * This function also registers this device with the tty layer
	 * and triggers invocation of the config_port() entry point.
	 */
	port->mapbase = res->start;
	port->irq = irq;
	port->dev = &pdev->dev;
	port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
	port->private_data = cdns_uart_data;
	cdns_uart_data->port = port;
	platform_set_drvdata(pdev, port);

	rc = uart_add_one_port(&cdns_uart_uart_driver, port);
	if (rc) {
		dev_err(&pdev->dev,
			"uart_add_one_port() failed; err=%i\n", rc);
		goto err_out_notif_unreg;
	}

	return 0;

1506
err_out_notif_unreg:
1507
#ifdef CONFIG_COMMON_CLK
1508 1509
	clk_notifier_unregister(cdns_uart_data->uartclk,
			&cdns_uart_data->clk_rate_change_nb);
1510
#endif
1511
err_out_clk_disable:
1512
	clk_unprepare(cdns_uart_data->uartclk);
1513
err_out_clk_dis_pclk:
1514
	clk_unprepare(cdns_uart_data->pclk);
1515 1516

	return rc;
1517 1518 1519
}

/**
1520
 * cdns_uart_remove - called when the platform driver is unregistered
1521 1522
 * @pdev: Pointer to the platform device structure
 *
S
Soren Brinkmann 已提交
1523
 * Return: 0 on success, negative errno otherwise
1524
 */
1525
static int cdns_uart_remove(struct platform_device *pdev)
1526
{
1527
	struct uart_port *port = platform_get_drvdata(pdev);
1528
	struct cdns_uart *cdns_uart_data = port->private_data;
1529
	int rc;
1530

1531
	/* Remove the cdns_uart port from the serial core */
1532
#ifdef CONFIG_COMMON_CLK
1533 1534
	clk_notifier_unregister(cdns_uart_data->uartclk,
			&cdns_uart_data->clk_rate_change_nb);
1535
#endif
1536
	rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
1537
	port->mapbase = 0;
1538 1539
	clk_unprepare(cdns_uart_data->uartclk);
	clk_unprepare(cdns_uart_data->pclk);
1540 1541 1542
	return rc;
}

1543 1544 1545
static struct platform_driver cdns_uart_platform_driver = {
	.probe   = cdns_uart_probe,
	.remove  = cdns_uart_remove,
1546
	.driver  = {
1547 1548 1549
		.name = CDNS_UART_NAME,
		.of_match_table = cdns_uart_of_match,
		.pm = &cdns_uart_dev_pm_ops,
1550 1551 1552
		},
};

1553
static int __init cdns_uart_init(void)
1554 1555 1556
{
	int retval = 0;

1557 1558
	/* Register the cdns_uart driver with the serial core */
	retval = uart_register_driver(&cdns_uart_uart_driver);
1559 1560 1561 1562
	if (retval)
		return retval;

	/* Register the platform driver */
1563
	retval = platform_driver_register(&cdns_uart_platform_driver);
1564
	if (retval)
1565
		uart_unregister_driver(&cdns_uart_uart_driver);
1566 1567 1568 1569

	return retval;
}

1570
static void __exit cdns_uart_exit(void)
1571 1572
{
	/* Unregister the platform driver */
1573
	platform_driver_unregister(&cdns_uart_platform_driver);
1574

1575 1576
	/* Unregister the cdns_uart driver */
	uart_unregister_driver(&cdns_uart_uart_driver);
1577 1578
}

1579 1580
module_init(cdns_uart_init);
module_exit(cdns_uart_exit);
1581

1582
MODULE_DESCRIPTION("Driver for Cadence UART");
1583 1584
MODULE_AUTHOR("Xilinx Inc.");
MODULE_LICENSE("GPL");