1 /*
2  * Copyright (C) 2008 Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  */
19 
20 #include <common.h>
21 #include <asm/io.h>
22 #include <asm/processor.h>
23 #include <asm/pci.h>
24 
25 #if defined(CONFIG_CPU_32BIT)
26 #define NOCACHE_OFFSET		0x00000000
27 #else
28 #define NOCACHE_OFFSET		0xa0000000
29 #endif
30 #define PLD_LEDCR		(0x04000008 + NOCACHE_OFFSET)
31 #define PLD_SWSR		(0x0400000a + NOCACHE_OFFSET)
32 #define PLD_VERSR		(0x0400000c + NOCACHE_OFFSET)
33 
34 #define SM107_DEVICEID		(0x13e00060 + NOCACHE_OFFSET)
35 
36 static void wait_ms(unsigned long time)
37 {
38 	while (time--)
39 		udelay(1000);
40 }
41 
42 static void test_pld(void)
43 {
44 	printf("PLD version = %04x\n", readb(PLD_VERSR));
45 }
46 
47 static void test_sm107(void)
48 {
49 	printf("SM107 device ID = %04x\n", readl(SM107_DEVICEID));
50 }
51 
52 static void test_led(void)
53 {
54 	printf("turn on LEDs 3, 5, 7, 9\n");
55 	writeb(0x55, PLD_LEDCR);
56 	wait_ms(2000);
57 	printf("turn on LEDs 4, 6, 8, 10\n");
58 	writeb(0xaa, PLD_LEDCR);
59 	wait_ms(2000);
60 	writeb(0x00, PLD_LEDCR);
61 }
62 
63 static void test_dipsw(void)
64 {
65 	printf("Please DIPSW set = B'0101\n");
66 	while (readb(PLD_SWSR) != 0x05) {
67 		if (ctrlc())
68 			return;
69 	}
70 	printf("Please DIPSW set = B'1010\n");
71 	while (readb(PLD_SWSR) != 0x0A) {
72 		if (ctrlc())
73 			return;
74 	}
75 	printf("DIPSW OK\n");
76 }
77 
78 static void test_net(void)
79 {
80 	unsigned long data;
81 
82 	writel(0x80000000, 0xfe0401c0);
83 	data = readl(0xfe040220);
84 	if (data == 0x816910ec)
85 		printf("Ethernet OK\n");
86 	else
87 		printf("Ethernet NG, data = %08x\n", (unsigned int)data);
88 }
89 
90 static void test_sata(void)
91 {
92 	unsigned long data;
93 
94 	writel(0x80000800, 0xfe0401c0);
95 	data = readl(0xfe040220);
96 	if (data == 0x35121095)
97 		printf("SATA OK\n");
98 	else
99 		printf("SATA NG, data = %08x\n", (unsigned int)data);
100 }
101 
102 static void test_pci(void)
103 {
104 	writel(0x80001800, 0xfe0401c0);
105 	printf("PCI CN1 ID = %08x\n", readl(0xfe040220));
106 
107 	writel(0x80001000, 0xfe0401c0);
108 	printf("PCI CN2 ID = %08x\n", readl(0xfe040220));
109 }
110 
111 int do_hw_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
112 {
113 	char *cmd;
114 
115 	if (argc != 2)
116 		return cmd_usage(cmdtp);
117 
118 	cmd = argv[1];
119 	switch (cmd[0]) {
120 	case 'a':	/* all */
121 		test_pld();
122 		test_led();
123 		test_dipsw();
124 		test_sm107();
125 		test_net();
126 		test_sata();
127 		test_pci();
128 		break;
129 	case 'p':	/* pld or pci */
130 		if (cmd[1] == 'l')
131 			test_pld();
132 		else
133 			test_pci();
134 		break;
135 	case 'l':	/* led */
136 		test_led();
137 		break;
138 	case 'd':	/* dipsw */
139 		test_dipsw();
140 		break;
141 	case 's':	/* sm107 or sata */
142 		if (cmd[1] == 'm')
143 			test_sm107();
144 		else
145 			test_sata();
146 		break;
147 	case 'n':	/* net */
148 		test_net();
149 		break;
150 	default:
151 		return cmd_usage(cmdtp);
152 	}
153 
154 	return 0;
155 }
156 
157 U_BOOT_CMD(
158 	hwtest,	2,	1,	do_hw_test,
159 	"hardware test for R0P7785LC0011RL board",
160 	"\n"
161 	"hwtest all   - test all hardware\n"
162 	"hwtest pld   - output PLD version\n"
163 	"hwtest led   - turn on LEDs\n"
164 	"hwtest dipsw - test DIP switch\n"
165 	"hwtest sm107 - output SM107 version\n"
166 	"hwtest net   - check RTL8110 ID\n"
167 	"hwtest sata  - check SiI3512 ID\n"
168 	"hwtest pci   - output PCI slot device ID"
169 );
170