aboutsummaryrefslogtreecommitdiffstats
path: root/board/samsung/i9300/i9300.c
blob: 06a03519ecfd7f4cda2926f2bd279a1d3232013d (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
/*
 * 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 <console.h>
#include <dm/uclass.h>
#include <extcon.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>

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

	return ret;
}

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;

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

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

	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);
	/* Wait 1 second to allow charger to be connected */
	mdelay(1000);
	if (charger_get_status(charger) == CHARGE_STATE_DISCHARGING) {
		printf("error: not charging. should probably shut down now...\n");
	} else {
		printf("charging!\n");
		/* TODO: wait until no longer at critical SoC, then boot into LPM */
	}
	return 0;
}

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 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)
{
#if 0
	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_setprop_string(blob, offs, "test", "blah");
	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);
#endif
	return 0;
}
#endif

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

	int keys = i9300_check_keycombo();
	int state = i9300_check_battery();

	if (state > 0) {
		printf("low power mode\n");
	}

	if (keys == COMBO_RECOVERY) {
		printf("Booting to recovery\n");
		env_set("bootcmd", "run recoveryboot");
	} else if (keys == COMBO_FASTBOOT) {
		printf("Activating fastboot mode\n");
		env_set("bootcmd", "run fastboot");
	} else if (keys == COMBO_UBOOT_CONSOLE) {
		printf("Dropping into u-boot console\n");
		env_set("bootcmd", "");
	} else {
		printf("Booting normally...\n");
		env_set("bootcmd", "run autoboot");
	}

	return 0;
}