blob: 6d9ca2933361500cb943314333eba87e41d11290 [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
209#define DEFAULT_BAUD_RATE 1843200 /* 1.8432MHz */
210
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;
224 int base_baud;
225 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_request(struct pci_dev *pdev, int fifosize,
368 int base_baud)
369{
370 struct eg20t_port *priv = pci_get_drvdata(pdev);
371
372 priv->trigger_level = 1;
373 priv->fcr = 0;
374}
375
376static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
377{
378 unsigned int msr = ioread8(base + UART_MSR);
379 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
380
381 return msr;
382}
383
384static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
385 unsigned int flag)
386{
387 u8 ier = ioread8(priv->membase + UART_IER);
388 ier |= flag & PCH_UART_IER_MASK;
389 iowrite8(ier, priv->membase + UART_IER);
390}
391
392static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
393 unsigned int flag)
394{
395 u8 ier = ioread8(priv->membase + UART_IER);
396 ier &= ~(flag & PCH_UART_IER_MASK);
397 iowrite8(ier, priv->membase + UART_IER);
398}
399
400static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
401 unsigned int parity, unsigned int bits,
402 unsigned int stb)
403{
404 unsigned int dll, dlm, lcr;
405 int div;
406
Uwe Kleine-König0e2adc02011-05-26 10:41:17 +0200407 div = DIV_ROUND_CLOSEST(priv->base_baud / 16, baud);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900408 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900409 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900410 return -EINVAL;
411 }
412
413 dll = (unsigned int)div & 0x00FFU;
414 dlm = ((unsigned int)div >> 8) & 0x00FFU;
415
416 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900417 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900418 return -EINVAL;
419 }
420
421 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900422 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900423 return -EINVAL;
424 }
425
426 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900427 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900428 return -EINVAL;
429 }
430
431 lcr = parity;
432 lcr |= bits;
433 lcr |= stb;
434
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900435 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900436 __func__, baud, div, lcr, jiffies);
437 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
438 iowrite8(dll, priv->membase + PCH_UART_DLL);
439 iowrite8(dlm, priv->membase + PCH_UART_DLM);
440 iowrite8(lcr, priv->membase + UART_LCR);
441
442 return 0;
443}
444
445static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
446 unsigned int flag)
447{
448 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900449 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
450 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900451 return -EINVAL;
452 }
453
454 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
455 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
456 priv->membase + UART_FCR);
457 iowrite8(priv->fcr, priv->membase + UART_FCR);
458
459 return 0;
460}
461
462static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
463 unsigned int dmamode,
464 unsigned int fifo_size, unsigned int trigger)
465{
466 u8 fcr;
467
468 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900469 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
470 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900471 return -EINVAL;
472 }
473
474 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900475 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
476 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900477 return -EINVAL;
478 }
479
480 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900481 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
482 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900483 return -EINVAL;
484 }
485
486 switch (priv->fifo_size) {
487 case 256:
488 priv->trigger_level =
489 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
490 break;
491 case 64:
492 priv->trigger_level =
493 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
494 break;
495 case 16:
496 priv->trigger_level =
497 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
498 break;
499 default:
500 priv->trigger_level =
501 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
502 break;
503 }
504 fcr =
505 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
506 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
507 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
508 priv->membase + UART_FCR);
509 iowrite8(fcr, priv->membase + UART_FCR);
510 priv->fcr = fcr;
511
512 return 0;
513}
514
515static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
516{
517 priv->dmsr = 0;
518 return get_msr(priv, priv->membase);
519}
520
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900521static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900522 const unsigned char *buf, int tx_size)
523{
524 int i;
525 unsigned int thr;
526
527 for (i = 0; i < tx_size;) {
528 thr = buf[i++];
529 iowrite8(thr, priv->membase + PCH_UART_THR);
530 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900531}
532
533static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
534 int rx_size)
535{
536 int i;
537 u8 rbr, lsr;
538
539 lsr = ioread8(priv->membase + UART_LSR);
540 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
541 i < rx_size && lsr & UART_LSR_DR;
542 lsr = ioread8(priv->membase + UART_LSR)) {
543 rbr = ioread8(priv->membase + PCH_UART_RBR);
544 buf[i++] = rbr;
545 }
546 return i;
547}
548
549static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
550{
551 unsigned int iir;
552 int ret;
553
554 iir = ioread8(priv->membase + UART_IIR);
555 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
556 return ret;
557}
558
559static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
560{
561 return ioread8(priv->membase + UART_LSR);
562}
563
564static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
565{
566 unsigned int lcr;
567
568 lcr = ioread8(priv->membase + UART_LCR);
569 if (on)
570 lcr |= PCH_UART_LCR_SB;
571 else
572 lcr &= ~PCH_UART_LCR_SB;
573
574 iowrite8(lcr, priv->membase + UART_LCR);
575}
576
577static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
578 int size)
579{
580 struct uart_port *port;
581 struct tty_struct *tty;
582
583 port = &priv->port;
584 tty = tty_port_tty_get(&port->state->port);
585 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900586 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900587 return -EBUSY;
588 }
589
590 tty_insert_flip_string(tty, buf, size);
591 tty_flip_buffer_push(tty);
592 tty_kref_put(tty);
593
594 return 0;
595}
596
597static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
598{
599 int ret;
600 struct uart_port *port = &priv->port;
601
602 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900603 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
604 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900605 buf[0] = port->x_char;
606 port->x_char = 0;
607 ret = 1;
608 } else {
609 ret = 0;
610 }
611
612 return ret;
613}
614
615static int dma_push_rx(struct eg20t_port *priv, int size)
616{
617 struct tty_struct *tty;
618 int room;
619 struct uart_port *port = &priv->port;
620
621 port = &priv->port;
622 tty = tty_port_tty_get(&port->state->port);
623 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900624 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900625 return 0;
626 }
627
628 room = tty_buffer_request_room(tty, size);
629
630 if (room < size)
631 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
632 size - room);
633 if (!room)
634 return room;
635
636 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
637
638 port->icount.rx += room;
639 tty_kref_put(tty);
640
641 return room;
642}
643
644static void pch_free_dma(struct uart_port *port)
645{
646 struct eg20t_port *priv;
647 priv = container_of(port, struct eg20t_port, port);
648
649 if (priv->chan_tx) {
650 dma_release_channel(priv->chan_tx);
651 priv->chan_tx = NULL;
652 }
653 if (priv->chan_rx) {
654 dma_release_channel(priv->chan_rx);
655 priv->chan_rx = NULL;
656 }
657 if (sg_dma_address(&priv->sg_rx))
658 dma_free_coherent(port->dev, port->fifosize,
659 sg_virt(&priv->sg_rx),
660 sg_dma_address(&priv->sg_rx));
661
662 return;
663}
664
665static bool filter(struct dma_chan *chan, void *slave)
666{
667 struct pch_dma_slave *param = slave;
668
669 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
670 chan->device->dev)) {
671 chan->private = param;
672 return true;
673 } else {
674 return false;
675 }
676}
677
678static void pch_request_dma(struct uart_port *port)
679{
680 dma_cap_mask_t mask;
681 struct dma_chan *chan;
682 struct pci_dev *dma_dev;
683 struct pch_dma_slave *param;
684 struct eg20t_port *priv =
685 container_of(port, struct eg20t_port, port);
686 dma_cap_zero(mask);
687 dma_cap_set(DMA_SLAVE, mask);
688
Tomoya MORINAGA6c4b47d2011-07-20 20:17:49 +0900689 dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
690 PCI_DEVFN(0xa, 0)); /* Get DMA's dev
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900691 information */
692 /* Set Tx DMA */
693 param = &priv->param_tx;
694 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900695 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
696
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900697 param->tx_reg = port->mapbase + UART_TX;
698 chan = dma_request_channel(mask, filter, param);
699 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900700 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
701 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900702 return;
703 }
704 priv->chan_tx = chan;
705
706 /* Set Rx DMA */
707 param = &priv->param_rx;
708 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900709 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
710
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900711 param->rx_reg = port->mapbase + UART_RX;
712 chan = dma_request_channel(mask, filter, param);
713 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900714 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
715 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900716 dma_release_channel(priv->chan_tx);
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +0900717 priv->chan_tx = NULL;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900718 return;
719 }
720
721 /* Get Consistent memory for DMA */
722 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
723 &priv->rx_buf_dma, GFP_KERNEL);
724 priv->chan_rx = chan;
725}
726
727static void pch_dma_rx_complete(void *arg)
728{
729 struct eg20t_port *priv = arg;
730 struct uart_port *port = &priv->port;
731 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900732 int count;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900733
734 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900735 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900736 return;
737 }
738
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900739 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
740 count = dma_push_rx(priv, priv->trigger_level);
741 if (count)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900742 tty_flip_buffer_push(tty);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900743 tty_kref_put(tty);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900744 async_tx_ack(priv->desc_rx);
745 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900746}
747
748static void pch_dma_tx_complete(void *arg)
749{
750 struct eg20t_port *priv = arg;
751 struct uart_port *port = &priv->port;
752 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900753 struct scatterlist *sg = priv->sg_tx_p;
754 int i;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900755
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900756 for (i = 0; i < priv->nent; i++, sg++) {
757 xmit->tail += sg_dma_len(sg);
758 port->icount.tx += sg_dma_len(sg);
759 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900760 xmit->tail &= UART_XMIT_SIZE - 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900761 async_tx_ack(priv->desc_tx);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900762 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900763 priv->tx_dma_use = 0;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900764 priv->nent = 0;
765 kfree(priv->sg_tx_p);
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900766 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900767}
768
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900769static int pop_tx(struct eg20t_port *priv, int size)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900770{
771 int count = 0;
772 struct uart_port *port = &priv->port;
773 struct circ_buf *xmit = &port->state->xmit;
774
775 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
776 goto pop_tx_end;
777
778 do {
779 int cnt_to_end =
780 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
781 int sz = min(size - count, cnt_to_end);
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900782 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900783 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
784 count += sz;
785 } while (!uart_circ_empty(xmit) && count < size);
786
787pop_tx_end:
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900788 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900789 count, size - count, jiffies);
790
791 return count;
792}
793
794static int handle_rx_to(struct eg20t_port *priv)
795{
796 struct pch_uart_buffer *buf;
797 int rx_size;
798 int ret;
799 if (!priv->start_rx) {
800 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
801 return 0;
802 }
803 buf = &priv->rxbuf;
804 do {
805 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
806 ret = push_rx(priv, buf->buf, rx_size);
807 if (ret)
808 return 0;
809 } while (rx_size == buf->size);
810
811 return PCH_UART_HANDLED_RX_INT;
812}
813
814static int handle_rx(struct eg20t_port *priv)
815{
816 return handle_rx_to(priv);
817}
818
819static int dma_handle_rx(struct eg20t_port *priv)
820{
821 struct uart_port *port = &priv->port;
822 struct dma_async_tx_descriptor *desc;
823 struct scatterlist *sg;
824
825 priv = container_of(port, struct eg20t_port, port);
826 sg = &priv->sg_rx;
827
828 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
829
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900830 sg_dma_len(sg) = priv->trigger_level;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900831
832 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900833 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
834 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900835
836 sg_dma_address(sg) = priv->rx_buf_dma;
837
838 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
Vinod Koula485df42011-10-14 10:47:38 +0530839 sg, 1, DMA_DEV_TO_MEM,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900840 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
841
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900842 if (!desc)
843 return 0;
844
845 priv->desc_rx = desc;
846 desc->callback = pch_dma_rx_complete;
847 desc->callback_param = priv;
848 desc->tx_submit(desc);
849 dma_async_issue_pending(priv->chan_rx);
850
851 return PCH_UART_HANDLED_RX_INT;
852}
853
854static unsigned int handle_tx(struct eg20t_port *priv)
855{
856 struct uart_port *port = &priv->port;
857 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900858 int fifo_size;
859 int tx_size;
860 int size;
861 int tx_empty;
862
863 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900864 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
865 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900866 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
867 priv->tx_empty = 1;
868 return 0;
869 }
870
871 fifo_size = max(priv->fifo_size, 1);
872 tx_empty = 1;
873 if (pop_tx_x(priv, xmit->buf)) {
874 pch_uart_hal_write(priv, xmit->buf, 1);
875 port->icount.tx++;
876 tx_empty = 0;
877 fifo_size--;
878 }
879 size = min(xmit->head - xmit->tail, fifo_size);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900880 if (size < 0)
881 size = fifo_size;
882
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900883 tx_size = pop_tx(priv, size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900884 if (tx_size > 0) {
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900885 port->icount.tx += tx_size;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900886 tx_empty = 0;
887 }
888
889 priv->tx_empty = tx_empty;
890
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900891 if (tx_empty) {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900892 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900893 uart_write_wakeup(port);
894 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900895
896 return PCH_UART_HANDLED_TX_INT;
897}
898
899static unsigned int dma_handle_tx(struct eg20t_port *priv)
900{
901 struct uart_port *port = &priv->port;
902 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900903 struct scatterlist *sg;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900904 int nent;
905 int fifo_size;
906 int tx_empty;
907 struct dma_async_tx_descriptor *desc;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900908 int num;
909 int i;
910 int bytes;
911 int size;
912 int rem;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900913
914 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900915 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
916 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900917 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
918 priv->tx_empty = 1;
919 return 0;
920 }
921
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900922 if (priv->tx_dma_use) {
923 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
924 __func__, jiffies);
925 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
926 priv->tx_empty = 1;
927 return 0;
928 }
929
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900930 fifo_size = max(priv->fifo_size, 1);
931 tx_empty = 1;
932 if (pop_tx_x(priv, xmit->buf)) {
933 pch_uart_hal_write(priv, xmit->buf, 1);
934 port->icount.tx++;
935 tx_empty = 0;
936 fifo_size--;
937 }
938
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900939 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
940 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
941 xmit->tail, UART_XMIT_SIZE));
942 if (!bytes) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900943 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900944 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
945 uart_write_wakeup(port);
946 return 0;
947 }
948
949 if (bytes > fifo_size) {
950 num = bytes / fifo_size + 1;
951 size = fifo_size;
952 rem = bytes % fifo_size;
953 } else {
954 num = 1;
955 size = bytes;
956 rem = bytes;
957 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900958
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900959 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
960 __func__, num, size, rem);
961
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900962 priv->tx_dma_use = 1;
963
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900964 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900965
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900966 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
967 sg = priv->sg_tx_p;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900968
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900969 for (i = 0; i < num; i++, sg++) {
970 if (i == (num - 1))
971 sg_set_page(sg, virt_to_page(xmit->buf),
972 rem, fifo_size * i);
973 else
974 sg_set_page(sg, virt_to_page(xmit->buf),
975 size, fifo_size * i);
976 }
977
978 sg = priv->sg_tx_p;
979 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900980 if (!nent) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900981 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900982 return 0;
983 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900984 priv->nent = nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900985
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900986 for (i = 0; i < nent; i++, sg++) {
987 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
988 fifo_size * i;
989 sg_dma_address(sg) = (sg_dma_address(sg) &
990 ~(UART_XMIT_SIZE - 1)) + sg->offset;
991 if (i == (nent - 1))
992 sg_dma_len(sg) = rem;
993 else
994 sg_dma_len(sg) = size;
995 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900996
997 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
Vinod Koula485df42011-10-14 10:47:38 +0530998 priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900999 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001000 if (!desc) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001001 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
1002 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001003 return 0;
1004 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001005 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001006 priv->desc_tx = desc;
1007 desc->callback = pch_dma_tx_complete;
1008 desc->callback_param = priv;
1009
1010 desc->tx_submit(desc);
1011
1012 dma_async_issue_pending(priv->chan_tx);
1013
1014 return PCH_UART_HANDLED_TX_INT;
1015}
1016
1017static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
1018{
1019 u8 fcr = ioread8(priv->membase + UART_FCR);
1020
1021 /* Reset FIFO */
1022 fcr |= UART_FCR_CLEAR_RCVR;
1023 iowrite8(fcr, priv->membase + UART_FCR);
1024
1025 if (lsr & PCH_UART_LSR_ERR)
1026 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
1027
1028 if (lsr & UART_LSR_FE)
1029 dev_err(&priv->pdev->dev, "Framing Error\n");
1030
1031 if (lsr & UART_LSR_PE)
1032 dev_err(&priv->pdev->dev, "Parity Error\n");
1033
1034 if (lsr & UART_LSR_OE)
1035 dev_err(&priv->pdev->dev, "Overrun Error\n");
1036}
1037
1038static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1039{
1040 struct eg20t_port *priv = dev_id;
1041 unsigned int handled;
1042 u8 lsr;
1043 int ret = 0;
1044 unsigned int iid;
1045 unsigned long flags;
1046
1047 spin_lock_irqsave(&priv->port.lock, flags);
1048 handled = 0;
1049 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
1050 switch (iid) {
1051 case PCH_UART_IID_RLS: /* Receiver Line Status */
1052 lsr = pch_uart_hal_get_line_status(priv);
1053 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1054 UART_LSR_PE | UART_LSR_OE)) {
1055 pch_uart_err_ir(priv, lsr);
1056 ret = PCH_UART_HANDLED_RX_ERR_INT;
1057 }
1058 break;
1059 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001060 if (priv->use_dma) {
1061 pch_uart_hal_disable_interrupt(priv,
1062 PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001063 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001064 if (!ret)
1065 pch_uart_hal_enable_interrupt(priv,
1066 PCH_UART_HAL_RX_INT);
1067 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001068 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001069 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001070 break;
1071 case PCH_UART_IID_RDR_TO: /* Received Data Ready
1072 (FIFO Timeout) */
1073 ret = handle_rx_to(priv);
1074 break;
1075 case PCH_UART_IID_THRE: /* Transmitter Holding Register
1076 Empty */
1077 if (priv->use_dma)
1078 ret = dma_handle_tx(priv);
1079 else
1080 ret = handle_tx(priv);
1081 break;
1082 case PCH_UART_IID_MS: /* Modem Status */
1083 ret = PCH_UART_HANDLED_MS_INT;
1084 break;
1085 default: /* Never junp to this label */
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001086 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
1087 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001088 ret = -1;
1089 break;
1090 }
1091 handled |= (unsigned int)ret;
1092 }
1093 if (handled == 0 && iid <= 1) {
1094 if (priv->int_dis_flag)
1095 priv->int_dis_flag = 0;
1096 }
1097
1098 spin_unlock_irqrestore(&priv->port.lock, flags);
1099 return IRQ_RETVAL(handled);
1100}
1101
1102/* This function tests whether the transmitter fifo and shifter for the port
1103 described by 'port' is empty. */
1104static unsigned int pch_uart_tx_empty(struct uart_port *port)
1105{
1106 struct eg20t_port *priv;
1107 int ret;
1108 priv = container_of(port, struct eg20t_port, port);
1109 if (priv->tx_empty)
1110 ret = TIOCSER_TEMT;
1111 else
1112 ret = 0;
1113
1114 return ret;
1115}
1116
1117/* Returns the current state of modem control inputs. */
1118static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1119{
1120 struct eg20t_port *priv;
1121 u8 modem;
1122 unsigned int ret = 0;
1123
1124 priv = container_of(port, struct eg20t_port, port);
1125 modem = pch_uart_hal_get_modem(priv);
1126
1127 if (modem & UART_MSR_DCD)
1128 ret |= TIOCM_CAR;
1129
1130 if (modem & UART_MSR_RI)
1131 ret |= TIOCM_RNG;
1132
1133 if (modem & UART_MSR_DSR)
1134 ret |= TIOCM_DSR;
1135
1136 if (modem & UART_MSR_CTS)
1137 ret |= TIOCM_CTS;
1138
1139 return ret;
1140}
1141
1142static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1143{
1144 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001145 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1146
1147 if (mctrl & TIOCM_DTR)
1148 mcr |= UART_MCR_DTR;
1149 if (mctrl & TIOCM_RTS)
1150 mcr |= UART_MCR_RTS;
1151 if (mctrl & TIOCM_LOOP)
1152 mcr |= UART_MCR_LOOP;
1153
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001154 if (priv->mcr & UART_MCR_AFE)
1155 mcr |= UART_MCR_AFE;
1156
1157 if (mctrl)
1158 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001159}
1160
1161static void pch_uart_stop_tx(struct uart_port *port)
1162{
1163 struct eg20t_port *priv;
1164 priv = container_of(port, struct eg20t_port, port);
1165 priv->start_tx = 0;
1166 priv->tx_dma_use = 0;
1167}
1168
1169static void pch_uart_start_tx(struct uart_port *port)
1170{
1171 struct eg20t_port *priv;
1172
1173 priv = container_of(port, struct eg20t_port, port);
1174
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001175 if (priv->use_dma) {
1176 if (priv->tx_dma_use) {
1177 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1178 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001179 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001180 }
1181 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001182
1183 priv->start_tx = 1;
1184 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1185}
1186
1187static void pch_uart_stop_rx(struct uart_port *port)
1188{
1189 struct eg20t_port *priv;
1190 priv = container_of(port, struct eg20t_port, port);
1191 priv->start_rx = 0;
1192 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1193 priv->int_dis_flag = 1;
1194}
1195
1196/* Enable the modem status interrupts. */
1197static void pch_uart_enable_ms(struct uart_port *port)
1198{
1199 struct eg20t_port *priv;
1200 priv = container_of(port, struct eg20t_port, port);
1201 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1202}
1203
1204/* Control the transmission of a break signal. */
1205static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1206{
1207 struct eg20t_port *priv;
1208 unsigned long flags;
1209
1210 priv = container_of(port, struct eg20t_port, port);
1211 spin_lock_irqsave(&port->lock, flags);
1212 pch_uart_hal_set_break(priv, ctl);
1213 spin_unlock_irqrestore(&port->lock, flags);
1214}
1215
1216/* Grab any interrupt resources and initialise any low level driver state. */
1217static int pch_uart_startup(struct uart_port *port)
1218{
1219 struct eg20t_port *priv;
1220 int ret;
1221 int fifo_size;
1222 int trigger_level;
1223
1224 priv = container_of(port, struct eg20t_port, port);
1225 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001226
1227 if (port->uartclk)
1228 priv->base_baud = port->uartclk;
1229 else
1230 port->uartclk = priv->base_baud;
1231
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001232 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1233 ret = pch_uart_hal_set_line(priv, default_baud,
1234 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1235 PCH_UART_HAL_STB1);
1236 if (ret)
1237 return ret;
1238
1239 switch (priv->fifo_size) {
1240 case 256:
1241 fifo_size = PCH_UART_HAL_FIFO256;
1242 break;
1243 case 64:
1244 fifo_size = PCH_UART_HAL_FIFO64;
1245 break;
1246 case 16:
1247 fifo_size = PCH_UART_HAL_FIFO16;
1248 case 1:
1249 default:
1250 fifo_size = PCH_UART_HAL_FIFO_DIS;
1251 break;
1252 }
1253
1254 switch (priv->trigger) {
1255 case PCH_UART_HAL_TRIGGER1:
1256 trigger_level = 1;
1257 break;
1258 case PCH_UART_HAL_TRIGGER_L:
1259 trigger_level = priv->fifo_size / 4;
1260 break;
1261 case PCH_UART_HAL_TRIGGER_M:
1262 trigger_level = priv->fifo_size / 2;
1263 break;
1264 case PCH_UART_HAL_TRIGGER_H:
1265 default:
1266 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1267 break;
1268 }
1269
1270 priv->trigger_level = trigger_level;
1271 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1272 fifo_size, priv->trigger);
1273 if (ret < 0)
1274 return ret;
1275
1276 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1277 KBUILD_MODNAME, priv);
1278 if (ret < 0)
1279 return ret;
1280
1281 if (priv->use_dma)
1282 pch_request_dma(port);
1283
1284 priv->start_rx = 1;
1285 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1286 uart_update_timeout(port, CS8, default_baud);
1287
1288 return 0;
1289}
1290
1291static void pch_uart_shutdown(struct uart_port *port)
1292{
1293 struct eg20t_port *priv;
1294 int ret;
1295
1296 priv = container_of(port, struct eg20t_port, port);
1297 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1298 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1299 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1300 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1301 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001302 dev_err(priv->port.dev,
1303 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001304
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +09001305 pch_free_dma(port);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001306
1307 free_irq(priv->port.irq, priv);
1308}
1309
1310/* Change the port parameters, including word length, parity, stop
1311 *bits. Update read_status_mask and ignore_status_mask to indicate
1312 *the types of events we are interested in receiving. */
1313static void pch_uart_set_termios(struct uart_port *port,
1314 struct ktermios *termios, struct ktermios *old)
1315{
1316 int baud;
1317 int rtn;
1318 unsigned int parity, bits, stb;
1319 struct eg20t_port *priv;
1320 unsigned long flags;
1321
1322 priv = container_of(port, struct eg20t_port, port);
1323 switch (termios->c_cflag & CSIZE) {
1324 case CS5:
1325 bits = PCH_UART_HAL_5BIT;
1326 break;
1327 case CS6:
1328 bits = PCH_UART_HAL_6BIT;
1329 break;
1330 case CS7:
1331 bits = PCH_UART_HAL_7BIT;
1332 break;
1333 default: /* CS8 */
1334 bits = PCH_UART_HAL_8BIT;
1335 break;
1336 }
1337 if (termios->c_cflag & CSTOPB)
1338 stb = PCH_UART_HAL_STB2;
1339 else
1340 stb = PCH_UART_HAL_STB1;
1341
1342 if (termios->c_cflag & PARENB) {
1343 if (!(termios->c_cflag & PARODD))
1344 parity = PCH_UART_HAL_PARITY_ODD;
1345 else
1346 parity = PCH_UART_HAL_PARITY_EVEN;
1347
1348 } else {
1349 parity = PCH_UART_HAL_PARITY_NONE;
1350 }
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001351
1352 /* Only UART0 has auto hardware flow function */
1353 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1354 priv->mcr |= UART_MCR_AFE;
1355 else
1356 priv->mcr &= ~UART_MCR_AFE;
1357
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001358 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1359
1360 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1361
1362 spin_lock_irqsave(&port->lock, flags);
1363
1364 uart_update_timeout(port, termios->c_cflag, baud);
1365 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1366 if (rtn)
1367 goto out;
1368
Tomoya MORINAGAa1d7cfe2011-10-27 15:45:18 +09001369 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001370 /* Don't rewrite B0 */
1371 if (tty_termios_baud_rate(termios))
1372 tty_termios_encode_baud_rate(termios, baud, baud);
1373
1374out:
1375 spin_unlock_irqrestore(&port->lock, flags);
1376}
1377
1378static const char *pch_uart_type(struct uart_port *port)
1379{
1380 return KBUILD_MODNAME;
1381}
1382
1383static void pch_uart_release_port(struct uart_port *port)
1384{
1385 struct eg20t_port *priv;
1386
1387 priv = container_of(port, struct eg20t_port, port);
1388 pci_iounmap(priv->pdev, priv->membase);
1389 pci_release_regions(priv->pdev);
1390}
1391
1392static int pch_uart_request_port(struct uart_port *port)
1393{
1394 struct eg20t_port *priv;
1395 int ret;
1396 void __iomem *membase;
1397
1398 priv = container_of(port, struct eg20t_port, port);
1399 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1400 if (ret < 0)
1401 return -EBUSY;
1402
1403 membase = pci_iomap(priv->pdev, 1, 0);
1404 if (!membase) {
1405 pci_release_regions(priv->pdev);
1406 return -EBUSY;
1407 }
1408 priv->membase = port->membase = membase;
1409
1410 return 0;
1411}
1412
1413static void pch_uart_config_port(struct uart_port *port, int type)
1414{
1415 struct eg20t_port *priv;
1416
1417 priv = container_of(port, struct eg20t_port, port);
1418 if (type & UART_CONFIG_TYPE) {
1419 port->type = priv->port_type;
1420 pch_uart_request_port(port);
1421 }
1422}
1423
1424static int pch_uart_verify_port(struct uart_port *port,
1425 struct serial_struct *serinfo)
1426{
1427 struct eg20t_port *priv;
1428
1429 priv = container_of(port, struct eg20t_port, port);
1430 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001431 dev_info(priv->port.dev,
1432 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001433 priv->use_dma = 0;
1434 serinfo->flags &= ~UPF_LOW_LATENCY;
1435 } else {
1436#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001437 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1438 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001439 return -EOPNOTSUPP;
1440#endif
1441 priv->use_dma = 1;
1442 priv->use_dma_flag = 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;
1522
1523 unsigned long flags;
1524 u8 ier;
1525 int locked = 1;
1526
1527 priv = pch_uart_ports[co->index];
1528
1529 touch_nmi_watchdog();
1530
1531 local_irq_save(flags);
1532 if (priv->port.sysrq) {
1533 /* serial8250_handle_port() already took the lock */
1534 locked = 0;
1535 } else if (oops_in_progress) {
1536 locked = spin_trylock(&priv->port.lock);
1537 } else
1538 spin_lock(&priv->port.lock);
1539
1540 /*
1541 * First save the IER then disable the interrupts
1542 */
1543 ier = ioread8(priv->membase + UART_IER);
1544
1545 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1546
1547 uart_console_write(&priv->port, s, count, pch_console_putchar);
1548
1549 /*
1550 * Finally, wait for transmitter to become empty
1551 * and restore the IER
1552 */
1553 wait_for_xmitr(priv, BOTH_EMPTY);
1554 iowrite8(ier, priv->membase + UART_IER);
1555
1556 if (locked)
1557 spin_unlock(&priv->port.lock);
1558 local_irq_restore(flags);
1559}
1560
1561static int __init pch_console_setup(struct console *co, char *options)
1562{
1563 struct uart_port *port;
1564 int baud = 9600;
1565 int bits = 8;
1566 int parity = 'n';
1567 int flow = 'n';
1568
1569 /*
1570 * Check whether an invalid uart number has been specified, and
1571 * if so, search for the first available port that does have
1572 * console support.
1573 */
1574 if (co->index >= PCH_UART_NR)
1575 co->index = 0;
1576 port = &pch_uart_ports[co->index]->port;
1577
1578 if (!port || (!port->iobase && !port->membase))
1579 return -ENODEV;
1580
1581 /* setup uartclock */
1582 port->uartclk = DEFAULT_BAUD_RATE;
1583
1584 if (options)
1585 uart_parse_options(options, &baud, &parity, &bits, &flow);
1586
1587 return uart_set_options(port, co, baud, parity, bits, flow);
1588}
1589
1590static struct uart_driver pch_uart_driver;
1591
1592static struct console pch_console = {
1593 .name = PCH_UART_DRIVER_DEVICE,
1594 .write = pch_console_write,
1595 .device = uart_console_device,
1596 .setup = pch_console_setup,
1597 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1598 .index = -1,
1599 .data = &pch_uart_driver,
1600};
1601
1602#define PCH_CONSOLE (&pch_console)
1603#else
1604#define PCH_CONSOLE NULL
1605#endif
1606
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001607static struct uart_driver pch_uart_driver = {
1608 .owner = THIS_MODULE,
1609 .driver_name = KBUILD_MODNAME,
1610 .dev_name = PCH_UART_DRIVER_DEVICE,
1611 .major = 0,
1612 .minor = 0,
1613 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001614 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001615};
1616
1617static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001618 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001619{
1620 struct eg20t_port *priv;
1621 int ret;
1622 unsigned int iobase;
1623 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001624 unsigned char *rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001625 int fifosize, base_baud;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001626 int port_type;
1627 struct pch_uart_driver_data *board;
Alexander Steinfb139df2011-06-15 15:08:55 -07001628 const char *board_name;
Feng Tangd0114112012-02-06 17:24:43 +08001629 char name[32]; /* for debugfs file name */
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001630
1631 board = &drv_dat[id->driver_data];
1632 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001633
1634 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1635 if (priv == NULL)
1636 goto init_port_alloc_err;
1637
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001638 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001639 if (!rxbuf)
1640 goto init_port_free_txbuf;
1641
Alexander Steine30f8672011-11-15 15:04:07 -08001642 base_baud = DEFAULT_BAUD_RATE;
Denis Turischev6ae705b2011-03-10 15:14:00 +02001643
1644 /* quirk for CM-iTC board */
Alexander Steinfb139df2011-06-15 15:08:55 -07001645 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1646 if (board_name && strstr(board_name, "CM-iTC"))
Denis Turischev6ae705b2011-03-10 15:14:00 +02001647 base_baud = 192000000; /* 192.0MHz */
1648
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001649 switch (port_type) {
1650 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001651 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001652 break;
1653 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001654 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001655 break;
1656 default:
1657 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1658 goto init_port_hal_free;
1659 }
1660
Alexander Steine4635952011-07-04 08:58:31 +02001661 pci_enable_msi(pdev);
1662
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001663 iobase = pci_resource_start(pdev, 0);
1664 mapbase = pci_resource_start(pdev, 1);
1665 priv->mapbase = mapbase;
1666 priv->iobase = iobase;
1667 priv->pdev = pdev;
1668 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001669 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001670 priv->rxbuf.size = PAGE_SIZE;
1671
1672 priv->fifo_size = fifosize;
1673 priv->base_baud = base_baud;
1674 priv->port_type = PORT_MAX_8250 + port_type + 1;
1675 priv->port.dev = &pdev->dev;
1676 priv->port.iobase = iobase;
1677 priv->port.membase = NULL;
1678 priv->port.mapbase = mapbase;
1679 priv->port.irq = pdev->irq;
1680 priv->port.iotype = UPIO_PORT;
1681 priv->port.ops = &pch_uart_ops;
1682 priv->port.flags = UPF_BOOT_AUTOCONF;
1683 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001684 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001685 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1686
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001687 spin_lock_init(&priv->port.lock);
1688
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001689 pci_set_drvdata(pdev, priv);
1690 pch_uart_hal_request(pdev, fifosize, base_baud);
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001691
Alexander Steine30f8672011-11-15 15:04:07 -08001692#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1693 pch_uart_ports[board->line_no] = priv;
1694#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001695 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1696 if (ret < 0)
1697 goto init_port_hal_free;
1698
Feng Tangd0114112012-02-06 17:24:43 +08001699#ifdef CONFIG_DEBUG_FS
1700 snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
1701 priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
1702 NULL, priv, &port_regs_ops);
1703#endif
1704
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001705 return priv;
1706
1707init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001708#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1709 pch_uart_ports[board->line_no] = NULL;
1710#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001711 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001712init_port_free_txbuf:
1713 kfree(priv);
1714init_port_alloc_err:
1715
1716 return NULL;
1717}
1718
1719static void pch_uart_exit_port(struct eg20t_port *priv)
1720{
Feng Tangd0114112012-02-06 17:24:43 +08001721
1722#ifdef CONFIG_DEBUG_FS
1723 if (priv->debugfs)
1724 debugfs_remove(priv->debugfs);
1725#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001726 uart_remove_one_port(&pch_uart_driver, &priv->port);
1727 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001728 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001729}
1730
1731static void pch_uart_pci_remove(struct pci_dev *pdev)
1732{
1733 struct eg20t_port *priv;
1734
1735 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001736
1737 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001738
1739#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1740 pch_uart_ports[priv->port.line] = NULL;
1741#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001742 pch_uart_exit_port(priv);
1743 pci_disable_device(pdev);
1744 kfree(priv);
1745 return;
1746}
1747#ifdef CONFIG_PM
1748static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1749{
1750 struct eg20t_port *priv = pci_get_drvdata(pdev);
1751
1752 uart_suspend_port(&pch_uart_driver, &priv->port);
1753
1754 pci_save_state(pdev);
1755 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1756 return 0;
1757}
1758
1759static int pch_uart_pci_resume(struct pci_dev *pdev)
1760{
1761 struct eg20t_port *priv = pci_get_drvdata(pdev);
1762 int ret;
1763
1764 pci_set_power_state(pdev, PCI_D0);
1765 pci_restore_state(pdev);
1766
1767 ret = pci_enable_device(pdev);
1768 if (ret) {
1769 dev_err(&pdev->dev,
1770 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1771 return ret;
1772 }
1773
1774 uart_resume_port(&pch_uart_driver, &priv->port);
1775
1776 return 0;
1777}
1778#else
1779#define pch_uart_pci_suspend NULL
1780#define pch_uart_pci_resume NULL
1781#endif
1782
1783static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1784 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001785 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001786 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001787 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001788 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001789 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001790 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001791 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001792 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001793 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001794 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001795 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001796 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001797 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001798 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1799 .driver_data = pch_ml7223_uart0},
1800 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1801 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +09001802 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1803 .driver_data = pch_ml7831_uart0},
1804 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1805 .driver_data = pch_ml7831_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001806 {0,},
1807};
1808
1809static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1810 const struct pci_device_id *id)
1811{
1812 int ret;
1813 struct eg20t_port *priv;
1814
1815 ret = pci_enable_device(pdev);
1816 if (ret < 0)
1817 goto probe_error;
1818
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001819 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001820 if (!priv) {
1821 ret = -EBUSY;
1822 goto probe_disable_device;
1823 }
1824 pci_set_drvdata(pdev, priv);
1825
1826 return ret;
1827
1828probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001829 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001830 pci_disable_device(pdev);
1831probe_error:
1832 return ret;
1833}
1834
1835static struct pci_driver pch_uart_pci_driver = {
1836 .name = "pch_uart",
1837 .id_table = pch_uart_pci_id,
1838 .probe = pch_uart_pci_probe,
1839 .remove = __devexit_p(pch_uart_pci_remove),
1840 .suspend = pch_uart_pci_suspend,
1841 .resume = pch_uart_pci_resume,
1842};
1843
1844static int __init pch_uart_module_init(void)
1845{
1846 int ret;
1847
1848 /* register as UART driver */
1849 ret = uart_register_driver(&pch_uart_driver);
1850 if (ret < 0)
1851 return ret;
1852
1853 /* register as PCI driver */
1854 ret = pci_register_driver(&pch_uart_pci_driver);
1855 if (ret < 0)
1856 uart_unregister_driver(&pch_uart_driver);
1857
1858 return ret;
1859}
1860module_init(pch_uart_module_init);
1861
1862static void __exit pch_uart_module_exit(void)
1863{
1864 pci_unregister_driver(&pch_uart_pci_driver);
1865 uart_unregister_driver(&pch_uart_driver);
1866}
1867module_exit(pch_uart_module_exit);
1868
1869MODULE_LICENSE("GPL v2");
1870MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1871module_param(default_baud, uint, S_IRUGO);