1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2005-2014, 2018-2019 Intel Corporation
5 #include <linux/types.h>
6 #include <linux/slab.h>
7 #include <linux/export.h>
10 #include "iwl-debug.h"
11 #include "iwl-eeprom-read.h"
17 * EEPROM access time values:
19 * Driver initiates EEPROM read by writing byte address << 1 to CSR_EEPROM_REG.
20 * Driver then polls CSR_EEPROM_REG for CSR_EEPROM_REG_READ_VALID_MSK (0x1).
21 * When polling, wait 10 uSec between polling loops, up to a maximum 5000 uSec.
22 * Driver reads 16-bit value from bits 31-16 of CSR_EEPROM_REG.
24 #define IWL_EEPROM_ACCESS_TIMEOUT 5000 /* uSec */
26 #define IWL_EEPROM_SEM_TIMEOUT 10 /* microseconds */
27 #define IWL_EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
31 * The device's EEPROM semaphore prevents conflicts between driver and uCode
32 * when accessing the EEPROM; each access is a series of pulses to/from the
33 * EEPROM chip, not a single event, so even reads could conflict if they
34 * weren't arbitrated by the semaphore.
37 #define EEPROM_SEM_TIMEOUT 10 /* milliseconds */
38 #define EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
40 static int iwl_eeprom_acquire_semaphore(struct iwl_trans
*trans
)
45 for (count
= 0; count
< EEPROM_SEM_RETRY_LIMIT
; count
++) {
46 /* Request semaphore */
47 iwl_set_bit(trans
, CSR_HW_IF_CONFIG_REG
,
48 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM
);
50 /* See if we got it */
51 ret
= iwl_poll_bit(trans
, CSR_HW_IF_CONFIG_REG
,
52 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM
,
53 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM
,
56 IWL_DEBUG_EEPROM(trans
->dev
,
57 "Acquired semaphore after %d tries.\n",
66 static void iwl_eeprom_release_semaphore(struct iwl_trans
*trans
)
68 iwl_clear_bit(trans
, CSR_HW_IF_CONFIG_REG
,
69 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM
);
72 static int iwl_eeprom_verify_signature(struct iwl_trans
*trans
, bool nvm_is_otp
)
74 u32 gp
= iwl_read32(trans
, CSR_EEPROM_GP
) & CSR_EEPROM_GP_VALID_MSK
;
76 IWL_DEBUG_EEPROM(trans
->dev
, "EEPROM signature=0x%08x\n", gp
);
79 case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP
:
81 IWL_ERR(trans
, "EEPROM with bad signature: 0x%08x\n",
86 case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K
:
87 case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K
:
89 IWL_ERR(trans
, "OTP with bad signature: 0x%08x\n", gp
);
93 case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP
:
96 "bad EEPROM/OTP signature, type=%s, EEPROM_GP=0x%08x\n",
97 nvm_is_otp
? "OTP" : "EEPROM", gp
);
102 /******************************************************************************
104 * OTP related functions
106 ******************************************************************************/
108 static void iwl_set_otp_access_absolute(struct iwl_trans
*trans
)
110 iwl_read32(trans
, CSR_OTP_GP_REG
);
112 iwl_clear_bit(trans
, CSR_OTP_GP_REG
,
113 CSR_OTP_GP_REG_OTP_ACCESS_MODE
);
116 static int iwl_nvm_is_otp(struct iwl_trans
*trans
)
120 /* OTP only valid for CP/PP and after */
121 switch (trans
->hw_rev
& CSR_HW_REV_TYPE_MSK
) {
122 case CSR_HW_REV_TYPE_NONE
:
123 IWL_ERR(trans
, "Unknown hardware type\n");
125 case CSR_HW_REV_TYPE_5300
:
126 case CSR_HW_REV_TYPE_5350
:
127 case CSR_HW_REV_TYPE_5100
:
128 case CSR_HW_REV_TYPE_5150
:
131 otpgp
= iwl_read32(trans
, CSR_OTP_GP_REG
);
132 if (otpgp
& CSR_OTP_GP_REG_DEVICE_SELECT
)
138 static int iwl_init_otp_access(struct iwl_trans
*trans
)
142 ret
= iwl_finish_nic_init(trans
, trans
->trans_cfg
);
146 iwl_set_bits_prph(trans
, APMG_PS_CTRL_REG
,
147 APMG_PS_CTRL_VAL_RESET_REQ
);
149 iwl_clear_bits_prph(trans
, APMG_PS_CTRL_REG
,
150 APMG_PS_CTRL_VAL_RESET_REQ
);
153 * CSR auto clock gate disable bit -
154 * this is only applicable for HW with OTP shadow RAM
156 if (trans
->trans_cfg
->base_params
->shadow_ram_support
)
157 iwl_set_bit(trans
, CSR_DBG_LINK_PWR_MGMT_REG
,
158 CSR_RESET_LINK_PWR_MGMT_DISABLED
);
163 static int iwl_read_otp_word(struct iwl_trans
*trans
, u16 addr
,
170 iwl_write32(trans
, CSR_EEPROM_REG
,
171 CSR_EEPROM_REG_MSK_ADDR
& (addr
<< 1));
172 ret
= iwl_poll_bit(trans
, CSR_EEPROM_REG
,
173 CSR_EEPROM_REG_READ_VALID_MSK
,
174 CSR_EEPROM_REG_READ_VALID_MSK
,
175 IWL_EEPROM_ACCESS_TIMEOUT
);
177 IWL_ERR(trans
, "Time out reading OTP[%d]\n", addr
);
180 r
= iwl_read32(trans
, CSR_EEPROM_REG
);
181 /* check for ECC errors: */
182 otpgp
= iwl_read32(trans
, CSR_OTP_GP_REG
);
183 if (otpgp
& CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK
) {
184 /* stop in this case */
185 /* set the uncorrectable OTP ECC bit for acknowledgment */
186 iwl_set_bit(trans
, CSR_OTP_GP_REG
,
187 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK
);
188 IWL_ERR(trans
, "Uncorrectable OTP ECC error, abort OTP read\n");
191 if (otpgp
& CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK
) {
192 /* continue in this case */
193 /* set the correctable OTP ECC bit for acknowledgment */
194 iwl_set_bit(trans
, CSR_OTP_GP_REG
,
195 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK
);
196 IWL_ERR(trans
, "Correctable OTP ECC error, continue read\n");
198 *eeprom_data
= cpu_to_le16(r
>> 16);
203 * iwl_is_otp_empty: check for empty OTP
205 static bool iwl_is_otp_empty(struct iwl_trans
*trans
)
207 u16 next_link_addr
= 0;
209 bool is_empty
= false;
211 /* locate the beginning of OTP link list */
212 if (!iwl_read_otp_word(trans
, next_link_addr
, &link_value
)) {
214 IWL_ERR(trans
, "OTP is empty\n");
218 IWL_ERR(trans
, "Unable to read first block of OTP list.\n");
227 * iwl_find_otp_image: find EEPROM image in OTP
228 * finding the OTP block that contains the EEPROM image.
229 * the last valid block on the link list (the block _before_ the last block)
230 * is the block we should read and used to configure the device.
231 * If all the available OTP blocks are full, the last block will be the block
232 * we should read and used to configure the device.
233 * only perform this operation if shadow RAM is disabled
235 static int iwl_find_otp_image(struct iwl_trans
*trans
,
238 u16 next_link_addr
= 0, valid_addr
;
239 __le16 link_value
= 0;
242 /* set addressing mode to absolute to traverse the link list */
243 iwl_set_otp_access_absolute(trans
);
245 /* checking for empty OTP or error */
246 if (iwl_is_otp_empty(trans
))
250 * start traverse link list
251 * until reach the max number of OTP blocks
252 * different devices have different number of OTP blocks
255 /* save current valid block address
256 * check for more block on the link list
258 valid_addr
= next_link_addr
;
259 next_link_addr
= le16_to_cpu(link_value
) * sizeof(u16
);
260 IWL_DEBUG_EEPROM(trans
->dev
, "OTP blocks %d addr 0x%x\n",
261 usedblocks
, next_link_addr
);
262 if (iwl_read_otp_word(trans
, next_link_addr
, &link_value
))
266 * reach the end of link list, return success and
267 * set address point to the starting address
270 *validblockaddr
= valid_addr
;
271 /* skip first 2 bytes (link list pointer) */
272 *validblockaddr
+= 2;
275 /* more in the link list, continue */
277 } while (usedblocks
<= trans
->trans_cfg
->base_params
->max_ll_items
);
279 /* OTP has no valid blocks */
280 IWL_DEBUG_EEPROM(trans
->dev
, "OTP has no valid blocks\n");
285 * iwl_read_eeprom - read EEPROM contents
287 * Load the EEPROM contents from adapter and return it
290 * NOTE: This routine uses the non-debug IO access functions.
292 int iwl_read_eeprom(struct iwl_trans
*trans
, u8
**eeprom
, size_t *eeprom_size
)
295 u32 gp
= iwl_read32(trans
, CSR_EEPROM_GP
);
299 u16 validblockaddr
= 0;
303 if (!eeprom
|| !eeprom_size
)
306 nvm_is_otp
= iwl_nvm_is_otp(trans
);
310 sz
= trans
->trans_cfg
->base_params
->eeprom_size
;
311 IWL_DEBUG_EEPROM(trans
->dev
, "NVM size = %d\n", sz
);
313 e
= kmalloc(sz
, GFP_KERNEL
);
317 ret
= iwl_eeprom_verify_signature(trans
, nvm_is_otp
);
319 IWL_ERR(trans
, "EEPROM not found, EEPROM_GP=0x%08x\n", gp
);
323 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
324 ret
= iwl_eeprom_acquire_semaphore(trans
);
326 IWL_ERR(trans
, "Failed to acquire EEPROM semaphore.\n");
331 ret
= iwl_init_otp_access(trans
);
333 IWL_ERR(trans
, "Failed to initialize OTP access.\n");
337 iwl_write32(trans
, CSR_EEPROM_GP
,
338 iwl_read32(trans
, CSR_EEPROM_GP
) &
339 ~CSR_EEPROM_GP_IF_OWNER_MSK
);
341 iwl_set_bit(trans
, CSR_OTP_GP_REG
,
342 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK
|
343 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK
);
344 /* traversing the linked list if no shadow ram supported */
345 if (!trans
->trans_cfg
->base_params
->shadow_ram_support
) {
346 ret
= iwl_find_otp_image(trans
, &validblockaddr
);
350 for (addr
= validblockaddr
; addr
< validblockaddr
+ sz
;
351 addr
+= sizeof(u16
)) {
354 ret
= iwl_read_otp_word(trans
, addr
, &eeprom_data
);
357 e
[cache_addr
/ 2] = eeprom_data
;
358 cache_addr
+= sizeof(u16
);
361 /* eeprom is an array of 16bit values */
362 for (addr
= 0; addr
< sz
; addr
+= sizeof(u16
)) {
365 iwl_write32(trans
, CSR_EEPROM_REG
,
366 CSR_EEPROM_REG_MSK_ADDR
& (addr
<< 1));
368 ret
= iwl_poll_bit(trans
, CSR_EEPROM_REG
,
369 CSR_EEPROM_REG_READ_VALID_MSK
,
370 CSR_EEPROM_REG_READ_VALID_MSK
,
371 IWL_EEPROM_ACCESS_TIMEOUT
);
374 "Time out reading EEPROM[%d]\n", addr
);
377 r
= iwl_read32(trans
, CSR_EEPROM_REG
);
378 e
[addr
/ 2] = cpu_to_le16(r
>> 16);
382 IWL_DEBUG_EEPROM(trans
->dev
, "NVM Type: %s\n",
383 nvm_is_otp
? "OTP" : "EEPROM");
385 iwl_eeprom_release_semaphore(trans
);
392 iwl_eeprom_release_semaphore(trans
);
398 IWL_EXPORT_SYMBOL(iwl_read_eeprom
);