iwl-eeprom-read.c 13 KB

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