vpd.c 11 KB

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