cmd_mem.c 28 KB

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