mem.c 29 KB

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