post.c 9.6 KB

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