ixgb_ee.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*******************************************************************************
  2. Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 2 of the License, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc., 59
  13. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. The full GNU General Public License is included in this distribution in the
  15. file called LICENSE.
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include "ixgb_hw.h"
  21. #include "ixgb_ee.h"
  22. /* Local prototypes */
  23. static uint16_t ixgb_shift_in_bits(struct ixgb_hw *hw);
  24. static void ixgb_shift_out_bits(struct ixgb_hw *hw,
  25. uint16_t data,
  26. uint16_t count);
  27. static void ixgb_standby_eeprom(struct ixgb_hw *hw);
  28. static boolean_t ixgb_wait_eeprom_command(struct ixgb_hw *hw);
  29. static void ixgb_cleanup_eeprom(struct ixgb_hw *hw);
  30. /******************************************************************************
  31. * Raises the EEPROM's clock input.
  32. *
  33. * hw - Struct containing variables accessed by shared code
  34. * eecd_reg - EECD's current value
  35. *****************************************************************************/
  36. static void
  37. ixgb_raise_clock(struct ixgb_hw *hw,
  38. uint32_t *eecd_reg)
  39. {
  40. /* Raise the clock input to the EEPROM (by setting the SK bit), and then
  41. * wait 50 microseconds.
  42. */
  43. *eecd_reg = *eecd_reg | IXGB_EECD_SK;
  44. IXGB_WRITE_REG(hw, EECD, *eecd_reg);
  45. udelay(50);
  46. return;
  47. }
  48. /******************************************************************************
  49. * Lowers the EEPROM's clock input.
  50. *
  51. * hw - Struct containing variables accessed by shared code
  52. * eecd_reg - EECD's current value
  53. *****************************************************************************/
  54. static void
  55. ixgb_lower_clock(struct ixgb_hw *hw,
  56. uint32_t *eecd_reg)
  57. {
  58. /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
  59. * wait 50 microseconds.
  60. */
  61. *eecd_reg = *eecd_reg & ~IXGB_EECD_SK;
  62. IXGB_WRITE_REG(hw, EECD, *eecd_reg);
  63. udelay(50);
  64. return;
  65. }
  66. /******************************************************************************
  67. * Shift data bits out to the EEPROM.
  68. *
  69. * hw - Struct containing variables accessed by shared code
  70. * data - data to send to the EEPROM
  71. * count - number of bits to shift out
  72. *****************************************************************************/
  73. static void
  74. ixgb_shift_out_bits(struct ixgb_hw *hw,
  75. uint16_t data,
  76. uint16_t count)
  77. {
  78. uint32_t eecd_reg;
  79. uint32_t mask;
  80. /* We need to shift "count" bits out to the EEPROM. So, value in the
  81. * "data" parameter will be shifted out to the EEPROM one bit at a time.
  82. * In order to do this, "data" must be broken down into bits.
  83. */
  84. mask = 0x01 << (count - 1);
  85. eecd_reg = IXGB_READ_REG(hw, EECD);
  86. eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
  87. do {
  88. /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
  89. * and then raising and then lowering the clock (the SK bit controls
  90. * the clock input to the EEPROM). A "0" is shifted out to the EEPROM
  91. * by setting "DI" to "0" and then raising and then lowering the clock.
  92. */
  93. eecd_reg &= ~IXGB_EECD_DI;
  94. if(data & mask)
  95. eecd_reg |= IXGB_EECD_DI;
  96. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  97. udelay(50);
  98. ixgb_raise_clock(hw, &eecd_reg);
  99. ixgb_lower_clock(hw, &eecd_reg);
  100. mask = mask >> 1;
  101. } while(mask);
  102. /* We leave the "DI" bit set to "0" when we leave this routine. */
  103. eecd_reg &= ~IXGB_EECD_DI;
  104. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  105. return;
  106. }
  107. /******************************************************************************
  108. * Shift data bits in from the EEPROM
  109. *
  110. * hw - Struct containing variables accessed by shared code
  111. *****************************************************************************/
  112. static uint16_t
  113. ixgb_shift_in_bits(struct ixgb_hw *hw)
  114. {
  115. uint32_t eecd_reg;
  116. uint32_t i;
  117. uint16_t data;
  118. /* In order to read a register from the EEPROM, we need to shift 16 bits
  119. * in from the EEPROM. Bits are "shifted in" by raising the clock input to
  120. * the EEPROM (setting the SK bit), and then reading the value of the "DO"
  121. * bit. During this "shifting in" process the "DI" bit should always be
  122. * clear..
  123. */
  124. eecd_reg = IXGB_READ_REG(hw, EECD);
  125. eecd_reg &= ~(IXGB_EECD_DO | IXGB_EECD_DI);
  126. data = 0;
  127. for(i = 0; i < 16; i++) {
  128. data = data << 1;
  129. ixgb_raise_clock(hw, &eecd_reg);
  130. eecd_reg = IXGB_READ_REG(hw, EECD);
  131. eecd_reg &= ~(IXGB_EECD_DI);
  132. if(eecd_reg & IXGB_EECD_DO)
  133. data |= 1;
  134. ixgb_lower_clock(hw, &eecd_reg);
  135. }
  136. return data;
  137. }
  138. /******************************************************************************
  139. * Prepares EEPROM for access
  140. *
  141. * hw - Struct containing variables accessed by shared code
  142. *
  143. * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
  144. * function should be called before issuing a command to the EEPROM.
  145. *****************************************************************************/
  146. static void
  147. ixgb_setup_eeprom(struct ixgb_hw *hw)
  148. {
  149. uint32_t eecd_reg;
  150. eecd_reg = IXGB_READ_REG(hw, EECD);
  151. /* Clear SK and DI */
  152. eecd_reg &= ~(IXGB_EECD_SK | IXGB_EECD_DI);
  153. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  154. /* Set CS */
  155. eecd_reg |= IXGB_EECD_CS;
  156. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  157. return;
  158. }
  159. /******************************************************************************
  160. * Returns EEPROM to a "standby" state
  161. *
  162. * hw - Struct containing variables accessed by shared code
  163. *****************************************************************************/
  164. static void
  165. ixgb_standby_eeprom(struct ixgb_hw *hw)
  166. {
  167. uint32_t eecd_reg;
  168. eecd_reg = IXGB_READ_REG(hw, EECD);
  169. /* Deselct EEPROM */
  170. eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_SK);
  171. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  172. udelay(50);
  173. /* Clock high */
  174. eecd_reg |= IXGB_EECD_SK;
  175. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  176. udelay(50);
  177. /* Select EEPROM */
  178. eecd_reg |= IXGB_EECD_CS;
  179. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  180. udelay(50);
  181. /* Clock low */
  182. eecd_reg &= ~IXGB_EECD_SK;
  183. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  184. udelay(50);
  185. return;
  186. }
  187. /******************************************************************************
  188. * Raises then lowers the EEPROM's clock pin
  189. *
  190. * hw - Struct containing variables accessed by shared code
  191. *****************************************************************************/
  192. static void
  193. ixgb_clock_eeprom(struct ixgb_hw *hw)
  194. {
  195. uint32_t eecd_reg;
  196. eecd_reg = IXGB_READ_REG(hw, EECD);
  197. /* Rising edge of clock */
  198. eecd_reg |= IXGB_EECD_SK;
  199. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  200. udelay(50);
  201. /* Falling edge of clock */
  202. eecd_reg &= ~IXGB_EECD_SK;
  203. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  204. udelay(50);
  205. return;
  206. }
  207. /******************************************************************************
  208. * Terminates a command by lowering the EEPROM's chip select pin
  209. *
  210. * hw - Struct containing variables accessed by shared code
  211. *****************************************************************************/
  212. static void
  213. ixgb_cleanup_eeprom(struct ixgb_hw *hw)
  214. {
  215. uint32_t eecd_reg;
  216. eecd_reg = IXGB_READ_REG(hw, EECD);
  217. eecd_reg &= ~(IXGB_EECD_CS | IXGB_EECD_DI);
  218. IXGB_WRITE_REG(hw, EECD, eecd_reg);
  219. ixgb_clock_eeprom(hw);
  220. return;
  221. }
  222. /******************************************************************************
  223. * Waits for the EEPROM to finish the current command.
  224. *
  225. * hw - Struct containing variables accessed by shared code
  226. *
  227. * The command is done when the EEPROM's data out pin goes high.
  228. *
  229. * Returns:
  230. * TRUE: EEPROM data pin is high before timeout.
  231. * FALSE: Time expired.
  232. *****************************************************************************/
  233. static boolean_t
  234. ixgb_wait_eeprom_command(struct ixgb_hw *hw)
  235. {
  236. uint32_t eecd_reg;
  237. uint32_t i;
  238. /* Toggle the CS line. This in effect tells to EEPROM to actually execute
  239. * the command in question.
  240. */
  241. ixgb_standby_eeprom(hw);
  242. /* Now read DO repeatedly until is high (equal to '1'). The EEEPROM will
  243. * signal that the command has been completed by raising the DO signal.
  244. * If DO does not go high in 10 milliseconds, then error out.
  245. */
  246. for(i = 0; i < 200; i++) {
  247. eecd_reg = IXGB_READ_REG(hw, EECD);
  248. if(eecd_reg & IXGB_EECD_DO)
  249. return (TRUE);
  250. udelay(50);
  251. }
  252. ASSERT(0);
  253. return (FALSE);
  254. }
  255. /******************************************************************************
  256. * Verifies that the EEPROM has a valid checksum
  257. *
  258. * hw - Struct containing variables accessed by shared code
  259. *
  260. * Reads the first 64 16 bit words of the EEPROM and sums the values read.
  261. * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
  262. * valid.
  263. *
  264. * Returns:
  265. * TRUE: Checksum is valid
  266. * FALSE: Checksum is not valid.
  267. *****************************************************************************/
  268. boolean_t
  269. ixgb_validate_eeprom_checksum(struct ixgb_hw *hw)
  270. {
  271. uint16_t checksum = 0;
  272. uint16_t i;
  273. for(i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++)
  274. checksum += ixgb_read_eeprom(hw, i);
  275. if(checksum == (uint16_t) EEPROM_SUM)
  276. return (TRUE);
  277. else
  278. return (FALSE);
  279. }
  280. /******************************************************************************
  281. * Calculates the EEPROM checksum and writes it to the EEPROM
  282. *
  283. * hw - Struct containing variables accessed by shared code
  284. *
  285. * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
  286. * Writes the difference to word offset 63 of the EEPROM.
  287. *****************************************************************************/
  288. void
  289. ixgb_update_eeprom_checksum(struct ixgb_hw *hw)
  290. {
  291. uint16_t checksum = 0;
  292. uint16_t i;
  293. for(i = 0; i < EEPROM_CHECKSUM_REG; i++)
  294. checksum += ixgb_read_eeprom(hw, i);
  295. checksum = (uint16_t) EEPROM_SUM - checksum;
  296. ixgb_write_eeprom(hw, EEPROM_CHECKSUM_REG, checksum);
  297. return;
  298. }
  299. /******************************************************************************
  300. * Writes a 16 bit word to a given offset in the EEPROM.
  301. *
  302. * hw - Struct containing variables accessed by shared code
  303. * reg - offset within the EEPROM to be written to
  304. * data - 16 bit word to be writen to the EEPROM
  305. *
  306. * If ixgb_update_eeprom_checksum is not called after this function, the
  307. * EEPROM will most likely contain an invalid checksum.
  308. *
  309. *****************************************************************************/
  310. void
  311. ixgb_write_eeprom(struct ixgb_hw *hw, uint16_t offset, uint16_t data)
  312. {
  313. struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
  314. /* Prepare the EEPROM for writing */
  315. ixgb_setup_eeprom(hw);
  316. /* Send the 9-bit EWEN (write enable) command to the EEPROM (5-bit opcode
  317. * plus 4-bit dummy). This puts the EEPROM into write/erase mode.
  318. */
  319. ixgb_shift_out_bits(hw, EEPROM_EWEN_OPCODE, 5);
  320. ixgb_shift_out_bits(hw, 0, 4);
  321. /* Prepare the EEPROM */
  322. ixgb_standby_eeprom(hw);
  323. /* Send the Write command (3-bit opcode + 6-bit addr) */
  324. ixgb_shift_out_bits(hw, EEPROM_WRITE_OPCODE, 3);
  325. ixgb_shift_out_bits(hw, offset, 6);
  326. /* Send the data */
  327. ixgb_shift_out_bits(hw, data, 16);
  328. ixgb_wait_eeprom_command(hw);
  329. /* Recover from write */
  330. ixgb_standby_eeprom(hw);
  331. /* Send the 9-bit EWDS (write disable) command to the EEPROM (5-bit
  332. * opcode plus 4-bit dummy). This takes the EEPROM out of write/erase
  333. * mode.
  334. */
  335. ixgb_shift_out_bits(hw, EEPROM_EWDS_OPCODE, 5);
  336. ixgb_shift_out_bits(hw, 0, 4);
  337. /* Done with writing */
  338. ixgb_cleanup_eeprom(hw);
  339. /* clear the init_ctrl_reg_1 to signify that the cache is invalidated */
  340. ee_map->init_ctrl_reg_1 = le16_to_cpu(EEPROM_ICW1_SIGNATURE_CLEAR);
  341. return;
  342. }
  343. /******************************************************************************
  344. * Reads a 16 bit word from the EEPROM.
  345. *
  346. * hw - Struct containing variables accessed by shared code
  347. * offset - offset of 16 bit word in the EEPROM to read
  348. *
  349. * Returns:
  350. * The 16-bit value read from the eeprom
  351. *****************************************************************************/
  352. uint16_t
  353. ixgb_read_eeprom(struct ixgb_hw *hw,
  354. uint16_t offset)
  355. {
  356. uint16_t data;
  357. /* Prepare the EEPROM for reading */
  358. ixgb_setup_eeprom(hw);
  359. /* Send the READ command (opcode + addr) */
  360. ixgb_shift_out_bits(hw, EEPROM_READ_OPCODE, 3);
  361. /*
  362. * We have a 64 word EEPROM, there are 6 address bits
  363. */
  364. ixgb_shift_out_bits(hw, offset, 6);
  365. /* Read the data */
  366. data = ixgb_shift_in_bits(hw);
  367. /* End this read operation */
  368. ixgb_standby_eeprom(hw);
  369. return (data);
  370. }
  371. /******************************************************************************
  372. * Reads eeprom and stores data in shared structure.
  373. * Validates eeprom checksum and eeprom signature.
  374. *
  375. * hw - Struct containing variables accessed by shared code
  376. *
  377. * Returns:
  378. * TRUE: if eeprom read is successful
  379. * FALSE: otherwise.
  380. *****************************************************************************/
  381. boolean_t
  382. ixgb_get_eeprom_data(struct ixgb_hw *hw)
  383. {
  384. uint16_t i;
  385. uint16_t checksum = 0;
  386. struct ixgb_ee_map_type *ee_map;
  387. DEBUGFUNC("ixgb_get_eeprom_data");
  388. ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
  389. DEBUGOUT("ixgb_ee: Reading eeprom data\n");
  390. for(i = 0; i < IXGB_EEPROM_SIZE ; i++) {
  391. uint16_t ee_data;
  392. ee_data = ixgb_read_eeprom(hw, i);
  393. checksum += ee_data;
  394. hw->eeprom[i] = le16_to_cpu(ee_data);
  395. }
  396. if (checksum != (uint16_t) EEPROM_SUM) {
  397. DEBUGOUT("ixgb_ee: Checksum invalid.\n");
  398. /* clear the init_ctrl_reg_1 to signify that the cache is
  399. * invalidated */
  400. ee_map->init_ctrl_reg_1 = le16_to_cpu(EEPROM_ICW1_SIGNATURE_CLEAR);
  401. return (FALSE);
  402. }
  403. if ((ee_map->init_ctrl_reg_1 & le16_to_cpu(EEPROM_ICW1_SIGNATURE_MASK))
  404. != le16_to_cpu(EEPROM_ICW1_SIGNATURE_VALID)) {
  405. DEBUGOUT("ixgb_ee: Signature invalid.\n");
  406. return(FALSE);
  407. }
  408. return(TRUE);
  409. }
  410. /******************************************************************************
  411. * Local function to check if the eeprom signature is good
  412. * If the eeprom signature is good, calls ixgb)get_eeprom_data.
  413. *
  414. * hw - Struct containing variables accessed by shared code
  415. *
  416. * Returns:
  417. * TRUE: eeprom signature was good and the eeprom read was successful
  418. * FALSE: otherwise.
  419. ******************************************************************************/
  420. static boolean_t
  421. ixgb_check_and_get_eeprom_data (struct ixgb_hw* hw)
  422. {
  423. struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
  424. if ((ee_map->init_ctrl_reg_1 & le16_to_cpu(EEPROM_ICW1_SIGNATURE_MASK))
  425. == le16_to_cpu(EEPROM_ICW1_SIGNATURE_VALID)) {
  426. return (TRUE);
  427. } else {
  428. return ixgb_get_eeprom_data(hw);
  429. }
  430. }
  431. /******************************************************************************
  432. * return a word from the eeprom
  433. *
  434. * hw - Struct containing variables accessed by shared code
  435. * index - Offset of eeprom word
  436. *
  437. * Returns:
  438. * Word at indexed offset in eeprom, if valid, 0 otherwise.
  439. ******************************************************************************/
  440. uint16_t
  441. ixgb_get_eeprom_word(struct ixgb_hw *hw, uint16_t index)
  442. {
  443. if ((index < IXGB_EEPROM_SIZE) &&
  444. (ixgb_check_and_get_eeprom_data(hw) == TRUE)) {
  445. return(hw->eeprom[index]);
  446. }
  447. return(0);
  448. }
  449. /******************************************************************************
  450. * return the mac address from EEPROM
  451. *
  452. * hw - Struct containing variables accessed by shared code
  453. * mac_addr - Ethernet Address if EEPROM contents are valid, 0 otherwise
  454. *
  455. * Returns: None.
  456. ******************************************************************************/
  457. void
  458. ixgb_get_ee_mac_addr(struct ixgb_hw *hw,
  459. uint8_t *mac_addr)
  460. {
  461. int i;
  462. struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
  463. DEBUGFUNC("ixgb_get_ee_mac_addr");
  464. if (ixgb_check_and_get_eeprom_data(hw) == TRUE) {
  465. for (i = 0; i < IXGB_ETH_LENGTH_OF_ADDRESS; i++) {
  466. mac_addr[i] = ee_map->mac_addr[i];
  467. DEBUGOUT2("mac(%d) = %.2X\n", i, mac_addr[i]);
  468. }
  469. }
  470. }
  471. /******************************************************************************
  472. * return the Printed Board Assembly number from EEPROM
  473. *
  474. * hw - Struct containing variables accessed by shared code
  475. *
  476. * Returns:
  477. * PBA number if EEPROM contents are valid, 0 otherwise
  478. ******************************************************************************/
  479. uint32_t
  480. ixgb_get_ee_pba_number(struct ixgb_hw *hw)
  481. {
  482. if(ixgb_check_and_get_eeprom_data(hw) == TRUE)
  483. return (le16_to_cpu(hw->eeprom[EEPROM_PBA_1_2_REG])
  484. | (le16_to_cpu(hw->eeprom[EEPROM_PBA_3_4_REG])<<16));
  485. return(0);
  486. }
  487. /******************************************************************************
  488. * return the Device Id from EEPROM
  489. *
  490. * hw - Struct containing variables accessed by shared code
  491. *
  492. * Returns:
  493. * Device Id if EEPROM contents are valid, 0 otherwise
  494. ******************************************************************************/
  495. uint16_t
  496. ixgb_get_ee_device_id(struct ixgb_hw *hw)
  497. {
  498. struct ixgb_ee_map_type *ee_map = (struct ixgb_ee_map_type *)hw->eeprom;
  499. if(ixgb_check_and_get_eeprom_data(hw) == TRUE)
  500. return (le16_to_cpu(ee_map->device_id));
  501. return (0);
  502. }