cmd_mem.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Memory Functions
  25. *
  26. * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
  27. */
  28. #include <common.h>
  29. #include <command.h>
  30. #if defined(CONFIG_CMD_MMC)
  31. #include <mmc.h>
  32. #endif
  33. #ifdef CONFIG_HAS_DATAFLASH
  34. #include <dataflash.h>
  35. #endif
  36. #include <watchdog.h>
  37. #if defined(CONFIG_CMD_MEMORY)
  38. #ifdef CMD_MEM_DEBUG
  39. #define PRINTF(fmt,args...) printf (fmt ,##args)
  40. #else
  41. #define PRINTF(fmt,args...)
  42. #endif
  43. static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
  44. /* Display values from last command.
  45. * Memory modify remembered values are different from display memory.
  46. */
  47. uint dp_last_addr, dp_last_size;
  48. uint dp_last_length = 0x40;
  49. uint mm_last_addr, mm_last_size;
  50. static ulong base_address = 0;
  51. /* Memory Display
  52. *
  53. * Syntax:
  54. * md{.b, .w, .l} {addr} {len}
  55. */
  56. #define DISP_LINE_LEN 16
  57. int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  58. {
  59. ulong addr, length;
  60. #if defined(CONFIG_HAS_DATAFLASH)
  61. ulong nbytes, linebytes;
  62. #endif
  63. int size;
  64. int rc = 0;
  65. /* We use the last specified parameters, unless new ones are
  66. * entered.
  67. */
  68. addr = dp_last_addr;
  69. size = dp_last_size;
  70. length = dp_last_length;
  71. if (argc < 2) {
  72. printf ("Usage:\n%s\n", cmdtp->usage);
  73. return 1;
  74. }
  75. if ((flag & CMD_FLAG_REPEAT) == 0) {
  76. /* New command specified. Check for a size specification.
  77. * Defaults to long if no or incorrect specification.
  78. */
  79. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  80. return 1;
  81. /* Address is specified since argc > 1
  82. */
  83. addr = simple_strtoul(argv[1], NULL, 16);
  84. addr += base_address;
  85. /* If another parameter, it is the length to display.
  86. * Length is the number of objects, not number of bytes.
  87. */
  88. if (argc > 2)
  89. length = simple_strtoul(argv[2], NULL, 16);
  90. }
  91. #if defined(CONFIG_HAS_DATAFLASH)
  92. /* Print the lines.
  93. *
  94. * We buffer all read data, so we can make sure data is read only
  95. * once, and all accesses are with the specified bus width.
  96. */
  97. nbytes = length * size;
  98. do {
  99. char linebuf[DISP_LINE_LEN];
  100. void* p;
  101. linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
  102. rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
  103. p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
  104. print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
  105. nbytes -= linebytes;
  106. addr += linebytes;
  107. if (ctrlc()) {
  108. rc = 1;
  109. break;
  110. }
  111. } while (nbytes > 0);
  112. #else
  113. # if defined(CONFIG_BLACKFIN)
  114. /* See if we're trying to display L1 inst */
  115. if (addr_bfin_on_chip_mem(addr)) {
  116. char linebuf[DISP_LINE_LEN];
  117. ulong linebytes, nbytes = length * size;
  118. do {
  119. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  120. memcpy(linebuf, (void *)addr, linebytes);
  121. print_buffer(addr, linebuf, size, linebytes/size, DISP_LINE_LEN/size);
  122. nbytes -= linebytes;
  123. addr += linebytes;
  124. if (ctrlc()) {
  125. rc = 1;
  126. break;
  127. }
  128. } while (nbytes > 0);
  129. } else
  130. # endif
  131. {
  132. /* Print the lines. */
  133. print_buffer(addr, (void*)addr, size, length, DISP_LINE_LEN/size);
  134. addr += size*length;
  135. }
  136. #endif
  137. dp_last_addr = addr;
  138. dp_last_length = length;
  139. dp_last_size = size;
  140. return (rc);
  141. }
  142. int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  143. {
  144. return mod_mem (cmdtp, 1, flag, argc, argv);
  145. }
  146. int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  147. {
  148. return mod_mem (cmdtp, 0, flag, argc, argv);
  149. }
  150. int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  151. {
  152. ulong addr, writeval, count;
  153. int size;
  154. if ((argc < 3) || (argc > 4)) {
  155. printf ("Usage:\n%s\n", cmdtp->usage);
  156. return 1;
  157. }
  158. /* Check for size specification.
  159. */
  160. if ((size = cmd_get_data_size(argv[0], 4)) < 1)
  161. return 1;
  162. /* Address is specified since argc > 1
  163. */
  164. addr = simple_strtoul(argv[1], NULL, 16);
  165. addr += base_address;
  166. /* Get the value to write.
  167. */
  168. writeval = simple_strtoul(argv[2], NULL, 16);
  169. /* Count ? */
  170. if (argc == 4) {
  171. count = simple_strtoul(argv[3], NULL, 16);
  172. } else {
  173. count = 1;
  174. }
  175. while (count-- > 0) {
  176. if (size == 4)
  177. *((ulong *)addr) = (ulong )writeval;
  178. else if (size == 2)
  179. *((ushort *)addr) = (ushort)writeval;
  180. else
  181. *((u_char *)addr) = (u_char)writeval;
  182. addr += size;
  183. }
  184. return 0;
  185. }
  186. #ifdef CONFIG_MX_CYCLIC
  187. int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  188. {
  189. int i;
  190. ulong count;
  191. if (argc < 4) {
  192. printf ("Usage:\n%s\n", cmdtp->usage);
  193. return 1;
  194. }
  195. count = simple_strtoul(argv[3], NULL, 10);
  196. for (;;) {
  197. do_mem_md (NULL, 0, 3, argv);
  198. /* delay for <count> ms... */
  199. for (i=0; i<count; i++)
  200. udelay (1000);
  201. /* check for ctrl-c to abort... */
  202. if (ctrlc()) {
  203. puts("Abort\n");
  204. return 0;
  205. }
  206. }
  207. return 0;
  208. }
  209. int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  210. {
  211. int i;
  212. ulong count;
  213. if (argc < 4) {
  214. printf ("Usage:\n%s\n", cmdtp->usage);
  215. return 1;
  216. }
  217. count = simple_strtoul(argv[3], NULL, 10);
  218. for (;;) {
  219. do_mem_mw (NULL, 0, 3, argv);
  220. /* delay for <count> ms... */
  221. for (i=0; i<count; i++)
  222. udelay (1000);
  223. /* check for ctrl-c to abort... */
  224. if (ctrlc()) {
  225. puts("Abort\n");
  226. return 0;
  227. }
  228. }
  229. return 0;
  230. }
  231. #endif /* CONFIG_MX_CYCLIC */
  232. int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  233. {
  234. ulong addr1, addr2, count, ngood;
  235. int size;
  236. int rcode = 0;
  237. if (argc != 4) {
  238. printf ("Usage:\n%s\n", cmdtp->usage);
  239. return 1;
  240. }
  241. /* Check for size specification.
  242. */
  243. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  244. return 1;
  245. addr1 = simple_strtoul(argv[1], NULL, 16);
  246. addr1 += base_address;
  247. addr2 = simple_strtoul(argv[2], NULL, 16);
  248. addr2 += base_address;
  249. count = simple_strtoul(argv[3], NULL, 16);
  250. #ifdef CONFIG_HAS_DATAFLASH
  251. if (addr_dataflash(addr1) | addr_dataflash(addr2)){
  252. puts ("Comparison with DataFlash space not supported.\n\r");
  253. return 0;
  254. }
  255. #endif
  256. #ifdef CONFIG_BLACKFIN
  257. if (addr_bfin_on_chip_mem(addr1) || addr_bfin_on_chip_mem(addr2)) {
  258. puts ("Comparison with L1 instruction memory 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(dest))
  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)
  396. #ifndef CFG_NO_FLASH
  397. && (addr2info(dest) == NULL)
  398. #endif
  399. ){
  400. int rc;
  401. rc = read_dataflash(addr, count * size, (char *) dest);
  402. if (rc != 1) {
  403. dataflash_perror (rc);
  404. return (1);
  405. }
  406. return 0;
  407. }
  408. if (addr_dataflash(addr) && addr_dataflash(dest)){
  409. puts ("Unsupported combination of source/destination.\n\r");
  410. return 1;
  411. }
  412. #endif
  413. #ifdef CONFIG_BLACKFIN
  414. /* See if we're copying to/from L1 inst */
  415. if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
  416. memcpy((void *)dest, (void *)addr, count * size);
  417. return 0;
  418. }
  419. #endif
  420. while (count-- > 0) {
  421. if (size == 4)
  422. *((ulong *)dest) = *((ulong *)addr);
  423. else if (size == 2)
  424. *((ushort *)dest) = *((ushort *)addr);
  425. else
  426. *((u_char *)dest) = *((u_char *)addr);
  427. addr += size;
  428. dest += size;
  429. }
  430. return 0;
  431. }
  432. int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  433. {
  434. if (argc > 1) {
  435. /* Set new base address.
  436. */
  437. base_address = simple_strtoul(argv[1], NULL, 16);
  438. }
  439. /* Print the current base address.
  440. */
  441. printf("Base Address: 0x%08lx\n", base_address);
  442. return 0;
  443. }
  444. int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  445. {
  446. ulong addr, length, i, junk;
  447. int size;
  448. volatile uint *longp;
  449. volatile ushort *shortp;
  450. volatile u_char *cp;
  451. if (argc < 3) {
  452. printf ("Usage:\n%s\n", cmdtp->usage);
  453. return 1;
  454. }
  455. /* Check for a size spefication.
  456. * Defaults to long if no or incorrect specification.
  457. */
  458. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  459. return 1;
  460. /* Address is always specified.
  461. */
  462. addr = simple_strtoul(argv[1], NULL, 16);
  463. /* Length is the number of objects, not number of bytes.
  464. */
  465. length = simple_strtoul(argv[2], NULL, 16);
  466. /* We want to optimize the loops to run as fast as possible.
  467. * If we have only one object, just run infinite loops.
  468. */
  469. if (length == 1) {
  470. if (size == 4) {
  471. longp = (uint *)addr;
  472. for (;;)
  473. i = *longp;
  474. }
  475. if (size == 2) {
  476. shortp = (ushort *)addr;
  477. for (;;)
  478. i = *shortp;
  479. }
  480. cp = (u_char *)addr;
  481. for (;;)
  482. i = *cp;
  483. }
  484. if (size == 4) {
  485. for (;;) {
  486. longp = (uint *)addr;
  487. i = length;
  488. while (i-- > 0)
  489. junk = *longp++;
  490. }
  491. }
  492. if (size == 2) {
  493. for (;;) {
  494. shortp = (ushort *)addr;
  495. i = length;
  496. while (i-- > 0)
  497. junk = *shortp++;
  498. }
  499. }
  500. for (;;) {
  501. cp = (u_char *)addr;
  502. i = length;
  503. while (i-- > 0)
  504. junk = *cp++;
  505. }
  506. }
  507. #ifdef CONFIG_LOOPW
  508. int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  509. {
  510. ulong addr, length, i, data;
  511. int size;
  512. volatile uint *longp;
  513. volatile ushort *shortp;
  514. volatile u_char *cp;
  515. if (argc < 4) {
  516. printf ("Usage:\n%s\n", cmdtp->usage);
  517. return 1;
  518. }
  519. /* Check for a size spefication.
  520. * Defaults to long if no or incorrect specification.
  521. */
  522. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  523. return 1;
  524. /* Address is always specified.
  525. */
  526. addr = simple_strtoul(argv[1], NULL, 16);
  527. /* Length is the number of objects, not number of bytes.
  528. */
  529. length = simple_strtoul(argv[2], NULL, 16);
  530. /* data to write */
  531. data = simple_strtoul(argv[3], NULL, 16);
  532. /* We want to optimize the loops to run as fast as possible.
  533. * If we have only one object, just run infinite loops.
  534. */
  535. if (length == 1) {
  536. if (size == 4) {
  537. longp = (uint *)addr;
  538. for (;;)
  539. *longp = data;
  540. }
  541. if (size == 2) {
  542. shortp = (ushort *)addr;
  543. for (;;)
  544. *shortp = data;
  545. }
  546. cp = (u_char *)addr;
  547. for (;;)
  548. *cp = data;
  549. }
  550. if (size == 4) {
  551. for (;;) {
  552. longp = (uint *)addr;
  553. i = length;
  554. while (i-- > 0)
  555. *longp++ = data;
  556. }
  557. }
  558. if (size == 2) {
  559. for (;;) {
  560. shortp = (ushort *)addr;
  561. i = length;
  562. while (i-- > 0)
  563. *shortp++ = data;
  564. }
  565. }
  566. for (;;) {
  567. cp = (u_char *)addr;
  568. i = length;
  569. while (i-- > 0)
  570. *cp++ = data;
  571. }
  572. }
  573. #endif /* CONFIG_LOOPW */
  574. /*
  575. * Perform a memory test. A more complete alternative test can be
  576. * configured using CFG_ALT_MEMTEST. The complete test loops until
  577. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  578. */
  579. int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  580. {
  581. vu_long *addr, *start, *end;
  582. ulong val;
  583. ulong readback;
  584. int rcode = 0;
  585. #if defined(CFG_ALT_MEMTEST)
  586. vu_long len;
  587. vu_long offset;
  588. vu_long test_offset;
  589. vu_long pattern;
  590. vu_long temp;
  591. vu_long anti_pattern;
  592. vu_long num_words;
  593. #if defined(CFG_MEMTEST_SCRATCH)
  594. vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
  595. #else
  596. vu_long *dummy = 0; /* yes, this is address 0x0, not NULL */
  597. #endif
  598. int j;
  599. int iterations = 1;
  600. static const ulong bitpattern[] = {
  601. 0x00000001, /* single bit */
  602. 0x00000003, /* two adjacent bits */
  603. 0x00000007, /* three adjacent bits */
  604. 0x0000000F, /* four adjacent bits */
  605. 0x00000005, /* two non-adjacent bits */
  606. 0x00000015, /* three non-adjacent bits */
  607. 0x00000055, /* four non-adjacent bits */
  608. 0xaaaaaaaa, /* alternating 1/0 */
  609. };
  610. #else
  611. ulong incr;
  612. ulong pattern;
  613. #endif
  614. if (argc > 1) {
  615. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  616. } else {
  617. start = (ulong *)CFG_MEMTEST_START;
  618. }
  619. if (argc > 2) {
  620. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  621. } else {
  622. end = (ulong *)(CFG_MEMTEST_END);
  623. }
  624. if (argc > 3) {
  625. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  626. } else {
  627. pattern = 0;
  628. }
  629. #if defined(CFG_ALT_MEMTEST)
  630. printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
  631. PRINTF("%s:%d: start 0x%p end 0x%p\n",
  632. __FUNCTION__, __LINE__, start, end);
  633. for (;;) {
  634. if (ctrlc()) {
  635. putc ('\n');
  636. return 1;
  637. }
  638. printf("Iteration: %6d\r", iterations);
  639. PRINTF("Iteration: %6d\n", iterations);
  640. iterations++;
  641. /*
  642. * Data line test: write a pattern to the first
  643. * location, write the 1's complement to a 'parking'
  644. * address (changes the state of the data bus so a
  645. * floating bus doen't give a false OK), and then
  646. * read the value back. Note that we read it back
  647. * into a variable because the next time we read it,
  648. * it might be right (been there, tough to explain to
  649. * the quality guys why it prints a failure when the
  650. * "is" and "should be" are obviously the same in the
  651. * error message).
  652. *
  653. * Rather than exhaustively testing, we test some
  654. * patterns by shifting '1' bits through a field of
  655. * '0's and '0' bits through a field of '1's (i.e.
  656. * pattern and ~pattern).
  657. */
  658. addr = start;
  659. for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
  660. val = bitpattern[j];
  661. for(; val != 0; val <<= 1) {
  662. *addr = val;
  663. *dummy = ~val; /* clear the test data off of the bus */
  664. readback = *addr;
  665. if(readback != val) {
  666. printf ("FAILURE (data line): "
  667. "expected %08lx, actual %08lx\n",
  668. val, readback);
  669. }
  670. *addr = ~val;
  671. *dummy = val;
  672. readback = *addr;
  673. if(readback != ~val) {
  674. printf ("FAILURE (data line): "
  675. "Is %08lx, should be %08lx\n",
  676. readback, ~val);
  677. }
  678. }
  679. }
  680. /*
  681. * Based on code whose Original Author and Copyright
  682. * information follows: Copyright (c) 1998 by Michael
  683. * Barr. This software is placed into the public
  684. * domain and may be used for any purpose. However,
  685. * this notice must not be changed or removed and no
  686. * warranty is either expressed or implied by its
  687. * publication or distribution.
  688. */
  689. /*
  690. * Address line test
  691. *
  692. * Description: Test the address bus wiring in a
  693. * memory region by performing a walking
  694. * 1's test on the relevant bits of the
  695. * address and checking for aliasing.
  696. * This test will find single-bit
  697. * address failures such as stuck -high,
  698. * stuck-low, and shorted pins. The base
  699. * address and size of the region are
  700. * selected by the caller.
  701. *
  702. * Notes: For best results, the selected base
  703. * address should have enough LSB 0's to
  704. * guarantee single address bit changes.
  705. * For example, to test a 64-Kbyte
  706. * region, select a base address on a
  707. * 64-Kbyte boundary. Also, select the
  708. * region size as a power-of-two if at
  709. * all possible.
  710. *
  711. * Returns: 0 if the test succeeds, 1 if the test fails.
  712. */
  713. len = ((ulong)end - (ulong)start)/sizeof(vu_long);
  714. pattern = (vu_long) 0xaaaaaaaa;
  715. anti_pattern = (vu_long) 0x55555555;
  716. PRINTF("%s:%d: length = 0x%.8lx\n",
  717. __FUNCTION__, __LINE__,
  718. len);
  719. /*
  720. * Write the default pattern at each of the
  721. * power-of-two offsets.
  722. */
  723. for (offset = 1; offset < len; offset <<= 1) {
  724. start[offset] = pattern;
  725. }
  726. /*
  727. * Check for address bits stuck high.
  728. */
  729. test_offset = 0;
  730. start[test_offset] = anti_pattern;
  731. for (offset = 1; offset < len; offset <<= 1) {
  732. temp = start[offset];
  733. if (temp != pattern) {
  734. printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
  735. " expected 0x%.8lx, actual 0x%.8lx\n",
  736. (ulong)&start[offset], pattern, temp);
  737. return 1;
  738. }
  739. }
  740. start[test_offset] = pattern;
  741. WATCHDOG_RESET();
  742. /*
  743. * Check for addr bits stuck low or shorted.
  744. */
  745. for (test_offset = 1; test_offset < len; test_offset <<= 1) {
  746. start[test_offset] = anti_pattern;
  747. for (offset = 1; offset < len; offset <<= 1) {
  748. temp = start[offset];
  749. if ((temp != pattern) && (offset != test_offset)) {
  750. printf ("\nFAILURE: Address bit stuck low or shorted @"
  751. " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
  752. (ulong)&start[offset], pattern, temp);
  753. return 1;
  754. }
  755. }
  756. start[test_offset] = pattern;
  757. }
  758. /*
  759. * Description: Test the integrity of a physical
  760. * memory device by performing an
  761. * increment/decrement test over the
  762. * entire region. In the process every
  763. * storage bit in the device is tested
  764. * as a zero and a one. The base address
  765. * and the size of the region are
  766. * selected by the caller.
  767. *
  768. * Returns: 0 if the test succeeds, 1 if the test fails.
  769. */
  770. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  771. /*
  772. * Fill memory with a known pattern.
  773. */
  774. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  775. WATCHDOG_RESET();
  776. start[offset] = pattern;
  777. }
  778. /*
  779. * Check each location and invert it for the second pass.
  780. */
  781. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  782. WATCHDOG_RESET();
  783. temp = start[offset];
  784. if (temp != pattern) {
  785. printf ("\nFAILURE (read/write) @ 0x%.8lx:"
  786. " expected 0x%.8lx, actual 0x%.8lx)\n",
  787. (ulong)&start[offset], pattern, temp);
  788. return 1;
  789. }
  790. anti_pattern = ~pattern;
  791. start[offset] = anti_pattern;
  792. }
  793. /*
  794. * Check each location for the inverted pattern and zero it.
  795. */
  796. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  797. WATCHDOG_RESET();
  798. anti_pattern = ~pattern;
  799. temp = start[offset];
  800. if (temp != anti_pattern) {
  801. printf ("\nFAILURE (read/write): @ 0x%.8lx:"
  802. " expected 0x%.8lx, actual 0x%.8lx)\n",
  803. (ulong)&start[offset], anti_pattern, temp);
  804. return 1;
  805. }
  806. start[offset] = 0;
  807. }
  808. }
  809. #else /* The original, quickie test */
  810. incr = 1;
  811. for (;;) {
  812. if (ctrlc()) {
  813. putc ('\n');
  814. return 1;
  815. }
  816. printf ("\rPattern %08lX Writing..."
  817. "%12s"
  818. "\b\b\b\b\b\b\b\b\b\b",
  819. pattern, "");
  820. for (addr=start,val=pattern; addr<end; addr++) {
  821. WATCHDOG_RESET();
  822. *addr = val;
  823. val += incr;
  824. }
  825. puts ("Reading...");
  826. for (addr=start,val=pattern; addr<end; addr++) {
  827. WATCHDOG_RESET();
  828. readback = *addr;
  829. if (readback != val) {
  830. printf ("\nMem error @ 0x%08X: "
  831. "found %08lX, expected %08lX\n",
  832. (uint)addr, readback, val);
  833. rcode = 1;
  834. }
  835. val += incr;
  836. }
  837. /*
  838. * Flip the pattern each time to make lots of zeros and
  839. * then, the next time, lots of ones. We decrement
  840. * the "negative" patterns and increment the "positive"
  841. * patterns to preserve this feature.
  842. */
  843. if(pattern & 0x80000000) {
  844. pattern = -pattern; /* complement & increment */
  845. }
  846. else {
  847. pattern = ~pattern;
  848. }
  849. incr = -incr;
  850. }
  851. #endif
  852. return rcode;
  853. }
  854. /* Modify memory.
  855. *
  856. * Syntax:
  857. * mm{.b, .w, .l} {addr}
  858. * nm{.b, .w, .l} {addr}
  859. */
  860. static int
  861. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  862. {
  863. ulong addr, i;
  864. int nbytes, size;
  865. extern char console_buffer[];
  866. if (argc != 2) {
  867. printf ("Usage:\n%s\n", cmdtp->usage);
  868. return 1;
  869. }
  870. #ifdef CONFIG_BOOT_RETRY_TIME
  871. reset_cmd_timeout(); /* got a good command to get here */
  872. #endif
  873. /* We use the last specified parameters, unless new ones are
  874. * entered.
  875. */
  876. addr = mm_last_addr;
  877. size = mm_last_size;
  878. if ((flag & CMD_FLAG_REPEAT) == 0) {
  879. /* New command specified. Check for a size specification.
  880. * Defaults to long if no or incorrect specification.
  881. */
  882. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  883. return 1;
  884. /* Address is specified since argc > 1
  885. */
  886. addr = simple_strtoul(argv[1], NULL, 16);
  887. addr += base_address;
  888. }
  889. #ifdef CONFIG_HAS_DATAFLASH
  890. if (addr_dataflash(addr)){
  891. puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
  892. return 0;
  893. }
  894. #endif
  895. #ifdef CONFIG_BLACKFIN
  896. if (addr_bfin_on_chip_mem(addr)) {
  897. puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
  898. return 0;
  899. }
  900. #endif
  901. /* Print the address, followed by value. Then accept input for
  902. * the next value. A non-converted value exits.
  903. */
  904. do {
  905. printf("%08lx:", addr);
  906. if (size == 4)
  907. printf(" %08x", *((uint *)addr));
  908. else if (size == 2)
  909. printf(" %04x", *((ushort *)addr));
  910. else
  911. printf(" %02x", *((u_char *)addr));
  912. nbytes = readline (" ? ");
  913. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  914. /* <CR> pressed as only input, don't modify current
  915. * location and move to next. "-" pressed will go back.
  916. */
  917. if (incrflag)
  918. addr += nbytes ? -size : size;
  919. nbytes = 1;
  920. #ifdef CONFIG_BOOT_RETRY_TIME
  921. reset_cmd_timeout(); /* good enough to not time out */
  922. #endif
  923. }
  924. #ifdef CONFIG_BOOT_RETRY_TIME
  925. else if (nbytes == -2) {
  926. break; /* timed out, exit the command */
  927. }
  928. #endif
  929. else {
  930. char *endp;
  931. i = simple_strtoul(console_buffer, &endp, 16);
  932. nbytes = endp - console_buffer;
  933. if (nbytes) {
  934. #ifdef CONFIG_BOOT_RETRY_TIME
  935. /* good enough to not time out
  936. */
  937. reset_cmd_timeout();
  938. #endif
  939. if (size == 4)
  940. *((uint *)addr) = i;
  941. else if (size == 2)
  942. *((ushort *)addr) = i;
  943. else
  944. *((u_char *)addr) = i;
  945. if (incrflag)
  946. addr += size;
  947. }
  948. }
  949. } while (nbytes);
  950. mm_last_addr = addr;
  951. mm_last_size = size;
  952. return 0;
  953. }
  954. #ifndef CONFIG_CRC32_VERIFY
  955. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  956. {
  957. ulong addr, length;
  958. ulong crc;
  959. ulong *ptr;
  960. if (argc < 3) {
  961. printf ("Usage:\n%s\n", cmdtp->usage);
  962. return 1;
  963. }
  964. addr = simple_strtoul (argv[1], NULL, 16);
  965. addr += base_address;
  966. length = simple_strtoul (argv[2], NULL, 16);
  967. crc = crc32 (0, (const uchar *) addr, length);
  968. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  969. addr, addr + length - 1, crc);
  970. if (argc > 3) {
  971. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  972. *ptr = crc;
  973. }
  974. return 0;
  975. }
  976. #else /* CONFIG_CRC32_VERIFY */
  977. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  978. {
  979. ulong addr, length;
  980. ulong crc;
  981. ulong *ptr;
  982. ulong vcrc;
  983. int verify;
  984. int ac;
  985. char **av;
  986. if (argc < 3) {
  987. usage:
  988. printf ("Usage:\n%s\n", cmdtp->usage);
  989. return 1;
  990. }
  991. av = argv + 1;
  992. ac = argc - 1;
  993. if (strcmp(*av, "-v") == 0) {
  994. verify = 1;
  995. av++;
  996. ac--;
  997. if (ac < 3)
  998. goto usage;
  999. } else
  1000. verify = 0;
  1001. addr = simple_strtoul(*av++, NULL, 16);
  1002. addr += base_address;
  1003. length = simple_strtoul(*av++, NULL, 16);
  1004. crc = crc32(0, (const uchar *) addr, length);
  1005. if (!verify) {
  1006. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  1007. addr, addr + length - 1, crc);
  1008. if (ac > 2) {
  1009. ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
  1010. *ptr = crc;
  1011. }
  1012. } else {
  1013. vcrc = simple_strtoul(*av++, NULL, 16);
  1014. if (vcrc != crc) {
  1015. printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
  1016. addr, addr + length - 1, crc, vcrc);
  1017. return 1;
  1018. }
  1019. }
  1020. return 0;
  1021. }
  1022. #endif /* CONFIG_CRC32_VERIFY */
  1023. #ifdef CONFIG_CMD_UNZIP
  1024. int gunzip (void *, int, unsigned char *, unsigned long *);
  1025. int do_unzip ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1026. {
  1027. unsigned long src, dst;
  1028. unsigned long src_len = ~0UL, dst_len = ~0UL;
  1029. int err;
  1030. switch (argc) {
  1031. case 4:
  1032. dst_len = simple_strtoul(argv[3], NULL, 16);
  1033. /* fall through */
  1034. case 3:
  1035. src = simple_strtoul(argv[1], NULL, 16);
  1036. dst = simple_strtoul(argv[2], NULL, 16);
  1037. break;
  1038. default:
  1039. printf ("Usage:\n%s\n", cmdtp->usage);
  1040. return 1;
  1041. }
  1042. return !!gunzip((void *) dst, dst_len, (void *) src, &src_len);
  1043. }
  1044. #endif /* CONFIG_CMD_UNZIP */
  1045. /**************************************************/
  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. #ifdef CONFIG_CMD_UNZIP
  1129. U_BOOT_CMD(
  1130. unzip, 4, 1, do_unzip,
  1131. "unzip - unzip a memory region\n",
  1132. "srcaddr dstaddr [dstsize]\n"
  1133. );
  1134. #endif /* CONFIG_CMD_UNZIP */
  1135. #endif