pch_uart.c 48.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
4
 */
5
#include <linux/kernel.h>
6
#include <linux/serial_reg.h>
7
#include <linux/slab.h>
8 9
#include <linux/module.h>
#include <linux/pci.h>
L
Liang Li 已提交
10
#include <linux/console.h>
11
#include <linux/serial_core.h>
12 13
#include <linux/tty.h>
#include <linux/tty_flip.h>
14 15
#include <linux/interrupt.h>
#include <linux/io.h>
16
#include <linux/dmi.h>
17 18
#include <linux/nmi.h>
#include <linux/delay.h>
19
#include <linux/of.h>
20

21
#include <linux/debugfs.h>
22 23 24 25 26 27 28 29 30
#include <linux/dmaengine.h>
#include <linux/pch_dma.h>

enum {
	PCH_UART_HANDLED_RX_INT_SHIFT,
	PCH_UART_HANDLED_TX_INT_SHIFT,
	PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
	PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
	PCH_UART_HANDLED_MS_INT_SHIFT,
31
	PCH_UART_HANDLED_LS_INT_SHIFT,
32 33 34 35
};

#define PCH_UART_DRIVER_DEVICE "ttyPCH"

36 37
/* Set the max number of UART port
 * Intel EG20T PCH: 4 port
38 39
 * LAPIS Semiconductor ML7213 IOH: 3 port
 * LAPIS Semiconductor ML7223 IOH: 2 port
40 41
*/
#define PCH_UART_NR	4
42 43 44 45 46 47 48 49 50

#define PCH_UART_HANDLED_RX_INT	(1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
#define PCH_UART_HANDLED_TX_INT	(1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
#define PCH_UART_HANDLED_RX_ERR_INT	(1<<((\
					PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
#define PCH_UART_HANDLED_RX_TRG_INT	(1<<((\
					PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
#define PCH_UART_HANDLED_MS_INT	(1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))

51 52
#define PCH_UART_HANDLED_LS_INT	(1<<((PCH_UART_HANDLED_LS_INT_SHIFT)<<1))

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#define PCH_UART_RBR		0x00
#define PCH_UART_THR		0x00

#define PCH_UART_IER_MASK	(PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
				PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
#define PCH_UART_IER_ERBFI	0x00000001
#define PCH_UART_IER_ETBEI	0x00000002
#define PCH_UART_IER_ELSI	0x00000004
#define PCH_UART_IER_EDSSI	0x00000008

#define PCH_UART_IIR_IP			0x00000001
#define PCH_UART_IIR_IID		0x00000006
#define PCH_UART_IIR_MSI		0x00000000
#define PCH_UART_IIR_TRI		0x00000002
#define PCH_UART_IIR_RRI		0x00000004
#define PCH_UART_IIR_REI		0x00000006
#define PCH_UART_IIR_TOI		0x00000008
#define PCH_UART_IIR_FIFO256		0x00000020
#define PCH_UART_IIR_FIFO64		PCH_UART_IIR_FIFO256
#define PCH_UART_IIR_FE			0x000000C0

#define PCH_UART_FCR_FIFOE		0x00000001
#define PCH_UART_FCR_RFR		0x00000002
#define PCH_UART_FCR_TFR		0x00000004
#define PCH_UART_FCR_DMS		0x00000008
#define PCH_UART_FCR_FIFO256		0x00000020
#define PCH_UART_FCR_RFTL		0x000000C0

#define PCH_UART_FCR_RFTL1		0x00000000
#define PCH_UART_FCR_RFTL64		0x00000040
#define PCH_UART_FCR_RFTL128		0x00000080
#define PCH_UART_FCR_RFTL224		0x000000C0
#define PCH_UART_FCR_RFTL16		PCH_UART_FCR_RFTL64
#define PCH_UART_FCR_RFTL32		PCH_UART_FCR_RFTL128
#define PCH_UART_FCR_RFTL56		PCH_UART_FCR_RFTL224
#define PCH_UART_FCR_RFTL4		PCH_UART_FCR_RFTL64
#define PCH_UART_FCR_RFTL8		PCH_UART_FCR_RFTL128
#define PCH_UART_FCR_RFTL14		PCH_UART_FCR_RFTL224
#define PCH_UART_FCR_RFTL_SHIFT		6

#define PCH_UART_LCR_WLS	0x00000003
#define PCH_UART_LCR_STB	0x00000004
#define PCH_UART_LCR_PEN	0x00000008
#define PCH_UART_LCR_EPS	0x00000010
#define PCH_UART_LCR_SP		0x00000020
#define PCH_UART_LCR_SB		0x00000040
#define PCH_UART_LCR_DLAB	0x00000080
#define PCH_UART_LCR_NP		0x00000000
#define PCH_UART_LCR_OP		PCH_UART_LCR_PEN
#define PCH_UART_LCR_EP		(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
#define PCH_UART_LCR_1P		(PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
#define PCH_UART_LCR_0P		(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
				PCH_UART_LCR_SP)

#define PCH_UART_LCR_5BIT	0x00000000
#define PCH_UART_LCR_6BIT	0x00000001
#define PCH_UART_LCR_7BIT	0x00000002
#define PCH_UART_LCR_8BIT	0x00000003

#define PCH_UART_MCR_DTR	0x00000001
#define PCH_UART_MCR_RTS	0x00000002
#define PCH_UART_MCR_OUT	0x0000000C
#define PCH_UART_MCR_LOOP	0x00000010
#define PCH_UART_MCR_AFE	0x00000020

#define PCH_UART_LSR_DR		0x00000001
#define PCH_UART_LSR_ERR	(1<<7)

#define PCH_UART_MSR_DCTS	0x00000001
#define PCH_UART_MSR_DDSR	0x00000002
#define PCH_UART_MSR_TERI	0x00000004
#define PCH_UART_MSR_DDCD	0x00000008
#define PCH_UART_MSR_CTS	0x00000010
#define PCH_UART_MSR_DSR	0x00000020
#define PCH_UART_MSR_RI		0x00000040
#define PCH_UART_MSR_DCD	0x00000080
#define PCH_UART_MSR_DELTA	(PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
				PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)

#define PCH_UART_DLL		0x00
#define PCH_UART_DLM		0x01

135 136
#define PCH_UART_BRCSR		0x0E

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
#define PCH_UART_IID_RLS	(PCH_UART_IIR_REI)
#define PCH_UART_IID_RDR	(PCH_UART_IIR_RRI)
#define PCH_UART_IID_RDR_TO	(PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
#define PCH_UART_IID_THRE	(PCH_UART_IIR_TRI)
#define PCH_UART_IID_MS		(PCH_UART_IIR_MSI)

#define PCH_UART_HAL_PARITY_NONE	(PCH_UART_LCR_NP)
#define PCH_UART_HAL_PARITY_ODD		(PCH_UART_LCR_OP)
#define PCH_UART_HAL_PARITY_EVEN	(PCH_UART_LCR_EP)
#define PCH_UART_HAL_PARITY_FIX1	(PCH_UART_LCR_1P)
#define PCH_UART_HAL_PARITY_FIX0	(PCH_UART_LCR_0P)
#define PCH_UART_HAL_5BIT		(PCH_UART_LCR_5BIT)
#define PCH_UART_HAL_6BIT		(PCH_UART_LCR_6BIT)
#define PCH_UART_HAL_7BIT		(PCH_UART_LCR_7BIT)
#define PCH_UART_HAL_8BIT		(PCH_UART_LCR_8BIT)
#define PCH_UART_HAL_STB1		0
#define PCH_UART_HAL_STB2		(PCH_UART_LCR_STB)

#define PCH_UART_HAL_CLR_TX_FIFO	(PCH_UART_FCR_TFR)
#define PCH_UART_HAL_CLR_RX_FIFO	(PCH_UART_FCR_RFR)
#define PCH_UART_HAL_CLR_ALL_FIFO	(PCH_UART_HAL_CLR_TX_FIFO | \
					PCH_UART_HAL_CLR_RX_FIFO)

#define PCH_UART_HAL_DMA_MODE0		0
#define PCH_UART_HAL_FIFO_DIS		0
#define PCH_UART_HAL_FIFO16		(PCH_UART_FCR_FIFOE)
#define PCH_UART_HAL_FIFO256		(PCH_UART_FCR_FIFOE | \
					PCH_UART_FCR_FIFO256)
#define PCH_UART_HAL_FIFO64		(PCH_UART_HAL_FIFO256)
#define PCH_UART_HAL_TRIGGER1		(PCH_UART_FCR_RFTL1)
#define PCH_UART_HAL_TRIGGER64		(PCH_UART_FCR_RFTL64)
#define PCH_UART_HAL_TRIGGER128		(PCH_UART_FCR_RFTL128)
#define PCH_UART_HAL_TRIGGER224		(PCH_UART_FCR_RFTL224)
#define PCH_UART_HAL_TRIGGER16		(PCH_UART_FCR_RFTL16)
#define PCH_UART_HAL_TRIGGER32		(PCH_UART_FCR_RFTL32)
#define PCH_UART_HAL_TRIGGER56		(PCH_UART_FCR_RFTL56)
#define PCH_UART_HAL_TRIGGER4		(PCH_UART_FCR_RFTL4)
#define PCH_UART_HAL_TRIGGER8		(PCH_UART_FCR_RFTL8)
#define PCH_UART_HAL_TRIGGER14		(PCH_UART_FCR_RFTL14)
#define PCH_UART_HAL_TRIGGER_L		(PCH_UART_FCR_RFTL64)
#define PCH_UART_HAL_TRIGGER_M		(PCH_UART_FCR_RFTL128)
#define PCH_UART_HAL_TRIGGER_H		(PCH_UART_FCR_RFTL224)

#define PCH_UART_HAL_RX_INT		(PCH_UART_IER_ERBFI)
#define PCH_UART_HAL_TX_INT		(PCH_UART_IER_ETBEI)
#define PCH_UART_HAL_RX_ERR_INT		(PCH_UART_IER_ELSI)
#define PCH_UART_HAL_MS_INT		(PCH_UART_IER_EDSSI)
#define PCH_UART_HAL_ALL_INT		(PCH_UART_IER_MASK)

#define PCH_UART_HAL_DTR		(PCH_UART_MCR_DTR)
#define PCH_UART_HAL_RTS		(PCH_UART_MCR_RTS)
#define PCH_UART_HAL_OUT		(PCH_UART_MCR_OUT)
#define PCH_UART_HAL_LOOP		(PCH_UART_MCR_LOOP)
#define PCH_UART_HAL_AFE		(PCH_UART_MCR_AFE)

192 193
#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)

194 195 196 197
#define DEFAULT_UARTCLK   1843200 /*   1.8432 MHz */
#define CMITC_UARTCLK   192000000 /* 192.0000 MHz */
#define FRI2_64_UARTCLK  64000000 /*  64.0000 MHz */
#define FRI2_48_UARTCLK  48000000 /*  48.0000 MHz */
198
#define NTC1_UARTCLK     64000000 /*  64.0000 MHz */
199
#define MINNOW_UARTCLK   50000000 /*  50.0000 MHz */
200

201 202 203 204 205 206 207 208 209 210 211 212 213
struct pch_uart_buffer {
	unsigned char *buf;
	int size;
};

struct eg20t_port {
	struct uart_port port;
	int port_type;
	void __iomem *membase;
	resource_size_t mapbase;
	unsigned int iobase;
	struct pci_dev *pdev;
	int fifo_size;
214
	unsigned int uartclk;
215 216 217 218 219 220 221 222
	int start_tx;
	int start_rx;
	int tx_empty;
	int trigger;
	int trigger_level;
	struct pch_uart_buffer rxbuf;
	unsigned int dmsr;
	unsigned int fcr;
223
	unsigned int mcr;
224 225 226 227 228 229 230
	unsigned int use_dma;
	struct dma_async_tx_descriptor	*desc_tx;
	struct dma_async_tx_descriptor	*desc_rx;
	struct pch_dma_slave		param_tx;
	struct pch_dma_slave		param_rx;
	struct dma_chan			*chan_tx;
	struct dma_chan			*chan_rx;
231 232
	struct scatterlist		*sg_tx_p;
	int				nent;
233
	int				orig_nent;
234 235 236 237
	struct scatterlist		sg_rx;
	int				tx_dma_use;
	void				*rx_buf_virt;
	dma_addr_t			rx_buf_dma;
238

239 240
#define IRQ_NAME_SIZE 17
	char				irq_name[IRQ_NAME_SIZE];
241 242 243

	/* protect the eg20t_port private structure and io access to membase */
	spinlock_t lock;
244 245
};

246 247
/**
 * struct pch_uart_driver_data - private data structure for UART-DMA
248
 * @port_type:			The type of UART port
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
 * @line_no:			UART port line number (0, 1, 2...)
 */
struct pch_uart_driver_data {
	int port_type;
	int line_no;
};

enum pch_uart_num_t {
	pch_et20t_uart0 = 0,
	pch_et20t_uart1,
	pch_et20t_uart2,
	pch_et20t_uart3,
	pch_ml7213_uart0,
	pch_ml7213_uart1,
	pch_ml7213_uart2,
264 265
	pch_ml7223_uart0,
	pch_ml7223_uart1,
266 267
	pch_ml7831_uart0,
	pch_ml7831_uart1,
268 269 270
};

static struct pch_uart_driver_data drv_dat[] = {
271 272 273 274 275 276 277 278 279 280 281
	[pch_et20t_uart0] = {PORT_PCH_8LINE, 0},
	[pch_et20t_uart1] = {PORT_PCH_2LINE, 1},
	[pch_et20t_uart2] = {PORT_PCH_2LINE, 2},
	[pch_et20t_uart3] = {PORT_PCH_2LINE, 3},
	[pch_ml7213_uart0] = {PORT_PCH_8LINE, 0},
	[pch_ml7213_uart1] = {PORT_PCH_2LINE, 1},
	[pch_ml7213_uart2] = {PORT_PCH_2LINE, 2},
	[pch_ml7223_uart0] = {PORT_PCH_8LINE, 0},
	[pch_ml7223_uart1] = {PORT_PCH_2LINE, 1},
	[pch_ml7831_uart0] = {PORT_PCH_8LINE, 0},
	[pch_ml7831_uart1] = {PORT_PCH_2LINE, 1},
282 283
};

284 285 286
#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
#endif
287
static unsigned int default_baud = 9600;
288
static unsigned int user_uartclk = 0;
289 290 291 292 293
static const int trigger_level_256[4] = { 1, 64, 128, 224 };
static const int trigger_level_64[4] = { 1, 16, 32, 56 };
static const int trigger_level_16[4] = { 1, 4, 8, 14 };
static const int trigger_level_1[4] = { 1, 1, 1, 1 };

294
#define PCH_REGS_BUFSIZE	1024
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309

static ssize_t port_show_regs(struct file *file, char __user *user_buf,
				size_t count, loff_t *ppos)
{
	struct eg20t_port *priv = file->private_data;
	char *buf;
	u32 len = 0;
	ssize_t ret;
	unsigned char lcr;

	buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
	if (!buf)
		return 0;

310
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
311 312
			"PCH EG20T port[%d] regs:\n", priv->port.line);

313
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
314
			"=================================\n");
315
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
316
			"IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
317
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
318
			"IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
319
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
320
			"LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
321
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
322
			"MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
323
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
324
			"LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
325
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
326
			"MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
327
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
328 329 330 331 332
			"BRCSR: \t0x%02x\n",
			ioread8(priv->membase + PCH_UART_BRCSR));

	lcr = ioread8(priv->membase + UART_LCR);
	iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
333
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
334
			"DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
335
	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
336 337 338 339 340 341 342 343 344 345 346 347 348
			"DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
	iowrite8(lcr, priv->membase + UART_LCR);

	if (len > PCH_REGS_BUFSIZE)
		len = PCH_REGS_BUFSIZE;

	ret =  simple_read_from_buffer(user_buf, count, ppos, buf, len);
	kfree(buf);
	return ret;
}

static const struct file_operations port_regs_ops = {
	.owner		= THIS_MODULE,
349
	.open		= simple_open,
350 351 352 353
	.read		= port_show_regs,
	.llseek		= default_llseek,
};

354
static const struct dmi_system_id pch_uart_dmi_table[] = {
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	{
		.ident = "CM-iTC",
		{
			DMI_MATCH(DMI_BOARD_NAME, "CM-iTC"),
		},
		(void *)CMITC_UARTCLK,
	},
	{
		.ident = "FRI2",
		{
			DMI_MATCH(DMI_BIOS_VERSION, "FRI2"),
		},
		(void *)FRI2_64_UARTCLK,
	},
	{
		.ident = "Fish River Island II",
		{
			DMI_MATCH(DMI_PRODUCT_NAME, "Fish River Island II"),
		},
		(void *)FRI2_48_UARTCLK,
	},
	{
		.ident = "COMe-mTT",
		{
			DMI_MATCH(DMI_BOARD_NAME, "COMe-mTT"),
		},
		(void *)NTC1_UARTCLK,
	},
	{
		.ident = "nanoETXexpress-TT",
		{
			DMI_MATCH(DMI_BOARD_NAME, "nanoETXexpress-TT"),
		},
		(void *)NTC1_UARTCLK,
	},
	{
		.ident = "MinnowBoard",
		{
			DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
		},
		(void *)MINNOW_UARTCLK,
	},
397
	{ }
398 399
};

400
/* Return UART clock, checking for board specific clocks. */
401
static unsigned int pch_uart_get_uartclk(void)
402
{
403
	const struct dmi_system_id *d;
404

405 406 407
	if (user_uartclk)
		return user_uartclk;

408 409
	d = dmi_first_match(pch_uart_dmi_table);
	if (d)
410
		return (unsigned long)d->driver_data;
411

412 413 414
	return DEFAULT_UARTCLK;
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
					  unsigned int flag)
{
	u8 ier = ioread8(priv->membase + UART_IER);
	ier |= flag & PCH_UART_IER_MASK;
	iowrite8(ier, priv->membase + UART_IER);
}

static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
					   unsigned int flag)
{
	u8 ier = ioread8(priv->membase + UART_IER);
	ier &= ~(flag & PCH_UART_IER_MASK);
	iowrite8(ier, priv->membase + UART_IER);
}

431
static int pch_uart_hal_set_line(struct eg20t_port *priv, unsigned int baud,
432 433 434 435 436 437
				 unsigned int parity, unsigned int bits,
				 unsigned int stb)
{
	unsigned int dll, dlm, lcr;
	int div;

438
	div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
439
	if (div < 0 || USHRT_MAX <= div) {
440
		dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
441 442 443 444 445 446 447
		return -EINVAL;
	}

	dll = (unsigned int)div & 0x00FFU;
	dlm = ((unsigned int)div >> 8) & 0x00FFU;

	if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
448
		dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
449 450 451 452
		return -EINVAL;
	}

	if (bits & ~PCH_UART_LCR_WLS) {
453
		dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
454 455 456 457
		return -EINVAL;
	}

	if (stb & ~PCH_UART_LCR_STB) {
458
		dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
459 460 461 462 463 464 465
		return -EINVAL;
	}

	lcr = parity;
	lcr |= bits;
	lcr |= stb;

466
	dev_dbg(priv->port.dev, "%s:baud = %u, div = %04x, lcr = %02x (%lu)\n",
467 468 469 470 471 472 473 474 475 476 477 478 479
		 __func__, baud, div, lcr, jiffies);
	iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
	iowrite8(dll, priv->membase + PCH_UART_DLL);
	iowrite8(dlm, priv->membase + PCH_UART_DLM);
	iowrite8(lcr, priv->membase + UART_LCR);

	return 0;
}

static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
				    unsigned int flag)
{
	if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
480 481
		dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
			__func__, flag);
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
		return -EINVAL;
	}

	iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
	iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
		 priv->membase + UART_FCR);
	iowrite8(priv->fcr, priv->membase + UART_FCR);

	return 0;
}

static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
				 unsigned int dmamode,
				 unsigned int fifo_size, unsigned int trigger)
{
	u8 fcr;

	if (dmamode & ~PCH_UART_FCR_DMS) {
500 501
		dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
			__func__, dmamode);
502 503 504 505
		return -EINVAL;
	}

	if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
506 507
		dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
			__func__, fifo_size);
508 509 510 511
		return -EINVAL;
	}

	if (trigger & ~PCH_UART_FCR_RFTL) {
512 513
		dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
			__func__, trigger);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
		return -EINVAL;
	}

	switch (priv->fifo_size) {
	case 256:
		priv->trigger_level =
		    trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
		break;
	case 64:
		priv->trigger_level =
		    trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
		break;
	case 16:
		priv->trigger_level =
		    trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
		break;
	default:
		priv->trigger_level =
		    trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
		break;
	}
	fcr =
	    dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
	iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
	iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
		 priv->membase + UART_FCR);
	iowrite8(fcr, priv->membase + UART_FCR);
	priv->fcr = fcr;

	return 0;
}

static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
{
548 549 550
	unsigned int msr = ioread8(priv->membase + UART_MSR);
	priv->dmsr = msr & PCH_UART_MSR_DELTA;
	return (u8)msr;
551 552
}

T
Tomoya MORINAGA 已提交
553
static void pch_uart_hal_write(struct eg20t_port *priv,
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
			      const unsigned char *buf, int tx_size)
{
	int i;
	unsigned int thr;

	for (i = 0; i < tx_size;) {
		thr = buf[i++];
		iowrite8(thr, priv->membase + PCH_UART_THR);
	}
}

static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
			     int rx_size)
{
	int i;
	u8 rbr, lsr;
L
Liang Li 已提交
570
	struct uart_port *port = &priv->port;
571 572 573

	lsr = ioread8(priv->membase + UART_LSR);
	for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
L
Liang Li 已提交
574
	     i < rx_size && lsr & (UART_LSR_DR | UART_LSR_BI);
575 576
	     lsr = ioread8(priv->membase + UART_LSR)) {
		rbr = ioread8(priv->membase + PCH_UART_RBR);
L
Liang Li 已提交
577 578 579 580 581 582

		if (lsr & UART_LSR_BI) {
			port->icount.brk++;
			if (uart_handle_break(port))
				continue;
		}
583 584
		if (uart_handle_sysrq_char(port, rbr))
			continue;
L
Liang Li 已提交
585

586 587 588 589 590
		buf[i++] = rbr;
	}
	return i;
}

T
Tomoya MORINAGA 已提交
591
static unsigned char pch_uart_hal_get_iid(struct eg20t_port *priv)
592
{
T
Tomoya MORINAGA 已提交
593 594
	return ioread8(priv->membase + UART_IIR) &\
		      (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
}

static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
{
	return ioread8(priv->membase + UART_LSR);
}

static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
{
	unsigned int lcr;

	lcr = ioread8(priv->membase + UART_LCR);
	if (on)
		lcr |= PCH_UART_LCR_SB;
	else
		lcr &= ~PCH_UART_LCR_SB;

	iowrite8(lcr, priv->membase + UART_LCR);
}

static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
		   int size)
{
J
Jiri Slaby 已提交
618 619
	struct uart_port *port = &priv->port;
	struct tty_port *tport = &port->state->port;
620

J
Jiri Slaby 已提交
621
	tty_insert_flip_string(tport, buf, size);
J
Jiri Slaby 已提交
622
	tty_flip_buffer_push(tport);
623 624 625 626 627 628

	return 0;
}

static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
{
629
	int ret = 0;
630 631 632
	struct uart_port *port = &priv->port;

	if (port->x_char) {
633 634
		dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
			__func__, port->x_char, jiffies);
635 636 637 638 639 640 641 642 643 644 645 646
		buf[0] = port->x_char;
		port->x_char = 0;
		ret = 1;
	}

	return ret;
}

static int dma_push_rx(struct eg20t_port *priv, int size)
{
	int room;
	struct uart_port *port = &priv->port;
647
	struct tty_port *tport = &port->state->port;
648

649
	room = tty_buffer_request_room(tport, size);
650 651 652 653 654

	if (room < size)
		dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
			 size - room);
	if (!room)
655
		return 0;
656

J
Jiri Slaby 已提交
657
	tty_insert_flip_string(tport, sg_virt(&priv->sg_rx), size);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

	port->icount.rx += room;

	return room;
}

static void pch_free_dma(struct uart_port *port)
{
	struct eg20t_port *priv;
	priv = container_of(port, struct eg20t_port, port);

	if (priv->chan_tx) {
		dma_release_channel(priv->chan_tx);
		priv->chan_tx = NULL;
	}
	if (priv->chan_rx) {
		dma_release_channel(priv->chan_rx);
		priv->chan_rx = NULL;
	}
677 678 679 680 681 682 683

	if (priv->rx_buf_dma) {
		dma_free_coherent(port->dev, port->fifosize, priv->rx_buf_virt,
				  priv->rx_buf_dma);
		priv->rx_buf_virt = NULL;
		priv->rx_buf_dma = 0;
	}
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

	return;
}

static bool filter(struct dma_chan *chan, void *slave)
{
	struct pch_dma_slave *param = slave;

	if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
						  chan->device->dev)) {
		chan->private = param;
		return true;
	} else {
		return false;
	}
}

static void pch_request_dma(struct uart_port *port)
{
	dma_cap_mask_t mask;
	struct dma_chan *chan;
	struct pci_dev *dma_dev;
	struct pch_dma_slave *param;
	struct eg20t_port *priv =
				container_of(port, struct eg20t_port, port);
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);

712 713 714 715
	/* Get DMA's dev information */
	dma_dev = pci_get_slot(priv->pdev->bus,
			PCI_DEVFN(PCI_SLOT(priv->pdev->devfn), 0));

716 717 718
	/* Set Tx DMA */
	param = &priv->param_tx;
	param->dma_dev = &dma_dev->dev;
719 720
	param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */

721 722 723
	param->tx_reg = port->mapbase + UART_TX;
	chan = dma_request_channel(mask, filter, param);
	if (!chan) {
724 725
		dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
			__func__);
726 727 728 729 730 731 732
		return;
	}
	priv->chan_tx = chan;

	/* Set Rx DMA */
	param = &priv->param_rx;
	param->dma_dev = &dma_dev->dev;
733 734
	param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */

735 736 737
	param->rx_reg = port->mapbase + UART_RX;
	chan = dma_request_channel(mask, filter, param);
	if (!chan) {
738 739
		dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
			__func__);
740
		dma_release_channel(priv->chan_tx);
741
		priv->chan_tx = NULL;
742 743 744 745 746 747 748 749 750 751 752 753 754
		return;
	}

	/* Get Consistent memory for DMA */
	priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
				    &priv->rx_buf_dma, GFP_KERNEL);
	priv->chan_rx = chan;
}

static void pch_dma_rx_complete(void *arg)
{
	struct eg20t_port *priv = arg;
	struct uart_port *port = &priv->port;
755
	int count;
756

757 758 759
	dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
	count = dma_push_rx(priv, priv->trigger_level);
	if (count)
J
Jiri Slaby 已提交
760
		tty_flip_buffer_push(&port->state->port);
761
	async_tx_ack(priv->desc_rx);
762 763
	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
					    PCH_UART_HAL_RX_ERR_INT);
764 765 766 767 768 769 770
}

static void pch_dma_tx_complete(void *arg)
{
	struct eg20t_port *priv = arg;
	struct uart_port *port = &priv->port;
	struct circ_buf *xmit = &port->state->xmit;
771 772
	struct scatterlist *sg = priv->sg_tx_p;
	int i;
773

774 775 776 777
	for (i = 0; i < priv->nent; i++, sg++) {
		xmit->tail += sg_dma_len(sg);
		port->icount.tx += sg_dma_len(sg);
	}
778 779
	xmit->tail &= UART_XMIT_SIZE - 1;
	async_tx_ack(priv->desc_tx);
780
	dma_unmap_sg(port->dev, sg, priv->orig_nent, DMA_TO_DEVICE);
781
	priv->tx_dma_use = 0;
782
	priv->nent = 0;
783
	priv->orig_nent = 0;
784
	kfree(priv->sg_tx_p);
785
	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
786 787
}

T
Tomoya MORINAGA 已提交
788
static int pop_tx(struct eg20t_port *priv, int size)
789 790 791 792 793 794 795 796 797 798 799 800
{
	int count = 0;
	struct uart_port *port = &priv->port;
	struct circ_buf *xmit = &port->state->xmit;

	if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
		goto pop_tx_end;

	do {
		int cnt_to_end =
		    CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
		int sz = min(size - count, cnt_to_end);
T
Tomoya MORINAGA 已提交
801
		pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
802 803 804 805 806
		xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
		count += sz;
	} while (!uart_circ_empty(xmit) && count < size);

pop_tx_end:
807
	dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
808 809 810 811 812 813 814 815 816 817 818
		 count, size - count, jiffies);

	return count;
}

static int handle_rx_to(struct eg20t_port *priv)
{
	struct pch_uart_buffer *buf;
	int rx_size;
	int ret;
	if (!priv->start_rx) {
819 820
		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
						     PCH_UART_HAL_RX_ERR_INT);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
		return 0;
	}
	buf = &priv->rxbuf;
	do {
		rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
		ret = push_rx(priv, buf->buf, rx_size);
		if (ret)
			return 0;
	} while (rx_size == buf->size);

	return PCH_UART_HANDLED_RX_INT;
}

static int handle_rx(struct eg20t_port *priv)
{
	return handle_rx_to(priv);
}

static int dma_handle_rx(struct eg20t_port *priv)
{
	struct uart_port *port = &priv->port;
	struct dma_async_tx_descriptor *desc;
	struct scatterlist *sg;

	priv = container_of(port, struct eg20t_port, port);
	sg = &priv->sg_rx;

	sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */

850
	sg_dma_len(sg) = priv->trigger_level;
851 852

	sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
853
		     sg_dma_len(sg), offset_in_page(priv->rx_buf_virt));
854 855 856

	sg_dma_address(sg) = priv->rx_buf_dma;

857
	desc = dmaengine_prep_slave_sg(priv->chan_rx,
858
			sg, 1, DMA_DEV_TO_MEM,
859 860
			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
	if (!desc)
		return 0;

	priv->desc_rx = desc;
	desc->callback = pch_dma_rx_complete;
	desc->callback_param = priv;
	desc->tx_submit(desc);
	dma_async_issue_pending(priv->chan_rx);

	return PCH_UART_HANDLED_RX_INT;
}

static unsigned int handle_tx(struct eg20t_port *priv)
{
	struct uart_port *port = &priv->port;
	struct circ_buf *xmit = &port->state->xmit;
	int fifo_size;
	int tx_size;
	int size;
	int tx_empty;

	if (!priv->start_tx) {
883 884
		dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
			__func__, jiffies);
885 886 887 888 889 890 891 892 893 894 895 896 897 898
		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
		priv->tx_empty = 1;
		return 0;
	}

	fifo_size = max(priv->fifo_size, 1);
	tx_empty = 1;
	if (pop_tx_x(priv, xmit->buf)) {
		pch_uart_hal_write(priv, xmit->buf, 1);
		port->icount.tx++;
		tx_empty = 0;
		fifo_size--;
	}
	size = min(xmit->head - xmit->tail, fifo_size);
899 900 901
	if (size < 0)
		size = fifo_size;

T
Tomoya MORINAGA 已提交
902
	tx_size = pop_tx(priv, size);
903
	if (tx_size > 0) {
T
Tomoya MORINAGA 已提交
904
		port->icount.tx += tx_size;
905 906 907 908 909
		tx_empty = 0;
	}

	priv->tx_empty = tx_empty;

910
	if (tx_empty) {
911
		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
912 913
		uart_write_wakeup(port);
	}
914 915 916 917 918 919 920 921

	return PCH_UART_HANDLED_TX_INT;
}

static unsigned int dma_handle_tx(struct eg20t_port *priv)
{
	struct uart_port *port = &priv->port;
	struct circ_buf *xmit = &port->state->xmit;
922
	struct scatterlist *sg;
923 924 925
	int nent;
	int fifo_size;
	struct dma_async_tx_descriptor *desc;
926 927 928 929 930
	int num;
	int i;
	int bytes;
	int size;
	int rem;
931 932

	if (!priv->start_tx) {
933 934
		dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
			__func__, jiffies);
935 936 937 938 939
		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
		priv->tx_empty = 1;
		return 0;
	}

940 941 942 943 944 945 946 947
	if (priv->tx_dma_use) {
		dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
			__func__, jiffies);
		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
		priv->tx_empty = 1;
		return 0;
	}

948 949 950 951 952 953 954
	fifo_size = max(priv->fifo_size, 1);
	if (pop_tx_x(priv, xmit->buf)) {
		pch_uart_hal_write(priv, xmit->buf, 1);
		port->icount.tx++;
		fifo_size--;
	}

955 956 957 958
	bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
			     UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
			     xmit->tail, UART_XMIT_SIZE));
	if (!bytes) {
959
		dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
960 961 962 963 964 965 966 967 968 969 970 971 972 973
		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
		uart_write_wakeup(port);
		return 0;
	}

	if (bytes > fifo_size) {
		num = bytes / fifo_size + 1;
		size = fifo_size;
		rem = bytes % fifo_size;
	} else {
		num = 1;
		size = bytes;
		rem = bytes;
	}
974

975 976 977
	dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
		__func__, num, size, rem);

978 979
	priv->tx_dma_use = 1;

J
Julia Lawall 已提交
980
	priv->sg_tx_p = kmalloc_array(num, sizeof(struct scatterlist), GFP_ATOMIC);
981 982 983 984
	if (!priv->sg_tx_p) {
		dev_err(priv->port.dev, "%s:kzalloc Failed\n", __func__);
		return 0;
	}
985

986 987
	sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
	sg = priv->sg_tx_p;
988

989 990 991 992 993 994 995 996 997 998 999
	for (i = 0; i < num; i++, sg++) {
		if (i == (num - 1))
			sg_set_page(sg, virt_to_page(xmit->buf),
				    rem, fifo_size * i);
		else
			sg_set_page(sg, virt_to_page(xmit->buf),
				    size, fifo_size * i);
	}

	sg = priv->sg_tx_p;
	nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
1000
	if (!nent) {
1001
		dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
1002 1003
		return 0;
	}
1004
	priv->orig_nent = num;
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	priv->nent = nent;

	for (i = 0; i < nent; i++, sg++) {
		sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
			      fifo_size * i;
		sg_dma_address(sg) = (sg_dma_address(sg) &
				    ~(UART_XMIT_SIZE - 1)) + sg->offset;
		if (i == (nent - 1))
			sg_dma_len(sg) = rem;
		else
			sg_dma_len(sg) = size;
	}
1017

1018
	desc = dmaengine_prep_slave_sg(priv->chan_tx,
1019
					priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
1020
					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1021
	if (!desc) {
1022
		dev_err(priv->port.dev, "%s:dmaengine_prep_slave_sg Failed\n",
1023
			__func__);
1024 1025
		return 0;
	}
1026
	dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	priv->desc_tx = desc;
	desc->callback = pch_dma_tx_complete;
	desc->callback_param = priv;

	desc->tx_submit(desc);

	dma_async_issue_pending(priv->chan_tx);

	return PCH_UART_HANDLED_TX_INT;
}

static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
{
1040 1041 1042 1043
	struct uart_port *port = &priv->port;
	struct tty_struct *tty = tty_port_tty_get(&port->state->port);
	char   *error_msg[5] = {};
	int    i = 0;
1044 1045

	if (lsr & PCH_UART_LSR_ERR)
1046 1047 1048 1049 1050 1051
		error_msg[i++] = "Error data in FIFO\n";

	if (lsr & UART_LSR_FE) {
		port->icount.frame++;
		error_msg[i++] = "  Framing Error\n";
	}
1052

1053 1054 1055 1056
	if (lsr & UART_LSR_PE) {
		port->icount.parity++;
		error_msg[i++] = "  Parity Error\n";
	}
1057

1058 1059 1060 1061
	if (lsr & UART_LSR_OE) {
		port->icount.overrun++;
		error_msg[i++] = "  Overrun Error\n";
	}
1062

1063 1064 1065
	if (tty == NULL) {
		for (i = 0; error_msg[i] != NULL; i++)
			dev_err(&priv->pdev->dev, error_msg[i]);
1066 1067
	} else {
		tty_kref_put(tty);
1068
	}
1069 1070 1071 1072 1073 1074 1075 1076
}

static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
{
	struct eg20t_port *priv = dev_id;
	unsigned int handled;
	u8 lsr;
	int ret = 0;
T
Tomoya MORINAGA 已提交
1077
	unsigned char iid;
1078
	unsigned long flags;
1079 1080
	int next = 1;
	u8 msr;
1081

1082
	spin_lock_irqsave(&priv->lock, flags);
1083
	handled = 0;
1084 1085 1086 1087
	while (next) {
		iid = pch_uart_hal_get_iid(priv);
		if (iid & PCH_UART_IIR_IP) /* No Interrupt */
			break;
1088 1089 1090 1091 1092 1093 1094
		switch (iid) {
		case PCH_UART_IID_RLS:	/* Receiver Line Status */
			lsr = pch_uart_hal_get_line_status(priv);
			if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
						UART_LSR_PE | UART_LSR_OE)) {
				pch_uart_err_ir(priv, lsr);
				ret = PCH_UART_HANDLED_RX_ERR_INT;
1095 1096
			} else {
				ret = PCH_UART_HANDLED_LS_INT;
1097 1098 1099
			}
			break;
		case PCH_UART_IID_RDR:	/* Received Data Ready */
1100 1101
			if (priv->use_dma) {
				pch_uart_hal_disable_interrupt(priv,
1102 1103
						PCH_UART_HAL_RX_INT |
						PCH_UART_HAL_RX_ERR_INT);
1104
				ret = dma_handle_rx(priv);
1105 1106
				if (!ret)
					pch_uart_hal_enable_interrupt(priv,
1107 1108
						PCH_UART_HAL_RX_INT |
						PCH_UART_HAL_RX_ERR_INT);
1109
			} else {
1110
				ret = handle_rx(priv);
1111
			}
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
			break;
		case PCH_UART_IID_RDR_TO:	/* Received Data Ready
						   (FIFO Timeout) */
			ret = handle_rx_to(priv);
			break;
		case PCH_UART_IID_THRE:	/* Transmitter Holding Register
						   Empty */
			if (priv->use_dma)
				ret = dma_handle_tx(priv);
			else
				ret = handle_tx(priv);
			break;
		case PCH_UART_IID_MS:	/* Modem Status */
1125 1126 1127 1128 1129 1130
			msr = pch_uart_hal_get_modem(priv);
			next = 0; /* MS ir prioirty is the lowest. So, MS ir
				     means final interrupt */
			if ((msr & UART_MSR_ANY_DELTA) == 0)
				break;
			ret |= PCH_UART_HANDLED_MS_INT;
1131 1132
			break;
		default:	/* Never junp to this label */
1133
			dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
1134
				iid, jiffies);
1135
			ret = -1;
1136
			next = 0;
1137 1138 1139 1140 1141
			break;
		}
		handled |= (unsigned int)ret;
	}

1142
	spin_unlock_irqrestore(&priv->lock, flags);
1143 1144 1145 1146 1147 1148 1149 1150
	return IRQ_RETVAL(handled);
}

/* This function tests whether the transmitter fifo and shifter for the port
						described by 'port' is empty. */
static unsigned int pch_uart_tx_empty(struct uart_port *port)
{
	struct eg20t_port *priv;
1151

1152 1153
	priv = container_of(port, struct eg20t_port, port);
	if (priv->tx_empty)
1154
		return TIOCSER_TEMT;
1155
	else
1156
		return 0;
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
}

/* Returns the current state of modem control inputs. */
static unsigned int pch_uart_get_mctrl(struct uart_port *port)
{
	struct eg20t_port *priv;
	u8 modem;
	unsigned int ret = 0;

	priv = container_of(port, struct eg20t_port, port);
	modem = pch_uart_hal_get_modem(priv);

	if (modem & UART_MSR_DCD)
		ret |= TIOCM_CAR;

	if (modem & UART_MSR_RI)
		ret |= TIOCM_RNG;

	if (modem & UART_MSR_DSR)
		ret |= TIOCM_DSR;

	if (modem & UART_MSR_CTS)
		ret |= TIOCM_CTS;

	return ret;
}

static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	u32 mcr = 0;
	struct eg20t_port *priv = container_of(port, struct eg20t_port, port);

	if (mctrl & TIOCM_DTR)
		mcr |= UART_MCR_DTR;
	if (mctrl & TIOCM_RTS)
		mcr |= UART_MCR_RTS;
	if (mctrl & TIOCM_LOOP)
		mcr |= UART_MCR_LOOP;

1196 1197 1198 1199 1200
	if (priv->mcr & UART_MCR_AFE)
		mcr |= UART_MCR_AFE;

	if (mctrl)
		iowrite8(mcr, priv->membase + UART_MCR);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
}

static void pch_uart_stop_tx(struct uart_port *port)
{
	struct eg20t_port *priv;
	priv = container_of(port, struct eg20t_port, port);
	priv->start_tx = 0;
	priv->tx_dma_use = 0;
}

static void pch_uart_start_tx(struct uart_port *port)
{
	struct eg20t_port *priv;

	priv = container_of(port, struct eg20t_port, port);

1217 1218 1219 1220
	if (priv->use_dma) {
		if (priv->tx_dma_use) {
			dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
				__func__);
1221
			return;
1222 1223
		}
	}
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

	priv->start_tx = 1;
	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
}

static void pch_uart_stop_rx(struct uart_port *port)
{
	struct eg20t_port *priv;
	priv = container_of(port, struct eg20t_port, port);
	priv->start_rx = 0;
1234 1235
	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
					     PCH_UART_HAL_RX_ERR_INT);
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
}

/* Enable the modem status interrupts. */
static void pch_uart_enable_ms(struct uart_port *port)
{
	struct eg20t_port *priv;
	priv = container_of(port, struct eg20t_port, port);
	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
}

/* Control the transmission of a break signal. */
static void pch_uart_break_ctl(struct uart_port *port, int ctl)
{
	struct eg20t_port *priv;
	unsigned long flags;

	priv = container_of(port, struct eg20t_port, port);
1253
	spin_lock_irqsave(&priv->lock, flags);
1254
	pch_uart_hal_set_break(priv, ctl);
1255
	spin_unlock_irqrestore(&priv->lock, flags);
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
}

/* Grab any interrupt resources and initialise any low level driver state. */
static int pch_uart_startup(struct uart_port *port)
{
	struct eg20t_port *priv;
	int ret;
	int fifo_size;
	int trigger_level;

	priv = container_of(port, struct eg20t_port, port);
	priv->tx_empty = 1;
1268 1269

	if (port->uartclk)
1270
		priv->uartclk = port->uartclk;
1271
	else
1272
		port->uartclk = priv->uartclk;
1273

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
	ret = pch_uart_hal_set_line(priv, default_baud,
			      PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
			      PCH_UART_HAL_STB1);
	if (ret)
		return ret;

	switch (priv->fifo_size) {
	case 256:
		fifo_size = PCH_UART_HAL_FIFO256;
		break;
	case 64:
		fifo_size = PCH_UART_HAL_FIFO64;
		break;
	case 16:
		fifo_size = PCH_UART_HAL_FIFO16;
1290
		break;
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	case 1:
	default:
		fifo_size = PCH_UART_HAL_FIFO_DIS;
		break;
	}

	switch (priv->trigger) {
	case PCH_UART_HAL_TRIGGER1:
		trigger_level = 1;
		break;
	case PCH_UART_HAL_TRIGGER_L:
		trigger_level = priv->fifo_size / 4;
		break;
	case PCH_UART_HAL_TRIGGER_M:
		trigger_level = priv->fifo_size / 2;
		break;
	case PCH_UART_HAL_TRIGGER_H:
	default:
		trigger_level = priv->fifo_size - (priv->fifo_size / 8);
		break;
	}

	priv->trigger_level = trigger_level;
	ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
				    fifo_size, priv->trigger);
	if (ret < 0)
		return ret;

	ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1320
			priv->irq_name, priv);
1321 1322 1323 1324 1325 1326 1327
	if (ret < 0)
		return ret;

	if (priv->use_dma)
		pch_request_dma(port);

	priv->start_rx = 1;
1328 1329
	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
					    PCH_UART_HAL_RX_ERR_INT);
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
	uart_update_timeout(port, CS8, default_baud);

	return 0;
}

static void pch_uart_shutdown(struct uart_port *port)
{
	struct eg20t_port *priv;
	int ret;

	priv = container_of(port, struct eg20t_port, port);
	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
	pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
	ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
			      PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
	if (ret)
1346 1347
		dev_err(priv->port.dev,
			"pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1348

1349
	pch_free_dma(port);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

	free_irq(priv->port.irq, priv);
}

/* Change the port parameters, including word length, parity, stop
 *bits.  Update read_status_mask and ignore_status_mask to indicate
 *the types of events we are interested in receiving.  */
static void pch_uart_set_termios(struct uart_port *port,
				 struct ktermios *termios, struct ktermios *old)
{
	int rtn;
1361
	unsigned int baud, parity, bits, stb;
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
	struct eg20t_port *priv;
	unsigned long flags;

	priv = container_of(port, struct eg20t_port, port);
	switch (termios->c_cflag & CSIZE) {
	case CS5:
		bits = PCH_UART_HAL_5BIT;
		break;
	case CS6:
		bits = PCH_UART_HAL_6BIT;
		break;
	case CS7:
		bits = PCH_UART_HAL_7BIT;
		break;
	default:		/* CS8 */
		bits = PCH_UART_HAL_8BIT;
		break;
	}
	if (termios->c_cflag & CSTOPB)
		stb = PCH_UART_HAL_STB2;
	else
		stb = PCH_UART_HAL_STB1;

	if (termios->c_cflag & PARENB) {
1386
		if (termios->c_cflag & PARODD)
1387 1388 1389 1390
			parity = PCH_UART_HAL_PARITY_ODD;
		else
			parity = PCH_UART_HAL_PARITY_EVEN;

1391
	} else
1392
		parity = PCH_UART_HAL_PARITY_NONE;
1393 1394 1395 1396 1397 1398 1399

	/* Only UART0 has auto hardware flow function */
	if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
		priv->mcr |= UART_MCR_AFE;
	else
		priv->mcr &= ~UART_MCR_AFE;

1400 1401 1402 1403
	termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */

	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);

1404 1405
	spin_lock_irqsave(&priv->lock, flags);
	spin_lock(&port->lock);
1406 1407 1408 1409 1410 1411

	uart_update_timeout(port, termios->c_cflag, baud);
	rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
	if (rtn)
		goto out;

1412
	pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
1413 1414 1415 1416 1417
	/* Don't rewrite B0 */
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);

out:
1418 1419
	spin_unlock(&port->lock);
	spin_unlock_irqrestore(&priv->lock, flags);
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
}

static const char *pch_uart_type(struct uart_port *port)
{
	return KBUILD_MODNAME;
}

static void pch_uart_release_port(struct uart_port *port)
{
	struct eg20t_port *priv;

	priv = container_of(port, struct eg20t_port, port);
	pci_iounmap(priv->pdev, priv->membase);
	pci_release_regions(priv->pdev);
}

static int pch_uart_request_port(struct uart_port *port)
{
	struct eg20t_port *priv;
	int ret;
	void __iomem *membase;

	priv = container_of(port, struct eg20t_port, port);
	ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
	if (ret < 0)
		return -EBUSY;

	membase = pci_iomap(priv->pdev, 1, 0);
	if (!membase) {
		pci_release_regions(priv->pdev);
		return -EBUSY;
	}
	priv->membase = port->membase = membase;

	return 0;
}

static void pch_uart_config_port(struct uart_port *port, int type)
{
	struct eg20t_port *priv;

	priv = container_of(port, struct eg20t_port, port);
	if (type & UART_CONFIG_TYPE) {
		port->type = priv->port_type;
		pch_uart_request_port(port);
	}
}

static int pch_uart_verify_port(struct uart_port *port,
				struct serial_struct *serinfo)
{
	struct eg20t_port *priv;

	priv = container_of(port, struct eg20t_port, port);
	if (serinfo->flags & UPF_LOW_LATENCY) {
1475 1476
		dev_info(priv->port.dev,
			"PCH UART : Use PIO Mode (without DMA)\n");
1477 1478 1479 1480
		priv->use_dma = 0;
		serinfo->flags &= ~UPF_LOW_LATENCY;
	} else {
#ifndef CONFIG_PCH_DMA
1481 1482
		dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
			__func__);
1483 1484
		return -EOPNOTSUPP;
#endif
1485
		if (!priv->use_dma) {
1486
			pch_request_dma(port);
1487 1488 1489 1490 1491 1492
			if (priv->chan_rx)
				priv->use_dma = 1;
		}
		dev_info(priv->port.dev, "PCH UART: %s\n",
				priv->use_dma ?
				"Use DMA Mode" : "No DMA");
1493 1494 1495 1496 1497
	}

	return 0;
}

1498
#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_PCH_UART_CONSOLE)
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
/*
 *	Wait for transmitter & holding register to empty
 */
static void wait_for_xmitr(struct eg20t_port *up, int bits)
{
	unsigned int status, tmout = 10000;

	/* Wait up to 10ms for the character(s) to be sent. */
	for (;;) {
		status = ioread8(up->membase + UART_LSR);

		if ((status & bits) == bits)
			break;
		if (--tmout == 0)
			break;
		udelay(1);
	}

	/* Wait up to 1s for flow control if necessary */
	if (up->port.flags & UPF_CONS_FLOW) {
		unsigned int tmout;
		for (tmout = 1000000; tmout; tmout--) {
			unsigned int msr = ioread8(up->membase + UART_MSR);
			if (msr & UART_MSR_CTS)
				break;
			udelay(1);
			touch_nmi_watchdog();
		}
	}
}
1529
#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_PCH_UART_CONSOLE */
1530

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
#ifdef CONFIG_CONSOLE_POLL
/*
 * Console polling routines for communicate via uart while
 * in an interrupt or debug context.
 */
static int pch_uart_get_poll_char(struct uart_port *port)
{
	struct eg20t_port *priv =
		container_of(port, struct eg20t_port, port);
	u8 lsr = ioread8(priv->membase + UART_LSR);

	if (!(lsr & UART_LSR_DR))
		return NO_POLL_CHAR;

	return ioread8(priv->membase + PCH_UART_RBR);
}


static void pch_uart_put_poll_char(struct uart_port *port,
			 unsigned char c)
{
	unsigned int ier;
	struct eg20t_port *priv =
		container_of(port, struct eg20t_port, port);

	/*
	 * First save the IER then disable the interrupts
	 */
	ier = ioread8(priv->membase + UART_IER);
	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);

	wait_for_xmitr(priv, UART_LSR_THRE);
	/*
	 * Send the character out.
	 */
	iowrite8(c, priv->membase + PCH_UART_THR);

	/*
	 * Finally, wait for transmitter to become empty
	 * and restore the IER
	 */
	wait_for_xmitr(priv, BOTH_EMPTY);
	iowrite8(ier, priv->membase + UART_IER);
}
#endif /* CONFIG_CONSOLE_POLL */

1577
static const struct uart_ops pch_uart_ops = {
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
	.tx_empty = pch_uart_tx_empty,
	.set_mctrl = pch_uart_set_mctrl,
	.get_mctrl = pch_uart_get_mctrl,
	.stop_tx = pch_uart_stop_tx,
	.start_tx = pch_uart_start_tx,
	.stop_rx = pch_uart_stop_rx,
	.enable_ms = pch_uart_enable_ms,
	.break_ctl = pch_uart_break_ctl,
	.startup = pch_uart_startup,
	.shutdown = pch_uart_shutdown,
	.set_termios = pch_uart_set_termios,
/*	.pm		= pch_uart_pm,		Not supported yet */
	.type = pch_uart_type,
	.release_port = pch_uart_release_port,
	.request_port = pch_uart_request_port,
	.config_port = pch_uart_config_port,
	.verify_port = pch_uart_verify_port,
#ifdef CONFIG_CONSOLE_POLL
	.poll_get_char = pch_uart_get_poll_char,
	.poll_put_char = pch_uart_put_poll_char,
#endif
};

#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
static void pch_console_putchar(struct uart_port *port, int ch)
{
	struct eg20t_port *priv =
		container_of(port, struct eg20t_port, port);

	wait_for_xmitr(priv, UART_LSR_THRE);
	iowrite8(ch, priv->membase + PCH_UART_THR);
}

/*
 *	Print a string to the serial port trying not to disturb
 *	any possible real use of the port...
 *
 *	The console_lock must be held when we get here.
 */
static void
pch_console_write(struct console *co, const char *s, unsigned int count)
{
	struct eg20t_port *priv;
	unsigned long flags;
1623 1624
	int priv_locked = 1;
	int port_locked = 1;
1625 1626 1627 1628 1629 1630 1631 1632
	u8 ier;

	priv = pch_uart_ports[co->index];

	touch_nmi_watchdog();

	local_irq_save(flags);
	if (priv->port.sysrq) {
L
Liang Li 已提交
1633 1634
		/* call to uart_handle_sysrq_char already took the priv lock */
		priv_locked = 0;
1635 1636
		/* serial8250_handle_port() already took the port lock */
		port_locked = 0;
1637
	} else if (oops_in_progress) {
1638 1639 1640 1641
		priv_locked = spin_trylock(&priv->lock);
		port_locked = spin_trylock(&priv->port.lock);
	} else {
		spin_lock(&priv->lock);
1642
		spin_lock(&priv->port.lock);
1643
	}
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

	/*
	 *	First save the IER then disable the interrupts
	 */
	ier = ioread8(priv->membase + UART_IER);

	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);

	uart_console_write(&priv->port, s, count, pch_console_putchar);

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
	wait_for_xmitr(priv, BOTH_EMPTY);
	iowrite8(ier, priv->membase + UART_IER);

1661
	if (port_locked)
1662
		spin_unlock(&priv->port.lock);
1663 1664
	if (priv_locked)
		spin_unlock(&priv->lock);
1665 1666 1667 1668 1669 1670
	local_irq_restore(flags);
}

static int __init pch_console_setup(struct console *co, char *options)
{
	struct uart_port *port;
1671
	int baud = default_baud;
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

	/*
	 * Check whether an invalid uart number has been specified, and
	 * if so, search for the first available port that does have
	 * console support.
	 */
	if (co->index >= PCH_UART_NR)
		co->index = 0;
	port = &pch_uart_ports[co->index]->port;

	if (!port || (!port->iobase && !port->membase))
		return -ENODEV;

1688
	port->uartclk = pch_uart_get_uartclk();
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710

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

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

static struct uart_driver pch_uart_driver;

static struct console pch_console = {
	.name		= PCH_UART_DRIVER_DEVICE,
	.write		= pch_console_write,
	.device		= uart_console_device,
	.setup		= pch_console_setup,
	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
	.index		= -1,
	.data		= &pch_uart_driver,
};

#define PCH_CONSOLE	(&pch_console)
#else
#define PCH_CONSOLE	NULL
1711
#endif	/* CONFIG_SERIAL_PCH_UART_CONSOLE */
1712

1713 1714 1715 1716 1717 1718 1719
static struct uart_driver pch_uart_driver = {
	.owner = THIS_MODULE,
	.driver_name = KBUILD_MODNAME,
	.dev_name = PCH_UART_DRIVER_DEVICE,
	.major = 0,
	.minor = 0,
	.nr = PCH_UART_NR,
1720
	.cons = PCH_CONSOLE,
1721 1722 1723
};

static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
1724
					     const struct pci_device_id *id)
1725 1726 1727 1728 1729
{
	struct eg20t_port *priv;
	int ret;
	unsigned int iobase;
	unsigned int mapbase;
1730
	unsigned char *rxbuf;
1731
	int fifosize;
1732 1733
	int port_type;
	struct pch_uart_driver_data *board;
1734
	char name[32];
1735 1736 1737

	board = &drv_dat[id->driver_data];
	port_type = board->port_type;
1738 1739 1740 1741 1742

	priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
	if (priv == NULL)
		goto init_port_alloc_err;

1743
	rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1744 1745 1746 1747
	if (!rxbuf)
		goto init_port_free_txbuf;

	switch (port_type) {
1748
	case PORT_PCH_8LINE:
1749
		fifosize = 256; /* EG20T/ML7213: UART0 */
1750
		break;
1751
	case PORT_PCH_2LINE:
1752
		fifosize = 64; /* EG20T:UART1~3  ML7213: UART1~2*/
1753 1754 1755 1756 1757 1758
		break;
	default:
		dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
		goto init_port_hal_free;
	}

A
Alexander Stein 已提交
1759
	pci_enable_msi(pdev);
1760
	pci_set_master(pdev);
A
Alexander Stein 已提交
1761

1762 1763
	spin_lock_init(&priv->lock);

1764 1765 1766 1767 1768 1769
	iobase = pci_resource_start(pdev, 0);
	mapbase = pci_resource_start(pdev, 1);
	priv->mapbase = mapbase;
	priv->iobase = iobase;
	priv->pdev = pdev;
	priv->tx_empty = 1;
1770
	priv->rxbuf.buf = rxbuf;
1771 1772 1773
	priv->rxbuf.size = PAGE_SIZE;

	priv->fifo_size = fifosize;
1774
	priv->uartclk = pch_uart_get_uartclk();
1775
	priv->port_type = port_type;
1776 1777 1778 1779 1780 1781 1782 1783 1784
	priv->port.dev = &pdev->dev;
	priv->port.iobase = iobase;
	priv->port.membase = NULL;
	priv->port.mapbase = mapbase;
	priv->port.irq = pdev->irq;
	priv->port.iotype = UPIO_PORT;
	priv->port.ops = &pch_uart_ops;
	priv->port.flags = UPF_BOOT_AUTOCONF;
	priv->port.fifosize = fifosize;
1785
	priv->port.line = board->line_no;
1786
	priv->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_PCH_UART_CONSOLE);
1787 1788
	priv->trigger = PCH_UART_HAL_TRIGGER_M;

1789 1790 1791 1792
	snprintf(priv->irq_name, IRQ_NAME_SIZE,
		 KBUILD_MODNAME ":" PCH_UART_DRIVER_DEVICE "%d",
		 priv->port.line);

T
Tomoya MORINAGA 已提交
1793 1794
	spin_lock_init(&priv->port.lock);

1795
	pci_set_drvdata(pdev, priv);
1796 1797
	priv->trigger_level = 1;
	priv->fcr = 0;
1798

1799 1800 1801 1802
	if (pdev->dev.of_node)
		of_property_read_u32(pdev->dev.of_node, "clock-frequency"
					 , &user_uartclk);

1803 1804 1805
#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
	pch_uart_ports[board->line_no] = priv;
#endif
1806 1807 1808 1809
	ret = uart_add_one_port(&pch_uart_driver, &priv->port);
	if (ret < 0)
		goto init_port_hal_free;

1810 1811 1812
	snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
	debugfs_create_file(name, S_IFREG | S_IRUGO, NULL, priv,
			    &port_regs_ops);
1813

1814 1815 1816
	return priv;

init_port_hal_free:
1817 1818 1819
#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
	pch_uart_ports[board->line_no] = NULL;
#endif
1820
	free_page((unsigned long)rxbuf);
1821 1822 1823 1824 1825 1826 1827 1828 1829
init_port_free_txbuf:
	kfree(priv);
init_port_alloc_err:

	return NULL;
}

static void pch_uart_exit_port(struct eg20t_port *priv)
{
1830
	char name[32];
1831

1832 1833
	snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
	debugfs_remove(debugfs_lookup(name, NULL));
1834
	uart_remove_one_port(&pch_uart_driver, &priv->port);
1835
	free_page((unsigned long)priv->rxbuf.buf);
1836 1837 1838 1839
}

static void pch_uart_pci_remove(struct pci_dev *pdev)
{
1840
	struct eg20t_port *priv = pci_get_drvdata(pdev);
A
Alexander Stein 已提交
1841 1842

	pci_disable_msi(pdev);
1843 1844 1845 1846

#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
	pch_uart_ports[priv->port.line] = NULL;
#endif
1847 1848 1849 1850 1851
	pch_uart_exit_port(priv);
	pci_disable_device(pdev);
	kfree(priv);
	return;
}
1852 1853

static int __maybe_unused pch_uart_pci_suspend(struct device *dev)
1854
{
1855
	struct eg20t_port *priv = dev_get_drvdata(dev);
1856 1857 1858 1859 1860 1861

	uart_suspend_port(&pch_uart_driver, &priv->port);

	return 0;
}

1862
static int __maybe_unused pch_uart_pci_resume(struct device *dev)
1863
{
1864
	struct eg20t_port *priv = dev_get_drvdata(dev);
1865 1866 1867 1868 1869 1870

	uart_resume_port(&pch_uart_driver, &priv->port);

	return 0;
}

1871
static const struct pci_device_id pch_uart_pci_id[] = {
1872
	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1873
	 .driver_data = pch_et20t_uart0},
1874
	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1875
	 .driver_data = pch_et20t_uart1},
1876
	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1877
	 .driver_data = pch_et20t_uart2},
1878
	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1879
	 .driver_data = pch_et20t_uart3},
1880
	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1881
	 .driver_data = pch_ml7213_uart0},
1882
	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1883
	 .driver_data = pch_ml7213_uart1},
1884
	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1885
	 .driver_data = pch_ml7213_uart2},
1886 1887 1888 1889
	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
	 .driver_data = pch_ml7223_uart0},
	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
	 .driver_data = pch_ml7223_uart1},
1890 1891 1892 1893
	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
	 .driver_data = pch_ml7831_uart0},
	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
	 .driver_data = pch_ml7831_uart1},
1894 1895 1896
	{0,},
};

B
Bill Pemberton 已提交
1897
static int pch_uart_pci_probe(struct pci_dev *pdev,
1898 1899 1900 1901 1902 1903 1904 1905 1906
					const struct pci_device_id *id)
{
	int ret;
	struct eg20t_port *priv;

	ret = pci_enable_device(pdev);
	if (ret < 0)
		goto probe_error;

1907
	priv = pch_uart_init_port(pdev, id);
1908 1909 1910 1911 1912 1913 1914 1915 1916
	if (!priv) {
		ret = -EBUSY;
		goto probe_disable_device;
	}
	pci_set_drvdata(pdev, priv);

	return ret;

probe_disable_device:
A
Alexander Stein 已提交
1917
	pci_disable_msi(pdev);
1918 1919 1920 1921 1922
	pci_disable_device(pdev);
probe_error:
	return ret;
}

1923 1924 1925 1926
static SIMPLE_DEV_PM_OPS(pch_uart_pci_pm_ops,
			 pch_uart_pci_suspend,
			 pch_uart_pci_resume);

1927 1928 1929 1930
static struct pci_driver pch_uart_pci_driver = {
	.name = "pch_uart",
	.id_table = pch_uart_pci_id,
	.probe = pch_uart_pci_probe,
1931
	.remove = pch_uart_pci_remove,
1932
	.driver.pm = &pch_uart_pci_pm_ops,
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
};

static int __init pch_uart_module_init(void)
{
	int ret;

	/* register as UART driver */
	ret = uart_register_driver(&pch_uart_driver);
	if (ret < 0)
		return ret;

	/* register as PCI driver */
	ret = pci_register_driver(&pch_uart_pci_driver);
	if (ret < 0)
		uart_unregister_driver(&pch_uart_driver);

	return ret;
}
module_init(pch_uart_module_init);

static void __exit pch_uart_module_exit(void)
{
	pci_unregister_driver(&pch_uart_pci_driver);
	uart_unregister_driver(&pch_uart_driver);
}
module_exit(pch_uart_module_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1962 1963
MODULE_DEVICE_TABLE(pci, pch_uart_pci_id);

1964
module_param(default_baud, uint, S_IRUGO);
1965 1966
MODULE_PARM_DESC(default_baud,
                 "Default BAUD for initial driver state and console (default 9600)");
1967
module_param(user_uartclk, uint, S_IRUGO);
1968 1969
MODULE_PARM_DESC(user_uartclk,
                 "Override UART default or board specific UART clock");