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