cmd_mem.c 28 KB

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