mem.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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_MX_CYCLIC
  144. static int do_mem_mdc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  145. {
  146. int i;
  147. ulong count;
  148. if (argc < 4)
  149. return CMD_RET_USAGE;
  150. count = simple_strtoul(argv[3], NULL, 10);
  151. for (;;) {
  152. do_mem_md (NULL, 0, 3, argv);
  153. /* delay for <count> ms... */
  154. for (i=0; i<count; i++)
  155. udelay (1000);
  156. /* check for ctrl-c to abort... */
  157. if (ctrlc()) {
  158. puts("Abort\n");
  159. return 0;
  160. }
  161. }
  162. return 0;
  163. }
  164. static int do_mem_mwc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  165. {
  166. int i;
  167. ulong count;
  168. if (argc < 4)
  169. return CMD_RET_USAGE;
  170. count = simple_strtoul(argv[3], NULL, 10);
  171. for (;;) {
  172. do_mem_mw (NULL, 0, 3, argv);
  173. /* delay for <count> ms... */
  174. for (i=0; i<count; i++)
  175. udelay (1000);
  176. /* check for ctrl-c to abort... */
  177. if (ctrlc()) {
  178. puts("Abort\n");
  179. return 0;
  180. }
  181. }
  182. return 0;
  183. }
  184. #endif /* CONFIG_MX_CYCLIC */
  185. static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  186. {
  187. ulong addr1, addr2, count, ngood, bytes;
  188. int size;
  189. int rcode = 0;
  190. const char *type;
  191. const void *buf1, *buf2, *base;
  192. #ifdef 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 ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
  706. vu_long pattern, int iteration)
  707. {
  708. vu_long *end;
  709. vu_long *addr;
  710. ulong errs = 0;
  711. ulong incr, length;
  712. ulong val, readback;
  713. /* Alternate the pattern */
  714. incr = 1;
  715. if (iteration & 1) {
  716. incr = -incr;
  717. /*
  718. * Flip the pattern each time to make lots of zeros and
  719. * then, the next time, lots of ones. We decrement
  720. * the "negative" patterns and increment the "positive"
  721. * patterns to preserve this feature.
  722. */
  723. if (pattern & 0x80000000)
  724. pattern = -pattern; /* complement & increment */
  725. else
  726. pattern = ~pattern;
  727. }
  728. length = (end_addr - start_addr) / sizeof(ulong);
  729. end = buf + length;
  730. printf("\rPattern %08lX Writing..."
  731. "%12s"
  732. "\b\b\b\b\b\b\b\b\b\b",
  733. pattern, "");
  734. for (addr = buf, val = pattern; addr < end; addr++) {
  735. WATCHDOG_RESET();
  736. *addr = val;
  737. val += incr;
  738. }
  739. puts("Reading...");
  740. for (addr = buf, val = pattern; addr < end; addr++) {
  741. WATCHDOG_RESET();
  742. readback = *addr;
  743. if (readback != val) {
  744. ulong offset = addr - buf;
  745. printf("\nMem error @ 0x%08X: "
  746. "found %08lX, expected %08lX\n",
  747. (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
  748. readback, val);
  749. errs++;
  750. if (ctrlc())
  751. return -1;
  752. }
  753. val += incr;
  754. }
  755. return errs;
  756. }
  757. /*
  758. * Perform a memory test. A more complete alternative test can be
  759. * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
  760. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  761. */
  762. static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
  763. char * const argv[])
  764. {
  765. ulong start, end;
  766. vu_long *buf, *dummy;
  767. ulong iteration_limit = 0;
  768. int ret;
  769. ulong errs = 0; /* number of errors, or -1 if interrupted */
  770. ulong pattern = 0;
  771. int iteration;
  772. #if defined(CONFIG_SYS_ALT_MEMTEST)
  773. const int alt_test = 1;
  774. #else
  775. const int alt_test = 0;
  776. #endif
  777. start = CONFIG_SYS_MEMTEST_START;
  778. end = CONFIG_SYS_MEMTEST_END;
  779. if (argc > 1)
  780. if (strict_strtoul(argv[1], 16, &start) < 0)
  781. return CMD_RET_USAGE;
  782. if (argc > 2)
  783. if (strict_strtoul(argv[2], 16, &end) < 0)
  784. return CMD_RET_USAGE;
  785. if (argc > 3)
  786. if (strict_strtoul(argv[3], 16, &pattern) < 0)
  787. return CMD_RET_USAGE;
  788. if (argc > 4)
  789. if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
  790. return CMD_RET_USAGE;
  791. if (end < start) {
  792. printf("Refusing to do empty test\n");
  793. return -1;
  794. }
  795. printf("Testing %08lx ... %08lx:\n", start, end);
  796. debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
  797. start, end);
  798. buf = map_sysmem(start, end - start);
  799. dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
  800. for (iteration = 0;
  801. !iteration_limit || iteration < iteration_limit;
  802. iteration++) {
  803. if (ctrlc()) {
  804. errs = -1UL;
  805. break;
  806. }
  807. printf("Iteration: %6d\r", iteration + 1);
  808. debug("\n");
  809. if (alt_test) {
  810. errs = mem_test_alt(buf, start, end, dummy);
  811. } else {
  812. errs = mem_test_quick(buf, start, end, pattern,
  813. iteration);
  814. }
  815. if (errs == -1UL)
  816. break;
  817. }
  818. /*
  819. * Work-around for eldk-4.2 which gives this warning if we try to
  820. * case in the unmap_sysmem() call:
  821. * warning: initialization discards qualifiers from pointer target type
  822. */
  823. {
  824. void *vbuf = (void *)buf;
  825. void *vdummy = (void *)dummy;
  826. unmap_sysmem(vbuf);
  827. unmap_sysmem(vdummy);
  828. }
  829. if (errs == -1UL) {
  830. /* Memory test was aborted - write a newline to finish off */
  831. putc('\n');
  832. ret = 1;
  833. } else {
  834. printf("Tested %d iteration(s) with %lu errors.\n",
  835. iteration, errs);
  836. ret = errs != 0;
  837. }
  838. return ret;
  839. }
  840. #endif /* CONFIG_CMD_MEMTEST */
  841. /* Modify memory.
  842. *
  843. * Syntax:
  844. * mm{.b, .w, .l, .q} {addr}
  845. * nm{.b, .w, .l, .q} {addr}
  846. */
  847. static int
  848. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
  849. {
  850. ulong addr;
  851. #ifdef MEM_SUPPORT_64BIT_DATA
  852. u64 i;
  853. #else
  854. ulong i;
  855. #endif
  856. int nbytes, size;
  857. void *ptr = NULL;
  858. if (argc != 2)
  859. return CMD_RET_USAGE;
  860. bootretry_reset_cmd_timeout(); /* got a good command to get here */
  861. /* We use the last specified parameters, unless new ones are
  862. * entered.
  863. */
  864. addr = mm_last_addr;
  865. size = mm_last_size;
  866. if ((flag & CMD_FLAG_REPEAT) == 0) {
  867. /* New command specified. Check for a size specification.
  868. * Defaults to long if no or incorrect specification.
  869. */
  870. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  871. return 1;
  872. /* Address is specified since argc > 1
  873. */
  874. addr = simple_strtoul(argv[1], NULL, 16);
  875. addr += base_address;
  876. }
  877. /* Print the address, followed by value. Then accept input for
  878. * the next value. A non-converted value exits.
  879. */
  880. do {
  881. ptr = map_sysmem(addr, size);
  882. printf("%08lx:", addr);
  883. if (size == 4)
  884. printf(" %08x", *((u32 *)ptr));
  885. #ifdef MEM_SUPPORT_64BIT_DATA
  886. else if (size == 8)
  887. printf(" %016llx", *((u64 *)ptr));
  888. #endif
  889. else if (size == 2)
  890. printf(" %04x", *((u16 *)ptr));
  891. else
  892. printf(" %02x", *((u8 *)ptr));
  893. nbytes = cli_readline(" ? ");
  894. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  895. /* <CR> pressed as only input, don't modify current
  896. * location and move to next. "-" pressed will go back.
  897. */
  898. if (incrflag)
  899. addr += nbytes ? -size : size;
  900. nbytes = 1;
  901. /* good enough to not time out */
  902. bootretry_reset_cmd_timeout();
  903. }
  904. #ifdef CONFIG_BOOT_RETRY_TIME
  905. else if (nbytes == -2) {
  906. break; /* timed out, exit the command */
  907. }
  908. #endif
  909. else {
  910. char *endp;
  911. #ifdef MEM_SUPPORT_64BIT_DATA
  912. i = simple_strtoull(console_buffer, &endp, 16);
  913. #else
  914. i = simple_strtoul(console_buffer, &endp, 16);
  915. #endif
  916. nbytes = endp - console_buffer;
  917. if (nbytes) {
  918. /* good enough to not time out
  919. */
  920. bootretry_reset_cmd_timeout();
  921. if (size == 4)
  922. *((u32 *)ptr) = i;
  923. #ifdef MEM_SUPPORT_64BIT_DATA
  924. else if (size == 8)
  925. *((u64 *)ptr) = i;
  926. #endif
  927. else if (size == 2)
  928. *((u16 *)ptr) = i;
  929. else
  930. *((u8 *)ptr) = i;
  931. if (incrflag)
  932. addr += size;
  933. }
  934. }
  935. } while (nbytes);
  936. if (ptr)
  937. unmap_sysmem(ptr);
  938. mm_last_addr = addr;
  939. mm_last_size = size;
  940. return 0;
  941. }
  942. #ifdef CONFIG_CMD_CRC32
  943. static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  944. {
  945. int flags = 0;
  946. int ac;
  947. char * const *av;
  948. if (argc < 3)
  949. return CMD_RET_USAGE;
  950. av = argv + 1;
  951. ac = argc - 1;
  952. #ifdef CONFIG_CRC32_VERIFY
  953. if (strcmp(*av, "-v") == 0) {
  954. flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
  955. av++;
  956. ac--;
  957. }
  958. #endif
  959. return hash_command("crc32", flags, cmdtp, flag, ac, av);
  960. }
  961. #endif
  962. #ifdef CONFIG_CMD_RANDOM
  963. static int do_random(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  964. {
  965. unsigned long addr, len;
  966. unsigned long seed; // NOT INITIALIZED ON PURPOSE
  967. unsigned int *buf, *start;
  968. unsigned char *buf8;
  969. unsigned int i;
  970. if (argc < 3 || argc > 4) {
  971. printf("usage: %s <addr> <len> [<seed>]\n", argv[0]);
  972. return 0;
  973. }
  974. len = simple_strtoul(argv[2], NULL, 16);
  975. addr = simple_strtoul(argv[1], NULL, 16);
  976. if (argc == 4) {
  977. seed = simple_strtoul(argv[3], NULL, 16);
  978. if (seed == 0) {
  979. printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
  980. seed = 0xDEADBEEF;
  981. }
  982. } else {
  983. seed = get_timer(0) ^ rand();
  984. }
  985. srand(seed);
  986. start = map_sysmem(addr, len);
  987. buf = start;
  988. for (i = 0; i < (len / 4); i++)
  989. *buf++ = rand();
  990. buf8 = (unsigned char *)buf;
  991. for (i = 0; i < (len % 4); i++)
  992. *buf8++ = rand() & 0xFF;
  993. unmap_sysmem(start);
  994. printf("%lu bytes filled with random data\n", len);
  995. return 1;
  996. }
  997. #endif
  998. /**************************************************/
  999. U_BOOT_CMD(
  1000. md, 3, 1, do_mem_md,
  1001. "memory display",
  1002. #ifdef MEM_SUPPORT_64BIT_DATA
  1003. "[.b, .w, .l, .q] address [# of objects]"
  1004. #else
  1005. "[.b, .w, .l] address [# of objects]"
  1006. #endif
  1007. );
  1008. U_BOOT_CMD(
  1009. mm, 2, 1, do_mem_mm,
  1010. "memory modify (auto-incrementing address)",
  1011. #ifdef MEM_SUPPORT_64BIT_DATA
  1012. "[.b, .w, .l, .q] address"
  1013. #else
  1014. "[.b, .w, .l] address"
  1015. #endif
  1016. );
  1017. U_BOOT_CMD(
  1018. nm, 2, 1, do_mem_nm,
  1019. "memory modify (constant address)",
  1020. #ifdef MEM_SUPPORT_64BIT_DATA
  1021. "[.b, .w, .l, .q] address"
  1022. #else
  1023. "[.b, .w, .l] address"
  1024. #endif
  1025. );
  1026. U_BOOT_CMD(
  1027. mw, 4, 1, do_mem_mw,
  1028. "memory write (fill)",
  1029. #ifdef MEM_SUPPORT_64BIT_DATA
  1030. "[.b, .w, .l, .q] address value [count]"
  1031. #else
  1032. "[.b, .w, .l] address value [count]"
  1033. #endif
  1034. );
  1035. U_BOOT_CMD(
  1036. cp, 4, 1, do_mem_cp,
  1037. "memory copy",
  1038. #ifdef MEM_SUPPORT_64BIT_DATA
  1039. "[.b, .w, .l, .q] source target count"
  1040. #else
  1041. "[.b, .w, .l] source target count"
  1042. #endif
  1043. );
  1044. U_BOOT_CMD(
  1045. cmp, 4, 1, do_mem_cmp,
  1046. "memory compare",
  1047. #ifdef MEM_SUPPORT_64BIT_DATA
  1048. "[.b, .w, .l, .q] addr1 addr2 count"
  1049. #else
  1050. "[.b, .w, .l] addr1 addr2 count"
  1051. #endif
  1052. );
  1053. #ifdef CONFIG_CMD_CRC32
  1054. #ifndef CONFIG_CRC32_VERIFY
  1055. U_BOOT_CMD(
  1056. crc32, 4, 1, do_mem_crc,
  1057. "checksum calculation",
  1058. "address count [addr]\n - compute CRC32 checksum [save at addr]"
  1059. );
  1060. #else /* CONFIG_CRC32_VERIFY */
  1061. U_BOOT_CMD(
  1062. crc32, 5, 1, do_mem_crc,
  1063. "checksum calculation",
  1064. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1065. "-v address count crc\n - verify crc of memory area"
  1066. );
  1067. #endif /* CONFIG_CRC32_VERIFY */
  1068. #endif
  1069. #ifdef CONFIG_CMD_MEMINFO
  1070. static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
  1071. char * const argv[])
  1072. {
  1073. puts("DRAM: ");
  1074. print_size(gd->ram_size, "\n");
  1075. return 0;
  1076. }
  1077. #endif
  1078. U_BOOT_CMD(
  1079. base, 2, 1, do_mem_base,
  1080. "print or set address offset",
  1081. "\n - print address offset for memory commands\n"
  1082. "base off\n - set address offset for memory commands to 'off'"
  1083. );
  1084. U_BOOT_CMD(
  1085. loop, 3, 1, do_mem_loop,
  1086. "infinite loop on address range",
  1087. #ifdef MEM_SUPPORT_64BIT_DATA
  1088. "[.b, .w, .l, .q] address number_of_objects"
  1089. #else
  1090. "[.b, .w, .l] address number_of_objects"
  1091. #endif
  1092. );
  1093. #ifdef CONFIG_LOOPW
  1094. U_BOOT_CMD(
  1095. loopw, 4, 1, do_mem_loopw,
  1096. "infinite write loop on address range",
  1097. #ifdef MEM_SUPPORT_64BIT_DATA
  1098. "[.b, .w, .l, .q] address number_of_objects data_to_write"
  1099. #else
  1100. "[.b, .w, .l] address number_of_objects data_to_write"
  1101. #endif
  1102. );
  1103. #endif /* CONFIG_LOOPW */
  1104. #ifdef CONFIG_CMD_MEMTEST
  1105. U_BOOT_CMD(
  1106. mtest, 5, 1, do_mem_mtest,
  1107. "simple RAM read/write test",
  1108. "[start [end [pattern [iterations]]]]"
  1109. );
  1110. #endif /* CONFIG_CMD_MEMTEST */
  1111. #ifdef CONFIG_MX_CYCLIC
  1112. U_BOOT_CMD(
  1113. mdc, 4, 1, do_mem_mdc,
  1114. "memory display cyclic",
  1115. #ifdef MEM_SUPPORT_64BIT_DATA
  1116. "[.b, .w, .l, .q] address count delay(ms)"
  1117. #else
  1118. "[.b, .w, .l] address count delay(ms)"
  1119. #endif
  1120. );
  1121. U_BOOT_CMD(
  1122. mwc, 4, 1, do_mem_mwc,
  1123. "memory write cyclic",
  1124. #ifdef MEM_SUPPORT_64BIT_DATA
  1125. "[.b, .w, .l, .q] address value delay(ms)"
  1126. #else
  1127. "[.b, .w, .l] address value delay(ms)"
  1128. #endif
  1129. );
  1130. #endif /* CONFIG_MX_CYCLIC */
  1131. #ifdef CONFIG_CMD_MEMINFO
  1132. U_BOOT_CMD(
  1133. meminfo, 3, 1, do_mem_info,
  1134. "display memory information",
  1135. ""
  1136. );
  1137. #endif
  1138. #ifdef CONFIG_CMD_RANDOM
  1139. U_BOOT_CMD(
  1140. random, 4, 0, do_random,
  1141. "fill memory with random pattern",
  1142. "<addr> <len> [<seed>]\n"
  1143. " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
  1144. );
  1145. #endif