blob: d76895696b272bbe1f99275dcf5cdfad22735aa9 [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 *
Stefan Roeseab25e882010-09-14 09:38:18 +02007 * Copyright 2010, Stefan Roese, DENX Software Engineering, sr@denx.de
8 *
Igor Lisitsina11e0692007-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 Roese5d7c73e2010-09-29 16:58:38 +020029#include <asm/ppc4xx.h>
Stefan Roeseab25e882010-09-14 09:38:18 +020030#include <ns16550.h>
31#include <asm/io.h>
Stefan Roese5d7c73e2010-09-29 16:58:38 +020032#include <serial.h>
Igor Lisitsina11e0692007-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 Lisitsina11e0692007-03-28 19:06:19 +040041#include <post.h>
42
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020043#if CONFIG_POST & CONFIG_SYS_POST_UART
Igor Lisitsina11e0692007-03-28 19:06:19 +040044
Stefan Roeseeb2b4012007-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-VILLARD6d0f6bc2008-10-16 15:01:15 +020049#ifndef CONFIG_SYS_POST_UART_TABLE
Stefan Roese5d7c73e2010-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 Roeseeb2b4012007-08-14 14:39:44 +020053#endif
Igor Lisitsina11e0692007-03-28 19:06:19 +040054
Igor Lisitsina11e0692007-03-28 19:06:19 +040055DECLARE_GLOBAL_DATA_PTR;
56
Stefan Roeseab25e882010-09-14 09:38:18 +020057static int test_ctlr (struct NS16550 *com_port, int index)
Igor Lisitsina11e0692007-03-28 19:06:19 +040058{
59 int res = -1;
60 char test_str[] = "*** UART Test String ***\r\n";
61 int i;
Stefan Roese5d7c73e2010-09-29 16:58:38 +020062 int divisor;
Igor Lisitsina11e0692007-03-28 19:06:19 +040063
Stefan Roese5d7c73e2010-09-29 16:58:38 +020064 divisor = (get_serial_clock() + (gd->baudrate * (16 / 2))) /
65 (16 * gd->baudrate);
66 NS16550_init(com_port, divisor);
67
68 /*
69 * Set internal loopback mode in UART
70 */
71 out_8(&com_port->mcr, in_8(&com_port->mcr) | UART_MCR_LOOP);
72
73 /* Reset FIFOs */
74 out_8(&com_port->fcr, UART_FCR_RXSR | UART_FCR_TXSR);
75 udelay(100);
76
77 /* Flush RX-FIFO */
78 while (NS16550_tstc(com_port))
79 NS16550_getc(com_port);
Igor Lisitsina11e0692007-03-28 19:06:19 +040080
81 for (i = 0; i < sizeof (test_str) - 1; i++) {
Stefan Roese5d7c73e2010-09-29 16:58:38 +020082 NS16550_putc(com_port, test_str[i]);
83 if (NS16550_getc(com_port) != test_str[i])
Igor Lisitsina11e0692007-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-VILLARD6d0f6bc2008-10-16 15:01:15 +020097 static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE;
Igor Lisitsina11e0692007-03-28 19:06:19 +040098
Stefan Roeseab25e882010-09-14 09:38:18 +020099 for (i = 0; i < ARRAY_SIZE(base); i++) {
100 if (test_ctlr((struct NS16550 *)base[i], i))
Igor Lisitsina11e0692007-03-28 19:06:19 +0400101 res = -1;
102 }
103 serial_reinit_all ();
104
105 return res;
106}
107
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200108#endif /* CONFIG_POST & CONFIG_SYS_POST_UART */