post.c 9.6 KB

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