cmd_mem.c 28 KB

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