2 * Copyright (C) 2012-2015 Spreadtrum Communications Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #if defined(CONFIG_SERIAL_SPRD_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18 #include <linux/clk.h>
19 #include <linux/console.h>
20 #include <linux/delay.h>
22 #include <linux/ioport.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/serial_core.h>
28 #include <linux/serial.h>
29 #include <linux/slab.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
35 #define SPRD_TTY_NAME "ttyS"
36 #define SPRD_FIFO_SIZE 128
37 #define SPRD_DEF_RATE 26000000
38 #define SPRD_BAUD_IO_LIMIT 3000000
39 #define SPRD_TIMEOUT 256
41 /* the offset of serial registers and BITs for them */
43 #define SPRD_TXD 0x0000
44 #define SPRD_RXD 0x0004
46 /* line status register and its BITs */
47 #define SPRD_LSR 0x0008
48 #define SPRD_LSR_OE BIT(4)
49 #define SPRD_LSR_FE BIT(3)
50 #define SPRD_LSR_PE BIT(2)
51 #define SPRD_LSR_BI BIT(7)
52 #define SPRD_LSR_TX_OVER BIT(15)
54 /* data number in TX and RX fifo */
55 #define SPRD_STS1 0x000C
57 /* interrupt enable register and its BITs */
58 #define SPRD_IEN 0x0010
59 #define SPRD_IEN_RX_FULL BIT(0)
60 #define SPRD_IEN_TX_EMPTY BIT(1)
61 #define SPRD_IEN_BREAK_DETECT BIT(7)
62 #define SPRD_IEN_TIMEOUT BIT(13)
64 /* interrupt clear register */
65 #define SPRD_ICLR 0x0014
67 /* line control register */
68 #define SPRD_LCR 0x0018
69 #define SPRD_LCR_STOP_1BIT 0x10
70 #define SPRD_LCR_STOP_2BIT 0x30
71 #define SPRD_LCR_DATA_LEN (BIT(2) | BIT(3))
72 #define SPRD_LCR_DATA_LEN5 0x0
73 #define SPRD_LCR_DATA_LEN6 0x4
74 #define SPRD_LCR_DATA_LEN7 0x8
75 #define SPRD_LCR_DATA_LEN8 0xc
76 #define SPRD_LCR_PARITY (BIT(0) | BIT(1))
77 #define SPRD_LCR_PARITY_EN 0x2
78 #define SPRD_LCR_EVEN_PAR 0x0
79 #define SPRD_LCR_ODD_PAR 0x1
81 /* control register 1 */
82 #define SPRD_CTL1 0x001C
83 #define RX_HW_FLOW_CTL_THLD BIT(6)
84 #define RX_HW_FLOW_CTL_EN BIT(7)
85 #define TX_HW_FLOW_CTL_EN BIT(8)
86 #define RX_TOUT_THLD_DEF 0x3E00
87 #define RX_HFC_THLD_DEF 0x40
89 /* fifo threshold register */
90 #define SPRD_CTL2 0x0020
91 #define THLD_TX_EMPTY 0x40
92 #define THLD_RX_FULL 0x40
94 /* config baud rate register */
95 #define SPRD_CLKD0 0x0024
96 #define SPRD_CLKD1 0x0028
98 /* interrupt mask status register */
99 #define SPRD_IMSR 0x002C
100 #define SPRD_IMSR_RX_FIFO_FULL BIT(0)
101 #define SPRD_IMSR_TX_FIFO_EMPTY BIT(1)
102 #define SPRD_IMSR_BREAK_DETECT BIT(7)
103 #define SPRD_IMSR_TIMEOUT BIT(13)
115 struct sprd_uart_port {
116 struct uart_port port;
117 struct reg_backup reg_bak;
121 static struct sprd_uart_port *sprd_port[UART_NR_MAX];
122 static int sprd_ports_num;
124 static inline unsigned int serial_in(struct uart_port *port, int offset)
126 return readl_relaxed(port->membase + offset);
129 static inline void serial_out(struct uart_port *port, int offset, int value)
131 writel_relaxed(value, port->membase + offset);
134 static unsigned int sprd_tx_empty(struct uart_port *port)
136 if (serial_in(port, SPRD_STS1) & 0xff00)
142 static unsigned int sprd_get_mctrl(struct uart_port *port)
144 return TIOCM_DSR | TIOCM_CTS;
147 static void sprd_set_mctrl(struct uart_port *port, unsigned int mctrl)
152 static void sprd_stop_tx(struct uart_port *port)
154 unsigned int ien, iclr;
156 iclr = serial_in(port, SPRD_ICLR);
157 ien = serial_in(port, SPRD_IEN);
159 iclr |= SPRD_IEN_TX_EMPTY;
160 ien &= ~SPRD_IEN_TX_EMPTY;
162 serial_out(port, SPRD_ICLR, iclr);
163 serial_out(port, SPRD_IEN, ien);
166 static void sprd_start_tx(struct uart_port *port)
170 ien = serial_in(port, SPRD_IEN);
171 if (!(ien & SPRD_IEN_TX_EMPTY)) {
172 ien |= SPRD_IEN_TX_EMPTY;
173 serial_out(port, SPRD_IEN, ien);
177 static void sprd_stop_rx(struct uart_port *port)
179 unsigned int ien, iclr;
181 iclr = serial_in(port, SPRD_ICLR);
182 ien = serial_in(port, SPRD_IEN);
184 ien &= ~(SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT);
185 iclr |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT;
187 serial_out(port, SPRD_IEN, ien);
188 serial_out(port, SPRD_ICLR, iclr);
191 /* The Sprd serial does not support this function. */
192 static void sprd_break_ctl(struct uart_port *port, int break_state)
197 static int handle_lsr_errors(struct uart_port *port,
204 if (*lsr & SPRD_LSR_BI) {
205 *lsr &= ~(SPRD_LSR_FE | SPRD_LSR_PE);
207 ret = uart_handle_break(port);
210 } else if (*lsr & SPRD_LSR_PE)
211 port->icount.parity++;
212 else if (*lsr & SPRD_LSR_FE)
213 port->icount.frame++;
214 if (*lsr & SPRD_LSR_OE)
215 port->icount.overrun++;
217 /* mask off conditions which should be ignored */
218 *lsr &= port->read_status_mask;
219 if (*lsr & SPRD_LSR_BI)
221 else if (*lsr & SPRD_LSR_PE)
223 else if (*lsr & SPRD_LSR_FE)
229 static inline void sprd_rx(struct uart_port *port)
231 struct tty_port *tty = &port->state->port;
232 unsigned int ch, flag, lsr, max_count = SPRD_TIMEOUT;
234 while ((serial_in(port, SPRD_STS1) & 0x00ff) && max_count--) {
235 lsr = serial_in(port, SPRD_LSR);
236 ch = serial_in(port, SPRD_RXD);
240 if (lsr & (SPRD_LSR_BI | SPRD_LSR_PE |
241 SPRD_LSR_FE | SPRD_LSR_OE))
242 if (handle_lsr_errors(port, &lsr, &flag))
244 if (uart_handle_sysrq_char(port, ch))
247 uart_insert_char(port, lsr, SPRD_LSR_OE, ch, flag);
250 tty_flip_buffer_push(tty);
253 static inline void sprd_tx(struct uart_port *port)
255 struct circ_buf *xmit = &port->state->xmit;
259 serial_out(port, SPRD_TXD, port->x_char);
265 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
270 count = THLD_TX_EMPTY;
272 serial_out(port, SPRD_TXD, xmit->buf[xmit->tail]);
273 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
275 if (uart_circ_empty(xmit))
277 } while (--count > 0);
279 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
280 uart_write_wakeup(port);
282 if (uart_circ_empty(xmit))
286 /* this handles the interrupt from one port */
287 static irqreturn_t sprd_handle_irq(int irq, void *dev_id)
289 struct uart_port *port = dev_id;
292 spin_lock(&port->lock);
294 ims = serial_in(port, SPRD_IMSR);
297 spin_unlock(&port->lock);
301 serial_out(port, SPRD_ICLR, ~0);
303 if (ims & (SPRD_IMSR_RX_FIFO_FULL |
304 SPRD_IMSR_BREAK_DETECT | SPRD_IMSR_TIMEOUT))
307 if (ims & SPRD_IMSR_TX_FIFO_EMPTY)
310 spin_unlock(&port->lock);
315 static int sprd_startup(struct uart_port *port)
318 unsigned int ien, fc;
319 unsigned int timeout;
320 struct sprd_uart_port *sp;
323 serial_out(port, SPRD_CTL2, ((THLD_TX_EMPTY << 8) | THLD_RX_FULL));
326 timeout = SPRD_TIMEOUT;
327 while (timeout-- && serial_in(port, SPRD_STS1) & 0x00ff)
328 serial_in(port, SPRD_RXD);
331 timeout = SPRD_TIMEOUT;
332 while (timeout-- && serial_in(port, SPRD_STS1) & 0xff00)
335 /* clear interrupt */
336 serial_out(port, SPRD_IEN, 0);
337 serial_out(port, SPRD_ICLR, ~0);
340 sp = container_of(port, struct sprd_uart_port, port);
341 snprintf(sp->name, sizeof(sp->name), "sprd_serial%d", port->line);
342 ret = devm_request_irq(port->dev, port->irq, sprd_handle_irq,
343 IRQF_SHARED, sp->name, port);
345 dev_err(port->dev, "fail to request serial irq %d, ret=%d\n",
349 fc = serial_in(port, SPRD_CTL1);
350 fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF;
351 serial_out(port, SPRD_CTL1, fc);
353 /* enable interrupt */
354 spin_lock_irqsave(&port->lock, flags);
355 ien = serial_in(port, SPRD_IEN);
356 ien |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT | SPRD_IEN_TIMEOUT;
357 serial_out(port, SPRD_IEN, ien);
358 spin_unlock_irqrestore(&port->lock, flags);
363 static void sprd_shutdown(struct uart_port *port)
365 serial_out(port, SPRD_IEN, 0);
366 serial_out(port, SPRD_ICLR, ~0);
367 devm_free_irq(port->dev, port->irq, port);
370 static void sprd_set_termios(struct uart_port *port,
371 struct ktermios *termios,
372 struct ktermios *old)
374 unsigned int baud, quot;
375 unsigned int lcr = 0, fc;
378 /* ask the core to calculate the divisor for us */
379 baud = uart_get_baud_rate(port, termios, old, 0, SPRD_BAUD_IO_LIMIT);
381 quot = (unsigned int)((port->uartclk + baud / 2) / baud);
383 /* set data length */
384 switch (termios->c_cflag & CSIZE) {
386 lcr |= SPRD_LCR_DATA_LEN5;
389 lcr |= SPRD_LCR_DATA_LEN6;
392 lcr |= SPRD_LCR_DATA_LEN7;
396 lcr |= SPRD_LCR_DATA_LEN8;
400 /* calculate stop bits */
401 lcr &= ~(SPRD_LCR_STOP_1BIT | SPRD_LCR_STOP_2BIT);
402 if (termios->c_cflag & CSTOPB)
403 lcr |= SPRD_LCR_STOP_2BIT;
405 lcr |= SPRD_LCR_STOP_1BIT;
407 /* calculate parity */
408 lcr &= ~SPRD_LCR_PARITY;
409 termios->c_cflag &= ~CMSPAR; /* no support mark/space */
410 if (termios->c_cflag & PARENB) {
411 lcr |= SPRD_LCR_PARITY_EN;
412 if (termios->c_cflag & PARODD)
413 lcr |= SPRD_LCR_ODD_PAR;
415 lcr |= SPRD_LCR_EVEN_PAR;
418 spin_lock_irqsave(&port->lock, flags);
420 /* update the per-port timeout */
421 uart_update_timeout(port, termios->c_cflag, baud);
423 port->read_status_mask = SPRD_LSR_OE;
424 if (termios->c_iflag & INPCK)
425 port->read_status_mask |= SPRD_LSR_FE | SPRD_LSR_PE;
426 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
427 port->read_status_mask |= SPRD_LSR_BI;
429 /* characters to ignore */
430 port->ignore_status_mask = 0;
431 if (termios->c_iflag & IGNPAR)
432 port->ignore_status_mask |= SPRD_LSR_PE | SPRD_LSR_FE;
433 if (termios->c_iflag & IGNBRK) {
434 port->ignore_status_mask |= SPRD_LSR_BI;
436 * If we're ignoring parity and break indicators,
437 * ignore overruns too (for real raw support).
439 if (termios->c_iflag & IGNPAR)
440 port->ignore_status_mask |= SPRD_LSR_OE;
444 fc = serial_in(port, SPRD_CTL1);
445 fc &= ~(RX_HW_FLOW_CTL_THLD | RX_HW_FLOW_CTL_EN | TX_HW_FLOW_CTL_EN);
446 if (termios->c_cflag & CRTSCTS) {
447 fc |= RX_HW_FLOW_CTL_THLD;
448 fc |= RX_HW_FLOW_CTL_EN;
449 fc |= TX_HW_FLOW_CTL_EN;
452 /* clock divider bit0~bit15 */
453 serial_out(port, SPRD_CLKD0, quot & 0xffff);
455 /* clock divider bit16~bit20 */
456 serial_out(port, SPRD_CLKD1, (quot & 0x1f0000) >> 16);
457 serial_out(port, SPRD_LCR, lcr);
458 fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF;
459 serial_out(port, SPRD_CTL1, fc);
461 spin_unlock_irqrestore(&port->lock, flags);
463 /* Don't rewrite B0 */
464 if (tty_termios_baud_rate(termios))
465 tty_termios_encode_baud_rate(termios, baud, baud);
468 static const char *sprd_type(struct uart_port *port)
473 static void sprd_release_port(struct uart_port *port)
478 static int sprd_request_port(struct uart_port *port)
483 static void sprd_config_port(struct uart_port *port, int flags)
485 if (flags & UART_CONFIG_TYPE)
486 port->type = PORT_SPRD;
489 static int sprd_verify_port(struct uart_port *port,
490 struct serial_struct *ser)
492 if (ser->type != PORT_SPRD)
494 if (port->irq != ser->irq)
499 static struct uart_ops serial_sprd_ops = {
500 .tx_empty = sprd_tx_empty,
501 .get_mctrl = sprd_get_mctrl,
502 .set_mctrl = sprd_set_mctrl,
503 .stop_tx = sprd_stop_tx,
504 .start_tx = sprd_start_tx,
505 .stop_rx = sprd_stop_rx,
506 .break_ctl = sprd_break_ctl,
507 .startup = sprd_startup,
508 .shutdown = sprd_shutdown,
509 .set_termios = sprd_set_termios,
511 .release_port = sprd_release_port,
512 .request_port = sprd_request_port,
513 .config_port = sprd_config_port,
514 .verify_port = sprd_verify_port,
517 #ifdef CONFIG_SERIAL_SPRD_CONSOLE
518 static inline void wait_for_xmitr(struct uart_port *port)
520 unsigned int status, tmout = 10000;
522 /* wait up to 10ms for the character(s) to be sent */
524 status = serial_in(port, SPRD_STS1);
528 } while (status & 0xff00);
531 static void sprd_console_putchar(struct uart_port *port, int ch)
533 wait_for_xmitr(port);
534 serial_out(port, SPRD_TXD, ch);
537 static void sprd_console_write(struct console *co, const char *s,
540 struct uart_port *port = &sprd_port[co->index]->port;
546 else if (oops_in_progress)
547 locked = spin_trylock_irqsave(&port->lock, flags);
549 spin_lock_irqsave(&port->lock, flags);
551 uart_console_write(port, s, count, sprd_console_putchar);
553 /* wait for transmitter to become empty */
554 wait_for_xmitr(port);
557 spin_unlock_irqrestore(&port->lock, flags);
560 static int __init sprd_console_setup(struct console *co, char *options)
562 struct uart_port *port;
568 if (co->index >= UART_NR_MAX || co->index < 0)
571 port = &sprd_port[co->index]->port;
573 pr_info("serial port %d not yet initialized\n", co->index);
577 uart_parse_options(options, &baud, &parity, &bits, &flow);
579 return uart_set_options(port, co, baud, parity, bits, flow);
582 static struct uart_driver sprd_uart_driver;
583 static struct console sprd_console = {
584 .name = SPRD_TTY_NAME,
585 .write = sprd_console_write,
586 .device = uart_console_device,
587 .setup = sprd_console_setup,
588 .flags = CON_PRINTBUFFER,
590 .data = &sprd_uart_driver,
593 #define SPRD_CONSOLE (&sprd_console)
595 /* Support for earlycon */
596 static void sprd_putc(struct uart_port *port, int c)
598 unsigned int timeout = SPRD_TIMEOUT;
601 !(readl(port->membase + SPRD_LSR) & SPRD_LSR_TX_OVER))
604 writeb(c, port->membase + SPRD_TXD);
607 static void sprd_early_write(struct console *con, const char *s,
610 struct earlycon_device *dev = con->data;
612 uart_console_write(&dev->port, s, n, sprd_putc);
615 static int __init sprd_early_console_setup(
616 struct earlycon_device *device,
619 if (!device->port.membase)
622 device->con->write = sprd_early_write;
626 EARLYCON_DECLARE(sprd_serial, sprd_early_console_setup);
627 OF_EARLYCON_DECLARE(sprd_serial, "sprd,sc9836-uart",
628 sprd_early_console_setup);
630 #else /* !CONFIG_SERIAL_SPRD_CONSOLE */
631 #define SPRD_CONSOLE NULL
634 static struct uart_driver sprd_uart_driver = {
635 .owner = THIS_MODULE,
636 .driver_name = "sprd_serial",
637 .dev_name = SPRD_TTY_NAME,
641 .cons = SPRD_CONSOLE,
644 static int sprd_probe_dt_alias(int index, struct device *dev)
646 struct device_node *np;
649 if (!IS_ENABLED(CONFIG_OF))
656 ret = of_alias_get_id(np, "serial");
657 if (IS_ERR_VALUE(ret))
659 else if (ret >= ARRAY_SIZE(sprd_port) || sprd_port[ret] != NULL) {
660 dev_warn(dev, "requested serial port %d not available.\n", ret);
667 static int sprd_remove(struct platform_device *dev)
669 struct sprd_uart_port *sup = platform_get_drvdata(dev);
672 uart_remove_one_port(&sprd_uart_driver, &sup->port);
673 sprd_port[sup->port.line] = NULL;
678 uart_unregister_driver(&sprd_uart_driver);
683 static int sprd_probe(struct platform_device *pdev)
685 struct resource *res;
686 struct uart_port *up;
692 for (index = 0; index < ARRAY_SIZE(sprd_port); index++)
693 if (sprd_port[index] == NULL)
696 if (index == ARRAY_SIZE(sprd_port))
699 index = sprd_probe_dt_alias(index, &pdev->dev);
701 sprd_port[index] = devm_kzalloc(&pdev->dev,
702 sizeof(*sprd_port[index]), GFP_KERNEL);
703 if (!sprd_port[index])
706 up = &sprd_port[index]->port;
707 up->dev = &pdev->dev;
709 up->type = PORT_SPRD;
710 up->iotype = SERIAL_IO_PORT;
711 up->uartclk = SPRD_DEF_RATE;
712 up->fifosize = SPRD_FIFO_SIZE;
713 up->ops = &serial_sprd_ops;
714 up->flags = UPF_BOOT_AUTOCONF;
716 clk = devm_clk_get(&pdev->dev, NULL);
718 up->uartclk = clk_get_rate(clk);
720 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
722 dev_err(&pdev->dev, "not provide mem resource\n");
725 up->mapbase = res->start;
726 up->membase = devm_ioremap_resource(&pdev->dev, res);
727 if (IS_ERR(up->membase))
728 return PTR_ERR(up->membase);
730 irq = platform_get_irq(pdev, 0);
732 dev_err(&pdev->dev, "not provide irq resource\n");
737 if (!sprd_ports_num) {
738 ret = uart_register_driver(&sprd_uart_driver);
740 pr_err("Failed to register SPRD-UART driver\n");
746 ret = uart_add_one_port(&sprd_uart_driver, up);
748 sprd_port[index] = NULL;
752 platform_set_drvdata(pdev, up);
757 static int sprd_suspend(struct device *dev)
759 struct sprd_uart_port *sup = dev_get_drvdata(dev);
761 uart_suspend_port(&sprd_uart_driver, &sup->port);
766 static int sprd_resume(struct device *dev)
768 struct sprd_uart_port *sup = dev_get_drvdata(dev);
770 uart_resume_port(&sprd_uart_driver, &sup->port);
775 static SIMPLE_DEV_PM_OPS(sprd_pm_ops, sprd_suspend, sprd_resume);
777 static const struct of_device_id serial_ids[] = {
778 {.compatible = "sprd,sc9836-uart",},
782 static struct platform_driver sprd_platform_driver = {
784 .remove = sprd_remove,
786 .name = "sprd_serial",
787 .of_match_table = of_match_ptr(serial_ids),
792 module_platform_driver(sprd_platform_driver);
794 MODULE_LICENSE("GPL v2");
795 MODULE_DESCRIPTION("Spreadtrum SoC serial driver series");