1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Intel Corporation */
4 #include <linux/bitfield.h>
5 #include <linux/delay.h>
10 * igc_acquire_nvm_i225 - Acquire exclusive access to EEPROM
11 * @hw: pointer to the HW structure
13 * Acquire the necessary semaphores for exclusive access to the EEPROM.
14 * Set the EEPROM access request bit and wait for EEPROM access grant bit.
15 * Return successful if access grant bit set, else clear the request for
16 * EEPROM access and return -IGC_ERR_NVM (-1).
18 static s32 igc_acquire_nvm_i225(struct igc_hw *hw)
20 return igc_acquire_swfw_sync_i225(hw, IGC_SWFW_EEP_SM);
24 * igc_release_nvm_i225 - Release exclusive access to EEPROM
25 * @hw: pointer to the HW structure
27 * Stop any current commands to the EEPROM and clear the EEPROM request bit,
28 * then release the semaphores acquired.
30 static void igc_release_nvm_i225(struct igc_hw *hw)
32 igc_release_swfw_sync_i225(hw, IGC_SWFW_EEP_SM);
36 * igc_get_hw_semaphore_i225 - Acquire hardware semaphore
37 * @hw: pointer to the HW structure
39 * Acquire the HW semaphore to access the PHY or NVM
41 static s32 igc_get_hw_semaphore_i225(struct igc_hw *hw)
43 s32 timeout = hw->nvm.word_size + 1;
47 /* Get the SW semaphore */
49 swsm = rd32(IGC_SWSM);
50 if (!(swsm & IGC_SWSM_SMBI))
53 usleep_range(500, 600);
58 /* In rare circumstances, the SW semaphore may already be held
59 * unintentionally. Clear the semaphore once before giving up.
61 if (hw->dev_spec._base.clear_semaphore_once) {
62 hw->dev_spec._base.clear_semaphore_once = false;
63 igc_put_hw_semaphore(hw);
64 for (i = 0; i < timeout; i++) {
65 swsm = rd32(IGC_SWSM);
66 if (!(swsm & IGC_SWSM_SMBI))
69 usleep_range(500, 600);
73 /* If we do not have the semaphore here, we have to give up. */
75 hw_dbg("Driver can't access device - SMBI bit is set.\n");
80 /* Get the FW semaphore. */
81 for (i = 0; i < timeout; i++) {
82 swsm = rd32(IGC_SWSM);
83 wr32(IGC_SWSM, swsm | IGC_SWSM_SWESMBI);
85 /* Semaphore acquired if bit latched */
86 if (rd32(IGC_SWSM) & IGC_SWSM_SWESMBI)
89 usleep_range(500, 600);
93 /* Release semaphores */
94 igc_put_hw_semaphore(hw);
95 hw_dbg("Driver can't access the NVM\n");
103 * igc_acquire_swfw_sync_i225 - Acquire SW/FW semaphore
104 * @hw: pointer to the HW structure
105 * @mask: specifies which semaphore to acquire
107 * Acquire the SW/FW semaphore to access the PHY or NVM. The mask
108 * will also specify which port we're acquiring the lock for.
110 s32 igc_acquire_swfw_sync_i225(struct igc_hw *hw, u16 mask)
112 s32 i = 0, timeout = 200;
113 u32 fwmask = mask << 16;
118 while (i < timeout) {
119 if (igc_get_hw_semaphore_i225(hw)) {
120 ret_val = -IGC_ERR_SWFW_SYNC;
124 swfw_sync = rd32(IGC_SW_FW_SYNC);
125 if (!(swfw_sync & (fwmask | swmask)))
128 /* Firmware currently using resource (fwmask) */
129 igc_put_hw_semaphore(hw);
135 hw_dbg("Driver can't access resource, SW_FW_SYNC timeout.\n");
136 ret_val = -IGC_ERR_SWFW_SYNC;
141 wr32(IGC_SW_FW_SYNC, swfw_sync);
143 igc_put_hw_semaphore(hw);
149 * igc_release_swfw_sync_i225 - Release SW/FW semaphore
150 * @hw: pointer to the HW structure
151 * @mask: specifies which semaphore to acquire
153 * Release the SW/FW semaphore used to access the PHY or NVM. The mask
154 * will also specify which port we're releasing the lock for.
156 void igc_release_swfw_sync_i225(struct igc_hw *hw, u16 mask)
160 /* Releasing the resource requires first getting the HW semaphore.
161 * If we fail to get the semaphore, there is nothing we can do,
162 * except log an error and quit. We are not allowed to hang here
163 * indefinitely, as it may cause denial of service or system crash.
165 if (igc_get_hw_semaphore_i225(hw)) {
166 hw_dbg("Failed to release SW_FW_SYNC.\n");
170 swfw_sync = rd32(IGC_SW_FW_SYNC);
172 wr32(IGC_SW_FW_SYNC, swfw_sync);
174 igc_put_hw_semaphore(hw);
178 * igc_read_nvm_srrd_i225 - Reads Shadow Ram using EERD register
179 * @hw: pointer to the HW structure
180 * @offset: offset of word in the Shadow Ram to read
181 * @words: number of words to read
182 * @data: word read from the Shadow Ram
184 * Reads a 16 bit word from the Shadow Ram using the EERD register.
185 * Uses necessary synchronization semaphores.
187 static s32 igc_read_nvm_srrd_i225(struct igc_hw *hw, u16 offset, u16 words,
193 /* We cannot hold synchronization semaphores for too long,
194 * because of forceful takeover procedure. However it is more efficient
195 * to read in bursts than synchronizing access for each word.
197 for (i = 0; i < words; i += IGC_EERD_EEWR_MAX_COUNT) {
198 count = (words - i) / IGC_EERD_EEWR_MAX_COUNT > 0 ?
199 IGC_EERD_EEWR_MAX_COUNT : (words - i);
201 status = hw->nvm.ops.acquire(hw);
205 status = igc_read_nvm_eerd(hw, offset, count, data + i);
206 hw->nvm.ops.release(hw);
215 * igc_write_nvm_srwr - Write to Shadow Ram using EEWR
216 * @hw: pointer to the HW structure
217 * @offset: offset within the Shadow Ram to be written to
218 * @words: number of words to write
219 * @data: 16 bit word(s) to be written to the Shadow Ram
221 * Writes data to Shadow Ram at offset using EEWR register.
223 * If igc_update_nvm_checksum is not called after this function , the
224 * Shadow Ram will most likely contain an invalid checksum.
226 static s32 igc_write_nvm_srwr(struct igc_hw *hw, u16 offset, u16 words,
229 struct igc_nvm_info *nvm = &hw->nvm;
230 s32 ret_val = -IGC_ERR_NVM;
231 u32 attempts = 100000;
234 /* A check for invalid values: offset too large, too many words,
235 * too many words for the offset, and not enough words.
237 if (offset >= nvm->word_size || (words > (nvm->word_size - offset)) ||
239 hw_dbg("nvm parameter(s) out of bounds\n");
243 for (i = 0; i < words; i++) {
244 ret_val = -IGC_ERR_NVM;
245 eewr = ((offset + i) << IGC_NVM_RW_ADDR_SHIFT) |
246 (data[i] << IGC_NVM_RW_REG_DATA) |
247 IGC_NVM_RW_REG_START;
249 wr32(IGC_SRWR, eewr);
251 for (k = 0; k < attempts; k++) {
252 if (IGC_NVM_RW_REG_DONE &
261 hw_dbg("Shadow RAM write EEWR timed out\n");
270 * igc_write_nvm_srwr_i225 - Write to Shadow RAM using EEWR
271 * @hw: pointer to the HW structure
272 * @offset: offset within the Shadow RAM to be written to
273 * @words: number of words to write
274 * @data: 16 bit word(s) to be written to the Shadow RAM
276 * Writes data to Shadow RAM at offset using EEWR register.
278 * If igc_update_nvm_checksum is not called after this function , the
279 * data will not be committed to FLASH and also Shadow RAM will most likely
280 * contain an invalid checksum.
282 * If error code is returned, data and Shadow RAM may be inconsistent - buffer
285 static s32 igc_write_nvm_srwr_i225(struct igc_hw *hw, u16 offset, u16 words,
291 /* We cannot hold synchronization semaphores for too long,
292 * because of forceful takeover procedure. However it is more efficient
293 * to write in bursts than synchronizing access for each word.
295 for (i = 0; i < words; i += IGC_EERD_EEWR_MAX_COUNT) {
296 count = (words - i) / IGC_EERD_EEWR_MAX_COUNT > 0 ?
297 IGC_EERD_EEWR_MAX_COUNT : (words - i);
299 status = hw->nvm.ops.acquire(hw);
303 status = igc_write_nvm_srwr(hw, offset, count, data + i);
304 hw->nvm.ops.release(hw);
313 * igc_validate_nvm_checksum_i225 - Validate EEPROM checksum
314 * @hw: pointer to the HW structure
316 * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
317 * and then verifies that the sum of the EEPROM is equal to 0xBABA.
319 static s32 igc_validate_nvm_checksum_i225(struct igc_hw *hw)
321 s32 (*read_op_ptr)(struct igc_hw *hw, u16 offset, u16 count,
325 status = hw->nvm.ops.acquire(hw);
329 /* Replace the read function with semaphore grabbing with
330 * the one that skips this for a while.
331 * We have semaphore taken already here.
333 read_op_ptr = hw->nvm.ops.read;
334 hw->nvm.ops.read = igc_read_nvm_eerd;
336 status = igc_validate_nvm_checksum(hw);
338 /* Revert original read operation. */
339 hw->nvm.ops.read = read_op_ptr;
341 hw->nvm.ops.release(hw);
348 * igc_pool_flash_update_done_i225 - Pool FLUDONE status
349 * @hw: pointer to the HW structure
351 static s32 igc_pool_flash_update_done_i225(struct igc_hw *hw)
353 s32 ret_val = -IGC_ERR_NVM;
356 for (i = 0; i < IGC_FLUDONE_ATTEMPTS; i++) {
357 reg = rd32(IGC_EECD);
358 if (reg & IGC_EECD_FLUDONE_I225) {
369 * igc_update_flash_i225 - Commit EEPROM to the flash
370 * @hw: pointer to the HW structure
372 static s32 igc_update_flash_i225(struct igc_hw *hw)
377 ret_val = igc_pool_flash_update_done_i225(hw);
378 if (ret_val == -IGC_ERR_NVM) {
379 hw_dbg("Flash update time out\n");
383 flup = rd32(IGC_EECD) | IGC_EECD_FLUPD_I225;
384 wr32(IGC_EECD, flup);
386 ret_val = igc_pool_flash_update_done_i225(hw);
388 hw_dbg("Flash update time out\n");
390 hw_dbg("Flash update complete\n");
397 * igc_update_nvm_checksum_i225 - Update EEPROM checksum
398 * @hw: pointer to the HW structure
400 * Updates the EEPROM checksum by reading/adding each word of the EEPROM
401 * up to the checksum. Then calculates the EEPROM checksum and writes the
402 * value to the EEPROM. Next commit EEPROM data onto the Flash.
404 static s32 igc_update_nvm_checksum_i225(struct igc_hw *hw)
410 /* Read the first word from the EEPROM. If this times out or fails, do
411 * not continue or we could be in for a very long wait while every
414 ret_val = igc_read_nvm_eerd(hw, 0, 1, &nvm_data);
416 hw_dbg("EEPROM read failed\n");
420 ret_val = hw->nvm.ops.acquire(hw);
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.
429 for (i = 0; i < NVM_CHECKSUM_REG; i++) {
430 ret_val = igc_read_nvm_eerd(hw, i, 1, &nvm_data);
432 hw->nvm.ops.release(hw);
433 hw_dbg("NVM Read Error while updating checksum.\n");
436 checksum += nvm_data;
438 checksum = (u16)NVM_SUM - checksum;
439 ret_val = igc_write_nvm_srwr(hw, NVM_CHECKSUM_REG, 1,
442 hw->nvm.ops.release(hw);
443 hw_dbg("NVM Write Error while updating checksum.\n");
447 hw->nvm.ops.release(hw);
449 ret_val = igc_update_flash_i225(hw);
456 * igc_get_flash_presence_i225 - Check if flash device is detected
457 * @hw: pointer to the HW structure
459 bool igc_get_flash_presence_i225(struct igc_hw *hw)
461 bool ret_val = false;
464 eec = rd32(IGC_EECD);
465 if (eec & IGC_EECD_FLASH_DETECTED_I225)
472 * igc_init_nvm_params_i225 - Init NVM func ptrs.
473 * @hw: pointer to the HW structure
475 s32 igc_init_nvm_params_i225(struct igc_hw *hw)
477 struct igc_nvm_info *nvm = &hw->nvm;
479 nvm->ops.acquire = igc_acquire_nvm_i225;
480 nvm->ops.release = igc_release_nvm_i225;
482 /* NVM Function Pointers */
483 if (igc_get_flash_presence_i225(hw)) {
484 nvm->ops.read = igc_read_nvm_srrd_i225;
485 nvm->ops.write = igc_write_nvm_srwr_i225;
486 nvm->ops.validate = igc_validate_nvm_checksum_i225;
487 nvm->ops.update = igc_update_nvm_checksum_i225;
489 nvm->ops.read = igc_read_nvm_eerd;
490 nvm->ops.write = NULL;
491 nvm->ops.validate = NULL;
492 nvm->ops.update = NULL;
498 * igc_set_eee_i225 - Enable/disable EEE support
499 * @hw: pointer to the HW structure
500 * @adv2p5G: boolean flag enabling 2.5G EEE advertisement
501 * @adv1G: boolean flag enabling 1G EEE advertisement
502 * @adv100M: boolean flag enabling 100M EEE advertisement
504 * Enable/disable EEE based on setting in dev_spec structure.
506 s32 igc_set_eee_i225(struct igc_hw *hw, bool adv2p5G, bool adv1G,
511 ipcnfg = rd32(IGC_IPCNFG);
512 eeer = rd32(IGC_EEER);
514 /* enable or disable per user setting */
515 if (hw->dev_spec._base.eee_enable) {
516 u32 eee_su = rd32(IGC_EEE_SU);
519 ipcnfg |= IGC_IPCNFG_EEE_100M_AN;
521 ipcnfg &= ~IGC_IPCNFG_EEE_100M_AN;
524 ipcnfg |= IGC_IPCNFG_EEE_1G_AN;
526 ipcnfg &= ~IGC_IPCNFG_EEE_1G_AN;
529 ipcnfg |= IGC_IPCNFG_EEE_2_5G_AN;
531 ipcnfg &= ~IGC_IPCNFG_EEE_2_5G_AN;
533 eeer |= (IGC_EEER_TX_LPI_EN | IGC_EEER_RX_LPI_EN |
536 /* This bit should not be set in normal operation. */
537 if (eee_su & IGC_EEE_SU_LPI_CLK_STP)
538 hw_dbg("LPI Clock Stop Bit should not be set!\n");
540 ipcnfg &= ~(IGC_IPCNFG_EEE_2_5G_AN | IGC_IPCNFG_EEE_1G_AN |
541 IGC_IPCNFG_EEE_100M_AN);
542 eeer &= ~(IGC_EEER_TX_LPI_EN | IGC_EEER_RX_LPI_EN |
545 wr32(IGC_IPCNFG, ipcnfg);
546 wr32(IGC_EEER, eeer);
553 /* igc_set_ltr_i225 - Set Latency Tolerance Reporting thresholds
554 * @hw: pointer to the HW structure
555 * @link: bool indicating link status
557 * Set the LTR thresholds based on the link speed (Mbps), EEE, and DMAC
558 * settings, otherwise specify that there is no LTR requirement.
560 s32 igc_set_ltr_i225(struct igc_hw *hw, bool link)
562 u32 tw_system, ltrc, ltrv, ltr_min, ltr_max, scale_min, scale_max;
566 /* If we do not have link, LTR thresholds are zero. */
568 hw->mac.ops.get_speed_and_duplex(hw, &speed, &duplex);
570 /* Check if using copper interface with EEE enabled or if the
571 * link speed is 10 Mbps.
573 if (hw->dev_spec._base.eee_enable &&
575 /* EEE enabled, so send LTRMAX threshold. */
576 ltrc = rd32(IGC_LTRC) |
578 wr32(IGC_LTRC, ltrc);
580 /* Calculate tw_system (nsec). */
581 if (speed == SPEED_100) {
582 tw_system = FIELD_GET(IGC_TW_SYSTEM_100_MASK,
583 rd32(IGC_EEE_SU)) * 500;
585 tw_system = (rd32(IGC_EEE_SU) &
586 IGC_TW_SYSTEM_1000_MASK) * 500;
592 /* Get the Rx packet buffer size. */
593 size = rd32(IGC_RXPBS) &
594 IGC_RXPBS_SIZE_I225_MASK;
596 /* Convert size to bytes, subtract the MTU, and then
597 * convert the size to bits.
603 hw_dbg("Invalid effective Rx buffer size %d\n",
605 return -IGC_ERR_CONFIG;
608 /* Calculate the thresholds. Since speed is in Mbps, simplify
609 * the calculation by multiplying size/speed by 1000 for result
610 * to be in nsec before dividing by the scale in nsec. Set the
611 * scale such that the LTR threshold fits in the register.
613 ltr_min = (1000 * size) / speed;
614 ltr_max = ltr_min + tw_system;
615 scale_min = (ltr_min / 1024) < 1024 ? IGC_LTRMINV_SCALE_1024 :
616 IGC_LTRMINV_SCALE_32768;
617 scale_max = (ltr_max / 1024) < 1024 ? IGC_LTRMAXV_SCALE_1024 :
618 IGC_LTRMAXV_SCALE_32768;
619 ltr_min /= scale_min == IGC_LTRMINV_SCALE_1024 ? 1024 : 32768;
621 ltr_max /= scale_max == IGC_LTRMAXV_SCALE_1024 ? 1024 : 32768;
624 /* Only write the LTR thresholds if they differ from before. */
625 ltrv = rd32(IGC_LTRMINV);
626 if (ltr_min != (ltrv & IGC_LTRMINV_LTRV_MASK)) {
627 ltrv = IGC_LTRMINV_LSNP_REQ | ltr_min |
628 (scale_min << IGC_LTRMINV_SCALE_SHIFT);
629 wr32(IGC_LTRMINV, ltrv);
632 ltrv = rd32(IGC_LTRMAXV);
633 if (ltr_max != (ltrv & IGC_LTRMAXV_LTRV_MASK)) {
634 ltrv = IGC_LTRMAXV_LSNP_REQ | ltr_max |
635 (scale_max << IGC_LTRMAXV_SCALE_SHIFT);
636 wr32(IGC_LTRMAXV, ltrv);