iwl-eeprom.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. /*
  132. * The device's EEPROM semaphore prevents conflicts between driver and uCode
  133. * when accessing the EEPROM; each access is a series of pulses to/from the
  134. * EEPROM chip, not a single event, so even reads could conflict if they
  135. * weren't arbitrated by the semaphore.
  136. */
  137. static int iwl_eeprom_acquire_semaphore(struct iwl_priv *priv)
  138. {
  139. u16 count;
  140. int ret;
  141. for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
  142. /* Request semaphore */
  143. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  144. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  145. /* See if we got it */
  146. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  147. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  148. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  149. EEPROM_SEM_TIMEOUT);
  150. if (ret >= 0) {
  151. IWL_DEBUG_EEPROM(priv,
  152. "Acquired semaphore after %d tries.\n",
  153. count+1);
  154. return ret;
  155. }
  156. }
  157. return ret;
  158. }
  159. static void iwl_eeprom_release_semaphore(struct iwl_priv *priv)
  160. {
  161. iwl_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
  162. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  163. }
  164. static int iwl_eeprom_verify_signature(struct iwl_priv *priv)
  165. {
  166. u32 gp = iwl_read32(priv, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
  167. int ret = 0;
  168. IWL_DEBUG_EEPROM(priv, "EEPROM signature=0x%08x\n", gp);
  169. switch (gp) {
  170. case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
  171. if (priv->nvm_device_type != NVM_DEVICE_TYPE_OTP) {
  172. IWL_ERR(priv, "EEPROM with bad signature: 0x%08x\n",
  173. gp);
  174. ret = -ENOENT;
  175. }
  176. break;
  177. case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
  178. case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
  179. if (priv->nvm_device_type != NVM_DEVICE_TYPE_EEPROM) {
  180. IWL_ERR(priv, "OTP with bad signature: 0x%08x\n", gp);
  181. ret = -ENOENT;
  182. }
  183. break;
  184. case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
  185. default:
  186. IWL_ERR(priv, "bad EEPROM/OTP signature, type=%s, "
  187. "EEPROM_GP=0x%08x\n",
  188. (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
  189. ? "OTP" : "EEPROM", gp);
  190. ret = -ENOENT;
  191. break;
  192. }
  193. return ret;
  194. }
  195. static void iwl_set_otp_access(struct iwl_priv *priv, enum iwl_access_mode mode)
  196. {
  197. iwl_read32(priv, CSR_OTP_GP_REG);
  198. if (mode == IWL_OTP_ACCESS_ABSOLUTE)
  199. iwl_clear_bit(priv, CSR_OTP_GP_REG,
  200. CSR_OTP_GP_REG_OTP_ACCESS_MODE);
  201. else
  202. iwl_set_bit(priv, CSR_OTP_GP_REG,
  203. CSR_OTP_GP_REG_OTP_ACCESS_MODE);
  204. }
  205. static int iwlcore_get_nvm_type(struct iwl_priv *priv, u32 hw_rev)
  206. {
  207. u32 otpgp;
  208. int nvm_type;
  209. /* OTP only valid for CP/PP and after */
  210. switch (hw_rev & CSR_HW_REV_TYPE_MSK) {
  211. case CSR_HW_REV_TYPE_NONE:
  212. IWL_ERR(priv, "Unknown hardware type\n");
  213. return -ENOENT;
  214. case CSR_HW_REV_TYPE_5300:
  215. case CSR_HW_REV_TYPE_5350:
  216. case CSR_HW_REV_TYPE_5100:
  217. case CSR_HW_REV_TYPE_5150:
  218. nvm_type = NVM_DEVICE_TYPE_EEPROM;
  219. break;
  220. default:
  221. otpgp = iwl_read32(priv, CSR_OTP_GP_REG);
  222. if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
  223. nvm_type = NVM_DEVICE_TYPE_OTP;
  224. else
  225. nvm_type = NVM_DEVICE_TYPE_EEPROM;
  226. break;
  227. }
  228. return nvm_type;
  229. }
  230. static int iwl_init_otp_access(struct iwl_priv *priv)
  231. {
  232. int ret;
  233. /* Enable 40MHz radio clock */
  234. iwl_write32(priv, CSR_GP_CNTRL,
  235. iwl_read32(priv, CSR_GP_CNTRL) |
  236. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  237. /* wait for clock to be ready */
  238. ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
  239. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  240. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  241. 25000);
  242. if (ret < 0)
  243. IWL_ERR(priv, "Time out access OTP\n");
  244. else {
  245. iwl_set_bits_prph(priv, APMG_PS_CTRL_REG,
  246. APMG_PS_CTRL_VAL_RESET_REQ);
  247. udelay(5);
  248. iwl_clear_bits_prph(priv, APMG_PS_CTRL_REG,
  249. APMG_PS_CTRL_VAL_RESET_REQ);
  250. /*
  251. * CSR auto clock gate disable bit -
  252. * this is only applicable for HW with OTP shadow RAM
  253. */
  254. if (priv->cfg->base_params->shadow_ram_support)
  255. iwl_set_bit(priv, CSR_DBG_LINK_PWR_MGMT_REG,
  256. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  257. }
  258. return ret;
  259. }
  260. static int iwl_read_otp_word(struct iwl_priv *priv, u16 addr, __le16 *eeprom_data)
  261. {
  262. int ret = 0;
  263. u32 r;
  264. u32 otpgp;
  265. iwl_write32(priv, CSR_EEPROM_REG,
  266. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  267. ret = iwl_poll_bit(priv, CSR_EEPROM_REG,
  268. CSR_EEPROM_REG_READ_VALID_MSK,
  269. CSR_EEPROM_REG_READ_VALID_MSK,
  270. IWL_EEPROM_ACCESS_TIMEOUT);
  271. if (ret < 0) {
  272. IWL_ERR(priv, "Time out reading OTP[%d]\n", addr);
  273. return ret;
  274. }
  275. r = iwl_read32(priv, CSR_EEPROM_REG);
  276. /* check for ECC errors: */
  277. otpgp = iwl_read32(priv, CSR_OTP_GP_REG);
  278. if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
  279. /* stop in this case */
  280. /* set the uncorrectable OTP ECC bit for acknowledgement */
  281. iwl_set_bit(priv, CSR_OTP_GP_REG,
  282. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  283. IWL_ERR(priv, "Uncorrectable OTP ECC error, abort OTP read\n");
  284. return -EINVAL;
  285. }
  286. if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
  287. /* continue in this case */
  288. /* set the correctable OTP ECC bit for acknowledgement */
  289. iwl_set_bit(priv, CSR_OTP_GP_REG,
  290. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
  291. IWL_ERR(priv, "Correctable OTP ECC error, continue read\n");
  292. }
  293. *eeprom_data = cpu_to_le16(r >> 16);
  294. return 0;
  295. }
  296. /*
  297. * iwl_is_otp_empty: check for empty OTP
  298. */
  299. static bool iwl_is_otp_empty(struct iwl_priv *priv)
  300. {
  301. u16 next_link_addr = 0;
  302. __le16 link_value;
  303. bool is_empty = false;
  304. /* locate the beginning of OTP link list */
  305. if (!iwl_read_otp_word(priv, next_link_addr, &link_value)) {
  306. if (!link_value) {
  307. IWL_ERR(priv, "OTP is empty\n");
  308. is_empty = true;
  309. }
  310. } else {
  311. IWL_ERR(priv, "Unable to read first block of OTP list.\n");
  312. is_empty = true;
  313. }
  314. return is_empty;
  315. }
  316. /*
  317. * iwl_find_otp_image: find EEPROM image in OTP
  318. * finding the OTP block that contains the EEPROM image.
  319. * the last valid block on the link list (the block _before_ the last block)
  320. * is the block we should read and used to configure the device.
  321. * If all the available OTP blocks are full, the last block will be the block
  322. * we should read and used to configure the device.
  323. * only perform this operation if shadow RAM is disabled
  324. */
  325. static int iwl_find_otp_image(struct iwl_priv *priv,
  326. u16 *validblockaddr)
  327. {
  328. u16 next_link_addr = 0, valid_addr;
  329. __le16 link_value = 0;
  330. int usedblocks = 0;
  331. /* set addressing mode to absolute to traverse the link list */
  332. iwl_set_otp_access(priv, IWL_OTP_ACCESS_ABSOLUTE);
  333. /* checking for empty OTP or error */
  334. if (iwl_is_otp_empty(priv))
  335. return -EINVAL;
  336. /*
  337. * start traverse link list
  338. * until reach the max number of OTP blocks
  339. * different devices have different number of OTP blocks
  340. */
  341. do {
  342. /* save current valid block address
  343. * check for more block on the link list
  344. */
  345. valid_addr = next_link_addr;
  346. next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
  347. IWL_DEBUG_EEPROM(priv, "OTP blocks %d addr 0x%x\n",
  348. usedblocks, next_link_addr);
  349. if (iwl_read_otp_word(priv, next_link_addr, &link_value))
  350. return -EINVAL;
  351. if (!link_value) {
  352. /*
  353. * reach the end of link list, return success and
  354. * set address point to the starting address
  355. * of the image
  356. */
  357. *validblockaddr = valid_addr;
  358. /* skip first 2 bytes (link list pointer) */
  359. *validblockaddr += 2;
  360. return 0;
  361. }
  362. /* more in the link list, continue */
  363. usedblocks++;
  364. } while (usedblocks <= priv->cfg->base_params->max_ll_items);
  365. /* OTP has no valid blocks */
  366. IWL_DEBUG_EEPROM(priv, "OTP has no valid blocks\n");
  367. return -EINVAL;
  368. }
  369. const u8 *iwl_eeprom_query_addr(const struct iwl_priv *priv, size_t offset)
  370. {
  371. return priv->cfg->ops->lib->eeprom_ops.query_addr(priv, offset);
  372. }
  373. u16 iwl_eeprom_query16(const struct iwl_priv *priv, size_t offset)
  374. {
  375. if (!priv->eeprom)
  376. return 0;
  377. return (u16)priv->eeprom[offset] | ((u16)priv->eeprom[offset + 1] << 8);
  378. }
  379. /**
  380. * iwl_eeprom_init - read EEPROM contents
  381. *
  382. * Load the EEPROM contents from adapter into priv->eeprom
  383. *
  384. * NOTE: This routine uses the non-debug IO access functions.
  385. */
  386. int iwl_eeprom_init(struct iwl_priv *priv, u32 hw_rev)
  387. {
  388. __le16 *e;
  389. u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
  390. int sz;
  391. int ret;
  392. u16 addr;
  393. u16 validblockaddr = 0;
  394. u16 cache_addr = 0;
  395. priv->nvm_device_type = iwlcore_get_nvm_type(priv, hw_rev);
  396. if (priv->nvm_device_type == -ENOENT)
  397. return -ENOENT;
  398. /* allocate eeprom */
  399. sz = priv->cfg->base_params->eeprom_size;
  400. IWL_DEBUG_EEPROM(priv, "NVM size = %d\n", sz);
  401. priv->eeprom = kzalloc(sz, GFP_KERNEL);
  402. if (!priv->eeprom) {
  403. ret = -ENOMEM;
  404. goto alloc_err;
  405. }
  406. e = (__le16 *)priv->eeprom;
  407. priv->cfg->ops->lib->apm_ops.init(priv);
  408. ret = iwl_eeprom_verify_signature(priv);
  409. if (ret < 0) {
  410. IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  411. ret = -ENOENT;
  412. goto err;
  413. }
  414. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  415. ret = iwl_eeprom_acquire_semaphore(priv);
  416. if (ret < 0) {
  417. IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
  418. ret = -ENOENT;
  419. goto err;
  420. }
  421. if (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP) {
  422. ret = iwl_init_otp_access(priv);
  423. if (ret) {
  424. IWL_ERR(priv, "Failed to initialize OTP access.\n");
  425. ret = -ENOENT;
  426. goto done;
  427. }
  428. iwl_write32(priv, CSR_EEPROM_GP,
  429. iwl_read32(priv, CSR_EEPROM_GP) &
  430. ~CSR_EEPROM_GP_IF_OWNER_MSK);
  431. iwl_set_bit(priv, CSR_OTP_GP_REG,
  432. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
  433. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  434. /* traversing the linked list if no shadow ram supported */
  435. if (!priv->cfg->base_params->shadow_ram_support) {
  436. if (iwl_find_otp_image(priv, &validblockaddr)) {
  437. ret = -ENOENT;
  438. goto done;
  439. }
  440. }
  441. for (addr = validblockaddr; addr < validblockaddr + sz;
  442. addr += sizeof(u16)) {
  443. __le16 eeprom_data;
  444. ret = iwl_read_otp_word(priv, addr, &eeprom_data);
  445. if (ret)
  446. goto done;
  447. e[cache_addr / 2] = eeprom_data;
  448. cache_addr += sizeof(u16);
  449. }
  450. } else {
  451. /* eeprom is an array of 16bit values */
  452. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  453. u32 r;
  454. iwl_write32(priv, CSR_EEPROM_REG,
  455. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  456. ret = iwl_poll_bit(priv, CSR_EEPROM_REG,
  457. CSR_EEPROM_REG_READ_VALID_MSK,
  458. CSR_EEPROM_REG_READ_VALID_MSK,
  459. IWL_EEPROM_ACCESS_TIMEOUT);
  460. if (ret < 0) {
  461. IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
  462. goto done;
  463. }
  464. r = iwl_read32(priv, CSR_EEPROM_REG);
  465. e[addr / 2] = cpu_to_le16(r >> 16);
  466. }
  467. }
  468. IWL_DEBUG_EEPROM(priv, "NVM Type: %s, version: 0x%x\n",
  469. (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
  470. ? "OTP" : "EEPROM",
  471. iwl_eeprom_query16(priv, EEPROM_VERSION));
  472. ret = 0;
  473. done:
  474. iwl_eeprom_release_semaphore(priv);
  475. err:
  476. if (ret)
  477. iwl_eeprom_free(priv);
  478. /* Reset chip to save power until we load uCode during "up". */
  479. iwl_apm_stop(priv);
  480. alloc_err:
  481. return ret;
  482. }
  483. void iwl_eeprom_free(struct iwl_priv *priv)
  484. {
  485. kfree(priv->eeprom);
  486. priv->eeprom = NULL;
  487. }
  488. static void iwl_init_band_reference(const struct iwl_priv *priv,
  489. int eep_band, int *eeprom_ch_count,
  490. const struct iwl_eeprom_channel **eeprom_ch_info,
  491. const u8 **eeprom_ch_index)
  492. {
  493. u32 offset = priv->cfg->ops->lib->
  494. eeprom_ops.regulatory_bands[eep_band - 1];
  495. switch (eep_band) {
  496. case 1: /* 2.4GHz band */
  497. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
  498. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  499. iwl_eeprom_query_addr(priv, offset);
  500. *eeprom_ch_index = iwl_eeprom_band_1;
  501. break;
  502. case 2: /* 4.9GHz band */
  503. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
  504. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  505. iwl_eeprom_query_addr(priv, offset);
  506. *eeprom_ch_index = iwl_eeprom_band_2;
  507. break;
  508. case 3: /* 5.2GHz band */
  509. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
  510. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  511. iwl_eeprom_query_addr(priv, offset);
  512. *eeprom_ch_index = iwl_eeprom_band_3;
  513. break;
  514. case 4: /* 5.5GHz band */
  515. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
  516. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  517. iwl_eeprom_query_addr(priv, offset);
  518. *eeprom_ch_index = iwl_eeprom_band_4;
  519. break;
  520. case 5: /* 5.7GHz band */
  521. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
  522. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  523. iwl_eeprom_query_addr(priv, offset);
  524. *eeprom_ch_index = iwl_eeprom_band_5;
  525. break;
  526. case 6: /* 2.4GHz ht40 channels */
  527. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
  528. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  529. iwl_eeprom_query_addr(priv, offset);
  530. *eeprom_ch_index = iwl_eeprom_band_6;
  531. break;
  532. case 7: /* 5 GHz ht40 channels */
  533. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
  534. *eeprom_ch_info = (struct iwl_eeprom_channel *)
  535. iwl_eeprom_query_addr(priv, offset);
  536. *eeprom_ch_index = iwl_eeprom_band_7;
  537. break;
  538. default:
  539. BUG();
  540. return;
  541. }
  542. }
  543. #define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
  544. ? # x " " : "")
  545. /**
  546. * iwl_mod_ht40_chan_info - Copy ht40 channel info into driver's priv.
  547. *
  548. * Does not set up a command, or touch hardware.
  549. */
  550. static int iwl_mod_ht40_chan_info(struct iwl_priv *priv,
  551. enum ieee80211_band band, u16 channel,
  552. const struct iwl_eeprom_channel *eeprom_ch,
  553. u8 clear_ht40_extension_channel)
  554. {
  555. struct iwl_channel_info *ch_info;
  556. ch_info = (struct iwl_channel_info *)
  557. iwl_get_channel_info(priv, band, channel);
  558. if (!is_channel_valid(ch_info))
  559. return -1;
  560. IWL_DEBUG_EEPROM(priv, "HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
  561. " Ad-Hoc %ssupported\n",
  562. ch_info->channel,
  563. is_channel_a_band(ch_info) ?
  564. "5.2" : "2.4",
  565. CHECK_AND_PRINT(IBSS),
  566. CHECK_AND_PRINT(ACTIVE),
  567. CHECK_AND_PRINT(RADAR),
  568. CHECK_AND_PRINT(WIDE),
  569. CHECK_AND_PRINT(DFS),
  570. eeprom_ch->flags,
  571. eeprom_ch->max_power_avg,
  572. ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
  573. && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
  574. "" : "not ");
  575. ch_info->ht40_eeprom = *eeprom_ch;
  576. ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
  577. ch_info->ht40_flags = eeprom_ch->flags;
  578. if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
  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_EEPROM(priv, "Channel map already initialized.\n");
  596. return 0;
  597. }
  598. IWL_DEBUG_EEPROM(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_EEPROM(priv, "Parsing data for %d channels.\n",
  606. priv->channel_count);
  607. priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
  608. priv->channel_count, GFP_KERNEL);
  609. if (!priv->channel_info) {
  610. IWL_ERR(priv, "Could not allocate channel_info\n");
  611. priv->channel_count = 0;
  612. return -ENOMEM;
  613. }
  614. ch_info = priv->channel_info;
  615. /* Loop through the 5 EEPROM bands adding them in order to the
  616. * channel map we maintain (that contains additional information than
  617. * what just in the EEPROM) */
  618. for (band = 1; band <= 5; band++) {
  619. iwl_init_band_reference(priv, band, &eeprom_ch_count,
  620. &eeprom_ch_info, &eeprom_ch_index);
  621. /* Loop through each band adding each of the channels */
  622. for (ch = 0; ch < eeprom_ch_count; ch++) {
  623. ch_info->channel = eeprom_ch_index[ch];
  624. ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
  625. IEEE80211_BAND_5GHZ;
  626. /* permanently store EEPROM's channel regulatory flags
  627. * and max power in channel info database. */
  628. ch_info->eeprom = eeprom_ch_info[ch];
  629. /* Copy the run-time flags so they are there even on
  630. * invalid channels */
  631. ch_info->flags = eeprom_ch_info[ch].flags;
  632. /* First write that ht40 is not enabled, and then enable
  633. * one by one */
  634. ch_info->ht40_extension_channel =
  635. IEEE80211_CHAN_NO_HT40;
  636. if (!(is_channel_valid(ch_info))) {
  637. IWL_DEBUG_EEPROM(priv,
  638. "Ch. %d Flags %x [%sGHz] - "
  639. "No traffic\n",
  640. ch_info->channel,
  641. ch_info->flags,
  642. is_channel_a_band(ch_info) ?
  643. "5.2" : "2.4");
  644. ch_info++;
  645. continue;
  646. }
  647. /* Initialize regulatory-based run-time data */
  648. ch_info->max_power_avg = ch_info->curr_txpow =
  649. eeprom_ch_info[ch].max_power_avg;
  650. ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
  651. ch_info->min_power = 0;
  652. IWL_DEBUG_EEPROM(priv, "Ch. %d [%sGHz] "
  653. "%s%s%s%s%s%s(0x%02x %ddBm):"
  654. " Ad-Hoc %ssupported\n",
  655. ch_info->channel,
  656. is_channel_a_band(ch_info) ?
  657. "5.2" : "2.4",
  658. CHECK_AND_PRINT_I(VALID),
  659. CHECK_AND_PRINT_I(IBSS),
  660. CHECK_AND_PRINT_I(ACTIVE),
  661. CHECK_AND_PRINT_I(RADAR),
  662. CHECK_AND_PRINT_I(WIDE),
  663. CHECK_AND_PRINT_I(DFS),
  664. eeprom_ch_info[ch].flags,
  665. eeprom_ch_info[ch].max_power_avg,
  666. ((eeprom_ch_info[ch].
  667. flags & EEPROM_CHANNEL_IBSS)
  668. && !(eeprom_ch_info[ch].
  669. flags & EEPROM_CHANNEL_RADAR))
  670. ? "" : "not ");
  671. ch_info++;
  672. }
  673. }
  674. /* Check if we do have HT40 channels */
  675. if (priv->cfg->ops->lib->eeprom_ops.regulatory_bands[5] ==
  676. EEPROM_REGULATORY_BAND_NO_HT40 &&
  677. priv->cfg->ops->lib->eeprom_ops.regulatory_bands[6] ==
  678. EEPROM_REGULATORY_BAND_NO_HT40)
  679. return 0;
  680. /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
  681. for (band = 6; band <= 7; band++) {
  682. enum ieee80211_band ieeeband;
  683. iwl_init_band_reference(priv, band, &eeprom_ch_count,
  684. &eeprom_ch_info, &eeprom_ch_index);
  685. /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
  686. ieeeband =
  687. (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  688. /* Loop through each band adding each of the channels */
  689. for (ch = 0; ch < eeprom_ch_count; ch++) {
  690. /* Set up driver's info for lower half */
  691. iwl_mod_ht40_chan_info(priv, ieeeband,
  692. eeprom_ch_index[ch],
  693. &eeprom_ch_info[ch],
  694. IEEE80211_CHAN_NO_HT40PLUS);
  695. /* Set up driver's info for upper half */
  696. iwl_mod_ht40_chan_info(priv, ieeeband,
  697. eeprom_ch_index[ch] + 4,
  698. &eeprom_ch_info[ch],
  699. IEEE80211_CHAN_NO_HT40MINUS);
  700. }
  701. }
  702. /* for newer device (6000 series and up)
  703. * EEPROM contain enhanced tx power information
  704. * driver need to process addition information
  705. * to determine the max channel tx power limits
  706. */
  707. if (priv->cfg->ops->lib->eeprom_ops.update_enhanced_txpower)
  708. priv->cfg->ops->lib->eeprom_ops.update_enhanced_txpower(priv);
  709. return 0;
  710. }
  711. /*
  712. * iwl_free_channel_map - undo allocations in iwl_init_channel_map
  713. */
  714. void iwl_free_channel_map(struct iwl_priv *priv)
  715. {
  716. kfree(priv->channel_info);
  717. priv->channel_count = 0;
  718. }
  719. /**
  720. * iwl_get_channel_info - Find driver's private channel info
  721. *
  722. * Based on band and channel number.
  723. */
  724. const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
  725. enum ieee80211_band band, u16 channel)
  726. {
  727. int i;
  728. switch (band) {
  729. case IEEE80211_BAND_5GHZ:
  730. for (i = 14; i < priv->channel_count; i++) {
  731. if (priv->channel_info[i].channel == channel)
  732. return &priv->channel_info[i];
  733. }
  734. break;
  735. case IEEE80211_BAND_2GHZ:
  736. if (channel >= 1 && channel <= 14)
  737. return &priv->channel_info[channel - 1];
  738. break;
  739. default:
  740. BUG();
  741. }
  742. return NULL;
  743. }
  744. void iwl_rf_config(struct iwl_priv *priv)
  745. {
  746. u16 radio_cfg;
  747. radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
  748. /* write radio config values to register */
  749. if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) <= EEPROM_RF_CONFIG_TYPE_MAX) {
  750. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  751. EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
  752. EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
  753. EEPROM_RF_CFG_DASH_MSK(radio_cfg));
  754. IWL_INFO(priv, "Radio type=0x%x-0x%x-0x%x\n",
  755. EEPROM_RF_CFG_TYPE_MSK(radio_cfg),
  756. EEPROM_RF_CFG_STEP_MSK(radio_cfg),
  757. EEPROM_RF_CFG_DASH_MSK(radio_cfg));
  758. } else
  759. WARN_ON(1);
  760. /* set CSR_HW_CONFIG_REG for uCode use */
  761. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  762. CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
  763. CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
  764. }