mem.c 29 KB

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