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