blob: 3b2fb93e1fa1aa530e7f94ee4752e465d66199e7 [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;
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;
230 struct scatterlist sg_rx;
231 int tx_dma_use;
232 void *rx_buf_virt;
233 dma_addr_t rx_buf_dma;
234};
235
236static unsigned int default_baud = 9600;
237static const int trigger_level_256[4] = { 1, 64, 128, 224 };
238static const int trigger_level_64[4] = { 1, 16, 32, 56 };
239static const int trigger_level_16[4] = { 1, 4, 8, 14 };
240static const int trigger_level_1[4] = { 1, 1, 1, 1 };
241
242static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
243 int base_baud)
244{
245 struct eg20t_port *priv = pci_get_drvdata(pdev);
246
247 priv->trigger_level = 1;
248 priv->fcr = 0;
249}
250
251static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
252{
253 unsigned int msr = ioread8(base + UART_MSR);
254 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
255
256 return msr;
257}
258
259static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
260 unsigned int flag)
261{
262 u8 ier = ioread8(priv->membase + UART_IER);
263 ier |= flag & PCH_UART_IER_MASK;
264 iowrite8(ier, priv->membase + UART_IER);
265}
266
267static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
268 unsigned int flag)
269{
270 u8 ier = ioread8(priv->membase + UART_IER);
271 ier &= ~(flag & PCH_UART_IER_MASK);
272 iowrite8(ier, priv->membase + UART_IER);
273}
274
275static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
276 unsigned int parity, unsigned int bits,
277 unsigned int stb)
278{
279 unsigned int dll, dlm, lcr;
280 int div;
281
282 div = DIV_ROUND(priv->base_baud / 16, baud);
283 if (div < 0 || USHRT_MAX <= div) {
284 pr_err("Invalid Baud(div=0x%x)\n", div);
285 return -EINVAL;
286 }
287
288 dll = (unsigned int)div & 0x00FFU;
289 dlm = ((unsigned int)div >> 8) & 0x00FFU;
290
291 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
292 pr_err("Invalid parity(0x%x)\n", parity);
293 return -EINVAL;
294 }
295
296 if (bits & ~PCH_UART_LCR_WLS) {
297 pr_err("Invalid bits(0x%x)\n", bits);
298 return -EINVAL;
299 }
300
301 if (stb & ~PCH_UART_LCR_STB) {
302 pr_err("Invalid STB(0x%x)\n", stb);
303 return -EINVAL;
304 }
305
306 lcr = parity;
307 lcr |= bits;
308 lcr |= stb;
309
310 pr_debug("%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
311 __func__, baud, div, lcr, jiffies);
312 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
313 iowrite8(dll, priv->membase + PCH_UART_DLL);
314 iowrite8(dlm, priv->membase + PCH_UART_DLM);
315 iowrite8(lcr, priv->membase + UART_LCR);
316
317 return 0;
318}
319
320static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
321 unsigned int flag)
322{
323 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
324 pr_err("%s:Invalid flag(0x%x)\n", __func__, flag);
325 return -EINVAL;
326 }
327
328 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
329 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
330 priv->membase + UART_FCR);
331 iowrite8(priv->fcr, priv->membase + UART_FCR);
332
333 return 0;
334}
335
336static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
337 unsigned int dmamode,
338 unsigned int fifo_size, unsigned int trigger)
339{
340 u8 fcr;
341
342 if (dmamode & ~PCH_UART_FCR_DMS) {
343 pr_err("%s:Invalid DMA Mode(0x%x)\n", __func__, dmamode);
344 return -EINVAL;
345 }
346
347 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
348 pr_err("%s:Invalid FIFO SIZE(0x%x)\n", __func__, fifo_size);
349 return -EINVAL;
350 }
351
352 if (trigger & ~PCH_UART_FCR_RFTL) {
353 pr_err("%s:Invalid TRIGGER(0x%x)\n", __func__, trigger);
354 return -EINVAL;
355 }
356
357 switch (priv->fifo_size) {
358 case 256:
359 priv->trigger_level =
360 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
361 break;
362 case 64:
363 priv->trigger_level =
364 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
365 break;
366 case 16:
367 priv->trigger_level =
368 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
369 break;
370 default:
371 priv->trigger_level =
372 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
373 break;
374 }
375 fcr =
376 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
377 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
378 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
379 priv->membase + UART_FCR);
380 iowrite8(fcr, priv->membase + UART_FCR);
381 priv->fcr = fcr;
382
383 return 0;
384}
385
386static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
387{
388 priv->dmsr = 0;
389 return get_msr(priv, priv->membase);
390}
391
392static int pch_uart_hal_write(struct eg20t_port *priv,
393 const unsigned char *buf, int tx_size)
394{
395 int i;
396 unsigned int thr;
397
398 for (i = 0; i < tx_size;) {
399 thr = buf[i++];
400 iowrite8(thr, priv->membase + PCH_UART_THR);
401 }
402 return i;
403}
404
405static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
406 int rx_size)
407{
408 int i;
409 u8 rbr, lsr;
410
411 lsr = ioread8(priv->membase + UART_LSR);
412 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
413 i < rx_size && lsr & UART_LSR_DR;
414 lsr = ioread8(priv->membase + UART_LSR)) {
415 rbr = ioread8(priv->membase + PCH_UART_RBR);
416 buf[i++] = rbr;
417 }
418 return i;
419}
420
421static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
422{
423 unsigned int iir;
424 int ret;
425
426 iir = ioread8(priv->membase + UART_IIR);
427 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
428 return ret;
429}
430
431static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
432{
433 return ioread8(priv->membase + UART_LSR);
434}
435
436static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
437{
438 unsigned int lcr;
439
440 lcr = ioread8(priv->membase + UART_LCR);
441 if (on)
442 lcr |= PCH_UART_LCR_SB;
443 else
444 lcr &= ~PCH_UART_LCR_SB;
445
446 iowrite8(lcr, priv->membase + UART_LCR);
447}
448
449static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
450 int size)
451{
452 struct uart_port *port;
453 struct tty_struct *tty;
454
455 port = &priv->port;
456 tty = tty_port_tty_get(&port->state->port);
457 if (!tty) {
458 pr_debug("%s:tty is busy now", __func__);
459 return -EBUSY;
460 }
461
462 tty_insert_flip_string(tty, buf, size);
463 tty_flip_buffer_push(tty);
464 tty_kref_put(tty);
465
466 return 0;
467}
468
469static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
470{
471 int ret;
472 struct uart_port *port = &priv->port;
473
474 if (port->x_char) {
475 pr_debug("%s:X character send %02x (%lu)\n", __func__,
476 port->x_char, jiffies);
477 buf[0] = port->x_char;
478 port->x_char = 0;
479 ret = 1;
480 } else {
481 ret = 0;
482 }
483
484 return ret;
485}
486
487static int dma_push_rx(struct eg20t_port *priv, int size)
488{
489 struct tty_struct *tty;
490 int room;
491 struct uart_port *port = &priv->port;
492
493 port = &priv->port;
494 tty = tty_port_tty_get(&port->state->port);
495 if (!tty) {
496 pr_debug("%s:tty is busy now", __func__);
497 return 0;
498 }
499
500 room = tty_buffer_request_room(tty, size);
501
502 if (room < size)
503 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
504 size - room);
505 if (!room)
506 return room;
507
508 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
509
510 port->icount.rx += room;
511 tty_kref_put(tty);
512
513 return room;
514}
515
516static void pch_free_dma(struct uart_port *port)
517{
518 struct eg20t_port *priv;
519 priv = container_of(port, struct eg20t_port, port);
520
521 if (priv->chan_tx) {
522 dma_release_channel(priv->chan_tx);
523 priv->chan_tx = NULL;
524 }
525 if (priv->chan_rx) {
526 dma_release_channel(priv->chan_rx);
527 priv->chan_rx = NULL;
528 }
529 if (sg_dma_address(&priv->sg_rx))
530 dma_free_coherent(port->dev, port->fifosize,
531 sg_virt(&priv->sg_rx),
532 sg_dma_address(&priv->sg_rx));
533
534 return;
535}
536
537static bool filter(struct dma_chan *chan, void *slave)
538{
539 struct pch_dma_slave *param = slave;
540
541 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
542 chan->device->dev)) {
543 chan->private = param;
544 return true;
545 } else {
546 return false;
547 }
548}
549
550static void pch_request_dma(struct uart_port *port)
551{
552 dma_cap_mask_t mask;
553 struct dma_chan *chan;
554 struct pci_dev *dma_dev;
555 struct pch_dma_slave *param;
556 struct eg20t_port *priv =
557 container_of(port, struct eg20t_port, port);
558 dma_cap_zero(mask);
559 dma_cap_set(DMA_SLAVE, mask);
560
561 dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
562 information */
563 /* Set Tx DMA */
564 param = &priv->param_tx;
565 param->dma_dev = &dma_dev->dev;
566 param->chan_id = priv->port.line;
567 param->tx_reg = port->mapbase + UART_TX;
568 chan = dma_request_channel(mask, filter, param);
569 if (!chan) {
570 pr_err("%s:dma_request_channel FAILS(Tx)\n", __func__);
571 return;
572 }
573 priv->chan_tx = chan;
574
575 /* Set Rx DMA */
576 param = &priv->param_rx;
577 param->dma_dev = &dma_dev->dev;
578 param->chan_id = priv->port.line + 1; /* Rx = Tx + 1 */
579 param->rx_reg = port->mapbase + UART_RX;
580 chan = dma_request_channel(mask, filter, param);
581 if (!chan) {
582 pr_err("%s:dma_request_channel FAILS(Rx)\n", __func__);
583 dma_release_channel(priv->chan_tx);
584 return;
585 }
586
587 /* Get Consistent memory for DMA */
588 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
589 &priv->rx_buf_dma, GFP_KERNEL);
590 priv->chan_rx = chan;
591}
592
593static void pch_dma_rx_complete(void *arg)
594{
595 struct eg20t_port *priv = arg;
596 struct uart_port *port = &priv->port;
597 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
598
599 if (!tty) {
600 pr_debug("%s:tty is busy now", __func__);
601 return;
602 }
603
604 if (dma_push_rx(priv, priv->trigger_level))
605 tty_flip_buffer_push(tty);
606
607 tty_kref_put(tty);
608}
609
610static void pch_dma_tx_complete(void *arg)
611{
612 struct eg20t_port *priv = arg;
613 struct uart_port *port = &priv->port;
614 struct circ_buf *xmit = &port->state->xmit;
615
616 xmit->tail += sg_dma_len(&priv->sg_tx);
617 xmit->tail &= UART_XMIT_SIZE - 1;
618 port->icount.tx += sg_dma_len(&priv->sg_tx);
619
620 async_tx_ack(priv->desc_tx);
621 priv->tx_dma_use = 0;
622}
623
624static int pop_tx(struct eg20t_port *priv, unsigned char *buf, int size)
625{
626 int count = 0;
627 struct uart_port *port = &priv->port;
628 struct circ_buf *xmit = &port->state->xmit;
629
630 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
631 goto pop_tx_end;
632
633 do {
634 int cnt_to_end =
635 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
636 int sz = min(size - count, cnt_to_end);
637 memcpy(&buf[count], &xmit->buf[xmit->tail], sz);
638 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
639 count += sz;
640 } while (!uart_circ_empty(xmit) && count < size);
641
642pop_tx_end:
643 pr_debug("%d characters. Remained %d characters. (%lu)\n",
644 count, size - count, jiffies);
645
646 return count;
647}
648
649static int handle_rx_to(struct eg20t_port *priv)
650{
651 struct pch_uart_buffer *buf;
652 int rx_size;
653 int ret;
654 if (!priv->start_rx) {
655 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
656 return 0;
657 }
658 buf = &priv->rxbuf;
659 do {
660 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
661 ret = push_rx(priv, buf->buf, rx_size);
662 if (ret)
663 return 0;
664 } while (rx_size == buf->size);
665
666 return PCH_UART_HANDLED_RX_INT;
667}
668
669static int handle_rx(struct eg20t_port *priv)
670{
671 return handle_rx_to(priv);
672}
673
674static int dma_handle_rx(struct eg20t_port *priv)
675{
676 struct uart_port *port = &priv->port;
677 struct dma_async_tx_descriptor *desc;
678 struct scatterlist *sg;
679
680 priv = container_of(port, struct eg20t_port, port);
681 sg = &priv->sg_rx;
682
683 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
684
685 sg_dma_len(sg) = priv->fifo_size;
686
687 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900688 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
689 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900690
691 sg_dma_address(sg) = priv->rx_buf_dma;
692
693 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
694 sg, 1, DMA_FROM_DEVICE,
695 DMA_PREP_INTERRUPT);
696 if (!desc)
697 return 0;
698
699 priv->desc_rx = desc;
700 desc->callback = pch_dma_rx_complete;
701 desc->callback_param = priv;
702 desc->tx_submit(desc);
703 dma_async_issue_pending(priv->chan_rx);
704
705 return PCH_UART_HANDLED_RX_INT;
706}
707
708static unsigned int handle_tx(struct eg20t_port *priv)
709{
710 struct uart_port *port = &priv->port;
711 struct circ_buf *xmit = &port->state->xmit;
712 int ret;
713 int fifo_size;
714 int tx_size;
715 int size;
716 int tx_empty;
717
718 if (!priv->start_tx) {
719 pr_info("%s:Tx isn't started. (%lu)\n", __func__, jiffies);
720 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
721 priv->tx_empty = 1;
722 return 0;
723 }
724
725 fifo_size = max(priv->fifo_size, 1);
726 tx_empty = 1;
727 if (pop_tx_x(priv, xmit->buf)) {
728 pch_uart_hal_write(priv, xmit->buf, 1);
729 port->icount.tx++;
730 tx_empty = 0;
731 fifo_size--;
732 }
733 size = min(xmit->head - xmit->tail, fifo_size);
734 tx_size = pop_tx(priv, xmit->buf, size);
735 if (tx_size > 0) {
736 ret = pch_uart_hal_write(priv, xmit->buf, tx_size);
737 port->icount.tx += ret;
738 tx_empty = 0;
739 }
740
741 priv->tx_empty = tx_empty;
742
743 if (tx_empty)
744 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
745
746 return PCH_UART_HANDLED_TX_INT;
747}
748
749static unsigned int dma_handle_tx(struct eg20t_port *priv)
750{
751 struct uart_port *port = &priv->port;
752 struct circ_buf *xmit = &port->state->xmit;
753 struct scatterlist *sg = &priv->sg_tx;
754 int nent;
755 int fifo_size;
756 int tx_empty;
757 struct dma_async_tx_descriptor *desc;
758
759 if (!priv->start_tx) {
760 pr_info("%s:Tx isn't started. (%lu)\n", __func__, jiffies);
761 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
762 priv->tx_empty = 1;
763 return 0;
764 }
765
766 fifo_size = max(priv->fifo_size, 1);
767 tx_empty = 1;
768 if (pop_tx_x(priv, xmit->buf)) {
769 pch_uart_hal_write(priv, xmit->buf, 1);
770 port->icount.tx++;
771 tx_empty = 0;
772 fifo_size--;
773 }
774
775 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
776
777 priv->tx_dma_use = 1;
778
779 sg_init_table(&priv->sg_tx, 1); /* Initialize SG table */
780
781 sg_set_page(&priv->sg_tx, virt_to_page(xmit->buf),
782 UART_XMIT_SIZE, (int)xmit->buf & ~PAGE_MASK);
783
784 nent = dma_map_sg(port->dev, &priv->sg_tx, 1, DMA_TO_DEVICE);
785 if (!nent) {
786 pr_err("%s:dma_map_sg Failed\n", __func__);
787 return 0;
788 }
789
790 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
791 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
792 sg->offset;
793 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail,
794 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
795 xmit->tail, UART_XMIT_SIZE));
796
797 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
798 sg, nent, DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
799 if (!desc) {
800 pr_err("%s:device_prep_slave_sg Failed\n", __func__);
801 return 0;
802 }
803
804 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
805
806 priv->desc_tx = desc;
807 desc->callback = pch_dma_tx_complete;
808 desc->callback_param = priv;
809
810 desc->tx_submit(desc);
811
812 dma_async_issue_pending(priv->chan_tx);
813
814 return PCH_UART_HANDLED_TX_INT;
815}
816
817static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
818{
819 u8 fcr = ioread8(priv->membase + UART_FCR);
820
821 /* Reset FIFO */
822 fcr |= UART_FCR_CLEAR_RCVR;
823 iowrite8(fcr, priv->membase + UART_FCR);
824
825 if (lsr & PCH_UART_LSR_ERR)
826 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
827
828 if (lsr & UART_LSR_FE)
829 dev_err(&priv->pdev->dev, "Framing Error\n");
830
831 if (lsr & UART_LSR_PE)
832 dev_err(&priv->pdev->dev, "Parity Error\n");
833
834 if (lsr & UART_LSR_OE)
835 dev_err(&priv->pdev->dev, "Overrun Error\n");
836}
837
838static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
839{
840 struct eg20t_port *priv = dev_id;
841 unsigned int handled;
842 u8 lsr;
843 int ret = 0;
844 unsigned int iid;
845 unsigned long flags;
846
847 spin_lock_irqsave(&priv->port.lock, flags);
848 handled = 0;
849 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
850 switch (iid) {
851 case PCH_UART_IID_RLS: /* Receiver Line Status */
852 lsr = pch_uart_hal_get_line_status(priv);
853 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
854 UART_LSR_PE | UART_LSR_OE)) {
855 pch_uart_err_ir(priv, lsr);
856 ret = PCH_UART_HANDLED_RX_ERR_INT;
857 }
858 break;
859 case PCH_UART_IID_RDR: /* Received Data Ready */
860 if (priv->use_dma)
861 ret = dma_handle_rx(priv);
862 else
863 ret = handle_rx(priv);
864 break;
865 case PCH_UART_IID_RDR_TO: /* Received Data Ready
866 (FIFO Timeout) */
867 ret = handle_rx_to(priv);
868 break;
869 case PCH_UART_IID_THRE: /* Transmitter Holding Register
870 Empty */
871 if (priv->use_dma)
872 ret = dma_handle_tx(priv);
873 else
874 ret = handle_tx(priv);
875 break;
876 case PCH_UART_IID_MS: /* Modem Status */
877 ret = PCH_UART_HANDLED_MS_INT;
878 break;
879 default: /* Never junp to this label */
880 pr_err("%s:iid=%d (%lu)\n", __func__, iid, jiffies);
881 ret = -1;
882 break;
883 }
884 handled |= (unsigned int)ret;
885 }
886 if (handled == 0 && iid <= 1) {
887 if (priv->int_dis_flag)
888 priv->int_dis_flag = 0;
889 }
890
891 spin_unlock_irqrestore(&priv->port.lock, flags);
892 return IRQ_RETVAL(handled);
893}
894
895/* This function tests whether the transmitter fifo and shifter for the port
896 described by 'port' is empty. */
897static unsigned int pch_uart_tx_empty(struct uart_port *port)
898{
899 struct eg20t_port *priv;
900 int ret;
901 priv = container_of(port, struct eg20t_port, port);
902 if (priv->tx_empty)
903 ret = TIOCSER_TEMT;
904 else
905 ret = 0;
906
907 return ret;
908}
909
910/* Returns the current state of modem control inputs. */
911static unsigned int pch_uart_get_mctrl(struct uart_port *port)
912{
913 struct eg20t_port *priv;
914 u8 modem;
915 unsigned int ret = 0;
916
917 priv = container_of(port, struct eg20t_port, port);
918 modem = pch_uart_hal_get_modem(priv);
919
920 if (modem & UART_MSR_DCD)
921 ret |= TIOCM_CAR;
922
923 if (modem & UART_MSR_RI)
924 ret |= TIOCM_RNG;
925
926 if (modem & UART_MSR_DSR)
927 ret |= TIOCM_DSR;
928
929 if (modem & UART_MSR_CTS)
930 ret |= TIOCM_CTS;
931
932 return ret;
933}
934
935static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
936{
937 u32 mcr = 0;
938 unsigned int dat;
939 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
940
941 if (mctrl & TIOCM_DTR)
942 mcr |= UART_MCR_DTR;
943 if (mctrl & TIOCM_RTS)
944 mcr |= UART_MCR_RTS;
945 if (mctrl & TIOCM_LOOP)
946 mcr |= UART_MCR_LOOP;
947
948 if (mctrl) {
949 dat = pch_uart_get_mctrl(port);
950 dat |= mcr;
951 iowrite8(dat, priv->membase + UART_MCR);
952 }
953}
954
955static void pch_uart_stop_tx(struct uart_port *port)
956{
957 struct eg20t_port *priv;
958 priv = container_of(port, struct eg20t_port, port);
959 priv->start_tx = 0;
960 priv->tx_dma_use = 0;
961}
962
963static void pch_uart_start_tx(struct uart_port *port)
964{
965 struct eg20t_port *priv;
966
967 priv = container_of(port, struct eg20t_port, port);
968
969 if (priv->use_dma)
970 if (priv->tx_dma_use)
971 return;
972
973 priv->start_tx = 1;
974 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
975}
976
977static void pch_uart_stop_rx(struct uart_port *port)
978{
979 struct eg20t_port *priv;
980 priv = container_of(port, struct eg20t_port, port);
981 priv->start_rx = 0;
982 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
983 priv->int_dis_flag = 1;
984}
985
986/* Enable the modem status interrupts. */
987static void pch_uart_enable_ms(struct uart_port *port)
988{
989 struct eg20t_port *priv;
990 priv = container_of(port, struct eg20t_port, port);
991 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
992}
993
994/* Control the transmission of a break signal. */
995static void pch_uart_break_ctl(struct uart_port *port, int ctl)
996{
997 struct eg20t_port *priv;
998 unsigned long flags;
999
1000 priv = container_of(port, struct eg20t_port, port);
1001 spin_lock_irqsave(&port->lock, flags);
1002 pch_uart_hal_set_break(priv, ctl);
1003 spin_unlock_irqrestore(&port->lock, flags);
1004}
1005
1006/* Grab any interrupt resources and initialise any low level driver state. */
1007static int pch_uart_startup(struct uart_port *port)
1008{
1009 struct eg20t_port *priv;
1010 int ret;
1011 int fifo_size;
1012 int trigger_level;
1013
1014 priv = container_of(port, struct eg20t_port, port);
1015 priv->tx_empty = 1;
1016 port->uartclk = priv->base_baud;
1017 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1018 ret = pch_uart_hal_set_line(priv, default_baud,
1019 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1020 PCH_UART_HAL_STB1);
1021 if (ret)
1022 return ret;
1023
1024 switch (priv->fifo_size) {
1025 case 256:
1026 fifo_size = PCH_UART_HAL_FIFO256;
1027 break;
1028 case 64:
1029 fifo_size = PCH_UART_HAL_FIFO64;
1030 break;
1031 case 16:
1032 fifo_size = PCH_UART_HAL_FIFO16;
1033 case 1:
1034 default:
1035 fifo_size = PCH_UART_HAL_FIFO_DIS;
1036 break;
1037 }
1038
1039 switch (priv->trigger) {
1040 case PCH_UART_HAL_TRIGGER1:
1041 trigger_level = 1;
1042 break;
1043 case PCH_UART_HAL_TRIGGER_L:
1044 trigger_level = priv->fifo_size / 4;
1045 break;
1046 case PCH_UART_HAL_TRIGGER_M:
1047 trigger_level = priv->fifo_size / 2;
1048 break;
1049 case PCH_UART_HAL_TRIGGER_H:
1050 default:
1051 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1052 break;
1053 }
1054
1055 priv->trigger_level = trigger_level;
1056 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1057 fifo_size, priv->trigger);
1058 if (ret < 0)
1059 return ret;
1060
1061 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1062 KBUILD_MODNAME, priv);
1063 if (ret < 0)
1064 return ret;
1065
1066 if (priv->use_dma)
1067 pch_request_dma(port);
1068
1069 priv->start_rx = 1;
1070 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1071 uart_update_timeout(port, CS8, default_baud);
1072
1073 return 0;
1074}
1075
1076static void pch_uart_shutdown(struct uart_port *port)
1077{
1078 struct eg20t_port *priv;
1079 int ret;
1080
1081 priv = container_of(port, struct eg20t_port, port);
1082 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1083 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1084 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1085 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1086 if (ret)
1087 pr_err("pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1088
1089 if (priv->use_dma_flag)
1090 pch_free_dma(port);
1091
1092 free_irq(priv->port.irq, priv);
1093}
1094
1095/* Change the port parameters, including word length, parity, stop
1096 *bits. Update read_status_mask and ignore_status_mask to indicate
1097 *the types of events we are interested in receiving. */
1098static void pch_uart_set_termios(struct uart_port *port,
1099 struct ktermios *termios, struct ktermios *old)
1100{
1101 int baud;
1102 int rtn;
1103 unsigned int parity, bits, stb;
1104 struct eg20t_port *priv;
1105 unsigned long flags;
1106
1107 priv = container_of(port, struct eg20t_port, port);
1108 switch (termios->c_cflag & CSIZE) {
1109 case CS5:
1110 bits = PCH_UART_HAL_5BIT;
1111 break;
1112 case CS6:
1113 bits = PCH_UART_HAL_6BIT;
1114 break;
1115 case CS7:
1116 bits = PCH_UART_HAL_7BIT;
1117 break;
1118 default: /* CS8 */
1119 bits = PCH_UART_HAL_8BIT;
1120 break;
1121 }
1122 if (termios->c_cflag & CSTOPB)
1123 stb = PCH_UART_HAL_STB2;
1124 else
1125 stb = PCH_UART_HAL_STB1;
1126
1127 if (termios->c_cflag & PARENB) {
1128 if (!(termios->c_cflag & PARODD))
1129 parity = PCH_UART_HAL_PARITY_ODD;
1130 else
1131 parity = PCH_UART_HAL_PARITY_EVEN;
1132
1133 } else {
1134 parity = PCH_UART_HAL_PARITY_NONE;
1135 }
1136 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1137
1138 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1139
1140 spin_lock_irqsave(&port->lock, flags);
1141
1142 uart_update_timeout(port, termios->c_cflag, baud);
1143 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1144 if (rtn)
1145 goto out;
1146
1147 /* Don't rewrite B0 */
1148 if (tty_termios_baud_rate(termios))
1149 tty_termios_encode_baud_rate(termios, baud, baud);
1150
1151out:
1152 spin_unlock_irqrestore(&port->lock, flags);
1153}
1154
1155static const char *pch_uart_type(struct uart_port *port)
1156{
1157 return KBUILD_MODNAME;
1158}
1159
1160static void pch_uart_release_port(struct uart_port *port)
1161{
1162 struct eg20t_port *priv;
1163
1164 priv = container_of(port, struct eg20t_port, port);
1165 pci_iounmap(priv->pdev, priv->membase);
1166 pci_release_regions(priv->pdev);
1167}
1168
1169static int pch_uart_request_port(struct uart_port *port)
1170{
1171 struct eg20t_port *priv;
1172 int ret;
1173 void __iomem *membase;
1174
1175 priv = container_of(port, struct eg20t_port, port);
1176 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1177 if (ret < 0)
1178 return -EBUSY;
1179
1180 membase = pci_iomap(priv->pdev, 1, 0);
1181 if (!membase) {
1182 pci_release_regions(priv->pdev);
1183 return -EBUSY;
1184 }
1185 priv->membase = port->membase = membase;
1186
1187 return 0;
1188}
1189
1190static void pch_uart_config_port(struct uart_port *port, int type)
1191{
1192 struct eg20t_port *priv;
1193
1194 priv = container_of(port, struct eg20t_port, port);
1195 if (type & UART_CONFIG_TYPE) {
1196 port->type = priv->port_type;
1197 pch_uart_request_port(port);
1198 }
1199}
1200
1201static int pch_uart_verify_port(struct uart_port *port,
1202 struct serial_struct *serinfo)
1203{
1204 struct eg20t_port *priv;
1205
1206 priv = container_of(port, struct eg20t_port, port);
1207 if (serinfo->flags & UPF_LOW_LATENCY) {
1208 pr_info("PCH UART : Use PIO Mode (without DMA)\n");
1209 priv->use_dma = 0;
1210 serinfo->flags &= ~UPF_LOW_LATENCY;
1211 } else {
1212#ifndef CONFIG_PCH_DMA
1213 pr_err("%s : PCH DMA is not Loaded.\n", __func__);
1214 return -EOPNOTSUPP;
1215#endif
1216 priv->use_dma = 1;
1217 priv->use_dma_flag = 1;
1218 pr_info("PCH UART : Use DMA Mode\n");
1219 }
1220
1221 return 0;
1222}
1223
1224static struct uart_ops pch_uart_ops = {
1225 .tx_empty = pch_uart_tx_empty,
1226 .set_mctrl = pch_uart_set_mctrl,
1227 .get_mctrl = pch_uart_get_mctrl,
1228 .stop_tx = pch_uart_stop_tx,
1229 .start_tx = pch_uart_start_tx,
1230 .stop_rx = pch_uart_stop_rx,
1231 .enable_ms = pch_uart_enable_ms,
1232 .break_ctl = pch_uart_break_ctl,
1233 .startup = pch_uart_startup,
1234 .shutdown = pch_uart_shutdown,
1235 .set_termios = pch_uart_set_termios,
1236/* .pm = pch_uart_pm, Not supported yet */
1237/* .set_wake = pch_uart_set_wake, Not supported yet */
1238 .type = pch_uart_type,
1239 .release_port = pch_uart_release_port,
1240 .request_port = pch_uart_request_port,
1241 .config_port = pch_uart_config_port,
1242 .verify_port = pch_uart_verify_port
1243};
1244
1245static struct uart_driver pch_uart_driver = {
1246 .owner = THIS_MODULE,
1247 .driver_name = KBUILD_MODNAME,
1248 .dev_name = PCH_UART_DRIVER_DEVICE,
1249 .major = 0,
1250 .minor = 0,
1251 .nr = PCH_UART_NR,
1252};
1253
1254static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001255 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001256{
1257 struct eg20t_port *priv;
1258 int ret;
1259 unsigned int iobase;
1260 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001261 unsigned char *rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001262 int fifosize, base_baud;
1263 static int num;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001264 int port_type = id->driver_data;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001265
1266 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1267 if (priv == NULL)
1268 goto init_port_alloc_err;
1269
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001270 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001271 if (!rxbuf)
1272 goto init_port_free_txbuf;
1273
1274 switch (port_type) {
1275 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001276 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001277 base_baud = 1843200; /* 1.8432MHz */
1278 break;
1279 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001280 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001281 base_baud = 1843200; /* 1.8432MHz */
1282 break;
1283 default:
1284 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1285 goto init_port_hal_free;
1286 }
1287
1288 iobase = pci_resource_start(pdev, 0);
1289 mapbase = pci_resource_start(pdev, 1);
1290 priv->mapbase = mapbase;
1291 priv->iobase = iobase;
1292 priv->pdev = pdev;
1293 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001294 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001295 priv->rxbuf.size = PAGE_SIZE;
1296
1297 priv->fifo_size = fifosize;
1298 priv->base_baud = base_baud;
1299 priv->port_type = PORT_MAX_8250 + port_type + 1;
1300 priv->port.dev = &pdev->dev;
1301 priv->port.iobase = iobase;
1302 priv->port.membase = NULL;
1303 priv->port.mapbase = mapbase;
1304 priv->port.irq = pdev->irq;
1305 priv->port.iotype = UPIO_PORT;
1306 priv->port.ops = &pch_uart_ops;
1307 priv->port.flags = UPF_BOOT_AUTOCONF;
1308 priv->port.fifosize = fifosize;
1309 priv->port.line = num++;
1310 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1311
1312 pci_set_drvdata(pdev, priv);
1313 pch_uart_hal_request(pdev, fifosize, base_baud);
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001314
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001315 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1316 if (ret < 0)
1317 goto init_port_hal_free;
1318
1319 return priv;
1320
1321init_port_hal_free:
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001322 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001323init_port_free_txbuf:
1324 kfree(priv);
1325init_port_alloc_err:
1326
1327 return NULL;
1328}
1329
1330static void pch_uart_exit_port(struct eg20t_port *priv)
1331{
1332 uart_remove_one_port(&pch_uart_driver, &priv->port);
1333 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001334 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001335}
1336
1337static void pch_uart_pci_remove(struct pci_dev *pdev)
1338{
1339 struct eg20t_port *priv;
1340
1341 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
1342 pch_uart_exit_port(priv);
1343 pci_disable_device(pdev);
1344 kfree(priv);
1345 return;
1346}
1347#ifdef CONFIG_PM
1348static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1349{
1350 struct eg20t_port *priv = pci_get_drvdata(pdev);
1351
1352 uart_suspend_port(&pch_uart_driver, &priv->port);
1353
1354 pci_save_state(pdev);
1355 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1356 return 0;
1357}
1358
1359static int pch_uart_pci_resume(struct pci_dev *pdev)
1360{
1361 struct eg20t_port *priv = pci_get_drvdata(pdev);
1362 int ret;
1363
1364 pci_set_power_state(pdev, PCI_D0);
1365 pci_restore_state(pdev);
1366
1367 ret = pci_enable_device(pdev);
1368 if (ret) {
1369 dev_err(&pdev->dev,
1370 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1371 return ret;
1372 }
1373
1374 uart_resume_port(&pch_uart_driver, &priv->port);
1375
1376 return 0;
1377}
1378#else
1379#define pch_uart_pci_suspend NULL
1380#define pch_uart_pci_resume NULL
1381#endif
1382
1383static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1384 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1385 .driver_data = PCH_UART_8LINE},
1386 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1387 .driver_data = PCH_UART_2LINE},
1388 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1389 .driver_data = PCH_UART_2LINE},
1390 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1391 .driver_data = PCH_UART_2LINE},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001392 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1393 .driver_data = PCH_UART_8LINE},
1394 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1395 .driver_data = PCH_UART_2LINE},
1396 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1397 .driver_data = PCH_UART_2LINE},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001398 {0,},
1399};
1400
1401static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1402 const struct pci_device_id *id)
1403{
1404 int ret;
1405 struct eg20t_port *priv;
1406
1407 ret = pci_enable_device(pdev);
1408 if (ret < 0)
1409 goto probe_error;
1410
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001411 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001412 if (!priv) {
1413 ret = -EBUSY;
1414 goto probe_disable_device;
1415 }
1416 pci_set_drvdata(pdev, priv);
1417
1418 return ret;
1419
1420probe_disable_device:
1421 pci_disable_device(pdev);
1422probe_error:
1423 return ret;
1424}
1425
1426static struct pci_driver pch_uart_pci_driver = {
1427 .name = "pch_uart",
1428 .id_table = pch_uart_pci_id,
1429 .probe = pch_uart_pci_probe,
1430 .remove = __devexit_p(pch_uart_pci_remove),
1431 .suspend = pch_uart_pci_suspend,
1432 .resume = pch_uart_pci_resume,
1433};
1434
1435static int __init pch_uart_module_init(void)
1436{
1437 int ret;
1438
1439 /* register as UART driver */
1440 ret = uart_register_driver(&pch_uart_driver);
1441 if (ret < 0)
1442 return ret;
1443
1444 /* register as PCI driver */
1445 ret = pci_register_driver(&pch_uart_pci_driver);
1446 if (ret < 0)
1447 uart_unregister_driver(&pch_uart_driver);
1448
1449 return ret;
1450}
1451module_init(pch_uart_module_init);
1452
1453static void __exit pch_uart_module_exit(void)
1454{
1455 pci_unregister_driver(&pch_uart_pci_driver);
1456 uart_unregister_driver(&pch_uart_driver);
1457}
1458module_exit(pch_uart_module_exit);
1459
1460MODULE_LICENSE("GPL v2");
1461MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1462module_param(default_baud, uint, S_IRUGO);