cmd_mem.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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 ulong mem_test_alt(vu_long *start, vu_long *end)
  537. {
  538. vu_long *addr;
  539. ulong errs = 0;
  540. ulong val, readback;
  541. int j;
  542. vu_long len;
  543. vu_long offset;
  544. vu_long test_offset;
  545. vu_long pattern;
  546. vu_long temp;
  547. vu_long anti_pattern;
  548. vu_long num_words;
  549. #if defined(CONFIG_SYS_MEMTEST_SCRATCH)
  550. vu_long *dummy = (vu_long *)CONFIG_SYS_MEMTEST_SCRATCH;
  551. #else
  552. vu_long *dummy = NULL; /* yes, this is address 0x0, not NULL */
  553. #endif
  554. static const ulong bitpattern[] = {
  555. 0x00000001, /* single bit */
  556. 0x00000003, /* two adjacent bits */
  557. 0x00000007, /* three adjacent bits */
  558. 0x0000000F, /* four adjacent bits */
  559. 0x00000005, /* two non-adjacent bits */
  560. 0x00000015, /* three non-adjacent bits */
  561. 0x00000055, /* four non-adjacent bits */
  562. 0xaaaaaaaa, /* alternating 1/0 */
  563. };
  564. /*
  565. * Data line test: write a pattern to the first
  566. * location, write the 1's complement to a 'parking'
  567. * address (changes the state of the data bus so a
  568. * floating bus doesn't give a false OK), and then
  569. * read the value back. Note that we read it back
  570. * into a variable because the next time we read it,
  571. * it might be right (been there, tough to explain to
  572. * the quality guys why it prints a failure when the
  573. * "is" and "should be" are obviously the same in the
  574. * error message).
  575. *
  576. * Rather than exhaustively testing, we test some
  577. * patterns by shifting '1' bits through a field of
  578. * '0's and '0' bits through a field of '1's (i.e.
  579. * pattern and ~pattern).
  580. */
  581. addr = start;
  582. for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
  583. val = bitpattern[j];
  584. for (; val != 0; val <<= 1) {
  585. *addr = val;
  586. *dummy = ~val; /* clear the test data off the bus */
  587. readback = *addr;
  588. if (readback != val) {
  589. printf("FAILURE (data line): "
  590. "expected %08lx, actual %08lx\n",
  591. val, readback);
  592. errs++;
  593. if (ctrlc())
  594. return -1;
  595. }
  596. *addr = ~val;
  597. *dummy = val;
  598. readback = *addr;
  599. if (readback != ~val) {
  600. printf("FAILURE (data line): "
  601. "Is %08lx, should be %08lx\n",
  602. readback, ~val);
  603. errs++;
  604. if (ctrlc())
  605. return -1;
  606. }
  607. }
  608. }
  609. /*
  610. * Based on code whose Original Author and Copyright
  611. * information follows: Copyright (c) 1998 by Michael
  612. * Barr. This software is placed into the public
  613. * domain and may be used for any purpose. However,
  614. * this notice must not be changed or removed and no
  615. * warranty is either expressed or implied by its
  616. * publication or distribution.
  617. */
  618. /*
  619. * Address line test
  620. * Description: Test the address bus wiring in a
  621. * memory region by performing a walking
  622. * 1's test on the relevant bits of the
  623. * address and checking for aliasing.
  624. * This test will find single-bit
  625. * address failures such as stuck-high,
  626. * stuck-low, and shorted pins. The base
  627. * address and size of the region are
  628. * selected by the caller.
  629. * Notes: For best results, the selected base
  630. * address should have enough LSB 0's to
  631. * guarantee single address bit changes.
  632. * For example, to test a 64-Kbyte
  633. * region, select a base address on a
  634. * 64-Kbyte boundary. Also, select the
  635. * region size as a power-of-two if at
  636. * all possible.
  637. *
  638. * Returns: 0 if the test succeeds, 1 if the test fails.
  639. */
  640. len = ((ulong)end - (ulong)start)/sizeof(vu_long);
  641. pattern = (vu_long) 0xaaaaaaaa;
  642. anti_pattern = (vu_long) 0x55555555;
  643. debug("%s:%d: length = 0x%.8lx\n",
  644. __func__, __LINE__, len);
  645. /*
  646. * Write the default pattern at each of the
  647. * power-of-two offsets.
  648. */
  649. for (offset = 1; offset < len; offset <<= 1)
  650. start[offset] = pattern;
  651. /*
  652. * Check for address bits stuck high.
  653. */
  654. test_offset = 0;
  655. start[test_offset] = anti_pattern;
  656. for (offset = 1; offset < len; offset <<= 1) {
  657. temp = start[offset];
  658. if (temp != pattern) {
  659. printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
  660. " expected 0x%.8lx, actual 0x%.8lx\n",
  661. (ulong)&start[offset], pattern, temp);
  662. errs++;
  663. if (ctrlc())
  664. return -1;
  665. }
  666. }
  667. start[test_offset] = pattern;
  668. WATCHDOG_RESET();
  669. /*
  670. * Check for addr bits stuck low or shorted.
  671. */
  672. for (test_offset = 1; test_offset < len; test_offset <<= 1) {
  673. start[test_offset] = anti_pattern;
  674. for (offset = 1; offset < len; offset <<= 1) {
  675. temp = start[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. (ulong)&start[offset], pattern, temp);
  681. errs++;
  682. if (ctrlc())
  683. return -1;
  684. }
  685. }
  686. start[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 = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
  701. /*
  702. * Fill memory with a known pattern.
  703. */
  704. for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
  705. WATCHDOG_RESET();
  706. start[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 = start[offset];
  714. if (temp != pattern) {
  715. printf("\nFAILURE (read/write) @ 0x%.8lx:"
  716. " expected 0x%.8lx, actual 0x%.8lx)\n",
  717. (ulong)&start[offset], pattern, temp);
  718. errs++;
  719. if (ctrlc())
  720. return -1;
  721. }
  722. anti_pattern = ~pattern;
  723. start[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 = start[offset];
  732. if (temp != anti_pattern) {
  733. printf("\nFAILURE (read/write): @ 0x%.8lx:"
  734. " expected 0x%.8lx, actual 0x%.8lx)\n",
  735. (ulong)&start[offset], anti_pattern, temp);
  736. errs++;
  737. if (ctrlc())
  738. return -1;
  739. }
  740. start[offset] = 0;
  741. }
  742. return 0;
  743. }
  744. static ulong mem_test_quick(vu_long *start, vu_long *end, vu_long pattern,
  745. int iteration)
  746. {
  747. vu_long *addr;
  748. ulong errs = 0;
  749. ulong incr;
  750. ulong val, readback;
  751. /* Alternate the pattern */
  752. incr = 1;
  753. if (iteration & 1) {
  754. incr = -incr;
  755. /*
  756. * Flip the pattern each time to make lots of zeros and
  757. * then, the next time, lots of ones. We decrement
  758. * the "negative" patterns and increment the "positive"
  759. * patterns to preserve this feature.
  760. */
  761. if (pattern & 0x80000000)
  762. pattern = -pattern; /* complement & increment */
  763. else
  764. pattern = ~pattern;
  765. }
  766. printf("\rPattern %08lX Writing..."
  767. "%12s"
  768. "\b\b\b\b\b\b\b\b\b\b",
  769. pattern, "");
  770. for (addr = start, val = pattern; addr < end; addr++) {
  771. WATCHDOG_RESET();
  772. *addr = val;
  773. val += incr;
  774. }
  775. puts("Reading...");
  776. for (addr = start, val = pattern; addr < end; addr++) {
  777. WATCHDOG_RESET();
  778. readback = *addr;
  779. if (readback != val) {
  780. printf("\nMem error @ 0x%08X: "
  781. "found %08lX, expected %08lX\n",
  782. (uint)(uintptr_t)addr, readback, val);
  783. errs++;
  784. if (ctrlc())
  785. return -1;
  786. }
  787. val += incr;
  788. }
  789. return 0;
  790. }
  791. /*
  792. * Perform a memory test. A more complete alternative test can be
  793. * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
  794. * interrupted by ctrl-c or by a failure of one of the sub-tests.
  795. */
  796. static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
  797. char * const argv[])
  798. {
  799. vu_long *start, *end;
  800. int iteration_limit;
  801. int ret;
  802. ulong errs = 0; /* number of errors, or -1 if interrupted */
  803. ulong pattern;
  804. int iteration;
  805. #if defined(CONFIG_SYS_ALT_MEMTEST)
  806. const int alt_test = 1;
  807. #else
  808. const int alt_test = 0;
  809. #endif
  810. if (argc > 1)
  811. start = (ulong *)simple_strtoul(argv[1], NULL, 16);
  812. else
  813. start = (ulong *)CONFIG_SYS_MEMTEST_START;
  814. if (argc > 2)
  815. end = (ulong *)simple_strtoul(argv[2], NULL, 16);
  816. else
  817. end = (ulong *)(CONFIG_SYS_MEMTEST_END);
  818. if (argc > 3)
  819. pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
  820. else
  821. pattern = 0;
  822. if (argc > 4)
  823. iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
  824. else
  825. iteration_limit = 0;
  826. printf("Testing %08x ... %08x:\n", (uint)(uintptr_t)start,
  827. (uint)(uintptr_t)end);
  828. debug("%s:%d: start 0x%p end 0x%p\n",
  829. __func__, __LINE__, start, end);
  830. for (iteration = 0;
  831. !iteration_limit || iteration < iteration_limit;
  832. iteration++) {
  833. if (ctrlc()) {
  834. errs = -1UL;
  835. break;
  836. }
  837. printf("Iteration: %6d\r", iteration + 1);
  838. debug("\n");
  839. if (alt_test)
  840. errs = mem_test_alt(start, end);
  841. else
  842. errs = mem_test_quick(start, end, pattern, iteration);
  843. }
  844. if (errs == -1UL) {
  845. /* Memory test was aborted - write a newline to finish off */
  846. putc('\n');
  847. ret = 1;
  848. } else {
  849. printf("Tested %d iteration(s) with %lu errors.\n",
  850. iteration, errs);
  851. ret = errs != 0;
  852. }
  853. return ret; /* not reached */
  854. }
  855. /* Modify memory.
  856. *
  857. * Syntax:
  858. * mm{.b, .w, .l} {addr}
  859. * nm{.b, .w, .l} {addr}
  860. */
  861. static int
  862. mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
  863. {
  864. ulong addr, i;
  865. int nbytes, size;
  866. void *ptr = NULL;
  867. if (argc != 2)
  868. return CMD_RET_USAGE;
  869. #ifdef CONFIG_BOOT_RETRY_TIME
  870. reset_cmd_timeout(); /* got a good command to get here */
  871. #endif
  872. /* We use the last specified parameters, unless new ones are
  873. * entered.
  874. */
  875. addr = mm_last_addr;
  876. size = mm_last_size;
  877. if ((flag & CMD_FLAG_REPEAT) == 0) {
  878. /* New command specified. Check for a size specification.
  879. * Defaults to long if no or incorrect specification.
  880. */
  881. if ((size = cmd_get_data_size(argv[0], 4)) < 0)
  882. return 1;
  883. /* Address is specified since argc > 1
  884. */
  885. addr = simple_strtoul(argv[1], NULL, 16);
  886. addr += base_address;
  887. }
  888. #ifdef CONFIG_HAS_DATAFLASH
  889. if (addr_dataflash(addr)){
  890. puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
  891. return 0;
  892. }
  893. #endif
  894. #ifdef CONFIG_BLACKFIN
  895. if (addr_bfin_on_chip_mem(addr)) {
  896. puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
  897. return 0;
  898. }
  899. #endif
  900. /* Print the address, followed by value. Then accept input for
  901. * the next value. A non-converted value exits.
  902. */
  903. do {
  904. ptr = map_sysmem(addr, size);
  905. printf("%08lx:", addr);
  906. if (size == 4)
  907. printf(" %08x", *((uint *)ptr));
  908. else if (size == 2)
  909. printf(" %04x", *((ushort *)ptr));
  910. else
  911. printf(" %02x", *((u_char *)ptr));
  912. nbytes = readline (" ? ");
  913. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  914. /* <CR> pressed as only input, don't modify current
  915. * location and move to next. "-" pressed will go back.
  916. */
  917. if (incrflag)
  918. addr += nbytes ? -size : size;
  919. nbytes = 1;
  920. #ifdef CONFIG_BOOT_RETRY_TIME
  921. reset_cmd_timeout(); /* good enough to not time out */
  922. #endif
  923. }
  924. #ifdef CONFIG_BOOT_RETRY_TIME
  925. else if (nbytes == -2) {
  926. break; /* timed out, exit the command */
  927. }
  928. #endif
  929. else {
  930. char *endp;
  931. i = simple_strtoul(console_buffer, &endp, 16);
  932. nbytes = endp - console_buffer;
  933. if (nbytes) {
  934. #ifdef CONFIG_BOOT_RETRY_TIME
  935. /* good enough to not time out
  936. */
  937. reset_cmd_timeout();
  938. #endif
  939. if (size == 4)
  940. *((uint *)ptr) = i;
  941. else if (size == 2)
  942. *((ushort *)ptr) = i;
  943. else
  944. *((u_char *)ptr) = i;
  945. if (incrflag)
  946. addr += size;
  947. }
  948. }
  949. } while (nbytes);
  950. if (ptr)
  951. unmap_sysmem(ptr);
  952. mm_last_addr = addr;
  953. mm_last_size = size;
  954. return 0;
  955. }
  956. #ifdef CONFIG_CMD_CRC32
  957. #ifndef CONFIG_CRC32_VERIFY
  958. static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  959. {
  960. ulong addr, length;
  961. ulong crc;
  962. ulong *ptr;
  963. if (argc < 3)
  964. return CMD_RET_USAGE;
  965. addr = simple_strtoul (argv[1], NULL, 16);
  966. addr += base_address;
  967. length = simple_strtoul (argv[2], NULL, 16);
  968. crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
  969. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  970. addr, addr + length - 1, crc);
  971. if (argc > 3) {
  972. ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
  973. *ptr = crc;
  974. }
  975. return 0;
  976. }
  977. #else /* CONFIG_CRC32_VERIFY */
  978. int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  979. {
  980. ulong addr, length;
  981. ulong crc;
  982. ulong *ptr;
  983. ulong vcrc;
  984. int verify;
  985. int ac;
  986. char * const *av;
  987. if (argc < 3) {
  988. usage:
  989. return CMD_RET_USAGE;
  990. }
  991. av = argv + 1;
  992. ac = argc - 1;
  993. if (strcmp(*av, "-v") == 0) {
  994. verify = 1;
  995. av++;
  996. ac--;
  997. if (ac < 3)
  998. goto usage;
  999. } else
  1000. verify = 0;
  1001. addr = simple_strtoul(*av++, NULL, 16);
  1002. addr += base_address;
  1003. length = simple_strtoul(*av++, NULL, 16);
  1004. crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
  1005. if (!verify) {
  1006. printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
  1007. addr, addr + length - 1, crc);
  1008. if (ac > 2) {
  1009. ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
  1010. *ptr = crc;
  1011. }
  1012. } else {
  1013. vcrc = simple_strtoul(*av++, NULL, 16);
  1014. if (vcrc != crc) {
  1015. printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
  1016. addr, addr + length - 1, crc, vcrc);
  1017. return 1;
  1018. }
  1019. }
  1020. return 0;
  1021. }
  1022. #endif /* CONFIG_CRC32_VERIFY */
  1023. #endif
  1024. /**************************************************/
  1025. U_BOOT_CMD(
  1026. md, 3, 1, do_mem_md,
  1027. "memory display",
  1028. "[.b, .w, .l] address [# of objects]"
  1029. );
  1030. U_BOOT_CMD(
  1031. mm, 2, 1, do_mem_mm,
  1032. "memory modify (auto-incrementing address)",
  1033. "[.b, .w, .l] address"
  1034. );
  1035. U_BOOT_CMD(
  1036. nm, 2, 1, do_mem_nm,
  1037. "memory modify (constant address)",
  1038. "[.b, .w, .l] address"
  1039. );
  1040. U_BOOT_CMD(
  1041. mw, 4, 1, do_mem_mw,
  1042. "memory write (fill)",
  1043. "[.b, .w, .l] address value [count]"
  1044. );
  1045. U_BOOT_CMD(
  1046. cp, 4, 1, do_mem_cp,
  1047. "memory copy",
  1048. "[.b, .w, .l] source target count"
  1049. );
  1050. U_BOOT_CMD(
  1051. cmp, 4, 1, do_mem_cmp,
  1052. "memory compare",
  1053. "[.b, .w, .l] addr1 addr2 count"
  1054. );
  1055. #ifdef CONFIG_CMD_CRC32
  1056. #ifndef CONFIG_CRC32_VERIFY
  1057. U_BOOT_CMD(
  1058. crc32, 4, 1, do_mem_crc,
  1059. "checksum calculation",
  1060. "address count [addr]\n - compute CRC32 checksum [save at addr]"
  1061. );
  1062. #else /* CONFIG_CRC32_VERIFY */
  1063. U_BOOT_CMD(
  1064. crc32, 5, 1, do_mem_crc,
  1065. "checksum calculation",
  1066. "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
  1067. "-v address count crc\n - verify crc of memory area"
  1068. );
  1069. #endif /* CONFIG_CRC32_VERIFY */
  1070. #endif
  1071. #ifdef CONFIG_CMD_MEMINFO
  1072. __weak void board_show_dram(ulong size)
  1073. {
  1074. puts("DRAM: ");
  1075. print_size(size, "\n");
  1076. }
  1077. static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
  1078. char * const argv[])
  1079. {
  1080. board_show_dram(gd->ram_size);
  1081. return 0;
  1082. }
  1083. #endif
  1084. U_BOOT_CMD(
  1085. base, 2, 1, do_mem_base,
  1086. "print or set address offset",
  1087. "\n - print address offset for memory commands\n"
  1088. "base off\n - set address offset for memory commands to 'off'"
  1089. );
  1090. U_BOOT_CMD(
  1091. loop, 3, 1, do_mem_loop,
  1092. "infinite loop on address range",
  1093. "[.b, .w, .l] address number_of_objects"
  1094. );
  1095. #ifdef CONFIG_LOOPW
  1096. U_BOOT_CMD(
  1097. loopw, 4, 1, do_mem_loopw,
  1098. "infinite write loop on address range",
  1099. "[.b, .w, .l] address number_of_objects data_to_write"
  1100. );
  1101. #endif /* CONFIG_LOOPW */
  1102. U_BOOT_CMD(
  1103. mtest, 5, 1, do_mem_mtest,
  1104. "simple RAM read/write test",
  1105. "[start [end [pattern [iterations]]]]"
  1106. );
  1107. #ifdef CONFIG_MX_CYCLIC
  1108. U_BOOT_CMD(
  1109. mdc, 4, 1, do_mem_mdc,
  1110. "memory display cyclic",
  1111. "[.b, .w, .l] address count delay(ms)"
  1112. );
  1113. U_BOOT_CMD(
  1114. mwc, 4, 1, do_mem_mwc,
  1115. "memory write cyclic",
  1116. "[.b, .w, .l] address value delay(ms)"
  1117. );
  1118. #endif /* CONFIG_MX_CYCLIC */
  1119. #ifdef CONFIG_CMD_MEMINFO
  1120. U_BOOT_CMD(
  1121. meminfo, 3, 1, do_mem_info,
  1122. "display memory information",
  1123. ""
  1124. );
  1125. #endif