cmd_mem.c 28 KB

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