1 /*
2 * xen/drivers/char/cadence-uart.c
3 *
4 * Driver for Cadence UART in Xilinx ZynqMP.
5 *
6 * Written by Edgar E. Iglesias <edgar.iglesias@gmail.com>
7 * Copyright (c) 2015 Xilinx Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20 #include <xen/console.h>
21 #include <xen/serial.h>
22 #include <xen/init.h>
23 #include <xen/irq.h>
24 #include <xen/device_tree.h>
25 #include <xen/errno.h>
26 #include <asm/device.h>
27 #include <xen/mm.h>
28 #include <xen/vmap.h>
29 #include <asm/cadence-uart.h>
30 #include <asm/io.h>
31
32 static struct cuart {
33 unsigned int irq;
34 void __iomem *regs;
35 /* UART with IRQ line: interrupt-driven I/O. */
36 struct irqaction irqaction;
37 struct vuart_info vuart;
38 } cuart_com = {0};
39
40 #define cuart_read(uart, off) readl((uart)->regs + (off))
41 #define cuart_write(uart, off,val) writel((val), (uart)->regs + (off))
42
cuart_interrupt(int irq,void * data,struct cpu_user_regs * regs)43 static void cuart_interrupt(int irq, void *data, struct cpu_user_regs *regs)
44 {
45 struct serial_port *port = data;
46 struct cuart *uart = port->uart;
47 unsigned int status;
48
49 do {
50 status = cuart_read(uart, R_UART_SR);
51 /* ACK. */
52 if ( status & UART_SR_INTR_RTRIG )
53 {
54 serial_rx_interrupt(port, regs);
55 cuart_write(uart, R_UART_CISR, UART_SR_INTR_RTRIG);
56 }
57 } while ( status & UART_SR_INTR_RTRIG );
58 }
59
cuart_init_preirq(struct serial_port * port)60 static void __init cuart_init_preirq(struct serial_port *port)
61 {
62 struct cuart *uart = port->uart;
63
64 cuart_write(uart, R_UART_MR, UART_MR_NO_PARITY);
65 /* Enable and Reset both the RX and TX paths. */
66 cuart_write(uart, R_UART_CR, UART_CR_RX_RST | UART_CR_TX_RST |
67 UART_CR_RX_ENABLE | UART_CR_TX_ENABLE);
68 }
69
cuart_init_postirq(struct serial_port * port)70 static void __init cuart_init_postirq(struct serial_port *port)
71 {
72 struct cuart *uart = port->uart;
73 int rc;
74
75 if ( uart->irq > 0 )
76 {
77 uart->irqaction.handler = cuart_interrupt;
78 uart->irqaction.name = "cadence-uart";
79 uart->irqaction.dev_id = port;
80 if ( (rc = setup_irq(uart->irq, 0, &uart->irqaction)) != 0 )
81 printk("ERROR: Failed to allocate cadence-uart IRQ %d\n", uart->irq);
82 }
83
84 /* Clear pending error interrupts */
85 cuart_write(uart, R_UART_RTRIG, 1);
86 cuart_write(uart, R_UART_CISR, ~0);
87
88 /* Unmask interrupts */
89 cuart_write(uart, R_UART_IDR, ~0);
90 cuart_write(uart, R_UART_IER, UART_SR_INTR_RTRIG);
91 }
92
cuart_suspend(struct serial_port * port)93 static void cuart_suspend(struct serial_port *port)
94 {
95 BUG();
96 }
97
cuart_resume(struct serial_port * port)98 static void cuart_resume(struct serial_port *port)
99 {
100 BUG();
101 }
102
cuart_tx_ready(struct serial_port * port)103 static int cuart_tx_ready(struct serial_port *port)
104 {
105 struct cuart *uart = port->uart;
106 unsigned int status = cuart_read(uart, R_UART_SR);
107
108 return !( status & UART_SR_INTR_TFUL );
109 }
110
cuart_putc(struct serial_port * port,char c)111 static void cuart_putc(struct serial_port *port, char c)
112 {
113 struct cuart *uart = port->uart;
114
115 cuart_write(uart, R_UART_TX, (uint32_t)(unsigned char)c);
116 }
117
cuart_getc(struct serial_port * port,char * pc)118 static int cuart_getc(struct serial_port *port, char *pc)
119 {
120 struct cuart *uart = port->uart;
121
122 if ( cuart_read(uart, R_UART_SR) & UART_SR_INTR_REMPTY )
123 return 0;
124
125 *pc = cuart_read(uart, R_UART_RX) & 0xff;
126 return 1;
127 }
128
cuart_irq(struct serial_port * port)129 static int __init cuart_irq(struct serial_port *port)
130 {
131 struct cuart *uart = port->uart;
132
133 return ( (uart->irq > 0) ? uart->irq : -1 );
134 }
135
cuart_vuart(struct serial_port * port)136 static const struct vuart_info *cuart_vuart(struct serial_port *port)
137 {
138 struct cuart *uart = port->uart;
139
140 return &uart->vuart;
141 }
142
143 static struct uart_driver __read_mostly cuart_driver = {
144 .init_preirq = cuart_init_preirq,
145 .init_postirq = cuart_init_postirq,
146 .endboot = NULL,
147 .suspend = cuart_suspend,
148 .resume = cuart_resume,
149 .tx_ready = cuart_tx_ready,
150 .putc = cuart_putc,
151 .getc = cuart_getc,
152 .irq = cuart_irq,
153 .vuart_info = cuart_vuart,
154 };
155
cuart_init(struct dt_device_node * dev,const void * data)156 static int __init cuart_init(struct dt_device_node *dev, const void *data)
157 {
158 const char *config = data;
159 struct cuart *uart;
160 int res;
161 u64 addr, size;
162
163 if ( strcmp(config, "") )
164 printk("WARNING: UART configuration is not supported\n");
165
166 uart = &cuart_com;
167
168 res = dt_device_get_address(dev, 0, &addr, &size);
169 if ( res )
170 {
171 printk("cadence: Unable to retrieve the base"
172 " address of the UART\n");
173 return res;
174 }
175
176 res = platform_get_irq(dev, 0);
177 if ( res < 0 )
178 {
179 printk("cadence: Unable to retrieve the IRQ\n");
180 return -EINVAL;
181 }
182 uart->irq = res;
183
184 uart->regs = ioremap_nocache(addr, size);
185 if ( !uart->regs )
186 {
187 printk("cadence: Unable to map the UART memory\n");
188 return -ENOMEM;
189 }
190
191 uart->vuart.base_addr = addr;
192 uart->vuart.size = size;
193 uart->vuart.data_off = R_UART_RX;
194 uart->vuart.status_off = R_UART_SR;
195 uart->vuart.status = UART_SR_INTR_TEMPTY;
196
197 /* Register with generic serial driver. */
198 serial_register_uart(SERHND_DTUART, &cuart_driver, uart);
199
200 dt_device_set_used_by(dev, DOMID_XEN);
201
202 return 0;
203 }
204
205 static const struct dt_device_match cuart_dt_match[] __initconst =
206 {
207 DT_MATCH_COMPATIBLE("cdns,uart-r1p8"),
208 DT_MATCH_COMPATIBLE("cdns,uart-r1p12"),
209 { /* sentinel */ },
210 };
211
212 DT_DEVICE_START(cuart, "Cadence UART", DEVICE_SERIAL)
213 .dt_match = cuart_dt_match,
214 .init = cuart_init,
215 DT_DEVICE_END
216
217 /*
218 * Local variables:
219 * mode: C
220 * c-file-style: "BSD"
221 * c-basic-offset: 4
222 * indent-tabs-mode: nil
223 * End:
224 */
225