cmd_mem.c 27 KB

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