blob: da0ba0fa2b9922716d0490b6acdae93760938282 [file] [log] [blame]
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001/*
2 *Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
3 *
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.
7 *
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.
12 *
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.
16 */
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>
23#include <linux/io.h>
24
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,
34};
35
36enum {
37 PCH_UART_8LINE,
38 PCH_UART_2LINE,
39};
40
41#define PCH_UART_DRIVER_DEVICE "ttyPCH"
42
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +090043/* Set the max number of UART port
44 * Intel EG20T PCH: 4 port
45 * OKI SEMICONDUCTOR ML7213 IOH: 3 port
46*/
47#define PCH_UART_NR 4
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090048
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))
56
57#define PCH_UART_RBR 0x00
58#define PCH_UART_THR 0x00
59
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
66
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
77
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
84
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
96
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 |\
109 PCH_UART_LCR_SP)
110
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
115
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
121
122#define PCH_UART_LSR_DR 0x00000001
123#define PCH_UART_LSR_ERR (1<<7)
124
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)
135
136#define PCH_UART_DLL 0x00
137#define PCH_UART_DLM 0x01
138
139#define DIV_ROUND(a, b) (((a) + ((b)/2)) / (b))
140
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)
146
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)
158
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)
163
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)
183
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)
189
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)
195
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +0900196#define PCI_VENDOR_ID_ROHM 0x10DB
197
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900198struct pch_uart_buffer {
199 unsigned char *buf;
200 int size;
201};
202
203struct eg20t_port {
204 struct uart_port port;
205 int port_type;
206 void __iomem *membase;
207 resource_size_t mapbase;
208 unsigned int iobase;
209 struct pci_dev *pdev;
210 int fifo_size;
211 int base_baud;
212 int start_tx;
213 int start_rx;
214 int tx_empty;
215 int int_dis_flag;
216 int trigger;
217 int trigger_level;
218 struct pch_uart_buffer rxbuf;
219 unsigned int dmsr;
220 unsigned int fcr;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +0900221 unsigned int mcr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900222 unsigned int use_dma;
223 unsigned int use_dma_flag;
224 struct dma_async_tx_descriptor *desc_tx;
225 struct dma_async_tx_descriptor *desc_rx;
226 struct pch_dma_slave param_tx;
227 struct pch_dma_slave param_rx;
228 struct dma_chan *chan_tx;
229 struct dma_chan *chan_rx;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900230 struct scatterlist *sg_tx_p;
231 int nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900232 struct scatterlist sg_rx;
233 int tx_dma_use;
234 void *rx_buf_virt;
235 dma_addr_t rx_buf_dma;
236};
237
238static unsigned int default_baud = 9600;
239static const int trigger_level_256[4] = { 1, 64, 128, 224 };
240static const int trigger_level_64[4] = { 1, 16, 32, 56 };
241static const int trigger_level_16[4] = { 1, 4, 8, 14 };
242static const int trigger_level_1[4] = { 1, 1, 1, 1 };
243
244static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
245 int base_baud)
246{
247 struct eg20t_port *priv = pci_get_drvdata(pdev);
248
249 priv->trigger_level = 1;
250 priv->fcr = 0;
251}
252
253static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
254{
255 unsigned int msr = ioread8(base + UART_MSR);
256 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
257
258 return msr;
259}
260
261static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
262 unsigned int flag)
263{
264 u8 ier = ioread8(priv->membase + UART_IER);
265 ier |= flag & PCH_UART_IER_MASK;
266 iowrite8(ier, priv->membase + UART_IER);
267}
268
269static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
270 unsigned int flag)
271{
272 u8 ier = ioread8(priv->membase + UART_IER);
273 ier &= ~(flag & PCH_UART_IER_MASK);
274 iowrite8(ier, priv->membase + UART_IER);
275}
276
277static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
278 unsigned int parity, unsigned int bits,
279 unsigned int stb)
280{
281 unsigned int dll, dlm, lcr;
282 int div;
283
284 div = DIV_ROUND(priv->base_baud / 16, baud);
285 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900286 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900287 return -EINVAL;
288 }
289
290 dll = (unsigned int)div & 0x00FFU;
291 dlm = ((unsigned int)div >> 8) & 0x00FFU;
292
293 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900294 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900295 return -EINVAL;
296 }
297
298 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900299 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900300 return -EINVAL;
301 }
302
303 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900304 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900305 return -EINVAL;
306 }
307
308 lcr = parity;
309 lcr |= bits;
310 lcr |= stb;
311
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900312 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900313 __func__, baud, div, lcr, jiffies);
314 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
315 iowrite8(dll, priv->membase + PCH_UART_DLL);
316 iowrite8(dlm, priv->membase + PCH_UART_DLM);
317 iowrite8(lcr, priv->membase + UART_LCR);
318
319 return 0;
320}
321
322static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
323 unsigned int flag)
324{
325 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900326 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
327 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900328 return -EINVAL;
329 }
330
331 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
332 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
333 priv->membase + UART_FCR);
334 iowrite8(priv->fcr, priv->membase + UART_FCR);
335
336 return 0;
337}
338
339static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
340 unsigned int dmamode,
341 unsigned int fifo_size, unsigned int trigger)
342{
343 u8 fcr;
344
345 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900346 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
347 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900348 return -EINVAL;
349 }
350
351 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900352 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
353 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900354 return -EINVAL;
355 }
356
357 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900358 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
359 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900360 return -EINVAL;
361 }
362
363 switch (priv->fifo_size) {
364 case 256:
365 priv->trigger_level =
366 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
367 break;
368 case 64:
369 priv->trigger_level =
370 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
371 break;
372 case 16:
373 priv->trigger_level =
374 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
375 break;
376 default:
377 priv->trigger_level =
378 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
379 break;
380 }
381 fcr =
382 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
383 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
384 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
385 priv->membase + UART_FCR);
386 iowrite8(fcr, priv->membase + UART_FCR);
387 priv->fcr = fcr;
388
389 return 0;
390}
391
392static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
393{
394 priv->dmsr = 0;
395 return get_msr(priv, priv->membase);
396}
397
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900398static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900399 const unsigned char *buf, int tx_size)
400{
401 int i;
402 unsigned int thr;
403
404 for (i = 0; i < tx_size;) {
405 thr = buf[i++];
406 iowrite8(thr, priv->membase + PCH_UART_THR);
407 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900408}
409
410static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
411 int rx_size)
412{
413 int i;
414 u8 rbr, lsr;
415
416 lsr = ioread8(priv->membase + UART_LSR);
417 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
418 i < rx_size && lsr & UART_LSR_DR;
419 lsr = ioread8(priv->membase + UART_LSR)) {
420 rbr = ioread8(priv->membase + PCH_UART_RBR);
421 buf[i++] = rbr;
422 }
423 return i;
424}
425
426static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
427{
428 unsigned int iir;
429 int ret;
430
431 iir = ioread8(priv->membase + UART_IIR);
432 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
433 return ret;
434}
435
436static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
437{
438 return ioread8(priv->membase + UART_LSR);
439}
440
441static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
442{
443 unsigned int lcr;
444
445 lcr = ioread8(priv->membase + UART_LCR);
446 if (on)
447 lcr |= PCH_UART_LCR_SB;
448 else
449 lcr &= ~PCH_UART_LCR_SB;
450
451 iowrite8(lcr, priv->membase + UART_LCR);
452}
453
454static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
455 int size)
456{
457 struct uart_port *port;
458 struct tty_struct *tty;
459
460 port = &priv->port;
461 tty = tty_port_tty_get(&port->state->port);
462 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900463 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900464 return -EBUSY;
465 }
466
467 tty_insert_flip_string(tty, buf, size);
468 tty_flip_buffer_push(tty);
469 tty_kref_put(tty);
470
471 return 0;
472}
473
474static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
475{
476 int ret;
477 struct uart_port *port = &priv->port;
478
479 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900480 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
481 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900482 buf[0] = port->x_char;
483 port->x_char = 0;
484 ret = 1;
485 } else {
486 ret = 0;
487 }
488
489 return ret;
490}
491
492static int dma_push_rx(struct eg20t_port *priv, int size)
493{
494 struct tty_struct *tty;
495 int room;
496 struct uart_port *port = &priv->port;
497
498 port = &priv->port;
499 tty = tty_port_tty_get(&port->state->port);
500 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900501 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900502 return 0;
503 }
504
505 room = tty_buffer_request_room(tty, size);
506
507 if (room < size)
508 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
509 size - room);
510 if (!room)
511 return room;
512
513 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
514
515 port->icount.rx += room;
516 tty_kref_put(tty);
517
518 return room;
519}
520
521static void pch_free_dma(struct uart_port *port)
522{
523 struct eg20t_port *priv;
524 priv = container_of(port, struct eg20t_port, port);
525
526 if (priv->chan_tx) {
527 dma_release_channel(priv->chan_tx);
528 priv->chan_tx = NULL;
529 }
530 if (priv->chan_rx) {
531 dma_release_channel(priv->chan_rx);
532 priv->chan_rx = NULL;
533 }
534 if (sg_dma_address(&priv->sg_rx))
535 dma_free_coherent(port->dev, port->fifosize,
536 sg_virt(&priv->sg_rx),
537 sg_dma_address(&priv->sg_rx));
538
539 return;
540}
541
542static bool filter(struct dma_chan *chan, void *slave)
543{
544 struct pch_dma_slave *param = slave;
545
546 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
547 chan->device->dev)) {
548 chan->private = param;
549 return true;
550 } else {
551 return false;
552 }
553}
554
555static void pch_request_dma(struct uart_port *port)
556{
557 dma_cap_mask_t mask;
558 struct dma_chan *chan;
559 struct pci_dev *dma_dev;
560 struct pch_dma_slave *param;
561 struct eg20t_port *priv =
562 container_of(port, struct eg20t_port, port);
563 dma_cap_zero(mask);
564 dma_cap_set(DMA_SLAVE, mask);
565
566 dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
567 information */
568 /* Set Tx DMA */
569 param = &priv->param_tx;
570 param->dma_dev = &dma_dev->dev;
571 param->chan_id = priv->port.line;
572 param->tx_reg = port->mapbase + UART_TX;
573 chan = dma_request_channel(mask, filter, param);
574 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900575 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
576 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900577 return;
578 }
579 priv->chan_tx = chan;
580
581 /* Set Rx DMA */
582 param = &priv->param_rx;
583 param->dma_dev = &dma_dev->dev;
584 param->chan_id = priv->port.line + 1; /* Rx = Tx + 1 */
585 param->rx_reg = port->mapbase + UART_RX;
586 chan = dma_request_channel(mask, filter, param);
587 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900588 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
589 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900590 dma_release_channel(priv->chan_tx);
591 return;
592 }
593
594 /* Get Consistent memory for DMA */
595 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
596 &priv->rx_buf_dma, GFP_KERNEL);
597 priv->chan_rx = chan;
598}
599
600static void pch_dma_rx_complete(void *arg)
601{
602 struct eg20t_port *priv = arg;
603 struct uart_port *port = &priv->port;
604 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900605 int count;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900606
607 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900608 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900609 return;
610 }
611
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900612 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
613 count = dma_push_rx(priv, priv->trigger_level);
614 if (count)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900615 tty_flip_buffer_push(tty);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900616 tty_kref_put(tty);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900617 async_tx_ack(priv->desc_rx);
618 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900619}
620
621static void pch_dma_tx_complete(void *arg)
622{
623 struct eg20t_port *priv = arg;
624 struct uart_port *port = &priv->port;
625 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900626 struct scatterlist *sg = priv->sg_tx_p;
627 int i;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900628
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900629 for (i = 0; i < priv->nent; i++, sg++) {
630 xmit->tail += sg_dma_len(sg);
631 port->icount.tx += sg_dma_len(sg);
632 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900633 xmit->tail &= UART_XMIT_SIZE - 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900634 async_tx_ack(priv->desc_tx);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900635 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900636 priv->tx_dma_use = 0;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900637 priv->nent = 0;
638 kfree(priv->sg_tx_p);
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900639 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900640}
641
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900642static int pop_tx(struct eg20t_port *priv, int size)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900643{
644 int count = 0;
645 struct uart_port *port = &priv->port;
646 struct circ_buf *xmit = &port->state->xmit;
647
648 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
649 goto pop_tx_end;
650
651 do {
652 int cnt_to_end =
653 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
654 int sz = min(size - count, cnt_to_end);
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900655 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900656 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
657 count += sz;
658 } while (!uart_circ_empty(xmit) && count < size);
659
660pop_tx_end:
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900661 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900662 count, size - count, jiffies);
663
664 return count;
665}
666
667static int handle_rx_to(struct eg20t_port *priv)
668{
669 struct pch_uart_buffer *buf;
670 int rx_size;
671 int ret;
672 if (!priv->start_rx) {
673 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
674 return 0;
675 }
676 buf = &priv->rxbuf;
677 do {
678 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
679 ret = push_rx(priv, buf->buf, rx_size);
680 if (ret)
681 return 0;
682 } while (rx_size == buf->size);
683
684 return PCH_UART_HANDLED_RX_INT;
685}
686
687static int handle_rx(struct eg20t_port *priv)
688{
689 return handle_rx_to(priv);
690}
691
692static int dma_handle_rx(struct eg20t_port *priv)
693{
694 struct uart_port *port = &priv->port;
695 struct dma_async_tx_descriptor *desc;
696 struct scatterlist *sg;
697
698 priv = container_of(port, struct eg20t_port, port);
699 sg = &priv->sg_rx;
700
701 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
702
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900703 sg_dma_len(sg) = priv->trigger_level;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900704
705 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900706 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
707 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900708
709 sg_dma_address(sg) = priv->rx_buf_dma;
710
711 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
712 sg, 1, DMA_FROM_DEVICE,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900713 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
714
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900715 if (!desc)
716 return 0;
717
718 priv->desc_rx = desc;
719 desc->callback = pch_dma_rx_complete;
720 desc->callback_param = priv;
721 desc->tx_submit(desc);
722 dma_async_issue_pending(priv->chan_rx);
723
724 return PCH_UART_HANDLED_RX_INT;
725}
726
727static unsigned int handle_tx(struct eg20t_port *priv)
728{
729 struct uart_port *port = &priv->port;
730 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900731 int fifo_size;
732 int tx_size;
733 int size;
734 int tx_empty;
735
736 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900737 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
738 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900739 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
740 priv->tx_empty = 1;
741 return 0;
742 }
743
744 fifo_size = max(priv->fifo_size, 1);
745 tx_empty = 1;
746 if (pop_tx_x(priv, xmit->buf)) {
747 pch_uart_hal_write(priv, xmit->buf, 1);
748 port->icount.tx++;
749 tx_empty = 0;
750 fifo_size--;
751 }
752 size = min(xmit->head - xmit->tail, fifo_size);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900753 if (size < 0)
754 size = fifo_size;
755
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900756 tx_size = pop_tx(priv, size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900757 if (tx_size > 0) {
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900758 port->icount.tx += tx_size;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900759 tx_empty = 0;
760 }
761
762 priv->tx_empty = tx_empty;
763
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900764 if (tx_empty) {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900765 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900766 uart_write_wakeup(port);
767 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900768
769 return PCH_UART_HANDLED_TX_INT;
770}
771
772static unsigned int dma_handle_tx(struct eg20t_port *priv)
773{
774 struct uart_port *port = &priv->port;
775 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900776 struct scatterlist *sg;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900777 int nent;
778 int fifo_size;
779 int tx_empty;
780 struct dma_async_tx_descriptor *desc;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900781 int num;
782 int i;
783 int bytes;
784 int size;
785 int rem;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900786
787 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900788 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
789 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900790 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
791 priv->tx_empty = 1;
792 return 0;
793 }
794
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900795 if (priv->tx_dma_use) {
796 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
797 __func__, jiffies);
798 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
799 priv->tx_empty = 1;
800 return 0;
801 }
802
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900803 fifo_size = max(priv->fifo_size, 1);
804 tx_empty = 1;
805 if (pop_tx_x(priv, xmit->buf)) {
806 pch_uart_hal_write(priv, xmit->buf, 1);
807 port->icount.tx++;
808 tx_empty = 0;
809 fifo_size--;
810 }
811
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900812 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
813 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
814 xmit->tail, UART_XMIT_SIZE));
815 if (!bytes) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900816 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900817 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
818 uart_write_wakeup(port);
819 return 0;
820 }
821
822 if (bytes > fifo_size) {
823 num = bytes / fifo_size + 1;
824 size = fifo_size;
825 rem = bytes % fifo_size;
826 } else {
827 num = 1;
828 size = bytes;
829 rem = bytes;
830 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900831
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900832 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
833 __func__, num, size, rem);
834
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900835 priv->tx_dma_use = 1;
836
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900837 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900838
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900839 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
840 sg = priv->sg_tx_p;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900841
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900842 for (i = 0; i < num; i++, sg++) {
843 if (i == (num - 1))
844 sg_set_page(sg, virt_to_page(xmit->buf),
845 rem, fifo_size * i);
846 else
847 sg_set_page(sg, virt_to_page(xmit->buf),
848 size, fifo_size * i);
849 }
850
851 sg = priv->sg_tx_p;
852 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900853 if (!nent) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900854 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900855 return 0;
856 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900857 priv->nent = nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900858
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900859 for (i = 0; i < nent; i++, sg++) {
860 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
861 fifo_size * i;
862 sg_dma_address(sg) = (sg_dma_address(sg) &
863 ~(UART_XMIT_SIZE - 1)) + sg->offset;
864 if (i == (nent - 1))
865 sg_dma_len(sg) = rem;
866 else
867 sg_dma_len(sg) = size;
868 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900869
870 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900871 priv->sg_tx_p, nent, DMA_TO_DEVICE,
872 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900873 if (!desc) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900874 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
875 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900876 return 0;
877 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900878 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900879 priv->desc_tx = desc;
880 desc->callback = pch_dma_tx_complete;
881 desc->callback_param = priv;
882
883 desc->tx_submit(desc);
884
885 dma_async_issue_pending(priv->chan_tx);
886
887 return PCH_UART_HANDLED_TX_INT;
888}
889
890static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
891{
892 u8 fcr = ioread8(priv->membase + UART_FCR);
893
894 /* Reset FIFO */
895 fcr |= UART_FCR_CLEAR_RCVR;
896 iowrite8(fcr, priv->membase + UART_FCR);
897
898 if (lsr & PCH_UART_LSR_ERR)
899 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
900
901 if (lsr & UART_LSR_FE)
902 dev_err(&priv->pdev->dev, "Framing Error\n");
903
904 if (lsr & UART_LSR_PE)
905 dev_err(&priv->pdev->dev, "Parity Error\n");
906
907 if (lsr & UART_LSR_OE)
908 dev_err(&priv->pdev->dev, "Overrun Error\n");
909}
910
911static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
912{
913 struct eg20t_port *priv = dev_id;
914 unsigned int handled;
915 u8 lsr;
916 int ret = 0;
917 unsigned int iid;
918 unsigned long flags;
919
920 spin_lock_irqsave(&priv->port.lock, flags);
921 handled = 0;
922 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
923 switch (iid) {
924 case PCH_UART_IID_RLS: /* Receiver Line Status */
925 lsr = pch_uart_hal_get_line_status(priv);
926 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
927 UART_LSR_PE | UART_LSR_OE)) {
928 pch_uart_err_ir(priv, lsr);
929 ret = PCH_UART_HANDLED_RX_ERR_INT;
930 }
931 break;
932 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900933 if (priv->use_dma) {
934 pch_uart_hal_disable_interrupt(priv,
935 PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900936 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900937 if (!ret)
938 pch_uart_hal_enable_interrupt(priv,
939 PCH_UART_HAL_RX_INT);
940 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900941 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900942 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900943 break;
944 case PCH_UART_IID_RDR_TO: /* Received Data Ready
945 (FIFO Timeout) */
946 ret = handle_rx_to(priv);
947 break;
948 case PCH_UART_IID_THRE: /* Transmitter Holding Register
949 Empty */
950 if (priv->use_dma)
951 ret = dma_handle_tx(priv);
952 else
953 ret = handle_tx(priv);
954 break;
955 case PCH_UART_IID_MS: /* Modem Status */
956 ret = PCH_UART_HANDLED_MS_INT;
957 break;
958 default: /* Never junp to this label */
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900959 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
960 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900961 ret = -1;
962 break;
963 }
964 handled |= (unsigned int)ret;
965 }
966 if (handled == 0 && iid <= 1) {
967 if (priv->int_dis_flag)
968 priv->int_dis_flag = 0;
969 }
970
971 spin_unlock_irqrestore(&priv->port.lock, flags);
972 return IRQ_RETVAL(handled);
973}
974
975/* This function tests whether the transmitter fifo and shifter for the port
976 described by 'port' is empty. */
977static unsigned int pch_uart_tx_empty(struct uart_port *port)
978{
979 struct eg20t_port *priv;
980 int ret;
981 priv = container_of(port, struct eg20t_port, port);
982 if (priv->tx_empty)
983 ret = TIOCSER_TEMT;
984 else
985 ret = 0;
986
987 return ret;
988}
989
990/* Returns the current state of modem control inputs. */
991static unsigned int pch_uart_get_mctrl(struct uart_port *port)
992{
993 struct eg20t_port *priv;
994 u8 modem;
995 unsigned int ret = 0;
996
997 priv = container_of(port, struct eg20t_port, port);
998 modem = pch_uart_hal_get_modem(priv);
999
1000 if (modem & UART_MSR_DCD)
1001 ret |= TIOCM_CAR;
1002
1003 if (modem & UART_MSR_RI)
1004 ret |= TIOCM_RNG;
1005
1006 if (modem & UART_MSR_DSR)
1007 ret |= TIOCM_DSR;
1008
1009 if (modem & UART_MSR_CTS)
1010 ret |= TIOCM_CTS;
1011
1012 return ret;
1013}
1014
1015static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1016{
1017 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001018 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1019
1020 if (mctrl & TIOCM_DTR)
1021 mcr |= UART_MCR_DTR;
1022 if (mctrl & TIOCM_RTS)
1023 mcr |= UART_MCR_RTS;
1024 if (mctrl & TIOCM_LOOP)
1025 mcr |= UART_MCR_LOOP;
1026
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001027 if (priv->mcr & UART_MCR_AFE)
1028 mcr |= UART_MCR_AFE;
1029
1030 if (mctrl)
1031 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001032}
1033
1034static void pch_uart_stop_tx(struct uart_port *port)
1035{
1036 struct eg20t_port *priv;
1037 priv = container_of(port, struct eg20t_port, port);
1038 priv->start_tx = 0;
1039 priv->tx_dma_use = 0;
1040}
1041
1042static void pch_uart_start_tx(struct uart_port *port)
1043{
1044 struct eg20t_port *priv;
1045
1046 priv = container_of(port, struct eg20t_port, port);
1047
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001048 if (priv->use_dma) {
1049 if (priv->tx_dma_use) {
1050 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1051 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001052 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001053 }
1054 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001055
1056 priv->start_tx = 1;
1057 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1058}
1059
1060static void pch_uart_stop_rx(struct uart_port *port)
1061{
1062 struct eg20t_port *priv;
1063 priv = container_of(port, struct eg20t_port, port);
1064 priv->start_rx = 0;
1065 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1066 priv->int_dis_flag = 1;
1067}
1068
1069/* Enable the modem status interrupts. */
1070static void pch_uart_enable_ms(struct uart_port *port)
1071{
1072 struct eg20t_port *priv;
1073 priv = container_of(port, struct eg20t_port, port);
1074 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1075}
1076
1077/* Control the transmission of a break signal. */
1078static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1079{
1080 struct eg20t_port *priv;
1081 unsigned long flags;
1082
1083 priv = container_of(port, struct eg20t_port, port);
1084 spin_lock_irqsave(&port->lock, flags);
1085 pch_uart_hal_set_break(priv, ctl);
1086 spin_unlock_irqrestore(&port->lock, flags);
1087}
1088
1089/* Grab any interrupt resources and initialise any low level driver state. */
1090static int pch_uart_startup(struct uart_port *port)
1091{
1092 struct eg20t_port *priv;
1093 int ret;
1094 int fifo_size;
1095 int trigger_level;
1096
1097 priv = container_of(port, struct eg20t_port, port);
1098 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001099
1100 if (port->uartclk)
1101 priv->base_baud = port->uartclk;
1102 else
1103 port->uartclk = priv->base_baud;
1104
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001105 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1106 ret = pch_uart_hal_set_line(priv, default_baud,
1107 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1108 PCH_UART_HAL_STB1);
1109 if (ret)
1110 return ret;
1111
1112 switch (priv->fifo_size) {
1113 case 256:
1114 fifo_size = PCH_UART_HAL_FIFO256;
1115 break;
1116 case 64:
1117 fifo_size = PCH_UART_HAL_FIFO64;
1118 break;
1119 case 16:
1120 fifo_size = PCH_UART_HAL_FIFO16;
1121 case 1:
1122 default:
1123 fifo_size = PCH_UART_HAL_FIFO_DIS;
1124 break;
1125 }
1126
1127 switch (priv->trigger) {
1128 case PCH_UART_HAL_TRIGGER1:
1129 trigger_level = 1;
1130 break;
1131 case PCH_UART_HAL_TRIGGER_L:
1132 trigger_level = priv->fifo_size / 4;
1133 break;
1134 case PCH_UART_HAL_TRIGGER_M:
1135 trigger_level = priv->fifo_size / 2;
1136 break;
1137 case PCH_UART_HAL_TRIGGER_H:
1138 default:
1139 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1140 break;
1141 }
1142
1143 priv->trigger_level = trigger_level;
1144 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1145 fifo_size, priv->trigger);
1146 if (ret < 0)
1147 return ret;
1148
1149 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1150 KBUILD_MODNAME, priv);
1151 if (ret < 0)
1152 return ret;
1153
1154 if (priv->use_dma)
1155 pch_request_dma(port);
1156
1157 priv->start_rx = 1;
1158 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1159 uart_update_timeout(port, CS8, default_baud);
1160
1161 return 0;
1162}
1163
1164static void pch_uart_shutdown(struct uart_port *port)
1165{
1166 struct eg20t_port *priv;
1167 int ret;
1168
1169 priv = container_of(port, struct eg20t_port, port);
1170 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1171 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1172 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1173 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1174 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001175 dev_err(priv->port.dev,
1176 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001177
1178 if (priv->use_dma_flag)
1179 pch_free_dma(port);
1180
1181 free_irq(priv->port.irq, priv);
1182}
1183
1184/* Change the port parameters, including word length, parity, stop
1185 *bits. Update read_status_mask and ignore_status_mask to indicate
1186 *the types of events we are interested in receiving. */
1187static void pch_uart_set_termios(struct uart_port *port,
1188 struct ktermios *termios, struct ktermios *old)
1189{
1190 int baud;
1191 int rtn;
1192 unsigned int parity, bits, stb;
1193 struct eg20t_port *priv;
1194 unsigned long flags;
1195
1196 priv = container_of(port, struct eg20t_port, port);
1197 switch (termios->c_cflag & CSIZE) {
1198 case CS5:
1199 bits = PCH_UART_HAL_5BIT;
1200 break;
1201 case CS6:
1202 bits = PCH_UART_HAL_6BIT;
1203 break;
1204 case CS7:
1205 bits = PCH_UART_HAL_7BIT;
1206 break;
1207 default: /* CS8 */
1208 bits = PCH_UART_HAL_8BIT;
1209 break;
1210 }
1211 if (termios->c_cflag & CSTOPB)
1212 stb = PCH_UART_HAL_STB2;
1213 else
1214 stb = PCH_UART_HAL_STB1;
1215
1216 if (termios->c_cflag & PARENB) {
1217 if (!(termios->c_cflag & PARODD))
1218 parity = PCH_UART_HAL_PARITY_ODD;
1219 else
1220 parity = PCH_UART_HAL_PARITY_EVEN;
1221
1222 } else {
1223 parity = PCH_UART_HAL_PARITY_NONE;
1224 }
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001225
1226 /* Only UART0 has auto hardware flow function */
1227 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1228 priv->mcr |= UART_MCR_AFE;
1229 else
1230 priv->mcr &= ~UART_MCR_AFE;
1231
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001232 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1233
1234 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1235
1236 spin_lock_irqsave(&port->lock, flags);
1237
1238 uart_update_timeout(port, termios->c_cflag, baud);
1239 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1240 if (rtn)
1241 goto out;
1242
1243 /* Don't rewrite B0 */
1244 if (tty_termios_baud_rate(termios))
1245 tty_termios_encode_baud_rate(termios, baud, baud);
1246
1247out:
1248 spin_unlock_irqrestore(&port->lock, flags);
1249}
1250
1251static const char *pch_uart_type(struct uart_port *port)
1252{
1253 return KBUILD_MODNAME;
1254}
1255
1256static void pch_uart_release_port(struct uart_port *port)
1257{
1258 struct eg20t_port *priv;
1259
1260 priv = container_of(port, struct eg20t_port, port);
1261 pci_iounmap(priv->pdev, priv->membase);
1262 pci_release_regions(priv->pdev);
1263}
1264
1265static int pch_uart_request_port(struct uart_port *port)
1266{
1267 struct eg20t_port *priv;
1268 int ret;
1269 void __iomem *membase;
1270
1271 priv = container_of(port, struct eg20t_port, port);
1272 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1273 if (ret < 0)
1274 return -EBUSY;
1275
1276 membase = pci_iomap(priv->pdev, 1, 0);
1277 if (!membase) {
1278 pci_release_regions(priv->pdev);
1279 return -EBUSY;
1280 }
1281 priv->membase = port->membase = membase;
1282
1283 return 0;
1284}
1285
1286static void pch_uart_config_port(struct uart_port *port, int type)
1287{
1288 struct eg20t_port *priv;
1289
1290 priv = container_of(port, struct eg20t_port, port);
1291 if (type & UART_CONFIG_TYPE) {
1292 port->type = priv->port_type;
1293 pch_uart_request_port(port);
1294 }
1295}
1296
1297static int pch_uart_verify_port(struct uart_port *port,
1298 struct serial_struct *serinfo)
1299{
1300 struct eg20t_port *priv;
1301
1302 priv = container_of(port, struct eg20t_port, port);
1303 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001304 dev_info(priv->port.dev,
1305 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001306 priv->use_dma = 0;
1307 serinfo->flags &= ~UPF_LOW_LATENCY;
1308 } else {
1309#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001310 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1311 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001312 return -EOPNOTSUPP;
1313#endif
1314 priv->use_dma = 1;
1315 priv->use_dma_flag = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001316 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001317 }
1318
1319 return 0;
1320}
1321
1322static struct uart_ops pch_uart_ops = {
1323 .tx_empty = pch_uart_tx_empty,
1324 .set_mctrl = pch_uart_set_mctrl,
1325 .get_mctrl = pch_uart_get_mctrl,
1326 .stop_tx = pch_uart_stop_tx,
1327 .start_tx = pch_uart_start_tx,
1328 .stop_rx = pch_uart_stop_rx,
1329 .enable_ms = pch_uart_enable_ms,
1330 .break_ctl = pch_uart_break_ctl,
1331 .startup = pch_uart_startup,
1332 .shutdown = pch_uart_shutdown,
1333 .set_termios = pch_uart_set_termios,
1334/* .pm = pch_uart_pm, Not supported yet */
1335/* .set_wake = pch_uart_set_wake, Not supported yet */
1336 .type = pch_uart_type,
1337 .release_port = pch_uart_release_port,
1338 .request_port = pch_uart_request_port,
1339 .config_port = pch_uart_config_port,
1340 .verify_port = pch_uart_verify_port
1341};
1342
1343static struct uart_driver pch_uart_driver = {
1344 .owner = THIS_MODULE,
1345 .driver_name = KBUILD_MODNAME,
1346 .dev_name = PCH_UART_DRIVER_DEVICE,
1347 .major = 0,
1348 .minor = 0,
1349 .nr = PCH_UART_NR,
1350};
1351
1352static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001353 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001354{
1355 struct eg20t_port *priv;
1356 int ret;
1357 unsigned int iobase;
1358 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001359 unsigned char *rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001360 int fifosize, base_baud;
1361 static int num;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001362 int port_type = id->driver_data;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001363
1364 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1365 if (priv == NULL)
1366 goto init_port_alloc_err;
1367
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001368 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001369 if (!rxbuf)
1370 goto init_port_free_txbuf;
1371
1372 switch (port_type) {
1373 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001374 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001375 base_baud = 1843200; /* 1.8432MHz */
1376 break;
1377 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001378 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001379 base_baud = 1843200; /* 1.8432MHz */
1380 break;
1381 default:
1382 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1383 goto init_port_hal_free;
1384 }
1385
1386 iobase = pci_resource_start(pdev, 0);
1387 mapbase = pci_resource_start(pdev, 1);
1388 priv->mapbase = mapbase;
1389 priv->iobase = iobase;
1390 priv->pdev = pdev;
1391 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001392 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001393 priv->rxbuf.size = PAGE_SIZE;
1394
1395 priv->fifo_size = fifosize;
1396 priv->base_baud = base_baud;
1397 priv->port_type = PORT_MAX_8250 + port_type + 1;
1398 priv->port.dev = &pdev->dev;
1399 priv->port.iobase = iobase;
1400 priv->port.membase = NULL;
1401 priv->port.mapbase = mapbase;
1402 priv->port.irq = pdev->irq;
1403 priv->port.iotype = UPIO_PORT;
1404 priv->port.ops = &pch_uart_ops;
1405 priv->port.flags = UPF_BOOT_AUTOCONF;
1406 priv->port.fifosize = fifosize;
1407 priv->port.line = num++;
1408 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1409
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001410 spin_lock_init(&priv->port.lock);
1411
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001412 pci_set_drvdata(pdev, priv);
1413 pch_uart_hal_request(pdev, fifosize, base_baud);
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001414
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001415 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1416 if (ret < 0)
1417 goto init_port_hal_free;
1418
1419 return priv;
1420
1421init_port_hal_free:
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001422 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001423init_port_free_txbuf:
1424 kfree(priv);
1425init_port_alloc_err:
1426
1427 return NULL;
1428}
1429
1430static void pch_uart_exit_port(struct eg20t_port *priv)
1431{
1432 uart_remove_one_port(&pch_uart_driver, &priv->port);
1433 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001434 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001435}
1436
1437static void pch_uart_pci_remove(struct pci_dev *pdev)
1438{
1439 struct eg20t_port *priv;
1440
1441 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
1442 pch_uart_exit_port(priv);
1443 pci_disable_device(pdev);
1444 kfree(priv);
1445 return;
1446}
1447#ifdef CONFIG_PM
1448static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1449{
1450 struct eg20t_port *priv = pci_get_drvdata(pdev);
1451
1452 uart_suspend_port(&pch_uart_driver, &priv->port);
1453
1454 pci_save_state(pdev);
1455 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1456 return 0;
1457}
1458
1459static int pch_uart_pci_resume(struct pci_dev *pdev)
1460{
1461 struct eg20t_port *priv = pci_get_drvdata(pdev);
1462 int ret;
1463
1464 pci_set_power_state(pdev, PCI_D0);
1465 pci_restore_state(pdev);
1466
1467 ret = pci_enable_device(pdev);
1468 if (ret) {
1469 dev_err(&pdev->dev,
1470 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1471 return ret;
1472 }
1473
1474 uart_resume_port(&pch_uart_driver, &priv->port);
1475
1476 return 0;
1477}
1478#else
1479#define pch_uart_pci_suspend NULL
1480#define pch_uart_pci_resume NULL
1481#endif
1482
1483static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1484 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1485 .driver_data = PCH_UART_8LINE},
1486 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1487 .driver_data = PCH_UART_2LINE},
1488 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1489 .driver_data = PCH_UART_2LINE},
1490 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1491 .driver_data = PCH_UART_2LINE},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001492 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1493 .driver_data = PCH_UART_8LINE},
1494 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1495 .driver_data = PCH_UART_2LINE},
1496 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1497 .driver_data = PCH_UART_2LINE},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001498 {0,},
1499};
1500
1501static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1502 const struct pci_device_id *id)
1503{
1504 int ret;
1505 struct eg20t_port *priv;
1506
1507 ret = pci_enable_device(pdev);
1508 if (ret < 0)
1509 goto probe_error;
1510
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001511 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001512 if (!priv) {
1513 ret = -EBUSY;
1514 goto probe_disable_device;
1515 }
1516 pci_set_drvdata(pdev, priv);
1517
1518 return ret;
1519
1520probe_disable_device:
1521 pci_disable_device(pdev);
1522probe_error:
1523 return ret;
1524}
1525
1526static struct pci_driver pch_uart_pci_driver = {
1527 .name = "pch_uart",
1528 .id_table = pch_uart_pci_id,
1529 .probe = pch_uart_pci_probe,
1530 .remove = __devexit_p(pch_uart_pci_remove),
1531 .suspend = pch_uart_pci_suspend,
1532 .resume = pch_uart_pci_resume,
1533};
1534
1535static int __init pch_uart_module_init(void)
1536{
1537 int ret;
1538
1539 /* register as UART driver */
1540 ret = uart_register_driver(&pch_uart_driver);
1541 if (ret < 0)
1542 return ret;
1543
1544 /* register as PCI driver */
1545 ret = pci_register_driver(&pch_uart_pci_driver);
1546 if (ret < 0)
1547 uart_unregister_driver(&pch_uart_driver);
1548
1549 return ret;
1550}
1551module_init(pch_uart_module_init);
1552
1553static void __exit pch_uart_module_exit(void)
1554{
1555 pci_unregister_driver(&pch_uart_pci_driver);
1556 uart_unregister_driver(&pch_uart_driver);
1557}
1558module_exit(pch_uart_module_exit);
1559
1560MODULE_LICENSE("GPL v2");
1561MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1562module_param(default_baud, uint, S_IRUGO);