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 #ifndef CONFIG_SYS_I2C_EEPROM_ADDR
14 # define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
15 # define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
18 #define EEPROM_LAYOUT_VER_OFFSET 44
19 #define BOARD_SERIAL_OFFSET 20
20 #define BOARD_SERIAL_OFFSET_LEGACY 8
21 #define BOARD_REV_OFFSET 0
22 #define BOARD_REV_OFFSET_LEGACY 6
23 #define BOARD_REV_SIZE 2
24 #define MAC_ADDR_OFFSET 4
25 #define MAC_ADDR_OFFSET_LEGACY 0
27 #define LAYOUT_INVALID 0
28 #define LAYOUT_LEGACY 0xff
30 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
32 static int cl_eeprom_read(uint offset, uchar *buf, int len)
35 unsigned int current_i2c_bus = i2c_get_bus_num();
37 res = i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
41 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
42 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
44 i2c_set_bus_num(current_i2c_bus);
49 static int cl_eeprom_setup_layout(void)
53 if (cl_eeprom_layout != LAYOUT_INVALID)
56 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
57 (uchar *)&cl_eeprom_layout, 1);
59 cl_eeprom_layout = LAYOUT_INVALID;
63 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
64 cl_eeprom_layout = LAYOUT_LEGACY;
69 void get_board_serial(struct tag_serialnr *serialnr)
74 memset(serialnr, 0, sizeof(*serialnr));
76 if (cl_eeprom_setup_layout())
79 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
80 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
82 if (cl_eeprom_read(offset, (uchar *)serial, 8))
85 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
86 serialnr->low = serial[0];
87 serialnr->high = serial[1];
92 * Routine: cl_eeprom_read_mac_addr
93 * Description: read mac address and store it in buf.
95 int cl_eeprom_read_mac_addr(uchar *buf)
99 if (cl_eeprom_setup_layout())
102 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
103 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
105 return cl_eeprom_read(offset, buf, 6);
109 * Routine: cl_eeprom_get_board_rev
110 * Description: read system revision from eeprom
112 u32 cl_eeprom_get_board_rev(void)
115 char str[5]; /* Legacy representation can contain at most 4 digits */
116 uint offset = BOARD_REV_OFFSET_LEGACY;
118 if (cl_eeprom_setup_layout())
121 if (cl_eeprom_layout != LAYOUT_LEGACY)
122 offset = BOARD_REV_OFFSET;
124 if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
128 * Convert legacy syntactic representation to semantic
129 * representation. i.e. for rev 1.00: 0x100 --> 0x64
131 if (cl_eeprom_layout == LAYOUT_LEGACY) {
132 sprintf(str, "%x", rev);
133 rev = simple_strtoul(str, NULL, 10);