blob: 665a0e48b1d6396f9ae0d674fa62977608aa6d99 [file] [log] [blame]
wdenk4989f872004-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 Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk4989f872004-03-14 15:06:13 +000010 */
11
Andreas Engel0813b122008-09-08 14:30:53 +020012/* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
wdenk4989f872004-03-14 15:06:13 +000013
14#include <common.h>
Simon Glassf35484d2014-09-22 17:30:57 -060015#include <errno.h>
Stuart Wood26136ef2008-06-02 16:42:19 -040016#include <watchdog.h>
Matt Waddeld6ce53e2010-10-07 15:48:46 -060017#include <asm/io.h>
Marek Vasut46e4d5f2012-09-14 22:38:46 +020018#include <serial.h>
Simon Glassf35484d2014-09-22 17:30:57 -060019#include <serial_pl01x.h>
Marek Vasut46e4d5f2012-09-14 22:38:46 +020020#include <linux/compiler.h>
Simon Glassf35484d2014-09-22 17:30:57 -060021#include "serial_pl01x_internal.h"
wdenk4989f872004-03-14 15:06:13 +000022
wdenkda04a8b2004-08-02 23:22:59 +000023static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
Simon Glassf35484d2014-09-22 17:30:57 -060024static enum pl01x_type pl01x_type __attribute__ ((section(".data")));
25static struct pl01x_regs *base_regs __attribute__ ((section(".data")));
wdenkda04a8b2004-08-02 23:22:59 +000026#define NUM_PORTS (sizeof(port)/sizeof(port[0]))
wdenk4989f872004-03-14 15:06:13 +000027
Matt Waddeld6ce53e2010-10-07 15:48:46 -060028DECLARE_GLOBAL_DATA_PTR;
wdenk4989f872004-03-14 15:06:13 +000029
Simon Glassf35484d2014-09-22 17:30:57 -060030static int pl01x_putc(struct pl01x_regs *regs, char c)
wdenk4989f872004-03-14 15:06:13 +000031{
Simon Glassf35484d2014-09-22 17:30:57 -060032 /* Wait until there is space in the FIFO */
33 if (readl(&regs->fr) & UART_PL01x_FR_TXFF)
34 return -EAGAIN;
wdenk4989f872004-03-14 15:06:13 +000035
Simon Glassf35484d2014-09-22 17:30:57 -060036 /* Send the character */
37 writel(c, &regs->dr);
wdenk4989f872004-03-14 15:06:13 +000038
Simon Glassf35484d2014-09-22 17:30:57 -060039 return 0;
40}
wdenk4989f872004-03-14 15:06:13 +000041
Simon Glassf35484d2014-09-22 17:30:57 -060042static int pl01x_getc(struct pl01x_regs *regs)
43{
44 unsigned int data;
wdenk4989f872004-03-14 15:06:13 +000045
Simon Glassf35484d2014-09-22 17:30:57 -060046 /* Wait until there is data in the FIFO */
47 if (readl(&regs->fr) & UART_PL01x_FR_RXFE)
48 return -EAGAIN;
wdenk4989f872004-03-14 15:06:13 +000049
Simon Glassf35484d2014-09-22 17:30:57 -060050 data = readl(&regs->dr);
wdenk4989f872004-03-14 15:06:13 +000051
Simon Glassf35484d2014-09-22 17:30:57 -060052 /* Check for an error flag */
53 if (data & 0xFFFFFF00) {
54 /* Clear the error */
55 writel(0xFFFFFFFF, &regs->ecr);
56 return -1;
wdenkc35ba4e2004-03-14 22:25:36 +000057 }
58
Simon Glassf35484d2014-09-22 17:30:57 -060059 return (int) data;
wdenk4989f872004-03-14 15:06:13 +000060}
61
Simon Glassf35484d2014-09-22 17:30:57 -060062static int pl01x_tstc(struct pl01x_regs *regs)
63{
64 WATCHDOG_RESET();
65 return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
66}
Andreas Engel80438612008-09-08 10:17:31 +020067
Simon Glassf35484d2014-09-22 17:30:57 -060068static int pl01x_generic_serial_init(struct pl01x_regs *regs,
69 enum pl01x_type type)
Andreas Engel80438612008-09-08 10:17:31 +020070{
John Rigby34e21ee2011-04-19 10:42:39 +000071 unsigned int lcr;
72
73#ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
Simon Glassf35484d2014-09-22 17:30:57 -060074 if (type == TYPE_PL011) {
75 /* Empty RX fifo if necessary */
76 if (readl(&regs->pl011_cr) & UART_PL011_CR_UARTEN) {
77 while (!(readl(&regs->fr) & UART_PL01x_FR_RXFE))
78 readl(&regs->dr);
79 }
John Rigby34e21ee2011-04-19 10:42:39 +000080 }
81#endif
Andreas Engel80438612008-09-08 10:17:31 +020082
Matt Waddeld6ce53e2010-10-07 15:48:46 -060083 /* First, disable everything */
Simon Glassf35484d2014-09-22 17:30:57 -060084 writel(0, &regs->pl010_cr);
Andreas Engel80438612008-09-08 10:17:31 +020085
Matt Waddeld6ce53e2010-10-07 15:48:46 -060086 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
John Rigby34e21ee2011-04-19 10:42:39 +000087 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
88 writel(lcr, &regs->pl011_lcrh);
89
Simon Glassf35484d2014-09-22 17:30:57 -060090 switch (type) {
91 case TYPE_PL010:
92 break;
93 case TYPE_PL011: {
John Rigby34e21ee2011-04-19 10:42:39 +000094#ifdef CONFIG_PL011_SERIAL_RLCR
John Rigby34e21ee2011-04-19 10:42:39 +000095 int i;
96
97 /*
98 * Program receive line control register after waiting
99 * 10 bus cycles. Delay be writing to readonly register
100 * 10 times
101 */
102 for (i = 0; i < 10; i++)
103 writel(lcr, &regs->fr);
Andreas Engel80438612008-09-08 10:17:31 +0200104
John Rigby34e21ee2011-04-19 10:42:39 +0000105 writel(lcr, &regs->pl011_rlcr);
Mathieu J. Poirier4a036fe2012-08-03 11:05:12 +0000106 /* lcrh needs to be set again for change to be effective */
107 writel(lcr, &regs->pl011_lcrh);
John Rigby34e21ee2011-04-19 10:42:39 +0000108#endif
Simon Glassf35484d2014-09-22 17:30:57 -0600109 break;
110 }
111 default:
112 return -EINVAL;
113 }
Andreas Engel80438612008-09-08 10:17:31 +0200114
115 return 0;
116}
117
Simon Glassf35484d2014-09-22 17:30:57 -0600118static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type,
119 int clock, int baudrate)
wdenk4989f872004-03-14 15:06:13 +0000120{
Simon Glassf35484d2014-09-22 17:30:57 -0600121 switch (type) {
122 case TYPE_PL010: {
123 unsigned int divisor;
wdenk4989f872004-03-14 15:06:13 +0000124
Simon Glassf35484d2014-09-22 17:30:57 -0600125 switch (baudrate) {
126 case 9600:
127 divisor = UART_PL010_BAUD_9600;
128 break;
129 case 19200:
130 divisor = UART_PL010_BAUD_9600;
131 break;
132 case 38400:
133 divisor = UART_PL010_BAUD_38400;
134 break;
135 case 57600:
136 divisor = UART_PL010_BAUD_57600;
137 break;
138 case 115200:
139 divisor = UART_PL010_BAUD_115200;
140 break;
141 default:
142 divisor = UART_PL010_BAUD_38400;
143 }
wdenk4989f872004-03-14 15:06:13 +0000144
Simon Glassf35484d2014-09-22 17:30:57 -0600145 writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
146 writel(divisor & 0xff, &regs->pl010_lcrl);
147
148 /* Finally, enable the UART */
149 writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
150 break;
151 }
152 case TYPE_PL011: {
153 unsigned int temp;
154 unsigned int divider;
155 unsigned int remainder;
156 unsigned int fraction;
157
158 /*
159 * Set baud rate
160 *
161 * IBRD = UART_CLK / (16 * BAUD_RATE)
162 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE)))
163 * / (16 * BAUD_RATE))
164 */
165 temp = 16 * baudrate;
166 divider = clock / temp;
167 remainder = clock % temp;
168 temp = (8 * remainder) / baudrate;
169 fraction = (temp >> 1) + (temp & 1);
170
171 writel(divider, &regs->pl011_ibrd);
172 writel(fraction, &regs->pl011_fbrd);
173
174 /* Finally, enable the UART */
175 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE |
176 UART_PL011_CR_RXE | UART_PL011_CR_RTS, &regs->pl011_cr);
177 break;
178 }
179 default:
180 return -EINVAL;
181 }
182
183 return 0;
wdenk4989f872004-03-14 15:06:13 +0000184}
185
Simon Glassf35484d2014-09-22 17:30:57 -0600186#ifndef CONFIG_DM_SERIAL
187static void pl01x_serial_init_baud(int baudrate)
wdenk4989f872004-03-14 15:06:13 +0000188{
Simon Glassf35484d2014-09-22 17:30:57 -0600189 int clock = 0;
190
191#if defined(CONFIG_PL010_SERIAL)
192 pl01x_type = TYPE_PL010;
193#elif defined(CONFIG_PL011_SERIAL)
194 pl01x_type = TYPE_PL011;
195 clock = CONFIG_PL011_CLOCK;
196#endif
197 base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX];
198
199 pl01x_generic_serial_init(base_regs, pl01x_type);
200 pl01x_generic_setbrg(base_regs, TYPE_PL010, clock, baudrate);
wdenk4989f872004-03-14 15:06:13 +0000201}
202
Simon Glassf35484d2014-09-22 17:30:57 -0600203/*
204 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
205 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
206 * Versatile PB has four UARTs.
207 */
208int pl01x_serial_init(void)
wdenk4989f872004-03-14 15:06:13 +0000209{
Simon Glassf35484d2014-09-22 17:30:57 -0600210 pl01x_serial_init_baud(CONFIG_BAUDRATE);
Linus Walleijb8058e82011-10-02 11:52:52 +0000211
Simon Glassf35484d2014-09-22 17:30:57 -0600212 return 0;
wdenk4989f872004-03-14 15:06:13 +0000213}
214
Simon Glassf35484d2014-09-22 17:30:57 -0600215static void pl01x_serial_putc(const char c)
wdenk4989f872004-03-14 15:06:13 +0000216{
Simon Glassf35484d2014-09-22 17:30:57 -0600217 if (c == '\n')
218 while (pl01x_putc(base_regs, '\r') == -EAGAIN);
wdenkc35ba4e2004-03-14 22:25:36 +0000219
Simon Glassf35484d2014-09-22 17:30:57 -0600220 while (pl01x_putc(base_regs, c) == -EAGAIN);
wdenk4989f872004-03-14 15:06:13 +0000221}
222
Simon Glassf35484d2014-09-22 17:30:57 -0600223static int pl01x_serial_getc(void)
wdenk4989f872004-03-14 15:06:13 +0000224{
Simon Glassf35484d2014-09-22 17:30:57 -0600225 while (1) {
226 int ch = pl01x_getc(base_regs);
wdenkc35ba4e2004-03-14 22:25:36 +0000227
Simon Glassf35484d2014-09-22 17:30:57 -0600228 if (ch == -EAGAIN) {
229 WATCHDOG_RESET();
230 continue;
231 }
wdenk4989f872004-03-14 15:06:13 +0000232
Simon Glassf35484d2014-09-22 17:30:57 -0600233 return ch;
wdenkc35ba4e2004-03-14 22:25:36 +0000234 }
wdenk4989f872004-03-14 15:06:13 +0000235}
236
Simon Glassf35484d2014-09-22 17:30:57 -0600237static int pl01x_serial_tstc(void)
wdenk4989f872004-03-14 15:06:13 +0000238{
Simon Glassf35484d2014-09-22 17:30:57 -0600239 return pl01x_tstc(base_regs);
240}
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530241
Simon Glassf35484d2014-09-22 17:30:57 -0600242static void pl01x_serial_setbrg(void)
243{
244 /*
245 * Flush FIFO and wait for non-busy before changing baudrate to avoid
246 * crap in console
247 */
248 while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE))
249 WATCHDOG_RESET();
250 while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY)
251 WATCHDOG_RESET();
252 pl01x_serial_init_baud(gd->baudrate);
wdenk4989f872004-03-14 15:06:13 +0000253}
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200254
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200255static struct serial_device pl01x_serial_drv = {
256 .name = "pl01x_serial",
257 .start = pl01x_serial_init,
258 .stop = NULL,
259 .setbrg = pl01x_serial_setbrg,
260 .putc = pl01x_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000261 .puts = default_serial_puts,
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200262 .getc = pl01x_serial_getc,
263 .tstc = pl01x_serial_tstc,
264};
265
266void pl01x_serial_initialize(void)
267{
268 serial_register(&pl01x_serial_drv);
269}
270
271__weak struct serial_device *default_serial_console(void)
272{
273 return &pl01x_serial_drv;
274}
Simon Glassf35484d2014-09-22 17:30:57 -0600275
276#endif /* nCONFIG_DM_SERIAL */