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