Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Sergei Poselenov | 3190dbe | 2007-07-05 08:17:37 +0200 | [diff] [blame] | 5 | * Author: Igor Lisitsin <igor@emcraft.com> |
| 6 | * |
Stefan Roese | d0c966a | 2010-09-14 09:38:18 +0200 | [diff] [blame] | 7 | * Copyright 2010, Stefan Roese, DENX Software Engineering, sr@denx.de |
| 8 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 13 | #include <asm/ppc4xx.h> |
Stefan Roese | d0c966a | 2010-09-14 09:38:18 +0200 | [diff] [blame] | 14 | #include <ns16550.h> |
| 15 | #include <asm/io.h> |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 16 | #include <serial.h> |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * UART test |
| 20 | * |
| 21 | * The controllers are configured to loopback mode and several |
| 22 | * characters are transmitted. |
| 23 | */ |
| 24 | |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 25 | #include <post.h> |
| 26 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 27 | #if CONFIG_POST & CONFIG_SYS_POST_UART |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 28 | |
Stefan Roese | 32a444b | 2007-08-14 14:39:44 +0200 | [diff] [blame] | 29 | /* |
| 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-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 33 | #ifndef CONFIG_SYS_POST_UART_TABLE |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 34 | #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 Roese | 32a444b | 2007-08-14 14:39:44 +0200 | [diff] [blame] | 37 | #endif |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 38 | |
Stefan Roese | d0c966a | 2010-09-14 09:38:18 +0200 | [diff] [blame] | 39 | DECLARE_GLOBAL_DATA_PTR; |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 40 | |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 41 | static int test_ctlr (struct NS16550 *com_port, int index) |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 42 | { |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 43 | int res = -1; |
| 44 | char test_str[] = "*** UART Test String ***\r\n"; |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 45 | int i; |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 46 | int divisor; |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 47 | |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 48 | divisor = (get_serial_clock() + (gd->baudrate * (16 / 2))) / |
| 49 | (16 * gd->baudrate); |
| 50 | NS16550_init(com_port, divisor); |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 51 | |
| 52 | /* |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 53 | * Set internal loopback mode in UART |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 54 | */ |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 55 | out_8(&com_port->mcr, in_8(&com_port->mcr) | UART_MCR_LOOP); |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 56 | |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 57 | /* Reset FIFOs */ |
| 58 | out_8(&com_port->fcr, UART_FCR_RXSR | UART_FCR_TXSR); |
| 59 | udelay(100); |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 60 | |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 61 | /* Flush RX-FIFO */ |
| 62 | while (NS16550_tstc(com_port)) |
| 63 | NS16550_getc(com_port); |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 64 | |
| 65 | for (i = 0; i < sizeof (test_str) - 1; i++) { |
Stefan Roese | a0a1479 | 2010-09-29 16:58:38 +0200 | [diff] [blame] | 66 | NS16550_putc(com_port, test_str[i]); |
| 67 | if (NS16550_getc(com_port) != test_str[i]) |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 68 | goto done; |
| 69 | } |
| 70 | res = 0; |
| 71 | done: |
| 72 | if (res) |
| 73 | post_log ("uart%d test failed\n", index); |
| 74 | |
| 75 | return res; |
| 76 | } |
| 77 | |
| 78 | int uart_post_test (int flags) |
| 79 | { |
| 80 | int i, res = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 81 | static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE; |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 82 | |
Stefan Roese | d0c966a | 2010-09-14 09:38:18 +0200 | [diff] [blame] | 83 | for (i = 0; i < ARRAY_SIZE(base); i++) { |
| 84 | if (test_ctlr((struct NS16550 *)base[i], i)) |
Igor Lisitsin | 95bcd38 | 2007-03-28 19:06:19 +0400 | [diff] [blame] | 85 | res = -1; |
| 86 | } |
| 87 | serial_reinit_all (); |
| 88 | |
| 89 | return res; |
| 90 | } |
| 91 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 92 | #endif /* CONFIG_POST & CONFIG_SYS_POST_UART */ |