24 #include <linux/module.h>
28 #include <linux/slab.h>
33 #include <linux/tty.h>
35 #include <linux/serial_core.h>
38 #include <linux/pci.h>
43 #define HSU_DMA_BUF_SIZE 2048
45 #define chan_readl(chan, offset) readl(chan->reg + offset)
46 #define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
48 #define mfd_readl(obj, offset) readl(obj->reg + offset)
49 #define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
51 static int hsu_dma_enable;
54 "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.");
121 unsigned char val = value & 0xff;
126 #ifdef CONFIG_DEBUG_FS
128 #define HSU_REGS_BUFSIZE 1024
131 static ssize_t port_show_regs(
struct file *
file,
char __user *user_buf,
132 size_t count, loff_t *ppos)
143 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
144 "MFD HSU port[%d] regs:\n", up->
index);
146 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
147 "=================================\n");
148 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
149 "IER: \t\t0x%08x\n", serial_in(up,
UART_IER));
150 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
151 "IIR: \t\t0x%08x\n", serial_in(up,
UART_IIR));
152 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
153 "LCR: \t\t0x%08x\n", serial_in(up,
UART_LCR));
154 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
155 "MCR: \t\t0x%08x\n", serial_in(up,
UART_MCR));
156 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
157 "LSR: \t\t0x%08x\n", serial_in(up,
UART_LSR));
158 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
159 "MSR: \t\t0x%08x\n", serial_in(up,
UART_MSR));
160 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
161 "FOR: \t\t0x%08x\n", serial_in(up,
UART_FOR));
162 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
163 "PS: \t\t0x%08x\n", serial_in(up,
UART_PS));
164 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
165 "MUL: \t\t0x%08x\n", serial_in(up,
UART_MUL));
166 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
167 "DIV: \t\t0x%08x\n", serial_in(up,
UART_DIV));
169 if (len > HSU_REGS_BUFSIZE)
170 len = HSU_REGS_BUFSIZE;
177 static ssize_t dma_show_regs(
struct file *file,
char __user *user_buf,
178 size_t count, loff_t *ppos)
189 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
190 "MFD HSU DMA channel [%d] regs:\n", chan->
id);
192 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
193 "=================================\n");
194 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
196 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
198 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
200 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
202 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
204 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
206 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
208 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
210 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
212 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
214 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
216 len +=
snprintf(buf + len, HSU_REGS_BUFSIZE - len,
219 if (len > HSU_REGS_BUFSIZE)
220 len = HSU_REGS_BUFSIZE;
230 .read = port_show_regs,
237 .read = dma_show_regs,
241 static int hsu_debugfs_init(
struct hsu_port *hsu)
250 for (i = 0; i < 3; i++) {
251 snprintf(name,
sizeof(name),
"port_%d_regs", i);
253 hsu->
debugfs, (
void *)(&hsu->
port[i]), &port_regs_ops);
256 for (i = 0; i < 6; i++) {
257 snprintf(name,
sizeof(name),
"dma_chan_%d_regs", i);
265 static void hsu_debugfs_remove(
struct hsu_port *hsu)
272 static inline int hsu_debugfs_init(
struct hsu_port *hsu)
277 static inline void hsu_debugfs_remove(
struct hsu_port *hsu)
305 up->
port.icount.tx += dbuf->
ofs;
368 static void serial_hsu_stop_tx(
struct uart_port *port)
425 tty_insert_flip_string(tty, dbuf->
buf, count);
445 static void serial_hsu_stop_rx(
struct uart_port *port)
463 unsigned int ch,
flag;
472 up->
port.icount.rx++;
477 dev_warn(up->
dev,
"We really rush into ERR/BI case"
478 "status = 0x%02x", *status);
482 up->
port.icount.brk++;
489 if (uart_handle_break(&up->
port))
492 up->
port.icount.parity++;
494 up->
port.icount.frame++;
496 up->
port.icount.overrun++;
499 *status &= up->
port.read_status_mask;
501 #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
503 up->
port.cons->index == up->
port.line) {
509 if (*status & UART_LSR_BI) {
511 }
else if (*status & UART_LSR_PE)
513 else if (*status & UART_LSR_FE)
532 if (up->
port.x_char) {
534 up->
port.icount.tx++;
539 serial_hsu_stop_tx(&up->
port);
544 count = up->
port.fifosize / 2;
550 up->
port.icount.tx++;
553 }
while (--count > 0);
559 serial_hsu_stop_tx(&up->
port);
562 static inline void check_modem_status(
struct uart_hsu_port *up)
572 up->
port.icount.rng++;
574 up->
port.icount.dsr++;
591 unsigned int iir, lsr;
600 if (
unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
601 UART_LSR_FE | UART_LSR_OE)))
603 "Got lsr irq while using DMA, lsr = 0x%2x\n",
605 check_modem_status(up);
606 spin_unlock_irqrestore(&up->
port.lock, flags);
612 spin_unlock_irqrestore(&up->
port.lock, flags);
618 receive_chars(up, &lsr);
619 check_modem_status(up);
625 spin_unlock_irqrestore(&up->
port.lock, flags);
629 static inline void dma_chan_irq(
struct hsu_dma_chan *chan)
658 spin_unlock_irqrestore(&up->
port.lock, flags);
670 for (i = 0; i < 6; i++) {
672 dma_chan_irq(&hsu->
chans[i]);
679 static unsigned int serial_hsu_tx_empty(
struct uart_port *port)
688 spin_unlock_irqrestore(&up->
port.lock, flags);
693 static unsigned int serial_hsu_get_mctrl(
struct uart_port *port)
714 static void serial_hsu_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
718 unsigned char mcr = 0;
736 static void serial_hsu_break_ctl(
struct uart_port *port,
int break_state)
743 if (break_state == -1)
748 spin_unlock_irqrestore(&up->
port.lock, flags);
757 static int serial_hsu_startup(
struct uart_port *port)
763 pm_runtime_get_sync(up->
dev);
786 serial_hsu_set_mctrl(&up->
port, up->
port.mctrl);
799 spin_unlock_irqrestore(&up->
port.lock, flags);
850 static void serial_hsu_shutdown(
struct uart_port *port)
862 up->
port.mctrl &= ~TIOCM_OUT2;
863 serial_hsu_set_mctrl(&up->
port, up->
port.mctrl);
864 spin_unlock_irqrestore(&up->
port.lock, flags);
873 pm_runtime_put(up->
dev);
882 unsigned char cval,
fcr = 0;
884 unsigned int baud, quot;
943 mul = baud / 500000 * 0x9C4;
953 if ((up->
port.uartclk / quot) < (2400 * 16))
955 else if ((up->
port.uartclk / quot) < (230400 * 16))
978 up->
port.ignore_status_mask = 0;
1011 serial_out(up,
UART_DLL, quot & 0xff);
1012 serial_out(up,
UART_DLM, quot >> 8);
1017 serial_hsu_set_mctrl(&up->
port, up->
port.mctrl);
1019 spin_unlock_irqrestore(&up->
port.lock, flags);
1024 unsigned int oldstate)
1028 static void serial_hsu_release_port(
struct uart_port *port)
1032 static int serial_hsu_request_port(
struct uart_port *port)
1037 static void serial_hsu_config_port(
struct uart_port *port,
int flags)
1063 #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1065 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1070 unsigned int status, tmout = 1000;
1076 if (status & UART_LSR_BI)
1093 static void serial_hsu_console_putchar(
struct uart_port *port,
int ch)
1109 serial_hsu_console_write(
struct console *co,
const char *
s,
unsigned int count)
1112 unsigned long flags;
1120 locked = spin_trylock(&up->
port.lock);
1122 spin_lock(&up->
port.lock);
1138 spin_unlock(&up->
port.lock);
1142 static struct console serial_hsu_console;
1153 if (co->
index == -1 || co->
index >= serial_hsu_reg.nr)
1155 up = serial_hsu_ports[co->
index];
1165 static struct console serial_hsu_console = {
1167 .write = serial_hsu_console_write,
1169 .setup = serial_hsu_console_setup,
1172 .data = &serial_hsu_reg,
1175 #define SERIAL_HSU_CONSOLE (&serial_hsu_console)
1177 #define SERIAL_HSU_CONSOLE NULL
1181 .tx_empty = serial_hsu_tx_empty,
1182 .set_mctrl = serial_hsu_set_mctrl,
1183 .get_mctrl = serial_hsu_get_mctrl,
1184 .stop_tx = serial_hsu_stop_tx,
1185 .start_tx = serial_hsu_start_tx,
1186 .stop_rx = serial_hsu_stop_rx,
1187 .enable_ms = serial_hsu_enable_ms,
1188 .break_ctl = serial_hsu_break_ctl,
1189 .startup = serial_hsu_startup,
1190 .shutdown = serial_hsu_shutdown,
1191 .set_termios = serial_hsu_set_termios,
1192 .pm = serial_hsu_pm,
1193 .type = serial_hsu_type,
1194 .release_port = serial_hsu_release_port,
1195 .request_port = serial_hsu_request_port,
1196 .config_port = serial_hsu_config_port,
1197 .verify_port = serial_hsu_verify_port,
1202 .driver_name =
"MFD serial",
1203 .dev_name =
"ttyMFD",
1213 void *
priv = pci_get_drvdata(pdev);
1217 if (priv && (pdev->
device != 0x081E)) {
1229 void *priv = pci_get_drvdata(pdev);
1239 "HSU: can't re-enable device, try to continue\n");
1241 if (priv && (pdev->
device != 0x081E)) {
1248 #define serial_hsu_suspend NULL
1249 #define serial_hsu_resume NULL
1252 #ifdef CONFIG_PM_RUNTIME
1274 #define serial_hsu_runtime_idle NULL
1275 #define serial_hsu_runtime_suspend NULL
1276 #define serial_hsu_runtime_resume NULL
1279 static const struct dev_pm_ops serial_hsu_pm_ops = {
1288 static int serial_hsu_probe(
struct pci_dev *pdev,
1327 pci_set_drvdata(pdev, phsu);
1342 pci_set_drvdata(pdev, uport);
1345 pm_runtime_put_noidle(&pdev->
dev);
1355 static void hsu_global_init(
void)
1367 hsu->
paddr = 0xffa28000;
1368 hsu->
iolen = 0x1000;
1371 pr_warning(
"HSU: error in request mem region\n");
1375 pr_err(
"HSU: error in ioremap\n");
1377 goto err_free_region;
1382 for (i = 0; i < 3; i++) {
1392 uport->
port.fifosize = 64;
1394 uport->
port.line =
i;
1397 uport->
port.uartclk = 115200 * 24 * 16;
1401 uport->
rxc = &hsu->
chans[i * 2 + 1];
1403 serial_hsu_ports[
i] =
uport;
1406 if (hsu_dma_enable & (1<<i))
1416 for (i = 0; i < 6; i++) {
1427 hsu_debugfs_init(hsu);
1436 static void serial_hsu_remove(
struct pci_dev *pdev)
1438 void *priv = pci_get_drvdata(pdev);
1445 pm_runtime_get_noresume(&pdev->
dev);
1448 if (pdev->
device != 0x081E) {
1453 pci_set_drvdata(pdev,
NULL);
1468 .name =
"HSU serial",
1469 .id_table = pci_ids,
1470 .probe = serial_hsu_probe,
1475 .pm = &serial_hsu_pm_ops,
1479 static int __init hsu_pci_init(
void)
1489 return pci_register_driver(&hsu_pci_driver);
1492 static void __exit hsu_pci_exit(
void)
1497 hsu_debugfs_remove(phsu);