mem.c 28 KB

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