3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
5 * SPDX-License-Identifier: GPL-2.0+
11 #define CONFIG_SYS_DEF_EEPROM_ADDR 0xa0
12 extern char iicReadByte(char, char);
13 extern ulong_t crc32(unsigned char *, unsigned long);
21 * vpd_reader() - reads VPD data from I2C EEPROMS.
22 * returns pointer to buffer or NULL.
24 static unsigned char *vpd_reader(unsigned char *buf, unsigned dev_addr,
25 unsigned off, unsigned count)
27 unsigned offset = off; /* Calculated offset */
30 * The main board EEPROM contains
31 * SDRAM SPD in the first 128 bytes,
34 if (dev_addr == CONFIG_SYS_DEF_EEPROM_ADDR)
35 offset += SDRAM_SPD_DATA_SIZE;
37 /* Try to read the I2C EEPROM */
42 for (i = 0; i < count; ++i)
43 buf[i] = iicReadByte(dev_addr, offset + i);
46 if (eeprom_read(dev_addr, offset, buf, count)) {
47 printf("Failed to read %d bytes from VPD EEPROM 0x%x @ 0x%x\n",
48 count, dev_addr, offset);
58 * vpd_get_packet() - returns next VPD packet or NULL.
60 static vpd_packet_t *vpd_get_packet(vpd_packet_t * vpd_packet)
62 vpd_packet_t *packet = vpd_packet;
65 if (packet->identifier == VPD_PID_TERM)
68 packet = (vpd_packet_t *) ((char *) packet +
77 * vpd_find_packet() - Locates and returns the specified
78 * VPD packet or NULL on error.
80 static vpd_packet_t *vpd_find_packet(vpd_t * vpd, unsigned char ident)
82 vpd_packet_t *packet = (vpd_packet_t *) &vpd->packets;
84 /* Guaranteed illegal */
85 if (ident == VPD_PID_GI)
88 /* Scan tuples looking for a match */
89 while ((packet->identifier != ident) &&
90 (packet->identifier != VPD_PID_TERM))
91 packet = vpd_get_packet(packet);
94 if ((packet->identifier) && (packet->identifier != ident))
101 * vpd_is_valid() - Validates contents of VPD data
102 * in I2C EEPROM. Returns 1 for
103 * success or 0 for failure.
105 static int vpd_is_valid(unsigned dev_addr, unsigned char *buf)
108 vpd_packet_t *packet;
109 vpd_t *vpd = (vpd_t *) buf;
110 unsigned short stored_crc16, calc_crc16 = 0xffff;
112 /* Check Eyecatcher */
114 ((char *) (vpd->header.eyecatcher), VPD_EYECATCHER,
115 VPD_EYE_SIZE) != 0) {
118 if (dev_addr == CONFIG_SYS_DEF_EEPROM_ADDR)
119 offset += SDRAM_SPD_DATA_SIZE;
120 printf("Error: VPD EEPROM 0x%x corrupt @ 0x%x\n", dev_addr,
127 if (vpd->header.size > VPD_MAX_EEPROM_SIZE) {
128 printf("Error: VPD EEPROM 0x%x contains bad size 0x%x\n",
129 dev_addr, vpd->header.size);
133 /* Now find the termination packet */
134 packet = vpd_find_packet(vpd, VPD_PID_TERM);
135 if (packet == NULL) {
136 printf("Error: VPD EEPROM 0x%x missing termination packet\n",
141 /* Calculate data size */
142 num_bytes = (unsigned long) ((unsigned char *) packet -
143 (unsigned char *) vpd +
144 sizeof(vpd_packet_t));
146 /* Find stored CRC and clear it */
147 packet = vpd_find_packet(vpd, VPD_PID_CRC);
148 if (packet == NULL) {
149 printf("Error: VPD EEPROM 0x%x missing CRC\n", dev_addr);
152 memcpy(&stored_crc16, packet->data, sizeof(ushort));
153 memset(packet->data, 0, sizeof(ushort));
155 /* OK, lets calculate the CRC and check it */
157 calc_crc16 = (0xffff & crc32(buf, num_bytes));
159 calc_crc16 = (0xffff & crc32(0, buf, num_bytes));
161 /* Now restore the CRC */
162 memcpy(packet->data, &stored_crc16, sizeof(ushort));
163 if (stored_crc16 != calc_crc16) {
164 printf("Error: VPD EEPROM 0x%x has bad CRC 0x%x\n",
165 dev_addr, stored_crc16);
174 * size_ok() - Check to see if packet size matches
175 * size of data we want. Returns 1 for
176 * good match or 0 for failure.
178 static int size_ok(vpd_packet_t *packet, unsigned long size)
180 if (packet->size != size) {
181 printf("VPD Packet 0x%x corrupt.\n", packet->identifier);
189 * strlen_ok() - Check to see if packet size matches
190 * strlen of the string we want to populate.
191 * Returns 1 for valid length or 0 for failure.
193 static int strlen_ok(vpd_packet_t *packet, unsigned long length)
195 if (packet->size >= length) {
196 printf("VPD Packet 0x%x corrupt.\n", packet->identifier);
204 * get_vpd_data() - populates the passed VPD structure 'vpdInfo'
205 * with data obtained from the specified
206 * I2C EEPROM 'dev_addr'. Returns 0 for
207 * success or 1 for failure.
209 int vpd_get_data(unsigned char dev_addr, VPD *vpdInfo)
211 unsigned char buf[VPD_EEPROM_SIZE];
212 vpd_t *vpd = (vpd_t *) buf;
213 vpd_packet_t *packet;
219 * Fill vpdInfo with 0s to blank out
220 * unused fields, fill vpdInfo->ethAddrs
221 * with all 0xffs so that other's code can
222 * determine how many real Ethernet addresses
223 * there are. OUIs starting with 0xff are
224 * broadcast addresses, and would never be
227 memset((void *) vpdInfo, 0, sizeof(VPD));
228 memset((void *) &vpdInfo->ethAddrs, 0xff, sizeof(vpdInfo->ethAddrs));
229 vpdInfo->_devAddr = dev_addr;
231 /* Read the minimum size first */
232 if (vpd_reader(buf, dev_addr, 0, VPD_EEPROM_SIZE) == NULL)
235 /* Check validity of VPD data */
236 if (!vpd_is_valid(dev_addr, buf)) {
237 printf("VPD Data is INVALID!\n");
242 * Walk all the packets and populate
243 * the VPD info structure.
245 packet = (vpd_packet_t *) &vpd->packets;
247 switch (packet->identifier) {
249 printf("Error: Illegal VPD value\n");
252 if (strlen_ok(packet, MAX_PROD_ID)) {
253 strncpy(vpdInfo->productId,
254 (char *) (packet->data),
259 if (size_ok(packet, sizeof(char)))
260 vpdInfo->revisionId = *packet->data;
263 if (size_ok(packet, sizeof(unsigned long))) {
264 memcpy(&vpdInfo->serialNum,
266 sizeof(unsigned long));
270 if (size_ok(packet, sizeof(unsigned char)))
271 vpdInfo->manuID = *packet->data;
274 if (size_ok(packet, sizeof(unsigned long))) {
275 memcpy(&vpdInfo->configOpt,
277 sizeof(unsigned long));
281 if (size_ok(packet, sizeof(unsigned long)))
282 memcpy(&vpdInfo->sysClk,
284 sizeof(unsigned long));
287 if (size_ok(packet, sizeof(unsigned long)))
288 memcpy(&vpdInfo->serClk,
290 sizeof(unsigned long));
293 if (size_ok(packet, 9)) { /* XXX - hardcoded,
295 memcpy(&vpdInfo->flashCfg, packet->data, 9);
298 case VPD_PID_ETHADDR:
299 memcpy(vpdInfo->ethAddrs, packet->data, packet->size);
302 if (size_ok(packet, sizeof(char)))
303 vpdInfo->numPOTS = (unsigned) *packet->data;
306 if (size_ok(packet, sizeof(char)))
307 vpdInfo->numDS1 = (unsigned) *packet->data;
313 printf("Warning: Found unknown VPD packet ID 0x%x\n",
317 } while ((packet = vpd_get_packet(packet)));
324 * vpd_init() - Initialize default VPD environment
326 int vpd_init(unsigned char dev_addr)
333 * vpd_print() - Pretty print the VPD data.
335 void vpd_print(VPD *vpdInfo)
337 const char *const sp = "";
338 const char *const sfmt = "%4s%-20s: \"%s\"\n";
339 const char *const cfmt = "%4s%-20s: '%c'\n";
340 const char *const dfmt = "%4s%-20s: %ld\n";
341 const char *const hfmt = "%4s%-20s: %08lX\n";
342 const char *const dsfmt = "%4s%-20s: %d\n";
343 const char *const hsfmt = "%4s%-20s: %04X\n";
344 const char *const dhfmt = "%4s%-20s: %ld (%lX)\n";
346 printf("VPD read from I2C device: %02X\n", vpdInfo->_devAddr);
348 if (vpdInfo->productId[0])
349 printf(sfmt, sp, "Product ID", vpdInfo->productId);
351 printf(sfmt, sp, "Product ID", "UNKNOWN");
353 if (vpdInfo->revisionId)
354 printf(cfmt, sp, "Revision ID", vpdInfo->revisionId);
356 if (vpdInfo->serialNum)
357 printf(dfmt, sp, "Serial Number", vpdInfo->serialNum);
360 printf(dfmt, sp, "Manufacture ID", (long) vpdInfo->manuID);
362 if (vpdInfo->configOpt)
363 printf(hfmt, sp, "Configuration", vpdInfo->configOpt);
366 printf(dhfmt, sp, "System Clock", vpdInfo->sysClk,
370 printf(dhfmt, sp, "Serial Clock", vpdInfo->serClk,
373 if (vpdInfo->numPOTS)
374 printf(dfmt, sp, "Number of POTS lines", vpdInfo->numPOTS);
377 printf(dfmt, sp, "Number of DS1s", vpdInfo->numDS1);
379 /* Print Ethernet Addresses */
380 if (vpdInfo->ethAddrs[0][0] != 0xff) {
383 printf("%4sEtherNet Address(es): ", sp);
384 for (i = 0; i < MAX_ETH_ADDRS; i++) {
385 if (vpdInfo->ethAddrs[i][0] != 0xff) {
386 for (j = 0; j < 6; j++) {
388 vpdInfo->ethAddrs[i][j]);
389 if (((j + 1) % 6) != 0)
394 if (((i + 1) % 3) == 0)
395 printf("\n%24s: ", sp);
401 if (vpdInfo->flashCfg.mfg && vpdInfo->flashCfg.dev) {
402 printf("Main Flash Configuration:\n");
403 printf(hsfmt, sp, "Manufacture ID", vpdInfo->flashCfg.mfg);
404 printf(hsfmt, sp, "Device ID", vpdInfo->flashCfg.dev);
405 printf(dsfmt, sp, "Device Width", vpdInfo->flashCfg.devWidth);
406 printf(dsfmt, sp, "Num. Devices", vpdInfo->flashCfg.numDevs);
407 printf(dsfmt, sp, "Num. Columns", vpdInfo->flashCfg.numCols);
408 printf(dsfmt, sp, "Column Width", vpdInfo->flashCfg.colWidth);
409 printf(dsfmt, sp, "WE Data Width",
410 vpdInfo->flashCfg.weDataWidth);