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