15 #include <linux/serial.h>
16 #include <linux/serial_core.h>
17 #include <linux/tty.h>
23 #include <linux/module.h>
25 #define XUARTPS_TTY_NAME "ttyPS"
26 #define XUARTPS_NAME "xuartps"
27 #define XUARTPS_MAJOR 0
28 #define XUARTPS_MINOR 0
29 #define XUARTPS_NR_PORTS 2
30 #define XUARTPS_FIFO_SIZE 16
31 #define XUARTPS_REGISTER_SPACE 0xFFF
33 #define xuartps_readl(offset) ioread32(port->membase + offset)
34 #define xuartps_writel(val, offset) iowrite32(val, port->membase + offset)
42 #define XUARTPS_CR_OFFSET 0x00
43 #define XUARTPS_MR_OFFSET 0x04
44 #define XUARTPS_IER_OFFSET 0x08
45 #define XUARTPS_IDR_OFFSET 0x0C
46 #define XUARTPS_IMR_OFFSET 0x10
47 #define XUARTPS_ISR_OFFSET 0x14
48 #define XUARTPS_BAUDGEN_OFFSET 0x18
49 #define XUARTPS_RXTOUT_OFFSET 0x1C
50 #define XUARTPS_RXWM_OFFSET 0x20
51 #define XUARTPS_MODEMCR_OFFSET 0x24
52 #define XUARTPS_MODEMSR_OFFSET 0x28
53 #define XUARTPS_SR_OFFSET 0x2C
54 #define XUARTPS_FIFO_OFFSET 0x30
55 #define XUARTPS_BAUDDIV_OFFSET 0x34
56 #define XUARTPS_FLOWDEL_OFFSET 0x38
57 #define XUARTPS_IRRX_PWIDTH_OFFSET 0x3C
59 #define XUARTPS_IRTX_PWIDTH_OFFSET 0x40
61 #define XUARTPS_TXWM_OFFSET 0x44
69 #define XUARTPS_CR_STOPBRK 0x00000100
70 #define XUARTPS_CR_STARTBRK 0x00000080
71 #define XUARTPS_CR_TX_DIS 0x00000020
72 #define XUARTPS_CR_TX_EN 0x00000010
73 #define XUARTPS_CR_RX_DIS 0x00000008
74 #define XUARTPS_CR_RX_EN 0x00000004
75 #define XUARTPS_CR_TXRST 0x00000002
76 #define XUARTPS_CR_RXRST 0x00000001
77 #define XUARTPS_CR_RST_TO 0x00000040
88 #define XUARTPS_MR_CLKSEL 0x00000001
89 #define XUARTPS_MR_CHMODE_L_LOOP 0x00000200
90 #define XUARTPS_MR_CHMODE_NORM 0x00000000
92 #define XUARTPS_MR_STOPMODE_2_BIT 0x00000080
93 #define XUARTPS_MR_STOPMODE_1_BIT 0x00000000
95 #define XUARTPS_MR_PARITY_NONE 0x00000020
96 #define XUARTPS_MR_PARITY_MARK 0x00000018
97 #define XUARTPS_MR_PARITY_SPACE 0x00000010
98 #define XUARTPS_MR_PARITY_ODD 0x00000008
99 #define XUARTPS_MR_PARITY_EVEN 0x00000000
101 #define XUARTPS_MR_CHARLEN_6_BIT 0x00000006
102 #define XUARTPS_MR_CHARLEN_7_BIT 0x00000004
103 #define XUARTPS_MR_CHARLEN_8_BIT 0x00000000
117 #define XUARTPS_IXR_TOUT 0x00000100
118 #define XUARTPS_IXR_PARITY 0x00000080
119 #define XUARTPS_IXR_FRAMING 0x00000040
120 #define XUARTPS_IXR_OVERRUN 0x00000020
121 #define XUARTPS_IXR_TXFULL 0x00000010
122 #define XUARTPS_IXR_TXEMPTY 0x00000008
123 #define XUARTPS_ISR_RXEMPTY 0x00000002
124 #define XUARTPS_IXR_RXTRIG 0x00000001
125 #define XUARTPS_IXR_RXFULL 0x00000004
126 #define XUARTPS_IXR_RXEMPTY 0x00000002
127 #define XUARTPS_IXR_MASK 0x00001FFF
135 #define XUARTPS_SR_RXEMPTY 0x00000002
136 #define XUARTPS_SR_TXEMPTY 0x00000008
137 #define XUARTPS_SR_TXFULL 0x00000010
138 #define XUARTPS_SR_RXTRIG 0x00000001
152 unsigned int isrstatus, numbytes;
181 if (isrstatus & XUARTPS_IXR_PARITY) {
192 XUARTPS_IXR_OVERRUN, data,
195 spin_unlock(&port->
lock);
198 spin_lock(&port->
lock);
225 port->
state->xmit.tail =
226 (port->
state->xmit.tail + 1) & \
239 spin_unlock_irqrestore(&port->
lock, flags);
253 static unsigned int xuartps_set_baud_rate(
struct uart_port *
port,
256 unsigned int sel_clk;
257 unsigned int calc_baud = 0;
258 unsigned int brgr_val, brdiv_val;
259 unsigned int bauderror;
273 sel_clk = sel_clk / 8;
276 for (brdiv_val = 4; brdiv_val < 255; brdiv_val++) {
278 brgr_val = sel_clk / (baud * (brdiv_val + 1));
279 if (brgr_val < 2 || brgr_val > 65535)
282 calc_baud = sel_clk / (brgr_val * (brdiv_val + 1));
284 if (baud > calc_baud)
285 bauderror = baud - calc_baud;
287 bauderror = calc_baud -
baud;
290 if (((bauderror * 100) /
baud) < 3) {
310 static void xuartps_start_tx(
struct uart_port *port)
335 port->
state->xmit.buf[port->
state->xmit.tail],
342 port->
state->xmit.tail = (port->
state->xmit.tail + 1) &
358 static void xuartps_stop_tx(
struct uart_port *port)
373 static void xuartps_stop_rx(
struct uart_port *port)
389 static unsigned int xuartps_tx_empty(
struct uart_port *port)
404 static void xuartps_break_ctl(
struct uart_port *port,
int ctl)
421 spin_unlock_irqrestore(&port->
lock, flags);
432 static void xuartps_set_termios(
struct uart_port *port,
435 unsigned int cval = 0;
455 baud = xuartps_set_baud_rate(port, baud);
538 spin_unlock_irqrestore(&port->
lock, flags);
547 static int xuartps_startup(
struct uart_port *port)
549 unsigned int retval = 0, status = 0;
605 static void xuartps_shutdown(
struct uart_port *port)
625 static const char *xuartps_type(
struct uart_port *port)
637 static int xuartps_verify_port(
struct uart_port *port,
642 if (port->
irq != ser->
irq)
661 static int xuartps_request_port(
struct uart_port *port)
670 dev_err(port->
dev,
"Unable to map registers\n");
684 static void xuartps_release_port(
struct uart_port *port)
698 static void xuartps_config_port(
struct uart_port *port,
int flags)
712 static unsigned int xuartps_get_mctrl(
struct uart_port *port)
717 static void xuartps_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
722 static void xuartps_enable_ms(
struct uart_port *port)
729 static struct uart_ops xuartps_ops = {
730 .set_mctrl = xuartps_set_mctrl,
731 .get_mctrl = xuartps_get_mctrl,
732 .enable_ms = xuartps_enable_ms,
734 .start_tx = xuartps_start_tx,
735 .stop_tx = xuartps_stop_tx,
736 .stop_rx = xuartps_stop_rx,
737 .tx_empty = xuartps_tx_empty,
738 .break_ctl = xuartps_break_ctl,
741 .set_termios = xuartps_set_termios,
742 .startup = xuartps_startup,
743 .shutdown = xuartps_shutdown,
744 .type = xuartps_type,
745 .verify_port = xuartps_verify_port,
748 .request_port = xuartps_request_port,
752 .release_port = xuartps_release_port,
756 .config_port = xuartps_config_port,
769 static struct uart_port *xuartps_get_port(
void)
776 if (xuartps_port[
id].
mapbase == 0)
779 if (
id >= XUARTPS_NR_PORTS)
782 port = &xuartps_port[
id];
792 port->
ops = &xuartps_ops;
801 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
807 static void xuartps_console_wait_tx(
struct uart_port *port)
810 != XUARTPS_SR_TXEMPTY)
820 static void xuartps_console_putchar(
struct uart_port *port,
int ch)
822 xuartps_console_wait_tx(port);
832 static void xuartps_console_write(
struct console *co,
const char *
s,
850 xuartps_console_wait_tx(port);
860 spin_unlock_irqrestore(&port->
lock, flags);
878 if (co->
index < 0 || co->
index >= XUARTPS_NR_PORTS)
894 static struct console xuartps_console = {
896 .write = xuartps_console_write,
898 .setup = xuartps_console_setup,
901 .data = &xuartps_uart_driver,
909 static int __init xuartps_console_init(
void)
928 #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
929 .cons = &xuartps_console,
950 const unsigned int *prop;
956 clk = *((
unsigned int *)(pdev->
dev.platform_data));
972 port = xuartps_get_port();
975 dev_err(&pdev->
dev,
"Cannot get uart_port structure\n");
990 "uart_add_one_port() failed; err=%i\n", rc);
1050 { .compatible =
"xlnx,xuartps", },
1055 #define xuartps_of_match NULL
1059 .probe = xuartps_probe,
1060 .remove =
__exit_p(xuartps_remove),
1061 .suspend = xuartps_suspend,
1062 .resume = xuartps_resume,
1078 static int __init xuartps_init(
void)
1098 static void __exit xuartps_exit(
void)