1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3 * Copyright(c) 2010-2017 Intel Corporation
8 #include "ngbe_eeprom.h"
11 * ngbe_init_eeprom_params - Initialize EEPROM params
12 * @hw: pointer to hardware structure
14 * Initializes the EEPROM parameters ngbe_rom_info within the
15 * ngbe_hw struct in order to set up EEPROM access.
17 s32 ngbe_init_eeprom_params(struct ngbe_hw *hw)
19 struct ngbe_rom_info *eeprom = &hw->rom;
23 if (eeprom->type != ngbe_eeprom_unknown)
26 eeprom->type = ngbe_eeprom_none;
27 /* Set default semaphore delay to 10ms which is a well
30 eeprom->semaphore_delay = 10; /*ms*/
31 /* Clear EEPROM page size, it will be initialized as needed */
32 eeprom->word_page_size = 0;
35 * Check for EEPROM present first.
36 * If not present leave as none
38 eec = rd32(hw, NGBE_SPISTAT);
39 if (!(eec & NGBE_SPISTAT_BPFLASH)) {
40 eeprom->type = ngbe_eeprom_flash;
43 * SPI EEPROM is assumed here. This code would need to
44 * change if a future EEPROM is not SPI.
47 eeprom->word_size = eeprom_size >> 1;
50 eeprom->address_bits = 16;
51 eeprom->sw_addr = 0x80;
53 DEBUGOUT("eeprom params: type = %d, size = %d, address bits: %d %d",
54 eeprom->type, eeprom->word_size,
55 eeprom->address_bits, eeprom->sw_addr);
61 * ngbe_get_eeprom_semaphore - Get hardware semaphore
62 * @hw: pointer to hardware structure
64 * Sets the hardware semaphores so EEPROM access can occur for bit-bang method
66 s32 ngbe_get_eeprom_semaphore(struct ngbe_hw *hw)
68 s32 status = NGBE_ERR_EEPROM;
73 /* Get SMBI software semaphore between device drivers first */
74 for (i = 0; i < timeout; i++) {
76 * If the SMBI bit is 0 when we read it, then the bit will be
77 * set and we have the semaphore
79 swsm = rd32(hw, NGBE_SWSEM);
80 if (!(swsm & NGBE_SWSEM_PF)) {
88 DEBUGOUT("Driver can't access the eeprom - SMBI Semaphore not granted.");
90 * this release is particularly important because our attempts
91 * above to get the semaphore may have succeeded, and if there
92 * was a timeout, we should unconditionally clear the semaphore
93 * bits to free the driver to make progress
95 ngbe_release_eeprom_semaphore(hw);
100 * If the SMBI bit is 0 when we read it, then the bit will be
101 * set and we have the semaphore
103 swsm = rd32(hw, NGBE_SWSEM);
104 if (!(swsm & NGBE_SWSEM_PF))
108 /* Now get the semaphore between SW/FW through the SWESMBI bit */
110 for (i = 0; i < timeout; i++) {
111 /* Set the SW EEPROM semaphore bit to request access */
112 wr32m(hw, NGBE_MNGSWSYNC,
113 NGBE_MNGSWSYNC_REQ, NGBE_MNGSWSYNC_REQ);
116 * If we set the bit successfully then we got the
119 swsm = rd32(hw, NGBE_MNGSWSYNC);
120 if (swsm & NGBE_MNGSWSYNC_REQ)
127 * Release semaphores and return error if SW EEPROM semaphore
128 * was not granted because we don't have access to the EEPROM
131 DEBUGOUT("SWESMBI Software EEPROM semaphore not granted.");
132 ngbe_release_eeprom_semaphore(hw);
133 status = NGBE_ERR_EEPROM;
136 DEBUGOUT("Software semaphore SMBI between device drivers not granted.");
143 * ngbe_release_eeprom_semaphore - Release hardware semaphore
144 * @hw: pointer to hardware structure
146 * This function clears hardware semaphore bits.
148 void ngbe_release_eeprom_semaphore(struct ngbe_hw *hw)
150 wr32m(hw, NGBE_MNGSWSYNC, NGBE_MNGSWSYNC_REQ, 0);
151 wr32m(hw, NGBE_SWSEM, NGBE_SWSEM_PF, 0);
156 * ngbe_ee_read_buffer- Read EEPROM word(s) using hostif
157 * @hw: pointer to hardware structure
158 * @offset: offset of word in the EEPROM to read
159 * @words: number of words
160 * @data: word(s) read from the EEPROM
162 * Reads a 16 bit word(s) from the EEPROM using the hostif.
164 s32 ngbe_ee_readw_buffer(struct ngbe_hw *hw,
165 u32 offset, u32 words, void *data)
167 const u32 mask = NGBE_MNGSEM_SWMBX | NGBE_MNGSEM_SWFLASH;
168 u32 addr = (offset << 1);
169 u32 len = (words << 1);
170 u8 *buf = (u8 *)data;
173 err = hw->mac.acquire_swfw_sync(hw, mask);
178 u32 seg = (len <= NGBE_PMMBX_DATA_SIZE
179 ? len : NGBE_PMMBX_DATA_SIZE);
181 err = ngbe_hic_sr_read(hw, addr, buf, seg);
190 hw->mac.release_swfw_sync(hw, mask);
195 * ngbe_ee_read32 - Read EEPROM word using a host interface cmd
196 * @hw: pointer to hardware structure
197 * @offset: offset of word in the EEPROM to read
198 * @data: word read from the EEPROM
200 * Reads a 32 bit word from the EEPROM using the hostif.
202 s32 ngbe_ee_read32(struct ngbe_hw *hw, u32 addr, u32 *data)
204 const u32 mask = NGBE_MNGSEM_SWMBX | NGBE_MNGSEM_SWFLASH;
207 err = hw->mac.acquire_swfw_sync(hw, mask);
211 err = ngbe_hic_sr_read(hw, addr, (u8 *)data, 4);
213 hw->mac.release_swfw_sync(hw, mask);
219 * ngbe_ee_write_buffer - Write EEPROM word(s) using hostif
220 * @hw: pointer to hardware structure
221 * @offset: offset of word in the EEPROM to write
222 * @words: number of words
223 * @data: word(s) write to the EEPROM
225 * Write a 16 bit word(s) to the EEPROM using the hostif.
227 s32 ngbe_ee_writew_buffer(struct ngbe_hw *hw,
228 u32 offset, u32 words, void *data)
230 const u32 mask = NGBE_MNGSEM_SWMBX | NGBE_MNGSEM_SWFLASH;
231 u32 addr = (offset << 1);
232 u32 len = (words << 1);
233 u8 *buf = (u8 *)data;
236 err = hw->mac.acquire_swfw_sync(hw, mask);
241 u32 seg = (len <= NGBE_PMMBX_DATA_SIZE
242 ? len : NGBE_PMMBX_DATA_SIZE);
244 err = ngbe_hic_sr_write(hw, addr, buf, seg);
252 hw->mac.release_swfw_sync(hw, mask);
257 * ngbe_validate_eeprom_checksum_em - Validate EEPROM checksum
258 * @hw: pointer to hardware structure
259 * @checksum_val: calculated checksum
261 * Performs checksum calculation and validates the EEPROM checksum. If the
262 * caller does not need checksum_val, the value can be NULL.
264 s32 ngbe_validate_eeprom_checksum_em(struct ngbe_hw *hw,
267 u32 eeprom_cksum_devcap = 0;
270 UNREFERENCED_PARAMETER(checksum_val);
272 /* Check EEPROM only once */
273 if (hw->bus.lan_id == 0) {
274 wr32(hw, NGBE_CALSUM_CAP_STATUS, 0x0);
275 wr32(hw, NGBE_EEPROM_VERSION_STORE_REG, 0x0);
277 eeprom_cksum_devcap = rd32(hw, NGBE_CALSUM_CAP_STATUS);
278 hw->rom.saved_version = rd32(hw, NGBE_EEPROM_VERSION_STORE_REG);
281 if (hw->bus.lan_id == 0 || eeprom_cksum_devcap == 0) {
282 err = ngbe_hic_check_cap(hw);
285 "The EEPROM checksum is not valid: %d", err);
290 hw->rom.cksum_devcap = eeprom_cksum_devcap & 0xffff;
296 * ngbe_save_eeprom_version
297 * @hw: pointer to hardware structure
299 * Save off EEPROM version number and Option Rom version which
300 * together make a unique identify for the eeprom
302 s32 ngbe_save_eeprom_version(struct ngbe_hw *hw)
306 u32 offset = (hw->rom.sw_addr + NGBE_EEPROM_VERSION_L) << 1;
308 if (hw->bus.lan_id == 0) {
309 hw->rom.read32(hw, offset, &eeprom_verl);
310 etrack_id = eeprom_verl;
311 wr32(hw, NGBE_EEPROM_VERSION_STORE_REG, etrack_id);
312 wr32(hw, NGBE_CALSUM_CAP_STATUS,
313 hw->rom.cksum_devcap | 0x10000);
314 } else if (hw->rom.cksum_devcap) {
315 etrack_id = hw->rom.saved_version;
317 hw->rom.read32(hw, offset, &eeprom_verl);
318 etrack_id = eeprom_verl;
321 hw->eeprom_id = etrack_id;