e1000_i210.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007-2012 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. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. ******************************************************************************/
  20. /* e1000_i210
  21. * e1000_i211
  22. */
  23. #include <linux/types.h>
  24. #include <linux/if_ether.h>
  25. #include "e1000_hw.h"
  26. #include "e1000_i210.h"
  27. static s32 igb_get_hw_semaphore_i210(struct e1000_hw *hw);
  28. static void igb_put_hw_semaphore_i210(struct e1000_hw *hw);
  29. static s32 igb_write_nvm_srwr(struct e1000_hw *hw, u16 offset, u16 words,
  30. u16 *data);
  31. static s32 igb_pool_flash_update_done_i210(struct e1000_hw *hw);
  32. /**
  33. * igb_acquire_nvm_i210 - Request for access to EEPROM
  34. * @hw: pointer to the HW structure
  35. *
  36. * Acquire the necessary semaphores for exclusive access to the EEPROM.
  37. * Set the EEPROM access request bit and wait for EEPROM access grant bit.
  38. * Return successful if access grant bit set, else clear the request for
  39. * EEPROM access and return -E1000_ERR_NVM (-1).
  40. **/
  41. s32 igb_acquire_nvm_i210(struct e1000_hw *hw)
  42. {
  43. return igb_acquire_swfw_sync_i210(hw, E1000_SWFW_EEP_SM);
  44. }
  45. /**
  46. * igb_release_nvm_i210 - Release exclusive access to EEPROM
  47. * @hw: pointer to the HW structure
  48. *
  49. * Stop any current commands to the EEPROM and clear the EEPROM request bit,
  50. * then release the semaphores acquired.
  51. **/
  52. void igb_release_nvm_i210(struct e1000_hw *hw)
  53. {
  54. igb_release_swfw_sync_i210(hw, E1000_SWFW_EEP_SM);
  55. }
  56. /**
  57. * igb_acquire_swfw_sync_i210 - Acquire SW/FW semaphore
  58. * @hw: pointer to the HW structure
  59. * @mask: specifies which semaphore to acquire
  60. *
  61. * Acquire the SW/FW semaphore to access the PHY or NVM. The mask
  62. * will also specify which port we're acquiring the lock for.
  63. **/
  64. s32 igb_acquire_swfw_sync_i210(struct e1000_hw *hw, u16 mask)
  65. {
  66. u32 swfw_sync;
  67. u32 swmask = mask;
  68. u32 fwmask = mask << 16;
  69. s32 ret_val = E1000_SUCCESS;
  70. s32 i = 0, timeout = 200; /* FIXME: find real value to use here */
  71. while (i < timeout) {
  72. if (igb_get_hw_semaphore_i210(hw)) {
  73. ret_val = -E1000_ERR_SWFW_SYNC;
  74. goto out;
  75. }
  76. swfw_sync = rd32(E1000_SW_FW_SYNC);
  77. if (!(swfw_sync & fwmask))
  78. break;
  79. /*
  80. * Firmware currently using resource (fwmask)
  81. */
  82. igb_put_hw_semaphore_i210(hw);
  83. mdelay(5);
  84. i++;
  85. }
  86. if (i == timeout) {
  87. hw_dbg("Driver can't access resource, SW_FW_SYNC timeout.\n");
  88. ret_val = -E1000_ERR_SWFW_SYNC;
  89. goto out;
  90. }
  91. swfw_sync |= swmask;
  92. wr32(E1000_SW_FW_SYNC, swfw_sync);
  93. igb_put_hw_semaphore_i210(hw);
  94. out:
  95. return ret_val;
  96. }
  97. /**
  98. * igb_release_swfw_sync_i210 - Release SW/FW semaphore
  99. * @hw: pointer to the HW structure
  100. * @mask: specifies which semaphore to acquire
  101. *
  102. * Release the SW/FW semaphore used to access the PHY or NVM. The mask
  103. * will also specify which port we're releasing the lock for.
  104. **/
  105. void igb_release_swfw_sync_i210(struct e1000_hw *hw, u16 mask)
  106. {
  107. u32 swfw_sync;
  108. while (igb_get_hw_semaphore_i210(hw) != E1000_SUCCESS)
  109. ; /* Empty */
  110. swfw_sync = rd32(E1000_SW_FW_SYNC);
  111. swfw_sync &= ~mask;
  112. wr32(E1000_SW_FW_SYNC, swfw_sync);
  113. igb_put_hw_semaphore_i210(hw);
  114. }
  115. /**
  116. * igb_get_hw_semaphore_i210 - Acquire hardware semaphore
  117. * @hw: pointer to the HW structure
  118. *
  119. * Acquire the HW semaphore to access the PHY or NVM
  120. **/
  121. static s32 igb_get_hw_semaphore_i210(struct e1000_hw *hw)
  122. {
  123. u32 swsm;
  124. s32 ret_val = E1000_SUCCESS;
  125. s32 timeout = hw->nvm.word_size + 1;
  126. s32 i = 0;
  127. /* Get the FW semaphore. */
  128. for (i = 0; i < timeout; i++) {
  129. swsm = rd32(E1000_SWSM);
  130. wr32(E1000_SWSM, swsm | E1000_SWSM_SWESMBI);
  131. /* Semaphore acquired if bit latched */
  132. if (rd32(E1000_SWSM) & E1000_SWSM_SWESMBI)
  133. break;
  134. udelay(50);
  135. }
  136. if (i == timeout) {
  137. /* Release semaphores */
  138. igb_put_hw_semaphore(hw);
  139. hw_dbg("Driver can't access the NVM\n");
  140. ret_val = -E1000_ERR_NVM;
  141. goto out;
  142. }
  143. out:
  144. return ret_val;
  145. }
  146. /**
  147. * igb_put_hw_semaphore_i210 - Release hardware semaphore
  148. * @hw: pointer to the HW structure
  149. *
  150. * Release hardware semaphore used to access the PHY or NVM
  151. **/
  152. static void igb_put_hw_semaphore_i210(struct e1000_hw *hw)
  153. {
  154. u32 swsm;
  155. swsm = rd32(E1000_SWSM);
  156. swsm &= ~E1000_SWSM_SWESMBI;
  157. wr32(E1000_SWSM, swsm);
  158. }
  159. /**
  160. * igb_read_nvm_srrd_i210 - Reads Shadow Ram using EERD register
  161. * @hw: pointer to the HW structure
  162. * @offset: offset of word in the Shadow Ram to read
  163. * @words: number of words to read
  164. * @data: word read from the Shadow Ram
  165. *
  166. * Reads a 16 bit word from the Shadow Ram using the EERD register.
  167. * Uses necessary synchronization semaphores.
  168. **/
  169. s32 igb_read_nvm_srrd_i210(struct e1000_hw *hw, u16 offset, u16 words,
  170. u16 *data)
  171. {
  172. s32 status = E1000_SUCCESS;
  173. u16 i, count;
  174. /* We cannot hold synchronization semaphores for too long,
  175. * because of forceful takeover procedure. However it is more efficient
  176. * to read in bursts than synchronizing access for each word. */
  177. for (i = 0; i < words; i += E1000_EERD_EEWR_MAX_COUNT) {
  178. count = (words - i) / E1000_EERD_EEWR_MAX_COUNT > 0 ?
  179. E1000_EERD_EEWR_MAX_COUNT : (words - i);
  180. if (hw->nvm.ops.acquire(hw) == E1000_SUCCESS) {
  181. status = igb_read_nvm_eerd(hw, offset, count,
  182. data + i);
  183. hw->nvm.ops.release(hw);
  184. } else {
  185. status = E1000_ERR_SWFW_SYNC;
  186. }
  187. if (status != E1000_SUCCESS)
  188. break;
  189. }
  190. return status;
  191. }
  192. /**
  193. * igb_write_nvm_srwr_i210 - Write to Shadow RAM using EEWR
  194. * @hw: pointer to the HW structure
  195. * @offset: offset within the Shadow RAM to be written to
  196. * @words: number of words to write
  197. * @data: 16 bit word(s) to be written to the Shadow RAM
  198. *
  199. * Writes data to Shadow RAM at offset using EEWR register.
  200. *
  201. * If e1000_update_nvm_checksum is not called after this function , the
  202. * data will not be committed to FLASH and also Shadow RAM will most likely
  203. * contain an invalid checksum.
  204. *
  205. * If error code is returned, data and Shadow RAM may be inconsistent - buffer
  206. * partially written.
  207. **/
  208. s32 igb_write_nvm_srwr_i210(struct e1000_hw *hw, u16 offset, u16 words,
  209. u16 *data)
  210. {
  211. s32 status = E1000_SUCCESS;
  212. u16 i, count;
  213. /* We cannot hold synchronization semaphores for too long,
  214. * because of forceful takeover procedure. However it is more efficient
  215. * to write in bursts than synchronizing access for each word. */
  216. for (i = 0; i < words; i += E1000_EERD_EEWR_MAX_COUNT) {
  217. count = (words - i) / E1000_EERD_EEWR_MAX_COUNT > 0 ?
  218. E1000_EERD_EEWR_MAX_COUNT : (words - i);
  219. if (hw->nvm.ops.acquire(hw) == E1000_SUCCESS) {
  220. status = igb_write_nvm_srwr(hw, offset, count,
  221. data + i);
  222. hw->nvm.ops.release(hw);
  223. } else {
  224. status = E1000_ERR_SWFW_SYNC;
  225. }
  226. if (status != E1000_SUCCESS)
  227. break;
  228. }
  229. return status;
  230. }
  231. /**
  232. * igb_write_nvm_srwr - Write to Shadow Ram using EEWR
  233. * @hw: pointer to the HW structure
  234. * @offset: offset within the Shadow Ram to be written to
  235. * @words: number of words to write
  236. * @data: 16 bit word(s) to be written to the Shadow Ram
  237. *
  238. * Writes data to Shadow Ram at offset using EEWR register.
  239. *
  240. * If igb_update_nvm_checksum is not called after this function , the
  241. * Shadow Ram will most likely contain an invalid checksum.
  242. **/
  243. static s32 igb_write_nvm_srwr(struct e1000_hw *hw, u16 offset, u16 words,
  244. u16 *data)
  245. {
  246. struct e1000_nvm_info *nvm = &hw->nvm;
  247. u32 i, k, eewr = 0;
  248. u32 attempts = 100000;
  249. s32 ret_val = E1000_SUCCESS;
  250. /*
  251. * A check for invalid values: offset too large, too many words,
  252. * too many words for the offset, and not enough words.
  253. */
  254. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  255. (words == 0)) {
  256. hw_dbg("nvm parameter(s) out of bounds\n");
  257. ret_val = -E1000_ERR_NVM;
  258. goto out;
  259. }
  260. for (i = 0; i < words; i++) {
  261. eewr = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) |
  262. (data[i] << E1000_NVM_RW_REG_DATA) |
  263. E1000_NVM_RW_REG_START;
  264. wr32(E1000_SRWR, eewr);
  265. for (k = 0; k < attempts; k++) {
  266. if (E1000_NVM_RW_REG_DONE &
  267. rd32(E1000_SRWR)) {
  268. ret_val = E1000_SUCCESS;
  269. break;
  270. }
  271. udelay(5);
  272. }
  273. if (ret_val != E1000_SUCCESS) {
  274. hw_dbg("Shadow RAM write EEWR timed out\n");
  275. break;
  276. }
  277. }
  278. out:
  279. return ret_val;
  280. }
  281. /**
  282. * igb_read_nvm_i211 - Read NVM wrapper function for I211
  283. * @hw: pointer to the HW structure
  284. * @address: the word address (aka eeprom offset) to read
  285. * @data: pointer to the data read
  286. *
  287. * Wrapper function to return data formerly found in the NVM.
  288. **/
  289. s32 igb_read_nvm_i211(struct e1000_hw *hw, u16 offset, u16 words,
  290. u16 *data)
  291. {
  292. s32 ret_val = E1000_SUCCESS;
  293. /* Only the MAC addr is required to be present in the iNVM */
  294. switch (offset) {
  295. case NVM_MAC_ADDR:
  296. ret_val = igb_read_invm_i211(hw, offset, &data[0]);
  297. ret_val |= igb_read_invm_i211(hw, offset+1, &data[1]);
  298. ret_val |= igb_read_invm_i211(hw, offset+2, &data[2]);
  299. if (ret_val != E1000_SUCCESS)
  300. hw_dbg("MAC Addr not found in iNVM\n");
  301. break;
  302. case NVM_ID_LED_SETTINGS:
  303. case NVM_INIT_CTRL_2:
  304. case NVM_INIT_CTRL_4:
  305. case NVM_LED_1_CFG:
  306. case NVM_LED_0_2_CFG:
  307. igb_read_invm_i211(hw, offset, data);
  308. break;
  309. case NVM_COMPAT:
  310. *data = ID_LED_DEFAULT_I210;
  311. break;
  312. case NVM_SUB_DEV_ID:
  313. *data = hw->subsystem_device_id;
  314. break;
  315. case NVM_SUB_VEN_ID:
  316. *data = hw->subsystem_vendor_id;
  317. break;
  318. case NVM_DEV_ID:
  319. *data = hw->device_id;
  320. break;
  321. case NVM_VEN_ID:
  322. *data = hw->vendor_id;
  323. break;
  324. default:
  325. hw_dbg("NVM word 0x%02x is not mapped.\n", offset);
  326. *data = NVM_RESERVED_WORD;
  327. break;
  328. }
  329. return ret_val;
  330. }
  331. /**
  332. * igb_read_invm_i211 - Reads OTP
  333. * @hw: pointer to the HW structure
  334. * @address: the word address (aka eeprom offset) to read
  335. * @data: pointer to the data read
  336. *
  337. * Reads 16-bit words from the OTP. Return error when the word is not
  338. * stored in OTP.
  339. **/
  340. s32 igb_read_invm_i211(struct e1000_hw *hw, u16 address, u16 *data)
  341. {
  342. s32 status = -E1000_ERR_INVM_VALUE_NOT_FOUND;
  343. u32 invm_dword;
  344. u16 i;
  345. u8 record_type, word_address;
  346. for (i = 0; i < E1000_INVM_SIZE; i++) {
  347. invm_dword = rd32(E1000_INVM_DATA_REG(i));
  348. /* Get record type */
  349. record_type = INVM_DWORD_TO_RECORD_TYPE(invm_dword);
  350. if (record_type == E1000_INVM_UNINITIALIZED_STRUCTURE)
  351. break;
  352. if (record_type == E1000_INVM_CSR_AUTOLOAD_STRUCTURE)
  353. i += E1000_INVM_CSR_AUTOLOAD_DATA_SIZE_IN_DWORDS;
  354. if (record_type == E1000_INVM_RSA_KEY_SHA256_STRUCTURE)
  355. i += E1000_INVM_RSA_KEY_SHA256_DATA_SIZE_IN_DWORDS;
  356. if (record_type == E1000_INVM_WORD_AUTOLOAD_STRUCTURE) {
  357. word_address = INVM_DWORD_TO_WORD_ADDRESS(invm_dword);
  358. if (word_address == (u8)address) {
  359. *data = INVM_DWORD_TO_WORD_DATA(invm_dword);
  360. hw_dbg("Read INVM Word 0x%02x = %x",
  361. address, *data);
  362. status = E1000_SUCCESS;
  363. break;
  364. }
  365. }
  366. }
  367. if (status != E1000_SUCCESS)
  368. hw_dbg("Requested word 0x%02x not found in OTP\n", address);
  369. return status;
  370. }
  371. /**
  372. * igb_validate_nvm_checksum_i210 - Validate EEPROM checksum
  373. * @hw: pointer to the HW structure
  374. *
  375. * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
  376. * and then verifies that the sum of the EEPROM is equal to 0xBABA.
  377. **/
  378. s32 igb_validate_nvm_checksum_i210(struct e1000_hw *hw)
  379. {
  380. s32 status = E1000_SUCCESS;
  381. s32 (*read_op_ptr)(struct e1000_hw *, u16, u16, u16 *);
  382. if (hw->nvm.ops.acquire(hw) == E1000_SUCCESS) {
  383. /*
  384. * Replace the read function with semaphore grabbing with
  385. * the one that skips this for a while.
  386. * We have semaphore taken already here.
  387. */
  388. read_op_ptr = hw->nvm.ops.read;
  389. hw->nvm.ops.read = igb_read_nvm_eerd;
  390. status = igb_validate_nvm_checksum(hw);
  391. /* Revert original read operation. */
  392. hw->nvm.ops.read = read_op_ptr;
  393. hw->nvm.ops.release(hw);
  394. } else {
  395. status = E1000_ERR_SWFW_SYNC;
  396. }
  397. return status;
  398. }
  399. /**
  400. * igb_update_nvm_checksum_i210 - Update EEPROM checksum
  401. * @hw: pointer to the HW structure
  402. *
  403. * Updates the EEPROM checksum by reading/adding each word of the EEPROM
  404. * up to the checksum. Then calculates the EEPROM checksum and writes the
  405. * value to the EEPROM. Next commit EEPROM data onto the Flash.
  406. **/
  407. s32 igb_update_nvm_checksum_i210(struct e1000_hw *hw)
  408. {
  409. s32 ret_val = E1000_SUCCESS;
  410. u16 checksum = 0;
  411. u16 i, nvm_data;
  412. /*
  413. * Read the first word from the EEPROM. If this times out or fails, do
  414. * not continue or we could be in for a very long wait while every
  415. * EEPROM read fails
  416. */
  417. ret_val = igb_read_nvm_eerd(hw, 0, 1, &nvm_data);
  418. if (ret_val != E1000_SUCCESS) {
  419. hw_dbg("EEPROM read failed\n");
  420. goto out;
  421. }
  422. if (hw->nvm.ops.acquire(hw) == E1000_SUCCESS) {
  423. /*
  424. * Do not use hw->nvm.ops.write, hw->nvm.ops.read
  425. * because we do not want to take the synchronization
  426. * semaphores twice here.
  427. */
  428. for (i = 0; i < NVM_CHECKSUM_REG; i++) {
  429. ret_val = igb_read_nvm_eerd(hw, i, 1, &nvm_data);
  430. if (ret_val) {
  431. hw->nvm.ops.release(hw);
  432. hw_dbg("NVM Read Error while updating checksum.\n");
  433. goto out;
  434. }
  435. checksum += nvm_data;
  436. }
  437. checksum = (u16) NVM_SUM - checksum;
  438. ret_val = igb_write_nvm_srwr(hw, NVM_CHECKSUM_REG, 1,
  439. &checksum);
  440. if (ret_val != E1000_SUCCESS) {
  441. hw->nvm.ops.release(hw);
  442. hw_dbg("NVM Write Error while updating checksum.\n");
  443. goto out;
  444. }
  445. hw->nvm.ops.release(hw);
  446. ret_val = igb_update_flash_i210(hw);
  447. } else {
  448. ret_val = -E1000_ERR_SWFW_SYNC;
  449. }
  450. out:
  451. return ret_val;
  452. }
  453. /**
  454. * igb_update_flash_i210 - Commit EEPROM to the flash
  455. * @hw: pointer to the HW structure
  456. *
  457. **/
  458. s32 igb_update_flash_i210(struct e1000_hw *hw)
  459. {
  460. s32 ret_val = E1000_SUCCESS;
  461. u32 flup;
  462. ret_val = igb_pool_flash_update_done_i210(hw);
  463. if (ret_val == -E1000_ERR_NVM) {
  464. hw_dbg("Flash update time out\n");
  465. goto out;
  466. }
  467. flup = rd32(E1000_EECD) | E1000_EECD_FLUPD_I210;
  468. wr32(E1000_EECD, flup);
  469. ret_val = igb_pool_flash_update_done_i210(hw);
  470. if (ret_val == E1000_SUCCESS)
  471. hw_dbg("Flash update complete\n");
  472. else
  473. hw_dbg("Flash update time out\n");
  474. out:
  475. return ret_val;
  476. }
  477. /**
  478. * igb_pool_flash_update_done_i210 - Pool FLUDONE status.
  479. * @hw: pointer to the HW structure
  480. *
  481. **/
  482. s32 igb_pool_flash_update_done_i210(struct e1000_hw *hw)
  483. {
  484. s32 ret_val = -E1000_ERR_NVM;
  485. u32 i, reg;
  486. for (i = 0; i < E1000_FLUDONE_ATTEMPTS; i++) {
  487. reg = rd32(E1000_EECD);
  488. if (reg & E1000_EECD_FLUDONE_I210) {
  489. ret_val = E1000_SUCCESS;
  490. break;
  491. }
  492. udelay(5);
  493. }
  494. return ret_val;
  495. }
  496. /**
  497. * igb_valid_led_default_i210 - Verify a valid default LED config
  498. * @hw: pointer to the HW structure
  499. * @data: pointer to the NVM (EEPROM)
  500. *
  501. * Read the EEPROM for the current default LED configuration. If the
  502. * LED configuration is not valid, set to a valid LED configuration.
  503. **/
  504. s32 igb_valid_led_default_i210(struct e1000_hw *hw, u16 *data)
  505. {
  506. s32 ret_val;
  507. ret_val = hw->nvm.ops.read(hw, NVM_ID_LED_SETTINGS, 1, data);
  508. if (ret_val) {
  509. hw_dbg("NVM Read Error\n");
  510. goto out;
  511. }
  512. if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF) {
  513. switch (hw->phy.media_type) {
  514. case e1000_media_type_internal_serdes:
  515. *data = ID_LED_DEFAULT_I210_SERDES;
  516. break;
  517. case e1000_media_type_copper:
  518. default:
  519. *data = ID_LED_DEFAULT_I210;
  520. break;
  521. }
  522. }
  523. out:
  524. return ret_val;
  525. }