cmd_mem.c 29 KB

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