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