mem.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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. printf("usage: %s <addr> <len> [<seed>]\n", argv[0]);
  1007. return 0;
  1008. }
  1009. len = simple_strtoul(argv[2], NULL, 16);
  1010. addr = simple_strtoul(argv[1], NULL, 16);
  1011. if (argc == 4) {
  1012. seed = simple_strtoul(argv[3], NULL, 16);
  1013. if (seed == 0) {
  1014. printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
  1015. seed = 0xDEADBEEF;
  1016. }
  1017. } else {
  1018. seed = get_timer(0) ^ rand();
  1019. }
  1020. srand(seed);
  1021. start = map_sysmem(addr, len);
  1022. buf = start;
  1023. for (i = 0; i < (len / 4); i++)
  1024. *buf++ = rand();
  1025. buf8 = (unsigned char *)buf;
  1026. for (i = 0; i < (len % 4); i++)
  1027. *buf8++ = rand() & 0xFF;
  1028. unmap_sysmem(start);
  1029. printf("%lu bytes filled with random data\n", len);
  1030. return 1;
  1031. }
  1032. #endif
  1033. /**************************************************/
  1034. U_BOOT_CMD(
  1035. md, 3, 1, do_mem_md,
  1036. "memory display",
  1037. #ifdef MEM_SUPPORT_64BIT_DATA
  1038. "[.b, .w, .l, .q] address [# of objects]"
  1039. #else
  1040. "[.b, .w, .l] address [# of objects]"
  1041. #endif
  1042. );
  1043. U_BOOT_CMD(
  1044. mm, 2, 1, do_mem_mm,
  1045. "memory modify (auto-incrementing address)",
  1046. #ifdef MEM_SUPPORT_64BIT_DATA
  1047. "[.b, .w, .l, .q] address"
  1048. #else
  1049. "[.b, .w, .l] address"
  1050. #endif
  1051. );
  1052. U_BOOT_CMD(
  1053. nm, 2, 1, do_mem_nm,
  1054. "memory modify (constant address)",
  1055. #ifdef MEM_SUPPORT_64BIT_DATA
  1056. "[.b, .w, .l, .q] address"
  1057. #else
  1058. "[.b, .w, .l] address"
  1059. #endif
  1060. );
  1061. U_BOOT_CMD(
  1062. mw, 4, 1, do_mem_mw,
  1063. "memory write (fill)",
  1064. #ifdef MEM_SUPPORT_64BIT_DATA
  1065. "[.b, .w, .l, .q] address value [count]"
  1066. #else
  1067. "[.b, .w, .l] address value [count]"
  1068. #endif
  1069. );
  1070. U_BOOT_CMD(
  1071. cp, 4, 1, do_mem_cp,
  1072. "memory copy",
  1073. #ifdef MEM_SUPPORT_64BIT_DATA
  1074. "[.b, .w, .l, .q] source target count"
  1075. #else
  1076. "[.b, .w, .l] source target count"
  1077. #endif
  1078. );
  1079. U_BOOT_CMD(
  1080. cmp, 4, 1, do_mem_cmp,
  1081. "memory compare",
  1082. #ifdef MEM_SUPPORT_64BIT_DATA
  1083. "[.b, .w, .l, .q] addr1 addr2 count"
  1084. #else
  1085. "[.b, .w, .l] addr1 addr2 count"
  1086. #endif
  1087. );
  1088. #ifdef CONFIG_CMD_CRC32
  1089. #ifndef CONFIG_CRC32_VERIFY
  1090. U_BOOT_CMD(
  1091. crc32, 4, 1, do_mem_crc,
  1092. "checksum calculation",
  1093. "address count [addr]\n - compute CRC32 checksum [save at addr]"
  1094. );
  1095. #else /* CONFIG_CRC32_VERIFY */
  1096. U_BOOT_CMD(
  1097. crc32, 5, 1, do_mem_crc,
  1098. "checksum calculation",
  1099. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1100. "-v address count crc\n - verify crc of memory area"
  1101. );
  1102. #endif /* CONFIG_CRC32_VERIFY */
  1103. #endif
  1104. #ifdef CONFIG_CMD_MEMINFO
  1105. static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
  1106. char * const argv[])
  1107. {
  1108. puts("DRAM: ");
  1109. print_size(gd->ram_size, "\n");
  1110. return 0;
  1111. }
  1112. #endif
  1113. U_BOOT_CMD(
  1114. base, 2, 1, do_mem_base,
  1115. "print or set address offset",
  1116. "\n - print address offset for memory commands\n"
  1117. "base off\n - set address offset for memory commands to 'off'"
  1118. );
  1119. U_BOOT_CMD(
  1120. loop, 3, 1, do_mem_loop,
  1121. "infinite loop on address range",
  1122. #ifdef MEM_SUPPORT_64BIT_DATA
  1123. "[.b, .w, .l, .q] address number_of_objects"
  1124. #else
  1125. "[.b, .w, .l] address number_of_objects"
  1126. #endif
  1127. );
  1128. #ifdef CONFIG_LOOPW
  1129. U_BOOT_CMD(
  1130. loopw, 4, 1, do_mem_loopw,
  1131. "infinite write loop on address range",
  1132. #ifdef MEM_SUPPORT_64BIT_DATA
  1133. "[.b, .w, .l, .q] address number_of_objects data_to_write"
  1134. #else
  1135. "[.b, .w, .l] address number_of_objects data_to_write"
  1136. #endif
  1137. );
  1138. #endif /* CONFIG_LOOPW */
  1139. #ifdef CONFIG_CMD_MEMTEST
  1140. U_BOOT_CMD(
  1141. mtest, 5, 1, do_mem_mtest,
  1142. "simple RAM read/write test",
  1143. "[start [end [pattern [iterations]]]]"
  1144. );
  1145. #endif /* CONFIG_CMD_MEMTEST */
  1146. #ifdef CONFIG_CMD_MX_CYCLIC
  1147. U_BOOT_CMD(
  1148. mdc, 4, 1, do_mem_mdc,
  1149. "memory display cyclic",
  1150. #ifdef MEM_SUPPORT_64BIT_DATA
  1151. "[.b, .w, .l, .q] address count delay(ms)"
  1152. #else
  1153. "[.b, .w, .l] address count delay(ms)"
  1154. #endif
  1155. );
  1156. U_BOOT_CMD(
  1157. mwc, 4, 1, do_mem_mwc,
  1158. "memory write cyclic",
  1159. #ifdef MEM_SUPPORT_64BIT_DATA
  1160. "[.b, .w, .l, .q] address value delay(ms)"
  1161. #else
  1162. "[.b, .w, .l] address value delay(ms)"
  1163. #endif
  1164. );
  1165. #endif /* CONFIG_CMD_MX_CYCLIC */
  1166. #ifdef CONFIG_CMD_MEMINFO
  1167. U_BOOT_CMD(
  1168. meminfo, 3, 1, do_mem_info,
  1169. "display memory information",
  1170. ""
  1171. );
  1172. #endif
  1173. #ifdef CONFIG_CMD_RANDOM
  1174. U_BOOT_CMD(
  1175. random, 4, 0, do_random,
  1176. "fill memory with random pattern",
  1177. "<addr> <len> [<seed>]\n"
  1178. " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
  1179. );
  1180. #endif