cmd_mem.c 29 KB

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