blob: 0cffda5fca2b57de188edecf52cc2cef9dce67c6 [file] [log] [blame]
Igor Lisitsina11e0692007-03-28 19:06:19 +04001/*
2 * (C) Copyright 2007
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Sergei Poselenovb4489622007-07-05 08:17:37 +02005 * Author: Igor Lisitsin <igor@emcraft.com>
6 *
Igor Lisitsina11e0692007-03-28 19:06:19 +04007 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27
28/*
29 * UART test
30 *
31 * The controllers are configured to loopback mode and several
32 * characters are transmitted.
33 */
34
35#ifdef CONFIG_POST
36
37#include <post.h>
38
39#if CONFIG_POST & CFG_POST_UART
40
Stefan Roeseeb2b4012007-08-14 14:39:44 +020041/*
42 * This table defines the UART's that should be tested and can
43 * be overridden in the board config file
44 */
45#ifndef CFG_POST_UART_TABLE
46#define CFG_POST_UART_TABLE {UART0_BASE, UART1_BASE, UART2_BASE, UART3_BASE}
47#endif
48
Igor Lisitsina11e0692007-03-28 19:06:19 +040049#include <asm/processor.h>
50#include <serial.h>
51
Stefan Roeseeb2b4012007-08-14 14:39:44 +020052#if defined(CONFIG_440)
53#if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
54 defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
Igor Lisitsina11e0692007-03-28 19:06:19 +040055#define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
56#define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
57#define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000500
58#define UART3_BASE CFG_PERIPHERAL_BASE + 0x00000600
Stefan Roeseeb2b4012007-08-14 14:39:44 +020059#else
60#define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000200
61#define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000300
62#endif
Igor Lisitsina11e0692007-03-28 19:06:19 +040063
Stefan Roeseeb2b4012007-08-14 14:39:44 +020064#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
65#define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000600
66#endif
67
68#if defined(CONFIG_440GP)
69#define CR0_MASK 0x3fff0000
70#define CR0_EXTCLK_ENA 0x00600000
71#define CR0_UDIV_POS 16
72#define UDIV_SUBTRACT 1
73#define UART0_SDR cntrl0
74#define MFREG(a, d) d = mfdcr(a)
75#define MTREG(a, d) mtdcr(a, d)
76#else /* #if defined(CONFIG_440GP) */
77/* all other 440 PPC's access clock divider via sdr register */
Igor Lisitsina11e0692007-03-28 19:06:19 +040078#define CR0_MASK 0xdfffffff
79#define CR0_EXTCLK_ENA 0x00800000
80#define CR0_UDIV_POS 0
81#define UDIV_SUBTRACT 0
82#define UART0_SDR sdr_uart0
83#define UART1_SDR sdr_uart1
Stefan Roeseeb2b4012007-08-14 14:39:44 +020084#if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
85 defined(CONFIG_440GR) || defined(CONFIG_440GRx) || \
86 defined(CONFIG_440SP) || defined(CONFIG_440SPe)
Igor Lisitsina11e0692007-03-28 19:06:19 +040087#define UART2_SDR sdr_uart2
Stefan Roeseeb2b4012007-08-14 14:39:44 +020088#endif
89#if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
90 defined(CONFIG_440GR) || defined(CONFIG_440GRx)
Igor Lisitsina11e0692007-03-28 19:06:19 +040091#define UART3_SDR sdr_uart3
Stefan Roeseeb2b4012007-08-14 14:39:44 +020092#endif
Igor Lisitsina11e0692007-03-28 19:06:19 +040093#define MFREG(a, d) mfsdr(a, d)
94#define MTREG(a, d) mtsdr(a, d)
Stefan Roeseeb2b4012007-08-14 14:39:44 +020095#endif /* #if defined(CONFIG_440GP) */
96#elif defined(CONFIG_405EP) || defined(CONFIG_405EZ)
97#define UART0_BASE 0xef600300
98#define UART1_BASE 0xef600400
99#define UCR0_MASK 0x0000007f
100#define UCR1_MASK 0x00007f00
101#define UCR0_UDIV_POS 0
102#define UCR1_UDIV_POS 8
103#define UDIV_MAX 127
104#else /* CONFIG_405GP || CONFIG_405CR */
105#define UART0_BASE 0xef600300
106#define UART1_BASE 0xef600400
107#define CR0_MASK 0x00001fff
108#define CR0_EXTCLK_ENA 0x000000c0
109#define CR0_UDIV_POS 1
110#define UDIV_MAX 32
111#endif
Igor Lisitsina11e0692007-03-28 19:06:19 +0400112
113#define UART_RBR 0x00
114#define UART_THR 0x00
115#define UART_IER 0x01
116#define UART_IIR 0x02
117#define UART_FCR 0x02
118#define UART_LCR 0x03
119#define UART_MCR 0x04
120#define UART_LSR 0x05
121#define UART_MSR 0x06
122#define UART_SCR 0x07
123#define UART_DLL 0x00
124#define UART_DLM 0x01
125
126/*
Stefan Roeseeb2b4012007-08-14 14:39:44 +0200127 * Line Status Register.
128 */
Igor Lisitsina11e0692007-03-28 19:06:19 +0400129#define asyncLSRDataReady1 0x01
130#define asyncLSROverrunError1 0x02
131#define asyncLSRParityError1 0x04
132#define asyncLSRFramingError1 0x08
133#define asyncLSRBreakInterrupt1 0x10
134#define asyncLSRTxHoldEmpty1 0x20
135#define asyncLSRTxShiftEmpty1 0x40
136#define asyncLSRRxFifoError1 0x80
137
138DECLARE_GLOBAL_DATA_PTR;
139
Stefan Roeseeb2b4012007-08-14 14:39:44 +0200140#if defined(CONFIG_440)
Igor Lisitsina11e0692007-03-28 19:06:19 +0400141static int uart_post_init (unsigned long dev_base)
142{
143 unsigned long reg;
144 unsigned long udiv;
145 unsigned short bdiv;
146 volatile char val;
147#ifdef CFG_EXT_SERIAL_CLOCK
148 unsigned long tmp;
149#endif
150 int i;
151
152 for (i = 0; i < 3500; i++) {
153 if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
154 break;
155 udelay (100);
156 }
157 MFREG(UART0_SDR, reg);
158 reg &= ~CR0_MASK;
159
160#ifdef CFG_EXT_SERIAL_CLOCK
161 reg |= CR0_EXTCLK_ENA;
162 udiv = 1;
163 tmp = gd->baudrate * 16;
164 bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
165#else
166 /* For 440, the cpu clock is on divider chain A, UART on divider
167 * chain B ... so cpu clock is irrelevant. Get the "optimized"
168 * values that are subject to the 1/2 opb clock constraint
169 */
170 serial_divs (gd->baudrate, &udiv, &bdiv);
171#endif
172
173 reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
174
175 /*
176 * Configure input clock to baudrate generator for all
177 * available serial ports here
178 */
179 MTREG(UART0_SDR, reg);
180#if defined(UART1_SDR)
181 MTREG(UART1_SDR, reg);
182#endif
183#if defined(UART2_SDR)
184 MTREG(UART2_SDR, reg);
185#endif
186#if defined(UART3_SDR)
187 MTREG(UART3_SDR, reg);
188#endif
189
190 out8(dev_base + UART_LCR, 0x80); /* set DLAB bit */
191 out8(dev_base + UART_DLL, bdiv); /* set baudrate divisor */
192 out8(dev_base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
193 out8(dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
194 out8(dev_base + UART_FCR, 0x00); /* disable FIFO */
195 out8(dev_base + UART_MCR, 0x10); /* enable loopback mode */
196 val = in8(dev_base + UART_LSR); /* clear line status */
197 val = in8(dev_base + UART_RBR); /* read receive buffer */
198 out8(dev_base + UART_SCR, 0x00); /* set scratchpad */
199 out8(dev_base + UART_IER, 0x00); /* set interrupt enable reg */
200
201 return 0;
202}
203
Stefan Roeseeb2b4012007-08-14 14:39:44 +0200204#else /* CONFIG_440 */
205
206static int uart_post_init (unsigned long dev_base)
207{
208 unsigned long reg;
209 unsigned long tmp;
210 unsigned long clk;
211 unsigned long udiv;
212 unsigned short bdiv;
213 volatile char val;
214 int i;
215
216 for (i = 0; i < 3500; i++) {
217 if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
218 break;
219 udelay (100);
220 }
221
222#if defined(CONFIG_405EZ)
223 serial_divs(gd->baudrate, &udiv, &bdiv);
224 clk = tmp = reg = 0;
225#else
226#ifdef CONFIG_405EP
227 reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
228 clk = gd->cpu_clk;
229 tmp = CFG_BASE_BAUD * 16;
230 udiv = (clk + tmp / 2) / tmp;
231 if (udiv > UDIV_MAX) /* max. n bits for udiv */
232 udiv = UDIV_MAX;
233 reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
234 reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
235 mtdcr (cpc0_ucr, reg);
236#else /* CONFIG_405EP */
237 reg = mfdcr(cntrl0) & ~CR0_MASK;
238#ifdef CFG_EXT_SERIAL_CLOCK
239 clk = CFG_EXT_SERIAL_CLOCK;
240 udiv = 1;
241 reg |= CR0_EXTCLK_ENA;
242#else
243 clk = gd->cpu_clk;
244#ifdef CFG_405_UART_ERRATA_59
245 udiv = 31; /* Errata 59: stuck at 31 */
246#else
247 tmp = CFG_BASE_BAUD * 16;
248 udiv = (clk + tmp / 2) / tmp;
249 if (udiv > UDIV_MAX) /* max. n bits for udiv */
250 udiv = UDIV_MAX;
251#endif
252#endif
253 reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
254 mtdcr (cntrl0, reg);
255#endif /* CONFIG_405EP */
256 tmp = gd->baudrate * udiv * 16;
257 bdiv = (clk + tmp / 2) / tmp;
258#endif /* CONFIG_405EZ */
259
260 out8(dev_base + UART_LCR, 0x80); /* set DLAB bit */
261 out8(dev_base + UART_DLL, bdiv); /* set baudrate divisor */
262 out8(dev_base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
263 out8(dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
264 out8(dev_base + UART_FCR, 0x00); /* disable FIFO */
265 out8(dev_base + UART_MCR, 0x10); /* enable loopback mode */
266 val = in8(dev_base + UART_LSR); /* clear line status */
267 val = in8(dev_base + UART_RBR); /* read receive buffer */
268 out8(dev_base + UART_SCR, 0x00); /* set scratchpad */
269 out8(dev_base + UART_IER, 0x00); /* set interrupt enable reg */
270
271 return (0);
272}
273#endif /* CONFIG_440 */
274
Igor Lisitsina11e0692007-03-28 19:06:19 +0400275static void uart_post_putc (unsigned long dev_base, char c)
276{
277 int i;
278
279 out8 (dev_base + UART_THR, c); /* put character out */
280
281 /* Wait for transfer completion */
282 for (i = 0; i < 3500; i++) {
283 if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
284 break;
285 udelay (100);
286 }
287}
288
289static int uart_post_getc (unsigned long dev_base)
290{
291 int i;
292
293 /* Wait for character available */
294 for (i = 0; i < 3500; i++) {
295 if (in8 (dev_base + UART_LSR) & asyncLSRDataReady1)
296 break;
297 udelay (100);
298 }
299 return 0xff & in8 (dev_base + UART_RBR);
300}
301
302static int test_ctlr (unsigned long dev_base, int index)
303{
304 int res = -1;
305 char test_str[] = "*** UART Test String ***\r\n";
306 int i;
307
308 uart_post_init (dev_base);
309
310 for (i = 0; i < sizeof (test_str) - 1; i++) {
311 uart_post_putc (dev_base, test_str[i]);
312 if (uart_post_getc (dev_base) != test_str[i])
313 goto done;
314 }
315 res = 0;
316done:
317 if (res)
318 post_log ("uart%d test failed\n", index);
319
320 return res;
321}
322
323int uart_post_test (int flags)
324{
325 int i, res = 0;
Stefan Roeseeb2b4012007-08-14 14:39:44 +0200326 static unsigned long base[] = CFG_POST_UART_TABLE;
Igor Lisitsina11e0692007-03-28 19:06:19 +0400327
328 for (i = 0; i < sizeof (base) / sizeof (base[0]); i++) {
329 if (test_ctlr (base[i], i))
330 res = -1;
331 }
332 serial_reinit_all ();
333
334 return res;
335}
336
337#endif /* CONFIG_POST & CFG_POST_UART */
Igor Lisitsina11e0692007-03-28 19:06:19 +0400338#endif /* CONFIG_POST */