cmd_mem.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  863. {
  864. ulong addr, length;
  865. ulong crc;
  866. ulong *ptr;
  867. if (argc < 3) {
  868. printf ("Usage:\n%s\n", cmdtp->usage);
  869. return 1;
  870. }
  871. addr = simple_strtoul (argv[1], NULL, 16);
  872. addr += base_address;
  873. length = simple_strtoul (argv[2], NULL, 16);
  874. crc = crc32 (0, (const uchar *) addr, length);
  875. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  876. addr, addr + length - 1, crc);
  877. if (argc > 3) {
  878. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  879. *ptr = crc;
  880. }
  881. return 0;
  882. }
  883. /**************************************************/
  884. #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
  885. U_BOOT_CMD(
  886. md, 3, 1, do_mem_md,
  887. "md - memory display\n",
  888. "[.b, .w, .l] address [# of objects]\n - memory display\n"
  889. );
  890. U_BOOT_CMD(
  891. mm, 2, 1, do_mem_mm,
  892. "mm - memory modify (auto-incrementing)\n",
  893. "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
  894. );
  895. U_BOOT_CMD(
  896. nm, 2, 1, do_mem_nm,
  897. "nm - memory modify (constant address)\n",
  898. "[.b, .w, .l] address\n - memory modify, read and keep address\n"
  899. );
  900. U_BOOT_CMD(
  901. mw, 4, 1, do_mem_mw,
  902. "mw - memory write (fill)\n",
  903. "[.b, .w, .l] address value [count]\n - write memory\n"
  904. );
  905. U_BOOT_CMD(
  906. cp, 4, 1, do_mem_cp,
  907. "cp - memory copy\n",
  908. "[.b, .w, .l] source target count\n - copy memory\n"
  909. );
  910. U_BOOT_CMD(
  911. cmp, 4, 1, do_mem_cmp,
  912. "cmp - memory compare\n",
  913. "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
  914. );
  915. U_BOOT_CMD(
  916. crc32, 4, 1, do_mem_crc,
  917. "crc32 - checksum calculation\n",
  918. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  919. );
  920. U_BOOT_CMD(
  921. base, 2, 1, do_mem_base,
  922. "base - print or set address offset\n",
  923. "\n - print address offset for memory commands\n"
  924. "base off\n - set address offset for memory commands to 'off'\n"
  925. );
  926. U_BOOT_CMD(
  927. loop, 3, 1, do_mem_loop,
  928. "loop - infinite loop on address range\n",
  929. "[.b, .w, .l] address number_of_objects\n"
  930. " - loop on a set of addresses\n"
  931. );
  932. U_BOOT_CMD(
  933. mtest, 4, 1, do_mem_mtest,
  934. "mtest - simple RAM test\n",
  935. "[start [end [pattern]]]\n"
  936. " - simple RAM read/write test\n"
  937. );
  938. #endif
  939. #endif /* CFG_CMD_MEMORY */