cmd_mem.c 24 KB

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