cmd_mem.c 28 KB

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