iwl-eeprom-read.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. * Copyright(c) 2018 Intel Corporation
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <linuxwifi@intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2018 Intel Corporation
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *****************************************************************************/
  64. #include <linux/types.h>
  65. #include <linux/slab.h>
  66. #include <linux/export.h>
  67. #include "iwl-drv.h"
  68. #include "iwl-debug.h"
  69. #include "iwl-eeprom-read.h"
  70. #include "iwl-io.h"
  71. #include "iwl-prph.h"
  72. #include "iwl-csr.h"
  73. /*
  74. * EEPROM access time values:
  75. *
  76. * Driver initiates EEPROM read by writing byte address << 1 to CSR_EEPROM_REG.
  77. * Driver then polls CSR_EEPROM_REG for CSR_EEPROM_REG_READ_VALID_MSK (0x1).
  78. * When polling, wait 10 uSec between polling loops, up to a maximum 5000 uSec.
  79. * Driver reads 16-bit value from bits 31-16 of CSR_EEPROM_REG.
  80. */
  81. #define IWL_EEPROM_ACCESS_TIMEOUT 5000 /* uSec */
  82. #define IWL_EEPROM_SEM_TIMEOUT 10 /* microseconds */
  83. #define IWL_EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
  84. /*
  85. * The device's EEPROM semaphore prevents conflicts between driver and uCode
  86. * when accessing the EEPROM; each access is a series of pulses to/from the
  87. * EEPROM chip, not a single event, so even reads could conflict if they
  88. * weren't arbitrated by the semaphore.
  89. */
  90. #define EEPROM_SEM_TIMEOUT 10 /* milliseconds */
  91. #define EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
  92. static int iwl_eeprom_acquire_semaphore(struct iwl_trans *trans)
  93. {
  94. u16 count;
  95. int ret;
  96. for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
  97. /* Request semaphore */
  98. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  99. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  100. /* See if we got it */
  101. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  102. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  103. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  104. EEPROM_SEM_TIMEOUT);
  105. if (ret >= 0) {
  106. IWL_DEBUG_EEPROM(trans->dev,
  107. "Acquired semaphore after %d tries.\n",
  108. count+1);
  109. return ret;
  110. }
  111. }
  112. return ret;
  113. }
  114. static void iwl_eeprom_release_semaphore(struct iwl_trans *trans)
  115. {
  116. iwl_clear_bit(trans, CSR_HW_IF_CONFIG_REG,
  117. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  118. }
  119. static int iwl_eeprom_verify_signature(struct iwl_trans *trans, bool nvm_is_otp)
  120. {
  121. u32 gp = iwl_read32(trans, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
  122. IWL_DEBUG_EEPROM(trans->dev, "EEPROM signature=0x%08x\n", gp);
  123. switch (gp) {
  124. case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
  125. if (!nvm_is_otp) {
  126. IWL_ERR(trans, "EEPROM with bad signature: 0x%08x\n",
  127. gp);
  128. return -ENOENT;
  129. }
  130. return 0;
  131. case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
  132. case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
  133. if (nvm_is_otp) {
  134. IWL_ERR(trans, "OTP with bad signature: 0x%08x\n", gp);
  135. return -ENOENT;
  136. }
  137. return 0;
  138. case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
  139. default:
  140. IWL_ERR(trans,
  141. "bad EEPROM/OTP signature, type=%s, EEPROM_GP=0x%08x\n",
  142. nvm_is_otp ? "OTP" : "EEPROM", gp);
  143. return -ENOENT;
  144. }
  145. }
  146. /******************************************************************************
  147. *
  148. * OTP related functions
  149. *
  150. ******************************************************************************/
  151. static void iwl_set_otp_access_absolute(struct iwl_trans *trans)
  152. {
  153. iwl_read32(trans, CSR_OTP_GP_REG);
  154. iwl_clear_bit(trans, CSR_OTP_GP_REG,
  155. CSR_OTP_GP_REG_OTP_ACCESS_MODE);
  156. }
  157. static int iwl_nvm_is_otp(struct iwl_trans *trans)
  158. {
  159. u32 otpgp;
  160. /* OTP only valid for CP/PP and after */
  161. switch (trans->hw_rev & CSR_HW_REV_TYPE_MSK) {
  162. case CSR_HW_REV_TYPE_NONE:
  163. IWL_ERR(trans, "Unknown hardware type\n");
  164. return -EIO;
  165. case CSR_HW_REV_TYPE_5300:
  166. case CSR_HW_REV_TYPE_5350:
  167. case CSR_HW_REV_TYPE_5100:
  168. case CSR_HW_REV_TYPE_5150:
  169. return 0;
  170. default:
  171. otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
  172. if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
  173. return 1;
  174. return 0;
  175. }
  176. }
  177. static int iwl_init_otp_access(struct iwl_trans *trans)
  178. {
  179. int ret;
  180. /* Enable 40MHz radio clock */
  181. iwl_write32(trans, CSR_GP_CNTRL,
  182. iwl_read32(trans, CSR_GP_CNTRL) |
  183. BIT(trans->cfg->csr->flag_init_done));
  184. /* wait for clock to be ready */
  185. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  186. BIT(trans->cfg->csr->flag_mac_clock_ready),
  187. BIT(trans->cfg->csr->flag_mac_clock_ready),
  188. 25000);
  189. if (ret < 0) {
  190. IWL_ERR(trans, "Time out access OTP\n");
  191. } else {
  192. iwl_set_bits_prph(trans, APMG_PS_CTRL_REG,
  193. APMG_PS_CTRL_VAL_RESET_REQ);
  194. udelay(5);
  195. iwl_clear_bits_prph(trans, APMG_PS_CTRL_REG,
  196. APMG_PS_CTRL_VAL_RESET_REQ);
  197. /*
  198. * CSR auto clock gate disable bit -
  199. * this is only applicable for HW with OTP shadow RAM
  200. */
  201. if (trans->cfg->base_params->shadow_ram_support)
  202. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  203. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  204. }
  205. return ret;
  206. }
  207. static int iwl_read_otp_word(struct iwl_trans *trans, u16 addr,
  208. __le16 *eeprom_data)
  209. {
  210. int ret = 0;
  211. u32 r;
  212. u32 otpgp;
  213. iwl_write32(trans, CSR_EEPROM_REG,
  214. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  215. ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
  216. CSR_EEPROM_REG_READ_VALID_MSK,
  217. CSR_EEPROM_REG_READ_VALID_MSK,
  218. IWL_EEPROM_ACCESS_TIMEOUT);
  219. if (ret < 0) {
  220. IWL_ERR(trans, "Time out reading OTP[%d]\n", addr);
  221. return ret;
  222. }
  223. r = iwl_read32(trans, CSR_EEPROM_REG);
  224. /* check for ECC errors: */
  225. otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
  226. if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
  227. /* stop in this case */
  228. /* set the uncorrectable OTP ECC bit for acknowledgment */
  229. iwl_set_bit(trans, CSR_OTP_GP_REG,
  230. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  231. IWL_ERR(trans, "Uncorrectable OTP ECC error, abort OTP read\n");
  232. return -EINVAL;
  233. }
  234. if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
  235. /* continue in this case */
  236. /* set the correctable OTP ECC bit for acknowledgment */
  237. iwl_set_bit(trans, CSR_OTP_GP_REG,
  238. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
  239. IWL_ERR(trans, "Correctable OTP ECC error, continue read\n");
  240. }
  241. *eeprom_data = cpu_to_le16(r >> 16);
  242. return 0;
  243. }
  244. /*
  245. * iwl_is_otp_empty: check for empty OTP
  246. */
  247. static bool iwl_is_otp_empty(struct iwl_trans *trans)
  248. {
  249. u16 next_link_addr = 0;
  250. __le16 link_value;
  251. bool is_empty = false;
  252. /* locate the beginning of OTP link list */
  253. if (!iwl_read_otp_word(trans, next_link_addr, &link_value)) {
  254. if (!link_value) {
  255. IWL_ERR(trans, "OTP is empty\n");
  256. is_empty = true;
  257. }
  258. } else {
  259. IWL_ERR(trans, "Unable to read first block of OTP list.\n");
  260. is_empty = true;
  261. }
  262. return is_empty;
  263. }
  264. /*
  265. * iwl_find_otp_image: find EEPROM image in OTP
  266. * finding the OTP block that contains the EEPROM image.
  267. * the last valid block on the link list (the block _before_ the last block)
  268. * is the block we should read and used to configure the device.
  269. * If all the available OTP blocks are full, the last block will be the block
  270. * we should read and used to configure the device.
  271. * only perform this operation if shadow RAM is disabled
  272. */
  273. static int iwl_find_otp_image(struct iwl_trans *trans,
  274. u16 *validblockaddr)
  275. {
  276. u16 next_link_addr = 0, valid_addr;
  277. __le16 link_value = 0;
  278. int usedblocks = 0;
  279. /* set addressing mode to absolute to traverse the link list */
  280. iwl_set_otp_access_absolute(trans);
  281. /* checking for empty OTP or error */
  282. if (iwl_is_otp_empty(trans))
  283. return -EINVAL;
  284. /*
  285. * start traverse link list
  286. * until reach the max number of OTP blocks
  287. * different devices have different number of OTP blocks
  288. */
  289. do {
  290. /* save current valid block address
  291. * check for more block on the link list
  292. */
  293. valid_addr = next_link_addr;
  294. next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
  295. IWL_DEBUG_EEPROM(trans->dev, "OTP blocks %d addr 0x%x\n",
  296. usedblocks, next_link_addr);
  297. if (iwl_read_otp_word(trans, next_link_addr, &link_value))
  298. return -EINVAL;
  299. if (!link_value) {
  300. /*
  301. * reach the end of link list, return success and
  302. * set address point to the starting address
  303. * of the image
  304. */
  305. *validblockaddr = valid_addr;
  306. /* skip first 2 bytes (link list pointer) */
  307. *validblockaddr += 2;
  308. return 0;
  309. }
  310. /* more in the link list, continue */
  311. usedblocks++;
  312. } while (usedblocks <= trans->cfg->base_params->max_ll_items);
  313. /* OTP has no valid blocks */
  314. IWL_DEBUG_EEPROM(trans->dev, "OTP has no valid blocks\n");
  315. return -EINVAL;
  316. }
  317. /**
  318. * iwl_read_eeprom - read EEPROM contents
  319. *
  320. * Load the EEPROM contents from adapter and return it
  321. * and its size.
  322. *
  323. * NOTE: This routine uses the non-debug IO access functions.
  324. */
  325. int iwl_read_eeprom(struct iwl_trans *trans, u8 **eeprom, size_t *eeprom_size)
  326. {
  327. __le16 *e;
  328. u32 gp = iwl_read32(trans, CSR_EEPROM_GP);
  329. int sz;
  330. int ret;
  331. u16 addr;
  332. u16 validblockaddr = 0;
  333. u16 cache_addr = 0;
  334. int nvm_is_otp;
  335. if (!eeprom || !eeprom_size)
  336. return -EINVAL;
  337. nvm_is_otp = iwl_nvm_is_otp(trans);
  338. if (nvm_is_otp < 0)
  339. return nvm_is_otp;
  340. sz = trans->cfg->base_params->eeprom_size;
  341. IWL_DEBUG_EEPROM(trans->dev, "NVM size = %d\n", sz);
  342. e = kmalloc(sz, GFP_KERNEL);
  343. if (!e)
  344. return -ENOMEM;
  345. ret = iwl_eeprom_verify_signature(trans, nvm_is_otp);
  346. if (ret < 0) {
  347. IWL_ERR(trans, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  348. goto err_free;
  349. }
  350. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  351. ret = iwl_eeprom_acquire_semaphore(trans);
  352. if (ret < 0) {
  353. IWL_ERR(trans, "Failed to acquire EEPROM semaphore.\n");
  354. goto err_free;
  355. }
  356. if (nvm_is_otp) {
  357. ret = iwl_init_otp_access(trans);
  358. if (ret) {
  359. IWL_ERR(trans, "Failed to initialize OTP access.\n");
  360. goto err_unlock;
  361. }
  362. iwl_write32(trans, CSR_EEPROM_GP,
  363. iwl_read32(trans, CSR_EEPROM_GP) &
  364. ~CSR_EEPROM_GP_IF_OWNER_MSK);
  365. iwl_set_bit(trans, CSR_OTP_GP_REG,
  366. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
  367. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  368. /* traversing the linked list if no shadow ram supported */
  369. if (!trans->cfg->base_params->shadow_ram_support) {
  370. ret = iwl_find_otp_image(trans, &validblockaddr);
  371. if (ret)
  372. goto err_unlock;
  373. }
  374. for (addr = validblockaddr; addr < validblockaddr + sz;
  375. addr += sizeof(u16)) {
  376. __le16 eeprom_data;
  377. ret = iwl_read_otp_word(trans, addr, &eeprom_data);
  378. if (ret)
  379. goto err_unlock;
  380. e[cache_addr / 2] = eeprom_data;
  381. cache_addr += sizeof(u16);
  382. }
  383. } else {
  384. /* eeprom is an array of 16bit values */
  385. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  386. u32 r;
  387. iwl_write32(trans, CSR_EEPROM_REG,
  388. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  389. ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
  390. CSR_EEPROM_REG_READ_VALID_MSK,
  391. CSR_EEPROM_REG_READ_VALID_MSK,
  392. IWL_EEPROM_ACCESS_TIMEOUT);
  393. if (ret < 0) {
  394. IWL_ERR(trans,
  395. "Time out reading EEPROM[%d]\n", addr);
  396. goto err_unlock;
  397. }
  398. r = iwl_read32(trans, CSR_EEPROM_REG);
  399. e[addr / 2] = cpu_to_le16(r >> 16);
  400. }
  401. }
  402. IWL_DEBUG_EEPROM(trans->dev, "NVM Type: %s\n",
  403. nvm_is_otp ? "OTP" : "EEPROM");
  404. iwl_eeprom_release_semaphore(trans);
  405. *eeprom_size = sz;
  406. *eeprom = (u8 *)e;
  407. return 0;
  408. err_unlock:
  409. iwl_eeprom_release_semaphore(trans);
  410. err_free:
  411. kfree(e);
  412. return ret;
  413. }
  414. IWL_EXPORT_SYMBOL(iwl_read_eeprom);