cmd_mem.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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(addr))
  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) && (addr2info(dest)==NULL) ){
  422. int rc;
  423. rc = read_dataflash(addr, count * size, (char *) dest);
  424. if (rc != 1) {
  425. dataflash_perror (rc);
  426. return (1);
  427. }
  428. return 0;
  429. }
  430. if (addr_dataflash(addr) && addr_dataflash(dest)){
  431. puts ("Unsupported combination of source/destination.\n\r");
  432. return 1;
  433. }
  434. #endif
  435. #ifdef CONFIG_BLACKFIN
  436. /* See if we're copying to/from L1 inst */
  437. if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
  438. memcpy((void *)dest, (void *)addr, count * size);
  439. return 0;
  440. }
  441. #endif
  442. while (count-- > 0) {
  443. if (size == 4)
  444. *((ulong *)dest) = *((ulong *)addr);
  445. else if (size == 2)
  446. *((ushort *)dest) = *((ushort *)addr);
  447. else
  448. *((u_char *)dest) = *((u_char *)addr);
  449. addr += size;
  450. dest += size;
  451. }
  452. return 0;
  453. }
  454. int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  455. {
  456. if (argc > 1) {
  457. /* Set new base address.
  458. */
  459. base_address = simple_strtoul(argv[1], NULL, 16);
  460. }
  461. /* Print the current base address.
  462. */
  463. printf("Base Address: 0x%08lx\n", base_address);
  464. return 0;
  465. }
  466. int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  467. {
  468. ulong addr, length, i, junk;
  469. int size;
  470. volatile uint *longp;
  471. volatile ushort *shortp;
  472. volatile u_char *cp;
  473. if (argc < 3) {
  474. printf ("Usage:\n%s\n", cmdtp->usage);
  475. return 1;
  476. }
  477. /* Check for a size spefication.
  478. * Defaults to long if no or incorrect specification.
  479. */
  480. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  481. return 1;
  482. /* Address is always specified.
  483. */
  484. addr = simple_strtoul(argv[1], NULL, 16);
  485. /* Length is the number of objects, not number of bytes.
  486. */
  487. length = simple_strtoul(argv[2], NULL, 16);
  488. /* We want to optimize the loops to run as fast as possible.
  489. * If we have only one object, just run infinite loops.
  490. */
  491. if (length == 1) {
  492. if (size == 4) {
  493. longp = (uint *)addr;
  494. for (;;)
  495. i = *longp;
  496. }
  497. if (size == 2) {
  498. shortp = (ushort *)addr;
  499. for (;;)
  500. i = *shortp;
  501. }
  502. cp = (u_char *)addr;
  503. for (;;)
  504. i = *cp;
  505. }
  506. if (size == 4) {
  507. for (;;) {
  508. longp = (uint *)addr;
  509. i = length;
  510. while (i-- > 0)
  511. junk = *longp++;
  512. }
  513. }
  514. if (size == 2) {
  515. for (;;) {
  516. shortp = (ushort *)addr;
  517. i = length;
  518. while (i-- > 0)
  519. junk = *shortp++;
  520. }
  521. }
  522. for (;;) {
  523. cp = (u_char *)addr;
  524. i = length;
  525. while (i-- > 0)
  526. junk = *cp++;
  527. }
  528. }
  529. #ifdef CONFIG_LOOPW
  530. int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  531. {
  532. ulong addr, length, i, data;
  533. int size;
  534. volatile uint *longp;
  535. volatile ushort *shortp;
  536. volatile u_char *cp;
  537. if (argc < 4) {
  538. printf ("Usage:\n%s\n", cmdtp->usage);
  539. return 1;
  540. }
  541. /* Check for a size spefication.
  542. * Defaults to long if no or incorrect specification.
  543. */
  544. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  545. return 1;
  546. /* Address is always specified.
  547. */
  548. addr = simple_strtoul(argv[1], NULL, 16);
  549. /* Length is the number of objects, not number of bytes.
  550. */
  551. length = simple_strtoul(argv[2], NULL, 16);
  552. /* data to write */
  553. data = simple_strtoul(argv[3], NULL, 16);
  554. /* We want to optimize the loops to run as fast as possible.
  555. * If we have only one object, just run infinite loops.
  556. */
  557. if (length == 1) {
  558. if (size == 4) {
  559. longp = (uint *)addr;
  560. for (;;)
  561. *longp = data;
  562. }
  563. if (size == 2) {
  564. shortp = (ushort *)addr;
  565. for (;;)
  566. *shortp = data;
  567. }
  568. cp = (u_char *)addr;
  569. for (;;)
  570. *cp = data;
  571. }
  572. if (size == 4) {
  573. for (;;) {
  574. longp = (uint *)addr;
  575. i = length;
  576. while (i-- > 0)
  577. *longp++ = data;
  578. }
  579. }
  580. if (size == 2) {
  581. for (;;) {
  582. shortp = (ushort *)addr;
  583. i = length;
  584. while (i-- > 0)
  585. *shortp++ = data;
  586. }
  587. }
  588. for (;;) {
  589. cp = (u_char *)addr;
  590. i = length;
  591. while (i-- > 0)
  592. *cp++ = data;
  593. }
  594. }
  595. #endif /* CONFIG_LOOPW */
  596. /*
  597. * Perform a memory test. A more complete alternative test can be
  598. * configured using CFG_ALT_MEMTEST. The complete test loops until
  599. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  600. */
  601. int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  602. {
  603. vu_long *addr, *start, *end;
  604. ulong val;
  605. ulong readback;
  606. int rcode = 0;
  607. #if defined(CFG_ALT_MEMTEST)
  608. vu_long addr_mask;
  609. vu_long offset;
  610. vu_long test_offset;
  611. vu_long pattern;
  612. vu_long temp;
  613. vu_long anti_pattern;
  614. vu_long num_words;
  615. #if defined(CFG_MEMTEST_SCRATCH)
  616. vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
  617. #else
  618. vu_long *dummy = 0; /* yes, this is address 0x0, not NULL */
  619. #endif
  620. int j;
  621. int iterations = 1;
  622. static const ulong bitpattern[] = {
  623. 0x00000001, /* single bit */
  624. 0x00000003, /* two adjacent bits */
  625. 0x00000007, /* three adjacent bits */
  626. 0x0000000F, /* four adjacent bits */
  627. 0x00000005, /* two non-adjacent bits */
  628. 0x00000015, /* three non-adjacent bits */
  629. 0x00000055, /* four non-adjacent bits */
  630. 0xaaaaaaaa, /* alternating 1/0 */
  631. };
  632. #else
  633. ulong incr;
  634. ulong pattern;
  635. #endif
  636. if (argc > 1) {
  637. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  638. } else {
  639. start = (ulong *)CFG_MEMTEST_START;
  640. }
  641. if (argc > 2) {
  642. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  643. } else {
  644. end = (ulong *)(CFG_MEMTEST_END);
  645. }
  646. if (argc > 3) {
  647. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  648. } else {
  649. pattern = 0;
  650. }
  651. #if defined(CFG_ALT_MEMTEST)
  652. printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
  653. PRINTF("%s:%d: start 0x%p end 0x%p\n",
  654. __FUNCTION__, __LINE__, start, end);
  655. for (;;) {
  656. if (ctrlc()) {
  657. putc ('\n');
  658. return 1;
  659. }
  660. printf("Iteration: %6d\r", iterations);
  661. PRINTF("Iteration: %6d\n", iterations);
  662. iterations++;
  663. /*
  664. * Data line test: write a pattern to the first
  665. * location, write the 1's complement to a 'parking'
  666. * address (changes the state of the data bus so a
  667. * floating bus doen't give a false OK), and then
  668. * read the value back. Note that we read it back
  669. * into a variable because the next time we read it,
  670. * it might be right (been there, tough to explain to
  671. * the quality guys why it prints a failure when the
  672. * "is" and "should be" are obviously the same in the
  673. * error message).
  674. *
  675. * Rather than exhaustively testing, we test some
  676. * patterns by shifting '1' bits through a field of
  677. * '0's and '0' bits through a field of '1's (i.e.
  678. * pattern and ~pattern).
  679. */
  680. addr = start;
  681. for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
  682. val = bitpattern[j];
  683. for(; val != 0; val <<= 1) {
  684. *addr = val;
  685. *dummy = ~val; /* clear the test data off of the bus */
  686. readback = *addr;
  687. if(readback != val) {
  688. printf ("FAILURE (data line): "
  689. "expected %08lx, actual %08lx\n",
  690. val, readback);
  691. }
  692. *addr = ~val;
  693. *dummy = val;
  694. readback = *addr;
  695. if(readback != ~val) {
  696. printf ("FAILURE (data line): "
  697. "Is %08lx, should be %08lx\n",
  698. readback, ~val);
  699. }
  700. }
  701. }
  702. /*
  703. * Based on code whose Original Author and Copyright
  704. * information follows: Copyright (c) 1998 by Michael
  705. * Barr. This software is placed into the public
  706. * domain and may be used for any purpose. However,
  707. * this notice must not be changed or removed and no
  708. * warranty is either expressed or implied by its
  709. * publication or distribution.
  710. */
  711. /*
  712. * Address line test
  713. *
  714. * Description: Test the address bus wiring in a
  715. * memory region by performing a walking
  716. * 1's test on the relevant bits of the
  717. * address and checking for aliasing.
  718. * This test will find single-bit
  719. * address failures such as stuck -high,
  720. * stuck-low, and shorted pins. The base
  721. * address and size of the region are
  722. * selected by the caller.
  723. *
  724. * Notes: For best results, the selected base
  725. * address should have enough LSB 0's to
  726. * guarantee single address bit changes.
  727. * For example, to test a 64-Kbyte
  728. * region, select a base address on a
  729. * 64-Kbyte boundary. Also, select the
  730. * region size as a power-of-two if at
  731. * all possible.
  732. *
  733. * Returns: 0 if the test succeeds, 1 if the test fails.
  734. *
  735. * ## NOTE ## Be sure to specify start and end
  736. * addresses such that addr_mask has
  737. * lots of bits set. For example an
  738. * address range of 01000000 02000000 is
  739. * bad while a range of 01000000
  740. * 01ffffff is perfect.
  741. */
  742. addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
  743. pattern = (vu_long) 0xaaaaaaaa;
  744. anti_pattern = (vu_long) 0x55555555;
  745. PRINTF("%s:%d: addr mask = 0x%.8lx\n",
  746. __FUNCTION__, __LINE__,
  747. addr_mask);
  748. /*
  749. * Write the default pattern at each of the
  750. * power-of-two offsets.
  751. */
  752. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  753. start[offset] = pattern;
  754. }
  755. /*
  756. * Check for address bits stuck high.
  757. */
  758. test_offset = 0;
  759. start[test_offset] = anti_pattern;
  760. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  761. temp = start[offset];
  762. if (temp != pattern) {
  763. printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
  764. " expected 0x%.8lx, actual 0x%.8lx\n",
  765. (ulong)&start[offset], pattern, temp);
  766. return 1;
  767. }
  768. }
  769. start[test_offset] = pattern;
  770. /*
  771. * Check for addr bits stuck low or shorted.
  772. */
  773. for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
  774. start[test_offset] = anti_pattern;
  775. for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
  776. temp = start[offset];
  777. if ((temp != pattern) && (offset != test_offset)) {
  778. printf ("\nFAILURE: Address bit stuck low or shorted @"
  779. " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
  780. (ulong)&start[offset], pattern, temp);
  781. return 1;
  782. }
  783. }
  784. start[test_offset] = pattern;
  785. }
  786. /*
  787. * Description: Test the integrity of a physical
  788. * memory device by performing an
  789. * increment/decrement test over the
  790. * entire region. In the process every
  791. * storage bit in the device is tested
  792. * as a zero and a one. The base address
  793. * and the size of the region are
  794. * selected by the caller.
  795. *
  796. * Returns: 0 if the test succeeds, 1 if the test fails.
  797. */
  798. num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  799. /*
  800. * Fill memory with a known pattern.
  801. */
  802. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  803. start[offset] = pattern;
  804. }
  805. /*
  806. * Check each location and invert it for the second pass.
  807. */
  808. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  809. temp = start[offset];
  810. if (temp != pattern) {
  811. printf ("\nFAILURE (read/write) @ 0x%.8lx:"
  812. " expected 0x%.8lx, actual 0x%.8lx)\n",
  813. (ulong)&start[offset], pattern, temp);
  814. return 1;
  815. }
  816. anti_pattern = ~pattern;
  817. start[offset] = anti_pattern;
  818. }
  819. /*
  820. * Check each location for the inverted pattern and zero it.
  821. */
  822. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  823. anti_pattern = ~pattern;
  824. temp = start[offset];
  825. if (temp != anti_pattern) {
  826. printf ("\nFAILURE (read/write): @ 0x%.8lx:"
  827. " expected 0x%.8lx, actual 0x%.8lx)\n",
  828. (ulong)&start[offset], anti_pattern, temp);
  829. return 1;
  830. }
  831. start[offset] = 0;
  832. }
  833. }
  834. #else /* The original, quickie test */
  835. incr = 1;
  836. for (;;) {
  837. if (ctrlc()) {
  838. putc ('\n');
  839. return 1;
  840. }
  841. printf ("\rPattern %08lX Writing..."
  842. "%12s"
  843. "\b\b\b\b\b\b\b\b\b\b",
  844. pattern, "");
  845. for (addr=start,val=pattern; addr<end; addr++) {
  846. *addr = val;
  847. val += incr;
  848. }
  849. puts ("Reading...");
  850. for (addr=start,val=pattern; addr<end; addr++) {
  851. readback = *addr;
  852. if (readback != val) {
  853. printf ("\nMem error @ 0x%08X: "
  854. "found %08lX, expected %08lX\n",
  855. (uint)addr, readback, val);
  856. rcode = 1;
  857. }
  858. val += incr;
  859. }
  860. /*
  861. * Flip the pattern each time to make lots of zeros and
  862. * then, the next time, lots of ones. We decrement
  863. * the "negative" patterns and increment the "positive"
  864. * patterns to preserve this feature.
  865. */
  866. if(pattern & 0x80000000) {
  867. pattern = -pattern; /* complement & increment */
  868. }
  869. else {
  870. pattern = ~pattern;
  871. }
  872. incr = -incr;
  873. }
  874. #endif
  875. return rcode;
  876. }
  877. /* Modify memory.
  878. *
  879. * Syntax:
  880. * mm{.b, .w, .l} {addr}
  881. * nm{.b, .w, .l} {addr}
  882. */
  883. static int
  884. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  885. {
  886. ulong addr, i;
  887. int nbytes, size;
  888. extern char console_buffer[];
  889. if (argc != 2) {
  890. printf ("Usage:\n%s\n", cmdtp->usage);
  891. return 1;
  892. }
  893. #ifdef CONFIG_BOOT_RETRY_TIME
  894. reset_cmd_timeout(); /* got a good command to get here */
  895. #endif
  896. /* We use the last specified parameters, unless new ones are
  897. * entered.
  898. */
  899. addr = mm_last_addr;
  900. size = mm_last_size;
  901. if ((flag & CMD_FLAG_REPEAT) == 0) {
  902. /* New command specified. Check for a size specification.
  903. * Defaults to long if no or incorrect specification.
  904. */
  905. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  906. return 1;
  907. /* Address is specified since argc > 1
  908. */
  909. addr = simple_strtoul(argv[1], NULL, 16);
  910. addr += base_address;
  911. }
  912. #ifdef CONFIG_HAS_DATAFLASH
  913. if (addr_dataflash(addr)){
  914. puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
  915. return 0;
  916. }
  917. #endif
  918. #ifdef CONFIG_BLACKFIN
  919. if (addr_bfin_on_chip_mem(addr)) {
  920. puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
  921. return 0;
  922. }
  923. #endif
  924. /* Print the address, followed by value. Then accept input for
  925. * the next value. A non-converted value exits.
  926. */
  927. do {
  928. printf("%08lx:", addr);
  929. if (size == 4)
  930. printf(" %08x", *((uint *)addr));
  931. else if (size == 2)
  932. printf(" %04x", *((ushort *)addr));
  933. else
  934. printf(" %02x", *((u_char *)addr));
  935. nbytes = readline (" ? ");
  936. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  937. /* <CR> pressed as only input, don't modify current
  938. * location and move to next. "-" pressed will go back.
  939. */
  940. if (incrflag)
  941. addr += nbytes ? -size : size;
  942. nbytes = 1;
  943. #ifdef CONFIG_BOOT_RETRY_TIME
  944. reset_cmd_timeout(); /* good enough to not time out */
  945. #endif
  946. }
  947. #ifdef CONFIG_BOOT_RETRY_TIME
  948. else if (nbytes == -2) {
  949. break; /* timed out, exit the command */
  950. }
  951. #endif
  952. else {
  953. char *endp;
  954. i = simple_strtoul(console_buffer, &endp, 16);
  955. nbytes = endp - console_buffer;
  956. if (nbytes) {
  957. #ifdef CONFIG_BOOT_RETRY_TIME
  958. /* good enough to not time out
  959. */
  960. reset_cmd_timeout();
  961. #endif
  962. if (size == 4)
  963. *((uint *)addr) = i;
  964. else if (size == 2)
  965. *((ushort *)addr) = i;
  966. else
  967. *((u_char *)addr) = i;
  968. if (incrflag)
  969. addr += size;
  970. }
  971. }
  972. } while (nbytes);
  973. mm_last_addr = addr;
  974. mm_last_size = size;
  975. return 0;
  976. }
  977. #ifndef CONFIG_CRC32_VERIFY
  978. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  979. {
  980. ulong addr, length;
  981. ulong crc;
  982. ulong *ptr;
  983. if (argc < 3) {
  984. printf ("Usage:\n%s\n", cmdtp->usage);
  985. return 1;
  986. }
  987. addr = simple_strtoul (argv[1], NULL, 16);
  988. addr += base_address;
  989. length = simple_strtoul (argv[2], NULL, 16);
  990. crc = crc32 (0, (const uchar *) addr, length);
  991. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  992. addr, addr + length - 1, crc);
  993. if (argc > 3) {
  994. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  995. *ptr = crc;
  996. }
  997. return 0;
  998. }
  999. #else /* CONFIG_CRC32_VERIFY */
  1000. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  1001. {
  1002. ulong addr, length;
  1003. ulong crc;
  1004. ulong *ptr;
  1005. ulong vcrc;
  1006. int verify;
  1007. int ac;
  1008. char **av;
  1009. if (argc < 3) {
  1010. usage:
  1011. printf ("Usage:\n%s\n", cmdtp->usage);
  1012. return 1;
  1013. }
  1014. av = argv + 1;
  1015. ac = argc - 1;
  1016. if (strcmp(*av, "-v") == 0) {
  1017. verify = 1;
  1018. av++;
  1019. ac--;
  1020. if (ac < 3)
  1021. goto usage;
  1022. } else
  1023. verify = 0;
  1024. addr = simple_strtoul(*av++, NULL, 16);
  1025. addr += base_address;
  1026. length = simple_strtoul(*av++, NULL, 16);
  1027. crc = crc32(0, (const uchar *) addr, length);
  1028. if (!verify) {
  1029. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  1030. addr, addr + length - 1, crc);
  1031. if (ac > 2) {
  1032. ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
  1033. *ptr = crc;
  1034. }
  1035. } else {
  1036. vcrc = simple_strtoul(*av++, NULL, 16);
  1037. if (vcrc != crc) {
  1038. printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
  1039. addr, addr + length - 1, crc, vcrc);
  1040. return 1;
  1041. }
  1042. }
  1043. return 0;
  1044. }
  1045. #endif /* CONFIG_CRC32_VERIFY */
  1046. /**************************************************/
  1047. #if defined(CONFIG_CMD_MEMORY)
  1048. U_BOOT_CMD(
  1049. md, 3, 1, do_mem_md,
  1050. "md - memory display\n",
  1051. "[.b, .w, .l] address [# of objects]\n - memory display\n"
  1052. );
  1053. U_BOOT_CMD(
  1054. mm, 2, 1, do_mem_mm,
  1055. "mm - memory modify (auto-incrementing)\n",
  1056. "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
  1057. );
  1058. U_BOOT_CMD(
  1059. nm, 2, 1, do_mem_nm,
  1060. "nm - memory modify (constant address)\n",
  1061. "[.b, .w, .l] address\n - memory modify, read and keep address\n"
  1062. );
  1063. U_BOOT_CMD(
  1064. mw, 4, 1, do_mem_mw,
  1065. "mw - memory write (fill)\n",
  1066. "[.b, .w, .l] address value [count]\n - write memory\n"
  1067. );
  1068. U_BOOT_CMD(
  1069. cp, 4, 1, do_mem_cp,
  1070. "cp - memory copy\n",
  1071. "[.b, .w, .l] source target count\n - copy memory\n"
  1072. );
  1073. U_BOOT_CMD(
  1074. cmp, 4, 1, do_mem_cmp,
  1075. "cmp - memory compare\n",
  1076. "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
  1077. );
  1078. #ifndef CONFIG_CRC32_VERIFY
  1079. U_BOOT_CMD(
  1080. crc32, 4, 1, do_mem_crc,
  1081. "crc32 - checksum calculation\n",
  1082. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1083. );
  1084. #else /* CONFIG_CRC32_VERIFY */
  1085. U_BOOT_CMD(
  1086. crc32, 5, 1, do_mem_crc,
  1087. "crc32 - checksum calculation\n",
  1088. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1089. "-v address count crc\n - verify crc of memory area\n"
  1090. );
  1091. #endif /* CONFIG_CRC32_VERIFY */
  1092. U_BOOT_CMD(
  1093. base, 2, 1, do_mem_base,
  1094. "base - print or set address offset\n",
  1095. "\n - print address offset for memory commands\n"
  1096. "base off\n - set address offset for memory commands to 'off'\n"
  1097. );
  1098. U_BOOT_CMD(
  1099. loop, 3, 1, do_mem_loop,
  1100. "loop - infinite loop on address range\n",
  1101. "[.b, .w, .l] address number_of_objects\n"
  1102. " - loop on a set of addresses\n"
  1103. );
  1104. #ifdef CONFIG_LOOPW
  1105. U_BOOT_CMD(
  1106. loopw, 4, 1, do_mem_loopw,
  1107. "loopw - infinite write loop on address range\n",
  1108. "[.b, .w, .l] address number_of_objects data_to_write\n"
  1109. " - loop on a set of addresses\n"
  1110. );
  1111. #endif /* CONFIG_LOOPW */
  1112. U_BOOT_CMD(
  1113. mtest, 4, 1, do_mem_mtest,
  1114. "mtest - simple RAM test\n",
  1115. "[start [end [pattern]]]\n"
  1116. " - simple RAM read/write test\n"
  1117. );
  1118. #ifdef CONFIG_MX_CYCLIC
  1119. U_BOOT_CMD(
  1120. mdc, 4, 1, do_mem_mdc,
  1121. "mdc - memory display cyclic\n",
  1122. "[.b, .w, .l] address count delay(ms)\n - memory display cyclic\n"
  1123. );
  1124. U_BOOT_CMD(
  1125. mwc, 4, 1, do_mem_mwc,
  1126. "mwc - memory write cyclic\n",
  1127. "[.b, .w, .l] address value delay(ms)\n - memory write cyclic\n"
  1128. );
  1129. #endif /* CONFIG_MX_CYCLIC */
  1130. #endif
  1131. #endif