xilinx_uartps.c 41.8 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 40
#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 */
#define CDNS_UART_REGISTER_SPACE	0xFFF
41

42 43
#define cdns_uart_readl(offset)		ioread32(port->membase + offset)
#define cdns_uart_writel(val, offset)	iowrite32(val, port->membase + offset)
44

S
Suneel 已提交
45 46 47 48 49 50 51 52 53 54
/* 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 已提交
55
/* Register offsets for the UART. */
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#define CDNS_UART_CR_OFFSET		0x00  /* Control Register */
#define CDNS_UART_MR_OFFSET		0x04  /* Mode Register */
#define CDNS_UART_IER_OFFSET		0x08  /* Interrupt Enable */
#define CDNS_UART_IDR_OFFSET		0x0C  /* Interrupt Disable */
#define CDNS_UART_IMR_OFFSET		0x10  /* Interrupt Mask */
#define CDNS_UART_ISR_OFFSET		0x14  /* Interrupt Status */
#define CDNS_UART_BAUDGEN_OFFSET	0x18  /* Baud Rate Generator */
#define CDNS_UART_RXTOUT_OFFSET		0x1C  /* RX Timeout */
#define CDNS_UART_RXWM_OFFSET		0x20  /* RX FIFO Trigger Level */
#define CDNS_UART_MODEMCR_OFFSET	0x24  /* Modem Control */
#define CDNS_UART_MODEMSR_OFFSET	0x28  /* Modem Status */
#define CDNS_UART_SR_OFFSET		0x2C  /* Channel Status */
#define CDNS_UART_FIFO_OFFSET		0x30  /* FIFO */
#define CDNS_UART_BAUDDIV_OFFSET	0x34  /* Baud Rate Divider */
#define CDNS_UART_FLOWDEL_OFFSET	0x38  /* Flow Delay */
#define CDNS_UART_IRRX_PWIDTH_OFFSET	0x3C  /* IR Min Received Pulse Width */
#define CDNS_UART_IRTX_PWIDTH_OFFSET	0x40  /* IR Transmitted pulse Width */
#define CDNS_UART_TXWM_OFFSET		0x44  /* TX FIFO Trigger Level */
S
Soren Brinkmann 已提交
74 75

/* Control Register Bit Definitions */
76 77 78 79 80 81 82 83 84
#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 */
85

S
Soren Brinkmann 已提交
86 87
/*
 * Mode Register:
88 89 90 91
 * 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.
 */
92 93 94
#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 */
95

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

99 100 101 102 103
#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 */
104

105 106 107
#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 */
108

S
Soren Brinkmann 已提交
109 110
/*
 * Interrupt Registers:
111 112 113 114 115 116 117 118 119
 * 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.
 */
120 121 122 123 124 125 126 127 128 129 130
#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 */
131

132
/* Goes in read_status_mask for break detection as the HW doesn't do it*/
133
#define CDNS_UART_IXR_BRK	0x80000000
134

S
Soren Brinkmann 已提交
135 136
/*
 * Channel Status Register:
137 138 139 140
 * 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.
 */
141 142 143 144
#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 */
145

146
/* baud dividers min/max values */
147 148 149
#define CDNS_UART_BDIV_MIN	4
#define CDNS_UART_BDIV_MAX	255
#define CDNS_UART_CD_MAX	65535
150

151
/**
152
 * struct cdns_uart - device data
153
 * @port:		Pointer to the UART port
154 155
 * @uartclk:		Reference clock
 * @pclk:		APB clock
156 157
 * @baud:		Current baud rate
 * @clk_rate_change_nb:	Notifier block for clock changes
158
 */
159
struct cdns_uart {
160
	struct uart_port	*port;
161 162
	struct clk		*uartclk;
	struct clk		*pclk;
163 164
	unsigned int		baud;
	struct notifier_block	clk_rate_change_nb;
165
};
166 167
#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
		clk_rate_change_nb);
168

169
/**
170
 * cdns_uart_isr - Interrupt handler
171 172 173
 * @irq: Irq number
 * @dev_id: Id of the port
 *
174 175
 * Return: IRQHANDLED
 */
176
static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
177 178 179 180 181 182 183 184 185 186 187 188
{
	struct uart_port *port = (struct uart_port *)dev_id;
	unsigned long flags;
	unsigned int isrstatus, numbytes;
	unsigned int data;
	char status = TTY_NORMAL;

	spin_lock_irqsave(&port->lock, flags);

	/* Read the interrupt status register to determine which
	 * interrupt(s) is/are active.
	 */
189
	isrstatus = cdns_uart_readl(CDNS_UART_ISR_OFFSET);
190

191 192 193 194 195
	/*
	 * 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.
	 */
196 197 198 199 200 201
	if (isrstatus & CDNS_UART_IXR_FRAMING) {
		while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
					CDNS_UART_SR_RXEMPTY)) {
			if (!cdns_uart_readl(CDNS_UART_FIFO_OFFSET)) {
				port->read_status_mask |= CDNS_UART_IXR_BRK;
				isrstatus &= ~CDNS_UART_IXR_FRAMING;
202 203
			}
		}
204
		cdns_uart_writel(CDNS_UART_IXR_FRAMING, CDNS_UART_ISR_OFFSET);
205 206
	}

207
	/* drop byte with parity error if IGNPAR specified */
208 209
	if (isrstatus & port->ignore_status_mask & CDNS_UART_IXR_PARITY)
		isrstatus &= ~(CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT);
210 211 212 213

	isrstatus &= port->read_status_mask;
	isrstatus &= ~port->ignore_status_mask;

214 215
	if ((isrstatus & CDNS_UART_IXR_TOUT) ||
		(isrstatus & CDNS_UART_IXR_RXTRIG)) {
216
		/* Receive Timeout Interrupt */
217 218 219
		while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
			CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
			data = cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
220 221 222

			/* Non-NULL byte after BREAK is garbage (99%) */
			if (data && (port->read_status_mask &
223 224
						CDNS_UART_IXR_BRK)) {
				port->read_status_mask &= ~CDNS_UART_IXR_BRK;
225 226 227 228 229
				port->icount.brk++;
				if (uart_handle_break(port))
					continue;
			}

230
#ifdef SUPPORT_SYSRQ
231 232 233 234 235 236 237 238 239 240 241 242 243
			/*
			 * uart_handle_sysrq_char() doesn't work if
			 * spinlocked, for some reason
			 */
			 if (port->sysrq) {
				spin_unlock(&port->lock);
				if (uart_handle_sysrq_char(port,
							(unsigned char)data)) {
					spin_lock(&port->lock);
					continue;
				}
				spin_lock(&port->lock);
			}
244
#endif
245

246 247
			port->icount.rx++;

248
			if (isrstatus & CDNS_UART_IXR_PARITY) {
249 250
				port->icount.parity++;
				status = TTY_PARITY;
251
			} else if (isrstatus & CDNS_UART_IXR_FRAMING) {
252 253
				port->icount.frame++;
				status = TTY_FRAME;
254
			} else if (isrstatus & CDNS_UART_IXR_OVERRUN) {
255
				port->icount.overrun++;
S
Soren Brinkmann 已提交
256
			}
257

258
			uart_insert_char(port, isrstatus, CDNS_UART_IXR_OVERRUN,
J
Jiri Slaby 已提交
259
					data, status);
260 261
		}
		spin_unlock(&port->lock);
J
Jiri Slaby 已提交
262
		tty_flip_buffer_push(&port->state->port);
263 264 265 266
		spin_lock(&port->lock);
	}

	/* Dispatch an appropriate handler */
267
	if ((isrstatus & CDNS_UART_IXR_TXEMPTY) == CDNS_UART_IXR_TXEMPTY) {
268
		if (uart_circ_empty(&port->state->xmit)) {
269 270
			cdns_uart_writel(CDNS_UART_IXR_TXEMPTY,
						CDNS_UART_IDR_OFFSET);
271 272 273 274 275 276 277
		} else {
			numbytes = port->fifosize;
			/* Break if no more data available in the UART buffer */
			while (numbytes--) {
				if (uart_circ_empty(&port->state->xmit))
					break;
				/* Get the data from the UART circular buffer
278
				 * and write it to the cdns_uart's TX_FIFO
279 280
				 * register.
				 */
281
				cdns_uart_writel(
282
					port->state->xmit.buf[port->state->xmit.
283
					tail], CDNS_UART_FIFO_OFFSET);
284 285 286 287 288 289 290

				port->icount.tx++;

				/* Adjust the tail of the UART buffer and wrap
				 * the buffer if it reaches limit.
				 */
				port->state->xmit.tail =
S
Soren Brinkmann 已提交
291
					(port->state->xmit.tail + 1) &
292 293 294 295 296 297 298 299 300
						(UART_XMIT_SIZE - 1);
			}

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

301
	cdns_uart_writel(isrstatus, CDNS_UART_ISR_OFFSET);
302 303 304 305 306 307 308 309

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

	return IRQ_HANDLED;
}

/**
310
 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
311 312 313 314 315
 * @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)
316
 * Return: baud rate, requested baud when possible, or actual baud when there
317 318 319 320 321 322 323 324 325 326 327 328
 *	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
 */
329 330
static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
		unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
331
{
332 333 334
	u32 cd, bdiv;
	unsigned int calc_baud;
	unsigned int bestbaud = 0;
335
	unsigned int bauderror;
336
	unsigned int besterror = ~0;
337

338
	if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
339 340 341 342 343
		*div8 = 1;
		clk /= 8;
	} else {
		*div8 = 0;
	}
344

345
	for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
346
		cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
347
		if (cd < 1 || cd > CDNS_UART_CD_MAX)
348 349
			continue;

350
		calc_baud = clk / (cd * (bdiv + 1));
351 352 353 354 355 356

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

357 358 359 360 361
		if (besterror > bauderror) {
			*rbdiv = bdiv;
			*rcd = cd;
			bestbaud = calc_baud;
			besterror = bauderror;
362 363
		}
	}
364 365 366 367 368 369
	/* use the values when percent error is acceptable */
	if (((besterror * 100) / baud) < 3)
		bestbaud = baud;

	return bestbaud;
}
370

371
/**
372
 * cdns_uart_set_baud_rate - Calculate and set the baud rate
373 374
 * @port: Handle to the uart port structure
 * @baud: Baud rate to set
375
 * Return: baud rate, requested baud when possible, or actual baud when there
376 377
 *	   was too much error, zero if no valid divisors are found.
 */
378
static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
379 380 381
		unsigned int baud)
{
	unsigned int calc_baud;
382
	u32 cd = 0, bdiv = 0;
383 384
	u32 mreg;
	int div8;
385
	struct cdns_uart *cdns_uart = port->private_data;
386

387
	calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
388 389 390
			&div8);

	/* Write new divisors to hardware */
391
	mreg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
392
	if (div8)
393
		mreg |= CDNS_UART_MR_CLKSEL;
394
	else
395 396 397 398 399
		mreg &= ~CDNS_UART_MR_CLKSEL;
	cdns_uart_writel(mreg, CDNS_UART_MR_OFFSET);
	cdns_uart_writel(cd, CDNS_UART_BAUDGEN_OFFSET);
	cdns_uart_writel(bdiv, CDNS_UART_BAUDDIV_OFFSET);
	cdns_uart->baud = baud;
400 401 402 403

	return calc_baud;
}

404
#ifdef CONFIG_COMMON_CLK
405
/**
406
 * cdns_uart_clk_notitifer_cb - Clock notifier callback
407 408 409
 * @nb:		Notifier block
 * @event:	Notify event
 * @data:	Notifier data
S
Soren Brinkmann 已提交
410
 * Return:	NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
411
 */
412
static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
413 414 415 416 417 418 419
		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;
420
	struct cdns_uart *cdns_uart = to_cdns_uart(nb);
421

422
	port = cdns_uart->port;
423 424 425 426 427 428
	if (port->suspended)
		return NOTIFY_OK;

	switch (event) {
	case PRE_RATE_CHANGE:
	{
S
Soren Brinkmann 已提交
429
		u32 bdiv, cd;
430 431 432 433 434 435
		int div8;

		/*
		 * Find out if current baud-rate can be achieved with new clock
		 * frequency.
		 */
436
		if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
437 438
					&bdiv, &cd, &div8)) {
			dev_warn(port->dev, "clock rate change rejected\n");
439
			return NOTIFY_BAD;
440
		}
441

442
		spin_lock_irqsave(&cdns_uart->port->lock, flags);
443 444

		/* Disable the TX and RX to set baud rate */
445 446 447
		ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
		ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
		cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
448

449
		spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
450 451 452 453 454 455 456 457 458

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

459
		spin_lock_irqsave(&cdns_uart->port->lock, flags);
460 461 462 463

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

464 465
		cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
				cdns_uart->baud);
466 467 468
		/* fall through */
	case ABORT_RATE_CHANGE:
		if (!locked)
469
			spin_lock_irqsave(&cdns_uart->port->lock, flags);
470 471

		/* Set TX/RX Reset */
472 473 474
		ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
		ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
		cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
475

476 477
		while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
				(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
478 479 480 481 482 483 484
			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.
		 */
485 486 487 488 489
		cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
		ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
		ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
		ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
		cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
490

491
		spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
492 493 494 495 496 497

		return NOTIFY_OK;
	default:
		return NOTIFY_DONE;
	}
}
498
#endif
499

500
/**
501
 * cdns_uart_start_tx -  Start transmitting bytes
502
 * @port: Handle to the uart port structure
503
 */
504
static void cdns_uart_start_tx(struct uart_port *port)
505 506 507 508 509 510
{
	unsigned int status, numbytes = port->fifosize;

	if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
		return;

511
	status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
512 513 514
	/* Set the TX enable bit and clear the TX disable bit to enable the
	 * transmitter.
	 */
515 516
	cdns_uart_writel((status & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
		CDNS_UART_CR_OFFSET);
517

518 519
	while (numbytes-- && ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
				CDNS_UART_SR_TXFULL)) != CDNS_UART_SR_TXFULL) {
520 521 522 523 524
		/* Break if no more data available in the UART buffer */
		if (uart_circ_empty(&port->state->xmit))
			break;

		/* Get the data from the UART circular buffer and
525
		 * write it to the cdns_uart's TX_FIFO register.
526
		 */
527
		cdns_uart_writel(
528
			port->state->xmit.buf[port->state->xmit.tail],
529
			CDNS_UART_FIFO_OFFSET);
530 531 532 533 534 535 536 537
		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);
	}
538
	cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_ISR_OFFSET);
539
	/* Enable the TX Empty interrupt */
540
	cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_IER_OFFSET);
541 542 543 544 545 546

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

/**
547
 * cdns_uart_stop_tx - Stop TX
548
 * @port: Handle to the uart port structure
549
 */
550
static void cdns_uart_stop_tx(struct uart_port *port)
551 552 553
{
	unsigned int regval;

554 555
	regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
	regval |= CDNS_UART_CR_TX_DIS;
556
	/* Disable the transmitter */
557
	cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
558 559 560
}

/**
561
 * cdns_uart_stop_rx - Stop RX
562
 * @port: Handle to the uart port structure
563
 */
564
static void cdns_uart_stop_rx(struct uart_port *port)
565 566 567
{
	unsigned int regval;

568 569
	regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
	regval |= CDNS_UART_CR_RX_DIS;
570
	/* Disable the receiver */
571
	cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
572 573 574
}

/**
575
 * cdns_uart_tx_empty -  Check whether TX is empty
576 577
 * @port: Handle to the uart port structure
 *
578 579
 * Return: TIOCSER_TEMT on success, 0 otherwise
 */
580
static unsigned int cdns_uart_tx_empty(struct uart_port *port)
581 582 583
{
	unsigned int status;

584
	status = cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY;
585 586 587 588
	return status ? TIOCSER_TEMT : 0;
}

/**
589
 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
590 591 592
 *			transmitting char breaks
 * @port: Handle to the uart port structure
 * @ctl: Value based on which start or stop decision is taken
593
 */
594
static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
595 596 597 598 599 600
{
	unsigned int status;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);

601
	status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
602 603

	if (ctl == -1)
604 605
		cdns_uart_writel(CDNS_UART_CR_STARTBRK | status,
					CDNS_UART_CR_OFFSET);
606
	else {
607 608 609
		if ((status & CDNS_UART_CR_STOPBRK) == 0)
			cdns_uart_writel(CDNS_UART_CR_STOPBRK | status,
					 CDNS_UART_CR_OFFSET);
610 611 612 613 614
	}
	spin_unlock_irqrestore(&port->lock, flags);
}

/**
615
 * cdns_uart_set_termios - termios operations, handling data length, parity,
616 617 618 619
 *				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
620
 */
621
static void cdns_uart_set_termios(struct uart_port *port,
622 623 624
				struct ktermios *termios, struct ktermios *old)
{
	unsigned int cval = 0;
625
	unsigned int baud, minbaud, maxbaud;
626 627 628 629 630 631
	unsigned long flags;
	unsigned int ctrl_reg, mode_reg;

	spin_lock_irqsave(&port->lock, flags);

	/* Empty the receive FIFO 1st before making changes */
632 633 634
	while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
		 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
		cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
635 636 637
	}

	/* Disable the TX and RX to set baud rate */
638 639 640
	ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
	ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
	cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
641

642 643 644 645 646
	/*
	 * 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()
	 */
647 648 649
	minbaud = port->uartclk /
			((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
	maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
650
	baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
651
	baud = cdns_uart_set_baud_rate(port, baud);
652 653 654
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);

S
Soren Brinkmann 已提交
655
	/* Update the per-port timeout. */
656 657 658
	uart_update_timeout(port, termios->c_cflag, baud);

	/* Set TX/RX Reset */
659 660 661
	ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
	ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
	cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
662

S
Soren Brinkmann 已提交
663 664
	/*
	 * Clear the RX disable and TX disable bits and then set the TX enable
665 666
	 * bit and RX enable bit to enable the transmitter and receiver.
	 */
667 668 669 670
	ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
	ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
	ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
	cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
671

672
	cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
673

674 675
	port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
			CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
676 677 678
	port->ignore_status_mask = 0;

	if (termios->c_iflag & INPCK)
679 680
		port->read_status_mask |= CDNS_UART_IXR_PARITY |
		CDNS_UART_IXR_FRAMING;
681 682

	if (termios->c_iflag & IGNPAR)
683 684
		port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
			CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
685 686 687

	/* ignore all characters if CREAD is not set */
	if ((termios->c_cflag & CREAD) == 0)
688 689 690
		port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
			CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
			CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
691

692
	mode_reg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
693 694 695 696

	/* Handling Data Size */
	switch (termios->c_cflag & CSIZE) {
	case CS6:
697
		cval |= CDNS_UART_MR_CHARLEN_6_BIT;
698 699
		break;
	case CS7:
700
		cval |= CDNS_UART_MR_CHARLEN_7_BIT;
701 702 703
		break;
	default:
	case CS8:
704
		cval |= CDNS_UART_MR_CHARLEN_8_BIT;
705 706 707 708 709 710 711
		termios->c_cflag &= ~CSIZE;
		termios->c_cflag |= CS8;
		break;
	}

	/* Handling Parity and Stop Bits length */
	if (termios->c_cflag & CSTOPB)
712
		cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
713
	else
714
		cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
715 716 717 718 719

	if (termios->c_cflag & PARENB) {
		/* Mark or Space parity */
		if (termios->c_cflag & CMSPAR) {
			if (termios->c_cflag & PARODD)
720
				cval |= CDNS_UART_MR_PARITY_MARK;
721
			else
722
				cval |= CDNS_UART_MR_PARITY_SPACE;
723 724
		} else {
			if (termios->c_cflag & PARODD)
725
				cval |= CDNS_UART_MR_PARITY_ODD;
726
			else
727
				cval |= CDNS_UART_MR_PARITY_EVEN;
728 729
		}
	} else {
730
		cval |= CDNS_UART_MR_PARITY_NONE;
731 732
	}
	cval |= mode_reg & 1;
733
	cdns_uart_writel(cval, CDNS_UART_MR_OFFSET);
734 735 736 737 738

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

/**
739
 * cdns_uart_startup - Called when an application opens a cdns_uart port
740 741
 * @port: Handle to the uart port structure
 *
S
Soren Brinkmann 已提交
742
 * Return: 0 on success, negative errno otherwise
743
 */
744
static int cdns_uart_startup(struct uart_port *port)
745 746 747
{
	unsigned int retval = 0, status = 0;

748
	retval = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME,
749 750 751 752 753
								(void *)port);
	if (retval)
		return retval;

	/* Disable the TX and RX */
754 755
	cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
						CDNS_UART_CR_OFFSET);
756 757 758 759

	/* Set the Control Register with TX/RX Enable, TX/RX Reset,
	 * no break chars.
	 */
760 761
	cdns_uart_writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
				CDNS_UART_CR_OFFSET);
762

763
	status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
764 765 766 767

	/* 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.
	 */
768 769 770
	cdns_uart_writel((status & ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS))
			| (CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN |
			CDNS_UART_CR_STOPBRK), CDNS_UART_CR_OFFSET);
771 772 773 774

	/* Set the Mode Register with normal mode,8 data bits,1 stop bit,
	 * no parity.
	 */
775 776 777
	cdns_uart_writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
		| CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
		 CDNS_UART_MR_OFFSET);
778

S
Suneel 已提交
779 780 781 782
	/*
	 * Set the RX FIFO Trigger level to use most of the FIFO, but it
	 * can be tuned with a module parameter
	 */
783
	cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
784

S
Suneel 已提交
785 786 787 788
	/*
	 * Receive Timeout register is enabled but it
	 * can be tuned with a module parameter
	 */
789
	cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
790

791
	/* Clear out any pending interrupts before enabling them */
792 793
	cdns_uart_writel(cdns_uart_readl(CDNS_UART_ISR_OFFSET),
			CDNS_UART_ISR_OFFSET);
794 795

	/* Set the Interrupt Registers with desired interrupts */
796 797 798 799
	cdns_uart_writel(CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_PARITY |
		CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN |
		CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT,
		CDNS_UART_IER_OFFSET);
800 801 802 803 804

	return retval;
}

/**
805
 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
806
 * @port: Handle to the uart port structure
807
 */
808
static void cdns_uart_shutdown(struct uart_port *port)
809 810 811 812
{
	int status;

	/* Disable interrupts */
813 814
	status = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
	cdns_uart_writel(status, CDNS_UART_IDR_OFFSET);
815 816

	/* Disable the TX and RX */
817 818
	cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
				 CDNS_UART_CR_OFFSET);
819 820 821 822
	free_irq(port->irq, port);
}

/**
823
 * cdns_uart_type - Set UART type to cdns_uart port
824 825
 * @port: Handle to the uart port structure
 *
826 827
 * Return: string on success, NULL otherwise
 */
828
static const char *cdns_uart_type(struct uart_port *port)
829
{
830
	return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
831 832 833
}

/**
834
 * cdns_uart_verify_port - Verify the port params
835 836 837
 * @port: Handle to the uart port structure
 * @ser: Handle to the structure whose members are compared
 *
S
Soren Brinkmann 已提交
838
 * Return: 0 on success, negative errno otherwise.
839
 */
840
static int cdns_uart_verify_port(struct uart_port *port,
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
					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;
}

/**
857 858
 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
 *				called when the driver adds a cdns_uart port via
859 860 861
 *				uart_add_one_port()
 * @port: Handle to the uart port structure
 *
S
Soren Brinkmann 已提交
862
 * Return: 0 on success, negative errno otherwise.
863
 */
864
static int cdns_uart_request_port(struct uart_port *port)
865
{
866 867
	if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
					 CDNS_UART_NAME)) {
868 869 870
		return -ENOMEM;
	}

871
	port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
872 873
	if (!port->membase) {
		dev_err(port->dev, "Unable to map registers\n");
874
		release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
875 876 877 878 879 880
		return -ENOMEM;
	}
	return 0;
}

/**
881
 * cdns_uart_release_port - Release UART port
882
 * @port: Handle to the uart port structure
S
Soren Brinkmann 已提交
883
 *
884 885
 * Release the memory region attached to a cdns_uart port. Called when the
 * driver removes a cdns_uart port via uart_remove_one_port().
886
 */
887
static void cdns_uart_release_port(struct uart_port *port)
888
{
889
	release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
890 891 892 893 894
	iounmap(port->membase);
	port->membase = NULL;
}

/**
895
 * cdns_uart_config_port - Configure UART port
896 897
 * @port: Handle to the uart port structure
 * @flags: If any
898
 */
899
static void cdns_uart_config_port(struct uart_port *port, int flags)
900
{
901
	if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
902 903 904 905
		port->type = PORT_XUARTPS;
}

/**
906
 * cdns_uart_get_mctrl - Get the modem control state
907 908
 * @port: Handle to the uart port structure
 *
909 910
 * Return: the modem control state
 */
911
static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
912 913 914 915
{
	return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
}

916
static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
917 918 919 920
{
	/* N/A */
}

921
#ifdef CONFIG_CONSOLE_POLL
922
static int cdns_uart_poll_get_char(struct uart_port *port)
923 924 925 926 927
{
	u32 imr;
	int c;

	/* Disable all interrupts */
928 929
	imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
	cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
930 931

	/* Check if FIFO is empty */
932
	if (cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_RXEMPTY)
933 934
		c = NO_POLL_CHAR;
	else /* Read a character */
935
		c = (unsigned char) cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
936 937

	/* Enable interrupts */
938
	cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
939 940 941 942

	return c;
}

943
static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
944 945 946 947
{
	u32 imr;

	/* Disable all interrupts */
948 949
	imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
	cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
950 951

	/* Wait until FIFO is empty */
952
	while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
953 954 955
		cpu_relax();

	/* Write a character */
956
	cdns_uart_writel(c, CDNS_UART_FIFO_OFFSET);
957 958

	/* Wait until FIFO is empty */
959
	while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
960 961 962
		cpu_relax();

	/* Enable interrupts */
963
	cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
964 965 966 967 968

	return;
}
#endif

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
static struct uart_ops cdns_uart_ops = {
	.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,
	.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,
985
#ifdef CONFIG_CONSOLE_POLL
986 987
	.poll_get_char	= cdns_uart_poll_get_char,
	.poll_put_char	= cdns_uart_poll_put_char,
988
#endif
989 990
};

991
static struct uart_port cdns_uart_port[2];
992 993

/**
994
 * cdns_uart_get_port - Configure the port from platform device resource info
995 996
 * @id: Port id
 *
997 998
 * Return: a pointer to a uart_port or NULL for failure
 */
999
static struct uart_port *cdns_uart_get_port(int id)
1000 1001 1002
{
	struct uart_port *port;

1003
	/* Try the given port id if failed use default method */
1004
	if (cdns_uart_port[id].mapbase != 0) {
1005
		/* Find the next unused port */
1006 1007
		for (id = 0; id < CDNS_UART_NR_PORTS; id++)
			if (cdns_uart_port[id].mapbase == 0)
1008 1009
				break;
	}
1010

1011
	if (id >= CDNS_UART_NR_PORTS)
1012 1013
		return NULL;

1014
	port = &cdns_uart_port[id];
1015 1016 1017 1018 1019 1020 1021 1022 1023

	/* At this point, we've got an empty uart_port struct, initialize it */
	spin_lock_init(&port->lock);
	port->membase	= NULL;
	port->iobase	= 1; /* mark port in use */
	port->irq	= 0;
	port->type	= PORT_UNKNOWN;
	port->iotype	= UPIO_MEM32;
	port->flags	= UPF_BOOT_AUTOCONF;
1024 1025
	port->ops	= &cdns_uart_ops;
	port->fifosize	= CDNS_UART_FIFO_SIZE;
1026 1027 1028 1029 1030 1031 1032
	port->line	= id;
	port->dev	= NULL;
	return port;
}

#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
/**
1033
 * cdns_uart_console_wait_tx - Wait for the TX to be full
1034
 * @port: Handle to the uart port structure
1035
 */
1036
static void cdns_uart_console_wait_tx(struct uart_port *port)
1037
{
1038 1039
	while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY)
				!= CDNS_UART_SR_TXEMPTY)
1040 1041 1042 1043
		barrier();
}

/**
1044
 * cdns_uart_console_putchar - write the character to the FIFO buffer
1045 1046
 * @port: Handle to the uart port structure
 * @ch: Character to be written
1047
 */
1048
static void cdns_uart_console_putchar(struct uart_port *port, int ch)
1049
{
1050 1051
	cdns_uart_console_wait_tx(port);
	cdns_uart_writel(ch, CDNS_UART_FIFO_OFFSET);
1052 1053 1054
}

/**
1055
 * cdns_uart_console_write - perform write operation
1056
 * @co: Console handle
1057 1058
 * @s: Pointer to character array
 * @count: No of characters
1059
 */
1060
static void cdns_uart_console_write(struct console *co, const char *s,
1061 1062
				unsigned int count)
{
1063
	struct uart_port *port = &cdns_uart_port[co->index];
1064
	unsigned long flags;
1065
	unsigned int imr, ctrl;
1066 1067 1068 1069 1070 1071 1072 1073
	int locked = 1;

	if (oops_in_progress)
		locked = spin_trylock_irqsave(&port->lock, flags);
	else
		spin_lock_irqsave(&port->lock, flags);

	/* save and disable interrupt */
1074 1075
	imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
	cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
1076

1077 1078 1079 1080
	/*
	 * Make sure that the tx part is enabled. Set the TX enable bit and
	 * clear the TX disable bit to enable the transmitter.
	 */
1081 1082 1083
	ctrl = cdns_uart_readl(CDNS_UART_CR_OFFSET);
	cdns_uart_writel((ctrl & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
		CDNS_UART_CR_OFFSET);
1084

1085 1086
	uart_console_write(port, s, count, cdns_uart_console_putchar);
	cdns_uart_console_wait_tx(port);
1087

1088
	cdns_uart_writel(ctrl, CDNS_UART_CR_OFFSET);
1089

1090
	/* restore interrupt state */
1091
	cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
1092 1093 1094 1095 1096 1097

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

/**
1098
 * cdns_uart_console_setup - Initialize the uart to default config
1099 1100 1101
 * @co: Console handle
 * @options: Initial settings of uart
 *
S
Soren Brinkmann 已提交
1102
 * Return: 0 on success, negative errno otherwise.
1103
 */
1104
static int __init cdns_uart_console_setup(struct console *co, char *options)
1105
{
1106
	struct uart_port *port = &cdns_uart_port[co->index];
1107 1108 1109 1110 1111
	int baud = 9600;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

1112
	if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
		return -EINVAL;

	if (!port->mapbase) {
		pr_debug("console on ttyPS%i not present\n", co->index);
		return -ENODEV;
	}

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

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

1126
static struct uart_driver cdns_uart_uart_driver;
1127

1128 1129 1130
static struct console cdns_uart_console = {
	.name	= CDNS_UART_TTY_NAME,
	.write	= cdns_uart_console_write,
1131
	.device	= uart_console_device,
1132
	.setup	= cdns_uart_console_setup,
1133 1134
	.flags	= CON_PRINTBUFFER,
	.index	= -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
1135
	.data	= &cdns_uart_uart_driver,
1136 1137 1138
};

/**
1139
 * cdns_uart_console_init - Initialization call
1140
 *
S
Soren Brinkmann 已提交
1141
 * Return: 0 on success, negative errno otherwise
1142
 */
1143
static int __init cdns_uart_console_init(void)
1144
{
1145
	register_console(&cdns_uart_console);
1146 1147 1148
	return 0;
}

1149
console_initcall(cdns_uart_console_init);
1150 1151 1152

#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */

1153
static struct uart_driver cdns_uart_uart_driver = {
S
Soren Brinkmann 已提交
1154
	.owner		= THIS_MODULE,
1155 1156 1157 1158 1159
	.driver_name	= CDNS_UART_NAME,
	.dev_name	= CDNS_UART_TTY_NAME,
	.major		= CDNS_UART_MAJOR,
	.minor		= CDNS_UART_MINOR,
	.nr		= CDNS_UART_NR_PORTS,
1160
#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1161
	.cons		= &cdns_uart_console,
1162 1163 1164
#endif
};

1165 1166
#ifdef CONFIG_PM_SLEEP
/**
1167
 * cdns_uart_suspend - suspend event
1168 1169
 * @device: Pointer to the device structure
 *
1170
 * Return: 0
1171
 */
1172
static int cdns_uart_suspend(struct device *device)
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
{
	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.
	 */
1191
	uart_suspend_port(&cdns_uart_uart_driver, port);
1192
	if (console_suspend_enabled && !may_wake) {
1193
		struct cdns_uart *cdns_uart = port->private_data;
1194

1195 1196
		clk_disable(cdns_uart->uartclk);
		clk_disable(cdns_uart->pclk);
1197 1198 1199 1200 1201
	} else {
		unsigned long flags = 0;

		spin_lock_irqsave(&port->lock, flags);
		/* Empty the receive FIFO 1st before making changes */
1202 1203 1204
		while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
					CDNS_UART_SR_RXEMPTY))
			cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
1205
		/* set RX trigger level to 1 */
1206
		cdns_uart_writel(1, CDNS_UART_RXWM_OFFSET);
1207
		/* disable RX timeout interrups */
1208
		cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IDR_OFFSET);
1209 1210 1211 1212 1213 1214 1215
		spin_unlock_irqrestore(&port->lock, flags);
	}

	return 0;
}

/**
1216
 * cdns_uart_resume - Resume after a previous suspend
1217 1218
 * @device: Pointer to the device structure
 *
1219
 * Return: 0
1220
 */
1221
static int cdns_uart_resume(struct device *device)
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
{
	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) {
1239
		struct cdns_uart *cdns_uart = port->private_data;
1240

1241 1242
		clk_enable(cdns_uart->pclk);
		clk_enable(cdns_uart->uartclk);
1243 1244 1245 1246

		spin_lock_irqsave(&port->lock, flags);

		/* Set TX/RX Reset */
1247 1248 1249 1250 1251
		ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
		ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
		cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
		while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
				(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
1252 1253 1254
			cpu_relax();

		/* restore rx timeout value */
1255
		cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
1256
		/* Enable Tx/Rx */
1257 1258 1259 1260
		ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
		ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
		ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
		cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
1261 1262 1263 1264 1265

		spin_unlock_irqrestore(&port->lock, flags);
	} else {
		spin_lock_irqsave(&port->lock, flags);
		/* restore original rx trigger level */
1266
		cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
1267
		/* enable RX timeout interrupt */
1268
		cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IER_OFFSET);
1269 1270 1271
		spin_unlock_irqrestore(&port->lock, flags);
	}

1272
	return uart_resume_port(&cdns_uart_uart_driver, port);
1273 1274 1275
}
#endif /* ! CONFIG_PM_SLEEP */

1276 1277
static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
		cdns_uart_resume);
1278

1279
/**
1280
 * cdns_uart_probe - Platform driver probe
1281 1282
 * @pdev: Pointer to the platform device structure
 *
S
Soren Brinkmann 已提交
1283
 * Return: 0 on success, negative errno otherwise
1284
 */
1285
static int cdns_uart_probe(struct platform_device *pdev)
1286
{
1287
	int rc, id;
1288 1289
	struct uart_port *port;
	struct resource *res, *res2;
1290
	struct cdns_uart *cdns_uart_data;
1291

1292
	cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
S
Soren Brinkmann 已提交
1293
			GFP_KERNEL);
1294
	if (!cdns_uart_data)
1295 1296
		return -ENOMEM;

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
	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");
1313
	}
1314 1315 1316
	if (IS_ERR(cdns_uart_data->uartclk)) {
		dev_err(&pdev->dev, "uart_clk clock not found.\n");
		return PTR_ERR(cdns_uart_data->uartclk);
1317 1318
	}

1319
	rc = clk_prepare_enable(cdns_uart_data->pclk);
1320
	if (rc) {
1321
		dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
S
Soren Brinkmann 已提交
1322
		return rc;
1323
	}
1324
	rc = clk_prepare_enable(cdns_uart_data->uartclk);
1325
	if (rc) {
1326
		dev_err(&pdev->dev, "Unable to enable device clock.\n");
1327
		goto err_out_clk_dis_pclk;
1328 1329 1330
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1331 1332 1333 1334
	if (!res) {
		rc = -ENODEV;
		goto err_out_clk_disable;
	}
1335 1336

	res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1337 1338 1339 1340
	if (!res2) {
		rc = -ENODEV;
		goto err_out_clk_disable;
	}
1341

1342
#ifdef CONFIG_COMMON_CLK
1343 1344 1345 1346
	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))
1347
		dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1348
#endif
1349 1350 1351 1352
	/* Look for a serialN alias */
	id = of_alias_get_id(pdev->dev.of_node, "serial");
	if (id < 0)
		id = 0;
1353

1354
	/* Initialize the port structure */
1355
	port = cdns_uart_get_port(id);
1356 1357 1358

	if (!port) {
		dev_err(&pdev->dev, "Cannot get uart_port structure\n");
1359
		rc = -ENODEV;
1360
		goto err_out_notif_unreg;
1361 1362 1363 1364 1365 1366 1367 1368
	} else {
		/* 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 = res2->start;
		port->dev = &pdev->dev;
1369 1370 1371
		port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
		port->private_data = cdns_uart_data;
		cdns_uart_data->port = port;
1372
		platform_set_drvdata(pdev, port);
1373
		rc = uart_add_one_port(&cdns_uart_uart_driver, port);
1374 1375 1376
		if (rc) {
			dev_err(&pdev->dev,
				"uart_add_one_port() failed; err=%i\n", rc);
1377
			goto err_out_notif_unreg;
1378 1379 1380
		}
		return 0;
	}
1381

1382
err_out_notif_unreg:
1383
#ifdef CONFIG_COMMON_CLK
1384 1385
	clk_notifier_unregister(cdns_uart_data->uartclk,
			&cdns_uart_data->clk_rate_change_nb);
1386
#endif
1387
err_out_clk_disable:
1388 1389 1390
	clk_disable_unprepare(cdns_uart_data->uartclk);
err_out_clk_dis_pclk:
	clk_disable_unprepare(cdns_uart_data->pclk);
1391 1392

	return rc;
1393 1394 1395
}

/**
1396
 * cdns_uart_remove - called when the platform driver is unregistered
1397 1398
 * @pdev: Pointer to the platform device structure
 *
S
Soren Brinkmann 已提交
1399
 * Return: 0 on success, negative errno otherwise
1400
 */
1401
static int cdns_uart_remove(struct platform_device *pdev)
1402
{
1403
	struct uart_port *port = platform_get_drvdata(pdev);
1404
	struct cdns_uart *cdns_uart_data = port->private_data;
1405
	int rc;
1406

1407
	/* Remove the cdns_uart port from the serial core */
1408
#ifdef CONFIG_COMMON_CLK
1409 1410
	clk_notifier_unregister(cdns_uart_data->uartclk,
			&cdns_uart_data->clk_rate_change_nb);
1411
#endif
1412
	rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
1413
	port->mapbase = 0;
1414 1415
	clk_disable_unprepare(cdns_uart_data->uartclk);
	clk_disable_unprepare(cdns_uart_data->pclk);
1416 1417 1418 1419
	return rc;
}

/* Match table for of_platform binding */
1420
static struct of_device_id cdns_uart_of_match[] = {
1421
	{ .compatible = "xlnx,xuartps", },
1422
	{ .compatible = "cdns,uart-r1p8", },
1423 1424
	{}
};
1425
MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1426

1427 1428 1429
static struct platform_driver cdns_uart_platform_driver = {
	.probe   = cdns_uart_probe,
	.remove  = cdns_uart_remove,
1430
	.driver  = {
1431 1432 1433
		.name = CDNS_UART_NAME,
		.of_match_table = cdns_uart_of_match,
		.pm = &cdns_uart_dev_pm_ops,
1434 1435 1436
		},
};

1437
static int __init cdns_uart_init(void)
1438 1439 1440
{
	int retval = 0;

1441 1442
	/* Register the cdns_uart driver with the serial core */
	retval = uart_register_driver(&cdns_uart_uart_driver);
1443 1444 1445 1446
	if (retval)
		return retval;

	/* Register the platform driver */
1447
	retval = platform_driver_register(&cdns_uart_platform_driver);
1448
	if (retval)
1449
		uart_unregister_driver(&cdns_uart_uart_driver);
1450 1451 1452 1453

	return retval;
}

1454
static void __exit cdns_uart_exit(void)
1455 1456
{
	/* Unregister the platform driver */
1457
	platform_driver_unregister(&cdns_uart_platform_driver);
1458

1459 1460
	/* Unregister the cdns_uart driver */
	uart_unregister_driver(&cdns_uart_uart_driver);
1461 1462
}

1463 1464
module_init(cdns_uart_init);
module_exit(cdns_uart_exit);
1465

1466
MODULE_DESCRIPTION("Driver for Cadence UART");
1467 1468
MODULE_AUTHOR("Xilinx Inc.");
MODULE_LICENSE("GPL");