Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2// Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
   3
   4/* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
   5#define __DISABLE_TRACE_MMIO__
   6
   7#include <linux/clk.h>
   8#include <linux/console.h>
   9#include <linux/io.h>
  10#include <linux/iopoll.h>
  11#include <linux/irq.h>
  12#include <linux/module.h>
  13#include <linux/of.h>
  14#include <linux/of_device.h>
  15#include <linux/pm_opp.h>
  16#include <linux/platform_device.h>
  17#include <linux/pm_runtime.h>
  18#include <linux/pm_wakeirq.h>
  19#include <linux/qcom-geni-se.h>
  20#include <linux/serial.h>
  21#include <linux/serial_core.h>
  22#include <linux/slab.h>
  23#include <linux/tty.h>
  24#include <linux/tty_flip.h>
  25#include <dt-bindings/interconnect/qcom,icc.h>
  26
  27/* UART specific GENI registers */
  28#define SE_UART_LOOPBACK_CFG		0x22c
  29#define SE_UART_IO_MACRO_CTRL		0x240
  30#define SE_UART_TX_TRANS_CFG		0x25c
  31#define SE_UART_TX_WORD_LEN		0x268
  32#define SE_UART_TX_STOP_BIT_LEN		0x26c
  33#define SE_UART_TX_TRANS_LEN		0x270
  34#define SE_UART_RX_TRANS_CFG		0x280
  35#define SE_UART_RX_WORD_LEN		0x28c
  36#define SE_UART_RX_STALE_CNT		0x294
  37#define SE_UART_TX_PARITY_CFG		0x2a4
  38#define SE_UART_RX_PARITY_CFG		0x2a8
  39#define SE_UART_MANUAL_RFR		0x2ac
  40
  41/* SE_UART_TRANS_CFG */
  42#define UART_TX_PAR_EN		BIT(0)
  43#define UART_CTS_MASK		BIT(1)
  44
  45/* SE_UART_TX_WORD_LEN */
  46#define TX_WORD_LEN_MSK		GENMASK(9, 0)
  47
  48/* SE_UART_TX_STOP_BIT_LEN */
  49#define TX_STOP_BIT_LEN_MSK	GENMASK(23, 0)
  50#define TX_STOP_BIT_LEN_1	0
  51#define TX_STOP_BIT_LEN_1_5	1
  52#define TX_STOP_BIT_LEN_2	2
  53
  54/* SE_UART_TX_TRANS_LEN */
  55#define TX_TRANS_LEN_MSK	GENMASK(23, 0)
  56
  57/* SE_UART_RX_TRANS_CFG */
  58#define UART_RX_INS_STATUS_BIT	BIT(2)
  59#define UART_RX_PAR_EN		BIT(3)
  60
  61/* SE_UART_RX_WORD_LEN */
  62#define RX_WORD_LEN_MASK	GENMASK(9, 0)
  63
  64/* SE_UART_RX_STALE_CNT */
  65#define RX_STALE_CNT		GENMASK(23, 0)
  66
  67/* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
  68#define PAR_CALC_EN		BIT(0)
  69#define PAR_MODE_MSK		GENMASK(2, 1)
  70#define PAR_MODE_SHFT		1
  71#define PAR_EVEN		0x00
  72#define PAR_ODD			0x01
  73#define PAR_SPACE		0x10
  74#define PAR_MARK		0x11
  75
  76/* SE_UART_MANUAL_RFR register fields */
  77#define UART_MANUAL_RFR_EN	BIT(31)
  78#define UART_RFR_NOT_READY	BIT(1)
  79#define UART_RFR_READY		BIT(0)
  80
  81/* UART M_CMD OP codes */
  82#define UART_START_TX		0x1
  83#define UART_START_BREAK	0x4
  84#define UART_STOP_BREAK		0x5
  85/* UART S_CMD OP codes */
  86#define UART_START_READ		0x1
  87#define UART_PARAM		0x1
  88
  89#define UART_OVERSAMPLING	32
  90#define STALE_TIMEOUT		16
  91#define DEFAULT_BITS_PER_CHAR	10
  92#define GENI_UART_CONS_PORTS	1
  93#define GENI_UART_PORTS		3
  94#define DEF_FIFO_DEPTH_WORDS	16
  95#define DEF_TX_WM		2
  96#define DEF_FIFO_WIDTH_BITS	32
  97#define UART_RX_WM		2
 
  98
  99/* SE_UART_LOOPBACK_CFG */
 100#define RX_TX_SORTED	BIT(0)
 101#define CTS_RTS_SORTED	BIT(1)
 102#define RX_TX_CTS_RTS_SORTED	(RX_TX_SORTED | CTS_RTS_SORTED)
 103
 104/* UART pin swap value */
 105#define DEFAULT_IO_MACRO_IO0_IO1_MASK		GENMASK(3, 0)
 106#define IO_MACRO_IO0_SEL		0x3
 107#define DEFAULT_IO_MACRO_IO2_IO3_MASK		GENMASK(15, 4)
 108#define IO_MACRO_IO2_IO3_SWAP		0x4640
 109
 110/* We always configure 4 bytes per FIFO word */
 111#define BYTES_PER_FIFO_WORD		4
 
 
 
 
 
 
 
 112
 113struct qcom_geni_private_data {
 114	/* NOTE: earlycon port will have NULL here */
 115	struct uart_driver *drv;
 116
 117	u32 poll_cached_bytes;
 118	unsigned int poll_cached_bytes_cnt;
 119
 120	u32 write_cached_bytes;
 121	unsigned int write_cached_bytes_cnt;
 122};
 123
 124struct qcom_geni_serial_port {
 125	struct uart_port uport;
 126	struct geni_se se;
 127	const char *name;
 128	u32 tx_fifo_depth;
 129	u32 tx_fifo_width;
 130	u32 rx_fifo_depth;
 
 
 131	bool setup;
 132	int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
 133	unsigned int baud;
 134	void *rx_fifo;
 135	u32 loopback;
 136	bool brk;
 137
 138	unsigned int tx_remaining;
 
 139	int wakeup_irq;
 140	bool rx_tx_swap;
 141	bool cts_rts_swap;
 142
 143	struct qcom_geni_private_data private_data;
 
 144};
 145
 146static const struct uart_ops qcom_geni_console_pops;
 147static const struct uart_ops qcom_geni_uart_pops;
 148static struct uart_driver qcom_geni_console_driver;
 149static struct uart_driver qcom_geni_uart_driver;
 150static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
 151static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
 152static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
 153static void qcom_geni_serial_stop_rx(struct uart_port *uport);
 154static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop);
 155
 156#define to_dev_port(ptr, member) \
 157		container_of(ptr, struct qcom_geni_serial_port, member)
 
 
 
 
 
 
 158
 159static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
 160	[0] = {
 161		.uport = {
 162				.iotype = UPIO_MEM,
 163				.ops = &qcom_geni_uart_pops,
 164				.flags = UPF_BOOT_AUTOCONF,
 165				.line = 0,
 166		},
 167	},
 168	[1] = {
 169		.uport = {
 170				.iotype = UPIO_MEM,
 171				.ops = &qcom_geni_uart_pops,
 172				.flags = UPF_BOOT_AUTOCONF,
 173				.line = 1,
 174		},
 175	},
 176	[2] = {
 177		.uport = {
 178				.iotype = UPIO_MEM,
 179				.ops = &qcom_geni_uart_pops,
 180				.flags = UPF_BOOT_AUTOCONF,
 181				.line = 2,
 182		},
 183	},
 184};
 185
 186static struct qcom_geni_serial_port qcom_geni_console_port = {
 187	.uport = {
 188		.iotype = UPIO_MEM,
 189		.ops = &qcom_geni_console_pops,
 190		.flags = UPF_BOOT_AUTOCONF,
 191		.line = 0,
 192	},
 193};
 194
 195static int qcom_geni_serial_request_port(struct uart_port *uport)
 196{
 197	struct platform_device *pdev = to_platform_device(uport->dev);
 198	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 199
 200	uport->membase = devm_platform_ioremap_resource(pdev, 0);
 201	if (IS_ERR(uport->membase))
 202		return PTR_ERR(uport->membase);
 203	port->se.base = uport->membase;
 204	return 0;
 205}
 206
 207static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
 208{
 209	if (cfg_flags & UART_CONFIG_TYPE) {
 210		uport->type = PORT_MSM;
 211		qcom_geni_serial_request_port(uport);
 212	}
 213}
 214
 215static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
 216{
 217	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
 218	u32 geni_ios;
 219
 220	if (uart_console(uport)) {
 221		mctrl |= TIOCM_CTS;
 222	} else {
 223		geni_ios = readl(uport->membase + SE_GENI_IOS);
 224		if (!(geni_ios & IO2_DATA_IN))
 225			mctrl |= TIOCM_CTS;
 226	}
 227
 228	return mctrl;
 229}
 230
 231static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
 232							unsigned int mctrl)
 233{
 234	u32 uart_manual_rfr = 0;
 235	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 236
 237	if (uart_console(uport))
 238		return;
 239
 240	if (mctrl & TIOCM_LOOP)
 241		port->loopback = RX_TX_CTS_RTS_SORTED;
 242
 243	if (!(mctrl & TIOCM_RTS) && !uport->suspended)
 244		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
 245	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
 246}
 247
 248static const char *qcom_geni_serial_get_type(struct uart_port *uport)
 249{
 250	return "MSM";
 251}
 252
 253static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
 254{
 255	struct qcom_geni_serial_port *port;
 256	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
 257
 258	if (line < 0 || line >= nr_ports)
 259		return ERR_PTR(-ENXIO);
 260
 261	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
 262	return port;
 263}
 264
 265static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
 266				int offset, int field, bool set)
 
 
 
 
 
 
 
 
 
 
 267{
 268	u32 reg;
 269	struct qcom_geni_serial_port *port;
 270	unsigned int baud;
 271	unsigned int fifo_bits;
 272	unsigned long timeout_us = 20000;
 273	struct qcom_geni_private_data *private_data = uport->private_data;
 274
 275	if (private_data->drv) {
 276		port = to_dev_port(uport, uport);
 277		baud = port->baud;
 278		if (!baud)
 279			baud = 115200;
 280		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
 281		/*
 282		 * Total polling iterations based on FIFO worth of bytes to be
 283		 * sent at current baud. Add a little fluff to the wait.
 284		 */
 285		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
 286	}
 287
 288	/*
 289	 * Use custom implementation instead of readl_poll_atomic since ktimer
 290	 * is not ready at the time of early console.
 291	 */
 292	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
 293	while (timeout_us) {
 294		reg = readl(uport->membase + offset);
 295		if ((bool)(reg & field) == set)
 296			return true;
 297		udelay(10);
 298		timeout_us -= 10;
 299	}
 300	return false;
 301}
 302
 
 
 
 
 
 
 303static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
 304{
 305	u32 m_cmd;
 306
 307	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
 308	m_cmd = UART_START_TX << M_OPCODE_SHFT;
 309	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
 310}
 311
 312static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
 313{
 314	int done;
 315	u32 irq_clear = M_CMD_DONE_EN;
 316
 317	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 318						M_CMD_DONE_EN, true);
 319	if (!done) {
 320		writel(M_GENI_CMD_ABORT, uport->membase +
 321						SE_GENI_M_CMD_CTRL_REG);
 322		irq_clear |= M_CMD_ABORT_EN;
 323		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 324							M_CMD_ABORT_EN, true);
 
 325	}
 326	writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
 327}
 328
 329static void qcom_geni_serial_abort_rx(struct uart_port *uport)
 330{
 331	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
 332
 333	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
 334	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
 335					S_GENI_CMD_ABORT, false);
 336	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
 337	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
 338}
 339
 340#ifdef CONFIG_CONSOLE_POLL
 341
 342static int qcom_geni_serial_get_char(struct uart_port *uport)
 343{
 344	struct qcom_geni_private_data *private_data = uport->private_data;
 345	u32 status;
 346	u32 word_cnt;
 347	int ret;
 348
 349	if (!private_data->poll_cached_bytes_cnt) {
 350		status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
 351		writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
 352
 353		status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
 354		writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
 355
 356		status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
 357		word_cnt = status & RX_FIFO_WC_MSK;
 358		if (!word_cnt)
 359			return NO_POLL_CHAR;
 360
 361		if (word_cnt == 1 && (status & RX_LAST))
 362			/*
 363			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
 364			 * treated as if it was BYTES_PER_FIFO_WORD.
 365			 */
 366			private_data->poll_cached_bytes_cnt =
 367				(status & RX_LAST_BYTE_VALID_MSK) >>
 368				RX_LAST_BYTE_VALID_SHFT;
 369
 370		if (private_data->poll_cached_bytes_cnt == 0)
 371			private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
 372
 373		private_data->poll_cached_bytes =
 374			readl(uport->membase + SE_GENI_RX_FIFOn);
 375	}
 376
 377	private_data->poll_cached_bytes_cnt--;
 378	ret = private_data->poll_cached_bytes & 0xff;
 379	private_data->poll_cached_bytes >>= 8;
 380
 381	return ret;
 382}
 383
 384static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
 385							unsigned char c)
 386{
 387	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
 
 
 
 
 
 388	qcom_geni_serial_setup_tx(uport, 1);
 389	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 390						M_TX_FIFO_WATERMARK_EN, true));
 391	writel(c, uport->membase + SE_GENI_TX_FIFOn);
 392	writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 393	qcom_geni_serial_poll_tx_done(uport);
 394}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 395#endif
 396
 397#ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
 
 
 
 
 
 
 
 
 398static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
 399{
 400	struct qcom_geni_private_data *private_data = uport->private_data;
 401
 402	private_data->write_cached_bytes =
 403		(private_data->write_cached_bytes >> 8) | (ch << 24);
 404	private_data->write_cached_bytes_cnt++;
 405
 406	if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
 407		writel(private_data->write_cached_bytes,
 408		       uport->membase + SE_GENI_TX_FIFOn);
 409		private_data->write_cached_bytes_cnt = 0;
 410	}
 411}
 412
 413static void
 414__qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
 415				 unsigned int count)
 416{
 417	struct qcom_geni_private_data *private_data = uport->private_data;
 418
 419	int i;
 420	u32 bytes_to_send = count;
 421
 422	for (i = 0; i < count; i++) {
 423		/*
 424		 * uart_console_write() adds a carriage return for each newline.
 425		 * Account for additional bytes to be written.
 426		 */
 427		if (s[i] == '\n')
 428			bytes_to_send++;
 429	}
 430
 431	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
 
 432	qcom_geni_serial_setup_tx(uport, bytes_to_send);
 433	for (i = 0; i < count; ) {
 434		size_t chars_to_write = 0;
 435		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
 436
 437		/*
 438		 * If the WM bit never set, then the Tx state machine is not
 439		 * in a valid state, so break, cancel/abort any existing
 440		 * command. Unfortunately the current data being written is
 441		 * lost.
 442		 */
 443		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 444						M_TX_FIFO_WATERMARK_EN, true))
 445			break;
 446		chars_to_write = min_t(size_t, count - i, avail / 2);
 447		uart_console_write(uport, s + i, chars_to_write,
 448						qcom_geni_serial_wr_char);
 449		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
 450							SE_GENI_M_IRQ_CLEAR);
 451		i += chars_to_write;
 452	}
 453
 454	if (private_data->write_cached_bytes_cnt) {
 455		private_data->write_cached_bytes >>= BITS_PER_BYTE *
 456			(BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
 457		writel(private_data->write_cached_bytes,
 458		       uport->membase + SE_GENI_TX_FIFOn);
 459		private_data->write_cached_bytes_cnt = 0;
 460	}
 461
 462	qcom_geni_serial_poll_tx_done(uport);
 463}
 464
 465static void qcom_geni_serial_console_write(struct console *co, const char *s,
 466			      unsigned int count)
 467{
 468	struct uart_port *uport;
 469	struct qcom_geni_serial_port *port;
 
 470	bool locked = true;
 471	unsigned long flags;
 472	u32 geni_status;
 473	u32 irq_en;
 474
 475	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
 476
 477	port = get_port_from_line(co->index, true);
 478	if (IS_ERR(port))
 479		return;
 480
 481	uport = &port->uport;
 482	if (oops_in_progress)
 483		locked = spin_trylock_irqsave(&uport->lock, flags);
 484	else
 485		spin_lock_irqsave(&uport->lock, flags);
 486
 487	geni_status = readl(uport->membase + SE_GENI_STATUS);
 488
 489	/* Cancel the current write to log the fault */
 490	if (!locked) {
 491		geni_se_cancel_m_cmd(&port->se);
 492		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 493						M_CMD_CANCEL_EN, true)) {
 494			geni_se_abort_m_cmd(&port->se);
 495			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 496							M_CMD_ABORT_EN, true);
 497			writel(M_CMD_ABORT_EN, uport->membase +
 498							SE_GENI_M_IRQ_CLEAR);
 499		}
 500		writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 501	} else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
 502		/*
 503		 * It seems we can't interrupt existing transfers if all data
 504		 * has been sent, in which case we need to look for done first.
 505		 */
 506		qcom_geni_serial_poll_tx_done(uport);
 507
 508		if (!uart_circ_empty(&uport->state->xmit)) {
 509			irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 510			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
 511					uport->membase + SE_GENI_M_IRQ_EN);
 512		}
 513	}
 514
 515	__qcom_geni_serial_console_write(uport, s, count);
 516
 517	if (port->tx_remaining)
 518		qcom_geni_serial_setup_tx(uport, port->tx_remaining);
 519
 520	if (locked)
 521		spin_unlock_irqrestore(&uport->lock, flags);
 522}
 523
 524static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
 525{
 526	u32 i;
 527	unsigned char buf[sizeof(u32)];
 528	struct tty_port *tport;
 529	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 530
 531	tport = &uport->state->port;
 532	for (i = 0; i < bytes; ) {
 533		int c;
 534		int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
 535
 536		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
 537		i += chunk;
 538		if (drop)
 539			continue;
 540
 541		for (c = 0; c < chunk; c++) {
 542			int sysrq;
 543
 544			uport->icount.rx++;
 545			if (port->brk && buf[c] == 0) {
 546				port->brk = false;
 547				if (uart_handle_break(uport))
 548					continue;
 549			}
 550
 551			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
 552
 553			if (!sysrq)
 554				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
 555		}
 556	}
 557	if (!drop)
 558		tty_flip_buffer_push(tport);
 559	return 0;
 560}
 561#else
 562static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
 563{
 564	return -EPERM;
 565}
 566
 
 567#endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
 568
 569static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
 570{
 571	struct tty_port *tport;
 572	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 573	u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
 574	u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
 575	int ret;
 576
 577	tport = &uport->state->port;
 578	ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
 579	if (drop)
 580		return 0;
 581
 582	ret = tty_insert_flip_string(tport, port->rx_fifo, bytes);
 583	if (ret != bytes) {
 584		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
 585				__func__, ret, bytes);
 586		WARN_ON_ONCE(1);
 587	}
 588	uport->icount.rx += ret;
 589	tty_flip_buffer_push(tport);
 590	return ret;
 591}
 592
 593static void qcom_geni_serial_start_tx(struct uart_port *uport)
 594{
 595	u32 irq_en;
 596	u32 status;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 597
 598	status = readl(uport->membase + SE_GENI_STATUS);
 599	if (status & M_GENI_CMD_ACTIVE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 600		return;
 601
 602	if (!qcom_geni_serial_tx_empty(uport))
 
 
 
 
 
 
 
 
 
 603		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 604
 605	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
 606	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
 607
 608	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
 609	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
 610}
 611
 612static void qcom_geni_serial_stop_tx(struct uart_port *uport)
 613{
 614	u32 irq_en;
 615	u32 status;
 616	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 617
 618	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 619	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
 620	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
 621	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
 622	status = readl(uport->membase + SE_GENI_STATUS);
 623	/* Possible stop tx is called multiple times. */
 624	if (!(status & M_GENI_CMD_ACTIVE))
 625		return;
 
 626
 627	geni_se_cancel_m_cmd(&port->se);
 628	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 629						M_CMD_CANCEL_EN, true)) {
 630		geni_se_abort_m_cmd(&port->se);
 631		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 632						M_CMD_ABORT_EN, true);
 633		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 634	}
 635	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 636}
 637
 638static void qcom_geni_serial_start_rx(struct uart_port *uport)
 639{
 640	u32 irq_en;
 641	u32 status;
 642	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 
 643
 644	status = readl(uport->membase + SE_GENI_STATUS);
 645	if (status & S_GENI_CMD_ACTIVE)
 646		qcom_geni_serial_stop_rx(uport);
 647
 648	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
 
 
 649
 650	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
 651	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
 652	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
 
 
 
 
 653
 654	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 655	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
 656	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
 
 
 
 
 
 
 
 
 
 
 
 657}
 658
 659static void qcom_geni_serial_stop_rx(struct uart_port *uport)
 660{
 661	u32 irq_en;
 662	u32 status;
 663	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 664	u32 s_irq_status;
 665
 666	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
 667	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
 668	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
 669
 670	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 671	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
 672	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
 673
 674	status = readl(uport->membase + SE_GENI_STATUS);
 675	/* Possible stop rx is called multiple times. */
 676	if (!(status & S_GENI_CMD_ACTIVE))
 677		return;
 678
 679	geni_se_cancel_s_cmd(&port->se);
 680	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
 681					S_CMD_CANCEL_EN, true);
 682	/*
 683	 * If timeout occurs secondary engine remains active
 684	 * and Abort sequence is executed.
 685	 */
 686	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
 687	/* Flush the Rx buffer */
 688	if (s_irq_status & S_RX_FIFO_LAST_EN)
 689		qcom_geni_serial_handle_rx(uport, true);
 690	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
 691
 692	status = readl(uport->membase + SE_GENI_STATUS);
 693	if (status & S_GENI_CMD_ACTIVE)
 694		qcom_geni_serial_abort_rx(uport);
 695}
 696
 697static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
 698{
 699	u32 status;
 700	u32 word_cnt;
 701	u32 last_word_byte_cnt;
 702	u32 last_word_partial;
 703	u32 total_bytes;
 704	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 705
 706	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
 707	word_cnt = status & RX_FIFO_WC_MSK;
 708	last_word_partial = status & RX_LAST;
 709	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
 710						RX_LAST_BYTE_VALID_SHFT;
 711
 712	if (!word_cnt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 713		return;
 714	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
 715	if (last_word_partial && last_word_byte_cnt)
 716		total_bytes += last_word_byte_cnt;
 717	else
 718		total_bytes += BYTES_PER_FIFO_WORD;
 719	port->handle_rx(uport, total_bytes, drop);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 720}
 721
 722static void qcom_geni_serial_handle_tx(struct uart_port *uport, bool done,
 723		bool active)
 724{
 725	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 726	struct circ_buf *xmit = &uport->state->xmit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 727	size_t avail;
 728	size_t remaining;
 729	size_t pending;
 730	int i;
 731	u32 status;
 732	u32 irq_en;
 733	unsigned int chunk;
 734	int tail;
 735
 736	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
 737
 738	/* Complete the current tx command before taking newly added data */
 739	if (active)
 740		pending = port->tx_remaining;
 741	else
 742		pending = uart_circ_chars_pending(xmit);
 743
 744	/* All data has been transmitted and acknowledged as received */
 745	if (!pending && !status && done) {
 746		qcom_geni_serial_stop_tx(uport);
 747		goto out_write_wakeup;
 748	}
 749
 750	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
 
 
 
 
 751	avail *= BYTES_PER_FIFO_WORD;
 752
 753	tail = xmit->tail;
 754	chunk = min(avail, pending);
 755	if (!chunk)
 756		goto out_write_wakeup;
 757
 758	if (!port->tx_remaining) {
 759		qcom_geni_serial_setup_tx(uport, pending);
 760		port->tx_remaining = pending;
 
 761
 762		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 763		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
 764			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
 765					uport->membase + SE_GENI_M_IRQ_EN);
 766	}
 767
 768	remaining = chunk;
 769	for (i = 0; i < chunk; ) {
 770		unsigned int tx_bytes;
 771		u8 buf[sizeof(u32)];
 772		int c;
 773
 774		memset(buf, 0, sizeof(buf));
 775		tx_bytes = min_t(size_t, remaining, BYTES_PER_FIFO_WORD);
 776
 777		for (c = 0; c < tx_bytes ; c++) {
 778			buf[c] = xmit->buf[tail++];
 779			tail &= UART_XMIT_SIZE - 1;
 780		}
 781
 782		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
 783
 784		i += tx_bytes;
 785		uport->icount.tx += tx_bytes;
 786		remaining -= tx_bytes;
 787		port->tx_remaining -= tx_bytes;
 788	}
 789
 790	xmit->tail = tail;
 791
 792	/*
 793	 * The tx fifo watermark is level triggered and latched. Though we had
 794	 * cleared it in qcom_geni_serial_isr it will have already reasserted
 795	 * so we must clear it again here after our writes.
 796	 */
 797	writel(M_TX_FIFO_WATERMARK_EN,
 798			uport->membase + SE_GENI_M_IRQ_CLEAR);
 799
 800out_write_wakeup:
 801	if (!port->tx_remaining) {
 802		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 803		if (irq_en & M_TX_FIFO_WATERMARK_EN)
 804			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
 805					uport->membase + SE_GENI_M_IRQ_EN);
 806	}
 807
 808	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 809		uart_write_wakeup(uport);
 810}
 811
 812static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
 813{
 814	u32 m_irq_en;
 815	u32 m_irq_status;
 816	u32 s_irq_status;
 817	u32 geni_status;
 
 
 
 818	struct uart_port *uport = dev;
 819	bool drop_rx = false;
 820	struct tty_port *tport = &uport->state->port;
 821	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 822
 823	if (uport->suspended)
 824		return IRQ_NONE;
 825
 826	spin_lock(&uport->lock);
 827
 828	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
 829	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
 
 
 830	geni_status = readl(uport->membase + SE_GENI_STATUS);
 
 831	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 832	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
 833	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
 
 
 834
 835	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
 836		goto out_unlock;
 837
 838	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
 839		uport->icount.overrun++;
 840		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
 841	}
 842
 843	if (m_irq_status & m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
 844		qcom_geni_serial_handle_tx(uport, m_irq_status & M_CMD_DONE_EN,
 845					geni_status & M_GENI_CMD_ACTIVE);
 846
 847	if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
 848		if (s_irq_status & S_GP_IRQ_0_EN)
 849			uport->icount.parity++;
 850		drop_rx = true;
 851	} else if (s_irq_status & S_GP_IRQ_2_EN ||
 852					s_irq_status & S_GP_IRQ_3_EN) {
 853		uport->icount.brk++;
 854		port->brk = true;
 855	}
 856
 857	if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
 858					s_irq_status & S_RX_FIFO_LAST_EN)
 859		qcom_geni_serial_handle_rx(uport, drop_rx);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 860
 861out_unlock:
 862	uart_unlock_and_check_sysrq(uport);
 863
 864	return IRQ_HANDLED;
 865}
 866
 867static int setup_fifos(struct qcom_geni_serial_port *port)
 868{
 869	struct uart_port *uport;
 870	u32 old_rx_fifo_depth = port->rx_fifo_depth;
 871
 872	uport = &port->uport;
 873	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
 874	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
 875	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
 876	uport->fifosize =
 877		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
 878
 879	if (port->rx_fifo && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
 880		port->rx_fifo = devm_krealloc(uport->dev, port->rx_fifo,
 881					      port->rx_fifo_depth * sizeof(u32),
 882					      GFP_KERNEL);
 883		if (!port->rx_fifo)
 
 
 
 
 
 884			return -ENOMEM;
 885	}
 886
 887	return 0;
 888}
 889
 890
 891static void qcom_geni_serial_shutdown(struct uart_port *uport)
 892{
 893	disable_irq(uport->irq);
 
 
 
 
 
 
 
 
 
 
 
 
 894}
 895
 896static int qcom_geni_serial_port_setup(struct uart_port *uport)
 897{
 898	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 899	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
 900	u32 proto;
 901	u32 pin_swap;
 902	int ret;
 903
 904	proto = geni_se_read_proto(&port->se);
 905	if (proto != GENI_SE_UART) {
 906		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
 907		return -ENXIO;
 908	}
 909
 910	qcom_geni_serial_stop_rx(uport);
 911
 912	ret = setup_fifos(port);
 913	if (ret)
 914		return ret;
 915
 916	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
 917
 918	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
 919	if (port->rx_tx_swap) {
 920		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
 921		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
 922	}
 923	if (port->cts_rts_swap) {
 924		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
 925		pin_swap |= IO_MACRO_IO0_SEL;
 926	}
 927	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
 928	if (port->rx_tx_swap || port->cts_rts_swap)
 929		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
 930
 931	/*
 932	 * Make an unconditional cancel on the main sequencer to reset
 933	 * it else we could end up in data loss scenarios.
 934	 */
 935	if (uart_console(uport))
 936		qcom_geni_serial_poll_tx_done(uport);
 937	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
 938			       false, true, true);
 939	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
 940	geni_se_select_mode(&port->se, GENI_SE_FIFO);
 941	qcom_geni_serial_start_rx(uport);
 942	port->setup = true;
 943
 944	return 0;
 945}
 946
 947static int qcom_geni_serial_startup(struct uart_port *uport)
 948{
 949	int ret;
 950	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
 951
 952	if (!port->setup) {
 953		ret = qcom_geni_serial_port_setup(uport);
 954		if (ret)
 955			return ret;
 956	}
 
 
 
 
 
 957	enable_irq(uport->irq);
 958
 959	return 0;
 960}
 961
 962static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
 963			unsigned int *clk_div, unsigned int percent_tol)
 964{
 965	unsigned long freq;
 966	unsigned long div, maxdiv;
 967	u64 mult;
 968	unsigned long offset, abs_tol, achieved;
 969
 970	abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
 971	maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
 972	div = 1;
 973	while (div <= maxdiv) {
 974		mult = (u64)div * desired_clk;
 975		if (mult != (unsigned long)mult)
 976			break;
 977
 978		offset = div * abs_tol;
 979		freq = clk_round_rate(clk, mult - offset);
 980
 981		/* Can only get lower if we're done */
 982		if (freq < mult - offset)
 983			break;
 984
 985		/*
 986		 * Re-calculate div in case rounding skipped rates but we
 987		 * ended up at a good one, then check for a match.
 988		 */
 989		div = DIV_ROUND_CLOSEST(freq, desired_clk);
 990		achieved = DIV_ROUND_CLOSEST(freq, div);
 991		if (achieved <= desired_clk + abs_tol &&
 992		    achieved >= desired_clk - abs_tol) {
 993			*clk_div = div;
 994			return freq;
 995		}
 996
 997		div = DIV_ROUND_UP(freq, desired_clk);
 998	}
 999
1000	return 0;
1001}
1002
1003static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
1004			unsigned int sampling_rate, unsigned int *clk_div)
1005{
1006	unsigned long ser_clk;
1007	unsigned long desired_clk;
1008
1009	desired_clk = baud * sampling_rate;
1010	if (!desired_clk)
1011		return 0;
1012
1013	/*
1014	 * try to find a clock rate within 2% tolerance, then within 5%
1015	 */
1016	ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1017	if (!ser_clk)
1018		ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1019
1020	return ser_clk;
1021}
1022
1023static void qcom_geni_serial_set_termios(struct uart_port *uport,
1024					 struct ktermios *termios,
1025					 const struct ktermios *old)
1026{
1027	unsigned int baud;
1028	u32 bits_per_char;
1029	u32 tx_trans_cfg;
1030	u32 tx_parity_cfg;
1031	u32 rx_trans_cfg;
1032	u32 rx_parity_cfg;
1033	u32 stop_bit_len;
1034	unsigned int clk_div;
1035	u32 ser_clk_cfg;
1036	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1037	unsigned long clk_rate;
1038	u32 ver, sampling_rate;
1039	unsigned int avg_bw_core;
 
1040
1041	qcom_geni_serial_stop_rx(uport);
1042	/* baud rate */
1043	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1044	port->baud = baud;
1045
1046	sampling_rate = UART_OVERSAMPLING;
1047	/* Sampling rate is halved for IP versions >= 2.5 */
1048	ver = geni_se_get_qup_hw_version(&port->se);
1049	if (ver >= QUP_SE_VERSION_2_5)
1050		sampling_rate /= 2;
1051
1052	clk_rate = get_clk_div_rate(port->se.clk, baud,
1053		sampling_rate, &clk_div);
1054	if (!clk_rate) {
1055		dev_err(port->se.dev,
1056			"Couldn't find suitable clock rate for %u\n",
1057			baud * sampling_rate);
1058		goto out_restart_rx;
1059	}
1060
1061	dev_dbg(port->se.dev, "desired_rate-%u, clk_rate-%lu, clk_div-%u\n",
1062			baud * sampling_rate, clk_rate, clk_div);
1063
1064	uport->uartclk = clk_rate;
 
1065	dev_pm_opp_set_rate(uport->dev, clk_rate);
1066	ser_clk_cfg = SER_CLK_EN;
1067	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1068
1069	/*
1070	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
1071	 * only.
1072	 */
1073	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
1074						: GENI_DEFAULT_BW;
1075	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
1076	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
1077	geni_icc_set_bw(&port->se);
1078
1079	/* parity */
1080	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
1081	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
1082	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
1083	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1084	if (termios->c_cflag & PARENB) {
1085		tx_trans_cfg |= UART_TX_PAR_EN;
1086		rx_trans_cfg |= UART_RX_PAR_EN;
1087		tx_parity_cfg |= PAR_CALC_EN;
1088		rx_parity_cfg |= PAR_CALC_EN;
1089		if (termios->c_cflag & PARODD) {
1090			tx_parity_cfg |= PAR_ODD;
1091			rx_parity_cfg |= PAR_ODD;
1092		} else if (termios->c_cflag & CMSPAR) {
1093			tx_parity_cfg |= PAR_SPACE;
1094			rx_parity_cfg |= PAR_SPACE;
1095		} else {
1096			tx_parity_cfg |= PAR_EVEN;
1097			rx_parity_cfg |= PAR_EVEN;
1098		}
1099	} else {
1100		tx_trans_cfg &= ~UART_TX_PAR_EN;
1101		rx_trans_cfg &= ~UART_RX_PAR_EN;
1102		tx_parity_cfg &= ~PAR_CALC_EN;
1103		rx_parity_cfg &= ~PAR_CALC_EN;
1104	}
1105
1106	/* bits per char */
1107	bits_per_char = tty_get_char_size(termios->c_cflag);
1108
1109	/* stop bits */
1110	if (termios->c_cflag & CSTOPB)
1111		stop_bit_len = TX_STOP_BIT_LEN_2;
1112	else
1113		stop_bit_len = TX_STOP_BIT_LEN_1;
1114
1115	/* flow control, clear the CTS_MASK bit if using flow control. */
1116	if (termios->c_cflag & CRTSCTS)
1117		tx_trans_cfg &= ~UART_CTS_MASK;
1118	else
1119		tx_trans_cfg |= UART_CTS_MASK;
1120
1121	if (baud)
1122		uart_update_timeout(uport, termios->c_cflag, baud);
1123
 
 
 
 
 
 
 
 
 
 
 
 
1124	if (!uart_console(uport))
1125		writel(port->loopback,
1126				uport->membase + SE_UART_LOOPBACK_CFG);
1127	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1128	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1129	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1130	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1131	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1132	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1133	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1134	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1135	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1136out_restart_rx:
1137	qcom_geni_serial_start_rx(uport);
1138}
1139
1140static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1141{
1142	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1143}
1144
1145#ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1146static int qcom_geni_console_setup(struct console *co, char *options)
1147{
1148	struct uart_port *uport;
1149	struct qcom_geni_serial_port *port;
1150	int baud = 115200;
1151	int bits = 8;
1152	int parity = 'n';
1153	int flow = 'n';
1154	int ret;
1155
1156	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1157		return -ENXIO;
1158
1159	port = get_port_from_line(co->index, true);
1160	if (IS_ERR(port)) {
1161		pr_err("Invalid line %d\n", co->index);
1162		return PTR_ERR(port);
1163	}
1164
1165	uport = &port->uport;
1166
1167	if (unlikely(!uport->membase))
1168		return -ENXIO;
1169
1170	if (!port->setup) {
1171		ret = qcom_geni_serial_port_setup(uport);
1172		if (ret)
1173			return ret;
1174	}
1175
1176	if (options)
1177		uart_parse_options(options, &baud, &parity, &bits, &flow);
1178
1179	return uart_set_options(uport, co, baud, parity, bits, flow);
1180}
1181
1182static void qcom_geni_serial_earlycon_write(struct console *con,
1183					const char *s, unsigned int n)
1184{
1185	struct earlycon_device *dev = con->data;
1186
1187	__qcom_geni_serial_console_write(&dev->port, s, n);
1188}
1189
1190#ifdef CONFIG_CONSOLE_POLL
1191static int qcom_geni_serial_earlycon_read(struct console *con,
1192					  char *s, unsigned int n)
1193{
1194	struct earlycon_device *dev = con->data;
1195	struct uart_port *uport = &dev->port;
1196	int num_read = 0;
1197	int ch;
1198
1199	while (num_read < n) {
1200		ch = qcom_geni_serial_get_char(uport);
1201		if (ch == NO_POLL_CHAR)
1202			break;
1203		s[num_read++] = ch;
1204	}
1205
1206	return num_read;
1207}
1208
1209static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1210						      struct console *con)
1211{
1212	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1213	con->read = qcom_geni_serial_earlycon_read;
1214}
1215#else
1216static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1217						      struct console *con) { }
1218#endif
1219
1220static struct qcom_geni_private_data earlycon_private_data;
1221
1222static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
1223								const char *opt)
1224{
1225	struct uart_port *uport = &dev->port;
1226	u32 tx_trans_cfg;
1227	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
1228	u32 rx_trans_cfg = 0;
1229	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
1230	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
1231	u32 bits_per_char;
1232	struct geni_se se;
1233
1234	if (!uport->membase)
1235		return -EINVAL;
1236
1237	uport->private_data = &earlycon_private_data;
1238
1239	memset(&se, 0, sizeof(se));
1240	se.base = uport->membase;
1241	if (geni_se_read_proto(&se) != GENI_SE_UART)
1242		return -ENXIO;
1243	/*
1244	 * Ignore Flow control.
1245	 * n = 8.
1246	 */
1247	tx_trans_cfg = UART_CTS_MASK;
1248	bits_per_char = BITS_PER_BYTE;
1249
1250	/*
1251	 * Make an unconditional cancel on the main sequencer to reset
1252	 * it else we could end up in data loss scenarios.
1253	 */
1254	qcom_geni_serial_poll_tx_done(uport);
1255	qcom_geni_serial_abort_rx(uport);
1256	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1257			       false, true, true);
1258	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
1259	geni_se_select_mode(&se, GENI_SE_FIFO);
1260
1261	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1262	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1263	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1264	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1265	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1266	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1267	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1268
1269	dev->con->write = qcom_geni_serial_earlycon_write;
1270	dev->con->setup = NULL;
1271	qcom_geni_serial_enable_early_read(&se, dev->con);
1272
1273	return 0;
1274}
1275OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
1276				qcom_geni_serial_earlycon_setup);
1277
1278static int __init console_register(struct uart_driver *drv)
1279{
1280	return uart_register_driver(drv);
1281}
1282
1283static void console_unregister(struct uart_driver *drv)
1284{
1285	uart_unregister_driver(drv);
1286}
1287
1288static struct console cons_ops = {
1289	.name = "ttyMSM",
1290	.write = qcom_geni_serial_console_write,
1291	.device = uart_console_device,
1292	.setup = qcom_geni_console_setup,
1293	.flags = CON_PRINTBUFFER,
1294	.index = -1,
1295	.data = &qcom_geni_console_driver,
1296};
1297
1298static struct uart_driver qcom_geni_console_driver = {
1299	.owner = THIS_MODULE,
1300	.driver_name = "qcom_geni_console",
1301	.dev_name = "ttyMSM",
1302	.nr =  GENI_UART_CONS_PORTS,
1303	.cons = &cons_ops,
1304};
1305#else
1306static int console_register(struct uart_driver *drv)
1307{
1308	return 0;
1309}
1310
1311static void console_unregister(struct uart_driver *drv)
1312{
1313}
1314#endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1315
1316static struct uart_driver qcom_geni_uart_driver = {
1317	.owner = THIS_MODULE,
1318	.driver_name = "qcom_geni_uart",
1319	.dev_name = "ttyHS",
1320	.nr =  GENI_UART_PORTS,
1321};
1322
1323static void qcom_geni_serial_pm(struct uart_port *uport,
1324		unsigned int new_state, unsigned int old_state)
1325{
1326	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1327
1328	/* If we've never been called, treat it as off */
1329	if (old_state == UART_PM_STATE_UNDEFINED)
1330		old_state = UART_PM_STATE_OFF;
1331
1332	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
1333		geni_icc_enable(&port->se);
 
 
1334		geni_se_resources_on(&port->se);
1335	} else if (new_state == UART_PM_STATE_OFF &&
1336			old_state == UART_PM_STATE_ON) {
1337		geni_se_resources_off(&port->se);
 
1338		geni_icc_disable(&port->se);
1339	}
1340}
1341
1342static const struct uart_ops qcom_geni_console_pops = {
1343	.tx_empty = qcom_geni_serial_tx_empty,
1344	.stop_tx = qcom_geni_serial_stop_tx,
1345	.start_tx = qcom_geni_serial_start_tx,
1346	.stop_rx = qcom_geni_serial_stop_rx,
1347	.start_rx = qcom_geni_serial_start_rx,
1348	.set_termios = qcom_geni_serial_set_termios,
1349	.startup = qcom_geni_serial_startup,
1350	.request_port = qcom_geni_serial_request_port,
1351	.config_port = qcom_geni_serial_config_port,
1352	.shutdown = qcom_geni_serial_shutdown,
 
1353	.type = qcom_geni_serial_get_type,
1354	.set_mctrl = qcom_geni_serial_set_mctrl,
1355	.get_mctrl = qcom_geni_serial_get_mctrl,
1356#ifdef CONFIG_CONSOLE_POLL
1357	.poll_get_char	= qcom_geni_serial_get_char,
1358	.poll_put_char	= qcom_geni_serial_poll_put_char,
 
1359#endif
1360	.pm = qcom_geni_serial_pm,
1361};
1362
1363static const struct uart_ops qcom_geni_uart_pops = {
1364	.tx_empty = qcom_geni_serial_tx_empty,
1365	.stop_tx = qcom_geni_serial_stop_tx,
1366	.start_tx = qcom_geni_serial_start_tx,
1367	.stop_rx = qcom_geni_serial_stop_rx,
 
1368	.set_termios = qcom_geni_serial_set_termios,
1369	.startup = qcom_geni_serial_startup,
1370	.request_port = qcom_geni_serial_request_port,
1371	.config_port = qcom_geni_serial_config_port,
1372	.shutdown = qcom_geni_serial_shutdown,
1373	.type = qcom_geni_serial_get_type,
1374	.set_mctrl = qcom_geni_serial_set_mctrl,
1375	.get_mctrl = qcom_geni_serial_get_mctrl,
1376	.pm = qcom_geni_serial_pm,
1377};
1378
1379static int qcom_geni_serial_probe(struct platform_device *pdev)
1380{
1381	int ret = 0;
1382	int line;
1383	struct qcom_geni_serial_port *port;
1384	struct uart_port *uport;
1385	struct resource *res;
1386	int irq;
1387	bool console = false;
1388	struct uart_driver *drv;
 
1389
1390	if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
1391		console = true;
 
1392
1393	if (console) {
1394		drv = &qcom_geni_console_driver;
1395		line = of_alias_get_id(pdev->dev.of_node, "serial");
1396	} else {
1397		drv = &qcom_geni_uart_driver;
1398		line = of_alias_get_id(pdev->dev.of_node, "serial");
1399		if (line == -ENODEV) /* compat with non-standard aliases */
1400			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
1401	}
1402
1403	port = get_port_from_line(line, console);
1404	if (IS_ERR(port)) {
1405		dev_err(&pdev->dev, "Invalid line %d\n", line);
1406		return PTR_ERR(port);
1407	}
1408
1409	uport = &port->uport;
1410	/* Don't allow 2 drivers to access the same port */
1411	if (uport->private_data)
1412		return -ENODEV;
1413
1414	uport->dev = &pdev->dev;
 
1415	port->se.dev = &pdev->dev;
1416	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1417	port->se.clk = devm_clk_get(&pdev->dev, "se");
1418	if (IS_ERR(port->se.clk)) {
1419		ret = PTR_ERR(port->se.clk);
1420		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1421		return ret;
1422	}
1423
1424	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1425	if (!res)
1426		return -EINVAL;
1427	uport->mapbase = res->start;
1428
1429	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1430	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1431	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1432
1433	if (!console) {
1434		port->rx_fifo = devm_kcalloc(uport->dev,
1435			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
1436		if (!port->rx_fifo)
1437			return -ENOMEM;
1438	}
1439
1440	ret = geni_icc_get(&port->se, NULL);
1441	if (ret)
1442		return ret;
1443	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
1444	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
1445
1446	/* Set BW for register access */
1447	ret = geni_icc_set_bw(&port->se);
1448	if (ret)
1449		return ret;
1450
1451	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1452			"qcom_geni_serial_%s%d",
1453			uart_console(uport) ? "console" : "uart", uport->line);
1454	if (!port->name)
1455		return -ENOMEM;
1456
1457	irq = platform_get_irq(pdev, 0);
1458	if (irq < 0)
1459		return irq;
1460	uport->irq = irq;
1461	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1462
1463	if (!console)
1464		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
1465
1466	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
1467		port->rx_tx_swap = true;
1468
1469	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
1470		port->cts_rts_swap = true;
1471
1472	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1473	if (ret)
1474		return ret;
1475	/* OPP table is optional */
1476	ret = devm_pm_opp_of_add_table(&pdev->dev);
1477	if (ret && ret != -ENODEV) {
1478		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1479		return ret;
1480	}
1481
1482	port->private_data.drv = drv;
1483	uport->private_data = &port->private_data;
1484	platform_set_drvdata(pdev, port);
1485	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
1486
1487	ret = uart_add_one_port(drv, uport);
1488	if (ret)
1489		return ret;
1490
1491	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1492	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1493			IRQF_TRIGGER_HIGH, port->name, uport);
1494	if (ret) {
1495		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1496		uart_remove_one_port(drv, uport);
1497		return ret;
1498	}
1499
1500	/*
1501	 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1502	 * enabled/disabled from dev_pm_arm_wake_irq during system
1503	 * suspend/resume respectively.
1504	 */
1505	pm_runtime_set_active(&pdev->dev);
1506
1507	if (port->wakeup_irq > 0) {
1508		device_init_wakeup(&pdev->dev, true);
1509		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1510						port->wakeup_irq);
1511		if (ret) {
1512			device_init_wakeup(&pdev->dev, false);
1513			uart_remove_one_port(drv, uport);
1514			return ret;
1515		}
1516	}
1517
1518	return 0;
1519}
1520
1521static int qcom_geni_serial_remove(struct platform_device *pdev)
1522{
1523	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1524	struct uart_driver *drv = port->private_data.drv;
1525
1526	dev_pm_clear_wake_irq(&pdev->dev);
1527	device_init_wakeup(&pdev->dev, false);
1528	uart_remove_one_port(drv, &port->uport);
1529
1530	return 0;
1531}
1532
1533static int qcom_geni_serial_sys_suspend(struct device *dev)
1534{
1535	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1536	struct uart_port *uport = &port->uport;
1537	struct qcom_geni_private_data *private_data = uport->private_data;
1538
1539	/*
1540	 * This is done so we can hit the lowest possible state in suspend
1541	 * even with no_console_suspend
1542	 */
1543	if (uart_console(uport)) {
1544		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
1545		geni_icc_set_bw(&port->se);
1546	}
1547	return uart_suspend_port(private_data->drv, uport);
1548}
1549
1550static int qcom_geni_serial_sys_resume(struct device *dev)
1551{
1552	int ret;
1553	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1554	struct uart_port *uport = &port->uport;
1555	struct qcom_geni_private_data *private_data = uport->private_data;
1556
1557	ret = uart_resume_port(private_data->drv, uport);
1558	if (uart_console(uport)) {
1559		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
1560		geni_icc_set_bw(&port->se);
1561	}
1562	return ret;
1563}
1564
1565static int qcom_geni_serial_sys_hib_resume(struct device *dev)
1566{
1567	int ret = 0;
1568	struct uart_port *uport;
1569	struct qcom_geni_private_data *private_data;
1570	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1571
1572	uport = &port->uport;
1573	private_data = uport->private_data;
1574
1575	if (uart_console(uport)) {
1576		geni_icc_set_tag(&port->se, 0x7);
1577		geni_icc_set_bw(&port->se);
1578		ret = uart_resume_port(private_data->drv, uport);
1579		/*
1580		 * For hibernation usecase clients for
1581		 * console UART won't call port setup during restore,
1582		 * hence call port setup for console uart.
1583		 */
1584		qcom_geni_serial_port_setup(uport);
1585	} else {
1586		/*
1587		 * Peripheral register settings are lost during hibernation.
1588		 * Update setup flag such that port setup happens again
1589		 * during next session. Clients of HS-UART will close and
1590		 * open the port during hibernation.
1591		 */
1592		port->setup = false;
1593	}
1594	return ret;
1595}
1596
1597static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1598	.suspend = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1599	.resume = pm_sleep_ptr(qcom_geni_serial_sys_resume),
1600	.freeze = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1601	.poweroff = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1602	.restore = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1603	.thaw = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1604};
1605
1606static const struct of_device_id qcom_geni_serial_match_table[] = {
1607	{ .compatible = "qcom,geni-debug-uart", },
1608	{ .compatible = "qcom,geni-uart", },
 
 
 
 
 
 
1609	{}
1610};
1611MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1612
1613static struct platform_driver qcom_geni_serial_platform_driver = {
1614	.remove = qcom_geni_serial_remove,
1615	.probe = qcom_geni_serial_probe,
1616	.driver = {
1617		.name = "qcom_geni_serial",
1618		.of_match_table = qcom_geni_serial_match_table,
1619		.pm = &qcom_geni_serial_pm_ops,
1620	},
1621};
1622
1623static int __init qcom_geni_serial_init(void)
1624{
1625	int ret;
1626
1627	ret = console_register(&qcom_geni_console_driver);
1628	if (ret)
1629		return ret;
1630
1631	ret = uart_register_driver(&qcom_geni_uart_driver);
1632	if (ret) {
1633		console_unregister(&qcom_geni_console_driver);
1634		return ret;
1635	}
1636
1637	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
1638	if (ret) {
1639		console_unregister(&qcom_geni_console_driver);
1640		uart_unregister_driver(&qcom_geni_uart_driver);
1641	}
1642	return ret;
1643}
1644module_init(qcom_geni_serial_init);
1645
1646static void __exit qcom_geni_serial_exit(void)
1647{
1648	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1649	console_unregister(&qcom_geni_console_driver);
1650	uart_unregister_driver(&qcom_geni_uart_driver);
1651}
1652module_exit(qcom_geni_serial_exit);
1653
1654MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1655MODULE_LICENSE("GPL v2");
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2// Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
   3
   4/* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
   5#define __DISABLE_TRACE_MMIO__
   6
   7#include <linux/clk.h>
   8#include <linux/console.h>
   9#include <linux/io.h>
  10#include <linux/iopoll.h>
  11#include <linux/irq.h>
  12#include <linux/module.h>
  13#include <linux/of.h>
 
  14#include <linux/pm_opp.h>
  15#include <linux/platform_device.h>
  16#include <linux/pm_runtime.h>
  17#include <linux/pm_wakeirq.h>
  18#include <linux/soc/qcom/geni-se.h>
  19#include <linux/serial.h>
  20#include <linux/serial_core.h>
  21#include <linux/slab.h>
  22#include <linux/tty.h>
  23#include <linux/tty_flip.h>
  24#include <dt-bindings/interconnect/qcom,icc.h>
  25
  26/* UART specific GENI registers */
  27#define SE_UART_LOOPBACK_CFG		0x22c
  28#define SE_UART_IO_MACRO_CTRL		0x240
  29#define SE_UART_TX_TRANS_CFG		0x25c
  30#define SE_UART_TX_WORD_LEN		0x268
  31#define SE_UART_TX_STOP_BIT_LEN		0x26c
  32#define SE_UART_TX_TRANS_LEN		0x270
  33#define SE_UART_RX_TRANS_CFG		0x280
  34#define SE_UART_RX_WORD_LEN		0x28c
  35#define SE_UART_RX_STALE_CNT		0x294
  36#define SE_UART_TX_PARITY_CFG		0x2a4
  37#define SE_UART_RX_PARITY_CFG		0x2a8
  38#define SE_UART_MANUAL_RFR		0x2ac
  39
  40/* SE_UART_TRANS_CFG */
  41#define UART_TX_PAR_EN			BIT(0)
  42#define UART_CTS_MASK			BIT(1)
 
 
 
  43
  44/* SE_UART_TX_STOP_BIT_LEN */
  45#define TX_STOP_BIT_LEN_1		0
  46#define TX_STOP_BIT_LEN_2		2
 
 
 
 
 
  47
  48/* SE_UART_RX_TRANS_CFG */
  49#define UART_RX_PAR_EN			BIT(3)
 
  50
  51/* SE_UART_RX_WORD_LEN */
  52#define RX_WORD_LEN_MASK		GENMASK(9, 0)
  53
  54/* SE_UART_RX_STALE_CNT */
  55#define RX_STALE_CNT			GENMASK(23, 0)
  56
  57/* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
  58#define PAR_CALC_EN			BIT(0)
  59#define PAR_EVEN			0x00
  60#define PAR_ODD				0x01
  61#define PAR_SPACE			0x10
 
 
 
  62
  63/* SE_UART_MANUAL_RFR register fields */
  64#define UART_MANUAL_RFR_EN		BIT(31)
  65#define UART_RFR_NOT_READY		BIT(1)
  66#define UART_RFR_READY			BIT(0)
  67
  68/* UART M_CMD OP codes */
  69#define UART_START_TX			0x1
 
 
  70/* UART S_CMD OP codes */
  71#define UART_START_READ			0x1
  72#define UART_PARAM			0x1
  73#define UART_PARAM_RFR_OPEN		BIT(7)
  74
  75#define UART_OVERSAMPLING		32
  76#define STALE_TIMEOUT			16
  77#define DEFAULT_BITS_PER_CHAR		10
  78#define GENI_UART_CONS_PORTS		1
  79#define GENI_UART_PORTS			3
  80#define DEF_FIFO_DEPTH_WORDS		16
  81#define DEF_TX_WM			2
  82#define DEF_FIFO_WIDTH_BITS		32
  83#define UART_RX_WM			2
  84
  85/* SE_UART_LOOPBACK_CFG */
  86#define RX_TX_SORTED			BIT(0)
  87#define CTS_RTS_SORTED			BIT(1)
  88#define RX_TX_CTS_RTS_SORTED		(RX_TX_SORTED | CTS_RTS_SORTED)
  89
  90/* UART pin swap value */
  91#define DEFAULT_IO_MACRO_IO0_IO1_MASK	GENMASK(3, 0)
  92#define IO_MACRO_IO0_SEL		0x3
  93#define DEFAULT_IO_MACRO_IO2_IO3_MASK	GENMASK(15, 4)
  94#define IO_MACRO_IO2_IO3_SWAP		0x4640
  95
  96/* We always configure 4 bytes per FIFO word */
  97#define BYTES_PER_FIFO_WORD		4U
  98
  99#define DMA_RX_BUF_SIZE		2048
 100
 101struct qcom_geni_device_data {
 102	bool console;
 103	enum geni_se_xfer_mode mode;
 104};
 105
 106struct qcom_geni_private_data {
 107	/* NOTE: earlycon port will have NULL here */
 108	struct uart_driver *drv;
 109
 110	u32 poll_cached_bytes;
 111	unsigned int poll_cached_bytes_cnt;
 112
 113	u32 write_cached_bytes;
 114	unsigned int write_cached_bytes_cnt;
 115};
 116
 117struct qcom_geni_serial_port {
 118	struct uart_port uport;
 119	struct geni_se se;
 120	const char *name;
 121	u32 tx_fifo_depth;
 122	u32 tx_fifo_width;
 123	u32 rx_fifo_depth;
 124	dma_addr_t tx_dma_addr;
 125	dma_addr_t rx_dma_addr;
 126	bool setup;
 127	unsigned long poll_timeout_us;
 128	unsigned long clk_rate;
 129	void *rx_buf;
 130	u32 loopback;
 131	bool brk;
 132
 133	unsigned int tx_remaining;
 134	unsigned int tx_queued;
 135	int wakeup_irq;
 136	bool rx_tx_swap;
 137	bool cts_rts_swap;
 138
 139	struct qcom_geni_private_data private_data;
 140	const struct qcom_geni_device_data *dev_data;
 141};
 142
 143static const struct uart_ops qcom_geni_console_pops;
 144static const struct uart_ops qcom_geni_uart_pops;
 145static struct uart_driver qcom_geni_console_driver;
 146static struct uart_driver qcom_geni_uart_driver;
 
 
 
 
 
 147
 148static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
 149static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
 150static int qcom_geni_serial_port_setup(struct uart_port *uport);
 151
 152static inline struct qcom_geni_serial_port *to_dev_port(struct uart_port *uport)
 153{
 154	return container_of(uport, struct qcom_geni_serial_port, uport);
 155}
 156
 157static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
 158	[0] = {
 159		.uport = {
 160			.iotype = UPIO_MEM,
 161			.ops = &qcom_geni_uart_pops,
 162			.flags = UPF_BOOT_AUTOCONF,
 163			.line = 0,
 164		},
 165	},
 166	[1] = {
 167		.uport = {
 168			.iotype = UPIO_MEM,
 169			.ops = &qcom_geni_uart_pops,
 170			.flags = UPF_BOOT_AUTOCONF,
 171			.line = 1,
 172		},
 173	},
 174	[2] = {
 175		.uport = {
 176			.iotype = UPIO_MEM,
 177			.ops = &qcom_geni_uart_pops,
 178			.flags = UPF_BOOT_AUTOCONF,
 179			.line = 2,
 180		},
 181	},
 182};
 183
 184static struct qcom_geni_serial_port qcom_geni_console_port = {
 185	.uport = {
 186		.iotype = UPIO_MEM,
 187		.ops = &qcom_geni_console_pops,
 188		.flags = UPF_BOOT_AUTOCONF,
 189		.line = 0,
 190	},
 191};
 192
 193static int qcom_geni_serial_request_port(struct uart_port *uport)
 194{
 195	struct platform_device *pdev = to_platform_device(uport->dev);
 196	struct qcom_geni_serial_port *port = to_dev_port(uport);
 197
 198	uport->membase = devm_platform_ioremap_resource(pdev, 0);
 199	if (IS_ERR(uport->membase))
 200		return PTR_ERR(uport->membase);
 201	port->se.base = uport->membase;
 202	return 0;
 203}
 204
 205static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
 206{
 207	if (cfg_flags & UART_CONFIG_TYPE) {
 208		uport->type = PORT_MSM;
 209		qcom_geni_serial_request_port(uport);
 210	}
 211}
 212
 213static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
 214{
 215	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
 216	u32 geni_ios;
 217
 218	if (uart_console(uport)) {
 219		mctrl |= TIOCM_CTS;
 220	} else {
 221		geni_ios = readl(uport->membase + SE_GENI_IOS);
 222		if (!(geni_ios & IO2_DATA_IN))
 223			mctrl |= TIOCM_CTS;
 224	}
 225
 226	return mctrl;
 227}
 228
 229static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
 230							unsigned int mctrl)
 231{
 232	u32 uart_manual_rfr = 0;
 233	struct qcom_geni_serial_port *port = to_dev_port(uport);
 234
 235	if (uart_console(uport))
 236		return;
 237
 238	if (mctrl & TIOCM_LOOP)
 239		port->loopback = RX_TX_CTS_RTS_SORTED;
 240
 241	if (!(mctrl & TIOCM_RTS) && !uport->suspended)
 242		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
 243	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
 244}
 245
 246static const char *qcom_geni_serial_get_type(struct uart_port *uport)
 247{
 248	return "MSM";
 249}
 250
 251static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
 252{
 253	struct qcom_geni_serial_port *port;
 254	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
 255
 256	if (line < 0 || line >= nr_ports)
 257		return ERR_PTR(-ENXIO);
 258
 259	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
 260	return port;
 261}
 262
 263static bool qcom_geni_serial_main_active(struct uart_port *uport)
 264{
 265	return readl(uport->membase + SE_GENI_STATUS) & M_GENI_CMD_ACTIVE;
 266}
 267
 268static bool qcom_geni_serial_secondary_active(struct uart_port *uport)
 269{
 270	return readl(uport->membase + SE_GENI_STATUS) & S_GENI_CMD_ACTIVE;
 271}
 272
 273static bool qcom_geni_serial_poll_bitfield(struct uart_port *uport,
 274					   unsigned int offset, u32 field, u32 val)
 275{
 276	u32 reg;
 277	struct qcom_geni_serial_port *port;
 
 
 278	unsigned long timeout_us = 20000;
 279	struct qcom_geni_private_data *private_data = uport->private_data;
 280
 281	if (private_data->drv) {
 282		port = to_dev_port(uport);
 283		if (port->poll_timeout_us)
 284			timeout_us = port->poll_timeout_us;
 
 
 
 
 
 
 
 285	}
 286
 287	/*
 288	 * Use custom implementation instead of readl_poll_atomic since ktimer
 289	 * is not ready at the time of early console.
 290	 */
 291	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
 292	while (timeout_us) {
 293		reg = readl(uport->membase + offset);
 294		if ((reg & field) == val)
 295			return true;
 296		udelay(10);
 297		timeout_us -= 10;
 298	}
 299	return false;
 300}
 301
 302static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
 303				      unsigned int offset, u32 field, bool set)
 304{
 305	return qcom_geni_serial_poll_bitfield(uport, offset, field, set ? field : 0);
 306}
 307
 308static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
 309{
 310	u32 m_cmd;
 311
 312	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
 313	m_cmd = UART_START_TX << M_OPCODE_SHFT;
 314	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
 315}
 316
 317static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
 318{
 319	int done;
 
 320
 321	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 322						M_CMD_DONE_EN, true);
 323	if (!done) {
 324		writel(M_GENI_CMD_ABORT, uport->membase +
 325						SE_GENI_M_CMD_CTRL_REG);
 
 326		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 327							M_CMD_ABORT_EN, true);
 328		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 329	}
 
 330}
 331
 332static void qcom_geni_serial_abort_rx(struct uart_port *uport)
 333{
 334	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
 335
 336	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
 337	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
 338					S_GENI_CMD_ABORT, false);
 339	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
 340	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
 341}
 342
 343#ifdef CONFIG_CONSOLE_POLL
 
 344static int qcom_geni_serial_get_char(struct uart_port *uport)
 345{
 346	struct qcom_geni_private_data *private_data = uport->private_data;
 347	u32 status;
 348	u32 word_cnt;
 349	int ret;
 350
 351	if (!private_data->poll_cached_bytes_cnt) {
 352		status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
 353		writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
 354
 355		status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
 356		writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
 357
 358		status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
 359		word_cnt = status & RX_FIFO_WC_MSK;
 360		if (!word_cnt)
 361			return NO_POLL_CHAR;
 362
 363		if (word_cnt == 1 && (status & RX_LAST))
 364			/*
 365			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
 366			 * treated as if it was BYTES_PER_FIFO_WORD.
 367			 */
 368			private_data->poll_cached_bytes_cnt =
 369				(status & RX_LAST_BYTE_VALID_MSK) >>
 370				RX_LAST_BYTE_VALID_SHFT;
 371
 372		if (private_data->poll_cached_bytes_cnt == 0)
 373			private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
 374
 375		private_data->poll_cached_bytes =
 376			readl(uport->membase + SE_GENI_RX_FIFOn);
 377	}
 378
 379	private_data->poll_cached_bytes_cnt--;
 380	ret = private_data->poll_cached_bytes & 0xff;
 381	private_data->poll_cached_bytes >>= 8;
 382
 383	return ret;
 384}
 385
 386static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
 387							unsigned char c)
 388{
 389	if (qcom_geni_serial_main_active(uport)) {
 390		qcom_geni_serial_poll_tx_done(uport);
 391		__qcom_geni_serial_cancel_tx_cmd(uport);
 392	}
 393
 394	writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 395	qcom_geni_serial_setup_tx(uport, 1);
 
 
 396	writel(c, uport->membase + SE_GENI_TX_FIFOn);
 
 397	qcom_geni_serial_poll_tx_done(uport);
 398}
 399
 400static int qcom_geni_serial_poll_init(struct uart_port *uport)
 401{
 402	struct qcom_geni_serial_port *port = to_dev_port(uport);
 403	int ret;
 404
 405	if (!port->setup) {
 406		ret = qcom_geni_serial_port_setup(uport);
 407		if (ret)
 408			return ret;
 409	}
 410
 411	if (!qcom_geni_serial_secondary_active(uport))
 412		geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
 413
 414	return 0;
 415}
 416#endif
 417
 418#ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
 419static void qcom_geni_serial_drain_fifo(struct uart_port *uport)
 420{
 421	struct qcom_geni_serial_port *port = to_dev_port(uport);
 422
 423	qcom_geni_serial_poll_bitfield(uport, SE_GENI_M_GP_LENGTH, GP_LENGTH,
 424			port->tx_queued);
 425}
 426
 427static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
 428{
 429	struct qcom_geni_private_data *private_data = uport->private_data;
 430
 431	private_data->write_cached_bytes =
 432		(private_data->write_cached_bytes >> 8) | (ch << 24);
 433	private_data->write_cached_bytes_cnt++;
 434
 435	if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
 436		writel(private_data->write_cached_bytes,
 437		       uport->membase + SE_GENI_TX_FIFOn);
 438		private_data->write_cached_bytes_cnt = 0;
 439	}
 440}
 441
 442static void
 443__qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
 444				 unsigned int count)
 445{
 446	struct qcom_geni_private_data *private_data = uport->private_data;
 447
 448	int i;
 449	u32 bytes_to_send = count;
 450
 451	for (i = 0; i < count; i++) {
 452		/*
 453		 * uart_console_write() adds a carriage return for each newline.
 454		 * Account for additional bytes to be written.
 455		 */
 456		if (s[i] == '\n')
 457			bytes_to_send++;
 458	}
 459
 460	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
 461	writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 462	qcom_geni_serial_setup_tx(uport, bytes_to_send);
 463	for (i = 0; i < count; ) {
 464		size_t chars_to_write = 0;
 465		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
 466
 467		/*
 468		 * If the WM bit never set, then the Tx state machine is not
 469		 * in a valid state, so break, cancel/abort any existing
 470		 * command. Unfortunately the current data being written is
 471		 * lost.
 472		 */
 473		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 474						M_TX_FIFO_WATERMARK_EN, true))
 475			break;
 476		chars_to_write = min_t(size_t, count - i, avail / 2);
 477		uart_console_write(uport, s + i, chars_to_write,
 478						qcom_geni_serial_wr_char);
 479		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
 480							SE_GENI_M_IRQ_CLEAR);
 481		i += chars_to_write;
 482	}
 483
 484	if (private_data->write_cached_bytes_cnt) {
 485		private_data->write_cached_bytes >>= BITS_PER_BYTE *
 486			(BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
 487		writel(private_data->write_cached_bytes,
 488		       uport->membase + SE_GENI_TX_FIFOn);
 489		private_data->write_cached_bytes_cnt = 0;
 490	}
 491
 492	qcom_geni_serial_poll_tx_done(uport);
 493}
 494
 495static void qcom_geni_serial_console_write(struct console *co, const char *s,
 496			      unsigned int count)
 497{
 498	struct uart_port *uport;
 499	struct qcom_geni_serial_port *port;
 500	u32 m_irq_en, s_irq_en;
 501	bool locked = true;
 502	unsigned long flags;
 
 
 503
 504	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
 505
 506	port = get_port_from_line(co->index, true);
 507	if (IS_ERR(port))
 508		return;
 509
 510	uport = &port->uport;
 511	if (oops_in_progress)
 512		locked = uart_port_trylock_irqsave(uport, &flags);
 513	else
 514		uart_port_lock_irqsave(uport, &flags);
 515
 516	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 517	s_irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
 518	writel(0, uport->membase + SE_GENI_M_IRQ_EN);
 519	writel(0, uport->membase + SE_GENI_S_IRQ_EN);
 520
 521	if (qcom_geni_serial_main_active(uport)) {
 522		/* Wait for completion or drain FIFO */
 523		if (!locked || port->tx_remaining == 0)
 524			qcom_geni_serial_poll_tx_done(uport);
 525		else
 526			qcom_geni_serial_drain_fifo(uport);
 
 
 
 
 
 
 
 
 
 527
 528		qcom_geni_serial_cancel_tx_cmd(uport);
 
 
 
 
 529	}
 530
 531	__qcom_geni_serial_console_write(uport, s, count);
 532
 533	writel(m_irq_en, uport->membase + SE_GENI_M_IRQ_EN);
 534	writel(s_irq_en, uport->membase + SE_GENI_S_IRQ_EN);
 535
 536	if (locked)
 537		uart_port_unlock_irqrestore(uport, flags);
 538}
 539
 540static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
 541{
 542	u32 i;
 543	unsigned char buf[sizeof(u32)];
 544	struct tty_port *tport;
 545	struct qcom_geni_serial_port *port = to_dev_port(uport);
 546
 547	tport = &uport->state->port;
 548	for (i = 0; i < bytes; ) {
 549		int c;
 550		int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
 551
 552		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
 553		i += chunk;
 554		if (drop)
 555			continue;
 556
 557		for (c = 0; c < chunk; c++) {
 558			int sysrq;
 559
 560			uport->icount.rx++;
 561			if (port->brk && buf[c] == 0) {
 562				port->brk = false;
 563				if (uart_handle_break(uport))
 564					continue;
 565			}
 566
 567			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
 568
 569			if (!sysrq)
 570				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
 571		}
 572	}
 573	if (!drop)
 574		tty_flip_buffer_push(tport);
 
 575}
 576#else
 577static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
 578{
 
 
 579
 580}
 581#endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
 582
 583static void handle_rx_uart(struct uart_port *uport, u32 bytes)
 584{
 585	struct qcom_geni_serial_port *port = to_dev_port(uport);
 586	struct tty_port *tport = &uport->state->port;
 
 
 587	int ret;
 588
 589	ret = tty_insert_flip_string(tport, port->rx_buf, bytes);
 
 
 
 
 
 590	if (ret != bytes) {
 591		dev_err_ratelimited(uport->dev, "failed to push data (%d < %u)\n",
 592				ret, bytes);
 
 593	}
 594	uport->icount.rx += ret;
 595	tty_flip_buffer_push(tport);
 
 596}
 597
 598static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
 599{
 600	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
 601}
 602
 603static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport)
 604{
 605	struct qcom_geni_serial_port *port = to_dev_port(uport);
 606	bool done;
 607
 608	if (!qcom_geni_serial_main_active(uport))
 609		return;
 610
 611	if (port->tx_dma_addr) {
 612		geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr,
 613				      port->tx_remaining);
 614		port->tx_dma_addr = 0;
 615		port->tx_remaining = 0;
 616	}
 617
 618	geni_se_cancel_m_cmd(&port->se);
 619
 620	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 621					 M_CMD_CANCEL_EN, true);
 622	if (!done) {
 623		geni_se_abort_m_cmd(&port->se);
 624		done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 625						 M_CMD_ABORT_EN, true);
 626		if (!done)
 627			dev_err_ratelimited(uport->dev, "M_CMD_ABORT_EN not set");
 628		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 629	}
 630
 631	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 632}
 633
 634static void qcom_geni_serial_start_tx_dma(struct uart_port *uport)
 635{
 636	struct qcom_geni_serial_port *port = to_dev_port(uport);
 637	struct tty_port *tport = &uport->state->port;
 638	unsigned int xmit_size;
 639	u8 *tail;
 640	int ret;
 641
 642	if (port->tx_dma_addr)
 643		return;
 644
 645	if (kfifo_is_empty(&tport->xmit_fifo))
 646		return;
 647
 648	xmit_size = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail,
 649			UART_XMIT_SIZE);
 650
 651	qcom_geni_serial_setup_tx(uport, xmit_size);
 652
 653	ret = geni_se_tx_dma_prep(&port->se, tail, xmit_size,
 654				  &port->tx_dma_addr);
 655	if (ret) {
 656		dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
 657		qcom_geni_serial_stop_tx_dma(uport);
 658		return;
 659	}
 660
 661	port->tx_remaining = xmit_size;
 662}
 663
 664static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
 665{
 666	unsigned char c;
 667	u32 irq_en;
 668
 669	/*
 670	 * Start a new transfer in case the previous command was cancelled and
 671	 * left data in the FIFO which may prevent the watermark interrupt
 672	 * from triggering. Note that the stale data is discarded.
 673	 */
 674	if (!qcom_geni_serial_main_active(uport) &&
 675	    !qcom_geni_serial_tx_empty(uport)) {
 676		if (uart_fifo_out(uport, &c, 1) == 1) {
 677			writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 678			qcom_geni_serial_setup_tx(uport, 1);
 679			writel(c, uport->membase + SE_GENI_TX_FIFOn);
 680		}
 681	}
 682
 683	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
 684	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
 
 685	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
 686	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
 687}
 688
 689static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
 690{
 691	u32 irq_en;
 
 
 692
 693	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 694	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
 695	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
 696	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
 697}
 698
 699static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
 700{
 701	struct qcom_geni_serial_port *port = to_dev_port(uport);
 702
 703	geni_se_cancel_m_cmd(&port->se);
 704	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 705						M_CMD_CANCEL_EN, true)) {
 706		geni_se_abort_m_cmd(&port->se);
 707		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
 708						M_CMD_ABORT_EN, true);
 709		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 710	}
 711	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
 712}
 713
 714static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
 715{
 716	struct qcom_geni_serial_port *port = to_dev_port(uport);
 717
 718	if (!qcom_geni_serial_main_active(uport))
 719		return;
 720
 721	__qcom_geni_serial_cancel_tx_cmd(uport);
 
 
 722
 723	port->tx_remaining = 0;
 724	port->tx_queued = 0;
 725}
 726
 727static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
 728{
 729	u32 status;
 730	u32 word_cnt;
 731	u32 last_word_byte_cnt;
 732	u32 last_word_partial;
 733	u32 total_bytes;
 734
 735	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
 736	word_cnt = status & RX_FIFO_WC_MSK;
 737	last_word_partial = status & RX_LAST;
 738	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
 739						RX_LAST_BYTE_VALID_SHFT;
 740
 741	if (!word_cnt)
 742		return;
 743	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
 744	if (last_word_partial && last_word_byte_cnt)
 745		total_bytes += last_word_byte_cnt;
 746	else
 747		total_bytes += BYTES_PER_FIFO_WORD;
 748	handle_rx_console(uport, total_bytes, drop);
 749}
 750
 751static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
 752{
 753	u32 irq_en;
 754	struct qcom_geni_serial_port *port = to_dev_port(uport);
 
 755	u32 s_irq_status;
 756
 757	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
 758	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
 759	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
 760
 761	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 762	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
 763	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
 764
 765	if (!qcom_geni_serial_secondary_active(uport))
 
 
 766		return;
 767
 768	geni_se_cancel_s_cmd(&port->se);
 769	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
 770					S_CMD_CANCEL_EN, true);
 771	/*
 772	 * If timeout occurs secondary engine remains active
 773	 * and Abort sequence is executed.
 774	 */
 775	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
 776	/* Flush the Rx buffer */
 777	if (s_irq_status & S_RX_FIFO_LAST_EN)
 778		qcom_geni_serial_handle_rx_fifo(uport, true);
 779	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
 780
 781	if (qcom_geni_serial_secondary_active(uport))
 
 782		qcom_geni_serial_abort_rx(uport);
 783}
 784
 785static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
 786{
 787	u32 irq_en;
 788	struct qcom_geni_serial_port *port = to_dev_port(uport);
 
 
 
 
 789
 790	if (qcom_geni_serial_secondary_active(uport))
 791		qcom_geni_serial_stop_rx_fifo(uport);
 
 
 
 792
 793	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
 794
 795	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
 796	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
 797	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
 798
 799	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 800	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
 801	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
 802}
 803
 804static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
 805{
 806	struct qcom_geni_serial_port *port = to_dev_port(uport);
 807	bool done;
 808
 809	if (!qcom_geni_serial_secondary_active(uport))
 810		return;
 811
 812	geni_se_cancel_s_cmd(&port->se);
 813	done = qcom_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
 814			RX_EOT, true);
 815	if (done) {
 816		writel(RX_EOT | RX_DMA_DONE,
 817				uport->membase + SE_DMA_RX_IRQ_CLR);
 818	} else {
 819		qcom_geni_serial_abort_rx(uport);
 820
 821		writel(1, uport->membase + SE_DMA_RX_FSM_RST);
 822		qcom_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
 823				RX_RESET_DONE, true);
 824		writel(RX_RESET_DONE | RX_DMA_DONE,
 825				uport->membase + SE_DMA_RX_IRQ_CLR);
 826	}
 827
 828	if (port->rx_dma_addr) {
 829		geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr,
 830				      DMA_RX_BUF_SIZE);
 831		port->rx_dma_addr = 0;
 832	}
 833}
 834
 835static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
 836{
 837	struct qcom_geni_serial_port *port = to_dev_port(uport);
 838	int ret;
 839
 840	if (qcom_geni_serial_secondary_active(uport))
 841		qcom_geni_serial_stop_rx_dma(uport);
 842
 843	geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
 844
 845	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
 846				  DMA_RX_BUF_SIZE,
 847				  &port->rx_dma_addr);
 848	if (ret) {
 849		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
 850		qcom_geni_serial_stop_rx_dma(uport);
 851	}
 852}
 853
 854static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
 855{
 856	struct qcom_geni_serial_port *port = to_dev_port(uport);
 857	u32 rx_in;
 858	int ret;
 859
 860	if (!qcom_geni_serial_secondary_active(uport))
 861		return;
 862
 863	if (!port->rx_dma_addr)
 864		return;
 865
 866	geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
 867	port->rx_dma_addr = 0;
 868
 869	rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
 870	if (!rx_in) {
 871		dev_warn(uport->dev, "serial engine reports 0 RX bytes in!\n");
 872		return;
 873	}
 874
 875	if (!drop)
 876		handle_rx_uart(uport, rx_in);
 877
 878	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
 879				  DMA_RX_BUF_SIZE,
 880				  &port->rx_dma_addr);
 881	if (ret) {
 882		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
 883		qcom_geni_serial_stop_rx_dma(uport);
 884	}
 885}
 886
 887static void qcom_geni_serial_start_rx(struct uart_port *uport)
 888{
 889	uport->ops->start_rx(uport);
 890}
 891
 892static void qcom_geni_serial_stop_rx(struct uart_port *uport)
 893{
 894	uport->ops->stop_rx(uport);
 895}
 896
 897static void qcom_geni_serial_stop_tx(struct uart_port *uport)
 898{
 899	uport->ops->stop_tx(uport);
 900}
 901
 902static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
 903					     unsigned int chunk)
 904{
 905	struct qcom_geni_serial_port *port = to_dev_port(uport);
 906	unsigned int tx_bytes, remaining = chunk;
 907	u8 buf[BYTES_PER_FIFO_WORD];
 908
 909	while (remaining) {
 910		memset(buf, 0, sizeof(buf));
 911		tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
 912
 913		uart_fifo_out(uport, buf, tx_bytes);
 914
 915		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
 916
 917		remaining -= tx_bytes;
 918		port->tx_remaining -= tx_bytes;
 919	}
 920}
 921
 922static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
 923					    bool done, bool active)
 924{
 925	struct qcom_geni_serial_port *port = to_dev_port(uport);
 926	struct tty_port *tport = &uport->state->port;
 927	size_t avail;
 
 928	size_t pending;
 
 929	u32 status;
 930	u32 irq_en;
 931	unsigned int chunk;
 
 932
 933	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
 934
 935	/* Complete the current tx command before taking newly added data */
 936	if (active)
 937		pending = port->tx_remaining;
 938	else
 939		pending = kfifo_len(&tport->xmit_fifo);
 940
 941	/* All data has been transmitted or command has been cancelled */
 942	if (!pending && done) {
 943		qcom_geni_serial_stop_tx_fifo(uport);
 944		goto out_write_wakeup;
 945	}
 946
 947	if (active)
 948		avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
 949	else
 950		avail = port->tx_fifo_depth;
 951
 952	avail *= BYTES_PER_FIFO_WORD;
 953
 
 954	chunk = min(avail, pending);
 955	if (!chunk)
 956		goto out_write_wakeup;
 957
 958	if (!active) {
 959		qcom_geni_serial_setup_tx(uport, pending);
 960		port->tx_remaining = pending;
 961		port->tx_queued = 0;
 962
 963		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 964		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
 965			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
 966					uport->membase + SE_GENI_M_IRQ_EN);
 967	}
 968
 969	qcom_geni_serial_send_chunk_fifo(uport, chunk);
 970	port->tx_queued += chunk;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 971
 972	/*
 973	 * The tx fifo watermark is level triggered and latched. Though we had
 974	 * cleared it in qcom_geni_serial_isr it will have already reasserted
 975	 * so we must clear it again here after our writes.
 976	 */
 977	writel(M_TX_FIFO_WATERMARK_EN,
 978			uport->membase + SE_GENI_M_IRQ_CLEAR);
 979
 980out_write_wakeup:
 981	if (!port->tx_remaining) {
 982		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
 983		if (irq_en & M_TX_FIFO_WATERMARK_EN)
 984			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
 985					uport->membase + SE_GENI_M_IRQ_EN);
 986	}
 987
 988	if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
 989		uart_write_wakeup(uport);
 990}
 991
 992static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
 993{
 994	struct qcom_geni_serial_port *port = to_dev_port(uport);
 995	struct tty_port *tport = &uport->state->port;
 996
 997	uart_xmit_advance(uport, port->tx_remaining);
 998	geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
 999	port->tx_dma_addr = 0;
1000	port->tx_remaining = 0;
1001
1002	if (!kfifo_is_empty(&tport->xmit_fifo))
1003		qcom_geni_serial_start_tx_dma(uport);
1004
1005	if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
1006		uart_write_wakeup(uport);
1007}
1008
1009static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
1010{
1011	u32 m_irq_en;
1012	u32 m_irq_status;
1013	u32 s_irq_status;
1014	u32 geni_status;
1015	u32 dma;
1016	u32 dma_tx_status;
1017	u32 dma_rx_status;
1018	struct uart_port *uport = dev;
1019	bool drop_rx = false;
1020	struct tty_port *tport = &uport->state->port;
1021	struct qcom_geni_serial_port *port = to_dev_port(uport);
1022
1023	if (uport->suspended)
1024		return IRQ_NONE;
1025
1026	uart_port_lock(uport);
1027
1028	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
1029	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
1030	dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
1031	dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
1032	geni_status = readl(uport->membase + SE_GENI_STATUS);
1033	dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
1034	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
1035	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
1036	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
1037	writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
1038	writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
1039
1040	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
1041		goto out_unlock;
1042
1043	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
1044		uport->icount.overrun++;
1045		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
1046	}
1047
1048	if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
 
 
 
 
1049		if (s_irq_status & S_GP_IRQ_0_EN)
1050			uport->icount.parity++;
1051		drop_rx = true;
1052	} else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
 
1053		uport->icount.brk++;
1054		port->brk = true;
1055	}
1056
1057	if (dma) {
1058		if (dma_tx_status & TX_DMA_DONE)
1059			qcom_geni_serial_handle_tx_dma(uport);
1060
1061		if (dma_rx_status) {
1062			if (dma_rx_status & RX_RESET_DONE)
1063				goto out_unlock;
1064
1065			if (dma_rx_status & RX_DMA_PARITY_ERR) {
1066				uport->icount.parity++;
1067				drop_rx = true;
1068			}
1069
1070			if (dma_rx_status & RX_DMA_BREAK)
1071				uport->icount.brk++;
1072
1073			if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
1074				qcom_geni_serial_handle_rx_dma(uport, drop_rx);
1075		}
1076	} else {
1077		if (m_irq_status & m_irq_en &
1078		    (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
1079			qcom_geni_serial_handle_tx_fifo(uport,
1080					m_irq_status & M_CMD_DONE_EN,
1081					geni_status & M_GENI_CMD_ACTIVE);
1082
1083		if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
1084			qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
1085	}
1086
1087out_unlock:
1088	uart_unlock_and_check_sysrq(uport);
1089
1090	return IRQ_HANDLED;
1091}
1092
1093static int setup_fifos(struct qcom_geni_serial_port *port)
1094{
1095	struct uart_port *uport;
1096	u32 old_rx_fifo_depth = port->rx_fifo_depth;
1097
1098	uport = &port->uport;
1099	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
1100	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
1101	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
1102	uport->fifosize =
1103		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
1104
1105	if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
1106		/*
1107		 * Use krealloc rather than krealloc_array because rx_buf is
1108		 * accessed as 1 byte entries as well as 4 byte entries so it's
1109		 * not necessarily an array.
1110		 */
1111		port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
1112					     port->rx_fifo_depth * sizeof(u32),
1113					     GFP_KERNEL);
1114		if (!port->rx_buf)
1115			return -ENOMEM;
1116	}
1117
1118	return 0;
1119}
1120
1121
1122static void qcom_geni_serial_shutdown(struct uart_port *uport)
1123{
1124	disable_irq(uport->irq);
1125
1126	uart_port_lock_irq(uport);
1127	qcom_geni_serial_stop_tx(uport);
1128	qcom_geni_serial_stop_rx(uport);
1129
1130	qcom_geni_serial_cancel_tx_cmd(uport);
1131	uart_port_unlock_irq(uport);
1132}
1133
1134static void qcom_geni_serial_flush_buffer(struct uart_port *uport)
1135{
1136	qcom_geni_serial_cancel_tx_cmd(uport);
1137}
1138
1139static int qcom_geni_serial_port_setup(struct uart_port *uport)
1140{
1141	struct qcom_geni_serial_port *port = to_dev_port(uport);
1142	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
1143	u32 proto;
1144	u32 pin_swap;
1145	int ret;
1146
1147	proto = geni_se_read_proto(&port->se);
1148	if (proto != GENI_SE_UART) {
1149		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
1150		return -ENXIO;
1151	}
1152
1153	qcom_geni_serial_stop_rx(uport);
1154
1155	ret = setup_fifos(port);
1156	if (ret)
1157		return ret;
1158
1159	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
1160
1161	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
1162	if (port->rx_tx_swap) {
1163		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
1164		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
1165	}
1166	if (port->cts_rts_swap) {
1167		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
1168		pin_swap |= IO_MACRO_IO0_SEL;
1169	}
1170	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
1171	if (port->rx_tx_swap || port->cts_rts_swap)
1172		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
1173
1174	/*
1175	 * Make an unconditional cancel on the main sequencer to reset
1176	 * it else we could end up in data loss scenarios.
1177	 */
1178	if (uart_console(uport))
1179		qcom_geni_serial_poll_tx_done(uport);
1180	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1181			       false, true, true);
1182	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
1183	geni_se_select_mode(&port->se, port->dev_data->mode);
 
1184	port->setup = true;
1185
1186	return 0;
1187}
1188
1189static int qcom_geni_serial_startup(struct uart_port *uport)
1190{
1191	int ret;
1192	struct qcom_geni_serial_port *port = to_dev_port(uport);
1193
1194	if (!port->setup) {
1195		ret = qcom_geni_serial_port_setup(uport);
1196		if (ret)
1197			return ret;
1198	}
1199
1200	uart_port_lock_irq(uport);
1201	qcom_geni_serial_start_rx(uport);
1202	uart_port_unlock_irq(uport);
1203
1204	enable_irq(uport->irq);
1205
1206	return 0;
1207}
1208
1209static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
1210			unsigned int *clk_div, unsigned int percent_tol)
1211{
1212	unsigned long freq;
1213	unsigned long div, maxdiv;
1214	u64 mult;
1215	unsigned long offset, abs_tol, achieved;
1216
1217	abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
1218	maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
1219	div = 1;
1220	while (div <= maxdiv) {
1221		mult = (u64)div * desired_clk;
1222		if (mult != (unsigned long)mult)
1223			break;
1224
1225		offset = div * abs_tol;
1226		freq = clk_round_rate(clk, mult - offset);
1227
1228		/* Can only get lower if we're done */
1229		if (freq < mult - offset)
1230			break;
1231
1232		/*
1233		 * Re-calculate div in case rounding skipped rates but we
1234		 * ended up at a good one, then check for a match.
1235		 */
1236		div = DIV_ROUND_CLOSEST(freq, desired_clk);
1237		achieved = DIV_ROUND_CLOSEST(freq, div);
1238		if (achieved <= desired_clk + abs_tol &&
1239		    achieved >= desired_clk - abs_tol) {
1240			*clk_div = div;
1241			return freq;
1242		}
1243
1244		div = DIV_ROUND_UP(freq, desired_clk);
1245	}
1246
1247	return 0;
1248}
1249
1250static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
1251			unsigned int sampling_rate, unsigned int *clk_div)
1252{
1253	unsigned long ser_clk;
1254	unsigned long desired_clk;
1255
1256	desired_clk = baud * sampling_rate;
1257	if (!desired_clk)
1258		return 0;
1259
1260	/*
1261	 * try to find a clock rate within 2% tolerance, then within 5%
1262	 */
1263	ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1264	if (!ser_clk)
1265		ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1266
1267	return ser_clk;
1268}
1269
1270static void qcom_geni_serial_set_termios(struct uart_port *uport,
1271					 struct ktermios *termios,
1272					 const struct ktermios *old)
1273{
1274	unsigned int baud;
1275	u32 bits_per_char;
1276	u32 tx_trans_cfg;
1277	u32 tx_parity_cfg;
1278	u32 rx_trans_cfg;
1279	u32 rx_parity_cfg;
1280	u32 stop_bit_len;
1281	unsigned int clk_div;
1282	u32 ser_clk_cfg;
1283	struct qcom_geni_serial_port *port = to_dev_port(uport);
1284	unsigned long clk_rate;
1285	u32 ver, sampling_rate;
1286	unsigned int avg_bw_core;
1287	unsigned long timeout;
1288
 
1289	/* baud rate */
1290	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
 
1291
1292	sampling_rate = UART_OVERSAMPLING;
1293	/* Sampling rate is halved for IP versions >= 2.5 */
1294	ver = geni_se_get_qup_hw_version(&port->se);
1295	if (ver >= QUP_SE_VERSION_2_5)
1296		sampling_rate /= 2;
1297
1298	clk_rate = get_clk_div_rate(port->se.clk, baud,
1299		sampling_rate, &clk_div);
1300	if (!clk_rate) {
1301		dev_err(port->se.dev,
1302			"Couldn't find suitable clock rate for %u\n",
1303			baud * sampling_rate);
1304		return;
1305	}
1306
1307	dev_dbg(port->se.dev, "desired_rate = %u, clk_rate = %lu, clk_div = %u\n",
1308			baud * sampling_rate, clk_rate, clk_div);
1309
1310	uport->uartclk = clk_rate;
1311	port->clk_rate = clk_rate;
1312	dev_pm_opp_set_rate(uport->dev, clk_rate);
1313	ser_clk_cfg = SER_CLK_EN;
1314	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1315
1316	/*
1317	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
1318	 * only.
1319	 */
1320	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
1321						: GENI_DEFAULT_BW;
1322	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
1323	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
1324	geni_icc_set_bw(&port->se);
1325
1326	/* parity */
1327	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
1328	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
1329	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
1330	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1331	if (termios->c_cflag & PARENB) {
1332		tx_trans_cfg |= UART_TX_PAR_EN;
1333		rx_trans_cfg |= UART_RX_PAR_EN;
1334		tx_parity_cfg |= PAR_CALC_EN;
1335		rx_parity_cfg |= PAR_CALC_EN;
1336		if (termios->c_cflag & PARODD) {
1337			tx_parity_cfg |= PAR_ODD;
1338			rx_parity_cfg |= PAR_ODD;
1339		} else if (termios->c_cflag & CMSPAR) {
1340			tx_parity_cfg |= PAR_SPACE;
1341			rx_parity_cfg |= PAR_SPACE;
1342		} else {
1343			tx_parity_cfg |= PAR_EVEN;
1344			rx_parity_cfg |= PAR_EVEN;
1345		}
1346	} else {
1347		tx_trans_cfg &= ~UART_TX_PAR_EN;
1348		rx_trans_cfg &= ~UART_RX_PAR_EN;
1349		tx_parity_cfg &= ~PAR_CALC_EN;
1350		rx_parity_cfg &= ~PAR_CALC_EN;
1351	}
1352
1353	/* bits per char */
1354	bits_per_char = tty_get_char_size(termios->c_cflag);
1355
1356	/* stop bits */
1357	if (termios->c_cflag & CSTOPB)
1358		stop_bit_len = TX_STOP_BIT_LEN_2;
1359	else
1360		stop_bit_len = TX_STOP_BIT_LEN_1;
1361
1362	/* flow control, clear the CTS_MASK bit if using flow control. */
1363	if (termios->c_cflag & CRTSCTS)
1364		tx_trans_cfg &= ~UART_CTS_MASK;
1365	else
1366		tx_trans_cfg |= UART_CTS_MASK;
1367
1368	if (baud) {
1369		uart_update_timeout(uport, termios->c_cflag, baud);
1370
1371		/*
1372		 * Make sure that qcom_geni_serial_poll_bitfield() waits for
1373		 * the FIFO, two-word intermediate transfer register and shift
1374		 * register to clear.
1375		 *
1376		 * Note that uart_fifo_timeout() also adds a 20 ms margin.
1377		 */
1378		timeout = jiffies_to_usecs(uart_fifo_timeout(uport));
1379		timeout += 3 * timeout / port->tx_fifo_depth;
1380		WRITE_ONCE(port->poll_timeout_us, timeout);
1381	}
1382
1383	if (!uart_console(uport))
1384		writel(port->loopback,
1385				uport->membase + SE_UART_LOOPBACK_CFG);
1386	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1387	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1388	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1389	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1390	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1391	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1392	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1393	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1394	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
 
 
 
 
 
 
 
1395}
1396
1397#ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1398static int qcom_geni_console_setup(struct console *co, char *options)
1399{
1400	struct uart_port *uport;
1401	struct qcom_geni_serial_port *port;
1402	int baud = 115200;
1403	int bits = 8;
1404	int parity = 'n';
1405	int flow = 'n';
1406	int ret;
1407
1408	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1409		return -ENXIO;
1410
1411	port = get_port_from_line(co->index, true);
1412	if (IS_ERR(port)) {
1413		pr_err("Invalid line %d\n", co->index);
1414		return PTR_ERR(port);
1415	}
1416
1417	uport = &port->uport;
1418
1419	if (unlikely(!uport->membase))
1420		return -ENXIO;
1421
1422	if (!port->setup) {
1423		ret = qcom_geni_serial_port_setup(uport);
1424		if (ret)
1425			return ret;
1426	}
1427
1428	if (options)
1429		uart_parse_options(options, &baud, &parity, &bits, &flow);
1430
1431	return uart_set_options(uport, co, baud, parity, bits, flow);
1432}
1433
1434static void qcom_geni_serial_earlycon_write(struct console *con,
1435					const char *s, unsigned int n)
1436{
1437	struct earlycon_device *dev = con->data;
1438
1439	__qcom_geni_serial_console_write(&dev->port, s, n);
1440}
1441
1442#ifdef CONFIG_CONSOLE_POLL
1443static int qcom_geni_serial_earlycon_read(struct console *con,
1444					  char *s, unsigned int n)
1445{
1446	struct earlycon_device *dev = con->data;
1447	struct uart_port *uport = &dev->port;
1448	int num_read = 0;
1449	int ch;
1450
1451	while (num_read < n) {
1452		ch = qcom_geni_serial_get_char(uport);
1453		if (ch == NO_POLL_CHAR)
1454			break;
1455		s[num_read++] = ch;
1456	}
1457
1458	return num_read;
1459}
1460
1461static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1462						      struct console *con)
1463{
1464	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1465	con->read = qcom_geni_serial_earlycon_read;
1466}
1467#else
1468static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1469						      struct console *con) { }
1470#endif
1471
1472static struct qcom_geni_private_data earlycon_private_data;
1473
1474static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
1475								const char *opt)
1476{
1477	struct uart_port *uport = &dev->port;
1478	u32 tx_trans_cfg;
1479	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
1480	u32 rx_trans_cfg = 0;
1481	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
1482	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
1483	u32 bits_per_char;
1484	struct geni_se se;
1485
1486	if (!uport->membase)
1487		return -EINVAL;
1488
1489	uport->private_data = &earlycon_private_data;
1490
1491	memset(&se, 0, sizeof(se));
1492	se.base = uport->membase;
1493	if (geni_se_read_proto(&se) != GENI_SE_UART)
1494		return -ENXIO;
1495	/*
1496	 * Ignore Flow control.
1497	 * n = 8.
1498	 */
1499	tx_trans_cfg = UART_CTS_MASK;
1500	bits_per_char = BITS_PER_BYTE;
1501
1502	/*
1503	 * Make an unconditional cancel on the main sequencer to reset
1504	 * it else we could end up in data loss scenarios.
1505	 */
1506	qcom_geni_serial_poll_tx_done(uport);
1507	qcom_geni_serial_abort_rx(uport);
1508	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1509			       false, true, true);
1510	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
1511	geni_se_select_mode(&se, GENI_SE_FIFO);
1512
1513	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1514	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1515	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1516	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1517	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1518	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1519	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1520
1521	dev->con->write = qcom_geni_serial_earlycon_write;
1522	dev->con->setup = NULL;
1523	qcom_geni_serial_enable_early_read(&se, dev->con);
1524
1525	return 0;
1526}
1527OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
1528				qcom_geni_serial_earlycon_setup);
1529
1530static int __init console_register(struct uart_driver *drv)
1531{
1532	return uart_register_driver(drv);
1533}
1534
1535static void console_unregister(struct uart_driver *drv)
1536{
1537	uart_unregister_driver(drv);
1538}
1539
1540static struct console cons_ops = {
1541	.name = "ttyMSM",
1542	.write = qcom_geni_serial_console_write,
1543	.device = uart_console_device,
1544	.setup = qcom_geni_console_setup,
1545	.flags = CON_PRINTBUFFER,
1546	.index = -1,
1547	.data = &qcom_geni_console_driver,
1548};
1549
1550static struct uart_driver qcom_geni_console_driver = {
1551	.owner = THIS_MODULE,
1552	.driver_name = "qcom_geni_console",
1553	.dev_name = "ttyMSM",
1554	.nr =  GENI_UART_CONS_PORTS,
1555	.cons = &cons_ops,
1556};
1557#else
1558static int console_register(struct uart_driver *drv)
1559{
1560	return 0;
1561}
1562
1563static void console_unregister(struct uart_driver *drv)
1564{
1565}
1566#endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1567
1568static struct uart_driver qcom_geni_uart_driver = {
1569	.owner = THIS_MODULE,
1570	.driver_name = "qcom_geni_uart",
1571	.dev_name = "ttyHS",
1572	.nr =  GENI_UART_PORTS,
1573};
1574
1575static void qcom_geni_serial_pm(struct uart_port *uport,
1576		unsigned int new_state, unsigned int old_state)
1577{
1578	struct qcom_geni_serial_port *port = to_dev_port(uport);
1579
1580	/* If we've never been called, treat it as off */
1581	if (old_state == UART_PM_STATE_UNDEFINED)
1582		old_state = UART_PM_STATE_OFF;
1583
1584	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
1585		geni_icc_enable(&port->se);
1586		if (port->clk_rate)
1587			dev_pm_opp_set_rate(uport->dev, port->clk_rate);
1588		geni_se_resources_on(&port->se);
1589	} else if (new_state == UART_PM_STATE_OFF &&
1590			old_state == UART_PM_STATE_ON) {
1591		geni_se_resources_off(&port->se);
1592		dev_pm_opp_set_rate(uport->dev, 0);
1593		geni_icc_disable(&port->se);
1594	}
1595}
1596
1597static const struct uart_ops qcom_geni_console_pops = {
1598	.tx_empty = qcom_geni_serial_tx_empty,
1599	.stop_tx = qcom_geni_serial_stop_tx_fifo,
1600	.start_tx = qcom_geni_serial_start_tx_fifo,
1601	.stop_rx = qcom_geni_serial_stop_rx_fifo,
1602	.start_rx = qcom_geni_serial_start_rx_fifo,
1603	.set_termios = qcom_geni_serial_set_termios,
1604	.startup = qcom_geni_serial_startup,
1605	.request_port = qcom_geni_serial_request_port,
1606	.config_port = qcom_geni_serial_config_port,
1607	.shutdown = qcom_geni_serial_shutdown,
1608	.flush_buffer = qcom_geni_serial_flush_buffer,
1609	.type = qcom_geni_serial_get_type,
1610	.set_mctrl = qcom_geni_serial_set_mctrl,
1611	.get_mctrl = qcom_geni_serial_get_mctrl,
1612#ifdef CONFIG_CONSOLE_POLL
1613	.poll_get_char	= qcom_geni_serial_get_char,
1614	.poll_put_char	= qcom_geni_serial_poll_put_char,
1615	.poll_init = qcom_geni_serial_poll_init,
1616#endif
1617	.pm = qcom_geni_serial_pm,
1618};
1619
1620static const struct uart_ops qcom_geni_uart_pops = {
1621	.tx_empty = qcom_geni_serial_tx_empty,
1622	.stop_tx = qcom_geni_serial_stop_tx_dma,
1623	.start_tx = qcom_geni_serial_start_tx_dma,
1624	.start_rx = qcom_geni_serial_start_rx_dma,
1625	.stop_rx = qcom_geni_serial_stop_rx_dma,
1626	.set_termios = qcom_geni_serial_set_termios,
1627	.startup = qcom_geni_serial_startup,
1628	.request_port = qcom_geni_serial_request_port,
1629	.config_port = qcom_geni_serial_config_port,
1630	.shutdown = qcom_geni_serial_shutdown,
1631	.type = qcom_geni_serial_get_type,
1632	.set_mctrl = qcom_geni_serial_set_mctrl,
1633	.get_mctrl = qcom_geni_serial_get_mctrl,
1634	.pm = qcom_geni_serial_pm,
1635};
1636
1637static int qcom_geni_serial_probe(struct platform_device *pdev)
1638{
1639	int ret = 0;
1640	int line;
1641	struct qcom_geni_serial_port *port;
1642	struct uart_port *uport;
1643	struct resource *res;
1644	int irq;
 
1645	struct uart_driver *drv;
1646	const struct qcom_geni_device_data *data;
1647
1648	data = of_device_get_match_data(&pdev->dev);
1649	if (!data)
1650		return -EINVAL;
1651
1652	if (data->console) {
1653		drv = &qcom_geni_console_driver;
1654		line = of_alias_get_id(pdev->dev.of_node, "serial");
1655	} else {
1656		drv = &qcom_geni_uart_driver;
1657		line = of_alias_get_id(pdev->dev.of_node, "serial");
1658		if (line == -ENODEV) /* compat with non-standard aliases */
1659			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
1660	}
1661
1662	port = get_port_from_line(line, data->console);
1663	if (IS_ERR(port)) {
1664		dev_err(&pdev->dev, "Invalid line %d\n", line);
1665		return PTR_ERR(port);
1666	}
1667
1668	uport = &port->uport;
1669	/* Don't allow 2 drivers to access the same port */
1670	if (uport->private_data)
1671		return -ENODEV;
1672
1673	uport->dev = &pdev->dev;
1674	port->dev_data = data;
1675	port->se.dev = &pdev->dev;
1676	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1677	port->se.clk = devm_clk_get(&pdev->dev, "se");
1678	if (IS_ERR(port->se.clk)) {
1679		ret = PTR_ERR(port->se.clk);
1680		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1681		return ret;
1682	}
1683
1684	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1685	if (!res)
1686		return -EINVAL;
1687	uport->mapbase = res->start;
1688
1689	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1690	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1691	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1692
1693	if (!data->console) {
1694		port->rx_buf = devm_kzalloc(uport->dev,
1695					    DMA_RX_BUF_SIZE, GFP_KERNEL);
1696		if (!port->rx_buf)
1697			return -ENOMEM;
1698	}
1699
1700	ret = geni_icc_get(&port->se, NULL);
1701	if (ret)
1702		return ret;
1703	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
1704	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
1705
1706	/* Set BW for register access */
1707	ret = geni_icc_set_bw(&port->se);
1708	if (ret)
1709		return ret;
1710
1711	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1712			"qcom_geni_serial_%s%d",
1713			uart_console(uport) ? "console" : "uart", uport->line);
1714	if (!port->name)
1715		return -ENOMEM;
1716
1717	irq = platform_get_irq(pdev, 0);
1718	if (irq < 0)
1719		return irq;
1720	uport->irq = irq;
1721	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1722
1723	if (!data->console)
1724		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
1725
1726	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
1727		port->rx_tx_swap = true;
1728
1729	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
1730		port->cts_rts_swap = true;
1731
1732	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1733	if (ret)
1734		return ret;
1735	/* OPP table is optional */
1736	ret = devm_pm_opp_of_add_table(&pdev->dev);
1737	if (ret && ret != -ENODEV) {
1738		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1739		return ret;
1740	}
1741
1742	port->private_data.drv = drv;
1743	uport->private_data = &port->private_data;
1744	platform_set_drvdata(pdev, port);
 
 
 
 
 
1745
1746	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1747	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1748			IRQF_TRIGGER_HIGH, port->name, uport);
1749	if (ret) {
1750		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
 
1751		return ret;
1752	}
1753
1754	ret = uart_add_one_port(drv, uport);
1755	if (ret)
1756		return ret;
 
 
 
1757
1758	if (port->wakeup_irq > 0) {
1759		device_init_wakeup(&pdev->dev, true);
1760		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1761						port->wakeup_irq);
1762		if (ret) {
1763			device_init_wakeup(&pdev->dev, false);
1764			uart_remove_one_port(drv, uport);
1765			return ret;
1766		}
1767	}
1768
1769	return 0;
1770}
1771
1772static void qcom_geni_serial_remove(struct platform_device *pdev)
1773{
1774	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1775	struct uart_driver *drv = port->private_data.drv;
1776
1777	dev_pm_clear_wake_irq(&pdev->dev);
1778	device_init_wakeup(&pdev->dev, false);
1779	uart_remove_one_port(drv, &port->uport);
 
 
1780}
1781
1782static int qcom_geni_serial_suspend(struct device *dev)
1783{
1784	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1785	struct uart_port *uport = &port->uport;
1786	struct qcom_geni_private_data *private_data = uport->private_data;
1787
1788	/*
1789	 * This is done so we can hit the lowest possible state in suspend
1790	 * even with no_console_suspend
1791	 */
1792	if (uart_console(uport)) {
1793		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
1794		geni_icc_set_bw(&port->se);
1795	}
1796	return uart_suspend_port(private_data->drv, uport);
1797}
1798
1799static int qcom_geni_serial_resume(struct device *dev)
1800{
1801	int ret;
1802	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1803	struct uart_port *uport = &port->uport;
1804	struct qcom_geni_private_data *private_data = uport->private_data;
1805
1806	ret = uart_resume_port(private_data->drv, uport);
1807	if (uart_console(uport)) {
1808		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
1809		geni_icc_set_bw(&port->se);
1810	}
1811	return ret;
1812}
1813
1814static const struct qcom_geni_device_data qcom_geni_console_data = {
1815	.console = true,
1816	.mode = GENI_SE_FIFO,
1817};
 
 
 
 
 
1818
1819static const struct qcom_geni_device_data qcom_geni_uart_data = {
1820	.console = false,
1821	.mode = GENI_SE_DMA,
1822};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1823
1824static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1825	SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_suspend, qcom_geni_serial_resume)
 
 
 
 
 
1826};
1827
1828static const struct of_device_id qcom_geni_serial_match_table[] = {
1829	{
1830		.compatible = "qcom,geni-debug-uart",
1831		.data = &qcom_geni_console_data,
1832	},
1833	{
1834		.compatible = "qcom,geni-uart",
1835		.data = &qcom_geni_uart_data,
1836	},
1837	{}
1838};
1839MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1840
1841static struct platform_driver qcom_geni_serial_platform_driver = {
1842	.remove = qcom_geni_serial_remove,
1843	.probe = qcom_geni_serial_probe,
1844	.driver = {
1845		.name = "qcom_geni_serial",
1846		.of_match_table = qcom_geni_serial_match_table,
1847		.pm = &qcom_geni_serial_pm_ops,
1848	},
1849};
1850
1851static int __init qcom_geni_serial_init(void)
1852{
1853	int ret;
1854
1855	ret = console_register(&qcom_geni_console_driver);
1856	if (ret)
1857		return ret;
1858
1859	ret = uart_register_driver(&qcom_geni_uart_driver);
1860	if (ret) {
1861		console_unregister(&qcom_geni_console_driver);
1862		return ret;
1863	}
1864
1865	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
1866	if (ret) {
1867		console_unregister(&qcom_geni_console_driver);
1868		uart_unregister_driver(&qcom_geni_uart_driver);
1869	}
1870	return ret;
1871}
1872module_init(qcom_geni_serial_init);
1873
1874static void __exit qcom_geni_serial_exit(void)
1875{
1876	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1877	console_unregister(&qcom_geni_console_driver);
1878	uart_unregister_driver(&qcom_geni_uart_driver);
1879}
1880module_exit(qcom_geni_serial_exit);
1881
1882MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1883MODULE_LICENSE("GPL v2");