cmd_mem.c 28 KB

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