rockchip: puma-rk3399: update board_init()
[platform/kernel/u-boot.git] / board / theobroma-systems / puma_rk3399 / puma-rk3399.c
1 /*
2  * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <misc.h>
10 #include <dm/pinctrl.h>
11 #include <dm/uclass-internal.h>
12 #include <asm/setup.h>
13 #include <asm/arch/periph.h>
14 #include <power/regulator.h>
15 #include <spl.h>
16 #include <u-boot/sha256.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 int board_init(void)
21 {
22         int ret;
23
24         /*
25          * We need to call into regulators_enable_boot_on() again, as the call
26          * during SPL may have not included all regulators.
27          */
28         ret = regulators_enable_boot_on(false);
29         if (ret)
30                 debug("%s: Cannot enable boot on regulator\n", __func__);
31
32         return 0;
33 }
34
35 void spl_board_init(void)
36 {
37         preloader_console_init();
38 }
39
40 static void setup_macaddr(void)
41 {
42 #if CONFIG_IS_ENABLED(CMD_NET)
43         int ret;
44         const char *cpuid = env_get("cpuid#");
45         u8 hash[SHA256_SUM_LEN];
46         int size = sizeof(hash);
47         u8 mac_addr[6];
48
49         /* Only generate a MAC address, if none is set in the environment */
50         if (env_get("ethaddr"))
51                 return;
52
53         if (!cpuid) {
54                 debug("%s: could not retrieve 'cpuid#'\n", __func__);
55                 return;
56         }
57
58         ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
59         if (ret) {
60                 debug("%s: failed to calculate SHA256\n", __func__);
61                 return;
62         }
63
64         /* Copy 6 bytes of the hash to base the MAC address on */
65         memcpy(mac_addr, hash, 6);
66
67         /* Make this a valid MAC address and set it */
68         mac_addr[0] &= 0xfe;  /* clear multicast bit */
69         mac_addr[0] |= 0x02;  /* set local assignment bit (IEEE802) */
70         eth_env_set_enetaddr("ethaddr", mac_addr);
71 #endif
72 }
73
74 static void setup_serial(void)
75 {
76 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
77         const u32 cpuid_offset = 0x7;
78         const u32 cpuid_length = 0x10;
79
80         struct udevice *dev;
81         int ret, i;
82         u8 cpuid[cpuid_length];
83         u8 low[cpuid_length/2], high[cpuid_length/2];
84         char cpuid_str[cpuid_length * 2 + 1];
85         u64 serialno;
86         char serialno_str[17];
87
88         /* retrieve the device */
89         ret = uclass_get_device_by_driver(UCLASS_MISC,
90                                           DM_GET_DRIVER(rockchip_efuse), &dev);
91         if (ret) {
92                 debug("%s: could not find efuse device\n", __func__);
93                 return;
94         }
95
96         /* read the cpu_id range from the efuses */
97         ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
98         if (ret) {
99                 debug("%s: reading cpuid from the efuses failed\n",
100                       __func__);
101                 return;
102         }
103
104         memset(cpuid_str, 0, sizeof(cpuid_str));
105         for (i = 0; i < 16; i++)
106                 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
107
108         debug("cpuid: %s\n", cpuid_str);
109
110         /*
111          * Mix the cpuid bytes using the same rules as in
112          *   ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
113          */
114         for (i = 0; i < 8; i++) {
115                 low[i] = cpuid[1 + (i << 1)];
116                 high[i] = cpuid[i << 1];
117         }
118
119         serialno = crc32_no_comp(0, low, 8);
120         serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
121         snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
122
123         env_set("cpuid#", cpuid_str);
124         env_set("serial#", serialno_str);
125 #endif
126 }
127
128 int misc_init_r(void)
129 {
130         setup_serial();
131         setup_macaddr();
132
133         return 0;
134 }
135
136 #ifdef CONFIG_SERIAL_TAG
137 void get_board_serial(struct tag_serialnr *serialnr)
138 {
139         char *serial_string;
140         u64 serial = 0;
141
142         serial_string = env_get("serial#");
143
144         if (serial_string)
145                 serial = simple_strtoull(serial_string, NULL, 16);
146
147         serialnr->high = (u32)(serial >> 32);
148         serialnr->low = (u32)(serial & 0xffffffff);
149 }
150 #endif