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