cmd_mem.c 30 KB

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