cmd_mem.c 25 KB

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