mem.c 29 KB

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