blob: 512d8beb3dfd4c8a6a5a58f62041da5cb0819a1a [file] [log] [blame]
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -07001/******************************************************************************
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 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07008 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -07009 *
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:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -070033 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -070034 * 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>
66#include <linux/slab.h>
67#include <linux/init.h>
68
69#include <net/mac80211.h>
70
71#include "iwl-commands.h"
72#include "iwl-dev.h"
73#include "iwl-core.h"
74#include "iwl-debug.h"
75#include "iwl-agn.h"
76#include "iwl-io.h"
77
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -070078/******************************************************************************
79 *
80 * EEPROM related functions
81 *
82******************************************************************************/
83
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -070084int iwl_eeprom_check_version(struct iwl_priv *priv)
85{
86 u16 eeprom_ver;
87 u16 calib_ver;
88
89 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
Don Fry16b80b72011-04-20 15:25:14 -070090 calib_ver = iwlagn_eeprom_calib_version(priv);
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -070091
92 if (eeprom_ver < priv->cfg->eeprom_ver ||
93 calib_ver < priv->cfg->eeprom_calib_ver)
94 goto err;
95
96 IWL_INFO(priv, "device EEPROM VER=0x%x, CALIB=0x%x\n",
97 eeprom_ver, calib_ver);
98
99 return 0;
100err:
101 IWL_ERR(priv, "Unsupported (too old) EEPROM VER=0x%x < 0x%x "
102 "CALIB=0x%x < 0x%x\n",
103 eeprom_ver, priv->cfg->eeprom_ver,
104 calib_ver, priv->cfg->eeprom_calib_ver);
105 return -EINVAL;
106
107}
108
Wey-Yi Guy21a5b3c2010-11-10 13:32:59 -0800109int iwl_eeprom_check_sku(struct iwl_priv *priv)
110{
Wey-Yi Guydbbf1752010-11-15 13:43:07 -0800111 u16 radio_cfg;
Wey-Yi Guy21a5b3c2010-11-10 13:32:59 -0800112
Wey-Yi Guy239712e2011-01-20 08:08:04 -0800113 if (!priv->cfg->sku) {
114 /* not using sku overwrite */
Wey-Yi Guy88950752011-06-06 12:05:46 -0700115 priv->cfg->sku = iwl_eeprom_query16(priv, EEPROM_SKU_CAP);
Wey-Yi Guy239712e2011-01-20 08:08:04 -0800116 }
Wey-Yi Guy21a5b3c2010-11-10 13:32:59 -0800117 if (!priv->cfg->sku) {
118 IWL_ERR(priv, "Invalid device sku\n");
119 return -EINVAL;
120 }
121
122 IWL_INFO(priv, "Device SKU: 0X%x\n", priv->cfg->sku);
123
Wey-Yi Guydbbf1752010-11-15 13:43:07 -0800124 if (!priv->cfg->valid_tx_ant && !priv->cfg->valid_rx_ant) {
125 /* not using .cfg overwrite */
126 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
127 priv->cfg->valid_tx_ant = EEPROM_RF_CFG_TX_ANT_MSK(radio_cfg);
Wey-Yi Guy38d59392011-01-18 07:59:13 -0800128 priv->cfg->valid_rx_ant = EEPROM_RF_CFG_RX_ANT_MSK(radio_cfg);
Wey-Yi Guydbbf1752010-11-15 13:43:07 -0800129 if (!priv->cfg->valid_tx_ant || !priv->cfg->valid_rx_ant) {
130 IWL_ERR(priv, "Invalid chain (0X%x, 0X%x)\n",
131 priv->cfg->valid_tx_ant,
132 priv->cfg->valid_rx_ant);
133 return -EINVAL;
134 }
135 IWL_INFO(priv, "Valid Tx ant: 0X%x, Valid Rx ant: 0X%x\n",
136 priv->cfg->valid_tx_ant, priv->cfg->valid_rx_ant);
137 }
138 /*
139 * for some special cases,
140 * EEPROM did not reflect the correct antenna setting
141 * so overwrite the valid tx/rx antenna from .cfg
142 */
Wey-Yi Guy21a5b3c2010-11-10 13:32:59 -0800143 return 0;
144}
145
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -0700146void iwl_eeprom_get_mac(const struct iwl_priv *priv, u8 *mac)
147{
148 const u8 *addr = priv->cfg->ops->lib->eeprom_ops.query_addr(priv,
149 EEPROM_MAC_ADDRESS);
150 memcpy(mac, addr, ETH_ALEN);
151}
152
153/**
154 * iwl_get_max_txpower_avg - get the highest tx power from all chains.
155 * find the highest tx power from all chains for the channel
156 */
157static s8 iwl_get_max_txpower_avg(struct iwl_priv *priv,
158 struct iwl_eeprom_enhanced_txpwr *enhanced_txpower,
159 int element, s8 *max_txpower_in_half_dbm)
160{
161 s8 max_txpower_avg = 0; /* (dBm) */
162
Wey-Yi Guy3be63ff2010-10-08 16:05:19 -0700163 /* Take the highest tx power from any valid chains */
164 if ((priv->cfg->valid_tx_ant & ANT_A) &&
165 (enhanced_txpower[element].chain_a_max > max_txpower_avg))
166 max_txpower_avg = enhanced_txpower[element].chain_a_max;
167 if ((priv->cfg->valid_tx_ant & ANT_B) &&
168 (enhanced_txpower[element].chain_b_max > max_txpower_avg))
169 max_txpower_avg = enhanced_txpower[element].chain_b_max;
170 if ((priv->cfg->valid_tx_ant & ANT_C) &&
171 (enhanced_txpower[element].chain_c_max > max_txpower_avg))
172 max_txpower_avg = enhanced_txpower[element].chain_c_max;
173 if (((priv->cfg->valid_tx_ant == ANT_AB) |
174 (priv->cfg->valid_tx_ant == ANT_BC) |
175 (priv->cfg->valid_tx_ant == ANT_AC)) &&
176 (enhanced_txpower[element].mimo2_max > max_txpower_avg))
177 max_txpower_avg = enhanced_txpower[element].mimo2_max;
178 if ((priv->cfg->valid_tx_ant == ANT_ABC) &&
179 (enhanced_txpower[element].mimo3_max > max_txpower_avg))
180 max_txpower_avg = enhanced_txpower[element].mimo3_max;
181
182 /*
183 * max. tx power in EEPROM is in 1/2 dBm format
184 * convert from 1/2 dBm to dBm (round-up convert)
185 * but we also do not want to loss 1/2 dBm resolution which
186 * will impact performance
187 */
188 *max_txpower_in_half_dbm = max_txpower_avg;
189 return (max_txpower_avg & 0x01) + (max_txpower_avg >> 1);
190}
191
Johannes Berg8d6748c2010-12-09 09:30:14 -0800192static void
193iwlcore_eeprom_enh_txp_read_element(struct iwl_priv *priv,
194 struct iwl_eeprom_enhanced_txpwr *txp,
195 s8 max_txpower_avg)
196{
197 int ch_idx;
198 bool is_ht40 = txp->flags & IWL_EEPROM_ENH_TXP_FL_40MHZ;
199 enum ieee80211_band band;
200
201 band = txp->flags & IWL_EEPROM_ENH_TXP_FL_BAND_52G ?
202 IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
203
204 for (ch_idx = 0; ch_idx < priv->channel_count; ch_idx++) {
205 struct iwl_channel_info *ch_info = &priv->channel_info[ch_idx];
206
207 /* update matching channel or from common data only */
208 if (txp->channel != 0 && ch_info->channel != txp->channel)
209 continue;
210
211 /* update matching band only */
212 if (band != ch_info->band)
213 continue;
214
215 if (ch_info->max_power_avg < max_txpower_avg && !is_ht40) {
216 ch_info->max_power_avg = max_txpower_avg;
217 ch_info->curr_txpow = max_txpower_avg;
218 ch_info->scan_power = max_txpower_avg;
219 }
220
221 if (is_ht40 && ch_info->ht40_max_power_avg < max_txpower_avg)
222 ch_info->ht40_max_power_avg = max_txpower_avg;
223 }
224}
225
226#define EEPROM_TXP_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT)
227#define EEPROM_TXP_ENTRY_LEN sizeof(struct iwl_eeprom_enhanced_txpwr)
228#define EEPROM_TXP_SZ_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT_SIZE)
229
Wey-Yi Guy33c68772010-12-03 10:33:36 -0800230#define TXP_CHECK_AND_PRINT(x) ((txp->flags & IWL_EEPROM_ENH_TXP_FL_##x) \
231 ? # x " " : "")
232
Johannes Bergc6fc1082010-12-09 12:56:42 -0800233void iwlcore_eeprom_enhanced_txpower(struct iwl_priv *priv)
Johannes Berg8d6748c2010-12-09 09:30:14 -0800234{
235 struct iwl_eeprom_enhanced_txpwr *txp_array, *txp;
236 int idx, entries;
237 __le16 *txp_len;
238 s8 max_txp_avg, max_txp_avg_halfdbm;
239
240 BUILD_BUG_ON(sizeof(struct iwl_eeprom_enhanced_txpwr) != 8);
241
242 /* the length is in 16-bit words, but we want entries */
243 txp_len = (__le16 *) iwlagn_eeprom_query_addr(priv, EEPROM_TXP_SZ_OFFS);
244 entries = le16_to_cpup(txp_len) * 2 / EEPROM_TXP_ENTRY_LEN;
245
246 txp_array = (void *) iwlagn_eeprom_query_addr(priv, EEPROM_TXP_OFFS);
Wey-Yi Guy33c68772010-12-03 10:33:36 -0800247
Johannes Berg8d6748c2010-12-09 09:30:14 -0800248 for (idx = 0; idx < entries; idx++) {
249 txp = &txp_array[idx];
Johannes Berg8d6748c2010-12-09 09:30:14 -0800250 /* skip invalid entries */
251 if (!(txp->flags & IWL_EEPROM_ENH_TXP_FL_VALID))
252 continue;
253
Wey-Yi Guy33c68772010-12-03 10:33:36 -0800254 IWL_DEBUG_EEPROM(priv, "%s %d:\t %s%s%s%s%s%s%s%s (0x%02x)\n",
255 (txp->channel && (txp->flags &
256 IWL_EEPROM_ENH_TXP_FL_COMMON_TYPE)) ?
257 "Common " : (txp->channel) ?
258 "Channel" : "Common",
259 (txp->channel),
260 TXP_CHECK_AND_PRINT(VALID),
261 TXP_CHECK_AND_PRINT(BAND_52G),
262 TXP_CHECK_AND_PRINT(OFDM),
263 TXP_CHECK_AND_PRINT(40MHZ),
264 TXP_CHECK_AND_PRINT(HT_AP),
265 TXP_CHECK_AND_PRINT(RES1),
266 TXP_CHECK_AND_PRINT(RES2),
267 TXP_CHECK_AND_PRINT(COMMON_TYPE),
268 txp->flags);
269 IWL_DEBUG_EEPROM(priv, "\t\t chain_A: 0x%02x "
270 "chain_B: 0X%02x chain_C: 0X%02x\n",
271 txp->chain_a_max, txp->chain_b_max,
272 txp->chain_c_max);
273 IWL_DEBUG_EEPROM(priv, "\t\t MIMO2: 0x%02x "
274 "MIMO3: 0x%02x High 20_on_40: 0x%02x "
275 "Low 20_on_40: 0x%02x\n",
276 txp->mimo2_max, txp->mimo3_max,
277 ((txp->delta_20_in_40 & 0xf0) >> 4),
278 (txp->delta_20_in_40 & 0x0f));
279
Johannes Berg8d6748c2010-12-09 09:30:14 -0800280 max_txp_avg = iwl_get_max_txpower_avg(priv, txp_array, idx,
281 &max_txp_avg_halfdbm);
282
283 /*
284 * Update the user limit values values to the highest
285 * power supported by any channel
286 */
287 if (max_txp_avg > priv->tx_power_user_lmt)
288 priv->tx_power_user_lmt = max_txp_avg;
289 if (max_txp_avg_halfdbm > priv->tx_power_lmt_in_half_dbm)
290 priv->tx_power_lmt_in_half_dbm = max_txp_avg_halfdbm;
291
292 iwlcore_eeprom_enh_txp_read_element(priv, txp, max_txp_avg);
293 }
294}