cmd_mem.c 27 KB

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