/
midas.c
558 lines (468 loc) · 14.1 KB
/
midas.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* midas board file
* Copyright (C) 2018 Simon Shields <simon@lineageos.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <asm/gpio.h>
#include <asm/arch/gpio.h>
#include <asm/arch/power.h>
#include <console.h>
#include <dm/uclass.h>
#include <extcon.h>
#include <led.h>
#include <linux/libfdt.h>
#include <mmc.h>
#include <power/max77686_pmic.h>
#include <power/max77693_muic.h>
#include <power/battery.h>
#include <power/charger.h>
#include <power/pmic.h>
#include <power/regulator.h>
#include <usb.h>
#include <usb/dwc2_udc.h>
#include "midas.h"
DECLARE_GLOBAL_DATA_PTR;
#define KEY_POWER (0)
#define KEY_VOL_UP (1)
#define KEY_VOL_DOWN (2)
#define KEY_HOME (3)
#define COMBO_POWER (1 << KEY_POWER)
#define COMBO_VOL_UP (1 << KEY_VOL_UP)
#define COMBO_VOL_DOWN (1 << KEY_VOL_DOWN)
#define COMBO_HOME (1 << KEY_HOME)
#define COMBO_RECOVERY (COMBO_POWER | COMBO_VOL_UP | COMBO_HOME)
#define COMBO_FASTBOOT (COMBO_POWER | COMBO_VOL_DOWN | COMBO_HOME)
#define COMBO_UBOOT_CONSOLE (COMBO_POWER | COMBO_VOL_UP | COMBO_VOL_DOWN)
static uint64_t board_serial = 0;
static char board_rev = 0xff;
static char board_serial_str[17];
void board_load_info(void)
{
struct mmc *emmc;
static const int rev_gpios[] = {
/* HW_REV: GPM1[5:2] */
EXYNOS4X12_GPIO_M15,
EXYNOS4X12_GPIO_M14,
EXYNOS4X12_GPIO_M13,
EXYNOS4X12_GPIO_M12,
};
board_rev = 0;
for (int i = 0; i < ARRAY_SIZE(rev_gpios); i++) {
gpio_request(rev_gpios[i], "HW_REV[0..3]");
gpio_cfg_pin(rev_gpios[i], S5P_GPIO_INPUT);
gpio_set_pull(rev_gpios[i], S5P_GPIO_PULL_DOWN);
board_rev <<= 1;
board_rev |= gpio_get_value(rev_gpios[i]);
}
/* Load serial number from eMMC CID */
emmc = find_mmc_device(0);
if (!emmc) {
pr_err("%s: couldn't get serial number - no eMMC device found!\n", __func__);
sprintf(board_serial_str, "%16x", 0);
return;
}
if (mmc_init(emmc)) {
pr_err("%s: eMMC init failed!\n", __func__);
} else {
board_serial = ((uint64_t)emmc->cid[2] << 32) | emmc->cid[3];
}
sprintf(board_serial_str, "%16llx", board_serial);
env_set("serial#", board_serial_str);
}
int get_board_rev(void) {
if (board_rev == 0xff)
board_load_info();
return board_rev;
}
static void board_gpio_init(void)
{
/* power and volume keys are externally pulled up */
/*
* GPX2[7] - power key. If we don't set pull to none within 8 seconds,
* PMIC thinks power key is being held down and will reset the board.
*/
gpio_request(EXYNOS4X12_GPIO_X27, "nPOWER");
gpio_cfg_pin(EXYNOS4X12_GPIO_X27, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X27, S5P_GPIO_PULL_NONE);
/* GPX2[2] - volume up */
gpio_request(EXYNOS4X12_GPIO_X22, "VOL_UP");
gpio_cfg_pin(EXYNOS4X12_GPIO_X22, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X22, S5P_GPIO_PULL_NONE);
/* GPX3[3] - volume down */
gpio_request(EXYNOS4X12_GPIO_X33, "VOL_DOWN");
gpio_cfg_pin(EXYNOS4X12_GPIO_X33, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X33, S5P_GPIO_PULL_NONE);
/* GPX0[1] - home key */
gpio_request(EXYNOS4X12_GPIO_X01, "HOME");
gpio_cfg_pin(EXYNOS4X12_GPIO_X01, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X01, S5P_GPIO_PULL_NONE);
/* externally pulled up IRQs */
gpio_request(EXYNOS4X12_GPIO_X07, "PMIC_IRQ");
gpio_cfg_pin(EXYNOS4X12_GPIO_X07, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X07, S5P_GPIO_PULL_NONE);
gpio_request(EXYNOS4X12_GPIO_X15, "IF_PMIC_IRQ");
gpio_cfg_pin(EXYNOS4X12_GPIO_X15, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X15, S5P_GPIO_PULL_NONE);
gpio_request(EXYNOS4X12_GPIO_X23, "FUEL_ALERT");
gpio_cfg_pin(EXYNOS4X12_GPIO_X23, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X23, S5P_GPIO_PULL_NONE);
gpio_request(EXYNOS4X12_GPIO_X34, "SDHCI2_CD");
gpio_cfg_pin(EXYNOS4X12_GPIO_X34, S5P_GPIO_INPUT);
gpio_set_pull(EXYNOS4X12_GPIO_X34, S5P_GPIO_PULL_NONE);
/* GPIOs the vendor kernel assumes have been properly configured */
gpio_request(EXYNOS4X12_GPIO_C01, "GPC0-1");
gpio_cfg_pin(EXYNOS4X12_GPIO_C01, S5P_GPIO_OUTPUT);
gpio_set_pull(EXYNOS4X12_GPIO_C01, S5P_GPIO_PULL_NONE);
gpio_set_value(EXYNOS4X12_GPIO_C01, 1);
gpio_request(EXYNOS4X12_GPIO_F21, "GPF2-1");
gpio_cfg_pin(EXYNOS4X12_GPIO_F21, S5P_GPIO_OUTPUT);
gpio_set_pull(EXYNOS4X12_GPIO_F21, S5P_GPIO_PULL_NONE);
gpio_set_value(EXYNOS4X12_GPIO_F21, 0);
}
/* Checks that battery is charged enough to allow boot */
static int midas_check_battery(void)
{
struct udevice *bat, *extcon, *charger;
int ret, state, current, old_soc, soc;
ret = uclass_get_device(UCLASS_BATTERY, 0, &bat);
if (ret) {
printf("%s: failed to get battery device: %d\n", __func__, ret);
return ret;
}
ret = uclass_get_device(UCLASS_EXTCON, 0, &extcon);
if (ret) {
printf("%s: failed to get extcon device: %d\n", __func__, ret);
return ret;
}
current = extcon_get_max_charge_current(extcon);
if (current < 0) {
printf("%s: Failed to get max charge current: %d\n", __func__, current);
return current;
}
old_soc = battery_get_soc(bat);
state = battery_get_status(bat);
if (state != BAT_STATE_NEED_CHARGING) {
printf("%s: Battery soc is OK - %d\n", __func__, old_soc);
return current > 0 ? BATTERY_LPM : BATTERY_NORMAL;
}
ret = uclass_get_device(UCLASS_CHARGER, 0, &charger);
if (ret) {
printf("%s: failed to get charger device: %d\n", __func__, ret);
return ret;
}
if (current == 0)
current = 460 * 1000;
ret = charger_set_current(charger, current);
if (ret < 0) {
printf("%s: Failed to set charge current: %d\n", __func__, ret);
return ret;
}
printf("Need charging: current charge level %d%%, will charge at %d uA\n", battery_get_soc(bat), current);
for (int i = 0; i < 50; i++) {
midas_led_action(LED_RED | LED_GREEN | LED_BLUE, LEDST_TOGGLE);
mdelay(500);
if (charger_get_status(charger) != CHARGE_STATE_DISCHARGING)
break;
}
midas_led_action(LED_RED | LED_GREEN | LED_BLUE, LEDST_OFF);
if (charger_get_status(charger) == CHARGE_STATE_DISCHARGING ||
charger_get_status(charger) == CHARGE_STATE_UNKNOWN) {
printf("error: not charging. shutting down.");
return BATTERY_ABORT;
}
while (charger_get_status(charger) != CHARGE_STATE_DISCHARGING && battery_get_status(bat) == BAT_STATE_NEED_CHARGING) {
soc = battery_get_soc(bat);
printf("%s: SoC started at %d, now %d\n", __func__, old_soc, soc);
midas_led_action(LED_RED, LEDST_TOGGLE);
mdelay(500);
}
if (battery_get_status(bat) == BAT_STATE_NEED_CHARGING) {
printf("error: battery too low - charger disconnected");
return BATTERY_ABORT;
}
return BATTERY_LPM;
}
static int midas_phy_control(int on)
{
int ret;
int type;
struct udevice *vuotg, *vhsic10, *vhsic18;
struct udevice *safeout;
struct udevice *extcon;
struct udevice *pmic;
ret = regulator_get_by_platname("VUOTG_3.0V", &vuotg);
if (ret) {
pr_err("Failed to get VUOTG_3.0V: %d\n", ret);
return -1;
}
ret = regulator_get_by_platname("VHSIC_1.0V", &vhsic10);
if (ret) {
pr_err("Failed to get VHSIC_1.0V: %d\n", ret);
return -1;
}
ret = regulator_get_by_platname("VHSIC_1.8V", &vhsic18);
if (ret) {
pr_err("Failed to get VHSIC_1.8V: %d\n", ret);
return -1;
}
ret = regulator_get_by_platname("ESAFEOUT1", &safeout);
if (ret) {
pr_err("Failed to get ESAFEOUT1: %d\n", ret);
return -1;
}
ret = extcon_get("muic-max77693", &extcon);
if (ret) {
pr_err("Failed to get max77693 extcon: %d\n", ret);
return -1;
}
ret = pmic_get("max77693_muic@25", &pmic);
if (ret) {
pr_err("Failed to get MUIC PMIC: %d!\n", ret);
return -1;
}
if (on) {
pr_info("Waiting 10 seconds for USB to be inserted...\n");
int i = 0;
do {
extcon_get_cable_id(extcon, &type);
if (type & EXTCON_TYPE_USB)
break;
if (ctrlc())
break;
mdelay(10);
} while (i++ < 10000);
if (!(type & EXTCON_TYPE_USB)) {
pr_info("No USB cable detected, aborting!\n");
return -1;
}
}
if (on) {
ret = regulator_set_enable(vhsic10, true);
if (ret) {
pr_err("Failed to enable VHSIC_1.0V: %d\n", ret);
return -1;
}
ret = regulator_set_enable(vhsic18, true);
if (ret) {
pr_err("Failed to enable VHSIC_1.8V: %d\n", ret);
return -1;
}
ret = regulator_set_enable(vuotg, true);
if (ret) {
pr_err("Failed to set VUOTG_3.0V to ON: %d\n", ret);
return -1;
}
ret = regulator_set_enable(safeout, true);
if (ret) {
pr_err("Failed to enable ESAFEOUT1: %d\n", ret);
return -1;
}
/* set MUIC path to USB */
ret = pmic_reg_write(pmic, MAX77693_MUIC_CONTROL1,
MAX77693_MUIC_CTRL1_DN1DP2);
if (ret) {
pr_err("Failed to set MUIC path to USB: %d\n", ret);
return -1;
}
} else {
ret = regulator_set_enable(vuotg, false);
if (ret) {
pr_err("Failed to disable VUOTG_3.0V: %d\n", ret);
return -1;
}
ret = regulator_set_enable(vhsic18, false);
if (ret) {
pr_err("Failed to disable VHSIC_1.8V: %d\n", ret);
return -1;
}
ret = regulator_set_enable(vhsic10, false);
if (ret) {
pr_err("Failed to disable VHSIC_1.0V: %d\n", ret);
return -1;
}
/* set MUIC path to UART */
ret = pmic_reg_write(pmic, MAX77693_MUIC_CONTROL1,
MAX77693_MUIC_CTRL1_UT1UR2);
if (ret) {
pr_err("Failed to set MUIC path to UART: %d\n", ret);
return -1;
}
}
return 0;
}
struct dwc2_plat_otg_data exynos4_otg_data = {
.phy_control = midas_phy_control,
.regs_phy = EXYNOS4X12_USBPHY_BASE,
.regs_otg = EXYNOS4X12_USBOTG_BASE,
.usb_phy_ctrl = EXYNOS4X12_USBPHY_CONTROL,
.usb_flags = PHY0_SLEEP,
};
int fb_set_reboot_flag(void)
{
struct exynos4412_power *pwr = (struct exynos4412_power *)samsung_get_base_power();
writel(INFORM_MAGIC | MODE_FASTBOOT, &pwr->inform3);
return 0;
}
int board_usb_init(int index, enum usb_init_type init)
{
return dwc2_udc_probe(&exynos4_otg_data);
}
int board_usb_cleanup(int index, enum usb_init_type init)
{
/* Set microUSB port back to serial console */
return midas_phy_control(0);
}
#ifdef CONFIG_OF_BOARD_SETUP
int ft_board_setup(void *blob, bd_t *bd)
{
int ret;
/* we don't want the OS to think we're running under secure firmware */
int offs = fdt_node_offset_by_compatible(blob, 0, "samsung,secure-firmware");
if (offs < 0) {
if (offs == -FDT_ERR_NOTFOUND) {
printf("%s: no secure firmware node!\n", __func__);
return 0;
}
printf("%s: failed to find secure firmware node: %d\n", __func__, offs);
return -EINVAL;
}
/* delete the node */
ret = fdt_del_node(blob, offs);
if (ret < 0) {
printf("%s: failed to remove secure firmware node: %d\n", __func__, ret);
return -EINVAL;
}
printf("FDT set up for OS %p\n", blob);
return 0;
}
#endif
/* get boot mode based on key combo */
static enum boot_mode midas_check_keycombo(void)
{
int ret = 0;
/* the hw keys are active-low */
ret |= !gpio_get_value(EXYNOS4X12_GPIO_X27) << KEY_POWER;
ret |= !gpio_get_value(EXYNOS4X12_GPIO_X22) << KEY_VOL_UP;
ret |= !gpio_get_value(EXYNOS4X12_GPIO_X33) << KEY_VOL_DOWN;
ret |= !gpio_get_value(EXYNOS4X12_GPIO_X01) << KEY_HOME;
switch (ret) {
case COMBO_RECOVERY:
return MODE_RECOVERY;
case COMBO_FASTBOOT:
return MODE_FASTBOOT;
case COMBO_UBOOT_CONSOLE:
return MODE_CONSOLE;
}
/* Power key pressed means that LPM should be skipped (i.e. this is a user initiated
* boot, even if we're plugged in).
* TODO: there's probably a better way to determine wakeup source... */
if (ret & KEY_POWER)
return MODE_SKIP_LPM;
return MODE_NONE;
}
/* get boot mode based on magic value in INFORM3 register */
static enum boot_mode midas_get_boot_mode(void)
{
struct exynos4412_power *pwr = (struct exynos4412_power *)samsung_get_base_power();
u32 inform = readl(&pwr->inform3);
/* clear out inform3 for subsequent boots */
writel(0, &pwr->inform3);
printf("inform3: 0x%08x: ", inform);
if ((inform & ~MODE_MAX) != INFORM_MAGIC) {
printf("invalid\n");
return MODE_NONE;
}
inform &= MODE_MAX;
printf("boot mode: %#x\n", inform);
if (inform >= MODE_LAST)
return MODE_NONE;
return inform;
}
/* power off board, indicating that it needs to be plugged in before it can boot */
static void midas_power_off(void)
{
struct exynos4412_power *pwr = (struct exynos4412_power *)samsung_get_base_power();
writel(readl(&pwr->ps_hold_control) & 0xfffffeff, &pwr->ps_hold_control);
while (1) {
midas_led_action(LED_RED | LED_GREEN, LEDST_TOGGLE);
mdelay(400);
}
}
static bool needs_network = false;
/* Enable the USB network interface if booting into "console" mode */
int board_eth_init(bd_t *bis)
{
if (!needs_network) {
return 0;
}
/* Probe the ethernet gadget */
int ret = usb_eth_initialize(bis);
if (ret < 0) {
printf("Error %d registering USB_ETHER\n", ret);
return ret;
}
/* Switch to the USB port */
board_usb_init(0, 0);
env_set("stdin", "serial@13820000,nc");
env_set("stdout", "serial@13820000,nc");
return ret;
}
int exynos_init(void)
{
board_gpio_init();
printf("Key combo: %#x\n", midas_check_keycombo());
return 0;
}
int exynos_late_init(void)
{
board_load_info();
env_set("bootmode", "normal");
/* magic value for boot mode in INFORM3 overrides key combo */
enum boot_mode mode = midas_get_boot_mode();
if (mode == MODE_NONE)
mode = midas_check_keycombo();
enum battery_boot_mode bat_state = midas_check_battery();
if (bat_state == BATTERY_ABORT) {
/* release PS_HOLD - turn off board */
midas_power_off();
} else if (bat_state == BATTERY_LPM && mode == MODE_NONE) {
/* boot into low-power charging mode */
env_set("bootmode", "lpm");
}
/*
* on the note 2: figure out which LCD is attached,
* and use the appropriate overlay when booting a kernel
*/
init_overlays();
/*
* We use the notification LED in the absence of proper LCD support
* to indicate roughly what's going on -
* more of a "sign of life" than anything else.
*/
switch (mode) {
case MODE_FASTBOOT:
printf("Activating fastboot mode\n");
midas_led_action(LED_BLUE, LEDST_ON);
env_set("bootcmd", "run fastboot");
break;
case MODE_RECOVERY:
printf("Booting to recovery\n");
midas_led_action(LED_RED, LEDST_ON);
env_set("bootcmd", "run recoveryboot");
env_set("bootmode", "recovery");
break;
case MODE_CONSOLE:
printf("Dropping into u-boot console (networked)\n");
needs_network = true;
midas_led_action(LED_GREEN, LEDST_ON);
env_set("bootcmd", NULL);
break;
default:
printf("Booting normally...\n");
midas_led_action(LED_GREEN | LED_RED, LEDST_ON);
env_set("bootcmd", "run autoboot");
}
return 0;
}