blob: d035f8ff913dd4336f5dd13dc54c08829ad90a52 [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;
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 MORINAGAb23954a32012-03-26 14:43:02 +09001086 dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001087 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;
Feng Tang30c6c6b2012-02-06 17:24:44 +08001107
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001108 priv = container_of(port, struct eg20t_port, port);
1109 if (priv->tx_empty)
Feng Tang30c6c6b2012-02-06 17:24:44 +08001110 return TIOCSER_TEMT;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001111 else
Feng Tang30c6c6b2012-02-06 17:24:44 +08001112 return 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001113}
1114
1115/* Returns the current state of modem control inputs. */
1116static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1117{
1118 struct eg20t_port *priv;
1119 u8 modem;
1120 unsigned int ret = 0;
1121
1122 priv = container_of(port, struct eg20t_port, port);
1123 modem = pch_uart_hal_get_modem(priv);
1124
1125 if (modem & UART_MSR_DCD)
1126 ret |= TIOCM_CAR;
1127
1128 if (modem & UART_MSR_RI)
1129 ret |= TIOCM_RNG;
1130
1131 if (modem & UART_MSR_DSR)
1132 ret |= TIOCM_DSR;
1133
1134 if (modem & UART_MSR_CTS)
1135 ret |= TIOCM_CTS;
1136
1137 return ret;
1138}
1139
1140static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1141{
1142 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001143 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1144
1145 if (mctrl & TIOCM_DTR)
1146 mcr |= UART_MCR_DTR;
1147 if (mctrl & TIOCM_RTS)
1148 mcr |= UART_MCR_RTS;
1149 if (mctrl & TIOCM_LOOP)
1150 mcr |= UART_MCR_LOOP;
1151
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001152 if (priv->mcr & UART_MCR_AFE)
1153 mcr |= UART_MCR_AFE;
1154
1155 if (mctrl)
1156 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001157}
1158
1159static void pch_uart_stop_tx(struct uart_port *port)
1160{
1161 struct eg20t_port *priv;
1162 priv = container_of(port, struct eg20t_port, port);
1163 priv->start_tx = 0;
1164 priv->tx_dma_use = 0;
1165}
1166
1167static void pch_uart_start_tx(struct uart_port *port)
1168{
1169 struct eg20t_port *priv;
1170
1171 priv = container_of(port, struct eg20t_port, port);
1172
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001173 if (priv->use_dma) {
1174 if (priv->tx_dma_use) {
1175 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1176 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001177 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001178 }
1179 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001180
1181 priv->start_tx = 1;
1182 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1183}
1184
1185static void pch_uart_stop_rx(struct uart_port *port)
1186{
1187 struct eg20t_port *priv;
1188 priv = container_of(port, struct eg20t_port, port);
1189 priv->start_rx = 0;
1190 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1191 priv->int_dis_flag = 1;
1192}
1193
1194/* Enable the modem status interrupts. */
1195static void pch_uart_enable_ms(struct uart_port *port)
1196{
1197 struct eg20t_port *priv;
1198 priv = container_of(port, struct eg20t_port, port);
1199 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1200}
1201
1202/* Control the transmission of a break signal. */
1203static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1204{
1205 struct eg20t_port *priv;
1206 unsigned long flags;
1207
1208 priv = container_of(port, struct eg20t_port, port);
1209 spin_lock_irqsave(&port->lock, flags);
1210 pch_uart_hal_set_break(priv, ctl);
1211 spin_unlock_irqrestore(&port->lock, flags);
1212}
1213
1214/* Grab any interrupt resources and initialise any low level driver state. */
1215static int pch_uart_startup(struct uart_port *port)
1216{
1217 struct eg20t_port *priv;
1218 int ret;
1219 int fifo_size;
1220 int trigger_level;
1221
1222 priv = container_of(port, struct eg20t_port, port);
1223 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001224
1225 if (port->uartclk)
Darren Harta8a3ec92012-03-09 09:51:48 -08001226 priv->uartclk = port->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001227 else
Darren Harta8a3ec92012-03-09 09:51:48 -08001228 port->uartclk = priv->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001229
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001230 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1231 ret = pch_uart_hal_set_line(priv, default_baud,
1232 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1233 PCH_UART_HAL_STB1);
1234 if (ret)
1235 return ret;
1236
1237 switch (priv->fifo_size) {
1238 case 256:
1239 fifo_size = PCH_UART_HAL_FIFO256;
1240 break;
1241 case 64:
1242 fifo_size = PCH_UART_HAL_FIFO64;
1243 break;
1244 case 16:
1245 fifo_size = PCH_UART_HAL_FIFO16;
1246 case 1:
1247 default:
1248 fifo_size = PCH_UART_HAL_FIFO_DIS;
1249 break;
1250 }
1251
1252 switch (priv->trigger) {
1253 case PCH_UART_HAL_TRIGGER1:
1254 trigger_level = 1;
1255 break;
1256 case PCH_UART_HAL_TRIGGER_L:
1257 trigger_level = priv->fifo_size / 4;
1258 break;
1259 case PCH_UART_HAL_TRIGGER_M:
1260 trigger_level = priv->fifo_size / 2;
1261 break;
1262 case PCH_UART_HAL_TRIGGER_H:
1263 default:
1264 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1265 break;
1266 }
1267
1268 priv->trigger_level = trigger_level;
1269 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1270 fifo_size, priv->trigger);
1271 if (ret < 0)
1272 return ret;
1273
1274 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1275 KBUILD_MODNAME, priv);
1276 if (ret < 0)
1277 return ret;
1278
1279 if (priv->use_dma)
1280 pch_request_dma(port);
1281
1282 priv->start_rx = 1;
1283 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1284 uart_update_timeout(port, CS8, default_baud);
1285
1286 return 0;
1287}
1288
1289static void pch_uart_shutdown(struct uart_port *port)
1290{
1291 struct eg20t_port *priv;
1292 int ret;
1293
1294 priv = container_of(port, struct eg20t_port, port);
1295 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1296 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1297 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1298 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1299 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001300 dev_err(priv->port.dev,
1301 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001302
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +09001303 pch_free_dma(port);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001304
1305 free_irq(priv->port.irq, priv);
1306}
1307
1308/* Change the port parameters, including word length, parity, stop
1309 *bits. Update read_status_mask and ignore_status_mask to indicate
1310 *the types of events we are interested in receiving. */
1311static void pch_uart_set_termios(struct uart_port *port,
1312 struct ktermios *termios, struct ktermios *old)
1313{
1314 int baud;
1315 int rtn;
1316 unsigned int parity, bits, stb;
1317 struct eg20t_port *priv;
1318 unsigned long flags;
1319
1320 priv = container_of(port, struct eg20t_port, port);
1321 switch (termios->c_cflag & CSIZE) {
1322 case CS5:
1323 bits = PCH_UART_HAL_5BIT;
1324 break;
1325 case CS6:
1326 bits = PCH_UART_HAL_6BIT;
1327 break;
1328 case CS7:
1329 bits = PCH_UART_HAL_7BIT;
1330 break;
1331 default: /* CS8 */
1332 bits = PCH_UART_HAL_8BIT;
1333 break;
1334 }
1335 if (termios->c_cflag & CSTOPB)
1336 stb = PCH_UART_HAL_STB2;
1337 else
1338 stb = PCH_UART_HAL_STB1;
1339
1340 if (termios->c_cflag & PARENB) {
1341 if (!(termios->c_cflag & PARODD))
1342 parity = PCH_UART_HAL_PARITY_ODD;
1343 else
1344 parity = PCH_UART_HAL_PARITY_EVEN;
1345
Feng Tang30c6c6b2012-02-06 17:24:44 +08001346 } else
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001347 parity = PCH_UART_HAL_PARITY_NONE;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001348
1349 /* Only UART0 has auto hardware flow function */
1350 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1351 priv->mcr |= UART_MCR_AFE;
1352 else
1353 priv->mcr &= ~UART_MCR_AFE;
1354
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001355 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1356
1357 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1358
1359 spin_lock_irqsave(&port->lock, flags);
1360
1361 uart_update_timeout(port, termios->c_cflag, baud);
1362 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1363 if (rtn)
1364 goto out;
1365
Tomoya MORINAGAa1d7cfe2011-10-27 15:45:18 +09001366 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001367 /* Don't rewrite B0 */
1368 if (tty_termios_baud_rate(termios))
1369 tty_termios_encode_baud_rate(termios, baud, baud);
1370
1371out:
1372 spin_unlock_irqrestore(&port->lock, flags);
1373}
1374
1375static const char *pch_uart_type(struct uart_port *port)
1376{
1377 return KBUILD_MODNAME;
1378}
1379
1380static void pch_uart_release_port(struct uart_port *port)
1381{
1382 struct eg20t_port *priv;
1383
1384 priv = container_of(port, struct eg20t_port, port);
1385 pci_iounmap(priv->pdev, priv->membase);
1386 pci_release_regions(priv->pdev);
1387}
1388
1389static int pch_uart_request_port(struct uart_port *port)
1390{
1391 struct eg20t_port *priv;
1392 int ret;
1393 void __iomem *membase;
1394
1395 priv = container_of(port, struct eg20t_port, port);
1396 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1397 if (ret < 0)
1398 return -EBUSY;
1399
1400 membase = pci_iomap(priv->pdev, 1, 0);
1401 if (!membase) {
1402 pci_release_regions(priv->pdev);
1403 return -EBUSY;
1404 }
1405 priv->membase = port->membase = membase;
1406
1407 return 0;
1408}
1409
1410static void pch_uart_config_port(struct uart_port *port, int type)
1411{
1412 struct eg20t_port *priv;
1413
1414 priv = container_of(port, struct eg20t_port, port);
1415 if (type & UART_CONFIG_TYPE) {
1416 port->type = priv->port_type;
1417 pch_uart_request_port(port);
1418 }
1419}
1420
1421static int pch_uart_verify_port(struct uart_port *port,
1422 struct serial_struct *serinfo)
1423{
1424 struct eg20t_port *priv;
1425
1426 priv = container_of(port, struct eg20t_port, port);
1427 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001428 dev_info(priv->port.dev,
1429 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001430 priv->use_dma = 0;
1431 serinfo->flags &= ~UPF_LOW_LATENCY;
1432 } else {
1433#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001434 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1435 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001436 return -EOPNOTSUPP;
1437#endif
1438 priv->use_dma = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001439 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001440 }
1441
1442 return 0;
1443}
1444
1445static struct uart_ops pch_uart_ops = {
1446 .tx_empty = pch_uart_tx_empty,
1447 .set_mctrl = pch_uart_set_mctrl,
1448 .get_mctrl = pch_uart_get_mctrl,
1449 .stop_tx = pch_uart_stop_tx,
1450 .start_tx = pch_uart_start_tx,
1451 .stop_rx = pch_uart_stop_rx,
1452 .enable_ms = pch_uart_enable_ms,
1453 .break_ctl = pch_uart_break_ctl,
1454 .startup = pch_uart_startup,
1455 .shutdown = pch_uart_shutdown,
1456 .set_termios = pch_uart_set_termios,
1457/* .pm = pch_uart_pm, Not supported yet */
1458/* .set_wake = pch_uart_set_wake, Not supported yet */
1459 .type = pch_uart_type,
1460 .release_port = pch_uart_release_port,
1461 .request_port = pch_uart_request_port,
1462 .config_port = pch_uart_config_port,
1463 .verify_port = pch_uart_verify_port
1464};
1465
Alexander Steine30f8672011-11-15 15:04:07 -08001466#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1467
1468/*
1469 * Wait for transmitter & holding register to empty
1470 */
1471static void wait_for_xmitr(struct eg20t_port *up, int bits)
1472{
1473 unsigned int status, tmout = 10000;
1474
1475 /* Wait up to 10ms for the character(s) to be sent. */
1476 for (;;) {
1477 status = ioread8(up->membase + UART_LSR);
1478
1479 if ((status & bits) == bits)
1480 break;
1481 if (--tmout == 0)
1482 break;
1483 udelay(1);
1484 }
1485
1486 /* Wait up to 1s for flow control if necessary */
1487 if (up->port.flags & UPF_CONS_FLOW) {
1488 unsigned int tmout;
1489 for (tmout = 1000000; tmout; tmout--) {
1490 unsigned int msr = ioread8(up->membase + UART_MSR);
1491 if (msr & UART_MSR_CTS)
1492 break;
1493 udelay(1);
1494 touch_nmi_watchdog();
1495 }
1496 }
1497}
1498
1499static void pch_console_putchar(struct uart_port *port, int ch)
1500{
1501 struct eg20t_port *priv =
1502 container_of(port, struct eg20t_port, port);
1503
1504 wait_for_xmitr(priv, UART_LSR_THRE);
1505 iowrite8(ch, priv->membase + PCH_UART_THR);
1506}
1507
1508/*
1509 * Print a string to the serial port trying not to disturb
1510 * any possible real use of the port...
1511 *
1512 * The console_lock must be held when we get here.
1513 */
1514static void
1515pch_console_write(struct console *co, const char *s, unsigned int count)
1516{
1517 struct eg20t_port *priv;
Alexander Steine30f8672011-11-15 15:04:07 -08001518 unsigned long flags;
1519 u8 ier;
1520 int locked = 1;
1521
1522 priv = pch_uart_ports[co->index];
1523
1524 touch_nmi_watchdog();
1525
1526 local_irq_save(flags);
1527 if (priv->port.sysrq) {
1528 /* serial8250_handle_port() already took the lock */
1529 locked = 0;
1530 } else if (oops_in_progress) {
1531 locked = spin_trylock(&priv->port.lock);
1532 } else
1533 spin_lock(&priv->port.lock);
1534
1535 /*
1536 * First save the IER then disable the interrupts
1537 */
1538 ier = ioread8(priv->membase + UART_IER);
1539
1540 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1541
1542 uart_console_write(&priv->port, s, count, pch_console_putchar);
1543
1544 /*
1545 * Finally, wait for transmitter to become empty
1546 * and restore the IER
1547 */
1548 wait_for_xmitr(priv, BOTH_EMPTY);
1549 iowrite8(ier, priv->membase + UART_IER);
1550
1551 if (locked)
1552 spin_unlock(&priv->port.lock);
1553 local_irq_restore(flags);
1554}
1555
1556static int __init pch_console_setup(struct console *co, char *options)
1557{
1558 struct uart_port *port;
Darren Hart7ce92512012-03-09 09:51:51 -08001559 int baud = default_baud;
Alexander Steine30f8672011-11-15 15:04:07 -08001560 int bits = 8;
1561 int parity = 'n';
1562 int flow = 'n';
1563
1564 /*
1565 * Check whether an invalid uart number has been specified, and
1566 * if so, search for the first available port that does have
1567 * console support.
1568 */
1569 if (co->index >= PCH_UART_NR)
1570 co->index = 0;
1571 port = &pch_uart_ports[co->index]->port;
1572
1573 if (!port || (!port->iobase && !port->membase))
1574 return -ENODEV;
1575
Darren Hart077175f2012-03-09 09:51:49 -08001576 port->uartclk = pch_uart_get_uartclk();
Alexander Steine30f8672011-11-15 15:04:07 -08001577
1578 if (options)
1579 uart_parse_options(options, &baud, &parity, &bits, &flow);
1580
1581 return uart_set_options(port, co, baud, parity, bits, flow);
1582}
1583
1584static struct uart_driver pch_uart_driver;
1585
1586static struct console pch_console = {
1587 .name = PCH_UART_DRIVER_DEVICE,
1588 .write = pch_console_write,
1589 .device = uart_console_device,
1590 .setup = pch_console_setup,
1591 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1592 .index = -1,
1593 .data = &pch_uart_driver,
1594};
1595
1596#define PCH_CONSOLE (&pch_console)
1597#else
1598#define PCH_CONSOLE NULL
1599#endif
1600
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001601static struct uart_driver pch_uart_driver = {
1602 .owner = THIS_MODULE,
1603 .driver_name = KBUILD_MODNAME,
1604 .dev_name = PCH_UART_DRIVER_DEVICE,
1605 .major = 0,
1606 .minor = 0,
1607 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001608 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001609};
1610
1611static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001612 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001613{
1614 struct eg20t_port *priv;
1615 int ret;
1616 unsigned int iobase;
1617 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001618 unsigned char *rxbuf;
Darren Hart077175f2012-03-09 09:51:49 -08001619 int fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001620 int port_type;
1621 struct pch_uart_driver_data *board;
Feng Tangd0114112012-02-06 17:24:43 +08001622 char name[32]; /* for debugfs file name */
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001623
1624 board = &drv_dat[id->driver_data];
1625 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001626
1627 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1628 if (priv == NULL)
1629 goto init_port_alloc_err;
1630
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001631 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001632 if (!rxbuf)
1633 goto init_port_free_txbuf;
1634
1635 switch (port_type) {
1636 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001637 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001638 break;
1639 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001640 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001641 break;
1642 default:
1643 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1644 goto init_port_hal_free;
1645 }
1646
Alexander Steine4635952011-07-04 08:58:31 +02001647 pci_enable_msi(pdev);
1648
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001649 iobase = pci_resource_start(pdev, 0);
1650 mapbase = pci_resource_start(pdev, 1);
1651 priv->mapbase = mapbase;
1652 priv->iobase = iobase;
1653 priv->pdev = pdev;
1654 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001655 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001656 priv->rxbuf.size = PAGE_SIZE;
1657
1658 priv->fifo_size = fifosize;
Darren Hart077175f2012-03-09 09:51:49 -08001659 priv->uartclk = pch_uart_get_uartclk();
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001660 priv->port_type = PORT_MAX_8250 + port_type + 1;
1661 priv->port.dev = &pdev->dev;
1662 priv->port.iobase = iobase;
1663 priv->port.membase = NULL;
1664 priv->port.mapbase = mapbase;
1665 priv->port.irq = pdev->irq;
1666 priv->port.iotype = UPIO_PORT;
1667 priv->port.ops = &pch_uart_ops;
1668 priv->port.flags = UPF_BOOT_AUTOCONF;
1669 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001670 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001671 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1672
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001673 spin_lock_init(&priv->port.lock);
1674
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001675 pci_set_drvdata(pdev, priv);
Feng Tang6f56d0f2012-02-06 17:24:45 +08001676 priv->trigger_level = 1;
1677 priv->fcr = 0;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001678
Alexander Steine30f8672011-11-15 15:04:07 -08001679#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1680 pch_uart_ports[board->line_no] = priv;
1681#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001682 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1683 if (ret < 0)
1684 goto init_port_hal_free;
1685
Feng Tangd0114112012-02-06 17:24:43 +08001686#ifdef CONFIG_DEBUG_FS
1687 snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
1688 priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
1689 NULL, priv, &port_regs_ops);
1690#endif
1691
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001692 return priv;
1693
1694init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001695#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1696 pch_uart_ports[board->line_no] = NULL;
1697#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001698 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001699init_port_free_txbuf:
1700 kfree(priv);
1701init_port_alloc_err:
1702
1703 return NULL;
1704}
1705
1706static void pch_uart_exit_port(struct eg20t_port *priv)
1707{
Feng Tangd0114112012-02-06 17:24:43 +08001708
1709#ifdef CONFIG_DEBUG_FS
1710 if (priv->debugfs)
1711 debugfs_remove(priv->debugfs);
1712#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001713 uart_remove_one_port(&pch_uart_driver, &priv->port);
1714 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001715 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001716}
1717
1718static void pch_uart_pci_remove(struct pci_dev *pdev)
1719{
Feng Tang6f56d0f2012-02-06 17:24:45 +08001720 struct eg20t_port *priv = pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001721
1722 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001723
1724#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1725 pch_uart_ports[priv->port.line] = NULL;
1726#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001727 pch_uart_exit_port(priv);
1728 pci_disable_device(pdev);
1729 kfree(priv);
1730 return;
1731}
1732#ifdef CONFIG_PM
1733static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1734{
1735 struct eg20t_port *priv = pci_get_drvdata(pdev);
1736
1737 uart_suspend_port(&pch_uart_driver, &priv->port);
1738
1739 pci_save_state(pdev);
1740 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1741 return 0;
1742}
1743
1744static int pch_uart_pci_resume(struct pci_dev *pdev)
1745{
1746 struct eg20t_port *priv = pci_get_drvdata(pdev);
1747 int ret;
1748
1749 pci_set_power_state(pdev, PCI_D0);
1750 pci_restore_state(pdev);
1751
1752 ret = pci_enable_device(pdev);
1753 if (ret) {
1754 dev_err(&pdev->dev,
1755 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1756 return ret;
1757 }
1758
1759 uart_resume_port(&pch_uart_driver, &priv->port);
1760
1761 return 0;
1762}
1763#else
1764#define pch_uart_pci_suspend NULL
1765#define pch_uart_pci_resume NULL
1766#endif
1767
1768static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1769 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001770 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001771 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001772 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001773 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001774 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001775 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001776 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001777 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001778 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001779 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001780 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001781 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001782 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001783 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1784 .driver_data = pch_ml7223_uart0},
1785 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1786 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +09001787 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1788 .driver_data = pch_ml7831_uart0},
1789 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1790 .driver_data = pch_ml7831_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001791 {0,},
1792};
1793
1794static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1795 const struct pci_device_id *id)
1796{
1797 int ret;
1798 struct eg20t_port *priv;
1799
1800 ret = pci_enable_device(pdev);
1801 if (ret < 0)
1802 goto probe_error;
1803
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001804 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001805 if (!priv) {
1806 ret = -EBUSY;
1807 goto probe_disable_device;
1808 }
1809 pci_set_drvdata(pdev, priv);
1810
1811 return ret;
1812
1813probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001814 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001815 pci_disable_device(pdev);
1816probe_error:
1817 return ret;
1818}
1819
1820static struct pci_driver pch_uart_pci_driver = {
1821 .name = "pch_uart",
1822 .id_table = pch_uart_pci_id,
1823 .probe = pch_uart_pci_probe,
1824 .remove = __devexit_p(pch_uart_pci_remove),
1825 .suspend = pch_uart_pci_suspend,
1826 .resume = pch_uart_pci_resume,
1827};
1828
1829static int __init pch_uart_module_init(void)
1830{
1831 int ret;
1832
1833 /* register as UART driver */
1834 ret = uart_register_driver(&pch_uart_driver);
1835 if (ret < 0)
1836 return ret;
1837
1838 /* register as PCI driver */
1839 ret = pci_register_driver(&pch_uart_pci_driver);
1840 if (ret < 0)
1841 uart_unregister_driver(&pch_uart_driver);
1842
1843 return ret;
1844}
1845module_init(pch_uart_module_init);
1846
1847static void __exit pch_uart_module_exit(void)
1848{
1849 pci_unregister_driver(&pch_uart_pci_driver);
1850 uart_unregister_driver(&pch_uart_driver);
1851}
1852module_exit(pch_uart_module_exit);
1853
1854MODULE_LICENSE("GPL v2");
1855MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1856module_param(default_baud, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001857MODULE_PARM_DESC(default_baud,
1858 "Default BAUD for initial driver state and console (default 9600)");
Darren Hart2a44feb2012-03-09 09:51:50 -08001859module_param(user_uartclk, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001860MODULE_PARM_DESC(user_uartclk,
1861 "Override UART default or board specific UART clock");