xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/intel/iwlwifi/iwl-eeprom-read.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
4*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
9*4882a593Smuzhiyun  * Copyright(c) 2018 - 2019 Intel Corporation
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
12*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
13*4882a593Smuzhiyun  * published by the Free Software Foundation.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
16*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18*4882a593Smuzhiyun  * General Public License for more details.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * The full GNU General Public License is included in this distribution
21*4882a593Smuzhiyun  * in the file called COPYING.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Contact Information:
24*4882a593Smuzhiyun  *  Intel Linux Wireless <linuxwifi@intel.com>
25*4882a593Smuzhiyun  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * BSD LICENSE
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
30*4882a593Smuzhiyun  * Copyright(c) 2018 - 2019 Intel Corporation
31*4882a593Smuzhiyun  * All rights reserved.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
34*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
35*4882a593Smuzhiyun  * are met:
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  *  * Redistributions of source code must retain the above copyright
38*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
39*4882a593Smuzhiyun  *  * Redistributions in binary form must reproduce the above copyright
40*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
41*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
42*4882a593Smuzhiyun  *    distribution.
43*4882a593Smuzhiyun  *  * Neither the name Intel Corporation nor the names of its
44*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
45*4882a593Smuzhiyun  *    from this software without specific prior written permission.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58*4882a593Smuzhiyun  *****************************************************************************/
59*4882a593Smuzhiyun #include <linux/types.h>
60*4882a593Smuzhiyun #include <linux/slab.h>
61*4882a593Smuzhiyun #include <linux/export.h>
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #include "iwl-drv.h"
64*4882a593Smuzhiyun #include "iwl-debug.h"
65*4882a593Smuzhiyun #include "iwl-eeprom-read.h"
66*4882a593Smuzhiyun #include "iwl-io.h"
67*4882a593Smuzhiyun #include "iwl-prph.h"
68*4882a593Smuzhiyun #include "iwl-csr.h"
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun  * EEPROM access time values:
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * Driver initiates EEPROM read by writing byte address << 1 to CSR_EEPROM_REG.
74*4882a593Smuzhiyun  * Driver then polls CSR_EEPROM_REG for CSR_EEPROM_REG_READ_VALID_MSK (0x1).
75*4882a593Smuzhiyun  * When polling, wait 10 uSec between polling loops, up to a maximum 5000 uSec.
76*4882a593Smuzhiyun  * Driver reads 16-bit value from bits 31-16 of CSR_EEPROM_REG.
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun #define IWL_EEPROM_ACCESS_TIMEOUT	5000 /* uSec */
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun #define IWL_EEPROM_SEM_TIMEOUT		10   /* microseconds */
81*4882a593Smuzhiyun #define IWL_EEPROM_SEM_RETRY_LIMIT	1000 /* number of attempts (not time) */
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * The device's EEPROM semaphore prevents conflicts between driver and uCode
86*4882a593Smuzhiyun  * when accessing the EEPROM; each access is a series of pulses to/from the
87*4882a593Smuzhiyun  * EEPROM chip, not a single event, so even reads could conflict if they
88*4882a593Smuzhiyun  * weren't arbitrated by the semaphore.
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun #define	EEPROM_SEM_TIMEOUT 10		/* milliseconds */
92*4882a593Smuzhiyun #define EEPROM_SEM_RETRY_LIMIT 1000	/* number of attempts (not time) */
93*4882a593Smuzhiyun 
iwl_eeprom_acquire_semaphore(struct iwl_trans * trans)94*4882a593Smuzhiyun static int iwl_eeprom_acquire_semaphore(struct iwl_trans *trans)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	u16 count;
97*4882a593Smuzhiyun 	int ret;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
100*4882a593Smuzhiyun 		/* Request semaphore */
101*4882a593Smuzhiyun 		iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
102*4882a593Smuzhiyun 			    CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 		/* See if we got it */
105*4882a593Smuzhiyun 		ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
106*4882a593Smuzhiyun 				CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
107*4882a593Smuzhiyun 				CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
108*4882a593Smuzhiyun 				EEPROM_SEM_TIMEOUT);
109*4882a593Smuzhiyun 		if (ret >= 0) {
110*4882a593Smuzhiyun 			IWL_DEBUG_EEPROM(trans->dev,
111*4882a593Smuzhiyun 					 "Acquired semaphore after %d tries.\n",
112*4882a593Smuzhiyun 					 count+1);
113*4882a593Smuzhiyun 			return ret;
114*4882a593Smuzhiyun 		}
115*4882a593Smuzhiyun 	}
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	return ret;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
iwl_eeprom_release_semaphore(struct iwl_trans * trans)120*4882a593Smuzhiyun static void iwl_eeprom_release_semaphore(struct iwl_trans *trans)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	iwl_clear_bit(trans, CSR_HW_IF_CONFIG_REG,
123*4882a593Smuzhiyun 		      CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
iwl_eeprom_verify_signature(struct iwl_trans * trans,bool nvm_is_otp)126*4882a593Smuzhiyun static int iwl_eeprom_verify_signature(struct iwl_trans *trans, bool nvm_is_otp)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	u32 gp = iwl_read32(trans, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	IWL_DEBUG_EEPROM(trans->dev, "EEPROM signature=0x%08x\n", gp);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	switch (gp) {
133*4882a593Smuzhiyun 	case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
134*4882a593Smuzhiyun 		if (!nvm_is_otp) {
135*4882a593Smuzhiyun 			IWL_ERR(trans, "EEPROM with bad signature: 0x%08x\n",
136*4882a593Smuzhiyun 				gp);
137*4882a593Smuzhiyun 			return -ENOENT;
138*4882a593Smuzhiyun 		}
139*4882a593Smuzhiyun 		return 0;
140*4882a593Smuzhiyun 	case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
141*4882a593Smuzhiyun 	case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
142*4882a593Smuzhiyun 		if (nvm_is_otp) {
143*4882a593Smuzhiyun 			IWL_ERR(trans, "OTP with bad signature: 0x%08x\n", gp);
144*4882a593Smuzhiyun 			return -ENOENT;
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 		return 0;
147*4882a593Smuzhiyun 	case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
148*4882a593Smuzhiyun 	default:
149*4882a593Smuzhiyun 		IWL_ERR(trans,
150*4882a593Smuzhiyun 			"bad EEPROM/OTP signature, type=%s, EEPROM_GP=0x%08x\n",
151*4882a593Smuzhiyun 			nvm_is_otp ? "OTP" : "EEPROM", gp);
152*4882a593Smuzhiyun 		return -ENOENT;
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /******************************************************************************
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * OTP related functions
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun ******************************************************************************/
161*4882a593Smuzhiyun 
iwl_set_otp_access_absolute(struct iwl_trans * trans)162*4882a593Smuzhiyun static void iwl_set_otp_access_absolute(struct iwl_trans *trans)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	iwl_read32(trans, CSR_OTP_GP_REG);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	iwl_clear_bit(trans, CSR_OTP_GP_REG,
167*4882a593Smuzhiyun 		      CSR_OTP_GP_REG_OTP_ACCESS_MODE);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
iwl_nvm_is_otp(struct iwl_trans * trans)170*4882a593Smuzhiyun static int iwl_nvm_is_otp(struct iwl_trans *trans)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	u32 otpgp;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* OTP only valid for CP/PP and after */
175*4882a593Smuzhiyun 	switch (trans->hw_rev & CSR_HW_REV_TYPE_MSK) {
176*4882a593Smuzhiyun 	case CSR_HW_REV_TYPE_NONE:
177*4882a593Smuzhiyun 		IWL_ERR(trans, "Unknown hardware type\n");
178*4882a593Smuzhiyun 		return -EIO;
179*4882a593Smuzhiyun 	case CSR_HW_REV_TYPE_5300:
180*4882a593Smuzhiyun 	case CSR_HW_REV_TYPE_5350:
181*4882a593Smuzhiyun 	case CSR_HW_REV_TYPE_5100:
182*4882a593Smuzhiyun 	case CSR_HW_REV_TYPE_5150:
183*4882a593Smuzhiyun 		return 0;
184*4882a593Smuzhiyun 	default:
185*4882a593Smuzhiyun 		otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
186*4882a593Smuzhiyun 		if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
187*4882a593Smuzhiyun 			return 1;
188*4882a593Smuzhiyun 		return 0;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
iwl_init_otp_access(struct iwl_trans * trans)192*4882a593Smuzhiyun static int iwl_init_otp_access(struct iwl_trans *trans)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	int ret;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	ret = iwl_finish_nic_init(trans, trans->trans_cfg);
197*4882a593Smuzhiyun 	if (ret)
198*4882a593Smuzhiyun 		return ret;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	iwl_set_bits_prph(trans, APMG_PS_CTRL_REG,
201*4882a593Smuzhiyun 			  APMG_PS_CTRL_VAL_RESET_REQ);
202*4882a593Smuzhiyun 	udelay(5);
203*4882a593Smuzhiyun 	iwl_clear_bits_prph(trans, APMG_PS_CTRL_REG,
204*4882a593Smuzhiyun 			    APMG_PS_CTRL_VAL_RESET_REQ);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/*
207*4882a593Smuzhiyun 	 * CSR auto clock gate disable bit -
208*4882a593Smuzhiyun 	 * this is only applicable for HW with OTP shadow RAM
209*4882a593Smuzhiyun 	 */
210*4882a593Smuzhiyun 	if (trans->trans_cfg->base_params->shadow_ram_support)
211*4882a593Smuzhiyun 		iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
212*4882a593Smuzhiyun 			    CSR_RESET_LINK_PWR_MGMT_DISABLED);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	return 0;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
iwl_read_otp_word(struct iwl_trans * trans,u16 addr,__le16 * eeprom_data)217*4882a593Smuzhiyun static int iwl_read_otp_word(struct iwl_trans *trans, u16 addr,
218*4882a593Smuzhiyun 			     __le16 *eeprom_data)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	int ret = 0;
221*4882a593Smuzhiyun 	u32 r;
222*4882a593Smuzhiyun 	u32 otpgp;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	iwl_write32(trans, CSR_EEPROM_REG,
225*4882a593Smuzhiyun 		    CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
226*4882a593Smuzhiyun 	ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
227*4882a593Smuzhiyun 				 CSR_EEPROM_REG_READ_VALID_MSK,
228*4882a593Smuzhiyun 				 CSR_EEPROM_REG_READ_VALID_MSK,
229*4882a593Smuzhiyun 				 IWL_EEPROM_ACCESS_TIMEOUT);
230*4882a593Smuzhiyun 	if (ret < 0) {
231*4882a593Smuzhiyun 		IWL_ERR(trans, "Time out reading OTP[%d]\n", addr);
232*4882a593Smuzhiyun 		return ret;
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 	r = iwl_read32(trans, CSR_EEPROM_REG);
235*4882a593Smuzhiyun 	/* check for ECC errors: */
236*4882a593Smuzhiyun 	otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
237*4882a593Smuzhiyun 	if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
238*4882a593Smuzhiyun 		/* stop in this case */
239*4882a593Smuzhiyun 		/* set the uncorrectable OTP ECC bit for acknowledgment */
240*4882a593Smuzhiyun 		iwl_set_bit(trans, CSR_OTP_GP_REG,
241*4882a593Smuzhiyun 			    CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
242*4882a593Smuzhiyun 		IWL_ERR(trans, "Uncorrectable OTP ECC error, abort OTP read\n");
243*4882a593Smuzhiyun 		return -EINVAL;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 	if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
246*4882a593Smuzhiyun 		/* continue in this case */
247*4882a593Smuzhiyun 		/* set the correctable OTP ECC bit for acknowledgment */
248*4882a593Smuzhiyun 		iwl_set_bit(trans, CSR_OTP_GP_REG,
249*4882a593Smuzhiyun 			    CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
250*4882a593Smuzhiyun 		IWL_ERR(trans, "Correctable OTP ECC error, continue read\n");
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 	*eeprom_data = cpu_to_le16(r >> 16);
253*4882a593Smuzhiyun 	return 0;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /*
257*4882a593Smuzhiyun  * iwl_is_otp_empty: check for empty OTP
258*4882a593Smuzhiyun  */
iwl_is_otp_empty(struct iwl_trans * trans)259*4882a593Smuzhiyun static bool iwl_is_otp_empty(struct iwl_trans *trans)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	u16 next_link_addr = 0;
262*4882a593Smuzhiyun 	__le16 link_value;
263*4882a593Smuzhiyun 	bool is_empty = false;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/* locate the beginning of OTP link list */
266*4882a593Smuzhiyun 	if (!iwl_read_otp_word(trans, next_link_addr, &link_value)) {
267*4882a593Smuzhiyun 		if (!link_value) {
268*4882a593Smuzhiyun 			IWL_ERR(trans, "OTP is empty\n");
269*4882a593Smuzhiyun 			is_empty = true;
270*4882a593Smuzhiyun 		}
271*4882a593Smuzhiyun 	} else {
272*4882a593Smuzhiyun 		IWL_ERR(trans, "Unable to read first block of OTP list.\n");
273*4882a593Smuzhiyun 		is_empty = true;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	return is_empty;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /*
281*4882a593Smuzhiyun  * iwl_find_otp_image: find EEPROM image in OTP
282*4882a593Smuzhiyun  *   finding the OTP block that contains the EEPROM image.
283*4882a593Smuzhiyun  *   the last valid block on the link list (the block _before_ the last block)
284*4882a593Smuzhiyun  *   is the block we should read and used to configure the device.
285*4882a593Smuzhiyun  *   If all the available OTP blocks are full, the last block will be the block
286*4882a593Smuzhiyun  *   we should read and used to configure the device.
287*4882a593Smuzhiyun  *   only perform this operation if shadow RAM is disabled
288*4882a593Smuzhiyun  */
iwl_find_otp_image(struct iwl_trans * trans,u16 * validblockaddr)289*4882a593Smuzhiyun static int iwl_find_otp_image(struct iwl_trans *trans,
290*4882a593Smuzhiyun 					u16 *validblockaddr)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	u16 next_link_addr = 0, valid_addr;
293*4882a593Smuzhiyun 	__le16 link_value = 0;
294*4882a593Smuzhiyun 	int usedblocks = 0;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* set addressing mode to absolute to traverse the link list */
297*4882a593Smuzhiyun 	iwl_set_otp_access_absolute(trans);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	/* checking for empty OTP or error */
300*4882a593Smuzhiyun 	if (iwl_is_otp_empty(trans))
301*4882a593Smuzhiyun 		return -EINVAL;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	/*
304*4882a593Smuzhiyun 	 * start traverse link list
305*4882a593Smuzhiyun 	 * until reach the max number of OTP blocks
306*4882a593Smuzhiyun 	 * different devices have different number of OTP blocks
307*4882a593Smuzhiyun 	 */
308*4882a593Smuzhiyun 	do {
309*4882a593Smuzhiyun 		/* save current valid block address
310*4882a593Smuzhiyun 		 * check for more block on the link list
311*4882a593Smuzhiyun 		 */
312*4882a593Smuzhiyun 		valid_addr = next_link_addr;
313*4882a593Smuzhiyun 		next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
314*4882a593Smuzhiyun 		IWL_DEBUG_EEPROM(trans->dev, "OTP blocks %d addr 0x%x\n",
315*4882a593Smuzhiyun 				 usedblocks, next_link_addr);
316*4882a593Smuzhiyun 		if (iwl_read_otp_word(trans, next_link_addr, &link_value))
317*4882a593Smuzhiyun 			return -EINVAL;
318*4882a593Smuzhiyun 		if (!link_value) {
319*4882a593Smuzhiyun 			/*
320*4882a593Smuzhiyun 			 * reach the end of link list, return success and
321*4882a593Smuzhiyun 			 * set address point to the starting address
322*4882a593Smuzhiyun 			 * of the image
323*4882a593Smuzhiyun 			 */
324*4882a593Smuzhiyun 			*validblockaddr = valid_addr;
325*4882a593Smuzhiyun 			/* skip first 2 bytes (link list pointer) */
326*4882a593Smuzhiyun 			*validblockaddr += 2;
327*4882a593Smuzhiyun 			return 0;
328*4882a593Smuzhiyun 		}
329*4882a593Smuzhiyun 		/* more in the link list, continue */
330*4882a593Smuzhiyun 		usedblocks++;
331*4882a593Smuzhiyun 	} while (usedblocks <= trans->trans_cfg->base_params->max_ll_items);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* OTP has no valid blocks */
334*4882a593Smuzhiyun 	IWL_DEBUG_EEPROM(trans->dev, "OTP has no valid blocks\n");
335*4882a593Smuzhiyun 	return -EINVAL;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun  * iwl_read_eeprom - read EEPROM contents
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * Load the EEPROM contents from adapter and return it
342*4882a593Smuzhiyun  * and its size.
343*4882a593Smuzhiyun  *
344*4882a593Smuzhiyun  * NOTE:  This routine uses the non-debug IO access functions.
345*4882a593Smuzhiyun  */
iwl_read_eeprom(struct iwl_trans * trans,u8 ** eeprom,size_t * eeprom_size)346*4882a593Smuzhiyun int iwl_read_eeprom(struct iwl_trans *trans, u8 **eeprom, size_t *eeprom_size)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	__le16 *e;
349*4882a593Smuzhiyun 	u32 gp = iwl_read32(trans, CSR_EEPROM_GP);
350*4882a593Smuzhiyun 	int sz;
351*4882a593Smuzhiyun 	int ret;
352*4882a593Smuzhiyun 	u16 addr;
353*4882a593Smuzhiyun 	u16 validblockaddr = 0;
354*4882a593Smuzhiyun 	u16 cache_addr = 0;
355*4882a593Smuzhiyun 	int nvm_is_otp;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	if (!eeprom || !eeprom_size)
358*4882a593Smuzhiyun 		return -EINVAL;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	nvm_is_otp = iwl_nvm_is_otp(trans);
361*4882a593Smuzhiyun 	if (nvm_is_otp < 0)
362*4882a593Smuzhiyun 		return nvm_is_otp;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	sz = trans->trans_cfg->base_params->eeprom_size;
365*4882a593Smuzhiyun 	IWL_DEBUG_EEPROM(trans->dev, "NVM size = %d\n", sz);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	e = kmalloc(sz, GFP_KERNEL);
368*4882a593Smuzhiyun 	if (!e)
369*4882a593Smuzhiyun 		return -ENOMEM;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	ret = iwl_eeprom_verify_signature(trans, nvm_is_otp);
372*4882a593Smuzhiyun 	if (ret < 0) {
373*4882a593Smuzhiyun 		IWL_ERR(trans, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
374*4882a593Smuzhiyun 		goto err_free;
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	/* Make sure driver (instead of uCode) is allowed to read EEPROM */
378*4882a593Smuzhiyun 	ret = iwl_eeprom_acquire_semaphore(trans);
379*4882a593Smuzhiyun 	if (ret < 0) {
380*4882a593Smuzhiyun 		IWL_ERR(trans, "Failed to acquire EEPROM semaphore.\n");
381*4882a593Smuzhiyun 		goto err_free;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	if (nvm_is_otp) {
385*4882a593Smuzhiyun 		ret = iwl_init_otp_access(trans);
386*4882a593Smuzhiyun 		if (ret) {
387*4882a593Smuzhiyun 			IWL_ERR(trans, "Failed to initialize OTP access.\n");
388*4882a593Smuzhiyun 			goto err_unlock;
389*4882a593Smuzhiyun 		}
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 		iwl_write32(trans, CSR_EEPROM_GP,
392*4882a593Smuzhiyun 			    iwl_read32(trans, CSR_EEPROM_GP) &
393*4882a593Smuzhiyun 			    ~CSR_EEPROM_GP_IF_OWNER_MSK);
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 		iwl_set_bit(trans, CSR_OTP_GP_REG,
396*4882a593Smuzhiyun 			    CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
397*4882a593Smuzhiyun 			    CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
398*4882a593Smuzhiyun 		/* traversing the linked list if no shadow ram supported */
399*4882a593Smuzhiyun 		if (!trans->trans_cfg->base_params->shadow_ram_support) {
400*4882a593Smuzhiyun 			ret = iwl_find_otp_image(trans, &validblockaddr);
401*4882a593Smuzhiyun 			if (ret)
402*4882a593Smuzhiyun 				goto err_unlock;
403*4882a593Smuzhiyun 		}
404*4882a593Smuzhiyun 		for (addr = validblockaddr; addr < validblockaddr + sz;
405*4882a593Smuzhiyun 		     addr += sizeof(u16)) {
406*4882a593Smuzhiyun 			__le16 eeprom_data;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 			ret = iwl_read_otp_word(trans, addr, &eeprom_data);
409*4882a593Smuzhiyun 			if (ret)
410*4882a593Smuzhiyun 				goto err_unlock;
411*4882a593Smuzhiyun 			e[cache_addr / 2] = eeprom_data;
412*4882a593Smuzhiyun 			cache_addr += sizeof(u16);
413*4882a593Smuzhiyun 		}
414*4882a593Smuzhiyun 	} else {
415*4882a593Smuzhiyun 		/* eeprom is an array of 16bit values */
416*4882a593Smuzhiyun 		for (addr = 0; addr < sz; addr += sizeof(u16)) {
417*4882a593Smuzhiyun 			u32 r;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 			iwl_write32(trans, CSR_EEPROM_REG,
420*4882a593Smuzhiyun 				    CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 			ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
423*4882a593Smuzhiyun 					   CSR_EEPROM_REG_READ_VALID_MSK,
424*4882a593Smuzhiyun 					   CSR_EEPROM_REG_READ_VALID_MSK,
425*4882a593Smuzhiyun 					   IWL_EEPROM_ACCESS_TIMEOUT);
426*4882a593Smuzhiyun 			if (ret < 0) {
427*4882a593Smuzhiyun 				IWL_ERR(trans,
428*4882a593Smuzhiyun 					"Time out reading EEPROM[%d]\n", addr);
429*4882a593Smuzhiyun 				goto err_unlock;
430*4882a593Smuzhiyun 			}
431*4882a593Smuzhiyun 			r = iwl_read32(trans, CSR_EEPROM_REG);
432*4882a593Smuzhiyun 			e[addr / 2] = cpu_to_le16(r >> 16);
433*4882a593Smuzhiyun 		}
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	IWL_DEBUG_EEPROM(trans->dev, "NVM Type: %s\n",
437*4882a593Smuzhiyun 			 nvm_is_otp ? "OTP" : "EEPROM");
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	iwl_eeprom_release_semaphore(trans);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	*eeprom_size = sz;
442*4882a593Smuzhiyun 	*eeprom = (u8 *)e;
443*4882a593Smuzhiyun 	return 0;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun  err_unlock:
446*4882a593Smuzhiyun 	iwl_eeprom_release_semaphore(trans);
447*4882a593Smuzhiyun  err_free:
448*4882a593Smuzhiyun 	kfree(e);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	return ret;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun IWL_EXPORT_SYMBOL(iwl_read_eeprom);
453