blob: 545b054c2b755e96327470b98f08d831b83a53b6 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Igor Lisitsina11e0692007-03-28 19:06:19 +040010 */
11
12#include <common.h>
Stefan Roese5d7c73e2010-09-29 16:58:38 +020013#include <asm/ppc4xx.h>
Stefan Roeseab25e882010-09-14 09:38:18 +020014#include <ns16550.h>
15#include <asm/io.h>
Stefan Roese5d7c73e2010-09-29 16:58:38 +020016#include <serial.h>
Igor Lisitsina11e0692007-03-28 19:06:19 +040017
18/*
19 * UART test
20 *
21 * The controllers are configured to loopback mode and several
22 * characters are transmitted.
23 */
24
Igor Lisitsina11e0692007-03-28 19:06:19 +040025#include <post.h>
26
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027#if CONFIG_POST & CONFIG_SYS_POST_UART
Igor Lisitsina11e0692007-03-28 19:06:19 +040028
Stefan Roeseeb2b4012007-08-14 14:39:44 +020029/*
30 * This table defines the UART's that should be tested and can
31 * be overridden in the board config file
32 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020033#ifndef CONFIG_SYS_POST_UART_TABLE
Stefan Roese5d7c73e2010-09-29 16:58:38 +020034#define CONFIG_SYS_POST_UART_TABLE { CONFIG_SYS_NS16550_COM1, \
35 CONFIG_SYS_NS16550_COM2, CONFIG_SYS_NS16550_COM3, \
36 CONFIG_SYS_NS16550_COM4 }
Stefan Roeseeb2b4012007-08-14 14:39:44 +020037#endif
Igor Lisitsina11e0692007-03-28 19:06:19 +040038
Igor Lisitsina11e0692007-03-28 19:06:19 +040039DECLARE_GLOBAL_DATA_PTR;
40
Stefan Roeseab25e882010-09-14 09:38:18 +020041static int test_ctlr (struct NS16550 *com_port, int index)
Igor Lisitsina11e0692007-03-28 19:06:19 +040042{
43 int res = -1;
44 char test_str[] = "*** UART Test String ***\r\n";
45 int i;
Stefan Roese5d7c73e2010-09-29 16:58:38 +020046 int divisor;
Igor Lisitsina11e0692007-03-28 19:06:19 +040047
Stefan Roese5d7c73e2010-09-29 16:58:38 +020048 divisor = (get_serial_clock() + (gd->baudrate * (16 / 2))) /
49 (16 * gd->baudrate);
50 NS16550_init(com_port, divisor);
51
52 /*
53 * Set internal loopback mode in UART
54 */
55 out_8(&com_port->mcr, in_8(&com_port->mcr) | UART_MCR_LOOP);
56
57 /* Reset FIFOs */
58 out_8(&com_port->fcr, UART_FCR_RXSR | UART_FCR_TXSR);
59 udelay(100);
60
61 /* Flush RX-FIFO */
62 while (NS16550_tstc(com_port))
63 NS16550_getc(com_port);
Igor Lisitsina11e0692007-03-28 19:06:19 +040064
65 for (i = 0; i < sizeof (test_str) - 1; i++) {
Stefan Roese5d7c73e2010-09-29 16:58:38 +020066 NS16550_putc(com_port, test_str[i]);
67 if (NS16550_getc(com_port) != test_str[i])
Igor Lisitsina11e0692007-03-28 19:06:19 +040068 goto done;
69 }
70 res = 0;
71done:
72 if (res)
73 post_log ("uart%d test failed\n", index);
74
75 return res;
76}
77
78int uart_post_test (int flags)
79{
80 int i, res = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020081 static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE;
Igor Lisitsina11e0692007-03-28 19:06:19 +040082
Stefan Roeseab25e882010-09-14 09:38:18 +020083 for (i = 0; i < ARRAY_SIZE(base); i++) {
84 if (test_ctlr((struct NS16550 *)base[i], i))
Igor Lisitsina11e0692007-03-28 19:06:19 +040085 res = -1;
86 }
87 serial_reinit_all ();
88
89 return res;
90}
91
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092#endif /* CONFIG_POST & CONFIG_SYS_POST_UART */