blob: 545b054c2b755e96327470b98f08d831b83a53b6 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Igor Lisitsin95bcd382007-03-28 19:06:19 +040010 */
11
12#include <common.h>
Stefan Roesea0a14792010-09-29 16:58:38 +020013#include <asm/ppc4xx.h>
Stefan Roesed0c966a2010-09-14 09:38:18 +020014#include <ns16550.h>
15#include <asm/io.h>
Stefan Roesea0a14792010-09-29 16:58:38 +020016#include <serial.h>
Igor Lisitsin95bcd382007-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 Lisitsin95bcd382007-03-28 19:06:19 +040025#include <post.h>
26
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020027#if CONFIG_POST & CONFIG_SYS_POST_UART
Igor Lisitsin95bcd382007-03-28 19:06:19 +040028
Stefan Roese32a444b2007-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-VILLARD03836942008-10-16 15:01:15 +020033#ifndef CONFIG_SYS_POST_UART_TABLE
Stefan Roesea0a14792010-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 Roese32a444b2007-08-14 14:39:44 +020037#endif
Igor Lisitsin95bcd382007-03-28 19:06:19 +040038
Stefan Roesed0c966a2010-09-14 09:38:18 +020039DECLARE_GLOBAL_DATA_PTR;
Igor Lisitsin95bcd382007-03-28 19:06:19 +040040
Stefan Roesea0a14792010-09-29 16:58:38 +020041static int test_ctlr (struct NS16550 *com_port, int index)
Igor Lisitsin95bcd382007-03-28 19:06:19 +040042{
Stefan Roesea0a14792010-09-29 16:58:38 +020043 int res = -1;
44 char test_str[] = "*** UART Test String ***\r\n";
Igor Lisitsin95bcd382007-03-28 19:06:19 +040045 int i;
Stefan Roesea0a14792010-09-29 16:58:38 +020046 int divisor;
Igor Lisitsin95bcd382007-03-28 19:06:19 +040047
Stefan Roesea0a14792010-09-29 16:58:38 +020048 divisor = (get_serial_clock() + (gd->baudrate * (16 / 2))) /
49 (16 * gd->baudrate);
50 NS16550_init(com_port, divisor);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040051
52 /*
Stefan Roesea0a14792010-09-29 16:58:38 +020053 * Set internal loopback mode in UART
Igor Lisitsin95bcd382007-03-28 19:06:19 +040054 */
Stefan Roesea0a14792010-09-29 16:58:38 +020055 out_8(&com_port->mcr, in_8(&com_port->mcr) | UART_MCR_LOOP);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040056
Stefan Roesea0a14792010-09-29 16:58:38 +020057 /* Reset FIFOs */
58 out_8(&com_port->fcr, UART_FCR_RXSR | UART_FCR_TXSR);
59 udelay(100);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040060
Stefan Roesea0a14792010-09-29 16:58:38 +020061 /* Flush RX-FIFO */
62 while (NS16550_tstc(com_port))
63 NS16550_getc(com_port);
Igor Lisitsin95bcd382007-03-28 19:06:19 +040064
65 for (i = 0; i < sizeof (test_str) - 1; i++) {
Stefan Roesea0a14792010-09-29 16:58:38 +020066 NS16550_putc(com_port, test_str[i]);
67 if (NS16550_getc(com_port) != test_str[i])
Igor Lisitsin95bcd382007-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-VILLARD03836942008-10-16 15:01:15 +020081 static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE;
Igor Lisitsin95bcd382007-03-28 19:06:19 +040082
Stefan Roesed0c966a2010-09-14 09:38:18 +020083 for (i = 0; i < ARRAY_SIZE(base); i++) {
84 if (test_ctlr((struct NS16550 *)base[i], i))
Igor Lisitsin95bcd382007-03-28 19:06:19 +040085 res = -1;
86 }
87 serial_reinit_all ();
88
89 return res;
90}
91
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020092#endif /* CONFIG_POST & CONFIG_SYS_POST_UART */