iwl-eeprom-read.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 - 2013 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 - 2013 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/types.h>
  63. #include <linux/slab.h>
  64. #include <linux/export.h>
  65. #include "iwl-debug.h"
  66. #include "iwl-eeprom-read.h"
  67. #include "iwl-io.h"
  68. #include "iwl-prph.h"
  69. #include "iwl-csr.h"
  70. /*
  71. * EEPROM access time values:
  72. *
  73. * Driver initiates EEPROM read by writing byte address << 1 to CSR_EEPROM_REG.
  74. * Driver then polls CSR_EEPROM_REG for CSR_EEPROM_REG_READ_VALID_MSK (0x1).
  75. * When polling, wait 10 uSec between polling loops, up to a maximum 5000 uSec.
  76. * Driver reads 16-bit value from bits 31-16 of CSR_EEPROM_REG.
  77. */
  78. #define IWL_EEPROM_ACCESS_TIMEOUT 5000 /* uSec */
  79. #define IWL_EEPROM_SEM_TIMEOUT 10 /* microseconds */
  80. #define IWL_EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
  81. /*
  82. * The device's EEPROM semaphore prevents conflicts between driver and uCode
  83. * when accessing the EEPROM; each access is a series of pulses to/from the
  84. * EEPROM chip, not a single event, so even reads could conflict if they
  85. * weren't arbitrated by the semaphore.
  86. */
  87. #define EEPROM_SEM_TIMEOUT 10 /* milliseconds */
  88. #define EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
  89. static int iwl_eeprom_acquire_semaphore(struct iwl_trans *trans)
  90. {
  91. u16 count;
  92. int ret;
  93. for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
  94. /* Request semaphore */
  95. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  96. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  97. /* See if we got it */
  98. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  99. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  100. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  101. EEPROM_SEM_TIMEOUT);
  102. if (ret >= 0) {
  103. IWL_DEBUG_EEPROM(trans->dev,
  104. "Acquired semaphore after %d tries.\n",
  105. count+1);
  106. return ret;
  107. }
  108. }
  109. return ret;
  110. }
  111. static void iwl_eeprom_release_semaphore(struct iwl_trans *trans)
  112. {
  113. iwl_clear_bit(trans, CSR_HW_IF_CONFIG_REG,
  114. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  115. }
  116. static int iwl_eeprom_verify_signature(struct iwl_trans *trans, bool nvm_is_otp)
  117. {
  118. u32 gp = iwl_read32(trans, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
  119. IWL_DEBUG_EEPROM(trans->dev, "EEPROM signature=0x%08x\n", gp);
  120. switch (gp) {
  121. case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
  122. if (!nvm_is_otp) {
  123. IWL_ERR(trans, "EEPROM with bad signature: 0x%08x\n",
  124. gp);
  125. return -ENOENT;
  126. }
  127. return 0;
  128. case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
  129. case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
  130. if (nvm_is_otp) {
  131. IWL_ERR(trans, "OTP with bad signature: 0x%08x\n", gp);
  132. return -ENOENT;
  133. }
  134. return 0;
  135. case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
  136. default:
  137. IWL_ERR(trans,
  138. "bad EEPROM/OTP signature, type=%s, EEPROM_GP=0x%08x\n",
  139. nvm_is_otp ? "OTP" : "EEPROM", gp);
  140. return -ENOENT;
  141. }
  142. }
  143. /******************************************************************************
  144. *
  145. * OTP related functions
  146. *
  147. ******************************************************************************/
  148. static void iwl_set_otp_access_absolute(struct iwl_trans *trans)
  149. {
  150. iwl_read32(trans, CSR_OTP_GP_REG);
  151. iwl_clear_bit(trans, CSR_OTP_GP_REG,
  152. CSR_OTP_GP_REG_OTP_ACCESS_MODE);
  153. }
  154. static int iwl_nvm_is_otp(struct iwl_trans *trans)
  155. {
  156. u32 otpgp;
  157. /* OTP only valid for CP/PP and after */
  158. switch (trans->hw_rev & CSR_HW_REV_TYPE_MSK) {
  159. case CSR_HW_REV_TYPE_NONE:
  160. IWL_ERR(trans, "Unknown hardware type\n");
  161. return -EIO;
  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. return 0;
  167. default:
  168. otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
  169. if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
  170. return 1;
  171. return 0;
  172. }
  173. }
  174. static int iwl_init_otp_access(struct iwl_trans *trans)
  175. {
  176. int ret;
  177. /* Enable 40MHz radio clock */
  178. iwl_write32(trans, CSR_GP_CNTRL,
  179. iwl_read32(trans, CSR_GP_CNTRL) |
  180. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  181. /* wait for clock to be ready */
  182. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  183. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  184. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  185. 25000);
  186. if (ret < 0) {
  187. IWL_ERR(trans, "Time out access OTP\n");
  188. } else {
  189. iwl_set_bits_prph(trans, APMG_PS_CTRL_REG,
  190. APMG_PS_CTRL_VAL_RESET_REQ);
  191. udelay(5);
  192. iwl_clear_bits_prph(trans, APMG_PS_CTRL_REG,
  193. APMG_PS_CTRL_VAL_RESET_REQ);
  194. /*
  195. * CSR auto clock gate disable bit -
  196. * this is only applicable for HW with OTP shadow RAM
  197. */
  198. if (trans->cfg->base_params->shadow_ram_support)
  199. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  200. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  201. }
  202. return ret;
  203. }
  204. static int iwl_read_otp_word(struct iwl_trans *trans, u16 addr,
  205. __le16 *eeprom_data)
  206. {
  207. int ret = 0;
  208. u32 r;
  209. u32 otpgp;
  210. iwl_write32(trans, CSR_EEPROM_REG,
  211. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  212. ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
  213. CSR_EEPROM_REG_READ_VALID_MSK,
  214. CSR_EEPROM_REG_READ_VALID_MSK,
  215. IWL_EEPROM_ACCESS_TIMEOUT);
  216. if (ret < 0) {
  217. IWL_ERR(trans, "Time out reading OTP[%d]\n", addr);
  218. return ret;
  219. }
  220. r = iwl_read32(trans, CSR_EEPROM_REG);
  221. /* check for ECC errors: */
  222. otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
  223. if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
  224. /* stop in this case */
  225. /* set the uncorrectable OTP ECC bit for acknowledgement */
  226. iwl_set_bit(trans, CSR_OTP_GP_REG,
  227. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  228. IWL_ERR(trans, "Uncorrectable OTP ECC error, abort OTP read\n");
  229. return -EINVAL;
  230. }
  231. if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
  232. /* continue in this case */
  233. /* set the correctable OTP ECC bit for acknowledgement */
  234. iwl_set_bit(trans, CSR_OTP_GP_REG,
  235. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
  236. IWL_ERR(trans, "Correctable OTP ECC error, continue read\n");
  237. }
  238. *eeprom_data = cpu_to_le16(r >> 16);
  239. return 0;
  240. }
  241. /*
  242. * iwl_is_otp_empty: check for empty OTP
  243. */
  244. static bool iwl_is_otp_empty(struct iwl_trans *trans)
  245. {
  246. u16 next_link_addr = 0;
  247. __le16 link_value;
  248. bool is_empty = false;
  249. /* locate the beginning of OTP link list */
  250. if (!iwl_read_otp_word(trans, next_link_addr, &link_value)) {
  251. if (!link_value) {
  252. IWL_ERR(trans, "OTP is empty\n");
  253. is_empty = true;
  254. }
  255. } else {
  256. IWL_ERR(trans, "Unable to read first block of OTP list.\n");
  257. is_empty = true;
  258. }
  259. return is_empty;
  260. }
  261. /*
  262. * iwl_find_otp_image: find EEPROM image in OTP
  263. * finding the OTP block that contains the EEPROM image.
  264. * the last valid block on the link list (the block _before_ the last block)
  265. * is the block we should read and used to configure the device.
  266. * If all the available OTP blocks are full, the last block will be the block
  267. * we should read and used to configure the device.
  268. * only perform this operation if shadow RAM is disabled
  269. */
  270. static int iwl_find_otp_image(struct iwl_trans *trans,
  271. u16 *validblockaddr)
  272. {
  273. u16 next_link_addr = 0, valid_addr;
  274. __le16 link_value = 0;
  275. int usedblocks = 0;
  276. /* set addressing mode to absolute to traverse the link list */
  277. iwl_set_otp_access_absolute(trans);
  278. /* checking for empty OTP or error */
  279. if (iwl_is_otp_empty(trans))
  280. return -EINVAL;
  281. /*
  282. * start traverse link list
  283. * until reach the max number of OTP blocks
  284. * different devices have different number of OTP blocks
  285. */
  286. do {
  287. /* save current valid block address
  288. * check for more block on the link list
  289. */
  290. valid_addr = next_link_addr;
  291. next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
  292. IWL_DEBUG_EEPROM(trans->dev, "OTP blocks %d addr 0x%x\n",
  293. usedblocks, next_link_addr);
  294. if (iwl_read_otp_word(trans, next_link_addr, &link_value))
  295. return -EINVAL;
  296. if (!link_value) {
  297. /*
  298. * reach the end of link list, return success and
  299. * set address point to the starting address
  300. * of the image
  301. */
  302. *validblockaddr = valid_addr;
  303. /* skip first 2 bytes (link list pointer) */
  304. *validblockaddr += 2;
  305. return 0;
  306. }
  307. /* more in the link list, continue */
  308. usedblocks++;
  309. } while (usedblocks <= trans->cfg->base_params->max_ll_items);
  310. /* OTP has no valid blocks */
  311. IWL_DEBUG_EEPROM(trans->dev, "OTP has no valid blocks\n");
  312. return -EINVAL;
  313. }
  314. /**
  315. * iwl_read_eeprom - read EEPROM contents
  316. *
  317. * Load the EEPROM contents from adapter and return it
  318. * and its size.
  319. *
  320. * NOTE: This routine uses the non-debug IO access functions.
  321. */
  322. int iwl_read_eeprom(struct iwl_trans *trans, u8 **eeprom, size_t *eeprom_size)
  323. {
  324. __le16 *e;
  325. u32 gp = iwl_read32(trans, CSR_EEPROM_GP);
  326. int sz;
  327. int ret;
  328. u16 addr;
  329. u16 validblockaddr = 0;
  330. u16 cache_addr = 0;
  331. int nvm_is_otp;
  332. if (!eeprom || !eeprom_size)
  333. return -EINVAL;
  334. nvm_is_otp = iwl_nvm_is_otp(trans);
  335. if (nvm_is_otp < 0)
  336. return nvm_is_otp;
  337. sz = trans->cfg->base_params->eeprom_size;
  338. IWL_DEBUG_EEPROM(trans->dev, "NVM size = %d\n", sz);
  339. e = kmalloc(sz, GFP_KERNEL);
  340. if (!e)
  341. return -ENOMEM;
  342. ret = iwl_eeprom_verify_signature(trans, nvm_is_otp);
  343. if (ret < 0) {
  344. IWL_ERR(trans, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  345. goto err_free;
  346. }
  347. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  348. ret = iwl_eeprom_acquire_semaphore(trans);
  349. if (ret < 0) {
  350. IWL_ERR(trans, "Failed to acquire EEPROM semaphore.\n");
  351. goto err_free;
  352. }
  353. if (nvm_is_otp) {
  354. ret = iwl_init_otp_access(trans);
  355. if (ret) {
  356. IWL_ERR(trans, "Failed to initialize OTP access.\n");
  357. goto err_unlock;
  358. }
  359. iwl_write32(trans, CSR_EEPROM_GP,
  360. iwl_read32(trans, CSR_EEPROM_GP) &
  361. ~CSR_EEPROM_GP_IF_OWNER_MSK);
  362. iwl_set_bit(trans, CSR_OTP_GP_REG,
  363. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
  364. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  365. /* traversing the linked list if no shadow ram supported */
  366. if (!trans->cfg->base_params->shadow_ram_support) {
  367. ret = iwl_find_otp_image(trans, &validblockaddr);
  368. if (ret)
  369. goto err_unlock;
  370. }
  371. for (addr = validblockaddr; addr < validblockaddr + sz;
  372. addr += sizeof(u16)) {
  373. __le16 eeprom_data;
  374. ret = iwl_read_otp_word(trans, addr, &eeprom_data);
  375. if (ret)
  376. goto err_unlock;
  377. e[cache_addr / 2] = eeprom_data;
  378. cache_addr += sizeof(u16);
  379. }
  380. } else {
  381. /* eeprom is an array of 16bit values */
  382. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  383. u32 r;
  384. iwl_write32(trans, CSR_EEPROM_REG,
  385. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  386. ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
  387. CSR_EEPROM_REG_READ_VALID_MSK,
  388. CSR_EEPROM_REG_READ_VALID_MSK,
  389. IWL_EEPROM_ACCESS_TIMEOUT);
  390. if (ret < 0) {
  391. IWL_ERR(trans,
  392. "Time out reading EEPROM[%d]\n", addr);
  393. goto err_unlock;
  394. }
  395. r = iwl_read32(trans, CSR_EEPROM_REG);
  396. e[addr / 2] = cpu_to_le16(r >> 16);
  397. }
  398. }
  399. IWL_DEBUG_EEPROM(trans->dev, "NVM Type: %s\n",
  400. nvm_is_otp ? "OTP" : "EEPROM");
  401. iwl_eeprom_release_semaphore(trans);
  402. *eeprom_size = sz;
  403. *eeprom = (u8 *)e;
  404. return 0;
  405. err_unlock:
  406. iwl_eeprom_release_semaphore(trans);
  407. err_free:
  408. kfree(e);
  409. return ret;
  410. }
  411. EXPORT_SYMBOL_GPL(iwl_read_eeprom);