blob: 2ce15680e1535f6a6907980770402754ee2fda5c [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001
3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#if defined(VXWORKS)
25# include <stdio.h>
26# include <string.h>
27# define CFG_DEF_EEPROM_ADDR 0xa0
28extern char iicReadByte( char, char );
29extern ulong_t crc32( unsigned char *, unsigned long );
30#else
31#include <common.h>
32#endif
33
34#include "vpd.h"
35
36/*
37 * vpd_reader() - reads VPD data from I2C EEPROMS.
38 * returns pointer to buffer or NULL.
39 */
40static unsigned char *
41vpd_reader(unsigned char *buf, unsigned dev_addr, unsigned off, unsigned count)
42{
43 unsigned offset = off; /* Calculated offset */
44
45 /*
46 * The main board EEPROM contains
47 * SDRAM SPD in the first 128 bytes,
48 * so skew the offset.
49 */
50 if (dev_addr == CFG_DEF_EEPROM_ADDR)
51 offset += SDRAM_SPD_DATA_SIZE;
52
53 /* Try to read the I2C EEPROM */
54#if defined(VXWORKS)
55 {
56 int i;
57 for( i = 0; i < count; ++i ) {
58 buf[ i ] = iicReadByte( dev_addr, offset+i );
59 }
60 }
61#else
62 if (eeprom_read(dev_addr, offset, buf, count)) {
63 printf("Failed to read %d bytes from VPD EEPROM 0x%x @ 0x%x\n",
64 count, dev_addr, offset);
65 return NULL;
66 }
67#endif
68
69 return buf;
70} /* vpd_reader() */
71
72
73/*
74 * vpd_get_packet() - returns next VPD packet or NULL.
75 */
76static vpd_packet_t *vpd_get_packet(vpd_packet_t *vpd_packet)
77{
78 vpd_packet_t *packet = vpd_packet;
79
80 if (packet != NULL) {
81 if (packet->identifier == VPD_PID_TERM)
82 return NULL;
83 else
84 packet = (vpd_packet_t *)((char *)packet + packet->size + 2);
85 }
86
87 return packet;
88} /* vpd_get_packet() */
89
90
91/*
92 * vpd_find_packet() - Locates and returns the specified
93 * VPD packet or NULL on error.
94 */
95static vpd_packet_t *vpd_find_packet(vpd_t *vpd, unsigned char ident)
96{
97 vpd_packet_t *packet = (vpd_packet_t *)&vpd->packets;
98
99 /* Guaranteed illegal */
100 if (ident == VPD_PID_GI)
101 return NULL;
102
103 /* Scan tuples looking for a match */
104 while ((packet->identifier != ident) &&
105 (packet->identifier != VPD_PID_TERM))
106 packet = vpd_get_packet(packet);
107
108 /* Did we find it? */
109 if ((packet->identifier) && (packet->identifier != ident))
110 return NULL;
111 return packet;
112}
113
114
115/*
116 * vpd_is_valid() - Validates contents of VPD data
117 * in I2C EEPROM. Returns 1 for
118 * success or 0 for failure.
119 */
120static int vpd_is_valid(unsigned dev_addr, unsigned char *buf)
121{
122 unsigned num_bytes;
123 vpd_packet_t *packet;
124 vpd_t *vpd = (vpd_t *)buf;
125 unsigned short stored_crc16, calc_crc16 = 0xffff;
126
127 /* Check Eyecatcher */
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200128 if (strncmp((char *)(vpd->header.eyecatcher), VPD_EYECATCHER, VPD_EYE_SIZE) != 0) {
wdenkc6097192002-11-03 00:24:07 +0000129 unsigned offset = 0;
130 if (dev_addr == CFG_DEF_EEPROM_ADDR)
131 offset += SDRAM_SPD_DATA_SIZE;
132 printf("Error: VPD EEPROM 0x%x corrupt @ 0x%x\n", dev_addr, offset);
133
134 return 0;
135 }
136
137 /* Check Length */
138 if (vpd->header.size> VPD_MAX_EEPROM_SIZE) {
139 printf("Error: VPD EEPROM 0x%x contains bad size 0x%x\n",
140 dev_addr, vpd->header.size);
141 return 0;
142 }
143
144 /* Now find the termination packet */
145 if ((packet = vpd_find_packet(vpd, VPD_PID_TERM)) == NULL) {
146 printf("Error: VPD EEPROM 0x%x missing termination packet\n",
147 dev_addr);
148 return 0;
149 }
150
151 /* Calculate data size */
152 num_bytes = (unsigned long)((unsigned char *)packet -
153 (unsigned char *)vpd + sizeof(vpd_packet_t));
154
155 /* Find stored CRC and clear it */
156 if ((packet = vpd_find_packet(vpd, VPD_PID_CRC)) == NULL) {
157 printf("Error: VPD EEPROM 0x%x missing CRC\n", dev_addr);
158 return 0;
159 }
160 stored_crc16 = *((ushort *)packet->data);
161 *(ushort *)packet->data = 0;
162
163 /* OK, lets calculate the CRC and check it */
164#if defined(VXWORKS)
165 calc_crc16 = (0xffff & crc32(buf, num_bytes));
166#else
167 calc_crc16 = (0xffff & crc32(0, buf, num_bytes));
168#endif
169 *(ushort *)packet->data = stored_crc16; /* Now restore the CRC */
170 if (stored_crc16 != calc_crc16) {
171 printf("Error: VPD EEPROM 0x%x has bad CRC 0x%x\n",
172 dev_addr, stored_crc16);
173 return 0;
174 }
175
176 return 1;
177} /* vpd_is_valid() */
178
179
180/*
181 * size_ok() - Check to see if packet size matches
182 * size of data we want. Returns 1 for
183 * good match or 0 for failure.
184 */
185static int size_ok(vpd_packet_t *packet, unsigned long size)
186{
187 if (packet->size != size) {
188 printf("VPD Packet 0x%x corrupt.\n", packet->identifier);
189 return 0;
190 }
191 return 1;
192} /* size_ok() */
193
194
195/*
196 * strlen_ok() - Check to see if packet size matches
197 * strlen of the string we want to populate.
198 * Returns 1 for valid length or 0 for failure.
199 */
200static int strlen_ok(vpd_packet_t *packet, unsigned long length)
201{
202 if (packet->size >= length) {
203 printf("VPD Packet 0x%x corrupt.\n", packet->identifier);
204 return 0;
205 }
206 return 1;
207} /* strlen_ok() */
208
209
210/*
211 * get_vpd_data() - populates the passed VPD structure 'vpdInfo'
212 * with data obtained from the specified
213 * I2C EEPROM 'dev_addr'. Returns 0 for
214 * success or 1 for failure.
215 */
216int vpd_get_data(unsigned char dev_addr, VPD *vpdInfo)
217{
218 unsigned char buf[VPD_EEPROM_SIZE];
219 vpd_t *vpd = (vpd_t *)buf;
220 vpd_packet_t *packet;
221
222 if (vpdInfo == NULL)
223 return 1;
224
225 /*
226 * Fill vpdInfo with 0s to blank out
227 * unused fields, fill vpdInfo->ethAddrs
228 * with all 0xffs so that other's code can
229 * determine how many real Ethernet addresses
230 * there are. OUIs starting with 0xff are
231 * broadcast addresses, and would never be
232 * permantely stored.
233 */
234 memset((void *)vpdInfo, 0, sizeof(VPD));
235 memset((void *)&vpdInfo->ethAddrs, 0xff, sizeof(vpdInfo->ethAddrs));
236 vpdInfo->_devAddr = dev_addr;
237
238 /* Read the minimum size first */
239 if (vpd_reader(buf, dev_addr, 0, VPD_EEPROM_SIZE) == NULL) {
240 return 1;
241 }
242
243 /* Check validity of VPD data */
244 if (!vpd_is_valid(dev_addr, buf)) {
245 printf("VPD Data is INVALID!\n");
246 return 1;
247 }
248
249 /*
250 * Walk all the packets and populate
251 * the VPD info structure.
252 */
253 packet = (vpd_packet_t *)&vpd->packets;
254 do {
255 switch (packet->identifier) {
256 case VPD_PID_GI:
257 printf("Error: Illegal VPD value\n");
258 break;
259 case VPD_PID_PID:
260 if (strlen_ok(packet, MAX_PROD_ID)) {
261 strncpy(vpdInfo->productId,
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200262 (char *)(packet->data), packet->size);
wdenkc6097192002-11-03 00:24:07 +0000263 }
264 break;
265 case VPD_PID_REV:
266 if (size_ok(packet, sizeof(char)))
267 vpdInfo->revisionId = *packet->data;
268 break;
269 case VPD_PID_SN:
270 if (size_ok(packet, sizeof(unsigned long))) {
271 vpdInfo->serialNum =
272 *(unsigned long *)packet->data;
273 }
274 break;
275 case VPD_PID_MANID:
276 if (size_ok(packet, sizeof(unsigned char)))
277 vpdInfo->manuID = *packet->data;
278 break;
279 case VPD_PID_PCO:
280 if (size_ok(packet, sizeof(unsigned long))) {
281 vpdInfo->configOpt =
282 *(unsigned long *)packet->data;
283 }
284 break;
285 case VPD_PID_SYSCLK:
286 if (size_ok(packet, sizeof(unsigned long)))
287 vpdInfo->sysClk = *(unsigned long *)packet->data;
288 break;
289 case VPD_PID_SERCLK:
290 if (size_ok(packet, sizeof(unsigned long)))
291 vpdInfo->serClk = *(unsigned long *)packet->data;
292 break;
293 case VPD_PID_FLASH:
294 if (size_ok(packet, 9)) { /* XXX - hardcoded,
295 padding in struct */
296 memcpy(&vpdInfo->flashCfg, packet->data, 9);
297 }
298 break;
299 case VPD_PID_ETHADDR:
300 memcpy(vpdInfo->ethAddrs, packet->data, packet->size);
301 break;
302 case VPD_PID_POTS:
303 if (size_ok(packet, sizeof(char)))
304 vpdInfo->numPOTS = (unsigned)*packet->data;
305 break;
306 case VPD_PID_DS1:
307 if (size_ok(packet, sizeof(char)))
308 vpdInfo->numDS1 = (unsigned)*packet->data;
309 case VPD_PID_GAL:
310 case VPD_PID_CRC:
311 case VPD_PID_TERM:
312 break;
313 default:
314 printf("Warning: Found unknown VPD packet ID 0x%x\n",
315 packet->identifier);
316 break;
317 }
318 } while ((packet = vpd_get_packet(packet)));
319
320 return 0;
321} /* end get_vpd_data() */
322
323
324/*
325 * vpd_init() - Initialize default VPD environment
326 */
327int vpd_init(unsigned char dev_addr)
328{
329 return (0);
330} /* vpd_init() */
331
332
333/*
334 * vpd_print() - Pretty print the VPD data.
335 */
336void vpd_print(VPD *vpdInfo)
337{
338 const char *const sp = "";
339 const char *const sfmt = "%4s%-20s: \"%s\"\n";
340 const char *const cfmt = "%4s%-20s: '%c'\n";
341 const char *const dfmt = "%4s%-20s: %ld\n";
342 const char *const hfmt = "%4s%-20s: %08lX\n";
343 const char *const dsfmt = "%4s%-20s: %d\n";
344 const char *const hsfmt = "%4s%-20s: %04X\n";
345 const char *const dhfmt = "%4s%-20s: %ld (%lX)\n";
346
347 printf("VPD read from I2C device: %02X\n", vpdInfo->_devAddr);
348
349 if (vpdInfo->productId[0])
350 printf(sfmt, sp, "Product ID", vpdInfo->productId);
351 else
352 printf(sfmt, sp, "Product ID", "UNKNOWN");
353
354 if (vpdInfo->revisionId)
355 printf(cfmt, sp, "Revision ID", vpdInfo->revisionId);
356
357 if (vpdInfo->serialNum)
358 printf(dfmt, sp, "Serial Number", vpdInfo->serialNum);
359
360 if (vpdInfo->manuID)
361 printf(dfmt, sp, "Manufacture ID", (long)vpdInfo->manuID);
362
363 if (vpdInfo->configOpt)
364 printf(hfmt, sp, "Configuration", vpdInfo->configOpt);
365
366 if (vpdInfo->sysClk)
367 printf(dhfmt, sp, "System Clock", vpdInfo->sysClk, vpdInfo->sysClk);
368
369 if (vpdInfo->serClk)
370 printf(dhfmt, sp, "Serial Clock", vpdInfo->serClk, vpdInfo->serClk);
371
372 if (vpdInfo->numPOTS)
373 printf(dfmt, sp, "Number of POTS lines", vpdInfo->numPOTS);
374
375 if (vpdInfo->numDS1)
376 printf(dfmt, sp, "Number of DS1s", vpdInfo->numDS1);
377
378 /* Print Ethernet Addresses */
379 if (vpdInfo->ethAddrs[0][0] != 0xff) {
380 int i, j;
381 printf("%4sEtherNet Address(es): ", sp);
382 for (i = 0; i < MAX_ETH_ADDRS; i++) {
383 if (vpdInfo->ethAddrs[i][0] != 0xff) {
384 for (j = 0; j < 6; j++) {
385 printf("%02X", vpdInfo->ethAddrs[i][j]);
386 if (((j + 1) % 6) != 0)
387 printf(":");
388 else
389 printf(" ");
390 }
391 if (((i + 1) % 3) == 0) printf("\n%24s: ", sp);
392 }
393 }
394 printf("\n");
395 }
396
397 if (vpdInfo->flashCfg.mfg && vpdInfo->flashCfg.dev) {
398 printf("Main Flash Configuration:\n");
399 printf(hsfmt, sp, "Manufacture ID", vpdInfo->flashCfg.mfg);
400 printf(hsfmt, sp, "Device ID", vpdInfo->flashCfg.dev);
401 printf(dsfmt, sp, "Device Width", vpdInfo->flashCfg.devWidth);
402 printf(dsfmt, sp, "Num. Devices", vpdInfo->flashCfg.numDevs);
403 printf(dsfmt, sp, "Num. Columns", vpdInfo->flashCfg.numCols);
404 printf(dsfmt, sp, "Column Width", vpdInfo->flashCfg.colWidth);
405 printf(dsfmt, sp, "WE Data Width", vpdInfo->flashCfg.weDataWidth);
406 }
407} /* vpd_print() */