cmd_mem.c 28 KB

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