cmd_mem.c 28 KB

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