post.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <stdio_dev.h>
  25. #include <watchdog.h>
  26. #include <post.h>
  27. #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
  28. #include <asm/gpio.h>
  29. #endif
  30. #ifdef CONFIG_LOGBUFFER
  31. #include <logbuff.h>
  32. #endif
  33. DECLARE_GLOBAL_DATA_PTR;
  34. #define POST_MAX_NUMBER 32
  35. #define BOOTMODE_MAGIC 0xDEAD0000
  36. int post_init_f(void)
  37. {
  38. int res = 0;
  39. unsigned int i;
  40. for (i = 0; i < post_list_size; i++) {
  41. struct post_test *test = post_list + i;
  42. if (test->init_f && test->init_f())
  43. res = -1;
  44. }
  45. gd->post_init_f_time = post_time_ms(0);
  46. if (!gd->post_init_f_time)
  47. printf("%s: post_time_ms not implemented\n", __FILE__);
  48. return res;
  49. }
  50. /*
  51. * Supply a default implementation for post_hotkeys_pressed() for boards
  52. * without hotkey support. We always return 0 here, so that the
  53. * long-running tests won't be started.
  54. *
  55. * Boards with hotkey support can override this weak default function
  56. * by defining one in their board specific code.
  57. */
  58. int __post_hotkeys_pressed(void)
  59. {
  60. #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
  61. int ret;
  62. unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
  63. ret = gpio_request(gpio, "hotkeys");
  64. if (ret) {
  65. printf("POST: gpio hotkey request failed\n");
  66. return 0;
  67. }
  68. gpio_direction_input(gpio);
  69. ret = gpio_get_value(gpio);
  70. gpio_free(gpio);
  71. return ret;
  72. #endif
  73. return 0; /* No hotkeys supported */
  74. }
  75. int post_hotkeys_pressed(void)
  76. __attribute__((weak, alias("__post_hotkeys_pressed")));
  77. void post_bootmode_init(void)
  78. {
  79. int bootmode = post_bootmode_get(0);
  80. int newword;
  81. if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST))
  82. newword = BOOTMODE_MAGIC | POST_SLOWTEST;
  83. else if (bootmode == 0)
  84. newword = BOOTMODE_MAGIC | POST_POWERON;
  85. else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST)
  86. newword = BOOTMODE_MAGIC | POST_NORMAL;
  87. else
  88. /* Use old value */
  89. newword = post_word_load() & ~POST_COLDBOOT;
  90. if (bootmode == 0)
  91. /* We are booting after power-on */
  92. newword |= POST_COLDBOOT;
  93. post_word_store(newword);
  94. /* Reset activity record */
  95. gd->post_log_word = 0;
  96. gd->post_log_res = 0;
  97. }
  98. int post_bootmode_get(unsigned int *last_test)
  99. {
  100. unsigned long word = post_word_load();
  101. int bootmode;
  102. if ((word & 0xFFFF0000) != BOOTMODE_MAGIC)
  103. return 0;
  104. bootmode = word & 0x7F;
  105. if (last_test && (bootmode & POST_POWERTEST))
  106. *last_test = (word >> 8) & 0xFF;
  107. return bootmode;
  108. }
  109. /* POST tests run before relocation only mark status bits .... */
  110. static void post_log_mark_start(unsigned long testid)
  111. {
  112. gd->post_log_word |= testid;
  113. }
  114. static void post_log_mark_succ(unsigned long testid)
  115. {
  116. gd->post_log_res |= testid;
  117. }
  118. /* ... and the messages are output once we are relocated */
  119. void post_output_backlog(void)
  120. {
  121. int j;
  122. for (j = 0; j < post_list_size; j++) {
  123. if (gd->post_log_word & (post_list[j].testid)) {
  124. post_log("POST %s ", post_list[j].cmd);
  125. if (gd->post_log_res & post_list[j].testid)
  126. post_log("PASSED\n");
  127. else {
  128. post_log("FAILED\n");
  129. show_boot_progress(-31);
  130. }
  131. }
  132. }
  133. }
  134. static void post_bootmode_test_on(unsigned int last_test)
  135. {
  136. unsigned long word = post_word_load();
  137. word |= POST_POWERTEST;
  138. word |= (last_test & 0xFF) << 8;
  139. post_word_store(word);
  140. }
  141. static void post_bootmode_test_off(void)
  142. {
  143. unsigned long word = post_word_load();
  144. word &= ~POST_POWERTEST;
  145. post_word_store(word);
  146. }
  147. #ifndef CONFIG_POST_SKIP_ENV_FLAGS
  148. static void post_get_env_flags(int *test_flags)
  149. {
  150. int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
  151. POST_CRITICAL };
  152. char *var[] = { "post_poweron", "post_normal", "post_slowtest",
  153. "post_critical" };
  154. int varnum = ARRAY_SIZE(var);
  155. char list[128]; /* long enough for POST list */
  156. char *name;
  157. char *s;
  158. int last;
  159. int i, j;
  160. for (i = 0; i < varnum; i++) {
  161. if (getenv_f(var[i], list, sizeof(list)) <= 0)
  162. continue;
  163. for (j = 0; j < post_list_size; j++)
  164. test_flags[j] &= ~flag[i];
  165. last = 0;
  166. name = list;
  167. while (!last) {
  168. while (*name && *name == ' ')
  169. name++;
  170. if (*name == 0)
  171. break;
  172. s = name + 1;
  173. while (*s && *s != ' ')
  174. s++;
  175. if (*s == 0)
  176. last = 1;
  177. else
  178. *s = 0;
  179. for (j = 0; j < post_list_size; j++) {
  180. if (strcmp(post_list[j].cmd, name) == 0) {
  181. test_flags[j] |= flag[i];
  182. break;
  183. }
  184. }
  185. if (j == post_list_size)
  186. printf("No such test: %s\n", name);
  187. name = s + 1;
  188. }
  189. }
  190. }
  191. #endif
  192. static void post_get_flags(int *test_flags)
  193. {
  194. int j;
  195. for (j = 0; j < post_list_size; j++)
  196. test_flags[j] = post_list[j].flags;
  197. #ifndef CONFIG_POST_SKIP_ENV_FLAGS
  198. post_get_env_flags(test_flags);
  199. #endif
  200. for (j = 0; j < post_list_size; j++)
  201. if (test_flags[j] & POST_POWERON)
  202. test_flags[j] |= POST_SLOWTEST;
  203. }
  204. void __show_post_progress(unsigned int test_num, int before, int result)
  205. {
  206. }
  207. void show_post_progress(unsigned int, int, int)
  208. __attribute__((weak, alias("__show_post_progress")));
  209. static int post_run_single(struct post_test *test,
  210. int test_flags, int flags, unsigned int i)
  211. {
  212. if ((flags & test_flags & POST_ALWAYS) &&
  213. (flags & test_flags & POST_MEM)) {
  214. WATCHDOG_RESET();
  215. if (!(flags & POST_REBOOT)) {
  216. if ((test_flags & POST_REBOOT) &&
  217. !(flags & POST_MANUAL)) {
  218. post_bootmode_test_on(
  219. (gd->flags & GD_FLG_POSTFAIL) ?
  220. POST_FAIL_SAVE | i : i);
  221. }
  222. if (test_flags & POST_PREREL)
  223. post_log_mark_start(test->testid);
  224. else
  225. post_log("POST %s ", test->cmd);
  226. }
  227. show_post_progress(i, POST_BEFORE, POST_FAILED);
  228. if (test_flags & POST_PREREL) {
  229. if ((*test->test)(flags) == 0) {
  230. post_log_mark_succ(test->testid);
  231. show_post_progress(i, POST_AFTER, POST_PASSED);
  232. } else {
  233. show_post_progress(i, POST_AFTER, POST_FAILED);
  234. if (test_flags & POST_CRITICAL)
  235. gd->flags |= GD_FLG_POSTFAIL;
  236. if (test_flags & POST_STOP)
  237. gd->flags |= GD_FLG_POSTSTOP;
  238. }
  239. } else {
  240. if ((*test->test)(flags) != 0) {
  241. post_log("FAILED\n");
  242. show_boot_progress(-32);
  243. show_post_progress(i, POST_AFTER, POST_FAILED);
  244. if (test_flags & POST_CRITICAL)
  245. gd->flags |= GD_FLG_POSTFAIL;
  246. if (test_flags & POST_STOP)
  247. gd->flags |= GD_FLG_POSTSTOP;
  248. } else {
  249. post_log("PASSED\n");
  250. show_post_progress(i, POST_AFTER, POST_PASSED);
  251. }
  252. }
  253. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
  254. post_bootmode_test_off();
  255. return 0;
  256. } else {
  257. return -1;
  258. }
  259. }
  260. int post_run(char *name, int flags)
  261. {
  262. unsigned int i;
  263. int test_flags[POST_MAX_NUMBER];
  264. post_get_flags(test_flags);
  265. if (name == NULL) {
  266. unsigned int last;
  267. if (gd->flags & GD_FLG_POSTSTOP)
  268. return 0;
  269. if (post_bootmode_get(&last) & POST_POWERTEST) {
  270. if (last & POST_FAIL_SAVE) {
  271. last &= ~POST_FAIL_SAVE;
  272. gd->flags |= GD_FLG_POSTFAIL;
  273. }
  274. if (last < post_list_size &&
  275. (flags & test_flags[last] & POST_ALWAYS) &&
  276. (flags & test_flags[last] & POST_MEM)) {
  277. post_run_single(post_list + last,
  278. test_flags[last],
  279. flags | POST_REBOOT, last);
  280. for (i = last + 1; i < post_list_size; i++) {
  281. if (gd->flags & GD_FLG_POSTSTOP)
  282. break;
  283. post_run_single(post_list + i,
  284. test_flags[i],
  285. flags, i);
  286. }
  287. }
  288. } else {
  289. for (i = 0; i < post_list_size; i++) {
  290. if (gd->flags & GD_FLG_POSTSTOP)
  291. break;
  292. post_run_single(post_list + i,
  293. test_flags[i],
  294. flags, i);
  295. }
  296. }
  297. return 0;
  298. } else {
  299. for (i = 0; i < post_list_size; i++) {
  300. if (strcmp(post_list[i].cmd, name) == 0)
  301. break;
  302. }
  303. if (i < post_list_size) {
  304. WATCHDOG_RESET();
  305. return post_run_single(post_list + i,
  306. test_flags[i],
  307. flags, i);
  308. } else {
  309. return -1;
  310. }
  311. }
  312. }
  313. static int post_info_single(struct post_test *test, int full)
  314. {
  315. if (test->flags & POST_MANUAL) {
  316. if (full)
  317. printf("%s - %s\n"
  318. " %s\n", test->cmd, test->name, test->desc);
  319. else
  320. printf(" %-15s - %s\n", test->cmd, test->name);
  321. return 0;
  322. } else {
  323. return -1;
  324. }
  325. }
  326. int post_info(char *name)
  327. {
  328. unsigned int i;
  329. if (name == NULL) {
  330. for (i = 0; i < post_list_size; i++)
  331. post_info_single(post_list + i, 0);
  332. return 0;
  333. } else {
  334. for (i = 0; i < post_list_size; i++) {
  335. if (strcmp(post_list[i].cmd, name) == 0)
  336. break;
  337. }
  338. if (i < post_list_size)
  339. return post_info_single(post_list + i, 1);
  340. else
  341. return -1;
  342. }
  343. }
  344. int post_log(char *format, ...)
  345. {
  346. va_list args;
  347. char printbuffer[CONFIG_SYS_PBSIZE];
  348. va_start(args, format);
  349. /* For this to work, printbuffer must be larger than
  350. * anything we ever want to print.
  351. */
  352. vsprintf(printbuffer, format, args);
  353. va_end(args);
  354. #ifdef CONFIG_LOGBUFFER
  355. /* Send to the logbuffer */
  356. logbuff_log(printbuffer);
  357. #else
  358. /* Send to the stdout file */
  359. puts(printbuffer);
  360. #endif
  361. return 0;
  362. }
  363. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  364. void post_reloc(void)
  365. {
  366. unsigned int i;
  367. /*
  368. * We have to relocate the test table manually
  369. */
  370. for (i = 0; i < post_list_size; i++) {
  371. ulong addr;
  372. struct post_test *test = post_list + i;
  373. if (test->name) {
  374. addr = (ulong)(test->name) + gd->reloc_off;
  375. test->name = (char *)addr;
  376. }
  377. if (test->cmd) {
  378. addr = (ulong)(test->cmd) + gd->reloc_off;
  379. test->cmd = (char *)addr;
  380. }
  381. if (test->desc) {
  382. addr = (ulong)(test->desc) + gd->reloc_off;
  383. test->desc = (char *)addr;
  384. }
  385. if (test->test) {
  386. addr = (ulong)(test->test) + gd->reloc_off;
  387. test->test = (int (*)(int flags)) addr;
  388. }
  389. if (test->init_f) {
  390. addr = (ulong)(test->init_f) + gd->reloc_off;
  391. test->init_f = (int (*)(void)) addr;
  392. }
  393. if (test->reloc) {
  394. addr = (ulong)(test->reloc) + gd->reloc_off;
  395. test->reloc = (void (*)(void)) addr;
  396. test->reloc();
  397. }
  398. }
  399. }
  400. #endif
  401. /*
  402. * Some tests (e.g. SYSMON) need the time when post_init_f started,
  403. * but we cannot use get_timer() at this point.
  404. *
  405. * On PowerPC we implement it using the timebase register.
  406. */
  407. unsigned long post_time_ms(unsigned long base)
  408. {
  409. #if defined(CONFIG_PPC) || defined(CONFIG_ARM)
  410. return (unsigned long)(get_ticks() / (get_tbclk() / CONFIG_SYS_HZ))
  411. - base;
  412. #else
  413. #warning "Not implemented yet"
  414. return 0; /* Not implemented yet */
  415. #endif
  416. }