ixgb_ee.c 17 KB

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