memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * (C) Copyright 2002
  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. #include <common.h>
  24. /* Memory test
  25. *
  26. * General observations:
  27. * o The recommended test sequence is to test the data lines: if they are
  28. * broken, nothing else will work properly. Then test the address
  29. * lines. Finally, test the cells in the memory now that the test
  30. * program knows that the address and data lines work properly.
  31. * This sequence also helps isolate and identify what is faulty.
  32. *
  33. * o For the address line test, it is a good idea to use the base
  34. * address of the lowest memory location, which causes a '1' bit to
  35. * walk through a field of zeros on the address lines and the highest
  36. * memory location, which causes a '0' bit to walk through a field of
  37. * '1's on the address line.
  38. *
  39. * o Floating buses can fool memory tests if the test routine writes
  40. * a value and then reads it back immediately. The problem is, the
  41. * write will charge the residual capacitance on the data bus so the
  42. * bus retains its state briefely. When the test program reads the
  43. * value back immediately, the capacitance of the bus can allow it
  44. * to read back what was written, even though the memory circuitry
  45. * is broken. To avoid this, the test program should write a test
  46. * pattern to the target location, write a different pattern elsewhere
  47. * to charge the residual capacitance in a differnt manner, then read
  48. * the target location back.
  49. *
  50. * o Always read the target location EXACTLY ONCE and save it in a local
  51. * variable. The problem with reading the target location more than
  52. * once is that the second and subsequent reads may work properly,
  53. * resulting in a failed test that tells the poor technician that
  54. * "Memory error at 00000000, wrote aaaaaaaa, read aaaaaaaa" which
  55. * doesn't help him one bit and causes puzzled phone calls. Been there,
  56. * done that.
  57. *
  58. * Data line test:
  59. * ---------------
  60. * This tests data lines for shorts and opens by forcing adjacent data
  61. * to opposite states. Because the data lines could be routed in an
  62. * arbitrary manner the must ensure test patterns ensure that every case
  63. * is tested. By using the following series of binary patterns every
  64. * combination of adjacent bits is test regardless of routing.
  65. *
  66. * ...101010101010101010101010
  67. * ...110011001100110011001100
  68. * ...111100001111000011110000
  69. * ...111111110000000011111111
  70. *
  71. * Carrying this out, gives us six hex patterns as follows:
  72. *
  73. * 0xaaaaaaaaaaaaaaaa
  74. * 0xcccccccccccccccc
  75. * 0xf0f0f0f0f0f0f0f0
  76. * 0xff00ff00ff00ff00
  77. * 0xffff0000ffff0000
  78. * 0xffffffff00000000
  79. *
  80. * To test for short and opens to other signals on our boards, we
  81. * simply test with the 1's complemnt of the paterns as well, resulting
  82. * in twelve patterns total.
  83. *
  84. * After writing a test pattern. a special pattern 0x0123456789ABCDEF is
  85. * written to a different address in case the data lines are floating.
  86. * Thus, if a byte lane fails, you will see part of the special
  87. * pattern in that byte lane when the test runs. For example, if the
  88. * xx__xxxxxxxxxxxx byte line fails, you will see aa23aaaaaaaaaaaa
  89. * (for the 'a' test pattern).
  90. *
  91. * Address line test:
  92. * ------------------
  93. * This function performs a test to verify that all the address lines
  94. * hooked up to the RAM work properly. If there is an address line
  95. * fault, it usually shows up as two different locations in the address
  96. * map (related by the faulty address line) mapping to one physical
  97. * memory storage location. The artifact that shows up is writing to
  98. * the first location "changes" the second location.
  99. *
  100. * To test all address lines, we start with the given base address and
  101. * xor the address with a '1' bit to flip one address line. For each
  102. * test, we shift the '1' bit left to test the next address line.
  103. *
  104. * In the actual code, we start with address sizeof(ulong) since our
  105. * test pattern we use is a ulong and thus, if we tried to test lower
  106. * order address bits, it wouldn't work because our pattern would
  107. * overwrite itself.
  108. *
  109. * Example for a 4 bit address space with the base at 0000:
  110. * 0000 <- base
  111. * 0001 <- test 1
  112. * 0010 <- test 2
  113. * 0100 <- test 3
  114. * 1000 <- test 4
  115. * Example for a 4 bit address space with the base at 0010:
  116. * 0010 <- base
  117. * 0011 <- test 1
  118. * 0000 <- (below the base address, skipped)
  119. * 0110 <- test 2
  120. * 1010 <- test 3
  121. *
  122. * The test locations are successively tested to make sure that they are
  123. * not "mirrored" onto the base address due to a faulty address line.
  124. * Note that the base and each test location are related by one address
  125. * line flipped. Note that the base address need not be all zeros.
  126. *
  127. * Memory tests 1-4:
  128. * -----------------
  129. * These tests verify RAM using sequential writes and reads
  130. * to/from RAM. There are several test cases that use different patterns to
  131. * verify RAM. Each test case fills a region of RAM with one pattern and
  132. * then reads the region back and compares its contents with the pattern.
  133. * The following patterns are used:
  134. *
  135. * 1a) zero pattern (0x00000000)
  136. * 1b) negative pattern (0xffffffff)
  137. * 1c) checkerboard pattern (0x55555555)
  138. * 1d) checkerboard pattern (0xaaaaaaaa)
  139. * 2) bit-flip pattern ((1 << (offset % 32))
  140. * 3) address pattern (offset)
  141. * 4) address pattern (~offset)
  142. *
  143. * Being run in normal mode, the test verifies only small 4Kb
  144. * regions of RAM around each 1Mb boundary. For example, for 64Mb
  145. * RAM the following areas are verified: 0x00000000-0x00000800,
  146. * 0x000ff800-0x00100800, 0x001ff800-0x00200800, ..., 0x03fff800-
  147. * 0x04000000. If the test is run in slow-test mode, it verifies
  148. * the whole RAM.
  149. */
  150. #ifdef CONFIG_POST
  151. #include <post.h>
  152. #include <watchdog.h>
  153. #if CONFIG_POST & CFG_POST_MEMORY
  154. DECLARE_GLOBAL_DATA_PTR;
  155. /*
  156. * Define INJECT_*_ERRORS for testing error detection in the presence of
  157. * _good_ hardware.
  158. */
  159. #undef INJECT_DATA_ERRORS
  160. #undef INJECT_ADDRESS_ERRORS
  161. #ifdef INJECT_DATA_ERRORS
  162. #warning "Injecting data line errors for testing purposes"
  163. #endif
  164. #ifdef INJECT_ADDRESS_ERRORS
  165. #warning "Injecting address line errors for testing purposes"
  166. #endif
  167. /*
  168. * This function performs a double word move from the data at
  169. * the source pointer to the location at the destination pointer.
  170. * This is helpful for testing memory on processors which have a 64 bit
  171. * wide data bus.
  172. *
  173. * On those PowerPC with FPU, use assembly and a floating point move:
  174. * this does a 64 bit move.
  175. *
  176. * For other processors, let the compiler generate the best code it can.
  177. */
  178. static void move64(unsigned long long *src, unsigned long long *dest)
  179. {
  180. #if defined(CONFIG_MPC8260) || defined(CONFIG_MPC824X)
  181. asm ("lfd 0, 0(3)\n\t" /* fpr0 = *scr */
  182. "stfd 0, 0(4)" /* *dest = fpr0 */
  183. : : : "fr0" ); /* Clobbers fr0 */
  184. return;
  185. #else
  186. *dest = *src;
  187. #endif
  188. }
  189. /*
  190. * This is 64 bit wide test patterns. Note that they reside in ROM
  191. * (which presumably works) and the tests write them to RAM which may
  192. * not work.
  193. *
  194. * The "otherpattern" is written to drive the data bus to values other
  195. * than the test pattern. This is for detecting floating bus lines.
  196. *
  197. */
  198. const static unsigned long long pattern[] = {
  199. 0xaaaaaaaaaaaaaaaaULL,
  200. 0xccccccccccccccccULL,
  201. 0xf0f0f0f0f0f0f0f0ULL,
  202. 0xff00ff00ff00ff00ULL,
  203. 0xffff0000ffff0000ULL,
  204. 0xffffffff00000000ULL,
  205. 0x00000000ffffffffULL,
  206. 0x0000ffff0000ffffULL,
  207. 0x00ff00ff00ff00ffULL,
  208. 0x0f0f0f0f0f0f0f0fULL,
  209. 0x3333333333333333ULL,
  210. 0x5555555555555555ULL
  211. };
  212. const unsigned long long otherpattern = 0x0123456789abcdefULL;
  213. static int memory_post_dataline(unsigned long long * pmem)
  214. {
  215. unsigned long long temp64 = 0;
  216. int num_patterns = sizeof(pattern)/ sizeof(pattern[0]);
  217. int i;
  218. unsigned int hi, lo, pathi, patlo;
  219. int ret = 0;
  220. for ( i = 0; i < num_patterns; i++) {
  221. move64((unsigned long long *)&(pattern[i]), pmem++);
  222. /*
  223. * Put a different pattern on the data lines: otherwise they
  224. * may float long enough to read back what we wrote.
  225. */
  226. move64((unsigned long long *)&otherpattern, pmem--);
  227. move64(pmem, &temp64);
  228. #ifdef INJECT_DATA_ERRORS
  229. temp64 ^= 0x00008000;
  230. #endif
  231. if (temp64 != pattern[i]){
  232. pathi = (pattern[i]>>32) & 0xffffffff;
  233. patlo = pattern[i] & 0xffffffff;
  234. hi = (temp64>>32) & 0xffffffff;
  235. lo = temp64 & 0xffffffff;
  236. post_log ("Memory (date line) error at %08x, "
  237. "wrote %08x%08x, read %08x%08x !\n",
  238. pmem, pathi, patlo, hi, lo);
  239. ret = -1;
  240. }
  241. }
  242. return ret;
  243. }
  244. static int memory_post_addrline(ulong *testaddr, ulong *base, ulong size)
  245. {
  246. ulong *target;
  247. ulong *end;
  248. ulong readback;
  249. ulong xor;
  250. int ret = 0;
  251. end = (ulong *)((ulong)base + size); /* pointer arith! */
  252. xor = 0;
  253. for(xor = sizeof(ulong); xor > 0; xor <<= 1) {
  254. target = (ulong *)((ulong)testaddr ^ xor);
  255. if((target >= base) && (target < end)) {
  256. *testaddr = ~*target;
  257. readback = *target;
  258. #ifdef INJECT_ADDRESS_ERRORS
  259. if(xor == 0x00008000) {
  260. readback = *testaddr;
  261. }
  262. #endif
  263. if(readback == *testaddr) {
  264. post_log ("Memory (address line) error at %08x<->%08x, "
  265. "XOR value %08x !\n",
  266. testaddr, target, xor);
  267. ret = -1;
  268. }
  269. }
  270. }
  271. return ret;
  272. }
  273. static int memory_post_test1 (unsigned long start,
  274. unsigned long size,
  275. unsigned long val)
  276. {
  277. unsigned long i;
  278. ulong *mem = (ulong *) start;
  279. ulong readback;
  280. int ret = 0;
  281. for (i = 0; i < size / sizeof (ulong); i++) {
  282. mem[i] = val;
  283. if (i % 1024 == 0)
  284. WATCHDOG_RESET ();
  285. }
  286. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  287. readback = mem[i];
  288. if (readback != val) {
  289. post_log ("Memory error at %08x, "
  290. "wrote %08x, read %08x !\n",
  291. mem + i, val, readback);
  292. ret = -1;
  293. break;
  294. }
  295. if (i % 1024 == 0)
  296. WATCHDOG_RESET ();
  297. }
  298. return ret;
  299. }
  300. static int memory_post_test2 (unsigned long start, unsigned long size)
  301. {
  302. unsigned long i;
  303. ulong *mem = (ulong *) start;
  304. ulong readback;
  305. int ret = 0;
  306. for (i = 0; i < size / sizeof (ulong); i++) {
  307. mem[i] = 1 << (i % 32);
  308. if (i % 1024 == 0)
  309. WATCHDOG_RESET ();
  310. }
  311. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  312. readback = mem[i];
  313. if (readback != (1 << (i % 32))) {
  314. post_log ("Memory error at %08x, "
  315. "wrote %08x, read %08x !\n",
  316. mem + i, 1 << (i % 32), readback);
  317. ret = -1;
  318. break;
  319. }
  320. if (i % 1024 == 0)
  321. WATCHDOG_RESET ();
  322. }
  323. return ret;
  324. }
  325. static int memory_post_test3 (unsigned long start, unsigned long size)
  326. {
  327. unsigned long i;
  328. ulong *mem = (ulong *) start;
  329. ulong readback;
  330. int ret = 0;
  331. for (i = 0; i < size / sizeof (ulong); i++) {
  332. mem[i] = i;
  333. if (i % 1024 == 0)
  334. WATCHDOG_RESET ();
  335. }
  336. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  337. readback = mem[i];
  338. if (readback != i) {
  339. post_log ("Memory error at %08x, "
  340. "wrote %08x, read %08x !\n",
  341. mem + i, i, readback);
  342. ret = -1;
  343. break;
  344. }
  345. if (i % 1024 == 0)
  346. WATCHDOG_RESET ();
  347. }
  348. return ret;
  349. }
  350. static int memory_post_test4 (unsigned long start, unsigned long size)
  351. {
  352. unsigned long i;
  353. ulong *mem = (ulong *) start;
  354. ulong readback;
  355. int ret = 0;
  356. for (i = 0; i < size / sizeof (ulong); i++) {
  357. mem[i] = ~i;
  358. if (i % 1024 == 0)
  359. WATCHDOG_RESET ();
  360. }
  361. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  362. readback = mem[i];
  363. if (readback != ~i) {
  364. post_log ("Memory error at %08x, "
  365. "wrote %08x, read %08x !\n",
  366. mem + i, ~i, readback);
  367. ret = -1;
  368. break;
  369. }
  370. if (i % 1024 == 0)
  371. WATCHDOG_RESET ();
  372. }
  373. return ret;
  374. }
  375. static int memory_post_tests (unsigned long start, unsigned long size)
  376. {
  377. int ret = 0;
  378. if (ret == 0)
  379. ret = memory_post_dataline ((unsigned long long *)start);
  380. WATCHDOG_RESET ();
  381. if (ret == 0)
  382. ret = memory_post_addrline ((ulong *)start, (ulong *)start, size);
  383. WATCHDOG_RESET ();
  384. if (ret == 0)
  385. ret = memory_post_addrline ((ulong *)(start + size - 8),
  386. (ulong *)start, size);
  387. WATCHDOG_RESET ();
  388. if (ret == 0)
  389. ret = memory_post_test1 (start, size, 0x00000000);
  390. WATCHDOG_RESET ();
  391. if (ret == 0)
  392. ret = memory_post_test1 (start, size, 0xffffffff);
  393. WATCHDOG_RESET ();
  394. if (ret == 0)
  395. ret = memory_post_test1 (start, size, 0x55555555);
  396. WATCHDOG_RESET ();
  397. if (ret == 0)
  398. ret = memory_post_test1 (start, size, 0xaaaaaaaa);
  399. WATCHDOG_RESET ();
  400. if (ret == 0)
  401. ret = memory_post_test2 (start, size);
  402. WATCHDOG_RESET ();
  403. if (ret == 0)
  404. ret = memory_post_test3 (start, size);
  405. WATCHDOG_RESET ();
  406. if (ret == 0)
  407. ret = memory_post_test4 (start, size);
  408. WATCHDOG_RESET ();
  409. return ret;
  410. }
  411. int memory_post_test (int flags)
  412. {
  413. int ret = 0;
  414. bd_t *bd = gd->bd;
  415. unsigned long memsize = (bd->bi_memsize >= 256 << 20 ?
  416. 256 << 20 : bd->bi_memsize) - (1 << 20);
  417. if (flags & POST_SLOWTEST) {
  418. ret = memory_post_tests (CFG_SDRAM_BASE, memsize);
  419. } else { /* POST_NORMAL */
  420. unsigned long i;
  421. for (i = 0; i < (memsize >> 20) && ret == 0; i++) {
  422. if (ret == 0)
  423. ret = memory_post_tests (i << 20, 0x800);
  424. if (ret == 0)
  425. ret = memory_post_tests ((i << 20) + 0xff800, 0x800);
  426. }
  427. }
  428. return ret;
  429. }
  430. #endif /* CONFIG_POST & CFG_POST_MEMORY */
  431. #endif /* CONFIG_POST */