2 * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
4 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
5 * Igor Grinberg <grinberg@compulab.co.il>
7 * SPDX-License-Identifier: GPL-2.0+
13 #define EEPROM_LAYOUT_VER_OFFSET 44
14 #define BOARD_SERIAL_OFFSET 20
15 #define BOARD_SERIAL_OFFSET_LEGACY 8
16 #define BOARD_REV_OFFSET 0
17 #define BOARD_REV_OFFSET_LEGACY 6
18 #define BOARD_REV_SIZE 2
19 #define MAC_ADDR_OFFSET 4
20 #define MAC_ADDR_OFFSET_LEGACY 0
22 #define LAYOUT_INVALID 0
23 #define LAYOUT_LEGACY 0xff
25 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
27 static int cl_eeprom_read(uint offset, uchar *buf, int len)
29 return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
30 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
33 static int cl_eeprom_setup_layout(void)
37 if (cl_eeprom_layout != LAYOUT_INVALID)
40 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
41 (uchar *)&cl_eeprom_layout, 1);
43 cl_eeprom_layout = LAYOUT_INVALID;
47 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
48 cl_eeprom_layout = LAYOUT_LEGACY;
53 void get_board_serial(struct tag_serialnr *serialnr)
58 memset(serialnr, 0, sizeof(*serialnr));
60 if (cl_eeprom_setup_layout())
63 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
64 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
66 if (cl_eeprom_read(offset, (uchar *)serial, 8))
69 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
70 serialnr->low = serial[0];
71 serialnr->high = serial[1];
76 * Routine: cl_eeprom_read_mac_addr
77 * Description: read mac address and store it in buf.
79 int cl_eeprom_read_mac_addr(uchar *buf)
83 if (cl_eeprom_setup_layout())
86 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
87 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
89 return cl_eeprom_read(offset, buf, 6);
93 * Routine: cl_eeprom_get_board_rev
94 * Description: read system revision from eeprom
96 u32 cl_eeprom_get_board_rev(void)
99 char str[5]; /* Legacy representation can contain at most 4 digits */
100 uint offset = BOARD_REV_OFFSET_LEGACY;
102 if (cl_eeprom_setup_layout())
105 if (cl_eeprom_layout != LAYOUT_LEGACY)
106 offset = BOARD_REV_OFFSET;
108 if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
112 * Convert legacy syntactic representation to semantic
113 * representation. i.e. for rev 1.00: 0x100 --> 0x64
115 if (cl_eeprom_layout == LAYOUT_LEGACY) {
116 sprintf(str, "%x", rev);
117 rev = simple_strtoul(str, NULL, 10);