2 *Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
4 *This program is free software; you can redistribute it and/or modify
5 *it under the terms of the GNU General Public License as published by
6 *the Free Software Foundation; version 2 of the License.
8 *This program is distributed in the hope that it will be useful,
9 *but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 *GNU General Public License for more details.
13 *You should have received a copy of the GNU General Public License
14 *along with this program; if not, write to the Free Software
15 *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 #include <linux/serial_reg.h>
18 #include <linux/pci.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/serial_core.h>
22 #include <linux/interrupt.h>
25 #include <linux/dmaengine.h>
26 #include <linux/pch_dma.h>
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,
41 #define PCH_UART_DRIVER_DEVICE "ttyPCH"
43 /* Set the max number of UART port
44 * Intel EG20T PCH: 4 port
45 * OKI SEMICONDUCTOR ML7213 IOH: 3 port
49 #define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
50 #define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
51 #define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
52 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
53 #define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
54 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
55 #define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
57 #define PCH_UART_RBR 0x00
58 #define PCH_UART_THR 0x00
60 #define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
61 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
62 #define PCH_UART_IER_ERBFI 0x00000001
63 #define PCH_UART_IER_ETBEI 0x00000002
64 #define PCH_UART_IER_ELSI 0x00000004
65 #define PCH_UART_IER_EDSSI 0x00000008
67 #define PCH_UART_IIR_IP 0x00000001
68 #define PCH_UART_IIR_IID 0x00000006
69 #define PCH_UART_IIR_MSI 0x00000000
70 #define PCH_UART_IIR_TRI 0x00000002
71 #define PCH_UART_IIR_RRI 0x00000004
72 #define PCH_UART_IIR_REI 0x00000006
73 #define PCH_UART_IIR_TOI 0x00000008
74 #define PCH_UART_IIR_FIFO256 0x00000020
75 #define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
76 #define PCH_UART_IIR_FE 0x000000C0
78 #define PCH_UART_FCR_FIFOE 0x00000001
79 #define PCH_UART_FCR_RFR 0x00000002
80 #define PCH_UART_FCR_TFR 0x00000004
81 #define PCH_UART_FCR_DMS 0x00000008
82 #define PCH_UART_FCR_FIFO256 0x00000020
83 #define PCH_UART_FCR_RFTL 0x000000C0
85 #define PCH_UART_FCR_RFTL1 0x00000000
86 #define PCH_UART_FCR_RFTL64 0x00000040
87 #define PCH_UART_FCR_RFTL128 0x00000080
88 #define PCH_UART_FCR_RFTL224 0x000000C0
89 #define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
90 #define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
91 #define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
92 #define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
93 #define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
94 #define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
95 #define PCH_UART_FCR_RFTL_SHIFT 6
97 #define PCH_UART_LCR_WLS 0x00000003
98 #define PCH_UART_LCR_STB 0x00000004
99 #define PCH_UART_LCR_PEN 0x00000008
100 #define PCH_UART_LCR_EPS 0x00000010
101 #define PCH_UART_LCR_SP 0x00000020
102 #define PCH_UART_LCR_SB 0x00000040
103 #define PCH_UART_LCR_DLAB 0x00000080
104 #define PCH_UART_LCR_NP 0x00000000
105 #define PCH_UART_LCR_OP PCH_UART_LCR_PEN
106 #define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
107 #define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
108 #define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
111 #define PCH_UART_LCR_5BIT 0x00000000
112 #define PCH_UART_LCR_6BIT 0x00000001
113 #define PCH_UART_LCR_7BIT 0x00000002
114 #define PCH_UART_LCR_8BIT 0x00000003
116 #define PCH_UART_MCR_DTR 0x00000001
117 #define PCH_UART_MCR_RTS 0x00000002
118 #define PCH_UART_MCR_OUT 0x0000000C
119 #define PCH_UART_MCR_LOOP 0x00000010
120 #define PCH_UART_MCR_AFE 0x00000020
122 #define PCH_UART_LSR_DR 0x00000001
123 #define PCH_UART_LSR_ERR (1<<7)
125 #define PCH_UART_MSR_DCTS 0x00000001
126 #define PCH_UART_MSR_DDSR 0x00000002
127 #define PCH_UART_MSR_TERI 0x00000004
128 #define PCH_UART_MSR_DDCD 0x00000008
129 #define PCH_UART_MSR_CTS 0x00000010
130 #define PCH_UART_MSR_DSR 0x00000020
131 #define PCH_UART_MSR_RI 0x00000040
132 #define PCH_UART_MSR_DCD 0x00000080
133 #define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
134 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
136 #define PCH_UART_DLL 0x00
137 #define PCH_UART_DLM 0x01
139 #define DIV_ROUND(a, b) (((a) + ((b)/2)) / (b))
141 #define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
142 #define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
143 #define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
144 #define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
145 #define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
147 #define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
148 #define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
149 #define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
150 #define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
151 #define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
152 #define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
153 #define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
154 #define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
155 #define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
156 #define PCH_UART_HAL_STB1 0
157 #define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
159 #define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
160 #define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
161 #define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
162 PCH_UART_HAL_CLR_RX_FIFO)
164 #define PCH_UART_HAL_DMA_MODE0 0
165 #define PCH_UART_HAL_FIFO_DIS 0
166 #define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
167 #define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
168 PCH_UART_FCR_FIFO256)
169 #define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
170 #define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
171 #define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
172 #define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
173 #define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
174 #define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
175 #define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
176 #define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
177 #define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
178 #define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
179 #define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
180 #define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
181 #define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
182 #define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
184 #define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
185 #define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
186 #define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
187 #define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
188 #define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
190 #define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
191 #define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
192 #define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
193 #define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
194 #define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
196 #define PCI_VENDOR_ID_ROHM 0x10DB
198 struct pch_uart_buffer {
204 struct uart_port port;
206 void __iomem *membase;
207 resource_size_t mapbase;
209 struct pci_dev *pdev;
218 struct pch_uart_buffer rxbuf;
221 unsigned int use_dma;
222 unsigned int use_dma_flag;
223 struct dma_async_tx_descriptor *desc_tx;
224 struct dma_async_tx_descriptor *desc_rx;
225 struct pch_dma_slave param_tx;
226 struct pch_dma_slave param_rx;
227 struct dma_chan *chan_tx;
228 struct dma_chan *chan_rx;
229 struct scatterlist *sg_tx_p;
231 struct scatterlist sg_rx;
234 dma_addr_t rx_buf_dma;
237 static unsigned int default_baud = 9600;
238 static const int trigger_level_256[4] = { 1, 64, 128, 224 };
239 static const int trigger_level_64[4] = { 1, 16, 32, 56 };
240 static const int trigger_level_16[4] = { 1, 4, 8, 14 };
241 static const int trigger_level_1[4] = { 1, 1, 1, 1 };
243 static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
246 struct eg20t_port *priv = pci_get_drvdata(pdev);
248 priv->trigger_level = 1;
252 static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
254 unsigned int msr = ioread8(base + UART_MSR);
255 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
260 static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
263 u8 ier = ioread8(priv->membase + UART_IER);
264 ier |= flag & PCH_UART_IER_MASK;
265 iowrite8(ier, priv->membase + UART_IER);
268 static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
271 u8 ier = ioread8(priv->membase + UART_IER);
272 ier &= ~(flag & PCH_UART_IER_MASK);
273 iowrite8(ier, priv->membase + UART_IER);
276 static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
277 unsigned int parity, unsigned int bits,
280 unsigned int dll, dlm, lcr;
283 div = DIV_ROUND(priv->base_baud / 16, baud);
284 if (div < 0 || USHRT_MAX <= div) {
285 pr_err("Invalid Baud(div=0x%x)\n", div);
289 dll = (unsigned int)div & 0x00FFU;
290 dlm = ((unsigned int)div >> 8) & 0x00FFU;
292 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
293 pr_err("Invalid parity(0x%x)\n", parity);
297 if (bits & ~PCH_UART_LCR_WLS) {
298 pr_err("Invalid bits(0x%x)\n", bits);
302 if (stb & ~PCH_UART_LCR_STB) {
303 pr_err("Invalid STB(0x%x)\n", stb);
311 pr_debug("%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
312 __func__, baud, div, lcr, jiffies);
313 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
314 iowrite8(dll, priv->membase + PCH_UART_DLL);
315 iowrite8(dlm, priv->membase + PCH_UART_DLM);
316 iowrite8(lcr, priv->membase + UART_LCR);
321 static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
324 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
325 pr_err("%s:Invalid flag(0x%x)\n", __func__, flag);
329 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
330 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
331 priv->membase + UART_FCR);
332 iowrite8(priv->fcr, priv->membase + UART_FCR);
337 static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
338 unsigned int dmamode,
339 unsigned int fifo_size, unsigned int trigger)
343 if (dmamode & ~PCH_UART_FCR_DMS) {
344 pr_err("%s:Invalid DMA Mode(0x%x)\n", __func__, dmamode);
348 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
349 pr_err("%s:Invalid FIFO SIZE(0x%x)\n", __func__, fifo_size);
353 if (trigger & ~PCH_UART_FCR_RFTL) {
354 pr_err("%s:Invalid TRIGGER(0x%x)\n", __func__, trigger);
358 switch (priv->fifo_size) {
360 priv->trigger_level =
361 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
364 priv->trigger_level =
365 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
368 priv->trigger_level =
369 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
372 priv->trigger_level =
373 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
377 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
378 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
379 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
380 priv->membase + UART_FCR);
381 iowrite8(fcr, priv->membase + UART_FCR);
387 static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
390 return get_msr(priv, priv->membase);
393 static int pch_uart_hal_write(struct eg20t_port *priv,
394 const unsigned char *buf, int tx_size)
399 for (i = 0; i < tx_size;) {
401 iowrite8(thr, priv->membase + PCH_UART_THR);
406 static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
412 lsr = ioread8(priv->membase + UART_LSR);
413 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
414 i < rx_size && lsr & UART_LSR_DR;
415 lsr = ioread8(priv->membase + UART_LSR)) {
416 rbr = ioread8(priv->membase + PCH_UART_RBR);
422 static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
427 iir = ioread8(priv->membase + UART_IIR);
428 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
432 static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
434 return ioread8(priv->membase + UART_LSR);
437 static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
441 lcr = ioread8(priv->membase + UART_LCR);
443 lcr |= PCH_UART_LCR_SB;
445 lcr &= ~PCH_UART_LCR_SB;
447 iowrite8(lcr, priv->membase + UART_LCR);
450 static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
453 struct uart_port *port;
454 struct tty_struct *tty;
457 tty = tty_port_tty_get(&port->state->port);
459 pr_debug("%s:tty is busy now", __func__);
463 tty_insert_flip_string(tty, buf, size);
464 tty_flip_buffer_push(tty);
470 static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
473 struct uart_port *port = &priv->port;
476 pr_debug("%s:X character send %02x (%lu)\n", __func__,
477 port->x_char, jiffies);
478 buf[0] = port->x_char;
488 static int dma_push_rx(struct eg20t_port *priv, int size)
490 struct tty_struct *tty;
492 struct uart_port *port = &priv->port;
495 tty = tty_port_tty_get(&port->state->port);
497 pr_debug("%s:tty is busy now", __func__);
501 room = tty_buffer_request_room(tty, size);
504 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
509 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
511 port->icount.rx += room;
517 static void pch_free_dma(struct uart_port *port)
519 struct eg20t_port *priv;
520 priv = container_of(port, struct eg20t_port, port);
523 dma_release_channel(priv->chan_tx);
524 priv->chan_tx = NULL;
527 dma_release_channel(priv->chan_rx);
528 priv->chan_rx = NULL;
530 if (sg_dma_address(&priv->sg_rx))
531 dma_free_coherent(port->dev, port->fifosize,
532 sg_virt(&priv->sg_rx),
533 sg_dma_address(&priv->sg_rx));
538 static bool filter(struct dma_chan *chan, void *slave)
540 struct pch_dma_slave *param = slave;
542 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
543 chan->device->dev)) {
544 chan->private = param;
551 static void pch_request_dma(struct uart_port *port)
554 struct dma_chan *chan;
555 struct pci_dev *dma_dev;
556 struct pch_dma_slave *param;
557 struct eg20t_port *priv =
558 container_of(port, struct eg20t_port, port);
560 dma_cap_set(DMA_SLAVE, mask);
562 dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
565 param = &priv->param_tx;
566 param->dma_dev = &dma_dev->dev;
567 param->chan_id = priv->port.line;
568 param->tx_reg = port->mapbase + UART_TX;
569 chan = dma_request_channel(mask, filter, param);
571 pr_err("%s:dma_request_channel FAILS(Tx)\n", __func__);
574 priv->chan_tx = chan;
577 param = &priv->param_rx;
578 param->dma_dev = &dma_dev->dev;
579 param->chan_id = priv->port.line + 1; /* Rx = Tx + 1 */
580 param->rx_reg = port->mapbase + UART_RX;
581 chan = dma_request_channel(mask, filter, param);
583 pr_err("%s:dma_request_channel FAILS(Rx)\n", __func__);
584 dma_release_channel(priv->chan_tx);
588 /* Get Consistent memory for DMA */
589 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
590 &priv->rx_buf_dma, GFP_KERNEL);
591 priv->chan_rx = chan;
594 static void pch_dma_rx_complete(void *arg)
596 struct eg20t_port *priv = arg;
597 struct uart_port *port = &priv->port;
598 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
602 pr_debug("%s:tty is busy now", __func__);
606 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
607 count = dma_push_rx(priv, priv->trigger_level);
609 tty_flip_buffer_push(tty);
611 async_tx_ack(priv->desc_rx);
612 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
615 static void pch_dma_tx_complete(void *arg)
617 struct eg20t_port *priv = arg;
618 struct uart_port *port = &priv->port;
619 struct circ_buf *xmit = &port->state->xmit;
620 struct scatterlist *sg = priv->sg_tx_p;
623 for (i = 0; i < priv->nent; i++, sg++) {
624 xmit->tail += sg_dma_len(sg);
625 port->icount.tx += sg_dma_len(sg);
627 xmit->tail &= UART_XMIT_SIZE - 1;
628 async_tx_ack(priv->desc_tx);
629 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
630 priv->tx_dma_use = 0;
632 kfree(priv->sg_tx_p);
633 if (uart_circ_chars_pending(xmit))
634 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
637 static int pop_tx(struct eg20t_port *priv, unsigned char *buf, int size)
640 struct uart_port *port = &priv->port;
641 struct circ_buf *xmit = &port->state->xmit;
643 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
648 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
649 int sz = min(size - count, cnt_to_end);
650 memcpy(&buf[count], &xmit->buf[xmit->tail], sz);
651 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
653 } while (!uart_circ_empty(xmit) && count < size);
656 pr_debug("%d characters. Remained %d characters. (%lu)\n",
657 count, size - count, jiffies);
662 static int handle_rx_to(struct eg20t_port *priv)
664 struct pch_uart_buffer *buf;
667 if (!priv->start_rx) {
668 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
673 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
674 ret = push_rx(priv, buf->buf, rx_size);
677 } while (rx_size == buf->size);
679 return PCH_UART_HANDLED_RX_INT;
682 static int handle_rx(struct eg20t_port *priv)
684 return handle_rx_to(priv);
687 static int dma_handle_rx(struct eg20t_port *priv)
689 struct uart_port *port = &priv->port;
690 struct dma_async_tx_descriptor *desc;
691 struct scatterlist *sg;
693 priv = container_of(port, struct eg20t_port, port);
696 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
698 sg_dma_len(sg) = priv->trigger_level;
700 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
701 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
704 sg_dma_address(sg) = priv->rx_buf_dma;
706 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
707 sg, 1, DMA_FROM_DEVICE,
708 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
713 priv->desc_rx = desc;
714 desc->callback = pch_dma_rx_complete;
715 desc->callback_param = priv;
716 desc->tx_submit(desc);
717 dma_async_issue_pending(priv->chan_rx);
719 return PCH_UART_HANDLED_RX_INT;
722 static unsigned int handle_tx(struct eg20t_port *priv)
724 struct uart_port *port = &priv->port;
725 struct circ_buf *xmit = &port->state->xmit;
732 if (!priv->start_tx) {
733 pr_info("%s:Tx isn't started. (%lu)\n", __func__, jiffies);
734 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
739 fifo_size = max(priv->fifo_size, 1);
741 if (pop_tx_x(priv, xmit->buf)) {
742 pch_uart_hal_write(priv, xmit->buf, 1);
747 size = min(xmit->head - xmit->tail, fifo_size);
751 tx_size = pop_tx(priv, xmit->buf, size);
753 ret = pch_uart_hal_write(priv, xmit->buf, tx_size);
754 port->icount.tx += ret;
758 priv->tx_empty = tx_empty;
761 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
762 uart_write_wakeup(port);
765 return PCH_UART_HANDLED_TX_INT;
768 static unsigned int dma_handle_tx(struct eg20t_port *priv)
770 struct uart_port *port = &priv->port;
771 struct circ_buf *xmit = &port->state->xmit;
772 struct scatterlist *sg;
776 struct dma_async_tx_descriptor *desc;
783 if (!priv->start_tx) {
784 pr_info("%s:Tx isn't started. (%lu)\n", __func__, jiffies);
785 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
790 fifo_size = max(priv->fifo_size, 1);
792 if (pop_tx_x(priv, xmit->buf)) {
793 pch_uart_hal_write(priv, xmit->buf, 1);
799 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
800 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
801 xmit->tail, UART_XMIT_SIZE));
803 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
804 uart_write_wakeup(port);
808 if (bytes > fifo_size) {
809 num = bytes / fifo_size + 1;
811 rem = bytes % fifo_size;
818 priv->tx_dma_use = 1;
820 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
822 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
825 for (i = 0; i < num; i++, sg++) {
827 sg_set_page(sg, virt_to_page(xmit->buf),
830 sg_set_page(sg, virt_to_page(xmit->buf),
831 size, fifo_size * i);
835 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
837 pr_err("%s:dma_map_sg Failed\n", __func__);
842 for (i = 0; i < nent; i++, sg++) {
843 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
845 sg_dma_address(sg) = (sg_dma_address(sg) &
846 ~(UART_XMIT_SIZE - 1)) + sg->offset;
848 sg_dma_len(sg) = rem;
850 sg_dma_len(sg) = size;
853 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
854 priv->sg_tx_p, nent, DMA_TO_DEVICE,
855 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
857 pr_err("%s:device_prep_slave_sg Failed\n", __func__);
860 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
861 priv->desc_tx = desc;
862 desc->callback = pch_dma_tx_complete;
863 desc->callback_param = priv;
865 desc->tx_submit(desc);
867 dma_async_issue_pending(priv->chan_tx);
869 return PCH_UART_HANDLED_TX_INT;
872 static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
874 u8 fcr = ioread8(priv->membase + UART_FCR);
877 fcr |= UART_FCR_CLEAR_RCVR;
878 iowrite8(fcr, priv->membase + UART_FCR);
880 if (lsr & PCH_UART_LSR_ERR)
881 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
883 if (lsr & UART_LSR_FE)
884 dev_err(&priv->pdev->dev, "Framing Error\n");
886 if (lsr & UART_LSR_PE)
887 dev_err(&priv->pdev->dev, "Parity Error\n");
889 if (lsr & UART_LSR_OE)
890 dev_err(&priv->pdev->dev, "Overrun Error\n");
893 static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
895 struct eg20t_port *priv = dev_id;
896 unsigned int handled;
902 spin_lock_irqsave(&priv->port.lock, flags);
904 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
906 case PCH_UART_IID_RLS: /* Receiver Line Status */
907 lsr = pch_uart_hal_get_line_status(priv);
908 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
909 UART_LSR_PE | UART_LSR_OE)) {
910 pch_uart_err_ir(priv, lsr);
911 ret = PCH_UART_HANDLED_RX_ERR_INT;
914 case PCH_UART_IID_RDR: /* Received Data Ready */
916 pch_uart_hal_disable_interrupt(priv,
917 PCH_UART_HAL_RX_INT);
918 ret = dma_handle_rx(priv);
920 pch_uart_hal_enable_interrupt(priv,
921 PCH_UART_HAL_RX_INT);
923 ret = handle_rx(priv);
926 case PCH_UART_IID_RDR_TO: /* Received Data Ready
928 ret = handle_rx_to(priv);
930 case PCH_UART_IID_THRE: /* Transmitter Holding Register
933 ret = dma_handle_tx(priv);
935 ret = handle_tx(priv);
937 case PCH_UART_IID_MS: /* Modem Status */
938 ret = PCH_UART_HANDLED_MS_INT;
940 default: /* Never junp to this label */
941 pr_err("%s:iid=%d (%lu)\n", __func__, iid, jiffies);
945 handled |= (unsigned int)ret;
947 if (handled == 0 && iid <= 1) {
948 if (priv->int_dis_flag)
949 priv->int_dis_flag = 0;
952 spin_unlock_irqrestore(&priv->port.lock, flags);
953 return IRQ_RETVAL(handled);
956 /* This function tests whether the transmitter fifo and shifter for the port
957 described by 'port' is empty. */
958 static unsigned int pch_uart_tx_empty(struct uart_port *port)
960 struct eg20t_port *priv;
962 priv = container_of(port, struct eg20t_port, port);
971 /* Returns the current state of modem control inputs. */
972 static unsigned int pch_uart_get_mctrl(struct uart_port *port)
974 struct eg20t_port *priv;
976 unsigned int ret = 0;
978 priv = container_of(port, struct eg20t_port, port);
979 modem = pch_uart_hal_get_modem(priv);
981 if (modem & UART_MSR_DCD)
984 if (modem & UART_MSR_RI)
987 if (modem & UART_MSR_DSR)
990 if (modem & UART_MSR_CTS)
996 static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1000 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1002 if (mctrl & TIOCM_DTR)
1003 mcr |= UART_MCR_DTR;
1004 if (mctrl & TIOCM_RTS)
1005 mcr |= UART_MCR_RTS;
1006 if (mctrl & TIOCM_LOOP)
1007 mcr |= UART_MCR_LOOP;
1010 dat = pch_uart_get_mctrl(port);
1012 iowrite8(dat, priv->membase + UART_MCR);
1016 static void pch_uart_stop_tx(struct uart_port *port)
1018 struct eg20t_port *priv;
1019 priv = container_of(port, struct eg20t_port, port);
1021 priv->tx_dma_use = 0;
1024 static void pch_uart_start_tx(struct uart_port *port)
1026 struct eg20t_port *priv;
1028 priv = container_of(port, struct eg20t_port, port);
1031 if (priv->tx_dma_use)
1035 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1038 static void pch_uart_stop_rx(struct uart_port *port)
1040 struct eg20t_port *priv;
1041 priv = container_of(port, struct eg20t_port, port);
1043 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1044 priv->int_dis_flag = 1;
1047 /* Enable the modem status interrupts. */
1048 static void pch_uart_enable_ms(struct uart_port *port)
1050 struct eg20t_port *priv;
1051 priv = container_of(port, struct eg20t_port, port);
1052 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1055 /* Control the transmission of a break signal. */
1056 static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1058 struct eg20t_port *priv;
1059 unsigned long flags;
1061 priv = container_of(port, struct eg20t_port, port);
1062 spin_lock_irqsave(&port->lock, flags);
1063 pch_uart_hal_set_break(priv, ctl);
1064 spin_unlock_irqrestore(&port->lock, flags);
1067 /* Grab any interrupt resources and initialise any low level driver state. */
1068 static int pch_uart_startup(struct uart_port *port)
1070 struct eg20t_port *priv;
1075 priv = container_of(port, struct eg20t_port, port);
1077 port->uartclk = priv->base_baud;
1078 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1079 ret = pch_uart_hal_set_line(priv, default_baud,
1080 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1085 switch (priv->fifo_size) {
1087 fifo_size = PCH_UART_HAL_FIFO256;
1090 fifo_size = PCH_UART_HAL_FIFO64;
1093 fifo_size = PCH_UART_HAL_FIFO16;
1096 fifo_size = PCH_UART_HAL_FIFO_DIS;
1100 switch (priv->trigger) {
1101 case PCH_UART_HAL_TRIGGER1:
1104 case PCH_UART_HAL_TRIGGER_L:
1105 trigger_level = priv->fifo_size / 4;
1107 case PCH_UART_HAL_TRIGGER_M:
1108 trigger_level = priv->fifo_size / 2;
1110 case PCH_UART_HAL_TRIGGER_H:
1112 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1116 priv->trigger_level = trigger_level;
1117 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1118 fifo_size, priv->trigger);
1122 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1123 KBUILD_MODNAME, priv);
1128 pch_request_dma(port);
1131 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1132 uart_update_timeout(port, CS8, default_baud);
1137 static void pch_uart_shutdown(struct uart_port *port)
1139 struct eg20t_port *priv;
1142 priv = container_of(port, struct eg20t_port, port);
1143 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1144 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1145 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1146 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1148 pr_err("pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1150 if (priv->use_dma_flag)
1153 free_irq(priv->port.irq, priv);
1156 /* Change the port parameters, including word length, parity, stop
1157 *bits. Update read_status_mask and ignore_status_mask to indicate
1158 *the types of events we are interested in receiving. */
1159 static void pch_uart_set_termios(struct uart_port *port,
1160 struct ktermios *termios, struct ktermios *old)
1164 unsigned int parity, bits, stb;
1165 struct eg20t_port *priv;
1166 unsigned long flags;
1168 priv = container_of(port, struct eg20t_port, port);
1169 switch (termios->c_cflag & CSIZE) {
1171 bits = PCH_UART_HAL_5BIT;
1174 bits = PCH_UART_HAL_6BIT;
1177 bits = PCH_UART_HAL_7BIT;
1180 bits = PCH_UART_HAL_8BIT;
1183 if (termios->c_cflag & CSTOPB)
1184 stb = PCH_UART_HAL_STB2;
1186 stb = PCH_UART_HAL_STB1;
1188 if (termios->c_cflag & PARENB) {
1189 if (!(termios->c_cflag & PARODD))
1190 parity = PCH_UART_HAL_PARITY_ODD;
1192 parity = PCH_UART_HAL_PARITY_EVEN;
1195 parity = PCH_UART_HAL_PARITY_NONE;
1197 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1199 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1201 spin_lock_irqsave(&port->lock, flags);
1203 uart_update_timeout(port, termios->c_cflag, baud);
1204 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1208 /* Don't rewrite B0 */
1209 if (tty_termios_baud_rate(termios))
1210 tty_termios_encode_baud_rate(termios, baud, baud);
1213 spin_unlock_irqrestore(&port->lock, flags);
1216 static const char *pch_uart_type(struct uart_port *port)
1218 return KBUILD_MODNAME;
1221 static void pch_uart_release_port(struct uart_port *port)
1223 struct eg20t_port *priv;
1225 priv = container_of(port, struct eg20t_port, port);
1226 pci_iounmap(priv->pdev, priv->membase);
1227 pci_release_regions(priv->pdev);
1230 static int pch_uart_request_port(struct uart_port *port)
1232 struct eg20t_port *priv;
1234 void __iomem *membase;
1236 priv = container_of(port, struct eg20t_port, port);
1237 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1241 membase = pci_iomap(priv->pdev, 1, 0);
1243 pci_release_regions(priv->pdev);
1246 priv->membase = port->membase = membase;
1251 static void pch_uart_config_port(struct uart_port *port, int type)
1253 struct eg20t_port *priv;
1255 priv = container_of(port, struct eg20t_port, port);
1256 if (type & UART_CONFIG_TYPE) {
1257 port->type = priv->port_type;
1258 pch_uart_request_port(port);
1262 static int pch_uart_verify_port(struct uart_port *port,
1263 struct serial_struct *serinfo)
1265 struct eg20t_port *priv;
1267 priv = container_of(port, struct eg20t_port, port);
1268 if (serinfo->flags & UPF_LOW_LATENCY) {
1269 pr_info("PCH UART : Use PIO Mode (without DMA)\n");
1271 serinfo->flags &= ~UPF_LOW_LATENCY;
1273 #ifndef CONFIG_PCH_DMA
1274 pr_err("%s : PCH DMA is not Loaded.\n", __func__);
1278 priv->use_dma_flag = 1;
1279 pr_info("PCH UART : Use DMA Mode\n");
1285 static struct uart_ops pch_uart_ops = {
1286 .tx_empty = pch_uart_tx_empty,
1287 .set_mctrl = pch_uart_set_mctrl,
1288 .get_mctrl = pch_uart_get_mctrl,
1289 .stop_tx = pch_uart_stop_tx,
1290 .start_tx = pch_uart_start_tx,
1291 .stop_rx = pch_uart_stop_rx,
1292 .enable_ms = pch_uart_enable_ms,
1293 .break_ctl = pch_uart_break_ctl,
1294 .startup = pch_uart_startup,
1295 .shutdown = pch_uart_shutdown,
1296 .set_termios = pch_uart_set_termios,
1297 /* .pm = pch_uart_pm, Not supported yet */
1298 /* .set_wake = pch_uart_set_wake, Not supported yet */
1299 .type = pch_uart_type,
1300 .release_port = pch_uart_release_port,
1301 .request_port = pch_uart_request_port,
1302 .config_port = pch_uart_config_port,
1303 .verify_port = pch_uart_verify_port
1306 static struct uart_driver pch_uart_driver = {
1307 .owner = THIS_MODULE,
1308 .driver_name = KBUILD_MODNAME,
1309 .dev_name = PCH_UART_DRIVER_DEVICE,
1315 static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
1316 const struct pci_device_id *id)
1318 struct eg20t_port *priv;
1320 unsigned int iobase;
1321 unsigned int mapbase;
1322 unsigned char *rxbuf;
1323 int fifosize, base_baud;
1325 int port_type = id->driver_data;
1327 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1329 goto init_port_alloc_err;
1331 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1333 goto init_port_free_txbuf;
1335 switch (port_type) {
1337 fifosize = 256; /* EG20T/ML7213: UART0 */
1338 base_baud = 1843200; /* 1.8432MHz */
1341 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
1342 base_baud = 1843200; /* 1.8432MHz */
1345 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1346 goto init_port_hal_free;
1349 iobase = pci_resource_start(pdev, 0);
1350 mapbase = pci_resource_start(pdev, 1);
1351 priv->mapbase = mapbase;
1352 priv->iobase = iobase;
1355 priv->rxbuf.buf = rxbuf;
1356 priv->rxbuf.size = PAGE_SIZE;
1358 priv->fifo_size = fifosize;
1359 priv->base_baud = base_baud;
1360 priv->port_type = PORT_MAX_8250 + port_type + 1;
1361 priv->port.dev = &pdev->dev;
1362 priv->port.iobase = iobase;
1363 priv->port.membase = NULL;
1364 priv->port.mapbase = mapbase;
1365 priv->port.irq = pdev->irq;
1366 priv->port.iotype = UPIO_PORT;
1367 priv->port.ops = &pch_uart_ops;
1368 priv->port.flags = UPF_BOOT_AUTOCONF;
1369 priv->port.fifosize = fifosize;
1370 priv->port.line = num++;
1371 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1373 spin_lock_init(&priv->port.lock);
1375 pci_set_drvdata(pdev, priv);
1376 pch_uart_hal_request(pdev, fifosize, base_baud);
1378 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1380 goto init_port_hal_free;
1385 free_page((unsigned long)rxbuf);
1386 init_port_free_txbuf:
1388 init_port_alloc_err:
1393 static void pch_uart_exit_port(struct eg20t_port *priv)
1395 uart_remove_one_port(&pch_uart_driver, &priv->port);
1396 pci_set_drvdata(priv->pdev, NULL);
1397 free_page((unsigned long)priv->rxbuf.buf);
1400 static void pch_uart_pci_remove(struct pci_dev *pdev)
1402 struct eg20t_port *priv;
1404 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
1405 pch_uart_exit_port(priv);
1406 pci_disable_device(pdev);
1411 static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1413 struct eg20t_port *priv = pci_get_drvdata(pdev);
1415 uart_suspend_port(&pch_uart_driver, &priv->port);
1417 pci_save_state(pdev);
1418 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1422 static int pch_uart_pci_resume(struct pci_dev *pdev)
1424 struct eg20t_port *priv = pci_get_drvdata(pdev);
1427 pci_set_power_state(pdev, PCI_D0);
1428 pci_restore_state(pdev);
1430 ret = pci_enable_device(pdev);
1433 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1437 uart_resume_port(&pch_uart_driver, &priv->port);
1442 #define pch_uart_pci_suspend NULL
1443 #define pch_uart_pci_resume NULL
1446 static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1447 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1448 .driver_data = PCH_UART_8LINE},
1449 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1450 .driver_data = PCH_UART_2LINE},
1451 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1452 .driver_data = PCH_UART_2LINE},
1453 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1454 .driver_data = PCH_UART_2LINE},
1455 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1456 .driver_data = PCH_UART_8LINE},
1457 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1458 .driver_data = PCH_UART_2LINE},
1459 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1460 .driver_data = PCH_UART_2LINE},
1464 static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1465 const struct pci_device_id *id)
1468 struct eg20t_port *priv;
1470 ret = pci_enable_device(pdev);
1474 priv = pch_uart_init_port(pdev, id);
1477 goto probe_disable_device;
1479 pci_set_drvdata(pdev, priv);
1483 probe_disable_device:
1484 pci_disable_device(pdev);
1489 static struct pci_driver pch_uart_pci_driver = {
1491 .id_table = pch_uart_pci_id,
1492 .probe = pch_uart_pci_probe,
1493 .remove = __devexit_p(pch_uart_pci_remove),
1494 .suspend = pch_uart_pci_suspend,
1495 .resume = pch_uart_pci_resume,
1498 static int __init pch_uart_module_init(void)
1502 /* register as UART driver */
1503 ret = uart_register_driver(&pch_uart_driver);
1507 /* register as PCI driver */
1508 ret = pci_register_driver(&pch_uart_pci_driver);
1510 uart_unregister_driver(&pch_uart_driver);
1514 module_init(pch_uart_module_init);
1516 static void __exit pch_uart_module_exit(void)
1518 pci_unregister_driver(&pch_uart_pci_driver);
1519 uart_unregister_driver(&pch_uart_driver);
1521 module_exit(pch_uart_module_exit);
1523 MODULE_LICENSE("GPL v2");
1524 MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1525 module_param(default_baud, uint, S_IRUGO);