cmd_mem.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. #if defined(CFG_MEMTEST_SCRATCH)
  490. vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
  491. #else
  492. vu_long *dummy = NULL;
  493. #endif
  494. int j;
  495. int iterations = 1;
  496. static const ulong bitpattern[] = {
  497. 0x00000001, /* single bit */
  498. 0x00000003, /* two adjacent bits */
  499. 0x00000007, /* three adjacent bits */
  500. 0x0000000F, /* four adjacent bits */
  501. 0x00000005, /* two non-adjacent bits */
  502. 0x00000015, /* three non-adjacent bits */
  503. 0x00000055, /* four non-adjacent bits */
  504. 0xaaaaaaaa, /* alternating 1/0 */
  505. };
  506. #else
  507. ulong incr;
  508. ulong pattern;
  509. int rcode = 0;
  510. #endif
  511. if (argc > 1) {
  512. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  513. } else {
  514. start = (ulong *)CFG_MEMTEST_START;
  515. }
  516. if (argc > 2) {
  517. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  518. } else {
  519. end = (ulong *)(CFG_MEMTEST_END);
  520. }
  521. if (argc > 3) {
  522. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  523. } else {
  524. pattern = 0;
  525. }
  526. #if defined(CFG_ALT_MEMTEST)
  527. printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
  528. PRINTF("%s:%d: start 0x%p end 0x%p\n",
  529. __FUNCTION__, __LINE__, start, end);
  530. for (;;) {
  531. if (ctrlc()) {
  532. putc ('\n');
  533. return 1;
  534. }
  535. printf("Iteration: %6d\r", iterations);
  536. PRINTF("Iteration: %6d\n", iterations);
  537. iterations++;
  538. /*
  539. * Data line test: write a pattern to the first
  540. * location, write the 1's complement to a 'parking'
  541. * address (changes the state of the data bus so a
  542. * floating bus doen't give a false OK), and then
  543. * read the value back. Note that we read it back
  544. * into a variable because the next time we read it,
  545. * it might be right (been there, tough to explain to
  546. * the quality guys why it prints a failure when the
  547. * "is" and "should be" are obviously the same in the
  548. * error message).
  549. *
  550. * Rather than exhaustively testing, we test some
  551. * patterns by shifting '1' bits through a field of
  552. * '0's and '0' bits through a field of '1's (i.e.
  553. * pattern and ~pattern).
  554. */
  555. addr = start;
  556. for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
  557. val = bitpattern[j];
  558. for(; val != 0; val <<= 1) {
  559. *addr = val;
  560. *dummy = ~val; /* clear the test data off of the bus */
  561. readback = *addr;
  562. if(readback != val) {
  563. printf ("FAILURE (data line): "
  564. "expected %08lx, actual %08lx\n",
  565. val, readback);
  566. }
  567. *addr = ~val;
  568. *dummy = val;
  569. readback = *addr;
  570. if(readback != ~val) {
  571. printf ("FAILURE (data line): "
  572. "Is %08lx, should be %08lx\n",
  573. val, readback);
  574. }
  575. }
  576. }
  577. /*
  578. * Based on code whose Original Author and Copyright
  579. * information follows: Copyright (c) 1998 by Michael
  580. * Barr. This software is placed into the public
  581. * domain and may be used for any purpose. However,
  582. * this notice must not be changed or removed and no
  583. * warranty is either expressed or implied by its
  584. * publication or distribution.
  585. */
  586. /*
  587. * Address line test
  588. *
  589. * Description: Test the address bus wiring in a
  590. * memory region by performing a walking
  591. * 1's test on the relevant bits of the
  592. * address and checking for aliasing.
  593. * This test will find single-bit
  594. * address failures such as stuck -high,
  595. * stuck-low, and shorted pins. The base
  596. * address and size of the region are
  597. * selected by the caller.
  598. *
  599. * Notes: For best results, the selected base
  600. * address should have enough LSB 0's to
  601. * guarantee single address bit changes.
  602. * For example, to test a 64-Kbyte
  603. * region, select a base address on a
  604. * 64-Kbyte boundary. Also, select the
  605. * region size as a power-of-two if at
  606. * all possible.
  607. *
  608. * Returns: 0 if the test succeeds, 1 if the test fails.
  609. *
  610. * ## NOTE ## Be sure to specify start and end
  611. * addresses such that addr_mask has
  612. * lots of bits set. For example an
  613. * address range of 01000000 02000000 is
  614. * bad while a range of 01000000
  615. * 01ffffff is perfect.
  616. */
  617. addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
  618. pattern = (vu_long) 0xaaaaaaaa;
  619. anti_pattern = (vu_long) 0x55555555;
  620. PRINTF("%s:%d: addr mask = 0x%.8lx\n",
  621. __FUNCTION__, __LINE__,
  622. addr_mask);
  623. /*
  624. * Write the default pattern at each of the
  625. * power-of-two offsets.
  626. */
  627. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  628. start[offset] = pattern;
  629. }
  630. /*
  631. * Check for address bits stuck high.
  632. */
  633. test_offset = 0;
  634. start[test_offset] = anti_pattern;
  635. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  636. temp = start[offset];
  637. if (temp != pattern) {
  638. printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
  639. " expected 0x%.8lx, actual 0x%.8lx\n",
  640. (ulong)&start[offset], pattern, temp);
  641. return 1;
  642. }
  643. }
  644. start[test_offset] = pattern;
  645. /*
  646. * Check for addr bits stuck low or shorted.
  647. */
  648. for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
  649. start[test_offset] = anti_pattern;
  650. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  651. temp = start[offset];
  652. if ((temp != pattern) && (offset != test_offset)) {
  653. printf ("\nFAILURE: Address bit stuck low or shorted @"
  654. " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
  655. (ulong)&start[offset], pattern, temp);
  656. return 1;
  657. }
  658. }
  659. start[test_offset] = pattern;
  660. }
  661. /*
  662. * Description: Test the integrity of a physical
  663. * memory device by performing an
  664. * increment/decrement test over the
  665. * entire region. In the process every
  666. * storage bit in the device is tested
  667. * as a zero and a one. The base address
  668. * and the size of the region are
  669. * selected by the caller.
  670. *
  671. * Returns: 0 if the test succeeds, 1 if the test fails.
  672. */
  673. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  674. /*
  675. * Fill memory with a known pattern.
  676. */
  677. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  678. start[offset] = pattern;
  679. }
  680. /*
  681. * Check each location and invert it for the second pass.
  682. */
  683. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  684. temp = start[offset];
  685. if (temp != pattern) {
  686. printf ("\nFAILURE (read/write) @ 0x%.8lx:"
  687. " expected 0x%.8lx, actual 0x%.8lx)\n",
  688. (ulong)&start[offset], pattern, temp);
  689. return 1;
  690. }
  691. anti_pattern = ~pattern;
  692. start[offset] = anti_pattern;
  693. }
  694. /*
  695. * Check each location for the inverted pattern and zero it.
  696. */
  697. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  698. anti_pattern = ~pattern;
  699. temp = start[offset];
  700. if (temp != anti_pattern) {
  701. printf ("\nFAILURE (read/write): @ 0x%.8lx:"
  702. " expected 0x%.8lx, actual 0x%.8lx)\n",
  703. (ulong)&start[offset], anti_pattern, temp);
  704. return 1;
  705. }
  706. start[offset] = 0;
  707. }
  708. }
  709. #else /* The original, quickie test */
  710. incr = 1;
  711. for (;;) {
  712. if (ctrlc()) {
  713. putc ('\n');
  714. return 1;
  715. }
  716. printf ("\rPattern %08lX Writing..."
  717. "%12s"
  718. "\b\b\b\b\b\b\b\b\b\b",
  719. pattern, "");
  720. for (addr=start,val=pattern; addr<end; addr++) {
  721. *addr = val;
  722. val += incr;
  723. }
  724. printf("Reading...");
  725. for (addr=start,val=pattern; addr<end; addr++) {
  726. readback = *addr;
  727. if (readback != val) {
  728. printf ("\nMem error @ 0x%08X: "
  729. "found %08lX, expected %08lX\n",
  730. (uint)addr, readback, val);
  731. rcode = 1;
  732. }
  733. val += incr;
  734. }
  735. /*
  736. * Flip the pattern each time to make lots of zeros and
  737. * then, the next time, lots of ones. We decrement
  738. * the "negative" patterns and increment the "positive"
  739. * patterns to preserve this feature.
  740. */
  741. if(pattern & 0x80000000) {
  742. pattern = -pattern; /* complement & increment */
  743. }
  744. else {
  745. pattern = ~pattern;
  746. }
  747. incr = -incr;
  748. }
  749. return rcode;
  750. #endif
  751. }
  752. /* Modify memory.
  753. *
  754. * Syntax:
  755. * mm{.b, .w, .l} {addr}
  756. * nm{.b, .w, .l} {addr}
  757. */
  758. static int
  759. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  760. {
  761. ulong addr, i;
  762. int nbytes, size;
  763. extern char console_buffer[];
  764. if (argc != 2) {
  765. printf ("Usage:\n%s\n", cmdtp->usage);
  766. return 1;
  767. }
  768. #ifdef CONFIG_BOOT_RETRY_TIME
  769. reset_cmd_timeout(); /* got a good command to get here */
  770. #endif
  771. /* We use the last specified parameters, unless new ones are
  772. * entered.
  773. */
  774. addr = mm_last_addr;
  775. size = mm_last_size;
  776. if ((flag & CMD_FLAG_REPEAT) == 0) {
  777. /* New command specified. Check for a size specification.
  778. * Defaults to long if no or incorrect specification.
  779. */
  780. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  781. return 1;
  782. /* Address is specified since argc > 1
  783. */
  784. addr = simple_strtoul(argv[1], NULL, 16);
  785. addr += base_address;
  786. }
  787. #ifdef CONFIG_HAS_DATAFLASH
  788. if (addr_dataflash(addr)){
  789. printf("Can't modify DataFlash in place. Use cp instead.\n\r");
  790. return 0;
  791. }
  792. #endif
  793. /* Print the address, followed by value. Then accept input for
  794. * the next value. A non-converted value exits.
  795. */
  796. do {
  797. printf("%08lx:", addr);
  798. if (size == 4)
  799. printf(" %08x", *((uint *)addr));
  800. else if (size == 2)
  801. printf(" %04x", *((ushort *)addr));
  802. else
  803. printf(" %02x", *((u_char *)addr));
  804. nbytes = readline (" ? ");
  805. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  806. /* <CR> pressed as only input, don't modify current
  807. * location and move to next. "-" pressed will go back.
  808. */
  809. if (incrflag)
  810. addr += nbytes ? -size : size;
  811. nbytes = 1;
  812. #ifdef CONFIG_BOOT_RETRY_TIME
  813. reset_cmd_timeout(); /* good enough to not time out */
  814. #endif
  815. }
  816. #ifdef CONFIG_BOOT_RETRY_TIME
  817. else if (nbytes == -2) {
  818. break; /* timed out, exit the command */
  819. }
  820. #endif
  821. else {
  822. char *endp;
  823. i = simple_strtoul(console_buffer, &endp, 16);
  824. nbytes = endp - console_buffer;
  825. if (nbytes) {
  826. #ifdef CONFIG_BOOT_RETRY_TIME
  827. /* good enough to not time out
  828. */
  829. reset_cmd_timeout();
  830. #endif
  831. if (size == 4)
  832. *((uint *)addr) = i;
  833. else if (size == 2)
  834. *((ushort *)addr) = i;
  835. else
  836. *((u_char *)addr) = i;
  837. if (incrflag)
  838. addr += size;
  839. }
  840. }
  841. } while (nbytes);
  842. mm_last_addr = addr;
  843. mm_last_size = size;
  844. return 0;
  845. }
  846. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  847. {
  848. ulong addr, length;
  849. ulong crc;
  850. ulong *ptr;
  851. if (argc < 3) {
  852. printf ("Usage:\n%s\n", cmdtp->usage);
  853. return 1;
  854. }
  855. addr = simple_strtoul (argv[1], NULL, 16);
  856. addr += base_address;
  857. length = simple_strtoul (argv[2], NULL, 16);
  858. crc = crc32 (0, (const uchar *) addr, length);
  859. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  860. addr, addr + length - 1, crc);
  861. if (argc > 3) {
  862. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  863. *ptr = crc;
  864. }
  865. return 0;
  866. }
  867. /**************************************************/
  868. #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
  869. U_BOOT_CMD(
  870. md, 3, 1, do_mem_md,
  871. "md - memory display\n",
  872. "[.b, .w, .l] address [# of objects]\n - memory display\n"
  873. );
  874. U_BOOT_CMD(
  875. mm, 2, 1, do_mem_mm,
  876. "mm - memory modify (auto-incrementing)\n",
  877. "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
  878. );
  879. U_BOOT_CMD(
  880. nm, 2, 1, do_mem_nm,
  881. "nm - memory modify (constant address)\n",
  882. "[.b, .w, .l] address\n - memory modify, read and keep address\n"
  883. );
  884. U_BOOT_CMD(
  885. mw, 4, 1, do_mem_mw,
  886. "mw - memory write (fill)\n",
  887. "[.b, .w, .l] address value [count]\n - write memory\n"
  888. );
  889. U_BOOT_CMD(
  890. cp, 4, 1, do_mem_cp,
  891. "cp - memory copy\n",
  892. "[.b, .w, .l] source target count\n - copy memory\n"
  893. );
  894. U_BOOT_CMD(
  895. cmp, 4, 1, do_mem_cmp,
  896. "cmp - memory compare\n",
  897. "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
  898. );
  899. U_BOOT_CMD(
  900. crc32, 4, 1, do_mem_crc,
  901. "crc32 - checksum calculation\n",
  902. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  903. );
  904. U_BOOT_CMD(
  905. base, 2, 1, do_mem_base,
  906. "base - print or set address offset\n",
  907. "\n - print address offset for memory commands\n"
  908. "base off\n - set address offset for memory commands to 'off'\n"
  909. );
  910. U_BOOT_CMD(
  911. loop, 3, 1, do_mem_loop,
  912. "loop - infinite loop on address range\n",
  913. "[.b, .w, .l] address number_of_objects\n"
  914. " - loop on a set of addresses\n"
  915. );
  916. U_BOOT_CMD(
  917. mtest, 4, 1, do_mem_mtest,
  918. "mtest - simple RAM test\n",
  919. "[start [end [pattern]]]\n"
  920. " - simple RAM read/write test\n"
  921. );
  922. #endif
  923. #endif /* CFG_CMD_MEMORY */