xref: /rk3399_rockchip-uboot/board/phytec/phycore_rk3288/phycore-rk3288.c (revision 7a6ed8e85fb413a7da484e9c30b1e9beab2e594a)
1 /*
2  * Copyright (C) 2017 PHYTEC Messtechnik GmbH
3  * Author: Wadim Egorov <w.egorov@phytec.de>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 
8 #include <asm/io.h>
9 #include <common.h>
10 #include <dm.h>
11 #include <i2c.h>
12 #include <i2c_eeprom.h>
13 #include <netdev.h>
14 #include "som.h"
15 
16 static int valid_rk3288_som(struct rk3288_som *som)
17 {
18 	unsigned char *p = (unsigned char *)som;
19 	unsigned char *e = p + sizeof(struct rk3288_som) - 1;
20 	int hw = 0;
21 
22 	while (p < e) {
23 		hw += hweight8(*p);
24 		p++;
25 	}
26 
27 	return hw == som->bs;
28 }
29 
30 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_OF_PLATDATA)
31 static int phycore_init(void)
32 {
33 	struct udevice *pmic;
34 	int ret;
35 
36 	ret = uclass_first_device_err(UCLASS_PMIC, &pmic);
37 	if (ret)
38 		return ret;
39 
40 #if defined(CONFIG_SPL_POWER_SUPPORT)
41 	/* Increase USB input current to 2A */
42 	ret = rk818_spl_configure_usb_input_current(pmic, 2000);
43 	if (ret)
44 		return ret;
45 
46 	/* Close charger when USB lower then 3.26V */
47 	ret = rk818_spl_configure_usb_chrg_shutdown(pmic, 3260000);
48 	if (ret)
49 		return ret;
50 #endif
51 
52 	return 0;
53 }
54 
55 int rk_board_init_f(void)
56 {
57 	int ret = 0;
58 
59 	if (of_machine_is_compatible("phytec,rk3288-phycore-som")) {
60 		ret = phycore_init();
61 		if (ret) {
62 			debug("Failed to set up phycore power settings: %d\n",
63 			      ret);
64 			return ret;
65 		}
66 	}
67 
68 	return 0;
69 }
70 #endif
71 
72 int rk3288_board_late_init(void)
73 {
74 	int ret;
75 	struct udevice *dev;
76 	struct rk3288_som opt;
77 	int off;
78 
79 	/* Get the identificatioin page of M24C32-D EEPROM */
80 	off = fdt_path_offset(gd->fdt_blob, "eeprom0");
81 	if (off < 0) {
82 		printf("%s: No eeprom0 path offset\n", __func__);
83 		return off;
84 	}
85 
86 	ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
87 	if (ret) {
88 		printf("%s: Could not find EEPROM\n", __func__);
89 		return ret;
90 	}
91 
92 	ret = i2c_set_chip_offset_len(dev, 2);
93 	if (ret)
94 		return ret;
95 
96 	ret = i2c_eeprom_read(dev, 0, (uint8_t *)&opt,
97 				sizeof(struct rk3288_som));
98 	if (ret) {
99 		printf("%s: Could not read EEPROM\n", __func__);
100 		return ret;
101 	}
102 
103 	if (opt.api_version != 0 || !valid_rk3288_som(&opt)) {
104 		printf("Invalid data or wrong EEPROM layout version.\n");
105 		/* Proceed anyway, since there is no fallback option */
106 	}
107 
108 	if (is_valid_ethaddr(opt.mac))
109 		eth_env_set_enetaddr("ethaddr", opt.mac);
110 
111 	return 0;
112 }
113