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