blob: dfb610e1a9d553429d1462335a6550f6d31afe04 [file] [log] [blame]
wdenk3d3befa2004-03-14 15:06:13 +00001/*
2 * (C) Copyright 2000
3 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
4 *
5 * (C) Copyright 2004
6 * ARM Ltd.
7 * Philippe Robin, <philippe.robin@arm.com>
8 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk3d3befa2004-03-14 15:06:13 +000010 */
11
Andreas Engel48d01922008-09-08 14:30:53 +020012/* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
wdenk3d3befa2004-03-14 15:06:13 +000013
14#include <common.h>
Stuart Wood8b616ed2008-06-02 16:42:19 -040015#include <watchdog.h>
Matt Waddel249d5212010-10-07 15:48:46 -060016#include <asm/io.h>
Marek Vasut39f61472012-09-14 22:38:46 +020017#include <serial.h>
18#include <linux/compiler.h>
Andreas Engel20c92262008-09-08 10:17:31 +020019#include "serial_pl01x.h"
wdenk3d3befa2004-03-14 15:06:13 +000020
Andreas Engel20c92262008-09-08 10:17:31 +020021/*
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.
25 */
wdenk3d3befa2004-03-14 15:06:13 +000026#define CONSOLE_PORT CONFIG_CONS_INDEX
wdenk6705d812004-08-02 23:22:59 +000027static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
28#define NUM_PORTS (sizeof(port)/sizeof(port[0]))
wdenk3d3befa2004-03-14 15:06:13 +000029
Andreas Engel20c92262008-09-08 10:17:31 +020030static void pl01x_putc (int portnum, char c);
31static int pl01x_getc (int portnum);
32static int pl01x_tstc (int portnum);
Matt Waddel249d5212010-10-07 15:48:46 -060033unsigned int baudrate = CONFIG_BAUDRATE;
34DECLARE_GLOBAL_DATA_PTR;
wdenk3d3befa2004-03-14 15:06:13 +000035
Rabin Vincent72d5e442010-05-05 09:23:07 +053036static struct pl01x_regs *pl01x_get_regs(int portnum)
37{
38 return (struct pl01x_regs *) port[portnum];
39}
40
Andreas Engel48d01922008-09-08 14:30:53 +020041#ifdef CONFIG_PL010_SERIAL
wdenk3d3befa2004-03-14 15:06:13 +000042
Marek Vasut39f61472012-09-14 22:38:46 +020043static int pl01x_serial_init(void)
wdenk3d3befa2004-03-14 15:06:13 +000044{
Rabin Vincent72d5e442010-05-05 09:23:07 +053045 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
wdenk42dfe7a2004-03-14 22:25:36 +000046 unsigned int divisor;
wdenk3d3befa2004-03-14 15:06:13 +000047
Matt Waddel249d5212010-10-07 15:48:46 -060048 /* First, disable everything */
Rabin Vincent72d5e442010-05-05 09:23:07 +053049 writel(0, &regs->pl010_cr);
wdenk3d3befa2004-03-14 15:06:13 +000050
Matt Waddel249d5212010-10-07 15:48:46 -060051 /* Set baud rate */
52 switch (baudrate) {
wdenk42dfe7a2004-03-14 22:25:36 +000053 case 9600:
54 divisor = UART_PL010_BAUD_9600;
55 break;
wdenk3d3befa2004-03-14 15:06:13 +000056
wdenk42dfe7a2004-03-14 22:25:36 +000057 case 19200:
58 divisor = UART_PL010_BAUD_9600;
59 break;
wdenk3d3befa2004-03-14 15:06:13 +000060
wdenk42dfe7a2004-03-14 22:25:36 +000061 case 38400:
62 divisor = UART_PL010_BAUD_38400;
63 break;
wdenk3d3befa2004-03-14 15:06:13 +000064
wdenk42dfe7a2004-03-14 22:25:36 +000065 case 57600:
66 divisor = UART_PL010_BAUD_57600;
67 break;
wdenk3d3befa2004-03-14 15:06:13 +000068
wdenk42dfe7a2004-03-14 22:25:36 +000069 case 115200:
70 divisor = UART_PL010_BAUD_115200;
71 break;
wdenk3d3befa2004-03-14 15:06:13 +000072
wdenk42dfe7a2004-03-14 22:25:36 +000073 default:
74 divisor = UART_PL010_BAUD_38400;
75 }
wdenk3d3befa2004-03-14 15:06:13 +000076
Rabin Vincent72d5e442010-05-05 09:23:07 +053077 writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
78 writel(divisor & 0xff, &regs->pl010_lcrl);
wdenk3d3befa2004-03-14 15:06:13 +000079
Matt Waddel249d5212010-10-07 15:48:46 -060080 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
Rabin Vincent72d5e442010-05-05 09:23:07 +053081 writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN, &regs->pl010_lcrh);
wdenk3d3befa2004-03-14 15:06:13 +000082
Matt Waddel249d5212010-10-07 15:48:46 -060083 /* Finally, enable the UART */
Rabin Vincent72d5e442010-05-05 09:23:07 +053084 writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
wdenk42dfe7a2004-03-14 22:25:36 +000085
Andreas Engel20c92262008-09-08 10:17:31 +020086 return 0;
wdenk3d3befa2004-03-14 15:06:13 +000087}
88
Andreas Engel48d01922008-09-08 14:30:53 +020089#endif /* CONFIG_PL010_SERIAL */
Andreas Engel20c92262008-09-08 10:17:31 +020090
Andreas Engel48d01922008-09-08 14:30:53 +020091#ifdef CONFIG_PL011_SERIAL
Andreas Engel20c92262008-09-08 10:17:31 +020092
Marek Vasut39f61472012-09-14 22:38:46 +020093static int pl01x_serial_init(void)
Andreas Engel20c92262008-09-08 10:17:31 +020094{
Rabin Vincent72d5e442010-05-05 09:23:07 +053095 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
Andreas Engel20c92262008-09-08 10:17:31 +020096 unsigned int temp;
97 unsigned int divider;
98 unsigned int remainder;
99 unsigned int fraction;
John Rigby910f1ae2011-04-19 10:42:39 +0000100 unsigned int lcr;
101
102#ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
103 /* Empty RX fifo if necessary */
104 if (readl(&regs->pl011_cr) & UART_PL011_CR_UARTEN) {
105 while (!(readl(&regs->fr) & UART_PL01x_FR_RXFE))
106 readl(&regs->dr);
107 }
108#endif
Andreas Engel20c92262008-09-08 10:17:31 +0200109
Matt Waddel249d5212010-10-07 15:48:46 -0600110 /* First, disable everything */
Rabin Vincent72d5e442010-05-05 09:23:07 +0530111 writel(0, &regs->pl011_cr);
Andreas Engel20c92262008-09-08 10:17:31 +0200112
113 /*
Matt Waddel249d5212010-10-07 15:48:46 -0600114 * Set baud rate
115 *
116 * IBRD = UART_CLK / (16 * BAUD_RATE)
117 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
Andreas Engel20c92262008-09-08 10:17:31 +0200118 */
Matt Waddel249d5212010-10-07 15:48:46 -0600119 temp = 16 * baudrate;
Andreas Engel20c92262008-09-08 10:17:31 +0200120 divider = CONFIG_PL011_CLOCK / temp;
121 remainder = CONFIG_PL011_CLOCK % temp;
Matt Waddel249d5212010-10-07 15:48:46 -0600122 temp = (8 * remainder) / baudrate;
Andreas Engel20c92262008-09-08 10:17:31 +0200123 fraction = (temp >> 1) + (temp & 1);
124
Rabin Vincent72d5e442010-05-05 09:23:07 +0530125 writel(divider, &regs->pl011_ibrd);
126 writel(fraction, &regs->pl011_fbrd);
Andreas Engel20c92262008-09-08 10:17:31 +0200127
Matt Waddel249d5212010-10-07 15:48:46 -0600128 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
John Rigby910f1ae2011-04-19 10:42:39 +0000129 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
130 writel(lcr, &regs->pl011_lcrh);
Andreas Engel20c92262008-09-08 10:17:31 +0200131
John Rigby910f1ae2011-04-19 10:42:39 +0000132#ifdef CONFIG_PL011_SERIAL_RLCR
133 {
134 int i;
135
136 /*
137 * Program receive line control register after waiting
138 * 10 bus cycles. Delay be writing to readonly register
139 * 10 times
140 */
141 for (i = 0; i < 10; i++)
142 writel(lcr, &regs->fr);
143
144 writel(lcr, &regs->pl011_rlcr);
Mathieu J. Poirier84dee302012-08-03 11:05:12 +0000145 /* lcrh needs to be set again for change to be effective */
146 writel(lcr, &regs->pl011_lcrh);
John Rigby910f1ae2011-04-19 10:42:39 +0000147 }
148#endif
Matt Waddel249d5212010-10-07 15:48:46 -0600149 /* Finally, enable the UART */
Joshua Housh10501df2012-12-02 17:09:26 +0000150 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE |
151 UART_PL011_CR_RTS, &regs->pl011_cr);
Andreas Engel20c92262008-09-08 10:17:31 +0200152
153 return 0;
154}
155
Andreas Engel48d01922008-09-08 14:30:53 +0200156#endif /* CONFIG_PL011_SERIAL */
Andreas Engel20c92262008-09-08 10:17:31 +0200157
Marek Vasut39f61472012-09-14 22:38:46 +0200158static void pl01x_serial_putc(const char c)
wdenk3d3befa2004-03-14 15:06:13 +0000159{
160 if (c == '\n')
Andreas Engel20c92262008-09-08 10:17:31 +0200161 pl01x_putc (CONSOLE_PORT, '\r');
wdenk3d3befa2004-03-14 15:06:13 +0000162
Andreas Engel20c92262008-09-08 10:17:31 +0200163 pl01x_putc (CONSOLE_PORT, c);
wdenk3d3befa2004-03-14 15:06:13 +0000164}
165
Marek Vasut39f61472012-09-14 22:38:46 +0200166static int pl01x_serial_getc(void)
wdenk3d3befa2004-03-14 15:06:13 +0000167{
Andreas Engel20c92262008-09-08 10:17:31 +0200168 return pl01x_getc (CONSOLE_PORT);
wdenk3d3befa2004-03-14 15:06:13 +0000169}
170
Marek Vasut39f61472012-09-14 22:38:46 +0200171static int pl01x_serial_tstc(void)
wdenk3d3befa2004-03-14 15:06:13 +0000172{
Andreas Engel20c92262008-09-08 10:17:31 +0200173 return pl01x_tstc (CONSOLE_PORT);
wdenk3d3befa2004-03-14 15:06:13 +0000174}
175
Marek Vasut39f61472012-09-14 22:38:46 +0200176static void pl01x_serial_setbrg(void)
wdenk3d3befa2004-03-14 15:06:13 +0000177{
Linus Walleij96baa4c2011-10-02 11:52:52 +0000178 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
179
Matt Waddel249d5212010-10-07 15:48:46 -0600180 baudrate = gd->baudrate;
Linus Walleij96baa4c2011-10-02 11:52:52 +0000181 /*
182 * Flush FIFO and wait for non-busy before changing baudrate to avoid
183 * crap in console
184 */
185 while (!(readl(&regs->fr) & UART_PL01x_FR_TXFE))
186 WATCHDOG_RESET();
187 while (readl(&regs->fr) & UART_PL01x_FR_BUSY)
188 WATCHDOG_RESET();
Matt Waddel249d5212010-10-07 15:48:46 -0600189 serial_init();
wdenk3d3befa2004-03-14 15:06:13 +0000190}
191
Andreas Engel20c92262008-09-08 10:17:31 +0200192static void pl01x_putc (int portnum, char c)
wdenk3d3befa2004-03-14 15:06:13 +0000193{
Rabin Vincent72d5e442010-05-05 09:23:07 +0530194 struct pl01x_regs *regs = pl01x_get_regs(portnum);
195
wdenk42dfe7a2004-03-14 22:25:36 +0000196 /* Wait until there is space in the FIFO */
Rabin Vincent72d5e442010-05-05 09:23:07 +0530197 while (readl(&regs->fr) & UART_PL01x_FR_TXFF)
Stuart Wood8b616ed2008-06-02 16:42:19 -0400198 WATCHDOG_RESET();
wdenk42dfe7a2004-03-14 22:25:36 +0000199
200 /* Send the character */
Rabin Vincent72d5e442010-05-05 09:23:07 +0530201 writel(c, &regs->dr);
wdenk3d3befa2004-03-14 15:06:13 +0000202}
203
Andreas Engel20c92262008-09-08 10:17:31 +0200204static int pl01x_getc (int portnum)
wdenk3d3befa2004-03-14 15:06:13 +0000205{
Rabin Vincent72d5e442010-05-05 09:23:07 +0530206 struct pl01x_regs *regs = pl01x_get_regs(portnum);
wdenk42dfe7a2004-03-14 22:25:36 +0000207 unsigned int data;
wdenk3d3befa2004-03-14 15:06:13 +0000208
wdenk42dfe7a2004-03-14 22:25:36 +0000209 /* Wait until there is data in the FIFO */
Rabin Vincent72d5e442010-05-05 09:23:07 +0530210 while (readl(&regs->fr) & UART_PL01x_FR_RXFE)
Stuart Wood8b616ed2008-06-02 16:42:19 -0400211 WATCHDOG_RESET();
wdenk42dfe7a2004-03-14 22:25:36 +0000212
Rabin Vincent72d5e442010-05-05 09:23:07 +0530213 data = readl(&regs->dr);
wdenk42dfe7a2004-03-14 22:25:36 +0000214
215 /* Check for an error flag */
216 if (data & 0xFFFFFF00) {
217 /* Clear the error */
Rabin Vincent72d5e442010-05-05 09:23:07 +0530218 writel(0xFFFFFFFF, &regs->ecr);
wdenk42dfe7a2004-03-14 22:25:36 +0000219 return -1;
220 }
221
222 return (int) data;
wdenk3d3befa2004-03-14 15:06:13 +0000223}
224
Andreas Engel20c92262008-09-08 10:17:31 +0200225static int pl01x_tstc (int portnum)
wdenk3d3befa2004-03-14 15:06:13 +0000226{
Rabin Vincent72d5e442010-05-05 09:23:07 +0530227 struct pl01x_regs *regs = pl01x_get_regs(portnum);
228
Stuart Wood8b616ed2008-06-02 16:42:19 -0400229 WATCHDOG_RESET();
Rabin Vincent72d5e442010-05-05 09:23:07 +0530230 return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
wdenk3d3befa2004-03-14 15:06:13 +0000231}
Marek Vasut39f61472012-09-14 22:38:46 +0200232
Marek Vasut39f61472012-09-14 22:38:46 +0200233static struct serial_device pl01x_serial_drv = {
234 .name = "pl01x_serial",
235 .start = pl01x_serial_init,
236 .stop = NULL,
237 .setbrg = pl01x_serial_setbrg,
238 .putc = pl01x_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000239 .puts = default_serial_puts,
Marek Vasut39f61472012-09-14 22:38:46 +0200240 .getc = pl01x_serial_getc,
241 .tstc = pl01x_serial_tstc,
242};
243
244void pl01x_serial_initialize(void)
245{
246 serial_register(&pl01x_serial_drv);
247}
248
249__weak struct serial_device *default_serial_console(void)
250{
251 return &pl01x_serial_drv;
252}