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+
14 #ifndef CONFIG_SYS_I2C_EEPROM_ADDR
15 # define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
16 # define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
19 #ifndef CONFIG_SYS_I2C_EEPROM_BUS
20 #define CONFIG_SYS_I2C_EEPROM_BUS 0
23 #define EEPROM_LAYOUT_VER_OFFSET 44
24 #define BOARD_SERIAL_OFFSET 20
25 #define BOARD_SERIAL_OFFSET_LEGACY 8
26 #define BOARD_REV_OFFSET 0
27 #define BOARD_REV_OFFSET_LEGACY 6
28 #define BOARD_REV_SIZE 2
29 #define PRODUCT_NAME_OFFSET 128
30 #define PRODUCT_NAME_SIZE 16
31 #define MAC_ADDR_OFFSET 4
32 #define MAC_ADDR_OFFSET_LEGACY 0
34 #define LAYOUT_INVALID 0
35 #define LAYOUT_LEGACY 0xff
37 static int cl_eeprom_bus;
38 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
40 static int cl_eeprom_read(uint offset, uchar *buf, int len)
43 unsigned int current_i2c_bus = i2c_get_bus_num();
45 res = i2c_set_bus_num(cl_eeprom_bus);
49 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
50 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
52 i2c_set_bus_num(current_i2c_bus);
57 static int cl_eeprom_setup(uint eeprom_bus)
62 * We know the setup was already done when the layout is set to a valid
63 * value and we're using the same bus as before.
65 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
68 cl_eeprom_bus = eeprom_bus;
69 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
70 (uchar *)&cl_eeprom_layout, 1);
72 cl_eeprom_layout = LAYOUT_INVALID;
76 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
77 cl_eeprom_layout = LAYOUT_LEGACY;
82 void get_board_serial(struct tag_serialnr *serialnr)
87 memset(serialnr, 0, sizeof(*serialnr));
89 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
92 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
93 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
95 if (cl_eeprom_read(offset, (uchar *)serial, 8))
98 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
99 serialnr->low = serial[0];
100 serialnr->high = serial[1];
105 * Routine: cl_eeprom_read_mac_addr
106 * Description: read mac address and store it in buf.
108 int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
113 err = cl_eeprom_setup(eeprom_bus);
117 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
118 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
120 return cl_eeprom_read(offset, buf, 6);
123 static u32 board_rev;
126 * Routine: cl_eeprom_get_board_rev
127 * Description: read system revision from eeprom
129 u32 cl_eeprom_get_board_rev(uint eeprom_bus)
131 char str[5]; /* Legacy representation can contain at most 4 digits */
132 uint offset = BOARD_REV_OFFSET_LEGACY;
137 if (cl_eeprom_setup(eeprom_bus))
140 if (cl_eeprom_layout != LAYOUT_LEGACY)
141 offset = BOARD_REV_OFFSET;
143 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
147 * Convert legacy syntactic representation to semantic
148 * representation. i.e. for rev 1.00: 0x100 --> 0x64
150 if (cl_eeprom_layout == LAYOUT_LEGACY) {
151 sprintf(str, "%x", board_rev);
152 board_rev = simple_strtoul(str, NULL, 10);
159 * Routine: cl_eeprom_get_board_rev
160 * Description: read system revision from eeprom
162 * @buf: buffer to store the product name
163 * @eeprom_bus: i2c bus num of the eeprom
165 * @return: 0 on success, < 0 on failure
167 int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
174 err = cl_eeprom_setup(eeprom_bus);
178 err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE);
179 if (!err) /* Protect ourselves from invalid data (unterminated str) */
180 buf[PRODUCT_NAME_SIZE - 1] = '\0';