123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- #if defined(CONFIG_SERIAL_ARC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
- #define SUPPORT_SYSRQ
- #endif
- #include <linux/module.h>
- #include <linux/serial.h>
- #include <linux/console.h>
- #include <linux/sysrq.h>
- #include <linux/platform_device.h>
- #include <linux/tty.h>
- #include <linux/tty_flip.h>
- #include <linux/serial_core.h>
- #include <linux/io.h>
- #include <linux/of_irq.h>
- #include <linux/of_address.h>
- #define ARC_UART_TX_FIFO_SIZE 1
- #define R_ID0 0
- #define R_ID1 4
- #define R_ID2 8
- #define R_ID3 12
- #define R_DATA 16
- #define R_STS 20
- #define R_BAUDL 24
- #define R_BAUDH 28
- #define RXIENB 0x04
- #define TXIENB 0x40
- #define RXEMPTY 0x20
- #define TXEMPTY 0x80
- #define RXFULL 0x08
- #define RXFULL1 0x10
- #define RXFERR 0x01
- #define RXOERR 0x02
- #define RBASE(port, reg) (port->membase + reg)
- #define UART_REG_SET(u, r, v) writeb((v), RBASE(u, r))
- #define UART_REG_GET(u, r) readb(RBASE(u, r))
- #define UART_REG_OR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) | (v))
- #define UART_REG_CLR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) & ~(v))
- #define UART_SET_DATA(uart, val) UART_REG_SET(uart, R_DATA, val)
- #define UART_GET_DATA(uart) UART_REG_GET(uart, R_DATA)
- #define UART_SET_BAUDH(uart, val) UART_REG_SET(uart, R_BAUDH, val)
- #define UART_SET_BAUDL(uart, val) UART_REG_SET(uart, R_BAUDL, val)
- #define UART_CLR_STATUS(uart, val) UART_REG_CLR(uart, R_STS, val)
- #define UART_GET_STATUS(uart) UART_REG_GET(uart, R_STS)
- #define UART_ALL_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB|TXIENB)
- #define UART_RX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB)
- #define UART_TX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, TXIENB)
- #define UART_ALL_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB|TXIENB)
- #define UART_RX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB)
- #define UART_TX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, TXIENB)
- #define ARC_SERIAL_DEV_NAME "ttyARC"
- struct arc_uart_port {
- struct uart_port port;
- unsigned long baud;
- };
- #define to_arc_port(uport) container_of(uport, struct arc_uart_port, port)
- static struct arc_uart_port arc_uart_ports[CONFIG_SERIAL_ARC_NR_PORTS];
- #ifdef CONFIG_SERIAL_ARC_CONSOLE
- static struct console arc_console;
- #endif
- #define DRIVER_NAME "arc-uart"
- static struct uart_driver arc_uart_driver = {
- .owner = THIS_MODULE,
- .driver_name = DRIVER_NAME,
- .dev_name = ARC_SERIAL_DEV_NAME,
- .major = 0,
- .minor = 0,
- .nr = CONFIG_SERIAL_ARC_NR_PORTS,
- #ifdef CONFIG_SERIAL_ARC_CONSOLE
- .cons = &arc_console,
- #endif
- };
- static void arc_serial_stop_rx(struct uart_port *port)
- {
- UART_RX_IRQ_DISABLE(port);
- }
- static void arc_serial_stop_tx(struct uart_port *port)
- {
- while (!(UART_GET_STATUS(port) & TXEMPTY))
- cpu_relax();
- UART_TX_IRQ_DISABLE(port);
- }
- static unsigned int arc_serial_tx_empty(struct uart_port *port)
- {
- unsigned int stat;
- stat = UART_GET_STATUS(port);
- if (stat & TXEMPTY)
- return TIOCSER_TEMT;
- return 0;
- }
- static void arc_serial_tx_chars(struct uart_port *port)
- {
- struct circ_buf *xmit = &port->state->xmit;
- int sent = 0;
- unsigned char ch;
- if (unlikely(port->x_char)) {
- UART_SET_DATA(port, port->x_char);
- port->icount.tx++;
- port->x_char = 0;
- sent = 1;
- } else if (!uart_circ_empty(xmit)) {
- ch = xmit->buf[xmit->tail];
- xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
- port->icount.tx++;
- while (!(UART_GET_STATUS(port) & TXEMPTY))
- cpu_relax();
- UART_SET_DATA(port, ch);
- sent = 1;
- }
-
- if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
- uart_write_wakeup(port);
- if (sent)
- UART_TX_IRQ_ENABLE(port);
- }
- static void arc_serial_start_tx(struct uart_port *port)
- {
- arc_serial_tx_chars(port);
- }
- static void arc_serial_rx_chars(struct uart_port *port, unsigned int status)
- {
- unsigned int ch, flg = 0;
-
- do {
-
- if (unlikely(status & (RXOERR | RXFERR))) {
- if (status & RXOERR) {
- port->icount.overrun++;
- flg = TTY_OVERRUN;
- UART_CLR_STATUS(port, RXOERR);
- }
- if (status & RXFERR) {
- port->icount.frame++;
- flg = TTY_FRAME;
- UART_CLR_STATUS(port, RXFERR);
- }
- } else
- flg = TTY_NORMAL;
- if (status & RXEMPTY)
- continue;
- ch = UART_GET_DATA(port);
- port->icount.rx++;
- if (!(uart_handle_sysrq_char(port, ch)))
- uart_insert_char(port, status, RXOERR, ch, flg);
- spin_unlock(&port->lock);
- tty_flip_buffer_push(&port->state->port);
- spin_lock(&port->lock);
- } while (!((status = UART_GET_STATUS(port)) & RXEMPTY));
- }
- static irqreturn_t arc_serial_isr(int irq, void *dev_id)
- {
- struct uart_port *port = dev_id;
- unsigned int status;
- status = UART_GET_STATUS(port);
-
- if (status & RXIENB) {
-
- spin_lock(&port->lock);
- arc_serial_rx_chars(port, status);
- spin_unlock(&port->lock);
- }
- if ((status & TXIENB) && (status & TXEMPTY)) {
-
- UART_TX_IRQ_DISABLE(port);
- spin_lock(&port->lock);
- if (!uart_tx_stopped(port))
- arc_serial_tx_chars(port);
- spin_unlock(&port->lock);
- }
- return IRQ_HANDLED;
- }
- static unsigned int arc_serial_get_mctrl(struct uart_port *port)
- {
-
- return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
- }
- static void arc_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
- {
-
- }
- static void arc_serial_break_ctl(struct uart_port *port, int break_state)
- {
-
- }
- static int arc_serial_startup(struct uart_port *port)
- {
-
- UART_ALL_IRQ_DISABLE(port);
- if (request_irq(port->irq, arc_serial_isr, 0, "arc uart rx-tx", port)) {
- dev_warn(port->dev, "Unable to attach ARC UART intr\n");
- return -EBUSY;
- }
- UART_RX_IRQ_ENABLE(port);
- return 0;
- }
- static void arc_serial_shutdown(struct uart_port *port)
- {
- free_irq(port->irq, port);
- }
- static void
- arc_serial_set_termios(struct uart_port *port, struct ktermios *new,
- struct ktermios *old)
- {
- struct arc_uart_port *uart = to_arc_port(port);
- unsigned int baud, uartl, uarth, hw_val;
- unsigned long flags;
-
- baud = uart_get_baud_rate(port, new, old, 0, 460800);
- hw_val = port->uartclk / (uart->baud * 4) - 1;
- uartl = hw_val & 0xFF;
- uarth = (hw_val >> 8) & 0xFF;
- spin_lock_irqsave(&port->lock, flags);
- UART_ALL_IRQ_DISABLE(port);
- UART_SET_BAUDL(port, uartl);
- UART_SET_BAUDH(port, uarth);
- UART_RX_IRQ_ENABLE(port);
-
- new->c_cflag &= ~(CMSPAR|CRTSCTS|CSIZE);
- new->c_cflag |= CS8;
- if (old)
- tty_termios_copy_hw(new, old);
-
- if (tty_termios_baud_rate(new))
- tty_termios_encode_baud_rate(new, baud, baud);
- uart_update_timeout(port, new->c_cflag, baud);
- spin_unlock_irqrestore(&port->lock, flags);
- }
- static const char *arc_serial_type(struct uart_port *port)
- {
- return port->type == PORT_ARC ? DRIVER_NAME : NULL;
- }
- static void arc_serial_release_port(struct uart_port *port)
- {
- }
- static int arc_serial_request_port(struct uart_port *port)
- {
- return 0;
- }
- static int
- arc_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
- {
- if (port->type != PORT_UNKNOWN && ser->type != PORT_ARC)
- return -EINVAL;
- return 0;
- }
- static void arc_serial_config_port(struct uart_port *port, int flags)
- {
- if (flags & UART_CONFIG_TYPE)
- port->type = PORT_ARC;
- }
- #ifdef CONFIG_CONSOLE_POLL
- static void arc_serial_poll_putchar(struct uart_port *port, unsigned char chr)
- {
- while (!(UART_GET_STATUS(port) & TXEMPTY))
- cpu_relax();
- UART_SET_DATA(port, chr);
- }
- static int arc_serial_poll_getchar(struct uart_port *port)
- {
- unsigned char chr;
- while (!(UART_GET_STATUS(port) & RXEMPTY))
- cpu_relax();
- chr = UART_GET_DATA(port);
- return chr;
- }
- #endif
- static const struct uart_ops arc_serial_pops = {
- .tx_empty = arc_serial_tx_empty,
- .set_mctrl = arc_serial_set_mctrl,
- .get_mctrl = arc_serial_get_mctrl,
- .stop_tx = arc_serial_stop_tx,
- .start_tx = arc_serial_start_tx,
- .stop_rx = arc_serial_stop_rx,
- .break_ctl = arc_serial_break_ctl,
- .startup = arc_serial_startup,
- .shutdown = arc_serial_shutdown,
- .set_termios = arc_serial_set_termios,
- .type = arc_serial_type,
- .release_port = arc_serial_release_port,
- .request_port = arc_serial_request_port,
- .config_port = arc_serial_config_port,
- .verify_port = arc_serial_verify_port,
- #ifdef CONFIG_CONSOLE_POLL
- .poll_put_char = arc_serial_poll_putchar,
- .poll_get_char = arc_serial_poll_getchar,
- #endif
- };
- #ifdef CONFIG_SERIAL_ARC_CONSOLE
- static int arc_serial_console_setup(struct console *co, char *options)
- {
- struct uart_port *port;
- int baud = 115200;
- int bits = 8;
- int parity = 'n';
- int flow = 'n';
- if (co->index < 0 || co->index >= CONFIG_SERIAL_ARC_NR_PORTS)
- return -ENODEV;
-
- port = &arc_uart_ports[co->index].port;
- if (!port->membase)
- return -ENODEV;
- if (options)
- uart_parse_options(options, &baud, &parity, &bits, &flow);
-
- return uart_set_options(port, co, baud, parity, bits, flow);
- }
- static void arc_serial_console_putchar(struct uart_port *port, int ch)
- {
- while (!(UART_GET_STATUS(port) & TXEMPTY))
- cpu_relax();
- UART_SET_DATA(port, (unsigned char)ch);
- }
- static void arc_serial_console_write(struct console *co, const char *s,
- unsigned int count)
- {
- struct uart_port *port = &arc_uart_ports[co->index].port;
- unsigned long flags;
- spin_lock_irqsave(&port->lock, flags);
- uart_console_write(port, s, count, arc_serial_console_putchar);
- spin_unlock_irqrestore(&port->lock, flags);
- }
- static struct console arc_console = {
- .name = ARC_SERIAL_DEV_NAME,
- .write = arc_serial_console_write,
- .device = uart_console_device,
- .setup = arc_serial_console_setup,
- .flags = CON_PRINTBUFFER,
- .index = -1,
- .data = &arc_uart_driver
- };
- static __init void arc_early_serial_write(struct console *con, const char *s,
- unsigned int n)
- {
- struct earlycon_device *dev = con->data;
- uart_console_write(&dev->port, s, n, arc_serial_console_putchar);
- }
- static int __init arc_early_console_setup(struct earlycon_device *dev,
- const char *opt)
- {
- struct uart_port *port = &dev->port;
- unsigned int l, h, hw_val;
- if (!dev->port.membase)
- return -ENODEV;
- hw_val = port->uartclk / (dev->baud * 4) - 1;
- l = hw_val & 0xFF;
- h = (hw_val >> 8) & 0xFF;
- UART_SET_BAUDL(port, l);
- UART_SET_BAUDH(port, h);
- dev->con->write = arc_early_serial_write;
- return 0;
- }
- OF_EARLYCON_DECLARE(arc_uart, "snps,arc-uart", arc_early_console_setup);
- #endif
- static int arc_serial_probe(struct platform_device *pdev)
- {
- struct device_node *np = pdev->dev.of_node;
- struct arc_uart_port *uart;
- struct uart_port *port;
- int dev_id;
- u32 val;
-
- if (!np)
- return -ENODEV;
- dev_id = of_alias_get_id(np, "serial");
- if (dev_id < 0)
- dev_id = 0;
- if (dev_id >= ARRAY_SIZE(arc_uart_ports)) {
- dev_err(&pdev->dev, "serial%d out of range\n", dev_id);
- return -EINVAL;
- }
- uart = &arc_uart_ports[dev_id];
- port = &uart->port;
- if (of_property_read_u32(np, "clock-frequency", &val)) {
- dev_err(&pdev->dev, "clock-frequency property NOTset\n");
- return -EINVAL;
- }
- port->uartclk = val;
- if (of_property_read_u32(np, "current-speed", &val)) {
- dev_err(&pdev->dev, "current-speed property NOT set\n");
- return -EINVAL;
- }
- uart->baud = val;
- port->membase = of_iomap(np, 0);
- if (!port->membase)
-
- return -ENXIO;
- port->irq = irq_of_parse_and_map(np, 0);
- port->dev = &pdev->dev;
- port->iotype = UPIO_MEM;
- port->flags = UPF_BOOT_AUTOCONF;
- port->line = dev_id;
- port->ops = &arc_serial_pops;
- port->fifosize = ARC_UART_TX_FIFO_SIZE;
-
- port->ignore_status_mask = 0;
- return uart_add_one_port(&arc_uart_driver, &arc_uart_ports[dev_id].port);
- }
- static int arc_serial_remove(struct platform_device *pdev)
- {
-
- return 0;
- }
- static const struct of_device_id arc_uart_dt_ids[] = {
- { .compatible = "snps,arc-uart" },
- { }
- };
- MODULE_DEVICE_TABLE(of, arc_uart_dt_ids);
- static struct platform_driver arc_platform_driver = {
- .probe = arc_serial_probe,
- .remove = arc_serial_remove,
- .driver = {
- .name = DRIVER_NAME,
- .of_match_table = arc_uart_dt_ids,
- },
- };
- static int __init arc_serial_init(void)
- {
- int ret;
- ret = uart_register_driver(&arc_uart_driver);
- if (ret)
- return ret;
- ret = platform_driver_register(&arc_platform_driver);
- if (ret)
- uart_unregister_driver(&arc_uart_driver);
- return ret;
- }
- static void __exit arc_serial_exit(void)
- {
- platform_driver_unregister(&arc_platform_driver);
- uart_unregister_driver(&arc_uart_driver);
- }
- module_init(arc_serial_init);
- module_exit(arc_serial_exit);
- MODULE_LICENSE("GPL");
- MODULE_ALIAS("platform:" DRIVER_NAME);
- MODULE_AUTHOR("Vineet Gupta");
- MODULE_DESCRIPTION("ARC(Synopsys) On-Chip(fpga) serial driver");
|