post.c 9.6 KB

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