cmd_mem.c 27 KB

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