memory.c 14 KB

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