ixgb_ee.c 16 KB

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