cmd_mem.c 29 KB

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