misc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2013 Samsung Electronics
  3. * Przemyslaw Marczak <p.marczak@samsung.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <lcd.h>
  9. #include <libtizen.h>
  10. #include <samsung/misc.h>
  11. #include <errno.h>
  12. #include <version.h>
  13. #include <malloc.h>
  14. #include <linux/sizes.h>
  15. #include <asm/arch/cpu.h>
  16. #include <asm/arch/gpio.h>
  17. #include <asm/gpio.h>
  18. #include <linux/input.h>
  19. #include <power/pmic.h>
  20. #include <mmc.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #ifdef CONFIG_SET_DFU_ALT_INFO
  23. void set_dfu_alt_info(void)
  24. {
  25. size_t buf_size = CONFIG_SET_DFU_ALT_BUF_LEN;
  26. ALLOC_CACHE_ALIGN_BUFFER(char, buf, buf_size);
  27. char *alt_info = "Settings not found!";
  28. char *status = "error!\n";
  29. char *alt_setting;
  30. char *alt_sep;
  31. int offset = 0;
  32. puts("DFU alt info setting: ");
  33. alt_setting = get_dfu_alt_boot();
  34. if (alt_setting) {
  35. setenv("dfu_alt_boot", alt_setting);
  36. offset = snprintf(buf, buf_size, "%s", alt_setting);
  37. }
  38. alt_setting = get_dfu_alt_system();
  39. if (alt_setting) {
  40. if (offset)
  41. alt_sep = ";";
  42. else
  43. alt_sep = "";
  44. offset += snprintf(buf + offset, buf_size - offset,
  45. "%s%s", alt_sep, alt_setting);
  46. }
  47. if (offset) {
  48. alt_info = buf;
  49. status = "done\n";
  50. }
  51. setenv("dfu_alt_info", alt_info);
  52. puts(status);
  53. }
  54. #endif
  55. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  56. void set_board_info(void)
  57. {
  58. char info[64];
  59. snprintf(info, ARRAY_SIZE(info), "%u.%u", (s5p_cpu_rev & 0xf0) >> 4,
  60. s5p_cpu_rev & 0xf);
  61. setenv("soc_rev", info);
  62. snprintf(info, ARRAY_SIZE(info), "%x", s5p_cpu_id);
  63. setenv("soc_id", info);
  64. #ifdef CONFIG_REVISION_TAG
  65. snprintf(info, ARRAY_SIZE(info), "%x", get_board_rev());
  66. setenv("board_rev", info);
  67. #endif
  68. #ifdef CONFIG_OF_LIBFDT
  69. const char *bdtype = "";
  70. const char *bdname = CONFIG_SYS_BOARD;
  71. #ifdef CONFIG_BOARD_TYPES
  72. bdtype = get_board_type();
  73. sprintf(info, "%s%s", bdname, bdtype);
  74. setenv("boardname", info);
  75. #endif
  76. snprintf(info, ARRAY_SIZE(info), "%s%x-%s%s.dtb",
  77. CONFIG_SYS_SOC, s5p_cpu_id, bdname, bdtype);
  78. setenv("fdtfile", info);
  79. #endif
  80. }
  81. #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
  82. #ifdef CONFIG_LCD_MENU
  83. static int power_key_pressed(u32 reg)
  84. {
  85. struct pmic *pmic;
  86. u32 status;
  87. u32 mask;
  88. pmic = pmic_get(KEY_PWR_PMIC_NAME);
  89. if (!pmic) {
  90. printf("%s: Not found\n", KEY_PWR_PMIC_NAME);
  91. return 0;
  92. }
  93. if (pmic_probe(pmic))
  94. return 0;
  95. if (reg == KEY_PWR_STATUS_REG)
  96. mask = KEY_PWR_STATUS_MASK;
  97. else
  98. mask = KEY_PWR_INTERRUPT_MASK;
  99. if (pmic_reg_read(pmic, reg, &status))
  100. return 0;
  101. return !!(status & mask);
  102. }
  103. static int key_pressed(int key)
  104. {
  105. int value;
  106. switch (key) {
  107. case KEY_POWER:
  108. value = power_key_pressed(KEY_PWR_INTERRUPT_REG);
  109. break;
  110. case KEY_VOLUMEUP:
  111. value = !gpio_get_value(KEY_VOL_UP_GPIO);
  112. break;
  113. case KEY_VOLUMEDOWN:
  114. value = !gpio_get_value(KEY_VOL_DOWN_GPIO);
  115. break;
  116. default:
  117. value = 0;
  118. break;
  119. }
  120. return value;
  121. }
  122. static int check_keys(void)
  123. {
  124. int keys = 0;
  125. if (key_pressed(KEY_POWER))
  126. keys += KEY_POWER;
  127. if (key_pressed(KEY_VOLUMEUP))
  128. keys += KEY_VOLUMEUP;
  129. if (key_pressed(KEY_VOLUMEDOWN))
  130. keys += KEY_VOLUMEDOWN;
  131. return keys;
  132. }
  133. /*
  134. * 0 BOOT_MODE_INFO
  135. * 1 BOOT_MODE_THOR
  136. * 2 BOOT_MODE_UMS
  137. * 3 BOOT_MODE_DFU
  138. * 4 BOOT_MODE_EXIT
  139. */
  140. static char *
  141. mode_name[BOOT_MODE_EXIT + 1][2] = {
  142. {"DEVICE", ""},
  143. {"THOR", "thor"},
  144. {"UMS", "ums"},
  145. {"DFU", "dfu"},
  146. {"GPT", "gpt"},
  147. {"ENV", "env"},
  148. {"EXIT", ""},
  149. };
  150. static char *
  151. mode_info[BOOT_MODE_EXIT + 1] = {
  152. "info",
  153. "downloader",
  154. "mass storage",
  155. "firmware update",
  156. "restore",
  157. "default",
  158. "and run normal boot"
  159. };
  160. static char *
  161. mode_cmd[BOOT_MODE_EXIT + 1] = {
  162. "",
  163. "thor 0 mmc 0",
  164. "ums 0 mmc 0",
  165. "dfu 0 mmc 0",
  166. "gpt write mmc 0 $partitions",
  167. "env default -a; saveenv",
  168. "",
  169. };
  170. static void display_board_info(void)
  171. {
  172. #ifdef CONFIG_GENERIC_MMC
  173. struct mmc *mmc = find_mmc_device(0);
  174. #endif
  175. vidinfo_t *vid = &panel_info;
  176. lcd_position_cursor(4, 4);
  177. lcd_printf("%s\n\t", U_BOOT_VERSION);
  178. lcd_puts("\n\t\tBoard Info:\n");
  179. #ifdef CONFIG_SYS_BOARD
  180. lcd_printf("\tBoard name: %s\n", CONFIG_SYS_BOARD);
  181. #endif
  182. #ifdef CONFIG_REVISION_TAG
  183. lcd_printf("\tBoard rev: %u\n", get_board_rev());
  184. #endif
  185. lcd_printf("\tDRAM banks: %u\n", CONFIG_NR_DRAM_BANKS);
  186. lcd_printf("\tDRAM size: %u MB\n", gd->ram_size / SZ_1M);
  187. #ifdef CONFIG_GENERIC_MMC
  188. if (mmc) {
  189. if (!mmc->capacity)
  190. mmc_init(mmc);
  191. lcd_printf("\teMMC size: %llu MB\n", mmc->capacity / SZ_1M);
  192. }
  193. #endif
  194. if (vid)
  195. lcd_printf("\tDisplay resolution: %u x % u\n",
  196. vid->vl_col, vid->vl_row);
  197. lcd_printf("\tDisplay BPP: %u\n", 1 << vid->vl_bpix);
  198. }
  199. static int mode_leave_menu(int mode)
  200. {
  201. char *exit_option;
  202. char *exit_reset = "reset";
  203. char *exit_back = "back";
  204. cmd_tbl_t *cmd;
  205. int cmd_result;
  206. int leave;
  207. lcd_clear();
  208. switch (mode) {
  209. case BOOT_MODE_EXIT:
  210. return 1;
  211. case BOOT_MODE_INFO:
  212. display_board_info();
  213. exit_option = exit_back;
  214. leave = 0;
  215. break;
  216. default:
  217. cmd = find_cmd(mode_name[mode][1]);
  218. if (cmd) {
  219. printf("Enter: %s %s\n", mode_name[mode][0],
  220. mode_info[mode]);
  221. lcd_printf("\n\n\t%s %s\n", mode_name[mode][0],
  222. mode_info[mode]);
  223. lcd_puts("\n\tDo not turn off device before finish!\n");
  224. cmd_result = run_command(mode_cmd[mode], 0);
  225. if (cmd_result == CMD_RET_SUCCESS) {
  226. printf("Command finished\n");
  227. lcd_clear();
  228. lcd_printf("\n\n\t%s finished\n",
  229. mode_name[mode][0]);
  230. exit_option = exit_reset;
  231. leave = 1;
  232. } else {
  233. printf("Command error\n");
  234. lcd_clear();
  235. lcd_printf("\n\n\t%s command error\n",
  236. mode_name[mode][0]);
  237. exit_option = exit_back;
  238. leave = 0;
  239. }
  240. } else {
  241. lcd_puts("\n\n\tThis mode is not supported.\n");
  242. exit_option = exit_back;
  243. leave = 0;
  244. }
  245. }
  246. lcd_printf("\n\n\tPress POWER KEY to %s\n", exit_option);
  247. /* Clear PWR button Rising edge interrupt status flag */
  248. power_key_pressed(KEY_PWR_INTERRUPT_REG);
  249. /* Wait for PWR key */
  250. while (!key_pressed(KEY_POWER))
  251. mdelay(1);
  252. lcd_clear();
  253. return leave;
  254. }
  255. static void display_download_menu(int mode)
  256. {
  257. char *selection[BOOT_MODE_EXIT + 1];
  258. int i;
  259. for (i = 0; i <= BOOT_MODE_EXIT; i++)
  260. selection[i] = "[ ]";
  261. selection[mode] = "[=>]";
  262. lcd_clear();
  263. lcd_printf("\n\n\t\tDownload Mode Menu\n\n");
  264. for (i = 0; i <= BOOT_MODE_EXIT; i++)
  265. lcd_printf("\t%s %s - %s\n\n", selection[i],
  266. mode_name[i][0],
  267. mode_info[i]);
  268. }
  269. static void download_menu(void)
  270. {
  271. int mode = 0;
  272. int last_mode = 0;
  273. int run;
  274. int key = 0;
  275. int timeout = 15; /* sec */
  276. int i;
  277. display_download_menu(mode);
  278. lcd_puts("\n");
  279. /* Start count if no key is pressed */
  280. while (check_keys())
  281. continue;
  282. while (timeout--) {
  283. lcd_printf("\r\tNormal boot will start in: %2.d seconds.",
  284. timeout);
  285. /* about 1000 ms in for loop */
  286. for (i = 0; i < 10; i++) {
  287. mdelay(100);
  288. key = check_keys();
  289. if (key)
  290. break;
  291. }
  292. if (key)
  293. break;
  294. }
  295. if (!key) {
  296. lcd_clear();
  297. return;
  298. }
  299. while (1) {
  300. run = 0;
  301. if (mode != last_mode)
  302. display_download_menu(mode);
  303. last_mode = mode;
  304. mdelay(200);
  305. key = check_keys();
  306. switch (key) {
  307. case KEY_POWER:
  308. run = 1;
  309. break;
  310. case KEY_VOLUMEUP:
  311. if (mode > 0)
  312. mode--;
  313. break;
  314. case KEY_VOLUMEDOWN:
  315. if (mode < BOOT_MODE_EXIT)
  316. mode++;
  317. break;
  318. default:
  319. break;
  320. }
  321. if (run) {
  322. if (mode_leave_menu(mode))
  323. run_command("reset", 0);
  324. display_download_menu(mode);
  325. }
  326. }
  327. lcd_clear();
  328. }
  329. void check_boot_mode(void)
  330. {
  331. int pwr_key;
  332. pwr_key = power_key_pressed(KEY_PWR_STATUS_REG);
  333. if (!pwr_key)
  334. return;
  335. /* Clear PWR button Rising edge interrupt status flag */
  336. power_key_pressed(KEY_PWR_INTERRUPT_REG);
  337. if (key_pressed(KEY_VOLUMEUP))
  338. download_menu();
  339. else if (key_pressed(KEY_VOLUMEDOWN))
  340. mode_leave_menu(BOOT_MODE_THOR);
  341. }
  342. void keys_init(void)
  343. {
  344. /* Set direction to input */
  345. gpio_direction_input(KEY_VOL_UP_GPIO);
  346. gpio_direction_input(KEY_VOL_DOWN_GPIO);
  347. }
  348. #endif /* CONFIG_LCD_MENU */
  349. #ifdef CONFIG_CMD_BMP
  350. void draw_logo(void)
  351. {
  352. int x, y;
  353. ulong addr;
  354. addr = panel_info.logo_addr;
  355. if (!addr) {
  356. error("There is no logo data.");
  357. return;
  358. }
  359. if (panel_info.vl_width >= panel_info.logo_width) {
  360. x = ((panel_info.vl_width - panel_info.logo_width) >> 1);
  361. x += panel_info.logo_x_offset; /* For X center align */
  362. } else {
  363. x = 0;
  364. printf("Warning: image width is bigger than display width\n");
  365. }
  366. if (panel_info.vl_height >= panel_info.logo_height) {
  367. y = ((panel_info.vl_height - panel_info.logo_height) >> 1);
  368. y += panel_info.logo_y_offset; /* For Y center align */
  369. } else {
  370. y = 0;
  371. printf("Warning: image height is bigger than display height\n");
  372. }
  373. bmp_display(addr, x, y);
  374. }
  375. #endif /* CONFIG_CMD_BMP */