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 <asm/io.h>
9 #include <common.h>
10 #include <dm.h>
11 #include <environment.h>
12 #include <i2c.h>
13 #include <i2c_eeprom.h>
14 #include <netdev.h>
15 #include "som.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
rk_board_late_init(void)31 int rk_board_late_init(void)
32 {
33 int ret;
34 struct udevice *dev;
35 struct rk3288_som opt;
36 int off;
37
38 /* Get the identificatioin page of M24C32-D EEPROM */
39 off = fdt_path_offset(gd->fdt_blob, "eeprom0");
40 if (off < 0) {
41 printf("%s: No eeprom0 path offset\n", __func__);
42 return off;
43 }
44
45 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
46 if (ret) {
47 printf("%s: Could not find EEPROM\n", __func__);
48 return ret;
49 }
50
51 ret = i2c_set_chip_offset_len(dev, 2);
52 if (ret)
53 return ret;
54
55 ret = i2c_eeprom_read(dev, 0, (uint8_t *)&opt,
56 sizeof(struct rk3288_som));
57 if (ret) {
58 printf("%s: Could not read EEPROM\n", __func__);
59 return ret;
60 }
61
62 if (opt.api_version != 0 || !valid_rk3288_som(&opt)) {
63 printf("Invalid data or wrong EEPROM layout version.\n");
64 /* Proceed anyway, since there is no fallback option */
65 }
66
67 if (is_valid_ethaddr(opt.mac))
68 eth_env_set_enetaddr("ethaddr", opt.mac);
69
70 return 0;
71 }
72