misc.c 9.1 KB

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