]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/compulab/common/eeprom.c
kconfig: zynq: Add ZYBO board
[people/ms/u-boot.git] / board / compulab / common / eeprom.c
1 /*
2 * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
3 *
4 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
5 * Igor Grinberg <grinberg@compulab.co.il>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10 #include <common.h>
11 #include <i2c.h>
12
13 #ifndef CONFIG_SYS_I2C_EEPROM_ADDR
14 # define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
15 # define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
16 #endif
17
18 #ifndef CONFIG_SYS_I2C_EEPROM_BUS
19 #define CONFIG_SYS_I2C_EEPROM_BUS 0
20 #endif
21
22 #define EEPROM_LAYOUT_VER_OFFSET 44
23 #define BOARD_SERIAL_OFFSET 20
24 #define BOARD_SERIAL_OFFSET_LEGACY 8
25 #define BOARD_REV_OFFSET 0
26 #define BOARD_REV_OFFSET_LEGACY 6
27 #define BOARD_REV_SIZE 2
28 #define MAC_ADDR_OFFSET 4
29 #define MAC_ADDR_OFFSET_LEGACY 0
30
31 #define LAYOUT_INVALID 0
32 #define LAYOUT_LEGACY 0xff
33
34 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
35
36 static int cl_eeprom_read(uint offset, uchar *buf, int len)
37 {
38 int res;
39 unsigned int current_i2c_bus = i2c_get_bus_num();
40
41 res = i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
42 if (res < 0)
43 return res;
44
45 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
46 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
47
48 i2c_set_bus_num(current_i2c_bus);
49
50 return res;
51 }
52
53 static int cl_eeprom_setup_layout(void)
54 {
55 int res;
56
57 if (cl_eeprom_layout != LAYOUT_INVALID)
58 return 0;
59
60 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
61 (uchar *)&cl_eeprom_layout, 1);
62 if (res) {
63 cl_eeprom_layout = LAYOUT_INVALID;
64 return res;
65 }
66
67 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
68 cl_eeprom_layout = LAYOUT_LEGACY;
69
70 return 0;
71 }
72
73 void get_board_serial(struct tag_serialnr *serialnr)
74 {
75 u32 serial[2];
76 uint offset;
77
78 memset(serialnr, 0, sizeof(*serialnr));
79
80 if (cl_eeprom_setup_layout())
81 return;
82
83 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
84 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
85
86 if (cl_eeprom_read(offset, (uchar *)serial, 8))
87 return;
88
89 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
90 serialnr->low = serial[0];
91 serialnr->high = serial[1];
92 }
93 }
94
95 /*
96 * Routine: cl_eeprom_read_mac_addr
97 * Description: read mac address and store it in buf.
98 */
99 int cl_eeprom_read_mac_addr(uchar *buf)
100 {
101 uint offset;
102
103 if (cl_eeprom_setup_layout())
104 return 0;
105
106 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
107 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
108
109 return cl_eeprom_read(offset, buf, 6);
110 }
111
112 /*
113 * Routine: cl_eeprom_get_board_rev
114 * Description: read system revision from eeprom
115 */
116 u32 cl_eeprom_get_board_rev(void)
117 {
118 u32 rev = 0;
119 char str[5]; /* Legacy representation can contain at most 4 digits */
120 uint offset = BOARD_REV_OFFSET_LEGACY;
121
122 if (cl_eeprom_setup_layout())
123 return 0;
124
125 if (cl_eeprom_layout != LAYOUT_LEGACY)
126 offset = BOARD_REV_OFFSET;
127
128 if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
129 return 0;
130
131 /*
132 * Convert legacy syntactic representation to semantic
133 * representation. i.e. for rev 1.00: 0x100 --> 0x64
134 */
135 if (cl_eeprom_layout == LAYOUT_LEGACY) {
136 sprintf(str, "%x", rev);
137 rev = simple_strtoul(str, NULL, 10);
138 }
139
140 return rev;
141 };