cmd_mem.c 28 KB

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