blob: 5178213835c68a9eb80d005604864e8b658d4228 [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 Harta8a3ec92012-03-09 09:51:48 -0800209#define DEFAULT_UARTCLK 1843200 /* 1.8432MHz */
Alexander Steine30f8672011-11-15 15:04:07 -0800210
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900211struct pch_uart_buffer {
212 unsigned char *buf;
213 int size;
214};
215
216struct eg20t_port {
217 struct uart_port port;
218 int port_type;
219 void __iomem *membase;
220 resource_size_t mapbase;
221 unsigned int iobase;
222 struct pci_dev *pdev;
223 int fifo_size;
Darren Harta8a3ec92012-03-09 09:51:48 -0800224 int uartclk;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900225 int start_tx;
226 int start_rx;
227 int tx_empty;
228 int int_dis_flag;
229 int trigger;
230 int trigger_level;
231 struct pch_uart_buffer rxbuf;
232 unsigned int dmsr;
233 unsigned int fcr;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +0900234 unsigned int mcr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900235 unsigned int use_dma;
236 unsigned int use_dma_flag;
237 struct dma_async_tx_descriptor *desc_tx;
238 struct dma_async_tx_descriptor *desc_rx;
239 struct pch_dma_slave param_tx;
240 struct pch_dma_slave param_rx;
241 struct dma_chan *chan_tx;
242 struct dma_chan *chan_rx;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900243 struct scatterlist *sg_tx_p;
244 int nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900245 struct scatterlist sg_rx;
246 int tx_dma_use;
247 void *rx_buf_virt;
248 dma_addr_t rx_buf_dma;
Feng Tangd0114112012-02-06 17:24:43 +0800249
250 struct dentry *debugfs;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900251};
252
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900253/**
254 * struct pch_uart_driver_data - private data structure for UART-DMA
255 * @port_type: The number of DMA channel
256 * @line_no: UART port line number (0, 1, 2...)
257 */
258struct pch_uart_driver_data {
259 int port_type;
260 int line_no;
261};
262
263enum pch_uart_num_t {
264 pch_et20t_uart0 = 0,
265 pch_et20t_uart1,
266 pch_et20t_uart2,
267 pch_et20t_uart3,
268 pch_ml7213_uart0,
269 pch_ml7213_uart1,
270 pch_ml7213_uart2,
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900271 pch_ml7223_uart0,
272 pch_ml7223_uart1,
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900273 pch_ml7831_uart0,
274 pch_ml7831_uart1,
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900275};
276
277static struct pch_uart_driver_data drv_dat[] = {
278 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
279 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
280 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
281 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
282 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
283 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
284 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900285 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
286 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900287 [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
288 [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900289};
290
Alexander Steine30f8672011-11-15 15:04:07 -0800291#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
292static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
293#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900294static unsigned int default_baud = 9600;
295static const int trigger_level_256[4] = { 1, 64, 128, 224 };
296static const int trigger_level_64[4] = { 1, 16, 32, 56 };
297static const int trigger_level_16[4] = { 1, 4, 8, 14 };
298static const int trigger_level_1[4] = { 1, 1, 1, 1 };
299
Feng Tangd0114112012-02-06 17:24:43 +0800300#ifdef CONFIG_DEBUG_FS
301
302#define PCH_REGS_BUFSIZE 1024
303static int pch_show_regs_open(struct inode *inode, struct file *file)
304{
305 file->private_data = inode->i_private;
306 return 0;
307}
308
309static ssize_t port_show_regs(struct file *file, char __user *user_buf,
310 size_t count, loff_t *ppos)
311{
312 struct eg20t_port *priv = file->private_data;
313 char *buf;
314 u32 len = 0;
315 ssize_t ret;
316 unsigned char lcr;
317
318 buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
319 if (!buf)
320 return 0;
321
322 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
323 "PCH EG20T port[%d] regs:\n", priv->port.line);
324
325 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
326 "=================================\n");
327 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
328 "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
329 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
330 "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
331 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
332 "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
333 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
334 "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
335 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
336 "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
337 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
338 "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
339 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
340 "BRCSR: \t0x%02x\n",
341 ioread8(priv->membase + PCH_UART_BRCSR));
342
343 lcr = ioread8(priv->membase + UART_LCR);
344 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
345 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
346 "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
347 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
348 "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
349 iowrite8(lcr, priv->membase + UART_LCR);
350
351 if (len > PCH_REGS_BUFSIZE)
352 len = PCH_REGS_BUFSIZE;
353
354 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
355 kfree(buf);
356 return ret;
357}
358
359static const struct file_operations port_regs_ops = {
360 .owner = THIS_MODULE,
361 .open = pch_show_regs_open,
362 .read = port_show_regs,
363 .llseek = default_llseek,
364};
365#endif /* CONFIG_DEBUG_FS */
366
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900367static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
368 unsigned int flag)
369{
370 u8 ier = ioread8(priv->membase + UART_IER);
371 ier |= flag & PCH_UART_IER_MASK;
372 iowrite8(ier, priv->membase + UART_IER);
373}
374
375static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
376 unsigned int flag)
377{
378 u8 ier = ioread8(priv->membase + UART_IER);
379 ier &= ~(flag & PCH_UART_IER_MASK);
380 iowrite8(ier, priv->membase + UART_IER);
381}
382
383static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
384 unsigned int parity, unsigned int bits,
385 unsigned int stb)
386{
387 unsigned int dll, dlm, lcr;
388 int div;
389
Darren Harta8a3ec92012-03-09 09:51:48 -0800390 div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900391 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900392 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900393 return -EINVAL;
394 }
395
396 dll = (unsigned int)div & 0x00FFU;
397 dlm = ((unsigned int)div >> 8) & 0x00FFU;
398
399 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900400 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900401 return -EINVAL;
402 }
403
404 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900405 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900406 return -EINVAL;
407 }
408
409 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900410 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900411 return -EINVAL;
412 }
413
414 lcr = parity;
415 lcr |= bits;
416 lcr |= stb;
417
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900418 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900419 __func__, baud, div, lcr, jiffies);
420 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
421 iowrite8(dll, priv->membase + PCH_UART_DLL);
422 iowrite8(dlm, priv->membase + PCH_UART_DLM);
423 iowrite8(lcr, priv->membase + UART_LCR);
424
425 return 0;
426}
427
428static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
429 unsigned int flag)
430{
431 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900432 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
433 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900434 return -EINVAL;
435 }
436
437 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
438 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
439 priv->membase + UART_FCR);
440 iowrite8(priv->fcr, priv->membase + UART_FCR);
441
442 return 0;
443}
444
445static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
446 unsigned int dmamode,
447 unsigned int fifo_size, unsigned int trigger)
448{
449 u8 fcr;
450
451 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900452 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
453 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900454 return -EINVAL;
455 }
456
457 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900458 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
459 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900460 return -EINVAL;
461 }
462
463 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900464 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
465 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900466 return -EINVAL;
467 }
468
469 switch (priv->fifo_size) {
470 case 256:
471 priv->trigger_level =
472 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
473 break;
474 case 64:
475 priv->trigger_level =
476 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
477 break;
478 case 16:
479 priv->trigger_level =
480 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
481 break;
482 default:
483 priv->trigger_level =
484 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
485 break;
486 }
487 fcr =
488 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
489 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
490 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
491 priv->membase + UART_FCR);
492 iowrite8(fcr, priv->membase + UART_FCR);
493 priv->fcr = fcr;
494
495 return 0;
496}
497
498static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
499{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800500 unsigned int msr = ioread8(priv->membase + UART_MSR);
501 priv->dmsr = msr & PCH_UART_MSR_DELTA;
502 return (u8)msr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900503}
504
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900505static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900506 const unsigned char *buf, int tx_size)
507{
508 int i;
509 unsigned int thr;
510
511 for (i = 0; i < tx_size;) {
512 thr = buf[i++];
513 iowrite8(thr, priv->membase + PCH_UART_THR);
514 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900515}
516
517static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
518 int rx_size)
519{
520 int i;
521 u8 rbr, lsr;
522
523 lsr = ioread8(priv->membase + UART_LSR);
524 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
525 i < rx_size && lsr & UART_LSR_DR;
526 lsr = ioread8(priv->membase + UART_LSR)) {
527 rbr = ioread8(priv->membase + PCH_UART_RBR);
528 buf[i++] = rbr;
529 }
530 return i;
531}
532
533static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
534{
535 unsigned int iir;
536 int ret;
537
538 iir = ioread8(priv->membase + UART_IIR);
539 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
540 return ret;
541}
542
543static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
544{
545 return ioread8(priv->membase + UART_LSR);
546}
547
548static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
549{
550 unsigned int lcr;
551
552 lcr = ioread8(priv->membase + UART_LCR);
553 if (on)
554 lcr |= PCH_UART_LCR_SB;
555 else
556 lcr &= ~PCH_UART_LCR_SB;
557
558 iowrite8(lcr, priv->membase + UART_LCR);
559}
560
561static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
562 int size)
563{
564 struct uart_port *port;
565 struct tty_struct *tty;
566
567 port = &priv->port;
568 tty = tty_port_tty_get(&port->state->port);
569 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900570 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900571 return -EBUSY;
572 }
573
574 tty_insert_flip_string(tty, buf, size);
575 tty_flip_buffer_push(tty);
576 tty_kref_put(tty);
577
578 return 0;
579}
580
581static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
582{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800583 int ret = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900584 struct uart_port *port = &priv->port;
585
586 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900587 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
588 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900589 buf[0] = port->x_char;
590 port->x_char = 0;
591 ret = 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900592 }
593
594 return ret;
595}
596
597static int dma_push_rx(struct eg20t_port *priv, int size)
598{
599 struct tty_struct *tty;
600 int room;
601 struct uart_port *port = &priv->port;
602
603 port = &priv->port;
604 tty = tty_port_tty_get(&port->state->port);
605 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900606 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900607 return 0;
608 }
609
610 room = tty_buffer_request_room(tty, size);
611
612 if (room < size)
613 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
614 size - room);
615 if (!room)
616 return room;
617
618 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
619
620 port->icount.rx += room;
621 tty_kref_put(tty);
622
623 return room;
624}
625
626static void pch_free_dma(struct uart_port *port)
627{
628 struct eg20t_port *priv;
629 priv = container_of(port, struct eg20t_port, port);
630
631 if (priv->chan_tx) {
632 dma_release_channel(priv->chan_tx);
633 priv->chan_tx = NULL;
634 }
635 if (priv->chan_rx) {
636 dma_release_channel(priv->chan_rx);
637 priv->chan_rx = NULL;
638 }
639 if (sg_dma_address(&priv->sg_rx))
640 dma_free_coherent(port->dev, port->fifosize,
641 sg_virt(&priv->sg_rx),
642 sg_dma_address(&priv->sg_rx));
643
644 return;
645}
646
647static bool filter(struct dma_chan *chan, void *slave)
648{
649 struct pch_dma_slave *param = slave;
650
651 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
652 chan->device->dev)) {
653 chan->private = param;
654 return true;
655 } else {
656 return false;
657 }
658}
659
660static void pch_request_dma(struct uart_port *port)
661{
662 dma_cap_mask_t mask;
663 struct dma_chan *chan;
664 struct pci_dev *dma_dev;
665 struct pch_dma_slave *param;
666 struct eg20t_port *priv =
667 container_of(port, struct eg20t_port, port);
668 dma_cap_zero(mask);
669 dma_cap_set(DMA_SLAVE, mask);
670
Tomoya MORINAGA6c4b47d2011-07-20 20:17:49 +0900671 dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
672 PCI_DEVFN(0xa, 0)); /* Get DMA's dev
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900673 information */
674 /* Set Tx DMA */
675 param = &priv->param_tx;
676 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900677 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
678
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900679 param->tx_reg = port->mapbase + UART_TX;
680 chan = dma_request_channel(mask, filter, param);
681 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900682 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
683 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900684 return;
685 }
686 priv->chan_tx = chan;
687
688 /* Set Rx DMA */
689 param = &priv->param_rx;
690 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900691 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
692
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900693 param->rx_reg = port->mapbase + UART_RX;
694 chan = dma_request_channel(mask, filter, param);
695 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900696 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
697 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900698 dma_release_channel(priv->chan_tx);
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +0900699 priv->chan_tx = NULL;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900700 return;
701 }
702
703 /* Get Consistent memory for DMA */
704 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
705 &priv->rx_buf_dma, GFP_KERNEL);
706 priv->chan_rx = chan;
707}
708
709static void pch_dma_rx_complete(void *arg)
710{
711 struct eg20t_port *priv = arg;
712 struct uart_port *port = &priv->port;
713 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900714 int count;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900715
716 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900717 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900718 return;
719 }
720
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900721 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
722 count = dma_push_rx(priv, priv->trigger_level);
723 if (count)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900724 tty_flip_buffer_push(tty);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900725 tty_kref_put(tty);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900726 async_tx_ack(priv->desc_rx);
727 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900728}
729
730static void pch_dma_tx_complete(void *arg)
731{
732 struct eg20t_port *priv = arg;
733 struct uart_port *port = &priv->port;
734 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900735 struct scatterlist *sg = priv->sg_tx_p;
736 int i;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900737
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900738 for (i = 0; i < priv->nent; i++, sg++) {
739 xmit->tail += sg_dma_len(sg);
740 port->icount.tx += sg_dma_len(sg);
741 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900742 xmit->tail &= UART_XMIT_SIZE - 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900743 async_tx_ack(priv->desc_tx);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900744 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900745 priv->tx_dma_use = 0;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900746 priv->nent = 0;
747 kfree(priv->sg_tx_p);
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900748 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900749}
750
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900751static int pop_tx(struct eg20t_port *priv, int size)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900752{
753 int count = 0;
754 struct uart_port *port = &priv->port;
755 struct circ_buf *xmit = &port->state->xmit;
756
757 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
758 goto pop_tx_end;
759
760 do {
761 int cnt_to_end =
762 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
763 int sz = min(size - count, cnt_to_end);
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900764 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900765 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
766 count += sz;
767 } while (!uart_circ_empty(xmit) && count < size);
768
769pop_tx_end:
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900770 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900771 count, size - count, jiffies);
772
773 return count;
774}
775
776static int handle_rx_to(struct eg20t_port *priv)
777{
778 struct pch_uart_buffer *buf;
779 int rx_size;
780 int ret;
781 if (!priv->start_rx) {
782 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
783 return 0;
784 }
785 buf = &priv->rxbuf;
786 do {
787 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
788 ret = push_rx(priv, buf->buf, rx_size);
789 if (ret)
790 return 0;
791 } while (rx_size == buf->size);
792
793 return PCH_UART_HANDLED_RX_INT;
794}
795
796static int handle_rx(struct eg20t_port *priv)
797{
798 return handle_rx_to(priv);
799}
800
801static int dma_handle_rx(struct eg20t_port *priv)
802{
803 struct uart_port *port = &priv->port;
804 struct dma_async_tx_descriptor *desc;
805 struct scatterlist *sg;
806
807 priv = container_of(port, struct eg20t_port, port);
808 sg = &priv->sg_rx;
809
810 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
811
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900812 sg_dma_len(sg) = priv->trigger_level;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900813
814 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900815 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
816 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900817
818 sg_dma_address(sg) = priv->rx_buf_dma;
819
820 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
Vinod Koula485df42011-10-14 10:47:38 +0530821 sg, 1, DMA_DEV_TO_MEM,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900822 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
823
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900824 if (!desc)
825 return 0;
826
827 priv->desc_rx = desc;
828 desc->callback = pch_dma_rx_complete;
829 desc->callback_param = priv;
830 desc->tx_submit(desc);
831 dma_async_issue_pending(priv->chan_rx);
832
833 return PCH_UART_HANDLED_RX_INT;
834}
835
836static unsigned int handle_tx(struct eg20t_port *priv)
837{
838 struct uart_port *port = &priv->port;
839 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900840 int fifo_size;
841 int tx_size;
842 int size;
843 int tx_empty;
844
845 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900846 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
847 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900848 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
849 priv->tx_empty = 1;
850 return 0;
851 }
852
853 fifo_size = max(priv->fifo_size, 1);
854 tx_empty = 1;
855 if (pop_tx_x(priv, xmit->buf)) {
856 pch_uart_hal_write(priv, xmit->buf, 1);
857 port->icount.tx++;
858 tx_empty = 0;
859 fifo_size--;
860 }
861 size = min(xmit->head - xmit->tail, fifo_size);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900862 if (size < 0)
863 size = fifo_size;
864
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900865 tx_size = pop_tx(priv, size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900866 if (tx_size > 0) {
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900867 port->icount.tx += tx_size;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900868 tx_empty = 0;
869 }
870
871 priv->tx_empty = tx_empty;
872
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900873 if (tx_empty) {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900874 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900875 uart_write_wakeup(port);
876 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900877
878 return PCH_UART_HANDLED_TX_INT;
879}
880
881static unsigned int dma_handle_tx(struct eg20t_port *priv)
882{
883 struct uart_port *port = &priv->port;
884 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900885 struct scatterlist *sg;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900886 int nent;
887 int fifo_size;
888 int tx_empty;
889 struct dma_async_tx_descriptor *desc;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900890 int num;
891 int i;
892 int bytes;
893 int size;
894 int rem;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900895
896 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900897 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
898 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900899 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
900 priv->tx_empty = 1;
901 return 0;
902 }
903
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900904 if (priv->tx_dma_use) {
905 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
906 __func__, jiffies);
907 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
908 priv->tx_empty = 1;
909 return 0;
910 }
911
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900912 fifo_size = max(priv->fifo_size, 1);
913 tx_empty = 1;
914 if (pop_tx_x(priv, xmit->buf)) {
915 pch_uart_hal_write(priv, xmit->buf, 1);
916 port->icount.tx++;
917 tx_empty = 0;
918 fifo_size--;
919 }
920
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900921 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
922 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
923 xmit->tail, UART_XMIT_SIZE));
924 if (!bytes) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900925 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900926 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
927 uart_write_wakeup(port);
928 return 0;
929 }
930
931 if (bytes > fifo_size) {
932 num = bytes / fifo_size + 1;
933 size = fifo_size;
934 rem = bytes % fifo_size;
935 } else {
936 num = 1;
937 size = bytes;
938 rem = bytes;
939 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900940
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900941 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
942 __func__, num, size, rem);
943
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900944 priv->tx_dma_use = 1;
945
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900946 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900947
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900948 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
949 sg = priv->sg_tx_p;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900950
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900951 for (i = 0; i < num; i++, sg++) {
952 if (i == (num - 1))
953 sg_set_page(sg, virt_to_page(xmit->buf),
954 rem, fifo_size * i);
955 else
956 sg_set_page(sg, virt_to_page(xmit->buf),
957 size, fifo_size * i);
958 }
959
960 sg = priv->sg_tx_p;
961 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900962 if (!nent) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900963 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900964 return 0;
965 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900966 priv->nent = nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900967
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900968 for (i = 0; i < nent; i++, sg++) {
969 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
970 fifo_size * i;
971 sg_dma_address(sg) = (sg_dma_address(sg) &
972 ~(UART_XMIT_SIZE - 1)) + sg->offset;
973 if (i == (nent - 1))
974 sg_dma_len(sg) = rem;
975 else
976 sg_dma_len(sg) = size;
977 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900978
979 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
Vinod Koula485df42011-10-14 10:47:38 +0530980 priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900981 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900982 if (!desc) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900983 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
984 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900985 return 0;
986 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900987 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900988 priv->desc_tx = desc;
989 desc->callback = pch_dma_tx_complete;
990 desc->callback_param = priv;
991
992 desc->tx_submit(desc);
993
994 dma_async_issue_pending(priv->chan_tx);
995
996 return PCH_UART_HANDLED_TX_INT;
997}
998
999static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
1000{
1001 u8 fcr = ioread8(priv->membase + UART_FCR);
1002
1003 /* Reset FIFO */
1004 fcr |= UART_FCR_CLEAR_RCVR;
1005 iowrite8(fcr, priv->membase + UART_FCR);
1006
1007 if (lsr & PCH_UART_LSR_ERR)
1008 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
1009
1010 if (lsr & UART_LSR_FE)
1011 dev_err(&priv->pdev->dev, "Framing Error\n");
1012
1013 if (lsr & UART_LSR_PE)
1014 dev_err(&priv->pdev->dev, "Parity Error\n");
1015
1016 if (lsr & UART_LSR_OE)
1017 dev_err(&priv->pdev->dev, "Overrun Error\n");
1018}
1019
1020static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1021{
1022 struct eg20t_port *priv = dev_id;
1023 unsigned int handled;
1024 u8 lsr;
1025 int ret = 0;
1026 unsigned int iid;
1027 unsigned long flags;
1028
1029 spin_lock_irqsave(&priv->port.lock, flags);
1030 handled = 0;
1031 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
1032 switch (iid) {
1033 case PCH_UART_IID_RLS: /* Receiver Line Status */
1034 lsr = pch_uart_hal_get_line_status(priv);
1035 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1036 UART_LSR_PE | UART_LSR_OE)) {
1037 pch_uart_err_ir(priv, lsr);
1038 ret = PCH_UART_HANDLED_RX_ERR_INT;
1039 }
1040 break;
1041 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001042 if (priv->use_dma) {
1043 pch_uart_hal_disable_interrupt(priv,
1044 PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001045 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001046 if (!ret)
1047 pch_uart_hal_enable_interrupt(priv,
1048 PCH_UART_HAL_RX_INT);
1049 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001050 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001051 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001052 break;
1053 case PCH_UART_IID_RDR_TO: /* Received Data Ready
1054 (FIFO Timeout) */
1055 ret = handle_rx_to(priv);
1056 break;
1057 case PCH_UART_IID_THRE: /* Transmitter Holding Register
1058 Empty */
1059 if (priv->use_dma)
1060 ret = dma_handle_tx(priv);
1061 else
1062 ret = handle_tx(priv);
1063 break;
1064 case PCH_UART_IID_MS: /* Modem Status */
1065 ret = PCH_UART_HANDLED_MS_INT;
1066 break;
1067 default: /* Never junp to this label */
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001068 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
1069 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001070 ret = -1;
1071 break;
1072 }
1073 handled |= (unsigned int)ret;
1074 }
1075 if (handled == 0 && iid <= 1) {
1076 if (priv->int_dis_flag)
1077 priv->int_dis_flag = 0;
1078 }
1079
1080 spin_unlock_irqrestore(&priv->port.lock, flags);
1081 return IRQ_RETVAL(handled);
1082}
1083
1084/* This function tests whether the transmitter fifo and shifter for the port
1085 described by 'port' is empty. */
1086static unsigned int pch_uart_tx_empty(struct uart_port *port)
1087{
1088 struct eg20t_port *priv;
Feng Tang30c6c6b2012-02-06 17:24:44 +08001089
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001090 priv = container_of(port, struct eg20t_port, port);
1091 if (priv->tx_empty)
Feng Tang30c6c6b2012-02-06 17:24:44 +08001092 return TIOCSER_TEMT;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001093 else
Feng Tang30c6c6b2012-02-06 17:24:44 +08001094 return 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001095}
1096
1097/* Returns the current state of modem control inputs. */
1098static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1099{
1100 struct eg20t_port *priv;
1101 u8 modem;
1102 unsigned int ret = 0;
1103
1104 priv = container_of(port, struct eg20t_port, port);
1105 modem = pch_uart_hal_get_modem(priv);
1106
1107 if (modem & UART_MSR_DCD)
1108 ret |= TIOCM_CAR;
1109
1110 if (modem & UART_MSR_RI)
1111 ret |= TIOCM_RNG;
1112
1113 if (modem & UART_MSR_DSR)
1114 ret |= TIOCM_DSR;
1115
1116 if (modem & UART_MSR_CTS)
1117 ret |= TIOCM_CTS;
1118
1119 return ret;
1120}
1121
1122static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1123{
1124 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001125 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1126
1127 if (mctrl & TIOCM_DTR)
1128 mcr |= UART_MCR_DTR;
1129 if (mctrl & TIOCM_RTS)
1130 mcr |= UART_MCR_RTS;
1131 if (mctrl & TIOCM_LOOP)
1132 mcr |= UART_MCR_LOOP;
1133
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001134 if (priv->mcr & UART_MCR_AFE)
1135 mcr |= UART_MCR_AFE;
1136
1137 if (mctrl)
1138 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001139}
1140
1141static void pch_uart_stop_tx(struct uart_port *port)
1142{
1143 struct eg20t_port *priv;
1144 priv = container_of(port, struct eg20t_port, port);
1145 priv->start_tx = 0;
1146 priv->tx_dma_use = 0;
1147}
1148
1149static void pch_uart_start_tx(struct uart_port *port)
1150{
1151 struct eg20t_port *priv;
1152
1153 priv = container_of(port, struct eg20t_port, port);
1154
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001155 if (priv->use_dma) {
1156 if (priv->tx_dma_use) {
1157 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1158 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001159 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001160 }
1161 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001162
1163 priv->start_tx = 1;
1164 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1165}
1166
1167static void pch_uart_stop_rx(struct uart_port *port)
1168{
1169 struct eg20t_port *priv;
1170 priv = container_of(port, struct eg20t_port, port);
1171 priv->start_rx = 0;
1172 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1173 priv->int_dis_flag = 1;
1174}
1175
1176/* Enable the modem status interrupts. */
1177static void pch_uart_enable_ms(struct uart_port *port)
1178{
1179 struct eg20t_port *priv;
1180 priv = container_of(port, struct eg20t_port, port);
1181 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1182}
1183
1184/* Control the transmission of a break signal. */
1185static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1186{
1187 struct eg20t_port *priv;
1188 unsigned long flags;
1189
1190 priv = container_of(port, struct eg20t_port, port);
1191 spin_lock_irqsave(&port->lock, flags);
1192 pch_uart_hal_set_break(priv, ctl);
1193 spin_unlock_irqrestore(&port->lock, flags);
1194}
1195
1196/* Grab any interrupt resources and initialise any low level driver state. */
1197static int pch_uart_startup(struct uart_port *port)
1198{
1199 struct eg20t_port *priv;
1200 int ret;
1201 int fifo_size;
1202 int trigger_level;
1203
1204 priv = container_of(port, struct eg20t_port, port);
1205 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001206
1207 if (port->uartclk)
Darren Harta8a3ec92012-03-09 09:51:48 -08001208 priv->uartclk = port->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001209 else
Darren Harta8a3ec92012-03-09 09:51:48 -08001210 port->uartclk = priv->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001211
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001212 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1213 ret = pch_uart_hal_set_line(priv, default_baud,
1214 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1215 PCH_UART_HAL_STB1);
1216 if (ret)
1217 return ret;
1218
1219 switch (priv->fifo_size) {
1220 case 256:
1221 fifo_size = PCH_UART_HAL_FIFO256;
1222 break;
1223 case 64:
1224 fifo_size = PCH_UART_HAL_FIFO64;
1225 break;
1226 case 16:
1227 fifo_size = PCH_UART_HAL_FIFO16;
1228 case 1:
1229 default:
1230 fifo_size = PCH_UART_HAL_FIFO_DIS;
1231 break;
1232 }
1233
1234 switch (priv->trigger) {
1235 case PCH_UART_HAL_TRIGGER1:
1236 trigger_level = 1;
1237 break;
1238 case PCH_UART_HAL_TRIGGER_L:
1239 trigger_level = priv->fifo_size / 4;
1240 break;
1241 case PCH_UART_HAL_TRIGGER_M:
1242 trigger_level = priv->fifo_size / 2;
1243 break;
1244 case PCH_UART_HAL_TRIGGER_H:
1245 default:
1246 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1247 break;
1248 }
1249
1250 priv->trigger_level = trigger_level;
1251 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1252 fifo_size, priv->trigger);
1253 if (ret < 0)
1254 return ret;
1255
1256 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1257 KBUILD_MODNAME, priv);
1258 if (ret < 0)
1259 return ret;
1260
1261 if (priv->use_dma)
1262 pch_request_dma(port);
1263
1264 priv->start_rx = 1;
1265 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1266 uart_update_timeout(port, CS8, default_baud);
1267
1268 return 0;
1269}
1270
1271static void pch_uart_shutdown(struct uart_port *port)
1272{
1273 struct eg20t_port *priv;
1274 int ret;
1275
1276 priv = container_of(port, struct eg20t_port, port);
1277 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1278 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1279 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1280 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1281 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001282 dev_err(priv->port.dev,
1283 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001284
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +09001285 pch_free_dma(port);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001286
1287 free_irq(priv->port.irq, priv);
1288}
1289
1290/* Change the port parameters, including word length, parity, stop
1291 *bits. Update read_status_mask and ignore_status_mask to indicate
1292 *the types of events we are interested in receiving. */
1293static void pch_uart_set_termios(struct uart_port *port,
1294 struct ktermios *termios, struct ktermios *old)
1295{
1296 int baud;
1297 int rtn;
1298 unsigned int parity, bits, stb;
1299 struct eg20t_port *priv;
1300 unsigned long flags;
1301
1302 priv = container_of(port, struct eg20t_port, port);
1303 switch (termios->c_cflag & CSIZE) {
1304 case CS5:
1305 bits = PCH_UART_HAL_5BIT;
1306 break;
1307 case CS6:
1308 bits = PCH_UART_HAL_6BIT;
1309 break;
1310 case CS7:
1311 bits = PCH_UART_HAL_7BIT;
1312 break;
1313 default: /* CS8 */
1314 bits = PCH_UART_HAL_8BIT;
1315 break;
1316 }
1317 if (termios->c_cflag & CSTOPB)
1318 stb = PCH_UART_HAL_STB2;
1319 else
1320 stb = PCH_UART_HAL_STB1;
1321
1322 if (termios->c_cflag & PARENB) {
1323 if (!(termios->c_cflag & PARODD))
1324 parity = PCH_UART_HAL_PARITY_ODD;
1325 else
1326 parity = PCH_UART_HAL_PARITY_EVEN;
1327
Feng Tang30c6c6b2012-02-06 17:24:44 +08001328 } else
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001329 parity = PCH_UART_HAL_PARITY_NONE;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001330
1331 /* Only UART0 has auto hardware flow function */
1332 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1333 priv->mcr |= UART_MCR_AFE;
1334 else
1335 priv->mcr &= ~UART_MCR_AFE;
1336
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001337 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1338
1339 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1340
1341 spin_lock_irqsave(&port->lock, flags);
1342
1343 uart_update_timeout(port, termios->c_cflag, baud);
1344 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1345 if (rtn)
1346 goto out;
1347
Tomoya MORINAGAa1d7cfe2011-10-27 15:45:18 +09001348 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001349 /* Don't rewrite B0 */
1350 if (tty_termios_baud_rate(termios))
1351 tty_termios_encode_baud_rate(termios, baud, baud);
1352
1353out:
1354 spin_unlock_irqrestore(&port->lock, flags);
1355}
1356
1357static const char *pch_uart_type(struct uart_port *port)
1358{
1359 return KBUILD_MODNAME;
1360}
1361
1362static void pch_uart_release_port(struct uart_port *port)
1363{
1364 struct eg20t_port *priv;
1365
1366 priv = container_of(port, struct eg20t_port, port);
1367 pci_iounmap(priv->pdev, priv->membase);
1368 pci_release_regions(priv->pdev);
1369}
1370
1371static int pch_uart_request_port(struct uart_port *port)
1372{
1373 struct eg20t_port *priv;
1374 int ret;
1375 void __iomem *membase;
1376
1377 priv = container_of(port, struct eg20t_port, port);
1378 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1379 if (ret < 0)
1380 return -EBUSY;
1381
1382 membase = pci_iomap(priv->pdev, 1, 0);
1383 if (!membase) {
1384 pci_release_regions(priv->pdev);
1385 return -EBUSY;
1386 }
1387 priv->membase = port->membase = membase;
1388
1389 return 0;
1390}
1391
1392static void pch_uart_config_port(struct uart_port *port, int type)
1393{
1394 struct eg20t_port *priv;
1395
1396 priv = container_of(port, struct eg20t_port, port);
1397 if (type & UART_CONFIG_TYPE) {
1398 port->type = priv->port_type;
1399 pch_uart_request_port(port);
1400 }
1401}
1402
1403static int pch_uart_verify_port(struct uart_port *port,
1404 struct serial_struct *serinfo)
1405{
1406 struct eg20t_port *priv;
1407
1408 priv = container_of(port, struct eg20t_port, port);
1409 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001410 dev_info(priv->port.dev,
1411 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001412 priv->use_dma = 0;
1413 serinfo->flags &= ~UPF_LOW_LATENCY;
1414 } else {
1415#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001416 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1417 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001418 return -EOPNOTSUPP;
1419#endif
1420 priv->use_dma = 1;
1421 priv->use_dma_flag = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001422 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001423 }
1424
1425 return 0;
1426}
1427
1428static struct uart_ops pch_uart_ops = {
1429 .tx_empty = pch_uart_tx_empty,
1430 .set_mctrl = pch_uart_set_mctrl,
1431 .get_mctrl = pch_uart_get_mctrl,
1432 .stop_tx = pch_uart_stop_tx,
1433 .start_tx = pch_uart_start_tx,
1434 .stop_rx = pch_uart_stop_rx,
1435 .enable_ms = pch_uart_enable_ms,
1436 .break_ctl = pch_uart_break_ctl,
1437 .startup = pch_uart_startup,
1438 .shutdown = pch_uart_shutdown,
1439 .set_termios = pch_uart_set_termios,
1440/* .pm = pch_uart_pm, Not supported yet */
1441/* .set_wake = pch_uart_set_wake, Not supported yet */
1442 .type = pch_uart_type,
1443 .release_port = pch_uart_release_port,
1444 .request_port = pch_uart_request_port,
1445 .config_port = pch_uart_config_port,
1446 .verify_port = pch_uart_verify_port
1447};
1448
Alexander Steine30f8672011-11-15 15:04:07 -08001449#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1450
1451/*
1452 * Wait for transmitter & holding register to empty
1453 */
1454static void wait_for_xmitr(struct eg20t_port *up, int bits)
1455{
1456 unsigned int status, tmout = 10000;
1457
1458 /* Wait up to 10ms for the character(s) to be sent. */
1459 for (;;) {
1460 status = ioread8(up->membase + UART_LSR);
1461
1462 if ((status & bits) == bits)
1463 break;
1464 if (--tmout == 0)
1465 break;
1466 udelay(1);
1467 }
1468
1469 /* Wait up to 1s for flow control if necessary */
1470 if (up->port.flags & UPF_CONS_FLOW) {
1471 unsigned int tmout;
1472 for (tmout = 1000000; tmout; tmout--) {
1473 unsigned int msr = ioread8(up->membase + UART_MSR);
1474 if (msr & UART_MSR_CTS)
1475 break;
1476 udelay(1);
1477 touch_nmi_watchdog();
1478 }
1479 }
1480}
1481
1482static void pch_console_putchar(struct uart_port *port, int ch)
1483{
1484 struct eg20t_port *priv =
1485 container_of(port, struct eg20t_port, port);
1486
1487 wait_for_xmitr(priv, UART_LSR_THRE);
1488 iowrite8(ch, priv->membase + PCH_UART_THR);
1489}
1490
1491/*
1492 * Print a string to the serial port trying not to disturb
1493 * any possible real use of the port...
1494 *
1495 * The console_lock must be held when we get here.
1496 */
1497static void
1498pch_console_write(struct console *co, const char *s, unsigned int count)
1499{
1500 struct eg20t_port *priv;
Alexander Steine30f8672011-11-15 15:04:07 -08001501 unsigned long flags;
1502 u8 ier;
1503 int locked = 1;
1504
1505 priv = pch_uart_ports[co->index];
1506
1507 touch_nmi_watchdog();
1508
1509 local_irq_save(flags);
1510 if (priv->port.sysrq) {
1511 /* serial8250_handle_port() already took the lock */
1512 locked = 0;
1513 } else if (oops_in_progress) {
1514 locked = spin_trylock(&priv->port.lock);
1515 } else
1516 spin_lock(&priv->port.lock);
1517
1518 /*
1519 * First save the IER then disable the interrupts
1520 */
1521 ier = ioread8(priv->membase + UART_IER);
1522
1523 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1524
1525 uart_console_write(&priv->port, s, count, pch_console_putchar);
1526
1527 /*
1528 * Finally, wait for transmitter to become empty
1529 * and restore the IER
1530 */
1531 wait_for_xmitr(priv, BOTH_EMPTY);
1532 iowrite8(ier, priv->membase + UART_IER);
1533
1534 if (locked)
1535 spin_unlock(&priv->port.lock);
1536 local_irq_restore(flags);
1537}
1538
1539static int __init pch_console_setup(struct console *co, char *options)
1540{
1541 struct uart_port *port;
1542 int baud = 9600;
1543 int bits = 8;
1544 int parity = 'n';
1545 int flow = 'n';
1546
1547 /*
1548 * Check whether an invalid uart number has been specified, and
1549 * if so, search for the first available port that does have
1550 * console support.
1551 */
1552 if (co->index >= PCH_UART_NR)
1553 co->index = 0;
1554 port = &pch_uart_ports[co->index]->port;
1555
1556 if (!port || (!port->iobase && !port->membase))
1557 return -ENODEV;
1558
1559 /* setup uartclock */
Darren Harta8a3ec92012-03-09 09:51:48 -08001560 port->uartclk = DEFAULT_UARTCLK;
Alexander Steine30f8672011-11-15 15:04:07 -08001561
1562 if (options)
1563 uart_parse_options(options, &baud, &parity, &bits, &flow);
1564
1565 return uart_set_options(port, co, baud, parity, bits, flow);
1566}
1567
1568static struct uart_driver pch_uart_driver;
1569
1570static struct console pch_console = {
1571 .name = PCH_UART_DRIVER_DEVICE,
1572 .write = pch_console_write,
1573 .device = uart_console_device,
1574 .setup = pch_console_setup,
1575 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1576 .index = -1,
1577 .data = &pch_uart_driver,
1578};
1579
1580#define PCH_CONSOLE (&pch_console)
1581#else
1582#define PCH_CONSOLE NULL
1583#endif
1584
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001585static struct uart_driver pch_uart_driver = {
1586 .owner = THIS_MODULE,
1587 .driver_name = KBUILD_MODNAME,
1588 .dev_name = PCH_UART_DRIVER_DEVICE,
1589 .major = 0,
1590 .minor = 0,
1591 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001592 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001593};
1594
1595static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001596 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001597{
1598 struct eg20t_port *priv;
1599 int ret;
1600 unsigned int iobase;
1601 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001602 unsigned char *rxbuf;
Darren Harta8a3ec92012-03-09 09:51:48 -08001603 int fifosize, uartclk;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001604 int port_type;
1605 struct pch_uart_driver_data *board;
Alexander Steinfb139df2011-06-15 15:08:55 -07001606 const char *board_name;
Feng Tangd0114112012-02-06 17:24:43 +08001607 char name[32]; /* for debugfs file name */
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001608
1609 board = &drv_dat[id->driver_data];
1610 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001611
1612 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1613 if (priv == NULL)
1614 goto init_port_alloc_err;
1615
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001616 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001617 if (!rxbuf)
1618 goto init_port_free_txbuf;
1619
Darren Harta8a3ec92012-03-09 09:51:48 -08001620 uartclk = DEFAULT_UARTCLK;
Denis Turischev6ae705b2011-03-10 15:14:00 +02001621
1622 /* quirk for CM-iTC board */
Alexander Steinfb139df2011-06-15 15:08:55 -07001623 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1624 if (board_name && strstr(board_name, "CM-iTC"))
Darren Harta8a3ec92012-03-09 09:51:48 -08001625 uartclk = 192000000; /* 192.0MHz */
Denis Turischev6ae705b2011-03-10 15:14:00 +02001626
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001627 switch (port_type) {
1628 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001629 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001630 break;
1631 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001632 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001633 break;
1634 default:
1635 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1636 goto init_port_hal_free;
1637 }
1638
Alexander Steine4635952011-07-04 08:58:31 +02001639 pci_enable_msi(pdev);
1640
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001641 iobase = pci_resource_start(pdev, 0);
1642 mapbase = pci_resource_start(pdev, 1);
1643 priv->mapbase = mapbase;
1644 priv->iobase = iobase;
1645 priv->pdev = pdev;
1646 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001647 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001648 priv->rxbuf.size = PAGE_SIZE;
1649
1650 priv->fifo_size = fifosize;
Darren Harta8a3ec92012-03-09 09:51:48 -08001651 priv->uartclk = uartclk;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001652 priv->port_type = PORT_MAX_8250 + port_type + 1;
1653 priv->port.dev = &pdev->dev;
1654 priv->port.iobase = iobase;
1655 priv->port.membase = NULL;
1656 priv->port.mapbase = mapbase;
1657 priv->port.irq = pdev->irq;
1658 priv->port.iotype = UPIO_PORT;
1659 priv->port.ops = &pch_uart_ops;
1660 priv->port.flags = UPF_BOOT_AUTOCONF;
1661 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001662 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001663 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1664
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001665 spin_lock_init(&priv->port.lock);
1666
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001667 pci_set_drvdata(pdev, priv);
Feng Tang6f56d0f2012-02-06 17:24:45 +08001668 priv->trigger_level = 1;
1669 priv->fcr = 0;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001670
Alexander Steine30f8672011-11-15 15:04:07 -08001671#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1672 pch_uart_ports[board->line_no] = priv;
1673#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001674 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1675 if (ret < 0)
1676 goto init_port_hal_free;
1677
Feng Tangd0114112012-02-06 17:24:43 +08001678#ifdef CONFIG_DEBUG_FS
1679 snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
1680 priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
1681 NULL, priv, &port_regs_ops);
1682#endif
1683
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001684 return priv;
1685
1686init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001687#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1688 pch_uart_ports[board->line_no] = NULL;
1689#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001690 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001691init_port_free_txbuf:
1692 kfree(priv);
1693init_port_alloc_err:
1694
1695 return NULL;
1696}
1697
1698static void pch_uart_exit_port(struct eg20t_port *priv)
1699{
Feng Tangd0114112012-02-06 17:24:43 +08001700
1701#ifdef CONFIG_DEBUG_FS
1702 if (priv->debugfs)
1703 debugfs_remove(priv->debugfs);
1704#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001705 uart_remove_one_port(&pch_uart_driver, &priv->port);
1706 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001707 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001708}
1709
1710static void pch_uart_pci_remove(struct pci_dev *pdev)
1711{
Feng Tang6f56d0f2012-02-06 17:24:45 +08001712 struct eg20t_port *priv = pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001713
1714 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001715
1716#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1717 pch_uart_ports[priv->port.line] = NULL;
1718#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001719 pch_uart_exit_port(priv);
1720 pci_disable_device(pdev);
1721 kfree(priv);
1722 return;
1723}
1724#ifdef CONFIG_PM
1725static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1726{
1727 struct eg20t_port *priv = pci_get_drvdata(pdev);
1728
1729 uart_suspend_port(&pch_uart_driver, &priv->port);
1730
1731 pci_save_state(pdev);
1732 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1733 return 0;
1734}
1735
1736static int pch_uart_pci_resume(struct pci_dev *pdev)
1737{
1738 struct eg20t_port *priv = pci_get_drvdata(pdev);
1739 int ret;
1740
1741 pci_set_power_state(pdev, PCI_D0);
1742 pci_restore_state(pdev);
1743
1744 ret = pci_enable_device(pdev);
1745 if (ret) {
1746 dev_err(&pdev->dev,
1747 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1748 return ret;
1749 }
1750
1751 uart_resume_port(&pch_uart_driver, &priv->port);
1752
1753 return 0;
1754}
1755#else
1756#define pch_uart_pci_suspend NULL
1757#define pch_uart_pci_resume NULL
1758#endif
1759
1760static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1761 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001762 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001763 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001764 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001765 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001766 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001767 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001768 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001769 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001770 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001771 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001772 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001773 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001774 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001775 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1776 .driver_data = pch_ml7223_uart0},
1777 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1778 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +09001779 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1780 .driver_data = pch_ml7831_uart0},
1781 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1782 .driver_data = pch_ml7831_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001783 {0,},
1784};
1785
1786static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1787 const struct pci_device_id *id)
1788{
1789 int ret;
1790 struct eg20t_port *priv;
1791
1792 ret = pci_enable_device(pdev);
1793 if (ret < 0)
1794 goto probe_error;
1795
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001796 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001797 if (!priv) {
1798 ret = -EBUSY;
1799 goto probe_disable_device;
1800 }
1801 pci_set_drvdata(pdev, priv);
1802
1803 return ret;
1804
1805probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001806 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001807 pci_disable_device(pdev);
1808probe_error:
1809 return ret;
1810}
1811
1812static struct pci_driver pch_uart_pci_driver = {
1813 .name = "pch_uart",
1814 .id_table = pch_uart_pci_id,
1815 .probe = pch_uart_pci_probe,
1816 .remove = __devexit_p(pch_uart_pci_remove),
1817 .suspend = pch_uart_pci_suspend,
1818 .resume = pch_uart_pci_resume,
1819};
1820
1821static int __init pch_uart_module_init(void)
1822{
1823 int ret;
1824
1825 /* register as UART driver */
1826 ret = uart_register_driver(&pch_uart_driver);
1827 if (ret < 0)
1828 return ret;
1829
1830 /* register as PCI driver */
1831 ret = pci_register_driver(&pch_uart_pci_driver);
1832 if (ret < 0)
1833 uart_unregister_driver(&pch_uart_driver);
1834
1835 return ret;
1836}
1837module_init(pch_uart_module_init);
1838
1839static void __exit pch_uart_module_exit(void)
1840{
1841 pci_unregister_driver(&pch_uart_pci_driver);
1842 uart_unregister_driver(&pch_uart_driver);
1843}
1844module_exit(pch_uart_module_exit);
1845
1846MODULE_LICENSE("GPL v2");
1847MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1848module_param(default_baud, uint, S_IRUGO);