iwl-eeprom-read.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2005-2014, 2018-2019, 2021 Intel Corporation
  4. */
  5. #include <linux/types.h>
  6. #include <linux/slab.h>
  7. #include <linux/export.h>
  8. #include "iwl-drv.h"
  9. #include "iwl-debug.h"
  10. #include "iwl-eeprom-read.h"
  11. #include "iwl-io.h"
  12. #include "iwl-prph.h"
  13. #include "iwl-csr.h"
  14. /*
  15. * EEPROM access time values:
  16. *
  17. * Driver initiates EEPROM read by writing byte address << 1 to CSR_EEPROM_REG.
  18. * Driver then polls CSR_EEPROM_REG for CSR_EEPROM_REG_READ_VALID_MSK (0x1).
  19. * When polling, wait 10 uSec between polling loops, up to a maximum 5000 uSec.
  20. * Driver reads 16-bit value from bits 31-16 of CSR_EEPROM_REG.
  21. */
  22. #define IWL_EEPROM_ACCESS_TIMEOUT 5000 /* uSec */
  23. /*
  24. * The device's EEPROM semaphore prevents conflicts between driver and uCode
  25. * when accessing the EEPROM; each access is a series of pulses to/from the
  26. * EEPROM chip, not a single event, so even reads could conflict if they
  27. * weren't arbitrated by the semaphore.
  28. */
  29. #define IWL_EEPROM_SEM_TIMEOUT 10 /* microseconds */
  30. #define IWL_EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
  31. static int iwl_eeprom_acquire_semaphore(struct iwl_trans *trans)
  32. {
  33. u16 count;
  34. int ret;
  35. for (count = 0; count < IWL_EEPROM_SEM_RETRY_LIMIT; count++) {
  36. /* Request semaphore */
  37. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  38. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  39. /* See if we got it */
  40. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  41. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  42. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  43. IWL_EEPROM_SEM_TIMEOUT);
  44. if (ret >= 0) {
  45. IWL_DEBUG_EEPROM(trans->dev,
  46. "Acquired semaphore after %d tries.\n",
  47. count+1);
  48. return ret;
  49. }
  50. }
  51. return ret;
  52. }
  53. static void iwl_eeprom_release_semaphore(struct iwl_trans *trans)
  54. {
  55. iwl_clear_bit(trans, CSR_HW_IF_CONFIG_REG,
  56. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  57. }
  58. static int iwl_eeprom_verify_signature(struct iwl_trans *trans, bool nvm_is_otp)
  59. {
  60. u32 gp = iwl_read32(trans, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
  61. IWL_DEBUG_EEPROM(trans->dev, "EEPROM signature=0x%08x\n", gp);
  62. switch (gp) {
  63. case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
  64. if (!nvm_is_otp) {
  65. IWL_ERR(trans, "EEPROM with bad signature: 0x%08x\n",
  66. gp);
  67. return -ENOENT;
  68. }
  69. return 0;
  70. case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
  71. case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
  72. if (nvm_is_otp) {
  73. IWL_ERR(trans, "OTP with bad signature: 0x%08x\n", gp);
  74. return -ENOENT;
  75. }
  76. return 0;
  77. case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
  78. default:
  79. IWL_ERR(trans,
  80. "bad EEPROM/OTP signature, type=%s, EEPROM_GP=0x%08x\n",
  81. nvm_is_otp ? "OTP" : "EEPROM", gp);
  82. return -ENOENT;
  83. }
  84. }
  85. /******************************************************************************
  86. *
  87. * OTP related functions
  88. *
  89. ******************************************************************************/
  90. static void iwl_set_otp_access_absolute(struct iwl_trans *trans)
  91. {
  92. iwl_read32(trans, CSR_OTP_GP_REG);
  93. iwl_clear_bit(trans, CSR_OTP_GP_REG,
  94. CSR_OTP_GP_REG_OTP_ACCESS_MODE);
  95. }
  96. static int iwl_nvm_is_otp(struct iwl_trans *trans)
  97. {
  98. u32 otpgp;
  99. /* OTP only valid for CP/PP and after */
  100. switch (trans->hw_rev & CSR_HW_REV_TYPE_MSK) {
  101. case CSR_HW_REV_TYPE_NONE:
  102. IWL_ERR(trans, "Unknown hardware type\n");
  103. return -EIO;
  104. case CSR_HW_REV_TYPE_5300:
  105. case CSR_HW_REV_TYPE_5350:
  106. case CSR_HW_REV_TYPE_5100:
  107. case CSR_HW_REV_TYPE_5150:
  108. return 0;
  109. default:
  110. otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
  111. if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
  112. return 1;
  113. return 0;
  114. }
  115. }
  116. static int iwl_init_otp_access(struct iwl_trans *trans)
  117. {
  118. int ret;
  119. ret = iwl_finish_nic_init(trans);
  120. if (ret)
  121. return ret;
  122. iwl_set_bits_prph(trans, APMG_PS_CTRL_REG,
  123. APMG_PS_CTRL_VAL_RESET_REQ);
  124. udelay(5);
  125. iwl_clear_bits_prph(trans, APMG_PS_CTRL_REG,
  126. APMG_PS_CTRL_VAL_RESET_REQ);
  127. /*
  128. * CSR auto clock gate disable bit -
  129. * this is only applicable for HW with OTP shadow RAM
  130. */
  131. if (trans->trans_cfg->base_params->shadow_ram_support)
  132. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  133. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  134. return 0;
  135. }
  136. static int iwl_read_otp_word(struct iwl_trans *trans, u16 addr,
  137. __le16 *eeprom_data)
  138. {
  139. int ret = 0;
  140. u32 r;
  141. u32 otpgp;
  142. iwl_write32(trans, CSR_EEPROM_REG,
  143. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  144. ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
  145. CSR_EEPROM_REG_READ_VALID_MSK,
  146. CSR_EEPROM_REG_READ_VALID_MSK,
  147. IWL_EEPROM_ACCESS_TIMEOUT);
  148. if (ret < 0) {
  149. IWL_ERR(trans, "Time out reading OTP[%d]\n", addr);
  150. return ret;
  151. }
  152. r = iwl_read32(trans, CSR_EEPROM_REG);
  153. /* check for ECC errors: */
  154. otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
  155. if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
  156. /* stop in this case */
  157. /* set the uncorrectable OTP ECC bit for acknowledgment */
  158. iwl_set_bit(trans, CSR_OTP_GP_REG,
  159. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  160. IWL_ERR(trans, "Uncorrectable OTP ECC error, abort OTP read\n");
  161. return -EINVAL;
  162. }
  163. if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
  164. /* continue in this case */
  165. /* set the correctable OTP ECC bit for acknowledgment */
  166. iwl_set_bit(trans, CSR_OTP_GP_REG,
  167. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
  168. IWL_ERR(trans, "Correctable OTP ECC error, continue read\n");
  169. }
  170. *eeprom_data = cpu_to_le16(r >> 16);
  171. return 0;
  172. }
  173. /*
  174. * iwl_is_otp_empty: check for empty OTP
  175. */
  176. static bool iwl_is_otp_empty(struct iwl_trans *trans)
  177. {
  178. u16 next_link_addr = 0;
  179. __le16 link_value;
  180. bool is_empty = false;
  181. /* locate the beginning of OTP link list */
  182. if (!iwl_read_otp_word(trans, next_link_addr, &link_value)) {
  183. if (!link_value) {
  184. IWL_ERR(trans, "OTP is empty\n");
  185. is_empty = true;
  186. }
  187. } else {
  188. IWL_ERR(trans, "Unable to read first block of OTP list.\n");
  189. is_empty = true;
  190. }
  191. return is_empty;
  192. }
  193. /*
  194. * iwl_find_otp_image: find EEPROM image in OTP
  195. * finding the OTP block that contains the EEPROM image.
  196. * the last valid block on the link list (the block _before_ the last block)
  197. * is the block we should read and used to configure the device.
  198. * If all the available OTP blocks are full, the last block will be the block
  199. * we should read and used to configure the device.
  200. * only perform this operation if shadow RAM is disabled
  201. */
  202. static int iwl_find_otp_image(struct iwl_trans *trans,
  203. u16 *validblockaddr)
  204. {
  205. u16 next_link_addr = 0, valid_addr;
  206. __le16 link_value = 0;
  207. int usedblocks = 0;
  208. /* set addressing mode to absolute to traverse the link list */
  209. iwl_set_otp_access_absolute(trans);
  210. /* checking for empty OTP or error */
  211. if (iwl_is_otp_empty(trans))
  212. return -EINVAL;
  213. /*
  214. * start traverse link list
  215. * until reach the max number of OTP blocks
  216. * different devices have different number of OTP blocks
  217. */
  218. do {
  219. /* save current valid block address
  220. * check for more block on the link list
  221. */
  222. valid_addr = next_link_addr;
  223. next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
  224. IWL_DEBUG_EEPROM(trans->dev, "OTP blocks %d addr 0x%x\n",
  225. usedblocks, next_link_addr);
  226. if (iwl_read_otp_word(trans, next_link_addr, &link_value))
  227. return -EINVAL;
  228. if (!link_value) {
  229. /*
  230. * reach the end of link list, return success and
  231. * set address point to the starting address
  232. * of the image
  233. */
  234. *validblockaddr = valid_addr;
  235. /* skip first 2 bytes (link list pointer) */
  236. *validblockaddr += 2;
  237. return 0;
  238. }
  239. /* more in the link list, continue */
  240. usedblocks++;
  241. } while (usedblocks <= trans->trans_cfg->base_params->max_ll_items);
  242. /* OTP has no valid blocks */
  243. IWL_DEBUG_EEPROM(trans->dev, "OTP has no valid blocks\n");
  244. return -EINVAL;
  245. }
  246. /*
  247. * iwl_read_eeprom - read EEPROM contents
  248. *
  249. * Load the EEPROM contents from adapter and return it
  250. * and its size.
  251. *
  252. * NOTE: This routine uses the non-debug IO access functions.
  253. */
  254. int iwl_read_eeprom(struct iwl_trans *trans, u8 **eeprom, size_t *eeprom_size)
  255. {
  256. __le16 *e;
  257. u32 gp = iwl_read32(trans, CSR_EEPROM_GP);
  258. int sz;
  259. int ret;
  260. u16 addr;
  261. u16 validblockaddr = 0;
  262. u16 cache_addr = 0;
  263. int nvm_is_otp;
  264. if (!eeprom || !eeprom_size)
  265. return -EINVAL;
  266. nvm_is_otp = iwl_nvm_is_otp(trans);
  267. if (nvm_is_otp < 0)
  268. return nvm_is_otp;
  269. sz = trans->trans_cfg->base_params->eeprom_size;
  270. IWL_DEBUG_EEPROM(trans->dev, "NVM size = %d\n", sz);
  271. e = kmalloc(sz, GFP_KERNEL);
  272. if (!e)
  273. return -ENOMEM;
  274. ret = iwl_eeprom_verify_signature(trans, nvm_is_otp);
  275. if (ret < 0) {
  276. IWL_ERR(trans, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  277. goto err_free;
  278. }
  279. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  280. ret = iwl_eeprom_acquire_semaphore(trans);
  281. if (ret < 0) {
  282. IWL_ERR(trans, "Failed to acquire EEPROM semaphore.\n");
  283. goto err_free;
  284. }
  285. if (nvm_is_otp) {
  286. ret = iwl_init_otp_access(trans);
  287. if (ret) {
  288. IWL_ERR(trans, "Failed to initialize OTP access.\n");
  289. goto err_unlock;
  290. }
  291. iwl_write32(trans, CSR_EEPROM_GP,
  292. iwl_read32(trans, CSR_EEPROM_GP) &
  293. ~CSR_EEPROM_GP_IF_OWNER_MSK);
  294. iwl_set_bit(trans, CSR_OTP_GP_REG,
  295. CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
  296. CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
  297. /* traversing the linked list if no shadow ram supported */
  298. if (!trans->trans_cfg->base_params->shadow_ram_support) {
  299. ret = iwl_find_otp_image(trans, &validblockaddr);
  300. if (ret)
  301. goto err_unlock;
  302. }
  303. for (addr = validblockaddr; addr < validblockaddr + sz;
  304. addr += sizeof(u16)) {
  305. __le16 eeprom_data;
  306. ret = iwl_read_otp_word(trans, addr, &eeprom_data);
  307. if (ret)
  308. goto err_unlock;
  309. e[cache_addr / 2] = eeprom_data;
  310. cache_addr += sizeof(u16);
  311. }
  312. } else {
  313. /* eeprom is an array of 16bit values */
  314. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  315. u32 r;
  316. iwl_write32(trans, CSR_EEPROM_REG,
  317. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  318. ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
  319. CSR_EEPROM_REG_READ_VALID_MSK,
  320. CSR_EEPROM_REG_READ_VALID_MSK,
  321. IWL_EEPROM_ACCESS_TIMEOUT);
  322. if (ret < 0) {
  323. IWL_ERR(trans,
  324. "Time out reading EEPROM[%d]\n", addr);
  325. goto err_unlock;
  326. }
  327. r = iwl_read32(trans, CSR_EEPROM_REG);
  328. e[addr / 2] = cpu_to_le16(r >> 16);
  329. }
  330. }
  331. IWL_DEBUG_EEPROM(trans->dev, "NVM Type: %s\n",
  332. nvm_is_otp ? "OTP" : "EEPROM");
  333. iwl_eeprom_release_semaphore(trans);
  334. *eeprom_size = sz;
  335. *eeprom = (u8 *)e;
  336. return 0;
  337. err_unlock:
  338. iwl_eeprom_release_semaphore(trans);
  339. err_free:
  340. kfree(e);
  341. return ret;
  342. }
  343. IWL_EXPORT_SYMBOL(iwl_read_eeprom);