blob: 65e74918470f7fc5f8b003a59248af42aa5797eb [file] [log] [blame]
Rafal Jaworowski500856e2008-01-09 19:39:36 +01001/*
Stefan Roesef2302d42008-08-06 14:05:38 +02002 * (C) Copyright 2007-2008 Semihalf
Rafal Jaworowski500856e2008-01-09 19:39:36 +01003 *
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 *
24 */
25
26#include <common.h>
27#include <linux/types.h>
28#include <api_public.h>
29
30#include "glue.h"
31
32#define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
33
Stefan Roesef2302d42008-08-06 14:05:38 +020034#define BUF_SZ 2048
35#define WAIT_SECS 5
36
37void test_dump_buf(void *, int);
Rafal Jaworowski500856e2008-01-09 19:39:36 +010038void test_dump_di(int);
Stefan Roesef2302d42008-08-06 14:05:38 +020039void test_dump_si(struct sys_info *);
Rafal Jaworowski500856e2008-01-09 19:39:36 +010040void test_dump_sig(struct api_signature *);
41
Stefan Roesef2302d42008-08-06 14:05:38 +020042static char buf[BUF_SZ];
Rafal Jaworowski500856e2008-01-09 19:39:36 +010043
Wolfgang Denk54841ab2010-06-28 22:00:46 +020044int main(int argc, char * const argv[])
Rafal Jaworowski500856e2008-01-09 19:39:36 +010045{
Rafal Jaworowski923aa482009-01-23 13:27:18 +010046 int rv = 0, h, i, j, devs_no;
Rafal Jaworowski500856e2008-01-09 19:39:36 +010047 struct api_signature *sig = NULL;
48 ulong start, now;
49 struct device_info *di;
Rafal Jaworowski923aa482009-01-23 13:27:18 +010050 lbasize_t rlen;
Rafal Jaworowski500856e2008-01-09 19:39:36 +010051
52 if (!api_search_sig(&sig))
53 return -1;
54
55 syscall_ptr = sig->syscall;
56 if (syscall_ptr == NULL)
57 return -2;
58
59 if (sig->version > API_SIG_VERSION)
60 return -3;
61
Stefan Roesef2302d42008-08-06 14:05:38 +020062 printf("API signature found @%x\n", (unsigned int)sig);
Rafal Jaworowski500856e2008-01-09 19:39:36 +010063 test_dump_sig(sig);
64
65 printf("\n*** Consumer API test ***\n");
Stefan Roesef2302d42008-08-06 14:05:38 +020066 printf("syscall ptr 0x%08x@%08x\n", (unsigned int)syscall_ptr,
67 (unsigned int)&syscall_ptr);
Rafal Jaworowski500856e2008-01-09 19:39:36 +010068
69 /* console activities */
70 ub_putc('B');
71
72 printf("*** Press any key to continue ***\n");
73 printf("got char 0x%x\n", ub_getc());
74
75 /* system info */
76 test_dump_si(ub_get_sys_info());
77
78 /* timing */
79 printf("\n*** Timing - wait a couple of secs ***\n");
80 start = ub_get_timer(0);
81 printf("\ntime: start %lu\n\n", start);
82 for (i = 0; i < WAIT_SECS; i++)
83 for (j = 0; j < 1000; j++)
84 ub_udelay(1000); /* wait 1 ms */
85
86 /* this is the number of milliseconds that passed from ub_get_timer(0) */
87 now = ub_get_timer(start);
88 printf("\ntime: now %lu\n\n", now);
89
90 /* enumerate devices */
91 printf("\n*** Enumerate devices ***\n");
92 devs_no = ub_dev_enum();
Wolfgang Denkd3a65322008-01-10 00:55:14 +010093
Rafal Jaworowski500856e2008-01-09 19:39:36 +010094 printf("Number of devices found: %d\n", devs_no);
95 if (devs_no == 0)
96 return -1;
97
Rafal Jaworowski500856e2008-01-09 19:39:36 +010098 printf("\n*** Show devices ***\n");
99 for (i = 0; i < devs_no; i++) {
100 test_dump_di(i);
101 printf("\n");
102 }
103
104 printf("\n*** Operations on devices ***\n");
105
106 /* test opening a device already opened */
107 h = 0;
108 if ((rv = ub_dev_open(h)) != 0) {
109 errf("open device %d error %d\n", h, rv);
110 return -1;
111 }
112 if ((rv = ub_dev_open(h)) != 0)
113 errf("open device %d error %d\n", h, rv);
114
115 ub_dev_close(h);
116
117 /* test storage */
118 printf("Trying storage devices...\n");
119 for (i = 0; i < devs_no; i++) {
120 di = ub_dev_get(i);
121
122 if (di->type & DEV_TYP_STOR)
123 break;
124
125 }
126 if (i == devs_no)
127 printf("No storage devices available\n");
128 else {
Stefan Roesef2302d42008-08-06 14:05:38 +0200129 memset(buf, 0, BUF_SZ);
130
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100131 if ((rv = ub_dev_open(i)) != 0)
132 errf("open device %d error %d\n", i, rv);
Stefan Roesef2302d42008-08-06 14:05:38 +0200133
Rafal Jaworowski923aa482009-01-23 13:27:18 +0100134 else if ((rv = ub_dev_read(i, buf, 1, 0, &rlen)) != 0)
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100135 errf("could not read from device %d, error %d\n", i, rv);
Rafal Jaworowski44a94e52009-01-23 13:27:17 +0100136 else {
137 printf("Sector 0 dump (512B):\n");
138 test_dump_buf(buf, 512);
139 }
Stefan Roesef2302d42008-08-06 14:05:38 +0200140
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100141 ub_dev_close(i);
142 }
143
144 /* test networking */
145 printf("Trying network devices...\n");
146 for (i = 0; i < devs_no; i++) {
147 di = ub_dev_get(i);
148
149 if (di->type == DEV_TYP_NET)
150 break;
151
152 }
153 if (i == devs_no)
154 printf("No network devices available\n");
155 else {
156 if ((rv = ub_dev_open(i)) != 0)
157 errf("open device %d error %d\n", i, rv);
158 else if ((rv = ub_dev_send(i, &buf, 2048)) != 0)
159 errf("could not send to device %d, error %d\n", i, rv);
160
161 ub_dev_close(i);
162 }
163
164 if (ub_dev_close(h) != 0)
165 errf("could not close device %d\n", h);
166
167 printf("\n*** Env vars ***\n");
168
169 printf("ethact = %s\n", ub_env_get("ethact"));
170 printf("old fileaddr = %s\n", ub_env_get("fileaddr"));
171 ub_env_set("fileaddr", "deadbeef");
172 printf("new fileaddr = %s\n", ub_env_get("fileaddr"));
173
174 const char *env = NULL;
175
176 while ((env = ub_env_enum(env)) != NULL)
177 printf("%s = %s\n", env, ub_env_get(env));
178
179 /* reset */
Rafal Jaworowski44a94e52009-01-23 13:27:17 +0100180 printf("\n*** Resetting board ***\n");
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100181 ub_reset();
182 printf("\nHmm, reset returned...?!\n");
183
184 return rv;
185}
186
187void test_dump_sig(struct api_signature *sig)
188{
189 printf("signature:\n");
190 printf(" version\t= %d\n", sig->version);
191 printf(" checksum\t= 0x%08x\n", sig->checksum);
Stefan Roesef2302d42008-08-06 14:05:38 +0200192 printf(" sc entry\t= 0x%08x\n", (unsigned int)sig->syscall);
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100193}
194
195void test_dump_si(struct sys_info *si)
196{
197 int i;
198
199 printf("sys info:\n");
Stefan Roesef2302d42008-08-06 14:05:38 +0200200 printf(" clkbus\t= 0x%08x\n", (unsigned int)si->clk_bus);
201 printf(" clkcpu\t= 0x%08x\n", (unsigned int)si->clk_cpu);
202 printf(" bar\t\t= 0x%08x\n", (unsigned int)si->bar);
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100203
204 printf("---\n");
205 for (i = 0; i < si->mr_no; i++) {
206 if (si->mr[i].flags == 0)
207 break;
208
209 printf(" start\t= 0x%08lx\n", si->mr[i].start);
210 printf(" size\t= 0x%08lx\n", si->mr[i].size);
211
212 switch(si->mr[i].flags & 0x000F) {
213 case MR_ATTR_FLASH:
214 printf(" type FLASH\n");
215 break;
216 case MR_ATTR_DRAM:
217 printf(" type DRAM\n");
218 break;
219 case MR_ATTR_SRAM:
220 printf(" type SRAM\n");
221 break;
222 default:
223 printf(" type UNKNOWN\n");
224 }
225 printf("---\n");
226 }
227}
228
Stefan Roesef2302d42008-08-06 14:05:38 +0200229static char *test_stor_typ(int type)
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100230{
231 if (type & DT_STOR_IDE)
232 return "IDE";
233
Stefan Roesef2302d42008-08-06 14:05:38 +0200234 if (type & DT_STOR_MMC)
235 return "MMC";
236
237 if (type & DT_STOR_SATA)
238 return "SATA";
239
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100240 if (type & DT_STOR_SCSI)
241 return "SCSI";
242
243 if (type & DT_STOR_USB)
244 return "USB";
245
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100246 return "Unknown";
247}
248
Stefan Roesef2302d42008-08-06 14:05:38 +0200249void test_dump_buf(void *buf, int len)
250{
251 int i;
252 int line_counter = 0;
253 int sep_flag = 0;
254 int addr = 0;
255
256 printf("%07x:\t", addr);
257
258 for (i = 0; i < len; i++) {
259 if (line_counter++ > 15) {
260 line_counter = 0;
261 sep_flag = 0;
262 addr += 16;
263 i--;
264 printf("\n%07x:\t", addr);
265 continue;
266 }
267
268 if (sep_flag++ > 1) {
269 sep_flag = 1;
270 printf(" ");
271 }
272
273 printf("%02x", *((char *)buf++));
274 }
275
276 printf("\n");
277}
278
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100279void test_dump_di(int handle)
280{
281 int i;
282 struct device_info *di = ub_dev_get(handle);
283
284 printf("device info (%d):\n", handle);
285 printf(" cookie\t= 0x%08x\n", (uint32_t)di->cookie);
286 printf(" type\t\t= 0x%08x\n", di->type);
287
288 if (di->type == DEV_TYP_NET) {
289 printf(" hwaddr\t= ");
290 for (i = 0; i < 6; i++)
291 printf("%02x ", di->di_net.hwaddr[i]);
292
293 printf("\n");
294
295 } else if (di->type & DEV_TYP_STOR) {
296 printf(" type\t\t= %s\n", test_stor_typ(di->type));
Stefan Roesef2302d42008-08-06 14:05:38 +0200297 printf(" blk size\t\t= %d\n", (unsigned int)di->di_stor.block_size);
298 printf(" blk count\t\t= %d\n", (unsigned int)di->di_stor.block_count);
Rafal Jaworowski500856e2008-01-09 19:39:36 +0100299 }
300}