cmd_mem.c 27 KB

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