blob: ad35130cd8a096f27281111c9fb134a9425deac5 [file] [log] [blame]
Feng Tangd843fc62010-07-27 08:20:22 +01001/*
2 * mfd.c: driver for High Speed UART device of Intel Medfield platform
3 *
4 * Refer pxa.c, 8250.c and some other drivers in drivers/serial/
5 *
Feng Tang06c77e22010-07-27 08:20:42 +01006 * (C) Copyright 2010 Intel Corporation
Feng Tangd843fc62010-07-27 08:20:22 +01007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
11 * of the License.
12 */
13
Feng Tangd843fc62010-07-27 08:20:22 +010014/* Notes:
Feng Tang06c77e22010-07-27 08:20:42 +010015 * 1. DMA channel allocation: 0/1 channel are assigned to port 0,
16 * 2/3 chan to port 1, 4/5 chan to port 3. Even number chans
17 * are used for RX, odd chans for TX
Feng Tangd843fc62010-07-27 08:20:22 +010018 *
Feng Tang06c77e22010-07-27 08:20:42 +010019 * 2. In A0 stepping, UART will not support TX half empty flag
Feng Tangd843fc62010-07-27 08:20:22 +010020 *
Feng Tang06c77e22010-07-27 08:20:42 +010021 * 3. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always
22 * asserted, only when the HW is reset the DDCD and DDSR will
23 * be triggered
Feng Tangd843fc62010-07-27 08:20:22 +010024 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/console.h>
29#include <linux/sysrq.h>
Andrew Morton63d66ca2010-09-30 15:15:28 -070030#include <linux/slab.h>
Feng Tangd843fc62010-07-27 08:20:22 +010031#include <linux/serial_reg.h>
32#include <linux/circ_buf.h>
33#include <linux/delay.h>
34#include <linux/interrupt.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial_mfd.h>
39#include <linux/dma-mapping.h>
40#include <linux/pci.h>
41#include <linux/io.h>
42#include <linux/debugfs.h>
43
44#define MFD_HSU_A0_STEPPING 1
45
46#define HSU_DMA_BUF_SIZE 2048
47
48#define chan_readl(chan, offset) readl(chan->reg + offset)
49#define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
50
51#define mfd_readl(obj, offset) readl(obj->reg + offset)
52#define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
53
Feng Tang669b7a02010-07-27 08:20:32 +010054#define HSU_DMA_TIMEOUT_CHECK_FREQ (HZ/10)
55
Feng Tangd843fc62010-07-27 08:20:22 +010056struct hsu_dma_buffer {
57 u8 *buf;
58 dma_addr_t dma_addr;
59 u32 dma_size;
60 u32 ofs;
61};
62
63struct hsu_dma_chan {
64 u32 id;
Feng Tang06c77e22010-07-27 08:20:42 +010065 enum dma_data_direction dirt;
Feng Tangd843fc62010-07-27 08:20:22 +010066 struct uart_hsu_port *uport;
Feng Tang669b7a02010-07-27 08:20:32 +010067 void __iomem *reg;
68 struct timer_list rx_timer; /* only needed by RX channel */
Feng Tangd843fc62010-07-27 08:20:22 +010069};
70
71struct uart_hsu_port {
72 struct uart_port port;
73 unsigned char ier;
74 unsigned char lcr;
75 unsigned char mcr;
76 unsigned int lsr_break_flag;
77 char name[12];
78 int index;
79 struct device *dev;
80
81 struct hsu_dma_chan *txc;
82 struct hsu_dma_chan *rxc;
83 struct hsu_dma_buffer txbuf;
84 struct hsu_dma_buffer rxbuf;
85 int use_dma; /* flag for DMA/PIO */
86 int running;
87 int dma_tx_on;
88};
89
90/* Top level data structure of HSU */
91struct hsu_port {
Feng Tangd843fc62010-07-27 08:20:22 +010092 void __iomem *reg;
93 unsigned long paddr;
94 unsigned long iolen;
95 u32 irq;
96
97 struct uart_hsu_port port[3];
98 struct hsu_dma_chan chans[10];
99
Feng Tangd843fc62010-07-27 08:20:22 +0100100 struct dentry *debugfs;
Feng Tangd843fc62010-07-27 08:20:22 +0100101};
102
Feng Tangd843fc62010-07-27 08:20:22 +0100103static inline unsigned int serial_in(struct uart_hsu_port *up, int offset)
104{
105 unsigned int val;
106
107 if (offset > UART_MSR) {
108 offset <<= 2;
109 val = readl(up->port.membase + offset);
110 } else
111 val = (unsigned int)readb(up->port.membase + offset);
112
113 return val;
114}
115
116static inline void serial_out(struct uart_hsu_port *up, int offset, int value)
117{
118 if (offset > UART_MSR) {
119 offset <<= 2;
120 writel(value, up->port.membase + offset);
121 } else {
122 unsigned char val = value & 0xff;
123 writeb(val, up->port.membase + offset);
124 }
125}
126
127#ifdef CONFIG_DEBUG_FS
128
129#define HSU_REGS_BUFSIZE 1024
130
131static int hsu_show_regs_open(struct inode *inode, struct file *file)
132{
133 file->private_data = inode->i_private;
134 return 0;
135}
136
137static ssize_t port_show_regs(struct file *file, char __user *user_buf,
138 size_t count, loff_t *ppos)
139{
140 struct uart_hsu_port *up = file->private_data;
141 char *buf;
142 u32 len = 0;
143 ssize_t ret;
144
145 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
146 if (!buf)
147 return 0;
148
149 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
150 "MFD HSU port[%d] regs:\n", up->index);
151
152 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
153 "=================================\n");
154 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
155 "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
156 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
157 "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
158 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
159 "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
160 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
161 "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
162 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
163 "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
164 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
165 "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
166 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
167 "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
168 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
169 "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
170 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
171 "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
172 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
173 "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
174
Dan Carpentera9589812010-08-12 09:50:09 +0200175 if (len > HSU_REGS_BUFSIZE)
176 len = HSU_REGS_BUFSIZE;
177
Feng Tangd843fc62010-07-27 08:20:22 +0100178 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
179 kfree(buf);
180 return ret;
181}
182
183static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
184 size_t count, loff_t *ppos)
185{
186 struct hsu_dma_chan *chan = file->private_data;
187 char *buf;
188 u32 len = 0;
189 ssize_t ret;
190
191 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
192 if (!buf)
193 return 0;
194
195 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
196 "MFD HSU DMA channel [%d] regs:\n", chan->id);
197
198 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
199 "=================================\n");
200 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
201 "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
202 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
203 "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
204 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
205 "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
206 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
207 "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
208 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
209 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
210 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
211 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
212 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
213 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
214 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
215 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
216 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
217 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
218 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
219 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
220 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
221 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
222 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
223 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
224
Dan Carpentera9589812010-08-12 09:50:09 +0200225 if (len > HSU_REGS_BUFSIZE)
226 len = HSU_REGS_BUFSIZE;
227
Feng Tangd843fc62010-07-27 08:20:22 +0100228 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
229 kfree(buf);
230 return ret;
231}
232
233static const struct file_operations port_regs_ops = {
234 .owner = THIS_MODULE,
235 .open = hsu_show_regs_open,
236 .read = port_show_regs,
237};
238
239static const struct file_operations dma_regs_ops = {
240 .owner = THIS_MODULE,
241 .open = hsu_show_regs_open,
242 .read = dma_show_regs,
243};
244
245static int hsu_debugfs_init(struct hsu_port *hsu)
246{
247 int i;
248 char name[32];
249
250 hsu->debugfs = debugfs_create_dir("hsu", NULL);
251 if (!hsu->debugfs)
252 return -ENOMEM;
253
254 for (i = 0; i < 3; i++) {
255 snprintf(name, sizeof(name), "port_%d_regs", i);
256 debugfs_create_file(name, S_IFREG | S_IRUGO,
257 hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
258 }
259
260 for (i = 0; i < 6; i++) {
261 snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
262 debugfs_create_file(name, S_IFREG | S_IRUGO,
263 hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
264 }
265
266 return 0;
267}
268
269static void hsu_debugfs_remove(struct hsu_port *hsu)
270{
271 if (hsu->debugfs)
272 debugfs_remove_recursive(hsu->debugfs);
273}
274
275#else
276static inline int hsu_debugfs_init(struct hsu_port *hsu)
277{
278 return 0;
279}
280
281static inline void hsu_debugfs_remove(struct hsu_port *hsu)
282{
283}
284#endif /* CONFIG_DEBUG_FS */
285
286static void serial_hsu_enable_ms(struct uart_port *port)
287{
288 struct uart_hsu_port *up =
289 container_of(port, struct uart_hsu_port, port);
290
291 up->ier |= UART_IER_MSI;
292 serial_out(up, UART_IER, up->ier);
293}
294
295void hsu_dma_tx(struct uart_hsu_port *up)
296{
297 struct circ_buf *xmit = &up->port.state->xmit;
298 struct hsu_dma_buffer *dbuf = &up->txbuf;
299 int count;
300
301 /* test_and_set_bit may be better, but anyway it's in lock protected mode */
302 if (up->dma_tx_on)
303 return;
304
305 /* Update the circ buf info */
306 xmit->tail += dbuf->ofs;
307 xmit->tail &= UART_XMIT_SIZE - 1;
308
309 up->port.icount.tx += dbuf->ofs;
310 dbuf->ofs = 0;
311
312 /* Disable the channel */
313 chan_writel(up->txc, HSU_CH_CR, 0x0);
314
315 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
316 dma_sync_single_for_device(up->port.dev,
317 dbuf->dma_addr,
318 dbuf->dma_size,
319 DMA_TO_DEVICE);
320
321 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
322 dbuf->ofs = count;
323
324 /* Reprogram the channel */
325 chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
326 chan_writel(up->txc, HSU_CH_D0TSR, count);
327
328 /* Reenable the channel */
329 chan_writel(up->txc, HSU_CH_DCR, 0x1
330 | (0x1 << 8)
331 | (0x1 << 16)
332 | (0x1 << 24));
Feng Tangd843fc62010-07-27 08:20:22 +0100333 up->dma_tx_on = 1;
334 chan_writel(up->txc, HSU_CH_CR, 0x1);
335 }
336
337 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
338 uart_write_wakeup(&up->port);
339}
340
341/* The buffer is already cache coherent */
342void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
343{
Feng Tangd843fc62010-07-27 08:20:22 +0100344 dbuf->ofs = 0;
345
346 chan_writel(rxc, HSU_CH_BSR, 32);
347 chan_writel(rxc, HSU_CH_MOTSR, 4);
348
349 chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
350 chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
351 chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
352 | (0x1 << 16)
353 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
354 );
355 chan_writel(rxc, HSU_CH_CR, 0x3);
Feng Tang669b7a02010-07-27 08:20:32 +0100356
357 mod_timer(&rxc->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
Feng Tangd843fc62010-07-27 08:20:22 +0100358}
359
360/* Protected by spin_lock_irqsave(port->lock) */
361static void serial_hsu_start_tx(struct uart_port *port)
362{
363 struct uart_hsu_port *up =
364 container_of(port, struct uart_hsu_port, port);
365
366 if (up->use_dma) {
367 hsu_dma_tx(up);
368 } else if (!(up->ier & UART_IER_THRI)) {
369 up->ier |= UART_IER_THRI;
370 serial_out(up, UART_IER, up->ier);
371 }
372}
373
374static void serial_hsu_stop_tx(struct uart_port *port)
375{
376 struct uart_hsu_port *up =
377 container_of(port, struct uart_hsu_port, port);
378 struct hsu_dma_chan *txc = up->txc;
379
380 if (up->use_dma)
381 chan_writel(txc, HSU_CH_CR, 0x0);
382 else if (up->ier & UART_IER_THRI) {
383 up->ier &= ~UART_IER_THRI;
384 serial_out(up, UART_IER, up->ier);
385 }
386}
387
388/* This is always called in spinlock protected mode, so
389 * modify timeout timer is safe here */
390void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
391{
392 struct hsu_dma_buffer *dbuf = &up->rxbuf;
393 struct hsu_dma_chan *chan = up->rxc;
394 struct uart_port *port = &up->port;
395 struct tty_struct *tty = port->state->port.tty;
396 int count;
397
398 if (!tty)
399 return;
400
401 /*
Feng Tang06c77e22010-07-27 08:20:42 +0100402 * First need to know how many is already transferred,
Feng Tangd843fc62010-07-27 08:20:22 +0100403 * then check if its a timeout DMA irq, and return
404 * the trail bytes out, push them up and reenable the
Feng Tang06c77e22010-07-27 08:20:42 +0100405 * channel
Feng Tangd843fc62010-07-27 08:20:22 +0100406 */
407
Feng Tang06c77e22010-07-27 08:20:42 +0100408 /* Timeout IRQ, need wait some time, see Errata 2 */
Feng Tangd843fc62010-07-27 08:20:22 +0100409 if (int_sts & 0xf00)
410 udelay(2);
411
412 /* Stop the channel */
413 chan_writel(chan, HSU_CH_CR, 0x0);
414
Feng Tangd843fc62010-07-27 08:20:22 +0100415 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
Feng Tang669b7a02010-07-27 08:20:32 +0100416 if (!count) {
Feng Tang06c77e22010-07-27 08:20:42 +0100417 /* Restart the channel before we leave */
Feng Tang669b7a02010-07-27 08:20:32 +0100418 chan_writel(chan, HSU_CH_CR, 0x3);
Feng Tangd843fc62010-07-27 08:20:22 +0100419 return;
Feng Tang669b7a02010-07-27 08:20:32 +0100420 }
Feng Tang669b7a02010-07-27 08:20:32 +0100421 del_timer(&chan->rx_timer);
Feng Tangd843fc62010-07-27 08:20:22 +0100422
423 dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
424 dbuf->dma_size, DMA_FROM_DEVICE);
425
426 /*
Feng Tang06c77e22010-07-27 08:20:42 +0100427 * Head will only wrap around when we recycle
Feng Tangd843fc62010-07-27 08:20:22 +0100428 * the DMA buffer, and when that happens, we
429 * explicitly set tail to 0. So head will
430 * always be greater than tail.
431 */
432 tty_insert_flip_string(tty, dbuf->buf, count);
433 port->icount.rx += count;
434
435 dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
436 dbuf->dma_size, DMA_FROM_DEVICE);
437
438 /* Reprogram the channel */
439 chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
440 chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
441 chan_writel(chan, HSU_CH_DCR, 0x1
442 | (0x1 << 8)
443 | (0x1 << 16)
444 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
445 );
Feng Tangd843fc62010-07-27 08:20:22 +0100446 tty_flip_buffer_push(tty);
Feng Tang669b7a02010-07-27 08:20:32 +0100447
448 chan_writel(chan, HSU_CH_CR, 0x3);
449 chan->rx_timer.expires = jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ;
450 add_timer(&chan->rx_timer);
451
Feng Tangd843fc62010-07-27 08:20:22 +0100452}
453
454static void serial_hsu_stop_rx(struct uart_port *port)
455{
456 struct uart_hsu_port *up =
457 container_of(port, struct uart_hsu_port, port);
458 struct hsu_dma_chan *chan = up->rxc;
459
460 if (up->use_dma)
461 chan_writel(chan, HSU_CH_CR, 0x2);
462 else {
463 up->ier &= ~UART_IER_RLSI;
464 up->port.read_status_mask &= ~UART_LSR_DR;
465 serial_out(up, UART_IER, up->ier);
466 }
467}
468
Feng Tangd843fc62010-07-27 08:20:22 +0100469static inline void receive_chars(struct uart_hsu_port *up, int *status)
470{
471 struct tty_struct *tty = up->port.state->port.tty;
472 unsigned int ch, flag;
473 unsigned int max_count = 256;
474
475 if (!tty)
476 return;
477
478 do {
479 ch = serial_in(up, UART_RX);
480 flag = TTY_NORMAL;
481 up->port.icount.rx++;
482
483 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
484 UART_LSR_FE | UART_LSR_OE))) {
485
486 dev_warn(up->dev, "We really rush into ERR/BI case"
487 "status = 0x%02x", *status);
488 /* For statistics only */
489 if (*status & UART_LSR_BI) {
490 *status &= ~(UART_LSR_FE | UART_LSR_PE);
491 up->port.icount.brk++;
492 /*
493 * We do the SysRQ and SAK checking
494 * here because otherwise the break
495 * may get masked by ignore_status_mask
496 * or read_status_mask.
497 */
498 if (uart_handle_break(&up->port))
499 goto ignore_char;
500 } else if (*status & UART_LSR_PE)
501 up->port.icount.parity++;
502 else if (*status & UART_LSR_FE)
503 up->port.icount.frame++;
504 if (*status & UART_LSR_OE)
505 up->port.icount.overrun++;
506
507 /* Mask off conditions which should be ignored. */
508 *status &= up->port.read_status_mask;
509
510#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
511 if (up->port.cons &&
512 up->port.cons->index == up->port.line) {
513 /* Recover the break flag from console xmit */
514 *status |= up->lsr_break_flag;
515 up->lsr_break_flag = 0;
516 }
517#endif
518 if (*status & UART_LSR_BI) {
519 flag = TTY_BREAK;
520 } else if (*status & UART_LSR_PE)
521 flag = TTY_PARITY;
522 else if (*status & UART_LSR_FE)
523 flag = TTY_FRAME;
524 }
525
526 if (uart_handle_sysrq_char(&up->port, ch))
527 goto ignore_char;
528
529 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
530 ignore_char:
531 *status = serial_in(up, UART_LSR);
532 } while ((*status & UART_LSR_DR) && max_count--);
533 tty_flip_buffer_push(tty);
534}
535
536static void transmit_chars(struct uart_hsu_port *up)
537{
538 struct circ_buf *xmit = &up->port.state->xmit;
539 int count;
Feng Tangd843fc62010-07-27 08:20:22 +0100540
541 if (up->port.x_char) {
542 serial_out(up, UART_TX, up->port.x_char);
543 up->port.icount.tx++;
544 up->port.x_char = 0;
545 return;
546 }
547 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
548 serial_hsu_stop_tx(&up->port);
549 return;
550 }
551
552#ifndef MFD_HSU_A0_STEPPING
553 count = up->port.fifosize / 2;
554#else
555 /*
556 * A0 only supports fully empty IRQ, and the first char written
557 * into it won't clear the EMPT bit, so we may need be cautious
558 * by useing a shorter buffer
559 */
Feng Tangd843fc62010-07-27 08:20:22 +0100560 count = up->port.fifosize - 4;
561#endif
562 do {
563 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
564 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Feng Tangd843fc62010-07-27 08:20:22 +0100565
566 up->port.icount.tx++;
567 if (uart_circ_empty(xmit))
568 break;
569 } while (--count > 0);
570
571 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
572 uart_write_wakeup(&up->port);
573
574 if (uart_circ_empty(xmit))
575 serial_hsu_stop_tx(&up->port);
576}
577
578static inline void check_modem_status(struct uart_hsu_port *up)
579{
580 int status;
581
582 status = serial_in(up, UART_MSR);
583
584 if ((status & UART_MSR_ANY_DELTA) == 0)
585 return;
586
587 if (status & UART_MSR_TERI)
588 up->port.icount.rng++;
589 if (status & UART_MSR_DDSR)
590 up->port.icount.dsr++;
591 /* We may only get DDCD when HW init and reset */
592 if (status & UART_MSR_DDCD)
593 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
Feng Tang06c77e22010-07-27 08:20:42 +0100594 /* Will start/stop_tx accordingly */
Feng Tangd843fc62010-07-27 08:20:22 +0100595 if (status & UART_MSR_DCTS)
596 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
597
598 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
599}
600
601/*
602 * This handles the interrupt from one port.
603 */
604static irqreturn_t port_irq(int irq, void *dev_id)
605{
606 struct uart_hsu_port *up = dev_id;
607 unsigned int iir, lsr;
608 unsigned long flags;
609
610 if (unlikely(!up->running))
611 return IRQ_NONE;
612
Feng Tang06c77e22010-07-27 08:20:42 +0100613 spin_lock_irqsave(&up->port.lock, flags);
Feng Tangd843fc62010-07-27 08:20:22 +0100614 if (up->use_dma) {
615 lsr = serial_in(up, UART_LSR);
616 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
617 UART_LSR_FE | UART_LSR_OE)))
618 dev_warn(up->dev,
619 "Got lsr irq while using DMA, lsr = 0x%2x\n",
620 lsr);
621 check_modem_status(up);
Feng Tang06c77e22010-07-27 08:20:42 +0100622 spin_unlock_irqrestore(&up->port.lock, flags);
Feng Tangd843fc62010-07-27 08:20:22 +0100623 return IRQ_HANDLED;
624 }
625
Feng Tangd843fc62010-07-27 08:20:22 +0100626 iir = serial_in(up, UART_IIR);
627 if (iir & UART_IIR_NO_INT) {
628 spin_unlock_irqrestore(&up->port.lock, flags);
629 return IRQ_NONE;
630 }
631
632 lsr = serial_in(up, UART_LSR);
Feng Tangd843fc62010-07-27 08:20:22 +0100633 if (lsr & UART_LSR_DR)
634 receive_chars(up, &lsr);
Feng Tang06c77e22010-07-27 08:20:42 +0100635 check_modem_status(up);
Feng Tangd843fc62010-07-27 08:20:22 +0100636
637 /* lsr will be renewed during the receive_chars */
638 if (lsr & UART_LSR_THRE)
639 transmit_chars(up);
640
641 spin_unlock_irqrestore(&up->port.lock, flags);
642 return IRQ_HANDLED;
643}
644
645static inline void dma_chan_irq(struct hsu_dma_chan *chan)
646{
647 struct uart_hsu_port *up = chan->uport;
648 unsigned long flags;
649 u32 int_sts;
650
651 spin_lock_irqsave(&up->port.lock, flags);
652
653 if (!up->use_dma || !up->running)
654 goto exit;
655
656 /*
657 * No matter what situation, need read clear the IRQ status
658 * There is a bug, see Errata 5, HSD 2900918
659 */
660 int_sts = chan_readl(chan, HSU_CH_SR);
661
662 /* Rx channel */
663 if (chan->dirt == DMA_FROM_DEVICE)
664 hsu_dma_rx(up, int_sts);
665
666 /* Tx channel */
667 if (chan->dirt == DMA_TO_DEVICE) {
Feng Tangd843fc62010-07-27 08:20:22 +0100668 chan_writel(chan, HSU_CH_CR, 0x0);
669 up->dma_tx_on = 0;
670 hsu_dma_tx(up);
671 }
672
673exit:
674 spin_unlock_irqrestore(&up->port.lock, flags);
675 return;
676}
677
678static irqreturn_t dma_irq(int irq, void *dev_id)
679{
680 struct hsu_port *hsu = dev_id;
681 u32 int_sts, i;
682
683 int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
684
685 /* Currently we only have 6 channels may be used */
686 for (i = 0; i < 6; i++) {
687 if (int_sts & 0x1)
688 dma_chan_irq(&hsu->chans[i]);
689 int_sts >>= 1;
690 }
691
692 return IRQ_HANDLED;
693}
694
695static unsigned int serial_hsu_tx_empty(struct uart_port *port)
696{
697 struct uart_hsu_port *up =
698 container_of(port, struct uart_hsu_port, port);
699 unsigned long flags;
700 unsigned int ret;
701
702 spin_lock_irqsave(&up->port.lock, flags);
703 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
704 spin_unlock_irqrestore(&up->port.lock, flags);
705
706 return ret;
707}
708
709static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
710{
711 struct uart_hsu_port *up =
712 container_of(port, struct uart_hsu_port, port);
713 unsigned char status;
714 unsigned int ret;
715
716 status = serial_in(up, UART_MSR);
717
718 ret = 0;
719 if (status & UART_MSR_DCD)
720 ret |= TIOCM_CAR;
721 if (status & UART_MSR_RI)
722 ret |= TIOCM_RNG;
723 if (status & UART_MSR_DSR)
724 ret |= TIOCM_DSR;
725 if (status & UART_MSR_CTS)
726 ret |= TIOCM_CTS;
727 return ret;
728}
729
730static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
731{
732 struct uart_hsu_port *up =
733 container_of(port, struct uart_hsu_port, port);
734 unsigned char mcr = 0;
735
736 if (mctrl & TIOCM_RTS)
737 mcr |= UART_MCR_RTS;
738 if (mctrl & TIOCM_DTR)
739 mcr |= UART_MCR_DTR;
740 if (mctrl & TIOCM_OUT1)
741 mcr |= UART_MCR_OUT1;
742 if (mctrl & TIOCM_OUT2)
743 mcr |= UART_MCR_OUT2;
744 if (mctrl & TIOCM_LOOP)
745 mcr |= UART_MCR_LOOP;
746
747 mcr |= up->mcr;
748
749 serial_out(up, UART_MCR, mcr);
750}
751
752static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
753{
754 struct uart_hsu_port *up =
755 container_of(port, struct uart_hsu_port, port);
756 unsigned long flags;
757
758 spin_lock_irqsave(&up->port.lock, flags);
759 if (break_state == -1)
760 up->lcr |= UART_LCR_SBC;
761 else
762 up->lcr &= ~UART_LCR_SBC;
763 serial_out(up, UART_LCR, up->lcr);
764 spin_unlock_irqrestore(&up->port.lock, flags);
765}
766
767/*
768 * What special to do:
769 * 1. chose the 64B fifo mode
770 * 2. make sure not to select half empty mode for A0 stepping
771 * 3. start dma or pio depends on configuration
772 * 4. we only allocate dma memory when needed
773 */
774static int serial_hsu_startup(struct uart_port *port)
775{
776 struct uart_hsu_port *up =
777 container_of(port, struct uart_hsu_port, port);
778 unsigned long flags;
779
780 /*
781 * Clear the FIFO buffers and disable them.
782 * (they will be reenabled in set_termios())
783 */
784 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
785 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
786 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
787 serial_out(up, UART_FCR, 0);
788
789 /* Clear the interrupt registers. */
790 (void) serial_in(up, UART_LSR);
791 (void) serial_in(up, UART_RX);
792 (void) serial_in(up, UART_IIR);
793 (void) serial_in(up, UART_MSR);
794
795 /* Now, initialize the UART, default is 8n1 */
796 serial_out(up, UART_LCR, UART_LCR_WLEN8);
797
798 spin_lock_irqsave(&up->port.lock, flags);
799
800 up->port.mctrl |= TIOCM_OUT2;
801 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
802
803 /*
804 * Finally, enable interrupts. Note: Modem status interrupts
805 * are set via set_termios(), which will be occurring imminently
806 * anyway, so we don't enable them here.
807 */
808 if (!up->use_dma)
809 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
810 else
811 up->ier = 0;
812 serial_out(up, UART_IER, up->ier);
813
814 spin_unlock_irqrestore(&up->port.lock, flags);
815
816 /* DMA init */
Feng Tangd843fc62010-07-27 08:20:22 +0100817 if (up->use_dma) {
818 struct hsu_dma_buffer *dbuf;
819 struct circ_buf *xmit = &port->state->xmit;
820
821 up->dma_tx_on = 0;
822
823 /* First allocate the RX buffer */
824 dbuf = &up->rxbuf;
825 dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
826 if (!dbuf->buf) {
827 up->use_dma = 0;
828 goto exit;
829 }
830 dbuf->dma_addr = dma_map_single(port->dev,
831 dbuf->buf,
832 HSU_DMA_BUF_SIZE,
833 DMA_FROM_DEVICE);
834 dbuf->dma_size = HSU_DMA_BUF_SIZE;
835
836 /* Start the RX channel right now */
837 hsu_dma_start_rx_chan(up->rxc, dbuf);
838
839 /* Next init the TX DMA */
840 dbuf = &up->txbuf;
841 dbuf->buf = xmit->buf;
842 dbuf->dma_addr = dma_map_single(port->dev,
843 dbuf->buf,
844 UART_XMIT_SIZE,
845 DMA_TO_DEVICE);
846 dbuf->dma_size = UART_XMIT_SIZE;
847
848 /* This should not be changed all around */
849 chan_writel(up->txc, HSU_CH_BSR, 32);
850 chan_writel(up->txc, HSU_CH_MOTSR, 4);
851 dbuf->ofs = 0;
852 }
853
854exit:
855 /* And clear the interrupt registers again for luck. */
856 (void) serial_in(up, UART_LSR);
857 (void) serial_in(up, UART_RX);
858 (void) serial_in(up, UART_IIR);
859 (void) serial_in(up, UART_MSR);
860
861 up->running = 1;
862 return 0;
863}
864
865static void serial_hsu_shutdown(struct uart_port *port)
866{
867 struct uart_hsu_port *up =
868 container_of(port, struct uart_hsu_port, port);
869 unsigned long flags;
870
Feng Tang669b7a02010-07-27 08:20:32 +0100871 del_timer_sync(&up->rxc->rx_timer);
872
Feng Tangd843fc62010-07-27 08:20:22 +0100873 /* Disable interrupts from this port */
874 up->ier = 0;
875 serial_out(up, UART_IER, 0);
876 up->running = 0;
877
878 spin_lock_irqsave(&up->port.lock, flags);
879 up->port.mctrl &= ~TIOCM_OUT2;
880 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
881 spin_unlock_irqrestore(&up->port.lock, flags);
882
883 /* Disable break condition and FIFOs */
884 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
885 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
886 UART_FCR_CLEAR_RCVR |
887 UART_FCR_CLEAR_XMIT);
888 serial_out(up, UART_FCR, 0);
889}
890
891static void
892serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
893 struct ktermios *old)
894{
895 struct uart_hsu_port *up =
896 container_of(port, struct uart_hsu_port, port);
897 struct tty_struct *tty = port->state->port.tty;
898 unsigned char cval, fcr = 0;
899 unsigned long flags;
900 unsigned int baud, quot;
901 u32 mul = 0x3600;
902 u32 ps = 0x10;
903
904 switch (termios->c_cflag & CSIZE) {
905 case CS5:
906 cval = UART_LCR_WLEN5;
907 break;
908 case CS6:
909 cval = UART_LCR_WLEN6;
910 break;
911 case CS7:
912 cval = UART_LCR_WLEN7;
913 break;
914 default:
915 case CS8:
916 cval = UART_LCR_WLEN8;
917 break;
918 }
919
920 /* CMSPAR isn't supported by this driver */
921 if (tty)
922 tty->termios->c_cflag &= ~CMSPAR;
923
924 if (termios->c_cflag & CSTOPB)
925 cval |= UART_LCR_STOP;
926 if (termios->c_cflag & PARENB)
927 cval |= UART_LCR_PARITY;
928 if (!(termios->c_cflag & PARODD))
929 cval |= UART_LCR_EPAR;
930
931 /*
932 * For those basic low baud rate we can get the direct
933 * scalar from 2746800, like 115200 = 2746800/24, for those
934 * higher baud rate, we have to handle them case by case,
935 * but DIV reg is never touched as its default value 0x3d09
936 */
937 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
938 quot = uart_get_divisor(port, baud);
939
940 switch (baud) {
941 case 3500000:
942 mul = 0x3345;
943 ps = 0xC;
944 quot = 1;
945 break;
946 case 2500000:
947 mul = 0x2710;
948 ps = 0x10;
949 quot = 1;
950 break;
951 case 18432000:
952 mul = 0x2400;
953 ps = 0x10;
954 quot = 1;
955 break;
956 case 1500000:
957 mul = 0x1D4C;
958 ps = 0xc;
959 quot = 1;
960 break;
961 default:
962 ;
963 }
964
965 if ((up->port.uartclk / quot) < (2400 * 16))
966 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
967 else if ((up->port.uartclk / quot) < (230400 * 16))
968 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
969 else
970 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
971
972 fcr |= UART_FCR_HSU_64B_FIFO;
973#ifdef MFD_HSU_A0_STEPPING
974 /* A0 doesn't support half empty IRQ */
975 fcr |= UART_FCR_FULL_EMPT_TXI;
976#endif
977
978 /*
979 * Ok, we're now changing the port state. Do it with
980 * interrupts disabled.
981 */
982 spin_lock_irqsave(&up->port.lock, flags);
983
984 /* Update the per-port timeout */
985 uart_update_timeout(port, termios->c_cflag, baud);
986
987 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
988 if (termios->c_iflag & INPCK)
989 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
990 if (termios->c_iflag & (BRKINT | PARMRK))
991 up->port.read_status_mask |= UART_LSR_BI;
992
993 /* Characters to ignore */
994 up->port.ignore_status_mask = 0;
995 if (termios->c_iflag & IGNPAR)
996 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
997 if (termios->c_iflag & IGNBRK) {
998 up->port.ignore_status_mask |= UART_LSR_BI;
999 /*
1000 * If we're ignoring parity and break indicators,
1001 * ignore overruns too (for real raw support).
1002 */
1003 if (termios->c_iflag & IGNPAR)
1004 up->port.ignore_status_mask |= UART_LSR_OE;
1005 }
1006
1007 /* Ignore all characters if CREAD is not set */
1008 if ((termios->c_cflag & CREAD) == 0)
1009 up->port.ignore_status_mask |= UART_LSR_DR;
1010
1011 /*
1012 * CTS flow control flag and modem status interrupts, disable
1013 * MSI by default
1014 */
1015 up->ier &= ~UART_IER_MSI;
1016 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1017 up->ier |= UART_IER_MSI;
1018
1019 serial_out(up, UART_IER, up->ier);
1020
1021 if (termios->c_cflag & CRTSCTS)
1022 up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
1023 else
1024 up->mcr &= ~UART_MCR_AFE;
1025
1026 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1027 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
1028 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
1029 serial_out(up, UART_LCR, cval); /* reset DLAB */
1030 serial_out(up, UART_MUL, mul); /* set MUL */
1031 serial_out(up, UART_PS, ps); /* set PS */
1032 up->lcr = cval; /* Save LCR */
1033 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
1034 serial_out(up, UART_FCR, fcr);
1035 spin_unlock_irqrestore(&up->port.lock, flags);
1036}
1037
1038static void
1039serial_hsu_pm(struct uart_port *port, unsigned int state,
1040 unsigned int oldstate)
1041{
1042}
1043
1044static void serial_hsu_release_port(struct uart_port *port)
1045{
1046}
1047
1048static int serial_hsu_request_port(struct uart_port *port)
1049{
1050 return 0;
1051}
1052
1053static void serial_hsu_config_port(struct uart_port *port, int flags)
1054{
Feng Tangd843fc62010-07-27 08:20:22 +01001055 struct uart_hsu_port *up =
1056 container_of(port, struct uart_hsu_port, port);
1057 up->port.type = PORT_MFD;
Feng Tangd843fc62010-07-27 08:20:22 +01001058}
1059
1060static int
1061serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
1062{
1063 /* We don't want the core code to modify any port params */
1064 return -EINVAL;
1065}
1066
1067static const char *
1068serial_hsu_type(struct uart_port *port)
1069{
1070 struct uart_hsu_port *up =
1071 container_of(port, struct uart_hsu_port, port);
1072 return up->name;
1073}
1074
1075/* Mainly for uart console use */
1076static struct uart_hsu_port *serial_hsu_ports[3];
1077static struct uart_driver serial_hsu_reg;
1078
1079#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1080
1081#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1082
1083/* Wait for transmitter & holding register to empty */
1084static inline void wait_for_xmitr(struct uart_hsu_port *up)
1085{
1086 unsigned int status, tmout = 1000;
1087
1088 /* Wait up to 1ms for the character to be sent. */
1089 do {
1090 status = serial_in(up, UART_LSR);
1091
1092 if (status & UART_LSR_BI)
1093 up->lsr_break_flag = UART_LSR_BI;
1094
1095 if (--tmout == 0)
1096 break;
1097 udelay(1);
1098 } while (!(status & BOTH_EMPTY));
1099
1100 /* Wait up to 1s for flow control if necessary */
1101 if (up->port.flags & UPF_CONS_FLOW) {
1102 tmout = 1000000;
1103 while (--tmout &&
1104 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1105 udelay(1);
1106 }
1107}
1108
1109static void serial_hsu_console_putchar(struct uart_port *port, int ch)
1110{
1111 struct uart_hsu_port *up =
1112 container_of(port, struct uart_hsu_port, port);
1113
1114 wait_for_xmitr(up);
1115 serial_out(up, UART_TX, ch);
1116}
1117
1118/*
1119 * Print a string to the serial port trying not to disturb
1120 * any possible real use of the port...
1121 *
1122 * The console_lock must be held when we get here.
1123 */
1124static void
1125serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
1126{
1127 struct uart_hsu_port *up = serial_hsu_ports[co->index];
1128 unsigned long flags;
1129 unsigned int ier;
1130 int locked = 1;
1131
1132 local_irq_save(flags);
1133 if (up->port.sysrq)
1134 locked = 0;
1135 else if (oops_in_progress) {
1136 locked = spin_trylock(&up->port.lock);
1137 } else
1138 spin_lock(&up->port.lock);
1139
1140 /* First save the IER then disable the interrupts */
1141 ier = serial_in(up, UART_IER);
1142 serial_out(up, UART_IER, 0);
1143
1144 uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
1145
1146 /*
1147 * Finally, wait for transmitter to become empty
1148 * and restore the IER
1149 */
1150 wait_for_xmitr(up);
1151 serial_out(up, UART_IER, ier);
1152
1153 if (locked)
1154 spin_unlock(&up->port.lock);
1155 local_irq_restore(flags);
1156}
1157
1158static struct console serial_hsu_console;
1159
1160static int __init
1161serial_hsu_console_setup(struct console *co, char *options)
1162{
1163 struct uart_hsu_port *up;
1164 int baud = 115200;
1165 int bits = 8;
1166 int parity = 'n';
1167 int flow = 'n';
1168 int ret;
1169
1170 if (co->index == -1 || co->index >= serial_hsu_reg.nr)
1171 co->index = 0;
1172 up = serial_hsu_ports[co->index];
1173 if (!up)
1174 return -ENODEV;
1175
1176 if (options)
1177 uart_parse_options(options, &baud, &parity, &bits, &flow);
1178
1179 ret = uart_set_options(&up->port, co, baud, parity, bits, flow);
1180
1181 return ret;
1182}
1183
1184static struct console serial_hsu_console = {
1185 .name = "ttyMFD",
1186 .write = serial_hsu_console_write,
1187 .device = uart_console_device,
1188 .setup = serial_hsu_console_setup,
1189 .flags = CON_PRINTBUFFER,
1190 .index = 2,
1191 .data = &serial_hsu_reg,
1192};
1193#endif
1194
1195struct uart_ops serial_hsu_pops = {
1196 .tx_empty = serial_hsu_tx_empty,
1197 .set_mctrl = serial_hsu_set_mctrl,
1198 .get_mctrl = serial_hsu_get_mctrl,
1199 .stop_tx = serial_hsu_stop_tx,
1200 .start_tx = serial_hsu_start_tx,
1201 .stop_rx = serial_hsu_stop_rx,
1202 .enable_ms = serial_hsu_enable_ms,
1203 .break_ctl = serial_hsu_break_ctl,
1204 .startup = serial_hsu_startup,
1205 .shutdown = serial_hsu_shutdown,
1206 .set_termios = serial_hsu_set_termios,
1207 .pm = serial_hsu_pm,
1208 .type = serial_hsu_type,
1209 .release_port = serial_hsu_release_port,
1210 .request_port = serial_hsu_request_port,
1211 .config_port = serial_hsu_config_port,
1212 .verify_port = serial_hsu_verify_port,
1213};
1214
1215static struct uart_driver serial_hsu_reg = {
1216 .owner = THIS_MODULE,
1217 .driver_name = "MFD serial",
1218 .dev_name = "ttyMFD",
1219 .major = TTY_MAJOR,
1220 .minor = 128,
1221 .nr = 3,
1222};
1223
1224#ifdef CONFIG_PM
1225static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
1226{
Feng Tang3c4108c2010-07-27 08:20:52 +01001227 void *priv = pci_get_drvdata(pdev);
Feng Tangd843fc62010-07-27 08:20:22 +01001228 struct uart_hsu_port *up;
1229
Feng Tang3c4108c2010-07-27 08:20:52 +01001230 /* Make sure this is not the internal dma controller */
1231 if (priv && (pdev->device != 0x081E)) {
1232 up = priv;
1233 uart_suspend_port(&serial_hsu_reg, &up->port);
1234 }
Feng Tangd843fc62010-07-27 08:20:22 +01001235
Feng Tang3c4108c2010-07-27 08:20:52 +01001236 pci_save_state(pdev);
1237 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Feng Tangd843fc62010-07-27 08:20:22 +01001238 return 0;
1239}
1240
1241static int serial_hsu_resume(struct pci_dev *pdev)
1242{
Feng Tang3c4108c2010-07-27 08:20:52 +01001243 void *priv = pci_get_drvdata(pdev);
Feng Tangd843fc62010-07-27 08:20:22 +01001244 struct uart_hsu_port *up;
Feng Tang3c4108c2010-07-27 08:20:52 +01001245 int ret;
Feng Tangd843fc62010-07-27 08:20:22 +01001246
Feng Tang3c4108c2010-07-27 08:20:52 +01001247 pci_set_power_state(pdev, PCI_D0);
1248 pci_restore_state(pdev);
1249
1250 ret = pci_enable_device(pdev);
1251 if (ret)
1252 dev_warn(&pdev->dev,
1253 "HSU: can't re-enable device, try to continue\n");
1254
1255 if (priv && (pdev->device != 0x081E)) {
1256 up = priv;
1257 uart_resume_port(&serial_hsu_reg, &up->port);
1258 }
Feng Tangd843fc62010-07-27 08:20:22 +01001259 return 0;
1260}
1261#else
1262#define serial_hsu_suspend NULL
1263#define serial_hsu_resume NULL
1264#endif
1265
1266/* temp global pointer before we settle down on using one or four PCI dev */
1267static struct hsu_port *phsu;
1268
1269static int serial_hsu_probe(struct pci_dev *pdev,
1270 const struct pci_device_id *ent)
1271{
1272 struct uart_hsu_port *uport;
1273 int index, ret;
1274
1275 printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
1276 pdev->vendor, pdev->device);
1277
1278 switch (pdev->device) {
1279 case 0x081B:
1280 index = 0;
1281 break;
1282 case 0x081C:
1283 index = 1;
1284 break;
1285 case 0x081D:
1286 index = 2;
1287 break;
1288 case 0x081E:
1289 /* internal DMA controller */
1290 index = 3;
1291 break;
1292 default:
1293 dev_err(&pdev->dev, "HSU: out of index!");
1294 return -ENODEV;
1295 }
1296
1297 ret = pci_enable_device(pdev);
1298 if (ret)
1299 return ret;
1300
1301 if (index == 3) {
1302 /* DMA controller */
1303 ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
1304 if (ret) {
1305 dev_err(&pdev->dev, "can not get IRQ\n");
1306 goto err_disable;
1307 }
1308 pci_set_drvdata(pdev, phsu);
1309 } else {
1310 /* UART port 0~2 */
1311 uport = &phsu->port[index];
1312 uport->port.irq = pdev->irq;
1313 uport->port.dev = &pdev->dev;
1314 uport->dev = &pdev->dev;
1315
1316 ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
1317 if (ret) {
1318 dev_err(&pdev->dev, "can not get IRQ\n");
1319 goto err_disable;
1320 }
1321 uart_add_one_port(&serial_hsu_reg, &uport->port);
1322
1323#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1324 if (index == 2) {
1325 register_console(&serial_hsu_console);
1326 uport->port.cons = &serial_hsu_console;
1327 }
1328#endif
1329 pci_set_drvdata(pdev, uport);
1330 }
1331
1332 return 0;
1333
1334err_disable:
1335 pci_disable_device(pdev);
1336 return ret;
1337}
1338
Feng Tang669b7a02010-07-27 08:20:32 +01001339static void hsu_dma_rx_timeout(unsigned long data)
1340{
1341 struct hsu_dma_chan *chan = (void *)data;
1342 struct uart_hsu_port *up = chan->uport;
1343 struct hsu_dma_buffer *dbuf = &up->rxbuf;
1344 int count = 0;
1345 unsigned long flags;
1346
1347 spin_lock_irqsave(&up->port.lock, flags);
1348
1349 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
1350
1351 if (!count) {
1352 mod_timer(&chan->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
1353 goto exit;
1354 }
1355
1356 hsu_dma_rx(up, 0);
1357exit:
1358 spin_unlock_irqrestore(&up->port.lock, flags);
1359}
1360
Feng Tangd843fc62010-07-27 08:20:22 +01001361static void hsu_global_init(void)
1362{
1363 struct hsu_port *hsu;
1364 struct uart_hsu_port *uport;
1365 struct hsu_dma_chan *dchan;
1366 int i, ret;
1367
1368 hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
1369 if (!hsu)
1370 return;
1371
1372 /* Get basic io resource and map it */
1373 hsu->paddr = 0xffa28000;
1374 hsu->iolen = 0x1000;
1375
1376 if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
1377 pr_warning("HSU: error in request mem region\n");
1378
1379 hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
1380 if (!hsu->reg) {
1381 pr_err("HSU: error in ioremap\n");
1382 ret = -ENOMEM;
1383 goto err_free_region;
1384 }
1385
1386 /* Initialise the 3 UART ports */
1387 uport = hsu->port;
1388 for (i = 0; i < 3; i++) {
1389 uport->port.type = PORT_MFD;
1390 uport->port.iotype = UPIO_MEM;
1391 uport->port.mapbase = (resource_size_t)hsu->paddr
1392 + HSU_PORT_REG_OFFSET
1393 + i * HSU_PORT_REG_LENGTH;
1394 uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
1395 + i * HSU_PORT_REG_LENGTH;
1396
1397 sprintf(uport->name, "hsu_port%d", i);
1398 uport->port.fifosize = 64;
1399 uport->port.ops = &serial_hsu_pops;
1400 uport->port.line = i;
1401 uport->port.flags = UPF_IOREMAP;
Feng Tang06c77e22010-07-27 08:20:42 +01001402 /* set the scalable maxim support rate to 2746800 bps */
Feng Tangd843fc62010-07-27 08:20:22 +01001403 uport->port.uartclk = 115200 * 24 * 16;
1404
1405 uport->running = 0;
1406 uport->txc = &hsu->chans[i * 2];
1407 uport->rxc = &hsu->chans[i * 2 + 1];
1408
1409 serial_hsu_ports[i] = uport;
1410 uport->index = i;
1411 uport++;
1412 }
1413
1414 /* Initialise 6 dma channels */
1415 dchan = hsu->chans;
1416 for (i = 0; i < 6; i++) {
1417 dchan->id = i;
1418 dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1419 dchan->uport = &hsu->port[i/2];
1420 dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
1421 i * HSU_DMA_CHANS_REG_LENGTH;
Feng Tang669b7a02010-07-27 08:20:32 +01001422
1423 /* Work around for RX */
1424 if (dchan->dirt == DMA_FROM_DEVICE) {
1425 init_timer(&dchan->rx_timer);
1426 dchan->rx_timer.function = hsu_dma_rx_timeout;
1427 dchan->rx_timer.data = (unsigned long)dchan;
1428 }
Feng Tangd843fc62010-07-27 08:20:22 +01001429 dchan++;
1430 }
1431
1432 phsu = hsu;
Feng Tangd843fc62010-07-27 08:20:22 +01001433 hsu_debugfs_init(hsu);
1434 return;
1435
1436err_free_region:
1437 release_mem_region(hsu->paddr, hsu->iolen);
1438 kfree(hsu);
1439 return;
1440}
1441
1442static void serial_hsu_remove(struct pci_dev *pdev)
1443{
Feng Tange3671ac2010-09-06 13:41:02 +01001444 void *priv = pci_get_drvdata(pdev);
1445 struct uart_hsu_port *up;
Feng Tangd843fc62010-07-27 08:20:22 +01001446
Feng Tange3671ac2010-09-06 13:41:02 +01001447 if (!priv)
Feng Tangd843fc62010-07-27 08:20:22 +01001448 return;
1449
Feng Tange3671ac2010-09-06 13:41:02 +01001450 /* For port 0/1/2, priv is the address of uart_hsu_port */
1451 if (pdev->device != 0x081E) {
1452 up = priv;
1453 uart_remove_one_port(&serial_hsu_reg, &up->port);
1454 }
Feng Tangd843fc62010-07-27 08:20:22 +01001455
1456 pci_set_drvdata(pdev, NULL);
Feng Tange3671ac2010-09-06 13:41:02 +01001457 free_irq(pdev->irq, priv);
Feng Tangd843fc62010-07-27 08:20:22 +01001458 pci_disable_device(pdev);
1459}
1460
1461/* First 3 are UART ports, and the 4th is the DMA */
1462static const struct pci_device_id pci_ids[] __devinitdata = {
1463 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
1464 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
1465 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
1466 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
1467 {},
1468};
1469
1470static struct pci_driver hsu_pci_driver = {
1471 .name = "HSU serial",
1472 .id_table = pci_ids,
1473 .probe = serial_hsu_probe,
1474 .remove = __devexit_p(serial_hsu_remove),
1475 .suspend = serial_hsu_suspend,
1476 .resume = serial_hsu_resume,
1477};
1478
1479static int __init hsu_pci_init(void)
1480{
1481 int ret;
1482
1483 hsu_global_init();
1484
1485 ret = uart_register_driver(&serial_hsu_reg);
1486 if (ret)
1487 return ret;
1488
1489 return pci_register_driver(&hsu_pci_driver);
1490}
1491
1492static void __exit hsu_pci_exit(void)
1493{
1494 pci_unregister_driver(&hsu_pci_driver);
1495 uart_unregister_driver(&serial_hsu_reg);
1496
1497 hsu_debugfs_remove(phsu);
1498
1499 kfree(phsu);
1500}
1501
1502module_init(hsu_pci_init);
1503module_exit(hsu_pci_exit);
1504
1505MODULE_LICENSE("GPL v2");
1506MODULE_ALIAS("platform:medfield-hsu");