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.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
63 #include <linux/firmware.h>
64 #include "iwl-trans.h"
66 #include "iwl-eeprom-parse.h"
67 #include "iwl-eeprom-read.h"
68 #include "iwl-nvm-parse.h"
70 /* list of NVM sections we are allowed/need to read */
71 static const int nvm_to_read
[] = {
74 NVM_SECTION_TYPE_CALIBRATION
,
75 NVM_SECTION_TYPE_PRODUCTION
,
78 /* Default NVM size to read */
79 #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
80 #define IWL_MAX_NVM_SECTION_SIZE 7000
82 #define NVM_WRITE_OPCODE 1
83 #define NVM_READ_OPCODE 0
86 * prepare the NVM host command w/ the pointers to the nvm buffer
89 static int iwl_nvm_write_chunk(struct iwl_mvm
*mvm
, u16 section
,
90 u16 offset
, u16 length
, const u8
*data
)
92 struct iwl_nvm_access_cmd nvm_access_cmd
= {
93 .offset
= cpu_to_le16(offset
),
94 .length
= cpu_to_le16(length
),
95 .type
= cpu_to_le16(section
),
96 .op_code
= NVM_WRITE_OPCODE
,
98 struct iwl_host_cmd cmd
= {
100 .len
= { sizeof(struct iwl_nvm_access_cmd
), length
},
101 .flags
= CMD_SYNC
| CMD_SEND_IN_RFKILL
,
102 .data
= { &nvm_access_cmd
, data
},
103 /* data may come from vmalloc, so use _DUP */
104 .dataflags
= { 0, IWL_HCMD_DFL_DUP
},
107 return iwl_mvm_send_cmd(mvm
, &cmd
);
110 static int iwl_nvm_read_chunk(struct iwl_mvm
*mvm
, u16 section
,
111 u16 offset
, u16 length
, u8
*data
)
113 struct iwl_nvm_access_cmd nvm_access_cmd
= {
114 .offset
= cpu_to_le16(offset
),
115 .length
= cpu_to_le16(length
),
116 .type
= cpu_to_le16(section
),
117 .op_code
= NVM_READ_OPCODE
,
119 struct iwl_nvm_access_resp
*nvm_resp
;
120 struct iwl_rx_packet
*pkt
;
121 struct iwl_host_cmd cmd
= {
122 .id
= NVM_ACCESS_CMD
,
123 .flags
= CMD_SYNC
| CMD_WANT_SKB
| CMD_SEND_IN_RFKILL
,
124 .data
= { &nvm_access_cmd
, },
126 int ret
, bytes_read
, offset_read
;
129 cmd
.len
[0] = sizeof(struct iwl_nvm_access_cmd
);
131 ret
= iwl_mvm_send_cmd(mvm
, &cmd
);
136 if (pkt
->hdr
.flags
& IWL_CMD_FAILED_MSK
) {
137 IWL_ERR(mvm
, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n",
143 /* Extract NVM response */
144 nvm_resp
= (void *)pkt
->data
;
145 ret
= le16_to_cpu(nvm_resp
->status
);
146 bytes_read
= le16_to_cpu(nvm_resp
->length
);
147 offset_read
= le16_to_cpu(nvm_resp
->offset
);
148 resp_data
= nvm_resp
->data
;
151 "NVM access command failed with status %d (device: %s)\n",
152 ret
, mvm
->cfg
->name
);
157 if (offset_read
!= offset
) {
158 IWL_ERR(mvm
, "NVM ACCESS response with invalid offset %d\n",
164 /* Write data to NVM */
165 memcpy(data
+ offset
, resp_data
, bytes_read
);
173 static int iwl_nvm_write_section(struct iwl_mvm
*mvm
, u16 section
,
174 const u8
*data
, u16 length
)
178 /* copy data in chunks of 2k (and remainder if any) */
180 while (offset
< length
) {
183 chunk_size
= min(IWL_NVM_DEFAULT_CHUNK_SIZE
,
186 ret
= iwl_nvm_write_chunk(mvm
, section
, offset
,
187 chunk_size
, data
+ offset
);
191 offset
+= chunk_size
;
198 * Reads an NVM section completely.
199 * NICs prior to 7000 family doesn't have a real NVM, but just read
200 * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
201 * by uCode, we need to manually check in this case that we don't
202 * overflow and try to read more than the EEPROM size.
203 * For 7000 family NICs, we supply the maximal size we can read, and
204 * the uCode fills the response with as much data as we can,
205 * without overflowing, so no check is needed.
207 static int iwl_nvm_read_section(struct iwl_mvm
*mvm
, u16 section
,
210 u16 length
, offset
= 0;
213 /* Set nvm section read length */
214 length
= IWL_NVM_DEFAULT_CHUNK_SIZE
;
218 /* Read the NVM until exhausted (reading less than requested) */
219 while (ret
== length
) {
220 ret
= iwl_nvm_read_chunk(mvm
, section
, offset
, length
, data
);
223 "Cannot read NVM from section %d offset %d, length %d\n",
224 section
, offset
, length
);
230 IWL_DEBUG_EEPROM(mvm
->trans
->dev
,
231 "NVM section %d read completed\n", section
);
235 static struct iwl_nvm_data
*
236 iwl_parse_nvm_sections(struct iwl_mvm
*mvm
)
238 struct iwl_nvm_section
*sections
= mvm
->nvm_sections
;
239 const __le16
*hw
, *sw
, *calib
;
241 /* Checking for required sections */
242 if (!mvm
->nvm_sections
[NVM_SECTION_TYPE_SW
].data
||
243 !mvm
->nvm_sections
[NVM_SECTION_TYPE_HW
].data
) {
244 IWL_ERR(mvm
, "Can't parse empty NVM sections\n");
248 if (WARN_ON(!mvm
->cfg
))
251 hw
= (const __le16
*)sections
[NVM_SECTION_TYPE_HW
].data
;
252 sw
= (const __le16
*)sections
[NVM_SECTION_TYPE_SW
].data
;
253 calib
= (const __le16
*)sections
[NVM_SECTION_TYPE_CALIBRATION
].data
;
254 return iwl_parse_nvm_data(mvm
->trans
->dev
, mvm
->cfg
, hw
, sw
, calib
,
255 iwl_fw_valid_tx_ant(mvm
->fw
),
256 iwl_fw_valid_rx_ant(mvm
->fw
));
259 #define MAX_NVM_FILE_LEN 16384
262 * Reads external NVM from a file into mvm->nvm_sections
264 * HOW TO CREATE THE NVM FILE FORMAT:
265 * ------------------------------
266 * 1. create hex file, format:
271 * rev - 6 bit (word1)
272 * len - 10 bit (word1)
274 * rsv - 12 bit (word2)
276 * 2. flip 8bits with 8 bits per line to get the right NVM file format
278 * 3. create binary file from the hex file
280 * 4. save as "iNVM_xxx.bin" under /lib/firmware
282 static int iwl_mvm_read_external_nvm(struct iwl_mvm
*mvm
)
284 int ret
, section_size
;
286 const struct firmware
*fw_entry
;
292 const u8
*eof
, *temp
;
294 #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
295 #define NVM_WORD2_ID(x) (x >> 12)
297 IWL_DEBUG_EEPROM(mvm
->trans
->dev
, "Read from external NVM\n");
300 * Obtain NVM image via request_firmware. Since we already used
301 * request_firmware_nowait() for the firmware binary load and only
302 * get here after that we assume the NVM request can be satisfied
305 ret
= request_firmware(&fw_entry
, iwlwifi_mod_params
.nvm_file
,
308 IWL_ERR(mvm
, "ERROR: %s isn't available %d\n",
309 iwlwifi_mod_params
.nvm_file
, ret
);
313 IWL_INFO(mvm
, "Loaded NVM file %s (%zu bytes)\n",
314 iwlwifi_mod_params
.nvm_file
, fw_entry
->size
);
316 if (fw_entry
->size
< sizeof(*file_sec
)) {
317 IWL_ERR(mvm
, "NVM file too small\n");
322 if (fw_entry
->size
> MAX_NVM_FILE_LEN
) {
323 IWL_ERR(mvm
, "NVM file too large\n");
328 eof
= fw_entry
->data
+ fw_entry
->size
;
330 file_sec
= (void *)fw_entry
->data
;
333 if (file_sec
->data
> eof
) {
335 "ERROR - NVM file too short for section header\n");
340 /* check for EOF marker */
341 if (!file_sec
->word1
&& !file_sec
->word2
) {
346 section_size
= 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec
->word1
));
347 section_id
= NVM_WORD2_ID(le16_to_cpu(file_sec
->word2
));
349 if (section_size
> IWL_MAX_NVM_SECTION_SIZE
) {
350 IWL_ERR(mvm
, "ERROR - section too large (%d)\n",
357 IWL_ERR(mvm
, "ERROR - section empty\n");
362 if (file_sec
->data
+ section_size
> eof
) {
364 "ERROR - NVM file too short for section (%d bytes)\n",
370 if (WARN(section_id
>= NVM_NUM_OF_SECTIONS
,
371 "Invalid NVM section ID %d\n", section_id
)) {
376 temp
= kmemdup(file_sec
->data
, section_size
, GFP_KERNEL
);
381 mvm
->nvm_sections
[section_id
].data
= temp
;
382 mvm
->nvm_sections
[section_id
].length
= section_size
;
384 /* advance to the next section */
385 file_sec
= (void *)(file_sec
->data
+ section_size
);
388 release_firmware(fw_entry
);
392 /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
393 int iwl_mvm_load_nvm_to_nic(struct iwl_mvm
*mvm
)
396 struct iwl_nvm_section
*sections
= mvm
->nvm_sections
;
398 IWL_DEBUG_EEPROM(mvm
->trans
->dev
, "'Write to NVM\n");
400 for (i
= 0; i
< ARRAY_SIZE(mvm
->nvm_sections
); i
++) {
401 if (!mvm
->nvm_sections
[i
].data
|| !mvm
->nvm_sections
[i
].length
)
403 ret
= iwl_nvm_write_section(mvm
, i
, sections
[i
].data
,
406 IWL_ERR(mvm
, "iwl_mvm_send_cmd failed: %d\n", ret
);
413 int iwl_nvm_init(struct iwl_mvm
*mvm
)
416 u8
*nvm_buffer
, *temp
;
418 /* load external NVM if configured */
419 if (iwlwifi_mod_params
.nvm_file
) {
420 /* move to External NVM flow */
421 ret
= iwl_mvm_read_external_nvm(mvm
);
425 /* Read From FW NVM */
426 IWL_DEBUG_EEPROM(mvm
->trans
->dev
, "Read from NVM\n");
428 /* TODO: find correct NVM max size for a section */
429 nvm_buffer
= kmalloc(mvm
->cfg
->base_params
->eeprom_size
,
433 for (i
= 0; i
< ARRAY_SIZE(nvm_to_read
); i
++) {
434 section
= nvm_to_read
[i
];
435 /* we override the constness for initial read */
436 ret
= iwl_nvm_read_section(mvm
, section
, nvm_buffer
);
439 temp
= kmemdup(nvm_buffer
, ret
, GFP_KERNEL
);
444 mvm
->nvm_sections
[section
].data
= temp
;
445 mvm
->nvm_sections
[section
].length
= ret
;
447 #ifdef CONFIG_IWLWIFI_DEBUGFS
449 case NVM_SECTION_TYPE_HW
:
450 mvm
->nvm_hw_blob
.data
= temp
;
451 mvm
->nvm_hw_blob
.size
= ret
;
453 case NVM_SECTION_TYPE_SW
:
454 mvm
->nvm_sw_blob
.data
= temp
;
455 mvm
->nvm_sw_blob
.size
= ret
;
457 case NVM_SECTION_TYPE_CALIBRATION
:
458 mvm
->nvm_calib_blob
.data
= temp
;
459 mvm
->nvm_calib_blob
.size
= ret
;
461 case NVM_SECTION_TYPE_PRODUCTION
:
462 mvm
->nvm_prod_blob
.data
= temp
;
463 mvm
->nvm_prod_blob
.size
= ret
;
466 WARN(1, "section: %d", section
);
475 mvm
->nvm_data
= iwl_parse_nvm_sections(mvm
);