cmd_mem.c 24 KB

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