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) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
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 <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
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.
64 *****************************************************************************/
65 #include <linux/firmware.h>
66 #include "iwl-trans.h"
69 #include "iwl-eeprom-parse.h"
70 #include "iwl-eeprom-read.h"
71 #include "iwl-nvm-parse.h"
73 /* Default NVM size to read */
74 #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
75 #define IWL_MAX_NVM_SECTION_SIZE 0x1b58
76 #define IWL_MAX_NVM_8000A_SECTION_SIZE 0xffc
77 #define IWL_MAX_NVM_8000B_SECTION_SIZE 0x1ffc
79 #define NVM_WRITE_OPCODE 1
80 #define NVM_READ_OPCODE 0
82 /* load nvm chunk response */
84 READ_NVM_CHUNK_SUCCEED
= 0,
85 READ_NVM_CHUNK_NOT_VALID_ADDRESS
= 1
89 * prepare the NVM host command w/ the pointers to the nvm buffer
92 static int iwl_nvm_write_chunk(struct iwl_mvm
*mvm
, u16 section
,
93 u16 offset
, u16 length
, const u8
*data
)
95 struct iwl_nvm_access_cmd nvm_access_cmd
= {
96 .offset
= cpu_to_le16(offset
),
97 .length
= cpu_to_le16(length
),
98 .type
= cpu_to_le16(section
),
99 .op_code
= NVM_WRITE_OPCODE
,
101 struct iwl_host_cmd cmd
= {
102 .id
= NVM_ACCESS_CMD
,
103 .len
= { sizeof(struct iwl_nvm_access_cmd
), length
},
104 .flags
= CMD_SEND_IN_RFKILL
,
105 .data
= { &nvm_access_cmd
, data
},
106 /* data may come from vmalloc, so use _DUP */
107 .dataflags
= { 0, IWL_HCMD_DFL_DUP
},
110 return iwl_mvm_send_cmd(mvm
, &cmd
);
113 static int iwl_nvm_read_chunk(struct iwl_mvm
*mvm
, u16 section
,
114 u16 offset
, u16 length
, u8
*data
)
116 struct iwl_nvm_access_cmd nvm_access_cmd
= {
117 .offset
= cpu_to_le16(offset
),
118 .length
= cpu_to_le16(length
),
119 .type
= cpu_to_le16(section
),
120 .op_code
= NVM_READ_OPCODE
,
122 struct iwl_nvm_access_resp
*nvm_resp
;
123 struct iwl_rx_packet
*pkt
;
124 struct iwl_host_cmd cmd
= {
125 .id
= NVM_ACCESS_CMD
,
126 .flags
= CMD_WANT_SKB
| CMD_SEND_IN_RFKILL
,
127 .data
= { &nvm_access_cmd
, },
129 int ret
, bytes_read
, offset_read
;
132 cmd
.len
[0] = sizeof(struct iwl_nvm_access_cmd
);
134 ret
= iwl_mvm_send_cmd(mvm
, &cmd
);
139 if (pkt
->hdr
.flags
& IWL_CMD_FAILED_MSK
) {
140 IWL_ERR(mvm
, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n",
146 /* Extract NVM response */
147 nvm_resp
= (void *)pkt
->data
;
148 ret
= le16_to_cpu(nvm_resp
->status
);
149 bytes_read
= le16_to_cpu(nvm_resp
->length
);
150 offset_read
= le16_to_cpu(nvm_resp
->offset
);
151 resp_data
= nvm_resp
->data
;
154 (ret
== READ_NVM_CHUNK_NOT_VALID_ADDRESS
)) {
156 * meaning of NOT_VALID_ADDRESS:
157 * driver try to read chunk from address that is
158 * multiple of 2K and got an error since addr is empty.
159 * meaning of (offset != 0): driver already
160 * read valid data from another chunk so this case
163 IWL_DEBUG_EEPROM(mvm
->trans
->dev
,
164 "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
168 IWL_DEBUG_EEPROM(mvm
->trans
->dev
,
169 "NVM access command failed with status %d (device: %s)\n",
170 ret
, mvm
->cfg
->name
);
176 if (offset_read
!= offset
) {
177 IWL_ERR(mvm
, "NVM ACCESS response with invalid offset %d\n",
183 /* Write data to NVM */
184 memcpy(data
+ offset
, resp_data
, bytes_read
);
192 static int iwl_nvm_write_section(struct iwl_mvm
*mvm
, u16 section
,
193 const u8
*data
, u16 length
)
197 /* copy data in chunks of 2k (and remainder if any) */
199 while (offset
< length
) {
202 chunk_size
= min(IWL_NVM_DEFAULT_CHUNK_SIZE
,
205 ret
= iwl_nvm_write_chunk(mvm
, section
, offset
,
206 chunk_size
, data
+ offset
);
210 offset
+= chunk_size
;
217 * Reads an NVM section completely.
218 * NICs prior to 7000 family doesn't have a real NVM, but just read
219 * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
220 * by uCode, we need to manually check in this case that we don't
221 * overflow and try to read more than the EEPROM size.
222 * For 7000 family NICs, we supply the maximal size we can read, and
223 * the uCode fills the response with as much data as we can,
224 * without overflowing, so no check is needed.
226 static int iwl_nvm_read_section(struct iwl_mvm
*mvm
, u16 section
,
227 u8
*data
, u32 size_read
)
229 u16 length
, offset
= 0;
232 /* Set nvm section read length */
233 length
= IWL_NVM_DEFAULT_CHUNK_SIZE
;
237 /* Read the NVM until exhausted (reading less than requested) */
238 while (ret
== length
) {
239 /* Check no memory assumptions fail and cause an overflow */
240 if ((size_read
+ offset
+ length
) >
241 mvm
->cfg
->base_params
->eeprom_size
) {
242 IWL_ERR(mvm
, "EEPROM size is too small for NVM\n");
246 ret
= iwl_nvm_read_chunk(mvm
, section
, offset
, length
, data
);
248 IWL_DEBUG_EEPROM(mvm
->trans
->dev
,
249 "Cannot read NVM from section %d offset %d, length %d\n",
250 section
, offset
, length
);
256 IWL_DEBUG_EEPROM(mvm
->trans
->dev
,
257 "NVM section %d read completed\n", section
);
261 static struct iwl_nvm_data
*
262 iwl_parse_nvm_sections(struct iwl_mvm
*mvm
)
264 struct iwl_nvm_section
*sections
= mvm
->nvm_sections
;
265 const __le16
*hw
, *sw
, *calib
, *regulatory
, *mac_override
;
267 /* Checking for required sections */
268 if (mvm
->trans
->cfg
->device_family
!= IWL_DEVICE_FAMILY_8000
) {
269 if (!mvm
->nvm_sections
[NVM_SECTION_TYPE_SW
].data
||
270 !mvm
->nvm_sections
[mvm
->cfg
->nvm_hw_section_num
].data
) {
271 IWL_ERR(mvm
, "Can't parse empty OTP/NVM sections\n");
275 /* SW and REGULATORY sections are mandatory */
276 if (!mvm
->nvm_sections
[NVM_SECTION_TYPE_SW
].data
||
277 !mvm
->nvm_sections
[NVM_SECTION_TYPE_REGULATORY
].data
) {
279 "Can't parse empty family 8000 OTP/NVM sections\n");
282 /* MAC_OVERRIDE or at least HW section must exist */
283 if (!mvm
->nvm_sections
[mvm
->cfg
->nvm_hw_section_num
].data
&&
284 !mvm
->nvm_sections
[NVM_SECTION_TYPE_MAC_OVERRIDE
].data
) {
286 "Can't parse mac_address, empty sections\n");
291 if (WARN_ON(!mvm
->cfg
))
294 hw
= (const __le16
*)sections
[mvm
->cfg
->nvm_hw_section_num
].data
;
295 sw
= (const __le16
*)sections
[NVM_SECTION_TYPE_SW
].data
;
296 calib
= (const __le16
*)sections
[NVM_SECTION_TYPE_CALIBRATION
].data
;
297 regulatory
= (const __le16
*)sections
[NVM_SECTION_TYPE_REGULATORY
].data
;
299 (const __le16
*)sections
[NVM_SECTION_TYPE_MAC_OVERRIDE
].data
;
301 return iwl_parse_nvm_data(mvm
->trans
->dev
, mvm
->cfg
, hw
, sw
, calib
,
302 regulatory
, mac_override
,
303 mvm
->fw
->valid_tx_ant
,
304 mvm
->fw
->valid_rx_ant
);
307 #define MAX_NVM_FILE_LEN 16384
310 * Reads external NVM from a file into mvm->nvm_sections
312 * HOW TO CREATE THE NVM FILE FORMAT:
313 * ------------------------------
314 * 1. create hex file, format:
319 * rev - 6 bit (word1)
320 * len - 10 bit (word1)
322 * rsv - 12 bit (word2)
324 * 2. flip 8bits with 8 bits per line to get the right NVM file format
326 * 3. create binary file from the hex file
328 * 4. save as "iNVM_xxx.bin" under /lib/firmware
330 static int iwl_mvm_read_external_nvm(struct iwl_mvm
*mvm
)
332 int ret
, section_size
;
334 const struct firmware
*fw_entry
;
340 const u8
*eof
, *temp
;
341 int max_section_size
;
342 const __le32
*dword_buff
;
344 #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
345 #define NVM_WORD2_ID(x) (x >> 12)
346 #define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8))
347 #define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4)
348 #define NVM_HEADER_0 (0x2A504C54)
349 #define NVM_HEADER_1 (0x4E564D2A)
350 #define NVM_HEADER_SIZE (4 * sizeof(u32))
352 IWL_DEBUG_EEPROM(mvm
->trans
->dev
, "Read from external NVM\n");
354 /* Maximal size depends on HW family and step */
355 if (mvm
->trans
->cfg
->device_family
!= IWL_DEVICE_FAMILY_8000
)
356 max_section_size
= IWL_MAX_NVM_SECTION_SIZE
;
357 else if (CSR_HW_REV_STEP(mvm
->trans
->hw_rev
) == SILICON_A_STEP
)
358 max_section_size
= IWL_MAX_NVM_8000A_SECTION_SIZE
;
359 else /* Family 8000 B-step */
360 max_section_size
= IWL_MAX_NVM_8000B_SECTION_SIZE
;
363 * Obtain NVM image via request_firmware. Since we already used
364 * request_firmware_nowait() for the firmware binary load and only
365 * get here after that we assume the NVM request can be satisfied
368 ret
= request_firmware(&fw_entry
, mvm
->nvm_file_name
,
371 IWL_ERR(mvm
, "ERROR: %s isn't available %d\n",
372 mvm
->nvm_file_name
, ret
);
376 IWL_INFO(mvm
, "Loaded NVM file %s (%zu bytes)\n",
377 mvm
->nvm_file_name
, fw_entry
->size
);
379 if (fw_entry
->size
> MAX_NVM_FILE_LEN
) {
380 IWL_ERR(mvm
, "NVM file too large\n");
385 eof
= fw_entry
->data
+ fw_entry
->size
;
386 dword_buff
= (__le32
*)fw_entry
->data
;
388 /* some NVM file will contain a header.
389 * The header is identified by 2 dwords header as follow:
390 * dword[0] = 0x2A504C54
391 * dword[1] = 0x4E564D2A
393 * This header must be skipped when providing the NVM data to the FW.
395 if (fw_entry
->size
> NVM_HEADER_SIZE
&&
396 dword_buff
[0] == cpu_to_le32(NVM_HEADER_0
) &&
397 dword_buff
[1] == cpu_to_le32(NVM_HEADER_1
)) {
398 file_sec
= (void *)(fw_entry
->data
+ NVM_HEADER_SIZE
);
399 IWL_INFO(mvm
, "NVM Version %08X\n", le32_to_cpu(dword_buff
[2]));
400 IWL_INFO(mvm
, "NVM Manufacturing date %08X\n",
401 le32_to_cpu(dword_buff
[3]));
403 file_sec
= (void *)fw_entry
->data
;
407 if (file_sec
->data
> eof
) {
409 "ERROR - NVM file too short for section header\n");
414 /* check for EOF marker */
415 if (!file_sec
->word1
&& !file_sec
->word2
) {
420 if (mvm
->trans
->cfg
->device_family
!= IWL_DEVICE_FAMILY_8000
) {
422 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec
->word1
));
423 section_id
= NVM_WORD2_ID(le16_to_cpu(file_sec
->word2
));
425 section_size
= 2 * NVM_WORD2_LEN_FAMILY_8000(
426 le16_to_cpu(file_sec
->word2
));
427 section_id
= NVM_WORD1_ID_FAMILY_8000(
428 le16_to_cpu(file_sec
->word1
));
431 if (section_size
> max_section_size
) {
432 IWL_ERR(mvm
, "ERROR - section too large (%d)\n",
439 IWL_ERR(mvm
, "ERROR - section empty\n");
444 if (file_sec
->data
+ section_size
> eof
) {
446 "ERROR - NVM file too short for section (%d bytes)\n",
452 if (WARN(section_id
>= NVM_MAX_NUM_SECTIONS
,
453 "Invalid NVM section ID %d\n", section_id
)) {
458 temp
= kmemdup(file_sec
->data
, section_size
, GFP_KERNEL
);
463 mvm
->nvm_sections
[section_id
].data
= temp
;
464 mvm
->nvm_sections
[section_id
].length
= section_size
;
466 /* advance to the next section */
467 file_sec
= (void *)(file_sec
->data
+ section_size
);
470 release_firmware(fw_entry
);
474 /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
475 int iwl_mvm_load_nvm_to_nic(struct iwl_mvm
*mvm
)
478 struct iwl_nvm_section
*sections
= mvm
->nvm_sections
;
480 IWL_DEBUG_EEPROM(mvm
->trans
->dev
, "'Write to NVM\n");
482 for (i
= 0; i
< ARRAY_SIZE(mvm
->nvm_sections
); i
++) {
483 if (!mvm
->nvm_sections
[i
].data
|| !mvm
->nvm_sections
[i
].length
)
485 ret
= iwl_nvm_write_section(mvm
, i
, sections
[i
].data
,
488 IWL_ERR(mvm
, "iwl_mvm_send_cmd failed: %d\n", ret
);
495 int iwl_nvm_init(struct iwl_mvm
*mvm
, bool read_nvm_from_nic
)
499 u8
*nvm_buffer
, *temp
;
501 if (WARN_ON_ONCE(mvm
->cfg
->nvm_hw_section_num
>= NVM_MAX_NUM_SECTIONS
))
504 /* load NVM values from nic */
505 if (read_nvm_from_nic
) {
506 /* Read From FW NVM */
507 IWL_DEBUG_EEPROM(mvm
->trans
->dev
, "Read from NVM\n");
509 nvm_buffer
= kmalloc(mvm
->cfg
->base_params
->eeprom_size
,
513 for (section
= 0; section
< NVM_MAX_NUM_SECTIONS
; section
++) {
514 /* we override the constness for initial read */
515 ret
= iwl_nvm_read_section(mvm
, section
, nvm_buffer
,
520 temp
= kmemdup(nvm_buffer
, ret
, GFP_KERNEL
);
525 mvm
->nvm_sections
[section
].data
= temp
;
526 mvm
->nvm_sections
[section
].length
= ret
;
528 #ifdef CONFIG_IWLWIFI_DEBUGFS
530 case NVM_SECTION_TYPE_SW
:
531 mvm
->nvm_sw_blob
.data
= temp
;
532 mvm
->nvm_sw_blob
.size
= ret
;
534 case NVM_SECTION_TYPE_CALIBRATION
:
535 mvm
->nvm_calib_blob
.data
= temp
;
536 mvm
->nvm_calib_blob
.size
= ret
;
538 case NVM_SECTION_TYPE_PRODUCTION
:
539 mvm
->nvm_prod_blob
.data
= temp
;
540 mvm
->nvm_prod_blob
.size
= ret
;
543 if (section
== mvm
->cfg
->nvm_hw_section_num
) {
544 mvm
->nvm_hw_blob
.data
= temp
;
545 mvm
->nvm_hw_blob
.size
= ret
;
552 IWL_ERR(mvm
, "OTP is blank\n");
556 /* load external NVM if configured */
557 if (mvm
->nvm_file_name
) {
558 /* move to External NVM flow */
559 ret
= iwl_mvm_read_external_nvm(mvm
);
564 /* parse the relevant nvm sections */
565 mvm
->nvm_data
= iwl_parse_nvm_sections(mvm
);
568 IWL_DEBUG_EEPROM(mvm
->trans
->dev
, "nvm version = %x\n",
569 mvm
->nvm_data
->nvm_version
);