xref: /openbmc/linux/drivers/tty/serial/pch_uart.c (revision dff03381)
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.h>
7 #include <linux/serial_reg.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/console.h>
12 #include <linux/serial_core.h>
13 #include <linux/tty.h>
14 #include <linux/tty_flip.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/dmi.h>
18 #include <linux/nmi.h>
19 #include <linux/delay.h>
20 #include <linux/of.h>
21 
22 #include <linux/debugfs.h>
23 #include <linux/dmaengine.h>
24 #include <linux/pch_dma.h>
25 
26 enum {
27 	PCH_UART_HANDLED_RX_INT_SHIFT,
28 	PCH_UART_HANDLED_TX_INT_SHIFT,
29 	PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
30 	PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
31 	PCH_UART_HANDLED_MS_INT_SHIFT,
32 	PCH_UART_HANDLED_LS_INT_SHIFT,
33 };
34 
35 #define PCH_UART_DRIVER_DEVICE "ttyPCH"
36 
37 /* Set the max number of UART port
38  * Intel EG20T PCH: 4 port
39  * LAPIS Semiconductor ML7213 IOH: 3 port
40  * LAPIS Semiconductor ML7223 IOH: 2 port
41 */
42 #define PCH_UART_NR	4
43 
44 #define PCH_UART_HANDLED_RX_INT	(1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
45 #define PCH_UART_HANDLED_TX_INT	(1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
46 #define PCH_UART_HANDLED_RX_ERR_INT	(1<<((\
47 					PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
48 #define PCH_UART_HANDLED_RX_TRG_INT	(1<<((\
49 					PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
50 #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 #define PCH_UART_RBR		0x00
55 #define PCH_UART_THR		0x00
56 
57 #define PCH_UART_IER_MASK	(PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
58 				PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
59 #define PCH_UART_IER_ERBFI	0x00000001
60 #define PCH_UART_IER_ETBEI	0x00000002
61 #define PCH_UART_IER_ELSI	0x00000004
62 #define PCH_UART_IER_EDSSI	0x00000008
63 
64 #define PCH_UART_IIR_IP			0x00000001
65 #define PCH_UART_IIR_IID		0x00000006
66 #define PCH_UART_IIR_MSI		0x00000000
67 #define PCH_UART_IIR_TRI		0x00000002
68 #define PCH_UART_IIR_RRI		0x00000004
69 #define PCH_UART_IIR_REI		0x00000006
70 #define PCH_UART_IIR_TOI		0x00000008
71 #define PCH_UART_IIR_FIFO256		0x00000020
72 #define PCH_UART_IIR_FIFO64		PCH_UART_IIR_FIFO256
73 #define PCH_UART_IIR_FE			0x000000C0
74 
75 #define PCH_UART_FCR_FIFOE		0x00000001
76 #define PCH_UART_FCR_RFR		0x00000002
77 #define PCH_UART_FCR_TFR		0x00000004
78 #define PCH_UART_FCR_DMS		0x00000008
79 #define PCH_UART_FCR_FIFO256		0x00000020
80 #define PCH_UART_FCR_RFTL		0x000000C0
81 
82 #define PCH_UART_FCR_RFTL1		0x00000000
83 #define PCH_UART_FCR_RFTL64		0x00000040
84 #define PCH_UART_FCR_RFTL128		0x00000080
85 #define PCH_UART_FCR_RFTL224		0x000000C0
86 #define PCH_UART_FCR_RFTL16		PCH_UART_FCR_RFTL64
87 #define PCH_UART_FCR_RFTL32		PCH_UART_FCR_RFTL128
88 #define PCH_UART_FCR_RFTL56		PCH_UART_FCR_RFTL224
89 #define PCH_UART_FCR_RFTL4		PCH_UART_FCR_RFTL64
90 #define PCH_UART_FCR_RFTL8		PCH_UART_FCR_RFTL128
91 #define PCH_UART_FCR_RFTL14		PCH_UART_FCR_RFTL224
92 #define PCH_UART_FCR_RFTL_SHIFT		6
93 
94 #define PCH_UART_LCR_WLS	0x00000003
95 #define PCH_UART_LCR_STB	0x00000004
96 #define PCH_UART_LCR_PEN	0x00000008
97 #define PCH_UART_LCR_EPS	0x00000010
98 #define PCH_UART_LCR_SP		0x00000020
99 #define PCH_UART_LCR_SB		0x00000040
100 #define PCH_UART_LCR_DLAB	0x00000080
101 #define PCH_UART_LCR_NP		0x00000000
102 #define PCH_UART_LCR_OP		PCH_UART_LCR_PEN
103 #define PCH_UART_LCR_EP		(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
104 #define PCH_UART_LCR_1P		(PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
105 #define PCH_UART_LCR_0P		(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
106 				PCH_UART_LCR_SP)
107 
108 #define PCH_UART_LCR_5BIT	0x00000000
109 #define PCH_UART_LCR_6BIT	0x00000001
110 #define PCH_UART_LCR_7BIT	0x00000002
111 #define PCH_UART_LCR_8BIT	0x00000003
112 
113 #define PCH_UART_MCR_DTR	0x00000001
114 #define PCH_UART_MCR_RTS	0x00000002
115 #define PCH_UART_MCR_OUT	0x0000000C
116 #define PCH_UART_MCR_LOOP	0x00000010
117 #define PCH_UART_MCR_AFE	0x00000020
118 
119 #define PCH_UART_LSR_DR		0x00000001
120 #define PCH_UART_LSR_ERR	(1<<7)
121 
122 #define PCH_UART_MSR_DCTS	0x00000001
123 #define PCH_UART_MSR_DDSR	0x00000002
124 #define PCH_UART_MSR_TERI	0x00000004
125 #define PCH_UART_MSR_DDCD	0x00000008
126 #define PCH_UART_MSR_CTS	0x00000010
127 #define PCH_UART_MSR_DSR	0x00000020
128 #define PCH_UART_MSR_RI		0x00000040
129 #define PCH_UART_MSR_DCD	0x00000080
130 #define PCH_UART_MSR_DELTA	(PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
131 				PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
132 
133 #define PCH_UART_DLL		0x00
134 #define PCH_UART_DLM		0x01
135 
136 #define PCH_UART_BRCSR		0x0E
137 
138 #define PCH_UART_IID_RLS	(PCH_UART_IIR_REI)
139 #define PCH_UART_IID_RDR	(PCH_UART_IIR_RRI)
140 #define PCH_UART_IID_RDR_TO	(PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
141 #define PCH_UART_IID_THRE	(PCH_UART_IIR_TRI)
142 #define PCH_UART_IID_MS		(PCH_UART_IIR_MSI)
143 
144 #define PCH_UART_HAL_PARITY_NONE	(PCH_UART_LCR_NP)
145 #define PCH_UART_HAL_PARITY_ODD		(PCH_UART_LCR_OP)
146 #define PCH_UART_HAL_PARITY_EVEN	(PCH_UART_LCR_EP)
147 #define PCH_UART_HAL_PARITY_FIX1	(PCH_UART_LCR_1P)
148 #define PCH_UART_HAL_PARITY_FIX0	(PCH_UART_LCR_0P)
149 #define PCH_UART_HAL_5BIT		(PCH_UART_LCR_5BIT)
150 #define PCH_UART_HAL_6BIT		(PCH_UART_LCR_6BIT)
151 #define PCH_UART_HAL_7BIT		(PCH_UART_LCR_7BIT)
152 #define PCH_UART_HAL_8BIT		(PCH_UART_LCR_8BIT)
153 #define PCH_UART_HAL_STB1		0
154 #define PCH_UART_HAL_STB2		(PCH_UART_LCR_STB)
155 
156 #define PCH_UART_HAL_CLR_TX_FIFO	(PCH_UART_FCR_TFR)
157 #define PCH_UART_HAL_CLR_RX_FIFO	(PCH_UART_FCR_RFR)
158 #define PCH_UART_HAL_CLR_ALL_FIFO	(PCH_UART_HAL_CLR_TX_FIFO | \
159 					PCH_UART_HAL_CLR_RX_FIFO)
160 
161 #define PCH_UART_HAL_DMA_MODE0		0
162 #define PCH_UART_HAL_FIFO_DIS		0
163 #define PCH_UART_HAL_FIFO16		(PCH_UART_FCR_FIFOE)
164 #define PCH_UART_HAL_FIFO256		(PCH_UART_FCR_FIFOE | \
165 					PCH_UART_FCR_FIFO256)
166 #define PCH_UART_HAL_FIFO64		(PCH_UART_HAL_FIFO256)
167 #define PCH_UART_HAL_TRIGGER1		(PCH_UART_FCR_RFTL1)
168 #define PCH_UART_HAL_TRIGGER64		(PCH_UART_FCR_RFTL64)
169 #define PCH_UART_HAL_TRIGGER128		(PCH_UART_FCR_RFTL128)
170 #define PCH_UART_HAL_TRIGGER224		(PCH_UART_FCR_RFTL224)
171 #define PCH_UART_HAL_TRIGGER16		(PCH_UART_FCR_RFTL16)
172 #define PCH_UART_HAL_TRIGGER32		(PCH_UART_FCR_RFTL32)
173 #define PCH_UART_HAL_TRIGGER56		(PCH_UART_FCR_RFTL56)
174 #define PCH_UART_HAL_TRIGGER4		(PCH_UART_FCR_RFTL4)
175 #define PCH_UART_HAL_TRIGGER8		(PCH_UART_FCR_RFTL8)
176 #define PCH_UART_HAL_TRIGGER14		(PCH_UART_FCR_RFTL14)
177 #define PCH_UART_HAL_TRIGGER_L		(PCH_UART_FCR_RFTL64)
178 #define PCH_UART_HAL_TRIGGER_M		(PCH_UART_FCR_RFTL128)
179 #define PCH_UART_HAL_TRIGGER_H		(PCH_UART_FCR_RFTL224)
180 
181 #define PCH_UART_HAL_RX_INT		(PCH_UART_IER_ERBFI)
182 #define PCH_UART_HAL_TX_INT		(PCH_UART_IER_ETBEI)
183 #define PCH_UART_HAL_RX_ERR_INT		(PCH_UART_IER_ELSI)
184 #define PCH_UART_HAL_MS_INT		(PCH_UART_IER_EDSSI)
185 #define PCH_UART_HAL_ALL_INT		(PCH_UART_IER_MASK)
186 
187 #define PCH_UART_HAL_DTR		(PCH_UART_MCR_DTR)
188 #define PCH_UART_HAL_RTS		(PCH_UART_MCR_RTS)
189 #define PCH_UART_HAL_OUT		(PCH_UART_MCR_OUT)
190 #define PCH_UART_HAL_LOOP		(PCH_UART_MCR_LOOP)
191 #define PCH_UART_HAL_AFE		(PCH_UART_MCR_AFE)
192 
193 #define DEFAULT_UARTCLK   1843200 /*   1.8432 MHz */
194 #define CMITC_UARTCLK   192000000 /* 192.0000 MHz */
195 #define FRI2_64_UARTCLK  64000000 /*  64.0000 MHz */
196 #define FRI2_48_UARTCLK  48000000 /*  48.0000 MHz */
197 #define NTC1_UARTCLK     64000000 /*  64.0000 MHz */
198 #define MINNOW_UARTCLK   50000000 /*  50.0000 MHz */
199 
200 struct pch_uart_buffer {
201 	unsigned char *buf;
202 	int size;
203 };
204 
205 struct eg20t_port {
206 	struct uart_port port;
207 	int port_type;
208 	void __iomem *membase;
209 	resource_size_t mapbase;
210 	unsigned int iobase;
211 	struct pci_dev *pdev;
212 	int fifo_size;
213 	unsigned int uartclk;
214 	int start_tx;
215 	int start_rx;
216 	int tx_empty;
217 	int trigger;
218 	int trigger_level;
219 	struct pch_uart_buffer rxbuf;
220 	unsigned int dmsr;
221 	unsigned int fcr;
222 	unsigned int mcr;
223 	unsigned int use_dma;
224 	struct dma_async_tx_descriptor	*desc_tx;
225 	struct dma_async_tx_descriptor	*desc_rx;
226 	struct pch_dma_slave		param_tx;
227 	struct pch_dma_slave		param_rx;
228 	struct dma_chan			*chan_tx;
229 	struct dma_chan			*chan_rx;
230 	struct scatterlist		*sg_tx_p;
231 	int				nent;
232 	int				orig_nent;
233 	struct scatterlist		sg_rx;
234 	int				tx_dma_use;
235 	void				*rx_buf_virt;
236 	dma_addr_t			rx_buf_dma;
237 
238 #define IRQ_NAME_SIZE 17
239 	char				irq_name[IRQ_NAME_SIZE];
240 
241 	/* protect the eg20t_port private structure and io access to membase */
242 	spinlock_t lock;
243 };
244 
245 /**
246  * struct pch_uart_driver_data - private data structure for UART-DMA
247  * @port_type:			The type of UART port
248  * @line_no:			UART port line number (0, 1, 2...)
249  */
250 struct pch_uart_driver_data {
251 	int port_type;
252 	int line_no;
253 };
254 
255 enum pch_uart_num_t {
256 	pch_et20t_uart0 = 0,
257 	pch_et20t_uart1,
258 	pch_et20t_uart2,
259 	pch_et20t_uart3,
260 	pch_ml7213_uart0,
261 	pch_ml7213_uart1,
262 	pch_ml7213_uart2,
263 	pch_ml7223_uart0,
264 	pch_ml7223_uart1,
265 	pch_ml7831_uart0,
266 	pch_ml7831_uart1,
267 };
268 
269 static struct pch_uart_driver_data drv_dat[] = {
270 	[pch_et20t_uart0] = {PORT_PCH_8LINE, 0},
271 	[pch_et20t_uart1] = {PORT_PCH_2LINE, 1},
272 	[pch_et20t_uart2] = {PORT_PCH_2LINE, 2},
273 	[pch_et20t_uart3] = {PORT_PCH_2LINE, 3},
274 	[pch_ml7213_uart0] = {PORT_PCH_8LINE, 0},
275 	[pch_ml7213_uart1] = {PORT_PCH_2LINE, 1},
276 	[pch_ml7213_uart2] = {PORT_PCH_2LINE, 2},
277 	[pch_ml7223_uart0] = {PORT_PCH_8LINE, 0},
278 	[pch_ml7223_uart1] = {PORT_PCH_2LINE, 1},
279 	[pch_ml7831_uart0] = {PORT_PCH_8LINE, 0},
280 	[pch_ml7831_uart1] = {PORT_PCH_2LINE, 1},
281 };
282 
283 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
284 static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
285 #endif
286 static unsigned int default_baud = 9600;
287 static unsigned int user_uartclk = 0;
288 static const int trigger_level_256[4] = { 1, 64, 128, 224 };
289 static const int trigger_level_64[4] = { 1, 16, 32, 56 };
290 static const int trigger_level_16[4] = { 1, 4, 8, 14 };
291 static const int trigger_level_1[4] = { 1, 1, 1, 1 };
292 
293 #define PCH_REGS_BUFSIZE	1024
294 
295 
296 static ssize_t port_show_regs(struct file *file, char __user *user_buf,
297 				size_t count, loff_t *ppos)
298 {
299 	struct eg20t_port *priv = file->private_data;
300 	char *buf;
301 	u32 len = 0;
302 	ssize_t ret;
303 	unsigned char lcr;
304 
305 	buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
306 	if (!buf)
307 		return 0;
308 
309 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
310 			"PCH EG20T port[%d] regs:\n", priv->port.line);
311 
312 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
313 			"=================================\n");
314 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
315 			"IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
316 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
317 			"IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
318 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
319 			"LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
320 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
321 			"MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
322 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
323 			"LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
324 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
325 			"MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
326 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
327 			"BRCSR: \t0x%02x\n",
328 			ioread8(priv->membase + PCH_UART_BRCSR));
329 
330 	lcr = ioread8(priv->membase + UART_LCR);
331 	iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
332 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
333 			"DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
334 	len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
335 			"DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
336 	iowrite8(lcr, priv->membase + UART_LCR);
337 
338 	if (len > PCH_REGS_BUFSIZE)
339 		len = PCH_REGS_BUFSIZE;
340 
341 	ret =  simple_read_from_buffer(user_buf, count, ppos, buf, len);
342 	kfree(buf);
343 	return ret;
344 }
345 
346 static const struct file_operations port_regs_ops = {
347 	.owner		= THIS_MODULE,
348 	.open		= simple_open,
349 	.read		= port_show_regs,
350 	.llseek		= default_llseek,
351 };
352 
353 static const struct dmi_system_id pch_uart_dmi_table[] = {
354 	{
355 		.ident = "CM-iTC",
356 		{
357 			DMI_MATCH(DMI_BOARD_NAME, "CM-iTC"),
358 		},
359 		(void *)CMITC_UARTCLK,
360 	},
361 	{
362 		.ident = "FRI2",
363 		{
364 			DMI_MATCH(DMI_BIOS_VERSION, "FRI2"),
365 		},
366 		(void *)FRI2_64_UARTCLK,
367 	},
368 	{
369 		.ident = "Fish River Island II",
370 		{
371 			DMI_MATCH(DMI_PRODUCT_NAME, "Fish River Island II"),
372 		},
373 		(void *)FRI2_48_UARTCLK,
374 	},
375 	{
376 		.ident = "COMe-mTT",
377 		{
378 			DMI_MATCH(DMI_BOARD_NAME, "COMe-mTT"),
379 		},
380 		(void *)NTC1_UARTCLK,
381 	},
382 	{
383 		.ident = "nanoETXexpress-TT",
384 		{
385 			DMI_MATCH(DMI_BOARD_NAME, "nanoETXexpress-TT"),
386 		},
387 		(void *)NTC1_UARTCLK,
388 	},
389 	{
390 		.ident = "MinnowBoard",
391 		{
392 			DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
393 		},
394 		(void *)MINNOW_UARTCLK,
395 	},
396 	{ }
397 };
398 
399 /* Return UART clock, checking for board specific clocks. */
400 static unsigned int pch_uart_get_uartclk(void)
401 {
402 	const struct dmi_system_id *d;
403 
404 	if (user_uartclk)
405 		return user_uartclk;
406 
407 	d = dmi_first_match(pch_uart_dmi_table);
408 	if (d)
409 		return (unsigned long)d->driver_data;
410 
411 	return DEFAULT_UARTCLK;
412 }
413 
414 static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
415 					  unsigned int flag)
416 {
417 	u8 ier = ioread8(priv->membase + UART_IER);
418 	ier |= flag & PCH_UART_IER_MASK;
419 	iowrite8(ier, priv->membase + UART_IER);
420 }
421 
422 static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
423 					   unsigned int flag)
424 {
425 	u8 ier = ioread8(priv->membase + UART_IER);
426 	ier &= ~(flag & PCH_UART_IER_MASK);
427 	iowrite8(ier, priv->membase + UART_IER);
428 }
429 
430 static int pch_uart_hal_set_line(struct eg20t_port *priv, unsigned int baud,
431 				 unsigned int parity, unsigned int bits,
432 				 unsigned int stb)
433 {
434 	unsigned int dll, dlm, lcr;
435 	int div;
436 
437 	div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
438 	if (div < 0 || USHRT_MAX <= div) {
439 		dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
440 		return -EINVAL;
441 	}
442 
443 	dll = (unsigned int)div & 0x00FFU;
444 	dlm = ((unsigned int)div >> 8) & 0x00FFU;
445 
446 	if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
447 		dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
448 		return -EINVAL;
449 	}
450 
451 	if (bits & ~PCH_UART_LCR_WLS) {
452 		dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
453 		return -EINVAL;
454 	}
455 
456 	if (stb & ~PCH_UART_LCR_STB) {
457 		dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
458 		return -EINVAL;
459 	}
460 
461 	lcr = parity;
462 	lcr |= bits;
463 	lcr |= stb;
464 
465 	dev_dbg(priv->port.dev, "%s:baud = %u, div = %04x, lcr = %02x (%lu)\n",
466 		 __func__, baud, div, lcr, jiffies);
467 	iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
468 	iowrite8(dll, priv->membase + PCH_UART_DLL);
469 	iowrite8(dlm, priv->membase + PCH_UART_DLM);
470 	iowrite8(lcr, priv->membase + UART_LCR);
471 
472 	return 0;
473 }
474 
475 static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
476 				    unsigned int flag)
477 {
478 	if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
479 		dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
480 			__func__, flag);
481 		return -EINVAL;
482 	}
483 
484 	iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
485 	iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
486 		 priv->membase + UART_FCR);
487 	iowrite8(priv->fcr, priv->membase + UART_FCR);
488 
489 	return 0;
490 }
491 
492 static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
493 				 unsigned int dmamode,
494 				 unsigned int fifo_size, unsigned int trigger)
495 {
496 	u8 fcr;
497 
498 	if (dmamode & ~PCH_UART_FCR_DMS) {
499 		dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
500 			__func__, dmamode);
501 		return -EINVAL;
502 	}
503 
504 	if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
505 		dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
506 			__func__, fifo_size);
507 		return -EINVAL;
508 	}
509 
510 	if (trigger & ~PCH_UART_FCR_RFTL) {
511 		dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
512 			__func__, trigger);
513 		return -EINVAL;
514 	}
515 
516 	switch (priv->fifo_size) {
517 	case 256:
518 		priv->trigger_level =
519 		    trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
520 		break;
521 	case 64:
522 		priv->trigger_level =
523 		    trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
524 		break;
525 	case 16:
526 		priv->trigger_level =
527 		    trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
528 		break;
529 	default:
530 		priv->trigger_level =
531 		    trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
532 		break;
533 	}
534 	fcr =
535 	    dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
536 	iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
537 	iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
538 		 priv->membase + UART_FCR);
539 	iowrite8(fcr, priv->membase + UART_FCR);
540 	priv->fcr = fcr;
541 
542 	return 0;
543 }
544 
545 static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
546 {
547 	unsigned int msr = ioread8(priv->membase + UART_MSR);
548 	priv->dmsr = msr & PCH_UART_MSR_DELTA;
549 	return (u8)msr;
550 }
551 
552 static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
553 			     int rx_size)
554 {
555 	int i;
556 	u8 rbr, lsr;
557 	struct uart_port *port = &priv->port;
558 
559 	lsr = ioread8(priv->membase + UART_LSR);
560 	for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
561 	     i < rx_size && lsr & (UART_LSR_DR | UART_LSR_BI);
562 	     lsr = ioread8(priv->membase + UART_LSR)) {
563 		rbr = ioread8(priv->membase + PCH_UART_RBR);
564 
565 		if (lsr & UART_LSR_BI) {
566 			port->icount.brk++;
567 			if (uart_handle_break(port))
568 				continue;
569 		}
570 		if (uart_handle_sysrq_char(port, rbr))
571 			continue;
572 
573 		buf[i++] = rbr;
574 	}
575 	return i;
576 }
577 
578 static unsigned char pch_uart_hal_get_iid(struct eg20t_port *priv)
579 {
580 	return ioread8(priv->membase + UART_IIR) &\
581 		      (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP);
582 }
583 
584 static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
585 {
586 	return ioread8(priv->membase + UART_LSR);
587 }
588 
589 static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
590 {
591 	unsigned int lcr;
592 
593 	lcr = ioread8(priv->membase + UART_LCR);
594 	if (on)
595 		lcr |= PCH_UART_LCR_SB;
596 	else
597 		lcr &= ~PCH_UART_LCR_SB;
598 
599 	iowrite8(lcr, priv->membase + UART_LCR);
600 }
601 
602 static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
603 		   int size)
604 {
605 	struct uart_port *port = &priv->port;
606 	struct tty_port *tport = &port->state->port;
607 
608 	tty_insert_flip_string(tport, buf, size);
609 	tty_flip_buffer_push(tport);
610 
611 	return 0;
612 }
613 
614 static int dma_push_rx(struct eg20t_port *priv, int size)
615 {
616 	int room;
617 	struct uart_port *port = &priv->port;
618 	struct tty_port *tport = &port->state->port;
619 
620 	room = tty_buffer_request_room(tport, size);
621 
622 	if (room < size)
623 		dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
624 			 size - room);
625 	if (!room)
626 		return 0;
627 
628 	tty_insert_flip_string(tport, sg_virt(&priv->sg_rx), size);
629 
630 	port->icount.rx += room;
631 
632 	return room;
633 }
634 
635 static void pch_free_dma(struct uart_port *port)
636 {
637 	struct eg20t_port *priv;
638 	priv = container_of(port, struct eg20t_port, port);
639 
640 	if (priv->chan_tx) {
641 		dma_release_channel(priv->chan_tx);
642 		priv->chan_tx = NULL;
643 	}
644 	if (priv->chan_rx) {
645 		dma_release_channel(priv->chan_rx);
646 		priv->chan_rx = NULL;
647 	}
648 
649 	if (priv->rx_buf_dma) {
650 		dma_free_coherent(port->dev, port->fifosize, priv->rx_buf_virt,
651 				  priv->rx_buf_dma);
652 		priv->rx_buf_virt = NULL;
653 		priv->rx_buf_dma = 0;
654 	}
655 
656 	return;
657 }
658 
659 static bool filter(struct dma_chan *chan, void *slave)
660 {
661 	struct pch_dma_slave *param = slave;
662 
663 	if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
664 						  chan->device->dev)) {
665 		chan->private = param;
666 		return true;
667 	} else {
668 		return false;
669 	}
670 }
671 
672 static void pch_request_dma(struct uart_port *port)
673 {
674 	dma_cap_mask_t mask;
675 	struct dma_chan *chan;
676 	struct pci_dev *dma_dev;
677 	struct pch_dma_slave *param;
678 	struct eg20t_port *priv =
679 				container_of(port, struct eg20t_port, port);
680 	dma_cap_zero(mask);
681 	dma_cap_set(DMA_SLAVE, mask);
682 
683 	/* Get DMA's dev information */
684 	dma_dev = pci_get_slot(priv->pdev->bus,
685 			PCI_DEVFN(PCI_SLOT(priv->pdev->devfn), 0));
686 
687 	/* Set Tx DMA */
688 	param = &priv->param_tx;
689 	param->dma_dev = &dma_dev->dev;
690 	param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
691 
692 	param->tx_reg = port->mapbase + UART_TX;
693 	chan = dma_request_channel(mask, filter, param);
694 	if (!chan) {
695 		dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
696 			__func__);
697 		return;
698 	}
699 	priv->chan_tx = chan;
700 
701 	/* Set Rx DMA */
702 	param = &priv->param_rx;
703 	param->dma_dev = &dma_dev->dev;
704 	param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
705 
706 	param->rx_reg = port->mapbase + UART_RX;
707 	chan = dma_request_channel(mask, filter, param);
708 	if (!chan) {
709 		dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
710 			__func__);
711 		dma_release_channel(priv->chan_tx);
712 		priv->chan_tx = NULL;
713 		return;
714 	}
715 
716 	/* Get Consistent memory for DMA */
717 	priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
718 				    &priv->rx_buf_dma, GFP_KERNEL);
719 	priv->chan_rx = chan;
720 }
721 
722 static void pch_dma_rx_complete(void *arg)
723 {
724 	struct eg20t_port *priv = arg;
725 	struct uart_port *port = &priv->port;
726 	int count;
727 
728 	dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
729 	count = dma_push_rx(priv, priv->trigger_level);
730 	if (count)
731 		tty_flip_buffer_push(&port->state->port);
732 	async_tx_ack(priv->desc_rx);
733 	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
734 					    PCH_UART_HAL_RX_ERR_INT);
735 }
736 
737 static void pch_dma_tx_complete(void *arg)
738 {
739 	struct eg20t_port *priv = arg;
740 	struct uart_port *port = &priv->port;
741 	struct circ_buf *xmit = &port->state->xmit;
742 	struct scatterlist *sg = priv->sg_tx_p;
743 	int i;
744 
745 	for (i = 0; i < priv->nent; i++, sg++) {
746 		xmit->tail += sg_dma_len(sg);
747 		port->icount.tx += sg_dma_len(sg);
748 	}
749 	xmit->tail &= UART_XMIT_SIZE - 1;
750 	async_tx_ack(priv->desc_tx);
751 	dma_unmap_sg(port->dev, sg, priv->orig_nent, DMA_TO_DEVICE);
752 	priv->tx_dma_use = 0;
753 	priv->nent = 0;
754 	priv->orig_nent = 0;
755 	kfree(priv->sg_tx_p);
756 	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
757 }
758 
759 static int handle_rx_to(struct eg20t_port *priv)
760 {
761 	struct pch_uart_buffer *buf;
762 	int rx_size;
763 	int ret;
764 	if (!priv->start_rx) {
765 		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
766 						     PCH_UART_HAL_RX_ERR_INT);
767 		return 0;
768 	}
769 	buf = &priv->rxbuf;
770 	do {
771 		rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
772 		ret = push_rx(priv, buf->buf, rx_size);
773 		if (ret)
774 			return 0;
775 	} while (rx_size == buf->size);
776 
777 	return PCH_UART_HANDLED_RX_INT;
778 }
779 
780 static int handle_rx(struct eg20t_port *priv)
781 {
782 	return handle_rx_to(priv);
783 }
784 
785 static int dma_handle_rx(struct eg20t_port *priv)
786 {
787 	struct uart_port *port = &priv->port;
788 	struct dma_async_tx_descriptor *desc;
789 	struct scatterlist *sg;
790 
791 	priv = container_of(port, struct eg20t_port, port);
792 	sg = &priv->sg_rx;
793 
794 	sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
795 
796 	sg_dma_len(sg) = priv->trigger_level;
797 
798 	sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
799 		     sg_dma_len(sg), offset_in_page(priv->rx_buf_virt));
800 
801 	sg_dma_address(sg) = priv->rx_buf_dma;
802 
803 	desc = dmaengine_prep_slave_sg(priv->chan_rx,
804 			sg, 1, DMA_DEV_TO_MEM,
805 			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
806 
807 	if (!desc)
808 		return 0;
809 
810 	priv->desc_rx = desc;
811 	desc->callback = pch_dma_rx_complete;
812 	desc->callback_param = priv;
813 	desc->tx_submit(desc);
814 	dma_async_issue_pending(priv->chan_rx);
815 
816 	return PCH_UART_HANDLED_RX_INT;
817 }
818 
819 static unsigned int handle_tx(struct eg20t_port *priv)
820 {
821 	struct uart_port *port = &priv->port;
822 	struct circ_buf *xmit = &port->state->xmit;
823 	int fifo_size;
824 	int tx_empty;
825 
826 	if (!priv->start_tx) {
827 		dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
828 			__func__, jiffies);
829 		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
830 		priv->tx_empty = 1;
831 		return 0;
832 	}
833 
834 	fifo_size = max(priv->fifo_size, 1);
835 	tx_empty = 1;
836 	if (port->x_char) {
837 		iowrite8(port->x_char, priv->membase + PCH_UART_THR);
838 		port->icount.tx++;
839 		port->x_char = 0;
840 		tx_empty = 0;
841 		fifo_size--;
842 	}
843 
844 	while (!uart_tx_stopped(port) && !uart_circ_empty(xmit) && fifo_size) {
845 		iowrite8(xmit->buf[xmit->tail], priv->membase + PCH_UART_THR);
846 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
847 		port->icount.tx++;
848 		fifo_size--;
849 		tx_empty = 0;
850 	}
851 
852 	priv->tx_empty = tx_empty;
853 
854 	if (tx_empty) {
855 		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
856 		uart_write_wakeup(port);
857 	}
858 
859 	return PCH_UART_HANDLED_TX_INT;
860 }
861 
862 static unsigned int dma_handle_tx(struct eg20t_port *priv)
863 {
864 	struct uart_port *port = &priv->port;
865 	struct circ_buf *xmit = &port->state->xmit;
866 	struct scatterlist *sg;
867 	int nent;
868 	int fifo_size;
869 	struct dma_async_tx_descriptor *desc;
870 	int num;
871 	int i;
872 	int bytes;
873 	int size;
874 	int rem;
875 
876 	if (!priv->start_tx) {
877 		dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
878 			__func__, jiffies);
879 		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
880 		priv->tx_empty = 1;
881 		return 0;
882 	}
883 
884 	if (priv->tx_dma_use) {
885 		dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
886 			__func__, jiffies);
887 		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
888 		priv->tx_empty = 1;
889 		return 0;
890 	}
891 
892 	fifo_size = max(priv->fifo_size, 1);
893 
894 	if (port->x_char) {
895 		iowrite8(port->x_char, priv->membase + PCH_UART_THR);
896 		port->icount.tx++;
897 		port->x_char = 0;
898 		fifo_size--;
899 	}
900 
901 	bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
902 			     UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
903 			     xmit->tail, UART_XMIT_SIZE));
904 	if (!bytes) {
905 		dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
906 		pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
907 		uart_write_wakeup(port);
908 		return 0;
909 	}
910 
911 	if (bytes > fifo_size) {
912 		num = bytes / fifo_size + 1;
913 		size = fifo_size;
914 		rem = bytes % fifo_size;
915 	} else {
916 		num = 1;
917 		size = bytes;
918 		rem = bytes;
919 	}
920 
921 	dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
922 		__func__, num, size, rem);
923 
924 	priv->tx_dma_use = 1;
925 
926 	priv->sg_tx_p = kmalloc_array(num, sizeof(struct scatterlist), GFP_ATOMIC);
927 	if (!priv->sg_tx_p) {
928 		dev_err(priv->port.dev, "%s:kzalloc Failed\n", __func__);
929 		return 0;
930 	}
931 
932 	sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
933 	sg = priv->sg_tx_p;
934 
935 	for (i = 0; i < num; i++, sg++) {
936 		if (i == (num - 1))
937 			sg_set_page(sg, virt_to_page(xmit->buf),
938 				    rem, fifo_size * i);
939 		else
940 			sg_set_page(sg, virt_to_page(xmit->buf),
941 				    size, fifo_size * i);
942 	}
943 
944 	sg = priv->sg_tx_p;
945 	nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
946 	if (!nent) {
947 		dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
948 		return 0;
949 	}
950 	priv->orig_nent = num;
951 	priv->nent = nent;
952 
953 	for (i = 0; i < nent; i++, sg++) {
954 		sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
955 			      fifo_size * i;
956 		sg_dma_address(sg) = (sg_dma_address(sg) &
957 				    ~(UART_XMIT_SIZE - 1)) + sg->offset;
958 		if (i == (nent - 1))
959 			sg_dma_len(sg) = rem;
960 		else
961 			sg_dma_len(sg) = size;
962 	}
963 
964 	desc = dmaengine_prep_slave_sg(priv->chan_tx,
965 					priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
966 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
967 	if (!desc) {
968 		dev_err(priv->port.dev, "%s:dmaengine_prep_slave_sg Failed\n",
969 			__func__);
970 		return 0;
971 	}
972 	dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
973 	priv->desc_tx = desc;
974 	desc->callback = pch_dma_tx_complete;
975 	desc->callback_param = priv;
976 
977 	desc->tx_submit(desc);
978 
979 	dma_async_issue_pending(priv->chan_tx);
980 
981 	return PCH_UART_HANDLED_TX_INT;
982 }
983 
984 static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
985 {
986 	struct uart_port *port = &priv->port;
987 	struct tty_struct *tty = tty_port_tty_get(&port->state->port);
988 	char   *error_msg[5] = {};
989 	int    i = 0;
990 
991 	if (lsr & PCH_UART_LSR_ERR)
992 		error_msg[i++] = "Error data in FIFO\n";
993 
994 	if (lsr & UART_LSR_FE) {
995 		port->icount.frame++;
996 		error_msg[i++] = "  Framing Error\n";
997 	}
998 
999 	if (lsr & UART_LSR_PE) {
1000 		port->icount.parity++;
1001 		error_msg[i++] = "  Parity Error\n";
1002 	}
1003 
1004 	if (lsr & UART_LSR_OE) {
1005 		port->icount.overrun++;
1006 		error_msg[i++] = "  Overrun Error\n";
1007 	}
1008 
1009 	if (tty == NULL) {
1010 		for (i = 0; error_msg[i] != NULL; i++)
1011 			dev_err(&priv->pdev->dev, error_msg[i]);
1012 	} else {
1013 		tty_kref_put(tty);
1014 	}
1015 }
1016 
1017 static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1018 {
1019 	struct eg20t_port *priv = dev_id;
1020 	unsigned int handled;
1021 	u8 lsr;
1022 	int ret = 0;
1023 	unsigned char iid;
1024 	unsigned long flags;
1025 	int next = 1;
1026 	u8 msr;
1027 
1028 	spin_lock_irqsave(&priv->lock, flags);
1029 	handled = 0;
1030 	while (next) {
1031 		iid = pch_uart_hal_get_iid(priv);
1032 		if (iid & PCH_UART_IIR_IP) /* No Interrupt */
1033 			break;
1034 		switch (iid) {
1035 		case PCH_UART_IID_RLS:	/* Receiver Line Status */
1036 			lsr = pch_uart_hal_get_line_status(priv);
1037 			if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1038 						UART_LSR_PE | UART_LSR_OE)) {
1039 				pch_uart_err_ir(priv, lsr);
1040 				ret = PCH_UART_HANDLED_RX_ERR_INT;
1041 			} else {
1042 				ret = PCH_UART_HANDLED_LS_INT;
1043 			}
1044 			break;
1045 		case PCH_UART_IID_RDR:	/* Received Data Ready */
1046 			if (priv->use_dma) {
1047 				pch_uart_hal_disable_interrupt(priv,
1048 						PCH_UART_HAL_RX_INT |
1049 						PCH_UART_HAL_RX_ERR_INT);
1050 				ret = dma_handle_rx(priv);
1051 				if (!ret)
1052 					pch_uart_hal_enable_interrupt(priv,
1053 						PCH_UART_HAL_RX_INT |
1054 						PCH_UART_HAL_RX_ERR_INT);
1055 			} else {
1056 				ret = handle_rx(priv);
1057 			}
1058 			break;
1059 		case PCH_UART_IID_RDR_TO:	/* Received Data Ready
1060 						   (FIFO Timeout) */
1061 			ret = handle_rx_to(priv);
1062 			break;
1063 		case PCH_UART_IID_THRE:	/* Transmitter Holding Register
1064 						   Empty */
1065 			if (priv->use_dma)
1066 				ret = dma_handle_tx(priv);
1067 			else
1068 				ret = handle_tx(priv);
1069 			break;
1070 		case PCH_UART_IID_MS:	/* Modem Status */
1071 			msr = pch_uart_hal_get_modem(priv);
1072 			next = 0; /* MS ir prioirty is the lowest. So, MS ir
1073 				     means final interrupt */
1074 			if ((msr & UART_MSR_ANY_DELTA) == 0)
1075 				break;
1076 			ret |= PCH_UART_HANDLED_MS_INT;
1077 			break;
1078 		default:	/* Never junp to this label */
1079 			dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
1080 				iid, jiffies);
1081 			ret = -1;
1082 			next = 0;
1083 			break;
1084 		}
1085 		handled |= (unsigned int)ret;
1086 	}
1087 
1088 	spin_unlock_irqrestore(&priv->lock, flags);
1089 	return IRQ_RETVAL(handled);
1090 }
1091 
1092 /* This function tests whether the transmitter fifo and shifter for the port
1093 						described by 'port' is empty. */
1094 static unsigned int pch_uart_tx_empty(struct uart_port *port)
1095 {
1096 	struct eg20t_port *priv;
1097 
1098 	priv = container_of(port, struct eg20t_port, port);
1099 	if (priv->tx_empty)
1100 		return TIOCSER_TEMT;
1101 	else
1102 		return 0;
1103 }
1104 
1105 /* Returns the current state of modem control inputs. */
1106 static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1107 {
1108 	struct eg20t_port *priv;
1109 	u8 modem;
1110 	unsigned int ret = 0;
1111 
1112 	priv = container_of(port, struct eg20t_port, port);
1113 	modem = pch_uart_hal_get_modem(priv);
1114 
1115 	if (modem & UART_MSR_DCD)
1116 		ret |= TIOCM_CAR;
1117 
1118 	if (modem & UART_MSR_RI)
1119 		ret |= TIOCM_RNG;
1120 
1121 	if (modem & UART_MSR_DSR)
1122 		ret |= TIOCM_DSR;
1123 
1124 	if (modem & UART_MSR_CTS)
1125 		ret |= TIOCM_CTS;
1126 
1127 	return ret;
1128 }
1129 
1130 static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1131 {
1132 	u32 mcr = 0;
1133 	struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1134 
1135 	if (mctrl & TIOCM_DTR)
1136 		mcr |= UART_MCR_DTR;
1137 	if (mctrl & TIOCM_RTS)
1138 		mcr |= UART_MCR_RTS;
1139 	if (mctrl & TIOCM_LOOP)
1140 		mcr |= UART_MCR_LOOP;
1141 
1142 	if (priv->mcr & UART_MCR_AFE)
1143 		mcr |= UART_MCR_AFE;
1144 
1145 	if (mctrl)
1146 		iowrite8(mcr, priv->membase + UART_MCR);
1147 }
1148 
1149 static void pch_uart_stop_tx(struct uart_port *port)
1150 {
1151 	struct eg20t_port *priv;
1152 	priv = container_of(port, struct eg20t_port, port);
1153 	priv->start_tx = 0;
1154 	priv->tx_dma_use = 0;
1155 }
1156 
1157 static void pch_uart_start_tx(struct uart_port *port)
1158 {
1159 	struct eg20t_port *priv;
1160 
1161 	priv = container_of(port, struct eg20t_port, port);
1162 
1163 	if (priv->use_dma) {
1164 		if (priv->tx_dma_use) {
1165 			dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1166 				__func__);
1167 			return;
1168 		}
1169 	}
1170 
1171 	priv->start_tx = 1;
1172 	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1173 }
1174 
1175 static void pch_uart_stop_rx(struct uart_port *port)
1176 {
1177 	struct eg20t_port *priv;
1178 	priv = container_of(port, struct eg20t_port, port);
1179 	priv->start_rx = 0;
1180 	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
1181 					     PCH_UART_HAL_RX_ERR_INT);
1182 }
1183 
1184 /* Enable the modem status interrupts. */
1185 static void pch_uart_enable_ms(struct uart_port *port)
1186 {
1187 	struct eg20t_port *priv;
1188 	priv = container_of(port, struct eg20t_port, port);
1189 	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1190 }
1191 
1192 /* Control the transmission of a break signal. */
1193 static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1194 {
1195 	struct eg20t_port *priv;
1196 	unsigned long flags;
1197 
1198 	priv = container_of(port, struct eg20t_port, port);
1199 	spin_lock_irqsave(&priv->lock, flags);
1200 	pch_uart_hal_set_break(priv, ctl);
1201 	spin_unlock_irqrestore(&priv->lock, flags);
1202 }
1203 
1204 /* Grab any interrupt resources and initialise any low level driver state. */
1205 static int pch_uart_startup(struct uart_port *port)
1206 {
1207 	struct eg20t_port *priv;
1208 	int ret;
1209 	int fifo_size;
1210 	int trigger_level;
1211 
1212 	priv = container_of(port, struct eg20t_port, port);
1213 	priv->tx_empty = 1;
1214 
1215 	if (port->uartclk)
1216 		priv->uartclk = port->uartclk;
1217 	else
1218 		port->uartclk = priv->uartclk;
1219 
1220 	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1221 	ret = pch_uart_hal_set_line(priv, default_baud,
1222 			      PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1223 			      PCH_UART_HAL_STB1);
1224 	if (ret)
1225 		return ret;
1226 
1227 	switch (priv->fifo_size) {
1228 	case 256:
1229 		fifo_size = PCH_UART_HAL_FIFO256;
1230 		break;
1231 	case 64:
1232 		fifo_size = PCH_UART_HAL_FIFO64;
1233 		break;
1234 	case 16:
1235 		fifo_size = PCH_UART_HAL_FIFO16;
1236 		break;
1237 	case 1:
1238 	default:
1239 		fifo_size = PCH_UART_HAL_FIFO_DIS;
1240 		break;
1241 	}
1242 
1243 	switch (priv->trigger) {
1244 	case PCH_UART_HAL_TRIGGER1:
1245 		trigger_level = 1;
1246 		break;
1247 	case PCH_UART_HAL_TRIGGER_L:
1248 		trigger_level = priv->fifo_size / 4;
1249 		break;
1250 	case PCH_UART_HAL_TRIGGER_M:
1251 		trigger_level = priv->fifo_size / 2;
1252 		break;
1253 	case PCH_UART_HAL_TRIGGER_H:
1254 	default:
1255 		trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1256 		break;
1257 	}
1258 
1259 	priv->trigger_level = trigger_level;
1260 	ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1261 				    fifo_size, priv->trigger);
1262 	if (ret < 0)
1263 		return ret;
1264 
1265 	ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1266 			priv->irq_name, priv);
1267 	if (ret < 0)
1268 		return ret;
1269 
1270 	if (priv->use_dma)
1271 		pch_request_dma(port);
1272 
1273 	priv->start_rx = 1;
1274 	pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
1275 					    PCH_UART_HAL_RX_ERR_INT);
1276 	uart_update_timeout(port, CS8, default_baud);
1277 
1278 	return 0;
1279 }
1280 
1281 static void pch_uart_shutdown(struct uart_port *port)
1282 {
1283 	struct eg20t_port *priv;
1284 	int ret;
1285 
1286 	priv = container_of(port, struct eg20t_port, port);
1287 	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1288 	pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1289 	ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1290 			      PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1291 	if (ret)
1292 		dev_err(priv->port.dev,
1293 			"pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1294 
1295 	pch_free_dma(port);
1296 
1297 	free_irq(priv->port.irq, priv);
1298 }
1299 
1300 /* Change the port parameters, including word length, parity, stop
1301  *bits.  Update read_status_mask and ignore_status_mask to indicate
1302  *the types of events we are interested in receiving.  */
1303 static void pch_uart_set_termios(struct uart_port *port,
1304 				 struct ktermios *termios, struct ktermios *old)
1305 {
1306 	int rtn;
1307 	unsigned int baud, parity, bits, stb;
1308 	struct eg20t_port *priv;
1309 	unsigned long flags;
1310 
1311 	priv = container_of(port, struct eg20t_port, port);
1312 	switch (termios->c_cflag & CSIZE) {
1313 	case CS5:
1314 		bits = PCH_UART_HAL_5BIT;
1315 		break;
1316 	case CS6:
1317 		bits = PCH_UART_HAL_6BIT;
1318 		break;
1319 	case CS7:
1320 		bits = PCH_UART_HAL_7BIT;
1321 		break;
1322 	default:		/* CS8 */
1323 		bits = PCH_UART_HAL_8BIT;
1324 		break;
1325 	}
1326 	if (termios->c_cflag & CSTOPB)
1327 		stb = PCH_UART_HAL_STB2;
1328 	else
1329 		stb = PCH_UART_HAL_STB1;
1330 
1331 	if (termios->c_cflag & PARENB) {
1332 		if (termios->c_cflag & PARODD)
1333 			parity = PCH_UART_HAL_PARITY_ODD;
1334 		else
1335 			parity = PCH_UART_HAL_PARITY_EVEN;
1336 
1337 	} else
1338 		parity = PCH_UART_HAL_PARITY_NONE;
1339 
1340 	/* Only UART0 has auto hardware flow function */
1341 	if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1342 		priv->mcr |= UART_MCR_AFE;
1343 	else
1344 		priv->mcr &= ~UART_MCR_AFE;
1345 
1346 	termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1347 
1348 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1349 
1350 	spin_lock_irqsave(&priv->lock, flags);
1351 	spin_lock(&port->lock);
1352 
1353 	uart_update_timeout(port, termios->c_cflag, baud);
1354 	rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1355 	if (rtn)
1356 		goto out;
1357 
1358 	pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
1359 	/* Don't rewrite B0 */
1360 	if (tty_termios_baud_rate(termios))
1361 		tty_termios_encode_baud_rate(termios, baud, baud);
1362 
1363 out:
1364 	spin_unlock(&port->lock);
1365 	spin_unlock_irqrestore(&priv->lock, flags);
1366 }
1367 
1368 static const char *pch_uart_type(struct uart_port *port)
1369 {
1370 	return KBUILD_MODNAME;
1371 }
1372 
1373 static void pch_uart_release_port(struct uart_port *port)
1374 {
1375 	struct eg20t_port *priv;
1376 
1377 	priv = container_of(port, struct eg20t_port, port);
1378 	pci_iounmap(priv->pdev, priv->membase);
1379 	pci_release_regions(priv->pdev);
1380 }
1381 
1382 static int pch_uart_request_port(struct uart_port *port)
1383 {
1384 	struct eg20t_port *priv;
1385 	int ret;
1386 	void __iomem *membase;
1387 
1388 	priv = container_of(port, struct eg20t_port, port);
1389 	ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1390 	if (ret < 0)
1391 		return -EBUSY;
1392 
1393 	membase = pci_iomap(priv->pdev, 1, 0);
1394 	if (!membase) {
1395 		pci_release_regions(priv->pdev);
1396 		return -EBUSY;
1397 	}
1398 	priv->membase = port->membase = membase;
1399 
1400 	return 0;
1401 }
1402 
1403 static void pch_uart_config_port(struct uart_port *port, int type)
1404 {
1405 	struct eg20t_port *priv;
1406 
1407 	priv = container_of(port, struct eg20t_port, port);
1408 	if (type & UART_CONFIG_TYPE) {
1409 		port->type = priv->port_type;
1410 		pch_uart_request_port(port);
1411 	}
1412 }
1413 
1414 static int pch_uart_verify_port(struct uart_port *port,
1415 				struct serial_struct *serinfo)
1416 {
1417 	struct eg20t_port *priv;
1418 
1419 	priv = container_of(port, struct eg20t_port, port);
1420 	if (serinfo->flags & UPF_LOW_LATENCY) {
1421 		dev_info(priv->port.dev,
1422 			"PCH UART : Use PIO Mode (without DMA)\n");
1423 		priv->use_dma = 0;
1424 		serinfo->flags &= ~UPF_LOW_LATENCY;
1425 	} else {
1426 #ifndef CONFIG_PCH_DMA
1427 		dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1428 			__func__);
1429 		return -EOPNOTSUPP;
1430 #endif
1431 		if (!priv->use_dma) {
1432 			pch_request_dma(port);
1433 			if (priv->chan_rx)
1434 				priv->use_dma = 1;
1435 		}
1436 		dev_info(priv->port.dev, "PCH UART: %s\n",
1437 				priv->use_dma ?
1438 				"Use DMA Mode" : "No DMA");
1439 	}
1440 
1441 	return 0;
1442 }
1443 
1444 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_PCH_UART_CONSOLE)
1445 /*
1446  *	Wait for transmitter & holding register to empty
1447  */
1448 static void wait_for_xmitr(struct eg20t_port *up, int bits)
1449 {
1450 	unsigned int status, tmout = 10000;
1451 
1452 	/* Wait up to 10ms for the character(s) to be sent. */
1453 	for (;;) {
1454 		status = ioread8(up->membase + UART_LSR);
1455 
1456 		if ((status & bits) == bits)
1457 			break;
1458 		if (--tmout == 0)
1459 			break;
1460 		udelay(1);
1461 	}
1462 
1463 	/* Wait up to 1s for flow control if necessary */
1464 	if (up->port.flags & UPF_CONS_FLOW) {
1465 		unsigned int tmout;
1466 		for (tmout = 1000000; tmout; tmout--) {
1467 			unsigned int msr = ioread8(up->membase + UART_MSR);
1468 			if (msr & UART_MSR_CTS)
1469 				break;
1470 			udelay(1);
1471 			touch_nmi_watchdog();
1472 		}
1473 	}
1474 }
1475 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_PCH_UART_CONSOLE */
1476 
1477 #ifdef CONFIG_CONSOLE_POLL
1478 /*
1479  * Console polling routines for communicate via uart while
1480  * in an interrupt or debug context.
1481  */
1482 static int pch_uart_get_poll_char(struct uart_port *port)
1483 {
1484 	struct eg20t_port *priv =
1485 		container_of(port, struct eg20t_port, port);
1486 	u8 lsr = ioread8(priv->membase + UART_LSR);
1487 
1488 	if (!(lsr & UART_LSR_DR))
1489 		return NO_POLL_CHAR;
1490 
1491 	return ioread8(priv->membase + PCH_UART_RBR);
1492 }
1493 
1494 
1495 static void pch_uart_put_poll_char(struct uart_port *port,
1496 			 unsigned char c)
1497 {
1498 	unsigned int ier;
1499 	struct eg20t_port *priv =
1500 		container_of(port, struct eg20t_port, port);
1501 
1502 	/*
1503 	 * First save the IER then disable the interrupts
1504 	 */
1505 	ier = ioread8(priv->membase + UART_IER);
1506 	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1507 
1508 	wait_for_xmitr(priv, UART_LSR_THRE);
1509 	/*
1510 	 * Send the character out.
1511 	 */
1512 	iowrite8(c, priv->membase + PCH_UART_THR);
1513 
1514 	/*
1515 	 * Finally, wait for transmitter to become empty
1516 	 * and restore the IER
1517 	 */
1518 	wait_for_xmitr(priv, UART_LSR_BOTH_EMPTY);
1519 	iowrite8(ier, priv->membase + UART_IER);
1520 }
1521 #endif /* CONFIG_CONSOLE_POLL */
1522 
1523 static const struct uart_ops pch_uart_ops = {
1524 	.tx_empty = pch_uart_tx_empty,
1525 	.set_mctrl = pch_uart_set_mctrl,
1526 	.get_mctrl = pch_uart_get_mctrl,
1527 	.stop_tx = pch_uart_stop_tx,
1528 	.start_tx = pch_uart_start_tx,
1529 	.stop_rx = pch_uart_stop_rx,
1530 	.enable_ms = pch_uart_enable_ms,
1531 	.break_ctl = pch_uart_break_ctl,
1532 	.startup = pch_uart_startup,
1533 	.shutdown = pch_uart_shutdown,
1534 	.set_termios = pch_uart_set_termios,
1535 /*	.pm		= pch_uart_pm,		Not supported yet */
1536 	.type = pch_uart_type,
1537 	.release_port = pch_uart_release_port,
1538 	.request_port = pch_uart_request_port,
1539 	.config_port = pch_uart_config_port,
1540 	.verify_port = pch_uart_verify_port,
1541 #ifdef CONFIG_CONSOLE_POLL
1542 	.poll_get_char = pch_uart_get_poll_char,
1543 	.poll_put_char = pch_uart_put_poll_char,
1544 #endif
1545 };
1546 
1547 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1548 
1549 static void pch_console_putchar(struct uart_port *port, unsigned char ch)
1550 {
1551 	struct eg20t_port *priv =
1552 		container_of(port, struct eg20t_port, port);
1553 
1554 	wait_for_xmitr(priv, UART_LSR_THRE);
1555 	iowrite8(ch, priv->membase + PCH_UART_THR);
1556 }
1557 
1558 /*
1559  *	Print a string to the serial port trying not to disturb
1560  *	any possible real use of the port...
1561  *
1562  *	The console_lock must be held when we get here.
1563  */
1564 static void
1565 pch_console_write(struct console *co, const char *s, unsigned int count)
1566 {
1567 	struct eg20t_port *priv;
1568 	unsigned long flags;
1569 	int priv_locked = 1;
1570 	int port_locked = 1;
1571 	u8 ier;
1572 
1573 	priv = pch_uart_ports[co->index];
1574 
1575 	touch_nmi_watchdog();
1576 
1577 	local_irq_save(flags);
1578 	if (priv->port.sysrq) {
1579 		/* call to uart_handle_sysrq_char already took the priv lock */
1580 		priv_locked = 0;
1581 		/* serial8250_handle_port() already took the port lock */
1582 		port_locked = 0;
1583 	} else if (oops_in_progress) {
1584 		priv_locked = spin_trylock(&priv->lock);
1585 		port_locked = spin_trylock(&priv->port.lock);
1586 	} else {
1587 		spin_lock(&priv->lock);
1588 		spin_lock(&priv->port.lock);
1589 	}
1590 
1591 	/*
1592 	 *	First save the IER then disable the interrupts
1593 	 */
1594 	ier = ioread8(priv->membase + UART_IER);
1595 
1596 	pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1597 
1598 	uart_console_write(&priv->port, s, count, pch_console_putchar);
1599 
1600 	/*
1601 	 *	Finally, wait for transmitter to become empty
1602 	 *	and restore the IER
1603 	 */
1604 	wait_for_xmitr(priv, UART_LSR_BOTH_EMPTY);
1605 	iowrite8(ier, priv->membase + UART_IER);
1606 
1607 	if (port_locked)
1608 		spin_unlock(&priv->port.lock);
1609 	if (priv_locked)
1610 		spin_unlock(&priv->lock);
1611 	local_irq_restore(flags);
1612 }
1613 
1614 static int __init pch_console_setup(struct console *co, char *options)
1615 {
1616 	struct uart_port *port;
1617 	int baud = default_baud;
1618 	int bits = 8;
1619 	int parity = 'n';
1620 	int flow = 'n';
1621 
1622 	/*
1623 	 * Check whether an invalid uart number has been specified, and
1624 	 * if so, search for the first available port that does have
1625 	 * console support.
1626 	 */
1627 	if (co->index >= PCH_UART_NR)
1628 		co->index = 0;
1629 	port = &pch_uart_ports[co->index]->port;
1630 
1631 	if (!port || (!port->iobase && !port->membase))
1632 		return -ENODEV;
1633 
1634 	port->uartclk = pch_uart_get_uartclk();
1635 
1636 	if (options)
1637 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1638 
1639 	return uart_set_options(port, co, baud, parity, bits, flow);
1640 }
1641 
1642 static struct uart_driver pch_uart_driver;
1643 
1644 static struct console pch_console = {
1645 	.name		= PCH_UART_DRIVER_DEVICE,
1646 	.write		= pch_console_write,
1647 	.device		= uart_console_device,
1648 	.setup		= pch_console_setup,
1649 	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
1650 	.index		= -1,
1651 	.data		= &pch_uart_driver,
1652 };
1653 
1654 #define PCH_CONSOLE	(&pch_console)
1655 #else
1656 #define PCH_CONSOLE	NULL
1657 #endif	/* CONFIG_SERIAL_PCH_UART_CONSOLE */
1658 
1659 static struct uart_driver pch_uart_driver = {
1660 	.owner = THIS_MODULE,
1661 	.driver_name = KBUILD_MODNAME,
1662 	.dev_name = PCH_UART_DRIVER_DEVICE,
1663 	.major = 0,
1664 	.minor = 0,
1665 	.nr = PCH_UART_NR,
1666 	.cons = PCH_CONSOLE,
1667 };
1668 
1669 static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
1670 					     const struct pci_device_id *id)
1671 {
1672 	struct eg20t_port *priv;
1673 	int ret;
1674 	unsigned int iobase;
1675 	unsigned int mapbase;
1676 	unsigned char *rxbuf;
1677 	int fifosize;
1678 	int port_type;
1679 	struct pch_uart_driver_data *board;
1680 	char name[32];
1681 
1682 	board = &drv_dat[id->driver_data];
1683 	port_type = board->port_type;
1684 
1685 	priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1686 	if (priv == NULL)
1687 		goto init_port_alloc_err;
1688 
1689 	rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1690 	if (!rxbuf)
1691 		goto init_port_free_txbuf;
1692 
1693 	switch (port_type) {
1694 	case PORT_PCH_8LINE:
1695 		fifosize = 256; /* EG20T/ML7213: UART0 */
1696 		break;
1697 	case PORT_PCH_2LINE:
1698 		fifosize = 64; /* EG20T:UART1~3  ML7213: UART1~2*/
1699 		break;
1700 	default:
1701 		dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1702 		goto init_port_hal_free;
1703 	}
1704 
1705 	pci_enable_msi(pdev);
1706 	pci_set_master(pdev);
1707 
1708 	spin_lock_init(&priv->lock);
1709 
1710 	iobase = pci_resource_start(pdev, 0);
1711 	mapbase = pci_resource_start(pdev, 1);
1712 	priv->mapbase = mapbase;
1713 	priv->iobase = iobase;
1714 	priv->pdev = pdev;
1715 	priv->tx_empty = 1;
1716 	priv->rxbuf.buf = rxbuf;
1717 	priv->rxbuf.size = PAGE_SIZE;
1718 
1719 	priv->fifo_size = fifosize;
1720 	priv->uartclk = pch_uart_get_uartclk();
1721 	priv->port_type = port_type;
1722 	priv->port.dev = &pdev->dev;
1723 	priv->port.iobase = iobase;
1724 	priv->port.membase = NULL;
1725 	priv->port.mapbase = mapbase;
1726 	priv->port.irq = pdev->irq;
1727 	priv->port.iotype = UPIO_PORT;
1728 	priv->port.ops = &pch_uart_ops;
1729 	priv->port.flags = UPF_BOOT_AUTOCONF;
1730 	priv->port.fifosize = fifosize;
1731 	priv->port.line = board->line_no;
1732 	priv->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_PCH_UART_CONSOLE);
1733 	priv->trigger = PCH_UART_HAL_TRIGGER_M;
1734 
1735 	snprintf(priv->irq_name, IRQ_NAME_SIZE,
1736 		 KBUILD_MODNAME ":" PCH_UART_DRIVER_DEVICE "%d",
1737 		 priv->port.line);
1738 
1739 	spin_lock_init(&priv->port.lock);
1740 
1741 	pci_set_drvdata(pdev, priv);
1742 	priv->trigger_level = 1;
1743 	priv->fcr = 0;
1744 
1745 	if (pdev->dev.of_node)
1746 		of_property_read_u32(pdev->dev.of_node, "clock-frequency"
1747 					 , &user_uartclk);
1748 
1749 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1750 	pch_uart_ports[board->line_no] = priv;
1751 #endif
1752 	ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1753 	if (ret < 0)
1754 		goto init_port_hal_free;
1755 
1756 	snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
1757 	debugfs_create_file(name, S_IFREG | S_IRUGO, NULL, priv,
1758 			    &port_regs_ops);
1759 
1760 	return priv;
1761 
1762 init_port_hal_free:
1763 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1764 	pch_uart_ports[board->line_no] = NULL;
1765 #endif
1766 	free_page((unsigned long)rxbuf);
1767 init_port_free_txbuf:
1768 	kfree(priv);
1769 init_port_alloc_err:
1770 
1771 	return NULL;
1772 }
1773 
1774 static void pch_uart_exit_port(struct eg20t_port *priv)
1775 {
1776 	char name[32];
1777 
1778 	snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
1779 	debugfs_remove(debugfs_lookup(name, NULL));
1780 	uart_remove_one_port(&pch_uart_driver, &priv->port);
1781 	free_page((unsigned long)priv->rxbuf.buf);
1782 }
1783 
1784 static void pch_uart_pci_remove(struct pci_dev *pdev)
1785 {
1786 	struct eg20t_port *priv = pci_get_drvdata(pdev);
1787 
1788 	pci_disable_msi(pdev);
1789 
1790 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1791 	pch_uart_ports[priv->port.line] = NULL;
1792 #endif
1793 	pch_uart_exit_port(priv);
1794 	pci_disable_device(pdev);
1795 	kfree(priv);
1796 	return;
1797 }
1798 
1799 static int __maybe_unused pch_uart_pci_suspend(struct device *dev)
1800 {
1801 	struct eg20t_port *priv = dev_get_drvdata(dev);
1802 
1803 	uart_suspend_port(&pch_uart_driver, &priv->port);
1804 
1805 	return 0;
1806 }
1807 
1808 static int __maybe_unused pch_uart_pci_resume(struct device *dev)
1809 {
1810 	struct eg20t_port *priv = dev_get_drvdata(dev);
1811 
1812 	uart_resume_port(&pch_uart_driver, &priv->port);
1813 
1814 	return 0;
1815 }
1816 
1817 static const struct pci_device_id pch_uart_pci_id[] = {
1818 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1819 	 .driver_data = pch_et20t_uart0},
1820 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1821 	 .driver_data = pch_et20t_uart1},
1822 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1823 	 .driver_data = pch_et20t_uart2},
1824 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1825 	 .driver_data = pch_et20t_uart3},
1826 	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1827 	 .driver_data = pch_ml7213_uart0},
1828 	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1829 	 .driver_data = pch_ml7213_uart1},
1830 	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1831 	 .driver_data = pch_ml7213_uart2},
1832 	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1833 	 .driver_data = pch_ml7223_uart0},
1834 	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1835 	 .driver_data = pch_ml7223_uart1},
1836 	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1837 	 .driver_data = pch_ml7831_uart0},
1838 	{PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1839 	 .driver_data = pch_ml7831_uart1},
1840 	{0,},
1841 };
1842 
1843 static int pch_uart_pci_probe(struct pci_dev *pdev,
1844 					const struct pci_device_id *id)
1845 {
1846 	int ret;
1847 	struct eg20t_port *priv;
1848 
1849 	ret = pci_enable_device(pdev);
1850 	if (ret < 0)
1851 		goto probe_error;
1852 
1853 	priv = pch_uart_init_port(pdev, id);
1854 	if (!priv) {
1855 		ret = -EBUSY;
1856 		goto probe_disable_device;
1857 	}
1858 	pci_set_drvdata(pdev, priv);
1859 
1860 	return ret;
1861 
1862 probe_disable_device:
1863 	pci_disable_msi(pdev);
1864 	pci_disable_device(pdev);
1865 probe_error:
1866 	return ret;
1867 }
1868 
1869 static SIMPLE_DEV_PM_OPS(pch_uart_pci_pm_ops,
1870 			 pch_uart_pci_suspend,
1871 			 pch_uart_pci_resume);
1872 
1873 static struct pci_driver pch_uart_pci_driver = {
1874 	.name = "pch_uart",
1875 	.id_table = pch_uart_pci_id,
1876 	.probe = pch_uart_pci_probe,
1877 	.remove = pch_uart_pci_remove,
1878 	.driver.pm = &pch_uart_pci_pm_ops,
1879 };
1880 
1881 static int __init pch_uart_module_init(void)
1882 {
1883 	int ret;
1884 
1885 	/* register as UART driver */
1886 	ret = uart_register_driver(&pch_uart_driver);
1887 	if (ret < 0)
1888 		return ret;
1889 
1890 	/* register as PCI driver */
1891 	ret = pci_register_driver(&pch_uart_pci_driver);
1892 	if (ret < 0)
1893 		uart_unregister_driver(&pch_uart_driver);
1894 
1895 	return ret;
1896 }
1897 module_init(pch_uart_module_init);
1898 
1899 static void __exit pch_uart_module_exit(void)
1900 {
1901 	pci_unregister_driver(&pch_uart_pci_driver);
1902 	uart_unregister_driver(&pch_uart_driver);
1903 }
1904 module_exit(pch_uart_module_exit);
1905 
1906 MODULE_LICENSE("GPL v2");
1907 MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1908 MODULE_DEVICE_TABLE(pci, pch_uart_pci_id);
1909 
1910 module_param(default_baud, uint, S_IRUGO);
1911 MODULE_PARM_DESC(default_baud,
1912                  "Default BAUD for initial driver state and console (default 9600)");
1913 module_param(user_uartclk, uint, S_IRUGO);
1914 MODULE_PARM_DESC(user_uartclk,
1915                  "Override UART default or board specific UART clock");
1916