memory-test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // taken from Qi boot loader
  2. /*
  3. * Copyright (c) 2009 Openmoko Inc.
  4. *
  5. * Author: Andy Green <andy@openmoko.com>
  6. *
  7. * Memory test routines
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #define APPLICATION_TITLE "Memory Test";
  26. #define APPLICATION_TITLE2 "Memory Check";
  27. #include "application.h"
  28. // the compiler cannot handle this:
  29. //extern uint8_t __START_SDRAM;
  30. //#define RAM_START (&__START_SDRAM)
  31. // so:
  32. #define RAM_START ((uint8_t *)0x10000000)
  33. // redirect Qi routines to the correct EEPROM routines
  34. #define puts print
  35. #define print32 print_hex
  36. #define printdec print_uint
  37. void memory_test(void *start, unsigned int length);
  38. void memory_check(void *start, unsigned int length);
  39. // this must be the first executable code as the loader executes from the first program address
  40. ReturnType mem(int block, int status)
  41. {
  42. APPLICATION_INITIALISE();
  43. {
  44. unsigned int size = ram_size();
  45. if (0 == status) {
  46. memory_test(RAM_START, size);
  47. } else {
  48. memory_check(RAM_START, size);
  49. }
  50. }
  51. APPLICATION_FINALISE(0, 0);
  52. }
  53. int memory_test_const32(void * start, unsigned int length, uint32_t value)
  54. {
  55. int errors = 0;
  56. uint32_t * p = (uint32_t *)start;
  57. uint32_t * pend = (uint32_t *)(start + length);
  58. int count = length >> 2;
  59. puts(".");
  60. while (p < pend)
  61. *p++ = value;
  62. p = (uint32_t *)start;
  63. count = length >> 2;
  64. while (count--)
  65. if (*p++ != value) {
  66. puts(" A:");
  67. print32((long)p - 4);
  68. puts("=");
  69. print32((uint32_t)p[-4]);
  70. puts("/");
  71. print32((uint32_t)value);
  72. errors++;
  73. }
  74. return errors;
  75. }
  76. int memory_test_ads(void * start, unsigned int length, uint32_t mask)
  77. {
  78. int errors = 0;
  79. uint32_t * p = (uint32_t *)start;
  80. uint32_t * pend = (uint32_t *)(start + length);
  81. puts(".");
  82. while (p < pend)
  83. if ((uint32_t)p & mask)
  84. *p++ = 0xffffffff;
  85. else
  86. *p++ = 0;
  87. p = (uint32_t *)start;
  88. while (p < pend) {
  89. if ((uint32_t)p & mask) {
  90. if (*p++ != 0xffffffff) {
  91. puts(" B:");
  92. print32((long)p - 4);
  93. puts("/");
  94. print32((uint32_t)mask);
  95. errors++;
  96. }
  97. } else {
  98. if (*p++) {
  99. puts(" C:");
  100. print32((long)p - 4);
  101. puts("/");
  102. print32((uint32_t)mask);
  103. errors++;
  104. }
  105. }
  106. }
  107. return errors;
  108. }
  109. int memory_test_walking1(void * start, unsigned int length)
  110. {
  111. int errors = 0;
  112. uint32_t value = 1;
  113. while (value) {
  114. errors += memory_test_const32(start, length, value);
  115. value <<= 1;
  116. }
  117. return errors;
  118. }
  119. /* negative runs == run forever */
  120. #define INTERRUPT_HERE() \
  121. do { \
  122. if (console_input_available()) { \
  123. console_input_char(); \
  124. return; \
  125. } \
  126. } while (0)
  127. void memory_test(void *start, unsigned int length)
  128. {
  129. int errors = 0;
  130. int series = 0;
  131. int mask;
  132. puts("\nMemory: ");
  133. print32((uint32_t)start);
  134. puts(" length ");
  135. printdec(length >> 20);
  136. puts(" MB\n");
  137. for (;;) {
  138. puts("Test series ");
  139. printdec(series + 1);
  140. puts(" ");
  141. /* these are looking at data issues, they flood the whole
  142. * array with the same data
  143. */
  144. errors += memory_test_const32(start, length, 0x55555555);
  145. INTERRUPT_HERE();
  146. errors += memory_test_const32(start, length, 0xaaaaaaaa);
  147. INTERRUPT_HERE();
  148. errors += memory_test_const32(start, length, 0x55aa55aa);
  149. INTERRUPT_HERE();
  150. errors += memory_test_const32(start, length, 0xaa55aa55);
  151. INTERRUPT_HERE();
  152. errors += memory_test_const32(start, length, 0x00ff00ff);
  153. INTERRUPT_HERE();
  154. errors += memory_test_const32(start, length, 0xff00ff00);
  155. INTERRUPT_HERE();
  156. errors += memory_test_walking1(start, length);
  157. INTERRUPT_HERE();
  158. puts("+");
  159. /* this is looking at addressing issues, it floods only
  160. * addresses meeting a walking mask with 0xffffffff (the rest
  161. * is zeroed), and makes sure all the bits are only seen where
  162. * they were placed
  163. */
  164. mask = 1;
  165. while (! (length & mask)) {
  166. errors += memory_test_ads(start, length, mask);
  167. INTERRUPT_HERE();
  168. mask = mask << 1;
  169. }
  170. if (0 == errors) {
  171. puts("OK");
  172. } else {
  173. puts(" Errors: ");
  174. printdec(errors);
  175. }
  176. puts("\n");
  177. series++;
  178. }
  179. }
  180. void memory_check(void *start, unsigned int length)
  181. {
  182. int i;
  183. int pass = 1;
  184. uint32_t mega = length >> 20;
  185. volatile uint32_t *memory = (uint32_t *)start;
  186. puts("\nMemory: ");
  187. print32((uint32_t)start);
  188. puts(" length ");
  189. printdec(mega);
  190. puts(" MB [");
  191. for (i = 0; i < mega; ++i) {
  192. int j;
  193. int flag = 1;
  194. for (j = 0; j < (1 << 20) ; j += 1024) {
  195. volatile uint32_t *p = (volatile uint32_t *)&memory[((i << 20) + j) >> 2];
  196. uint32_t s = *p;
  197. *p = ~s;
  198. if (*p != ~s) {
  199. flag = 0;
  200. pass = 0;
  201. break;
  202. }
  203. *p = 0x5aa55aa5;
  204. if (*p != 0x5aa55aa5) {
  205. flag = 0;
  206. pass = 0;
  207. break;
  208. }
  209. *p = 0xa55aa55a;
  210. if (*p != 0xa55aa55a) {
  211. flag = 0;
  212. pass = 0;
  213. break;
  214. }
  215. *p = s;
  216. if (*p != s) {
  217. flag = 0;
  218. pass = 0;
  219. break;
  220. }
  221. if (0 == j && 0 != i) {
  222. *memory = 0;
  223. *p = 0xcafedeca;
  224. if (0 != *memory) {
  225. flag = 2;
  226. pass = 0;
  227. break;
  228. }
  229. }
  230. }
  231. if (2 == flag) {
  232. print_char('M');
  233. } else {
  234. print_char(flag ? '.' : 'F');
  235. }
  236. }
  237. print("]\n");
  238. print(pass ? "PASS" : "FAIL");
  239. print(": Memory Check\n");
  240. }