memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /*
  155. * Define INJECT_*_ERRORS for testing error detection in the presence of
  156. * _good_ hardware.
  157. */
  158. #undef INJECT_DATA_ERRORS
  159. #undef INJECT_ADDRESS_ERRORS
  160. #ifdef INJECT_DATA_ERRORS
  161. #warning "Injecting data line errors for testing purposes"
  162. #endif
  163. #ifdef INJECT_ADDRESS_ERRORS
  164. #warning "Injecting address line errors for testing purposes"
  165. #endif
  166. /*
  167. * This function performs a double word move from the data at
  168. * the source pointer to the location at the destination pointer.
  169. * This is helpful for testing memory on processors which have a 64 bit
  170. * wide data bus.
  171. *
  172. * On those PowerPC with FPU, use assembly and a floating point move:
  173. * this does a 64 bit move.
  174. *
  175. * For other processors, let the compiler generate the best code it can.
  176. */
  177. static void move64(unsigned long long *src, unsigned long long *dest)
  178. {
  179. #if defined(CONFIG_MPC8260) || defined(CONFIG_MPC824X)
  180. asm ("lfd 0, 0(3)\n\t" /* fpr0 = *scr */
  181. "stfd 0, 0(4)" /* *dest = fpr0 */
  182. : : : "fr0" ); /* Clobbers fr0 */
  183. return;
  184. #else
  185. *dest = *src;
  186. #endif
  187. }
  188. /*
  189. * This is 64 bit wide test patterns. Note that they reside in ROM
  190. * (which presumably works) and the tests write them to RAM which may
  191. * not work.
  192. *
  193. * The "otherpattern" is written to drive the data bus to values other
  194. * than the test pattern. This is for detecting floating bus lines.
  195. *
  196. */
  197. const static unsigned long long pattern[] = {
  198. 0xaaaaaaaaaaaaaaaa,
  199. 0xcccccccccccccccc,
  200. 0xf0f0f0f0f0f0f0f0,
  201. 0xff00ff00ff00ff00,
  202. 0xffff0000ffff0000,
  203. 0xffffffff00000000,
  204. 0x00000000ffffffff,
  205. 0x0000ffff0000ffff,
  206. 0x00ff00ff00ff00ff,
  207. 0x0f0f0f0f0f0f0f0f,
  208. 0x3333333333333333,
  209. 0x5555555555555555};
  210. const unsigned long long otherpattern = 0x0123456789abcdef;
  211. static int memory_post_dataline(unsigned long long * pmem)
  212. {
  213. unsigned long long temp64;
  214. int num_patterns = sizeof(pattern)/ sizeof(pattern[0]);
  215. int i;
  216. unsigned int hi, lo, pathi, patlo;
  217. int ret = 0;
  218. for ( i = 0; i < num_patterns; i++) {
  219. move64((unsigned long long *)&(pattern[i]), pmem++);
  220. /*
  221. * Put a different pattern on the data lines: otherwise they
  222. * may float long enough to read back what we wrote.
  223. */
  224. move64((unsigned long long *)&otherpattern, pmem--);
  225. move64(pmem, &temp64);
  226. #ifdef INJECT_DATA_ERRORS
  227. temp64 ^= 0x00008000;
  228. #endif
  229. if (temp64 != pattern[i]){
  230. pathi = (pattern[i]>>32) & 0xffffffff;
  231. patlo = pattern[i] & 0xffffffff;
  232. hi = (temp64>>32) & 0xffffffff;
  233. lo = temp64 & 0xffffffff;
  234. post_log ("Memory (date line) error at %08x, "
  235. "wrote %08x%08x, read %08x%08x !\n",
  236. pmem, pathi, patlo, hi, lo);
  237. ret = -1;
  238. }
  239. }
  240. return ret;
  241. }
  242. static int memory_post_addrline(ulong *testaddr, ulong *base, ulong size)
  243. {
  244. ulong *target;
  245. ulong *end;
  246. ulong readback;
  247. ulong xor;
  248. int ret = 0;
  249. end = (ulong *)((ulong)base + size); /* pointer arith! */
  250. xor = 0;
  251. for(xor = sizeof(ulong); xor > 0; xor <<= 1) {
  252. target = (ulong *)((ulong)testaddr ^ xor);
  253. if((target >= base) && (target < end)) {
  254. *testaddr = ~*target;
  255. readback = *target;
  256. #ifdef INJECT_ADDRESS_ERRORS
  257. if(xor == 0x00008000) {
  258. readback = *testaddr;
  259. }
  260. #endif
  261. if(readback == *testaddr) {
  262. post_log ("Memory (address line) error at %08x<->%08x, "
  263. "XOR value %08x !\n",
  264. testaddr, target, xor);
  265. ret = -1;
  266. }
  267. }
  268. }
  269. return ret;
  270. }
  271. static int memory_post_test1 (unsigned long start,
  272. unsigned long size,
  273. unsigned long val)
  274. {
  275. unsigned long i;
  276. ulong *mem = (ulong *) start;
  277. ulong readback;
  278. int ret = 0;
  279. for (i = 0; i < size / sizeof (ulong); i++) {
  280. mem[i] = val;
  281. if (i % 1024 == 0)
  282. WATCHDOG_RESET ();
  283. }
  284. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  285. readback = mem[i];
  286. if (readback != val) {
  287. post_log ("Memory error at %08x, "
  288. "wrote %08x, read %08x !\n",
  289. mem + i, val, readback);
  290. ret = -1;
  291. break;
  292. }
  293. if (i % 1024 == 0)
  294. WATCHDOG_RESET ();
  295. }
  296. return ret;
  297. }
  298. static int memory_post_test2 (unsigned long start, unsigned long size)
  299. {
  300. unsigned long i;
  301. ulong *mem = (ulong *) start;
  302. ulong readback;
  303. int ret = 0;
  304. for (i = 0; i < size / sizeof (ulong); i++) {
  305. mem[i] = 1 << (i % 32);
  306. if (i % 1024 == 0)
  307. WATCHDOG_RESET ();
  308. }
  309. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  310. readback = mem[i];
  311. if (readback != (1 << (i % 32))) {
  312. post_log ("Memory error at %08x, "
  313. "wrote %08x, read %08x !\n",
  314. mem + i, 1 << (i % 32), readback);
  315. ret = -1;
  316. break;
  317. }
  318. if (i % 1024 == 0)
  319. WATCHDOG_RESET ();
  320. }
  321. return ret;
  322. }
  323. static int memory_post_test3 (unsigned long start, unsigned long size)
  324. {
  325. unsigned long i;
  326. ulong *mem = (ulong *) start;
  327. ulong readback;
  328. int ret = 0;
  329. for (i = 0; i < size / sizeof (ulong); i++) {
  330. mem[i] = i;
  331. if (i % 1024 == 0)
  332. WATCHDOG_RESET ();
  333. }
  334. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  335. readback = mem[i];
  336. if (readback != i) {
  337. post_log ("Memory error at %08x, "
  338. "wrote %08x, read %08x !\n",
  339. mem + i, i, readback);
  340. ret = -1;
  341. break;
  342. }
  343. if (i % 1024 == 0)
  344. WATCHDOG_RESET ();
  345. }
  346. return ret;
  347. }
  348. static int memory_post_test4 (unsigned long start, unsigned long size)
  349. {
  350. unsigned long i;
  351. ulong *mem = (ulong *) start;
  352. ulong readback;
  353. int ret = 0;
  354. for (i = 0; i < size / sizeof (ulong); i++) {
  355. mem[i] = ~i;
  356. if (i % 1024 == 0)
  357. WATCHDOG_RESET ();
  358. }
  359. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  360. readback = mem[i];
  361. if (readback != ~i) {
  362. post_log ("Memory error at %08x, "
  363. "wrote %08x, read %08x !\n",
  364. mem + i, ~i, readback);
  365. ret = -1;
  366. break;
  367. }
  368. if (i % 1024 == 0)
  369. WATCHDOG_RESET ();
  370. }
  371. return ret;
  372. }
  373. static int memory_post_tests (unsigned long start, unsigned long size)
  374. {
  375. int ret = 0;
  376. if (ret == 0)
  377. ret = memory_post_dataline ((long long *)start);
  378. WATCHDOG_RESET ();
  379. if (ret == 0)
  380. ret = memory_post_addrline ((long *)start, (long *)start, size);
  381. WATCHDOG_RESET ();
  382. if (ret == 0)
  383. ret = memory_post_addrline ((long *)(start + size - 8),
  384. (long *)start, size);
  385. WATCHDOG_RESET ();
  386. if (ret == 0)
  387. ret = memory_post_test1 (start, size, 0x00000000);
  388. WATCHDOG_RESET ();
  389. if (ret == 0)
  390. ret = memory_post_test1 (start, size, 0xffffffff);
  391. WATCHDOG_RESET ();
  392. if (ret == 0)
  393. ret = memory_post_test1 (start, size, 0x55555555);
  394. WATCHDOG_RESET ();
  395. if (ret == 0)
  396. ret = memory_post_test1 (start, size, 0xaaaaaaaa);
  397. WATCHDOG_RESET ();
  398. if (ret == 0)
  399. ret = memory_post_test2 (start, size);
  400. WATCHDOG_RESET ();
  401. if (ret == 0)
  402. ret = memory_post_test3 (start, size);
  403. WATCHDOG_RESET ();
  404. if (ret == 0)
  405. ret = memory_post_test4 (start, size);
  406. WATCHDOG_RESET ();
  407. return ret;
  408. }
  409. int memory_post_test (int flags)
  410. {
  411. int ret = 0;
  412. DECLARE_GLOBAL_DATA_PTR;
  413. bd_t *bd = gd->bd;
  414. unsigned long memsize = (bd->bi_memsize >= 256 << 20 ?
  415. 256 << 20 : bd->bi_memsize) - (1 << 20);
  416. if (flags & POST_SLOWTEST) {
  417. ret = memory_post_tests (CFG_SDRAM_BASE, memsize);
  418. } else { /* POST_NORMAL */
  419. unsigned long i;
  420. for (i = 0; i < (memsize >> 20) && ret == 0; i++) {
  421. if (ret == 0)
  422. ret = memory_post_tests (i << 20, 0x800);
  423. if (ret == 0)
  424. ret = memory_post_tests ((i << 20) + 0xff800, 0x800);
  425. }
  426. }
  427. return ret;
  428. }
  429. #endif /* CONFIG_POST & CFG_POST_MEMORY */
  430. #endif /* CONFIG_POST */