iwl-eeprom.c 24 KB

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