post.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #include <console.h>
  25. #include <watchdog.h>
  26. #include <post.h>
  27. #ifdef CONFIG_LOGBUFFER
  28. #include <logbuff.h>
  29. #endif
  30. #ifdef CONFIG_POST
  31. #define POST_MAX_NUMBER 32
  32. #define BOOTMODE_MAGIC 0xDEAD0000
  33. int post_init_f (void)
  34. {
  35. DECLARE_GLOBAL_DATA_PTR;
  36. int res = 0;
  37. unsigned int i;
  38. for (i = 0; i < post_list_size; i++) {
  39. struct post_test *test = post_list + i;
  40. if (test->init_f && test->init_f()) {
  41. res = -1;
  42. }
  43. }
  44. gd->post_init_f_time = post_time_ms(0);
  45. if (!gd->post_init_f_time)
  46. {
  47. printf("post/post.c: post_time_ms seems not to be implemented\n");
  48. }
  49. return res;
  50. }
  51. void post_bootmode_init (void)
  52. {
  53. DECLARE_GLOBAL_DATA_PTR;
  54. int bootmode = post_bootmode_get (0);
  55. if (post_hotkeys_pressed(gd) && !(bootmode & POST_POWERTEST)) {
  56. bootmode = POST_SLOWTEST;
  57. } else if (bootmode == 0) {
  58. bootmode = POST_POWERON;
  59. } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
  60. bootmode = POST_NORMAL;
  61. } else {
  62. return;
  63. }
  64. post_word_store (BOOTMODE_MAGIC | bootmode);
  65. /* Reset activity record */
  66. gd->post_log_word = 0;
  67. }
  68. int post_bootmode_get (unsigned int *last_test)
  69. {
  70. unsigned long word = post_word_load ();
  71. int bootmode;
  72. if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
  73. return 0;
  74. }
  75. bootmode = word & 0xFF;
  76. if (last_test && (bootmode & POST_POWERTEST)) {
  77. *last_test = (word >> 8) & 0xFF;
  78. }
  79. return bootmode;
  80. }
  81. /* POST tests run before relocation only mark status bits .... */
  82. static void post_log_mark_start ( unsigned long testid )
  83. {
  84. DECLARE_GLOBAL_DATA_PTR;
  85. gd->post_log_word |= (testid)<<16;
  86. }
  87. static void post_log_mark_succ ( unsigned long testid )
  88. {
  89. DECLARE_GLOBAL_DATA_PTR;
  90. gd->post_log_word |= testid;
  91. }
  92. /* ... and the messages are output once we are relocated */
  93. void post_output_backlog ( void )
  94. {
  95. DECLARE_GLOBAL_DATA_PTR;
  96. int j;
  97. for (j = 0; j < post_list_size; j++) {
  98. if (gd->post_log_word & (post_list[j].testid<<16)) {
  99. post_log ("POST %s ", post_list[j].cmd);
  100. if (gd->post_log_word & post_list[j].testid)
  101. post_log ("PASSED\n");
  102. else
  103. post_log ("FAILED\n");
  104. }
  105. }
  106. }
  107. static void post_bootmode_test_on (unsigned int last_test)
  108. {
  109. unsigned long word = post_word_load ();
  110. word |= POST_POWERTEST;
  111. word |= (last_test & 0xFF) << 8;
  112. post_word_store (word);
  113. }
  114. static void post_bootmode_test_off (void)
  115. {
  116. unsigned long word = post_word_load ();
  117. word &= ~POST_POWERTEST;
  118. post_word_store (word);
  119. }
  120. static void post_get_flags (int *test_flags)
  121. {
  122. int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST };
  123. char *var[] = { "post_poweron", "post_normal", "post_slowtest" };
  124. int varnum = sizeof (var) / sizeof (var[0]);
  125. char list[128]; /* long enough for POST list */
  126. char *name;
  127. char *s;
  128. int last;
  129. int i, j;
  130. for (j = 0; j < post_list_size; j++) {
  131. test_flags[j] = post_list[j].flags;
  132. }
  133. for (i = 0; i < varnum; i++) {
  134. if (getenv_r (var[i], list, sizeof (list)) <= 0)
  135. continue;
  136. for (j = 0; j < post_list_size; j++) {
  137. test_flags[j] &= ~flag[i];
  138. }
  139. last = 0;
  140. name = list;
  141. while (!last) {
  142. while (*name && *name == ' ')
  143. name++;
  144. if (*name == 0)
  145. break;
  146. s = name + 1;
  147. while (*s && *s != ' ')
  148. s++;
  149. if (*s == 0)
  150. last = 1;
  151. else
  152. *s = 0;
  153. for (j = 0; j < post_list_size; j++) {
  154. if (strcmp (post_list[j].cmd, name) == 0) {
  155. test_flags[j] |= flag[i];
  156. break;
  157. }
  158. }
  159. if (j == post_list_size) {
  160. printf ("No such test: %s\n", name);
  161. }
  162. name = s + 1;
  163. }
  164. }
  165. for (j = 0; j < post_list_size; j++) {
  166. if (test_flags[j] & POST_POWERON) {
  167. test_flags[j] |= POST_SLOWTEST;
  168. }
  169. }
  170. }
  171. static int post_run_single (struct post_test *test,
  172. int test_flags, int flags, unsigned int i)
  173. {
  174. if ((flags & test_flags & POST_ALWAYS) &&
  175. (flags & test_flags & POST_MEM)) {
  176. WATCHDOG_RESET ();
  177. if (!(flags & POST_REBOOT)) {
  178. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  179. post_bootmode_test_on (i);
  180. }
  181. if (test_flags & POST_PREREL)
  182. post_log_mark_start ( test->testid );
  183. else
  184. post_log ("POST %s ", test->cmd);
  185. }
  186. if (test_flags & POST_PREREL) {
  187. if ((*test->test) (flags) == 0)
  188. post_log_mark_succ ( test->testid );
  189. } else {
  190. if ((*test->test) (flags) != 0)
  191. post_log ("FAILED\n");
  192. else
  193. post_log ("PASSED\n");
  194. }
  195. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  196. post_bootmode_test_off ();
  197. }
  198. return 0;
  199. } else {
  200. return -1;
  201. }
  202. }
  203. int post_run (char *name, int flags)
  204. {
  205. unsigned int i;
  206. int test_flags[POST_MAX_NUMBER];
  207. post_get_flags (test_flags);
  208. if (name == NULL) {
  209. unsigned int last;
  210. if (post_bootmode_get (&last) & POST_POWERTEST) {
  211. if (last < post_list_size &&
  212. (flags & test_flags[last] & POST_ALWAYS) &&
  213. (flags & test_flags[last] & POST_MEM)) {
  214. post_run_single (post_list + last,
  215. test_flags[last],
  216. flags | POST_REBOOT, last);
  217. for (i = last + 1; i < post_list_size; i++) {
  218. post_run_single (post_list + i,
  219. test_flags[i],
  220. flags, i);
  221. }
  222. }
  223. } else {
  224. for (i = 0; i < post_list_size; i++) {
  225. post_run_single (post_list + i,
  226. test_flags[i],
  227. flags, i);
  228. }
  229. }
  230. return 0;
  231. } else {
  232. for (i = 0; i < post_list_size; i++) {
  233. if (strcmp (post_list[i].cmd, name) == 0)
  234. break;
  235. }
  236. if (i < post_list_size) {
  237. return post_run_single (post_list + i,
  238. test_flags[i],
  239. flags, i);
  240. } else {
  241. return -1;
  242. }
  243. }
  244. }
  245. static int post_info_single (struct post_test *test, int full)
  246. {
  247. if (test->flags & POST_MANUAL) {
  248. if (full)
  249. printf ("%s - %s\n"
  250. " %s\n", test->cmd, test->name, test->desc);
  251. else
  252. printf (" %-15s - %s\n", test->cmd, test->name);
  253. return 0;
  254. } else {
  255. return -1;
  256. }
  257. }
  258. int post_info (char *name)
  259. {
  260. unsigned int i;
  261. if (name == NULL) {
  262. for (i = 0; i < post_list_size; i++) {
  263. post_info_single (post_list + i, 0);
  264. }
  265. return 0;
  266. } else {
  267. for (i = 0; i < post_list_size; i++) {
  268. if (strcmp (post_list[i].cmd, name) == 0)
  269. break;
  270. }
  271. if (i < post_list_size) {
  272. return post_info_single (post_list + i, 1);
  273. } else {
  274. return -1;
  275. }
  276. }
  277. }
  278. int post_log (char *format, ...)
  279. {
  280. va_list args;
  281. uint i;
  282. char printbuffer[CFG_PBSIZE];
  283. va_start (args, format);
  284. /* For this to work, printbuffer must be larger than
  285. * anything we ever want to print.
  286. */
  287. i = vsprintf (printbuffer, format, args);
  288. va_end (args);
  289. #ifdef CONFIG_LOGBUFFER
  290. /* Send to the logbuffer */
  291. logbuff_log (printbuffer);
  292. #else
  293. /* Send to the stdout file */
  294. puts (printbuffer);
  295. #endif
  296. return 0;
  297. }
  298. void post_reloc (void)
  299. {
  300. DECLARE_GLOBAL_DATA_PTR;
  301. unsigned int i;
  302. /*
  303. * We have to relocate the test table manually
  304. */
  305. for (i = 0; i < post_list_size; i++) {
  306. ulong addr;
  307. struct post_test *test = post_list + i;
  308. if (test->name) {
  309. addr = (ulong) (test->name) + gd->reloc_off;
  310. test->name = (char *) addr;
  311. }
  312. if (test->cmd) {
  313. addr = (ulong) (test->cmd) + gd->reloc_off;
  314. test->cmd = (char *) addr;
  315. }
  316. if (test->desc) {
  317. addr = (ulong) (test->desc) + gd->reloc_off;
  318. test->desc = (char *) addr;
  319. }
  320. if (test->test) {
  321. addr = (ulong) (test->test) + gd->reloc_off;
  322. test->test = (int (*)(int flags)) addr;
  323. }
  324. if (test->init_f) {
  325. addr = (ulong) (test->init_f) + gd->reloc_off;
  326. test->init_f = (int (*)(void)) addr;
  327. }
  328. if (test->reloc) {
  329. addr = (ulong) (test->reloc) + gd->reloc_off;
  330. test->reloc = (void (*)(void)) addr;
  331. test->reloc();
  332. }
  333. }
  334. }
  335. /*
  336. * Some tests (e.g. SYSMON) need the time when post_init_f started,
  337. * but we cannot use get_timer() at this point.
  338. *
  339. * On PowerPC we implement it using the timebase register.
  340. */
  341. unsigned long post_time_ms (unsigned long base)
  342. {
  343. #ifdef CONFIG_PPC
  344. return (unsigned long)get_ticks () / (get_tbclk () / CFG_HZ) - base;
  345. #else
  346. return 0; /* Not implemented yet */
  347. #endif
  348. }
  349. #endif /* CONFIG_POST */