3 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
7 * Philippe Robin, <philippe.robin@arm.com>
9 * SPDX-License-Identifier: GPL-2.0+
12 /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
18 #include <linux/compiler.h>
19 #include "serial_pl01x.h"
22 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
23 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
24 * Versatile PB has four UARTs.
26 #define CONSOLE_PORT CONFIG_CONS_INDEX
27 static volatile unsigned char *const port
[] = CONFIG_PL01x_PORTS
;
28 #define NUM_PORTS (sizeof(port)/sizeof(port[0]))
30 static void pl01x_putc (int portnum
, char c
);
31 static int pl01x_getc (int portnum
);
32 static int pl01x_tstc (int portnum
);
33 unsigned int baudrate
= CONFIG_BAUDRATE
;
34 DECLARE_GLOBAL_DATA_PTR
;
36 static struct pl01x_regs
*pl01x_get_regs(int portnum
)
38 return (struct pl01x_regs
*) port
[portnum
];
41 #ifdef CONFIG_PL010_SERIAL
43 static int pl01x_serial_init(void)
45 struct pl01x_regs
*regs
= pl01x_get_regs(CONSOLE_PORT
);
48 /* First, disable everything */
49 writel(0, ®s
->pl010_cr
);
54 divisor
= UART_PL010_BAUD_9600
;
58 divisor
= UART_PL010_BAUD_9600
;
62 divisor
= UART_PL010_BAUD_38400
;
66 divisor
= UART_PL010_BAUD_57600
;
70 divisor
= UART_PL010_BAUD_115200
;
74 divisor
= UART_PL010_BAUD_38400
;
77 writel((divisor
& 0xf00) >> 8, ®s
->pl010_lcrm
);
78 writel(divisor
& 0xff, ®s
->pl010_lcrl
);
80 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
81 writel(UART_PL010_LCRH_WLEN_8
| UART_PL010_LCRH_FEN
, ®s
->pl010_lcrh
);
83 /* Finally, enable the UART */
84 writel(UART_PL010_CR_UARTEN
, ®s
->pl010_cr
);
89 #endif /* CONFIG_PL010_SERIAL */
91 #ifdef CONFIG_PL011_SERIAL
93 static int pl01x_serial_init(void)
95 struct pl01x_regs
*regs
= pl01x_get_regs(CONSOLE_PORT
);
98 unsigned int remainder
;
99 unsigned int fraction
;
102 #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
103 /* Empty RX fifo if necessary */
104 if (readl(®s
->pl011_cr
) & UART_PL011_CR_UARTEN
) {
105 while (!(readl(®s
->fr
) & UART_PL01x_FR_RXFE
))
110 /* First, disable everything */
111 writel(0, ®s
->pl011_cr
);
116 * IBRD = UART_CLK / (16 * BAUD_RATE)
117 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
119 temp
= 16 * baudrate
;
120 divider
= CONFIG_PL011_CLOCK
/ temp
;
121 remainder
= CONFIG_PL011_CLOCK
% temp
;
122 temp
= (8 * remainder
) / baudrate
;
123 fraction
= (temp
>> 1) + (temp
& 1);
125 writel(divider
, ®s
->pl011_ibrd
);
126 writel(fraction
, ®s
->pl011_fbrd
);
128 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
129 lcr
= UART_PL011_LCRH_WLEN_8
| UART_PL011_LCRH_FEN
;
130 writel(lcr
, ®s
->pl011_lcrh
);
132 #ifdef CONFIG_PL011_SERIAL_RLCR
137 * Program receive line control register after waiting
138 * 10 bus cycles. Delay be writing to readonly register
141 for (i
= 0; i
< 10; i
++)
142 writel(lcr
, ®s
->fr
);
144 writel(lcr
, ®s
->pl011_rlcr
);
145 /* lcrh needs to be set again for change to be effective */
146 writel(lcr
, ®s
->pl011_lcrh
);
149 /* Finally, enable the UART */
150 writel(UART_PL011_CR_UARTEN
| UART_PL011_CR_TXE
| UART_PL011_CR_RXE
|
151 UART_PL011_CR_RTS
, ®s
->pl011_cr
);
156 #endif /* CONFIG_PL011_SERIAL */
158 static void pl01x_serial_putc(const char c
)
161 pl01x_putc (CONSOLE_PORT
, '\r');
163 pl01x_putc (CONSOLE_PORT
, c
);
166 static int pl01x_serial_getc(void)
168 return pl01x_getc (CONSOLE_PORT
);
171 static int pl01x_serial_tstc(void)
173 return pl01x_tstc (CONSOLE_PORT
);
176 static void pl01x_serial_setbrg(void)
178 struct pl01x_regs
*regs
= pl01x_get_regs(CONSOLE_PORT
);
180 baudrate
= gd
->baudrate
;
182 * Flush FIFO and wait for non-busy before changing baudrate to avoid
185 while (!(readl(®s
->fr
) & UART_PL01x_FR_TXFE
))
187 while (readl(®s
->fr
) & UART_PL01x_FR_BUSY
)
192 static void pl01x_putc (int portnum
, char c
)
194 struct pl01x_regs
*regs
= pl01x_get_regs(portnum
);
196 /* Wait until there is space in the FIFO */
197 while (readl(®s
->fr
) & UART_PL01x_FR_TXFF
)
200 /* Send the character */
201 writel(c
, ®s
->dr
);
204 static int pl01x_getc (int portnum
)
206 struct pl01x_regs
*regs
= pl01x_get_regs(portnum
);
209 /* Wait until there is data in the FIFO */
210 while (readl(®s
->fr
) & UART_PL01x_FR_RXFE
)
213 data
= readl(®s
->dr
);
215 /* Check for an error flag */
216 if (data
& 0xFFFFFF00) {
217 /* Clear the error */
218 writel(0xFFFFFFFF, ®s
->ecr
);
225 static int pl01x_tstc (int portnum
)
227 struct pl01x_regs
*regs
= pl01x_get_regs(portnum
);
230 return !(readl(®s
->fr
) & UART_PL01x_FR_RXFE
);
233 static struct serial_device pl01x_serial_drv
= {
234 .name
= "pl01x_serial",
235 .start
= pl01x_serial_init
,
237 .setbrg
= pl01x_serial_setbrg
,
238 .putc
= pl01x_serial_putc
,
239 .puts
= default_serial_puts
,
240 .getc
= pl01x_serial_getc
,
241 .tstc
= pl01x_serial_tstc
,
244 void pl01x_serial_initialize(void)
246 serial_register(&pl01x_serial_drv
);
249 __weak
struct serial_device
*default_serial_console(void)
251 return &pl01x_serial_drv
;