misc.c 9.1 KB

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