post.c 9.7 KB

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