memory.c 14 KB

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