cmd_mem.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Memory Functions
  25. *
  26. * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
  27. */
  28. #include <common.h>
  29. #include <command.h>
  30. #if (CONFIG_COMMANDS & CFG_CMD_MMC)
  31. #include <mmc.h>
  32. #endif
  33. #ifdef CONFIG_HAS_DATAFLASH
  34. #include <dataflash.h>
  35. #endif
  36. #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | \
  37. CFG_CMD_I2C | \
  38. CFG_CMD_ITEST | \
  39. CFG_CMD_PCI | \
  40. CMD_CMD_PORTIO ) )
  41. int cmd_get_data_size(char* arg, int default_size)
  42. {
  43. /* Check for a size specification .b, .w or .l.
  44. */
  45. int len = strlen(arg);
  46. if (len > 2 && arg[len-2] == '.') {
  47. switch(arg[len-1]) {
  48. case 'b':
  49. return 1;
  50. case 'w':
  51. return 2;
  52. case 'l':
  53. return 4;
  54. case 's':
  55. return -2;
  56. default:
  57. return -1;
  58. }
  59. }
  60. return default_size;
  61. }
  62. #endif
  63. #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
  64. #ifdef CMD_MEM_DEBUG
  65. #define PRINTF(fmt,args...) printf (fmt ,##args)
  66. #else
  67. #define PRINTF(fmt,args...)
  68. #endif
  69. static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
  70. /* Display values from last command.
  71. * Memory modify remembered values are different from display memory.
  72. */
  73. uint dp_last_addr, dp_last_size;
  74. uint dp_last_length = 0x40;
  75. uint mm_last_addr, mm_last_size;
  76. static ulong base_address = 0;
  77. /* Memory Display
  78. *
  79. * Syntax:
  80. * md{.b, .w, .l} {addr} {len}
  81. */
  82. #define DISP_LINE_LEN 16
  83. int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  84. {
  85. ulong addr, length;
  86. ulong i, nbytes, linebytes;
  87. u_char *cp;
  88. int size;
  89. int rc = 0;
  90. /* We use the last specified parameters, unless new ones are
  91. * entered.
  92. */
  93. addr = dp_last_addr;
  94. size = dp_last_size;
  95. length = dp_last_length;
  96. if (argc < 2) {
  97. printf ("Usage:\n%s\n", cmdtp->usage);
  98. return 1;
  99. }
  100. if ((flag & CMD_FLAG_REPEAT) == 0) {
  101. /* New command specified. Check for a size specification.
  102. * Defaults to long if no or incorrect specification.
  103. */
  104. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  105. return 1;
  106. /* Address is specified since argc > 1
  107. */
  108. addr = simple_strtoul(argv[1], NULL, 16);
  109. addr += base_address;
  110. /* If another parameter, it is the length to display.
  111. * Length is the number of objects, not number of bytes.
  112. */
  113. if (argc > 2)
  114. length = simple_strtoul(argv[2], NULL, 16);
  115. }
  116. /* Print the lines.
  117. *
  118. * We buffer all read data, so we can make sure data is read only
  119. * once, and all accesses are with the specified bus width.
  120. */
  121. nbytes = length * size;
  122. do {
  123. char linebuf[DISP_LINE_LEN];
  124. uint *uip = (uint *)linebuf;
  125. ushort *usp = (ushort *)linebuf;
  126. u_char *ucp = (u_char *)linebuf;
  127. #ifdef CONFIG_HAS_DATAFLASH
  128. int rc;
  129. #endif
  130. printf("%08lx:", addr);
  131. linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
  132. #ifdef CONFIG_HAS_DATAFLASH
  133. if ((rc = read_dataflash(addr, (linebytes/size)*size, linebuf)) == DATAFLASH_OK){
  134. /* if outside dataflash */
  135. /*if (rc != 1) {
  136. dataflash_perror (rc);
  137. return (1);
  138. }*/
  139. for (i=0; i<linebytes; i+= size) {
  140. if (size == 4) {
  141. printf(" %08x", *uip++);
  142. } else if (size == 2) {
  143. printf(" %04x", *usp++);
  144. } else {
  145. printf(" %02x", *ucp++);
  146. }
  147. addr += size;
  148. }
  149. } else { /* addr does not correspond to DataFlash */
  150. #endif
  151. for (i=0; i<linebytes; i+= size) {
  152. if (size == 4) {
  153. printf(" %08x", (*uip++ = *((uint *)addr)));
  154. } else if (size == 2) {
  155. printf(" %04x", (*usp++ = *((ushort *)addr)));
  156. } else {
  157. printf(" %02x", (*ucp++ = *((u_char *)addr)));
  158. }
  159. addr += size;
  160. }
  161. #ifdef CONFIG_HAS_DATAFLASH
  162. }
  163. #endif
  164. puts (" ");
  165. cp = linebuf;
  166. for (i=0; i<linebytes; i++) {
  167. if ((*cp < 0x20) || (*cp > 0x7e))
  168. putc ('.');
  169. else
  170. printf("%c", *cp);
  171. cp++;
  172. }
  173. putc ('\n');
  174. nbytes -= linebytes;
  175. if (ctrlc()) {
  176. rc = 1;
  177. break;
  178. }
  179. } while (nbytes > 0);
  180. dp_last_addr = addr;
  181. dp_last_length = length;
  182. dp_last_size = size;
  183. return (rc);
  184. }
  185. int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  186. {
  187. return mod_mem (cmdtp, 1, flag, argc, argv);
  188. }
  189. int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  190. {
  191. return mod_mem (cmdtp, 0, flag, argc, argv);
  192. }
  193. int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  194. {
  195. ulong addr, writeval, count;
  196. int size;
  197. if ((argc < 3) || (argc > 4)) {
  198. printf ("Usage:\n%s\n", cmdtp->usage);
  199. return 1;
  200. }
  201. /* Check for size specification.
  202. */
  203. if ((size = cmd_get_data_size(argv[0], 4)) < 1)
  204. return 1;
  205. /* Address is specified since argc > 1
  206. */
  207. addr = simple_strtoul(argv[1], NULL, 16);
  208. addr += base_address;
  209. /* Get the value to write.
  210. */
  211. writeval = simple_strtoul(argv[2], NULL, 16);
  212. /* Count ? */
  213. if (argc == 4) {
  214. count = simple_strtoul(argv[3], NULL, 16);
  215. } else {
  216. count = 1;
  217. }
  218. while (count-- > 0) {
  219. if (size == 4)
  220. *((ulong *)addr) = (ulong )writeval;
  221. else if (size == 2)
  222. *((ushort *)addr) = (ushort)writeval;
  223. else
  224. *((u_char *)addr) = (u_char)writeval;
  225. addr += size;
  226. }
  227. return 0;
  228. }
  229. int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  230. {
  231. ulong addr1, addr2, count, ngood;
  232. int size;
  233. int rcode = 0;
  234. if (argc != 4) {
  235. printf ("Usage:\n%s\n", cmdtp->usage);
  236. return 1;
  237. }
  238. /* Check for size specification.
  239. */
  240. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  241. return 1;
  242. addr1 = simple_strtoul(argv[1], NULL, 16);
  243. addr1 += base_address;
  244. addr2 = simple_strtoul(argv[2], NULL, 16);
  245. addr2 += base_address;
  246. count = simple_strtoul(argv[3], NULL, 16);
  247. #ifdef CONFIG_HAS_DATAFLASH
  248. if (addr_dataflash(addr1) | addr_dataflash(addr2)){
  249. puts ("Comparison with DataFlash space not supported.\n\r");
  250. return 0;
  251. }
  252. #endif
  253. ngood = 0;
  254. while (count-- > 0) {
  255. if (size == 4) {
  256. ulong word1 = *(ulong *)addr1;
  257. ulong word2 = *(ulong *)addr2;
  258. if (word1 != word2) {
  259. printf("word at 0x%08lx (0x%08lx) "
  260. "!= word at 0x%08lx (0x%08lx)\n",
  261. addr1, word1, addr2, word2);
  262. rcode = 1;
  263. break;
  264. }
  265. }
  266. else if (size == 2) {
  267. ushort hword1 = *(ushort *)addr1;
  268. ushort hword2 = *(ushort *)addr2;
  269. if (hword1 != hword2) {
  270. printf("halfword at 0x%08lx (0x%04x) "
  271. "!= halfword at 0x%08lx (0x%04x)\n",
  272. addr1, hword1, addr2, hword2);
  273. rcode = 1;
  274. break;
  275. }
  276. }
  277. else {
  278. u_char byte1 = *(u_char *)addr1;
  279. u_char byte2 = *(u_char *)addr2;
  280. if (byte1 != byte2) {
  281. printf("byte at 0x%08lx (0x%02x) "
  282. "!= byte at 0x%08lx (0x%02x)\n",
  283. addr1, byte1, addr2, byte2);
  284. rcode = 1;
  285. break;
  286. }
  287. }
  288. ngood++;
  289. addr1 += size;
  290. addr2 += size;
  291. }
  292. printf("Total of %ld %s%s were the same\n",
  293. ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
  294. ngood == 1 ? "" : "s");
  295. return rcode;
  296. }
  297. int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  298. {
  299. ulong addr, dest, count;
  300. int size;
  301. if (argc != 4) {
  302. printf ("Usage:\n%s\n", cmdtp->usage);
  303. return 1;
  304. }
  305. /* Check for size specification.
  306. */
  307. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  308. return 1;
  309. addr = simple_strtoul(argv[1], NULL, 16);
  310. addr += base_address;
  311. dest = simple_strtoul(argv[2], NULL, 16);
  312. dest += base_address;
  313. count = simple_strtoul(argv[3], NULL, 16);
  314. if (count == 0) {
  315. puts ("Zero length ???\n");
  316. return 1;
  317. }
  318. #ifndef CFG_NO_FLASH
  319. /* check if we are copying to Flash */
  320. if ( (addr2info(dest) != NULL)
  321. #ifdef CONFIG_HAS_DATAFLASH
  322. && (!addr_dataflash(addr))
  323. #endif
  324. ) {
  325. int rc;
  326. puts ("Copy to Flash... ");
  327. rc = flash_write ((uchar *)addr, dest, count*size);
  328. if (rc != 0) {
  329. flash_perror (rc);
  330. return (1);
  331. }
  332. puts ("done\n");
  333. return 0;
  334. }
  335. #endif
  336. #if (CONFIG_COMMANDS & CFG_CMD_MMC)
  337. if (mmc2info(dest)) {
  338. int rc;
  339. puts ("Copy to MMC... ");
  340. switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
  341. case 0:
  342. putc ('\n');
  343. return 1;
  344. case -1:
  345. puts ("failed\n");
  346. return 1;
  347. default:
  348. printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
  349. return 1;
  350. }
  351. puts ("done\n");
  352. return 0;
  353. }
  354. if (mmc2info(addr)) {
  355. int rc;
  356. puts ("Copy from MMC... ");
  357. switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
  358. case 0:
  359. putc ('\n');
  360. return 1;
  361. case -1:
  362. puts ("failed\n");
  363. return 1;
  364. default:
  365. printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
  366. return 1;
  367. }
  368. puts ("done\n");
  369. return 0;
  370. }
  371. #endif
  372. #ifdef CONFIG_HAS_DATAFLASH
  373. /* Check if we are copying from RAM or Flash to DataFlash */
  374. if (addr_dataflash(dest) && !addr_dataflash(addr)){
  375. int rc;
  376. puts ("Copy to DataFlash... ");
  377. rc = write_dataflash (dest, addr, count*size);
  378. if (rc != 1) {
  379. dataflash_perror (rc);
  380. return (1);
  381. }
  382. puts ("done\n");
  383. return 0;
  384. }
  385. /* Check if we are copying from DataFlash to RAM */
  386. if (addr_dataflash(addr) && !addr_dataflash(dest) && (addr2info(dest)==NULL) ){
  387. int rc;
  388. rc = read_dataflash(addr, count * size, (char *) dest);
  389. if (rc != 1) {
  390. dataflash_perror (rc);
  391. return (1);
  392. }
  393. return 0;
  394. }
  395. if (addr_dataflash(addr) && addr_dataflash(dest)){
  396. puts ("Unsupported combination of source/destination.\n\r");
  397. return 1;
  398. }
  399. #endif
  400. while (count-- > 0) {
  401. if (size == 4)
  402. *((ulong *)dest) = *((ulong *)addr);
  403. else if (size == 2)
  404. *((ushort *)dest) = *((ushort *)addr);
  405. else
  406. *((u_char *)dest) = *((u_char *)addr);
  407. addr += size;
  408. dest += size;
  409. }
  410. return 0;
  411. }
  412. int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  413. {
  414. if (argc > 1) {
  415. /* Set new base address.
  416. */
  417. base_address = simple_strtoul(argv[1], NULL, 16);
  418. }
  419. /* Print the current base address.
  420. */
  421. printf("Base Address: 0x%08lx\n", base_address);
  422. return 0;
  423. }
  424. int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  425. {
  426. ulong addr, length, i, junk;
  427. int size;
  428. volatile uint *longp;
  429. volatile ushort *shortp;
  430. volatile u_char *cp;
  431. if (argc < 3) {
  432. printf ("Usage:\n%s\n", cmdtp->usage);
  433. return 1;
  434. }
  435. /* Check for a size spefication.
  436. * Defaults to long if no or incorrect specification.
  437. */
  438. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  439. return 1;
  440. /* Address is always specified.
  441. */
  442. addr = simple_strtoul(argv[1], NULL, 16);
  443. /* Length is the number of objects, not number of bytes.
  444. */
  445. length = simple_strtoul(argv[2], NULL, 16);
  446. /* We want to optimize the loops to run as fast as possible.
  447. * If we have only one object, just run infinite loops.
  448. */
  449. if (length == 1) {
  450. if (size == 4) {
  451. longp = (uint *)addr;
  452. for (;;)
  453. i = *longp;
  454. }
  455. if (size == 2) {
  456. shortp = (ushort *)addr;
  457. for (;;)
  458. i = *shortp;
  459. }
  460. cp = (u_char *)addr;
  461. for (;;)
  462. i = *cp;
  463. }
  464. if (size == 4) {
  465. for (;;) {
  466. longp = (uint *)addr;
  467. i = length;
  468. while (i-- > 0)
  469. junk = *longp++;
  470. }
  471. }
  472. if (size == 2) {
  473. for (;;) {
  474. shortp = (ushort *)addr;
  475. i = length;
  476. while (i-- > 0)
  477. junk = *shortp++;
  478. }
  479. }
  480. for (;;) {
  481. cp = (u_char *)addr;
  482. i = length;
  483. while (i-- > 0)
  484. junk = *cp++;
  485. }
  486. }
  487. #ifdef CONFIG_LOOPW
  488. int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  489. {
  490. ulong addr, length, i, data;
  491. int size;
  492. volatile uint *longp;
  493. volatile ushort *shortp;
  494. volatile u_char *cp;
  495. if (argc < 4) {
  496. printf ("Usage:\n%s\n", cmdtp->usage);
  497. return 1;
  498. }
  499. /* Check for a size spefication.
  500. * Defaults to long if no or incorrect specification.
  501. */
  502. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  503. return 1;
  504. /* Address is always specified.
  505. */
  506. addr = simple_strtoul(argv[1], NULL, 16);
  507. /* Length is the number of objects, not number of bytes.
  508. */
  509. length = simple_strtoul(argv[2], NULL, 16);
  510. /* data to write */
  511. data = simple_strtoul(argv[3], NULL, 16);
  512. /* We want to optimize the loops to run as fast as possible.
  513. * If we have only one object, just run infinite loops.
  514. */
  515. if (length == 1) {
  516. if (size == 4) {
  517. longp = (uint *)addr;
  518. for (;;)
  519. *longp = data;
  520. }
  521. if (size == 2) {
  522. shortp = (ushort *)addr;
  523. for (;;)
  524. *shortp = data;
  525. }
  526. cp = (u_char *)addr;
  527. for (;;)
  528. *cp = data;
  529. }
  530. if (size == 4) {
  531. for (;;) {
  532. longp = (uint *)addr;
  533. i = length;
  534. while (i-- > 0)
  535. *longp++ = data;
  536. }
  537. }
  538. if (size == 2) {
  539. for (;;) {
  540. shortp = (ushort *)addr;
  541. i = length;
  542. while (i-- > 0)
  543. *shortp++ = data;
  544. }
  545. }
  546. for (;;) {
  547. cp = (u_char *)addr;
  548. i = length;
  549. while (i-- > 0)
  550. *cp++ = data;
  551. }
  552. }
  553. #endif /* CONFIG_LOOPW */
  554. /*
  555. * Perform a memory test. A more complete alternative test can be
  556. * configured using CFG_ALT_MEMTEST. The complete test loops until
  557. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  558. */
  559. int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  560. {
  561. vu_long *addr, *start, *end;
  562. ulong val;
  563. ulong readback;
  564. #if defined(CFG_ALT_MEMTEST)
  565. vu_long addr_mask;
  566. vu_long offset;
  567. vu_long test_offset;
  568. vu_long pattern;
  569. vu_long temp;
  570. vu_long anti_pattern;
  571. vu_long num_words;
  572. #if defined(CFG_MEMTEST_SCRATCH)
  573. vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
  574. #else
  575. vu_long *dummy = NULL;
  576. #endif
  577. int j;
  578. int iterations = 1;
  579. static const ulong bitpattern[] = {
  580. 0x00000001, /* single bit */
  581. 0x00000003, /* two adjacent bits */
  582. 0x00000007, /* three adjacent bits */
  583. 0x0000000F, /* four adjacent bits */
  584. 0x00000005, /* two non-adjacent bits */
  585. 0x00000015, /* three non-adjacent bits */
  586. 0x00000055, /* four non-adjacent bits */
  587. 0xaaaaaaaa, /* alternating 1/0 */
  588. };
  589. #else
  590. ulong incr;
  591. ulong pattern;
  592. int rcode = 0;
  593. #endif
  594. if (argc > 1) {
  595. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  596. } else {
  597. start = (ulong *)CFG_MEMTEST_START;
  598. }
  599. if (argc > 2) {
  600. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  601. } else {
  602. end = (ulong *)(CFG_MEMTEST_END);
  603. }
  604. if (argc > 3) {
  605. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  606. } else {
  607. pattern = 0;
  608. }
  609. #if defined(CFG_ALT_MEMTEST)
  610. printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
  611. PRINTF("%s:%d: start 0x%p end 0x%p\n",
  612. __FUNCTION__, __LINE__, start, end);
  613. for (;;) {
  614. if (ctrlc()) {
  615. putc ('\n');
  616. return 1;
  617. }
  618. printf("Iteration: %6d\r", iterations);
  619. PRINTF("Iteration: %6d\n", iterations);
  620. iterations++;
  621. /*
  622. * Data line test: write a pattern to the first
  623. * location, write the 1's complement to a 'parking'
  624. * address (changes the state of the data bus so a
  625. * floating bus doen't give a false OK), and then
  626. * read the value back. Note that we read it back
  627. * into a variable because the next time we read it,
  628. * it might be right (been there, tough to explain to
  629. * the quality guys why it prints a failure when the
  630. * "is" and "should be" are obviously the same in the
  631. * error message).
  632. *
  633. * Rather than exhaustively testing, we test some
  634. * patterns by shifting '1' bits through a field of
  635. * '0's and '0' bits through a field of '1's (i.e.
  636. * pattern and ~pattern).
  637. */
  638. addr = start;
  639. for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
  640. val = bitpattern[j];
  641. for(; val != 0; val <<= 1) {
  642. *addr = val;
  643. *dummy = ~val; /* clear the test data off of the bus */
  644. readback = *addr;
  645. if(readback != val) {
  646. printf ("FAILURE (data line): "
  647. "expected %08lx, actual %08lx\n",
  648. val, readback);
  649. }
  650. *addr = ~val;
  651. *dummy = val;
  652. readback = *addr;
  653. if(readback != ~val) {
  654. printf ("FAILURE (data line): "
  655. "Is %08lx, should be %08lx\n",
  656. readback, ~val);
  657. }
  658. }
  659. }
  660. /*
  661. * Based on code whose Original Author and Copyright
  662. * information follows: Copyright (c) 1998 by Michael
  663. * Barr. This software is placed into the public
  664. * domain and may be used for any purpose. However,
  665. * this notice must not be changed or removed and no
  666. * warranty is either expressed or implied by its
  667. * publication or distribution.
  668. */
  669. /*
  670. * Address line test
  671. *
  672. * Description: Test the address bus wiring in a
  673. * memory region by performing a walking
  674. * 1's test on the relevant bits of the
  675. * address and checking for aliasing.
  676. * This test will find single-bit
  677. * address failures such as stuck -high,
  678. * stuck-low, and shorted pins. The base
  679. * address and size of the region are
  680. * selected by the caller.
  681. *
  682. * Notes: For best results, the selected base
  683. * address should have enough LSB 0's to
  684. * guarantee single address bit changes.
  685. * For example, to test a 64-Kbyte
  686. * region, select a base address on a
  687. * 64-Kbyte boundary. Also, select the
  688. * region size as a power-of-two if at
  689. * all possible.
  690. *
  691. * Returns: 0 if the test succeeds, 1 if the test fails.
  692. *
  693. * ## NOTE ## Be sure to specify start and end
  694. * addresses such that addr_mask has
  695. * lots of bits set. For example an
  696. * address range of 01000000 02000000 is
  697. * bad while a range of 01000000
  698. * 01ffffff is perfect.
  699. */
  700. addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
  701. pattern = (vu_long) 0xaaaaaaaa;
  702. anti_pattern = (vu_long) 0x55555555;
  703. PRINTF("%s:%d: addr mask = 0x%.8lx\n",
  704. __FUNCTION__, __LINE__,
  705. addr_mask);
  706. /*
  707. * Write the default pattern at each of the
  708. * power-of-two offsets.
  709. */
  710. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  711. start[offset] = pattern;
  712. }
  713. /*
  714. * Check for address bits stuck high.
  715. */
  716. test_offset = 0;
  717. start[test_offset] = anti_pattern;
  718. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  719. temp = start[offset];
  720. if (temp != pattern) {
  721. printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
  722. " expected 0x%.8lx, actual 0x%.8lx\n",
  723. (ulong)&start[offset], pattern, temp);
  724. return 1;
  725. }
  726. }
  727. start[test_offset] = pattern;
  728. /*
  729. * Check for addr bits stuck low or shorted.
  730. */
  731. for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
  732. start[test_offset] = anti_pattern;
  733. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  734. temp = start[offset];
  735. if ((temp != pattern) && (offset != test_offset)) {
  736. printf ("\nFAILURE: Address bit stuck low or shorted @"
  737. " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
  738. (ulong)&start[offset], pattern, temp);
  739. return 1;
  740. }
  741. }
  742. start[test_offset] = pattern;
  743. }
  744. /*
  745. * Description: Test the integrity of a physical
  746. * memory device by performing an
  747. * increment/decrement test over the
  748. * entire region. In the process every
  749. * storage bit in the device is tested
  750. * as a zero and a one. The base address
  751. * and the size of the region are
  752. * selected by the caller.
  753. *
  754. * Returns: 0 if the test succeeds, 1 if the test fails.
  755. */
  756. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  757. /*
  758. * Fill memory with a known pattern.
  759. */
  760. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  761. start[offset] = pattern;
  762. }
  763. /*
  764. * Check each location and invert it for the second pass.
  765. */
  766. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  767. temp = start[offset];
  768. if (temp != pattern) {
  769. printf ("\nFAILURE (read/write) @ 0x%.8lx:"
  770. " expected 0x%.8lx, actual 0x%.8lx)\n",
  771. (ulong)&start[offset], pattern, temp);
  772. return 1;
  773. }
  774. anti_pattern = ~pattern;
  775. start[offset] = anti_pattern;
  776. }
  777. /*
  778. * Check each location for the inverted pattern and zero it.
  779. */
  780. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  781. anti_pattern = ~pattern;
  782. temp = start[offset];
  783. if (temp != anti_pattern) {
  784. printf ("\nFAILURE (read/write): @ 0x%.8lx:"
  785. " expected 0x%.8lx, actual 0x%.8lx)\n",
  786. (ulong)&start[offset], anti_pattern, temp);
  787. return 1;
  788. }
  789. start[offset] = 0;
  790. }
  791. }
  792. #else /* The original, quickie test */
  793. incr = 1;
  794. for (;;) {
  795. if (ctrlc()) {
  796. putc ('\n');
  797. return 1;
  798. }
  799. printf ("\rPattern %08lX Writing..."
  800. "%12s"
  801. "\b\b\b\b\b\b\b\b\b\b",
  802. pattern, "");
  803. for (addr=start,val=pattern; addr<end; addr++) {
  804. *addr = val;
  805. val += incr;
  806. }
  807. puts ("Reading...");
  808. for (addr=start,val=pattern; addr<end; addr++) {
  809. readback = *addr;
  810. if (readback != val) {
  811. printf ("\nMem error @ 0x%08X: "
  812. "found %08lX, expected %08lX\n",
  813. (uint)addr, readback, val);
  814. rcode = 1;
  815. }
  816. val += incr;
  817. }
  818. /*
  819. * Flip the pattern each time to make lots of zeros and
  820. * then, the next time, lots of ones. We decrement
  821. * the "negative" patterns and increment the "positive"
  822. * patterns to preserve this feature.
  823. */
  824. if(pattern & 0x80000000) {
  825. pattern = -pattern; /* complement & increment */
  826. }
  827. else {
  828. pattern = ~pattern;
  829. }
  830. incr = -incr;
  831. }
  832. return rcode;
  833. #endif
  834. }
  835. /* Modify memory.
  836. *
  837. * Syntax:
  838. * mm{.b, .w, .l} {addr}
  839. * nm{.b, .w, .l} {addr}
  840. */
  841. static int
  842. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  843. {
  844. ulong addr, i;
  845. int nbytes, size;
  846. extern char console_buffer[];
  847. if (argc != 2) {
  848. printf ("Usage:\n%s\n", cmdtp->usage);
  849. return 1;
  850. }
  851. #ifdef CONFIG_BOOT_RETRY_TIME
  852. reset_cmd_timeout(); /* got a good command to get here */
  853. #endif
  854. /* We use the last specified parameters, unless new ones are
  855. * entered.
  856. */
  857. addr = mm_last_addr;
  858. size = mm_last_size;
  859. if ((flag & CMD_FLAG_REPEAT) == 0) {
  860. /* New command specified. Check for a size specification.
  861. * Defaults to long if no or incorrect specification.
  862. */
  863. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  864. return 1;
  865. /* Address is specified since argc > 1
  866. */
  867. addr = simple_strtoul(argv[1], NULL, 16);
  868. addr += base_address;
  869. }
  870. #ifdef CONFIG_HAS_DATAFLASH
  871. if (addr_dataflash(addr)){
  872. puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
  873. return 0;
  874. }
  875. #endif
  876. /* Print the address, followed by value. Then accept input for
  877. * the next value. A non-converted value exits.
  878. */
  879. do {
  880. printf("%08lx:", addr);
  881. if (size == 4)
  882. printf(" %08x", *((uint *)addr));
  883. else if (size == 2)
  884. printf(" %04x", *((ushort *)addr));
  885. else
  886. printf(" %02x", *((u_char *)addr));
  887. nbytes = readline (" ? ");
  888. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  889. /* <CR> pressed as only input, don't modify current
  890. * location and move to next. "-" pressed will go back.
  891. */
  892. if (incrflag)
  893. addr += nbytes ? -size : size;
  894. nbytes = 1;
  895. #ifdef CONFIG_BOOT_RETRY_TIME
  896. reset_cmd_timeout(); /* good enough to not time out */
  897. #endif
  898. }
  899. #ifdef CONFIG_BOOT_RETRY_TIME
  900. else if (nbytes == -2) {
  901. break; /* timed out, exit the command */
  902. }
  903. #endif
  904. else {
  905. char *endp;
  906. i = simple_strtoul(console_buffer, &endp, 16);
  907. nbytes = endp - console_buffer;
  908. if (nbytes) {
  909. #ifdef CONFIG_BOOT_RETRY_TIME
  910. /* good enough to not time out
  911. */
  912. reset_cmd_timeout();
  913. #endif
  914. if (size == 4)
  915. *((uint *)addr) = i;
  916. else if (size == 2)
  917. *((ushort *)addr) = i;
  918. else
  919. *((u_char *)addr) = i;
  920. if (incrflag)
  921. addr += size;
  922. }
  923. }
  924. } while (nbytes);
  925. mm_last_addr = addr;
  926. mm_last_size = size;
  927. return 0;
  928. }
  929. #ifndef CONFIG_CRC32_VERIFY
  930. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  931. {
  932. ulong addr, length;
  933. ulong crc;
  934. ulong *ptr;
  935. if (argc < 3) {
  936. printf ("Usage:\n%s\n", cmdtp->usage);
  937. return 1;
  938. }
  939. addr = simple_strtoul (argv[1], NULL, 16);
  940. addr += base_address;
  941. length = simple_strtoul (argv[2], NULL, 16);
  942. crc = crc32 (0, (const uchar *) addr, length);
  943. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  944. addr, addr + length - 1, crc);
  945. if (argc > 3) {
  946. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  947. *ptr = crc;
  948. }
  949. return 0;
  950. }
  951. #else /* CONFIG_CRC32_VERIFY */
  952. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  953. {
  954. ulong addr, length;
  955. ulong crc;
  956. ulong *ptr;
  957. ulong vcrc;
  958. int verify;
  959. int ac;
  960. char **av;
  961. if (argc < 3) {
  962. usage:
  963. printf ("Usage:\n%s\n", cmdtp->usage);
  964. return 1;
  965. }
  966. av = argv + 1;
  967. ac = argc - 1;
  968. if (strcmp(*av, "-v") == 0) {
  969. verify = 1;
  970. av++;
  971. ac--;
  972. if (ac < 3)
  973. goto usage;
  974. } else
  975. verify = 0;
  976. addr = simple_strtoul(*av++, NULL, 16);
  977. addr += base_address;
  978. length = simple_strtoul(*av++, NULL, 16);
  979. crc = crc32(0, (const uchar *) addr, length);
  980. if (!verify) {
  981. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  982. addr, addr + length - 1, crc);
  983. if (ac > 2) {
  984. ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
  985. *ptr = crc;
  986. }
  987. } else {
  988. vcrc = simple_strtoul(*av++, NULL, 16);
  989. if (vcrc != crc) {
  990. printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
  991. addr, addr + length - 1, crc, vcrc);
  992. return 1;
  993. }
  994. }
  995. return 0;
  996. }
  997. #endif /* CONFIG_CRC32_VERIFY */
  998. /**************************************************/
  999. #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
  1000. U_BOOT_CMD(
  1001. md, 3, 1, do_mem_md,
  1002. "md - memory display\n",
  1003. "[.b, .w, .l] address [# of objects]\n - memory display\n"
  1004. );
  1005. U_BOOT_CMD(
  1006. mm, 2, 1, do_mem_mm,
  1007. "mm - memory modify (auto-incrementing)\n",
  1008. "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
  1009. );
  1010. U_BOOT_CMD(
  1011. nm, 2, 1, do_mem_nm,
  1012. "nm - memory modify (constant address)\n",
  1013. "[.b, .w, .l] address\n - memory modify, read and keep address\n"
  1014. );
  1015. U_BOOT_CMD(
  1016. mw, 4, 1, do_mem_mw,
  1017. "mw - memory write (fill)\n",
  1018. "[.b, .w, .l] address value [count]\n - write memory\n"
  1019. );
  1020. U_BOOT_CMD(
  1021. cp, 4, 1, do_mem_cp,
  1022. "cp - memory copy\n",
  1023. "[.b, .w, .l] source target count\n - copy memory\n"
  1024. );
  1025. U_BOOT_CMD(
  1026. cmp, 4, 1, do_mem_cmp,
  1027. "cmp - memory compare\n",
  1028. "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
  1029. );
  1030. #ifndef CONFIG_CRC32_VERIFY
  1031. U_BOOT_CMD(
  1032. crc32, 4, 1, do_mem_crc,
  1033. "crc32 - checksum calculation\n",
  1034. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1035. );
  1036. #else /* CONFIG_CRC32_VERIFY */
  1037. U_BOOT_CMD(
  1038. crc32, 5, 1, do_mem_crc,
  1039. "crc32 - checksum calculation\n",
  1040. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1041. "-v address count crc\n - verify crc of memory area\n"
  1042. );
  1043. #endif /* CONFIG_CRC32_VERIFY */
  1044. U_BOOT_CMD(
  1045. base, 2, 1, do_mem_base,
  1046. "base - print or set address offset\n",
  1047. "\n - print address offset for memory commands\n"
  1048. "base off\n - set address offset for memory commands to 'off'\n"
  1049. );
  1050. U_BOOT_CMD(
  1051. loop, 3, 1, do_mem_loop,
  1052. "loop - infinite loop on address range\n",
  1053. "[.b, .w, .l] address number_of_objects\n"
  1054. " - loop on a set of addresses\n"
  1055. );
  1056. #ifdef CONFIG_LOOPW
  1057. U_BOOT_CMD(
  1058. loopw, 4, 1, do_mem_loopw,
  1059. "loopw - infinite write loop on address range\n",
  1060. "[.b, .w, .l] address number_of_objects data_to_write\n"
  1061. " - loop on a set of addresses\n"
  1062. );
  1063. #endif /* CONFIG_LOOPW */
  1064. U_BOOT_CMD(
  1065. mtest, 4, 1, do_mem_mtest,
  1066. "mtest - simple RAM test\n",
  1067. "[start [end [pattern]]]\n"
  1068. " - simple RAM read/write test\n"
  1069. );
  1070. #endif
  1071. #endif /* CFG_CMD_MEMORY */