]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/freescale/common/sys_eeprom.c
Move the MPC8641HPCN board under board/freescale.
[karo-tx-uboot.git] / board / freescale / common / sys_eeprom.c
1 /*
2  * Copyright 2006 Freescale Semiconductor
3  * York Sun (yorksun@freescale.com)
4  * Haiying Wang (haiying.wang@freescale.com)
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26 #include <command.h>
27 #include <i2c.h>
28 #include <linux/ctype.h>
29
30 #ifdef CFG_ID_EEPROM
31 typedef struct {
32         unsigned char id[4];            /* 0x0000 - 0x0003 */
33         unsigned char sn[12];           /* 0x0004 - 0x000F */
34         unsigned char errata[5];        /* 0x0010 - 0x0014 */
35         unsigned char date[7];          /* 0x0015 - 0x001a */
36         unsigned char res_1[37];        /* 0x001b - 0x003f */
37         unsigned char tab_size;         /* 0x0040 */
38         unsigned char tab_flag;         /* 0x0041 */
39         unsigned char mac[8][6];        /* 0x0042 - 0x0071 */
40         unsigned char res_2[126];       /* 0x0072 - 0x00ef */
41         unsigned int crc;               /* 0x00f0 - 0x00f3 crc32 checksum */
42 } EEPROM_data;
43
44 static EEPROM_data mac_data;
45
46 int mac_show(void)
47 {
48         int i;
49         unsigned char ethaddr[8][18];
50
51         printf("ID %c%c%c%c\n",
52                mac_data.id[0],
53                mac_data.id[1],
54                mac_data.id[2],
55                mac_data.id[3]);
56         printf("Errata %c%c%c%c%c\n",
57                mac_data.errata[0],
58                mac_data.errata[1],
59                mac_data.errata[2],
60                mac_data.errata[3],
61                mac_data.errata[4]);
62         printf("Date %c%c%c%c%c%c%c\n",
63                mac_data.date[0],
64                mac_data.date[1],
65                mac_data.date[2],
66                mac_data.date[3],
67                mac_data.date[4],
68                mac_data.date[5],
69                mac_data.date[6]);
70         for (i = 0; i < 8; i++) {
71                 sprintf((char *)ethaddr[i],
72                         "%02x:%02x:%02x:%02x:%02x:%02x",
73                         mac_data.mac[i][0],
74                         mac_data.mac[i][1],
75                         mac_data.mac[i][2],
76                         mac_data.mac[i][3],
77                         mac_data.mac[i][4],
78                         mac_data.mac[i][5]);
79                 printf("MAC %d %s\n", i, ethaddr[i]);
80         }
81
82         setenv("ethaddr",  (char *)ethaddr[0]);
83         setenv("eth1addr", (char *)ethaddr[1]);
84         setenv("eth2addr", (char *)ethaddr[2]);
85         setenv("eth3addr", (char *)ethaddr[3]);
86
87         return 0;
88 }
89
90 int mac_read(void)
91 {
92         int ret, length;
93         unsigned int crc = 0;
94         unsigned char dev = ID_EEPROM_ADDR, *data;
95
96         length = sizeof(EEPROM_data);
97         ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length);
98         if (ret) {
99                 printf("Read failed.\n");
100                 return -1;
101         }
102
103         data = (unsigned char *)(&mac_data);
104         printf("Check CRC on reading ...");
105         crc = crc32(crc, data, length - 4);
106         if (crc != mac_data.crc) {
107                 printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n",
108                      mac_data.crc, crc);
109                 return -1;
110         } else {
111                 printf("CRC OK\n");
112                 mac_show();
113         }
114         return 0;
115 }
116
117 int mac_prog(void)
118 {
119         int ret, i, length;
120         unsigned int crc = 0;
121         unsigned char dev = ID_EEPROM_ADDR, *ptr;
122         unsigned char *eeprom_data = (unsigned char *)(&mac_data);
123
124         for (i = 0; i < sizeof(mac_data.res_1); i++)
125                 mac_data.res_1[i] = 0;
126         for (i = 0; i < sizeof(mac_data.res_2); i++)
127                 mac_data.res_2[i] = 0;
128         length = sizeof(EEPROM_data);
129         crc = crc32(crc, eeprom_data, length - 4);
130         mac_data.crc = crc;
131         for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) {
132                 ret =
133                     i2c_write(dev, i, 1, ptr,
134                               (length - i) < 8 ? (length - i) : 8);
135                 udelay(5000);   /* 5ms write cycle timing */
136                 if (ret)
137                         break;
138         }
139         if (ret) {
140                 printf("Programming failed.\n");
141                 return -1;
142         } else {
143                 printf("Programming %d bytes. Reading back ...\n", length);
144                 mac_read();
145         }
146         return 0;
147 }
148
149 int do_mac(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
150 {
151         int i;
152         char cmd = 's';
153         unsigned long long mac_val;
154
155         if (i2c_probe(ID_EEPROM_ADDR) != 0)
156                 return -1;
157
158         if (argc > 1) {
159                 cmd = argv[1][0];
160                 switch (cmd) {
161                 case 'r':       /* display */
162                         mac_read();
163                         break;
164                 case 's':       /* save */
165                         mac_prog();
166                         break;
167                 case 'i':       /* id */
168                         for (i = 0; i < 4; i++) {
169                                 mac_data.id[i] = argv[2][i];
170                         }
171                         break;
172                 case 'n':       /* serial number */
173                         for (i = 0; i < 12; i++) {
174                                 mac_data.sn[i] = argv[2][i];
175                         }
176                         break;
177                 case 'e':       /* errata */
178                         for (i = 0; i < 5; i++) {
179                                 mac_data.errata[i] = argv[2][i];
180                         }
181                         break;
182                 case 'd':       /* date */
183                         for (i = 0; i < 7; i++) {
184                                 mac_data.date[i] = argv[2][i];
185                         }
186                         break;
187                 case 'p':       /* number of ports */
188                         mac_data.tab_size =
189                             (unsigned char)simple_strtoul(argv[2], NULL, 16);
190                         break;
191                 case '0':       /* mac 0 */
192                 case '1':       /* mac 1 */
193                 case '2':       /* mac 2 */
194                 case '3':       /* mac 3 */
195                 case '4':       /* mac 4 */
196                 case '5':       /* mac 5 */
197                 case '6':       /* mac 6 */
198                 case '7':       /* mac 7 */
199                         mac_val = simple_strtoull(argv[2], NULL, 16);
200                         for (i = 0; i < 6; i++) {
201                                 mac_data.mac[cmd - '0'][i] =
202                                     *((unsigned char *)
203                                       (((unsigned int)(&mac_val)) + i + 2));
204                         }
205                         break;
206                 case 'h':       /* help */
207                 default:
208                         printf("Usage:\n%s\n", cmdtp->usage);
209                         break;
210                 }
211         } else {
212                 mac_show();
213         }
214         return 0;
215 }
216
217 int mac_read_from_eeprom(void)
218 {
219         int length, i;
220         unsigned char dev = ID_EEPROM_ADDR;
221         unsigned char *data;
222         unsigned char ethaddr[4][18];
223         unsigned char enetvar[32];
224         unsigned int crc = 0;
225
226         length = sizeof(EEPROM_data);
227         if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) {
228                 printf("Read failed.\n");
229                 return -1;
230         }
231
232         data = (unsigned char *)(&mac_data);
233         crc = crc32(crc, data, length - 4);
234         if (crc != mac_data.crc) {
235                 return -1;
236         } else {
237                 for (i = 0; i < 4; i++) {
238                         if (memcmp(&mac_data.mac[i], "\0\0\0\0\0\0", 6)) {
239                                 sprintf((char *)ethaddr[i],
240                                         "%02x:%02x:%02x:%02x:%02x:%02x",
241                                         mac_data.mac[i][0],
242                                         mac_data.mac[i][1],
243                                         mac_data.mac[i][2],
244                                         mac_data.mac[i][3],
245                                         mac_data.mac[i][4],
246                                         mac_data.mac[i][5]);
247                                 sprintf((char *)enetvar,
248                                         i ? "eth%daddr" : "ethaddr",
249                                         i);
250                                 setenv((char *)enetvar, (char *)ethaddr[i]);
251                         }
252                 }
253         }
254         return 0;
255 }
256 #endif /* CFG_ID_EEPROM */