]> git.sur5r.net Git - u-boot/blob - board/compulab/common/eeprom.c
Merge branch 'uboot'
[u-boot] / 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 #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
26
27 #define LAYOUT_INVALID  0
28 #define LAYOUT_LEGACY   0xff
29
30 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
31
32 static int cl_eeprom_read(uint offset, uchar *buf, int len)
33 {
34         int res;
35         unsigned int current_i2c_bus = i2c_get_bus_num();
36
37         res = i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
38         if (res < 0)
39                 return res;
40
41         res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
42                         CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
43
44         i2c_set_bus_num(current_i2c_bus);
45
46         return res;
47 }
48
49 static int cl_eeprom_setup_layout(void)
50 {
51         int res;
52
53         if (cl_eeprom_layout != LAYOUT_INVALID)
54                 return 0;
55
56         res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
57                              (uchar *)&cl_eeprom_layout, 1);
58         if (res) {
59                 cl_eeprom_layout = LAYOUT_INVALID;
60                 return res;
61         }
62
63         if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
64                 cl_eeprom_layout = LAYOUT_LEGACY;
65
66         return 0;
67 }
68
69 void get_board_serial(struct tag_serialnr *serialnr)
70 {
71         u32 serial[2];
72         uint offset;
73
74         memset(serialnr, 0, sizeof(*serialnr));
75
76         if (cl_eeprom_setup_layout())
77                 return;
78
79         offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
80                 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
81
82         if (cl_eeprom_read(offset, (uchar *)serial, 8))
83                 return;
84
85         if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
86                 serialnr->low = serial[0];
87                 serialnr->high = serial[1];
88         }
89 }
90
91 /*
92  * Routine: cl_eeprom_read_mac_addr
93  * Description: read mac address and store it in buf.
94  */
95 int cl_eeprom_read_mac_addr(uchar *buf)
96 {
97         uint offset;
98
99         if (cl_eeprom_setup_layout())
100                 return 0;
101
102         offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
103                         MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
104
105         return cl_eeprom_read(offset, buf, 6);
106 }
107
108 /*
109  * Routine: cl_eeprom_get_board_rev
110  * Description: read system revision from eeprom
111  */
112 u32 cl_eeprom_get_board_rev(void)
113 {
114         u32 rev = 0;
115         char str[5]; /* Legacy representation can contain at most 4 digits */
116         uint offset = BOARD_REV_OFFSET_LEGACY;
117
118         if (cl_eeprom_setup_layout())
119                 return 0;
120
121         if (cl_eeprom_layout != LAYOUT_LEGACY)
122                 offset = BOARD_REV_OFFSET;
123
124         if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
125                 return 0;
126
127         /*
128          * Convert legacy syntactic representation to semantic
129          * representation. i.e. for rev 1.00: 0x100 --> 0x64
130          */
131         if (cl_eeprom_layout == LAYOUT_LEGACY) {
132                 sprintf(str, "%x", rev);
133                 rev = simple_strtoul(str, NULL, 10);
134         }
135
136         return rev;
137 };