blob: 110595ed33f5af47a6b07663492650667480d83c [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 */
Alexander Steine30f8672011-11-15 15:04:07 -0800213
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900214struct pch_uart_buffer {
215 unsigned char *buf;
216 int size;
217};
218
219struct eg20t_port {
220 struct uart_port port;
221 int port_type;
222 void __iomem *membase;
223 resource_size_t mapbase;
224 unsigned int iobase;
225 struct pci_dev *pdev;
226 int fifo_size;
Darren Harta8a3ec92012-03-09 09:51:48 -0800227 int uartclk;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900228 int start_tx;
229 int start_rx;
230 int tx_empty;
231 int int_dis_flag;
232 int trigger;
233 int trigger_level;
234 struct pch_uart_buffer rxbuf;
235 unsigned int dmsr;
236 unsigned int fcr;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +0900237 unsigned int mcr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900238 unsigned int use_dma;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900239 struct dma_async_tx_descriptor *desc_tx;
240 struct dma_async_tx_descriptor *desc_rx;
241 struct pch_dma_slave param_tx;
242 struct pch_dma_slave param_rx;
243 struct dma_chan *chan_tx;
244 struct dma_chan *chan_rx;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900245 struct scatterlist *sg_tx_p;
246 int nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900247 struct scatterlist sg_rx;
248 int tx_dma_use;
249 void *rx_buf_virt;
250 dma_addr_t rx_buf_dma;
Feng Tangd0114112012-02-06 17:24:43 +0800251
252 struct dentry *debugfs;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900253};
254
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900255/**
256 * struct pch_uart_driver_data - private data structure for UART-DMA
257 * @port_type: The number of DMA channel
258 * @line_no: UART port line number (0, 1, 2...)
259 */
260struct pch_uart_driver_data {
261 int port_type;
262 int line_no;
263};
264
265enum pch_uart_num_t {
266 pch_et20t_uart0 = 0,
267 pch_et20t_uart1,
268 pch_et20t_uart2,
269 pch_et20t_uart3,
270 pch_ml7213_uart0,
271 pch_ml7213_uart1,
272 pch_ml7213_uart2,
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900273 pch_ml7223_uart0,
274 pch_ml7223_uart1,
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900275 pch_ml7831_uart0,
276 pch_ml7831_uart1,
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900277};
278
279static struct pch_uart_driver_data drv_dat[] = {
280 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
281 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
282 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
283 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
284 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
285 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
286 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900287 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
288 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900289 [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
290 [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900291};
292
Alexander Steine30f8672011-11-15 15:04:07 -0800293#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
294static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
295#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900296static unsigned int default_baud = 9600;
Darren Hart2a44feb2012-03-09 09:51:50 -0800297static unsigned int user_uartclk = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900298static const int trigger_level_256[4] = { 1, 64, 128, 224 };
299static const int trigger_level_64[4] = { 1, 16, 32, 56 };
300static const int trigger_level_16[4] = { 1, 4, 8, 14 };
301static const int trigger_level_1[4] = { 1, 1, 1, 1 };
302
Feng Tangd0114112012-02-06 17:24:43 +0800303#ifdef CONFIG_DEBUG_FS
304
305#define PCH_REGS_BUFSIZE 1024
Stephen Boyd234e3402012-04-05 14:25:11 -0700306
Feng Tangd0114112012-02-06 17:24:43 +0800307
308static ssize_t port_show_regs(struct file *file, char __user *user_buf,
309 size_t count, loff_t *ppos)
310{
311 struct eg20t_port *priv = file->private_data;
312 char *buf;
313 u32 len = 0;
314 ssize_t ret;
315 unsigned char lcr;
316
317 buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
318 if (!buf)
319 return 0;
320
321 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
322 "PCH EG20T port[%d] regs:\n", priv->port.line);
323
324 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
325 "=================================\n");
326 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
327 "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
328 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
329 "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
330 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
331 "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
332 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
333 "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
334 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
335 "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
336 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
337 "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
338 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
339 "BRCSR: \t0x%02x\n",
340 ioread8(priv->membase + PCH_UART_BRCSR));
341
342 lcr = ioread8(priv->membase + UART_LCR);
343 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
344 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
345 "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
346 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
347 "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
348 iowrite8(lcr, priv->membase + UART_LCR);
349
350 if (len > PCH_REGS_BUFSIZE)
351 len = PCH_REGS_BUFSIZE;
352
353 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
354 kfree(buf);
355 return ret;
356}
357
358static const struct file_operations port_regs_ops = {
359 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700360 .open = simple_open,
Feng Tangd0114112012-02-06 17:24:43 +0800361 .read = port_show_regs,
362 .llseek = default_llseek,
363};
364#endif /* CONFIG_DEBUG_FS */
365
Darren Hart077175f2012-03-09 09:51:49 -0800366/* Return UART clock, checking for board specific clocks. */
367static int pch_uart_get_uartclk(void)
368{
369 const char *cmp;
370
Darren Hart2a44feb2012-03-09 09:51:50 -0800371 if (user_uartclk)
372 return user_uartclk;
373
Darren Hart077175f2012-03-09 09:51:49 -0800374 cmp = dmi_get_system_info(DMI_BOARD_NAME);
375 if (cmp && strstr(cmp, "CM-iTC"))
376 return CMITC_UARTCLK;
377
378 cmp = dmi_get_system_info(DMI_BIOS_VERSION);
379 if (cmp && strnstr(cmp, "FRI2", 4))
380 return FRI2_64_UARTCLK;
381
382 cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
383 if (cmp && strstr(cmp, "Fish River Island II"))
384 return FRI2_48_UARTCLK;
385
386 return DEFAULT_UARTCLK;
387}
388
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900389static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
390 unsigned int flag)
391{
392 u8 ier = ioread8(priv->membase + UART_IER);
393 ier |= flag & PCH_UART_IER_MASK;
394 iowrite8(ier, priv->membase + UART_IER);
395}
396
397static void pch_uart_hal_disable_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 int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
406 unsigned int parity, unsigned int bits,
407 unsigned int stb)
408{
409 unsigned int dll, dlm, lcr;
410 int div;
411
Darren Harta8a3ec92012-03-09 09:51:48 -0800412 div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900413 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900414 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900415 return -EINVAL;
416 }
417
418 dll = (unsigned int)div & 0x00FFU;
419 dlm = ((unsigned int)div >> 8) & 0x00FFU;
420
421 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900422 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900423 return -EINVAL;
424 }
425
426 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900427 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900428 return -EINVAL;
429 }
430
431 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900432 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900433 return -EINVAL;
434 }
435
436 lcr = parity;
437 lcr |= bits;
438 lcr |= stb;
439
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900440 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900441 __func__, baud, div, lcr, jiffies);
442 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
443 iowrite8(dll, priv->membase + PCH_UART_DLL);
444 iowrite8(dlm, priv->membase + PCH_UART_DLM);
445 iowrite8(lcr, priv->membase + UART_LCR);
446
447 return 0;
448}
449
450static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
451 unsigned int flag)
452{
453 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900454 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
455 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900456 return -EINVAL;
457 }
458
459 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
460 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
461 priv->membase + UART_FCR);
462 iowrite8(priv->fcr, priv->membase + UART_FCR);
463
464 return 0;
465}
466
467static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
468 unsigned int dmamode,
469 unsigned int fifo_size, unsigned int trigger)
470{
471 u8 fcr;
472
473 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900474 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
475 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900476 return -EINVAL;
477 }
478
479 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900480 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
481 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900482 return -EINVAL;
483 }
484
485 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900486 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
487 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900488 return -EINVAL;
489 }
490
491 switch (priv->fifo_size) {
492 case 256:
493 priv->trigger_level =
494 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
495 break;
496 case 64:
497 priv->trigger_level =
498 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
499 break;
500 case 16:
501 priv->trigger_level =
502 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
503 break;
504 default:
505 priv->trigger_level =
506 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
507 break;
508 }
509 fcr =
510 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
511 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
512 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
513 priv->membase + UART_FCR);
514 iowrite8(fcr, priv->membase + UART_FCR);
515 priv->fcr = fcr;
516
517 return 0;
518}
519
520static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
521{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800522 unsigned int msr = ioread8(priv->membase + UART_MSR);
523 priv->dmsr = msr & PCH_UART_MSR_DELTA;
524 return (u8)msr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900525}
526
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900527static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900528 const unsigned char *buf, int tx_size)
529{
530 int i;
531 unsigned int thr;
532
533 for (i = 0; i < tx_size;) {
534 thr = buf[i++];
535 iowrite8(thr, priv->membase + PCH_UART_THR);
536 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900537}
538
539static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
540 int rx_size)
541{
542 int i;
543 u8 rbr, lsr;
544
545 lsr = ioread8(priv->membase + UART_LSR);
546 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
547 i < rx_size && lsr & UART_LSR_DR;
548 lsr = ioread8(priv->membase + UART_LSR)) {
549 rbr = ioread8(priv->membase + PCH_UART_RBR);
550 buf[i++] = rbr;
551 }
552 return i;
553}
554
555static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
556{
557 unsigned int iir;
558 int ret;
559
560 iir = ioread8(priv->membase + UART_IIR);
561 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
562 return ret;
563}
564
565static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
566{
567 return ioread8(priv->membase + UART_LSR);
568}
569
570static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
571{
572 unsigned int lcr;
573
574 lcr = ioread8(priv->membase + UART_LCR);
575 if (on)
576 lcr |= PCH_UART_LCR_SB;
577 else
578 lcr &= ~PCH_UART_LCR_SB;
579
580 iowrite8(lcr, priv->membase + UART_LCR);
581}
582
583static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
584 int size)
585{
586 struct uart_port *port;
587 struct tty_struct *tty;
588
589 port = &priv->port;
590 tty = tty_port_tty_get(&port->state->port);
591 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900592 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900593 return -EBUSY;
594 }
595
596 tty_insert_flip_string(tty, buf, size);
597 tty_flip_buffer_push(tty);
598 tty_kref_put(tty);
599
600 return 0;
601}
602
603static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
604{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800605 int ret = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900606 struct uart_port *port = &priv->port;
607
608 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900609 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
610 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900611 buf[0] = port->x_char;
612 port->x_char = 0;
613 ret = 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900614 }
615
616 return ret;
617}
618
619static int dma_push_rx(struct eg20t_port *priv, int size)
620{
621 struct tty_struct *tty;
622 int room;
623 struct uart_port *port = &priv->port;
624
625 port = &priv->port;
626 tty = tty_port_tty_get(&port->state->port);
627 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900628 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900629 return 0;
630 }
631
632 room = tty_buffer_request_room(tty, size);
633
634 if (room < size)
635 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
636 size - room);
637 if (!room)
638 return room;
639
640 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
641
642 port->icount.rx += room;
643 tty_kref_put(tty);
644
645 return room;
646}
647
648static void pch_free_dma(struct uart_port *port)
649{
650 struct eg20t_port *priv;
651 priv = container_of(port, struct eg20t_port, port);
652
653 if (priv->chan_tx) {
654 dma_release_channel(priv->chan_tx);
655 priv->chan_tx = NULL;
656 }
657 if (priv->chan_rx) {
658 dma_release_channel(priv->chan_rx);
659 priv->chan_rx = NULL;
660 }
661 if (sg_dma_address(&priv->sg_rx))
662 dma_free_coherent(port->dev, port->fifosize,
663 sg_virt(&priv->sg_rx),
664 sg_dma_address(&priv->sg_rx));
665
666 return;
667}
668
669static bool filter(struct dma_chan *chan, void *slave)
670{
671 struct pch_dma_slave *param = slave;
672
673 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
674 chan->device->dev)) {
675 chan->private = param;
676 return true;
677 } else {
678 return false;
679 }
680}
681
682static void pch_request_dma(struct uart_port *port)
683{
684 dma_cap_mask_t mask;
685 struct dma_chan *chan;
686 struct pci_dev *dma_dev;
687 struct pch_dma_slave *param;
688 struct eg20t_port *priv =
689 container_of(port, struct eg20t_port, port);
690 dma_cap_zero(mask);
691 dma_cap_set(DMA_SLAVE, mask);
692
Tomoya MORINAGA6c4b47d2011-07-20 20:17:49 +0900693 dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
694 PCI_DEVFN(0xa, 0)); /* Get DMA's dev
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900695 information */
696 /* Set Tx DMA */
697 param = &priv->param_tx;
698 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900699 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
700
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900701 param->tx_reg = port->mapbase + UART_TX;
702 chan = dma_request_channel(mask, filter, param);
703 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900704 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
705 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900706 return;
707 }
708 priv->chan_tx = chan;
709
710 /* Set Rx DMA */
711 param = &priv->param_rx;
712 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900713 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
714
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900715 param->rx_reg = port->mapbase + UART_RX;
716 chan = dma_request_channel(mask, filter, param);
717 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900718 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
719 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900720 dma_release_channel(priv->chan_tx);
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +0900721 priv->chan_tx = NULL;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900722 return;
723 }
724
725 /* Get Consistent memory for DMA */
726 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
727 &priv->rx_buf_dma, GFP_KERNEL);
728 priv->chan_rx = chan;
729}
730
731static void pch_dma_rx_complete(void *arg)
732{
733 struct eg20t_port *priv = arg;
734 struct uart_port *port = &priv->port;
735 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900736 int count;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900737
738 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900739 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900740 return;
741 }
742
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900743 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
744 count = dma_push_rx(priv, priv->trigger_level);
745 if (count)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900746 tty_flip_buffer_push(tty);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900747 tty_kref_put(tty);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900748 async_tx_ack(priv->desc_rx);
749 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900750}
751
752static void pch_dma_tx_complete(void *arg)
753{
754 struct eg20t_port *priv = arg;
755 struct uart_port *port = &priv->port;
756 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900757 struct scatterlist *sg = priv->sg_tx_p;
758 int i;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900759
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900760 for (i = 0; i < priv->nent; i++, sg++) {
761 xmit->tail += sg_dma_len(sg);
762 port->icount.tx += sg_dma_len(sg);
763 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900764 xmit->tail &= UART_XMIT_SIZE - 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900765 async_tx_ack(priv->desc_tx);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900766 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900767 priv->tx_dma_use = 0;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900768 priv->nent = 0;
769 kfree(priv->sg_tx_p);
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900770 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900771}
772
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900773static int pop_tx(struct eg20t_port *priv, int size)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900774{
775 int count = 0;
776 struct uart_port *port = &priv->port;
777 struct circ_buf *xmit = &port->state->xmit;
778
779 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
780 goto pop_tx_end;
781
782 do {
783 int cnt_to_end =
784 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
785 int sz = min(size - count, cnt_to_end);
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900786 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900787 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
788 count += sz;
789 } while (!uart_circ_empty(xmit) && count < size);
790
791pop_tx_end:
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900792 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900793 count, size - count, jiffies);
794
795 return count;
796}
797
798static int handle_rx_to(struct eg20t_port *priv)
799{
800 struct pch_uart_buffer *buf;
801 int rx_size;
802 int ret;
803 if (!priv->start_rx) {
804 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
805 return 0;
806 }
807 buf = &priv->rxbuf;
808 do {
809 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
810 ret = push_rx(priv, buf->buf, rx_size);
811 if (ret)
812 return 0;
813 } while (rx_size == buf->size);
814
815 return PCH_UART_HANDLED_RX_INT;
816}
817
818static int handle_rx(struct eg20t_port *priv)
819{
820 return handle_rx_to(priv);
821}
822
823static int dma_handle_rx(struct eg20t_port *priv)
824{
825 struct uart_port *port = &priv->port;
826 struct dma_async_tx_descriptor *desc;
827 struct scatterlist *sg;
828
829 priv = container_of(port, struct eg20t_port, port);
830 sg = &priv->sg_rx;
831
832 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
833
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900834 sg_dma_len(sg) = priv->trigger_level;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900835
836 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900837 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
838 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900839
840 sg_dma_address(sg) = priv->rx_buf_dma;
841
Alexandre Bounine16052822012-03-08 16:11:18 -0500842 desc = dmaengine_prep_slave_sg(priv->chan_rx,
Vinod Koula485df42011-10-14 10:47:38 +0530843 sg, 1, DMA_DEV_TO_MEM,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900844 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
845
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900846 if (!desc)
847 return 0;
848
849 priv->desc_rx = desc;
850 desc->callback = pch_dma_rx_complete;
851 desc->callback_param = priv;
852 desc->tx_submit(desc);
853 dma_async_issue_pending(priv->chan_rx);
854
855 return PCH_UART_HANDLED_RX_INT;
856}
857
858static unsigned int handle_tx(struct eg20t_port *priv)
859{
860 struct uart_port *port = &priv->port;
861 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900862 int fifo_size;
863 int tx_size;
864 int size;
865 int tx_empty;
866
867 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900868 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
869 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900870 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
871 priv->tx_empty = 1;
872 return 0;
873 }
874
875 fifo_size = max(priv->fifo_size, 1);
876 tx_empty = 1;
877 if (pop_tx_x(priv, xmit->buf)) {
878 pch_uart_hal_write(priv, xmit->buf, 1);
879 port->icount.tx++;
880 tx_empty = 0;
881 fifo_size--;
882 }
883 size = min(xmit->head - xmit->tail, fifo_size);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900884 if (size < 0)
885 size = fifo_size;
886
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900887 tx_size = pop_tx(priv, size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900888 if (tx_size > 0) {
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900889 port->icount.tx += tx_size;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900890 tx_empty = 0;
891 }
892
893 priv->tx_empty = tx_empty;
894
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900895 if (tx_empty) {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900896 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900897 uart_write_wakeup(port);
898 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900899
900 return PCH_UART_HANDLED_TX_INT;
901}
902
903static unsigned int dma_handle_tx(struct eg20t_port *priv)
904{
905 struct uart_port *port = &priv->port;
906 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900907 struct scatterlist *sg;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900908 int nent;
909 int fifo_size;
910 int tx_empty;
911 struct dma_async_tx_descriptor *desc;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900912 int num;
913 int i;
914 int bytes;
915 int size;
916 int rem;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900917
918 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900919 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
920 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900921 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
922 priv->tx_empty = 1;
923 return 0;
924 }
925
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900926 if (priv->tx_dma_use) {
927 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
928 __func__, jiffies);
929 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
930 priv->tx_empty = 1;
931 return 0;
932 }
933
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900934 fifo_size = max(priv->fifo_size, 1);
935 tx_empty = 1;
936 if (pop_tx_x(priv, xmit->buf)) {
937 pch_uart_hal_write(priv, xmit->buf, 1);
938 port->icount.tx++;
939 tx_empty = 0;
940 fifo_size--;
941 }
942
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900943 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
944 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
945 xmit->tail, UART_XMIT_SIZE));
946 if (!bytes) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900947 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900948 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
949 uart_write_wakeup(port);
950 return 0;
951 }
952
953 if (bytes > fifo_size) {
954 num = bytes / fifo_size + 1;
955 size = fifo_size;
956 rem = bytes % fifo_size;
957 } else {
958 num = 1;
959 size = bytes;
960 rem = bytes;
961 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900962
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900963 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
964 __func__, num, size, rem);
965
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900966 priv->tx_dma_use = 1;
967
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900968 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900969
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900970 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
971 sg = priv->sg_tx_p;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900972
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900973 for (i = 0; i < num; i++, sg++) {
974 if (i == (num - 1))
975 sg_set_page(sg, virt_to_page(xmit->buf),
976 rem, fifo_size * i);
977 else
978 sg_set_page(sg, virt_to_page(xmit->buf),
979 size, fifo_size * i);
980 }
981
982 sg = priv->sg_tx_p;
983 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900984 if (!nent) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900985 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900986 return 0;
987 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900988 priv->nent = nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900989
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900990 for (i = 0; i < nent; i++, sg++) {
991 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
992 fifo_size * i;
993 sg_dma_address(sg) = (sg_dma_address(sg) &
994 ~(UART_XMIT_SIZE - 1)) + sg->offset;
995 if (i == (nent - 1))
996 sg_dma_len(sg) = rem;
997 else
998 sg_dma_len(sg) = size;
999 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001000
Alexandre Bounine16052822012-03-08 16:11:18 -05001001 desc = dmaengine_prep_slave_sg(priv->chan_tx,
Vinod Koula485df42011-10-14 10:47:38 +05301002 priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001003 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001004 if (!desc) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001005 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
1006 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001007 return 0;
1008 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001009 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001010 priv->desc_tx = desc;
1011 desc->callback = pch_dma_tx_complete;
1012 desc->callback_param = priv;
1013
1014 desc->tx_submit(desc);
1015
1016 dma_async_issue_pending(priv->chan_tx);
1017
1018 return PCH_UART_HANDLED_TX_INT;
1019}
1020
1021static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
1022{
1023 u8 fcr = ioread8(priv->membase + UART_FCR);
1024
1025 /* Reset FIFO */
1026 fcr |= UART_FCR_CLEAR_RCVR;
1027 iowrite8(fcr, priv->membase + UART_FCR);
1028
1029 if (lsr & PCH_UART_LSR_ERR)
1030 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
1031
1032 if (lsr & UART_LSR_FE)
1033 dev_err(&priv->pdev->dev, "Framing Error\n");
1034
1035 if (lsr & UART_LSR_PE)
1036 dev_err(&priv->pdev->dev, "Parity Error\n");
1037
1038 if (lsr & UART_LSR_OE)
1039 dev_err(&priv->pdev->dev, "Overrun Error\n");
1040}
1041
1042static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1043{
1044 struct eg20t_port *priv = dev_id;
1045 unsigned int handled;
1046 u8 lsr;
1047 int ret = 0;
1048 unsigned int iid;
1049 unsigned long flags;
1050
1051 spin_lock_irqsave(&priv->port.lock, flags);
1052 handled = 0;
1053 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
1054 switch (iid) {
1055 case PCH_UART_IID_RLS: /* Receiver Line Status */
1056 lsr = pch_uart_hal_get_line_status(priv);
1057 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1058 UART_LSR_PE | UART_LSR_OE)) {
1059 pch_uart_err_ir(priv, lsr);
1060 ret = PCH_UART_HANDLED_RX_ERR_INT;
1061 }
1062 break;
1063 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001064 if (priv->use_dma) {
1065 pch_uart_hal_disable_interrupt(priv,
1066 PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001067 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001068 if (!ret)
1069 pch_uart_hal_enable_interrupt(priv,
1070 PCH_UART_HAL_RX_INT);
1071 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001072 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001073 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001074 break;
1075 case PCH_UART_IID_RDR_TO: /* Received Data Ready
1076 (FIFO Timeout) */
1077 ret = handle_rx_to(priv);
1078 break;
1079 case PCH_UART_IID_THRE: /* Transmitter Holding Register
1080 Empty */
1081 if (priv->use_dma)
1082 ret = dma_handle_tx(priv);
1083 else
1084 ret = handle_tx(priv);
1085 break;
1086 case PCH_UART_IID_MS: /* Modem Status */
1087 ret = PCH_UART_HANDLED_MS_INT;
1088 break;
1089 default: /* Never junp to this label */
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001090 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
1091 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001092 ret = -1;
1093 break;
1094 }
1095 handled |= (unsigned int)ret;
1096 }
1097 if (handled == 0 && iid <= 1) {
1098 if (priv->int_dis_flag)
1099 priv->int_dis_flag = 0;
1100 }
1101
1102 spin_unlock_irqrestore(&priv->port.lock, flags);
1103 return IRQ_RETVAL(handled);
1104}
1105
1106/* This function tests whether the transmitter fifo and shifter for the port
1107 described by 'port' is empty. */
1108static unsigned int pch_uart_tx_empty(struct uart_port *port)
1109{
1110 struct eg20t_port *priv;
Feng Tang30c6c6b2012-02-06 17:24:44 +08001111
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001112 priv = container_of(port, struct eg20t_port, port);
1113 if (priv->tx_empty)
Feng Tang30c6c6b2012-02-06 17:24:44 +08001114 return TIOCSER_TEMT;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001115 else
Feng Tang30c6c6b2012-02-06 17:24:44 +08001116 return 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001117}
1118
1119/* Returns the current state of modem control inputs. */
1120static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1121{
1122 struct eg20t_port *priv;
1123 u8 modem;
1124 unsigned int ret = 0;
1125
1126 priv = container_of(port, struct eg20t_port, port);
1127 modem = pch_uart_hal_get_modem(priv);
1128
1129 if (modem & UART_MSR_DCD)
1130 ret |= TIOCM_CAR;
1131
1132 if (modem & UART_MSR_RI)
1133 ret |= TIOCM_RNG;
1134
1135 if (modem & UART_MSR_DSR)
1136 ret |= TIOCM_DSR;
1137
1138 if (modem & UART_MSR_CTS)
1139 ret |= TIOCM_CTS;
1140
1141 return ret;
1142}
1143
1144static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1145{
1146 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001147 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1148
1149 if (mctrl & TIOCM_DTR)
1150 mcr |= UART_MCR_DTR;
1151 if (mctrl & TIOCM_RTS)
1152 mcr |= UART_MCR_RTS;
1153 if (mctrl & TIOCM_LOOP)
1154 mcr |= UART_MCR_LOOP;
1155
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001156 if (priv->mcr & UART_MCR_AFE)
1157 mcr |= UART_MCR_AFE;
1158
1159 if (mctrl)
1160 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001161}
1162
1163static void pch_uart_stop_tx(struct uart_port *port)
1164{
1165 struct eg20t_port *priv;
1166 priv = container_of(port, struct eg20t_port, port);
1167 priv->start_tx = 0;
1168 priv->tx_dma_use = 0;
1169}
1170
1171static void pch_uart_start_tx(struct uart_port *port)
1172{
1173 struct eg20t_port *priv;
1174
1175 priv = container_of(port, struct eg20t_port, port);
1176
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001177 if (priv->use_dma) {
1178 if (priv->tx_dma_use) {
1179 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1180 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001181 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001182 }
1183 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001184
1185 priv->start_tx = 1;
1186 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1187}
1188
1189static void pch_uart_stop_rx(struct uart_port *port)
1190{
1191 struct eg20t_port *priv;
1192 priv = container_of(port, struct eg20t_port, port);
1193 priv->start_rx = 0;
1194 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1195 priv->int_dis_flag = 1;
1196}
1197
1198/* Enable the modem status interrupts. */
1199static void pch_uart_enable_ms(struct uart_port *port)
1200{
1201 struct eg20t_port *priv;
1202 priv = container_of(port, struct eg20t_port, port);
1203 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1204}
1205
1206/* Control the transmission of a break signal. */
1207static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1208{
1209 struct eg20t_port *priv;
1210 unsigned long flags;
1211
1212 priv = container_of(port, struct eg20t_port, port);
1213 spin_lock_irqsave(&port->lock, flags);
1214 pch_uart_hal_set_break(priv, ctl);
1215 spin_unlock_irqrestore(&port->lock, flags);
1216}
1217
1218/* Grab any interrupt resources and initialise any low level driver state. */
1219static int pch_uart_startup(struct uart_port *port)
1220{
1221 struct eg20t_port *priv;
1222 int ret;
1223 int fifo_size;
1224 int trigger_level;
1225
1226 priv = container_of(port, struct eg20t_port, port);
1227 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001228
1229 if (port->uartclk)
Darren Harta8a3ec92012-03-09 09:51:48 -08001230 priv->uartclk = port->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001231 else
Darren Harta8a3ec92012-03-09 09:51:48 -08001232 port->uartclk = priv->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001233
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001234 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1235 ret = pch_uart_hal_set_line(priv, default_baud,
1236 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1237 PCH_UART_HAL_STB1);
1238 if (ret)
1239 return ret;
1240
1241 switch (priv->fifo_size) {
1242 case 256:
1243 fifo_size = PCH_UART_HAL_FIFO256;
1244 break;
1245 case 64:
1246 fifo_size = PCH_UART_HAL_FIFO64;
1247 break;
1248 case 16:
1249 fifo_size = PCH_UART_HAL_FIFO16;
1250 case 1:
1251 default:
1252 fifo_size = PCH_UART_HAL_FIFO_DIS;
1253 break;
1254 }
1255
1256 switch (priv->trigger) {
1257 case PCH_UART_HAL_TRIGGER1:
1258 trigger_level = 1;
1259 break;
1260 case PCH_UART_HAL_TRIGGER_L:
1261 trigger_level = priv->fifo_size / 4;
1262 break;
1263 case PCH_UART_HAL_TRIGGER_M:
1264 trigger_level = priv->fifo_size / 2;
1265 break;
1266 case PCH_UART_HAL_TRIGGER_H:
1267 default:
1268 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1269 break;
1270 }
1271
1272 priv->trigger_level = trigger_level;
1273 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1274 fifo_size, priv->trigger);
1275 if (ret < 0)
1276 return ret;
1277
1278 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1279 KBUILD_MODNAME, priv);
1280 if (ret < 0)
1281 return ret;
1282
1283 if (priv->use_dma)
1284 pch_request_dma(port);
1285
1286 priv->start_rx = 1;
1287 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1288 uart_update_timeout(port, CS8, default_baud);
1289
1290 return 0;
1291}
1292
1293static void pch_uart_shutdown(struct uart_port *port)
1294{
1295 struct eg20t_port *priv;
1296 int ret;
1297
1298 priv = container_of(port, struct eg20t_port, port);
1299 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1300 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1301 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1302 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1303 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001304 dev_err(priv->port.dev,
1305 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001306
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +09001307 pch_free_dma(port);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001308
1309 free_irq(priv->port.irq, priv);
1310}
1311
1312/* Change the port parameters, including word length, parity, stop
1313 *bits. Update read_status_mask and ignore_status_mask to indicate
1314 *the types of events we are interested in receiving. */
1315static void pch_uart_set_termios(struct uart_port *port,
1316 struct ktermios *termios, struct ktermios *old)
1317{
1318 int baud;
1319 int rtn;
1320 unsigned int parity, bits, stb;
1321 struct eg20t_port *priv;
1322 unsigned long flags;
1323
1324 priv = container_of(port, struct eg20t_port, port);
1325 switch (termios->c_cflag & CSIZE) {
1326 case CS5:
1327 bits = PCH_UART_HAL_5BIT;
1328 break;
1329 case CS6:
1330 bits = PCH_UART_HAL_6BIT;
1331 break;
1332 case CS7:
1333 bits = PCH_UART_HAL_7BIT;
1334 break;
1335 default: /* CS8 */
1336 bits = PCH_UART_HAL_8BIT;
1337 break;
1338 }
1339 if (termios->c_cflag & CSTOPB)
1340 stb = PCH_UART_HAL_STB2;
1341 else
1342 stb = PCH_UART_HAL_STB1;
1343
1344 if (termios->c_cflag & PARENB) {
1345 if (!(termios->c_cflag & PARODD))
1346 parity = PCH_UART_HAL_PARITY_ODD;
1347 else
1348 parity = PCH_UART_HAL_PARITY_EVEN;
1349
Feng Tang30c6c6b2012-02-06 17:24:44 +08001350 } else
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001351 parity = PCH_UART_HAL_PARITY_NONE;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001352
1353 /* Only UART0 has auto hardware flow function */
1354 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1355 priv->mcr |= UART_MCR_AFE;
1356 else
1357 priv->mcr &= ~UART_MCR_AFE;
1358
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001359 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1360
1361 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1362
1363 spin_lock_irqsave(&port->lock, flags);
1364
1365 uart_update_timeout(port, termios->c_cflag, baud);
1366 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1367 if (rtn)
1368 goto out;
1369
Tomoya MORINAGAa1d7cfe2011-10-27 15:45:18 +09001370 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001371 /* Don't rewrite B0 */
1372 if (tty_termios_baud_rate(termios))
1373 tty_termios_encode_baud_rate(termios, baud, baud);
1374
1375out:
1376 spin_unlock_irqrestore(&port->lock, flags);
1377}
1378
1379static const char *pch_uart_type(struct uart_port *port)
1380{
1381 return KBUILD_MODNAME;
1382}
1383
1384static void pch_uart_release_port(struct uart_port *port)
1385{
1386 struct eg20t_port *priv;
1387
1388 priv = container_of(port, struct eg20t_port, port);
1389 pci_iounmap(priv->pdev, priv->membase);
1390 pci_release_regions(priv->pdev);
1391}
1392
1393static int pch_uart_request_port(struct uart_port *port)
1394{
1395 struct eg20t_port *priv;
1396 int ret;
1397 void __iomem *membase;
1398
1399 priv = container_of(port, struct eg20t_port, port);
1400 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1401 if (ret < 0)
1402 return -EBUSY;
1403
1404 membase = pci_iomap(priv->pdev, 1, 0);
1405 if (!membase) {
1406 pci_release_regions(priv->pdev);
1407 return -EBUSY;
1408 }
1409 priv->membase = port->membase = membase;
1410
1411 return 0;
1412}
1413
1414static void pch_uart_config_port(struct uart_port *port, int type)
1415{
1416 struct eg20t_port *priv;
1417
1418 priv = container_of(port, struct eg20t_port, port);
1419 if (type & UART_CONFIG_TYPE) {
1420 port->type = priv->port_type;
1421 pch_uart_request_port(port);
1422 }
1423}
1424
1425static int pch_uart_verify_port(struct uart_port *port,
1426 struct serial_struct *serinfo)
1427{
1428 struct eg20t_port *priv;
1429
1430 priv = container_of(port, struct eg20t_port, port);
1431 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001432 dev_info(priv->port.dev,
1433 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001434 priv->use_dma = 0;
1435 serinfo->flags &= ~UPF_LOW_LATENCY;
1436 } else {
1437#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001438 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1439 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001440 return -EOPNOTSUPP;
1441#endif
1442 priv->use_dma = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001443 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001444 }
1445
1446 return 0;
1447}
1448
1449static struct uart_ops pch_uart_ops = {
1450 .tx_empty = pch_uart_tx_empty,
1451 .set_mctrl = pch_uart_set_mctrl,
1452 .get_mctrl = pch_uart_get_mctrl,
1453 .stop_tx = pch_uart_stop_tx,
1454 .start_tx = pch_uart_start_tx,
1455 .stop_rx = pch_uart_stop_rx,
1456 .enable_ms = pch_uart_enable_ms,
1457 .break_ctl = pch_uart_break_ctl,
1458 .startup = pch_uart_startup,
1459 .shutdown = pch_uart_shutdown,
1460 .set_termios = pch_uart_set_termios,
1461/* .pm = pch_uart_pm, Not supported yet */
1462/* .set_wake = pch_uart_set_wake, Not supported yet */
1463 .type = pch_uart_type,
1464 .release_port = pch_uart_release_port,
1465 .request_port = pch_uart_request_port,
1466 .config_port = pch_uart_config_port,
1467 .verify_port = pch_uart_verify_port
1468};
1469
Alexander Steine30f8672011-11-15 15:04:07 -08001470#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1471
1472/*
1473 * Wait for transmitter & holding register to empty
1474 */
1475static void wait_for_xmitr(struct eg20t_port *up, int bits)
1476{
1477 unsigned int status, tmout = 10000;
1478
1479 /* Wait up to 10ms for the character(s) to be sent. */
1480 for (;;) {
1481 status = ioread8(up->membase + UART_LSR);
1482
1483 if ((status & bits) == bits)
1484 break;
1485 if (--tmout == 0)
1486 break;
1487 udelay(1);
1488 }
1489
1490 /* Wait up to 1s for flow control if necessary */
1491 if (up->port.flags & UPF_CONS_FLOW) {
1492 unsigned int tmout;
1493 for (tmout = 1000000; tmout; tmout--) {
1494 unsigned int msr = ioread8(up->membase + UART_MSR);
1495 if (msr & UART_MSR_CTS)
1496 break;
1497 udelay(1);
1498 touch_nmi_watchdog();
1499 }
1500 }
1501}
1502
1503static void pch_console_putchar(struct uart_port *port, int ch)
1504{
1505 struct eg20t_port *priv =
1506 container_of(port, struct eg20t_port, port);
1507
1508 wait_for_xmitr(priv, UART_LSR_THRE);
1509 iowrite8(ch, priv->membase + PCH_UART_THR);
1510}
1511
1512/*
1513 * Print a string to the serial port trying not to disturb
1514 * any possible real use of the port...
1515 *
1516 * The console_lock must be held when we get here.
1517 */
1518static void
1519pch_console_write(struct console *co, const char *s, unsigned int count)
1520{
1521 struct eg20t_port *priv;
Alexander Steine30f8672011-11-15 15:04:07 -08001522 unsigned long flags;
1523 u8 ier;
1524 int locked = 1;
1525
1526 priv = pch_uart_ports[co->index];
1527
1528 touch_nmi_watchdog();
1529
1530 local_irq_save(flags);
1531 if (priv->port.sysrq) {
1532 /* serial8250_handle_port() already took the lock */
1533 locked = 0;
1534 } else if (oops_in_progress) {
1535 locked = spin_trylock(&priv->port.lock);
1536 } else
1537 spin_lock(&priv->port.lock);
1538
1539 /*
1540 * First save the IER then disable the interrupts
1541 */
1542 ier = ioread8(priv->membase + UART_IER);
1543
1544 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1545
1546 uart_console_write(&priv->port, s, count, pch_console_putchar);
1547
1548 /*
1549 * Finally, wait for transmitter to become empty
1550 * and restore the IER
1551 */
1552 wait_for_xmitr(priv, BOTH_EMPTY);
1553 iowrite8(ier, priv->membase + UART_IER);
1554
1555 if (locked)
1556 spin_unlock(&priv->port.lock);
1557 local_irq_restore(flags);
1558}
1559
1560static int __init pch_console_setup(struct console *co, char *options)
1561{
1562 struct uart_port *port;
Darren Hart7ce92512012-03-09 09:51:51 -08001563 int baud = default_baud;
Alexander Steine30f8672011-11-15 15:04:07 -08001564 int bits = 8;
1565 int parity = 'n';
1566 int flow = 'n';
1567
1568 /*
1569 * Check whether an invalid uart number has been specified, and
1570 * if so, search for the first available port that does have
1571 * console support.
1572 */
1573 if (co->index >= PCH_UART_NR)
1574 co->index = 0;
1575 port = &pch_uart_ports[co->index]->port;
1576
1577 if (!port || (!port->iobase && !port->membase))
1578 return -ENODEV;
1579
Darren Hart077175f2012-03-09 09:51:49 -08001580 port->uartclk = pch_uart_get_uartclk();
Alexander Steine30f8672011-11-15 15:04:07 -08001581
1582 if (options)
1583 uart_parse_options(options, &baud, &parity, &bits, &flow);
1584
1585 return uart_set_options(port, co, baud, parity, bits, flow);
1586}
1587
1588static struct uart_driver pch_uart_driver;
1589
1590static struct console pch_console = {
1591 .name = PCH_UART_DRIVER_DEVICE,
1592 .write = pch_console_write,
1593 .device = uart_console_device,
1594 .setup = pch_console_setup,
1595 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1596 .index = -1,
1597 .data = &pch_uart_driver,
1598};
1599
1600#define PCH_CONSOLE (&pch_console)
1601#else
1602#define PCH_CONSOLE NULL
1603#endif
1604
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001605static struct uart_driver pch_uart_driver = {
1606 .owner = THIS_MODULE,
1607 .driver_name = KBUILD_MODNAME,
1608 .dev_name = PCH_UART_DRIVER_DEVICE,
1609 .major = 0,
1610 .minor = 0,
1611 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001612 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001613};
1614
1615static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001616 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001617{
1618 struct eg20t_port *priv;
1619 int ret;
1620 unsigned int iobase;
1621 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001622 unsigned char *rxbuf;
Darren Hart077175f2012-03-09 09:51:49 -08001623 int fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001624 int port_type;
1625 struct pch_uart_driver_data *board;
Feng Tangd0114112012-02-06 17:24:43 +08001626 char name[32]; /* for debugfs file name */
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001627
1628 board = &drv_dat[id->driver_data];
1629 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001630
1631 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1632 if (priv == NULL)
1633 goto init_port_alloc_err;
1634
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001635 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001636 if (!rxbuf)
1637 goto init_port_free_txbuf;
1638
1639 switch (port_type) {
1640 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001641 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001642 break;
1643 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001644 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001645 break;
1646 default:
1647 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1648 goto init_port_hal_free;
1649 }
1650
Alexander Steine4635952011-07-04 08:58:31 +02001651 pci_enable_msi(pdev);
1652
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001653 iobase = pci_resource_start(pdev, 0);
1654 mapbase = pci_resource_start(pdev, 1);
1655 priv->mapbase = mapbase;
1656 priv->iobase = iobase;
1657 priv->pdev = pdev;
1658 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001659 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001660 priv->rxbuf.size = PAGE_SIZE;
1661
1662 priv->fifo_size = fifosize;
Darren Hart077175f2012-03-09 09:51:49 -08001663 priv->uartclk = pch_uart_get_uartclk();
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001664 priv->port_type = PORT_MAX_8250 + port_type + 1;
1665 priv->port.dev = &pdev->dev;
1666 priv->port.iobase = iobase;
1667 priv->port.membase = NULL;
1668 priv->port.mapbase = mapbase;
1669 priv->port.irq = pdev->irq;
1670 priv->port.iotype = UPIO_PORT;
1671 priv->port.ops = &pch_uart_ops;
1672 priv->port.flags = UPF_BOOT_AUTOCONF;
1673 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001674 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001675 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1676
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001677 spin_lock_init(&priv->port.lock);
1678
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001679 pci_set_drvdata(pdev, priv);
Feng Tang6f56d0f2012-02-06 17:24:45 +08001680 priv->trigger_level = 1;
1681 priv->fcr = 0;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001682
Alexander Steine30f8672011-11-15 15:04:07 -08001683#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1684 pch_uart_ports[board->line_no] = priv;
1685#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001686 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1687 if (ret < 0)
1688 goto init_port_hal_free;
1689
Feng Tangd0114112012-02-06 17:24:43 +08001690#ifdef CONFIG_DEBUG_FS
1691 snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
1692 priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
1693 NULL, priv, &port_regs_ops);
1694#endif
1695
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001696 return priv;
1697
1698init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001699#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1700 pch_uart_ports[board->line_no] = NULL;
1701#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001702 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001703init_port_free_txbuf:
1704 kfree(priv);
1705init_port_alloc_err:
1706
1707 return NULL;
1708}
1709
1710static void pch_uart_exit_port(struct eg20t_port *priv)
1711{
Feng Tangd0114112012-02-06 17:24:43 +08001712
1713#ifdef CONFIG_DEBUG_FS
1714 if (priv->debugfs)
1715 debugfs_remove(priv->debugfs);
1716#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001717 uart_remove_one_port(&pch_uart_driver, &priv->port);
1718 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001719 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001720}
1721
1722static void pch_uart_pci_remove(struct pci_dev *pdev)
1723{
Feng Tang6f56d0f2012-02-06 17:24:45 +08001724 struct eg20t_port *priv = pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001725
1726 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001727
1728#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1729 pch_uart_ports[priv->port.line] = NULL;
1730#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001731 pch_uart_exit_port(priv);
1732 pci_disable_device(pdev);
1733 kfree(priv);
1734 return;
1735}
1736#ifdef CONFIG_PM
1737static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1738{
1739 struct eg20t_port *priv = pci_get_drvdata(pdev);
1740
1741 uart_suspend_port(&pch_uart_driver, &priv->port);
1742
1743 pci_save_state(pdev);
1744 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1745 return 0;
1746}
1747
1748static int pch_uart_pci_resume(struct pci_dev *pdev)
1749{
1750 struct eg20t_port *priv = pci_get_drvdata(pdev);
1751 int ret;
1752
1753 pci_set_power_state(pdev, PCI_D0);
1754 pci_restore_state(pdev);
1755
1756 ret = pci_enable_device(pdev);
1757 if (ret) {
1758 dev_err(&pdev->dev,
1759 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1760 return ret;
1761 }
1762
1763 uart_resume_port(&pch_uart_driver, &priv->port);
1764
1765 return 0;
1766}
1767#else
1768#define pch_uart_pci_suspend NULL
1769#define pch_uart_pci_resume NULL
1770#endif
1771
1772static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1773 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001774 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001775 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001776 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001777 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001778 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001779 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001780 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001781 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001782 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001783 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001784 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001785 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001786 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001787 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1788 .driver_data = pch_ml7223_uart0},
1789 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1790 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +09001791 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1792 .driver_data = pch_ml7831_uart0},
1793 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1794 .driver_data = pch_ml7831_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001795 {0,},
1796};
1797
1798static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1799 const struct pci_device_id *id)
1800{
1801 int ret;
1802 struct eg20t_port *priv;
1803
1804 ret = pci_enable_device(pdev);
1805 if (ret < 0)
1806 goto probe_error;
1807
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001808 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001809 if (!priv) {
1810 ret = -EBUSY;
1811 goto probe_disable_device;
1812 }
1813 pci_set_drvdata(pdev, priv);
1814
1815 return ret;
1816
1817probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001818 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001819 pci_disable_device(pdev);
1820probe_error:
1821 return ret;
1822}
1823
1824static struct pci_driver pch_uart_pci_driver = {
1825 .name = "pch_uart",
1826 .id_table = pch_uart_pci_id,
1827 .probe = pch_uart_pci_probe,
1828 .remove = __devexit_p(pch_uart_pci_remove),
1829 .suspend = pch_uart_pci_suspend,
1830 .resume = pch_uart_pci_resume,
1831};
1832
1833static int __init pch_uart_module_init(void)
1834{
1835 int ret;
1836
1837 /* register as UART driver */
1838 ret = uart_register_driver(&pch_uart_driver);
1839 if (ret < 0)
1840 return ret;
1841
1842 /* register as PCI driver */
1843 ret = pci_register_driver(&pch_uart_pci_driver);
1844 if (ret < 0)
1845 uart_unregister_driver(&pch_uart_driver);
1846
1847 return ret;
1848}
1849module_init(pch_uart_module_init);
1850
1851static void __exit pch_uart_module_exit(void)
1852{
1853 pci_unregister_driver(&pch_uart_pci_driver);
1854 uart_unregister_driver(&pch_uart_driver);
1855}
1856module_exit(pch_uart_module_exit);
1857
1858MODULE_LICENSE("GPL v2");
1859MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1860module_param(default_baud, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001861MODULE_PARM_DESC(default_baud,
1862 "Default BAUD for initial driver state and console (default 9600)");
Darren Hart2a44feb2012-03-09 09:51:50 -08001863module_param(user_uartclk, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001864MODULE_PARM_DESC(user_uartclk,
1865 "Override UART default or board specific UART clock");