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)
34 return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
35 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
38 static int cl_eeprom_setup_layout(void)
42 if (cl_eeprom_layout != LAYOUT_INVALID)
45 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
46 (uchar *)&cl_eeprom_layout, 1);
48 cl_eeprom_layout = LAYOUT_INVALID;
52 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
53 cl_eeprom_layout = LAYOUT_LEGACY;
58 void get_board_serial(struct tag_serialnr *serialnr)
63 memset(serialnr, 0, sizeof(*serialnr));
65 if (cl_eeprom_setup_layout())
68 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
69 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
71 if (cl_eeprom_read(offset, (uchar *)serial, 8))
74 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
75 serialnr->low = serial[0];
76 serialnr->high = serial[1];
81 * Routine: cl_eeprom_read_mac_addr
82 * Description: read mac address and store it in buf.
84 int cl_eeprom_read_mac_addr(uchar *buf)
88 if (cl_eeprom_setup_layout())
91 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
92 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
94 return cl_eeprom_read(offset, buf, 6);
98 * Routine: cl_eeprom_get_board_rev
99 * Description: read system revision from eeprom
101 u32 cl_eeprom_get_board_rev(void)
104 char str[5]; /* Legacy representation can contain at most 4 digits */
105 uint offset = BOARD_REV_OFFSET_LEGACY;
107 if (cl_eeprom_setup_layout())
110 if (cl_eeprom_layout != LAYOUT_LEGACY)
111 offset = BOARD_REV_OFFSET;
113 if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
117 * Convert legacy syntactic representation to semantic
118 * representation. i.e. for rev 1.00: 0x100 --> 0x64
120 if (cl_eeprom_layout == LAYOUT_LEGACY) {
121 sprintf(str, "%x", rev);
122 rev = simple_strtoul(str, NULL, 10);