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. #include <post.h>
  151. #include <watchdog.h>
  152. #if CONFIG_POST & CONFIG_SYS_POST_MEMORY
  153. DECLARE_GLOBAL_DATA_PTR;
  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(const 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. 0xaaaaaaaaaaaaaaaaULL,
  199. 0xccccccccccccccccULL,
  200. 0xf0f0f0f0f0f0f0f0ULL,
  201. 0xff00ff00ff00ff00ULL,
  202. 0xffff0000ffff0000ULL,
  203. 0xffffffff00000000ULL,
  204. 0x00000000ffffffffULL,
  205. 0x0000ffff0000ffffULL,
  206. 0x00ff00ff00ff00ffULL,
  207. 0x0f0f0f0f0f0f0f0fULL,
  208. 0x3333333333333333ULL,
  209. 0x5555555555555555ULL
  210. };
  211. const unsigned long long otherpattern = 0x0123456789abcdefULL;
  212. static int memory_post_dataline(unsigned long long * pmem)
  213. {
  214. unsigned long long temp64 = 0;
  215. int num_patterns = sizeof(pattern)/ sizeof(pattern[0]);
  216. int i;
  217. unsigned int hi, lo, pathi, patlo;
  218. int ret = 0;
  219. for ( i = 0; i < num_patterns; i++) {
  220. move64(&(pattern[i]), pmem++);
  221. /*
  222. * Put a different pattern on the data lines: otherwise they
  223. * may float long enough to read back what we wrote.
  224. */
  225. move64(&otherpattern, pmem--);
  226. move64(pmem, &temp64);
  227. #ifdef INJECT_DATA_ERRORS
  228. temp64 ^= 0x00008000;
  229. #endif
  230. if (temp64 != pattern[i]){
  231. pathi = (pattern[i]>>32) & 0xffffffff;
  232. patlo = pattern[i] & 0xffffffff;
  233. hi = (temp64>>32) & 0xffffffff;
  234. lo = temp64 & 0xffffffff;
  235. post_log ("Memory (date line) error at %08x, "
  236. "wrote %08x%08x, read %08x%08x !\n",
  237. pmem, pathi, patlo, hi, lo);
  238. ret = -1;
  239. }
  240. }
  241. return ret;
  242. }
  243. static int memory_post_addrline(ulong *testaddr, ulong *base, ulong size)
  244. {
  245. ulong *target;
  246. ulong *end;
  247. ulong readback;
  248. ulong xor;
  249. int ret = 0;
  250. end = (ulong *)((ulong)base + size); /* pointer arith! */
  251. xor = 0;
  252. for(xor = sizeof(ulong); xor > 0; xor <<= 1) {
  253. target = (ulong *)((ulong)testaddr ^ xor);
  254. if((target >= base) && (target < end)) {
  255. *testaddr = ~*target;
  256. readback = *target;
  257. #ifdef INJECT_ADDRESS_ERRORS
  258. if(xor == 0x00008000) {
  259. readback = *testaddr;
  260. }
  261. #endif
  262. if(readback == *testaddr) {
  263. post_log ("Memory (address line) error at %08x<->%08x, "
  264. "XOR value %08x !\n",
  265. testaddr, target, xor);
  266. ret = -1;
  267. }
  268. }
  269. }
  270. return ret;
  271. }
  272. static int memory_post_test1 (unsigned long start,
  273. unsigned long size,
  274. unsigned long val)
  275. {
  276. unsigned long i;
  277. ulong *mem = (ulong *) start;
  278. ulong readback;
  279. int ret = 0;
  280. for (i = 0; i < size / sizeof (ulong); i++) {
  281. mem[i] = val;
  282. if (i % 1024 == 0)
  283. WATCHDOG_RESET ();
  284. }
  285. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  286. readback = mem[i];
  287. if (readback != val) {
  288. post_log ("Memory error at %08x, "
  289. "wrote %08x, read %08x !\n",
  290. mem + i, val, readback);
  291. ret = -1;
  292. break;
  293. }
  294. if (i % 1024 == 0)
  295. WATCHDOG_RESET ();
  296. }
  297. return ret;
  298. }
  299. static int memory_post_test2 (unsigned long start, unsigned long size)
  300. {
  301. unsigned long i;
  302. ulong *mem = (ulong *) start;
  303. ulong readback;
  304. int ret = 0;
  305. for (i = 0; i < size / sizeof (ulong); i++) {
  306. mem[i] = 1 << (i % 32);
  307. if (i % 1024 == 0)
  308. WATCHDOG_RESET ();
  309. }
  310. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  311. readback = mem[i];
  312. if (readback != (1 << (i % 32))) {
  313. post_log ("Memory error at %08x, "
  314. "wrote %08x, read %08x !\n",
  315. mem + i, 1 << (i % 32), readback);
  316. ret = -1;
  317. break;
  318. }
  319. if (i % 1024 == 0)
  320. WATCHDOG_RESET ();
  321. }
  322. return ret;
  323. }
  324. static int memory_post_test3 (unsigned long start, unsigned long size)
  325. {
  326. unsigned long i;
  327. ulong *mem = (ulong *) start;
  328. ulong readback;
  329. int ret = 0;
  330. for (i = 0; i < size / sizeof (ulong); i++) {
  331. mem[i] = i;
  332. if (i % 1024 == 0)
  333. WATCHDOG_RESET ();
  334. }
  335. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  336. readback = mem[i];
  337. if (readback != i) {
  338. post_log ("Memory error at %08x, "
  339. "wrote %08x, read %08x !\n",
  340. mem + i, i, readback);
  341. ret = -1;
  342. break;
  343. }
  344. if (i % 1024 == 0)
  345. WATCHDOG_RESET ();
  346. }
  347. return ret;
  348. }
  349. static int memory_post_test4 (unsigned long start, unsigned long size)
  350. {
  351. unsigned long i;
  352. ulong *mem = (ulong *) start;
  353. ulong readback;
  354. int ret = 0;
  355. for (i = 0; i < size / sizeof (ulong); i++) {
  356. mem[i] = ~i;
  357. if (i % 1024 == 0)
  358. WATCHDOG_RESET ();
  359. }
  360. for (i = 0; i < size / sizeof (ulong) && ret == 0; i++) {
  361. readback = mem[i];
  362. if (readback != ~i) {
  363. post_log ("Memory error at %08x, "
  364. "wrote %08x, read %08x !\n",
  365. mem + i, ~i, readback);
  366. ret = -1;
  367. break;
  368. }
  369. if (i % 1024 == 0)
  370. WATCHDOG_RESET ();
  371. }
  372. return ret;
  373. }
  374. static int memory_post_tests (unsigned long start, unsigned long size)
  375. {
  376. int ret = 0;
  377. if (ret == 0)
  378. ret = memory_post_dataline ((unsigned long long *)start);
  379. WATCHDOG_RESET ();
  380. if (ret == 0)
  381. ret = memory_post_addrline ((ulong *)start, (ulong *)start, size);
  382. WATCHDOG_RESET ();
  383. if (ret == 0)
  384. ret = memory_post_addrline ((ulong *)(start + size - 8),
  385. (ulong *)start, size);
  386. WATCHDOG_RESET ();
  387. if (ret == 0)
  388. ret = memory_post_test1 (start, size, 0x00000000);
  389. WATCHDOG_RESET ();
  390. if (ret == 0)
  391. ret = memory_post_test1 (start, size, 0xffffffff);
  392. WATCHDOG_RESET ();
  393. if (ret == 0)
  394. ret = memory_post_test1 (start, size, 0x55555555);
  395. WATCHDOG_RESET ();
  396. if (ret == 0)
  397. ret = memory_post_test1 (start, size, 0xaaaaaaaa);
  398. WATCHDOG_RESET ();
  399. if (ret == 0)
  400. ret = memory_post_test2 (start, size);
  401. WATCHDOG_RESET ();
  402. if (ret == 0)
  403. ret = memory_post_test3 (start, size);
  404. WATCHDOG_RESET ();
  405. if (ret == 0)
  406. ret = memory_post_test4 (start, size);
  407. WATCHDOG_RESET ();
  408. return ret;
  409. }
  410. int memory_post_test (int flags)
  411. {
  412. int ret = 0;
  413. bd_t *bd = gd->bd;
  414. unsigned long memsize = (bd->bi_memsize >= 256 << 20 ?
  415. 256 << 20 : bd->bi_memsize) - (1 << 20);
  416. /* Limit area to be tested with the board info struct */
  417. if (CONFIG_SYS_SDRAM_BASE + memsize > (ulong)bd)
  418. memsize = (ulong)bd - CONFIG_SYS_SDRAM_BASE;
  419. if (flags & POST_SLOWTEST) {
  420. ret = memory_post_tests (CONFIG_SYS_SDRAM_BASE, memsize);
  421. } else { /* POST_NORMAL */
  422. unsigned long i;
  423. for (i = 0; i < (memsize >> 20) && ret == 0; i++) {
  424. if (ret == 0)
  425. ret = memory_post_tests (i << 20, 0x800);
  426. if (ret == 0)
  427. ret = memory_post_tests ((i << 20) + 0xff800, 0x800);
  428. }
  429. }
  430. return ret;
  431. }
  432. #endif /* CONFIG_POST & CONFIG_SYS_POST_MEMORY */