1 // SPDX-License-Identifier: GPL-2.0
3 *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
5 #include <linux/kernel.h>
6 #include <linux/serial_reg.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
10 #include <linux/console.h>
11 #include <linux/serial_core.h>
12 #include <linux/tty.h>
13 #include <linux/tty_flip.h>
14 #include <linux/interrupt.h>
16 #include <linux/dmi.h>
17 #include <linux/nmi.h>
18 #include <linux/delay.h>
21 #include <linux/debugfs.h>
22 #include <linux/dmaengine.h>
23 #include <linux/pch_dma.h>
26 PCH_UART_HANDLED_RX_INT_SHIFT,
27 PCH_UART_HANDLED_TX_INT_SHIFT,
28 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
29 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
30 PCH_UART_HANDLED_MS_INT_SHIFT,
31 PCH_UART_HANDLED_LS_INT_SHIFT,
34 #define PCH_UART_DRIVER_DEVICE "ttyPCH"
36 /* Set the max number of UART port
37 * Intel EG20T PCH: 4 port
38 * LAPIS Semiconductor ML7213 IOH: 3 port
39 * LAPIS Semiconductor ML7223 IOH: 2 port
43 #define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
44 #define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
45 #define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
46 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
47 #define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
48 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
49 #define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
51 #define PCH_UART_HANDLED_LS_INT (1<<((PCH_UART_HANDLED_LS_INT_SHIFT)<<1))
53 #define PCH_UART_RBR 0x00
54 #define PCH_UART_THR 0x00
56 #define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
57 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
58 #define PCH_UART_IER_ERBFI 0x00000001
59 #define PCH_UART_IER_ETBEI 0x00000002
60 #define PCH_UART_IER_ELSI 0x00000004
61 #define PCH_UART_IER_EDSSI 0x00000008
63 #define PCH_UART_IIR_IP 0x00000001
64 #define PCH_UART_IIR_IID 0x00000006
65 #define PCH_UART_IIR_MSI 0x00000000
66 #define PCH_UART_IIR_TRI 0x00000002
67 #define PCH_UART_IIR_RRI 0x00000004
68 #define PCH_UART_IIR_REI 0x00000006
69 #define PCH_UART_IIR_TOI 0x00000008
70 #define PCH_UART_IIR_FIFO256 0x00000020
71 #define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
72 #define PCH_UART_IIR_FE 0x000000C0
74 #define PCH_UART_FCR_FIFOE 0x00000001
75 #define PCH_UART_FCR_RFR 0x00000002
76 #define PCH_UART_FCR_TFR 0x00000004
77 #define PCH_UART_FCR_DMS 0x00000008
78 #define PCH_UART_FCR_FIFO256 0x00000020
79 #define PCH_UART_FCR_RFTL 0x000000C0
81 #define PCH_UART_FCR_RFTL1 0x00000000
82 #define PCH_UART_FCR_RFTL64 0x00000040
83 #define PCH_UART_FCR_RFTL128 0x00000080
84 #define PCH_UART_FCR_RFTL224 0x000000C0
85 #define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
86 #define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
87 #define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
88 #define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
89 #define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
90 #define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
91 #define PCH_UART_FCR_RFTL_SHIFT 6
93 #define PCH_UART_LCR_WLS 0x00000003
94 #define PCH_UART_LCR_STB 0x00000004
95 #define PCH_UART_LCR_PEN 0x00000008
96 #define PCH_UART_LCR_EPS 0x00000010
97 #define PCH_UART_LCR_SP 0x00000020
98 #define PCH_UART_LCR_SB 0x00000040
99 #define PCH_UART_LCR_DLAB 0x00000080
100 #define PCH_UART_LCR_NP 0x00000000
101 #define PCH_UART_LCR_OP PCH_UART_LCR_PEN
102 #define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
103 #define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
104 #define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
107 #define PCH_UART_LCR_5BIT 0x00000000
108 #define PCH_UART_LCR_6BIT 0x00000001
109 #define PCH_UART_LCR_7BIT 0x00000002
110 #define PCH_UART_LCR_8BIT 0x00000003
112 #define PCH_UART_MCR_DTR 0x00000001
113 #define PCH_UART_MCR_RTS 0x00000002
114 #define PCH_UART_MCR_OUT 0x0000000C
115 #define PCH_UART_MCR_LOOP 0x00000010
116 #define PCH_UART_MCR_AFE 0x00000020
118 #define PCH_UART_LSR_DR 0x00000001
119 #define PCH_UART_LSR_ERR (1<<7)
121 #define PCH_UART_MSR_DCTS 0x00000001
122 #define PCH_UART_MSR_DDSR 0x00000002
123 #define PCH_UART_MSR_TERI 0x00000004
124 #define PCH_UART_MSR_DDCD 0x00000008
125 #define PCH_UART_MSR_CTS 0x00000010
126 #define PCH_UART_MSR_DSR 0x00000020
127 #define PCH_UART_MSR_RI 0x00000040
128 #define PCH_UART_MSR_DCD 0x00000080
129 #define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
130 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
132 #define PCH_UART_DLL 0x00
133 #define PCH_UART_DLM 0x01
135 #define PCH_UART_BRCSR 0x0E
137 #define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
138 #define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
139 #define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
140 #define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
141 #define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
143 #define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
144 #define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
145 #define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
146 #define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
147 #define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
148 #define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
149 #define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
150 #define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
151 #define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
152 #define PCH_UART_HAL_STB1 0
153 #define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
155 #define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
156 #define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
157 #define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
158 PCH_UART_HAL_CLR_RX_FIFO)
160 #define PCH_UART_HAL_DMA_MODE0 0
161 #define PCH_UART_HAL_FIFO_DIS 0
162 #define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
163 #define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
164 PCH_UART_FCR_FIFO256)
165 #define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
166 #define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
167 #define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
168 #define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
169 #define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
170 #define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
171 #define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
172 #define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
173 #define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
174 #define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
175 #define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
176 #define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
177 #define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
178 #define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
180 #define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
181 #define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
182 #define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
183 #define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
184 #define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
186 #define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
187 #define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
188 #define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
189 #define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
190 #define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
192 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
194 #define DEFAULT_UARTCLK 1843200 /* 1.8432 MHz */
195 #define CMITC_UARTCLK 192000000 /* 192.0000 MHz */
196 #define FRI2_64_UARTCLK 64000000 /* 64.0000 MHz */
197 #define FRI2_48_UARTCLK 48000000 /* 48.0000 MHz */
198 #define NTC1_UARTCLK 64000000 /* 64.0000 MHz */
199 #define MINNOW_UARTCLK 50000000 /* 50.0000 MHz */
201 struct pch_uart_buffer {
207 struct uart_port port;
209 void __iomem *membase;
210 resource_size_t mapbase;
212 struct pci_dev *pdev;
214 unsigned int uartclk;
220 struct pch_uart_buffer rxbuf;
224 unsigned int use_dma;
225 struct dma_async_tx_descriptor *desc_tx;
226 struct dma_async_tx_descriptor *desc_rx;
227 struct pch_dma_slave param_tx;
228 struct pch_dma_slave param_rx;
229 struct dma_chan *chan_tx;
230 struct dma_chan *chan_rx;
231 struct scatterlist *sg_tx_p;
234 struct scatterlist sg_rx;
237 dma_addr_t rx_buf_dma;
239 #define IRQ_NAME_SIZE 17
240 char irq_name[IRQ_NAME_SIZE];
242 /* protect the eg20t_port private structure and io access to membase */
247 * struct pch_uart_driver_data - private data structure for UART-DMA
248 * @port_type: The type of UART port
249 * @line_no: UART port line number (0, 1, 2...)
251 struct pch_uart_driver_data {
256 enum pch_uart_num_t {
270 static struct pch_uart_driver_data drv_dat[] = {
271 [pch_et20t_uart0] = {PORT_PCH_8LINE, 0},
272 [pch_et20t_uart1] = {PORT_PCH_2LINE, 1},
273 [pch_et20t_uart2] = {PORT_PCH_2LINE, 2},
274 [pch_et20t_uart3] = {PORT_PCH_2LINE, 3},
275 [pch_ml7213_uart0] = {PORT_PCH_8LINE, 0},
276 [pch_ml7213_uart1] = {PORT_PCH_2LINE, 1},
277 [pch_ml7213_uart2] = {PORT_PCH_2LINE, 2},
278 [pch_ml7223_uart0] = {PORT_PCH_8LINE, 0},
279 [pch_ml7223_uart1] = {PORT_PCH_2LINE, 1},
280 [pch_ml7831_uart0] = {PORT_PCH_8LINE, 0},
281 [pch_ml7831_uart1] = {PORT_PCH_2LINE, 1},
284 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
285 static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
287 static unsigned int default_baud = 9600;
288 static unsigned int user_uartclk = 0;
289 static const int trigger_level_256[4] = { 1, 64, 128, 224 };
290 static const int trigger_level_64[4] = { 1, 16, 32, 56 };
291 static const int trigger_level_16[4] = { 1, 4, 8, 14 };
292 static const int trigger_level_1[4] = { 1, 1, 1, 1 };
294 #define PCH_REGS_BUFSIZE 1024
297 static ssize_t port_show_regs(struct file *file, char __user *user_buf,
298 size_t count, loff_t *ppos)
300 struct eg20t_port *priv = file->private_data;
306 buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
310 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
311 "PCH EG20T port[%d] regs:\n", priv->port.line);
313 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
314 "=================================\n");
315 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
316 "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
317 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
318 "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
319 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
320 "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
321 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
322 "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
323 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
324 "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
325 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
326 "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
327 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
329 ioread8(priv->membase + PCH_UART_BRCSR));
331 lcr = ioread8(priv->membase + UART_LCR);
332 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
333 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
334 "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
335 len += scnprintf(buf + len, PCH_REGS_BUFSIZE - len,
336 "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
337 iowrite8(lcr, priv->membase + UART_LCR);
339 if (len > PCH_REGS_BUFSIZE)
340 len = PCH_REGS_BUFSIZE;
342 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
347 static const struct file_operations port_regs_ops = {
348 .owner = THIS_MODULE,
350 .read = port_show_regs,
351 .llseek = default_llseek,
354 static const struct dmi_system_id pch_uart_dmi_table[] = {
358 DMI_MATCH(DMI_BOARD_NAME, "CM-iTC"),
360 (void *)CMITC_UARTCLK,
365 DMI_MATCH(DMI_BIOS_VERSION, "FRI2"),
367 (void *)FRI2_64_UARTCLK,
370 .ident = "Fish River Island II",
372 DMI_MATCH(DMI_PRODUCT_NAME, "Fish River Island II"),
374 (void *)FRI2_48_UARTCLK,
379 DMI_MATCH(DMI_BOARD_NAME, "COMe-mTT"),
381 (void *)NTC1_UARTCLK,
384 .ident = "nanoETXexpress-TT",
386 DMI_MATCH(DMI_BOARD_NAME, "nanoETXexpress-TT"),
388 (void *)NTC1_UARTCLK,
391 .ident = "MinnowBoard",
393 DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
395 (void *)MINNOW_UARTCLK,
400 /* Return UART clock, checking for board specific clocks. */
401 static unsigned int pch_uart_get_uartclk(void)
403 const struct dmi_system_id *d;
408 d = dmi_first_match(pch_uart_dmi_table);
410 return (unsigned long)d->driver_data;
412 return DEFAULT_UARTCLK;
415 static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
418 u8 ier = ioread8(priv->membase + UART_IER);
419 ier |= flag & PCH_UART_IER_MASK;
420 iowrite8(ier, priv->membase + UART_IER);
423 static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
426 u8 ier = ioread8(priv->membase + UART_IER);
427 ier &= ~(flag & PCH_UART_IER_MASK);
428 iowrite8(ier, priv->membase + UART_IER);
431 static int pch_uart_hal_set_line(struct eg20t_port *priv, unsigned int baud,
432 unsigned int parity, unsigned int bits,
435 unsigned int dll, dlm, lcr;
438 div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
439 if (div < 0 || USHRT_MAX <= div) {
440 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
444 dll = (unsigned int)div & 0x00FFU;
445 dlm = ((unsigned int)div >> 8) & 0x00FFU;
447 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
448 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
452 if (bits & ~PCH_UART_LCR_WLS) {
453 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
457 if (stb & ~PCH_UART_LCR_STB) {
458 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
466 dev_dbg(priv->port.dev, "%s:baud = %u, div = %04x, lcr = %02x (%lu)\n",
467 __func__, baud, div, lcr, jiffies);
468 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
469 iowrite8(dll, priv->membase + PCH_UART_DLL);
470 iowrite8(dlm, priv->membase + PCH_UART_DLM);
471 iowrite8(lcr, priv->membase + UART_LCR);
476 static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
479 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
480 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
485 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
486 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
487 priv->membase + UART_FCR);
488 iowrite8(priv->fcr, priv->membase + UART_FCR);
493 static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
494 unsigned int dmamode,
495 unsigned int fifo_size, unsigned int trigger)
499 if (dmamode & ~PCH_UART_FCR_DMS) {
500 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
505 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
506 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
507 __func__, fifo_size);
511 if (trigger & ~PCH_UART_FCR_RFTL) {
512 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
517 switch (priv->fifo_size) {
519 priv->trigger_level =
520 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
523 priv->trigger_level =
524 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
527 priv->trigger_level =
528 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
531 priv->trigger_level =
532 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
536 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
537 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
538 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
539 priv->membase + UART_FCR);
540 iowrite8(fcr, priv->membase + UART_FCR);
546 static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
548 unsigned int msr = ioread8(priv->membase + UART_MSR);
549 priv->dmsr = msr & PCH_UART_MSR_DELTA;
553 static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
558 struct uart_port *port = &priv->port;
560 lsr = ioread8(priv->membase + UART_LSR);
561 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
562 i < rx_size && lsr & (UART_LSR_DR | UART_LSR_BI);
563 lsr = ioread8(priv->membase + UART_LSR)) {
564 rbr = ioread8(priv->membase + PCH_UART_RBR);
566 if (lsr & UART_LSR_BI) {
568 if (uart_handle_break(port))
571 if (uart_handle_sysrq_char(port, rbr))
579 static unsigned char pch_uart_hal_get_iid(struct eg20t_port *priv)
581 return ioread8(priv->membase + UART_IIR) &\
582 (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP);
585 static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
587 return ioread8(priv->membase + UART_LSR);
590 static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
594 lcr = ioread8(priv->membase + UART_LCR);
596 lcr |= PCH_UART_LCR_SB;
598 lcr &= ~PCH_UART_LCR_SB;
600 iowrite8(lcr, priv->membase + UART_LCR);
603 static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
606 struct uart_port *port = &priv->port;
607 struct tty_port *tport = &port->state->port;
609 tty_insert_flip_string(tport, buf, size);
610 tty_flip_buffer_push(tport);
615 static int dma_push_rx(struct eg20t_port *priv, int size)
618 struct uart_port *port = &priv->port;
619 struct tty_port *tport = &port->state->port;
621 room = tty_buffer_request_room(tport, size);
624 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
629 tty_insert_flip_string(tport, sg_virt(&priv->sg_rx), size);
631 port->icount.rx += room;
636 static void pch_free_dma(struct uart_port *port)
638 struct eg20t_port *priv;
639 priv = container_of(port, struct eg20t_port, port);
642 dma_release_channel(priv->chan_tx);
643 priv->chan_tx = NULL;
646 dma_release_channel(priv->chan_rx);
647 priv->chan_rx = NULL;
650 if (priv->rx_buf_dma) {
651 dma_free_coherent(port->dev, port->fifosize, priv->rx_buf_virt,
653 priv->rx_buf_virt = NULL;
654 priv->rx_buf_dma = 0;
660 static bool filter(struct dma_chan *chan, void *slave)
662 struct pch_dma_slave *param = slave;
664 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
665 chan->device->dev)) {
666 chan->private = param;
673 static void pch_request_dma(struct uart_port *port)
676 struct dma_chan *chan;
677 struct pci_dev *dma_dev;
678 struct pch_dma_slave *param;
679 struct eg20t_port *priv =
680 container_of(port, struct eg20t_port, port);
682 dma_cap_set(DMA_SLAVE, mask);
684 /* Get DMA's dev information */
685 dma_dev = pci_get_slot(priv->pdev->bus,
686 PCI_DEVFN(PCI_SLOT(priv->pdev->devfn), 0));
689 param = &priv->param_tx;
690 param->dma_dev = &dma_dev->dev;
691 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
693 param->tx_reg = port->mapbase + UART_TX;
694 chan = dma_request_channel(mask, filter, param);
696 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
700 priv->chan_tx = chan;
703 param = &priv->param_rx;
704 param->dma_dev = &dma_dev->dev;
705 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
707 param->rx_reg = port->mapbase + UART_RX;
708 chan = dma_request_channel(mask, filter, param);
710 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
712 dma_release_channel(priv->chan_tx);
713 priv->chan_tx = NULL;
717 /* Get Consistent memory for DMA */
718 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
719 &priv->rx_buf_dma, GFP_KERNEL);
720 priv->chan_rx = chan;
723 static void pch_dma_rx_complete(void *arg)
725 struct eg20t_port *priv = arg;
726 struct uart_port *port = &priv->port;
729 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
730 count = dma_push_rx(priv, priv->trigger_level);
732 tty_flip_buffer_push(&port->state->port);
733 async_tx_ack(priv->desc_rx);
734 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
735 PCH_UART_HAL_RX_ERR_INT);
738 static void pch_dma_tx_complete(void *arg)
740 struct eg20t_port *priv = arg;
741 struct uart_port *port = &priv->port;
742 struct circ_buf *xmit = &port->state->xmit;
743 struct scatterlist *sg = priv->sg_tx_p;
746 for (i = 0; i < priv->nent; i++, sg++) {
747 xmit->tail += sg_dma_len(sg);
748 port->icount.tx += sg_dma_len(sg);
750 xmit->tail &= UART_XMIT_SIZE - 1;
751 async_tx_ack(priv->desc_tx);
752 dma_unmap_sg(port->dev, sg, priv->orig_nent, DMA_TO_DEVICE);
753 priv->tx_dma_use = 0;
756 kfree(priv->sg_tx_p);
757 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
760 static int handle_rx_to(struct eg20t_port *priv)
762 struct pch_uart_buffer *buf;
765 if (!priv->start_rx) {
766 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
767 PCH_UART_HAL_RX_ERR_INT);
772 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
773 ret = push_rx(priv, buf->buf, rx_size);
776 } while (rx_size == buf->size);
778 return PCH_UART_HANDLED_RX_INT;
781 static int handle_rx(struct eg20t_port *priv)
783 return handle_rx_to(priv);
786 static int dma_handle_rx(struct eg20t_port *priv)
788 struct uart_port *port = &priv->port;
789 struct dma_async_tx_descriptor *desc;
790 struct scatterlist *sg;
792 priv = container_of(port, struct eg20t_port, port);
795 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
797 sg_dma_len(sg) = priv->trigger_level;
799 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
800 sg_dma_len(sg), offset_in_page(priv->rx_buf_virt));
802 sg_dma_address(sg) = priv->rx_buf_dma;
804 desc = dmaengine_prep_slave_sg(priv->chan_rx,
805 sg, 1, DMA_DEV_TO_MEM,
806 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
811 priv->desc_rx = desc;
812 desc->callback = pch_dma_rx_complete;
813 desc->callback_param = priv;
814 desc->tx_submit(desc);
815 dma_async_issue_pending(priv->chan_rx);
817 return PCH_UART_HANDLED_RX_INT;
820 static unsigned int handle_tx(struct eg20t_port *priv)
822 struct uart_port *port = &priv->port;
823 struct circ_buf *xmit = &port->state->xmit;
827 if (!priv->start_tx) {
828 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
830 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
835 fifo_size = max(priv->fifo_size, 1);
838 iowrite8(port->x_char, priv->membase + PCH_UART_THR);
845 while (!uart_tx_stopped(port) && !uart_circ_empty(xmit) && fifo_size) {
846 iowrite8(xmit->buf[xmit->tail], priv->membase + PCH_UART_THR);
847 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
853 priv->tx_empty = tx_empty;
856 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
857 uart_write_wakeup(port);
860 return PCH_UART_HANDLED_TX_INT;
863 static unsigned int dma_handle_tx(struct eg20t_port *priv)
865 struct uart_port *port = &priv->port;
866 struct circ_buf *xmit = &port->state->xmit;
867 struct scatterlist *sg;
870 struct dma_async_tx_descriptor *desc;
877 if (!priv->start_tx) {
878 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
880 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
885 if (priv->tx_dma_use) {
886 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
888 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
893 fifo_size = max(priv->fifo_size, 1);
896 iowrite8(port->x_char, priv->membase + PCH_UART_THR);
902 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
903 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
904 xmit->tail, UART_XMIT_SIZE));
906 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
907 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
908 uart_write_wakeup(port);
912 if (bytes > fifo_size) {
913 num = bytes / fifo_size + 1;
915 rem = bytes % fifo_size;
922 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
923 __func__, num, size, rem);
925 priv->tx_dma_use = 1;
927 priv->sg_tx_p = kmalloc_array(num, sizeof(struct scatterlist), GFP_ATOMIC);
928 if (!priv->sg_tx_p) {
929 dev_err(priv->port.dev, "%s:kzalloc Failed\n", __func__);
933 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
936 for (i = 0; i < num; i++, sg++) {
938 sg_set_page(sg, virt_to_page(xmit->buf),
941 sg_set_page(sg, virt_to_page(xmit->buf),
942 size, fifo_size * i);
946 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
948 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
951 priv->orig_nent = num;
954 for (i = 0; i < nent; i++, sg++) {
955 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
957 sg_dma_address(sg) = (sg_dma_address(sg) &
958 ~(UART_XMIT_SIZE - 1)) + sg->offset;
960 sg_dma_len(sg) = rem;
962 sg_dma_len(sg) = size;
965 desc = dmaengine_prep_slave_sg(priv->chan_tx,
966 priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
967 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
969 dev_err(priv->port.dev, "%s:dmaengine_prep_slave_sg Failed\n",
973 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
974 priv->desc_tx = desc;
975 desc->callback = pch_dma_tx_complete;
976 desc->callback_param = priv;
978 desc->tx_submit(desc);
980 dma_async_issue_pending(priv->chan_tx);
982 return PCH_UART_HANDLED_TX_INT;
985 static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
987 struct uart_port *port = &priv->port;
988 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
989 char *error_msg[5] = {};
992 if (lsr & PCH_UART_LSR_ERR)
993 error_msg[i++] = "Error data in FIFO\n";
995 if (lsr & UART_LSR_FE) {
996 port->icount.frame++;
997 error_msg[i++] = " Framing Error\n";
1000 if (lsr & UART_LSR_PE) {
1001 port->icount.parity++;
1002 error_msg[i++] = " Parity Error\n";
1005 if (lsr & UART_LSR_OE) {
1006 port->icount.overrun++;
1007 error_msg[i++] = " Overrun Error\n";
1011 for (i = 0; error_msg[i] != NULL; i++)
1012 dev_err(&priv->pdev->dev, error_msg[i]);
1018 static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1020 struct eg20t_port *priv = dev_id;
1021 unsigned int handled;
1025 unsigned long flags;
1029 spin_lock_irqsave(&priv->lock, flags);
1032 iid = pch_uart_hal_get_iid(priv);
1033 if (iid & PCH_UART_IIR_IP) /* No Interrupt */
1036 case PCH_UART_IID_RLS: /* Receiver Line Status */
1037 lsr = pch_uart_hal_get_line_status(priv);
1038 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1039 UART_LSR_PE | UART_LSR_OE)) {
1040 pch_uart_err_ir(priv, lsr);
1041 ret = PCH_UART_HANDLED_RX_ERR_INT;
1043 ret = PCH_UART_HANDLED_LS_INT;
1046 case PCH_UART_IID_RDR: /* Received Data Ready */
1047 if (priv->use_dma) {
1048 pch_uart_hal_disable_interrupt(priv,
1049 PCH_UART_HAL_RX_INT |
1050 PCH_UART_HAL_RX_ERR_INT);
1051 ret = dma_handle_rx(priv);
1053 pch_uart_hal_enable_interrupt(priv,
1054 PCH_UART_HAL_RX_INT |
1055 PCH_UART_HAL_RX_ERR_INT);
1057 ret = handle_rx(priv);
1060 case PCH_UART_IID_RDR_TO: /* Received Data Ready
1062 ret = handle_rx_to(priv);
1064 case PCH_UART_IID_THRE: /* Transmitter Holding Register
1067 ret = dma_handle_tx(priv);
1069 ret = handle_tx(priv);
1071 case PCH_UART_IID_MS: /* Modem Status */
1072 msr = pch_uart_hal_get_modem(priv);
1073 next = 0; /* MS ir prioirty is the lowest. So, MS ir
1074 means final interrupt */
1075 if ((msr & UART_MSR_ANY_DELTA) == 0)
1077 ret |= PCH_UART_HANDLED_MS_INT;
1079 default: /* Never junp to this label */
1080 dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
1086 handled |= (unsigned int)ret;
1089 spin_unlock_irqrestore(&priv->lock, flags);
1090 return IRQ_RETVAL(handled);
1093 /* This function tests whether the transmitter fifo and shifter for the port
1094 described by 'port' is empty. */
1095 static unsigned int pch_uart_tx_empty(struct uart_port *port)
1097 struct eg20t_port *priv;
1099 priv = container_of(port, struct eg20t_port, port);
1101 return TIOCSER_TEMT;
1106 /* Returns the current state of modem control inputs. */
1107 static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1109 struct eg20t_port *priv;
1111 unsigned int ret = 0;
1113 priv = container_of(port, struct eg20t_port, port);
1114 modem = pch_uart_hal_get_modem(priv);
1116 if (modem & UART_MSR_DCD)
1119 if (modem & UART_MSR_RI)
1122 if (modem & UART_MSR_DSR)
1125 if (modem & UART_MSR_CTS)
1131 static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1134 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1136 if (mctrl & TIOCM_DTR)
1137 mcr |= UART_MCR_DTR;
1138 if (mctrl & TIOCM_RTS)
1139 mcr |= UART_MCR_RTS;
1140 if (mctrl & TIOCM_LOOP)
1141 mcr |= UART_MCR_LOOP;
1143 if (priv->mcr & UART_MCR_AFE)
1144 mcr |= UART_MCR_AFE;
1147 iowrite8(mcr, priv->membase + UART_MCR);
1150 static void pch_uart_stop_tx(struct uart_port *port)
1152 struct eg20t_port *priv;
1153 priv = container_of(port, struct eg20t_port, port);
1155 priv->tx_dma_use = 0;
1158 static void pch_uart_start_tx(struct uart_port *port)
1160 struct eg20t_port *priv;
1162 priv = container_of(port, struct eg20t_port, port);
1164 if (priv->use_dma) {
1165 if (priv->tx_dma_use) {
1166 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1173 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1176 static void pch_uart_stop_rx(struct uart_port *port)
1178 struct eg20t_port *priv;
1179 priv = container_of(port, struct eg20t_port, port);
1181 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
1182 PCH_UART_HAL_RX_ERR_INT);
1185 /* Enable the modem status interrupts. */
1186 static void pch_uart_enable_ms(struct uart_port *port)
1188 struct eg20t_port *priv;
1189 priv = container_of(port, struct eg20t_port, port);
1190 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1193 /* Control the transmission of a break signal. */
1194 static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1196 struct eg20t_port *priv;
1197 unsigned long flags;
1199 priv = container_of(port, struct eg20t_port, port);
1200 spin_lock_irqsave(&priv->lock, flags);
1201 pch_uart_hal_set_break(priv, ctl);
1202 spin_unlock_irqrestore(&priv->lock, flags);
1205 /* Grab any interrupt resources and initialise any low level driver state. */
1206 static int pch_uart_startup(struct uart_port *port)
1208 struct eg20t_port *priv;
1213 priv = container_of(port, struct eg20t_port, port);
1217 priv->uartclk = port->uartclk;
1219 port->uartclk = priv->uartclk;
1221 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1222 ret = pch_uart_hal_set_line(priv, default_baud,
1223 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1228 switch (priv->fifo_size) {
1230 fifo_size = PCH_UART_HAL_FIFO256;
1233 fifo_size = PCH_UART_HAL_FIFO64;
1236 fifo_size = PCH_UART_HAL_FIFO16;
1240 fifo_size = PCH_UART_HAL_FIFO_DIS;
1244 switch (priv->trigger) {
1245 case PCH_UART_HAL_TRIGGER1:
1248 case PCH_UART_HAL_TRIGGER_L:
1249 trigger_level = priv->fifo_size / 4;
1251 case PCH_UART_HAL_TRIGGER_M:
1252 trigger_level = priv->fifo_size / 2;
1254 case PCH_UART_HAL_TRIGGER_H:
1256 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1260 priv->trigger_level = trigger_level;
1261 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1262 fifo_size, priv->trigger);
1266 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1267 priv->irq_name, priv);
1272 pch_request_dma(port);
1275 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
1276 PCH_UART_HAL_RX_ERR_INT);
1277 uart_update_timeout(port, CS8, default_baud);
1282 static void pch_uart_shutdown(struct uart_port *port)
1284 struct eg20t_port *priv;
1287 priv = container_of(port, struct eg20t_port, port);
1288 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1289 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1290 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1291 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1293 dev_err(priv->port.dev,
1294 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1298 free_irq(priv->port.irq, priv);
1301 /* Change the port parameters, including word length, parity, stop
1302 *bits. Update read_status_mask and ignore_status_mask to indicate
1303 *the types of events we are interested in receiving. */
1304 static void pch_uart_set_termios(struct uart_port *port,
1305 struct ktermios *termios, struct ktermios *old)
1308 unsigned int baud, parity, bits, stb;
1309 struct eg20t_port *priv;
1310 unsigned long flags;
1312 priv = container_of(port, struct eg20t_port, port);
1313 switch (termios->c_cflag & CSIZE) {
1315 bits = PCH_UART_HAL_5BIT;
1318 bits = PCH_UART_HAL_6BIT;
1321 bits = PCH_UART_HAL_7BIT;
1324 bits = PCH_UART_HAL_8BIT;
1327 if (termios->c_cflag & CSTOPB)
1328 stb = PCH_UART_HAL_STB2;
1330 stb = PCH_UART_HAL_STB1;
1332 if (termios->c_cflag & PARENB) {
1333 if (termios->c_cflag & PARODD)
1334 parity = PCH_UART_HAL_PARITY_ODD;
1336 parity = PCH_UART_HAL_PARITY_EVEN;
1339 parity = PCH_UART_HAL_PARITY_NONE;
1341 /* Only UART0 has auto hardware flow function */
1342 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1343 priv->mcr |= UART_MCR_AFE;
1345 priv->mcr &= ~UART_MCR_AFE;
1347 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1349 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1351 spin_lock_irqsave(&priv->lock, flags);
1352 spin_lock(&port->lock);
1354 uart_update_timeout(port, termios->c_cflag, baud);
1355 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1359 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
1360 /* Don't rewrite B0 */
1361 if (tty_termios_baud_rate(termios))
1362 tty_termios_encode_baud_rate(termios, baud, baud);
1365 spin_unlock(&port->lock);
1366 spin_unlock_irqrestore(&priv->lock, flags);
1369 static const char *pch_uart_type(struct uart_port *port)
1371 return KBUILD_MODNAME;
1374 static void pch_uart_release_port(struct uart_port *port)
1376 struct eg20t_port *priv;
1378 priv = container_of(port, struct eg20t_port, port);
1379 pci_iounmap(priv->pdev, priv->membase);
1380 pci_release_regions(priv->pdev);
1383 static int pch_uart_request_port(struct uart_port *port)
1385 struct eg20t_port *priv;
1387 void __iomem *membase;
1389 priv = container_of(port, struct eg20t_port, port);
1390 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1394 membase = pci_iomap(priv->pdev, 1, 0);
1396 pci_release_regions(priv->pdev);
1399 priv->membase = port->membase = membase;
1404 static void pch_uart_config_port(struct uart_port *port, int type)
1406 struct eg20t_port *priv;
1408 priv = container_of(port, struct eg20t_port, port);
1409 if (type & UART_CONFIG_TYPE) {
1410 port->type = priv->port_type;
1411 pch_uart_request_port(port);
1415 static int pch_uart_verify_port(struct uart_port *port,
1416 struct serial_struct *serinfo)
1418 struct eg20t_port *priv;
1420 priv = container_of(port, struct eg20t_port, port);
1421 if (serinfo->flags & UPF_LOW_LATENCY) {
1422 dev_info(priv->port.dev,
1423 "PCH UART : Use PIO Mode (without DMA)\n");
1425 serinfo->flags &= ~UPF_LOW_LATENCY;
1427 #ifndef CONFIG_PCH_DMA
1428 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1432 if (!priv->use_dma) {
1433 pch_request_dma(port);
1437 dev_info(priv->port.dev, "PCH UART: %s\n",
1439 "Use DMA Mode" : "No DMA");
1445 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_PCH_UART_CONSOLE)
1447 * Wait for transmitter & holding register to empty
1449 static void wait_for_xmitr(struct eg20t_port *up, int bits)
1451 unsigned int status, tmout = 10000;
1453 /* Wait up to 10ms for the character(s) to be sent. */
1455 status = ioread8(up->membase + UART_LSR);
1457 if ((status & bits) == bits)
1464 /* Wait up to 1s for flow control if necessary */
1465 if (up->port.flags & UPF_CONS_FLOW) {
1467 for (tmout = 1000000; tmout; tmout--) {
1468 unsigned int msr = ioread8(up->membase + UART_MSR);
1469 if (msr & UART_MSR_CTS)
1472 touch_nmi_watchdog();
1476 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_PCH_UART_CONSOLE */
1478 #ifdef CONFIG_CONSOLE_POLL
1480 * Console polling routines for communicate via uart while
1481 * in an interrupt or debug context.
1483 static int pch_uart_get_poll_char(struct uart_port *port)
1485 struct eg20t_port *priv =
1486 container_of(port, struct eg20t_port, port);
1487 u8 lsr = ioread8(priv->membase + UART_LSR);
1489 if (!(lsr & UART_LSR_DR))
1490 return NO_POLL_CHAR;
1492 return ioread8(priv->membase + PCH_UART_RBR);
1496 static void pch_uart_put_poll_char(struct uart_port *port,
1500 struct eg20t_port *priv =
1501 container_of(port, struct eg20t_port, port);
1504 * First save the IER then disable the interrupts
1506 ier = ioread8(priv->membase + UART_IER);
1507 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1509 wait_for_xmitr(priv, UART_LSR_THRE);
1511 * Send the character out.
1513 iowrite8(c, priv->membase + PCH_UART_THR);
1516 * Finally, wait for transmitter to become empty
1517 * and restore the IER
1519 wait_for_xmitr(priv, BOTH_EMPTY);
1520 iowrite8(ier, priv->membase + UART_IER);
1522 #endif /* CONFIG_CONSOLE_POLL */
1524 static const struct uart_ops pch_uart_ops = {
1525 .tx_empty = pch_uart_tx_empty,
1526 .set_mctrl = pch_uart_set_mctrl,
1527 .get_mctrl = pch_uart_get_mctrl,
1528 .stop_tx = pch_uart_stop_tx,
1529 .start_tx = pch_uart_start_tx,
1530 .stop_rx = pch_uart_stop_rx,
1531 .enable_ms = pch_uart_enable_ms,
1532 .break_ctl = pch_uart_break_ctl,
1533 .startup = pch_uart_startup,
1534 .shutdown = pch_uart_shutdown,
1535 .set_termios = pch_uart_set_termios,
1536 /* .pm = pch_uart_pm, Not supported yet */
1537 .type = pch_uart_type,
1538 .release_port = pch_uart_release_port,
1539 .request_port = pch_uart_request_port,
1540 .config_port = pch_uart_config_port,
1541 .verify_port = pch_uart_verify_port,
1542 #ifdef CONFIG_CONSOLE_POLL
1543 .poll_get_char = pch_uart_get_poll_char,
1544 .poll_put_char = pch_uart_put_poll_char,
1548 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1550 static void pch_console_putchar(struct uart_port *port, unsigned char ch)
1552 struct eg20t_port *priv =
1553 container_of(port, struct eg20t_port, port);
1555 wait_for_xmitr(priv, UART_LSR_THRE);
1556 iowrite8(ch, priv->membase + PCH_UART_THR);
1560 * Print a string to the serial port trying not to disturb
1561 * any possible real use of the port...
1563 * The console_lock must be held when we get here.
1566 pch_console_write(struct console *co, const char *s, unsigned int count)
1568 struct eg20t_port *priv;
1569 unsigned long flags;
1570 int priv_locked = 1;
1571 int port_locked = 1;
1574 priv = pch_uart_ports[co->index];
1576 touch_nmi_watchdog();
1578 local_irq_save(flags);
1579 if (priv->port.sysrq) {
1580 /* call to uart_handle_sysrq_char already took the priv lock */
1582 /* serial8250_handle_port() already took the port lock */
1584 } else if (oops_in_progress) {
1585 priv_locked = spin_trylock(&priv->lock);
1586 port_locked = spin_trylock(&priv->port.lock);
1588 spin_lock(&priv->lock);
1589 spin_lock(&priv->port.lock);
1593 * First save the IER then disable the interrupts
1595 ier = ioread8(priv->membase + UART_IER);
1597 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1599 uart_console_write(&priv->port, s, count, pch_console_putchar);
1602 * Finally, wait for transmitter to become empty
1603 * and restore the IER
1605 wait_for_xmitr(priv, BOTH_EMPTY);
1606 iowrite8(ier, priv->membase + UART_IER);
1609 spin_unlock(&priv->port.lock);
1611 spin_unlock(&priv->lock);
1612 local_irq_restore(flags);
1615 static int __init pch_console_setup(struct console *co, char *options)
1617 struct uart_port *port;
1618 int baud = default_baud;
1624 * Check whether an invalid uart number has been specified, and
1625 * if so, search for the first available port that does have
1628 if (co->index >= PCH_UART_NR)
1630 port = &pch_uart_ports[co->index]->port;
1632 if (!port || (!port->iobase && !port->membase))
1635 port->uartclk = pch_uart_get_uartclk();
1638 uart_parse_options(options, &baud, &parity, &bits, &flow);
1640 return uart_set_options(port, co, baud, parity, bits, flow);
1643 static struct uart_driver pch_uart_driver;
1645 static struct console pch_console = {
1646 .name = PCH_UART_DRIVER_DEVICE,
1647 .write = pch_console_write,
1648 .device = uart_console_device,
1649 .setup = pch_console_setup,
1650 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1652 .data = &pch_uart_driver,
1655 #define PCH_CONSOLE (&pch_console)
1657 #define PCH_CONSOLE NULL
1658 #endif /* CONFIG_SERIAL_PCH_UART_CONSOLE */
1660 static struct uart_driver pch_uart_driver = {
1661 .owner = THIS_MODULE,
1662 .driver_name = KBUILD_MODNAME,
1663 .dev_name = PCH_UART_DRIVER_DEVICE,
1667 .cons = PCH_CONSOLE,
1670 static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
1671 const struct pci_device_id *id)
1673 struct eg20t_port *priv;
1675 unsigned int iobase;
1676 unsigned int mapbase;
1677 unsigned char *rxbuf;
1680 struct pch_uart_driver_data *board;
1683 board = &drv_dat[id->driver_data];
1684 port_type = board->port_type;
1686 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1688 goto init_port_alloc_err;
1690 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1692 goto init_port_free_txbuf;
1694 switch (port_type) {
1695 case PORT_PCH_8LINE:
1696 fifosize = 256; /* EG20T/ML7213: UART0 */
1698 case PORT_PCH_2LINE:
1699 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
1702 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1703 goto init_port_hal_free;
1706 pci_enable_msi(pdev);
1707 pci_set_master(pdev);
1709 spin_lock_init(&priv->lock);
1711 iobase = pci_resource_start(pdev, 0);
1712 mapbase = pci_resource_start(pdev, 1);
1713 priv->mapbase = mapbase;
1714 priv->iobase = iobase;
1717 priv->rxbuf.buf = rxbuf;
1718 priv->rxbuf.size = PAGE_SIZE;
1720 priv->fifo_size = fifosize;
1721 priv->uartclk = pch_uart_get_uartclk();
1722 priv->port_type = port_type;
1723 priv->port.dev = &pdev->dev;
1724 priv->port.iobase = iobase;
1725 priv->port.membase = NULL;
1726 priv->port.mapbase = mapbase;
1727 priv->port.irq = pdev->irq;
1728 priv->port.iotype = UPIO_PORT;
1729 priv->port.ops = &pch_uart_ops;
1730 priv->port.flags = UPF_BOOT_AUTOCONF;
1731 priv->port.fifosize = fifosize;
1732 priv->port.line = board->line_no;
1733 priv->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_PCH_UART_CONSOLE);
1734 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1736 snprintf(priv->irq_name, IRQ_NAME_SIZE,
1737 KBUILD_MODNAME ":" PCH_UART_DRIVER_DEVICE "%d",
1740 spin_lock_init(&priv->port.lock);
1742 pci_set_drvdata(pdev, priv);
1743 priv->trigger_level = 1;
1746 if (pdev->dev.of_node)
1747 of_property_read_u32(pdev->dev.of_node, "clock-frequency"
1750 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1751 pch_uart_ports[board->line_no] = priv;
1753 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1755 goto init_port_hal_free;
1757 snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
1758 debugfs_create_file(name, S_IFREG | S_IRUGO, NULL, priv,
1764 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1765 pch_uart_ports[board->line_no] = NULL;
1767 free_page((unsigned long)rxbuf);
1768 init_port_free_txbuf:
1770 init_port_alloc_err:
1775 static void pch_uart_exit_port(struct eg20t_port *priv)
1779 snprintf(name, sizeof(name), "uart%d_regs", priv->port.line);
1780 debugfs_remove(debugfs_lookup(name, NULL));
1781 uart_remove_one_port(&pch_uart_driver, &priv->port);
1782 free_page((unsigned long)priv->rxbuf.buf);
1785 static void pch_uart_pci_remove(struct pci_dev *pdev)
1787 struct eg20t_port *priv = pci_get_drvdata(pdev);
1789 pci_disable_msi(pdev);
1791 #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1792 pch_uart_ports[priv->port.line] = NULL;
1794 pch_uart_exit_port(priv);
1795 pci_disable_device(pdev);
1800 static int __maybe_unused pch_uart_pci_suspend(struct device *dev)
1802 struct eg20t_port *priv = dev_get_drvdata(dev);
1804 uart_suspend_port(&pch_uart_driver, &priv->port);
1809 static int __maybe_unused pch_uart_pci_resume(struct device *dev)
1811 struct eg20t_port *priv = dev_get_drvdata(dev);
1813 uart_resume_port(&pch_uart_driver, &priv->port);
1818 static const struct pci_device_id pch_uart_pci_id[] = {
1819 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1820 .driver_data = pch_et20t_uart0},
1821 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1822 .driver_data = pch_et20t_uart1},
1823 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1824 .driver_data = pch_et20t_uart2},
1825 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1826 .driver_data = pch_et20t_uart3},
1827 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1828 .driver_data = pch_ml7213_uart0},
1829 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1830 .driver_data = pch_ml7213_uart1},
1831 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1832 .driver_data = pch_ml7213_uart2},
1833 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1834 .driver_data = pch_ml7223_uart0},
1835 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1836 .driver_data = pch_ml7223_uart1},
1837 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1838 .driver_data = pch_ml7831_uart0},
1839 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1840 .driver_data = pch_ml7831_uart1},
1844 static int pch_uart_pci_probe(struct pci_dev *pdev,
1845 const struct pci_device_id *id)
1848 struct eg20t_port *priv;
1850 ret = pci_enable_device(pdev);
1854 priv = pch_uart_init_port(pdev, id);
1857 goto probe_disable_device;
1859 pci_set_drvdata(pdev, priv);
1863 probe_disable_device:
1864 pci_disable_msi(pdev);
1865 pci_disable_device(pdev);
1870 static SIMPLE_DEV_PM_OPS(pch_uart_pci_pm_ops,
1871 pch_uart_pci_suspend,
1872 pch_uart_pci_resume);
1874 static struct pci_driver pch_uart_pci_driver = {
1876 .id_table = pch_uart_pci_id,
1877 .probe = pch_uart_pci_probe,
1878 .remove = pch_uart_pci_remove,
1879 .driver.pm = &pch_uart_pci_pm_ops,
1882 static int __init pch_uart_module_init(void)
1886 /* register as UART driver */
1887 ret = uart_register_driver(&pch_uart_driver);
1891 /* register as PCI driver */
1892 ret = pci_register_driver(&pch_uart_pci_driver);
1894 uart_unregister_driver(&pch_uart_driver);
1898 module_init(pch_uart_module_init);
1900 static void __exit pch_uart_module_exit(void)
1902 pci_unregister_driver(&pch_uart_pci_driver);
1903 uart_unregister_driver(&pch_uart_driver);
1905 module_exit(pch_uart_module_exit);
1907 MODULE_LICENSE("GPL v2");
1908 MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1909 MODULE_DEVICE_TABLE(pci, pch_uart_pci_id);
1911 module_param(default_baud, uint, S_IRUGO);
1912 MODULE_PARM_DESC(default_baud,
1913 "Default BAUD for initial driver state and console (default 9600)");
1914 module_param(user_uartclk, uint, S_IRUGO);
1915 MODULE_PARM_DESC(user_uartclk,
1916 "Override UART default or board specific UART clock");