blob: d76895696b272bbe1f99275dcf5cdfad22735aa9 [file] [log] [blame]
Igor Lisitsin95bcd382007-03-28 19:06:19 +04001/*
2 * (C) Copyright 2007
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Sergei Poselenov3190dbe2007-07-05 08:17:37 +02005 * Author: Igor Lisitsin <igor@emcraft.com>
6 *
Stefan Roesed0c966a2010-09-14 09:38:18 +02007 * Copyright 2010, Stefan Roese, DENX Software Engineering, sr@denx.de
8 *
Igor Lisitsin95bcd382007-03-28 19:06:19 +04009 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
Stefan Roesea0a14792010-09-29 16:58:38 +020029#include <asm/ppc4xx.h>
Stefan Roesed0c966a2010-09-14 09:38:18 +020030#include <ns16550.h>
31#include <asm/io.h>
Stefan Roesea0a14792010-09-29 16:58:38 +020032#include <serial.h>
Igor Lisitsin95bcd382007-03-28 19:06:19 +040033
34/*
35 * UART test
36 *
37 * The controllers are configured to loopback mode and several
38 * characters are transmitted.
39 */
40
Igor Lisitsin95bcd382007-03-28 19:06:19 +040041#include <post.h>
42
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020043#if CONFIG_POST & CONFIG_SYS_POST_UART
Igor Lisitsin95bcd382007-03-28 19:06:19 +040044
Stefan Roese32a444b2007-08-14 14:39:44 +020045/*
46 * This table defines the UART's that should be tested and can
47 * be overridden in the board config file
48 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020049#ifndef CONFIG_SYS_POST_UART_TABLE
Stefan Roesea0a14792010-09-29 16:58:38 +020050#define CONFIG_SYS_POST_UART_TABLE { CONFIG_SYS_NS16550_COM1, \
51 CONFIG_SYS_NS16550_COM2, CONFIG_SYS_NS16550_COM3, \
52 CONFIG_SYS_NS16550_COM4 }
Stefan Roese32a444b2007-08-14 14:39:44 +020053#endif
Igor Lisitsin95bcd382007-03-28 19:06:19 +040054
Stefan Roesed0c966a2010-09-14 09:38:18 +020055DECLARE_GLOBAL_DATA_PTR;
Igor Lisitsin95bcd382007-03-28 19:06:19 +040056
Stefan Roesea0a14792010-09-29 16:58:38 +020057static int test_ctlr (struct NS16550 *com_port, int index)
Igor Lisitsin95bcd382007-03-28 19:06:19 +040058{
Stefan Roesea0a14792010-09-29 16:58:38 +020059 int res = -1;
60 char test_str[] = "*** UART Test String ***\r\n";
Igor Lisitsin95bcd382007-03-28 19:06:19 +040061 int i;
Stefan Roesea0a14792010-09-29 16:58:38 +020062 int divisor;
Igor Lisitsin95bcd382007-03-28 19:06:19 +040063
Stefan Roesea0a14792010-09-29 16:58:38 +020064 divisor = (get_serial_clock() + (gd->baudrate * (16 / 2))) /
65 (16 * gd->baudrate);
66 NS16550_init(com_port, divisor);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040067
68 /*
Stefan Roesea0a14792010-09-29 16:58:38 +020069 * Set internal loopback mode in UART
Igor Lisitsin95bcd382007-03-28 19:06:19 +040070 */
Stefan Roesea0a14792010-09-29 16:58:38 +020071 out_8(&com_port->mcr, in_8(&com_port->mcr) | UART_MCR_LOOP);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040072
Stefan Roesea0a14792010-09-29 16:58:38 +020073 /* Reset FIFOs */
74 out_8(&com_port->fcr, UART_FCR_RXSR | UART_FCR_TXSR);
75 udelay(100);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040076
Stefan Roesea0a14792010-09-29 16:58:38 +020077 /* Flush RX-FIFO */
78 while (NS16550_tstc(com_port))
79 NS16550_getc(com_port);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040080
81 for (i = 0; i < sizeof (test_str) - 1; i++) {
Stefan Roesea0a14792010-09-29 16:58:38 +020082 NS16550_putc(com_port, test_str[i]);
83 if (NS16550_getc(com_port) != test_str[i])
Igor Lisitsin95bcd382007-03-28 19:06:19 +040084 goto done;
85 }
86 res = 0;
87done:
88 if (res)
89 post_log ("uart%d test failed\n", index);
90
91 return res;
92}
93
94int uart_post_test (int flags)
95{
96 int i, res = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020097 static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE;
Igor Lisitsin95bcd382007-03-28 19:06:19 +040098
Stefan Roesed0c966a2010-09-14 09:38:18 +020099 for (i = 0; i < ARRAY_SIZE(base); i++) {
100 if (test_ctlr((struct NS16550 *)base[i], i))
Igor Lisitsin95bcd382007-03-28 19:06:19 +0400101 res = -1;
102 }
103 serial_reinit_all ();
104
105 return res;
106}
107
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108#endif /* CONFIG_POST & CONFIG_SYS_POST_UART */