cmd_mem.c 20 KB

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