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