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 <init.h>
8 #include <log.h>
9 #include <net.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <dm.h>
13 #include <env.h>
14 #include <env_internal.h>
15 #include <i2c.h>
16 #include <i2c_eeprom.h>
17 #include <netdev.h>
18 #include <linux/bitops.h>
19 #include "som.h"
20
valid_rk3288_som(struct rk3288_som * som)21 static int valid_rk3288_som(struct rk3288_som *som)
22 {
23 unsigned char *p = (unsigned char *)som;
24 unsigned char *e = p + sizeof(struct rk3288_som) - 1;
25 int hw = 0;
26
27 while (p < e) {
28 hw += hweight8(*p);
29 p++;
30 }
31
32 return hw == som->bs;
33 }
34
rk3288_board_late_init(void)35 int rk3288_board_late_init(void)
36 {
37 int ret;
38 struct udevice *dev;
39 struct rk3288_som opt;
40 int off;
41
42 /* Get the identificatioin page of M24C32-D EEPROM */
43 off = fdt_path_offset(gd->fdt_blob, "eeprom0");
44 if (off < 0) {
45 printf("%s: No eeprom0 path offset\n", __func__);
46 return off;
47 }
48
49 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
50 if (ret) {
51 printf("%s: Could not find EEPROM\n", __func__);
52 return ret;
53 }
54
55 ret = i2c_set_chip_offset_len(dev, 2);
56 if (ret)
57 return ret;
58
59 ret = i2c_eeprom_read(dev, 0, (uint8_t *)&opt,
60 sizeof(struct rk3288_som));
61 if (ret) {
62 printf("%s: Could not read EEPROM\n", __func__);
63 return ret;
64 }
65
66 if (opt.api_version != 0 || !valid_rk3288_som(&opt)) {
67 printf("Invalid data or wrong EEPROM layout version.\n");
68 /* Proceed anyway, since there is no fallback option */
69 }
70
71 if (is_valid_ethaddr(opt.mac))
72 eth_env_set_enetaddr("ethaddr", opt.mac);
73
74 return 0;
75 }
76