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