cmd_mem.c 24 KB

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