cmd_mem.c 29 KB

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