blob: 8a18eb045300959ca35a90e5957025de7cb68b99 [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 */
Michael Brunner11bbd5b2012-03-23 11:06:37 +0100213#define NTC1_UARTCLK 64000000 /* 64.0000 MHz */
Alexander Steine30f8672011-11-15 15:04:07 -0800214
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900215struct pch_uart_buffer {
216 unsigned char *buf;
217 int size;
218};
219
220struct eg20t_port {
221 struct uart_port port;
222 int port_type;
223 void __iomem *membase;
224 resource_size_t mapbase;
225 unsigned int iobase;
226 struct pci_dev *pdev;
227 int fifo_size;
Darren Harta8a3ec92012-03-09 09:51:48 -0800228 int uartclk;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900229 int start_tx;
230 int start_rx;
231 int tx_empty;
232 int int_dis_flag;
233 int trigger;
234 int trigger_level;
235 struct pch_uart_buffer rxbuf;
236 unsigned int dmsr;
237 unsigned int fcr;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +0900238 unsigned int mcr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900239 unsigned int use_dma;
240 unsigned int use_dma_flag;
241 struct dma_async_tx_descriptor *desc_tx;
242 struct dma_async_tx_descriptor *desc_rx;
243 struct pch_dma_slave param_tx;
244 struct pch_dma_slave param_rx;
245 struct dma_chan *chan_tx;
246 struct dma_chan *chan_rx;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900247 struct scatterlist *sg_tx_p;
248 int nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900249 struct scatterlist sg_rx;
250 int tx_dma_use;
251 void *rx_buf_virt;
252 dma_addr_t rx_buf_dma;
Feng Tangd0114112012-02-06 17:24:43 +0800253
254 struct dentry *debugfs;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900255};
256
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900257/**
258 * struct pch_uart_driver_data - private data structure for UART-DMA
259 * @port_type: The number of DMA channel
260 * @line_no: UART port line number (0, 1, 2...)
261 */
262struct pch_uart_driver_data {
263 int port_type;
264 int line_no;
265};
266
267enum pch_uart_num_t {
268 pch_et20t_uart0 = 0,
269 pch_et20t_uart1,
270 pch_et20t_uart2,
271 pch_et20t_uart3,
272 pch_ml7213_uart0,
273 pch_ml7213_uart1,
274 pch_ml7213_uart2,
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900275 pch_ml7223_uart0,
276 pch_ml7223_uart1,
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900277 pch_ml7831_uart0,
278 pch_ml7831_uart1,
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900279};
280
281static struct pch_uart_driver_data drv_dat[] = {
282 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
283 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
284 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
285 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
286 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
287 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
288 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +0900289 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
290 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +0900291 [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
292 [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900293};
294
Alexander Steine30f8672011-11-15 15:04:07 -0800295#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
296static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
297#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900298static unsigned int default_baud = 9600;
Darren Hart2a44feb2012-03-09 09:51:50 -0800299static unsigned int user_uartclk = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900300static const int trigger_level_256[4] = { 1, 64, 128, 224 };
301static const int trigger_level_64[4] = { 1, 16, 32, 56 };
302static const int trigger_level_16[4] = { 1, 4, 8, 14 };
303static const int trigger_level_1[4] = { 1, 1, 1, 1 };
304
Feng Tangd0114112012-02-06 17:24:43 +0800305#ifdef CONFIG_DEBUG_FS
306
307#define PCH_REGS_BUFSIZE 1024
308static int pch_show_regs_open(struct inode *inode, struct file *file)
309{
310 file->private_data = inode->i_private;
311 return 0;
312}
313
314static ssize_t port_show_regs(struct file *file, char __user *user_buf,
315 size_t count, loff_t *ppos)
316{
317 struct eg20t_port *priv = file->private_data;
318 char *buf;
319 u32 len = 0;
320 ssize_t ret;
321 unsigned char lcr;
322
323 buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
324 if (!buf)
325 return 0;
326
327 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
328 "PCH EG20T port[%d] regs:\n", priv->port.line);
329
330 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
331 "=================================\n");
332 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
333 "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
334 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
335 "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
336 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
337 "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
338 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
339 "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
340 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
341 "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
342 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
343 "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
344 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
345 "BRCSR: \t0x%02x\n",
346 ioread8(priv->membase + PCH_UART_BRCSR));
347
348 lcr = ioread8(priv->membase + UART_LCR);
349 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
350 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
351 "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
352 len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
353 "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
354 iowrite8(lcr, priv->membase + UART_LCR);
355
356 if (len > PCH_REGS_BUFSIZE)
357 len = PCH_REGS_BUFSIZE;
358
359 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
360 kfree(buf);
361 return ret;
362}
363
364static const struct file_operations port_regs_ops = {
365 .owner = THIS_MODULE,
366 .open = pch_show_regs_open,
367 .read = port_show_regs,
368 .llseek = default_llseek,
369};
370#endif /* CONFIG_DEBUG_FS */
371
Darren Hart077175f2012-03-09 09:51:49 -0800372/* Return UART clock, checking for board specific clocks. */
373static int pch_uart_get_uartclk(void)
374{
375 const char *cmp;
376
Darren Hart2a44feb2012-03-09 09:51:50 -0800377 if (user_uartclk)
378 return user_uartclk;
379
Darren Hart077175f2012-03-09 09:51:49 -0800380 cmp = dmi_get_system_info(DMI_BOARD_NAME);
381 if (cmp && strstr(cmp, "CM-iTC"))
382 return CMITC_UARTCLK;
383
384 cmp = dmi_get_system_info(DMI_BIOS_VERSION);
385 if (cmp && strnstr(cmp, "FRI2", 4))
386 return FRI2_64_UARTCLK;
387
388 cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
389 if (cmp && strstr(cmp, "Fish River Island II"))
390 return FRI2_48_UARTCLK;
391
Michael Brunner11bbd5b2012-03-23 11:06:37 +0100392 /* Kontron COMe-mTT10 (nanoETXexpress-TT) */
393 cmp = dmi_get_system_info(DMI_BOARD_NAME);
394 if (cmp && (strstr(cmp, "COMe-mTT") ||
395 strstr(cmp, "nanoETXexpress-TT")))
396 return NTC1_UARTCLK;
397
Darren Hart077175f2012-03-09 09:51:49 -0800398 return DEFAULT_UARTCLK;
399}
400
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900401static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
402 unsigned int flag)
403{
404 u8 ier = ioread8(priv->membase + UART_IER);
405 ier |= flag & PCH_UART_IER_MASK;
406 iowrite8(ier, priv->membase + UART_IER);
407}
408
409static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
410 unsigned int flag)
411{
412 u8 ier = ioread8(priv->membase + UART_IER);
413 ier &= ~(flag & PCH_UART_IER_MASK);
414 iowrite8(ier, priv->membase + UART_IER);
415}
416
417static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
418 unsigned int parity, unsigned int bits,
419 unsigned int stb)
420{
421 unsigned int dll, dlm, lcr;
422 int div;
423
Darren Harta8a3ec92012-03-09 09:51:48 -0800424 div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900425 if (div < 0 || USHRT_MAX <= div) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900426 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900427 return -EINVAL;
428 }
429
430 dll = (unsigned int)div & 0x00FFU;
431 dlm = ((unsigned int)div >> 8) & 0x00FFU;
432
433 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900434 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900435 return -EINVAL;
436 }
437
438 if (bits & ~PCH_UART_LCR_WLS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900439 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900440 return -EINVAL;
441 }
442
443 if (stb & ~PCH_UART_LCR_STB) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900444 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900445 return -EINVAL;
446 }
447
448 lcr = parity;
449 lcr |= bits;
450 lcr |= stb;
451
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900452 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900453 __func__, baud, div, lcr, jiffies);
454 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
455 iowrite8(dll, priv->membase + PCH_UART_DLL);
456 iowrite8(dlm, priv->membase + PCH_UART_DLM);
457 iowrite8(lcr, priv->membase + UART_LCR);
458
459 return 0;
460}
461
462static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
463 unsigned int flag)
464{
465 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900466 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
467 __func__, flag);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900468 return -EINVAL;
469 }
470
471 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
472 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
473 priv->membase + UART_FCR);
474 iowrite8(priv->fcr, priv->membase + UART_FCR);
475
476 return 0;
477}
478
479static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
480 unsigned int dmamode,
481 unsigned int fifo_size, unsigned int trigger)
482{
483 u8 fcr;
484
485 if (dmamode & ~PCH_UART_FCR_DMS) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900486 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
487 __func__, dmamode);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900488 return -EINVAL;
489 }
490
491 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900492 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
493 __func__, fifo_size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900494 return -EINVAL;
495 }
496
497 if (trigger & ~PCH_UART_FCR_RFTL) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900498 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
499 __func__, trigger);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900500 return -EINVAL;
501 }
502
503 switch (priv->fifo_size) {
504 case 256:
505 priv->trigger_level =
506 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
507 break;
508 case 64:
509 priv->trigger_level =
510 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
511 break;
512 case 16:
513 priv->trigger_level =
514 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
515 break;
516 default:
517 priv->trigger_level =
518 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
519 break;
520 }
521 fcr =
522 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
523 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
524 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
525 priv->membase + UART_FCR);
526 iowrite8(fcr, priv->membase + UART_FCR);
527 priv->fcr = fcr;
528
529 return 0;
530}
531
532static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
533{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800534 unsigned int msr = ioread8(priv->membase + UART_MSR);
535 priv->dmsr = msr & PCH_UART_MSR_DELTA;
536 return (u8)msr;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900537}
538
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900539static void pch_uart_hal_write(struct eg20t_port *priv,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900540 const unsigned char *buf, int tx_size)
541{
542 int i;
543 unsigned int thr;
544
545 for (i = 0; i < tx_size;) {
546 thr = buf[i++];
547 iowrite8(thr, priv->membase + PCH_UART_THR);
548 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900549}
550
551static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
552 int rx_size)
553{
554 int i;
555 u8 rbr, lsr;
556
557 lsr = ioread8(priv->membase + UART_LSR);
558 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
559 i < rx_size && lsr & UART_LSR_DR;
560 lsr = ioread8(priv->membase + UART_LSR)) {
561 rbr = ioread8(priv->membase + PCH_UART_RBR);
562 buf[i++] = rbr;
563 }
564 return i;
565}
566
567static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
568{
569 unsigned int iir;
570 int ret;
571
572 iir = ioread8(priv->membase + UART_IIR);
573 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
574 return ret;
575}
576
577static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
578{
579 return ioread8(priv->membase + UART_LSR);
580}
581
582static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
583{
584 unsigned int lcr;
585
586 lcr = ioread8(priv->membase + UART_LCR);
587 if (on)
588 lcr |= PCH_UART_LCR_SB;
589 else
590 lcr &= ~PCH_UART_LCR_SB;
591
592 iowrite8(lcr, priv->membase + UART_LCR);
593}
594
595static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
596 int size)
597{
598 struct uart_port *port;
599 struct tty_struct *tty;
600
601 port = &priv->port;
602 tty = tty_port_tty_get(&port->state->port);
603 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900604 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900605 return -EBUSY;
606 }
607
608 tty_insert_flip_string(tty, buf, size);
609 tty_flip_buffer_push(tty);
610 tty_kref_put(tty);
611
612 return 0;
613}
614
615static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
616{
Feng Tang30c6c6b2012-02-06 17:24:44 +0800617 int ret = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900618 struct uart_port *port = &priv->port;
619
620 if (port->x_char) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900621 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
622 __func__, port->x_char, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900623 buf[0] = port->x_char;
624 port->x_char = 0;
625 ret = 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900626 }
627
628 return ret;
629}
630
631static int dma_push_rx(struct eg20t_port *priv, int size)
632{
633 struct tty_struct *tty;
634 int room;
635 struct uart_port *port = &priv->port;
636
637 port = &priv->port;
638 tty = tty_port_tty_get(&port->state->port);
639 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900640 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900641 return 0;
642 }
643
644 room = tty_buffer_request_room(tty, size);
645
646 if (room < size)
647 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
648 size - room);
649 if (!room)
650 return room;
651
652 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
653
654 port->icount.rx += room;
655 tty_kref_put(tty);
656
657 return room;
658}
659
660static void pch_free_dma(struct uart_port *port)
661{
662 struct eg20t_port *priv;
663 priv = container_of(port, struct eg20t_port, port);
664
665 if (priv->chan_tx) {
666 dma_release_channel(priv->chan_tx);
667 priv->chan_tx = NULL;
668 }
669 if (priv->chan_rx) {
670 dma_release_channel(priv->chan_rx);
671 priv->chan_rx = NULL;
672 }
673 if (sg_dma_address(&priv->sg_rx))
674 dma_free_coherent(port->dev, port->fifosize,
675 sg_virt(&priv->sg_rx),
676 sg_dma_address(&priv->sg_rx));
677
678 return;
679}
680
681static bool filter(struct dma_chan *chan, void *slave)
682{
683 struct pch_dma_slave *param = slave;
684
685 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
686 chan->device->dev)) {
687 chan->private = param;
688 return true;
689 } else {
690 return false;
691 }
692}
693
694static void pch_request_dma(struct uart_port *port)
695{
696 dma_cap_mask_t mask;
697 struct dma_chan *chan;
698 struct pci_dev *dma_dev;
699 struct pch_dma_slave *param;
700 struct eg20t_port *priv =
701 container_of(port, struct eg20t_port, port);
702 dma_cap_zero(mask);
703 dma_cap_set(DMA_SLAVE, mask);
704
Tomoya MORINAGA6c4b47d2011-07-20 20:17:49 +0900705 dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
706 PCI_DEVFN(0xa, 0)); /* Get DMA's dev
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900707 information */
708 /* Set Tx DMA */
709 param = &priv->param_tx;
710 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900711 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
712
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900713 param->tx_reg = port->mapbase + UART_TX;
714 chan = dma_request_channel(mask, filter, param);
715 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900716 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
717 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900718 return;
719 }
720 priv->chan_tx = chan;
721
722 /* Set Rx DMA */
723 param = &priv->param_rx;
724 param->dma_dev = &dma_dev->dev;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +0900725 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
726
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900727 param->rx_reg = port->mapbase + UART_RX;
728 chan = dma_request_channel(mask, filter, param);
729 if (!chan) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900730 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
731 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900732 dma_release_channel(priv->chan_tx);
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +0900733 priv->chan_tx = NULL;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900734 return;
735 }
736
737 /* Get Consistent memory for DMA */
738 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
739 &priv->rx_buf_dma, GFP_KERNEL);
740 priv->chan_rx = chan;
741}
742
743static void pch_dma_rx_complete(void *arg)
744{
745 struct eg20t_port *priv = arg;
746 struct uart_port *port = &priv->port;
747 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900748 int count;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900749
750 if (!tty) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900751 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900752 return;
753 }
754
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900755 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
756 count = dma_push_rx(priv, priv->trigger_level);
757 if (count)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900758 tty_flip_buffer_push(tty);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900759 tty_kref_put(tty);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900760 async_tx_ack(priv->desc_rx);
761 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900762}
763
764static void pch_dma_tx_complete(void *arg)
765{
766 struct eg20t_port *priv = arg;
767 struct uart_port *port = &priv->port;
768 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900769 struct scatterlist *sg = priv->sg_tx_p;
770 int i;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900771
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900772 for (i = 0; i < priv->nent; i++, sg++) {
773 xmit->tail += sg_dma_len(sg);
774 port->icount.tx += sg_dma_len(sg);
775 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900776 xmit->tail &= UART_XMIT_SIZE - 1;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900777 async_tx_ack(priv->desc_tx);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900778 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900779 priv->tx_dma_use = 0;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900780 priv->nent = 0;
781 kfree(priv->sg_tx_p);
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900782 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900783}
784
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900785static int pop_tx(struct eg20t_port *priv, int size)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900786{
787 int count = 0;
788 struct uart_port *port = &priv->port;
789 struct circ_buf *xmit = &port->state->xmit;
790
791 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
792 goto pop_tx_end;
793
794 do {
795 int cnt_to_end =
796 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
797 int sz = min(size - count, cnt_to_end);
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900798 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900799 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
800 count += sz;
801 } while (!uart_circ_empty(xmit) && count < size);
802
803pop_tx_end:
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900804 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900805 count, size - count, jiffies);
806
807 return count;
808}
809
810static int handle_rx_to(struct eg20t_port *priv)
811{
812 struct pch_uart_buffer *buf;
813 int rx_size;
814 int ret;
815 if (!priv->start_rx) {
816 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
817 return 0;
818 }
819 buf = &priv->rxbuf;
820 do {
821 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
822 ret = push_rx(priv, buf->buf, rx_size);
823 if (ret)
824 return 0;
825 } while (rx_size == buf->size);
826
827 return PCH_UART_HANDLED_RX_INT;
828}
829
830static int handle_rx(struct eg20t_port *priv)
831{
832 return handle_rx_to(priv);
833}
834
835static int dma_handle_rx(struct eg20t_port *priv)
836{
837 struct uart_port *port = &priv->port;
838 struct dma_async_tx_descriptor *desc;
839 struct scatterlist *sg;
840
841 priv = container_of(port, struct eg20t_port, port);
842 sg = &priv->sg_rx;
843
844 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
845
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900846 sg_dma_len(sg) = priv->trigger_level;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900847
848 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
Tomoya MORINAGA1c518992010-12-16 16:13:29 +0900849 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
850 ~PAGE_MASK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900851
852 sg_dma_address(sg) = priv->rx_buf_dma;
853
Alexandre Bounine16052822012-03-08 16:11:18 -0500854 desc = dmaengine_prep_slave_sg(priv->chan_rx,
Vinod Koula485df42011-10-14 10:47:38 +0530855 sg, 1, DMA_DEV_TO_MEM,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900856 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
857
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900858 if (!desc)
859 return 0;
860
861 priv->desc_rx = desc;
862 desc->callback = pch_dma_rx_complete;
863 desc->callback_param = priv;
864 desc->tx_submit(desc);
865 dma_async_issue_pending(priv->chan_rx);
866
867 return PCH_UART_HANDLED_RX_INT;
868}
869
870static unsigned int handle_tx(struct eg20t_port *priv)
871{
872 struct uart_port *port = &priv->port;
873 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900874 int fifo_size;
875 int tx_size;
876 int size;
877 int tx_empty;
878
879 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900880 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
881 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900882 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
883 priv->tx_empty = 1;
884 return 0;
885 }
886
887 fifo_size = max(priv->fifo_size, 1);
888 tx_empty = 1;
889 if (pop_tx_x(priv, xmit->buf)) {
890 pch_uart_hal_write(priv, xmit->buf, 1);
891 port->icount.tx++;
892 tx_empty = 0;
893 fifo_size--;
894 }
895 size = min(xmit->head - xmit->tail, fifo_size);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900896 if (size < 0)
897 size = fifo_size;
898
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900899 tx_size = pop_tx(priv, size);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900900 if (tx_size > 0) {
Tomoya MORINAGA18220762011-02-23 10:03:14 +0900901 port->icount.tx += tx_size;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900902 tx_empty = 0;
903 }
904
905 priv->tx_empty = tx_empty;
906
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900907 if (tx_empty) {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900908 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900909 uart_write_wakeup(port);
910 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900911
912 return PCH_UART_HANDLED_TX_INT;
913}
914
915static unsigned int dma_handle_tx(struct eg20t_port *priv)
916{
917 struct uart_port *port = &priv->port;
918 struct circ_buf *xmit = &port->state->xmit;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900919 struct scatterlist *sg;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900920 int nent;
921 int fifo_size;
922 int tx_empty;
923 struct dma_async_tx_descriptor *desc;
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900924 int num;
925 int i;
926 int bytes;
927 int size;
928 int rem;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900929
930 if (!priv->start_tx) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900931 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
932 __func__, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900933 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
934 priv->tx_empty = 1;
935 return 0;
936 }
937
Tomoya MORINAGA60d10312011-02-23 10:03:18 +0900938 if (priv->tx_dma_use) {
939 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
940 __func__, jiffies);
941 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
942 priv->tx_empty = 1;
943 return 0;
944 }
945
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900946 fifo_size = max(priv->fifo_size, 1);
947 tx_empty = 1;
948 if (pop_tx_x(priv, xmit->buf)) {
949 pch_uart_hal_write(priv, xmit->buf, 1);
950 port->icount.tx++;
951 tx_empty = 0;
952 fifo_size--;
953 }
954
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900955 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
956 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
957 xmit->tail, UART_XMIT_SIZE));
958 if (!bytes) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900959 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900960 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
961 uart_write_wakeup(port);
962 return 0;
963 }
964
965 if (bytes > fifo_size) {
966 num = bytes / fifo_size + 1;
967 size = fifo_size;
968 rem = bytes % fifo_size;
969 } else {
970 num = 1;
971 size = bytes;
972 rem = bytes;
973 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900974
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900975 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
976 __func__, num, size, rem);
977
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900978 priv->tx_dma_use = 1;
979
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900980 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900981
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900982 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
983 sg = priv->sg_tx_p;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900984
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +0900985 for (i = 0; i < num; i++, sg++) {
986 if (i == (num - 1))
987 sg_set_page(sg, virt_to_page(xmit->buf),
988 rem, fifo_size * i);
989 else
990 sg_set_page(sg, virt_to_page(xmit->buf),
991 size, fifo_size * i);
992 }
993
994 sg = priv->sg_tx_p;
995 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900996 if (!nent) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +0900997 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +0900998 return 0;
999 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001000 priv->nent = nent;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001001
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001002 for (i = 0; i < nent; i++, sg++) {
1003 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
1004 fifo_size * i;
1005 sg_dma_address(sg) = (sg_dma_address(sg) &
1006 ~(UART_XMIT_SIZE - 1)) + sg->offset;
1007 if (i == (nent - 1))
1008 sg_dma_len(sg) = rem;
1009 else
1010 sg_dma_len(sg) = size;
1011 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001012
Alexandre Bounine16052822012-03-08 16:11:18 -05001013 desc = dmaengine_prep_slave_sg(priv->chan_tx,
Vinod Koula485df42011-10-14 10:47:38 +05301014 priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001015 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001016 if (!desc) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001017 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
1018 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001019 return 0;
1020 }
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001021 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001022 priv->desc_tx = desc;
1023 desc->callback = pch_dma_tx_complete;
1024 desc->callback_param = priv;
1025
1026 desc->tx_submit(desc);
1027
1028 dma_async_issue_pending(priv->chan_tx);
1029
1030 return PCH_UART_HANDLED_TX_INT;
1031}
1032
1033static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
1034{
1035 u8 fcr = ioread8(priv->membase + UART_FCR);
1036
1037 /* Reset FIFO */
1038 fcr |= UART_FCR_CLEAR_RCVR;
1039 iowrite8(fcr, priv->membase + UART_FCR);
1040
1041 if (lsr & PCH_UART_LSR_ERR)
1042 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
1043
1044 if (lsr & UART_LSR_FE)
1045 dev_err(&priv->pdev->dev, "Framing Error\n");
1046
1047 if (lsr & UART_LSR_PE)
1048 dev_err(&priv->pdev->dev, "Parity Error\n");
1049
1050 if (lsr & UART_LSR_OE)
1051 dev_err(&priv->pdev->dev, "Overrun Error\n");
1052}
1053
1054static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
1055{
1056 struct eg20t_port *priv = dev_id;
1057 unsigned int handled;
1058 u8 lsr;
1059 int ret = 0;
1060 unsigned int iid;
1061 unsigned long flags;
1062
1063 spin_lock_irqsave(&priv->port.lock, flags);
1064 handled = 0;
1065 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
1066 switch (iid) {
1067 case PCH_UART_IID_RLS: /* Receiver Line Status */
1068 lsr = pch_uart_hal_get_line_status(priv);
1069 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
1070 UART_LSR_PE | UART_LSR_OE)) {
1071 pch_uart_err_ir(priv, lsr);
1072 ret = PCH_UART_HANDLED_RX_ERR_INT;
1073 }
1074 break;
1075 case PCH_UART_IID_RDR: /* Received Data Ready */
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001076 if (priv->use_dma) {
1077 pch_uart_hal_disable_interrupt(priv,
1078 PCH_UART_HAL_RX_INT);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001079 ret = dma_handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001080 if (!ret)
1081 pch_uart_hal_enable_interrupt(priv,
1082 PCH_UART_HAL_RX_INT);
1083 } else {
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001084 ret = handle_rx(priv);
Tomoya MORINAGAda3564e2011-02-23 10:03:12 +09001085 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001086 break;
1087 case PCH_UART_IID_RDR_TO: /* Received Data Ready
1088 (FIFO Timeout) */
1089 ret = handle_rx_to(priv);
1090 break;
1091 case PCH_UART_IID_THRE: /* Transmitter Holding Register
1092 Empty */
1093 if (priv->use_dma)
1094 ret = dma_handle_tx(priv);
1095 else
1096 ret = handle_tx(priv);
1097 break;
1098 case PCH_UART_IID_MS: /* Modem Status */
1099 ret = PCH_UART_HANDLED_MS_INT;
1100 break;
1101 default: /* Never junp to this label */
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001102 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
1103 iid, jiffies);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001104 ret = -1;
1105 break;
1106 }
1107 handled |= (unsigned int)ret;
1108 }
1109 if (handled == 0 && iid <= 1) {
1110 if (priv->int_dis_flag)
1111 priv->int_dis_flag = 0;
1112 }
1113
1114 spin_unlock_irqrestore(&priv->port.lock, flags);
1115 return IRQ_RETVAL(handled);
1116}
1117
1118/* This function tests whether the transmitter fifo and shifter for the port
1119 described by 'port' is empty. */
1120static unsigned int pch_uart_tx_empty(struct uart_port *port)
1121{
1122 struct eg20t_port *priv;
Feng Tang30c6c6b2012-02-06 17:24:44 +08001123
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001124 priv = container_of(port, struct eg20t_port, port);
1125 if (priv->tx_empty)
Feng Tang30c6c6b2012-02-06 17:24:44 +08001126 return TIOCSER_TEMT;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001127 else
Feng Tang30c6c6b2012-02-06 17:24:44 +08001128 return 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001129}
1130
1131/* Returns the current state of modem control inputs. */
1132static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1133{
1134 struct eg20t_port *priv;
1135 u8 modem;
1136 unsigned int ret = 0;
1137
1138 priv = container_of(port, struct eg20t_port, port);
1139 modem = pch_uart_hal_get_modem(priv);
1140
1141 if (modem & UART_MSR_DCD)
1142 ret |= TIOCM_CAR;
1143
1144 if (modem & UART_MSR_RI)
1145 ret |= TIOCM_RNG;
1146
1147 if (modem & UART_MSR_DSR)
1148 ret |= TIOCM_DSR;
1149
1150 if (modem & UART_MSR_CTS)
1151 ret |= TIOCM_CTS;
1152
1153 return ret;
1154}
1155
1156static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1157{
1158 u32 mcr = 0;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001159 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1160
1161 if (mctrl & TIOCM_DTR)
1162 mcr |= UART_MCR_DTR;
1163 if (mctrl & TIOCM_RTS)
1164 mcr |= UART_MCR_RTS;
1165 if (mctrl & TIOCM_LOOP)
1166 mcr |= UART_MCR_LOOP;
1167
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001168 if (priv->mcr & UART_MCR_AFE)
1169 mcr |= UART_MCR_AFE;
1170
1171 if (mctrl)
1172 iowrite8(mcr, priv->membase + UART_MCR);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001173}
1174
1175static void pch_uart_stop_tx(struct uart_port *port)
1176{
1177 struct eg20t_port *priv;
1178 priv = container_of(port, struct eg20t_port, port);
1179 priv->start_tx = 0;
1180 priv->tx_dma_use = 0;
1181}
1182
1183static void pch_uart_start_tx(struct uart_port *port)
1184{
1185 struct eg20t_port *priv;
1186
1187 priv = container_of(port, struct eg20t_port, port);
1188
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001189 if (priv->use_dma) {
1190 if (priv->tx_dma_use) {
1191 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1192 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001193 return;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001194 }
1195 }
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001196
1197 priv->start_tx = 1;
1198 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1199}
1200
1201static void pch_uart_stop_rx(struct uart_port *port)
1202{
1203 struct eg20t_port *priv;
1204 priv = container_of(port, struct eg20t_port, port);
1205 priv->start_rx = 0;
1206 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1207 priv->int_dis_flag = 1;
1208}
1209
1210/* Enable the modem status interrupts. */
1211static void pch_uart_enable_ms(struct uart_port *port)
1212{
1213 struct eg20t_port *priv;
1214 priv = container_of(port, struct eg20t_port, port);
1215 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1216}
1217
1218/* Control the transmission of a break signal. */
1219static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1220{
1221 struct eg20t_port *priv;
1222 unsigned long flags;
1223
1224 priv = container_of(port, struct eg20t_port, port);
1225 spin_lock_irqsave(&port->lock, flags);
1226 pch_uart_hal_set_break(priv, ctl);
1227 spin_unlock_irqrestore(&port->lock, flags);
1228}
1229
1230/* Grab any interrupt resources and initialise any low level driver state. */
1231static int pch_uart_startup(struct uart_port *port)
1232{
1233 struct eg20t_port *priv;
1234 int ret;
1235 int fifo_size;
1236 int trigger_level;
1237
1238 priv = container_of(port, struct eg20t_port, port);
1239 priv->tx_empty = 1;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001240
1241 if (port->uartclk)
Darren Harta8a3ec92012-03-09 09:51:48 -08001242 priv->uartclk = port->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001243 else
Darren Harta8a3ec92012-03-09 09:51:48 -08001244 port->uartclk = priv->uartclk;
Tomoya MORINAGAaac6c0b2011-02-23 10:03:16 +09001245
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001246 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1247 ret = pch_uart_hal_set_line(priv, default_baud,
1248 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1249 PCH_UART_HAL_STB1);
1250 if (ret)
1251 return ret;
1252
1253 switch (priv->fifo_size) {
1254 case 256:
1255 fifo_size = PCH_UART_HAL_FIFO256;
1256 break;
1257 case 64:
1258 fifo_size = PCH_UART_HAL_FIFO64;
1259 break;
1260 case 16:
1261 fifo_size = PCH_UART_HAL_FIFO16;
1262 case 1:
1263 default:
1264 fifo_size = PCH_UART_HAL_FIFO_DIS;
1265 break;
1266 }
1267
1268 switch (priv->trigger) {
1269 case PCH_UART_HAL_TRIGGER1:
1270 trigger_level = 1;
1271 break;
1272 case PCH_UART_HAL_TRIGGER_L:
1273 trigger_level = priv->fifo_size / 4;
1274 break;
1275 case PCH_UART_HAL_TRIGGER_M:
1276 trigger_level = priv->fifo_size / 2;
1277 break;
1278 case PCH_UART_HAL_TRIGGER_H:
1279 default:
1280 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1281 break;
1282 }
1283
1284 priv->trigger_level = trigger_level;
1285 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1286 fifo_size, priv->trigger);
1287 if (ret < 0)
1288 return ret;
1289
1290 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1291 KBUILD_MODNAME, priv);
1292 if (ret < 0)
1293 return ret;
1294
1295 if (priv->use_dma)
1296 pch_request_dma(port);
1297
1298 priv->start_rx = 1;
1299 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1300 uart_update_timeout(port, CS8, default_baud);
1301
1302 return 0;
1303}
1304
1305static void pch_uart_shutdown(struct uart_port *port)
1306{
1307 struct eg20t_port *priv;
1308 int ret;
1309
1310 priv = container_of(port, struct eg20t_port, port);
1311 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1312 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1313 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1314 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1315 if (ret)
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001316 dev_err(priv->port.dev,
1317 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001318
Tomoya MORINAGA90f04c22011-11-11 10:55:27 +09001319 pch_free_dma(port);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001320
1321 free_irq(priv->port.irq, priv);
1322}
1323
1324/* Change the port parameters, including word length, parity, stop
1325 *bits. Update read_status_mask and ignore_status_mask to indicate
1326 *the types of events we are interested in receiving. */
1327static void pch_uart_set_termios(struct uart_port *port,
1328 struct ktermios *termios, struct ktermios *old)
1329{
1330 int baud;
1331 int rtn;
1332 unsigned int parity, bits, stb;
1333 struct eg20t_port *priv;
1334 unsigned long flags;
1335
1336 priv = container_of(port, struct eg20t_port, port);
1337 switch (termios->c_cflag & CSIZE) {
1338 case CS5:
1339 bits = PCH_UART_HAL_5BIT;
1340 break;
1341 case CS6:
1342 bits = PCH_UART_HAL_6BIT;
1343 break;
1344 case CS7:
1345 bits = PCH_UART_HAL_7BIT;
1346 break;
1347 default: /* CS8 */
1348 bits = PCH_UART_HAL_8BIT;
1349 break;
1350 }
1351 if (termios->c_cflag & CSTOPB)
1352 stb = PCH_UART_HAL_STB2;
1353 else
1354 stb = PCH_UART_HAL_STB1;
1355
1356 if (termios->c_cflag & PARENB) {
1357 if (!(termios->c_cflag & PARODD))
1358 parity = PCH_UART_HAL_PARITY_ODD;
1359 else
1360 parity = PCH_UART_HAL_PARITY_EVEN;
1361
Feng Tang30c6c6b2012-02-06 17:24:44 +08001362 } else
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001363 parity = PCH_UART_HAL_PARITY_NONE;
Tomoya MORINAGA9af71552011-02-23 10:03:17 +09001364
1365 /* Only UART0 has auto hardware flow function */
1366 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1367 priv->mcr |= UART_MCR_AFE;
1368 else
1369 priv->mcr &= ~UART_MCR_AFE;
1370
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001371 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1372
1373 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1374
1375 spin_lock_irqsave(&port->lock, flags);
1376
1377 uart_update_timeout(port, termios->c_cflag, baud);
1378 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1379 if (rtn)
1380 goto out;
1381
Tomoya MORINAGAa1d7cfe2011-10-27 15:45:18 +09001382 pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001383 /* Don't rewrite B0 */
1384 if (tty_termios_baud_rate(termios))
1385 tty_termios_encode_baud_rate(termios, baud, baud);
1386
1387out:
1388 spin_unlock_irqrestore(&port->lock, flags);
1389}
1390
1391static const char *pch_uart_type(struct uart_port *port)
1392{
1393 return KBUILD_MODNAME;
1394}
1395
1396static void pch_uart_release_port(struct uart_port *port)
1397{
1398 struct eg20t_port *priv;
1399
1400 priv = container_of(port, struct eg20t_port, port);
1401 pci_iounmap(priv->pdev, priv->membase);
1402 pci_release_regions(priv->pdev);
1403}
1404
1405static int pch_uart_request_port(struct uart_port *port)
1406{
1407 struct eg20t_port *priv;
1408 int ret;
1409 void __iomem *membase;
1410
1411 priv = container_of(port, struct eg20t_port, port);
1412 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1413 if (ret < 0)
1414 return -EBUSY;
1415
1416 membase = pci_iomap(priv->pdev, 1, 0);
1417 if (!membase) {
1418 pci_release_regions(priv->pdev);
1419 return -EBUSY;
1420 }
1421 priv->membase = port->membase = membase;
1422
1423 return 0;
1424}
1425
1426static void pch_uart_config_port(struct uart_port *port, int type)
1427{
1428 struct eg20t_port *priv;
1429
1430 priv = container_of(port, struct eg20t_port, port);
1431 if (type & UART_CONFIG_TYPE) {
1432 port->type = priv->port_type;
1433 pch_uart_request_port(port);
1434 }
1435}
1436
1437static int pch_uart_verify_port(struct uart_port *port,
1438 struct serial_struct *serinfo)
1439{
1440 struct eg20t_port *priv;
1441
1442 priv = container_of(port, struct eg20t_port, port);
1443 if (serinfo->flags & UPF_LOW_LATENCY) {
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001444 dev_info(priv->port.dev,
1445 "PCH UART : Use PIO Mode (without DMA)\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001446 priv->use_dma = 0;
1447 serinfo->flags &= ~UPF_LOW_LATENCY;
1448 } else {
1449#ifndef CONFIG_PCH_DMA
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001450 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1451 __func__);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001452 return -EOPNOTSUPP;
1453#endif
1454 priv->use_dma = 1;
1455 priv->use_dma_flag = 1;
Tomoya MORINAGA23877fd2011-02-23 10:03:15 +09001456 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001457 }
1458
1459 return 0;
1460}
1461
1462static struct uart_ops pch_uart_ops = {
1463 .tx_empty = pch_uart_tx_empty,
1464 .set_mctrl = pch_uart_set_mctrl,
1465 .get_mctrl = pch_uart_get_mctrl,
1466 .stop_tx = pch_uart_stop_tx,
1467 .start_tx = pch_uart_start_tx,
1468 .stop_rx = pch_uart_stop_rx,
1469 .enable_ms = pch_uart_enable_ms,
1470 .break_ctl = pch_uart_break_ctl,
1471 .startup = pch_uart_startup,
1472 .shutdown = pch_uart_shutdown,
1473 .set_termios = pch_uart_set_termios,
1474/* .pm = pch_uart_pm, Not supported yet */
1475/* .set_wake = pch_uart_set_wake, Not supported yet */
1476 .type = pch_uart_type,
1477 .release_port = pch_uart_release_port,
1478 .request_port = pch_uart_request_port,
1479 .config_port = pch_uart_config_port,
1480 .verify_port = pch_uart_verify_port
1481};
1482
Alexander Steine30f8672011-11-15 15:04:07 -08001483#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1484
1485/*
1486 * Wait for transmitter & holding register to empty
1487 */
1488static void wait_for_xmitr(struct eg20t_port *up, int bits)
1489{
1490 unsigned int status, tmout = 10000;
1491
1492 /* Wait up to 10ms for the character(s) to be sent. */
1493 for (;;) {
1494 status = ioread8(up->membase + UART_LSR);
1495
1496 if ((status & bits) == bits)
1497 break;
1498 if (--tmout == 0)
1499 break;
1500 udelay(1);
1501 }
1502
1503 /* Wait up to 1s for flow control if necessary */
1504 if (up->port.flags & UPF_CONS_FLOW) {
1505 unsigned int tmout;
1506 for (tmout = 1000000; tmout; tmout--) {
1507 unsigned int msr = ioread8(up->membase + UART_MSR);
1508 if (msr & UART_MSR_CTS)
1509 break;
1510 udelay(1);
1511 touch_nmi_watchdog();
1512 }
1513 }
1514}
1515
1516static void pch_console_putchar(struct uart_port *port, int ch)
1517{
1518 struct eg20t_port *priv =
1519 container_of(port, struct eg20t_port, port);
1520
1521 wait_for_xmitr(priv, UART_LSR_THRE);
1522 iowrite8(ch, priv->membase + PCH_UART_THR);
1523}
1524
1525/*
1526 * Print a string to the serial port trying not to disturb
1527 * any possible real use of the port...
1528 *
1529 * The console_lock must be held when we get here.
1530 */
1531static void
1532pch_console_write(struct console *co, const char *s, unsigned int count)
1533{
1534 struct eg20t_port *priv;
Alexander Steine30f8672011-11-15 15:04:07 -08001535 unsigned long flags;
1536 u8 ier;
1537 int locked = 1;
1538
1539 priv = pch_uart_ports[co->index];
1540
1541 touch_nmi_watchdog();
1542
1543 local_irq_save(flags);
1544 if (priv->port.sysrq) {
1545 /* serial8250_handle_port() already took the lock */
1546 locked = 0;
1547 } else if (oops_in_progress) {
1548 locked = spin_trylock(&priv->port.lock);
1549 } else
1550 spin_lock(&priv->port.lock);
1551
1552 /*
1553 * First save the IER then disable the interrupts
1554 */
1555 ier = ioread8(priv->membase + UART_IER);
1556
1557 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1558
1559 uart_console_write(&priv->port, s, count, pch_console_putchar);
1560
1561 /*
1562 * Finally, wait for transmitter to become empty
1563 * and restore the IER
1564 */
1565 wait_for_xmitr(priv, BOTH_EMPTY);
1566 iowrite8(ier, priv->membase + UART_IER);
1567
1568 if (locked)
1569 spin_unlock(&priv->port.lock);
1570 local_irq_restore(flags);
1571}
1572
1573static int __init pch_console_setup(struct console *co, char *options)
1574{
1575 struct uart_port *port;
Darren Hart7ce92512012-03-09 09:51:51 -08001576 int baud = default_baud;
Alexander Steine30f8672011-11-15 15:04:07 -08001577 int bits = 8;
1578 int parity = 'n';
1579 int flow = 'n';
1580
1581 /*
1582 * Check whether an invalid uart number has been specified, and
1583 * if so, search for the first available port that does have
1584 * console support.
1585 */
1586 if (co->index >= PCH_UART_NR)
1587 co->index = 0;
1588 port = &pch_uart_ports[co->index]->port;
1589
1590 if (!port || (!port->iobase && !port->membase))
1591 return -ENODEV;
1592
Darren Hart077175f2012-03-09 09:51:49 -08001593 port->uartclk = pch_uart_get_uartclk();
Alexander Steine30f8672011-11-15 15:04:07 -08001594
1595 if (options)
1596 uart_parse_options(options, &baud, &parity, &bits, &flow);
1597
1598 return uart_set_options(port, co, baud, parity, bits, flow);
1599}
1600
1601static struct uart_driver pch_uart_driver;
1602
1603static struct console pch_console = {
1604 .name = PCH_UART_DRIVER_DEVICE,
1605 .write = pch_console_write,
1606 .device = uart_console_device,
1607 .setup = pch_console_setup,
1608 .flags = CON_PRINTBUFFER | CON_ANYTIME,
1609 .index = -1,
1610 .data = &pch_uart_driver,
1611};
1612
1613#define PCH_CONSOLE (&pch_console)
1614#else
1615#define PCH_CONSOLE NULL
1616#endif
1617
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001618static struct uart_driver pch_uart_driver = {
1619 .owner = THIS_MODULE,
1620 .driver_name = KBUILD_MODNAME,
1621 .dev_name = PCH_UART_DRIVER_DEVICE,
1622 .major = 0,
1623 .minor = 0,
1624 .nr = PCH_UART_NR,
Alexander Steine30f8672011-11-15 15:04:07 -08001625 .cons = PCH_CONSOLE,
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001626};
1627
1628static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001629 const struct pci_device_id *id)
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001630{
1631 struct eg20t_port *priv;
1632 int ret;
1633 unsigned int iobase;
1634 unsigned int mapbase;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001635 unsigned char *rxbuf;
Darren Hart077175f2012-03-09 09:51:49 -08001636 int fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001637 int port_type;
1638 struct pch_uart_driver_data *board;
Feng Tangd0114112012-02-06 17:24:43 +08001639 char name[32]; /* for debugfs file name */
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001640
1641 board = &drv_dat[id->driver_data];
1642 port_type = board->port_type;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001643
1644 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1645 if (priv == NULL)
1646 goto init_port_alloc_err;
1647
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001648 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001649 if (!rxbuf)
1650 goto init_port_free_txbuf;
1651
1652 switch (port_type) {
1653 case PORT_UNKNOWN:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001654 fifosize = 256; /* EG20T/ML7213: UART0 */
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001655 break;
1656 case PORT_8250:
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001657 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001658 break;
1659 default:
1660 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1661 goto init_port_hal_free;
1662 }
1663
Alexander Steine4635952011-07-04 08:58:31 +02001664 pci_enable_msi(pdev);
Tomoya MORINAGA867c9022012-04-02 14:36:22 +09001665 pci_set_master(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001666
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001667 iobase = pci_resource_start(pdev, 0);
1668 mapbase = pci_resource_start(pdev, 1);
1669 priv->mapbase = mapbase;
1670 priv->iobase = iobase;
1671 priv->pdev = pdev;
1672 priv->tx_empty = 1;
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001673 priv->rxbuf.buf = rxbuf;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001674 priv->rxbuf.size = PAGE_SIZE;
1675
1676 priv->fifo_size = fifosize;
Darren Hart077175f2012-03-09 09:51:49 -08001677 priv->uartclk = pch_uart_get_uartclk();
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001678 priv->port_type = PORT_MAX_8250 + port_type + 1;
1679 priv->port.dev = &pdev->dev;
1680 priv->port.iobase = iobase;
1681 priv->port.membase = NULL;
1682 priv->port.mapbase = mapbase;
1683 priv->port.irq = pdev->irq;
1684 priv->port.iotype = UPIO_PORT;
1685 priv->port.ops = &pch_uart_ops;
1686 priv->port.flags = UPF_BOOT_AUTOCONF;
1687 priv->port.fifosize = fifosize;
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001688 priv->port.line = board->line_no;
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001689 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1690
Tomoya MORINAGA7e461322011-02-23 10:03:13 +09001691 spin_lock_init(&priv->port.lock);
1692
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001693 pci_set_drvdata(pdev, priv);
Feng Tang6f56d0f2012-02-06 17:24:45 +08001694 priv->trigger_level = 1;
1695 priv->fcr = 0;
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001696
Alexander Steine30f8672011-11-15 15:04:07 -08001697#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1698 pch_uart_ports[board->line_no] = priv;
1699#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001700 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1701 if (ret < 0)
1702 goto init_port_hal_free;
1703
Feng Tangd0114112012-02-06 17:24:43 +08001704#ifdef CONFIG_DEBUG_FS
1705 snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
1706 priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
1707 NULL, priv, &port_regs_ops);
1708#endif
1709
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001710 return priv;
1711
1712init_port_hal_free:
Alexander Steine30f8672011-11-15 15:04:07 -08001713#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1714 pch_uart_ports[board->line_no] = NULL;
1715#endif
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001716 free_page((unsigned long)rxbuf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001717init_port_free_txbuf:
1718 kfree(priv);
1719init_port_alloc_err:
1720
1721 return NULL;
1722}
1723
1724static void pch_uart_exit_port(struct eg20t_port *priv)
1725{
Feng Tangd0114112012-02-06 17:24:43 +08001726
1727#ifdef CONFIG_DEBUG_FS
1728 if (priv->debugfs)
1729 debugfs_remove(priv->debugfs);
1730#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001731 uart_remove_one_port(&pch_uart_driver, &priv->port);
1732 pci_set_drvdata(priv->pdev, NULL);
Tomoya MORINAGA1c518992010-12-16 16:13:29 +09001733 free_page((unsigned long)priv->rxbuf.buf);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001734}
1735
1736static void pch_uart_pci_remove(struct pci_dev *pdev)
1737{
Feng Tang6f56d0f2012-02-06 17:24:45 +08001738 struct eg20t_port *priv = pci_get_drvdata(pdev);
Alexander Steine4635952011-07-04 08:58:31 +02001739
1740 pci_disable_msi(pdev);
Alexander Steine30f8672011-11-15 15:04:07 -08001741
1742#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
1743 pch_uart_ports[priv->port.line] = NULL;
1744#endif
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001745 pch_uart_exit_port(priv);
1746 pci_disable_device(pdev);
1747 kfree(priv);
1748 return;
1749}
1750#ifdef CONFIG_PM
1751static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1752{
1753 struct eg20t_port *priv = pci_get_drvdata(pdev);
1754
1755 uart_suspend_port(&pch_uart_driver, &priv->port);
1756
1757 pci_save_state(pdev);
1758 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1759 return 0;
1760}
1761
1762static int pch_uart_pci_resume(struct pci_dev *pdev)
1763{
1764 struct eg20t_port *priv = pci_get_drvdata(pdev);
1765 int ret;
1766
1767 pci_set_power_state(pdev, PCI_D0);
1768 pci_restore_state(pdev);
1769
1770 ret = pci_enable_device(pdev);
1771 if (ret) {
1772 dev_err(&pdev->dev,
1773 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1774 return ret;
1775 }
1776
1777 uart_resume_port(&pch_uart_driver, &priv->port);
1778
1779 return 0;
1780}
1781#else
1782#define pch_uart_pci_suspend NULL
1783#define pch_uart_pci_resume NULL
1784#endif
1785
1786static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1787 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001788 .driver_data = pch_et20t_uart0},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001789 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001790 .driver_data = pch_et20t_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001791 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001792 .driver_data = pch_et20t_uart2},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001793 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001794 .driver_data = pch_et20t_uart3},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001795 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001796 .driver_data = pch_ml7213_uart0},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001797 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001798 .driver_data = pch_ml7213_uart1},
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001799 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
Tomoya MORINAGAfec38d12011-02-23 10:03:19 +09001800 .driver_data = pch_ml7213_uart2},
Tomoya MORINAGA177c2cb2011-05-09 17:25:20 +09001801 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1802 .driver_data = pch_ml7223_uart0},
1803 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1804 .driver_data = pch_ml7223_uart1},
Tomoya MORINAGA8249f742011-10-28 09:38:49 +09001805 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
1806 .driver_data = pch_ml7831_uart0},
1807 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
1808 .driver_data = pch_ml7831_uart1},
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001809 {0,},
1810};
1811
1812static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1813 const struct pci_device_id *id)
1814{
1815 int ret;
1816 struct eg20t_port *priv;
1817
1818 ret = pci_enable_device(pdev);
1819 if (ret < 0)
1820 goto probe_error;
1821
Tomoya MORINAGA4564e1e2011-01-28 18:00:01 +09001822 priv = pch_uart_init_port(pdev, id);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001823 if (!priv) {
1824 ret = -EBUSY;
1825 goto probe_disable_device;
1826 }
1827 pci_set_drvdata(pdev, priv);
1828
1829 return ret;
1830
1831probe_disable_device:
Alexander Steine4635952011-07-04 08:58:31 +02001832 pci_disable_msi(pdev);
Tomoya MORINAGA3c6a4832010-11-17 09:55:54 +09001833 pci_disable_device(pdev);
1834probe_error:
1835 return ret;
1836}
1837
1838static struct pci_driver pch_uart_pci_driver = {
1839 .name = "pch_uart",
1840 .id_table = pch_uart_pci_id,
1841 .probe = pch_uart_pci_probe,
1842 .remove = __devexit_p(pch_uart_pci_remove),
1843 .suspend = pch_uart_pci_suspend,
1844 .resume = pch_uart_pci_resume,
1845};
1846
1847static int __init pch_uart_module_init(void)
1848{
1849 int ret;
1850
1851 /* register as UART driver */
1852 ret = uart_register_driver(&pch_uart_driver);
1853 if (ret < 0)
1854 return ret;
1855
1856 /* register as PCI driver */
1857 ret = pci_register_driver(&pch_uart_pci_driver);
1858 if (ret < 0)
1859 uart_unregister_driver(&pch_uart_driver);
1860
1861 return ret;
1862}
1863module_init(pch_uart_module_init);
1864
1865static void __exit pch_uart_module_exit(void)
1866{
1867 pci_unregister_driver(&pch_uart_pci_driver);
1868 uart_unregister_driver(&pch_uart_driver);
1869}
1870module_exit(pch_uart_module_exit);
1871
1872MODULE_LICENSE("GPL v2");
1873MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1874module_param(default_baud, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001875MODULE_PARM_DESC(default_baud,
1876 "Default BAUD for initial driver state and console (default 9600)");
Darren Hart2a44feb2012-03-09 09:51:50 -08001877module_param(user_uartclk, uint, S_IRUGO);
Darren Harta46f5532012-03-09 09:51:52 -08001878MODULE_PARM_DESC(user_uartclk,
1879 "Override UART default or board specific UART clock");