cmd_mem.c 28 KB

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