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