iwl-agn-eeprom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. *
  8. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  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:
  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. *
  33. * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
  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. #include <linux/kernel.h>
  63. #include <linux/module.h>
  64. #include <linux/slab.h>
  65. #include <linux/init.h>
  66. #include <net/mac80211.h>
  67. #include "iwl-commands.h"
  68. #include "iwl-dev.h"
  69. #include "iwl-core.h"
  70. #include "iwl-debug.h"
  71. #include "iwl-agn.h"
  72. #include "iwl-io.h"
  73. /******************************************************************************
  74. *
  75. * EEPROM related functions
  76. *
  77. ******************************************************************************/
  78. /*
  79. * The device's EEPROM semaphore prevents conflicts between driver and uCode
  80. * when accessing the EEPROM; each access is a series of pulses to/from the
  81. * EEPROM chip, not a single event, so even reads could conflict if they
  82. * weren't arbitrated by the semaphore.
  83. */
  84. int iwlcore_eeprom_acquire_semaphore(struct iwl_priv *priv)
  85. {
  86. u16 count;
  87. int ret;
  88. for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
  89. /* Request semaphore */
  90. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  91. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  92. /* See if we got it */
  93. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  94. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  95. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  96. EEPROM_SEM_TIMEOUT);
  97. if (ret >= 0) {
  98. IWL_DEBUG_EEPROM(priv,
  99. "Acquired semaphore after %d tries.\n",
  100. count+1);
  101. return ret;
  102. }
  103. }
  104. return ret;
  105. }
  106. void iwlcore_eeprom_release_semaphore(struct iwl_priv *priv)
  107. {
  108. iwl_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
  109. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  110. }
  111. int iwl_eeprom_check_version(struct iwl_priv *priv)
  112. {
  113. u16 eeprom_ver;
  114. u16 calib_ver;
  115. eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
  116. calib_ver = priv->cfg->ops->lib->eeprom_ops.calib_version(priv);
  117. if (eeprom_ver < priv->cfg->eeprom_ver ||
  118. calib_ver < priv->cfg->eeprom_calib_ver)
  119. goto err;
  120. IWL_INFO(priv, "device EEPROM VER=0x%x, CALIB=0x%x\n",
  121. eeprom_ver, calib_ver);
  122. return 0;
  123. err:
  124. IWL_ERR(priv, "Unsupported (too old) EEPROM VER=0x%x < 0x%x "
  125. "CALIB=0x%x < 0x%x\n",
  126. eeprom_ver, priv->cfg->eeprom_ver,
  127. calib_ver, priv->cfg->eeprom_calib_ver);
  128. return -EINVAL;
  129. }
  130. int iwl_eeprom_check_sku(struct iwl_priv *priv)
  131. {
  132. u16 eeprom_sku;
  133. u16 radio_cfg;
  134. eeprom_sku = iwl_eeprom_query16(priv, EEPROM_SKU_CAP);
  135. if (!priv->cfg->sku) {
  136. /* not using sku overwrite */
  137. priv->cfg->sku =
  138. ((eeprom_sku & EEPROM_SKU_CAP_BAND_SELECTION) >>
  139. EEPROM_SKU_CAP_BAND_POS);
  140. if (eeprom_sku & EEPROM_SKU_CAP_11N_ENABLE)
  141. priv->cfg->sku |= IWL_SKU_N;
  142. }
  143. if (!priv->cfg->sku) {
  144. IWL_ERR(priv, "Invalid device sku\n");
  145. return -EINVAL;
  146. }
  147. IWL_INFO(priv, "Device SKU: 0X%x\n", priv->cfg->sku);
  148. if (!priv->cfg->valid_tx_ant && !priv->cfg->valid_rx_ant) {
  149. /* not using .cfg overwrite */
  150. radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
  151. priv->cfg->valid_tx_ant = EEPROM_RF_CFG_TX_ANT_MSK(radio_cfg);
  152. priv->cfg->valid_rx_ant = EEPROM_RF_CFG_RX_ANT_MSK(radio_cfg);
  153. if (!priv->cfg->valid_tx_ant || !priv->cfg->valid_rx_ant) {
  154. IWL_ERR(priv, "Invalid chain (0X%x, 0X%x)\n",
  155. priv->cfg->valid_tx_ant,
  156. priv->cfg->valid_rx_ant);
  157. return -EINVAL;
  158. }
  159. IWL_INFO(priv, "Valid Tx ant: 0X%x, Valid Rx ant: 0X%x\n",
  160. priv->cfg->valid_tx_ant, priv->cfg->valid_rx_ant);
  161. }
  162. /*
  163. * for some special cases,
  164. * EEPROM did not reflect the correct antenna setting
  165. * so overwrite the valid tx/rx antenna from .cfg
  166. */
  167. return 0;
  168. }
  169. void iwl_eeprom_get_mac(const struct iwl_priv *priv, u8 *mac)
  170. {
  171. const u8 *addr = priv->cfg->ops->lib->eeprom_ops.query_addr(priv,
  172. EEPROM_MAC_ADDRESS);
  173. memcpy(mac, addr, ETH_ALEN);
  174. }
  175. /**
  176. * iwl_get_max_txpower_avg - get the highest tx power from all chains.
  177. * find the highest tx power from all chains for the channel
  178. */
  179. static s8 iwl_get_max_txpower_avg(struct iwl_priv *priv,
  180. struct iwl_eeprom_enhanced_txpwr *enhanced_txpower,
  181. int element, s8 *max_txpower_in_half_dbm)
  182. {
  183. s8 max_txpower_avg = 0; /* (dBm) */
  184. /* Take the highest tx power from any valid chains */
  185. if ((priv->cfg->valid_tx_ant & ANT_A) &&
  186. (enhanced_txpower[element].chain_a_max > max_txpower_avg))
  187. max_txpower_avg = enhanced_txpower[element].chain_a_max;
  188. if ((priv->cfg->valid_tx_ant & ANT_B) &&
  189. (enhanced_txpower[element].chain_b_max > max_txpower_avg))
  190. max_txpower_avg = enhanced_txpower[element].chain_b_max;
  191. if ((priv->cfg->valid_tx_ant & ANT_C) &&
  192. (enhanced_txpower[element].chain_c_max > max_txpower_avg))
  193. max_txpower_avg = enhanced_txpower[element].chain_c_max;
  194. if (((priv->cfg->valid_tx_ant == ANT_AB) |
  195. (priv->cfg->valid_tx_ant == ANT_BC) |
  196. (priv->cfg->valid_tx_ant == ANT_AC)) &&
  197. (enhanced_txpower[element].mimo2_max > max_txpower_avg))
  198. max_txpower_avg = enhanced_txpower[element].mimo2_max;
  199. if ((priv->cfg->valid_tx_ant == ANT_ABC) &&
  200. (enhanced_txpower[element].mimo3_max > max_txpower_avg))
  201. max_txpower_avg = enhanced_txpower[element].mimo3_max;
  202. /*
  203. * max. tx power in EEPROM is in 1/2 dBm format
  204. * convert from 1/2 dBm to dBm (round-up convert)
  205. * but we also do not want to loss 1/2 dBm resolution which
  206. * will impact performance
  207. */
  208. *max_txpower_in_half_dbm = max_txpower_avg;
  209. return (max_txpower_avg & 0x01) + (max_txpower_avg >> 1);
  210. }
  211. static void
  212. iwlcore_eeprom_enh_txp_read_element(struct iwl_priv *priv,
  213. struct iwl_eeprom_enhanced_txpwr *txp,
  214. s8 max_txpower_avg)
  215. {
  216. int ch_idx;
  217. bool is_ht40 = txp->flags & IWL_EEPROM_ENH_TXP_FL_40MHZ;
  218. enum ieee80211_band band;
  219. band = txp->flags & IWL_EEPROM_ENH_TXP_FL_BAND_52G ?
  220. IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
  221. for (ch_idx = 0; ch_idx < priv->channel_count; ch_idx++) {
  222. struct iwl_channel_info *ch_info = &priv->channel_info[ch_idx];
  223. /* update matching channel or from common data only */
  224. if (txp->channel != 0 && ch_info->channel != txp->channel)
  225. continue;
  226. /* update matching band only */
  227. if (band != ch_info->band)
  228. continue;
  229. if (ch_info->max_power_avg < max_txpower_avg && !is_ht40) {
  230. ch_info->max_power_avg = max_txpower_avg;
  231. ch_info->curr_txpow = max_txpower_avg;
  232. ch_info->scan_power = max_txpower_avg;
  233. }
  234. if (is_ht40 && ch_info->ht40_max_power_avg < max_txpower_avg)
  235. ch_info->ht40_max_power_avg = max_txpower_avg;
  236. }
  237. }
  238. #define EEPROM_TXP_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT)
  239. #define EEPROM_TXP_ENTRY_LEN sizeof(struct iwl_eeprom_enhanced_txpwr)
  240. #define EEPROM_TXP_SZ_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT_SIZE)
  241. #define TXP_CHECK_AND_PRINT(x) ((txp->flags & IWL_EEPROM_ENH_TXP_FL_##x) \
  242. ? # x " " : "")
  243. void iwlcore_eeprom_enhanced_txpower(struct iwl_priv *priv)
  244. {
  245. struct iwl_eeprom_enhanced_txpwr *txp_array, *txp;
  246. int idx, entries;
  247. __le16 *txp_len;
  248. s8 max_txp_avg, max_txp_avg_halfdbm;
  249. BUILD_BUG_ON(sizeof(struct iwl_eeprom_enhanced_txpwr) != 8);
  250. /* the length is in 16-bit words, but we want entries */
  251. txp_len = (__le16 *) iwlagn_eeprom_query_addr(priv, EEPROM_TXP_SZ_OFFS);
  252. entries = le16_to_cpup(txp_len) * 2 / EEPROM_TXP_ENTRY_LEN;
  253. txp_array = (void *) iwlagn_eeprom_query_addr(priv, EEPROM_TXP_OFFS);
  254. for (idx = 0; idx < entries; idx++) {
  255. txp = &txp_array[idx];
  256. /* skip invalid entries */
  257. if (!(txp->flags & IWL_EEPROM_ENH_TXP_FL_VALID))
  258. continue;
  259. IWL_DEBUG_EEPROM(priv, "%s %d:\t %s%s%s%s%s%s%s%s (0x%02x)\n",
  260. (txp->channel && (txp->flags &
  261. IWL_EEPROM_ENH_TXP_FL_COMMON_TYPE)) ?
  262. "Common " : (txp->channel) ?
  263. "Channel" : "Common",
  264. (txp->channel),
  265. TXP_CHECK_AND_PRINT(VALID),
  266. TXP_CHECK_AND_PRINT(BAND_52G),
  267. TXP_CHECK_AND_PRINT(OFDM),
  268. TXP_CHECK_AND_PRINT(40MHZ),
  269. TXP_CHECK_AND_PRINT(HT_AP),
  270. TXP_CHECK_AND_PRINT(RES1),
  271. TXP_CHECK_AND_PRINT(RES2),
  272. TXP_CHECK_AND_PRINT(COMMON_TYPE),
  273. txp->flags);
  274. IWL_DEBUG_EEPROM(priv, "\t\t chain_A: 0x%02x "
  275. "chain_B: 0X%02x chain_C: 0X%02x\n",
  276. txp->chain_a_max, txp->chain_b_max,
  277. txp->chain_c_max);
  278. IWL_DEBUG_EEPROM(priv, "\t\t MIMO2: 0x%02x "
  279. "MIMO3: 0x%02x High 20_on_40: 0x%02x "
  280. "Low 20_on_40: 0x%02x\n",
  281. txp->mimo2_max, txp->mimo3_max,
  282. ((txp->delta_20_in_40 & 0xf0) >> 4),
  283. (txp->delta_20_in_40 & 0x0f));
  284. max_txp_avg = iwl_get_max_txpower_avg(priv, txp_array, idx,
  285. &max_txp_avg_halfdbm);
  286. /*
  287. * Update the user limit values values to the highest
  288. * power supported by any channel
  289. */
  290. if (max_txp_avg > priv->tx_power_user_lmt)
  291. priv->tx_power_user_lmt = max_txp_avg;
  292. if (max_txp_avg_halfdbm > priv->tx_power_lmt_in_half_dbm)
  293. priv->tx_power_lmt_in_half_dbm = max_txp_avg_halfdbm;
  294. iwlcore_eeprom_enh_txp_read_element(priv, txp, max_txp_avg);
  295. }
  296. }