demo.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 *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. if (!api_search_sig(&sig))
  45. return -1;
  46. syscall_ptr = sig->syscall;
  47. if (syscall_ptr == NULL)
  48. return -2;
  49. if (sig->version > API_SIG_VERSION)
  50. return -3;
  51. printf("API signature found @%x\n", (unsigned int)sig);
  52. test_dump_sig(sig);
  53. printf("\n*** Consumer API test ***\n");
  54. printf("syscall ptr 0x%08x@%08x\n", (unsigned int)syscall_ptr,
  55. (unsigned int)&syscall_ptr);
  56. /* console activities */
  57. ub_putc('B');
  58. printf("*** Press any key to continue ***\n");
  59. printf("got char 0x%x\n", ub_getc());
  60. /* system info */
  61. test_dump_si(ub_get_sys_info());
  62. /* timing */
  63. printf("\n*** Timing - wait a couple of secs ***\n");
  64. start = ub_get_timer(0);
  65. printf("\ntime: start %lu\n\n", start);
  66. for (i = 0; i < WAIT_SECS; i++)
  67. for (j = 0; j < 1000; j++)
  68. ub_udelay(1000); /* wait 1 ms */
  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. /* enumerate devices */
  73. printf("\n*** Enumerate devices ***\n");
  74. devs_no = ub_dev_enum();
  75. printf("Number of devices found: %d\n", devs_no);
  76. if (devs_no == 0)
  77. return -1;
  78. printf("\n*** Show devices ***\n");
  79. for (i = 0; i < devs_no; i++) {
  80. test_dump_di(i);
  81. printf("\n");
  82. }
  83. printf("\n*** Operations on devices ***\n");
  84. /* test opening a device already opened */
  85. h = 0;
  86. if ((rv = ub_dev_open(h)) != 0) {
  87. errf("open device %d error %d\n", h, rv);
  88. return -1;
  89. }
  90. if ((rv = ub_dev_open(h)) != 0)
  91. errf("open device %d error %d\n", h, rv);
  92. ub_dev_close(h);
  93. /* test storage */
  94. printf("Trying storage devices...\n");
  95. for (i = 0; i < devs_no; i++) {
  96. di = ub_dev_get(i);
  97. if (di->type & DEV_TYP_STOR)
  98. break;
  99. }
  100. if (i == devs_no)
  101. printf("No storage devices available\n");
  102. else {
  103. memset(buf, 0, BUF_SZ);
  104. if ((rv = ub_dev_open(i)) != 0)
  105. errf("open device %d error %d\n", i, rv);
  106. else if ((rv = ub_dev_read(i, buf, 1, 0, &rlen)) != 0)
  107. errf("could not read from device %d, error %d\n", i, rv);
  108. else {
  109. printf("Sector 0 dump (512B):\n");
  110. test_dump_buf(buf, 512);
  111. }
  112. ub_dev_close(i);
  113. }
  114. /* test networking */
  115. printf("Trying network devices...\n");
  116. for (i = 0; i < devs_no; i++) {
  117. di = ub_dev_get(i);
  118. if (di->type == DEV_TYP_NET)
  119. break;
  120. }
  121. if (i == devs_no)
  122. printf("No network devices available\n");
  123. else {
  124. if ((rv = ub_dev_open(i)) != 0)
  125. errf("open device %d error %d\n", i, rv);
  126. else if ((rv = ub_dev_send(i, &buf, 2048)) != 0)
  127. errf("could not send to device %d, error %d\n", i, rv);
  128. ub_dev_close(i);
  129. }
  130. if (ub_dev_close(h) != 0)
  131. errf("could not close device %d\n", h);
  132. printf("\n*** Env vars ***\n");
  133. printf("ethact = %s\n", ub_env_get("ethact"));
  134. printf("old fileaddr = %s\n", ub_env_get("fileaddr"));
  135. ub_env_set("fileaddr", "deadbeef");
  136. printf("new fileaddr = %s\n", ub_env_get("fileaddr"));
  137. const char *env = NULL;
  138. while ((env = ub_env_enum(env)) != NULL)
  139. printf("%s = %s\n", env, ub_env_get(env));
  140. /* reset */
  141. printf("\n*** Resetting board ***\n");
  142. ub_reset();
  143. printf("\nHmm, reset returned...?!\n");
  144. return rv;
  145. }
  146. void test_dump_sig(struct api_signature *sig)
  147. {
  148. printf("signature:\n");
  149. printf(" version\t= %d\n", sig->version);
  150. printf(" checksum\t= 0x%08x\n", sig->checksum);
  151. printf(" sc entry\t= 0x%08x\n", (unsigned int)sig->syscall);
  152. }
  153. void test_dump_si(struct sys_info *si)
  154. {
  155. int i;
  156. printf("sys info:\n");
  157. printf(" clkbus\t= 0x%08x\n", (unsigned int)si->clk_bus);
  158. printf(" clkcpu\t= 0x%08x\n", (unsigned int)si->clk_cpu);
  159. printf(" bar\t\t= 0x%08x\n", (unsigned int)si->bar);
  160. printf("---\n");
  161. for (i = 0; i < si->mr_no; i++) {
  162. if (si->mr[i].flags == 0)
  163. break;
  164. printf(" start\t= 0x%08lx\n", si->mr[i].start);
  165. printf(" size\t= 0x%08lx\n", si->mr[i].size);
  166. switch(si->mr[i].flags & 0x000F) {
  167. case MR_ATTR_FLASH:
  168. printf(" type FLASH\n");
  169. break;
  170. case MR_ATTR_DRAM:
  171. printf(" type DRAM\n");
  172. break;
  173. case MR_ATTR_SRAM:
  174. printf(" type SRAM\n");
  175. break;
  176. default:
  177. printf(" type UNKNOWN\n");
  178. }
  179. printf("---\n");
  180. }
  181. }
  182. static char *test_stor_typ(int type)
  183. {
  184. if (type & DT_STOR_IDE)
  185. return "IDE";
  186. if (type & DT_STOR_MMC)
  187. return "MMC";
  188. if (type & DT_STOR_SATA)
  189. return "SATA";
  190. if (type & DT_STOR_SCSI)
  191. return "SCSI";
  192. if (type & DT_STOR_USB)
  193. return "USB";
  194. return "Unknown";
  195. }
  196. void test_dump_buf(void *buf, int len)
  197. {
  198. int i;
  199. int line_counter = 0;
  200. int sep_flag = 0;
  201. int addr = 0;
  202. printf("%07x:\t", addr);
  203. for (i = 0; i < len; i++) {
  204. if (line_counter++ > 15) {
  205. line_counter = 0;
  206. sep_flag = 0;
  207. addr += 16;
  208. i--;
  209. printf("\n%07x:\t", addr);
  210. continue;
  211. }
  212. if (sep_flag++ > 1) {
  213. sep_flag = 1;
  214. printf(" ");
  215. }
  216. printf("%02x", *((char *)buf++));
  217. }
  218. printf("\n");
  219. }
  220. void test_dump_di(int handle)
  221. {
  222. int i;
  223. struct device_info *di = ub_dev_get(handle);
  224. printf("device info (%d):\n", handle);
  225. printf(" cookie\t= 0x%08x\n", (uint32_t)di->cookie);
  226. printf(" type\t\t= 0x%08x\n", di->type);
  227. if (di->type == DEV_TYP_NET) {
  228. printf(" hwaddr\t= ");
  229. for (i = 0; i < 6; i++)
  230. printf("%02x ", di->di_net.hwaddr[i]);
  231. printf("\n");
  232. } else if (di->type & DEV_TYP_STOR) {
  233. printf(" type\t\t= %s\n", test_stor_typ(di->type));
  234. printf(" blk size\t\t= %d\n", (unsigned int)di->di_stor.block_size);
  235. printf(" blk count\t\t= %d\n", (unsigned int)di->di_stor.block_count);
  236. }
  237. }