blob: b950d059a78141d90697a1a951c3f3401c75af6c [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 */
Uwe Kleine-König0e2adc02011-05-26 10:41:17 +020017#include <linux/kernel.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090018#include <linux/serial_reg.h>
Andrew Morton023bc8e2011-05-24 17:13:44 -070019#include <linux/slab.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090020#include <linux/module.h>
21#include <linux/pci.h>
22#include <linux/serial_core.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020023#include <linux/tty.h>
24#include <linux/tty_flip.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090025#include <linux/interrupt.h>
26#include <linux/io.h>
Denis Turischev6ae705b2011-03-10 15:14:00 +020027#include <linux/dmi.h>
Alexander Steine30f8672011-11-15 15:04:07 -080028#include <linux/console.h>
29#include <linux/nmi.h>
30#include <linux/delay.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090031
32#include <linux/dmaengine.h>
33#include <linux/pch_dma.h>
34
35enum {
36 PCH_UART_HANDLED_RX_INT_SHIFT,
37 PCH_UART_HANDLED_TX_INT_SHIFT,
38 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
39 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
40 PCH_UART_HANDLED_MS_INT_SHIFT,
41};
42
43enum {
44 PCH_UART_8LINE,
45 PCH_UART_2LINE,
46};
47
48#define PCH_UART_DRIVER_DEVICE "ttyPCH"
49
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +090050/* Set the max number of UART port
51 * Intel EG20T PCH: 4 port
52 * OKI SEMICONDUCTOR ML7213 IOH: 3 port
Alexander Steinbff52fd2011-07-04 15:35:51 +020053 * OKI SEMICONDUCTOR ML7223 IOH: 2 port
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +090054*/
55#define PCH_UART_NR 4
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090056
57#define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
58#define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
59#define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
60 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
61#define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
62 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
63#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
64
65#define PCH_UART_RBR 0x00
66#define PCH_UART_THR 0x00
67
68#define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
69 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
70#define PCH_UART_IER_ERBFI 0x00000001
71#define PCH_UART_IER_ETBEI 0x00000002
72#define PCH_UART_IER_ELSI 0x00000004
73#define PCH_UART_IER_EDSSI 0x00000008
74
75#define PCH_UART_IIR_IP 0x00000001
76#define PCH_UART_IIR_IID 0x00000006
77#define PCH_UART_IIR_MSI 0x00000000
78#define PCH_UART_IIR_TRI 0x00000002
79#define PCH_UART_IIR_RRI 0x00000004
80#define PCH_UART_IIR_REI 0x00000006
81#define PCH_UART_IIR_TOI 0x00000008
82#define PCH_UART_IIR_FIFO256 0x00000020
83#define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
84#define PCH_UART_IIR_FE 0x000000C0
85
86#define PCH_UART_FCR_FIFOE 0x00000001
87#define PCH_UART_FCR_RFR 0x00000002
88#define PCH_UART_FCR_TFR 0x00000004
89#define PCH_UART_FCR_DMS 0x00000008
90#define PCH_UART_FCR_FIFO256 0x00000020
91#define PCH_UART_FCR_RFTL 0x000000C0
92
93#define PCH_UART_FCR_RFTL1 0x00000000
94#define PCH_UART_FCR_RFTL64 0x00000040
95#define PCH_UART_FCR_RFTL128 0x00000080
96#define PCH_UART_FCR_RFTL224 0x000000C0
97#define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
98#define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
99#define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
100#define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
101#define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
102#define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
103#define PCH_UART_FCR_RFTL_SHIFT 6
104
105#define PCH_UART_LCR_WLS 0x00000003
106#define PCH_UART_LCR_STB 0x00000004
107#define PCH_UART_LCR_PEN 0x00000008
108#define PCH_UART_LCR_EPS 0x00000010
109#define PCH_UART_LCR_SP 0x00000020
110#define PCH_UART_LCR_SB 0x00000040
111#define PCH_UART_LCR_DLAB 0x00000080
112#define PCH_UART_LCR_NP 0x00000000
113#define PCH_UART_LCR_OP PCH_UART_LCR_PEN
114#define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
115#define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
116#define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
117 PCH_UART_LCR_SP)
118
119#define PCH_UART_LCR_5BIT 0x00000000
120#define PCH_UART_LCR_6BIT 0x00000001
121#define PCH_UART_LCR_7BIT 0x00000002
122#define PCH_UART_LCR_8BIT 0x00000003
123
124#define PCH_UART_MCR_DTR 0x00000001
125#define PCH_UART_MCR_RTS 0x00000002
126#define PCH_UART_MCR_OUT 0x0000000C
127#define PCH_UART_MCR_LOOP 0x00000010
128#define PCH_UART_MCR_AFE 0x00000020
129
130#define PCH_UART_LSR_DR 0x00000001
131#define PCH_UART_LSR_ERR (1<<7)
132
133#define PCH_UART_MSR_DCTS 0x00000001
134#define PCH_UART_MSR_DDSR 0x00000002
135#define PCH_UART_MSR_TERI 0x00000004
136#define PCH_UART_MSR_DDCD 0x00000008
137#define PCH_UART_MSR_CTS 0x00000010
138#define PCH_UART_MSR_DSR 0x00000020
139#define PCH_UART_MSR_RI 0x00000040
140#define PCH_UART_MSR_DCD 0x00000080
141#define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
142 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
143
144#define PCH_UART_DLL 0x00
145#define PCH_UART_DLM 0x01
146
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900147#define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
148#define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
149#define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
150#define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
151#define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
152
153#define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
154#define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
155#define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
156#define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
157#define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
158#define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
159#define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
160#define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
161#define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
162#define PCH_UART_HAL_STB1 0
163#define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
164
165#define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
166#define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
167#define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
168 PCH_UART_HAL_CLR_RX_FIFO)
169
170#define PCH_UART_HAL_DMA_MODE0 0
171#define PCH_UART_HAL_FIFO_DIS 0
172#define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
173#define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
174 PCH_UART_FCR_FIFO256)
175#define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
176#define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
177#define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
178#define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
179#define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
180#define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
181#define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
182#define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
183#define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
184#define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
185#define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
186#define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
187#define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
188#define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
189
190#define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
191#define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
192#define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
193#define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
194#define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
195
196#define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
197#define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
198#define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
199#define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
200#define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
201
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +0900202#define PCI_VENDOR_ID_ROHM 0x10DB
203
Alexander Steine30f8672011-11-15 15:04:07 -0800204#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
205
206#define DEFAULT_BAUD_RATE 1843200 /* 1.8432MHz */
207
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900208struct pch_uart_buffer {
209 unsigned char *buf;
210 int size;
211};
212
213struct eg20t_port {
214 struct uart_port port;
215 int port_type;
216 void __iomem *membase;
217 resource_size_t mapbase;
218 unsigned int iobase;
219 struct pci_dev *pdev;
220 int fifo_size;
221 int base_baud;
222 int start_tx;
223 int start_rx;
224 int tx_empty;
225 int int_dis_flag;
226 int trigger;
227 int trigger_level;
228 struct pch_uart_buffer rxbuf;
229 unsigned int dmsr;
230 unsigned int fcr;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +0900231 unsigned int mcr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900232 unsigned int use_dma;
233 unsigned int use_dma_flag;
234 struct dma_async_tx_descriptor *desc_tx;
235 struct dma_async_tx_descriptor *desc_rx;
236 struct pch_dma_slave param_tx;
237 struct pch_dma_slave param_rx;
238 struct dma_chan *chan_tx;
239 struct dma_chan *chan_rx;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900240 struct scatterlist *sg_tx_p;
241 int nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900242 struct scatterlist sg_rx;
243 int tx_dma_use;
244 void *rx_buf_virt;
245 dma_addr_t rx_buf_dma;
246};
247
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900248/**
249 * struct pch_uart_driver_data - private data structure for UART-DMA
250 * @port_type: The number of DMA channel
251 * @line_no: UART port line number (0, 1, 2...)
252 */
253struct pch_uart_driver_data {
254 int port_type;
255 int line_no;
256};
257
258enum pch_uart_num_t {
259 pch_et20t_uart0 = 0,
260 pch_et20t_uart1,
261 pch_et20t_uart2,
262 pch_et20t_uart3,
263 pch_ml7213_uart0,
264 pch_ml7213_uart1,
265 pch_ml7213_uart2,
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900266 pch_ml7223_uart0,
267 pch_ml7223_uart1,
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900268};
269
270static struct pch_uart_driver_data drv_dat[] = {
271 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
272 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
273 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
274 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
275 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
276 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
277 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900278 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
279 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900280};
281
Alexander Steine30f8672011-11-15 15:04:07 -0800282#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
283static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
284#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900285static unsigned int default_baud = 9600;
286static const int trigger_level_256[4] = { 1, 64, 128, 224 };
287static const int trigger_level_64[4] = { 1, 16, 32, 56 };
288static const int trigger_level_16[4] = { 1, 4, 8, 14 };
289static const int trigger_level_1[4] = { 1, 1, 1, 1 };
290
291static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
292 int base_baud)
293{
294 struct eg20t_port *priv = pci_get_drvdata(pdev);
295
296 priv->trigger_level = 1;
297 priv->fcr = 0;
298}
299
300static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
301{
302 unsigned int msr = ioread8(base + UART_MSR);
303 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
304
305 return msr;
306}
307
308static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
309 unsigned int flag)
310{
311 u8 ier = ioread8(priv->membase + UART_IER);
312 ier |= flag & PCH_UART_IER_MASK;
313 iowrite8(ier, priv->membase + UART_IER);
314}
315
316static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
317 unsigned int flag)
318{
319 u8 ier = ioread8(priv->membase + UART_IER);
320 ier &= ~(flag & PCH_UART_IER_MASK);
321 iowrite8(ier, priv->membase + UART_IER);
322}
323
324static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
325 unsigned int parity, unsigned int bits,
326 unsigned int stb)
327{
328 unsigned int dll, dlm, lcr;
329 int div;
330
Uwe Kleine-König0e2adc02011-05-26 10:41:17 +0200331 div = DIV_ROUND_CLOSEST(priv->base_baud / 16, baud);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900332 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900333 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900334 return -EINVAL;
335 }
336
337 dll = (unsigned int)div & 0x00FFU;
338 dlm = ((unsigned int)div >> 8) & 0x00FFU;
339
340 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900341 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900342 return -EINVAL;
343 }
344
345 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900346 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900347 return -EINVAL;
348 }
349
350 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900351 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900352 return -EINVAL;
353 }
354
355 lcr = parity;
356 lcr |= bits;
357 lcr |= stb;
358
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900359 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900360 __func__, baud, div, lcr, jiffies);
361 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
362 iowrite8(dll, priv->membase + PCH_UART_DLL);
363 iowrite8(dlm, priv->membase + PCH_UART_DLM);
364 iowrite8(lcr, priv->membase + UART_LCR);
365
366 return 0;
367}
368
369static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
370 unsigned int flag)
371{
372 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900373 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
374 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900375 return -EINVAL;
376 }
377
378 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
379 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
380 priv->membase + UART_FCR);
381 iowrite8(priv->fcr, priv->membase + UART_FCR);
382
383 return 0;
384}
385
386static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
387 unsigned int dmamode,
388 unsigned int fifo_size, unsigned int trigger)
389{
390 u8 fcr;
391
392 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900393 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
394 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900395 return -EINVAL;
396 }
397
398 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900399 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
400 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900401 return -EINVAL;
402 }
403
404 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900405 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
406 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900407 return -EINVAL;
408 }
409
410 switch (priv->fifo_size) {
411 case 256:
412 priv->trigger_level =
413 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
414 break;
415 case 64:
416 priv->trigger_level =
417 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
418 break;
419 case 16:
420 priv->trigger_level =
421 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
422 break;
423 default:
424 priv->trigger_level =
425 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
426 break;
427 }
428 fcr =
429 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
430 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
431 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
432 priv->membase + UART_FCR);
433 iowrite8(fcr, priv->membase + UART_FCR);
434 priv->fcr = fcr;
435
436 return 0;
437}
438
439static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
440{
441 priv->dmsr = 0;
442 return get_msr(priv, priv->membase);
443}
444
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900445static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900446 const unsigned char *buf, int tx_size)
447{
448 int i;
449 unsigned int thr;
450
451 for (i = 0; i < tx_size;) {
452 thr = buf[i++];
453 iowrite8(thr, priv->membase + PCH_UART_THR);
454 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900455}
456
457static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
458 int rx_size)
459{
460 int i;
461 u8 rbr, lsr;
462
463 lsr = ioread8(priv->membase + UART_LSR);
464 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
465 i < rx_size && lsr & UART_LSR_DR;
466 lsr = ioread8(priv->membase + UART_LSR)) {
467 rbr = ioread8(priv->membase + PCH_UART_RBR);
468 buf[i++] = rbr;
469 }
470 return i;
471}
472
473static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
474{
475 unsigned int iir;
476 int ret;
477
478 iir = ioread8(priv->membase + UART_IIR);
479 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
480 return ret;
481}
482
483static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
484{
485 return ioread8(priv->membase + UART_LSR);
486}
487
488static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
489{
490 unsigned int lcr;
491
492 lcr = ioread8(priv->membase + UART_LCR);
493 if (on)
494 lcr |= PCH_UART_LCR_SB;
495 else
496 lcr &= ~PCH_UART_LCR_SB;
497
498 iowrite8(lcr, priv->membase + UART_LCR);
499}
500
501static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
502 int size)
503{
504 struct uart_port *port;
505 struct tty_struct *tty;
506
507 port = &priv->port;
508 tty = tty_port_tty_get(&port->state->port);
509 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900510 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900511 return -EBUSY;
512 }
513
514 tty_insert_flip_string(tty, buf, size);
515 tty_flip_buffer_push(tty);
516 tty_kref_put(tty);
517
518 return 0;
519}
520
521static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
522{
523 int ret;
524 struct uart_port *port = &priv->port;
525
526 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900527 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
528 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900529 buf[0] = port->x_char;
530 port->x_char = 0;
531 ret = 1;
532 } else {
533 ret = 0;
534 }
535
536 return ret;
537}
538
539static int dma_push_rx(struct eg20t_port *priv, int size)
540{
541 struct tty_struct *tty;
542 int room;
543 struct uart_port *port = &priv->port;
544
545 port = &priv->port;
546 tty = tty_port_tty_get(&port->state->port);
547 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900548 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900549 return 0;
550 }
551
552 room = tty_buffer_request_room(tty, size);
553
554 if (room < size)
555 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
556 size - room);
557 if (!room)
558 return room;
559
560 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
561
562 port->icount.rx += room;
563 tty_kref_put(tty);
564
565 return room;
566}
567
568static void pch_free_dma(struct uart_port *port)
569{
570 struct eg20t_port *priv;
571 priv = container_of(port, struct eg20t_port, port);
572
573 if (priv->chan_tx) {
574 dma_release_channel(priv->chan_tx);
575 priv->chan_tx = NULL;
576 }
577 if (priv->chan_rx) {
578 dma_release_channel(priv->chan_rx);
579 priv->chan_rx = NULL;
580 }
581 if (sg_dma_address(&priv->sg_rx))
582 dma_free_coherent(port->dev, port->fifosize,
583 sg_virt(&priv->sg_rx),
584 sg_dma_address(&priv->sg_rx));
585
586 return;
587}
588
589static bool filter(struct dma_chan *chan, void *slave)
590{
591 struct pch_dma_slave *param = slave;
592
593 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
594 chan->device->dev)) {
595 chan->private = param;
596 return true;
597 } else {
598 return false;
599 }
600}
601
602static void pch_request_dma(struct uart_port *port)
603{
604 dma_cap_mask_t mask;
605 struct dma_chan *chan;
606 struct pci_dev *dma_dev;
607 struct pch_dma_slave *param;
608 struct eg20t_port *priv =
609 container_of(port, struct eg20t_port, port);
610 dma_cap_zero(mask);
611 dma_cap_set(DMA_SLAVE, mask);
612
Tomoya MORINAGA6c4b47d2011-07-20 20:17:49 +0900613 dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
614 PCI_DEVFN(0xa, 0)); /* Get DMA's dev
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900615 information */
616 /* Set Tx DMA */
617 param = &priv->param_tx;
618 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900619 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
620
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900621 param->tx_reg = port->mapbase + UART_TX;
622 chan = dma_request_channel(mask, filter, param);
623 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900624 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
625 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900626 return;
627 }
628 priv->chan_tx = chan;
629
630 /* Set Rx DMA */
631 param = &priv->param_rx;
632 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900633 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
634
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900635 param->rx_reg = port->mapbase + UART_RX;
636 chan = dma_request_channel(mask, filter, param);
637 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900638 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
639 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900640 dma_release_channel(priv->chan_tx);
641 return;
642 }
643
644 /* Get Consistent memory for DMA */
645 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
646 &priv->rx_buf_dma, GFP_KERNEL);
647 priv->chan_rx = chan;
648}
649
650static void pch_dma_rx_complete(void *arg)
651{
652 struct eg20t_port *priv = arg;
653 struct uart_port *port = &priv->port;
654 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900655 int count;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900656
657 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900658 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900659 return;
660 }
661
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900662 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
663 count = dma_push_rx(priv, priv->trigger_level);
664 if (count)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900665 tty_flip_buffer_push(tty);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900666 tty_kref_put(tty);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900667 async_tx_ack(priv->desc_rx);
668 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900669}
670
671static void pch_dma_tx_complete(void *arg)
672{
673 struct eg20t_port *priv = arg;
674 struct uart_port *port = &priv->port;
675 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900676 struct scatterlist *sg = priv->sg_tx_p;
677 int i;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900678
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900679 for (i = 0; i < priv->nent; i++, sg++) {
680 xmit->tail += sg_dma_len(sg);
681 port->icount.tx += sg_dma_len(sg);
682 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900683 xmit->tail &= UART_XMIT_SIZE - 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900684 async_tx_ack(priv->desc_tx);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900685 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900686 priv->tx_dma_use = 0;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900687 priv->nent = 0;
688 kfree(priv->sg_tx_p);
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900689 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900690}
691
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900692static int pop_tx(struct eg20t_port *priv, int size)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900693{
694 int count = 0;
695 struct uart_port *port = &priv->port;
696 struct circ_buf *xmit = &port->state->xmit;
697
698 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
699 goto pop_tx_end;
700
701 do {
702 int cnt_to_end =
703 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
704 int sz = min(size - count, cnt_to_end);
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900705 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900706 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
707 count += sz;
708 } while (!uart_circ_empty(xmit) && count < size);
709
710pop_tx_end:
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900711 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900712 count, size - count, jiffies);
713
714 return count;
715}
716
717static int handle_rx_to(struct eg20t_port *priv)
718{
719 struct pch_uart_buffer *buf;
720 int rx_size;
721 int ret;
722 if (!priv->start_rx) {
723 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
724 return 0;
725 }
726 buf = &priv->rxbuf;
727 do {
728 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
729 ret = push_rx(priv, buf->buf, rx_size);
730 if (ret)
731 return 0;
732 } while (rx_size == buf->size);
733
734 return PCH_UART_HANDLED_RX_INT;
735}
736
737static int handle_rx(struct eg20t_port *priv)
738{
739 return handle_rx_to(priv);
740}
741
742static int dma_handle_rx(struct eg20t_port *priv)
743{
744 struct uart_port *port = &priv->port;
745 struct dma_async_tx_descriptor *desc;
746 struct scatterlist *sg;
747
748 priv = container_of(port, struct eg20t_port, port);
749 sg = &priv->sg_rx;
750
751 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
752
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900753 sg_dma_len(sg) = priv->trigger_level;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900754
755 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900756 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
757 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900758
759 sg_dma_address(sg) = priv->rx_buf_dma;
760
761 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
762 sg, 1, DMA_FROM_DEVICE,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900763 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
764
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900765 if (!desc)
766 return 0;
767
768 priv->desc_rx = desc;
769 desc->callback = pch_dma_rx_complete;
770 desc->callback_param = priv;
771 desc->tx_submit(desc);
772 dma_async_issue_pending(priv->chan_rx);
773
774 return PCH_UART_HANDLED_RX_INT;
775}
776
777static unsigned int handle_tx(struct eg20t_port *priv)
778{
779 struct uart_port *port = &priv->port;
780 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900781 int fifo_size;
782 int tx_size;
783 int size;
784 int tx_empty;
785
786 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900787 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
788 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900789 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
790 priv->tx_empty = 1;
791 return 0;
792 }
793
794 fifo_size = max(priv->fifo_size, 1);
795 tx_empty = 1;
796 if (pop_tx_x(priv, xmit->buf)) {
797 pch_uart_hal_write(priv, xmit->buf, 1);
798 port->icount.tx++;
799 tx_empty = 0;
800 fifo_size--;
801 }
802 size = min(xmit->head - xmit->tail, fifo_size);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900803 if (size < 0)
804 size = fifo_size;
805
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900806 tx_size = pop_tx(priv, size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900807 if (tx_size > 0) {
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900808 port->icount.tx += tx_size;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900809 tx_empty = 0;
810 }
811
812 priv->tx_empty = tx_empty;
813
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900814 if (tx_empty) {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900815 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900816 uart_write_wakeup(port);
817 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900818
819 return PCH_UART_HANDLED_TX_INT;
820}
821
822static unsigned int dma_handle_tx(struct eg20t_port *priv)
823{
824 struct uart_port *port = &priv->port;
825 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900826 struct scatterlist *sg;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900827 int nent;
828 int fifo_size;
829 int tx_empty;
830 struct dma_async_tx_descriptor *desc;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900831 int num;
832 int i;
833 int bytes;
834 int size;
835 int rem;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900836
837 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900838 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
839 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900840 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
841 priv->tx_empty = 1;
842 return 0;
843 }
844
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900845 if (priv->tx_dma_use) {
846 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
847 __func__, jiffies);
848 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
849 priv->tx_empty = 1;
850 return 0;
851 }
852
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900853 fifo_size = max(priv->fifo_size, 1);
854 tx_empty = 1;
855 if (pop_tx_x(priv, xmit->buf)) {
856 pch_uart_hal_write(priv, xmit->buf, 1);
857 port->icount.tx++;
858 tx_empty = 0;
859 fifo_size--;
860 }
861
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900862 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
863 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
864 xmit->tail, UART_XMIT_SIZE));
865 if (!bytes) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900866 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900867 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
868 uart_write_wakeup(port);
869 return 0;
870 }
871
872 if (bytes > fifo_size) {
873 num = bytes / fifo_size + 1;
874 size = fifo_size;
875 rem = bytes % fifo_size;
876 } else {
877 num = 1;
878 size = bytes;
879 rem = bytes;
880 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900881
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900882 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
883 __func__, num, size, rem);
884
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900885 priv->tx_dma_use = 1;
886
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900887 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900888
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900889 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
890 sg = priv->sg_tx_p;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900891
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900892 for (i = 0; i < num; i++, sg++) {
893 if (i == (num - 1))
894 sg_set_page(sg, virt_to_page(xmit->buf),
895 rem, fifo_size * i);
896 else
897 sg_set_page(sg, virt_to_page(xmit->buf),
898 size, fifo_size * i);
899 }
900
901 sg = priv->sg_tx_p;
902 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900903 if (!nent) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900904 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900905 return 0;
906 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900907 priv->nent = nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900908
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900909 for (i = 0; i < nent; i++, sg++) {
910 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
911 fifo_size * i;
912 sg_dma_address(sg) = (sg_dma_address(sg) &
913 ~(UART_XMIT_SIZE - 1)) + sg->offset;
914 if (i == (nent - 1))
915 sg_dma_len(sg) = rem;
916 else
917 sg_dma_len(sg) = size;
918 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900919
920 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900921 priv->sg_tx_p, nent, DMA_TO_DEVICE,
922 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900923 if (!desc) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900924 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
925 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900926 return 0;
927 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900928 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900929 priv->desc_tx = desc;
930 desc->callback = pch_dma_tx_complete;
931 desc->callback_param = priv;
932
933 desc->tx_submit(desc);
934
935 dma_async_issue_pending(priv->chan_tx);
936
937 return PCH_UART_HANDLED_TX_INT;
938}
939
940static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
941{
942 u8 fcr = ioread8(priv->membase + UART_FCR);
943
944 /* Reset FIFO */
945 fcr |= UART_FCR_CLEAR_RCVR;
946 iowrite8(fcr, priv->membase + UART_FCR);
947
948 if (lsr & PCH_UART_LSR_ERR)
949 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
950
951 if (lsr & UART_LSR_FE)
952 dev_err(&priv->pdev->dev, "Framing Error\n");
953
954 if (lsr & UART_LSR_PE)
955 dev_err(&priv->pdev->dev, "Parity Error\n");
956
957 if (lsr & UART_LSR_OE)
958 dev_err(&priv->pdev->dev, "Overrun Error\n");
959}
960
961static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
962{
963 struct eg20t_port *priv = dev_id;
964 unsigned int handled;
965 u8 lsr;
966 int ret = 0;
967 unsigned int iid;
968 unsigned long flags;
969
970 spin_lock_irqsave(&priv->port.lock, flags);
971 handled = 0;
972 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
973 switch (iid) {
974 case PCH_UART_IID_RLS: /* Receiver Line Status */
975 lsr = pch_uart_hal_get_line_status(priv);
976 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
977 UART_LSR_PE | UART_LSR_OE)) {
978 pch_uart_err_ir(priv, lsr);
979 ret = PCH_UART_HANDLED_RX_ERR_INT;
980 }
981 break;
982 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900983 if (priv->use_dma) {
984 pch_uart_hal_disable_interrupt(priv,
985 PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900986 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900987 if (!ret)
988 pch_uart_hal_enable_interrupt(priv,
989 PCH_UART_HAL_RX_INT);
990 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900991 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900992 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900993 break;
994 case PCH_UART_IID_RDR_TO: /* Received Data Ready
995 (FIFO Timeout) */
996 ret = handle_rx_to(priv);
997 break;
998 case PCH_UART_IID_THRE: /* Transmitter Holding Register
999 Empty */
1000 if (priv->use_dma)
1001 ret = dma_handle_tx(priv);
1002 else
1003 ret = handle_tx(priv);
1004 break;
1005 case PCH_UART_IID_MS: /* Modem Status */
1006 ret = PCH_UART_HANDLED_MS_INT;
1007 break;
1008 default: /* Never junp to this label */
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001009 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
1010 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001011 ret = -1;
1012 break;
1013 }
1014 handled |= (unsigned int)ret;
1015 }
1016 if (handled == 0 && iid <= 1) {
1017 if (priv->int_dis_flag)
1018 priv->int_dis_flag = 0;
1019 }
1020
1021 spin_unlock_irqrestore(&priv->port.lock, flags);
1022 return IRQ_RETVAL(handled);
1023}
1024
1025/* This function tests whether the transmitter fifo and shifter for the port
1026 described by 'port' is empty. */
1027static unsigned int pch_uart_tx_empty(struct uart_port *port)
1028{
1029 struct eg20t_port *priv;
1030 int ret;
1031 priv = container_of(port, struct eg20t_port, port);
1032 if (priv->tx_empty)
1033 ret = TIOCSER_TEMT;
1034 else
1035 ret = 0;
1036
1037 return ret;
1038}
1039
1040/* Returns the current state of modem control inputs. */
1041static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1042{
1043 struct eg20t_port *priv;
1044 u8 modem;
1045 unsigned int ret = 0;
1046
1047 priv = container_of(port, struct eg20t_port, port);
1048 modem = pch_uart_hal_get_modem(priv);
1049
1050 if (modem & UART_MSR_DCD)
1051 ret |= TIOCM_CAR;
1052
1053 if (modem & UART_MSR_RI)
1054 ret |= TIOCM_RNG;
1055
1056 if (modem & UART_MSR_DSR)
1057 ret |= TIOCM_DSR;
1058
1059 if (modem & UART_MSR_CTS)
1060 ret |= TIOCM_CTS;
1061
1062 return ret;
1063}
1064
1065static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1066{
1067 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001068 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1069
1070 if (mctrl & TIOCM_DTR)
1071 mcr |= UART_MCR_DTR;
1072 if (mctrl & TIOCM_RTS)
1073 mcr |= UART_MCR_RTS;
1074 if (mctrl & TIOCM_LOOP)
1075 mcr |= UART_MCR_LOOP;
1076
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001077 if (priv->mcr & UART_MCR_AFE)
1078 mcr |= UART_MCR_AFE;
1079
1080 if (mctrl)
1081 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001082}
1083
1084static void pch_uart_stop_tx(struct uart_port *port)
1085{
1086 struct eg20t_port *priv;
1087 priv = container_of(port, struct eg20t_port, port);
1088 priv->start_tx = 0;
1089 priv->tx_dma_use = 0;
1090}
1091
1092static void pch_uart_start_tx(struct uart_port *port)
1093{
1094 struct eg20t_port *priv;
1095
1096 priv = container_of(port, struct eg20t_port, port);
1097
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001098 if (priv->use_dma) {
1099 if (priv->tx_dma_use) {
1100 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1101 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001102 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001103 }
1104 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001105
1106 priv->start_tx = 1;
1107 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1108}
1109
1110static void pch_uart_stop_rx(struct uart_port *port)
1111{
1112 struct eg20t_port *priv;
1113 priv = container_of(port, struct eg20t_port, port);
1114 priv->start_rx = 0;
1115 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1116 priv->int_dis_flag = 1;
1117}
1118
1119/* Enable the modem status interrupts. */
1120static void pch_uart_enable_ms(struct uart_port *port)
1121{
1122 struct eg20t_port *priv;
1123 priv = container_of(port, struct eg20t_port, port);
1124 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1125}
1126
1127/* Control the transmission of a break signal. */
1128static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1129{
1130 struct eg20t_port *priv;
1131 unsigned long flags;
1132
1133 priv = container_of(port, struct eg20t_port, port);
1134 spin_lock_irqsave(&port->lock, flags);
1135 pch_uart_hal_set_break(priv, ctl);
1136 spin_unlock_irqrestore(&port->lock, flags);
1137}
1138
1139/* Grab any interrupt resources and initialise any low level driver state. */
1140static int pch_uart_startup(struct uart_port *port)
1141{
1142 struct eg20t_port *priv;
1143 int ret;
1144 int fifo_size;
1145 int trigger_level;
1146
1147 priv = container_of(port, struct eg20t_port, port);
1148 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001149
1150 if (port->uartclk)
1151 priv->base_baud = port->uartclk;
1152 else
1153 port->uartclk = priv->base_baud;
1154
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001155 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1156 ret = pch_uart_hal_set_line(priv, default_baud,
1157 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1158 PCH_UART_HAL_STB1);
1159 if (ret)
1160 return ret;
1161
1162 switch (priv->fifo_size) {
1163 case 256:
1164 fifo_size = PCH_UART_HAL_FIFO256;
1165 break;
1166 case 64:
1167 fifo_size = PCH_UART_HAL_FIFO64;
1168 break;
1169 case 16:
1170 fifo_size = PCH_UART_HAL_FIFO16;
1171 case 1:
1172 default:
1173 fifo_size = PCH_UART_HAL_FIFO_DIS;
1174 break;
1175 }
1176
1177 switch (priv->trigger) {
1178 case PCH_UART_HAL_TRIGGER1:
1179 trigger_level = 1;
1180 break;
1181 case PCH_UART_HAL_TRIGGER_L:
1182 trigger_level = priv->fifo_size / 4;
1183 break;
1184 case PCH_UART_HAL_TRIGGER_M:
1185 trigger_level = priv->fifo_size / 2;
1186 break;
1187 case PCH_UART_HAL_TRIGGER_H:
1188 default:
1189 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1190 break;
1191 }
1192
1193 priv->trigger_level = trigger_level;
1194 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1195 fifo_size, priv->trigger);
1196 if (ret < 0)
1197 return ret;
1198
1199 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1200 KBUILD_MODNAME, priv);
1201 if (ret < 0)
1202 return ret;
1203
1204 if (priv->use_dma)
1205 pch_request_dma(port);
1206
1207 priv->start_rx = 1;
1208 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1209 uart_update_timeout(port, CS8, default_baud);
1210
1211 return 0;
1212}
1213
1214static void pch_uart_shutdown(struct uart_port *port)
1215{
1216 struct eg20t_port *priv;
1217 int ret;
1218
1219 priv = container_of(port, struct eg20t_port, port);
1220 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1221 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1222 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1223 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1224 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001225 dev_err(priv->port.dev,
1226 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001227
1228 if (priv->use_dma_flag)
1229 pch_free_dma(port);
1230
1231 free_irq(priv->port.irq, priv);
1232}
1233
1234/* Change the port parameters, including word length, parity, stop
1235 *bits. Update read_status_mask and ignore_status_mask to indicate
1236 *the types of events we are interested in receiving. */
1237static void pch_uart_set_termios(struct uart_port *port,
1238 struct ktermios *termios, struct ktermios *old)
1239{
1240 int baud;
1241 int rtn;
1242 unsigned int parity, bits, stb;
1243 struct eg20t_port *priv;
1244 unsigned long flags;
1245
1246 priv = container_of(port, struct eg20t_port, port);
1247 switch (termios->c_cflag & CSIZE) {
1248 case CS5:
1249 bits = PCH_UART_HAL_5BIT;
1250 break;
1251 case CS6:
1252 bits = PCH_UART_HAL_6BIT;
1253 break;
1254 case CS7:
1255 bits = PCH_UART_HAL_7BIT;
1256 break;
1257 default: /* CS8 */
1258 bits = PCH_UART_HAL_8BIT;
1259 break;
1260 }
1261 if (termios->c_cflag & CSTOPB)
1262 stb = PCH_UART_HAL_STB2;
1263 else
1264 stb = PCH_UART_HAL_STB1;
1265
1266 if (termios->c_cflag & PARENB) {
1267 if (!(termios->c_cflag & PARODD))
1268 parity = PCH_UART_HAL_PARITY_ODD;
1269 else
1270 parity = PCH_UART_HAL_PARITY_EVEN;
1271
1272 } else {
1273 parity = PCH_UART_HAL_PARITY_NONE;
1274 }
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001275
1276 /* Only UART0 has auto hardware flow function */
1277 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1278 priv->mcr |= UART_MCR_AFE;
1279 else
1280 priv->mcr &= ~UART_MCR_AFE;
1281
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001282 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1283
1284 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1285
1286 spin_lock_irqsave(&port->lock, flags);
1287
1288 uart_update_timeout(port, termios->c_cflag, baud);
1289 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1290 if (rtn)
1291 goto out;
1292
1293 /* Don't rewrite B0 */
1294 if (tty_termios_baud_rate(termios))
1295 tty_termios_encode_baud_rate(termios, baud, baud);
1296
1297out:
1298 spin_unlock_irqrestore(&port->lock, flags);
1299}
1300
1301static const char *pch_uart_type(struct uart_port *port)
1302{
1303 return KBUILD_MODNAME;
1304}
1305
1306static void pch_uart_release_port(struct uart_port *port)
1307{
1308 struct eg20t_port *priv;
1309
1310 priv = container_of(port, struct eg20t_port, port);
1311 pci_iounmap(priv->pdev, priv->membase);
1312 pci_release_regions(priv->pdev);
1313}
1314
1315static int pch_uart_request_port(struct uart_port *port)
1316{
1317 struct eg20t_port *priv;
1318 int ret;
1319 void __iomem *membase;
1320
1321 priv = container_of(port, struct eg20t_port, port);
1322 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1323 if (ret < 0)
1324 return -EBUSY;
1325
1326 membase = pci_iomap(priv->pdev, 1, 0);
1327 if (!membase) {
1328 pci_release_regions(priv->pdev);
1329 return -EBUSY;
1330 }
1331 priv->membase = port->membase = membase;
1332
1333 return 0;
1334}
1335
1336static void pch_uart_config_port(struct uart_port *port, int type)
1337{
1338 struct eg20t_port *priv;
1339
1340 priv = container_of(port, struct eg20t_port, port);
1341 if (type & UART_CONFIG_TYPE) {
1342 port->type = priv->port_type;
1343 pch_uart_request_port(port);
1344 }
1345}
1346
1347static int pch_uart_verify_port(struct uart_port *port,
1348 struct serial_struct *serinfo)
1349{
1350 struct eg20t_port *priv;
1351
1352 priv = container_of(port, struct eg20t_port, port);
1353 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001354 dev_info(priv->port.dev,
1355 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001356 priv->use_dma = 0;
1357 serinfo->flags &= ~UPF_LOW_LATENCY;
1358 } else {
1359#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001360 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1361 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001362 return -EOPNOTSUPP;
1363#endif
1364 priv->use_dma = 1;
1365 priv->use_dma_flag = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001366 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001367 }
1368
1369 return 0;
1370}
1371
1372static struct uart_ops pch_uart_ops = {
1373 .tx_empty = pch_uart_tx_empty,
1374 .set_mctrl = pch_uart_set_mctrl,
1375 .get_mctrl = pch_uart_get_mctrl,
1376 .stop_tx = pch_uart_stop_tx,
1377 .start_tx = pch_uart_start_tx,
1378 .stop_rx = pch_uart_stop_rx,
1379 .enable_ms = pch_uart_enable_ms,
1380 .break_ctl = pch_uart_break_ctl,
1381 .startup = pch_uart_startup,
1382 .shutdown = pch_uart_shutdown,
1383 .set_termios = pch_uart_set_termios,
1384/* .pm = pch_uart_pm, Not supported yet */
1385/* .set_wake = pch_uart_set_wake, Not supported yet */
1386 .type = pch_uart_type,
1387 .release_port = pch_uart_release_port,
1388 .request_port = pch_uart_request_port,
1389 .config_port = pch_uart_config_port,
1390 .verify_port = pch_uart_verify_port
1391};
1392
Alexander Steine30f8672011-11-15 15:04:07 -08001393#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1394
1395/*
1396 * Wait for transmitter & holding register to empty
1397 */
1398static void wait_for_xmitr(struct eg20t_port *up, int bits)
1399{
1400 unsigned int status, tmout = 10000;
1401
1402 /* Wait up to 10ms for the character(s) to be sent. */
1403 for (;;) {
1404 status = ioread8(up->membase + UART_LSR);
1405
1406 if ((status & bits) == bits)
1407 break;
1408 if (--tmout == 0)
1409 break;
1410 udelay(1);
1411 }
1412
1413 /* Wait up to 1s for flow control if necessary */
1414 if (up->port.flags & UPF_CONS_FLOW) {
1415 unsigned int tmout;
1416 for (tmout = 1000000; tmout; tmout--) {
1417 unsigned int msr = ioread8(up->membase + UART_MSR);
1418 if (msr & UART_MSR_CTS)
1419 break;
1420 udelay(1);
1421 touch_nmi_watchdog();
1422 }
1423 }
1424}
1425
1426static void pch_console_putchar(struct uart_port *port, int ch)
1427{
1428 struct eg20t_port *priv =
1429 container_of(port, struct eg20t_port, port);
1430
1431 wait_for_xmitr(priv, UART_LSR_THRE);
1432 iowrite8(ch, priv->membase + PCH_UART_THR);
1433}
1434
1435/*
1436 * Print a string to the serial port trying not to disturb
1437 * any possible real use of the port...
1438 *
1439 * The console_lock must be held when we get here.
1440 */
1441static void
1442pch_console_write(struct console *co, const char *s, unsigned int count)
1443{
1444 struct eg20t_port *priv;
1445
1446 unsigned long flags;
1447 u8 ier;
1448 int locked = 1;
1449
1450 priv = pch_uart_ports[co->index];
1451
1452 touch_nmi_watchdog();
1453
1454 local_irq_save(flags);
1455 if (priv->port.sysrq) {
1456 /* serial8250_handle_port() already took the lock */
1457 locked = 0;
1458 } else if (oops_in_progress) {
1459 locked = spin_trylock(&priv->port.lock);
1460 } else
1461 spin_lock(&priv->port.lock);
1462
1463 /*
1464 * First save the IER then disable the interrupts
1465 */
1466 ier = ioread8(priv->membase + UART_IER);
1467
1468 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1469
1470 uart_console_write(&priv->port, s, count, pch_console_putchar);
1471
1472 /*
1473 * Finally, wait for transmitter to become empty
1474 * and restore the IER
1475 */
1476 wait_for_xmitr(priv, BOTH_EMPTY);
1477 iowrite8(ier, priv->membase + UART_IER);
1478
1479 if (locked)
1480 spin_unlock(&priv->port.lock);
1481 local_irq_restore(flags);
1482}
1483
1484static int __init pch_console_setup(struct console *co, char *options)
1485{
1486 struct uart_port *port;
1487 int baud = 9600;
1488 int bits = 8;
1489 int parity = 'n';
1490 int flow = 'n';
1491
1492 /*
1493 * Check whether an invalid uart number has been specified, and
1494 * if so, search for the first available port that does have
1495 * console support.
1496 */
1497 if (co->index >= PCH_UART_NR)
1498 co->index = 0;
1499 port = &pch_uart_ports[co->index]->port;
1500
1501 if (!port || (!port->iobase && !port->membase))
1502 return -ENODEV;
1503
1504 /* setup uartclock */
1505 port->uartclk = DEFAULT_BAUD_RATE;
1506
1507 if (options)
1508 uart_parse_options(options, &baud, &parity, &bits, &flow);
1509
1510 return uart_set_options(port, co, baud, parity, bits, flow);
1511}
1512
1513static struct uart_driver pch_uart_driver;
1514
1515static struct console pch_console = {
1516 .name = PCH_UART_DRIVER_DEVICE,
1517 .write = pch_console_write,
1518 .device = uart_console_device,
1519 .setup = pch_console_setup,
1520 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1521 .index = -1,
1522 .data = &pch_uart_driver,
1523};
1524
1525#define PCH_CONSOLE (&pch_console)
1526#else
1527#define PCH_CONSOLE NULL
1528#endif
1529
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001530static struct uart_driver pch_uart_driver = {
1531 .owner = THIS_MODULE,
1532 .driver_name = KBUILD_MODNAME,
1533 .dev_name = PCH_UART_DRIVER_DEVICE,
1534 .major = 0,
1535 .minor = 0,
1536 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001537 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001538};
1539
1540static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001541 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001542{
1543 struct eg20t_port *priv;
1544 int ret;
1545 unsigned int iobase;
1546 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001547 unsigned char *rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001548 int fifosize, base_baud;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001549 int port_type;
1550 struct pch_uart_driver_data *board;
Alexander Steinfb139df2011-06-15 15:08:55 -07001551 const char *board_name;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001552
1553 board = &drv_dat[id->driver_data];
1554 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001555
1556 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1557 if (priv == NULL)
1558 goto init_port_alloc_err;
1559
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001560 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001561 if (!rxbuf)
1562 goto init_port_free_txbuf;
1563
Alexander Steine30f8672011-11-15 15:04:07 -08001564 base_baud = DEFAULT_BAUD_RATE;
Denis Turischev6ae705b2011-03-10 15:14:00 +02001565
1566 /* quirk for CM-iTC board */
Alexander Steinfb139df2011-06-15 15:08:55 -07001567 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1568 if (board_name && strstr(board_name, "CM-iTC"))
Denis Turischev6ae705b2011-03-10 15:14:00 +02001569 base_baud = 192000000; /* 192.0MHz */
1570
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001571 switch (port_type) {
1572 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001573 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001574 break;
1575 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001576 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001577 break;
1578 default:
1579 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1580 goto init_port_hal_free;
1581 }
1582
Alexander Steine4635952011-07-04 08:58:31 +02001583 pci_enable_msi(pdev);
1584
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001585 iobase = pci_resource_start(pdev, 0);
1586 mapbase = pci_resource_start(pdev, 1);
1587 priv->mapbase = mapbase;
1588 priv->iobase = iobase;
1589 priv->pdev = pdev;
1590 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001591 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001592 priv->rxbuf.size = PAGE_SIZE;
1593
1594 priv->fifo_size = fifosize;
1595 priv->base_baud = base_baud;
1596 priv->port_type = PORT_MAX_8250 + port_type + 1;
1597 priv->port.dev = &pdev->dev;
1598 priv->port.iobase = iobase;
1599 priv->port.membase = NULL;
1600 priv->port.mapbase = mapbase;
1601 priv->port.irq = pdev->irq;
1602 priv->port.iotype = UPIO_PORT;
1603 priv->port.ops = &pch_uart_ops;
1604 priv->port.flags = UPF_BOOT_AUTOCONF;
1605 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001606 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001607 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1608
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001609 spin_lock_init(&priv->port.lock);
1610
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001611 pci_set_drvdata(pdev, priv);
1612 pch_uart_hal_request(pdev, fifosize, base_baud);
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001613
Alexander Steine30f8672011-11-15 15:04:07 -08001614#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1615 pch_uart_ports[board->line_no] = priv;
1616#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001617 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1618 if (ret < 0)
1619 goto init_port_hal_free;
1620
1621 return priv;
1622
1623init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001624#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1625 pch_uart_ports[board->line_no] = NULL;
1626#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001627 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001628init_port_free_txbuf:
1629 kfree(priv);
1630init_port_alloc_err:
1631
1632 return NULL;
1633}
1634
1635static void pch_uart_exit_port(struct eg20t_port *priv)
1636{
1637 uart_remove_one_port(&pch_uart_driver, &priv->port);
1638 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001639 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001640}
1641
1642static void pch_uart_pci_remove(struct pci_dev *pdev)
1643{
1644 struct eg20t_port *priv;
1645
1646 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001647
1648 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001649
1650#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1651 pch_uart_ports[priv->port.line] = NULL;
1652#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001653 pch_uart_exit_port(priv);
1654 pci_disable_device(pdev);
1655 kfree(priv);
1656 return;
1657}
1658#ifdef CONFIG_PM
1659static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1660{
1661 struct eg20t_port *priv = pci_get_drvdata(pdev);
1662
1663 uart_suspend_port(&pch_uart_driver, &priv->port);
1664
1665 pci_save_state(pdev);
1666 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1667 return 0;
1668}
1669
1670static int pch_uart_pci_resume(struct pci_dev *pdev)
1671{
1672 struct eg20t_port *priv = pci_get_drvdata(pdev);
1673 int ret;
1674
1675 pci_set_power_state(pdev, PCI_D0);
1676 pci_restore_state(pdev);
1677
1678 ret = pci_enable_device(pdev);
1679 if (ret) {
1680 dev_err(&pdev->dev,
1681 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1682 return ret;
1683 }
1684
1685 uart_resume_port(&pch_uart_driver, &priv->port);
1686
1687 return 0;
1688}
1689#else
1690#define pch_uart_pci_suspend NULL
1691#define pch_uart_pci_resume NULL
1692#endif
1693
1694static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1695 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001696 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001697 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001698 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001699 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001700 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001701 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001702 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001703 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001704 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001705 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001706 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001707 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001708 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001709 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1710 .driver_data = pch_ml7223_uart0},
1711 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1712 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001713 {0,},
1714};
1715
1716static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1717 const struct pci_device_id *id)
1718{
1719 int ret;
1720 struct eg20t_port *priv;
1721
1722 ret = pci_enable_device(pdev);
1723 if (ret < 0)
1724 goto probe_error;
1725
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001726 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001727 if (!priv) {
1728 ret = -EBUSY;
1729 goto probe_disable_device;
1730 }
1731 pci_set_drvdata(pdev, priv);
1732
1733 return ret;
1734
1735probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001736 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001737 pci_disable_device(pdev);
1738probe_error:
1739 return ret;
1740}
1741
1742static struct pci_driver pch_uart_pci_driver = {
1743 .name = "pch_uart",
1744 .id_table = pch_uart_pci_id,
1745 .probe = pch_uart_pci_probe,
1746 .remove = __devexit_p(pch_uart_pci_remove),
1747 .suspend = pch_uart_pci_suspend,
1748 .resume = pch_uart_pci_resume,
1749};
1750
1751static int __init pch_uart_module_init(void)
1752{
1753 int ret;
1754
1755 /* register as UART driver */
1756 ret = uart_register_driver(&pch_uart_driver);
1757 if (ret < 0)
1758 return ret;
1759
1760 /* register as PCI driver */
1761 ret = pci_register_driver(&pch_uart_pci_driver);
1762 if (ret < 0)
1763 uart_unregister_driver(&pch_uart_driver);
1764
1765 return ret;
1766}
1767module_init(pch_uart_module_init);
1768
1769static void __exit pch_uart_module_exit(void)
1770{
1771 pci_unregister_driver(&pch_uart_pci_driver);
1772 uart_unregister_driver(&pch_uart_driver);
1773}
1774module_exit(pch_uart_module_exit);
1775
1776MODULE_LICENSE("GPL v2");
1777MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1778module_param(default_baud, uint, S_IRUGO);