cmd_mem.c 30 KB

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