mem.c 27 KB

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