aboutsummaryrefslogtreecommitdiffstats
path: root/board/samsung/i9300/i9300.c
blob: 8dfc9b682d2664ff5d64c7fc0d98f43867982996 (plain)
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
/*
 * i9300 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 "i9300.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 = find_mmc_device(0);
	static const int rev_gpios[] = {
		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]);
	}

	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);
}

static int i9300_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\n", __func__);
		return current > 0;
	}

	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: charging at %d uA\n", current);
	i9300_led_action(LED_RED | LED_GREEN | LED_BLUE, LEDST_ON);
	mdelay(500);
	i9300_led_action(LED_RED | LED_GREEN | LED_BLUE, LEDST_OFF);
	if (charger_get_status(charger) == CHARGE_STATE_DISCHARGING) {
		printf("error: not charging. shutting down.");
		return BATTERY_ABORT;
	}

	printf("charging!\n");
	while (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);
		i9300_led_action(LED_RED, LEDST_TOGGLE);
		mdelay(500);
	}
	return BATTERY_LPM;
}

static int i9300_phy_control(int on)
{
	int ret;
	int type;
	struct udevice *vuotg;
	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("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_mode(vuotg, OPMODE_ON);
		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_mode(vuotg, OPMODE_LPM);
		if (ret) {
			pr_err("Failed to set VUOTG_3.0V to LPM: %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 = i9300_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)
{
	pr_info("Board usb init! %d %d\n", index, init);
	return dwc2_udc_probe(&exynos4_otg_data);
}

int board_usb_cleanup(int index, enum usb_init_type init)
{
	return i9300_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

static enum boot_mode i9300_check_keycombo(void)
{
	int ret = 0;

	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;
	}

	return 0;
}

static enum boot_mode i9300_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;
}

static void i9300_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) {
		i9300_led_action(LED_RED | LED_GREEN, LEDST_TOGGLE);
		mdelay(400);
	}
}

int exynos_init(void)
{
	board_gpio_init();

	printf("Key combo: %#x\n", i9300_check_keycombo());
	return 0;
}

int exynos_late_init(void)
{
	board_load_info();

	enum boot_mode mode = i9300_get_boot_mode();
	if (mode == MODE_NONE)
		mode = i9300_check_keycombo();

	enum battery_boot_mode bat_state = i9300_check_battery();
	if (bat_state == BATTERY_ABORT) {
		/* release PS_HOLD - turn off board */
		i9300_power_off();
	}

	switch (mode) {
	case MODE_FASTBOOT:
		printf("Activating fastboot mode\n");
		i9300_led_action(LED_BLUE, LEDST_ON);
		env_set("bootcmd", "run fastboot");
		break;
	case MODE_RECOVERY:
		printf("Booting to recovery\n");
		i9300_led_action(LED_RED, LEDST_ON);
		env_set("bootcmd", "run recoveryboot");
		break;
	case MODE_CONSOLE:
		printf("Dropping into u-boot console\n");
		i9300_led_action(LED_GREEN, LEDST_ON);
		env_set("bootcmd", NULL);
		break;
	default:
		printf("Booting normally...\n");
		i9300_led_action(LED_GREEN | LED_RED, LEDST_ON);
		env_set("bootcmd", "run autoboot");

	}

	return 0;
}