1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright (C) 2010 Samsung Electronics
4  *  Minkyu Kang <mk7.kang@samsung.com>
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  */
7 
8 #include <env.h>
9 #include <log.h>
10 #include <spi.h>
11 #include <asm/global_data.h>
12 #include <asm/io.h>
13 #include <asm/gpio.h>
14 #include <asm/arch/adc.h>
15 #include <asm/arch/pinmux.h>
16 #include <asm/arch/watchdog.h>
17 #include <linux/delay.h>
18 #include <power/pmic.h>
19 #include <usb.h>
20 #include <usb/dwc2_udc.h>
21 #include <asm/arch/cpu.h>
22 #include <power/max8998_pmic.h>
23 #include <libtizen.h>
24 #include <samsung/misc.h>
25 #include <usb_mass_storage.h>
26 #include <asm/mach-types.h>
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
30 unsigned int board_rev;
31 static int init_pmic_lcd(void);
32 
33 #ifdef CONFIG_REVISION_TAG
get_board_rev(void)34 u32 get_board_rev(void)
35 {
36 	return board_rev;
37 }
38 #endif
39 
exynos_power_init(void)40 int exynos_power_init(void)
41 {
42 	return init_pmic_lcd();
43 }
44 
get_hwrev(void)45 static int get_hwrev(void)
46 {
47 	return board_rev & 0xFF;
48 }
49 
get_adc_value(int channel)50 static unsigned short get_adc_value(int channel)
51 {
52 	struct s5p_adc *adc = (struct s5p_adc *)samsung_get_base_adc();
53 	unsigned short ret = 0;
54 	unsigned int reg;
55 	unsigned int loop = 0;
56 
57 	writel(channel & 0xF, &adc->adcmux);
58 	writel((1 << 14) | (49 << 6), &adc->adccon);
59 	writel(1000 & 0xffff, &adc->adcdly);
60 	writel(readl(&adc->adccon) | (1 << 16), &adc->adccon); /* 12 bit */
61 	udelay(10);
62 	writel(readl(&adc->adccon) | (1 << 0), &adc->adccon); /* Enable */
63 	udelay(10);
64 
65 	do {
66 		udelay(1);
67 		reg = readl(&adc->adccon);
68 	} while (!(reg & (1 << 15)) && (loop++ < 1000));
69 
70 	ret = readl(&adc->adcdat0) & 0xFFF;
71 
72 	return ret;
73 }
74 
adc_power_control(int on)75 static int adc_power_control(int on)
76 {
77 	struct udevice *dev;
78 	int ret;
79 	u8 reg;
80 
81 	ret = pmic_get("max8998-pmic", &dev);
82 	if (ret) {
83 		puts("Failed to get MAX8998!\n");
84 		return ret;
85 	}
86 
87 	reg = pmic_reg_read(dev, MAX8998_REG_ONOFF1);
88 	if (on)
89 		reg |= MAX8998_LDO4;
90 	else
91 		reg &= ~MAX8998_LDO4;
92 
93 	ret = pmic_reg_write(dev, MAX8998_REG_ONOFF1, reg);
94 	if (ret) {
95 		puts("MAX8998 LDO setting error\n");
96 		return -EINVAL;
97 	}
98 
99 	return 0;
100 }
101 
get_hw_revision(void)102 static unsigned int get_hw_revision(void)
103 {
104 	int hwrev, mode0, mode1;
105 
106 	adc_power_control(1);
107 
108 	mode0 = get_adc_value(1);		/* HWREV_MODE0 */
109 	mode1 = get_adc_value(2);		/* HWREV_MODE1 */
110 
111 	/*
112 	 * XXX Always set the default hwrev as the latest board
113 	 * ADC = (voltage) / 3.3 * 4096
114 	 */
115 	hwrev = 3;
116 
117 #define IS_RANGE(x, min, max)	((x) > (min) && (x) < (max))
118 	if (IS_RANGE(mode0, 80, 200) && IS_RANGE(mode1, 80, 200))
119 		hwrev = 0x0;		/* 0.01V	0.01V */
120 	if (IS_RANGE(mode0, 750, 1000) && IS_RANGE(mode1, 80, 200))
121 		hwrev = 0x1;		/* 610mV	0.01V */
122 	if (IS_RANGE(mode0, 1300, 1700) && IS_RANGE(mode1, 80, 200))
123 		hwrev = 0x2;		/* 1.16V	0.01V */
124 	if (IS_RANGE(mode0, 2000, 2400) && IS_RANGE(mode1, 80, 200))
125 		hwrev = 0x3;		/* 1.79V	0.01V */
126 #undef IS_RANGE
127 
128 	debug("mode0: %d, mode1: %d, hwrev 0x%x\n", mode0, mode1, hwrev);
129 
130 	adc_power_control(0);
131 
132 	return hwrev;
133 }
134 
check_hw_revision(void)135 static void check_hw_revision(void)
136 {
137 	int hwrev;
138 
139 	hwrev = get_hw_revision();
140 
141 	board_rev |= hwrev;
142 }
143 
144 #ifdef CONFIG_USB_GADGET
s5pc210_phy_control(int on)145 static int s5pc210_phy_control(int on)
146 {
147 	struct udevice *dev;
148 	int ret;
149 	u8 reg;
150 
151 	ret = pmic_get("max8998-pmic", &dev);
152 	if (ret) {
153 		puts("Failed to get MAX8998!\n");
154 		return ret;
155 	}
156 
157 	if (on) {
158 		reg = pmic_reg_read(dev, MAX8998_REG_BUCK_ACTIVE_DISCHARGE3);
159 		reg |= MAX8998_SAFEOUT1;
160 		ret |= pmic_reg_write(dev,
161 			MAX8998_REG_BUCK_ACTIVE_DISCHARGE3, reg);
162 
163 		reg = pmic_reg_read(dev, MAX8998_REG_ONOFF1);
164 		reg |= MAX8998_LDO3;
165 		ret |= pmic_reg_write(dev, MAX8998_REG_ONOFF1, reg);
166 
167 		reg = pmic_reg_read(dev, MAX8998_REG_ONOFF2);
168 		reg |= MAX8998_LDO8;
169 		ret |= pmic_reg_write(dev, MAX8998_REG_ONOFF2, reg);
170 
171 	} else {
172 		reg = pmic_reg_read(dev, MAX8998_REG_ONOFF2);
173 		reg &= ~MAX8998_LDO8;
174 		ret |= pmic_reg_write(dev, MAX8998_REG_ONOFF2, reg);
175 
176 		reg = pmic_reg_read(dev, MAX8998_REG_ONOFF1);
177 		reg &= ~MAX8998_LDO3;
178 		ret |= pmic_reg_write(dev, MAX8998_REG_ONOFF1, reg);
179 
180 		reg = pmic_reg_read(dev, MAX8998_REG_BUCK_ACTIVE_DISCHARGE3);
181 		reg &= ~MAX8998_SAFEOUT1;
182 		ret |= pmic_reg_write(dev,
183 			MAX8998_REG_BUCK_ACTIVE_DISCHARGE3, reg);
184 	}
185 
186 	if (ret) {
187 		puts("MAX8998 LDO setting error!\n");
188 		return -EINVAL;
189 	}
190 
191 	return 0;
192 }
193 
194 struct dwc2_plat_otg_data s5pc210_otg_data = {
195 	.phy_control = s5pc210_phy_control,
196 	.regs_phy = EXYNOS4_USBPHY_BASE,
197 	.regs_otg = EXYNOS4_USBOTG_BASE,
198 	.usb_phy_ctrl = EXYNOS4_USBPHY_CONTROL,
199 	.usb_flags = PHY0_SLEEP,
200 };
201 #endif
202 
board_usb_init(int index,enum usb_init_type init)203 int board_usb_init(int index, enum usb_init_type init)
204 {
205 	debug("USB_udc_probe\n");
206 	return dwc2_udc_probe(&s5pc210_otg_data);
207 }
208 
exynos_early_init_f(void)209 int exynos_early_init_f(void)
210 {
211 	wdt_stop();
212 
213 	return 0;
214 }
215 
init_pmic_lcd(void)216 static int init_pmic_lcd(void)
217 {
218 	struct udevice *dev;
219 	unsigned char val;
220 	int ret = 0;
221 
222 	ret = pmic_get("max8998-pmic", &dev);
223 	if (ret) {
224 		puts("Failed to get MAX8998 for init_pmic_lcd()!\n");
225 		return ret;
226 	}
227 
228 	/* LDO7 1.8V */
229 	val = 0x02; /* (1800 - 1600) / 100; */
230 	ret |= pmic_reg_write(dev,  MAX8998_REG_LDO7, val);
231 
232 	/* LDO17 3.0V */
233 	val = 0xe; /* (3000 - 1600) / 100; */
234 	ret |= pmic_reg_write(dev,  MAX8998_REG_LDO17, val);
235 
236 	/* Disable unneeded regulators */
237 	/*
238 	 * ONOFF1
239 	 * Buck1 ON, Buck2 OFF, Buck3 ON, Buck4 ON
240 	 * LDO2 ON, LDO3 OFF, LDO4 OFF, LDO5 ON
241 	 */
242 	val = 0xB9;
243 	ret |= pmic_reg_write(dev,  MAX8998_REG_ONOFF1, val);
244 
245 	/* ONOFF2
246 	 * LDO6 OFF, LDO7 ON, LDO8 OFF, LDO9 ON,
247 	 * LDO10 OFF, LDO11 OFF, LDO12 OFF, LDO13 OFF
248 	 */
249 	val = 0x50;
250 	ret |= pmic_reg_write(dev,  MAX8998_REG_ONOFF2, val);
251 
252 	/* ONOFF3
253 	 * LDO14 OFF, LDO15 OFF, LGO16 OFF, LDO17 OFF
254 	 * EPWRHOLD OFF, EBATTMON OFF, ELBCNFG2 OFF, ELBCNFG1 OFF
255 	 */
256 	val = 0x00;
257 	ret |= pmic_reg_write(dev,  MAX8998_REG_ONOFF3, val);
258 
259 	if (ret) {
260 		puts("LCD pmic initialisation error!\n");
261 		return -EINVAL;
262 	}
263 
264 	return 0;
265 }
266 
exynos_init(void)267 void exynos_init(void)
268 {
269 	gd->bd->bi_arch_number = MACH_TYPE_UNIVERSAL_C210;
270 
271 	switch (get_hwrev()) {
272 	case 0:
273 		/*
274 		 * Set the low to enable LDO_EN
275 		 * But when you use the test board for eMMC booting
276 		 * you should set it HIGH since it removes the inverter
277 		 */
278 		/* MASSMEMORY_EN: XMDMDATA_6: GPE3[6] */
279 		gpio_request(EXYNOS4_GPIO_E36, "ldo_en");
280 		gpio_direction_output(EXYNOS4_GPIO_E36, 0);
281 		break;
282 	default:
283 		/*
284 		 * Default reset state is High and there's no inverter
285 		 * But set it as HIGH to ensure
286 		 */
287 		/* MASSMEMORY_EN: XMDMADDR_3: GPE1[3] */
288 		gpio_request(EXYNOS4_GPIO_E13, "massmemory_en");
289 		gpio_direction_output(EXYNOS4_GPIO_E13, 1);
290 		break;
291 	}
292 
293 	check_hw_revision();
294 	printf("HW Revision:\t0x%x\n", board_rev);
295 }
296