mem.c 28 KB

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