1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2018 Intel Corporation
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <linuxwifi@intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2018 Intel Corporation
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *****************************************************************************/
64 #include <linux/types.h>
65 #include <linux/slab.h>
66 #include <linux/export.h>
69 #include "iwl-debug.h"
70 #include "iwl-eeprom-read.h"
76 * EEPROM access time values:
78 * Driver initiates EEPROM read by writing byte address << 1 to CSR_EEPROM_REG.
79 * Driver then polls CSR_EEPROM_REG for CSR_EEPROM_REG_READ_VALID_MSK (0x1).
80 * When polling, wait 10 uSec between polling loops, up to a maximum 5000 uSec.
81 * Driver reads 16-bit value from bits 31-16 of CSR_EEPROM_REG.
83 #define IWL_EEPROM_ACCESS_TIMEOUT 5000 /* uSec */
85 #define IWL_EEPROM_SEM_TIMEOUT 10 /* microseconds */
86 #define IWL_EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
90 * The device's EEPROM semaphore prevents conflicts between driver and uCode
91 * when accessing the EEPROM; each access is a series of pulses to/from the
92 * EEPROM chip, not a single event, so even reads could conflict if they
93 * weren't arbitrated by the semaphore.
96 #define EEPROM_SEM_TIMEOUT 10 /* milliseconds */
97 #define EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
99 static int iwl_eeprom_acquire_semaphore(struct iwl_trans *trans)
104 for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
105 /* Request semaphore */
106 iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
107 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
109 /* See if we got it */
110 ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
111 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
112 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
115 IWL_DEBUG_EEPROM(trans->dev,
116 "Acquired semaphore after %d tries.\n",
125 static void iwl_eeprom_release_semaphore(struct iwl_trans *trans)
127 iwl_clear_bit(trans, CSR_HW_IF_CONFIG_REG,
128 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
131 static int iwl_eeprom_verify_signature(struct iwl_trans *trans, bool nvm_is_otp)
133 u32 gp = iwl_read32(trans, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
135 IWL_DEBUG_EEPROM(trans->dev, "EEPROM signature=0x%08x\n", gp);
138 case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
140 IWL_ERR(trans, "EEPROM with bad signature: 0x%08x\n",
145 case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
146 case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
148 IWL_ERR(trans, "OTP with bad signature: 0x%08x\n", gp);
152 case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
155 "bad EEPROM/OTP signature, type=%s, EEPROM_GP=0x%08x\n",
156 nvm_is_otp ? "OTP" : "EEPROM", gp);
161 /******************************************************************************
163 * OTP related functions
165 ******************************************************************************/
167 static void iwl_set_otp_access_absolute(struct iwl_trans *trans)
169 iwl_read32(trans, CSR_OTP_GP_REG);
171 iwl_clear_bit(trans, CSR_OTP_GP_REG,
172 CSR_OTP_GP_REG_OTP_ACCESS_MODE);
175 static int iwl_nvm_is_otp(struct iwl_trans *trans)
179 /* OTP only valid for CP/PP and after */
180 switch (trans->hw_rev & CSR_HW_REV_TYPE_MSK) {
181 case CSR_HW_REV_TYPE_NONE:
182 IWL_ERR(trans, "Unknown hardware type\n");
184 case CSR_HW_REV_TYPE_5300:
185 case CSR_HW_REV_TYPE_5350:
186 case CSR_HW_REV_TYPE_5100:
187 case CSR_HW_REV_TYPE_5150:
190 otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
191 if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
197 static int iwl_init_otp_access(struct iwl_trans *trans)
201 /* Enable 40MHz radio clock */
202 iwl_write32(trans, CSR_GP_CNTRL,
203 iwl_read32(trans, CSR_GP_CNTRL) |
204 BIT(trans->cfg->csr->flag_init_done));
206 /* wait for clock to be ready */
207 ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
208 BIT(trans->cfg->csr->flag_mac_clock_ready),
209 BIT(trans->cfg->csr->flag_mac_clock_ready),
212 IWL_ERR(trans, "Time out access OTP\n");
214 iwl_set_bits_prph(trans, APMG_PS_CTRL_REG,
215 APMG_PS_CTRL_VAL_RESET_REQ);
217 iwl_clear_bits_prph(trans, APMG_PS_CTRL_REG,
218 APMG_PS_CTRL_VAL_RESET_REQ);
221 * CSR auto clock gate disable bit -
222 * this is only applicable for HW with OTP shadow RAM
224 if (trans->cfg->base_params->shadow_ram_support)
225 iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
226 CSR_RESET_LINK_PWR_MGMT_DISABLED);
231 static int iwl_read_otp_word(struct iwl_trans *trans, u16 addr,
238 iwl_write32(trans, CSR_EEPROM_REG,
239 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
240 ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
241 CSR_EEPROM_REG_READ_VALID_MSK,
242 CSR_EEPROM_REG_READ_VALID_MSK,
243 IWL_EEPROM_ACCESS_TIMEOUT);
245 IWL_ERR(trans, "Time out reading OTP[%d]\n", addr);
248 r = iwl_read32(trans, CSR_EEPROM_REG);
249 /* check for ECC errors: */
250 otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
251 if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
252 /* stop in this case */
253 /* set the uncorrectable OTP ECC bit for acknowledgment */
254 iwl_set_bit(trans, CSR_OTP_GP_REG,
255 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
256 IWL_ERR(trans, "Uncorrectable OTP ECC error, abort OTP read\n");
259 if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
260 /* continue in this case */
261 /* set the correctable OTP ECC bit for acknowledgment */
262 iwl_set_bit(trans, CSR_OTP_GP_REG,
263 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
264 IWL_ERR(trans, "Correctable OTP ECC error, continue read\n");
266 *eeprom_data = cpu_to_le16(r >> 16);
271 * iwl_is_otp_empty: check for empty OTP
273 static bool iwl_is_otp_empty(struct iwl_trans *trans)
275 u16 next_link_addr = 0;
277 bool is_empty = false;
279 /* locate the beginning of OTP link list */
280 if (!iwl_read_otp_word(trans, next_link_addr, &link_value)) {
282 IWL_ERR(trans, "OTP is empty\n");
286 IWL_ERR(trans, "Unable to read first block of OTP list.\n");
295 * iwl_find_otp_image: find EEPROM image in OTP
296 * finding the OTP block that contains the EEPROM image.
297 * the last valid block on the link list (the block _before_ the last block)
298 * is the block we should read and used to configure the device.
299 * If all the available OTP blocks are full, the last block will be the block
300 * we should read and used to configure the device.
301 * only perform this operation if shadow RAM is disabled
303 static int iwl_find_otp_image(struct iwl_trans *trans,
306 u16 next_link_addr = 0, valid_addr;
307 __le16 link_value = 0;
310 /* set addressing mode to absolute to traverse the link list */
311 iwl_set_otp_access_absolute(trans);
313 /* checking for empty OTP or error */
314 if (iwl_is_otp_empty(trans))
318 * start traverse link list
319 * until reach the max number of OTP blocks
320 * different devices have different number of OTP blocks
323 /* save current valid block address
324 * check for more block on the link list
326 valid_addr = next_link_addr;
327 next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
328 IWL_DEBUG_EEPROM(trans->dev, "OTP blocks %d addr 0x%x\n",
329 usedblocks, next_link_addr);
330 if (iwl_read_otp_word(trans, next_link_addr, &link_value))
334 * reach the end of link list, return success and
335 * set address point to the starting address
338 *validblockaddr = valid_addr;
339 /* skip first 2 bytes (link list pointer) */
340 *validblockaddr += 2;
343 /* more in the link list, continue */
345 } while (usedblocks <= trans->cfg->base_params->max_ll_items);
347 /* OTP has no valid blocks */
348 IWL_DEBUG_EEPROM(trans->dev, "OTP has no valid blocks\n");
353 * iwl_read_eeprom - read EEPROM contents
355 * Load the EEPROM contents from adapter and return it
358 * NOTE: This routine uses the non-debug IO access functions.
360 int iwl_read_eeprom(struct iwl_trans *trans, u8 **eeprom, size_t *eeprom_size)
363 u32 gp = iwl_read32(trans, CSR_EEPROM_GP);
367 u16 validblockaddr = 0;
371 if (!eeprom || !eeprom_size)
374 nvm_is_otp = iwl_nvm_is_otp(trans);
378 sz = trans->cfg->base_params->eeprom_size;
379 IWL_DEBUG_EEPROM(trans->dev, "NVM size = %d\n", sz);
381 e = kmalloc(sz, GFP_KERNEL);
385 ret = iwl_eeprom_verify_signature(trans, nvm_is_otp);
387 IWL_ERR(trans, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
391 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
392 ret = iwl_eeprom_acquire_semaphore(trans);
394 IWL_ERR(trans, "Failed to acquire EEPROM semaphore.\n");
399 ret = iwl_init_otp_access(trans);
401 IWL_ERR(trans, "Failed to initialize OTP access.\n");
405 iwl_write32(trans, CSR_EEPROM_GP,
406 iwl_read32(trans, CSR_EEPROM_GP) &
407 ~CSR_EEPROM_GP_IF_OWNER_MSK);
409 iwl_set_bit(trans, CSR_OTP_GP_REG,
410 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
411 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
412 /* traversing the linked list if no shadow ram supported */
413 if (!trans->cfg->base_params->shadow_ram_support) {
414 ret = iwl_find_otp_image(trans, &validblockaddr);
418 for (addr = validblockaddr; addr < validblockaddr + sz;
419 addr += sizeof(u16)) {
422 ret = iwl_read_otp_word(trans, addr, &eeprom_data);
425 e[cache_addr / 2] = eeprom_data;
426 cache_addr += sizeof(u16);
429 /* eeprom is an array of 16bit values */
430 for (addr = 0; addr < sz; addr += sizeof(u16)) {
433 iwl_write32(trans, CSR_EEPROM_REG,
434 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
436 ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
437 CSR_EEPROM_REG_READ_VALID_MSK,
438 CSR_EEPROM_REG_READ_VALID_MSK,
439 IWL_EEPROM_ACCESS_TIMEOUT);
442 "Time out reading EEPROM[%d]\n", addr);
445 r = iwl_read32(trans, CSR_EEPROM_REG);
446 e[addr / 2] = cpu_to_le16(r >> 16);
450 IWL_DEBUG_EEPROM(trans->dev, "NVM Type: %s\n",
451 nvm_is_otp ? "OTP" : "EEPROM");
453 iwl_eeprom_release_semaphore(trans);
460 iwl_eeprom_release_semaphore(trans);
466 IWL_EXPORT_SYMBOL(iwl_read_eeprom);