mem.c 27 KB

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