cmd_mem.c 27 KB

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