cmd_mem.c 30 KB

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