cmd_mem.c 28 KB

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