cmd_mem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. #include <cmd_mem.h>
  31. #if (CONFIG_COMMANDS & CFG_CMD_MMC)
  32. #include <mmc.h>
  33. #endif
  34. #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | CFG_CMD_PCI | CFG_CMD_I2C\
  35. | CMD_CMD_PORTIO))
  36. int cmd_get_data_size(char* arg, int default_size)
  37. {
  38. /* Check for a size specification .b, .w or .l.
  39. */
  40. int len = strlen(arg);
  41. if (len > 2 && arg[len-2] == '.') {
  42. switch(arg[len-1]) {
  43. case 'b':
  44. return 1;
  45. case 'w':
  46. return 2;
  47. case 'l':
  48. return 4;
  49. }
  50. }
  51. return default_size;
  52. }
  53. #endif
  54. #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
  55. #ifdef CMD_MEM_DEBUG
  56. #define PRINTF(fmt,args...) printf (fmt ,##args)
  57. #else
  58. #define PRINTF(fmt,args...)
  59. #endif
  60. static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
  61. /* Display values from last command.
  62. * Memory modify remembered values are different from display memory.
  63. */
  64. uint dp_last_addr, dp_last_size;
  65. uint dp_last_length = 0x40;
  66. uint mm_last_addr, mm_last_size;
  67. static ulong base_address = 0;
  68. /* Memory Display
  69. *
  70. * Syntax:
  71. * md{.b, .w, .l} {addr} {len}
  72. */
  73. #define DISP_LINE_LEN 16
  74. int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  75. {
  76. ulong addr, size, length;
  77. ulong i, nbytes, linebytes;
  78. u_char *cp;
  79. int rc = 0;
  80. /* We use the last specified parameters, unless new ones are
  81. * entered.
  82. */
  83. addr = dp_last_addr;
  84. size = dp_last_size;
  85. length = dp_last_length;
  86. if (argc < 2) {
  87. printf ("Usage:\n%s\n", cmdtp->usage);
  88. return 1;
  89. }
  90. if ((flag & CMD_FLAG_REPEAT) == 0) {
  91. /* New command specified. Check for a size specification.
  92. * Defaults to long if no or incorrect specification.
  93. */
  94. size = cmd_get_data_size(argv[0], 4);
  95. /* Address is specified since argc > 1
  96. */
  97. addr = simple_strtoul(argv[1], NULL, 16);
  98. addr += base_address;
  99. /* If another parameter, it is the length to display.
  100. * Length is the number of objects, not number of bytes.
  101. */
  102. if (argc > 2)
  103. length = simple_strtoul(argv[2], NULL, 16);
  104. }
  105. /* Print the lines.
  106. *
  107. * We buffer all read data, so we can make sure data is read only
  108. * once, and all accesses are with the specified bus width.
  109. */
  110. nbytes = length * size;
  111. do {
  112. char linebuf[DISP_LINE_LEN];
  113. uint *uip = (uint *)linebuf;
  114. ushort *usp = (ushort *)linebuf;
  115. u_char *ucp = (u_char *)linebuf;
  116. printf("%08lx:", addr);
  117. linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
  118. for (i=0; i<linebytes; i+= size) {
  119. if (size == 4) {
  120. printf(" %08x", (*uip++ = *((uint *)addr)));
  121. } else if (size == 2) {
  122. printf(" %04x", (*usp++ = *((ushort *)addr)));
  123. } else {
  124. printf(" %02x", (*ucp++ = *((u_char *)addr)));
  125. }
  126. addr += size;
  127. }
  128. printf(" ");
  129. cp = linebuf;
  130. for (i=0; i<linebytes; i++) {
  131. if ((*cp < 0x20) || (*cp > 0x7e))
  132. printf(".");
  133. else
  134. printf("%c", *cp);
  135. cp++;
  136. }
  137. printf("\n");
  138. nbytes -= linebytes;
  139. if (ctrlc()) {
  140. rc = 1;
  141. break;
  142. }
  143. } while (nbytes > 0);
  144. dp_last_addr = addr;
  145. dp_last_length = length;
  146. dp_last_size = size;
  147. return (rc);
  148. }
  149. int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  150. {
  151. return mod_mem (cmdtp, 1, flag, argc, argv);
  152. }
  153. int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  154. {
  155. return mod_mem (cmdtp, 0, flag, argc, argv);
  156. }
  157. int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  158. {
  159. ulong addr, size, writeval, count;
  160. if ((argc < 3) || (argc > 4)) {
  161. printf ("Usage:\n%s\n", cmdtp->usage);
  162. return 1;
  163. }
  164. /* Check for size specification.
  165. */
  166. size = cmd_get_data_size(argv[0], 4);
  167. /* Address is specified since argc > 1
  168. */
  169. addr = simple_strtoul(argv[1], NULL, 16);
  170. addr += base_address;
  171. /* Get the value to write.
  172. */
  173. writeval = simple_strtoul(argv[2], NULL, 16);
  174. /* Count ? */
  175. if (argc == 4) {
  176. count = simple_strtoul(argv[3], NULL, 16);
  177. } else {
  178. count = 1;
  179. }
  180. while (count-- > 0) {
  181. if (size == 4)
  182. *((ulong *)addr) = (ulong )writeval;
  183. else if (size == 2)
  184. *((ushort *)addr) = (ushort)writeval;
  185. else
  186. *((u_char *)addr) = (u_char)writeval;
  187. addr += size;
  188. }
  189. return 0;
  190. }
  191. int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  192. {
  193. ulong size, addr1, addr2, count, ngood;
  194. int rcode = 0;
  195. if (argc != 4) {
  196. printf ("Usage:\n%s\n", cmdtp->usage);
  197. return 1;
  198. }
  199. /* Check for size specification.
  200. */
  201. size = cmd_get_data_size(argv[0], 4);
  202. addr1 = simple_strtoul(argv[1], NULL, 16);
  203. addr1 += base_address;
  204. addr2 = simple_strtoul(argv[2], NULL, 16);
  205. addr2 += base_address;
  206. count = simple_strtoul(argv[3], NULL, 16);
  207. ngood = 0;
  208. while (count-- > 0) {
  209. if (size == 4) {
  210. ulong word1 = *(ulong *)addr1;
  211. ulong word2 = *(ulong *)addr2;
  212. if (word1 != word2) {
  213. printf("word at 0x%08lx (0x%08lx) "
  214. "!= word at 0x%08lx (0x%08lx)\n",
  215. addr1, word1, addr2, word2);
  216. rcode = 1;
  217. break;
  218. }
  219. }
  220. else if (size == 2) {
  221. ushort hword1 = *(ushort *)addr1;
  222. ushort hword2 = *(ushort *)addr2;
  223. if (hword1 != hword2) {
  224. printf("halfword at 0x%08lx (0x%04x) "
  225. "!= halfword at 0x%08lx (0x%04x)\n",
  226. addr1, hword1, addr2, hword2);
  227. rcode = 1;
  228. break;
  229. }
  230. }
  231. else {
  232. u_char byte1 = *(u_char *)addr1;
  233. u_char byte2 = *(u_char *)addr2;
  234. if (byte1 != byte2) {
  235. printf("byte at 0x%08lx (0x%02x) "
  236. "!= byte at 0x%08lx (0x%02x)\n",
  237. addr1, byte1, addr2, byte2);
  238. rcode = 1;
  239. break;
  240. }
  241. }
  242. ngood++;
  243. addr1 += size;
  244. addr2 += size;
  245. }
  246. printf("Total of %ld %s%s were the same\n",
  247. ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
  248. ngood == 1 ? "" : "s");
  249. return rcode;
  250. }
  251. int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  252. {
  253. ulong addr, size, dest, count;
  254. if (argc != 4) {
  255. printf ("Usage:\n%s\n", cmdtp->usage);
  256. return 1;
  257. }
  258. /* Check for size specification.
  259. */
  260. size = cmd_get_data_size(argv[0], 4);
  261. addr = simple_strtoul(argv[1], NULL, 16);
  262. addr += base_address;
  263. dest = simple_strtoul(argv[2], NULL, 16);
  264. dest += base_address;
  265. count = simple_strtoul(argv[3], NULL, 16);
  266. if (count == 0) {
  267. puts ("Zero length ???\n");
  268. return 1;
  269. }
  270. #ifndef CFG_NO_FLASH
  271. /* check if we are copying to Flash */
  272. if (addr2info(dest) != NULL) {
  273. int rc;
  274. printf ("Copy to Flash... ");
  275. rc = flash_write ((uchar *)addr, dest, count*size);
  276. if (rc != 0) {
  277. flash_perror (rc);
  278. return (1);
  279. }
  280. puts ("done\n");
  281. return 0;
  282. }
  283. #endif
  284. #if (CONFIG_COMMANDS & CFG_CMD_MMC)
  285. if (mmc2info(dest)) {
  286. int rc;
  287. printf ("Copy to MMC... ");
  288. switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
  289. case 0:
  290. printf ("\n");
  291. return 1;
  292. case -1:
  293. printf("failed\n");
  294. return 1;
  295. default:
  296. printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
  297. return 1;
  298. }
  299. puts ("done\n");
  300. return 0;
  301. }
  302. if (mmc2info(addr)) {
  303. int rc;
  304. printf ("Copy from MMC... ");
  305. switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
  306. case 0:
  307. printf ("\n");
  308. return 1;
  309. case -1:
  310. printf("failed\n");
  311. return 1;
  312. default:
  313. printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
  314. return 1;
  315. }
  316. puts ("done\n");
  317. return 0;
  318. }
  319. #endif
  320. while (count-- > 0) {
  321. if (size == 4)
  322. *((ulong *)dest) = *((ulong *)addr);
  323. else if (size == 2)
  324. *((ushort *)dest) = *((ushort *)addr);
  325. else
  326. *((u_char *)dest) = *((u_char *)addr);
  327. addr += size;
  328. dest += size;
  329. }
  330. return 0;
  331. }
  332. int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  333. {
  334. if (argc > 1) {
  335. /* Set new base address.
  336. */
  337. base_address = simple_strtoul(argv[1], NULL, 16);
  338. }
  339. /* Print the current base address.
  340. */
  341. printf("Base Address: 0x%08lx\n", base_address);
  342. return 0;
  343. }
  344. int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  345. {
  346. ulong addr, size, length, i, junk;
  347. volatile uint *longp;
  348. volatile ushort *shortp;
  349. volatile u_char *cp;
  350. if (argc < 3) {
  351. printf ("Usage:\n%s\n", cmdtp->usage);
  352. return 1;
  353. }
  354. /* Check for a size spefication.
  355. * Defaults to long if no or incorrect specification.
  356. */
  357. size = cmd_get_data_size(argv[0], 4);
  358. /* Address is always specified.
  359. */
  360. addr = simple_strtoul(argv[1], NULL, 16);
  361. /* Length is the number of objects, not number of bytes.
  362. */
  363. length = simple_strtoul(argv[2], NULL, 16);
  364. /* We want to optimize the loops to run as fast as possible.
  365. * If we have only one object, just run infinite loops.
  366. */
  367. if (length == 1) {
  368. if (size == 4) {
  369. longp = (uint *)addr;
  370. for (;;)
  371. i = *longp;
  372. }
  373. if (size == 2) {
  374. shortp = (ushort *)addr;
  375. for (;;)
  376. i = *shortp;
  377. }
  378. cp = (u_char *)addr;
  379. for (;;)
  380. i = *cp;
  381. }
  382. if (size == 4) {
  383. for (;;) {
  384. longp = (uint *)addr;
  385. i = length;
  386. while (i-- > 0)
  387. junk = *longp++;
  388. }
  389. }
  390. if (size == 2) {
  391. for (;;) {
  392. shortp = (ushort *)addr;
  393. i = length;
  394. while (i-- > 0)
  395. junk = *shortp++;
  396. }
  397. }
  398. for (;;) {
  399. cp = (u_char *)addr;
  400. i = length;
  401. while (i-- > 0)
  402. junk = *cp++;
  403. }
  404. }
  405. /*
  406. * Perform a memory test. A more complete alternative test can be
  407. * configured using CFG_ALT_MEMTEST. The complete test loops until
  408. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  409. */
  410. int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  411. {
  412. vu_long *addr, *start, *end;
  413. ulong val;
  414. ulong readback;
  415. #if defined(CFG_ALT_MEMTEST)
  416. vu_long addr_mask;
  417. vu_long offset;
  418. vu_long test_offset;
  419. vu_long pattern;
  420. vu_long temp;
  421. vu_long anti_pattern;
  422. vu_long num_words;
  423. vu_long *dummy = NULL;
  424. int j;
  425. int iterations = 1;
  426. static const ulong bitpattern[] = {
  427. 0x00000001, /* single bit */
  428. 0x00000003, /* two adjacent bits */
  429. 0x00000007, /* three adjacent bits */
  430. 0x0000000F, /* four adjacent bits */
  431. 0x00000005, /* two non-adjacent bits */
  432. 0x00000015, /* three non-adjacent bits */
  433. 0x00000055, /* four non-adjacent bits */
  434. 0xaaaaaaaa, /* alternating 1/0 */
  435. };
  436. #else
  437. ulong incr;
  438. ulong pattern;
  439. int rcode = 0;
  440. #endif
  441. if (argc > 1) {
  442. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  443. } else {
  444. start = (ulong *)CFG_MEMTEST_START;
  445. }
  446. if (argc > 2) {
  447. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  448. } else {
  449. end = (ulong *)(CFG_MEMTEST_END);
  450. }
  451. if (argc > 3) {
  452. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  453. } else {
  454. pattern = 0;
  455. }
  456. #if defined(CFG_ALT_MEMTEST)
  457. printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
  458. PRINTF("%s:%d: start 0x%p end 0x%p\n",
  459. __FUNCTION__, __LINE__, start, end);
  460. for (;;) {
  461. if (ctrlc()) {
  462. putc ('\n');
  463. return 1;
  464. }
  465. printf("Iteration: %6d\r", iterations);
  466. PRINTF("Iteration: %6d\n", iterations);
  467. iterations++;
  468. /*
  469. * Data line test: write a pattern to the first
  470. * location, write the 1's complement to a 'parking'
  471. * address (changes the state of the data bus so a
  472. * floating bus doen't give a false OK), and then
  473. * read the value back. Note that we read it back
  474. * into a variable because the next time we read it,
  475. * it might be right (been there, tough to explain to
  476. * the quality guys why it prints a failure when the
  477. * "is" and "should be" are obviously the same in the
  478. * error message).
  479. *
  480. * Rather than exhaustively testing, we test some
  481. * patterns by shifting '1' bits through a field of
  482. * '0's and '0' bits through a field of '1's (i.e.
  483. * pattern and ~pattern).
  484. */
  485. addr = start;
  486. for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
  487. val = bitpattern[j];
  488. for(; val != 0; val <<= 1) {
  489. *addr = val;
  490. *dummy = ~val; /* clear the test data off of the bus */
  491. readback = *addr;
  492. if(readback != val) {
  493. printf ("FAILURE (data line): "
  494. "expected %08lx, actual %08lx\n",
  495. val, readback);
  496. }
  497. *addr = ~val;
  498. *dummy = val;
  499. readback = *addr;
  500. if(readback != ~val) {
  501. printf ("FAILURE (data line): "
  502. "Is %08lx, should be %08lx\n",
  503. val, readback);
  504. }
  505. }
  506. }
  507. /*
  508. * Based on code whose Original Author and Copyright
  509. * information follows: Copyright (c) 1998 by Michael
  510. * Barr. This software is placed into the public
  511. * domain and may be used for any purpose. However,
  512. * this notice must not be changed or removed and no
  513. * warranty is either expressed or implied by its
  514. * publication or distribution.
  515. */
  516. /*
  517. * Address line test
  518. *
  519. * Description: Test the address bus wiring in a
  520. * memory region by performing a walking
  521. * 1's test on the relevant bits of the
  522. * address and checking for aliasing.
  523. * This test will find single-bit
  524. * address failures such as stuck -high,
  525. * stuck-low, and shorted pins. The base
  526. * address and size of the region are
  527. * selected by the caller.
  528. *
  529. * Notes: For best results, the selected base
  530. * address should have enough LSB 0's to
  531. * guarantee single address bit changes.
  532. * For example, to test a 64-Kbyte
  533. * region, select a base address on a
  534. * 64-Kbyte boundary. Also, select the
  535. * region size as a power-of-two if at
  536. * all possible.
  537. *
  538. * Returns: 0 if the test succeeds, 1 if the test fails.
  539. *
  540. * ## NOTE ## Be sure to specify start and end
  541. * addresses such that addr_mask has
  542. * lots of bits set. For example an
  543. * address range of 01000000 02000000 is
  544. * bad while a range of 01000000
  545. * 01ffffff is perfect.
  546. */
  547. addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
  548. pattern = (vu_long) 0xaaaaaaaa;
  549. anti_pattern = (vu_long) 0x55555555;
  550. PRINTF("%s:%d: addr mask = 0x%.8lx\n",
  551. __FUNCTION__, __LINE__,
  552. addr_mask);
  553. /*
  554. * Write the default pattern at each of the
  555. * power-of-two offsets.
  556. */
  557. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  558. start[offset] = pattern;
  559. }
  560. /*
  561. * Check for address bits stuck high.
  562. */
  563. test_offset = 0;
  564. start[test_offset] = anti_pattern;
  565. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  566. temp = start[offset];
  567. if (temp != pattern) {
  568. printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
  569. " expected 0x%.8lx, actual 0x%.8lx\n",
  570. (ulong)&start[offset], pattern, temp);
  571. return 1;
  572. }
  573. }
  574. start[test_offset] = pattern;
  575. /*
  576. * Check for addr bits stuck low or shorted.
  577. */
  578. for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
  579. start[test_offset] = anti_pattern;
  580. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  581. temp = start[offset];
  582. if ((temp != pattern) && (offset != test_offset)) {
  583. printf ("\nFAILURE: Address bit stuck low or shorted @"
  584. " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
  585. (ulong)&start[offset], pattern, temp);
  586. return 1;
  587. }
  588. }
  589. start[test_offset] = pattern;
  590. }
  591. /*
  592. * Description: Test the integrity of a physical
  593. * memory device by performing an
  594. * increment/decrement test over the
  595. * entire region. In the process every
  596. * storage bit in the device is tested
  597. * as a zero and a one. The base address
  598. * and the size of the region are
  599. * selected by the caller.
  600. *
  601. * Returns: 0 if the test succeeds, 1 if the test fails.
  602. */
  603. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  604. /*
  605. * Fill memory with a known pattern.
  606. */
  607. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  608. start[offset] = pattern;
  609. }
  610. /*
  611. * Check each location and invert it for the second pass.
  612. */
  613. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  614. temp = start[offset];
  615. if (temp != pattern) {
  616. printf ("\nFAILURE (read/write) @ 0x%.8lx:"
  617. " expected 0x%.8lx, actual 0x%.8lx)\n",
  618. (ulong)&start[offset], pattern, temp);
  619. return 1;
  620. }
  621. anti_pattern = ~pattern;
  622. start[offset] = anti_pattern;
  623. }
  624. /*
  625. * Check each location for the inverted pattern and zero it.
  626. */
  627. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  628. anti_pattern = ~pattern;
  629. temp = start[offset];
  630. if (temp != anti_pattern) {
  631. printf ("\nFAILURE (read/write): @ 0x%.8lx:"
  632. " expected 0x%.8lx, actual 0x%.8lx)\n",
  633. (ulong)&start[offset], anti_pattern, temp);
  634. return 1;
  635. }
  636. start[offset] = 0;
  637. }
  638. }
  639. #else /* The original, quickie test */
  640. incr = 1;
  641. for (;;) {
  642. if (ctrlc()) {
  643. putc ('\n');
  644. return 1;
  645. }
  646. printf ("\rPattern %08lX Writing..."
  647. "%12s"
  648. "\b\b\b\b\b\b\b\b\b\b",
  649. pattern, "");
  650. for (addr=start,val=pattern; addr<end; addr++) {
  651. *addr = val;
  652. val += incr;
  653. }
  654. printf("Reading...");
  655. for (addr=start,val=pattern; addr<end; addr++) {
  656. readback = *addr;
  657. if (readback != val) {
  658. printf ("\nMem error @ 0x%08X: "
  659. "found %08lX, expected %08lX\n",
  660. (uint)addr, readback, val);
  661. rcode = 1;
  662. }
  663. val += incr;
  664. }
  665. /*
  666. * Flip the pattern each time to make lots of zeros and
  667. * then, the next time, lots of ones. We decrement
  668. * the "negative" patterns and increment the "positive"
  669. * patterns to preserve this feature.
  670. */
  671. if(pattern & 0x80000000) {
  672. pattern = -pattern; /* complement & increment */
  673. }
  674. else {
  675. pattern = ~pattern;
  676. }
  677. incr = -incr;
  678. }
  679. return rcode;
  680. #endif
  681. }
  682. /* Modify memory.
  683. *
  684. * Syntax:
  685. * mm{.b, .w, .l} {addr}
  686. * nm{.b, .w, .l} {addr}
  687. */
  688. static int
  689. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  690. {
  691. ulong addr, size, i;
  692. int nbytes;
  693. extern char console_buffer[];
  694. if (argc != 2) {
  695. printf ("Usage:\n%s\n", cmdtp->usage);
  696. return 1;
  697. }
  698. #ifdef CONFIG_BOOT_RETRY_TIME
  699. reset_cmd_timeout(); /* got a good command to get here */
  700. #endif
  701. /* We use the last specified parameters, unless new ones are
  702. * entered.
  703. */
  704. addr = mm_last_addr;
  705. size = mm_last_size;
  706. if ((flag & CMD_FLAG_REPEAT) == 0) {
  707. /* New command specified. Check for a size specification.
  708. * Defaults to long if no or incorrect specification.
  709. */
  710. size = cmd_get_data_size(argv[0], 4);
  711. /* Address is specified since argc > 1
  712. */
  713. addr = simple_strtoul(argv[1], NULL, 16);
  714. addr += base_address;
  715. }
  716. /* Print the address, followed by value. Then accept input for
  717. * the next value. A non-converted value exits.
  718. */
  719. do {
  720. printf("%08lx:", addr);
  721. if (size == 4)
  722. printf(" %08x", *((uint *)addr));
  723. else if (size == 2)
  724. printf(" %04x", *((ushort *)addr));
  725. else
  726. printf(" %02x", *((u_char *)addr));
  727. nbytes = readline (" ? ");
  728. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  729. /* <CR> pressed as only input, don't modify current
  730. * location and move to next. "-" pressed will go back.
  731. */
  732. if (incrflag)
  733. addr += nbytes ? -size : size;
  734. nbytes = 1;
  735. #ifdef CONFIG_BOOT_RETRY_TIME
  736. reset_cmd_timeout(); /* good enough to not time out */
  737. #endif
  738. }
  739. #ifdef CONFIG_BOOT_RETRY_TIME
  740. else if (nbytes == -2) {
  741. break; /* timed out, exit the command */
  742. }
  743. #endif
  744. else {
  745. char *endp;
  746. i = simple_strtoul(console_buffer, &endp, 16);
  747. nbytes = endp - console_buffer;
  748. if (nbytes) {
  749. #ifdef CONFIG_BOOT_RETRY_TIME
  750. /* good enough to not time out
  751. */
  752. reset_cmd_timeout();
  753. #endif
  754. if (size == 4)
  755. *((uint *)addr) = i;
  756. else if (size == 2)
  757. *((ushort *)addr) = i;
  758. else
  759. *((u_char *)addr) = i;
  760. if (incrflag)
  761. addr += size;
  762. }
  763. }
  764. } while (nbytes);
  765. mm_last_addr = addr;
  766. mm_last_size = size;
  767. return 0;
  768. }
  769. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  770. {
  771. ulong addr, length;
  772. ulong crc;
  773. ulong *ptr;
  774. if (argc < 3) {
  775. printf ("Usage:\n%s\n", cmdtp->usage);
  776. return 1;
  777. }
  778. addr = simple_strtoul (argv[1], NULL, 16);
  779. addr += base_address;
  780. length = simple_strtoul (argv[2], NULL, 16);
  781. crc = crc32 (0, (const uchar *) addr, length);
  782. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  783. addr, addr + length - 1, crc);
  784. if (argc > 3) {
  785. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  786. *ptr = crc;
  787. }
  788. return 0;
  789. }
  790. #endif /* CFG_CMD_MEMORY */