blob: 60d585ab4870300210b6837507895edd08682b12 [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 Tang085a4f72011-02-22 15:28:10 +080019 * 2. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always
Feng Tang06c77e22010-07-27 08:20:42 +010020 * asserted, only when the HW is reset the DDCD and DDSR will
21 * be triggered
Feng Tangd843fc62010-07-27 08:20:22 +010022 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/console.h>
27#include <linux/sysrq.h>
Andrew Morton63d66ca2010-09-30 15:15:28 -070028#include <linux/slab.h>
Feng Tangd843fc62010-07-27 08:20:22 +010029#include <linux/serial_reg.h>
30#include <linux/circ_buf.h>
31#include <linux/delay.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_core.h>
36#include <linux/serial_mfd.h>
37#include <linux/dma-mapping.h>
38#include <linux/pci.h>
Feng Tang50827fb2012-11-15 16:03:16 +080039#include <linux/nmi.h>
Feng Tangd843fc62010-07-27 08:20:22 +010040#include <linux/io.h>
41#include <linux/debugfs.h>
Kristen Carlson Accardi88e51732011-08-26 11:35:16 +010042#include <linux/pm_runtime.h>
Feng Tangd843fc62010-07-27 08:20:22 +010043
Feng Tangd843fc62010-07-27 08:20:22 +010044#define HSU_DMA_BUF_SIZE 2048
45
46#define chan_readl(chan, offset) readl(chan->reg + offset)
47#define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
48
49#define mfd_readl(obj, offset) readl(obj->reg + offset)
50#define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
51
Feng Tangf023eab2011-02-22 15:28:12 +080052static int hsu_dma_enable;
53module_param(hsu_dma_enable, int, 0);
Joe Perches85ee7a12011-04-23 20:38:19 -070054MODULE_PARM_DESC(hsu_dma_enable,
55 "It is a bitmap to set working mode, if bit[x] is 1, then port[x] will work in DMA mode, otherwise in PIO mode.");
Feng Tangf023eab2011-02-22 15:28:12 +080056
Feng Tangd843fc62010-07-27 08:20:22 +010057struct hsu_dma_buffer {
58 u8 *buf;
59 dma_addr_t dma_addr;
60 u32 dma_size;
61 u32 ofs;
62};
63
64struct hsu_dma_chan {
65 u32 id;
Feng Tang06c77e22010-07-27 08:20:42 +010066 enum dma_data_direction dirt;
Feng Tangd843fc62010-07-27 08:20:22 +010067 struct uart_hsu_port *uport;
Feng Tang669b7a02010-07-27 08:20:32 +010068 void __iomem *reg;
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
Feng Tangd843fc62010-07-27 08:20:22 +0100131
132static ssize_t port_show_regs(struct file *file, char __user *user_buf,
133 size_t count, loff_t *ppos)
134{
135 struct uart_hsu_port *up = file->private_data;
136 char *buf;
137 u32 len = 0;
138 ssize_t ret;
139
140 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
141 if (!buf)
142 return 0;
143
144 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
145 "MFD HSU port[%d] regs:\n", up->index);
146
147 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
148 "=================================\n");
149 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
150 "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
151 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
152 "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
153 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
154 "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
155 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
156 "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
157 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
158 "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
159 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
160 "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
161 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
162 "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
163 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
164 "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
165 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
166 "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
167 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
168 "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
169
Dan Carpentera9589812010-08-12 09:50:09 +0200170 if (len > HSU_REGS_BUFSIZE)
171 len = HSU_REGS_BUFSIZE;
172
Feng Tangd843fc62010-07-27 08:20:22 +0100173 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
174 kfree(buf);
175 return ret;
176}
177
178static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
179 size_t count, loff_t *ppos)
180{
181 struct hsu_dma_chan *chan = file->private_data;
182 char *buf;
183 u32 len = 0;
184 ssize_t ret;
185
186 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
187 if (!buf)
188 return 0;
189
190 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
191 "MFD HSU DMA channel [%d] regs:\n", chan->id);
192
193 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
194 "=================================\n");
195 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
196 "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
197 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
198 "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
199 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
200 "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
201 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
202 "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
203 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
204 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
205 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
206 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
207 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
208 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
209 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
210 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
211 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
212 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
213 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
214 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
215 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
216 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
217 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
218 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
219
Dan Carpentera9589812010-08-12 09:50:09 +0200220 if (len > HSU_REGS_BUFSIZE)
221 len = HSU_REGS_BUFSIZE;
222
Feng Tangd843fc62010-07-27 08:20:22 +0100223 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
224 kfree(buf);
225 return ret;
226}
227
228static const struct file_operations port_regs_ops = {
229 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700230 .open = simple_open,
Feng Tangd843fc62010-07-27 08:20:22 +0100231 .read = port_show_regs,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200232 .llseek = default_llseek,
Feng Tangd843fc62010-07-27 08:20:22 +0100233};
234
235static const struct file_operations dma_regs_ops = {
236 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700237 .open = simple_open,
Feng Tangd843fc62010-07-27 08:20:22 +0100238 .read = dma_show_regs,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200239 .llseek = default_llseek,
Feng Tangd843fc62010-07-27 08:20:22 +0100240};
241
242static int hsu_debugfs_init(struct hsu_port *hsu)
243{
244 int i;
245 char name[32];
246
247 hsu->debugfs = debugfs_create_dir("hsu", NULL);
248 if (!hsu->debugfs)
249 return -ENOMEM;
250
251 for (i = 0; i < 3; i++) {
252 snprintf(name, sizeof(name), "port_%d_regs", i);
253 debugfs_create_file(name, S_IFREG | S_IRUGO,
254 hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
255 }
256
257 for (i = 0; i < 6; i++) {
258 snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
259 debugfs_create_file(name, S_IFREG | S_IRUGO,
260 hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
261 }
262
263 return 0;
264}
265
266static void hsu_debugfs_remove(struct hsu_port *hsu)
267{
268 if (hsu->debugfs)
269 debugfs_remove_recursive(hsu->debugfs);
270}
271
272#else
273static inline int hsu_debugfs_init(struct hsu_port *hsu)
274{
275 return 0;
276}
277
278static inline void hsu_debugfs_remove(struct hsu_port *hsu)
279{
280}
281#endif /* CONFIG_DEBUG_FS */
282
283static void serial_hsu_enable_ms(struct uart_port *port)
284{
285 struct uart_hsu_port *up =
286 container_of(port, struct uart_hsu_port, port);
287
288 up->ier |= UART_IER_MSI;
289 serial_out(up, UART_IER, up->ier);
290}
291
292void hsu_dma_tx(struct uart_hsu_port *up)
293{
294 struct circ_buf *xmit = &up->port.state->xmit;
295 struct hsu_dma_buffer *dbuf = &up->txbuf;
296 int count;
297
298 /* test_and_set_bit may be better, but anyway it's in lock protected mode */
299 if (up->dma_tx_on)
300 return;
301
302 /* Update the circ buf info */
303 xmit->tail += dbuf->ofs;
304 xmit->tail &= UART_XMIT_SIZE - 1;
305
306 up->port.icount.tx += dbuf->ofs;
307 dbuf->ofs = 0;
308
309 /* Disable the channel */
310 chan_writel(up->txc, HSU_CH_CR, 0x0);
311
312 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
313 dma_sync_single_for_device(up->port.dev,
314 dbuf->dma_addr,
315 dbuf->dma_size,
316 DMA_TO_DEVICE);
317
318 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
319 dbuf->ofs = count;
320
321 /* Reprogram the channel */
322 chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
323 chan_writel(up->txc, HSU_CH_D0TSR, count);
324
325 /* Reenable the channel */
326 chan_writel(up->txc, HSU_CH_DCR, 0x1
327 | (0x1 << 8)
328 | (0x1 << 16)
329 | (0x1 << 24));
Feng Tangd843fc62010-07-27 08:20:22 +0100330 up->dma_tx_on = 1;
331 chan_writel(up->txc, HSU_CH_CR, 0x1);
332 }
333
334 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
335 uart_write_wakeup(&up->port);
336}
337
338/* The buffer is already cache coherent */
339void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
340{
Feng Tangd843fc62010-07-27 08:20:22 +0100341 dbuf->ofs = 0;
342
343 chan_writel(rxc, HSU_CH_BSR, 32);
344 chan_writel(rxc, HSU_CH_MOTSR, 4);
345
346 chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
347 chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
348 chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
349 | (0x1 << 16)
350 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
351 );
352 chan_writel(rxc, HSU_CH_CR, 0x3);
353}
354
355/* Protected by spin_lock_irqsave(port->lock) */
356static void serial_hsu_start_tx(struct uart_port *port)
357{
358 struct uart_hsu_port *up =
359 container_of(port, struct uart_hsu_port, port);
360
361 if (up->use_dma) {
362 hsu_dma_tx(up);
363 } else if (!(up->ier & UART_IER_THRI)) {
364 up->ier |= UART_IER_THRI;
365 serial_out(up, UART_IER, up->ier);
366 }
367}
368
369static void serial_hsu_stop_tx(struct uart_port *port)
370{
371 struct uart_hsu_port *up =
372 container_of(port, struct uart_hsu_port, port);
373 struct hsu_dma_chan *txc = up->txc;
374
375 if (up->use_dma)
376 chan_writel(txc, HSU_CH_CR, 0x0);
377 else if (up->ier & UART_IER_THRI) {
378 up->ier &= ~UART_IER_THRI;
379 serial_out(up, UART_IER, up->ier);
380 }
381}
382
383/* This is always called in spinlock protected mode, so
384 * modify timeout timer is safe here */
385void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
386{
387 struct hsu_dma_buffer *dbuf = &up->rxbuf;
388 struct hsu_dma_chan *chan = up->rxc;
389 struct uart_port *port = &up->port;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100390 struct tty_port *tport = &port->state->port;
391 struct tty_struct *tty = tport->tty;
Feng Tangd843fc62010-07-27 08:20:22 +0100392 int count;
393
394 if (!tty)
395 return;
396
397 /*
Feng Tang06c77e22010-07-27 08:20:42 +0100398 * First need to know how many is already transferred,
Feng Tangd843fc62010-07-27 08:20:22 +0100399 * then check if its a timeout DMA irq, and return
400 * the trail bytes out, push them up and reenable the
Feng Tang06c77e22010-07-27 08:20:42 +0100401 * channel
Feng Tangd843fc62010-07-27 08:20:22 +0100402 */
403
Feng Tang06c77e22010-07-27 08:20:42 +0100404 /* Timeout IRQ, need wait some time, see Errata 2 */
Feng Tangd843fc62010-07-27 08:20:22 +0100405 if (int_sts & 0xf00)
406 udelay(2);
407
408 /* Stop the channel */
409 chan_writel(chan, HSU_CH_CR, 0x0);
410
Feng Tangd843fc62010-07-27 08:20:22 +0100411 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
Feng Tang669b7a02010-07-27 08:20:32 +0100412 if (!count) {
Feng Tang06c77e22010-07-27 08:20:42 +0100413 /* Restart the channel before we leave */
Feng Tang669b7a02010-07-27 08:20:32 +0100414 chan_writel(chan, HSU_CH_CR, 0x3);
Feng Tangd843fc62010-07-27 08:20:22 +0100415 return;
Feng Tang669b7a02010-07-27 08:20:32 +0100416 }
Feng Tangd843fc62010-07-27 08:20:22 +0100417
418 dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
419 dbuf->dma_size, DMA_FROM_DEVICE);
420
421 /*
Feng Tang06c77e22010-07-27 08:20:42 +0100422 * Head will only wrap around when we recycle
Feng Tangd843fc62010-07-27 08:20:22 +0100423 * the DMA buffer, and when that happens, we
424 * explicitly set tail to 0. So head will
425 * always be greater than tail.
426 */
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100427 tty_insert_flip_string(tport, dbuf->buf, count);
Feng Tangd843fc62010-07-27 08:20:22 +0100428 port->icount.rx += count;
429
430 dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
431 dbuf->dma_size, DMA_FROM_DEVICE);
432
433 /* Reprogram the channel */
434 chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
435 chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
436 chan_writel(chan, HSU_CH_DCR, 0x1
437 | (0x1 << 8)
438 | (0x1 << 16)
439 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
440 );
Feng Tangd843fc62010-07-27 08:20:22 +0100441 tty_flip_buffer_push(tty);
Feng Tang669b7a02010-07-27 08:20:32 +0100442
443 chan_writel(chan, HSU_CH_CR, 0x3);
Feng Tang669b7a02010-07-27 08:20:32 +0100444
Feng Tangd843fc62010-07-27 08:20:22 +0100445}
446
447static void serial_hsu_stop_rx(struct uart_port *port)
448{
449 struct uart_hsu_port *up =
450 container_of(port, struct uart_hsu_port, port);
451 struct hsu_dma_chan *chan = up->rxc;
452
453 if (up->use_dma)
454 chan_writel(chan, HSU_CH_CR, 0x2);
455 else {
456 up->ier &= ~UART_IER_RLSI;
457 up->port.read_status_mask &= ~UART_LSR_DR;
458 serial_out(up, UART_IER, up->ier);
459 }
460}
461
Feng Tangd843fc62010-07-27 08:20:22 +0100462static inline void receive_chars(struct uart_hsu_port *up, int *status)
463{
464 struct tty_struct *tty = up->port.state->port.tty;
465 unsigned int ch, flag;
466 unsigned int max_count = 256;
467
468 if (!tty)
469 return;
470
471 do {
472 ch = serial_in(up, UART_RX);
473 flag = TTY_NORMAL;
474 up->port.icount.rx++;
475
476 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
477 UART_LSR_FE | UART_LSR_OE))) {
478
479 dev_warn(up->dev, "We really rush into ERR/BI case"
480 "status = 0x%02x", *status);
481 /* For statistics only */
482 if (*status & UART_LSR_BI) {
483 *status &= ~(UART_LSR_FE | UART_LSR_PE);
484 up->port.icount.brk++;
485 /*
486 * We do the SysRQ and SAK checking
487 * here because otherwise the break
488 * may get masked by ignore_status_mask
489 * or read_status_mask.
490 */
491 if (uart_handle_break(&up->port))
492 goto ignore_char;
493 } else if (*status & UART_LSR_PE)
494 up->port.icount.parity++;
495 else if (*status & UART_LSR_FE)
496 up->port.icount.frame++;
497 if (*status & UART_LSR_OE)
498 up->port.icount.overrun++;
499
500 /* Mask off conditions which should be ignored. */
501 *status &= up->port.read_status_mask;
502
503#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
504 if (up->port.cons &&
505 up->port.cons->index == up->port.line) {
506 /* Recover the break flag from console xmit */
507 *status |= up->lsr_break_flag;
508 up->lsr_break_flag = 0;
509 }
510#endif
511 if (*status & UART_LSR_BI) {
512 flag = TTY_BREAK;
513 } else if (*status & UART_LSR_PE)
514 flag = TTY_PARITY;
515 else if (*status & UART_LSR_FE)
516 flag = TTY_FRAME;
517 }
518
519 if (uart_handle_sysrq_char(&up->port, ch))
520 goto ignore_char;
521
522 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
523 ignore_char:
524 *status = serial_in(up, UART_LSR);
525 } while ((*status & UART_LSR_DR) && max_count--);
526 tty_flip_buffer_push(tty);
527}
528
529static void transmit_chars(struct uart_hsu_port *up)
530{
531 struct circ_buf *xmit = &up->port.state->xmit;
532 int count;
Feng Tangd843fc62010-07-27 08:20:22 +0100533
534 if (up->port.x_char) {
535 serial_out(up, UART_TX, up->port.x_char);
536 up->port.icount.tx++;
537 up->port.x_char = 0;
538 return;
539 }
540 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
541 serial_hsu_stop_tx(&up->port);
542 return;
543 }
544
Feng Tang085a4f72011-02-22 15:28:10 +0800545 /* The IRQ is for TX FIFO half-empty */
Feng Tangd843fc62010-07-27 08:20:22 +0100546 count = up->port.fifosize / 2;
Feng Tang085a4f72011-02-22 15:28:10 +0800547
Feng Tangd843fc62010-07-27 08:20:22 +0100548 do {
549 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
550 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Feng Tangd843fc62010-07-27 08:20:22 +0100551
552 up->port.icount.tx++;
553 if (uart_circ_empty(xmit))
554 break;
555 } while (--count > 0);
556
557 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
558 uart_write_wakeup(&up->port);
559
560 if (uart_circ_empty(xmit))
561 serial_hsu_stop_tx(&up->port);
562}
563
564static inline void check_modem_status(struct uart_hsu_port *up)
565{
566 int status;
567
568 status = serial_in(up, UART_MSR);
569
570 if ((status & UART_MSR_ANY_DELTA) == 0)
571 return;
572
573 if (status & UART_MSR_TERI)
574 up->port.icount.rng++;
575 if (status & UART_MSR_DDSR)
576 up->port.icount.dsr++;
577 /* We may only get DDCD when HW init and reset */
578 if (status & UART_MSR_DDCD)
579 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
Feng Tang06c77e22010-07-27 08:20:42 +0100580 /* Will start/stop_tx accordingly */
Feng Tangd843fc62010-07-27 08:20:22 +0100581 if (status & UART_MSR_DCTS)
582 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
583
584 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
585}
586
587/*
588 * This handles the interrupt from one port.
589 */
590static irqreturn_t port_irq(int irq, void *dev_id)
591{
592 struct uart_hsu_port *up = dev_id;
593 unsigned int iir, lsr;
594 unsigned long flags;
595
596 if (unlikely(!up->running))
597 return IRQ_NONE;
598
Feng Tang06c77e22010-07-27 08:20:42 +0100599 spin_lock_irqsave(&up->port.lock, flags);
Feng Tangd843fc62010-07-27 08:20:22 +0100600 if (up->use_dma) {
601 lsr = serial_in(up, UART_LSR);
602 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
603 UART_LSR_FE | UART_LSR_OE)))
604 dev_warn(up->dev,
605 "Got lsr irq while using DMA, lsr = 0x%2x\n",
606 lsr);
607 check_modem_status(up);
Feng Tang06c77e22010-07-27 08:20:42 +0100608 spin_unlock_irqrestore(&up->port.lock, flags);
Feng Tangd843fc62010-07-27 08:20:22 +0100609 return IRQ_HANDLED;
610 }
611
Feng Tangd843fc62010-07-27 08:20:22 +0100612 iir = serial_in(up, UART_IIR);
613 if (iir & UART_IIR_NO_INT) {
614 spin_unlock_irqrestore(&up->port.lock, flags);
615 return IRQ_NONE;
616 }
617
618 lsr = serial_in(up, UART_LSR);
Feng Tangd843fc62010-07-27 08:20:22 +0100619 if (lsr & UART_LSR_DR)
620 receive_chars(up, &lsr);
Feng Tang06c77e22010-07-27 08:20:42 +0100621 check_modem_status(up);
Feng Tangd843fc62010-07-27 08:20:22 +0100622
623 /* lsr will be renewed during the receive_chars */
624 if (lsr & UART_LSR_THRE)
625 transmit_chars(up);
626
627 spin_unlock_irqrestore(&up->port.lock, flags);
628 return IRQ_HANDLED;
629}
630
631static inline void dma_chan_irq(struct hsu_dma_chan *chan)
632{
633 struct uart_hsu_port *up = chan->uport;
634 unsigned long flags;
635 u32 int_sts;
636
637 spin_lock_irqsave(&up->port.lock, flags);
638
639 if (!up->use_dma || !up->running)
640 goto exit;
641
642 /*
643 * No matter what situation, need read clear the IRQ status
644 * There is a bug, see Errata 5, HSD 2900918
645 */
646 int_sts = chan_readl(chan, HSU_CH_SR);
647
648 /* Rx channel */
649 if (chan->dirt == DMA_FROM_DEVICE)
650 hsu_dma_rx(up, int_sts);
651
652 /* Tx channel */
653 if (chan->dirt == DMA_TO_DEVICE) {
Feng Tangd843fc62010-07-27 08:20:22 +0100654 chan_writel(chan, HSU_CH_CR, 0x0);
655 up->dma_tx_on = 0;
656 hsu_dma_tx(up);
657 }
658
659exit:
660 spin_unlock_irqrestore(&up->port.lock, flags);
661 return;
662}
663
664static irqreturn_t dma_irq(int irq, void *dev_id)
665{
666 struct hsu_port *hsu = dev_id;
667 u32 int_sts, i;
668
669 int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
670
671 /* Currently we only have 6 channels may be used */
672 for (i = 0; i < 6; i++) {
673 if (int_sts & 0x1)
674 dma_chan_irq(&hsu->chans[i]);
675 int_sts >>= 1;
676 }
677
678 return IRQ_HANDLED;
679}
680
681static unsigned int serial_hsu_tx_empty(struct uart_port *port)
682{
683 struct uart_hsu_port *up =
684 container_of(port, struct uart_hsu_port, port);
685 unsigned long flags;
686 unsigned int ret;
687
688 spin_lock_irqsave(&up->port.lock, flags);
689 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
690 spin_unlock_irqrestore(&up->port.lock, flags);
691
692 return ret;
693}
694
695static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
696{
697 struct uart_hsu_port *up =
698 container_of(port, struct uart_hsu_port, port);
699 unsigned char status;
700 unsigned int ret;
701
702 status = serial_in(up, UART_MSR);
703
704 ret = 0;
705 if (status & UART_MSR_DCD)
706 ret |= TIOCM_CAR;
707 if (status & UART_MSR_RI)
708 ret |= TIOCM_RNG;
709 if (status & UART_MSR_DSR)
710 ret |= TIOCM_DSR;
711 if (status & UART_MSR_CTS)
712 ret |= TIOCM_CTS;
713 return ret;
714}
715
716static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
717{
718 struct uart_hsu_port *up =
719 container_of(port, struct uart_hsu_port, port);
720 unsigned char mcr = 0;
721
722 if (mctrl & TIOCM_RTS)
723 mcr |= UART_MCR_RTS;
724 if (mctrl & TIOCM_DTR)
725 mcr |= UART_MCR_DTR;
726 if (mctrl & TIOCM_OUT1)
727 mcr |= UART_MCR_OUT1;
728 if (mctrl & TIOCM_OUT2)
729 mcr |= UART_MCR_OUT2;
730 if (mctrl & TIOCM_LOOP)
731 mcr |= UART_MCR_LOOP;
732
733 mcr |= up->mcr;
734
735 serial_out(up, UART_MCR, mcr);
736}
737
738static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
739{
740 struct uart_hsu_port *up =
741 container_of(port, struct uart_hsu_port, port);
742 unsigned long flags;
743
744 spin_lock_irqsave(&up->port.lock, flags);
745 if (break_state == -1)
746 up->lcr |= UART_LCR_SBC;
747 else
748 up->lcr &= ~UART_LCR_SBC;
749 serial_out(up, UART_LCR, up->lcr);
750 spin_unlock_irqrestore(&up->port.lock, flags);
751}
752
753/*
754 * What special to do:
755 * 1. chose the 64B fifo mode
Feng Tang085a4f72011-02-22 15:28:10 +0800756 * 2. start dma or pio depends on configuration
757 * 3. we only allocate dma memory when needed
Feng Tangd843fc62010-07-27 08:20:22 +0100758 */
759static int serial_hsu_startup(struct uart_port *port)
760{
761 struct uart_hsu_port *up =
762 container_of(port, struct uart_hsu_port, port);
763 unsigned long flags;
764
Kristen Carlson Accardi88e51732011-08-26 11:35:16 +0100765 pm_runtime_get_sync(up->dev);
766
Feng Tangd843fc62010-07-27 08:20:22 +0100767 /*
768 * Clear the FIFO buffers and disable them.
769 * (they will be reenabled in set_termios())
770 */
771 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
772 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
773 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
774 serial_out(up, UART_FCR, 0);
775
776 /* Clear the interrupt registers. */
777 (void) serial_in(up, UART_LSR);
778 (void) serial_in(up, UART_RX);
779 (void) serial_in(up, UART_IIR);
780 (void) serial_in(up, UART_MSR);
781
782 /* Now, initialize the UART, default is 8n1 */
783 serial_out(up, UART_LCR, UART_LCR_WLEN8);
784
785 spin_lock_irqsave(&up->port.lock, flags);
786
787 up->port.mctrl |= TIOCM_OUT2;
788 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
789
790 /*
791 * Finally, enable interrupts. Note: Modem status interrupts
792 * are set via set_termios(), which will be occurring imminently
793 * anyway, so we don't enable them here.
794 */
795 if (!up->use_dma)
796 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
797 else
798 up->ier = 0;
799 serial_out(up, UART_IER, up->ier);
800
801 spin_unlock_irqrestore(&up->port.lock, flags);
802
803 /* DMA init */
Feng Tangd843fc62010-07-27 08:20:22 +0100804 if (up->use_dma) {
805 struct hsu_dma_buffer *dbuf;
806 struct circ_buf *xmit = &port->state->xmit;
807
808 up->dma_tx_on = 0;
809
810 /* First allocate the RX buffer */
811 dbuf = &up->rxbuf;
812 dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
813 if (!dbuf->buf) {
814 up->use_dma = 0;
815 goto exit;
816 }
817 dbuf->dma_addr = dma_map_single(port->dev,
818 dbuf->buf,
819 HSU_DMA_BUF_SIZE,
820 DMA_FROM_DEVICE);
821 dbuf->dma_size = HSU_DMA_BUF_SIZE;
822
823 /* Start the RX channel right now */
824 hsu_dma_start_rx_chan(up->rxc, dbuf);
825
826 /* Next init the TX DMA */
827 dbuf = &up->txbuf;
828 dbuf->buf = xmit->buf;
829 dbuf->dma_addr = dma_map_single(port->dev,
830 dbuf->buf,
831 UART_XMIT_SIZE,
832 DMA_TO_DEVICE);
833 dbuf->dma_size = UART_XMIT_SIZE;
834
835 /* This should not be changed all around */
836 chan_writel(up->txc, HSU_CH_BSR, 32);
837 chan_writel(up->txc, HSU_CH_MOTSR, 4);
838 dbuf->ofs = 0;
839 }
840
841exit:
842 /* And clear the interrupt registers again for luck. */
843 (void) serial_in(up, UART_LSR);
844 (void) serial_in(up, UART_RX);
845 (void) serial_in(up, UART_IIR);
846 (void) serial_in(up, UART_MSR);
847
848 up->running = 1;
849 return 0;
850}
851
852static void serial_hsu_shutdown(struct uart_port *port)
853{
854 struct uart_hsu_port *up =
855 container_of(port, struct uart_hsu_port, port);
856 unsigned long flags;
857
858 /* Disable interrupts from this port */
859 up->ier = 0;
860 serial_out(up, UART_IER, 0);
861 up->running = 0;
862
863 spin_lock_irqsave(&up->port.lock, flags);
864 up->port.mctrl &= ~TIOCM_OUT2;
865 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
866 spin_unlock_irqrestore(&up->port.lock, flags);
867
868 /* Disable break condition and FIFOs */
869 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
870 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
871 UART_FCR_CLEAR_RCVR |
872 UART_FCR_CLEAR_XMIT);
873 serial_out(up, UART_FCR, 0);
Kristen Carlson Accardi88e51732011-08-26 11:35:16 +0100874
875 pm_runtime_put(up->dev);
Feng Tangd843fc62010-07-27 08:20:22 +0100876}
877
878static void
879serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
880 struct ktermios *old)
881{
882 struct uart_hsu_port *up =
883 container_of(port, struct uart_hsu_port, port);
Feng Tangd843fc62010-07-27 08:20:22 +0100884 unsigned char cval, fcr = 0;
885 unsigned long flags;
886 unsigned int baud, quot;
Feng Tanga5880a92010-11-19 11:01:48 +0800887 u32 ps, mul;
Feng Tangd843fc62010-07-27 08:20:22 +0100888
889 switch (termios->c_cflag & CSIZE) {
890 case CS5:
891 cval = UART_LCR_WLEN5;
892 break;
893 case CS6:
894 cval = UART_LCR_WLEN6;
895 break;
896 case CS7:
897 cval = UART_LCR_WLEN7;
898 break;
899 default:
900 case CS8:
901 cval = UART_LCR_WLEN8;
902 break;
903 }
904
905 /* CMSPAR isn't supported by this driver */
Alan Cox604fdb72011-11-10 13:17:55 +0000906 termios->c_cflag &= ~CMSPAR;
Feng Tangd843fc62010-07-27 08:20:22 +0100907
908 if (termios->c_cflag & CSTOPB)
909 cval |= UART_LCR_STOP;
910 if (termios->c_cflag & PARENB)
911 cval |= UART_LCR_PARITY;
912 if (!(termios->c_cflag & PARODD))
913 cval |= UART_LCR_EPAR;
914
915 /*
Feng Tange5586ec2010-10-14 17:47:35 +0800916 * The base clk is 50Mhz, and the baud rate come from:
917 * baud = 50M * MUL / (DIV * PS * DLAB)
918 *
Feng Tangd843fc62010-07-27 08:20:22 +0100919 * For those basic low baud rate we can get the direct
Feng Tange5586ec2010-10-14 17:47:35 +0800920 * scalar from 2746800, like 115200 = 2746800/24. For those
921 * higher baud rate, we handle them case by case, mainly by
922 * adjusting the MUL/PS registers, and DIV register is kept
923 * as default value 0x3d09 to make things simple
Feng Tangd843fc62010-07-27 08:20:22 +0100924 */
925 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
Feng Tangd843fc62010-07-27 08:20:22 +0100926
Feng Tange5586ec2010-10-14 17:47:35 +0800927 quot = 1;
Feng Tanga5880a92010-11-19 11:01:48 +0800928 ps = 0x10;
929 mul = 0x3600;
Feng Tangd843fc62010-07-27 08:20:22 +0100930 switch (baud) {
931 case 3500000:
932 mul = 0x3345;
933 ps = 0xC;
Feng Tange5586ec2010-10-14 17:47:35 +0800934 break;
Feng Tange5586ec2010-10-14 17:47:35 +0800935 case 1843200:
Feng Tangd843fc62010-07-27 08:20:22 +0100936 mul = 0x2400;
Feng Tangd843fc62010-07-27 08:20:22 +0100937 break;
Feng Tanga5880a92010-11-19 11:01:48 +0800938 case 3000000:
939 case 2500000:
940 case 2000000:
Feng Tangd843fc62010-07-27 08:20:22 +0100941 case 1500000:
Feng Tange5586ec2010-10-14 17:47:35 +0800942 case 1000000:
Feng Tange5586ec2010-10-14 17:47:35 +0800943 case 500000:
Feng Tanga5880a92010-11-19 11:01:48 +0800944 /* mul/ps/quot = 0x9C4/0x10/0x1 will make a 500000 bps */
945 mul = baud / 500000 * 0x9C4;
Feng Tangd843fc62010-07-27 08:20:22 +0100946 break;
947 default:
Feng Tange5586ec2010-10-14 17:47:35 +0800948 /* Use uart_get_divisor to get quot for other baud rates */
949 quot = 0;
Feng Tangd843fc62010-07-27 08:20:22 +0100950 }
951
Feng Tange5586ec2010-10-14 17:47:35 +0800952 if (!quot)
953 quot = uart_get_divisor(port, baud);
954
Feng Tangd843fc62010-07-27 08:20:22 +0100955 if ((up->port.uartclk / quot) < (2400 * 16))
956 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
957 else if ((up->port.uartclk / quot) < (230400 * 16))
958 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
959 else
960 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
961
962 fcr |= UART_FCR_HSU_64B_FIFO;
Feng Tangd843fc62010-07-27 08:20:22 +0100963
964 /*
965 * Ok, we're now changing the port state. Do it with
966 * interrupts disabled.
967 */
968 spin_lock_irqsave(&up->port.lock, flags);
969
970 /* Update the per-port timeout */
971 uart_update_timeout(port, termios->c_cflag, baud);
972
973 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
974 if (termios->c_iflag & INPCK)
975 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
976 if (termios->c_iflag & (BRKINT | PARMRK))
977 up->port.read_status_mask |= UART_LSR_BI;
978
979 /* Characters to ignore */
980 up->port.ignore_status_mask = 0;
981 if (termios->c_iflag & IGNPAR)
982 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
983 if (termios->c_iflag & IGNBRK) {
984 up->port.ignore_status_mask |= UART_LSR_BI;
985 /*
986 * If we're ignoring parity and break indicators,
987 * ignore overruns too (for real raw support).
988 */
989 if (termios->c_iflag & IGNPAR)
990 up->port.ignore_status_mask |= UART_LSR_OE;
991 }
992
993 /* Ignore all characters if CREAD is not set */
994 if ((termios->c_cflag & CREAD) == 0)
995 up->port.ignore_status_mask |= UART_LSR_DR;
996
997 /*
998 * CTS flow control flag and modem status interrupts, disable
999 * MSI by default
1000 */
1001 up->ier &= ~UART_IER_MSI;
1002 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1003 up->ier |= UART_IER_MSI;
1004
1005 serial_out(up, UART_IER, up->ier);
1006
1007 if (termios->c_cflag & CRTSCTS)
1008 up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
1009 else
1010 up->mcr &= ~UART_MCR_AFE;
1011
1012 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1013 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
1014 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
1015 serial_out(up, UART_LCR, cval); /* reset DLAB */
1016 serial_out(up, UART_MUL, mul); /* set MUL */
1017 serial_out(up, UART_PS, ps); /* set PS */
1018 up->lcr = cval; /* Save LCR */
1019 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
1020 serial_out(up, UART_FCR, fcr);
1021 spin_unlock_irqrestore(&up->port.lock, flags);
1022}
1023
1024static void
1025serial_hsu_pm(struct uart_port *port, unsigned int state,
1026 unsigned int oldstate)
1027{
1028}
1029
1030static void serial_hsu_release_port(struct uart_port *port)
1031{
1032}
1033
1034static int serial_hsu_request_port(struct uart_port *port)
1035{
1036 return 0;
1037}
1038
1039static void serial_hsu_config_port(struct uart_port *port, int flags)
1040{
Feng Tangd843fc62010-07-27 08:20:22 +01001041 struct uart_hsu_port *up =
1042 container_of(port, struct uart_hsu_port, port);
1043 up->port.type = PORT_MFD;
Feng Tangd843fc62010-07-27 08:20:22 +01001044}
1045
1046static int
1047serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
1048{
1049 /* We don't want the core code to modify any port params */
1050 return -EINVAL;
1051}
1052
1053static const char *
1054serial_hsu_type(struct uart_port *port)
1055{
1056 struct uart_hsu_port *up =
1057 container_of(port, struct uart_hsu_port, port);
1058 return up->name;
1059}
1060
1061/* Mainly for uart console use */
1062static struct uart_hsu_port *serial_hsu_ports[3];
1063static struct uart_driver serial_hsu_reg;
1064
1065#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1066
1067#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1068
1069/* Wait for transmitter & holding register to empty */
1070static inline void wait_for_xmitr(struct uart_hsu_port *up)
1071{
1072 unsigned int status, tmout = 1000;
1073
1074 /* Wait up to 1ms for the character to be sent. */
1075 do {
1076 status = serial_in(up, UART_LSR);
1077
1078 if (status & UART_LSR_BI)
1079 up->lsr_break_flag = UART_LSR_BI;
1080
1081 if (--tmout == 0)
1082 break;
1083 udelay(1);
1084 } while (!(status & BOTH_EMPTY));
1085
1086 /* Wait up to 1s for flow control if necessary */
1087 if (up->port.flags & UPF_CONS_FLOW) {
1088 tmout = 1000000;
1089 while (--tmout &&
1090 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1091 udelay(1);
1092 }
1093}
1094
1095static void serial_hsu_console_putchar(struct uart_port *port, int ch)
1096{
1097 struct uart_hsu_port *up =
1098 container_of(port, struct uart_hsu_port, port);
1099
1100 wait_for_xmitr(up);
1101 serial_out(up, UART_TX, ch);
1102}
1103
1104/*
1105 * Print a string to the serial port trying not to disturb
1106 * any possible real use of the port...
1107 *
1108 * The console_lock must be held when we get here.
1109 */
1110static void
1111serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
1112{
1113 struct uart_hsu_port *up = serial_hsu_ports[co->index];
1114 unsigned long flags;
1115 unsigned int ier;
1116 int locked = 1;
1117
Feng Tang50827fb2012-11-15 16:03:16 +08001118 touch_nmi_watchdog();
1119
Feng Tangd843fc62010-07-27 08:20:22 +01001120 local_irq_save(flags);
1121 if (up->port.sysrq)
1122 locked = 0;
1123 else if (oops_in_progress) {
1124 locked = spin_trylock(&up->port.lock);
1125 } else
1126 spin_lock(&up->port.lock);
1127
1128 /* First save the IER then disable the interrupts */
1129 ier = serial_in(up, UART_IER);
1130 serial_out(up, UART_IER, 0);
1131
1132 uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
1133
1134 /*
1135 * Finally, wait for transmitter to become empty
1136 * and restore the IER
1137 */
1138 wait_for_xmitr(up);
1139 serial_out(up, UART_IER, ier);
1140
1141 if (locked)
1142 spin_unlock(&up->port.lock);
1143 local_irq_restore(flags);
1144}
1145
1146static struct console serial_hsu_console;
1147
1148static int __init
1149serial_hsu_console_setup(struct console *co, char *options)
1150{
1151 struct uart_hsu_port *up;
1152 int baud = 115200;
1153 int bits = 8;
1154 int parity = 'n';
1155 int flow = 'n';
Feng Tangd843fc62010-07-27 08:20:22 +01001156
1157 if (co->index == -1 || co->index >= serial_hsu_reg.nr)
1158 co->index = 0;
1159 up = serial_hsu_ports[co->index];
1160 if (!up)
1161 return -ENODEV;
1162
1163 if (options)
1164 uart_parse_options(options, &baud, &parity, &bits, &flow);
1165
Mika Westerbergb82e3242011-11-10 13:18:09 +00001166 return uart_set_options(&up->port, co, baud, parity, bits, flow);
Feng Tangd843fc62010-07-27 08:20:22 +01001167}
1168
1169static struct console serial_hsu_console = {
1170 .name = "ttyMFD",
1171 .write = serial_hsu_console_write,
1172 .device = uart_console_device,
1173 .setup = serial_hsu_console_setup,
1174 .flags = CON_PRINTBUFFER,
Mika Westerbergb82e3242011-11-10 13:18:09 +00001175 .index = -1,
Feng Tangd843fc62010-07-27 08:20:22 +01001176 .data = &serial_hsu_reg,
1177};
Mika Westerbergb82e3242011-11-10 13:18:09 +00001178
1179#define SERIAL_HSU_CONSOLE (&serial_hsu_console)
1180#else
1181#define SERIAL_HSU_CONSOLE NULL
Feng Tangd843fc62010-07-27 08:20:22 +01001182#endif
1183
1184struct uart_ops serial_hsu_pops = {
1185 .tx_empty = serial_hsu_tx_empty,
1186 .set_mctrl = serial_hsu_set_mctrl,
1187 .get_mctrl = serial_hsu_get_mctrl,
1188 .stop_tx = serial_hsu_stop_tx,
1189 .start_tx = serial_hsu_start_tx,
1190 .stop_rx = serial_hsu_stop_rx,
1191 .enable_ms = serial_hsu_enable_ms,
1192 .break_ctl = serial_hsu_break_ctl,
1193 .startup = serial_hsu_startup,
1194 .shutdown = serial_hsu_shutdown,
1195 .set_termios = serial_hsu_set_termios,
1196 .pm = serial_hsu_pm,
1197 .type = serial_hsu_type,
1198 .release_port = serial_hsu_release_port,
1199 .request_port = serial_hsu_request_port,
1200 .config_port = serial_hsu_config_port,
1201 .verify_port = serial_hsu_verify_port,
1202};
1203
1204static struct uart_driver serial_hsu_reg = {
1205 .owner = THIS_MODULE,
1206 .driver_name = "MFD serial",
1207 .dev_name = "ttyMFD",
1208 .major = TTY_MAJOR,
1209 .minor = 128,
1210 .nr = 3,
Mika Westerbergb82e3242011-11-10 13:18:09 +00001211 .cons = SERIAL_HSU_CONSOLE,
Feng Tangd843fc62010-07-27 08:20:22 +01001212};
1213
1214#ifdef CONFIG_PM
1215static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
1216{
Feng Tang3c4108c2010-07-27 08:20:52 +01001217 void *priv = pci_get_drvdata(pdev);
Feng Tangd843fc62010-07-27 08:20:22 +01001218 struct uart_hsu_port *up;
1219
Feng Tang3c4108c2010-07-27 08:20:52 +01001220 /* Make sure this is not the internal dma controller */
1221 if (priv && (pdev->device != 0x081E)) {
1222 up = priv;
1223 uart_suspend_port(&serial_hsu_reg, &up->port);
1224 }
Feng Tangd843fc62010-07-27 08:20:22 +01001225
Feng Tang3c4108c2010-07-27 08:20:52 +01001226 pci_save_state(pdev);
1227 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Feng Tangd843fc62010-07-27 08:20:22 +01001228 return 0;
1229}
1230
1231static int serial_hsu_resume(struct pci_dev *pdev)
1232{
Feng Tang3c4108c2010-07-27 08:20:52 +01001233 void *priv = pci_get_drvdata(pdev);
Feng Tangd843fc62010-07-27 08:20:22 +01001234 struct uart_hsu_port *up;
Feng Tang3c4108c2010-07-27 08:20:52 +01001235 int ret;
Feng Tangd843fc62010-07-27 08:20:22 +01001236
Feng Tang3c4108c2010-07-27 08:20:52 +01001237 pci_set_power_state(pdev, PCI_D0);
1238 pci_restore_state(pdev);
1239
1240 ret = pci_enable_device(pdev);
1241 if (ret)
1242 dev_warn(&pdev->dev,
1243 "HSU: can't re-enable device, try to continue\n");
1244
1245 if (priv && (pdev->device != 0x081E)) {
1246 up = priv;
1247 uart_resume_port(&serial_hsu_reg, &up->port);
1248 }
Feng Tangd843fc62010-07-27 08:20:22 +01001249 return 0;
1250}
1251#else
1252#define serial_hsu_suspend NULL
1253#define serial_hsu_resume NULL
1254#endif
1255
Kristen Carlson Accardi88e51732011-08-26 11:35:16 +01001256#ifdef CONFIG_PM_RUNTIME
1257static int serial_hsu_runtime_idle(struct device *dev)
1258{
1259 int err;
1260
1261 err = pm_schedule_suspend(dev, 500);
1262 if (err)
1263 return -EBUSY;
1264
1265 return 0;
1266}
1267
1268static int serial_hsu_runtime_suspend(struct device *dev)
1269{
1270 return 0;
1271}
1272
1273static int serial_hsu_runtime_resume(struct device *dev)
1274{
1275 return 0;
1276}
1277#else
1278#define serial_hsu_runtime_idle NULL
1279#define serial_hsu_runtime_suspend NULL
1280#define serial_hsu_runtime_resume NULL
1281#endif
1282
1283static const struct dev_pm_ops serial_hsu_pm_ops = {
1284 .runtime_suspend = serial_hsu_runtime_suspend,
1285 .runtime_resume = serial_hsu_runtime_resume,
1286 .runtime_idle = serial_hsu_runtime_idle,
1287};
1288
Feng Tangd843fc62010-07-27 08:20:22 +01001289/* temp global pointer before we settle down on using one or four PCI dev */
1290static struct hsu_port *phsu;
1291
1292static int serial_hsu_probe(struct pci_dev *pdev,
1293 const struct pci_device_id *ent)
1294{
1295 struct uart_hsu_port *uport;
1296 int index, ret;
1297
1298 printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
1299 pdev->vendor, pdev->device);
1300
1301 switch (pdev->device) {
1302 case 0x081B:
1303 index = 0;
1304 break;
1305 case 0x081C:
1306 index = 1;
1307 break;
1308 case 0x081D:
1309 index = 2;
1310 break;
1311 case 0x081E:
1312 /* internal DMA controller */
1313 index = 3;
1314 break;
1315 default:
1316 dev_err(&pdev->dev, "HSU: out of index!");
1317 return -ENODEV;
1318 }
1319
1320 ret = pci_enable_device(pdev);
1321 if (ret)
1322 return ret;
1323
1324 if (index == 3) {
1325 /* DMA controller */
1326 ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
1327 if (ret) {
1328 dev_err(&pdev->dev, "can not get IRQ\n");
1329 goto err_disable;
1330 }
1331 pci_set_drvdata(pdev, phsu);
1332 } else {
1333 /* UART port 0~2 */
1334 uport = &phsu->port[index];
1335 uport->port.irq = pdev->irq;
1336 uport->port.dev = &pdev->dev;
1337 uport->dev = &pdev->dev;
1338
1339 ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
1340 if (ret) {
1341 dev_err(&pdev->dev, "can not get IRQ\n");
1342 goto err_disable;
1343 }
1344 uart_add_one_port(&serial_hsu_reg, &uport->port);
1345
Feng Tangd843fc62010-07-27 08:20:22 +01001346 pci_set_drvdata(pdev, uport);
1347 }
1348
Kristen Carlson Accardi88e51732011-08-26 11:35:16 +01001349 pm_runtime_put_noidle(&pdev->dev);
1350 pm_runtime_allow(&pdev->dev);
1351
Feng Tangd843fc62010-07-27 08:20:22 +01001352 return 0;
1353
1354err_disable:
1355 pci_disable_device(pdev);
1356 return ret;
1357}
1358
1359static void hsu_global_init(void)
1360{
1361 struct hsu_port *hsu;
1362 struct uart_hsu_port *uport;
1363 struct hsu_dma_chan *dchan;
1364 int i, ret;
1365
1366 hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
1367 if (!hsu)
1368 return;
1369
1370 /* Get basic io resource and map it */
1371 hsu->paddr = 0xffa28000;
1372 hsu->iolen = 0x1000;
1373
1374 if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
1375 pr_warning("HSU: error in request mem region\n");
1376
1377 hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
1378 if (!hsu->reg) {
1379 pr_err("HSU: error in ioremap\n");
1380 ret = -ENOMEM;
1381 goto err_free_region;
1382 }
1383
1384 /* Initialise the 3 UART ports */
1385 uport = hsu->port;
1386 for (i = 0; i < 3; i++) {
1387 uport->port.type = PORT_MFD;
1388 uport->port.iotype = UPIO_MEM;
1389 uport->port.mapbase = (resource_size_t)hsu->paddr
1390 + HSU_PORT_REG_OFFSET
1391 + i * HSU_PORT_REG_LENGTH;
1392 uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
1393 + i * HSU_PORT_REG_LENGTH;
1394
1395 sprintf(uport->name, "hsu_port%d", i);
1396 uport->port.fifosize = 64;
1397 uport->port.ops = &serial_hsu_pops;
1398 uport->port.line = i;
1399 uport->port.flags = UPF_IOREMAP;
Feng Tang06c77e22010-07-27 08:20:42 +01001400 /* set the scalable maxim support rate to 2746800 bps */
Feng Tangd843fc62010-07-27 08:20:22 +01001401 uport->port.uartclk = 115200 * 24 * 16;
1402
1403 uport->running = 0;
1404 uport->txc = &hsu->chans[i * 2];
1405 uport->rxc = &hsu->chans[i * 2 + 1];
1406
1407 serial_hsu_ports[i] = uport;
1408 uport->index = i;
Feng Tangf023eab2011-02-22 15:28:12 +08001409
1410 if (hsu_dma_enable & (1<<i))
1411 uport->use_dma = 1;
1412 else
1413 uport->use_dma = 0;
1414
Feng Tangd843fc62010-07-27 08:20:22 +01001415 uport++;
1416 }
1417
1418 /* Initialise 6 dma channels */
1419 dchan = hsu->chans;
1420 for (i = 0; i < 6; i++) {
1421 dchan->id = i;
1422 dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1423 dchan->uport = &hsu->port[i/2];
1424 dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
1425 i * HSU_DMA_CHANS_REG_LENGTH;
Feng Tang669b7a02010-07-27 08:20:32 +01001426
Feng Tangd843fc62010-07-27 08:20:22 +01001427 dchan++;
1428 }
1429
1430 phsu = hsu;
Feng Tangd843fc62010-07-27 08:20:22 +01001431 hsu_debugfs_init(hsu);
1432 return;
1433
1434err_free_region:
1435 release_mem_region(hsu->paddr, hsu->iolen);
1436 kfree(hsu);
1437 return;
1438}
1439
1440static void serial_hsu_remove(struct pci_dev *pdev)
1441{
Feng Tange3671ac2010-09-06 13:41:02 +01001442 void *priv = pci_get_drvdata(pdev);
1443 struct uart_hsu_port *up;
Feng Tangd843fc62010-07-27 08:20:22 +01001444
Feng Tange3671ac2010-09-06 13:41:02 +01001445 if (!priv)
Feng Tangd843fc62010-07-27 08:20:22 +01001446 return;
1447
Kristen Carlson Accardi88e51732011-08-26 11:35:16 +01001448 pm_runtime_forbid(&pdev->dev);
1449 pm_runtime_get_noresume(&pdev->dev);
1450
Feng Tange3671ac2010-09-06 13:41:02 +01001451 /* For port 0/1/2, priv is the address of uart_hsu_port */
1452 if (pdev->device != 0x081E) {
1453 up = priv;
1454 uart_remove_one_port(&serial_hsu_reg, &up->port);
1455 }
Feng Tangd843fc62010-07-27 08:20:22 +01001456
1457 pci_set_drvdata(pdev, NULL);
Feng Tange3671ac2010-09-06 13:41:02 +01001458 free_irq(pdev->irq, priv);
Feng Tangd843fc62010-07-27 08:20:22 +01001459 pci_disable_device(pdev);
1460}
1461
1462/* First 3 are UART ports, and the 4th is the DMA */
Bill Pemberton512f82a2012-11-19 13:25:19 -05001463static const struct pci_device_id pci_ids[] = {
Feng Tangd843fc62010-07-27 08:20:22 +01001464 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
1465 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
1466 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
1467 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
1468 {},
1469};
1470
1471static struct pci_driver hsu_pci_driver = {
1472 .name = "HSU serial",
1473 .id_table = pci_ids,
1474 .probe = serial_hsu_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001475 .remove = serial_hsu_remove,
Feng Tangd843fc62010-07-27 08:20:22 +01001476 .suspend = serial_hsu_suspend,
1477 .resume = serial_hsu_resume,
Kristen Carlson Accardi88e51732011-08-26 11:35:16 +01001478 .driver = {
1479 .pm = &serial_hsu_pm_ops,
1480 },
Feng Tangd843fc62010-07-27 08:20:22 +01001481};
1482
1483static int __init hsu_pci_init(void)
1484{
1485 int ret;
1486
1487 hsu_global_init();
1488
1489 ret = uart_register_driver(&serial_hsu_reg);
1490 if (ret)
1491 return ret;
1492
1493 return pci_register_driver(&hsu_pci_driver);
1494}
1495
1496static void __exit hsu_pci_exit(void)
1497{
1498 pci_unregister_driver(&hsu_pci_driver);
1499 uart_unregister_driver(&serial_hsu_reg);
1500
1501 hsu_debugfs_remove(phsu);
1502
1503 kfree(phsu);
1504}
1505
1506module_init(hsu_pci_init);
1507module_exit(hsu_pci_exit);
1508
1509MODULE_LICENSE("GPL v2");
1510MODULE_ALIAS("platform:medfield-hsu");