1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Intel Corporation */
4 #include <linux/delay.h>
9 * igc_get_hw_semaphore_i225 - Acquire hardware semaphore
10 * @hw: pointer to the HW structure
12 * Acquire the necessary semaphores for exclusive access to the EEPROM.
13 * Set the EEPROM access request bit and wait for EEPROM access grant bit.
14 * Return successful if access grant bit set, else clear the request for
15 * EEPROM access and return -IGC_ERR_NVM (-1).
17 static s32 igc_acquire_nvm_i225(struct igc_hw *hw)
19 return igc_acquire_swfw_sync_i225(hw, IGC_SWFW_EEP_SM);
23 * igc_release_nvm_i225 - Release exclusive access to EEPROM
24 * @hw: pointer to the HW structure
26 * Stop any current commands to the EEPROM and clear the EEPROM request bit,
27 * then release the semaphores acquired.
29 static void igc_release_nvm_i225(struct igc_hw *hw)
31 igc_release_swfw_sync_i225(hw, IGC_SWFW_EEP_SM);
35 * igc_get_hw_semaphore_i225 - Acquire hardware semaphore
36 * @hw: pointer to the HW structure
38 * Acquire the HW semaphore to access the PHY or NVM
40 static s32 igc_get_hw_semaphore_i225(struct igc_hw *hw)
42 s32 timeout = hw->nvm.word_size + 1;
46 /* Get the SW semaphore */
48 swsm = rd32(IGC_SWSM);
49 if (!(swsm & IGC_SWSM_SMBI))
52 usleep_range(500, 600);
57 /* In rare circumstances, the SW semaphore may already be held
58 * unintentionally. Clear the semaphore once before giving up.
60 if (hw->dev_spec._base.clear_semaphore_once) {
61 hw->dev_spec._base.clear_semaphore_once = false;
62 igc_put_hw_semaphore(hw);
63 for (i = 0; i < timeout; i++) {
64 swsm = rd32(IGC_SWSM);
65 if (!(swsm & IGC_SWSM_SMBI))
68 usleep_range(500, 600);
72 /* If we do not have the semaphore here, we have to give up. */
74 hw_dbg("Driver can't access device - SMBI bit is set.\n");
79 /* Get the FW semaphore. */
80 for (i = 0; i < timeout; i++) {
81 swsm = rd32(IGC_SWSM);
82 wr32(IGC_SWSM, swsm | IGC_SWSM_SWESMBI);
84 /* Semaphore acquired if bit latched */
85 if (rd32(IGC_SWSM) & IGC_SWSM_SWESMBI)
88 usleep_range(500, 600);
92 /* Release semaphores */
93 igc_put_hw_semaphore(hw);
94 hw_dbg("Driver can't access the NVM\n");
102 * igc_acquire_swfw_sync_i225 - Acquire SW/FW semaphore
103 * @hw: pointer to the HW structure
104 * @mask: specifies which semaphore to acquire
106 * Acquire the SW/FW semaphore to access the PHY or NVM. The mask
107 * will also specify which port we're acquiring the lock for.
109 s32 igc_acquire_swfw_sync_i225(struct igc_hw *hw, u16 mask)
111 s32 i = 0, timeout = 200;
112 u32 fwmask = mask << 16;
117 while (i < timeout) {
118 if (igc_get_hw_semaphore_i225(hw)) {
119 ret_val = -IGC_ERR_SWFW_SYNC;
123 swfw_sync = rd32(IGC_SW_FW_SYNC);
124 if (!(swfw_sync & (fwmask | swmask)))
127 /* Firmware currently using resource (fwmask) */
128 igc_put_hw_semaphore(hw);
134 hw_dbg("Driver can't access resource, SW_FW_SYNC timeout.\n");
135 ret_val = -IGC_ERR_SWFW_SYNC;
140 wr32(IGC_SW_FW_SYNC, swfw_sync);
142 igc_put_hw_semaphore(hw);
148 * igc_release_swfw_sync_i225 - Release SW/FW semaphore
149 * @hw: pointer to the HW structure
150 * @mask: specifies which semaphore to acquire
152 * Release the SW/FW semaphore used to access the PHY or NVM. The mask
153 * will also specify which port we're releasing the lock for.
155 void igc_release_swfw_sync_i225(struct igc_hw *hw, u16 mask)
159 while (igc_get_hw_semaphore_i225(hw))
162 swfw_sync = rd32(IGC_SW_FW_SYNC);
164 wr32(IGC_SW_FW_SYNC, swfw_sync);
166 igc_put_hw_semaphore(hw);
170 * igc_read_nvm_srrd_i225 - Reads Shadow Ram using EERD register
171 * @hw: pointer to the HW structure
172 * @offset: offset of word in the Shadow Ram to read
173 * @words: number of words to read
174 * @data: word read from the Shadow Ram
176 * Reads a 16 bit word from the Shadow Ram using the EERD register.
177 * Uses necessary synchronization semaphores.
179 static s32 igc_read_nvm_srrd_i225(struct igc_hw *hw, u16 offset, u16 words,
185 /* We cannot hold synchronization semaphores for too long,
186 * because of forceful takeover procedure. However it is more efficient
187 * to read in bursts than synchronizing access for each word.
189 for (i = 0; i < words; i += IGC_EERD_EEWR_MAX_COUNT) {
190 count = (words - i) / IGC_EERD_EEWR_MAX_COUNT > 0 ?
191 IGC_EERD_EEWR_MAX_COUNT : (words - i);
193 status = hw->nvm.ops.acquire(hw);
197 status = igc_read_nvm_eerd(hw, offset, count, data + i);
198 hw->nvm.ops.release(hw);
207 * igc_write_nvm_srwr - Write to Shadow Ram using EEWR
208 * @hw: pointer to the HW structure
209 * @offset: offset within the Shadow Ram to be written to
210 * @words: number of words to write
211 * @data: 16 bit word(s) to be written to the Shadow Ram
213 * Writes data to Shadow Ram at offset using EEWR register.
215 * If igc_update_nvm_checksum is not called after this function , the
216 * Shadow Ram will most likely contain an invalid checksum.
218 static s32 igc_write_nvm_srwr(struct igc_hw *hw, u16 offset, u16 words,
221 struct igc_nvm_info *nvm = &hw->nvm;
222 u32 attempts = 100000;
226 /* A check for invalid values: offset too large, too many words,
227 * too many words for the offset, and not enough words.
229 if (offset >= nvm->word_size || (words > (nvm->word_size - offset)) ||
231 hw_dbg("nvm parameter(s) out of bounds\n");
232 ret_val = -IGC_ERR_NVM;
236 for (i = 0; i < words; i++) {
237 eewr = ((offset + i) << IGC_NVM_RW_ADDR_SHIFT) |
238 (data[i] << IGC_NVM_RW_REG_DATA) |
239 IGC_NVM_RW_REG_START;
241 wr32(IGC_SRWR, eewr);
243 for (k = 0; k < attempts; k++) {
244 if (IGC_NVM_RW_REG_DONE &
253 hw_dbg("Shadow RAM write EEWR timed out\n");
263 * igc_write_nvm_srwr_i225 - Write to Shadow RAM using EEWR
264 * @hw: pointer to the HW structure
265 * @offset: offset within the Shadow RAM to be written to
266 * @words: number of words to write
267 * @data: 16 bit word(s) to be written to the Shadow RAM
269 * Writes data to Shadow RAM at offset using EEWR register.
271 * If igc_update_nvm_checksum is not called after this function , the
272 * data will not be committed to FLASH and also Shadow RAM will most likely
273 * contain an invalid checksum.
275 * If error code is returned, data and Shadow RAM may be inconsistent - buffer
278 static s32 igc_write_nvm_srwr_i225(struct igc_hw *hw, u16 offset, u16 words,
284 /* We cannot hold synchronization semaphores for too long,
285 * because of forceful takeover procedure. However it is more efficient
286 * to write in bursts than synchronizing access for each word.
288 for (i = 0; i < words; i += IGC_EERD_EEWR_MAX_COUNT) {
289 count = (words - i) / IGC_EERD_EEWR_MAX_COUNT > 0 ?
290 IGC_EERD_EEWR_MAX_COUNT : (words - i);
292 status = hw->nvm.ops.acquire(hw);
296 status = igc_write_nvm_srwr(hw, offset, count, data + i);
297 hw->nvm.ops.release(hw);
306 * igc_validate_nvm_checksum_i225 - Validate EEPROM checksum
307 * @hw: pointer to the HW structure
309 * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
310 * and then verifies that the sum of the EEPROM is equal to 0xBABA.
312 static s32 igc_validate_nvm_checksum_i225(struct igc_hw *hw)
314 s32 (*read_op_ptr)(struct igc_hw *hw, u16 offset, u16 count,
318 status = hw->nvm.ops.acquire(hw);
322 /* Replace the read function with semaphore grabbing with
323 * the one that skips this for a while.
324 * We have semaphore taken already here.
326 read_op_ptr = hw->nvm.ops.read;
327 hw->nvm.ops.read = igc_read_nvm_eerd;
329 status = igc_validate_nvm_checksum(hw);
331 /* Revert original read operation. */
332 hw->nvm.ops.read = read_op_ptr;
334 hw->nvm.ops.release(hw);
341 * igc_pool_flash_update_done_i225 - Pool FLUDONE status
342 * @hw: pointer to the HW structure
344 static s32 igc_pool_flash_update_done_i225(struct igc_hw *hw)
346 s32 ret_val = -IGC_ERR_NVM;
349 for (i = 0; i < IGC_FLUDONE_ATTEMPTS; i++) {
350 reg = rd32(IGC_EECD);
351 if (reg & IGC_EECD_FLUDONE_I225) {
362 * igc_update_flash_i225 - Commit EEPROM to the flash
363 * @hw: pointer to the HW structure
365 static s32 igc_update_flash_i225(struct igc_hw *hw)
370 ret_val = igc_pool_flash_update_done_i225(hw);
371 if (ret_val == -IGC_ERR_NVM) {
372 hw_dbg("Flash update time out\n");
376 flup = rd32(IGC_EECD) | IGC_EECD_FLUPD_I225;
377 wr32(IGC_EECD, flup);
379 ret_val = igc_pool_flash_update_done_i225(hw);
381 hw_dbg("Flash update time out\n");
383 hw_dbg("Flash update complete\n");
390 * igc_update_nvm_checksum_i225 - Update EEPROM checksum
391 * @hw: pointer to the HW structure
393 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
394 * up to the checksum. Then calculates the EEPROM checksum and writes the
395 * value to the EEPROM. Next commit EEPROM data onto the Flash.
397 static s32 igc_update_nvm_checksum_i225(struct igc_hw *hw)
403 /* Read the first word from the EEPROM. If this times out or fails, do
404 * not continue or we could be in for a very long wait while every
407 ret_val = igc_read_nvm_eerd(hw, 0, 1, &nvm_data);
409 hw_dbg("EEPROM read failed\n");
413 ret_val = hw->nvm.ops.acquire(hw);
417 /* Do not use hw->nvm.ops.write, hw->nvm.ops.read
418 * because we do not want to take the synchronization
419 * semaphores twice here.
422 for (i = 0; i < NVM_CHECKSUM_REG; i++) {
423 ret_val = igc_read_nvm_eerd(hw, i, 1, &nvm_data);
425 hw->nvm.ops.release(hw);
426 hw_dbg("NVM Read Error while updating checksum.\n");
429 checksum += nvm_data;
431 checksum = (u16)NVM_SUM - checksum;
432 ret_val = igc_write_nvm_srwr(hw, NVM_CHECKSUM_REG, 1,
435 hw->nvm.ops.release(hw);
436 hw_dbg("NVM Write Error while updating checksum.\n");
440 hw->nvm.ops.release(hw);
442 ret_val = igc_update_flash_i225(hw);
449 * igc_get_flash_presence_i225 - Check if flash device is detected
450 * @hw: pointer to the HW structure
452 bool igc_get_flash_presence_i225(struct igc_hw *hw)
454 bool ret_val = false;
457 eec = rd32(IGC_EECD);
458 if (eec & IGC_EECD_FLASH_DETECTED_I225)
465 * igc_init_nvm_params_i225 - Init NVM func ptrs.
466 * @hw: pointer to the HW structure
468 s32 igc_init_nvm_params_i225(struct igc_hw *hw)
470 struct igc_nvm_info *nvm = &hw->nvm;
472 nvm->ops.acquire = igc_acquire_nvm_i225;
473 nvm->ops.release = igc_release_nvm_i225;
475 /* NVM Function Pointers */
476 if (igc_get_flash_presence_i225(hw)) {
477 hw->nvm.type = igc_nvm_flash_hw;
478 nvm->ops.read = igc_read_nvm_srrd_i225;
479 nvm->ops.write = igc_write_nvm_srwr_i225;
480 nvm->ops.validate = igc_validate_nvm_checksum_i225;
481 nvm->ops.update = igc_update_nvm_checksum_i225;
483 hw->nvm.type = igc_nvm_invm;
484 nvm->ops.read = igc_read_nvm_eerd;
485 nvm->ops.write = NULL;
486 nvm->ops.validate = NULL;
487 nvm->ops.update = NULL;
493 * igc_set_eee_i225 - Enable/disable EEE support
494 * @hw: pointer to the HW structure
495 * @adv2p5G: boolean flag enabling 2.5G EEE advertisement
496 * @adv1G: boolean flag enabling 1G EEE advertisement
497 * @adv100M: boolean flag enabling 100M EEE advertisement
499 * Enable/disable EEE based on setting in dev_spec structure.
501 s32 igc_set_eee_i225(struct igc_hw *hw, bool adv2p5G, bool adv1G,
506 ipcnfg = rd32(IGC_IPCNFG);
507 eeer = rd32(IGC_EEER);
509 /* enable or disable per user setting */
510 if (hw->dev_spec._base.eee_enable) {
511 u32 eee_su = rd32(IGC_EEE_SU);
514 ipcnfg |= IGC_IPCNFG_EEE_100M_AN;
516 ipcnfg &= ~IGC_IPCNFG_EEE_100M_AN;
519 ipcnfg |= IGC_IPCNFG_EEE_1G_AN;
521 ipcnfg &= ~IGC_IPCNFG_EEE_1G_AN;
524 ipcnfg |= IGC_IPCNFG_EEE_2_5G_AN;
526 ipcnfg &= ~IGC_IPCNFG_EEE_2_5G_AN;
528 eeer |= (IGC_EEER_TX_LPI_EN | IGC_EEER_RX_LPI_EN |
531 /* This bit should not be set in normal operation. */
532 if (eee_su & IGC_EEE_SU_LPI_CLK_STP)
533 hw_dbg("LPI Clock Stop Bit should not be set!\n");
535 ipcnfg &= ~(IGC_IPCNFG_EEE_2_5G_AN | IGC_IPCNFG_EEE_1G_AN |
536 IGC_IPCNFG_EEE_100M_AN);
537 eeer &= ~(IGC_EEER_TX_LPI_EN | IGC_EEER_RX_LPI_EN |
540 wr32(IGC_IPCNFG, ipcnfg);
541 wr32(IGC_EEER, eeer);
548 /* igc_set_ltr_i225 - Set Latency Tolerance Reporting thresholds
549 * @hw: pointer to the HW structure
550 * @link: bool indicating link status
552 * Set the LTR thresholds based on the link speed (Mbps), EEE, and DMAC
553 * settings, otherwise specify that there is no LTR requirement.
555 s32 igc_set_ltr_i225(struct igc_hw *hw, bool link)
557 u32 tw_system, ltrc, ltrv, ltr_min, ltr_max, scale_min, scale_max;
561 /* If we do not have link, LTR thresholds are zero. */
563 hw->mac.ops.get_speed_and_duplex(hw, &speed, &duplex);
565 /* Check if using copper interface with EEE enabled or if the
566 * link speed is 10 Mbps.
568 if (hw->dev_spec._base.eee_enable &&
570 /* EEE enabled, so send LTRMAX threshold. */
571 ltrc = rd32(IGC_LTRC) |
573 wr32(IGC_LTRC, ltrc);
575 /* Calculate tw_system (nsec). */
576 if (speed == SPEED_100) {
577 tw_system = ((rd32(IGC_EEE_SU) &
578 IGC_TW_SYSTEM_100_MASK) >>
579 IGC_TW_SYSTEM_100_SHIFT) * 500;
581 tw_system = (rd32(IGC_EEE_SU) &
582 IGC_TW_SYSTEM_1000_MASK) * 500;
588 /* Get the Rx packet buffer size. */
589 size = rd32(IGC_RXPBS) &
590 IGC_RXPBS_SIZE_I225_MASK;
592 /* Calculations vary based on DMAC settings. */
593 if (rd32(IGC_DMACR) & IGC_DMACR_DMAC_EN) {
594 size -= (rd32(IGC_DMACR) &
595 IGC_DMACR_DMACTHR_MASK) >>
596 IGC_DMACR_DMACTHR_SHIFT;
597 /* Convert size to bits. */
600 /* Convert size to bytes, subtract the MTU, and then
601 * convert the size to bits.
608 hw_dbg("Invalid effective Rx buffer size %d\n",
610 return -IGC_ERR_CONFIG;
613 /* Calculate the thresholds. Since speed is in Mbps, simplify
614 * the calculation by multiplying size/speed by 1000 for result
615 * to be in nsec before dividing by the scale in nsec. Set the
616 * scale such that the LTR threshold fits in the register.
618 ltr_min = (1000 * size) / speed;
619 ltr_max = ltr_min + tw_system;
620 scale_min = (ltr_min / 1024) < 1024 ? IGC_LTRMINV_SCALE_1024 :
621 IGC_LTRMINV_SCALE_32768;
622 scale_max = (ltr_max / 1024) < 1024 ? IGC_LTRMAXV_SCALE_1024 :
623 IGC_LTRMAXV_SCALE_32768;
624 ltr_min /= scale_min == IGC_LTRMINV_SCALE_1024 ? 1024 : 32768;
626 ltr_max /= scale_max == IGC_LTRMAXV_SCALE_1024 ? 1024 : 32768;
629 /* Only write the LTR thresholds if they differ from before. */
630 ltrv = rd32(IGC_LTRMINV);
631 if (ltr_min != (ltrv & IGC_LTRMINV_LTRV_MASK)) {
632 ltrv = IGC_LTRMINV_LSNP_REQ | ltr_min |
633 (scale_min << IGC_LTRMINV_SCALE_SHIFT);
634 wr32(IGC_LTRMINV, ltrv);
637 ltrv = rd32(IGC_LTRMAXV);
638 if (ltr_max != (ltrv & IGC_LTRMAXV_LTRV_MASK)) {
639 ltrv = IGC_LTRMAXV_LSNP_REQ | ltr_max |
640 (scale_min << IGC_LTRMAXV_SCALE_SHIFT);
641 wr32(IGC_LTRMAXV, ltrv);