1a13110a9SKlaus Goger /*
2a13110a9SKlaus Goger  * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
3a13110a9SKlaus Goger  *
4a13110a9SKlaus Goger  * SPDX-License-Identifier:     GPL-2.0+
5a13110a9SKlaus Goger  */
6a13110a9SKlaus Goger #include <common.h>
7a13110a9SKlaus Goger #include <dm.h>
8*e92e5803SPhilipp Tomsich #include <misc.h>
9*e92e5803SPhilipp Tomsich #include <ram.h>
10a13110a9SKlaus Goger #include <dm/pinctrl.h>
11a13110a9SKlaus Goger #include <dm/uclass-internal.h>
12a13110a9SKlaus Goger #include <asm/arch/periph.h>
13a13110a9SKlaus Goger #include <power/regulator.h>
14*e92e5803SPhilipp Tomsich #include <u-boot/sha256.h>
15*e92e5803SPhilipp Tomsich 
16*e92e5803SPhilipp Tomsich DECLARE_GLOBAL_DATA_PTR;
17*e92e5803SPhilipp Tomsich 
18*e92e5803SPhilipp Tomsich #define RK3399_CPUID_OFF  0x7
19*e92e5803SPhilipp Tomsich #define RK3399_CPUID_LEN  0x10
20a13110a9SKlaus Goger 
21a13110a9SKlaus Goger DECLARE_GLOBAL_DATA_PTR;
22a13110a9SKlaus Goger 
23a13110a9SKlaus Goger int board_init(void)
24a13110a9SKlaus Goger {
25a13110a9SKlaus Goger 	struct udevice *pinctrl, *regulator;
26a13110a9SKlaus Goger 	int ret;
27a13110a9SKlaus Goger 
28a13110a9SKlaus Goger 	/*
29a13110a9SKlaus Goger 	 * The PWM does not have decicated interrupt number in dts and can
30a13110a9SKlaus Goger 	 * not get periph_id by pinctrl framework, so let's init them here.
31a13110a9SKlaus Goger 	 * The PWM2 and PWM3 are for pwm regulators.
32a13110a9SKlaus Goger 	 */
33a13110a9SKlaus Goger 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
34a13110a9SKlaus Goger 	if (ret) {
35a13110a9SKlaus Goger 		debug("%s: Cannot find pinctrl device\n", __func__);
36a13110a9SKlaus Goger 		goto out;
37a13110a9SKlaus Goger 	}
38a13110a9SKlaus Goger 
39a13110a9SKlaus Goger 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM2);
40a13110a9SKlaus Goger 	if (ret) {
41a13110a9SKlaus Goger 		debug("%s PWM2 pinctrl init fail!\n", __func__);
42a13110a9SKlaus Goger 		goto out;
43a13110a9SKlaus Goger 	}
44a13110a9SKlaus Goger 
45a13110a9SKlaus Goger 	/* rk3399 need to init vdd_center to get the correct output voltage */
46a13110a9SKlaus Goger 	ret = regulator_get_by_platname("vdd_center", &regulator);
47a13110a9SKlaus Goger 	if (ret)
48a13110a9SKlaus Goger 		debug("%s: Cannot get vdd_center regulator\n", __func__);
49a13110a9SKlaus Goger 
50a13110a9SKlaus Goger 	ret = regulator_get_by_platname("vcc5v0_host", &regulator);
51a13110a9SKlaus Goger 	if (ret) {
52a13110a9SKlaus Goger 		debug("%s vcc5v0_host init fail! ret %d\n", __func__, ret);
53a13110a9SKlaus Goger 		goto out;
54a13110a9SKlaus Goger 	}
55a13110a9SKlaus Goger 
56a13110a9SKlaus Goger 	ret = regulator_set_enable(regulator, true);
57a13110a9SKlaus Goger 	if (ret) {
58a13110a9SKlaus Goger 		debug("%s vcc5v0-host-en set fail!\n", __func__);
59a13110a9SKlaus Goger 		goto out;
60a13110a9SKlaus Goger 	}
61a13110a9SKlaus Goger 
62a13110a9SKlaus Goger out:
63a13110a9SKlaus Goger 	return 0;
64a13110a9SKlaus Goger }
65a13110a9SKlaus Goger 
66a13110a9SKlaus Goger int dram_init(void)
67a13110a9SKlaus Goger {
68*e92e5803SPhilipp Tomsich 	struct ram_info ram;
69*e92e5803SPhilipp Tomsich 	struct udevice *dev;
70*e92e5803SPhilipp Tomsich 	int ret;
71*e92e5803SPhilipp Tomsich 
72*e92e5803SPhilipp Tomsich 	ret = uclass_get_device(UCLASS_RAM, 0, &dev);
73*e92e5803SPhilipp Tomsich 	if (ret) {
74*e92e5803SPhilipp Tomsich 		debug("DRAM init failed: %d\n", ret);
75*e92e5803SPhilipp Tomsich 		return ret;
76*e92e5803SPhilipp Tomsich 	}
77*e92e5803SPhilipp Tomsich 	ret = ram_get_info(dev, &ram);
78*e92e5803SPhilipp Tomsich 	if (ret) {
79*e92e5803SPhilipp Tomsich 		debug("Cannot get DRAM size: %d\n", ret);
80*e92e5803SPhilipp Tomsich 		return ret;
81*e92e5803SPhilipp Tomsich 	}
82*e92e5803SPhilipp Tomsich 	debug("SDRAM base=%llx, size=%x\n", ram.base, (unsigned int)ram.size);
83*e92e5803SPhilipp Tomsich 	gd->ram_size = ram.size;
84*e92e5803SPhilipp Tomsich 
85a13110a9SKlaus Goger 	return 0;
86a13110a9SKlaus Goger }
87a13110a9SKlaus Goger 
88a13110a9SKlaus Goger int dram_init_banksize(void)
89a13110a9SKlaus Goger {
90a13110a9SKlaus Goger 	/* Reserve 0x200000 for ATF bl31 */
91a13110a9SKlaus Goger 	gd->bd->bi_dram[0].start = 0x200000;
92a13110a9SKlaus Goger 	gd->bd->bi_dram[0].size = 0x7e000000;
93a13110a9SKlaus Goger 
94a13110a9SKlaus Goger 	return 0;
95a13110a9SKlaus Goger }
96