cmd_mem.c 24 KB

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