32 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36 #include <linux/module.h>
41 #include <linux/device.h>
42 #include <linux/tty.h>
44 #include <linux/serial_core.h>
45 #include <linux/serial.h>
49 #include <linux/slab.h>
54 #include <linux/types.h>
64 #define SERIAL_AMBA_MAJOR 204
65 #define SERIAL_AMBA_MINOR 64
66 #define SERIAL_AMBA_NR UART_NR
68 #define AMBA_ISR_PASS_LIMIT 256
70 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
71 #define UART_DUMMY_DR_RX (1 << 16)
89 .oversampling =
false,
90 .dma_threshold =
false,
91 .cts_event_workaround =
false,
100 .dma_threshold =
true,
101 .cts_event_workaround =
true,
150 #ifdef CONFIG_DMA_ENGINE
170 while (max_count--) {
179 uap->
port.icount.rx++;
185 uap->
port.icount.brk++;
186 if (uart_handle_break(&uap->
port))
189 uap->
port.icount.parity++;
191 uap->
port.icount.frame++;
193 uap->
port.icount.overrun++;
195 ch &= uap->
port.read_status_mask;
197 if (ch & UART011_DR_BE)
199 else if (ch & UART011_DR_PE)
201 else if (ch & UART011_DR_FE)
220 #ifdef CONFIG_DMA_ENGINE
222 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
279 dmaengine_slave_config(chan, &tx_conf);
280 uap->dmatx.chan = chan;
283 dma_chan_name(uap->dmatx.chan));
301 dmaengine_slave_config(chan, &rx_conf);
302 uap->dmarx.chan = chan;
305 dma_chan_name(uap->dmarx.chan));
323 static int __init pl011_dma_initcall(
void)
328 struct dma_uap *dmau =
list_entry(node,
struct dma_uap, node);
329 pl011_dma_probe_initcall(dmau->uap);
340 struct dma_uap *dmau = kzalloc(
sizeof(
struct dma_uap),
GFP_KERNEL);
349 pl011_dma_probe_initcall(uap);
369 static void pl011_dma_tx_callback(
void *
data)
377 if (uap->dmatx.queued)
396 uap->dmatx.queued =
false;
397 spin_unlock_irqrestore(&uap->
port.lock, flags);
401 if (pl011_dma_tx_refill(uap) <= 0) {
409 spin_unlock_irqrestore(&uap->
port.lock, flags);
437 uap->dmatx.queued =
false;
448 if (count > PL011_DMA_BUFFER_SIZE)
449 count = PL011_DMA_BUFFER_SIZE;
465 uap->dmatx.queued =
false;
474 uap->dmatx.queued =
false;
484 desc->
callback = pl011_dma_tx_callback;
488 dmaengine_submit(desc);
495 uap->dmatx.queued =
true;
520 if (!uap->using_tx_dma)
528 if (uap->dmatx.queued) {
540 if (pl011_dma_tx_refill(uap) > 0) {
554 if (uap->dmatx.queued) {
555 uap->
dmacr &= ~UART011_TXDMAE;
572 if (!uap->using_tx_dma)
575 if (!uap->
port.x_char) {
579 if (!uap->dmatx.queued) {
580 if (pl011_dma_tx_refill(uap) > 0) {
588 }
else if (!(uap->
dmacr & UART011_TXDMAE)) {
601 uap->
dmacr &= ~UART011_TXDMAE;
614 uap->
port.icount.tx++;
615 uap->
port.x_char = 0;
632 if (!uap->using_tx_dma)
636 spin_unlock(&uap->
port.lock);
637 dmaengine_terminate_all(uap->dmatx.chan);
638 spin_lock(&uap->
port.lock);
639 if (uap->dmatx.queued) {
642 uap->dmatx.queued =
false;
643 uap->
dmacr &= ~UART011_TXDMAE;
648 static void pl011_dma_rx_callback(
void *data);
652 struct dma_chan *rxchan = uap->dmarx.chan;
661 sgbuf = uap->dmarx.use_buf_b ?
662 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
663 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->
sg, 1,
672 uap->dmarx.running =
false;
673 dmaengine_terminate_all(rxchan);
678 desc->
callback = pl011_dma_rx_callback;
680 dmarx->
cookie = dmaengine_submit(desc);
681 dma_async_issue_pending(rxchan);
685 uap->dmarx.running =
true;
699 u32 pending,
bool use_buf_b,
704 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
719 dma_count = tty_insert_flip_string(uap->
port.state->port.tty,
720 sgbuf->
buf, pending);
725 uap->
port.icount.rx += dma_count;
726 if (dma_count < pending)
728 "couldn't insert all characters (TTY is full?)\n");
735 if (dma_count == pending && readfifo) {
751 fifotaken = pl011_fifo_to_tty(uap);
754 spin_unlock(&uap->
port.lock);
756 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
757 dma_count, fifotaken);
759 spin_lock(&uap->
port.lock);
777 if (dmaengine_pause(rxchan))
778 dev_err(uap->
port.dev,
"unable to pause DMA transfer\n");
779 dmastat = rxchan->
device->device_tx_status(rxchan,
782 dev_err(uap->
port.dev,
"unable to pause DMA transfer\n");
787 uap->dmarx.running =
false;
789 pending = sgbuf->
sg.length -
state.residue;
790 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
792 dmaengine_terminate_all(rxchan);
798 pl011_dma_rx_chars(uap, pending, dmarx->
use_buf_b,
true);
802 if (pl011_dma_rx_trigger_dma(uap)) {
803 dev_dbg(uap->
port.dev,
"could not retrigger RX DMA job "
804 "fall back to interrupt mode\n");
810 static void pl011_dma_rx_callback(
void *data)
829 spin_lock_irq(&uap->
port.lock);
835 pending = sgbuf->
sg.length -
state.residue;
836 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
838 dmaengine_terminate_all(rxchan);
840 uap->dmarx.running =
false;
842 ret = pl011_dma_rx_trigger_dma(uap);
844 pl011_dma_rx_chars(uap, pending, lastbuf,
false);
845 spin_unlock_irq(&uap->
port.lock);
851 dev_dbg(uap->
port.dev,
"could not retrigger RX DMA job "
852 "fall back to interrupt mode\n");
874 if (!uap->dmatx.chan)
878 if (!uap->dmatx.buf) {
879 dev_err(uap->
port.dev,
"no memory for DMA TX buffer\n");
884 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
887 uap->
port.fifosize = PL011_DMA_BUFFER_SIZE;
888 uap->using_tx_dma =
true;
890 if (!uap->dmarx.chan)
894 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
897 dev_err(uap->
port.dev,
"failed to init DMA %s: %d\n",
902 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
905 dev_err(uap->
port.dev,
"failed to init DMA %s: %d\n",
907 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
912 uap->using_rx_dma =
true;
924 if (uap->
vendor->dma_threshold)
928 if (uap->using_rx_dma) {
929 if (pl011_dma_rx_trigger_dma(uap))
930 dev_dbg(uap->
port.dev,
"could not trigger initial "
931 "RX DMA job, fall back to interrupt mode\n");
937 if (!(uap->using_tx_dma || uap->using_rx_dma))
944 spin_lock_irq(&uap->
port.lock);
947 spin_unlock_irq(&uap->
port.lock);
949 if (uap->using_tx_dma) {
951 dmaengine_terminate_all(uap->dmatx.chan);
952 if (uap->dmatx.queued) {
955 uap->dmatx.queued =
false;
958 kfree(uap->dmatx.buf);
959 uap->using_tx_dma =
false;
962 if (uap->using_rx_dma) {
963 dmaengine_terminate_all(uap->dmarx.chan);
965 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
DMA_FROM_DEVICE);
966 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
DMA_FROM_DEVICE);
967 uap->using_rx_dma =
false;
971 static inline bool pl011_dma_rx_available(
struct uart_amba_port *uap)
973 return uap->using_rx_dma;
976 static inline bool pl011_dma_rx_running(
struct uart_amba_port *uap)
978 return uap->using_rx_dma && uap->dmarx.running;
1009 static inline bool pl011_dma_tx_start(
struct uart_amba_port *uap)
1022 static inline int pl011_dma_rx_trigger_dma(
struct uart_amba_port *uap)
1027 static inline bool pl011_dma_rx_available(
struct uart_amba_port *uap)
1032 static inline bool pl011_dma_rx_running(
struct uart_amba_port *uap)
1037 #define pl011_dma_flush_buffer NULL
1040 static void pl011_stop_tx(
struct uart_port *port)
1046 pl011_dma_tx_stop(uap);
1049 static void pl011_start_tx(
struct uart_port *port)
1053 if (!pl011_dma_tx_start(uap)) {
1059 static void pl011_stop_rx(
struct uart_port *port)
1067 pl011_dma_rx_stop(uap);
1070 static void pl011_enable_ms(
struct uart_port *port)
1082 pl011_fifo_to_tty(uap);
1084 spin_unlock(&uap->
port.lock);
1090 if (pl011_dma_rx_available(uap)) {
1091 if (pl011_dma_rx_trigger_dma(uap)) {
1092 dev_dbg(uap->
port.dev,
"could not trigger RX DMA job "
1093 "fall back to interrupt mode again\n");
1099 spin_lock(&uap->
port.lock);
1107 if (uap->
port.x_char) {
1109 uap->
port.icount.tx++;
1110 uap->
port.x_char = 0;
1114 pl011_stop_tx(&uap->
port);
1119 if (pl011_dma_tx_irq(uap))
1126 uap->
port.icount.tx++;
1129 }
while (--count > 0);
1135 pl011_stop_tx(&uap->
port);
1154 uap->
port.icount.dsr++;
1165 unsigned long flags;
1168 unsigned int dummy_read;
1175 if (uap->
vendor->cts_event_workaround) {
1193 if (pl011_dma_rx_running(uap))
1194 pl011_dma_rx_irq(uap);
1196 pl011_rx_chars(uap);
1200 pl011_modem_status(uap);
1202 pl011_tx_chars(uap);
1204 if (pass_counter-- == 0)
1208 }
while (status != 0);
1212 spin_unlock_irqrestore(&uap->
port.lock, flags);
1217 static unsigned int pl011_tx_empty(
struct uart_port *port)
1224 static unsigned int pl011_get_mctrl(
struct uart_port *port)
1230 #define TIOCMBIT(uartbit, tiocmbit) \
1231 if (status & uartbit) \
1242 static void pl011_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
1249 #define TIOCMBIT(tiocmbit, uartbit) \
1250 if (mctrl & tiocmbit) \
1270 static void pl011_break_ctl(
struct uart_port *port,
int break_state)
1273 unsigned long flags;
1278 if (break_state == -1)
1283 spin_unlock_irqrestore(&uap->
port.lock, flags);
1286 #ifdef CONFIG_CONSOLE_POLL
1288 static void pl011_quiesce_irqs(
struct uart_port *port)
1310 static int pl011_get_poll_char(
struct uart_port *port)
1319 pl011_quiesce_irqs(port);
1322 if (status & UART01x_FR_RXFE)
1328 static void pl011_put_poll_char(
struct uart_port *port,
1341 static int pl011_hwinit(
struct uart_port *port)
1351 "could not set default pins\n");
1357 retval = clk_prepare_enable(uap->
clk);
1374 if (uap->
port.dev->platform_data) {
1377 plat = uap->
port.dev->platform_data;
1386 static int pl011_startup(
struct uart_port *port)
1392 retval = pl011_hwinit(port);
1421 for (i = 0; i < 10; ++
i)
1440 pl011_dma_startup(uap);
1447 spin_lock_irq(&uap->
port.lock);
1452 if (!pl011_dma_rx_running(uap))
1455 spin_unlock_irq(&uap->
port.lock);
1460 clk_disable_unprepare(uap->
clk);
1474 static void pl011_shutdown(
struct uart_port *port)
1483 spin_lock_irq(&uap->
port.lock);
1487 spin_unlock_irq(&uap->
port.lock);
1489 pl011_dma_shutdown(uap);
1512 pl011_shutdown_channel(uap, uap->
lcrh_rx);
1514 pl011_shutdown_channel(uap, uap->
lcrh_tx);
1519 clk_disable_unprepare(uap->
clk);
1525 "could not set pins to sleep state\n");
1529 if (uap->
port.dev->platform_data) {
1532 plat = uap->
port.dev->platform_data;
1544 unsigned int lcr_h,
old_cr;
1545 unsigned long flags;
1546 unsigned int baud, quot, clkdiv;
1548 if (uap->
vendor->oversampling)
1624 pl011_enable_ms(port);
1641 if (uap->
vendor->oversampling) {
1642 if (baud > port->
uartclk / 16)
1654 if (uap->
vendor->oversampling) {
1655 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1657 else if ((baud > 3250000) && (quot > 2))
1677 for (i = 0; i < 10; ++
i)
1683 spin_unlock_irqrestore(&port->
lock, flags);
1686 static const char *pl011_type(
struct uart_port *port)
1695 static void pl011_release_port(
struct uart_port *port)
1703 static int pl011_request_port(
struct uart_port *port)
1712 static void pl011_config_port(
struct uart_port *port,
int flags)
1716 pl011_request_port(port);
1735 static struct uart_ops amba_pl011_pops = {
1736 .tx_empty = pl011_tx_empty,
1737 .set_mctrl = pl011_set_mctrl,
1738 .get_mctrl = pl011_get_mctrl,
1739 .stop_tx = pl011_stop_tx,
1740 .start_tx = pl011_start_tx,
1741 .stop_rx = pl011_stop_rx,
1742 .enable_ms = pl011_enable_ms,
1743 .break_ctl = pl011_break_ctl,
1744 .startup = pl011_startup,
1745 .shutdown = pl011_shutdown,
1747 .set_termios = pl011_set_termios,
1749 .release_port = pl011_release_port,
1750 .request_port = pl011_request_port,
1751 .config_port = pl011_config_port,
1752 .verify_port = pl011_verify_port,
1753 #ifdef CONFIG_CONSOLE_POLL
1754 .poll_init = pl011_hwinit,
1755 .poll_get_char = pl011_get_poll_char,
1756 .poll_put_char = pl011_put_poll_char,
1762 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1764 static void pl011_console_putchar(
struct uart_port *port,
int ch)
1774 pl011_console_write(
struct console *co,
const char *
s,
unsigned int count)
1778 unsigned long flags;
1784 if (uap->
port.sysrq)
1787 locked = spin_trylock(&uap->
port.lock);
1789 spin_lock(&uap->
port.lock);
1807 }
while (status & UART01x_FR_BUSY);
1811 spin_unlock(&uap->
port.lock);
1819 int *parity,
int *
bits)
1822 unsigned int lcr_h, ibrd, fbrd;
1842 *baud = uap->
port.uartclk * 4 / (64 * ibrd + fbrd);
1844 if (uap->
vendor->oversampling) {
1868 uap = amba_ports[co->
index];
1877 "could not set default pins\n");
1884 if (uap->
port.dev->platform_data) {
1887 plat = uap->
port.dev->platform_data;
1897 pl011_console_get_options(uap, &baud, &parity, &bits);
1903 static struct console amba_console = {
1905 .write = pl011_console_write,
1907 .setup = pl011_console_setup,
1913 #define AMBA_CONSOLE (&amba_console)
1915 #define AMBA_CONSOLE NULL
1920 .driver_name =
"ttyAMA",
1921 .dev_name =
"ttyAMA",
1928 static int pl011_probe_dt_alias(
int index,
struct device *dev)
1931 static bool seen_dev_with_alias =
false;
1932 static bool seen_dev_without_alias =
false;
1944 seen_dev_without_alias =
true;
1947 seen_dev_with_alias =
true;
1949 dev_warn(dev,
"requested serial port %d not available.\n", ret);
1954 if (seen_dev_with_alias && seen_dev_without_alias)
1955 dev_warn(dev,
"aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
1968 if (amba_ports[i] ==
NULL)
1982 i = pl011_probe_dt_alias(i, &dev->
dev);
1998 dev_err(&dev->
dev,
"could not get default pinstate\n");
2003 dev_dbg(&dev->
dev,
"could not get sleep pinstate\n");
2006 if (IS_ERR(uap->
clk)) {
2007 ret = PTR_ERR(uap->
clk);
2017 uap->
port.mapbase = dev->
res.start;
2018 uap->
port.membase = base;
2022 uap->
port.ops = &amba_pl011_pops;
2025 pl011_dma_probe(uap);
2033 amba_ports[
i] = uap;
2039 amba_ports[
i] =
NULL;
2040 pl011_dma_remove(uap);
2061 if (amba_ports[i] == uap)
2062 amba_ports[
i] =
NULL;
2064 pl011_dma_remove(uap);
2093 static struct amba_id pl011_ids[] = {
2097 .data = &vendor_arm,
2111 .name =
"uart-pl011",
2113 .id_table = pl011_ids,
2114 .probe = pl011_probe,
2115 .remove = pl011_remove,
2117 .suspend = pl011_suspend,
2118 .resume = pl011_resume,
2122 static int __init pl011_init(
void)
2136 static void __exit pl011_exit(
void)