cmd_mem.c 28 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]); j++) {
  583. val = bitpattern[j];
  584. for (; val != 0; val <<= 1) {
  585. *addr = val;
  586. *dummy = ~val; /* clear the test data off the bus */
  587. readback = *addr;
  588. if (readback != val) {
  589. printf("FAILURE (data line): "
  590. "expected %08lx, actual %08lx\n",
  591. val, readback);
  592. errs++;
  593. if (ctrlc()) {
  594. putc('\n');
  595. return -1;
  596. }
  597. }
  598. *addr = ~val;
  599. *dummy = val;
  600. readback = *addr;
  601. if (readback != ~val) {
  602. printf("FAILURE (data line): "
  603. "Is %08lx, should be %08lx\n",
  604. readback, ~val);
  605. errs++;
  606. if (ctrlc()) {
  607. putc('\n');
  608. return -1;
  609. }
  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 = ((ulong)end - (ulong)start)/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. putc('\n');
  669. return -1;
  670. }
  671. }
  672. }
  673. start[test_offset] = pattern;
  674. WATCHDOG_RESET();
  675. /*
  676. * Check for addr bits stuck low or shorted.
  677. */
  678. for (test_offset = 1; test_offset < len; test_offset <<= 1) {
  679. start[test_offset] = anti_pattern;
  680. for (offset = 1; offset < len; offset <<= 1) {
  681. temp = start[offset];
  682. if ((temp != pattern) && (offset != test_offset)) {
  683. printf("\nFAILURE: Address bit stuck low or"
  684. " shorted @ 0x%.8lx: expected 0x%.8lx,"
  685. " actual 0x%.8lx\n",
  686. (ulong)&start[offset], pattern, temp);
  687. errs++;
  688. if (ctrlc()) {
  689. putc('\n');
  690. return -1;
  691. }
  692. }
  693. }
  694. start[test_offset] = pattern;
  695. }
  696. /*
  697. * Description: Test the integrity of a physical
  698. * memory device by performing an
  699. * increment/decrement test over the
  700. * entire region. In the process every
  701. * storage bit in the device is tested
  702. * as a zero and a one. The base address
  703. * and the size of the region are
  704. * selected by the caller.
  705. *
  706. * Returns: 0 if the test succeeds, 1 if the test fails.
  707. */
  708. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  709. /*
  710. * Fill memory with a known pattern.
  711. */
  712. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  713. WATCHDOG_RESET();
  714. start[offset] = pattern;
  715. }
  716. /*
  717. * Check each location and invert it for the second pass.
  718. */
  719. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  720. WATCHDOG_RESET();
  721. temp = start[offset];
  722. if (temp != pattern) {
  723. printf("\nFAILURE (read/write) @ 0x%.8lx:"
  724. " expected 0x%.8lx, actual 0x%.8lx)\n",
  725. (ulong)&start[offset], pattern, temp);
  726. errs++;
  727. if (ctrlc()) {
  728. putc('\n');
  729. return -1;
  730. }
  731. }
  732. anti_pattern = ~pattern;
  733. start[offset] = anti_pattern;
  734. }
  735. /*
  736. * Check each location for the inverted pattern and zero it.
  737. */
  738. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  739. WATCHDOG_RESET();
  740. anti_pattern = ~pattern;
  741. temp = start[offset];
  742. if (temp != anti_pattern) {
  743. printf("\nFAILURE (read/write): @ 0x%.8lx:"
  744. " expected 0x%.8lx, actual 0x%.8lx)\n",
  745. (ulong)&start[offset], anti_pattern, temp);
  746. errs++;
  747. if (ctrlc()) {
  748. putc('\n');
  749. return -1;
  750. }
  751. }
  752. start[offset] = 0;
  753. }
  754. return 0;
  755. }
  756. static ulong mem_test_quick(vu_long *start, vu_long *end, vu_long pattern,
  757. int iteration)
  758. {
  759. vu_long *addr;
  760. ulong errs = 0;
  761. ulong incr;
  762. ulong val, readback;
  763. /* Alternate the pattern */
  764. incr = 1;
  765. if (iteration & 1) {
  766. incr = -incr;
  767. /*
  768. * Flip the pattern each time to make lots of zeros and
  769. * then, the next time, lots of ones. We decrement
  770. * the "negative" patterns and increment the "positive"
  771. * patterns to preserve this feature.
  772. */
  773. if (pattern & 0x80000000)
  774. pattern = -pattern; /* complement & increment */
  775. else
  776. pattern = ~pattern;
  777. }
  778. printf("\rPattern %08lX Writing..."
  779. "%12s"
  780. "\b\b\b\b\b\b\b\b\b\b",
  781. pattern, "");
  782. for (addr = start, val = pattern; addr < end; addr++) {
  783. WATCHDOG_RESET();
  784. *addr = val;
  785. val += incr;
  786. }
  787. puts("Reading...");
  788. for (addr = start, val = pattern; addr < end; addr++) {
  789. WATCHDOG_RESET();
  790. readback = *addr;
  791. if (readback != val) {
  792. printf("\nMem error @ 0x%08X: "
  793. "found %08lX, expected %08lX\n",
  794. (uint)(uintptr_t)addr, readback, val);
  795. errs++;
  796. if (ctrlc()) {
  797. putc('\n');
  798. return -1;
  799. }
  800. }
  801. val += incr;
  802. }
  803. return 0;
  804. }
  805. /*
  806. * Perform a memory test. A more complete alternative test can be
  807. * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
  808. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  809. */
  810. static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
  811. char * const argv[])
  812. {
  813. vu_long *start, *end;
  814. int iteration_limit;
  815. int ret;
  816. ulong errs = 0; /* number of errors, or -1 if interrupted */
  817. ulong pattern;
  818. int iteration;
  819. #if defined(CONFIG_SYS_ALT_MEMTEST)
  820. const int alt_test = 1;
  821. #else
  822. const int alt_test = 0;
  823. #endif
  824. if (argc > 1)
  825. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  826. else
  827. start = (ulong *)CONFIG_SYS_MEMTEST_START;
  828. if (argc > 2)
  829. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  830. else
  831. end = (ulong *)(CONFIG_SYS_MEMTEST_END);
  832. if (argc > 3)
  833. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  834. else
  835. pattern = 0;
  836. if (argc > 4)
  837. iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
  838. else
  839. iteration_limit = 0;
  840. printf("Testing %08x ... %08x:\n", (uint)(uintptr_t)start,
  841. (uint)(uintptr_t)end);
  842. debug("%s:%d: start 0x%p end 0x%p\n",
  843. __func__, __LINE__, start, end);
  844. for (iteration = 0;
  845. !iteration_limit || iteration < iteration_limit;
  846. iteration++) {
  847. if (ctrlc()) {
  848. putc('\n');
  849. errs = -1UL;
  850. break;
  851. }
  852. printf("Iteration: %6d\r", iteration + 1);
  853. debug("\n");
  854. if (alt_test)
  855. errs = mem_test_alt(start, end);
  856. else
  857. errs = mem_test_quick(start, end, pattern, iteration);
  858. }
  859. if (errs == -1UL) {
  860. /* Memory test was aborted */
  861. ret = 1;
  862. } else {
  863. printf("Tested %d iteration(s) with %lu errors.\n",
  864. iteration, errs);
  865. ret = errs != 0;
  866. }
  867. return ret; /* not reached */
  868. }
  869. /* Modify memory.
  870. *
  871. * Syntax:
  872. * mm{.b, .w, .l} {addr}
  873. * nm{.b, .w, .l} {addr}
  874. */
  875. static int
  876. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
  877. {
  878. ulong addr, i;
  879. int nbytes, size;
  880. void *ptr = NULL;
  881. if (argc != 2)
  882. return CMD_RET_USAGE;
  883. #ifdef CONFIG_BOOT_RETRY_TIME
  884. reset_cmd_timeout(); /* got a good command to get here */
  885. #endif
  886. /* We use the last specified parameters, unless new ones are
  887. * entered.
  888. */
  889. addr = mm_last_addr;
  890. size = mm_last_size;
  891. if ((flag & CMD_FLAG_REPEAT) == 0) {
  892. /* New command specified. Check for a size specification.
  893. * Defaults to long if no or incorrect specification.
  894. */
  895. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  896. return 1;
  897. /* Address is specified since argc > 1
  898. */
  899. addr = simple_strtoul(argv[1], NULL, 16);
  900. addr += base_address;
  901. }
  902. #ifdef CONFIG_HAS_DATAFLASH
  903. if (addr_dataflash(addr)){
  904. puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
  905. return 0;
  906. }
  907. #endif
  908. #ifdef CONFIG_BLACKFIN
  909. if (addr_bfin_on_chip_mem(addr)) {
  910. puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
  911. return 0;
  912. }
  913. #endif
  914. /* Print the address, followed by value. Then accept input for
  915. * the next value. A non-converted value exits.
  916. */
  917. do {
  918. ptr = map_sysmem(addr, size);
  919. printf("%08lx:", addr);
  920. if (size == 4)
  921. printf(" %08x", *((uint *)ptr));
  922. else if (size == 2)
  923. printf(" %04x", *((ushort *)ptr));
  924. else
  925. printf(" %02x", *((u_char *)ptr));
  926. nbytes = readline (" ? ");
  927. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  928. /* <CR> pressed as only input, don't modify current
  929. * location and move to next. "-" pressed will go back.
  930. */
  931. if (incrflag)
  932. addr += nbytes ? -size : size;
  933. nbytes = 1;
  934. #ifdef CONFIG_BOOT_RETRY_TIME
  935. reset_cmd_timeout(); /* good enough to not time out */
  936. #endif
  937. }
  938. #ifdef CONFIG_BOOT_RETRY_TIME
  939. else if (nbytes == -2) {
  940. break; /* timed out, exit the command */
  941. }
  942. #endif
  943. else {
  944. char *endp;
  945. i = simple_strtoul(console_buffer, &endp, 16);
  946. nbytes = endp - console_buffer;
  947. if (nbytes) {
  948. #ifdef CONFIG_BOOT_RETRY_TIME
  949. /* good enough to not time out
  950. */
  951. reset_cmd_timeout();
  952. #endif
  953. if (size == 4)
  954. *((uint *)ptr) = i;
  955. else if (size == 2)
  956. *((ushort *)ptr) = i;
  957. else
  958. *((u_char *)ptr) = i;
  959. if (incrflag)
  960. addr += size;
  961. }
  962. }
  963. } while (nbytes);
  964. if (ptr)
  965. unmap_sysmem(ptr);
  966. mm_last_addr = addr;
  967. mm_last_size = size;
  968. return 0;
  969. }
  970. #ifdef CONFIG_CMD_CRC32
  971. #ifndef CONFIG_CRC32_VERIFY
  972. static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  973. {
  974. ulong addr, length;
  975. ulong crc;
  976. ulong *ptr;
  977. if (argc < 3)
  978. return CMD_RET_USAGE;
  979. addr = simple_strtoul (argv[1], NULL, 16);
  980. addr += base_address;
  981. length = simple_strtoul (argv[2], NULL, 16);
  982. crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
  983. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  984. addr, addr + length - 1, crc);
  985. if (argc > 3) {
  986. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  987. *ptr = crc;
  988. }
  989. return 0;
  990. }
  991. #else /* CONFIG_CRC32_VERIFY */
  992. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  993. {
  994. ulong addr, length;
  995. ulong crc;
  996. ulong *ptr;
  997. ulong vcrc;
  998. int verify;
  999. int ac;
  1000. char * const *av;
  1001. if (argc < 3) {
  1002. usage:
  1003. return CMD_RET_USAGE;
  1004. }
  1005. av = argv + 1;
  1006. ac = argc - 1;
  1007. if (strcmp(*av, "-v") == 0) {
  1008. verify = 1;
  1009. av++;
  1010. ac--;
  1011. if (ac < 3)
  1012. goto usage;
  1013. } else
  1014. verify = 0;
  1015. addr = simple_strtoul(*av++, NULL, 16);
  1016. addr += base_address;
  1017. length = simple_strtoul(*av++, NULL, 16);
  1018. crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
  1019. if (!verify) {
  1020. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  1021. addr, addr + length - 1, crc);
  1022. if (ac > 2) {
  1023. ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
  1024. *ptr = crc;
  1025. }
  1026. } else {
  1027. vcrc = simple_strtoul(*av++, NULL, 16);
  1028. if (vcrc != crc) {
  1029. printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
  1030. addr, addr + length - 1, crc, vcrc);
  1031. return 1;
  1032. }
  1033. }
  1034. return 0;
  1035. }
  1036. #endif /* CONFIG_CRC32_VERIFY */
  1037. #endif
  1038. /**************************************************/
  1039. U_BOOT_CMD(
  1040. md, 3, 1, do_mem_md,
  1041. "memory display",
  1042. "[.b, .w, .l] address [# of objects]"
  1043. );
  1044. U_BOOT_CMD(
  1045. mm, 2, 1, do_mem_mm,
  1046. "memory modify (auto-incrementing address)",
  1047. "[.b, .w, .l] address"
  1048. );
  1049. U_BOOT_CMD(
  1050. nm, 2, 1, do_mem_nm,
  1051. "memory modify (constant address)",
  1052. "[.b, .w, .l] address"
  1053. );
  1054. U_BOOT_CMD(
  1055. mw, 4, 1, do_mem_mw,
  1056. "memory write (fill)",
  1057. "[.b, .w, .l] address value [count]"
  1058. );
  1059. U_BOOT_CMD(
  1060. cp, 4, 1, do_mem_cp,
  1061. "memory copy",
  1062. "[.b, .w, .l] source target count"
  1063. );
  1064. U_BOOT_CMD(
  1065. cmp, 4, 1, do_mem_cmp,
  1066. "memory compare",
  1067. "[.b, .w, .l] addr1 addr2 count"
  1068. );
  1069. #ifdef CONFIG_CMD_CRC32
  1070. #ifndef CONFIG_CRC32_VERIFY
  1071. U_BOOT_CMD(
  1072. crc32, 4, 1, do_mem_crc,
  1073. "checksum calculation",
  1074. "address count [addr]\n - compute CRC32 checksum [save at addr]"
  1075. );
  1076. #else /* CONFIG_CRC32_VERIFY */
  1077. U_BOOT_CMD(
  1078. crc32, 5, 1, do_mem_crc,
  1079. "checksum calculation",
  1080. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1081. "-v address count crc\n - verify crc of memory area"
  1082. );
  1083. #endif /* CONFIG_CRC32_VERIFY */
  1084. #endif
  1085. #ifdef CONFIG_CMD_MEMINFO
  1086. __weak void board_show_dram(ulong size)
  1087. {
  1088. puts("DRAM: ");
  1089. print_size(size, "\n");
  1090. }
  1091. static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
  1092. char * const argv[])
  1093. {
  1094. board_show_dram(gd->ram_size);
  1095. return 0;
  1096. }
  1097. #endif
  1098. U_BOOT_CMD(
  1099. base, 2, 1, do_mem_base,
  1100. "print or set address offset",
  1101. "\n - print address offset for memory commands\n"
  1102. "base off\n - set address offset for memory commands to 'off'"
  1103. );
  1104. U_BOOT_CMD(
  1105. loop, 3, 1, do_mem_loop,
  1106. "infinite loop on address range",
  1107. "[.b, .w, .l] address number_of_objects"
  1108. );
  1109. #ifdef CONFIG_LOOPW
  1110. U_BOOT_CMD(
  1111. loopw, 4, 1, do_mem_loopw,
  1112. "infinite write loop on address range",
  1113. "[.b, .w, .l] address number_of_objects data_to_write"
  1114. );
  1115. #endif /* CONFIG_LOOPW */
  1116. U_BOOT_CMD(
  1117. mtest, 5, 1, do_mem_mtest,
  1118. "simple RAM read/write test",
  1119. "[start [end [pattern [iterations]]]]"
  1120. );
  1121. #ifdef CONFIG_MX_CYCLIC
  1122. U_BOOT_CMD(
  1123. mdc, 4, 1, do_mem_mdc,
  1124. "memory display cyclic",
  1125. "[.b, .w, .l] address count delay(ms)"
  1126. );
  1127. U_BOOT_CMD(
  1128. mwc, 4, 1, do_mem_mwc,
  1129. "memory write cyclic",
  1130. "[.b, .w, .l] address value delay(ms)"
  1131. );
  1132. #endif /* CONFIG_MX_CYCLIC */
  1133. #ifdef CONFIG_CMD_MEMINFO
  1134. U_BOOT_CMD(
  1135. meminfo, 3, 1, do_mem_info,
  1136. "display memory information",
  1137. ""
  1138. );
  1139. #endif