xref: /optee_os/core/drivers/pl011.c (revision b1469ba0bfd0371eb52bd50f5c52eeda7a8f5f1e)
1 /*
2  * Copyright (c) 2014, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <assert.h>
28 #include <drivers/pl011.h>
29 #include <io.h>
30 #include <keep.h>
31 #include <kernel/dt.h>
32 #include <stdlib.h>
33 #include <trace.h>
34 #include <types_ext.h>
35 #include <util.h>
36 
37 #define UART_DR		0x00 /* data register */
38 #define UART_RSR_ECR	0x04 /* receive status or error clear */
39 #define UART_DMAWM	0x08 /* DMA watermark configure */
40 #define UART_TIMEOUT	0x0C /* Timeout period */
41 /* reserved space */
42 #define UART_FR		0x18 /* flag register */
43 #define UART_ILPR	0x20 /* IrDA low-poer */
44 #define UART_IBRD	0x24 /* integer baud register */
45 #define UART_FBRD	0x28 /* fractional baud register */
46 #define UART_LCR_H	0x2C /* line control register */
47 #define UART_CR		0x30 /* control register */
48 #define UART_IFLS	0x34 /* interrupt FIFO level select */
49 #define UART_IMSC	0x38 /* interrupt mask set/clear */
50 #define UART_RIS	0x3C /* raw interrupt register */
51 #define UART_MIS	0x40 /* masked interrupt register */
52 #define UART_ICR	0x44 /* interrupt clear register */
53 #define UART_DMACR	0x48 /* DMA control register */
54 
55 /* flag register bits */
56 #define UART_FR_RTXDIS	(1 << 13)
57 #define UART_FR_TERI	(1 << 12)
58 #define UART_FR_DDCD	(1 << 11)
59 #define UART_FR_DDSR	(1 << 10)
60 #define UART_FR_DCTS	(1 << 9)
61 #define UART_FR_RI	(1 << 8)
62 #define UART_FR_TXFE	(1 << 7)
63 #define UART_FR_RXFF	(1 << 6)
64 #define UART_FR_TXFF	(1 << 5)
65 #define UART_FR_RXFE	(1 << 4)
66 #define UART_FR_BUSY	(1 << 3)
67 #define UART_FR_DCD	(1 << 2)
68 #define UART_FR_DSR	(1 << 1)
69 #define UART_FR_CTS	(1 << 0)
70 
71 /* transmit/receive line register bits */
72 #define UART_LCRH_SPS		(1 << 7)
73 #define UART_LCRH_WLEN_8	(3 << 5)
74 #define UART_LCRH_WLEN_7	(2 << 5)
75 #define UART_LCRH_WLEN_6	(1 << 5)
76 #define UART_LCRH_WLEN_5	(0 << 5)
77 #define UART_LCRH_FEN		(1 << 4)
78 #define UART_LCRH_STP2		(1 << 3)
79 #define UART_LCRH_EPS		(1 << 2)
80 #define UART_LCRH_PEN		(1 << 1)
81 #define UART_LCRH_BRK		(1 << 0)
82 
83 /* control register bits */
84 #define UART_CR_CTSEN		(1 << 15)
85 #define UART_CR_RTSEN		(1 << 14)
86 #define UART_CR_OUT2		(1 << 13)
87 #define UART_CR_OUT1		(1 << 12)
88 #define UART_CR_RTS		(1 << 11)
89 #define UART_CR_DTR		(1 << 10)
90 #define UART_CR_RXE		(1 << 9)
91 #define UART_CR_TXE		(1 << 8)
92 #define UART_CR_LPE		(1 << 7)
93 #define UART_CR_OVSFACT		(1 << 3)
94 #define UART_CR_UARTEN		(1 << 0)
95 
96 #define UART_IMSC_RTIM		(1 << 6)
97 #define UART_IMSC_RXIM		(1 << 4)
98 
99 static vaddr_t chip_to_base(struct serial_chip *chip)
100 {
101 	struct pl011_data *pd =
102 		container_of(chip, struct pl011_data, chip);
103 
104 	return io_pa_or_va(&pd->base);
105 }
106 
107 static void pl011_flush(struct serial_chip *chip)
108 {
109 	vaddr_t base = chip_to_base(chip);
110 
111 	/*
112 	 * Wait for the transmit FIFO to be empty.
113 	 * It can happen that Linux initializes the OP-TEE driver with the
114 	 * console UART disabled; avoid an infinite loop by checking the UART
115 	 * enabled flag. Checking it in the loop makes the code safe against
116 	 * asynchronous disable.
117 	 */
118 	while ((read32(base + UART_CR) & UART_CR_UARTEN) &&
119 	       !(read32(base + UART_FR) & UART_FR_TXFE))
120 		;
121 }
122 
123 static bool pl011_have_rx_data(struct serial_chip *chip)
124 {
125 	vaddr_t base = chip_to_base(chip);
126 
127 	return !(read32(base + UART_FR) & UART_FR_RXFE);
128 }
129 
130 static int pl011_getchar(struct serial_chip *chip)
131 {
132 	vaddr_t base = chip_to_base(chip);
133 
134 	while (!pl011_have_rx_data(chip))
135 		;
136 	return read32(base + UART_DR) & 0xff;
137 }
138 
139 static void pl011_putc(struct serial_chip *chip, int ch)
140 {
141 	vaddr_t base = chip_to_base(chip);
142 
143 	/* Wait until there is space in the FIFO or device is disabled */
144 	while (read32(base + UART_FR) & UART_FR_TXFF)
145 		;
146 
147 	/* Send the character */
148 	write32(ch, base + UART_DR);
149 }
150 
151 static const struct serial_ops pl011_ops = {
152 	.flush = pl011_flush,
153 	.getchar = pl011_getchar,
154 	.have_rx_data = pl011_have_rx_data,
155 	.putc = pl011_putc,
156 };
157 KEEP_PAGER(pl011_ops);
158 
159 void pl011_init(struct pl011_data *pd, paddr_t pbase, uint32_t uart_clk,
160 		uint32_t baud_rate)
161 {
162 	vaddr_t base;
163 
164 	pd->base.pa = pbase;
165 	pd->chip.ops = &pl011_ops;
166 
167 	base = io_pa_or_va(&pd->base);
168 
169 	/* Clear all errors */
170 	write32(0, base + UART_RSR_ECR);
171 	/* Disable everything */
172 	write32(0, base + UART_CR);
173 
174 	if (baud_rate) {
175 		uint32_t divisor = (uart_clk * 4) / baud_rate;
176 
177 		write32(divisor >> 6, base + UART_IBRD);
178 		write32(divisor & 0x3f, base + UART_FBRD);
179 	}
180 
181 	/* Configure TX to 8 bits, 1 stop bit, no parity, fifo disabled. */
182 	write32(UART_LCRH_WLEN_8, base + UART_LCR_H);
183 
184 	/* Enable interrupts for receive and receive timeout */
185 	write32(UART_IMSC_RXIM | UART_IMSC_RTIM, base + UART_IMSC);
186 
187 	/* Enable UART and RX/TX */
188 	write32(UART_CR_UARTEN | UART_CR_TXE | UART_CR_RXE, base + UART_CR);
189 
190 	pl011_flush(&pd->chip);
191 }
192 
193 #ifdef CFG_DT
194 
195 static struct serial_chip *pl011_dev_alloc(void)
196 {
197 	struct pl011_data *pd = malloc(sizeof(*pd));
198 
199 	if (!pd)
200 		return NULL;
201 	return &pd->chip;
202 }
203 
204 static int pl011_dev_init(struct serial_chip *chip, const void *fdt, int offs,
205 			  const char *parms)
206 {
207 	struct pl011_data *pd = container_of(chip, struct pl011_data, chip);
208 	vaddr_t vbase;
209 	paddr_t pbase;
210 	size_t size;
211 
212 	if (parms && parms[0])
213 		IMSG("pl011: device parameters ignored (%s)", parms);
214 
215 	if (dt_map_dev(fdt, offs, &vbase, &size) < 0)
216 		return -1;
217 
218 	if (size != 0x1000) {
219 		EMSG("pl011: unexpected register size: %zx", size);
220 		return -1;
221 	}
222 
223 	pbase = virt_to_phys((void *)vbase);
224 	pl011_init(pd, pbase, 0, 0);
225 
226 	return 0;
227 }
228 
229 static void pl011_dev_free(struct serial_chip *chip)
230 {
231 	struct pl011_data *pd = container_of(chip, struct pl011_data, chip);
232 
233 	free(pd);
234 }
235 
236 static const struct serial_driver pl011_driver = {
237 	.dev_alloc = pl011_dev_alloc,
238 	.dev_init = pl011_dev_init,
239 	.dev_free = pl011_dev_free,
240 };
241 
242 static const struct dt_device_match pl011_match_table[] = {
243 	{ .compatible = "arm,pl011" },
244 	{ 0 }
245 };
246 
247 const struct dt_driver pl011_dt_driver __dt_driver = {
248 	.name = "pl011",
249 	.match_table = pl011_match_table,
250 	.driver = &pl011_driver,
251 };
252 
253 #endif /* CFG_DT */
254