blob: 103a25f77d14d43c8d6fbce99b6efb3878c83427 [file] [log] [blame]
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001/*
Tomoya MORINAGAeca9dfa2011-10-28 09:38:50 +09002 *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09003 *
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
Feng Tangd0114112012-02-06 17:24:43 +080032#include <linux/debugfs.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090033#include <linux/dmaengine.h>
34#include <linux/pch_dma.h>
35
36enum {
37 PCH_UART_HANDLED_RX_INT_SHIFT,
38 PCH_UART_HANDLED_TX_INT_SHIFT,
39 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
40 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
41 PCH_UART_HANDLED_MS_INT_SHIFT,
42};
43
44enum {
45 PCH_UART_8LINE,
46 PCH_UART_2LINE,
47};
48
49#define PCH_UART_DRIVER_DEVICE "ttyPCH"
50
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +090051/* Set the max number of UART port
52 * Intel EG20T PCH: 4 port
Tomoya MORINAGAeca9dfa2011-10-28 09:38:50 +090053 * LAPIS Semiconductor ML7213 IOH: 3 port
54 * LAPIS Semiconductor ML7223 IOH: 2 port
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +090055*/
56#define PCH_UART_NR 4
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090057
58#define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
59#define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
60#define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
61 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
62#define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
63 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
64#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
65
66#define PCH_UART_RBR 0x00
67#define PCH_UART_THR 0x00
68
69#define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
70 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
71#define PCH_UART_IER_ERBFI 0x00000001
72#define PCH_UART_IER_ETBEI 0x00000002
73#define PCH_UART_IER_ELSI 0x00000004
74#define PCH_UART_IER_EDSSI 0x00000008
75
76#define PCH_UART_IIR_IP 0x00000001
77#define PCH_UART_IIR_IID 0x00000006
78#define PCH_UART_IIR_MSI 0x00000000
79#define PCH_UART_IIR_TRI 0x00000002
80#define PCH_UART_IIR_RRI 0x00000004
81#define PCH_UART_IIR_REI 0x00000006
82#define PCH_UART_IIR_TOI 0x00000008
83#define PCH_UART_IIR_FIFO256 0x00000020
84#define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
85#define PCH_UART_IIR_FE 0x000000C0
86
87#define PCH_UART_FCR_FIFOE 0x00000001
88#define PCH_UART_FCR_RFR 0x00000002
89#define PCH_UART_FCR_TFR 0x00000004
90#define PCH_UART_FCR_DMS 0x00000008
91#define PCH_UART_FCR_FIFO256 0x00000020
92#define PCH_UART_FCR_RFTL 0x000000C0
93
94#define PCH_UART_FCR_RFTL1 0x00000000
95#define PCH_UART_FCR_RFTL64 0x00000040
96#define PCH_UART_FCR_RFTL128 0x00000080
97#define PCH_UART_FCR_RFTL224 0x000000C0
98#define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
99#define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
100#define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
101#define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
102#define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
103#define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
104#define PCH_UART_FCR_RFTL_SHIFT 6
105
106#define PCH_UART_LCR_WLS 0x00000003
107#define PCH_UART_LCR_STB 0x00000004
108#define PCH_UART_LCR_PEN 0x00000008
109#define PCH_UART_LCR_EPS 0x00000010
110#define PCH_UART_LCR_SP 0x00000020
111#define PCH_UART_LCR_SB 0x00000040
112#define PCH_UART_LCR_DLAB 0x00000080
113#define PCH_UART_LCR_NP 0x00000000
114#define PCH_UART_LCR_OP PCH_UART_LCR_PEN
115#define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
116#define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
117#define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
118 PCH_UART_LCR_SP)
119
120#define PCH_UART_LCR_5BIT 0x00000000
121#define PCH_UART_LCR_6BIT 0x00000001
122#define PCH_UART_LCR_7BIT 0x00000002
123#define PCH_UART_LCR_8BIT 0x00000003
124
125#define PCH_UART_MCR_DTR 0x00000001
126#define PCH_UART_MCR_RTS 0x00000002
127#define PCH_UART_MCR_OUT 0x0000000C
128#define PCH_UART_MCR_LOOP 0x00000010
129#define PCH_UART_MCR_AFE 0x00000020
130
131#define PCH_UART_LSR_DR 0x00000001
132#define PCH_UART_LSR_ERR (1<<7)
133
134#define PCH_UART_MSR_DCTS 0x00000001
135#define PCH_UART_MSR_DDSR 0x00000002
136#define PCH_UART_MSR_TERI 0x00000004
137#define PCH_UART_MSR_DDCD 0x00000008
138#define PCH_UART_MSR_CTS 0x00000010
139#define PCH_UART_MSR_DSR 0x00000020
140#define PCH_UART_MSR_RI 0x00000040
141#define PCH_UART_MSR_DCD 0x00000080
142#define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
143 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
144
145#define PCH_UART_DLL 0x00
146#define PCH_UART_DLM 0x01
147
Feng Tangd0114112012-02-06 17:24:43 +0800148#define PCH_UART_BRCSR 0x0E
149
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900150#define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
151#define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
152#define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
153#define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
154#define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
155
156#define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
157#define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
158#define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
159#define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
160#define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
161#define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
162#define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
163#define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
164#define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
165#define PCH_UART_HAL_STB1 0
166#define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
167
168#define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
169#define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
170#define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
171 PCH_UART_HAL_CLR_RX_FIFO)
172
173#define PCH_UART_HAL_DMA_MODE0 0
174#define PCH_UART_HAL_FIFO_DIS 0
175#define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
176#define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
177 PCH_UART_FCR_FIFO256)
178#define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
179#define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
180#define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
181#define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
182#define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
183#define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
184#define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
185#define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
186#define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
187#define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
188#define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
189#define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
190#define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
191#define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
192
193#define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
194#define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
195#define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
196#define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
197#define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
198
199#define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
200#define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
201#define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
202#define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
203#define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
204
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +0900205#define PCI_VENDOR_ID_ROHM 0x10DB
206
Alexander Steine30f8672011-11-15 15:04:07 -0800207#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
208
Darren Hart077175f2012-03-09 09:51:49 -0800209#define DEFAULT_UARTCLK 1843200 /* 1.8432 MHz */
210#define CMITC_UARTCLK 192000000 /* 192.0000 MHz */
211#define FRI2_64_UARTCLK 64000000 /* 64.0000 MHz */
212#define FRI2_48_UARTCLK 48000000 /* 48.0000 MHz */
Michael Brunner11bbd5b2012-03-23 11:06:37 +0100213#define NTC1_UARTCLK 64000000 /* 64.0000 MHz */
Alexander Steine30f8672011-11-15 15:04:07 -0800214
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900215struct pch_uart_buffer {
216 unsigned char *buf;
217 int size;
218};
219
220struct eg20t_port {
221 struct uart_port port;
222 int port_type;
223 void __iomem *membase;
224 resource_size_t mapbase;
225 unsigned int iobase;
226 struct pci_dev *pdev;
227 int fifo_size;
Darren Harta8a3ec92012-03-09 09:51:48 -0800228 int uartclk;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900229 int start_tx;
230 int start_rx;
231 int tx_empty;
232 int int_dis_flag;
233 int trigger;
234 int trigger_level;
235 struct pch_uart_buffer rxbuf;
236 unsigned int dmsr;
237 unsigned int fcr;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +0900238 unsigned int mcr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900239 unsigned int use_dma;
240 unsigned int use_dma_flag;
241 struct dma_async_tx_descriptor *desc_tx;
242 struct dma_async_tx_descriptor *desc_rx;
243 struct pch_dma_slave param_tx;
244 struct pch_dma_slave param_rx;
245 struct dma_chan *chan_tx;
246 struct dma_chan *chan_rx;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900247 struct scatterlist *sg_tx_p;
248 int nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900249 struct scatterlist sg_rx;
250 int tx_dma_use;
251 void *rx_buf_virt;
252 dma_addr_t rx_buf_dma;
Feng Tangd0114112012-02-06 17:24:43 +0800253
254 struct dentry *debugfs;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900255};
256
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900257/**
258 * struct pch_uart_driver_data - private data structure for UART-DMA
259 * @port_type: The number of DMA channel
260 * @line_no: UART port line number (0, 1, 2...)
261 */
262struct pch_uart_driver_data {
263 int port_type;
264 int line_no;
265};
266
267enum pch_uart_num_t {
268 pch_et20t_uart0 = 0,
269 pch_et20t_uart1,
270 pch_et20t_uart2,
271 pch_et20t_uart3,
272 pch_ml7213_uart0,
273 pch_ml7213_uart1,
274 pch_ml7213_uart2,
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900275 pch_ml7223_uart0,
276 pch_ml7223_uart1,
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900277 pch_ml7831_uart0,
278 pch_ml7831_uart1,
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900279};
280
281static struct pch_uart_driver_data drv_dat[] = {
282 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
283 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
284 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
285 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
286 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
287 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
288 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900289 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
290 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900291 [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
292 [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900293};
294
Alexander Steine30f8672011-11-15 15:04:07 -0800295#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
296static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
297#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900298static unsigned int default_baud = 9600;
Darren Hart2a44feb2012-03-09 09:51:50 -0800299static unsigned int user_uartclk = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900300static const int trigger_level_256[4] = { 1, 64, 128, 224 };
301static const int trigger_level_64[4] = { 1, 16, 32, 56 };
302static const int trigger_level_16[4] = { 1, 4, 8, 14 };
303static const int trigger_level_1[4] = { 1, 1, 1, 1 };
304
Feng Tangd0114112012-02-06 17:24:43 +0800305#ifdef CONFIG_DEBUG_FS
306
307#define PCH_REGS_BUFSIZE 1024
Stephen Boyd234e3402012-04-05 14:25:11 -0700308
Feng Tangd0114112012-02-06 17:24:43 +0800309
310static ssize_t port_show_regs(struct file *file, char __user *user_buf,
311 size_t count, loff_t *ppos)
312{
313 struct eg20t_port *priv = file->private_data;
314 char *buf;
315 u32 len = 0;
316 ssize_t ret;
317 unsigned char lcr;
318
319 buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
320 if (!buf)
321 return 0;
322
323 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
324 "PCH EG20T port[%d] regs:\n", priv->port.line);
325
326 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
327 "=================================\n");
328 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
329 "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
330 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
331 "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
332 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
333 "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
334 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
335 "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
336 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
337 "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
338 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
339 "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
340 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
341 "BRCSR: \t0x%02x\n",
342 ioread8(priv->membase + PCH_UART_BRCSR));
343
344 lcr = ioread8(priv->membase + UART_LCR);
345 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
346 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
347 "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
348 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
349 "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
350 iowrite8(lcr, priv->membase + UART_LCR);
351
352 if (len > PCH_REGS_BUFSIZE)
353 len = PCH_REGS_BUFSIZE;
354
355 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
356 kfree(buf);
357 return ret;
358}
359
360static const struct file_operations port_regs_ops = {
361 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700362 .open = simple_open,
Feng Tangd0114112012-02-06 17:24:43 +0800363 .read = port_show_regs,
364 .llseek = default_llseek,
365};
366#endif /* CONFIG_DEBUG_FS */
367
Darren Hart077175f2012-03-09 09:51:49 -0800368/* Return UART clock, checking for board specific clocks. */
369static int pch_uart_get_uartclk(void)
370{
371 const char *cmp;
372
Darren Hart2a44feb2012-03-09 09:51:50 -0800373 if (user_uartclk)
374 return user_uartclk;
375
Darren Hart077175f2012-03-09 09:51:49 -0800376 cmp = dmi_get_system_info(DMI_BOARD_NAME);
377 if (cmp && strstr(cmp, "CM-iTC"))
378 return CMITC_UARTCLK;
379
380 cmp = dmi_get_system_info(DMI_BIOS_VERSION);
381 if (cmp && strnstr(cmp, "FRI2", 4))
382 return FRI2_64_UARTCLK;
383
384 cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
385 if (cmp && strstr(cmp, "Fish River Island II"))
386 return FRI2_48_UARTCLK;
387
Michael Brunner11bbd5b2012-03-23 11:06:37 +0100388 /* Kontron COMe-mTT10 (nanoETXexpress-TT) */
389 cmp = dmi_get_system_info(DMI_BOARD_NAME);
390 if (cmp && (strstr(cmp, "COMe-mTT") ||
391 strstr(cmp, "nanoETXexpress-TT")))
392 return NTC1_UARTCLK;
393
Darren Hart077175f2012-03-09 09:51:49 -0800394 return DEFAULT_UARTCLK;
395}
396
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900397static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
398 unsigned int flag)
399{
400 u8 ier = ioread8(priv->membase + UART_IER);
401 ier |= flag & PCH_UART_IER_MASK;
402 iowrite8(ier, priv->membase + UART_IER);
403}
404
405static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
406 unsigned int flag)
407{
408 u8 ier = ioread8(priv->membase + UART_IER);
409 ier &= ~(flag & PCH_UART_IER_MASK);
410 iowrite8(ier, priv->membase + UART_IER);
411}
412
413static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
414 unsigned int parity, unsigned int bits,
415 unsigned int stb)
416{
417 unsigned int dll, dlm, lcr;
418 int div;
419
Darren Harta8a3ec92012-03-09 09:51:48 -0800420 div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900421 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900422 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900423 return -EINVAL;
424 }
425
426 dll = (unsigned int)div & 0x00FFU;
427 dlm = ((unsigned int)div >> 8) & 0x00FFU;
428
429 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900430 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900431 return -EINVAL;
432 }
433
434 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900435 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900436 return -EINVAL;
437 }
438
439 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900440 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900441 return -EINVAL;
442 }
443
444 lcr = parity;
445 lcr |= bits;
446 lcr |= stb;
447
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900448 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900449 __func__, baud, div, lcr, jiffies);
450 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
451 iowrite8(dll, priv->membase + PCH_UART_DLL);
452 iowrite8(dlm, priv->membase + PCH_UART_DLM);
453 iowrite8(lcr, priv->membase + UART_LCR);
454
455 return 0;
456}
457
458static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
459 unsigned int flag)
460{
461 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900462 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
463 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900464 return -EINVAL;
465 }
466
467 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
468 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
469 priv->membase + UART_FCR);
470 iowrite8(priv->fcr, priv->membase + UART_FCR);
471
472 return 0;
473}
474
475static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
476 unsigned int dmamode,
477 unsigned int fifo_size, unsigned int trigger)
478{
479 u8 fcr;
480
481 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900482 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
483 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900484 return -EINVAL;
485 }
486
487 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900488 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
489 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900490 return -EINVAL;
491 }
492
493 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900494 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
495 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900496 return -EINVAL;
497 }
498
499 switch (priv->fifo_size) {
500 case 256:
501 priv->trigger_level =
502 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
503 break;
504 case 64:
505 priv->trigger_level =
506 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
507 break;
508 case 16:
509 priv->trigger_level =
510 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
511 break;
512 default:
513 priv->trigger_level =
514 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
515 break;
516 }
517 fcr =
518 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
519 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
520 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
521 priv->membase + UART_FCR);
522 iowrite8(fcr, priv->membase + UART_FCR);
523 priv->fcr = fcr;
524
525 return 0;
526}
527
528static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
529{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800530 unsigned int msr = ioread8(priv->membase + UART_MSR);
531 priv->dmsr = msr & PCH_UART_MSR_DELTA;
532 return (u8)msr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900533}
534
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900535static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900536 const unsigned char *buf, int tx_size)
537{
538 int i;
539 unsigned int thr;
540
541 for (i = 0; i < tx_size;) {
542 thr = buf[i++];
543 iowrite8(thr, priv->membase + PCH_UART_THR);
544 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900545}
546
547static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
548 int rx_size)
549{
550 int i;
551 u8 rbr, lsr;
552
553 lsr = ioread8(priv->membase + UART_LSR);
554 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
555 i < rx_size && lsr & UART_LSR_DR;
556 lsr = ioread8(priv->membase + UART_LSR)) {
557 rbr = ioread8(priv->membase + PCH_UART_RBR);
558 buf[i++] = rbr;
559 }
560 return i;
561}
562
563static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
564{
565 unsigned int iir;
566 int ret;
567
568 iir = ioread8(priv->membase + UART_IIR);
569 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
570 return ret;
571}
572
573static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
574{
575 return ioread8(priv->membase + UART_LSR);
576}
577
578static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
579{
580 unsigned int lcr;
581
582 lcr = ioread8(priv->membase + UART_LCR);
583 if (on)
584 lcr |= PCH_UART_LCR_SB;
585 else
586 lcr &= ~PCH_UART_LCR_SB;
587
588 iowrite8(lcr, priv->membase + UART_LCR);
589}
590
591static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
592 int size)
593{
594 struct uart_port *port;
595 struct tty_struct *tty;
596
597 port = &priv->port;
598 tty = tty_port_tty_get(&port->state->port);
599 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900600 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900601 return -EBUSY;
602 }
603
604 tty_insert_flip_string(tty, buf, size);
605 tty_flip_buffer_push(tty);
606 tty_kref_put(tty);
607
608 return 0;
609}
610
611static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
612{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800613 int ret = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900614 struct uart_port *port = &priv->port;
615
616 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900617 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
618 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900619 buf[0] = port->x_char;
620 port->x_char = 0;
621 ret = 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900622 }
623
624 return ret;
625}
626
627static int dma_push_rx(struct eg20t_port *priv, int size)
628{
629 struct tty_struct *tty;
630 int room;
631 struct uart_port *port = &priv->port;
632
633 port = &priv->port;
634 tty = tty_port_tty_get(&port->state->port);
635 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900636 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900637 return 0;
638 }
639
640 room = tty_buffer_request_room(tty, size);
641
642 if (room < size)
643 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
644 size - room);
645 if (!room)
646 return room;
647
648 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
649
650 port->icount.rx += room;
651 tty_kref_put(tty);
652
653 return room;
654}
655
656static void pch_free_dma(struct uart_port *port)
657{
658 struct eg20t_port *priv;
659 priv = container_of(port, struct eg20t_port, port);
660
661 if (priv->chan_tx) {
662 dma_release_channel(priv->chan_tx);
663 priv->chan_tx = NULL;
664 }
665 if (priv->chan_rx) {
666 dma_release_channel(priv->chan_rx);
667 priv->chan_rx = NULL;
668 }
669 if (sg_dma_address(&priv->sg_rx))
670 dma_free_coherent(port->dev, port->fifosize,
671 sg_virt(&priv->sg_rx),
672 sg_dma_address(&priv->sg_rx));
673
674 return;
675}
676
677static bool filter(struct dma_chan *chan, void *slave)
678{
679 struct pch_dma_slave *param = slave;
680
681 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
682 chan->device->dev)) {
683 chan->private = param;
684 return true;
685 } else {
686 return false;
687 }
688}
689
690static void pch_request_dma(struct uart_port *port)
691{
692 dma_cap_mask_t mask;
693 struct dma_chan *chan;
694 struct pci_dev *dma_dev;
695 struct pch_dma_slave *param;
696 struct eg20t_port *priv =
697 container_of(port, struct eg20t_port, port);
698 dma_cap_zero(mask);
699 dma_cap_set(DMA_SLAVE, mask);
700
Tomoya MORINAGA6c4b47d2011-07-20 20:17:49 +0900701 dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
702 PCI_DEVFN(0xa, 0)); /* Get DMA's dev
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900703 information */
704 /* Set Tx DMA */
705 param = &priv->param_tx;
706 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900707 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
708
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900709 param->tx_reg = port->mapbase + UART_TX;
710 chan = dma_request_channel(mask, filter, param);
711 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900712 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
713 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900714 return;
715 }
716 priv->chan_tx = chan;
717
718 /* Set Rx DMA */
719 param = &priv->param_rx;
720 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900721 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
722
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900723 param->rx_reg = port->mapbase + UART_RX;
724 chan = dma_request_channel(mask, filter, param);
725 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900726 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
727 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900728 dma_release_channel(priv->chan_tx);
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +0900729 priv->chan_tx = NULL;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900730 return;
731 }
732
733 /* Get Consistent memory for DMA */
734 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
735 &priv->rx_buf_dma, GFP_KERNEL);
736 priv->chan_rx = chan;
737}
738
739static void pch_dma_rx_complete(void *arg)
740{
741 struct eg20t_port *priv = arg;
742 struct uart_port *port = &priv->port;
743 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900744 int count;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900745
746 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900747 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900748 return;
749 }
750
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900751 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
752 count = dma_push_rx(priv, priv->trigger_level);
753 if (count)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900754 tty_flip_buffer_push(tty);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900755 tty_kref_put(tty);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900756 async_tx_ack(priv->desc_rx);
Tomoya MORINAGA439ec7b2012-07-06 17:19:42 +0900757 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
758 PCH_UART_HAL_RX_ERR_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900759}
760
761static void pch_dma_tx_complete(void *arg)
762{
763 struct eg20t_port *priv = arg;
764 struct uart_port *port = &priv->port;
765 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900766 struct scatterlist *sg = priv->sg_tx_p;
767 int i;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900768
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900769 for (i = 0; i < priv->nent; i++, sg++) {
770 xmit->tail += sg_dma_len(sg);
771 port->icount.tx += sg_dma_len(sg);
772 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900773 xmit->tail &= UART_XMIT_SIZE - 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900774 async_tx_ack(priv->desc_tx);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900775 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900776 priv->tx_dma_use = 0;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900777 priv->nent = 0;
778 kfree(priv->sg_tx_p);
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900779 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900780}
781
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900782static int pop_tx(struct eg20t_port *priv, int size)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900783{
784 int count = 0;
785 struct uart_port *port = &priv->port;
786 struct circ_buf *xmit = &port->state->xmit;
787
788 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
789 goto pop_tx_end;
790
791 do {
792 int cnt_to_end =
793 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
794 int sz = min(size - count, cnt_to_end);
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900795 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900796 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
797 count += sz;
798 } while (!uart_circ_empty(xmit) && count < size);
799
800pop_tx_end:
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900801 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900802 count, size - count, jiffies);
803
804 return count;
805}
806
807static int handle_rx_to(struct eg20t_port *priv)
808{
809 struct pch_uart_buffer *buf;
810 int rx_size;
811 int ret;
812 if (!priv->start_rx) {
Tomoya MORINAGA439ec7b2012-07-06 17:19:42 +0900813 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
814 PCH_UART_HAL_RX_ERR_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900815 return 0;
816 }
817 buf = &priv->rxbuf;
818 do {
819 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
820 ret = push_rx(priv, buf->buf, rx_size);
821 if (ret)
822 return 0;
823 } while (rx_size == buf->size);
824
825 return PCH_UART_HANDLED_RX_INT;
826}
827
828static int handle_rx(struct eg20t_port *priv)
829{
830 return handle_rx_to(priv);
831}
832
833static int dma_handle_rx(struct eg20t_port *priv)
834{
835 struct uart_port *port = &priv->port;
836 struct dma_async_tx_descriptor *desc;
837 struct scatterlist *sg;
838
839 priv = container_of(port, struct eg20t_port, port);
840 sg = &priv->sg_rx;
841
842 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
843
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900844 sg_dma_len(sg) = priv->trigger_level;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900845
846 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900847 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
848 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900849
850 sg_dma_address(sg) = priv->rx_buf_dma;
851
Alexandre Bounine16052822012-03-08 16:11:18 -0500852 desc = dmaengine_prep_slave_sg(priv->chan_rx,
Vinod Koula485df42011-10-14 10:47:38 +0530853 sg, 1, DMA_DEV_TO_MEM,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900854 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
855
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900856 if (!desc)
857 return 0;
858
859 priv->desc_rx = desc;
860 desc->callback = pch_dma_rx_complete;
861 desc->callback_param = priv;
862 desc->tx_submit(desc);
863 dma_async_issue_pending(priv->chan_rx);
864
865 return PCH_UART_HANDLED_RX_INT;
866}
867
868static unsigned int handle_tx(struct eg20t_port *priv)
869{
870 struct uart_port *port = &priv->port;
871 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900872 int fifo_size;
873 int tx_size;
874 int size;
875 int tx_empty;
876
877 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900878 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
879 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900880 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
881 priv->tx_empty = 1;
882 return 0;
883 }
884
885 fifo_size = max(priv->fifo_size, 1);
886 tx_empty = 1;
887 if (pop_tx_x(priv, xmit->buf)) {
888 pch_uart_hal_write(priv, xmit->buf, 1);
889 port->icount.tx++;
890 tx_empty = 0;
891 fifo_size--;
892 }
893 size = min(xmit->head - xmit->tail, fifo_size);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900894 if (size < 0)
895 size = fifo_size;
896
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900897 tx_size = pop_tx(priv, size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900898 if (tx_size > 0) {
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900899 port->icount.tx += tx_size;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900900 tx_empty = 0;
901 }
902
903 priv->tx_empty = tx_empty;
904
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900905 if (tx_empty) {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900906 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900907 uart_write_wakeup(port);
908 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900909
910 return PCH_UART_HANDLED_TX_INT;
911}
912
913static unsigned int dma_handle_tx(struct eg20t_port *priv)
914{
915 struct uart_port *port = &priv->port;
916 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900917 struct scatterlist *sg;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900918 int nent;
919 int fifo_size;
920 int tx_empty;
921 struct dma_async_tx_descriptor *desc;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900922 int num;
923 int i;
924 int bytes;
925 int size;
926 int rem;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900927
928 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900929 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
930 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900931 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
932 priv->tx_empty = 1;
933 return 0;
934 }
935
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900936 if (priv->tx_dma_use) {
937 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
938 __func__, jiffies);
939 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
940 priv->tx_empty = 1;
941 return 0;
942 }
943
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900944 fifo_size = max(priv->fifo_size, 1);
945 tx_empty = 1;
946 if (pop_tx_x(priv, xmit->buf)) {
947 pch_uart_hal_write(priv, xmit->buf, 1);
948 port->icount.tx++;
949 tx_empty = 0;
950 fifo_size--;
951 }
952
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900953 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
954 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
955 xmit->tail, UART_XMIT_SIZE));
956 if (!bytes) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900957 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900958 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
959 uart_write_wakeup(port);
960 return 0;
961 }
962
963 if (bytes > fifo_size) {
964 num = bytes / fifo_size + 1;
965 size = fifo_size;
966 rem = bytes % fifo_size;
967 } else {
968 num = 1;
969 size = bytes;
970 rem = bytes;
971 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900972
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900973 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
974 __func__, num, size, rem);
975
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900976 priv->tx_dma_use = 1;
977
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900978 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900979
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900980 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
981 sg = priv->sg_tx_p;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900982
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900983 for (i = 0; i < num; i++, sg++) {
984 if (i == (num - 1))
985 sg_set_page(sg, virt_to_page(xmit->buf),
986 rem, fifo_size * i);
987 else
988 sg_set_page(sg, virt_to_page(xmit->buf),
989 size, fifo_size * i);
990 }
991
992 sg = priv->sg_tx_p;
993 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900994 if (!nent) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900995 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900996 return 0;
997 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900998 priv->nent = nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900999
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001000 for (i = 0; i < nent; i++, sg++) {
1001 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
1002 fifo_size * i;
1003 sg_dma_address(sg) = (sg_dma_address(sg) &
1004 ~(UART_XMIT_SIZE - 1)) + sg->offset;
1005 if (i == (nent - 1))
1006 sg_dma_len(sg) = rem;
1007 else
1008 sg_dma_len(sg) = size;
1009 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001010
Alexandre Bounine16052822012-03-08 16:11:18 -05001011 desc = dmaengine_prep_slave_sg(priv->chan_tx,
Vinod Koula485df42011-10-14 10:47:38 +05301012 priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001013 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001014 if (!desc) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001015 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
1016 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001017 return 0;
1018 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001019 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001020 priv->desc_tx = desc;
1021 desc->callback = pch_dma_tx_complete;
1022 desc->callback_param = priv;
1023
1024 desc->tx_submit(desc);
1025
1026 dma_async_issue_pending(priv->chan_tx);
1027
1028 return PCH_UART_HANDLED_TX_INT;
1029}
1030
1031static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
1032{
1033 u8 fcr = ioread8(priv->membase + UART_FCR);
1034
1035 /* Reset FIFO */
1036 fcr |= UART_FCR_CLEAR_RCVR;
1037 iowrite8(fcr, priv->membase + UART_FCR);
1038
1039 if (lsr & PCH_UART_LSR_ERR)
1040 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
1041
1042 if (lsr & UART_LSR_FE)
1043 dev_err(&priv->pdev->dev, "Framing Error\n");
1044
1045 if (lsr & UART_LSR_PE)
1046 dev_err(&priv->pdev->dev, "Parity Error\n");
1047
1048 if (lsr & UART_LSR_OE)
1049 dev_err(&priv->pdev->dev, "Overrun Error\n");
1050}
1051
1052static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1053{
1054 struct eg20t_port *priv = dev_id;
1055 unsigned int handled;
1056 u8 lsr;
1057 int ret = 0;
1058 unsigned int iid;
1059 unsigned long flags;
1060
1061 spin_lock_irqsave(&priv->port.lock, flags);
1062 handled = 0;
1063 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
1064 switch (iid) {
1065 case PCH_UART_IID_RLS: /* Receiver Line Status */
1066 lsr = pch_uart_hal_get_line_status(priv);
1067 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1068 UART_LSR_PE | UART_LSR_OE)) {
1069 pch_uart_err_ir(priv, lsr);
1070 ret = PCH_UART_HANDLED_RX_ERR_INT;
1071 }
1072 break;
1073 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001074 if (priv->use_dma) {
1075 pch_uart_hal_disable_interrupt(priv,
Tomoya MORINAGA439ec7b2012-07-06 17:19:42 +09001076 PCH_UART_HAL_RX_INT |
1077 PCH_UART_HAL_RX_ERR_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001078 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001079 if (!ret)
1080 pch_uart_hal_enable_interrupt(priv,
Tomoya MORINAGA439ec7b2012-07-06 17:19:42 +09001081 PCH_UART_HAL_RX_INT |
1082 PCH_UART_HAL_RX_ERR_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001083 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001084 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001085 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001086 break;
1087 case PCH_UART_IID_RDR_TO: /* Received Data Ready
1088 (FIFO Timeout) */
1089 ret = handle_rx_to(priv);
1090 break;
1091 case PCH_UART_IID_THRE: /* Transmitter Holding Register
1092 Empty */
1093 if (priv->use_dma)
1094 ret = dma_handle_tx(priv);
1095 else
1096 ret = handle_tx(priv);
1097 break;
1098 case PCH_UART_IID_MS: /* Modem Status */
1099 ret = PCH_UART_HANDLED_MS_INT;
1100 break;
1101 default: /* Never junp to this label */
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001102 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
1103 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001104 ret = -1;
1105 break;
1106 }
1107 handled |= (unsigned int)ret;
1108 }
1109 if (handled == 0 && iid <= 1) {
1110 if (priv->int_dis_flag)
1111 priv->int_dis_flag = 0;
1112 }
1113
1114 spin_unlock_irqrestore(&priv->port.lock, flags);
1115 return IRQ_RETVAL(handled);
1116}
1117
1118/* This function tests whether the transmitter fifo and shifter for the port
1119 described by 'port' is empty. */
1120static unsigned int pch_uart_tx_empty(struct uart_port *port)
1121{
1122 struct eg20t_port *priv;
Feng Tang30c6c6b2012-02-06 17:24:44 +08001123
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001124 priv = container_of(port, struct eg20t_port, port);
1125 if (priv->tx_empty)
Feng Tang30c6c6b2012-02-06 17:24:44 +08001126 return TIOCSER_TEMT;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001127 else
Feng Tang30c6c6b2012-02-06 17:24:44 +08001128 return 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001129}
1130
1131/* Returns the current state of modem control inputs. */
1132static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1133{
1134 struct eg20t_port *priv;
1135 u8 modem;
1136 unsigned int ret = 0;
1137
1138 priv = container_of(port, struct eg20t_port, port);
1139 modem = pch_uart_hal_get_modem(priv);
1140
1141 if (modem & UART_MSR_DCD)
1142 ret |= TIOCM_CAR;
1143
1144 if (modem & UART_MSR_RI)
1145 ret |= TIOCM_RNG;
1146
1147 if (modem & UART_MSR_DSR)
1148 ret |= TIOCM_DSR;
1149
1150 if (modem & UART_MSR_CTS)
1151 ret |= TIOCM_CTS;
1152
1153 return ret;
1154}
1155
1156static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1157{
1158 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001159 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1160
1161 if (mctrl & TIOCM_DTR)
1162 mcr |= UART_MCR_DTR;
1163 if (mctrl & TIOCM_RTS)
1164 mcr |= UART_MCR_RTS;
1165 if (mctrl & TIOCM_LOOP)
1166 mcr |= UART_MCR_LOOP;
1167
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001168 if (priv->mcr & UART_MCR_AFE)
1169 mcr |= UART_MCR_AFE;
1170
1171 if (mctrl)
1172 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001173}
1174
1175static void pch_uart_stop_tx(struct uart_port *port)
1176{
1177 struct eg20t_port *priv;
1178 priv = container_of(port, struct eg20t_port, port);
1179 priv->start_tx = 0;
1180 priv->tx_dma_use = 0;
1181}
1182
1183static void pch_uart_start_tx(struct uart_port *port)
1184{
1185 struct eg20t_port *priv;
1186
1187 priv = container_of(port, struct eg20t_port, port);
1188
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001189 if (priv->use_dma) {
1190 if (priv->tx_dma_use) {
1191 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1192 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001193 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001194 }
1195 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001196
1197 priv->start_tx = 1;
1198 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1199}
1200
1201static void pch_uart_stop_rx(struct uart_port *port)
1202{
1203 struct eg20t_port *priv;
1204 priv = container_of(port, struct eg20t_port, port);
1205 priv->start_rx = 0;
Tomoya MORINAGA439ec7b2012-07-06 17:19:42 +09001206 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
1207 PCH_UART_HAL_RX_ERR_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001208 priv->int_dis_flag = 1;
1209}
1210
1211/* Enable the modem status interrupts. */
1212static void pch_uart_enable_ms(struct uart_port *port)
1213{
1214 struct eg20t_port *priv;
1215 priv = container_of(port, struct eg20t_port, port);
1216 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1217}
1218
1219/* Control the transmission of a break signal. */
1220static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1221{
1222 struct eg20t_port *priv;
1223 unsigned long flags;
1224
1225 priv = container_of(port, struct eg20t_port, port);
1226 spin_lock_irqsave(&port->lock, flags);
1227 pch_uart_hal_set_break(priv, ctl);
1228 spin_unlock_irqrestore(&port->lock, flags);
1229}
1230
1231/* Grab any interrupt resources and initialise any low level driver state. */
1232static int pch_uart_startup(struct uart_port *port)
1233{
1234 struct eg20t_port *priv;
1235 int ret;
1236 int fifo_size;
1237 int trigger_level;
1238
1239 priv = container_of(port, struct eg20t_port, port);
1240 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001241
1242 if (port->uartclk)
Darren Harta8a3ec92012-03-09 09:51:48 -08001243 priv->uartclk = port->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001244 else
Darren Harta8a3ec92012-03-09 09:51:48 -08001245 port->uartclk = priv->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001246
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001247 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1248 ret = pch_uart_hal_set_line(priv, default_baud,
1249 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1250 PCH_UART_HAL_STB1);
1251 if (ret)
1252 return ret;
1253
1254 switch (priv->fifo_size) {
1255 case 256:
1256 fifo_size = PCH_UART_HAL_FIFO256;
1257 break;
1258 case 64:
1259 fifo_size = PCH_UART_HAL_FIFO64;
1260 break;
1261 case 16:
1262 fifo_size = PCH_UART_HAL_FIFO16;
Alan Cox17361b32012-07-02 18:51:38 +01001263 break;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001264 case 1:
1265 default:
1266 fifo_size = PCH_UART_HAL_FIFO_DIS;
1267 break;
1268 }
1269
1270 switch (priv->trigger) {
1271 case PCH_UART_HAL_TRIGGER1:
1272 trigger_level = 1;
1273 break;
1274 case PCH_UART_HAL_TRIGGER_L:
1275 trigger_level = priv->fifo_size / 4;
1276 break;
1277 case PCH_UART_HAL_TRIGGER_M:
1278 trigger_level = priv->fifo_size / 2;
1279 break;
1280 case PCH_UART_HAL_TRIGGER_H:
1281 default:
1282 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1283 break;
1284 }
1285
1286 priv->trigger_level = trigger_level;
1287 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1288 fifo_size, priv->trigger);
1289 if (ret < 0)
1290 return ret;
1291
1292 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1293 KBUILD_MODNAME, priv);
1294 if (ret < 0)
1295 return ret;
1296
1297 if (priv->use_dma)
1298 pch_request_dma(port);
1299
1300 priv->start_rx = 1;
Tomoya MORINAGA439ec7b2012-07-06 17:19:42 +09001301 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
1302 PCH_UART_HAL_RX_ERR_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001303 uart_update_timeout(port, CS8, default_baud);
1304
1305 return 0;
1306}
1307
1308static void pch_uart_shutdown(struct uart_port *port)
1309{
1310 struct eg20t_port *priv;
1311 int ret;
1312
1313 priv = container_of(port, struct eg20t_port, port);
1314 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1315 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1316 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1317 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1318 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001319 dev_err(priv->port.dev,
1320 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001321
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +09001322 pch_free_dma(port);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001323
1324 free_irq(priv->port.irq, priv);
1325}
1326
1327/* Change the port parameters, including word length, parity, stop
1328 *bits. Update read_status_mask and ignore_status_mask to indicate
1329 *the types of events we are interested in receiving. */
1330static void pch_uart_set_termios(struct uart_port *port,
1331 struct ktermios *termios, struct ktermios *old)
1332{
1333 int baud;
1334 int rtn;
1335 unsigned int parity, bits, stb;
1336 struct eg20t_port *priv;
1337 unsigned long flags;
1338
1339 priv = container_of(port, struct eg20t_port, port);
1340 switch (termios->c_cflag & CSIZE) {
1341 case CS5:
1342 bits = PCH_UART_HAL_5BIT;
1343 break;
1344 case CS6:
1345 bits = PCH_UART_HAL_6BIT;
1346 break;
1347 case CS7:
1348 bits = PCH_UART_HAL_7BIT;
1349 break;
1350 default: /* CS8 */
1351 bits = PCH_UART_HAL_8BIT;
1352 break;
1353 }
1354 if (termios->c_cflag & CSTOPB)
1355 stb = PCH_UART_HAL_STB2;
1356 else
1357 stb = PCH_UART_HAL_STB1;
1358
1359 if (termios->c_cflag & PARENB) {
Tomoya MORINAGA3dcb5362012-07-06 17:19:43 +09001360 if (termios->c_cflag & PARODD)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001361 parity = PCH_UART_HAL_PARITY_ODD;
1362 else
1363 parity = PCH_UART_HAL_PARITY_EVEN;
1364
Feng Tang30c6c6b2012-02-06 17:24:44 +08001365 } else
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001366 parity = PCH_UART_HAL_PARITY_NONE;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001367
1368 /* Only UART0 has auto hardware flow function */
1369 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1370 priv->mcr |= UART_MCR_AFE;
1371 else
1372 priv->mcr &= ~UART_MCR_AFE;
1373
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001374 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1375
1376 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1377
1378 spin_lock_irqsave(&port->lock, flags);
1379
1380 uart_update_timeout(port, termios->c_cflag, baud);
1381 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1382 if (rtn)
1383 goto out;
1384
Tomoya MORINAGAa1d7cfe2011-10-27 15:45:18 +09001385 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001386 /* Don't rewrite B0 */
1387 if (tty_termios_baud_rate(termios))
1388 tty_termios_encode_baud_rate(termios, baud, baud);
1389
1390out:
1391 spin_unlock_irqrestore(&port->lock, flags);
1392}
1393
1394static const char *pch_uart_type(struct uart_port *port)
1395{
1396 return KBUILD_MODNAME;
1397}
1398
1399static void pch_uart_release_port(struct uart_port *port)
1400{
1401 struct eg20t_port *priv;
1402
1403 priv = container_of(port, struct eg20t_port, port);
1404 pci_iounmap(priv->pdev, priv->membase);
1405 pci_release_regions(priv->pdev);
1406}
1407
1408static int pch_uart_request_port(struct uart_port *port)
1409{
1410 struct eg20t_port *priv;
1411 int ret;
1412 void __iomem *membase;
1413
1414 priv = container_of(port, struct eg20t_port, port);
1415 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1416 if (ret < 0)
1417 return -EBUSY;
1418
1419 membase = pci_iomap(priv->pdev, 1, 0);
1420 if (!membase) {
1421 pci_release_regions(priv->pdev);
1422 return -EBUSY;
1423 }
1424 priv->membase = port->membase = membase;
1425
1426 return 0;
1427}
1428
1429static void pch_uart_config_port(struct uart_port *port, int type)
1430{
1431 struct eg20t_port *priv;
1432
1433 priv = container_of(port, struct eg20t_port, port);
1434 if (type & UART_CONFIG_TYPE) {
1435 port->type = priv->port_type;
1436 pch_uart_request_port(port);
1437 }
1438}
1439
1440static int pch_uart_verify_port(struct uart_port *port,
1441 struct serial_struct *serinfo)
1442{
1443 struct eg20t_port *priv;
1444
1445 priv = container_of(port, struct eg20t_port, port);
1446 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001447 dev_info(priv->port.dev,
1448 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001449 priv->use_dma = 0;
1450 serinfo->flags &= ~UPF_LOW_LATENCY;
1451 } else {
1452#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001453 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1454 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001455 return -EOPNOTSUPP;
1456#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001457 priv->use_dma_flag = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001458 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGAaf6d17c2012-04-12 10:47:50 +09001459 if (!priv->use_dma)
1460 pch_request_dma(port);
1461 priv->use_dma = 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001462 }
1463
1464 return 0;
1465}
1466
1467static struct uart_ops pch_uart_ops = {
1468 .tx_empty = pch_uart_tx_empty,
1469 .set_mctrl = pch_uart_set_mctrl,
1470 .get_mctrl = pch_uart_get_mctrl,
1471 .stop_tx = pch_uart_stop_tx,
1472 .start_tx = pch_uart_start_tx,
1473 .stop_rx = pch_uart_stop_rx,
1474 .enable_ms = pch_uart_enable_ms,
1475 .break_ctl = pch_uart_break_ctl,
1476 .startup = pch_uart_startup,
1477 .shutdown = pch_uart_shutdown,
1478 .set_termios = pch_uart_set_termios,
1479/* .pm = pch_uart_pm, Not supported yet */
1480/* .set_wake = pch_uart_set_wake, Not supported yet */
1481 .type = pch_uart_type,
1482 .release_port = pch_uart_release_port,
1483 .request_port = pch_uart_request_port,
1484 .config_port = pch_uart_config_port,
1485 .verify_port = pch_uart_verify_port
1486};
1487
Alexander Steine30f8672011-11-15 15:04:07 -08001488#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1489
1490/*
1491 * Wait for transmitter & holding register to empty
1492 */
1493static void wait_for_xmitr(struct eg20t_port *up, int bits)
1494{
1495 unsigned int status, tmout = 10000;
1496
1497 /* Wait up to 10ms for the character(s) to be sent. */
1498 for (;;) {
1499 status = ioread8(up->membase + UART_LSR);
1500
1501 if ((status & bits) == bits)
1502 break;
1503 if (--tmout == 0)
1504 break;
1505 udelay(1);
1506 }
1507
1508 /* Wait up to 1s for flow control if necessary */
1509 if (up->port.flags & UPF_CONS_FLOW) {
1510 unsigned int tmout;
1511 for (tmout = 1000000; tmout; tmout--) {
1512 unsigned int msr = ioread8(up->membase + UART_MSR);
1513 if (msr & UART_MSR_CTS)
1514 break;
1515 udelay(1);
1516 touch_nmi_watchdog();
1517 }
1518 }
1519}
1520
1521static void pch_console_putchar(struct uart_port *port, int ch)
1522{
1523 struct eg20t_port *priv =
1524 container_of(port, struct eg20t_port, port);
1525
1526 wait_for_xmitr(priv, UART_LSR_THRE);
1527 iowrite8(ch, priv->membase + PCH_UART_THR);
1528}
1529
1530/*
1531 * Print a string to the serial port trying not to disturb
1532 * any possible real use of the port...
1533 *
1534 * The console_lock must be held when we get here.
1535 */
1536static void
1537pch_console_write(struct console *co, const char *s, unsigned int count)
1538{
1539 struct eg20t_port *priv;
Alexander Steine30f8672011-11-15 15:04:07 -08001540 unsigned long flags;
1541 u8 ier;
1542 int locked = 1;
1543
1544 priv = pch_uart_ports[co->index];
1545
1546 touch_nmi_watchdog();
1547
1548 local_irq_save(flags);
1549 if (priv->port.sysrq) {
1550 /* serial8250_handle_port() already took the lock */
1551 locked = 0;
1552 } else if (oops_in_progress) {
1553 locked = spin_trylock(&priv->port.lock);
1554 } else
1555 spin_lock(&priv->port.lock);
1556
1557 /*
1558 * First save the IER then disable the interrupts
1559 */
1560 ier = ioread8(priv->membase + UART_IER);
1561
1562 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1563
1564 uart_console_write(&priv->port, s, count, pch_console_putchar);
1565
1566 /*
1567 * Finally, wait for transmitter to become empty
1568 * and restore the IER
1569 */
1570 wait_for_xmitr(priv, BOTH_EMPTY);
1571 iowrite8(ier, priv->membase + UART_IER);
1572
1573 if (locked)
1574 spin_unlock(&priv->port.lock);
1575 local_irq_restore(flags);
1576}
1577
1578static int __init pch_console_setup(struct console *co, char *options)
1579{
1580 struct uart_port *port;
Darren Hart7ce92512012-03-09 09:51:51 -08001581 int baud = default_baud;
Alexander Steine30f8672011-11-15 15:04:07 -08001582 int bits = 8;
1583 int parity = 'n';
1584 int flow = 'n';
1585
1586 /*
1587 * Check whether an invalid uart number has been specified, and
1588 * if so, search for the first available port that does have
1589 * console support.
1590 */
1591 if (co->index >= PCH_UART_NR)
1592 co->index = 0;
1593 port = &pch_uart_ports[co->index]->port;
1594
1595 if (!port || (!port->iobase && !port->membase))
1596 return -ENODEV;
1597
Darren Hart077175f2012-03-09 09:51:49 -08001598 port->uartclk = pch_uart_get_uartclk();
Alexander Steine30f8672011-11-15 15:04:07 -08001599
1600 if (options)
1601 uart_parse_options(options, &baud, &parity, &bits, &flow);
1602
1603 return uart_set_options(port, co, baud, parity, bits, flow);
1604}
1605
1606static struct uart_driver pch_uart_driver;
1607
1608static struct console pch_console = {
1609 .name = PCH_UART_DRIVER_DEVICE,
1610 .write = pch_console_write,
1611 .device = uart_console_device,
1612 .setup = pch_console_setup,
1613 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1614 .index = -1,
1615 .data = &pch_uart_driver,
1616};
1617
1618#define PCH_CONSOLE (&pch_console)
1619#else
1620#define PCH_CONSOLE NULL
1621#endif
1622
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001623static struct uart_driver pch_uart_driver = {
1624 .owner = THIS_MODULE,
1625 .driver_name = KBUILD_MODNAME,
1626 .dev_name = PCH_UART_DRIVER_DEVICE,
1627 .major = 0,
1628 .minor = 0,
1629 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001630 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001631};
1632
1633static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001634 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001635{
1636 struct eg20t_port *priv;
1637 int ret;
1638 unsigned int iobase;
1639 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001640 unsigned char *rxbuf;
Darren Hart077175f2012-03-09 09:51:49 -08001641 int fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001642 int port_type;
1643 struct pch_uart_driver_data *board;
Feng Tangd0114112012-02-06 17:24:43 +08001644 char name[32]; /* for debugfs file name */
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001645
1646 board = &drv_dat[id->driver_data];
1647 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001648
1649 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1650 if (priv == NULL)
1651 goto init_port_alloc_err;
1652
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001653 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001654 if (!rxbuf)
1655 goto init_port_free_txbuf;
1656
1657 switch (port_type) {
1658 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001659 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001660 break;
1661 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001662 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001663 break;
1664 default:
1665 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1666 goto init_port_hal_free;
1667 }
1668
Alexander Steine4635952011-07-04 08:58:31 +02001669 pci_enable_msi(pdev);
Tomoya MORINAGA867c9022012-04-02 14:36:22 +09001670 pci_set_master(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001671
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001672 iobase = pci_resource_start(pdev, 0);
1673 mapbase = pci_resource_start(pdev, 1);
1674 priv->mapbase = mapbase;
1675 priv->iobase = iobase;
1676 priv->pdev = pdev;
1677 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001678 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001679 priv->rxbuf.size = PAGE_SIZE;
1680
1681 priv->fifo_size = fifosize;
Darren Hart077175f2012-03-09 09:51:49 -08001682 priv->uartclk = pch_uart_get_uartclk();
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001683 priv->port_type = PORT_MAX_8250 + port_type + 1;
1684 priv->port.dev = &pdev->dev;
1685 priv->port.iobase = iobase;
1686 priv->port.membase = NULL;
1687 priv->port.mapbase = mapbase;
1688 priv->port.irq = pdev->irq;
1689 priv->port.iotype = UPIO_PORT;
1690 priv->port.ops = &pch_uart_ops;
1691 priv->port.flags = UPF_BOOT_AUTOCONF;
1692 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001693 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001694 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1695
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001696 spin_lock_init(&priv->port.lock);
1697
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001698 pci_set_drvdata(pdev, priv);
Feng Tang6f56d0f2012-02-06 17:24:45 +08001699 priv->trigger_level = 1;
1700 priv->fcr = 0;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001701
Alexander Steine30f8672011-11-15 15:04:07 -08001702#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1703 pch_uart_ports[board->line_no] = priv;
1704#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001705 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1706 if (ret < 0)
1707 goto init_port_hal_free;
1708
Feng Tangd0114112012-02-06 17:24:43 +08001709#ifdef CONFIG_DEBUG_FS
1710 snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
1711 priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
1712 NULL, priv, &port_regs_ops);
1713#endif
1714
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001715 return priv;
1716
1717init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001718#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1719 pch_uart_ports[board->line_no] = NULL;
1720#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001721 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001722init_port_free_txbuf:
1723 kfree(priv);
1724init_port_alloc_err:
1725
1726 return NULL;
1727}
1728
1729static void pch_uart_exit_port(struct eg20t_port *priv)
1730{
Feng Tangd0114112012-02-06 17:24:43 +08001731
1732#ifdef CONFIG_DEBUG_FS
1733 if (priv->debugfs)
1734 debugfs_remove(priv->debugfs);
1735#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001736 uart_remove_one_port(&pch_uart_driver, &priv->port);
1737 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001738 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001739}
1740
1741static void pch_uart_pci_remove(struct pci_dev *pdev)
1742{
Feng Tang6f56d0f2012-02-06 17:24:45 +08001743 struct eg20t_port *priv = pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001744
1745 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001746
1747#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1748 pch_uart_ports[priv->port.line] = NULL;
1749#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001750 pch_uart_exit_port(priv);
1751 pci_disable_device(pdev);
1752 kfree(priv);
1753 return;
1754}
1755#ifdef CONFIG_PM
1756static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1757{
1758 struct eg20t_port *priv = pci_get_drvdata(pdev);
1759
1760 uart_suspend_port(&pch_uart_driver, &priv->port);
1761
1762 pci_save_state(pdev);
1763 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1764 return 0;
1765}
1766
1767static int pch_uart_pci_resume(struct pci_dev *pdev)
1768{
1769 struct eg20t_port *priv = pci_get_drvdata(pdev);
1770 int ret;
1771
1772 pci_set_power_state(pdev, PCI_D0);
1773 pci_restore_state(pdev);
1774
1775 ret = pci_enable_device(pdev);
1776 if (ret) {
1777 dev_err(&pdev->dev,
1778 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1779 return ret;
1780 }
1781
1782 uart_resume_port(&pch_uart_driver, &priv->port);
1783
1784 return 0;
1785}
1786#else
1787#define pch_uart_pci_suspend NULL
1788#define pch_uart_pci_resume NULL
1789#endif
1790
1791static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1792 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001793 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001794 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001795 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001796 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001797 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001798 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001799 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001800 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001801 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001802 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001803 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001804 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001805 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001806 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1807 .driver_data = pch_ml7223_uart0},
1808 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1809 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +09001810 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1811 .driver_data = pch_ml7831_uart0},
1812 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1813 .driver_data = pch_ml7831_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001814 {0,},
1815};
1816
1817static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1818 const struct pci_device_id *id)
1819{
1820 int ret;
1821 struct eg20t_port *priv;
1822
1823 ret = pci_enable_device(pdev);
1824 if (ret < 0)
1825 goto probe_error;
1826
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001827 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001828 if (!priv) {
1829 ret = -EBUSY;
1830 goto probe_disable_device;
1831 }
1832 pci_set_drvdata(pdev, priv);
1833
1834 return ret;
1835
1836probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001837 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001838 pci_disable_device(pdev);
1839probe_error:
1840 return ret;
1841}
1842
1843static struct pci_driver pch_uart_pci_driver = {
1844 .name = "pch_uart",
1845 .id_table = pch_uart_pci_id,
1846 .probe = pch_uart_pci_probe,
1847 .remove = __devexit_p(pch_uart_pci_remove),
1848 .suspend = pch_uart_pci_suspend,
1849 .resume = pch_uart_pci_resume,
1850};
1851
1852static int __init pch_uart_module_init(void)
1853{
1854 int ret;
1855
1856 /* register as UART driver */
1857 ret = uart_register_driver(&pch_uart_driver);
1858 if (ret < 0)
1859 return ret;
1860
1861 /* register as PCI driver */
1862 ret = pci_register_driver(&pch_uart_pci_driver);
1863 if (ret < 0)
1864 uart_unregister_driver(&pch_uart_driver);
1865
1866 return ret;
1867}
1868module_init(pch_uart_module_init);
1869
1870static void __exit pch_uart_module_exit(void)
1871{
1872 pci_unregister_driver(&pch_uart_pci_driver);
1873 uart_unregister_driver(&pch_uart_driver);
1874}
1875module_exit(pch_uart_module_exit);
1876
1877MODULE_LICENSE("GPL v2");
1878MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1879module_param(default_baud, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001880MODULE_PARM_DESC(default_baud,
1881 "Default BAUD for initial driver state and console (default 9600)");
Darren Hart2a44feb2012-03-09 09:51:50 -08001882module_param(user_uartclk, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001883MODULE_PARM_DESC(user_uartclk,
1884 "Override UART default or board specific UART clock");