post.c 8.5 KB

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