cmd_mem.c 29 KB

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