demo.c 7.6 KB

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