misc.c 9.2 KB

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