Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 2 | /* |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 3 | * (C) Copyright 2007-2008 Semihalf |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 4 | * |
| 5 | * Written by: Rafal Jaworowski <raj@semihalf.com> |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <api_public.h> |
| 11 | |
| 12 | #include "glue.h" |
| 13 | |
| 14 | #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0) |
| 15 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 16 | #define BUF_SZ 2048 |
| 17 | #define WAIT_SECS 5 |
| 18 | |
| 19 | void test_dump_buf(void *, int); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 20 | void test_dump_di(int); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 21 | void test_dump_si(struct sys_info *); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 22 | void test_dump_sig(struct api_signature *); |
| 23 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 24 | static char buf[BUF_SZ]; |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 25 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 26 | int main(int argc, char * const argv[]) |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 27 | { |
Rafal Jaworowski | 923aa48 | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 28 | int rv = 0, h, i, j, devs_no; |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 29 | struct api_signature *sig = NULL; |
| 30 | ulong start, now; |
| 31 | struct device_info *di; |
Rafal Jaworowski | 923aa48 | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 32 | lbasize_t rlen; |
Che-Liang Chiou | a2a5729 | 2011-10-20 23:04:22 +0000 | [diff] [blame] | 33 | struct display_info disinfo; |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 34 | |
| 35 | if (!api_search_sig(&sig)) |
| 36 | return -1; |
| 37 | |
| 38 | syscall_ptr = sig->syscall; |
| 39 | if (syscall_ptr == NULL) |
| 40 | return -2; |
| 41 | |
| 42 | if (sig->version > API_SIG_VERSION) |
| 43 | return -3; |
| 44 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 45 | printf("API signature found @%x\n", (unsigned int)sig); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 46 | test_dump_sig(sig); |
| 47 | |
| 48 | printf("\n*** Consumer API test ***\n"); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 49 | printf("syscall ptr 0x%08x@%08x\n", (unsigned int)syscall_ptr, |
| 50 | (unsigned int)&syscall_ptr); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 51 | |
| 52 | /* console activities */ |
| 53 | ub_putc('B'); |
| 54 | |
| 55 | printf("*** Press any key to continue ***\n"); |
| 56 | printf("got char 0x%x\n", ub_getc()); |
| 57 | |
| 58 | /* system info */ |
| 59 | test_dump_si(ub_get_sys_info()); |
| 60 | |
| 61 | /* timing */ |
| 62 | printf("\n*** Timing - wait a couple of secs ***\n"); |
| 63 | start = ub_get_timer(0); |
| 64 | printf("\ntime: start %lu\n\n", start); |
| 65 | for (i = 0; i < WAIT_SECS; i++) |
| 66 | for (j = 0; j < 1000; j++) |
| 67 | ub_udelay(1000); /* wait 1 ms */ |
| 68 | |
| 69 | /* this is the number of milliseconds that passed from ub_get_timer(0) */ |
| 70 | now = ub_get_timer(start); |
| 71 | printf("\ntime: now %lu\n\n", now); |
| 72 | |
| 73 | /* enumerate devices */ |
| 74 | printf("\n*** Enumerate devices ***\n"); |
| 75 | devs_no = ub_dev_enum(); |
Wolfgang Denk | d3a6532 | 2008-01-10 00:55:14 +0100 | [diff] [blame] | 76 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 77 | printf("Number of devices found: %d\n", devs_no); |
| 78 | if (devs_no == 0) |
| 79 | return -1; |
| 80 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 81 | printf("\n*** Show devices ***\n"); |
| 82 | for (i = 0; i < devs_no; i++) { |
| 83 | test_dump_di(i); |
| 84 | printf("\n"); |
| 85 | } |
| 86 | |
| 87 | printf("\n*** Operations on devices ***\n"); |
| 88 | |
| 89 | /* test opening a device already opened */ |
| 90 | h = 0; |
| 91 | if ((rv = ub_dev_open(h)) != 0) { |
| 92 | errf("open device %d error %d\n", h, rv); |
| 93 | return -1; |
| 94 | } |
| 95 | if ((rv = ub_dev_open(h)) != 0) |
| 96 | errf("open device %d error %d\n", h, rv); |
| 97 | |
| 98 | ub_dev_close(h); |
| 99 | |
| 100 | /* test storage */ |
| 101 | printf("Trying storage devices...\n"); |
| 102 | for (i = 0; i < devs_no; i++) { |
| 103 | di = ub_dev_get(i); |
| 104 | |
| 105 | if (di->type & DEV_TYP_STOR) |
| 106 | break; |
| 107 | |
| 108 | } |
| 109 | if (i == devs_no) |
| 110 | printf("No storage devices available\n"); |
| 111 | else { |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 112 | memset(buf, 0, BUF_SZ); |
| 113 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 114 | if ((rv = ub_dev_open(i)) != 0) |
| 115 | errf("open device %d error %d\n", i, rv); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 116 | |
Rafal Jaworowski | 923aa48 | 2009-01-23 13:27:18 +0100 | [diff] [blame] | 117 | else if ((rv = ub_dev_read(i, buf, 1, 0, &rlen)) != 0) |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 118 | errf("could not read from device %d, error %d\n", i, rv); |
Rafal Jaworowski | 44a94e5 | 2009-01-23 13:27:17 +0100 | [diff] [blame] | 119 | else { |
| 120 | printf("Sector 0 dump (512B):\n"); |
| 121 | test_dump_buf(buf, 512); |
| 122 | } |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 123 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 124 | ub_dev_close(i); |
| 125 | } |
| 126 | |
| 127 | /* test networking */ |
| 128 | printf("Trying network devices...\n"); |
| 129 | for (i = 0; i < devs_no; i++) { |
| 130 | di = ub_dev_get(i); |
| 131 | |
| 132 | if (di->type == DEV_TYP_NET) |
| 133 | break; |
| 134 | |
| 135 | } |
| 136 | if (i == devs_no) |
| 137 | printf("No network devices available\n"); |
| 138 | else { |
| 139 | if ((rv = ub_dev_open(i)) != 0) |
| 140 | errf("open device %d error %d\n", i, rv); |
| 141 | else if ((rv = ub_dev_send(i, &buf, 2048)) != 0) |
| 142 | errf("could not send to device %d, error %d\n", i, rv); |
| 143 | |
| 144 | ub_dev_close(i); |
| 145 | } |
| 146 | |
| 147 | if (ub_dev_close(h) != 0) |
| 148 | errf("could not close device %d\n", h); |
| 149 | |
| 150 | printf("\n*** Env vars ***\n"); |
| 151 | |
| 152 | printf("ethact = %s\n", ub_env_get("ethact")); |
| 153 | printf("old fileaddr = %s\n", ub_env_get("fileaddr")); |
| 154 | ub_env_set("fileaddr", "deadbeef"); |
| 155 | printf("new fileaddr = %s\n", ub_env_get("fileaddr")); |
| 156 | |
| 157 | const char *env = NULL; |
| 158 | |
| 159 | while ((env = ub_env_enum(env)) != NULL) |
| 160 | printf("%s = %s\n", env, ub_env_get(env)); |
| 161 | |
Che-Liang Chiou | a2a5729 | 2011-10-20 23:04:22 +0000 | [diff] [blame] | 162 | printf("\n*** Display ***\n"); |
| 163 | |
| 164 | if (ub_display_get_info(DISPLAY_TYPE_LCD, &disinfo)) { |
| 165 | printf("LCD info: failed\n"); |
| 166 | } else { |
| 167 | printf("LCD info:\n"); |
| 168 | printf(" pixel width: %d\n", disinfo.pixel_width); |
| 169 | printf(" pixel height: %d\n", disinfo.pixel_height); |
| 170 | printf(" screen rows: %d\n", disinfo.screen_rows); |
| 171 | printf(" screen cols: %d\n", disinfo.screen_cols); |
| 172 | } |
| 173 | if (ub_display_get_info(DISPLAY_TYPE_VIDEO, &disinfo)) { |
| 174 | printf("video info: failed\n"); |
| 175 | } else { |
| 176 | printf("video info:\n"); |
| 177 | printf(" pixel width: %d\n", disinfo.pixel_width); |
| 178 | printf(" pixel height: %d\n", disinfo.pixel_height); |
| 179 | printf(" screen rows: %d\n", disinfo.screen_rows); |
| 180 | printf(" screen cols: %d\n", disinfo.screen_cols); |
| 181 | } |
| 182 | |
| 183 | printf("*** Press any key to continue ***\n"); |
| 184 | printf("got char 0x%x\n", ub_getc()); |
| 185 | |
| 186 | /* |
| 187 | * This only clears messages on screen, not on serial port. It is |
| 188 | * equivalent to a no-op if no display is available. |
| 189 | */ |
| 190 | ub_display_clear(); |
| 191 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 192 | /* reset */ |
Rafal Jaworowski | 44a94e5 | 2009-01-23 13:27:17 +0100 | [diff] [blame] | 193 | printf("\n*** Resetting board ***\n"); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 194 | ub_reset(); |
| 195 | printf("\nHmm, reset returned...?!\n"); |
| 196 | |
| 197 | return rv; |
| 198 | } |
| 199 | |
| 200 | void test_dump_sig(struct api_signature *sig) |
| 201 | { |
| 202 | printf("signature:\n"); |
| 203 | printf(" version\t= %d\n", sig->version); |
| 204 | printf(" checksum\t= 0x%08x\n", sig->checksum); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 205 | printf(" sc entry\t= 0x%08x\n", (unsigned int)sig->syscall); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void test_dump_si(struct sys_info *si) |
| 209 | { |
| 210 | int i; |
| 211 | |
| 212 | printf("sys info:\n"); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 213 | printf(" clkbus\t= 0x%08x\n", (unsigned int)si->clk_bus); |
| 214 | printf(" clkcpu\t= 0x%08x\n", (unsigned int)si->clk_cpu); |
| 215 | printf(" bar\t\t= 0x%08x\n", (unsigned int)si->bar); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 216 | |
| 217 | printf("---\n"); |
| 218 | for (i = 0; i < si->mr_no; i++) { |
| 219 | if (si->mr[i].flags == 0) |
| 220 | break; |
| 221 | |
| 222 | printf(" start\t= 0x%08lx\n", si->mr[i].start); |
| 223 | printf(" size\t= 0x%08lx\n", si->mr[i].size); |
| 224 | |
| 225 | switch(si->mr[i].flags & 0x000F) { |
| 226 | case MR_ATTR_FLASH: |
| 227 | printf(" type FLASH\n"); |
| 228 | break; |
| 229 | case MR_ATTR_DRAM: |
| 230 | printf(" type DRAM\n"); |
| 231 | break; |
| 232 | case MR_ATTR_SRAM: |
| 233 | printf(" type SRAM\n"); |
| 234 | break; |
| 235 | default: |
| 236 | printf(" type UNKNOWN\n"); |
| 237 | } |
| 238 | printf("---\n"); |
| 239 | } |
| 240 | } |
| 241 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 242 | static char *test_stor_typ(int type) |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 243 | { |
| 244 | if (type & DT_STOR_IDE) |
| 245 | return "IDE"; |
| 246 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 247 | if (type & DT_STOR_MMC) |
| 248 | return "MMC"; |
| 249 | |
| 250 | if (type & DT_STOR_SATA) |
| 251 | return "SATA"; |
| 252 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 253 | if (type & DT_STOR_SCSI) |
| 254 | return "SCSI"; |
| 255 | |
| 256 | if (type & DT_STOR_USB) |
| 257 | return "USB"; |
| 258 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 259 | return "Unknown"; |
| 260 | } |
| 261 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 262 | void test_dump_buf(void *buf, int len) |
| 263 | { |
| 264 | int i; |
| 265 | int line_counter = 0; |
| 266 | int sep_flag = 0; |
| 267 | int addr = 0; |
| 268 | |
| 269 | printf("%07x:\t", addr); |
| 270 | |
| 271 | for (i = 0; i < len; i++) { |
| 272 | if (line_counter++ > 15) { |
| 273 | line_counter = 0; |
| 274 | sep_flag = 0; |
| 275 | addr += 16; |
| 276 | i--; |
| 277 | printf("\n%07x:\t", addr); |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | if (sep_flag++ > 1) { |
| 282 | sep_flag = 1; |
| 283 | printf(" "); |
| 284 | } |
| 285 | |
| 286 | printf("%02x", *((char *)buf++)); |
| 287 | } |
| 288 | |
| 289 | printf("\n"); |
| 290 | } |
| 291 | |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 292 | void test_dump_di(int handle) |
| 293 | { |
| 294 | int i; |
| 295 | struct device_info *di = ub_dev_get(handle); |
| 296 | |
| 297 | printf("device info (%d):\n", handle); |
| 298 | printf(" cookie\t= 0x%08x\n", (uint32_t)di->cookie); |
| 299 | printf(" type\t\t= 0x%08x\n", di->type); |
| 300 | |
| 301 | if (di->type == DEV_TYP_NET) { |
| 302 | printf(" hwaddr\t= "); |
| 303 | for (i = 0; i < 6; i++) |
| 304 | printf("%02x ", di->di_net.hwaddr[i]); |
| 305 | |
| 306 | printf("\n"); |
| 307 | |
| 308 | } else if (di->type & DEV_TYP_STOR) { |
| 309 | printf(" type\t\t= %s\n", test_stor_typ(di->type)); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 310 | printf(" blk size\t\t= %d\n", (unsigned int)di->di_stor.block_size); |
| 311 | printf(" blk count\t\t= %d\n", (unsigned int)di->di_stor.block_count); |
Rafal Jaworowski | 500856e | 2008-01-09 19:39:36 +0100 | [diff] [blame] | 312 | } |
| 313 | } |