vpd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  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. #if defined(VXWORKS)
  24. # include <stdio.h>
  25. # include <string.h>
  26. # define CONFIG_SYS_DEF_EEPROM_ADDR 0xa0
  27. extern char iicReadByte( char, char );
  28. extern ulong_t crc32( unsigned char *, unsigned long );
  29. #else
  30. #include <common.h>
  31. #endif
  32. #include "vpd.h"
  33. /*
  34. * vpd_reader() - reads VPD data from I2C EEPROMS.
  35. * returns pointer to buffer or NULL.
  36. */
  37. static unsigned char *
  38. vpd_reader(unsigned char *buf, unsigned dev_addr, unsigned off, unsigned count)
  39. {
  40. unsigned offset = off; /* Calculated offset */
  41. /*
  42. * The main board EEPROM contains
  43. * SDRAM SPD in the first 128 bytes,
  44. * so skew the offset.
  45. */
  46. if (dev_addr == CONFIG_SYS_DEF_EEPROM_ADDR)
  47. offset += SDRAM_SPD_DATA_SIZE;
  48. /* Try to read the I2C EEPROM */
  49. #if defined(VXWORKS)
  50. {
  51. int i;
  52. for( i = 0; i < count; ++i ) {
  53. buf[ i ] = iicReadByte( dev_addr, offset+i );
  54. }
  55. }
  56. #else
  57. if (eeprom_read(dev_addr, offset, buf, count)) {
  58. printf("Failed to read %d bytes from VPD EEPROM 0x%x @ 0x%x\n",
  59. count, dev_addr, offset);
  60. return NULL;
  61. }
  62. #endif
  63. return buf;
  64. } /* vpd_reader() */
  65. /*
  66. * vpd_get_packet() - returns next VPD packet or NULL.
  67. */
  68. static vpd_packet_t *vpd_get_packet(vpd_packet_t *vpd_packet)
  69. {
  70. vpd_packet_t *packet = vpd_packet;
  71. if (packet != NULL) {
  72. if (packet->identifier == VPD_PID_TERM)
  73. return NULL;
  74. else
  75. packet = (vpd_packet_t *)((char *)packet + packet->size + 2);
  76. }
  77. return packet;
  78. } /* vpd_get_packet() */
  79. /*
  80. * vpd_find_packet() - Locates and returns the specified
  81. * VPD packet or NULL on error.
  82. */
  83. static vpd_packet_t *vpd_find_packet(vpd_t *vpd, unsigned char ident)
  84. {
  85. vpd_packet_t *packet = (vpd_packet_t *)&vpd->packets;
  86. /* Guaranteed illegal */
  87. if (ident == VPD_PID_GI)
  88. return NULL;
  89. /* Scan tuples looking for a match */
  90. while ((packet->identifier != ident) &&
  91. (packet->identifier != VPD_PID_TERM))
  92. packet = vpd_get_packet(packet);
  93. /* Did we find it? */
  94. if ((packet->identifier) && (packet->identifier != ident))
  95. return NULL;
  96. return packet;
  97. }
  98. /*
  99. * vpd_is_valid() - Validates contents of VPD data
  100. * in I2C EEPROM. Returns 1 for
  101. * success or 0 for failure.
  102. */
  103. static int vpd_is_valid(unsigned dev_addr, unsigned char *buf)
  104. {
  105. unsigned num_bytes;
  106. vpd_packet_t *packet;
  107. vpd_t *vpd = (vpd_t *)buf;
  108. unsigned short stored_crc16, calc_crc16 = 0xffff;
  109. /* Check Eyecatcher */
  110. if (strncmp((char *)(vpd->header.eyecatcher), VPD_EYECATCHER, VPD_EYE_SIZE) != 0) {
  111. unsigned offset = 0;
  112. if (dev_addr == CONFIG_SYS_DEF_EEPROM_ADDR)
  113. offset += SDRAM_SPD_DATA_SIZE;
  114. printf("Error: VPD EEPROM 0x%x corrupt @ 0x%x\n", dev_addr, offset);
  115. return 0;
  116. }
  117. /* Check Length */
  118. if (vpd->header.size> VPD_MAX_EEPROM_SIZE) {
  119. printf("Error: VPD EEPROM 0x%x contains bad size 0x%x\n",
  120. dev_addr, vpd->header.size);
  121. return 0;
  122. }
  123. /* Now find the termination packet */
  124. if ((packet = vpd_find_packet(vpd, VPD_PID_TERM)) == NULL) {
  125. printf("Error: VPD EEPROM 0x%x missing termination packet\n",
  126. dev_addr);
  127. return 0;
  128. }
  129. /* Calculate data size */
  130. num_bytes = (unsigned long)((unsigned char *)packet -
  131. (unsigned char *)vpd + sizeof(vpd_packet_t));
  132. /* Find stored CRC and clear it */
  133. if ((packet = vpd_find_packet(vpd, VPD_PID_CRC)) == NULL) {
  134. printf("Error: VPD EEPROM 0x%x missing CRC\n", dev_addr);
  135. return 0;
  136. }
  137. stored_crc16 = *((ushort *)packet->data);
  138. *(ushort *)packet->data = 0;
  139. /* OK, lets calculate the CRC and check it */
  140. #if defined(VXWORKS)
  141. calc_crc16 = (0xffff & crc32(buf, num_bytes));
  142. #else
  143. calc_crc16 = (0xffff & crc32(0, buf, num_bytes));
  144. #endif
  145. *(ushort *)packet->data = stored_crc16; /* Now restore the CRC */
  146. if (stored_crc16 != calc_crc16) {
  147. printf("Error: VPD EEPROM 0x%x has bad CRC 0x%x\n",
  148. dev_addr, stored_crc16);
  149. return 0;
  150. }
  151. return 1;
  152. } /* vpd_is_valid() */
  153. /*
  154. * size_ok() - Check to see if packet size matches
  155. * size of data we want. Returns 1 for
  156. * good match or 0 for failure.
  157. */
  158. static int size_ok(vpd_packet_t *packet, unsigned long size)
  159. {
  160. if (packet->size != size) {
  161. printf("VPD Packet 0x%x corrupt.\n", packet->identifier);
  162. return 0;
  163. }
  164. return 1;
  165. } /* size_ok() */
  166. /*
  167. * strlen_ok() - Check to see if packet size matches
  168. * strlen of the string we want to populate.
  169. * Returns 1 for valid length or 0 for failure.
  170. */
  171. static int strlen_ok(vpd_packet_t *packet, unsigned long length)
  172. {
  173. if (packet->size >= length) {
  174. printf("VPD Packet 0x%x corrupt.\n", packet->identifier);
  175. return 0;
  176. }
  177. return 1;
  178. } /* strlen_ok() */
  179. /*
  180. * get_vpd_data() - populates the passed VPD structure 'vpdInfo'
  181. * with data obtained from the specified
  182. * I2C EEPROM 'dev_addr'. Returns 0 for
  183. * success or 1 for failure.
  184. */
  185. int vpd_get_data(unsigned char dev_addr, VPD *vpdInfo)
  186. {
  187. unsigned char buf[VPD_EEPROM_SIZE];
  188. vpd_t *vpd = (vpd_t *)buf;
  189. vpd_packet_t *packet;
  190. if (vpdInfo == NULL)
  191. return 1;
  192. /*
  193. * Fill vpdInfo with 0s to blank out
  194. * unused fields, fill vpdInfo->ethAddrs
  195. * with all 0xffs so that other's code can
  196. * determine how many real Ethernet addresses
  197. * there are. OUIs starting with 0xff are
  198. * broadcast addresses, and would never be
  199. * permantely stored.
  200. */
  201. memset((void *)vpdInfo, 0, sizeof(VPD));
  202. memset((void *)&vpdInfo->ethAddrs, 0xff, sizeof(vpdInfo->ethAddrs));
  203. vpdInfo->_devAddr = dev_addr;
  204. /* Read the minimum size first */
  205. if (vpd_reader(buf, dev_addr, 0, VPD_EEPROM_SIZE) == NULL) {
  206. return 1;
  207. }
  208. /* Check validity of VPD data */
  209. if (!vpd_is_valid(dev_addr, buf)) {
  210. printf("VPD Data is INVALID!\n");
  211. return 1;
  212. }
  213. /*
  214. * Walk all the packets and populate
  215. * the VPD info structure.
  216. */
  217. packet = (vpd_packet_t *)&vpd->packets;
  218. do {
  219. switch (packet->identifier) {
  220. case VPD_PID_GI:
  221. printf("Error: Illegal VPD value\n");
  222. break;
  223. case VPD_PID_PID:
  224. if (strlen_ok(packet, MAX_PROD_ID)) {
  225. strncpy(vpdInfo->productId,
  226. (char *)(packet->data), packet->size);
  227. }
  228. break;
  229. case VPD_PID_REV:
  230. if (size_ok(packet, sizeof(char)))
  231. vpdInfo->revisionId = *packet->data;
  232. break;
  233. case VPD_PID_SN:
  234. if (size_ok(packet, sizeof(unsigned long))) {
  235. vpdInfo->serialNum =
  236. *(unsigned long *)packet->data;
  237. }
  238. break;
  239. case VPD_PID_MANID:
  240. if (size_ok(packet, sizeof(unsigned char)))
  241. vpdInfo->manuID = *packet->data;
  242. break;
  243. case VPD_PID_PCO:
  244. if (size_ok(packet, sizeof(unsigned long))) {
  245. vpdInfo->configOpt =
  246. *(unsigned long *)packet->data;
  247. }
  248. break;
  249. case VPD_PID_SYSCLK:
  250. if (size_ok(packet, sizeof(unsigned long)))
  251. vpdInfo->sysClk = *(unsigned long *)packet->data;
  252. break;
  253. case VPD_PID_SERCLK:
  254. if (size_ok(packet, sizeof(unsigned long)))
  255. vpdInfo->serClk = *(unsigned long *)packet->data;
  256. break;
  257. case VPD_PID_FLASH:
  258. if (size_ok(packet, 9)) { /* XXX - hardcoded,
  259. padding in struct */
  260. memcpy(&vpdInfo->flashCfg, packet->data, 9);
  261. }
  262. break;
  263. case VPD_PID_ETHADDR:
  264. memcpy(vpdInfo->ethAddrs, packet->data, packet->size);
  265. break;
  266. case VPD_PID_POTS:
  267. if (size_ok(packet, sizeof(char)))
  268. vpdInfo->numPOTS = (unsigned)*packet->data;
  269. break;
  270. case VPD_PID_DS1:
  271. if (size_ok(packet, sizeof(char)))
  272. vpdInfo->numDS1 = (unsigned)*packet->data;
  273. case VPD_PID_GAL:
  274. case VPD_PID_CRC:
  275. case VPD_PID_TERM:
  276. break;
  277. default:
  278. printf("Warning: Found unknown VPD packet ID 0x%x\n",
  279. packet->identifier);
  280. break;
  281. }
  282. } while ((packet = vpd_get_packet(packet)));
  283. return 0;
  284. } /* end get_vpd_data() */
  285. /*
  286. * vpd_init() - Initialize default VPD environment
  287. */
  288. int vpd_init(unsigned char dev_addr)
  289. {
  290. return (0);
  291. } /* vpd_init() */
  292. /*
  293. * vpd_print() - Pretty print the VPD data.
  294. */
  295. void vpd_print(VPD *vpdInfo)
  296. {
  297. const char *const sp = "";
  298. const char *const sfmt = "%4s%-20s: \"%s\"\n";
  299. const char *const cfmt = "%4s%-20s: '%c'\n";
  300. const char *const dfmt = "%4s%-20s: %ld\n";
  301. const char *const hfmt = "%4s%-20s: %08lX\n";
  302. const char *const dsfmt = "%4s%-20s: %d\n";
  303. const char *const hsfmt = "%4s%-20s: %04X\n";
  304. const char *const dhfmt = "%4s%-20s: %ld (%lX)\n";
  305. printf("VPD read from I2C device: %02X\n", vpdInfo->_devAddr);
  306. if (vpdInfo->productId[0])
  307. printf(sfmt, sp, "Product ID", vpdInfo->productId);
  308. else
  309. printf(sfmt, sp, "Product ID", "UNKNOWN");
  310. if (vpdInfo->revisionId)
  311. printf(cfmt, sp, "Revision ID", vpdInfo->revisionId);
  312. if (vpdInfo->serialNum)
  313. printf(dfmt, sp, "Serial Number", vpdInfo->serialNum);
  314. if (vpdInfo->manuID)
  315. printf(dfmt, sp, "Manufacture ID", (long)vpdInfo->manuID);
  316. if (vpdInfo->configOpt)
  317. printf(hfmt, sp, "Configuration", vpdInfo->configOpt);
  318. if (vpdInfo->sysClk)
  319. printf(dhfmt, sp, "System Clock", vpdInfo->sysClk, vpdInfo->sysClk);
  320. if (vpdInfo->serClk)
  321. printf(dhfmt, sp, "Serial Clock", vpdInfo->serClk, vpdInfo->serClk);
  322. if (vpdInfo->numPOTS)
  323. printf(dfmt, sp, "Number of POTS lines", vpdInfo->numPOTS);
  324. if (vpdInfo->numDS1)
  325. printf(dfmt, sp, "Number of DS1s", vpdInfo->numDS1);
  326. /* Print Ethernet Addresses */
  327. if (vpdInfo->ethAddrs[0][0] != 0xff) {
  328. int i, j;
  329. printf("%4sEtherNet Address(es): ", sp);
  330. for (i = 0; i < MAX_ETH_ADDRS; i++) {
  331. if (vpdInfo->ethAddrs[i][0] != 0xff) {
  332. for (j = 0; j < 6; j++) {
  333. printf("%02X", vpdInfo->ethAddrs[i][j]);
  334. if (((j + 1) % 6) != 0)
  335. printf(":");
  336. else
  337. printf(" ");
  338. }
  339. if (((i + 1) % 3) == 0) printf("\n%24s: ", sp);
  340. }
  341. }
  342. printf("\n");
  343. }
  344. if (vpdInfo->flashCfg.mfg && vpdInfo->flashCfg.dev) {
  345. printf("Main Flash Configuration:\n");
  346. printf(hsfmt, sp, "Manufacture ID", vpdInfo->flashCfg.mfg);
  347. printf(hsfmt, sp, "Device ID", vpdInfo->flashCfg.dev);
  348. printf(dsfmt, sp, "Device Width", vpdInfo->flashCfg.devWidth);
  349. printf(dsfmt, sp, "Num. Devices", vpdInfo->flashCfg.numDevs);
  350. printf(dsfmt, sp, "Num. Columns", vpdInfo->flashCfg.numCols);
  351. printf(dsfmt, sp, "Column Width", vpdInfo->flashCfg.colWidth);
  352. printf(dsfmt, sp, "WE Data Width", vpdInfo->flashCfg.weDataWidth);
  353. }
  354. } /* vpd_print() */