cmd_mem.c 24 KB

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