]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/iwlwifi/iwl-eeprom.c
iwlwifi: clean up iwl-shared.h includes
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / iwlwifi / iwl-eeprom.c
CommitLineData
34cf6ff6
AK
1/******************************************************************************
2 *
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.
5 *
6 * GPL LICENSE SUMMARY
7 *
4e318262 8 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
34cf6ff6
AK
9 *
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.
13 *
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.
18 *
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,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
759ef89f 28 * Intel Linux Wireless <ilw@linux.intel.com>
34cf6ff6
AK
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
4e318262 33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34cf6ff6
AK
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
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
45 * distribution.
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.
49 *
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.
61 *****************************************************************************/
62
63
64#include <linux/kernel.h>
65#include <linux/module.h>
5a0e3ad6 66#include <linux/slab.h>
34cf6ff6
AK
67#include <linux/init.h>
68
69#include <net/mac80211.h>
70
3e0d4cb1 71#include "iwl-dev.h"
34cf6ff6 72#include "iwl-core.h"
0a6857e7 73#include "iwl-debug.h"
701cb099 74#include "iwl-agn.h"
34cf6ff6 75#include "iwl-eeprom.h"
3395f6e9 76#include "iwl-io.h"
eae63b85 77#include "iwl-prph.h"
34cf6ff6 78
bf85ea4f
AK
79/************************** EEPROM BANDS ****************************
80 *
81 * The iwl_eeprom_band definitions below provide the mapping from the
82 * EEPROM contents to the specific channel number supported for each
83 * band.
84 *
85 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
86 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
87 * The specific geography and calibration information for that channel
88 * is contained in the eeprom map itself.
89 *
90 * During init, we copy the eeprom information and channel map
91 * information into priv->channel_info_24/52 and priv->channel_map_24/52
92 *
93 * channel_map_24/52 provides the index in the channel_info array for a
94 * given channel. We have to have two separate maps as there is channel
95 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
96 * band_2
97 *
98 * A value of 0xff stored in the channel_map indicates that the channel
99 * is not supported by the hardware at all.
100 *
101 * A value of 0xfe in the channel_map indicates that the channel is not
102 * valid for Tx with the current hardware. This means that
103 * while the system can tune and receive on a given channel, it may not
104 * be able to associate or transmit any frames on that
105 * channel. There is no corresponding channel information for that
106 * entry.
107 *
108 *********************************************************************/
109
110/* 2.4 GHz */
111const u8 iwl_eeprom_band_1[14] = {
112 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
113};
114
115/* 5.2 GHz bands */
116static const u8 iwl_eeprom_band_2[] = { /* 4915-5080MHz */
117 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
118};
119
120static const u8 iwl_eeprom_band_3[] = { /* 5170-5320MHz */
121 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
122};
123
124static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
125 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
126};
127
128static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
129 145, 149, 153, 157, 161, 165
130};
131
7aafef1c 132static const u8 iwl_eeprom_band_6[] = { /* 2.4 ht40 channel */
bf85ea4f
AK
133 1, 2, 3, 4, 5, 6, 7
134};
135
7aafef1c 136static const u8 iwl_eeprom_band_7[] = { /* 5.2 ht40 channel */
bf85ea4f
AK
137 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
138};
139
34cf6ff6
AK
140/******************************************************************************
141 *
701cb099 142 * generic NVM functions
34cf6ff6
AK
143 *
144******************************************************************************/
145
16b80b71
DF
146/*
147 * The device's EEPROM semaphore prevents conflicts between driver and uCode
148 * when accessing the EEPROM; each access is a series of pulses to/from the
149 * EEPROM chip, not a single event, so even reads could conflict if they
150 * weren't arbitrated by the semaphore.
151 */
90304749
EG
152
153#define EEPROM_SEM_TIMEOUT 10 /* milliseconds */
154#define EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
155
ca77d534 156static int iwl_eeprom_acquire_semaphore(struct iwl_trans *trans)
16b80b71
DF
157{
158 u16 count;
159 int ret;
160
161 for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
162 /* Request semaphore */
ca77d534 163 iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
16b80b71
DF
164 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
165
166 /* See if we got it */
ca77d534 167 ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
16b80b71
DF
168 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
169 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
170 EEPROM_SEM_TIMEOUT);
171 if (ret >= 0) {
ca77d534 172 IWL_DEBUG_EEPROM(trans,
16b80b71
DF
173 "Acquired semaphore after %d tries.\n",
174 count+1);
175 return ret;
176 }
177 }
178
179 return ret;
180}
181
ca77d534 182static void iwl_eeprom_release_semaphore(struct iwl_trans *trans)
16b80b71 183{
ca77d534 184 iwl_clear_bit(trans, CSR_HW_IF_CONFIG_REG,
16b80b71
DF
185 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
186
187}
188
11483b5c 189static int iwl_eeprom_verify_signature(struct iwl_priv *priv)
34cf6ff6 190{
68e8dfda 191 u32 gp = iwl_read32(priv->trans, CSR_EEPROM_GP) &
1042db2a 192 CSR_EEPROM_GP_VALID_MSK;
f41bb897
WYG
193 int ret = 0;
194
11483b5c 195 IWL_DEBUG_EEPROM(priv, "EEPROM signature=0x%08x\n", gp);
f41bb897
WYG
196 switch (gp) {
197 case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
11483b5c
JB
198 if (priv->nvm_device_type != NVM_DEVICE_TYPE_OTP) {
199 IWL_ERR(priv, "EEPROM with bad signature: 0x%08x\n",
f41bb897
WYG
200 gp);
201 ret = -ENOENT;
202 }
203 break;
204 case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
205 case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
11483b5c
JB
206 if (priv->nvm_device_type != NVM_DEVICE_TYPE_EEPROM) {
207 IWL_ERR(priv, "OTP with bad signature: 0x%08x\n", gp);
f41bb897
WYG
208 ret = -ENOENT;
209 }
210 break;
211 case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
212 default:
11483b5c 213 IWL_ERR(priv, "bad EEPROM/OTP signature, type=%s, "
f41bb897 214 "EEPROM_GP=0x%08x\n",
11483b5c 215 (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
f41bb897
WYG
216 ? "OTP" : "EEPROM", gp);
217 ret = -ENOENT;
218 break;
34cf6ff6 219 }
f41bb897 220 return ret;
34cf6ff6 221}
34cf6ff6 222
11483b5c 223u16 iwl_eeprom_query16(struct iwl_priv *priv, size_t offset)
701cb099 224{
11483b5c 225 if (!priv->eeprom)
701cb099 226 return 0;
11483b5c 227 return (u16)priv->eeprom[offset] | ((u16)priv->eeprom[offset + 1] << 8);
701cb099
WYG
228}
229
230int iwl_eeprom_check_version(struct iwl_priv *priv)
231{
232 u16 eeprom_ver;
233 u16 calib_ver;
234
11483b5c
JB
235 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
236 calib_ver = iwl_eeprom_calib_version(priv);
701cb099 237
2152268f
EG
238 if (eeprom_ver < priv->cfg->eeprom_ver ||
239 calib_ver < priv->cfg->eeprom_calib_ver)
701cb099
WYG
240 goto err;
241
242 IWL_INFO(priv, "device EEPROM VER=0x%x, CALIB=0x%x\n",
243 eeprom_ver, calib_ver);
244
245 return 0;
246err:
247 IWL_ERR(priv, "Unsupported (too old) EEPROM VER=0x%x < 0x%x "
248 "CALIB=0x%x < 0x%x\n",
2152268f
EG
249 eeprom_ver, priv->cfg->eeprom_ver,
250 calib_ver, priv->cfg->eeprom_calib_ver);
701cb099
WYG
251 return -EINVAL;
252
253}
254
54708d8d 255int iwl_eeprom_init_hw_params(struct iwl_priv *priv)
701cb099
WYG
256{
257 u16 radio_cfg;
258
11483b5c 259 priv->hw_params.sku = iwl_eeprom_query16(priv, EEPROM_SKU_CAP);
9e295116 260 if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE &&
2152268f 261 !priv->cfg->ht_params) {
54708d8d
JB
262 IWL_ERR(priv, "Invalid 11n configuration\n");
263 return -EINVAL;
701cb099 264 }
54708d8d 265
9e295116 266 if (!priv->hw_params.sku) {
701cb099
WYG
267 IWL_ERR(priv, "Invalid device sku\n");
268 return -EINVAL;
269 }
270
9e295116 271 IWL_INFO(priv, "Device SKU: 0x%X\n", priv->hw_params.sku);
701cb099 272
11483b5c 273 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
7e79a393 274
9e295116
JB
275 priv->hw_params.valid_tx_ant = EEPROM_RF_CFG_TX_ANT_MSK(radio_cfg);
276 priv->hw_params.valid_rx_ant = EEPROM_RF_CFG_RX_ANT_MSK(radio_cfg);
7e79a393
JB
277
278 /* check overrides (some devices have wrong EEPROM) */
2152268f
EG
279 if (priv->cfg->valid_tx_ant)
280 priv->hw_params.valid_tx_ant = priv->cfg->valid_tx_ant;
281 if (priv->cfg->valid_rx_ant)
282 priv->hw_params.valid_rx_ant = priv->cfg->valid_rx_ant;
7e79a393 283
9e295116 284 if (!priv->hw_params.valid_tx_ant || !priv->hw_params.valid_rx_ant) {
7e79a393 285 IWL_ERR(priv, "Invalid chain (0x%X, 0x%X)\n",
9e295116
JB
286 priv->hw_params.valid_tx_ant,
287 priv->hw_params.valid_rx_ant);
7e79a393 288 return -EINVAL;
701cb099 289 }
7e79a393
JB
290
291 IWL_INFO(priv, "Valid Tx ant: 0x%X, Valid Rx ant: 0x%X\n",
9e295116 292 priv->hw_params.valid_tx_ant, priv->hw_params.valid_rx_ant);
7e79a393 293
701cb099
WYG
294 return 0;
295}
296
11483b5c 297u16 iwl_eeprom_calib_version(struct iwl_priv *priv)
701cb099 298{
11483b5c
JB
299 struct iwl_eeprom_calib_hdr *hdr;
300
301 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
302 EEPROM_CALIB_ALL);
303 return hdr->version;
304}
305
306static u32 eeprom_indirect_address(struct iwl_priv *priv, u32 address)
307{
308 u16 offset = 0;
309
310 if ((address & INDIRECT_ADDRESS) == 0)
311 return address;
312
313 switch (address & INDIRECT_TYPE_MSK) {
314 case INDIRECT_HOST:
315 offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
316 break;
317 case INDIRECT_GENERAL:
318 offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
319 break;
320 case INDIRECT_REGULATORY:
321 offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
322 break;
323 case INDIRECT_TXP_LIMIT:
324 offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT);
325 break;
326 case INDIRECT_TXP_LIMIT_SIZE:
327 offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT_SIZE);
328 break;
329 case INDIRECT_CALIBRATION:
330 offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
331 break;
332 case INDIRECT_PROCESS_ADJST:
333 offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
334 break;
335 case INDIRECT_OTHERS:
336 offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
337 break;
338 default:
339 IWL_ERR(priv, "illegal indirect type: 0x%X\n",
340 address & INDIRECT_TYPE_MSK);
341 break;
342 }
343
344 /* translate the offset from words to byte */
345 return (address & ADDRESS_MSK) + (offset << 1);
346}
347
348const u8 *iwl_eeprom_query_addr(struct iwl_priv *priv, size_t offset)
349{
350 u32 address = eeprom_indirect_address(priv, offset);
2152268f 351 BUG_ON(address >= priv->cfg->base_params->eeprom_size);
11483b5c
JB
352 return &priv->eeprom[address];
353}
354
355void iwl_eeprom_get_mac(struct iwl_priv *priv, u8 *mac)
356{
357 const u8 *addr = iwl_eeprom_query_addr(priv,
701cb099
WYG
358 EEPROM_MAC_ADDRESS);
359 memcpy(mac, addr, ETH_ALEN);
360}
361
362/******************************************************************************
363 *
364 * OTP related functions
365 *
366******************************************************************************/
367
ca77d534
EG
368static void iwl_set_otp_access(struct iwl_trans *trans,
369 enum iwl_access_mode mode)
415e4993 370{
ca77d534 371 iwl_read32(trans, CSR_OTP_GP_REG);
415e4993 372
415e4993 373 if (mode == IWL_OTP_ACCESS_ABSOLUTE)
ca77d534 374 iwl_clear_bit(trans, CSR_OTP_GP_REG,
70817b5e 375 CSR_OTP_GP_REG_OTP_ACCESS_MODE);
415e4993 376 else
ca77d534 377 iwl_set_bit(trans, CSR_OTP_GP_REG,
70817b5e 378 CSR_OTP_GP_REG_OTP_ACCESS_MODE);
415e4993
WYG
379}
380
ca77d534 381static int iwl_get_nvm_type(struct iwl_trans *trans, u32 hw_rev)
0848e297
WYG
382{
383 u32 otpgp;
384 int nvm_type;
385
386 /* OTP only valid for CP/PP and after */
e98a1302 387 switch (hw_rev & CSR_HW_REV_TYPE_MSK) {
b23a0524 388 case CSR_HW_REV_TYPE_NONE:
ca77d534 389 IWL_ERR(trans, "Unknown hardware type\n");
b23a0524 390 return -ENOENT;
0848e297
WYG
391 case CSR_HW_REV_TYPE_5300:
392 case CSR_HW_REV_TYPE_5350:
393 case CSR_HW_REV_TYPE_5100:
394 case CSR_HW_REV_TYPE_5150:
395 nvm_type = NVM_DEVICE_TYPE_EEPROM;
396 break;
397 default:
ca77d534 398 otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
0848e297
WYG
399 if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
400 nvm_type = NVM_DEVICE_TYPE_OTP;
401 else
402 nvm_type = NVM_DEVICE_TYPE_EEPROM;
403 break;
404 }
405 return nvm_type;
406}
407
ca77d534 408static int iwl_init_otp_access(struct iwl_trans *trans)
0848e297
WYG
409{
410 int ret;
411
412 /* Enable 40MHz radio clock */
ca77d534
EG
413 iwl_write32(trans, CSR_GP_CNTRL,
414 iwl_read32(trans, CSR_GP_CNTRL) |
02a7fa00 415 CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
0848e297
WYG
416
417 /* wait for clock to be ready */
ca77d534 418 ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
02a7fa00
JB
419 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
420 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
421 25000);
0848e297 422 if (ret < 0)
ca77d534 423 IWL_ERR(trans, "Time out access OTP\n");
0848e297 424 else {
ca77d534 425 iwl_set_bits_prph(trans, APMG_PS_CTRL_REG,
d77b034f
RC
426 APMG_PS_CTRL_VAL_RESET_REQ);
427 udelay(5);
ca77d534 428 iwl_clear_bits_prph(trans, APMG_PS_CTRL_REG,
d77b034f 429 APMG_PS_CTRL_VAL_RESET_REQ);
32004ee4
WYG
430
431 /*
432 * CSR auto clock gate disable bit -
433 * this is only applicable for HW with OTP shadow RAM
434 */
035f7ff2 435 if (trans->cfg->base_params->shadow_ram_support)
ca77d534 436 iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
32004ee4 437 CSR_RESET_LINK_PWR_MGMT_DISABLED);
0848e297
WYG
438 }
439 return ret;
440}
441
ca77d534
EG
442static int iwl_read_otp_word(struct iwl_trans *trans, u16 addr,
443 __le16 *eeprom_data)
415e4993
WYG
444{
445 int ret = 0;
446 u32 r;
447 u32 otpgp;
448
ca77d534 449 iwl_write32(trans, CSR_EEPROM_REG,
02a7fa00 450 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
ca77d534 451 ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
02a7fa00
JB
452 CSR_EEPROM_REG_READ_VALID_MSK,
453 CSR_EEPROM_REG_READ_VALID_MSK,
454 IWL_EEPROM_ACCESS_TIMEOUT);
415e4993 455 if (ret < 0) {
ca77d534 456 IWL_ERR(trans, "Time out reading OTP[%d]\n", addr);
415e4993
WYG
457 return ret;
458 }
ca77d534 459 r = iwl_read32(trans, CSR_EEPROM_REG);
415e4993 460 /* check for ECC errors: */
ca77d534 461 otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
415e4993
WYG
462 if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
463 /* stop in this case */
464 /* set the uncorrectable OTP ECC bit for acknowledgement */
ca77d534 465 iwl_set_bit(trans, CSR_OTP_GP_REG,
415e4993 466 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
ca77d534 467 IWL_ERR(trans, "Uncorrectable OTP ECC error, abort OTP read\n");
415e4993
WYG
468 return -EINVAL;
469 }
470 if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
471 /* continue in this case */
472 /* set the correctable OTP ECC bit for acknowledgement */
ca77d534 473 iwl_set_bit(trans, CSR_OTP_GP_REG,
415e4993 474 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
ca77d534 475 IWL_ERR(trans, "Correctable OTP ECC error, continue read\n");
415e4993 476 }
af6b8ee3 477 *eeprom_data = cpu_to_le16(r >> 16);
415e4993
WYG
478 return 0;
479}
480
481/*
482 * iwl_is_otp_empty: check for empty OTP
483 */
ca77d534 484static bool iwl_is_otp_empty(struct iwl_trans *trans)
415e4993 485{
af6b8ee3
JB
486 u16 next_link_addr = 0;
487 __le16 link_value;
415e4993
WYG
488 bool is_empty = false;
489
490 /* locate the beginning of OTP link list */
ca77d534 491 if (!iwl_read_otp_word(trans, next_link_addr, &link_value)) {
415e4993 492 if (!link_value) {
ca77d534 493 IWL_ERR(trans, "OTP is empty\n");
415e4993
WYG
494 is_empty = true;
495 }
496 } else {
ca77d534 497 IWL_ERR(trans, "Unable to read first block of OTP list.\n");
415e4993
WYG
498 is_empty = true;
499 }
500
501 return is_empty;
502}
503
504
505/*
506 * iwl_find_otp_image: find EEPROM image in OTP
507 * finding the OTP block that contains the EEPROM image.
508 * the last valid block on the link list (the block _before_ the last block)
509 * is the block we should read and used to configure the device.
510 * If all the available OTP blocks are full, the last block will be the block
511 * we should read and used to configure the device.
512 * only perform this operation if shadow RAM is disabled
513 */
ca77d534 514static int iwl_find_otp_image(struct iwl_trans *trans,
415e4993
WYG
515 u16 *validblockaddr)
516{
af6b8ee3
JB
517 u16 next_link_addr = 0, valid_addr;
518 __le16 link_value = 0;
415e4993
WYG
519 int usedblocks = 0;
520
521 /* set addressing mode to absolute to traverse the link list */
ca77d534 522 iwl_set_otp_access(trans, IWL_OTP_ACCESS_ABSOLUTE);
415e4993
WYG
523
524 /* checking for empty OTP or error */
ca77d534 525 if (iwl_is_otp_empty(trans))
415e4993
WYG
526 return -EINVAL;
527
528 /*
529 * start traverse link list
530 * until reach the max number of OTP blocks
531 * different devices have different number of OTP blocks
532 */
533 do {
534 /* save current valid block address
535 * check for more block on the link list
536 */
537 valid_addr = next_link_addr;
af6b8ee3 538 next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
ca77d534 539 IWL_DEBUG_EEPROM(trans, "OTP blocks %d addr 0x%x\n",
415e4993 540 usedblocks, next_link_addr);
ca77d534 541 if (iwl_read_otp_word(trans, next_link_addr, &link_value))
415e4993
WYG
542 return -EINVAL;
543 if (!link_value) {
544 /*
2facba76 545 * reach the end of link list, return success and
415e4993
WYG
546 * set address point to the starting address
547 * of the image
548 */
2facba76
JS
549 *validblockaddr = valid_addr;
550 /* skip first 2 bytes (link list pointer) */
551 *validblockaddr += 2;
552 return 0;
415e4993
WYG
553 }
554 /* more in the link list, continue */
555 usedblocks++;
035f7ff2 556 } while (usedblocks <= trans->cfg->base_params->max_ll_items);
2facba76
JS
557
558 /* OTP has no valid blocks */
ca77d534 559 IWL_DEBUG_EEPROM(trans, "OTP has no valid blocks\n");
2facba76 560 return -EINVAL;
415e4993
WYG
561}
562
701cb099
WYG
563/******************************************************************************
564 *
565 * Tx Power related functions
566 *
567******************************************************************************/
568/**
569 * iwl_get_max_txpower_avg - get the highest tx power from all chains.
570 * find the highest tx power from all chains for the channel
571 */
706c4ff6 572static s8 iwl_get_max_txpower_avg(const struct iwl_cfg *cfg,
701cb099
WYG
573 struct iwl_eeprom_enhanced_txpwr *enhanced_txpower,
574 int element, s8 *max_txpower_in_half_dbm)
3be63ff0 575{
701cb099
WYG
576 s8 max_txpower_avg = 0; /* (dBm) */
577
578 /* Take the highest tx power from any valid chains */
1431b216 579 if ((cfg->valid_tx_ant & ANT_A) &&
701cb099
WYG
580 (enhanced_txpower[element].chain_a_max > max_txpower_avg))
581 max_txpower_avg = enhanced_txpower[element].chain_a_max;
1431b216 582 if ((cfg->valid_tx_ant & ANT_B) &&
701cb099
WYG
583 (enhanced_txpower[element].chain_b_max > max_txpower_avg))
584 max_txpower_avg = enhanced_txpower[element].chain_b_max;
1431b216 585 if ((cfg->valid_tx_ant & ANT_C) &&
701cb099
WYG
586 (enhanced_txpower[element].chain_c_max > max_txpower_avg))
587 max_txpower_avg = enhanced_txpower[element].chain_c_max;
1431b216
DF
588 if (((cfg->valid_tx_ant == ANT_AB) |
589 (cfg->valid_tx_ant == ANT_BC) |
590 (cfg->valid_tx_ant == ANT_AC)) &&
701cb099
WYG
591 (enhanced_txpower[element].mimo2_max > max_txpower_avg))
592 max_txpower_avg = enhanced_txpower[element].mimo2_max;
1431b216 593 if ((cfg->valid_tx_ant == ANT_ABC) &&
701cb099
WYG
594 (enhanced_txpower[element].mimo3_max > max_txpower_avg))
595 max_txpower_avg = enhanced_txpower[element].mimo3_max;
596
597 /*
598 * max. tx power in EEPROM is in 1/2 dBm format
599 * convert from 1/2 dBm to dBm (round-up convert)
600 * but we also do not want to loss 1/2 dBm resolution which
601 * will impact performance
602 */
603 *max_txpower_in_half_dbm = max_txpower_avg;
604 return (max_txpower_avg & 0x01) + (max_txpower_avg >> 1);
605}
606
607static void
608iwl_eeprom_enh_txp_read_element(struct iwl_priv *priv,
609 struct iwl_eeprom_enhanced_txpwr *txp,
610 s8 max_txpower_avg)
611{
612 int ch_idx;
613 bool is_ht40 = txp->flags & IWL_EEPROM_ENH_TXP_FL_40MHZ;
614 enum ieee80211_band band;
615
616 band = txp->flags & IWL_EEPROM_ENH_TXP_FL_BAND_52G ?
617 IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
618
619 for (ch_idx = 0; ch_idx < priv->channel_count; ch_idx++) {
620 struct iwl_channel_info *ch_info = &priv->channel_info[ch_idx];
621
622 /* update matching channel or from common data only */
623 if (txp->channel != 0 && ch_info->channel != txp->channel)
624 continue;
625
626 /* update matching band only */
627 if (band != ch_info->band)
628 continue;
629
630 if (ch_info->max_power_avg < max_txpower_avg && !is_ht40) {
631 ch_info->max_power_avg = max_txpower_avg;
632 ch_info->curr_txpow = max_txpower_avg;
633 ch_info->scan_power = max_txpower_avg;
634 }
635
636 if (is_ht40 && ch_info->ht40_max_power_avg < max_txpower_avg)
637 ch_info->ht40_max_power_avg = max_txpower_avg;
638 }
639}
640
641#define EEPROM_TXP_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT)
642#define EEPROM_TXP_ENTRY_LEN sizeof(struct iwl_eeprom_enhanced_txpwr)
643#define EEPROM_TXP_SZ_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT_SIZE)
644
645#define TXP_CHECK_AND_PRINT(x) ((txp->flags & IWL_EEPROM_ENH_TXP_FL_##x) \
646 ? # x " " : "")
647
51dc51d1 648static void iwl_eeprom_enhanced_txpower(struct iwl_priv *priv)
701cb099
WYG
649{
650 struct iwl_eeprom_enhanced_txpwr *txp_array, *txp;
651 int idx, entries;
652 __le16 *txp_len;
653 s8 max_txp_avg, max_txp_avg_halfdbm;
654
655 BUILD_BUG_ON(sizeof(struct iwl_eeprom_enhanced_txpwr) != 8);
656
657 /* the length is in 16-bit words, but we want entries */
11483b5c 658 txp_len = (__le16 *) iwl_eeprom_query_addr(priv, EEPROM_TXP_SZ_OFFS);
701cb099
WYG
659 entries = le16_to_cpup(txp_len) * 2 / EEPROM_TXP_ENTRY_LEN;
660
11483b5c 661 txp_array = (void *) iwl_eeprom_query_addr(priv, EEPROM_TXP_OFFS);
701cb099
WYG
662
663 for (idx = 0; idx < entries; idx++) {
664 txp = &txp_array[idx];
665 /* skip invalid entries */
666 if (!(txp->flags & IWL_EEPROM_ENH_TXP_FL_VALID))
667 continue;
668
669 IWL_DEBUG_EEPROM(priv, "%s %d:\t %s%s%s%s%s%s%s%s (0x%02x)\n",
670 (txp->channel && (txp->flags &
671 IWL_EEPROM_ENH_TXP_FL_COMMON_TYPE)) ?
672 "Common " : (txp->channel) ?
673 "Channel" : "Common",
674 (txp->channel),
675 TXP_CHECK_AND_PRINT(VALID),
676 TXP_CHECK_AND_PRINT(BAND_52G),
677 TXP_CHECK_AND_PRINT(OFDM),
678 TXP_CHECK_AND_PRINT(40MHZ),
679 TXP_CHECK_AND_PRINT(HT_AP),
680 TXP_CHECK_AND_PRINT(RES1),
681 TXP_CHECK_AND_PRINT(RES2),
682 TXP_CHECK_AND_PRINT(COMMON_TYPE),
683 txp->flags);
684 IWL_DEBUG_EEPROM(priv, "\t\t chain_A: 0x%02x "
685 "chain_B: 0X%02x chain_C: 0X%02x\n",
686 txp->chain_a_max, txp->chain_b_max,
687 txp->chain_c_max);
688 IWL_DEBUG_EEPROM(priv, "\t\t MIMO2: 0x%02x "
689 "MIMO3: 0x%02x High 20_on_40: 0x%02x "
690 "Low 20_on_40: 0x%02x\n",
691 txp->mimo2_max, txp->mimo3_max,
692 ((txp->delta_20_in_40 & 0xf0) >> 4),
693 (txp->delta_20_in_40 & 0x0f));
694
2152268f 695 max_txp_avg = iwl_get_max_txpower_avg(priv->cfg, txp_array, idx,
701cb099
WYG
696 &max_txp_avg_halfdbm);
697
698 /*
699 * Update the user limit values values to the highest
700 * power supported by any channel
701 */
702 if (max_txp_avg > priv->tx_power_user_lmt)
703 priv->tx_power_user_lmt = max_txp_avg;
704 if (max_txp_avg_halfdbm > priv->tx_power_lmt_in_half_dbm)
705 priv->tx_power_lmt_in_half_dbm = max_txp_avg_halfdbm;
706
707 iwl_eeprom_enh_txp_read_element(priv, txp, max_txp_avg);
708 }
3be63ff0 709}
3be63ff0 710
34cf6ff6
AK
711/**
712 * iwl_eeprom_init - read EEPROM contents
713 *
11483b5c 714 * Load the EEPROM contents from adapter into priv->eeprom
34cf6ff6
AK
715 *
716 * NOTE: This routine uses the non-debug IO access functions.
717 */
11483b5c 718int iwl_eeprom_init(struct iwl_priv *priv, u32 hw_rev)
34cf6ff6 719{
af6b8ee3 720 __le16 *e;
68e8dfda 721 u32 gp = iwl_read32(priv->trans, CSR_EEPROM_GP);
0848e297 722 int sz;
34cf6ff6 723 int ret;
34cf6ff6 724 u16 addr;
415e4993
WYG
725 u16 validblockaddr = 0;
726 u16 cache_addr = 0;
0848e297 727
68e8dfda 728 priv->nvm_device_type = iwl_get_nvm_type(priv->trans, hw_rev);
11483b5c 729 if (priv->nvm_device_type == -ENOENT)
b23a0524 730 return -ENOENT;
073d3f5f 731 /* allocate eeprom */
2152268f 732 sz = priv->cfg->base_params->eeprom_size;
11483b5c
JB
733 IWL_DEBUG_EEPROM(priv, "NVM size = %d\n", sz);
734 priv->eeprom = kzalloc(sz, GFP_KERNEL);
735 if (!priv->eeprom) {
073d3f5f
TW
736 ret = -ENOMEM;
737 goto alloc_err;
738 }
11483b5c 739 e = (__le16 *)priv->eeprom;
34cf6ff6 740
11483b5c 741 ret = iwl_eeprom_verify_signature(priv);
073d3f5f 742 if (ret < 0) {
11483b5c 743 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
073d3f5f
TW
744 ret = -ENOENT;
745 goto err;
34cf6ff6
AK
746 }
747
748 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
68e8dfda 749 ret = iwl_eeprom_acquire_semaphore(priv->trans);
34cf6ff6 750 if (ret < 0) {
11483b5c 751 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
073d3f5f
TW
752 ret = -ENOENT;
753 goto err;
34cf6ff6 754 }
88521364 755
11483b5c 756 if (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP) {
88521364 757
68e8dfda 758 ret = iwl_init_otp_access(priv->trans);
0848e297 759 if (ret) {
11483b5c 760 IWL_ERR(priv, "Failed to initialize OTP access.\n");
0848e297 761 ret = -ENOENT;
415e4993 762 goto done;
0848e297 763 }
68e8dfda
EG
764 iwl_write32(priv->trans, CSR_EEPROM_GP,
765 iwl_read32(priv->trans, CSR_EEPROM_GP) &
02a7fa00 766 ~CSR_EEPROM_GP_IF_OWNER_MSK);
415e4993 767
68e8dfda 768 iwl_set_bit(priv->trans, CSR_OTP_GP_REG,
0848e297
WYG
769 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
770 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
415e4993 771 /* traversing the linked list if no shadow ram supported */
2152268f 772 if (!priv->cfg->base_params->shadow_ram_support) {
68e8dfda 773 if (iwl_find_otp_image(priv->trans, &validblockaddr)) {
415e4993 774 ret = -ENOENT;
0848e297
WYG
775 goto done;
776 }
415e4993
WYG
777 }
778 for (addr = validblockaddr; addr < validblockaddr + sz;
779 addr += sizeof(u16)) {
af6b8ee3 780 __le16 eeprom_data;
415e4993 781
68e8dfda 782 ret = iwl_read_otp_word(priv->trans, addr,
11483b5c 783 &eeprom_data);
415e4993 784 if (ret)
0848e297 785 goto done;
415e4993
WYG
786 e[cache_addr / 2] = eeprom_data;
787 cache_addr += sizeof(u16);
0848e297
WYG
788 }
789 } else {
790 /* eeprom is an array of 16bit values */
791 for (addr = 0; addr < sz; addr += sizeof(u16)) {
792 u32 r;
793
68e8dfda 794 iwl_write32(priv->trans, CSR_EEPROM_REG,
02a7fa00 795 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
0848e297 796
68e8dfda 797 ret = iwl_poll_bit(priv->trans, CSR_EEPROM_REG,
1739d332 798 CSR_EEPROM_REG_READ_VALID_MSK,
0848e297
WYG
799 CSR_EEPROM_REG_READ_VALID_MSK,
800 IWL_EEPROM_ACCESS_TIMEOUT);
801 if (ret < 0) {
11483b5c 802 IWL_ERR(priv,
ca77d534 803 "Time out reading EEPROM[%d]\n", addr);
0848e297
WYG
804 goto done;
805 }
68e8dfda 806 r = iwl_read32(priv->trans, CSR_EEPROM_REG);
af6b8ee3 807 e[addr / 2] = cpu_to_le16(r >> 16);
34cf6ff6 808 }
34cf6ff6 809 }
d1358f62 810
11483b5c
JB
811 IWL_DEBUG_EEPROM(priv, "NVM Type: %s, version: 0x%x\n",
812 (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
d1358f62 813 ? "OTP" : "EEPROM",
11483b5c 814 iwl_eeprom_query16(priv, EEPROM_VERSION));
d1358f62 815
34cf6ff6 816 ret = 0;
34cf6ff6 817done:
68e8dfda 818 iwl_eeprom_release_semaphore(priv->trans);
d1358f62 819
073d3f5f
TW
820err:
821 if (ret)
11483b5c 822 iwl_eeprom_free(priv);
073d3f5f 823alloc_err:
34cf6ff6
AK
824 return ret;
825}
34cf6ff6 826
11483b5c 827void iwl_eeprom_free(struct iwl_priv *priv)
073d3f5f 828{
11483b5c
JB
829 kfree(priv->eeprom);
830 priv->eeprom = NULL;
073d3f5f 831}
073d3f5f 832
11483b5c 833static void iwl_init_band_reference(struct iwl_priv *priv,
073d3f5f
TW
834 int eep_band, int *eeprom_ch_count,
835 const struct iwl_eeprom_channel **eeprom_ch_info,
836 const u8 **eeprom_ch_index)
bf85ea4f 837{
e9676695 838 u32 offset = priv->lib->
073d3f5f
TW
839 eeprom_ops.regulatory_bands[eep_band - 1];
840 switch (eep_band) {
bf85ea4f
AK
841 case 1: /* 2.4GHz band */
842 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
073d3f5f 843 *eeprom_ch_info = (struct iwl_eeprom_channel *)
11483b5c 844 iwl_eeprom_query_addr(priv, offset);
bf85ea4f
AK
845 *eeprom_ch_index = iwl_eeprom_band_1;
846 break;
847 case 2: /* 4.9GHz band */
848 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
073d3f5f 849 *eeprom_ch_info = (struct iwl_eeprom_channel *)
11483b5c 850 iwl_eeprom_query_addr(priv, offset);
bf85ea4f
AK
851 *eeprom_ch_index = iwl_eeprom_band_2;
852 break;
853 case 3: /* 5.2GHz band */
854 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
073d3f5f 855 *eeprom_ch_info = (struct iwl_eeprom_channel *)
11483b5c 856 iwl_eeprom_query_addr(priv, offset);
bf85ea4f
AK
857 *eeprom_ch_index = iwl_eeprom_band_3;
858 break;
859 case 4: /* 5.5GHz band */
860 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
073d3f5f 861 *eeprom_ch_info = (struct iwl_eeprom_channel *)
11483b5c 862 iwl_eeprom_query_addr(priv, offset);
bf85ea4f
AK
863 *eeprom_ch_index = iwl_eeprom_band_4;
864 break;
865 case 5: /* 5.7GHz band */
866 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
073d3f5f 867 *eeprom_ch_info = (struct iwl_eeprom_channel *)
11483b5c 868 iwl_eeprom_query_addr(priv, offset);
bf85ea4f
AK
869 *eeprom_ch_index = iwl_eeprom_band_5;
870 break;
7aafef1c 871 case 6: /* 2.4GHz ht40 channels */
bf85ea4f 872 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
073d3f5f 873 *eeprom_ch_info = (struct iwl_eeprom_channel *)
11483b5c 874 iwl_eeprom_query_addr(priv, offset);
bf85ea4f
AK
875 *eeprom_ch_index = iwl_eeprom_band_6;
876 break;
7aafef1c 877 case 7: /* 5 GHz ht40 channels */
bf85ea4f 878 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
073d3f5f 879 *eeprom_ch_info = (struct iwl_eeprom_channel *)
11483b5c 880 iwl_eeprom_query_addr(priv, offset);
bf85ea4f
AK
881 *eeprom_ch_index = iwl_eeprom_band_7;
882 break;
883 default:
884 BUG();
885 return;
886 }
887}
888
889#define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
890 ? # x " " : "")
bf85ea4f 891/**
3b24716f 892 * iwl_mod_ht40_chan_info - Copy ht40 channel info into driver's priv.
bf85ea4f
AK
893 *
894 * Does not set up a command, or touch hardware.
895 */
3b24716f 896static int iwl_mod_ht40_chan_info(struct iwl_priv *priv,
bf85ea4f 897 enum ieee80211_band band, u16 channel,
073d3f5f 898 const struct iwl_eeprom_channel *eeprom_ch,
3b24716f 899 u8 clear_ht40_extension_channel)
bf85ea4f
AK
900{
901 struct iwl_channel_info *ch_info;
902
903 ch_info = (struct iwl_channel_info *)
8622e705 904 iwl_get_channel_info(priv, band, channel);
bf85ea4f
AK
905
906 if (!is_channel_valid(ch_info))
907 return -1;
908
d058ff8b 909 IWL_DEBUG_EEPROM(priv, "HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
630fe9b6 910 " Ad-Hoc %ssupported\n",
bf85ea4f
AK
911 ch_info->channel,
912 is_channel_a_band(ch_info) ?
913 "5.2" : "2.4",
914 CHECK_AND_PRINT(IBSS),
915 CHECK_AND_PRINT(ACTIVE),
916 CHECK_AND_PRINT(RADAR),
917 CHECK_AND_PRINT(WIDE),
bf85ea4f
AK
918 CHECK_AND_PRINT(DFS),
919 eeprom_ch->flags,
920 eeprom_ch->max_power_avg,
921 ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
922 && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
923 "" : "not ");
924
7aafef1c
WYG
925 ch_info->ht40_eeprom = *eeprom_ch;
926 ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
7aafef1c 927 ch_info->ht40_flags = eeprom_ch->flags;
6c3069b1
RC
928 if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
929 ch_info->ht40_extension_channel &= ~clear_ht40_extension_channel;
bf85ea4f
AK
930
931 return 0;
932}
933
934#define CHECK_AND_PRINT_I(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
935 ? # x " " : "")
936
937/**
938 * iwl_init_channel_map - Set up driver's info for all possible channels
939 */
940int iwl_init_channel_map(struct iwl_priv *priv)
941{
942 int eeprom_ch_count = 0;
943 const u8 *eeprom_ch_index = NULL;
073d3f5f 944 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
bf85ea4f
AK
945 int band, ch;
946 struct iwl_channel_info *ch_info;
947
948 if (priv->channel_count) {
d058ff8b 949 IWL_DEBUG_EEPROM(priv, "Channel map already initialized.\n");
bf85ea4f
AK
950 return 0;
951 }
952
d058ff8b 953 IWL_DEBUG_EEPROM(priv, "Initializing regulatory info from EEPROM\n");
bf85ea4f
AK
954
955 priv->channel_count =
956 ARRAY_SIZE(iwl_eeprom_band_1) +
957 ARRAY_SIZE(iwl_eeprom_band_2) +
958 ARRAY_SIZE(iwl_eeprom_band_3) +
959 ARRAY_SIZE(iwl_eeprom_band_4) +
960 ARRAY_SIZE(iwl_eeprom_band_5);
961
d058ff8b
WYG
962 IWL_DEBUG_EEPROM(priv, "Parsing data for %d channels.\n",
963 priv->channel_count);
bf85ea4f 964
7f90dce1
EG
965 priv->channel_info = kcalloc(priv->channel_count,
966 sizeof(struct iwl_channel_info),
967 GFP_KERNEL);
bf85ea4f 968 if (!priv->channel_info) {
15b1687c 969 IWL_ERR(priv, "Could not allocate channel_info\n");
bf85ea4f
AK
970 priv->channel_count = 0;
971 return -ENOMEM;
972 }
973
974 ch_info = priv->channel_info;
975
976 /* Loop through the 5 EEPROM bands adding them in order to the
977 * channel map we maintain (that contains additional information than
978 * what just in the EEPROM) */
979 for (band = 1; band <= 5; band++) {
980
981 iwl_init_band_reference(priv, band, &eeprom_ch_count,
982 &eeprom_ch_info, &eeprom_ch_index);
983
984 /* Loop through each band adding each of the channels */
985 for (ch = 0; ch < eeprom_ch_count; ch++) {
986 ch_info->channel = eeprom_ch_index[ch];
987 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
988 IEEE80211_BAND_5GHZ;
989
990 /* permanently store EEPROM's channel regulatory flags
991 * and max power in channel info database. */
992 ch_info->eeprom = eeprom_ch_info[ch];
993
994 /* Copy the run-time flags so they are there even on
995 * invalid channels */
996 ch_info->flags = eeprom_ch_info[ch].flags;
7aafef1c 997 /* First write that ht40 is not enabled, and then enable
963f5517 998 * one by one */
7aafef1c 999 ch_info->ht40_extension_channel =
3b24716f 1000 IEEE80211_CHAN_NO_HT40;
bf85ea4f
AK
1001
1002 if (!(is_channel_valid(ch_info))) {
d058ff8b
WYG
1003 IWL_DEBUG_EEPROM(priv,
1004 "Ch. %d Flags %x [%sGHz] - "
bf85ea4f
AK
1005 "No traffic\n",
1006 ch_info->channel,
1007 ch_info->flags,
1008 is_channel_a_band(ch_info) ?
1009 "5.2" : "2.4");
1010 ch_info++;
1011 continue;
1012 }
1013
1014 /* Initialize regulatory-based run-time data */
1015 ch_info->max_power_avg = ch_info->curr_txpow =
1016 eeprom_ch_info[ch].max_power_avg;
1017 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
1018 ch_info->min_power = 0;
1019
d058ff8b
WYG
1020 IWL_DEBUG_EEPROM(priv, "Ch. %d [%sGHz] "
1021 "%s%s%s%s%s%s(0x%02x %ddBm):"
630fe9b6 1022 " Ad-Hoc %ssupported\n",
bf85ea4f
AK
1023 ch_info->channel,
1024 is_channel_a_band(ch_info) ?
1025 "5.2" : "2.4",
1026 CHECK_AND_PRINT_I(VALID),
1027 CHECK_AND_PRINT_I(IBSS),
1028 CHECK_AND_PRINT_I(ACTIVE),
1029 CHECK_AND_PRINT_I(RADAR),
1030 CHECK_AND_PRINT_I(WIDE),
bf85ea4f
AK
1031 CHECK_AND_PRINT_I(DFS),
1032 eeprom_ch_info[ch].flags,
1033 eeprom_ch_info[ch].max_power_avg,
1034 ((eeprom_ch_info[ch].
1035 flags & EEPROM_CHANNEL_IBSS)
1036 && !(eeprom_ch_info[ch].
1037 flags & EEPROM_CHANNEL_RADAR))
1038 ? "" : "not ");
1039
bf85ea4f
AK
1040 ch_info++;
1041 }
1042 }
1043
7aafef1c 1044 /* Check if we do have HT40 channels */
e9676695 1045 if (priv->lib->eeprom_ops.regulatory_bands[5] ==
7aafef1c 1046 EEPROM_REGULATORY_BAND_NO_HT40 &&
e9676695 1047 priv->lib->eeprom_ops.regulatory_bands[6] ==
7aafef1c 1048 EEPROM_REGULATORY_BAND_NO_HT40)
e6148917
SO
1049 return 0;
1050
7aafef1c 1051 /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
bf85ea4f
AK
1052 for (band = 6; band <= 7; band++) {
1053 enum ieee80211_band ieeeband;
bf85ea4f
AK
1054
1055 iwl_init_band_reference(priv, band, &eeprom_ch_count,
1056 &eeprom_ch_info, &eeprom_ch_index);
1057
1058 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
1059 ieeeband =
1060 (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1061
1062 /* Loop through each band adding each of the channels */
1063 for (ch = 0; ch < eeprom_ch_count; ch++) {
bf85ea4f 1064 /* Set up driver's info for lower half */
3b24716f 1065 iwl_mod_ht40_chan_info(priv, ieeeband,
da6833cb 1066 eeprom_ch_index[ch],
3b24716f
ZY
1067 &eeprom_ch_info[ch],
1068 IEEE80211_CHAN_NO_HT40PLUS);
bf85ea4f
AK
1069
1070 /* Set up driver's info for upper half */
3b24716f
ZY
1071 iwl_mod_ht40_chan_info(priv, ieeeband,
1072 eeprom_ch_index[ch] + 4,
1073 &eeprom_ch_info[ch],
1074 IEEE80211_CHAN_NO_HT40MINUS);
bf85ea4f
AK
1075 }
1076 }
1077
ab9fd1bf
WYG
1078 /* for newer device (6000 series and up)
1079 * EEPROM contain enhanced tx power information
1080 * driver need to process addition information
1081 * to determine the max channel tx power limits
1082 */
e9676695 1083 if (priv->lib->eeprom_ops.enhanced_txpower)
51dc51d1 1084 iwl_eeprom_enhanced_txpower(priv);
ab9fd1bf 1085
bf85ea4f
AK
1086 return 0;
1087}
bf85ea4f
AK
1088
1089/*
da6833cb 1090 * iwl_free_channel_map - undo allocations in iwl_init_channel_map
bf85ea4f
AK
1091 */
1092void iwl_free_channel_map(struct iwl_priv *priv)
1093{
1094 kfree(priv->channel_info);
1095 priv->channel_count = 0;
1096}
bf85ea4f
AK
1097
1098/**
1099 * iwl_get_channel_info - Find driver's private channel info
1100 *
1101 * Based on band and channel number.
1102 */
82a66bbb
TW
1103const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
1104 enum ieee80211_band band, u16 channel)
bf85ea4f
AK
1105{
1106 int i;
1107
1108 switch (band) {
1109 case IEEE80211_BAND_5GHZ:
1110 for (i = 14; i < priv->channel_count; i++) {
1111 if (priv->channel_info[i].channel == channel)
1112 return &priv->channel_info[i];
1113 }
1114 break;
1115 case IEEE80211_BAND_2GHZ:
1116 if (channel >= 1 && channel <= 14)
1117 return &priv->channel_info[channel - 1];
1118 break;
1119 default:
1120 BUG();
1121 }
1122
1123 return NULL;
1124}
86cb3b4e
WYG
1125
1126void iwl_rf_config(struct iwl_priv *priv)
1127{
1128 u16 radio_cfg;
1129
11483b5c 1130 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
86cb3b4e
WYG
1131
1132 /* write radio config values to register */
1133 if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) <= EEPROM_RF_CONFIG_TYPE_MAX) {
68e8dfda 1134 iwl_set_bit(priv->trans, CSR_HW_IF_CONFIG_REG,
86cb3b4e
WYG
1135 EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
1136 EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
1137 EEPROM_RF_CFG_DASH_MSK(radio_cfg));
1138 IWL_INFO(priv, "Radio type=0x%x-0x%x-0x%x\n",
1139 EEPROM_RF_CFG_TYPE_MSK(radio_cfg),
1140 EEPROM_RF_CFG_STEP_MSK(radio_cfg),
1141 EEPROM_RF_CFG_DASH_MSK(radio_cfg));
1142 } else
1143 WARN_ON(1);
1144
1145 /* set CSR_HW_CONFIG_REG for uCode use */
68e8dfda 1146 iwl_set_bit(priv->trans, CSR_HW_IF_CONFIG_REG,
86cb3b4e
WYG
1147 CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
1148 CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
1149}