1 /*
2  * Copyright (C) 2008 Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <console.h>
9 #include <asm/io.h>
10 #include <asm/processor.h>
11 #include <asm/pci.h>
12 
13 #if defined(CONFIG_CPU_32BIT)
14 #define NOCACHE_OFFSET		0x00000000
15 #else
16 #define NOCACHE_OFFSET		0xa0000000
17 #endif
18 #define PLD_LEDCR		(0x04000008 + NOCACHE_OFFSET)
19 #define PLD_SWSR		(0x0400000a + NOCACHE_OFFSET)
20 #define PLD_VERSR		(0x0400000c + NOCACHE_OFFSET)
21 
22 #define SM107_DEVICEID		(0x13e00060 + NOCACHE_OFFSET)
23 
24 static void test_pld(void)
25 {
26 	printf("PLD version = %04x\n", readb(PLD_VERSR));
27 }
28 
29 static void test_sm107(void)
30 {
31 	printf("SM107 device ID = %04x\n", readl(SM107_DEVICEID));
32 }
33 
34 static void test_led(void)
35 {
36 	printf("turn on LEDs 3, 5, 7, 9\n");
37 	writeb(0x55, PLD_LEDCR);
38 	mdelay(2000);
39 	printf("turn on LEDs 4, 6, 8, 10\n");
40 	writeb(0xaa, PLD_LEDCR);
41 	mdelay(2000);
42 	writeb(0x00, PLD_LEDCR);
43 }
44 
45 static void test_dipsw(void)
46 {
47 	printf("Please DIPSW set = B'0101\n");
48 	while (readb(PLD_SWSR) != 0x05) {
49 		if (ctrlc())
50 			return;
51 	}
52 	printf("Please DIPSW set = B'1010\n");
53 	while (readb(PLD_SWSR) != 0x0A) {
54 		if (ctrlc())
55 			return;
56 	}
57 	printf("DIPSW OK\n");
58 }
59 
60 static void test_net(void)
61 {
62 	unsigned long data;
63 
64 	writel(0x80000000, 0xfe0401c0);
65 	data = readl(0xfe040220);
66 	if (data == 0x816910ec)
67 		printf("Ethernet OK\n");
68 	else
69 		printf("Ethernet NG, data = %08x\n", (unsigned int)data);
70 }
71 
72 static void test_sata(void)
73 {
74 	unsigned long data;
75 
76 	writel(0x80000800, 0xfe0401c0);
77 	data = readl(0xfe040220);
78 	if (data == 0x35121095)
79 		printf("SATA OK\n");
80 	else
81 		printf("SATA NG, data = %08x\n", (unsigned int)data);
82 }
83 
84 static void test_pci(void)
85 {
86 	writel(0x80001800, 0xfe0401c0);
87 	printf("PCI CN1 ID = %08x\n", readl(0xfe040220));
88 
89 	writel(0x80001000, 0xfe0401c0);
90 	printf("PCI CN2 ID = %08x\n", readl(0xfe040220));
91 }
92 
93 int do_hw_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
94 {
95 	char *cmd;
96 
97 	if (argc != 2)
98 		return cmd_usage(cmdtp);
99 
100 	cmd = argv[1];
101 	switch (cmd[0]) {
102 	case 'a':	/* all */
103 		test_pld();
104 		test_led();
105 		test_dipsw();
106 		test_sm107();
107 		test_net();
108 		test_sata();
109 		test_pci();
110 		break;
111 	case 'p':	/* pld or pci */
112 		if (cmd[1] == 'l')
113 			test_pld();
114 		else
115 			test_pci();
116 		break;
117 	case 'l':	/* led */
118 		test_led();
119 		break;
120 	case 'd':	/* dipsw */
121 		test_dipsw();
122 		break;
123 	case 's':	/* sm107 or sata */
124 		if (cmd[1] == 'm')
125 			test_sm107();
126 		else
127 			test_sata();
128 		break;
129 	case 'n':	/* net */
130 		test_net();
131 		break;
132 	default:
133 		return cmd_usage(cmdtp);
134 	}
135 
136 	return 0;
137 }
138 
139 U_BOOT_CMD(
140 	hwtest,	2,	1,	do_hw_test,
141 	"hardware test for R0P7785LC0011RL board",
142 	"\n"
143 	"hwtest all   - test all hardware\n"
144 	"hwtest pld   - output PLD version\n"
145 	"hwtest led   - turn on LEDs\n"
146 	"hwtest dipsw - test DIP switch\n"
147 	"hwtest sm107 - output SM107 version\n"
148 	"hwtest net   - check RTL8110 ID\n"
149 	"hwtest sata  - check SiI3512 ID\n"
150 	"hwtest pci   - output PCI slot device ID"
151 );
152