cmd_mem.c 27 KB

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