11 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
15 #define DRIVER_NAME "bfin-uart"
16 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
18 #include <linux/module.h>
26 #include <linux/tty.h>
28 #include <linux/serial_core.h>
32 #include <linux/slab.h>
36 #include <asm/cacheflush.h>
40 #ifdef CONFIG_SERIAL_BFIN_MODULE
41 # undef CONFIG_EARLY_PRINTK
44 #ifdef CONFIG_SERIAL_BFIN_MODULE
45 # undef CONFIG_EARLY_PRINTK
49 #define BFIN_SERIAL_DEV_NAME "ttyBF"
50 #define BFIN_SERIAL_MAJOR 204
51 #define BFIN_SERIAL_MINOR 64
55 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
56 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
58 # ifndef CONFIG_SERIAL_BFIN_PIO
59 # error KGDB only support UART in PIO mode.
62 static int kgdboc_port_line;
63 static int kgdboc_break_enabled;
68 #define DMA_RX_XCOUNT 512
69 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
71 #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
73 #ifdef CONFIG_SERIAL_BFIN_DMA
81 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
82 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
83 static unsigned int bfin_serial_get_mctrl(
struct uart_port *
port)
86 if (uart->cts_pin < 0)
96 static void bfin_serial_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
99 if (uart->rts_pin < 0)
115 unsigned int status = bfin_serial_get_mctrl(&uart->
port);
116 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
119 UART_CLEAR_SCTS(uart);
135 static unsigned int bfin_serial_get_mctrl(
struct uart_port *port)
140 static void bfin_serial_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
148 static void bfin_serial_stop_tx(
struct uart_port *port)
151 #ifdef CONFIG_SERIAL_BFIN_DMA
158 #ifdef CONFIG_SERIAL_BFIN_DMA
161 uart->
port.icount.tx += uart->tx_count;
165 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
167 UART_PUT_LSR(uart,
TFI);
176 static void bfin_serial_start_tx(
struct uart_port *port)
185 if (tty->termios.c_line ==
N_IRDA)
186 bfin_serial_reset_irda(port);
188 #ifdef CONFIG_SERIAL_BFIN_DMA
190 bfin_serial_dma_tx_chars(uart);
193 bfin_serial_tx_chars(uart);
200 static void bfin_serial_stop_rx(
struct uart_port *port)
210 static void bfin_serial_enable_ms(
struct uart_port *port)
215 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
216 # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
217 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
219 # define UART_GET_ANOMALY_THRESHOLD(uart) 0
220 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
223 #ifdef CONFIG_SERIAL_BFIN_PIO
227 unsigned int status, ch, flg;
231 UART_CLEAR_LSR(uart);
234 uart->
port.icount.rx++;
236 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
237 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
239 && kgdboc_break_enabled)
245 if (!uart->
port.state || !uart->
port.state->port.tty)
248 tty = uart->
port.state->port.tty;
263 if (anomaly_start.
tv_sec) {
267 if ((~ch & (~ch + 1)) & 0xff)
268 goto known_good_char;
272 goto known_good_char;
280 goto known_good_char;
285 anomaly_start =
curr;
297 if (bfin_revid() < 5)
299 uart->
port.icount.brk++;
300 if (uart_handle_break(&uart->
port))
302 status &= ~(
PE |
FE);
305 uart->
port.icount.parity++;
307 uart->
port.icount.overrun++;
309 uart->
port.icount.frame++;
311 status &= uart->
port.read_status_mask;
315 else if (status & PE)
317 else if (status & FE)
336 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
338 UART_PUT_LSR(uart,
TFI);
349 if (uart->
port.x_char) {
351 uart->
port.icount.tx++;
352 uart->
port.x_char = 0;
358 uart->
port.icount.tx++;
365 static irqreturn_t bfin_serial_rx_int(
int irq,
void *dev_id)
370 bfin_serial_rx_chars(uart);
375 static irqreturn_t bfin_serial_tx_int(
int irq,
void *dev_id)
379 spin_lock(&uart->
port.lock);
381 bfin_serial_tx_chars(uart);
382 spin_unlock(&uart->
port.lock);
388 #ifdef CONFIG_SERIAL_BFIN_DMA
401 if (uart->
port.x_char) {
403 uart->
port.icount.tx++;
404 uart->
port.x_char = 0;
411 (
unsigned long)(xmit->
buf+xmit->
tail+uart->tx_count));
412 set_dma_config(uart->tx_dma_channel,
418 set_dma_start_addr(uart->tx_dma_channel, (
unsigned long)(xmit->
buf+xmit->
tail));
419 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
420 set_dma_x_modify(uart->tx_dma_channel, 1);
433 UART_CLEAR_LSR(uart);
435 uart->
port.icount.rx +=
436 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
440 uart->
port.icount.brk++;
441 if (uart_handle_break(&uart->
port))
442 goto dma_ignore_char;
443 status &= ~(
PE |
FE);
446 uart->
port.icount.parity++;
448 uart->
port.icount.overrun++;
450 uart->
port.icount.frame++;
452 status &= uart->
port.read_status_mask;
456 else if (status & PE)
458 else if (status & FE)
463 for (i = uart->rx_dma_buf.tail; ; i++) {
466 if (i == uart->rx_dma_buf.head)
470 uart->rx_dma_buf.buf[i], flg);
481 dma_disable_irq_nosync(uart->rx_dma_channel);
482 spin_lock_bh(&uart->rx_lock);
493 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
494 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
497 uart->rx_dma_nrows = 0;
506 if (pos > uart->rx_dma_buf.tail ||
507 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/
DMA_RX_XCOUNT)) {
508 uart->rx_dma_buf.head =
pos;
509 bfin_serial_dma_rx_chars(uart);
510 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
513 spin_unlock_bh(&uart->rx_lock);
514 dma_enable_irq(uart->rx_dma_channel);
519 static irqreturn_t bfin_serial_dma_tx_int(
int irq,
void *dev_id)
524 spin_lock(&uart->
port.lock);
525 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&
DMA_RUN)) {
527 clear_dma_irqstat(uart->tx_dma_channel);
534 uart->
port.icount.tx += uart->tx_count;
535 if (!(xmit->
tail == 0 && xmit->
head == 0)) {
542 bfin_serial_dma_tx_chars(uart);
545 spin_unlock(&uart->
port.lock);
549 static irqreturn_t bfin_serial_dma_rx_int(
int irq,
void *dev_id)
552 unsigned int irqstat;
555 spin_lock(&uart->rx_lock);
556 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
557 clear_dma_irqstat(uart->rx_dma_channel);
559 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
560 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
563 uart->rx_dma_nrows = 0;
566 if (pos > uart->rx_dma_buf.tail ||
567 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/
DMA_RX_XCOUNT)) {
568 uart->rx_dma_buf.head =
pos;
569 bfin_serial_dma_rx_chars(uart);
570 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
573 spin_unlock(&uart->rx_lock);
582 static unsigned int bfin_serial_tx_empty(
struct uart_port *port)
594 static void bfin_serial_break_ctl(
struct uart_port *port,
int break_state)
606 static int bfin_serial_startup(
struct uart_port *port)
610 #ifdef CONFIG_SERIAL_BFIN_DMA
613 if (
request_dma(uart->rx_dma_channel,
"BFIN_UART_RX") < 0) {
618 if (
request_dma(uart->tx_dma_channel,
"BFIN_UART_TX") < 0) {
628 uart->rx_dma_buf.head = 0;
629 uart->rx_dma_buf.tail = 0;
630 uart->rx_dma_nrows = 0;
632 set_dma_config(uart->rx_dma_channel,
638 set_dma_x_modify(uart->rx_dma_channel, 1);
640 set_dma_y_modify(uart->rx_dma_channel, 1);
641 set_dma_start_addr(uart->rx_dma_channel, (
unsigned long)uart->rx_dma_buf.buf);
644 uart->rx_dma_timer.data = (
unsigned long)(uart);
645 uart->rx_dma_timer.function = (
void *)bfin_serial_rx_dma_timeout;
649 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
650 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
651 if (kgdboc_port_line == uart->
port.line && kgdboc_break_enabled)
652 kgdboc_break_enabled = 0;
656 "BFIN_UART_RX", uart)) {
662 (uart->
tx_irq, bfin_serial_tx_int, 0,
663 "BFIN_UART_TX", uart)) {
678 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
690 uart_dma_ch_rx = uart_dma_ch_tx = 0;
694 if (uart_dma_ch_rx &&
701 if (uart_dma_ch_tx &&
711 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
712 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
717 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
718 if (uart->cts_pin >= 0) {
720 bfin_serial_mctrl_cts_int,
722 0,
"BFIN_UART_CTS", uart)) {
724 pr_info(
"Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
727 if (uart->rts_pin >= 0) {
729 pr_info(
"fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
735 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
736 if (uart->cts_pin >= 0) {
740 dev_info(port->
dev,
"Unable to attach BlackFin UART Modem Status interrupt.\n");
753 static void bfin_serial_shutdown(
struct uart_port *port)
757 #ifdef CONFIG_SERIAL_BFIN_DMA
766 switch (uart->
port.irq) {
783 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
784 if (uart->cts_pin >= 0)
786 if (uart->rts_pin >= 0)
789 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
790 if (uart->cts_pin >= 0)
801 unsigned int baud, quot;
802 unsigned int ier, lcr = 0;
828 "supported in case of anomaly 05000231.\n");
895 spin_unlock_irqrestore(&uart->
port.lock, flags);
898 static const char *bfin_serial_type(
struct uart_port *port)
908 static void bfin_serial_release_port(
struct uart_port *port)
915 static int bfin_serial_request_port(
struct uart_port *port)
923 static void bfin_serial_config_port(
struct uart_port *port,
int flags)
928 bfin_serial_request_port(&uart->
port) == 0)
947 static void bfin_serial_set_ldisc(
struct uart_port *port,
int ld)
965 static void bfin_serial_reset_irda(
struct uart_port *port)
979 #ifdef CONFIG_CONSOLE_POLL
984 static void bfin_serial_poll_put_char(
struct uart_port *port,
unsigned char chr)
995 static int bfin_serial_poll_get_char(
struct uart_port *port)
1010 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1011 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1012 static void bfin_kgdboc_port_shutdown(
struct uart_port *port)
1014 if (kgdboc_break_enabled) {
1015 kgdboc_break_enabled = 0;
1016 bfin_serial_shutdown(port);
1020 static int bfin_kgdboc_port_startup(
struct uart_port *port)
1022 kgdboc_port_line = port->
line;
1023 kgdboc_break_enabled = !bfin_serial_startup(port);
1028 static struct uart_ops bfin_serial_pops = {
1029 .tx_empty = bfin_serial_tx_empty,
1030 .set_mctrl = bfin_serial_set_mctrl,
1031 .get_mctrl = bfin_serial_get_mctrl,
1032 .stop_tx = bfin_serial_stop_tx,
1033 .start_tx = bfin_serial_start_tx,
1034 .stop_rx = bfin_serial_stop_rx,
1035 .enable_ms = bfin_serial_enable_ms,
1036 .break_ctl = bfin_serial_break_ctl,
1037 .startup = bfin_serial_startup,
1038 .shutdown = bfin_serial_shutdown,
1039 .set_termios = bfin_serial_set_termios,
1040 .set_ldisc = bfin_serial_set_ldisc,
1041 .type = bfin_serial_type,
1042 .release_port = bfin_serial_release_port,
1043 .request_port = bfin_serial_request_port,
1044 .config_port = bfin_serial_config_port,
1045 .verify_port = bfin_serial_verify_port,
1046 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1047 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1048 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1049 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1051 #ifdef CONFIG_CONSOLE_POLL
1052 .poll_put_char = bfin_serial_poll_put_char,
1053 .poll_get_char = bfin_serial_poll_get_char,
1057 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1064 int *parity,
int *
bits)
1094 pr_debug(
"%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1099 static void bfin_serial_console_putchar(
struct uart_port *port,
int ch)
1110 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1111 #define CLASS_BFIN_CONSOLE "bfin-console"
1116 bfin_serial_console_write(
struct console *co,
const char *
s,
unsigned int count)
1119 unsigned long flags;
1123 spin_unlock_irqrestore(&uart->
port.lock, flags);
1134 # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1135 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1149 uart = bfin_serial_ports[co->
index];
1156 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1161 static struct console bfin_serial_console = {
1163 .write = bfin_serial_console_write,
1165 .setup = bfin_serial_console_setup,
1168 .data = &bfin_serial_reg,
1170 #define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
1172 #define BFIN_SERIAL_CONSOLE NULL
1175 #ifdef CONFIG_EARLY_PRINTK
1177 #define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
1183 bfin_earlyprintk_console_write(
struct console *co,
const char *
s,
unsigned int count)
1185 unsigned long flags;
1187 if (bfin_earlyprintk_port.port.line != co->
index)
1192 bfin_serial_console_putchar);
1193 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
1203 .
name =
"early_BFuart",
1204 .write = bfin_earlyprintk_console_write,
1208 .data = &bfin_serial_reg,
1243 dev_err(&pdev->
dev,
"Wrong bfin uart platform device id.\n");
1247 if (bfin_serial_ports[pdev->
id] ==
NULL) {
1252 "fail to malloc bfin_serial_port\n");
1255 bfin_serial_ports[pdev->
id] =
uart;
1257 #ifdef CONFIG_EARLY_PRINTK
1258 if (!(bfin_earlyprintk_port.port.membase
1259 && bfin_earlyprintk_port.port.line == pdev->
id)) {
1269 "fail to request bfin serial peripherals\n");
1270 goto out_error_free_mem;
1272 #ifdef CONFIG_EARLY_PRINTK
1279 uart->
port.ops = &bfin_serial_pops;
1280 uart->
port.line = pdev->
id;
1286 dev_err(&pdev->
dev,
"Cannot get IORESOURCE_MEM\n");
1288 goto out_error_free_peripherals;
1292 if (!uart->
port.membase) {
1295 goto out_error_free_peripherals;
1301 dev_err(&pdev->
dev,
"No uart TX IRQ specified\n");
1303 goto out_error_unmap;
1308 dev_err(&pdev->
dev,
"No uart RX IRQ specified\n");
1310 goto out_error_unmap;
1316 dev_err(&pdev->
dev,
"No uart status IRQ specified\n");
1318 goto out_error_unmap;
1321 #ifdef CONFIG_SERIAL_BFIN_DMA
1328 dev_err(&pdev->
dev,
"No uart TX DMA channel specified\n");
1330 goto out_error_unmap;
1332 uart->tx_dma_channel = res->
start;
1336 dev_err(&pdev->
dev,
"No uart RX DMA channel specified\n");
1338 goto out_error_unmap;
1340 uart->rx_dma_channel = res->
start;
1345 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1346 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1351 uart->cts_pin = res->
start;
1352 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1361 uart->rts_pin = res->
start;
1365 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1366 if (!is_early_platform_device(pdev)) {
1368 uart = bfin_serial_ports[pdev->
id];
1372 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1382 out_error_free_peripherals:
1384 (
unsigned short *)pdev->
dev.platform_data);
1387 bfin_serial_ports[pdev->
id] =
NULL;
1403 (
unsigned short *)pdev->
dev.platform_data);
1405 bfin_serial_ports[pdev->
id] =
NULL;
1412 .probe = bfin_serial_probe,
1414 .suspend = bfin_serial_suspend,
1415 .resume = bfin_serial_resume,
1422 #if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1425 .pdrv = &bfin_serial_driver,
1429 static int __init bfin_serial_rs_console_init(
void)
1442 #ifdef CONFIG_EARLY_PRINTK
1453 dev_err(&pdev->
dev,
"Wrong earlyprintk platform device id.\n");
1461 "fail to request bfin serial peripherals\n");
1467 dev_err(&pdev->
dev,
"Cannot get IORESOURCE_MEM\n");
1469 goto out_error_free_peripherals;
1472 bfin_earlyprintk_port.port.membase =
ioremap(res->
start,
1473 resource_size(res));
1474 if (!bfin_earlyprintk_port.port.membase) {
1477 goto out_error_free_peripherals;
1479 bfin_earlyprintk_port.port.mapbase = res->
start;
1480 bfin_earlyprintk_port.port.line = pdev->
id;
1481 bfin_earlyprintk_port.port.uartclk =
get_sclk();
1487 out_error_free_peripherals:
1489 (
unsigned short *)pdev->
dev.platform_data);
1495 .
probe = bfin_earlyprintk_probe,
1504 .pdrv = &bfin_earlyprintk_driver,
1508 struct console __init *bfin_earlyserial_init(
unsigned int port,
1526 if (!bfin_earlyprintk_port.port.membase)
1529 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1537 bfin_early_serial_console.index =
port;
1543 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &
t, &
t);
1545 return &bfin_early_serial_console;
1549 static int __init bfin_serial_init(
void)
1553 pr_info(
"Blackfin serial driver\n");
1557 pr_err(
"failed to register %s:%d\n",
1563 pr_err(
"fail to register bfin uart\n");
1570 static void __exit bfin_serial_exit(
void)