iwl-eeprom.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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-eeprom.h"
  72. #include "iwl-io.h"
  73. /************************** EEPROM BANDS ****************************
  74. *
  75. * The iwl_eeprom_band definitions below provide the mapping from the
  76. * EEPROM contents to the specific channel number supported for each
  77. * band.
  78. *
  79. * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
  80. * definition below maps to physical channel 42 in the 5.2GHz spectrum.
  81. * The specific geography and calibration information for that channel
  82. * is contained in the eeprom map itself.
  83. *
  84. * During init, we copy the eeprom information and channel map
  85. * information into priv->channel_info_24/52 and priv->channel_map_24/52
  86. *
  87. * channel_map_24/52 provides the index in the channel_info array for a
  88. * given channel. We have to have two separate maps as there is channel
  89. * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
  90. * band_2
  91. *
  92. * A value of 0xff stored in the channel_map indicates that the channel
  93. * is not supported by the hardware at all.
  94. *
  95. * A value of 0xfe in the channel_map indicates that the channel is not
  96. * valid for Tx with the current hardware. This means that
  97. * while the system can tune and receive on a given channel, it may not
  98. * be able to associate or transmit any frames on that
  99. * channel. There is no corresponding channel information for that
  100. * entry.
  101. *
  102. *********************************************************************/
  103. /* 2.4 GHz */
  104. const u8 iwl_eeprom_band_1[14] = {
  105. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  106. };
  107. /* 5.2 GHz bands */
  108. static const u8 iwl_eeprom_band_2[] = { /* 4915-5080MHz */
  109. 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
  110. };
  111. static const u8 iwl_eeprom_band_3[] = { /* 5170-5320MHz */
  112. 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
  113. };
  114. static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
  115. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
  116. };
  117. static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
  118. 145, 149, 153, 157, 161, 165
  119. };
  120. static const u8 iwl_eeprom_band_6[] = { /* 2.4 ht40 channel */
  121. 1, 2, 3, 4, 5, 6, 7
  122. };
  123. static const u8 iwl_eeprom_band_7[] = { /* 5.2 ht40 channel */
  124. 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
  125. };
  126. /******************************************************************************
  127. *
  128. * EEPROM related functions
  129. *
  130. ******************************************************************************/
  131. static int iwl_eeprom_verify_signature(struct iwl_priv *priv)
  132. {
  133. u32 gp = iwl_read32(priv, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
  134. int ret = 0;
  135. IWL_DEBUG_EEPROM(priv, "EEPROM signature=0x%08x\n", gp);
  136. switch (gp) {
  137. case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
  138. if (priv->nvm_device_type != NVM_DEVICE_TYPE_OTP) {
  139. IWL_ERR(priv, "EEPROM with bad signature: 0x%08x\n",
  140. gp);
  141. ret = -ENOENT;
  142. }
  143. break;
  144. case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
  145. case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
  146. if (priv->nvm_device_type != NVM_DEVICE_TYPE_EEPROM) {
  147. IWL_ERR(priv, "OTP with bad signature: 0x%08x\n", gp);
  148. ret = -ENOENT;
  149. }
  150. break;
  151. case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
  152. default:
  153. IWL_ERR(priv, "bad EEPROM/OTP signature, type=%s, "
  154. "EEPROM_GP=0x%08x\n",
  155. (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
  156. ? "OTP" : "EEPROM", gp);
  157. ret = -ENOENT;
  158. break;
  159. }
  160. return ret;
  161. }
  162. static void iwl_set_otp_access(struct iwl_priv *priv, enum iwl_access_mode mode)
  163. {
  164. u32 otpgp;
  165. otpgp = iwl_read32(priv, CSR_OTP_GP_REG);
  166. if (mode == IWL_OTP_ACCESS_ABSOLUTE)
  167. iwl_clear_bit(priv, CSR_OTP_GP_REG,
  168. CSR_OTP_GP_REG_OTP_ACCESS_MODE);
  169. else
  170. iwl_set_bit(priv, CSR_OTP_GP_REG,
  171. CSR_OTP_GP_REG_OTP_ACCESS_MODE);
  172. }
  173. static int iwlcore_get_nvm_type(struct iwl_priv *priv, u32 hw_rev)
  174. {
  175. u32 otpgp;
  176. int nvm_type;
  177. /* OTP only valid for CP/PP and after */
  178. switch (hw_rev & CSR_HW_REV_TYPE_MSK) {
  179. case CSR_HW_REV_TYPE_NONE:
  180. IWL_ERR(priv, "Unknown hardware type\n");
  181. return -ENOENT;
  182. case CSR_HW_REV_TYPE_5300:
  183. case CSR_HW_REV_TYPE_5350:
  184. case CSR_HW_REV_TYPE_5100:
  185. case CSR_HW_REV_TYPE_5150:
  186. nvm_type = NVM_DEVICE_TYPE_EEPROM;
  187. break;
  188. default:
  189. otpgp = iwl_read32(priv, CSR_OTP_GP_REG);
  190. if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
  191. nvm_type = NVM_DEVICE_TYPE_OTP;
  192. else
  193. nvm_type = NVM_DEVICE_TYPE_EEPROM;
  194. break;
  195. }
  196. return nvm_type;
  197. }
  198. static int iwl_init_otp_access(struct iwl_priv *priv)
  199. {
  200. int ret;
  201. /* Enable 40MHz radio clock */
  202. iwl_write32(priv, CSR_GP_CNTRL,
  203. iwl_read32(priv, CSR_GP_CNTRL) |
  204. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  205. /* wait for clock to be ready */
  206. ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
  207. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  208. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  209. 25000);
  210. if (ret < 0)
  211. IWL_ERR(priv, "Time out access OTP\n");
  212. else {
  213. iwl_set_bits_prph(priv, APMG_PS_CTRL_REG,
  214. APMG_PS_CTRL_VAL_RESET_REQ);
  215. udelay(5);
  216. iwl_clear_bits_prph(priv, APMG_PS_CTRL_REG,
  217. APMG_PS_CTRL_VAL_RESET_REQ);
  218. /*
  219. * CSR auto clock gate disable bit -
  220. * this is only applicable for HW with OTP shadow RAM
  221. */
  222. if (priv->cfg->base_params->shadow_ram_support)
  223. iwl_set_bit(priv, CSR_DBG_LINK_PWR_MGMT_REG,
  224. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  225. }
  226. return ret;
  227. }
  228. static int iwl_read_otp_word(struct iwl_priv *priv, u16 addr, __le16 *eeprom_data)
  229. {
  230. int ret = 0;
  231. u32 r;
  232. u32 otpgp;
  233. iwl_write32(priv, CSR_EEPROM_REG,
  234. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  235. ret = iwl_poll_bit(priv, CSR_EEPROM_REG,
  236. CSR_EEPROM_REG_READ_VALID_MSK,
  237. CSR_EEPROM_REG_READ_VALID_MSK,
  238. IWL_EEPROM_ACCESS_TIMEOUT);
  239. if (ret < 0) {
  240. IWL_ERR(priv, "Time out reading OTP[%d]\n", addr);
  241. return ret;
  242. }
  243. r = iwl_read32(priv, CSR_EEPROM_REG);
  244. /* check for ECC errors: */
  245. otpgp = iwl_read32(priv, CSR_OTP_GP_REG);
  246. if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
  247. /* stop in this case */
  248. /* set the uncorrectable OTP ECC bit for acknowledgement */
  249. iwl_set_bit(priv, CSR_OTP_GP_REG,
  250. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  251. IWL_ERR(priv, "Uncorrectable OTP ECC error, abort OTP read\n");
  252. return -EINVAL;
  253. }
  254. if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
  255. /* continue in this case */
  256. /* set the correctable OTP ECC bit for acknowledgement */
  257. iwl_set_bit(priv, CSR_OTP_GP_REG,
  258. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
  259. IWL_ERR(priv, "Correctable OTP ECC error, continue read\n");
  260. }
  261. *eeprom_data = cpu_to_le16(r >> 16);
  262. return 0;
  263. }
  264. /*
  265. * iwl_is_otp_empty: check for empty OTP
  266. */
  267. static bool iwl_is_otp_empty(struct iwl_priv *priv)
  268. {
  269. u16 next_link_addr = 0;
  270. __le16 link_value;
  271. bool is_empty = false;
  272. /* locate the beginning of OTP link list */
  273. if (!iwl_read_otp_word(priv, next_link_addr, &link_value)) {
  274. if (!link_value) {
  275. IWL_ERR(priv, "OTP is empty\n");
  276. is_empty = true;
  277. }
  278. } else {
  279. IWL_ERR(priv, "Unable to read first block of OTP list.\n");
  280. is_empty = true;
  281. }
  282. return is_empty;
  283. }
  284. /*
  285. * iwl_find_otp_image: find EEPROM image in OTP
  286. * finding the OTP block that contains the EEPROM image.
  287. * the last valid block on the link list (the block _before_ the last block)
  288. * is the block we should read and used to configure the device.
  289. * If all the available OTP blocks are full, the last block will be the block
  290. * we should read and used to configure the device.
  291. * only perform this operation if shadow RAM is disabled
  292. */
  293. static int iwl_find_otp_image(struct iwl_priv *priv,
  294. u16 *validblockaddr)
  295. {
  296. u16 next_link_addr = 0, valid_addr;
  297. __le16 link_value = 0;
  298. int usedblocks = 0;
  299. /* set addressing mode to absolute to traverse the link list */
  300. iwl_set_otp_access(priv, IWL_OTP_ACCESS_ABSOLUTE);
  301. /* checking for empty OTP or error */
  302. if (iwl_is_otp_empty(priv))
  303. return -EINVAL;
  304. /*
  305. * start traverse link list
  306. * until reach the max number of OTP blocks
  307. * different devices have different number of OTP blocks
  308. */
  309. do {
  310. /* save current valid block address
  311. * check for more block on the link list
  312. */
  313. valid_addr = next_link_addr;
  314. next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
  315. IWL_DEBUG_EEPROM(priv, "OTP blocks %d addr 0x%x\n",
  316. usedblocks, next_link_addr);
  317. if (iwl_read_otp_word(priv, next_link_addr, &link_value))
  318. return -EINVAL;
  319. if (!link_value) {
  320. /*
  321. * reach the end of link list, return success and
  322. * set address point to the starting address
  323. * of the image
  324. */
  325. *validblockaddr = valid_addr;
  326. /* skip first 2 bytes (link list pointer) */
  327. *validblockaddr += 2;
  328. return 0;
  329. }
  330. /* more in the link list, continue */
  331. usedblocks++;
  332. } while (usedblocks <= priv->cfg->base_params->max_ll_items);
  333. /* OTP has no valid blocks */
  334. IWL_DEBUG_EEPROM(priv, "OTP has no valid blocks\n");
  335. return -EINVAL;
  336. }
  337. const u8 *iwl_eeprom_query_addr(const struct iwl_priv *priv, size_t offset)
  338. {
  339. return priv->cfg->ops->lib->eeprom_ops.query_addr(priv, offset);
  340. }
  341. u16 iwl_eeprom_query16(const struct iwl_priv *priv, size_t offset)
  342. {
  343. if (!priv->eeprom)
  344. return 0;
  345. return (u16)priv->eeprom[offset] | ((u16)priv->eeprom[offset + 1] << 8);
  346. }
  347. /**
  348. * iwl_eeprom_init - read EEPROM contents
  349. *
  350. * Load the EEPROM contents from adapter into priv->eeprom
  351. *
  352. * NOTE: This routine uses the non-debug IO access functions.
  353. */
  354. int iwl_eeprom_init(struct iwl_priv *priv, u32 hw_rev)
  355. {
  356. __le16 *e;
  357. u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
  358. int sz;
  359. int ret;
  360. u16 addr;
  361. u16 validblockaddr = 0;
  362. u16 cache_addr = 0;
  363. priv->nvm_device_type = iwlcore_get_nvm_type(priv, hw_rev);
  364. if (priv->nvm_device_type == -ENOENT)
  365. return -ENOENT;
  366. /* allocate eeprom */
  367. sz = priv->cfg->base_params->eeprom_size;
  368. IWL_DEBUG_EEPROM(priv, "NVM size = %d\n", sz);
  369. priv->eeprom = kzalloc(sz, GFP_KERNEL);
  370. if (!priv->eeprom) {
  371. ret = -ENOMEM;
  372. goto alloc_err;
  373. }
  374. e = (__le16 *)priv->eeprom;
  375. priv->cfg->ops->lib->apm_ops.init(priv);
  376. ret = iwl_eeprom_verify_signature(priv);
  377. if (ret < 0) {
  378. IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  379. ret = -ENOENT;
  380. goto err;
  381. }
  382. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  383. ret = priv->cfg->ops->lib->eeprom_ops.acquire_semaphore(priv);
  384. if (ret < 0) {
  385. IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
  386. ret = -ENOENT;
  387. goto err;
  388. }
  389. if (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP) {
  390. ret = iwl_init_otp_access(priv);
  391. if (ret) {
  392. IWL_ERR(priv, "Failed to initialize OTP access.\n");
  393. ret = -ENOENT;
  394. goto done;
  395. }
  396. iwl_write32(priv, CSR_EEPROM_GP,
  397. iwl_read32(priv, CSR_EEPROM_GP) &
  398. ~CSR_EEPROM_GP_IF_OWNER_MSK);
  399. iwl_set_bit(priv, CSR_OTP_GP_REG,
  400. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
  401. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  402. /* traversing the linked list if no shadow ram supported */
  403. if (!priv->cfg->base_params->shadow_ram_support) {
  404. if (iwl_find_otp_image(priv, &validblockaddr)) {
  405. ret = -ENOENT;
  406. goto done;
  407. }
  408. }
  409. for (addr = validblockaddr; addr < validblockaddr + sz;
  410. addr += sizeof(u16)) {
  411. __le16 eeprom_data;
  412. ret = iwl_read_otp_word(priv, addr, &eeprom_data);
  413. if (ret)
  414. goto done;
  415. e[cache_addr / 2] = eeprom_data;
  416. cache_addr += sizeof(u16);
  417. }
  418. } else {
  419. /* eeprom is an array of 16bit values */
  420. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  421. u32 r;
  422. iwl_write32(priv, CSR_EEPROM_REG,
  423. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  424. ret = iwl_poll_bit(priv, CSR_EEPROM_REG,
  425. CSR_EEPROM_REG_READ_VALID_MSK,
  426. CSR_EEPROM_REG_READ_VALID_MSK,
  427. IWL_EEPROM_ACCESS_TIMEOUT);
  428. if (ret < 0) {
  429. IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
  430. goto done;
  431. }
  432. r = iwl_read32(priv, CSR_EEPROM_REG);
  433. e[addr / 2] = cpu_to_le16(r >> 16);
  434. }
  435. }
  436. IWL_DEBUG_EEPROM(priv, "NVM Type: %s, version: 0x%x\n",
  437. (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
  438. ? "OTP" : "EEPROM",
  439. iwl_eeprom_query16(priv, EEPROM_VERSION));
  440. ret = 0;
  441. done:
  442. priv->cfg->ops->lib->eeprom_ops.release_semaphore(priv);
  443. err:
  444. if (ret)
  445. iwl_eeprom_free(priv);
  446. /* Reset chip to save power until we load uCode during "up". */
  447. iwl_apm_stop(priv);
  448. alloc_err:
  449. return ret;
  450. }
  451. void iwl_eeprom_free(struct iwl_priv *priv)
  452. {
  453. kfree(priv->eeprom);
  454. priv->eeprom = NULL;
  455. }
  456. static void iwl_init_band_reference(const struct iwl_priv *priv,
  457. int eep_band, int *eeprom_ch_count,
  458. const struct iwl_eeprom_channel **eeprom_ch_info,
  459. const u8 **eeprom_ch_index)
  460. {
  461. u32 offset = priv->cfg->ops->lib->
  462. eeprom_ops.regulatory_bands[eep_band - 1];
  463. switch (eep_band) {
  464. case 1: /* 2.4GHz band */
  465. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
  466. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  467. iwl_eeprom_query_addr(priv, offset);
  468. *eeprom_ch_index = iwl_eeprom_band_1;
  469. break;
  470. case 2: /* 4.9GHz band */
  471. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
  472. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  473. iwl_eeprom_query_addr(priv, offset);
  474. *eeprom_ch_index = iwl_eeprom_band_2;
  475. break;
  476. case 3: /* 5.2GHz band */
  477. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
  478. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  479. iwl_eeprom_query_addr(priv, offset);
  480. *eeprom_ch_index = iwl_eeprom_band_3;
  481. break;
  482. case 4: /* 5.5GHz band */
  483. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
  484. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  485. iwl_eeprom_query_addr(priv, offset);
  486. *eeprom_ch_index = iwl_eeprom_band_4;
  487. break;
  488. case 5: /* 5.7GHz band */
  489. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
  490. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  491. iwl_eeprom_query_addr(priv, offset);
  492. *eeprom_ch_index = iwl_eeprom_band_5;
  493. break;
  494. case 6: /* 2.4GHz ht40 channels */
  495. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
  496. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  497. iwl_eeprom_query_addr(priv, offset);
  498. *eeprom_ch_index = iwl_eeprom_band_6;
  499. break;
  500. case 7: /* 5 GHz ht40 channels */
  501. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
  502. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  503. iwl_eeprom_query_addr(priv, offset);
  504. *eeprom_ch_index = iwl_eeprom_band_7;
  505. break;
  506. default:
  507. BUG();
  508. return;
  509. }
  510. }
  511. #define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
  512. ? # x " " : "")
  513. /**
  514. * iwl_mod_ht40_chan_info - Copy ht40 channel info into driver's priv.
  515. *
  516. * Does not set up a command, or touch hardware.
  517. */
  518. static int iwl_mod_ht40_chan_info(struct iwl_priv *priv,
  519. enum ieee80211_band band, u16 channel,
  520. const struct iwl_eeprom_channel *eeprom_ch,
  521. u8 clear_ht40_extension_channel)
  522. {
  523. struct iwl_channel_info *ch_info;
  524. ch_info = (struct iwl_channel_info *)
  525. iwl_get_channel_info(priv, band, channel);
  526. if (!is_channel_valid(ch_info))
  527. return -1;
  528. IWL_DEBUG_EEPROM(priv, "HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
  529. " Ad-Hoc %ssupported\n",
  530. ch_info->channel,
  531. is_channel_a_band(ch_info) ?
  532. "5.2" : "2.4",
  533. CHECK_AND_PRINT(IBSS),
  534. CHECK_AND_PRINT(ACTIVE),
  535. CHECK_AND_PRINT(RADAR),
  536. CHECK_AND_PRINT(WIDE),
  537. CHECK_AND_PRINT(DFS),
  538. eeprom_ch->flags,
  539. eeprom_ch->max_power_avg,
  540. ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
  541. && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
  542. "" : "not ");
  543. ch_info->ht40_eeprom = *eeprom_ch;
  544. ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
  545. ch_info->ht40_flags = eeprom_ch->flags;
  546. if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
  547. ch_info->ht40_extension_channel &= ~clear_ht40_extension_channel;
  548. return 0;
  549. }
  550. #define CHECK_AND_PRINT_I(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
  551. ? # x " " : "")
  552. /**
  553. * iwl_init_channel_map - Set up driver's info for all possible channels
  554. */
  555. int iwl_init_channel_map(struct iwl_priv *priv)
  556. {
  557. int eeprom_ch_count = 0;
  558. const u8 *eeprom_ch_index = NULL;
  559. const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
  560. int band, ch;
  561. struct iwl_channel_info *ch_info;
  562. if (priv->channel_count) {
  563. IWL_DEBUG_EEPROM(priv, "Channel map already initialized.\n");
  564. return 0;
  565. }
  566. IWL_DEBUG_EEPROM(priv, "Initializing regulatory info from EEPROM\n");
  567. priv->channel_count =
  568. ARRAY_SIZE(iwl_eeprom_band_1) +
  569. ARRAY_SIZE(iwl_eeprom_band_2) +
  570. ARRAY_SIZE(iwl_eeprom_band_3) +
  571. ARRAY_SIZE(iwl_eeprom_band_4) +
  572. ARRAY_SIZE(iwl_eeprom_band_5);
  573. IWL_DEBUG_EEPROM(priv, "Parsing data for %d channels.\n",
  574. priv->channel_count);
  575. priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
  576. priv->channel_count, GFP_KERNEL);
  577. if (!priv->channel_info) {
  578. IWL_ERR(priv, "Could not allocate channel_info\n");
  579. priv->channel_count = 0;
  580. return -ENOMEM;
  581. }
  582. ch_info = priv->channel_info;
  583. /* Loop through the 5 EEPROM bands adding them in order to the
  584. * channel map we maintain (that contains additional information than
  585. * what just in the EEPROM) */
  586. for (band = 1; band <= 5; band++) {
  587. iwl_init_band_reference(priv, band, &eeprom_ch_count,
  588. &eeprom_ch_info, &eeprom_ch_index);
  589. /* Loop through each band adding each of the channels */
  590. for (ch = 0; ch < eeprom_ch_count; ch++) {
  591. ch_info->channel = eeprom_ch_index[ch];
  592. ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
  593. IEEE80211_BAND_5GHZ;
  594. /* permanently store EEPROM's channel regulatory flags
  595. * and max power in channel info database. */
  596. ch_info->eeprom = eeprom_ch_info[ch];
  597. /* Copy the run-time flags so they are there even on
  598. * invalid channels */
  599. ch_info->flags = eeprom_ch_info[ch].flags;
  600. /* First write that ht40 is not enabled, and then enable
  601. * one by one */
  602. ch_info->ht40_extension_channel =
  603. IEEE80211_CHAN_NO_HT40;
  604. if (!(is_channel_valid(ch_info))) {
  605. IWL_DEBUG_EEPROM(priv,
  606. "Ch. %d Flags %x [%sGHz] - "
  607. "No traffic\n",
  608. ch_info->channel,
  609. ch_info->flags,
  610. is_channel_a_band(ch_info) ?
  611. "5.2" : "2.4");
  612. ch_info++;
  613. continue;
  614. }
  615. /* Initialize regulatory-based run-time data */
  616. ch_info->max_power_avg = ch_info->curr_txpow =
  617. eeprom_ch_info[ch].max_power_avg;
  618. ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
  619. ch_info->min_power = 0;
  620. IWL_DEBUG_EEPROM(priv, "Ch. %d [%sGHz] "
  621. "%s%s%s%s%s%s(0x%02x %ddBm):"
  622. " Ad-Hoc %ssupported\n",
  623. ch_info->channel,
  624. is_channel_a_band(ch_info) ?
  625. "5.2" : "2.4",
  626. CHECK_AND_PRINT_I(VALID),
  627. CHECK_AND_PRINT_I(IBSS),
  628. CHECK_AND_PRINT_I(ACTIVE),
  629. CHECK_AND_PRINT_I(RADAR),
  630. CHECK_AND_PRINT_I(WIDE),
  631. CHECK_AND_PRINT_I(DFS),
  632. eeprom_ch_info[ch].flags,
  633. eeprom_ch_info[ch].max_power_avg,
  634. ((eeprom_ch_info[ch].
  635. flags & EEPROM_CHANNEL_IBSS)
  636. && !(eeprom_ch_info[ch].
  637. flags & EEPROM_CHANNEL_RADAR))
  638. ? "" : "not ");
  639. ch_info++;
  640. }
  641. }
  642. /* Check if we do have HT40 channels */
  643. if (priv->cfg->ops->lib->eeprom_ops.regulatory_bands[5] ==
  644. EEPROM_REGULATORY_BAND_NO_HT40 &&
  645. priv->cfg->ops->lib->eeprom_ops.regulatory_bands[6] ==
  646. EEPROM_REGULATORY_BAND_NO_HT40)
  647. return 0;
  648. /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
  649. for (band = 6; band <= 7; band++) {
  650. enum ieee80211_band ieeeband;
  651. iwl_init_band_reference(priv, band, &eeprom_ch_count,
  652. &eeprom_ch_info, &eeprom_ch_index);
  653. /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
  654. ieeeband =
  655. (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  656. /* Loop through each band adding each of the channels */
  657. for (ch = 0; ch < eeprom_ch_count; ch++) {
  658. /* Set up driver's info for lower half */
  659. iwl_mod_ht40_chan_info(priv, ieeeband,
  660. eeprom_ch_index[ch],
  661. &eeprom_ch_info[ch],
  662. IEEE80211_CHAN_NO_HT40PLUS);
  663. /* Set up driver's info for upper half */
  664. iwl_mod_ht40_chan_info(priv, ieeeband,
  665. eeprom_ch_index[ch] + 4,
  666. &eeprom_ch_info[ch],
  667. IEEE80211_CHAN_NO_HT40MINUS);
  668. }
  669. }
  670. /* for newer device (6000 series and up)
  671. * EEPROM contain enhanced tx power information
  672. * driver need to process addition information
  673. * to determine the max channel tx power limits
  674. */
  675. if (priv->cfg->ops->lib->eeprom_ops.update_enhanced_txpower)
  676. priv->cfg->ops->lib->eeprom_ops.update_enhanced_txpower(priv);
  677. return 0;
  678. }
  679. /*
  680. * iwl_free_channel_map - undo allocations in iwl_init_channel_map
  681. */
  682. void iwl_free_channel_map(struct iwl_priv *priv)
  683. {
  684. kfree(priv->channel_info);
  685. priv->channel_count = 0;
  686. }
  687. /**
  688. * iwl_get_channel_info - Find driver's private channel info
  689. *
  690. * Based on band and channel number.
  691. */
  692. const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
  693. enum ieee80211_band band, u16 channel)
  694. {
  695. int i;
  696. switch (band) {
  697. case IEEE80211_BAND_5GHZ:
  698. for (i = 14; i < priv->channel_count; i++) {
  699. if (priv->channel_info[i].channel == channel)
  700. return &priv->channel_info[i];
  701. }
  702. break;
  703. case IEEE80211_BAND_2GHZ:
  704. if (channel >= 1 && channel <= 14)
  705. return &priv->channel_info[channel - 1];
  706. break;
  707. default:
  708. BUG();
  709. }
  710. return NULL;
  711. }