25 #include <linux/module.h>
26 #include <linux/tty.h>
28 #include <linux/slab.h>
30 #include <linux/serial.h>
44 #include <asm/ioctls.h>
46 #include <asm/mach/serial_at91.h>
47 #include <mach/board.h>
54 #define PDC_BUFFER_SIZE 512
56 #define PDC_RX_TIMEOUT (3 * 10)
58 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
62 #include <linux/serial_core.h>
67 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
72 #define SERIAL_ATMEL_MAJOR 204
73 #define MINOR_START 154
74 #define ATMEL_DEVICENAME "ttyAT"
80 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
81 #define MINOR_START 64
82 #define ATMEL_DEVICENAME "ttyS"
86 #define ATMEL_ISR_PASS_LIMIT 256
89 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
90 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
91 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
92 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
93 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
94 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
95 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
96 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
97 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
98 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
99 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
100 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
101 #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
104 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
105 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
107 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
108 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
109 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
110 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
111 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
113 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
114 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
115 #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
132 #define ATMEL_SERIAL_RINGSIZE 1024
162 static unsigned long atmel_ports_in_use;
165 static struct console atmel_console;
168 #if defined(CONFIG_OF)
169 static const struct of_device_id atmel_serial_dt_ids[] = {
171 { .compatible =
"atmel,at91sam9260-usart" },
184 #ifdef CONFIG_SERIAL_ATMEL_PDC
227 atmel_port->
rs485 = *rs485conf;
230 dev_dbg(port->
dev,
"Setting UART to RS485\n");
236 dev_dbg(port->
dev,
"Setting UART to RS232\n");
237 if (atmel_use_dma_tx(port))
248 spin_unlock_irqrestore(&port->
lock, flags);
269 #ifdef CONFIG_ARCH_AT91RM9200
307 dev_dbg(port->
dev,
"Setting UART to RS485\n");
308 if ((atmel_port->
rs485.delay_rts_after_send) > 0)
310 atmel_port->
rs485.delay_rts_after_send);
313 dev_dbg(port->
dev,
"Setting UART to RS232\n");
345 static void atmel_stop_tx(
struct uart_port *port)
349 if (atmel_use_dma_tx(port)) {
358 atmel_start_rx(port);
364 static void atmel_start_tx(
struct uart_port *port)
368 if (atmel_use_dma_tx(port)) {
388 static void atmel_start_rx(
struct uart_port *port)
394 if (atmel_use_dma_rx(port)) {
407 static void atmel_stop_rx(
struct uart_port *port)
411 if (atmel_use_dma_rx(port)) {
424 static void atmel_enable_ms(
struct uart_port *port)
433 static void atmel_break_ctl(
struct uart_port *port,
int break_state)
435 if (break_state != 0)
445 atmel_buffer_rx_char(
struct uart_port *port,
unsigned int status,
469 static void atmel_pdc_rxerr(
struct uart_port *port,
unsigned int status)
490 static void atmel_rx_chars(
struct uart_port *port)
503 if (
unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
510 if (status & ATMEL_US_RXBRK
523 status &= ~ATMEL_US_RXBRK;
528 atmel_buffer_rx_char(port, status, ch);
532 tasklet_schedule(&atmel_port->
tasklet);
539 static void atmel_tx_chars(
struct uart_port *port)
572 atmel_handle_receive(
struct uart_port *port,
unsigned int pending)
576 if (atmel_use_dma_rx(port)) {
587 tasklet_schedule(&atmel_port->
tasklet);
591 ATMEL_US_FRAME | ATMEL_US_PARE))
592 atmel_pdc_rxerr(port, pending);
596 if (pending & ATMEL_US_RXRDY)
597 atmel_rx_chars(port);
598 else if (pending & ATMEL_US_RXBRK) {
613 atmel_handle_transmit(
struct uart_port *port,
unsigned int pending)
620 tasklet_schedule(&atmel_port->
tasklet);
628 atmel_handle_status(
struct uart_port *port,
unsigned int pending,
636 tasklet_schedule(&atmel_port->
tasklet);
646 unsigned int status, pending, pass_counter = 0;
654 atmel_handle_receive(port, pending);
655 atmel_handle_status(port, pending, status);
656 atmel_handle_transmit(port, pending);
665 static void atmel_tx_dma(
struct uart_port *port)
706 atmel_start_rx(port);
714 static void atmel_rx_from_ring(
struct uart_port *port)
739 if (
unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
741 if (status & ATMEL_US_RXBRK) {
746 if (uart_handle_break(port))
749 if (status & ATMEL_US_PARE)
751 if (status & ATMEL_US_FRAME)
758 if (status & ATMEL_US_RXBRK)
760 else if (status & ATMEL_US_PARE)
762 else if (status & ATMEL_US_FRAME)
777 spin_unlock(&port->
lock);
779 spin_lock(&port->
lock);
782 static void atmel_rx_from_dma(
struct uart_port *port)
796 pdc = &atmel_port->
pdc_rx[rx_idx];
812 if (
likely(head != tail)) {
824 tty_insert_flip_string(tty, pdc->
buf + pdc->
ofs, count);
851 spin_unlock(&port->
lock);
853 spin_lock(&port->
lock);
861 static void atmel_tasklet_func(
unsigned long data)
866 unsigned int status_change;
869 spin_lock(&port->
lock);
871 if (atmel_use_dma_tx(port))
874 atmel_tx_chars(port);
880 | ATMEL_US_DCD | ATMEL_US_CTS)) {
884 if (status_change & ATMEL_US_DSR)
886 if (status_change & ATMEL_US_DCD)
888 if (status_change & ATMEL_US_CTS)
896 if (atmel_use_dma_rx(port))
897 atmel_rx_from_dma(port);
899 atmel_rx_from_ring(port);
901 spin_unlock(&port->
lock);
907 static int atmel_startup(
struct uart_port *port)
924 tty ? tty->
name :
"atmel_serial", port);
926 printk(
"atmel_serial: atmel_startup - Can't get irq\n");
933 if (atmel_use_dma_rx(port)) {
936 for (i = 0; i < 2; i++) {
943 atmel_port->
pdc_rx[0].dma_addr,
967 if (atmel_use_dma_tx(port)) {
984 if (atmel_open_hook) {
985 retval = atmel_open_hook(port);
1003 if (atmel_use_dma_rx(port)) {
1022 static void atmel_shutdown(
struct uart_port *port)
1028 atmel_stop_rx(port);
1029 atmel_stop_tx(port);
1034 if (atmel_use_dma_rx(port)) {
1037 for (i = 0; i < 2; i++) {
1047 if (atmel_use_dma_tx(port)) {
1071 if (atmel_close_hook)
1072 atmel_close_hook(port);
1079 static void atmel_flush_buffer(
struct uart_port *port)
1083 if (atmel_use_dma_tx(port)) {
1085 atmel_port->
pdc_tx.ofs = 0;
1092 static void atmel_serial_pm(
struct uart_port *port,
unsigned int state,
1093 unsigned int oldstate)
1130 unsigned long flags;
1196 if (atmel_use_dma_rx(port))
1235 dev_dbg(port->
dev,
"Setting UART to RS485\n");
1236 if ((atmel_port->
rs485.delay_rts_after_send) > 0)
1238 atmel_port->
rs485.delay_rts_after_send);
1241 dev_dbg(port->
dev,
"Setting UART to RS232\n");
1257 port->
ops->enable_ms(port);
1259 spin_unlock_irqrestore(&port->
lock, flags);
1262 static void atmel_set_ldisc(
struct uart_port *port,
int new)
1266 atmel_enable_ms(port);
1275 static const char *atmel_type(
struct uart_port *port)
1283 static void atmel_release_port(
struct uart_port *port)
1299 static int atmel_request_port(
struct uart_port *port)
1321 static void atmel_config_port(
struct uart_port *port,
int flags)
1325 atmel_request_port(port);
1337 if (port->
irq != ser->
irq)
1352 #ifdef CONFIG_CONSOLE_POLL
1353 static int atmel_poll_get_char(
struct uart_port *port)
1361 static void atmel_poll_put_char(
struct uart_port *port,
unsigned char ch)
1371 atmel_ioctl(
struct uart_port *port,
unsigned int cmd,
unsigned long arg)
1386 &(to_atmel_uart_port(port)->rs485),
1399 static struct uart_ops atmel_pops = {
1400 .tx_empty = atmel_tx_empty,
1401 .set_mctrl = atmel_set_mctrl,
1402 .get_mctrl = atmel_get_mctrl,
1403 .stop_tx = atmel_stop_tx,
1404 .start_tx = atmel_start_tx,
1405 .stop_rx = atmel_stop_rx,
1406 .enable_ms = atmel_enable_ms,
1407 .break_ctl = atmel_break_ctl,
1408 .startup = atmel_startup,
1409 .shutdown = atmel_shutdown,
1410 .flush_buffer = atmel_flush_buffer,
1411 .set_termios = atmel_set_termios,
1412 .set_ldisc = atmel_set_ldisc,
1414 .release_port = atmel_release_port,
1415 .request_port = atmel_request_port,
1416 .config_port = atmel_config_port,
1417 .verify_port = atmel_verify_port,
1418 .pm = atmel_serial_pm,
1419 .ioctl = atmel_ioctl,
1420 #ifdef CONFIG_CONSOLE_POLL
1421 .poll_get_char = atmel_poll_get_char,
1422 .poll_put_char = atmel_poll_put_char,
1443 rs485_delay, 2) == 0) {
1448 rs485conf->
flags = 0;
1467 if (pdev->
dev.of_node) {
1468 atmel_of_init_port(atmel_port, pdev->
dev.of_node);
1477 port->
ops = &atmel_pops;
1484 (
unsigned long)port);
1488 if (pdata && pdata->
regs) {
1497 if (!atmel_port->
clk) {
1508 else if (atmel_use_dma_tx(port)) {
1527 atmel_open_hook = fns->
open;
1528 atmel_close_hook = fns->
close;
1529 atmel_pops.
pm = fns->
pm;
1535 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1536 static void atmel_console_putchar(
struct uart_port *port,
int ch)
1546 static void atmel_console_write(
struct console *co,
const char *
s,
u_int count)
1585 static void __init atmel_console_get_options(
struct uart_port *port,
int *baud,
1586 int *parity,
int *
bits)
1588 unsigned int mr, quot;
1616 *baud = port->
uartclk / (16 * (quot - 1));
1641 atmel_console_get_options(port, &baud, &parity, &bits);
1648 static struct console atmel_console = {
1650 .write = atmel_console_write,
1652 .setup = atmel_console_setup,
1655 .data = &atmel_uart,
1658 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
1663 static int __init atmel_console_init(
void)
1665 if (atmel_default_console_device) {
1667 atmel_default_console_device->
dev.platform_data;
1668 int id = pdata->
num;
1675 atmel_init_port(port, atmel_default_console_device);
1687 static int __init atmel_late_console_init(
void)
1689 if (atmel_default_console_device
1698 static inline bool atmel_is_console_port(
struct uart_port *port)
1704 #define ATMEL_CONSOLE_DEVICE NULL
1706 static inline bool atmel_is_console_port(
struct uart_port *port)
1714 .driver_name =
"atmel_serial",
1723 static bool atmel_serial_clk_will_stop(
void)
1725 #ifdef CONFIG_ARCH_AT91
1735 struct uart_port *port = platform_get_drvdata(pdev);
1746 if (atmel_serial_clk_will_stop())
1756 struct uart_port *port = platform_get_drvdata(pdev);
1765 #define atmel_serial_suspend NULL
1766 #define atmel_serial_resume NULL
1789 sizeof(atmel_ports_in_use));
1802 port = &atmel_ports[
ret];
1806 atmel_init_port(port, pdev);
1808 if (!atmel_use_dma_rx(&port->
uart)) {
1813 goto err_alloc_ring;
1821 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1822 if (atmel_is_console_port(&port->
uart)
1833 platform_set_drvdata(pdev, port);
1846 if (!atmel_is_console_port(&port->
uart)) {
1856 struct uart_port *port = platform_get_drvdata(pdev);
1861 platform_set_drvdata(pdev,
NULL);
1878 .probe = atmel_serial_probe,
1883 .name =
"atmel_usart",
1889 static int __init atmel_serial_init(
void)
1904 static void __exit atmel_serial_exit(
void)