blob: d11096c8ca81f04131ef747ab1f71b9fbccae9a3 [file] [log] [blame]
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001/*
Tomoya MORINAGAeca9dfa2011-10-28 09:38:50 +09002 *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09003 *
4 *This program is free software; you can redistribute it and/or modify
5 *it under the terms of the GNU General Public License as published by
6 *the Free Software Foundation; version 2 of the License.
7 *
8 *This program is distributed in the hope that it will be useful,
9 *but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 *GNU General Public License for more details.
12 *
13 *You should have received a copy of the GNU General Public License
14 *along with this program; if not, write to the Free Software
15 *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 */
Uwe Kleine-König0e2adc02011-05-26 10:41:17 +020017#include <linux/kernel.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090018#include <linux/serial_reg.h>
Andrew Morton023bc8e2011-05-24 17:13:44 -070019#include <linux/slab.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090020#include <linux/module.h>
21#include <linux/pci.h>
22#include <linux/serial_core.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020023#include <linux/tty.h>
24#include <linux/tty_flip.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090025#include <linux/interrupt.h>
26#include <linux/io.h>
Denis Turischev6ae705b2011-03-10 15:14:00 +020027#include <linux/dmi.h>
Alexander Steine30f8672011-11-15 15:04:07 -080028#include <linux/console.h>
29#include <linux/nmi.h>
30#include <linux/delay.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090031
Feng Tangd0114112012-02-06 17:24:43 +080032#include <linux/debugfs.h>
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090033#include <linux/dmaengine.h>
34#include <linux/pch_dma.h>
35
36enum {
37 PCH_UART_HANDLED_RX_INT_SHIFT,
38 PCH_UART_HANDLED_TX_INT_SHIFT,
39 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
40 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
41 PCH_UART_HANDLED_MS_INT_SHIFT,
42};
43
44enum {
45 PCH_UART_8LINE,
46 PCH_UART_2LINE,
47};
48
49#define PCH_UART_DRIVER_DEVICE "ttyPCH"
50
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +090051/* Set the max number of UART port
52 * Intel EG20T PCH: 4 port
Tomoya MORINAGAeca9dfa2011-10-28 09:38:50 +090053 * LAPIS Semiconductor ML7213 IOH: 3 port
54 * LAPIS Semiconductor ML7223 IOH: 2 port
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +090055*/
56#define PCH_UART_NR 4
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +090057
58#define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
59#define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
60#define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
61 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
62#define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
63 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
64#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
65
66#define PCH_UART_RBR 0x00
67#define PCH_UART_THR 0x00
68
69#define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
70 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
71#define PCH_UART_IER_ERBFI 0x00000001
72#define PCH_UART_IER_ETBEI 0x00000002
73#define PCH_UART_IER_ELSI 0x00000004
74#define PCH_UART_IER_EDSSI 0x00000008
75
76#define PCH_UART_IIR_IP 0x00000001
77#define PCH_UART_IIR_IID 0x00000006
78#define PCH_UART_IIR_MSI 0x00000000
79#define PCH_UART_IIR_TRI 0x00000002
80#define PCH_UART_IIR_RRI 0x00000004
81#define PCH_UART_IIR_REI 0x00000006
82#define PCH_UART_IIR_TOI 0x00000008
83#define PCH_UART_IIR_FIFO256 0x00000020
84#define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
85#define PCH_UART_IIR_FE 0x000000C0
86
87#define PCH_UART_FCR_FIFOE 0x00000001
88#define PCH_UART_FCR_RFR 0x00000002
89#define PCH_UART_FCR_TFR 0x00000004
90#define PCH_UART_FCR_DMS 0x00000008
91#define PCH_UART_FCR_FIFO256 0x00000020
92#define PCH_UART_FCR_RFTL 0x000000C0
93
94#define PCH_UART_FCR_RFTL1 0x00000000
95#define PCH_UART_FCR_RFTL64 0x00000040
96#define PCH_UART_FCR_RFTL128 0x00000080
97#define PCH_UART_FCR_RFTL224 0x000000C0
98#define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
99#define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
100#define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
101#define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
102#define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
103#define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
104#define PCH_UART_FCR_RFTL_SHIFT 6
105
106#define PCH_UART_LCR_WLS 0x00000003
107#define PCH_UART_LCR_STB 0x00000004
108#define PCH_UART_LCR_PEN 0x00000008
109#define PCH_UART_LCR_EPS 0x00000010
110#define PCH_UART_LCR_SP 0x00000020
111#define PCH_UART_LCR_SB 0x00000040
112#define PCH_UART_LCR_DLAB 0x00000080
113#define PCH_UART_LCR_NP 0x00000000
114#define PCH_UART_LCR_OP PCH_UART_LCR_PEN
115#define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
116#define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
117#define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
118 PCH_UART_LCR_SP)
119
120#define PCH_UART_LCR_5BIT 0x00000000
121#define PCH_UART_LCR_6BIT 0x00000001
122#define PCH_UART_LCR_7BIT 0x00000002
123#define PCH_UART_LCR_8BIT 0x00000003
124
125#define PCH_UART_MCR_DTR 0x00000001
126#define PCH_UART_MCR_RTS 0x00000002
127#define PCH_UART_MCR_OUT 0x0000000C
128#define PCH_UART_MCR_LOOP 0x00000010
129#define PCH_UART_MCR_AFE 0x00000020
130
131#define PCH_UART_LSR_DR 0x00000001
132#define PCH_UART_LSR_ERR (1<<7)
133
134#define PCH_UART_MSR_DCTS 0x00000001
135#define PCH_UART_MSR_DDSR 0x00000002
136#define PCH_UART_MSR_TERI 0x00000004
137#define PCH_UART_MSR_DDCD 0x00000008
138#define PCH_UART_MSR_CTS 0x00000010
139#define PCH_UART_MSR_DSR 0x00000020
140#define PCH_UART_MSR_RI 0x00000040
141#define PCH_UART_MSR_DCD 0x00000080
142#define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
143 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
144
145#define PCH_UART_DLL 0x00
146#define PCH_UART_DLM 0x01
147
Feng Tangd0114112012-02-06 17:24:43 +0800148#define PCH_UART_BRCSR 0x0E
149
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900150#define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
151#define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
152#define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
153#define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
154#define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
155
156#define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
157#define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
158#define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
159#define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
160#define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
161#define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
162#define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
163#define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
164#define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
165#define PCH_UART_HAL_STB1 0
166#define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
167
168#define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
169#define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
170#define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
171 PCH_UART_HAL_CLR_RX_FIFO)
172
173#define PCH_UART_HAL_DMA_MODE0 0
174#define PCH_UART_HAL_FIFO_DIS 0
175#define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
176#define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
177 PCH_UART_FCR_FIFO256)
178#define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
179#define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
180#define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
181#define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
182#define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
183#define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
184#define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
185#define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
186#define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
187#define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
188#define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
189#define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
190#define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
191#define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
192
193#define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
194#define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
195#define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
196#define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
197#define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
198
199#define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
200#define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
201#define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
202#define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
203#define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
204
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +0900205#define PCI_VENDOR_ID_ROHM 0x10DB
206
Alexander Steine30f8672011-11-15 15:04:07 -0800207#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
208
Darren Hart077175f2012-03-09 09:51:49 -0800209#define DEFAULT_UARTCLK 1843200 /* 1.8432 MHz */
210#define CMITC_UARTCLK 192000000 /* 192.0000 MHz */
211#define FRI2_64_UARTCLK 64000000 /* 64.0000 MHz */
212#define FRI2_48_UARTCLK 48000000 /* 48.0000 MHz */
Alexander Steine30f8672011-11-15 15:04:07 -0800213
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900214struct pch_uart_buffer {
215 unsigned char *buf;
216 int size;
217};
218
219struct eg20t_port {
220 struct uart_port port;
221 int port_type;
222 void __iomem *membase;
223 resource_size_t mapbase;
224 unsigned int iobase;
225 struct pci_dev *pdev;
226 int fifo_size;
Darren Harta8a3ec92012-03-09 09:51:48 -0800227 int uartclk;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900228 int start_tx;
229 int start_rx;
230 int tx_empty;
231 int int_dis_flag;
232 int trigger;
233 int trigger_level;
234 struct pch_uart_buffer rxbuf;
235 unsigned int dmsr;
236 unsigned int fcr;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +0900237 unsigned int mcr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900238 unsigned int use_dma;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900239 struct dma_async_tx_descriptor *desc_tx;
240 struct dma_async_tx_descriptor *desc_rx;
241 struct pch_dma_slave param_tx;
242 struct pch_dma_slave param_rx;
243 struct dma_chan *chan_tx;
244 struct dma_chan *chan_rx;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900245 struct scatterlist *sg_tx_p;
246 int nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900247 struct scatterlist sg_rx;
248 int tx_dma_use;
249 void *rx_buf_virt;
250 dma_addr_t rx_buf_dma;
Feng Tangd0114112012-02-06 17:24:43 +0800251
252 struct dentry *debugfs;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900253};
254
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900255/**
256 * struct pch_uart_driver_data - private data structure for UART-DMA
257 * @port_type: The number of DMA channel
258 * @line_no: UART port line number (0, 1, 2...)
259 */
260struct pch_uart_driver_data {
261 int port_type;
262 int line_no;
263};
264
265enum pch_uart_num_t {
266 pch_et20t_uart0 = 0,
267 pch_et20t_uart1,
268 pch_et20t_uart2,
269 pch_et20t_uart3,
270 pch_ml7213_uart0,
271 pch_ml7213_uart1,
272 pch_ml7213_uart2,
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900273 pch_ml7223_uart0,
274 pch_ml7223_uart1,
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900275 pch_ml7831_uart0,
276 pch_ml7831_uart1,
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900277};
278
279static struct pch_uart_driver_data drv_dat[] = {
280 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
281 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
282 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
283 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
284 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
285 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
286 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900287 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
288 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900289 [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
290 [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900291};
292
Alexander Steine30f8672011-11-15 15:04:07 -0800293#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
294static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
295#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900296static unsigned int default_baud = 9600;
Darren Hart2a44feb2012-03-09 09:51:50 -0800297static unsigned int user_uartclk = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900298static const int trigger_level_256[4] = { 1, 64, 128, 224 };
299static const int trigger_level_64[4] = { 1, 16, 32, 56 };
300static const int trigger_level_16[4] = { 1, 4, 8, 14 };
301static const int trigger_level_1[4] = { 1, 1, 1, 1 };
302
Feng Tangd0114112012-02-06 17:24:43 +0800303#ifdef CONFIG_DEBUG_FS
304
305#define PCH_REGS_BUFSIZE 1024
Stephen Boyd234e3402012-04-05 14:25:11 -0700306
Feng Tangd0114112012-02-06 17:24:43 +0800307
308static ssize_t port_show_regs(struct file *file, char __user *user_buf,
309 size_t count, loff_t *ppos)
310{
311 struct eg20t_port *priv = file->private_data;
312 char *buf;
313 u32 len = 0;
314 ssize_t ret;
315 unsigned char lcr;
316
317 buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
318 if (!buf)
319 return 0;
320
321 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
322 "PCH EG20T port[%d] regs:\n", priv->port.line);
323
324 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
325 "=================================\n");
326 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
327 "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
328 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
329 "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
330 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
331 "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
332 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
333 "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
334 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
335 "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
336 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
337 "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
338 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
339 "BRCSR: \t0x%02x\n",
340 ioread8(priv->membase + PCH_UART_BRCSR));
341
342 lcr = ioread8(priv->membase + UART_LCR);
343 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
344 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
345 "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
346 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
347 "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
348 iowrite8(lcr, priv->membase + UART_LCR);
349
350 if (len > PCH_REGS_BUFSIZE)
351 len = PCH_REGS_BUFSIZE;
352
353 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
354 kfree(buf);
355 return ret;
356}
357
358static const struct file_operations port_regs_ops = {
359 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700360 .open = simple_open,
Feng Tangd0114112012-02-06 17:24:43 +0800361 .read = port_show_regs,
362 .llseek = default_llseek,
363};
364#endif /* CONFIG_DEBUG_FS */
365
Darren Hart077175f2012-03-09 09:51:49 -0800366/* Return UART clock, checking for board specific clocks. */
367static int pch_uart_get_uartclk(void)
368{
369 const char *cmp;
370
Darren Hart2a44feb2012-03-09 09:51:50 -0800371 if (user_uartclk)
372 return user_uartclk;
373
Darren Hart077175f2012-03-09 09:51:49 -0800374 cmp = dmi_get_system_info(DMI_BOARD_NAME);
375 if (cmp && strstr(cmp, "CM-iTC"))
376 return CMITC_UARTCLK;
377
378 cmp = dmi_get_system_info(DMI_BIOS_VERSION);
379 if (cmp && strnstr(cmp, "FRI2", 4))
380 return FRI2_64_UARTCLK;
381
382 cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
383 if (cmp && strstr(cmp, "Fish River Island II"))
384 return FRI2_48_UARTCLK;
385
386 return DEFAULT_UARTCLK;
387}
388
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900389static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
390 unsigned int flag)
391{
392 u8 ier = ioread8(priv->membase + UART_IER);
393 ier |= flag & PCH_UART_IER_MASK;
394 iowrite8(ier, priv->membase + UART_IER);
395}
396
397static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
398 unsigned int flag)
399{
400 u8 ier = ioread8(priv->membase + UART_IER);
401 ier &= ~(flag & PCH_UART_IER_MASK);
402 iowrite8(ier, priv->membase + UART_IER);
403}
404
405static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
406 unsigned int parity, unsigned int bits,
407 unsigned int stb)
408{
409 unsigned int dll, dlm, lcr;
410 int div;
411
Darren Harta8a3ec92012-03-09 09:51:48 -0800412 div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900413 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900414 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900415 return -EINVAL;
416 }
417
418 dll = (unsigned int)div & 0x00FFU;
419 dlm = ((unsigned int)div >> 8) & 0x00FFU;
420
421 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900422 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900423 return -EINVAL;
424 }
425
426 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900427 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900428 return -EINVAL;
429 }
430
431 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900432 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900433 return -EINVAL;
434 }
435
436 lcr = parity;
437 lcr |= bits;
438 lcr |= stb;
439
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900440 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900441 __func__, baud, div, lcr, jiffies);
442 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
443 iowrite8(dll, priv->membase + PCH_UART_DLL);
444 iowrite8(dlm, priv->membase + PCH_UART_DLM);
445 iowrite8(lcr, priv->membase + UART_LCR);
446
447 return 0;
448}
449
450static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
451 unsigned int flag)
452{
453 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900454 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
455 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900456 return -EINVAL;
457 }
458
459 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
460 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
461 priv->membase + UART_FCR);
462 iowrite8(priv->fcr, priv->membase + UART_FCR);
463
464 return 0;
465}
466
467static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
468 unsigned int dmamode,
469 unsigned int fifo_size, unsigned int trigger)
470{
471 u8 fcr;
472
473 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900474 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
475 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900476 return -EINVAL;
477 }
478
479 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900480 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
481 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900482 return -EINVAL;
483 }
484
485 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900486 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
487 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900488 return -EINVAL;
489 }
490
491 switch (priv->fifo_size) {
492 case 256:
493 priv->trigger_level =
494 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
495 break;
496 case 64:
497 priv->trigger_level =
498 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
499 break;
500 case 16:
501 priv->trigger_level =
502 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
503 break;
504 default:
505 priv->trigger_level =
506 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
507 break;
508 }
509 fcr =
510 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
511 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
512 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
513 priv->membase + UART_FCR);
514 iowrite8(fcr, priv->membase + UART_FCR);
515 priv->fcr = fcr;
516
517 return 0;
518}
519
520static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
521{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800522 unsigned int msr = ioread8(priv->membase + UART_MSR);
523 priv->dmsr = msr & PCH_UART_MSR_DELTA;
524 return (u8)msr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900525}
526
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900527static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900528 const unsigned char *buf, int tx_size)
529{
530 int i;
531 unsigned int thr;
532
533 for (i = 0; i < tx_size;) {
534 thr = buf[i++];
535 iowrite8(thr, priv->membase + PCH_UART_THR);
536 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900537}
538
539static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
540 int rx_size)
541{
542 int i;
543 u8 rbr, lsr;
544
545 lsr = ioread8(priv->membase + UART_LSR);
546 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
547 i < rx_size && lsr & UART_LSR_DR;
548 lsr = ioread8(priv->membase + UART_LSR)) {
549 rbr = ioread8(priv->membase + PCH_UART_RBR);
550 buf[i++] = rbr;
551 }
552 return i;
553}
554
Tomoya MORINAGA2a583642012-03-26 14:43:01 +0900555static unsigned char pch_uart_hal_get_iid(struct eg20t_port *priv)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900556{
Tomoya MORINAGA2a583642012-03-26 14:43:01 +0900557 return ioread8(priv->membase + UART_IIR) &\
558 (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900559}
560
561static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
562{
563 return ioread8(priv->membase + UART_LSR);
564}
565
566static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
567{
568 unsigned int lcr;
569
570 lcr = ioread8(priv->membase + UART_LCR);
571 if (on)
572 lcr |= PCH_UART_LCR_SB;
573 else
574 lcr &= ~PCH_UART_LCR_SB;
575
576 iowrite8(lcr, priv->membase + UART_LCR);
577}
578
579static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
580 int size)
581{
582 struct uart_port *port;
583 struct tty_struct *tty;
584
585 port = &priv->port;
586 tty = tty_port_tty_get(&port->state->port);
587 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900588 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900589 return -EBUSY;
590 }
591
592 tty_insert_flip_string(tty, buf, size);
593 tty_flip_buffer_push(tty);
594 tty_kref_put(tty);
595
596 return 0;
597}
598
599static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
600{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800601 int ret = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900602 struct uart_port *port = &priv->port;
603
604 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900605 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
606 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900607 buf[0] = port->x_char;
608 port->x_char = 0;
609 ret = 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900610 }
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
Alexandre Bounine16052822012-03-08 16:11:18 -0500838 desc = dmaengine_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
Alexandre Bounine16052822012-03-08 16:11:18 -0500997 desc = dmaengine_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;
Tomoya MORINAGA2a583642012-03-26 14:43:01 +09001044 unsigned char iid;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001045 unsigned long flags;
Tomoya MORINAGA5181fb32012-03-26 14:43:03 +09001046 int next = 1;
1047 u8 msr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001048
1049 spin_lock_irqsave(&priv->port.lock, flags);
1050 handled = 0;
Tomoya MORINAGA5181fb32012-03-26 14:43:03 +09001051 while (next) {
1052 iid = pch_uart_hal_get_iid(priv);
1053 if (iid & PCH_UART_IIR_IP) /* No Interrupt */
1054 break;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001055 switch (iid) {
1056 case PCH_UART_IID_RLS: /* Receiver Line Status */
1057 lsr = pch_uart_hal_get_line_status(priv);
1058 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1059 UART_LSR_PE | UART_LSR_OE)) {
1060 pch_uart_err_ir(priv, lsr);
1061 ret = PCH_UART_HANDLED_RX_ERR_INT;
1062 }
1063 break;
1064 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001065 if (priv->use_dma) {
1066 pch_uart_hal_disable_interrupt(priv,
1067 PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001068 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001069 if (!ret)
1070 pch_uart_hal_enable_interrupt(priv,
1071 PCH_UART_HAL_RX_INT);
1072 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001073 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001074 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001075 break;
1076 case PCH_UART_IID_RDR_TO: /* Received Data Ready
1077 (FIFO Timeout) */
1078 ret = handle_rx_to(priv);
1079 break;
1080 case PCH_UART_IID_THRE: /* Transmitter Holding Register
1081 Empty */
1082 if (priv->use_dma)
1083 ret = dma_handle_tx(priv);
1084 else
1085 ret = handle_tx(priv);
1086 break;
1087 case PCH_UART_IID_MS: /* Modem Status */
Tomoya MORINAGA5181fb32012-03-26 14:43:03 +09001088 msr = pch_uart_hal_get_modem(priv);
1089 next = 0; /* MS ir prioirty is the lowest. So, MS ir
1090 means final interrupt */
1091 if ((msr & UART_MSR_ANY_DELTA) == 0)
1092 break;
1093 ret |= PCH_UART_HANDLED_MS_INT;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001094 break;
1095 default: /* Never junp to this label */
Tomoya MORINAGAb23954a32012-03-26 14:43:02 +09001096 dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001097 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001098 ret = -1;
Tomoya MORINAGA5181fb32012-03-26 14:43:03 +09001099 next = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001100 break;
1101 }
1102 handled |= (unsigned int)ret;
1103 }
1104 if (handled == 0 && iid <= 1) {
1105 if (priv->int_dis_flag)
1106 priv->int_dis_flag = 0;
1107 }
1108
1109 spin_unlock_irqrestore(&priv->port.lock, flags);
1110 return IRQ_RETVAL(handled);
1111}
1112
1113/* This function tests whether the transmitter fifo and shifter for the port
1114 described by 'port' is empty. */
1115static unsigned int pch_uart_tx_empty(struct uart_port *port)
1116{
1117 struct eg20t_port *priv;
Feng Tang30c6c6b2012-02-06 17:24:44 +08001118
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001119 priv = container_of(port, struct eg20t_port, port);
1120 if (priv->tx_empty)
Feng Tang30c6c6b2012-02-06 17:24:44 +08001121 return TIOCSER_TEMT;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001122 else
Feng Tang30c6c6b2012-02-06 17:24:44 +08001123 return 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001124}
1125
1126/* Returns the current state of modem control inputs. */
1127static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1128{
1129 struct eg20t_port *priv;
1130 u8 modem;
1131 unsigned int ret = 0;
1132
1133 priv = container_of(port, struct eg20t_port, port);
1134 modem = pch_uart_hal_get_modem(priv);
1135
1136 if (modem & UART_MSR_DCD)
1137 ret |= TIOCM_CAR;
1138
1139 if (modem & UART_MSR_RI)
1140 ret |= TIOCM_RNG;
1141
1142 if (modem & UART_MSR_DSR)
1143 ret |= TIOCM_DSR;
1144
1145 if (modem & UART_MSR_CTS)
1146 ret |= TIOCM_CTS;
1147
1148 return ret;
1149}
1150
1151static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1152{
1153 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001154 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1155
1156 if (mctrl & TIOCM_DTR)
1157 mcr |= UART_MCR_DTR;
1158 if (mctrl & TIOCM_RTS)
1159 mcr |= UART_MCR_RTS;
1160 if (mctrl & TIOCM_LOOP)
1161 mcr |= UART_MCR_LOOP;
1162
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001163 if (priv->mcr & UART_MCR_AFE)
1164 mcr |= UART_MCR_AFE;
1165
1166 if (mctrl)
1167 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001168}
1169
1170static void pch_uart_stop_tx(struct uart_port *port)
1171{
1172 struct eg20t_port *priv;
1173 priv = container_of(port, struct eg20t_port, port);
1174 priv->start_tx = 0;
1175 priv->tx_dma_use = 0;
1176}
1177
1178static void pch_uart_start_tx(struct uart_port *port)
1179{
1180 struct eg20t_port *priv;
1181
1182 priv = container_of(port, struct eg20t_port, port);
1183
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001184 if (priv->use_dma) {
1185 if (priv->tx_dma_use) {
1186 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1187 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001188 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001189 }
1190 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001191
1192 priv->start_tx = 1;
1193 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1194}
1195
1196static void pch_uart_stop_rx(struct uart_port *port)
1197{
1198 struct eg20t_port *priv;
1199 priv = container_of(port, struct eg20t_port, port);
1200 priv->start_rx = 0;
1201 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1202 priv->int_dis_flag = 1;
1203}
1204
1205/* Enable the modem status interrupts. */
1206static void pch_uart_enable_ms(struct uart_port *port)
1207{
1208 struct eg20t_port *priv;
1209 priv = container_of(port, struct eg20t_port, port);
1210 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1211}
1212
1213/* Control the transmission of a break signal. */
1214static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1215{
1216 struct eg20t_port *priv;
1217 unsigned long flags;
1218
1219 priv = container_of(port, struct eg20t_port, port);
1220 spin_lock_irqsave(&port->lock, flags);
1221 pch_uart_hal_set_break(priv, ctl);
1222 spin_unlock_irqrestore(&port->lock, flags);
1223}
1224
1225/* Grab any interrupt resources and initialise any low level driver state. */
1226static int pch_uart_startup(struct uart_port *port)
1227{
1228 struct eg20t_port *priv;
1229 int ret;
1230 int fifo_size;
1231 int trigger_level;
1232
1233 priv = container_of(port, struct eg20t_port, port);
1234 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001235
1236 if (port->uartclk)
Darren Harta8a3ec92012-03-09 09:51:48 -08001237 priv->uartclk = port->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001238 else
Darren Harta8a3ec92012-03-09 09:51:48 -08001239 port->uartclk = priv->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001240
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001241 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1242 ret = pch_uart_hal_set_line(priv, default_baud,
1243 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1244 PCH_UART_HAL_STB1);
1245 if (ret)
1246 return ret;
1247
1248 switch (priv->fifo_size) {
1249 case 256:
1250 fifo_size = PCH_UART_HAL_FIFO256;
1251 break;
1252 case 64:
1253 fifo_size = PCH_UART_HAL_FIFO64;
1254 break;
1255 case 16:
1256 fifo_size = PCH_UART_HAL_FIFO16;
1257 case 1:
1258 default:
1259 fifo_size = PCH_UART_HAL_FIFO_DIS;
1260 break;
1261 }
1262
1263 switch (priv->trigger) {
1264 case PCH_UART_HAL_TRIGGER1:
1265 trigger_level = 1;
1266 break;
1267 case PCH_UART_HAL_TRIGGER_L:
1268 trigger_level = priv->fifo_size / 4;
1269 break;
1270 case PCH_UART_HAL_TRIGGER_M:
1271 trigger_level = priv->fifo_size / 2;
1272 break;
1273 case PCH_UART_HAL_TRIGGER_H:
1274 default:
1275 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1276 break;
1277 }
1278
1279 priv->trigger_level = trigger_level;
1280 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1281 fifo_size, priv->trigger);
1282 if (ret < 0)
1283 return ret;
1284
1285 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1286 KBUILD_MODNAME, priv);
1287 if (ret < 0)
1288 return ret;
1289
1290 if (priv->use_dma)
1291 pch_request_dma(port);
1292
1293 priv->start_rx = 1;
1294 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1295 uart_update_timeout(port, CS8, default_baud);
1296
1297 return 0;
1298}
1299
1300static void pch_uart_shutdown(struct uart_port *port)
1301{
1302 struct eg20t_port *priv;
1303 int ret;
1304
1305 priv = container_of(port, struct eg20t_port, port);
1306 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1307 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1308 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1309 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1310 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001311 dev_err(priv->port.dev,
1312 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001313
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +09001314 pch_free_dma(port);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001315
1316 free_irq(priv->port.irq, priv);
1317}
1318
1319/* Change the port parameters, including word length, parity, stop
1320 *bits. Update read_status_mask and ignore_status_mask to indicate
1321 *the types of events we are interested in receiving. */
1322static void pch_uart_set_termios(struct uart_port *port,
1323 struct ktermios *termios, struct ktermios *old)
1324{
1325 int baud;
1326 int rtn;
1327 unsigned int parity, bits, stb;
1328 struct eg20t_port *priv;
1329 unsigned long flags;
1330
1331 priv = container_of(port, struct eg20t_port, port);
1332 switch (termios->c_cflag & CSIZE) {
1333 case CS5:
1334 bits = PCH_UART_HAL_5BIT;
1335 break;
1336 case CS6:
1337 bits = PCH_UART_HAL_6BIT;
1338 break;
1339 case CS7:
1340 bits = PCH_UART_HAL_7BIT;
1341 break;
1342 default: /* CS8 */
1343 bits = PCH_UART_HAL_8BIT;
1344 break;
1345 }
1346 if (termios->c_cflag & CSTOPB)
1347 stb = PCH_UART_HAL_STB2;
1348 else
1349 stb = PCH_UART_HAL_STB1;
1350
1351 if (termios->c_cflag & PARENB) {
1352 if (!(termios->c_cflag & PARODD))
1353 parity = PCH_UART_HAL_PARITY_ODD;
1354 else
1355 parity = PCH_UART_HAL_PARITY_EVEN;
1356
Feng Tang30c6c6b2012-02-06 17:24:44 +08001357 } else
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001358 parity = PCH_UART_HAL_PARITY_NONE;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001359
1360 /* Only UART0 has auto hardware flow function */
1361 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1362 priv->mcr |= UART_MCR_AFE;
1363 else
1364 priv->mcr &= ~UART_MCR_AFE;
1365
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001366 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1367
1368 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1369
1370 spin_lock_irqsave(&port->lock, flags);
1371
1372 uart_update_timeout(port, termios->c_cflag, baud);
1373 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1374 if (rtn)
1375 goto out;
1376
Tomoya MORINAGAa1d7cfe2011-10-27 15:45:18 +09001377 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001378 /* Don't rewrite B0 */
1379 if (tty_termios_baud_rate(termios))
1380 tty_termios_encode_baud_rate(termios, baud, baud);
1381
1382out:
1383 spin_unlock_irqrestore(&port->lock, flags);
1384}
1385
1386static const char *pch_uart_type(struct uart_port *port)
1387{
1388 return KBUILD_MODNAME;
1389}
1390
1391static void pch_uart_release_port(struct uart_port *port)
1392{
1393 struct eg20t_port *priv;
1394
1395 priv = container_of(port, struct eg20t_port, port);
1396 pci_iounmap(priv->pdev, priv->membase);
1397 pci_release_regions(priv->pdev);
1398}
1399
1400static int pch_uart_request_port(struct uart_port *port)
1401{
1402 struct eg20t_port *priv;
1403 int ret;
1404 void __iomem *membase;
1405
1406 priv = container_of(port, struct eg20t_port, port);
1407 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1408 if (ret < 0)
1409 return -EBUSY;
1410
1411 membase = pci_iomap(priv->pdev, 1, 0);
1412 if (!membase) {
1413 pci_release_regions(priv->pdev);
1414 return -EBUSY;
1415 }
1416 priv->membase = port->membase = membase;
1417
1418 return 0;
1419}
1420
1421static void pch_uart_config_port(struct uart_port *port, int type)
1422{
1423 struct eg20t_port *priv;
1424
1425 priv = container_of(port, struct eg20t_port, port);
1426 if (type & UART_CONFIG_TYPE) {
1427 port->type = priv->port_type;
1428 pch_uart_request_port(port);
1429 }
1430}
1431
1432static int pch_uart_verify_port(struct uart_port *port,
1433 struct serial_struct *serinfo)
1434{
1435 struct eg20t_port *priv;
1436
1437 priv = container_of(port, struct eg20t_port, port);
1438 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001439 dev_info(priv->port.dev,
1440 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001441 priv->use_dma = 0;
1442 serinfo->flags &= ~UPF_LOW_LATENCY;
1443 } else {
1444#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001445 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1446 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001447 return -EOPNOTSUPP;
1448#endif
1449 priv->use_dma = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001450 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001451 }
1452
1453 return 0;
1454}
1455
1456static struct uart_ops pch_uart_ops = {
1457 .tx_empty = pch_uart_tx_empty,
1458 .set_mctrl = pch_uart_set_mctrl,
1459 .get_mctrl = pch_uart_get_mctrl,
1460 .stop_tx = pch_uart_stop_tx,
1461 .start_tx = pch_uart_start_tx,
1462 .stop_rx = pch_uart_stop_rx,
1463 .enable_ms = pch_uart_enable_ms,
1464 .break_ctl = pch_uart_break_ctl,
1465 .startup = pch_uart_startup,
1466 .shutdown = pch_uart_shutdown,
1467 .set_termios = pch_uart_set_termios,
1468/* .pm = pch_uart_pm, Not supported yet */
1469/* .set_wake = pch_uart_set_wake, Not supported yet */
1470 .type = pch_uart_type,
1471 .release_port = pch_uart_release_port,
1472 .request_port = pch_uart_request_port,
1473 .config_port = pch_uart_config_port,
1474 .verify_port = pch_uart_verify_port
1475};
1476
Alexander Steine30f8672011-11-15 15:04:07 -08001477#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1478
1479/*
1480 * Wait for transmitter & holding register to empty
1481 */
1482static void wait_for_xmitr(struct eg20t_port *up, int bits)
1483{
1484 unsigned int status, tmout = 10000;
1485
1486 /* Wait up to 10ms for the character(s) to be sent. */
1487 for (;;) {
1488 status = ioread8(up->membase + UART_LSR);
1489
1490 if ((status & bits) == bits)
1491 break;
1492 if (--tmout == 0)
1493 break;
1494 udelay(1);
1495 }
1496
1497 /* Wait up to 1s for flow control if necessary */
1498 if (up->port.flags & UPF_CONS_FLOW) {
1499 unsigned int tmout;
1500 for (tmout = 1000000; tmout; tmout--) {
1501 unsigned int msr = ioread8(up->membase + UART_MSR);
1502 if (msr & UART_MSR_CTS)
1503 break;
1504 udelay(1);
1505 touch_nmi_watchdog();
1506 }
1507 }
1508}
1509
1510static void pch_console_putchar(struct uart_port *port, int ch)
1511{
1512 struct eg20t_port *priv =
1513 container_of(port, struct eg20t_port, port);
1514
1515 wait_for_xmitr(priv, UART_LSR_THRE);
1516 iowrite8(ch, priv->membase + PCH_UART_THR);
1517}
1518
1519/*
1520 * Print a string to the serial port trying not to disturb
1521 * any possible real use of the port...
1522 *
1523 * The console_lock must be held when we get here.
1524 */
1525static void
1526pch_console_write(struct console *co, const char *s, unsigned int count)
1527{
1528 struct eg20t_port *priv;
Alexander Steine30f8672011-11-15 15:04:07 -08001529 unsigned long flags;
1530 u8 ier;
1531 int locked = 1;
1532
1533 priv = pch_uart_ports[co->index];
1534
1535 touch_nmi_watchdog();
1536
1537 local_irq_save(flags);
1538 if (priv->port.sysrq) {
1539 /* serial8250_handle_port() already took the lock */
1540 locked = 0;
1541 } else if (oops_in_progress) {
1542 locked = spin_trylock(&priv->port.lock);
1543 } else
1544 spin_lock(&priv->port.lock);
1545
1546 /*
1547 * First save the IER then disable the interrupts
1548 */
1549 ier = ioread8(priv->membase + UART_IER);
1550
1551 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1552
1553 uart_console_write(&priv->port, s, count, pch_console_putchar);
1554
1555 /*
1556 * Finally, wait for transmitter to become empty
1557 * and restore the IER
1558 */
1559 wait_for_xmitr(priv, BOTH_EMPTY);
1560 iowrite8(ier, priv->membase + UART_IER);
1561
1562 if (locked)
1563 spin_unlock(&priv->port.lock);
1564 local_irq_restore(flags);
1565}
1566
1567static int __init pch_console_setup(struct console *co, char *options)
1568{
1569 struct uart_port *port;
Darren Hart7ce92512012-03-09 09:51:51 -08001570 int baud = default_baud;
Alexander Steine30f8672011-11-15 15:04:07 -08001571 int bits = 8;
1572 int parity = 'n';
1573 int flow = 'n';
1574
1575 /*
1576 * Check whether an invalid uart number has been specified, and
1577 * if so, search for the first available port that does have
1578 * console support.
1579 */
1580 if (co->index >= PCH_UART_NR)
1581 co->index = 0;
1582 port = &pch_uart_ports[co->index]->port;
1583
1584 if (!port || (!port->iobase && !port->membase))
1585 return -ENODEV;
1586
Darren Hart077175f2012-03-09 09:51:49 -08001587 port->uartclk = pch_uart_get_uartclk();
Alexander Steine30f8672011-11-15 15:04:07 -08001588
1589 if (options)
1590 uart_parse_options(options, &baud, &parity, &bits, &flow);
1591
1592 return uart_set_options(port, co, baud, parity, bits, flow);
1593}
1594
1595static struct uart_driver pch_uart_driver;
1596
1597static struct console pch_console = {
1598 .name = PCH_UART_DRIVER_DEVICE,
1599 .write = pch_console_write,
1600 .device = uart_console_device,
1601 .setup = pch_console_setup,
1602 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1603 .index = -1,
1604 .data = &pch_uart_driver,
1605};
1606
1607#define PCH_CONSOLE (&pch_console)
1608#else
1609#define PCH_CONSOLE NULL
1610#endif
1611
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001612static struct uart_driver pch_uart_driver = {
1613 .owner = THIS_MODULE,
1614 .driver_name = KBUILD_MODNAME,
1615 .dev_name = PCH_UART_DRIVER_DEVICE,
1616 .major = 0,
1617 .minor = 0,
1618 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001619 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001620};
1621
1622static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001623 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001624{
1625 struct eg20t_port *priv;
1626 int ret;
1627 unsigned int iobase;
1628 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001629 unsigned char *rxbuf;
Darren Hart077175f2012-03-09 09:51:49 -08001630 int fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001631 int port_type;
1632 struct pch_uart_driver_data *board;
Feng Tangd0114112012-02-06 17:24:43 +08001633 char name[32]; /* for debugfs file name */
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001634
1635 board = &drv_dat[id->driver_data];
1636 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001637
1638 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1639 if (priv == NULL)
1640 goto init_port_alloc_err;
1641
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001642 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001643 if (!rxbuf)
1644 goto init_port_free_txbuf;
1645
1646 switch (port_type) {
1647 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001648 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001649 break;
1650 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001651 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001652 break;
1653 default:
1654 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1655 goto init_port_hal_free;
1656 }
1657
Alexander Steine4635952011-07-04 08:58:31 +02001658 pci_enable_msi(pdev);
1659
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001660 iobase = pci_resource_start(pdev, 0);
1661 mapbase = pci_resource_start(pdev, 1);
1662 priv->mapbase = mapbase;
1663 priv->iobase = iobase;
1664 priv->pdev = pdev;
1665 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001666 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001667 priv->rxbuf.size = PAGE_SIZE;
1668
1669 priv->fifo_size = fifosize;
Darren Hart077175f2012-03-09 09:51:49 -08001670 priv->uartclk = pch_uart_get_uartclk();
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001671 priv->port_type = PORT_MAX_8250 + port_type + 1;
1672 priv->port.dev = &pdev->dev;
1673 priv->port.iobase = iobase;
1674 priv->port.membase = NULL;
1675 priv->port.mapbase = mapbase;
1676 priv->port.irq = pdev->irq;
1677 priv->port.iotype = UPIO_PORT;
1678 priv->port.ops = &pch_uart_ops;
1679 priv->port.flags = UPF_BOOT_AUTOCONF;
1680 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001681 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001682 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1683
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001684 spin_lock_init(&priv->port.lock);
1685
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001686 pci_set_drvdata(pdev, priv);
Feng Tang6f56d0f2012-02-06 17:24:45 +08001687 priv->trigger_level = 1;
1688 priv->fcr = 0;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001689
Alexander Steine30f8672011-11-15 15:04:07 -08001690#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1691 pch_uart_ports[board->line_no] = priv;
1692#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001693 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1694 if (ret < 0)
1695 goto init_port_hal_free;
1696
Feng Tangd0114112012-02-06 17:24:43 +08001697#ifdef CONFIG_DEBUG_FS
1698 snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
1699 priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
1700 NULL, priv, &port_regs_ops);
1701#endif
1702
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001703 return priv;
1704
1705init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001706#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1707 pch_uart_ports[board->line_no] = NULL;
1708#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001709 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001710init_port_free_txbuf:
1711 kfree(priv);
1712init_port_alloc_err:
1713
1714 return NULL;
1715}
1716
1717static void pch_uart_exit_port(struct eg20t_port *priv)
1718{
Feng Tangd0114112012-02-06 17:24:43 +08001719
1720#ifdef CONFIG_DEBUG_FS
1721 if (priv->debugfs)
1722 debugfs_remove(priv->debugfs);
1723#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001724 uart_remove_one_port(&pch_uart_driver, &priv->port);
1725 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001726 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001727}
1728
1729static void pch_uart_pci_remove(struct pci_dev *pdev)
1730{
Feng Tang6f56d0f2012-02-06 17:24:45 +08001731 struct eg20t_port *priv = pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001732
1733 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001734
1735#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1736 pch_uart_ports[priv->port.line] = NULL;
1737#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001738 pch_uart_exit_port(priv);
1739 pci_disable_device(pdev);
1740 kfree(priv);
1741 return;
1742}
1743#ifdef CONFIG_PM
1744static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1745{
1746 struct eg20t_port *priv = pci_get_drvdata(pdev);
1747
1748 uart_suspend_port(&pch_uart_driver, &priv->port);
1749
1750 pci_save_state(pdev);
1751 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1752 return 0;
1753}
1754
1755static int pch_uart_pci_resume(struct pci_dev *pdev)
1756{
1757 struct eg20t_port *priv = pci_get_drvdata(pdev);
1758 int ret;
1759
1760 pci_set_power_state(pdev, PCI_D0);
1761 pci_restore_state(pdev);
1762
1763 ret = pci_enable_device(pdev);
1764 if (ret) {
1765 dev_err(&pdev->dev,
1766 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1767 return ret;
1768 }
1769
1770 uart_resume_port(&pch_uart_driver, &priv->port);
1771
1772 return 0;
1773}
1774#else
1775#define pch_uart_pci_suspend NULL
1776#define pch_uart_pci_resume NULL
1777#endif
1778
1779static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1780 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001781 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001782 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001783 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001784 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001785 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001786 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001787 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001788 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001789 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001790 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001791 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001792 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001793 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001794 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1795 .driver_data = pch_ml7223_uart0},
1796 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1797 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +09001798 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1799 .driver_data = pch_ml7831_uart0},
1800 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1801 .driver_data = pch_ml7831_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001802 {0,},
1803};
1804
1805static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1806 const struct pci_device_id *id)
1807{
1808 int ret;
1809 struct eg20t_port *priv;
1810
1811 ret = pci_enable_device(pdev);
1812 if (ret < 0)
1813 goto probe_error;
1814
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001815 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001816 if (!priv) {
1817 ret = -EBUSY;
1818 goto probe_disable_device;
1819 }
1820 pci_set_drvdata(pdev, priv);
1821
1822 return ret;
1823
1824probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001825 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001826 pci_disable_device(pdev);
1827probe_error:
1828 return ret;
1829}
1830
1831static struct pci_driver pch_uart_pci_driver = {
1832 .name = "pch_uart",
1833 .id_table = pch_uart_pci_id,
1834 .probe = pch_uart_pci_probe,
1835 .remove = __devexit_p(pch_uart_pci_remove),
1836 .suspend = pch_uart_pci_suspend,
1837 .resume = pch_uart_pci_resume,
1838};
1839
1840static int __init pch_uart_module_init(void)
1841{
1842 int ret;
1843
1844 /* register as UART driver */
1845 ret = uart_register_driver(&pch_uart_driver);
1846 if (ret < 0)
1847 return ret;
1848
1849 /* register as PCI driver */
1850 ret = pci_register_driver(&pch_uart_pci_driver);
1851 if (ret < 0)
1852 uart_unregister_driver(&pch_uart_driver);
1853
1854 return ret;
1855}
1856module_init(pch_uart_module_init);
1857
1858static void __exit pch_uart_module_exit(void)
1859{
1860 pci_unregister_driver(&pch_uart_pci_driver);
1861 uart_unregister_driver(&pch_uart_driver);
1862}
1863module_exit(pch_uart_module_exit);
1864
1865MODULE_LICENSE("GPL v2");
1866MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1867module_param(default_baud, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001868MODULE_PARM_DESC(default_baud,
1869 "Default BAUD for initial driver state and console (default 9600)");
Darren Hart2a44feb2012-03-09 09:51:50 -08001870module_param(user_uartclk, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001871MODULE_PARM_DESC(user_uartclk,
1872 "Override UART default or board specific UART clock");