cmd_mem.c 29 KB

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