common_nxp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2019-2021 NXP
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. ******************************************************************************/
  21. /*
  22. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  23. *
  24. ******************************************************************************/
  25. #include "common.h"
  26. #include "common_nxp.h"
  27. /**
  28. * get_nfcc_chip_type_dl() - get chip type in fw download command;
  29. * @nfc_dev: nfc device data structure
  30. *
  31. * Perform get version command and determine chip
  32. * type from response.
  33. *
  34. * @Return: enum chip_types value
  35. *
  36. */
  37. static enum chip_types get_nfcc_chip_type_dl(struct nfc_dev *nfc_dev)
  38. {
  39. int ret = 0;
  40. uint8_t *cmd = nfc_dev->write_kbuf;
  41. uint8_t *rsp = nfc_dev->read_kbuf;
  42. enum chip_types chip_type = CHIP_UNKNOWN;
  43. *cmd++ = DL_CMD;
  44. *cmd++ = DL_GET_VERSION_CMD_PAYLOAD_LEN;
  45. *cmd++ = DL_GET_VERSION_CMD_ID;
  46. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  47. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  48. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  49. *cmd++ = DL_GET_VERSION_CMD_CRC_1;
  50. *cmd++ = DL_GET_VERSION_CMD_CRC_2;
  51. pr_debug("NxpDrv: %s:Sending GET_VERSION cmd of size = %d\n", __func__, DL_GET_VERSION_CMD_LEN);
  52. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, DL_GET_VERSION_CMD_LEN,
  53. MAX_RETRY_COUNT);
  54. if (ret <= 0) {
  55. pr_err("NxpDrv: %s: - nfc get version cmd error ret %d\n", __func__, ret);
  56. goto err;
  57. }
  58. memset(rsp, 0x00, DL_GET_VERSION_RSP_LEN_2);
  59. pr_debug("NxpDrv: %s:Reading response of GET_VERSION cmd\n", __func__);
  60. ret = nfc_dev->nfc_read(nfc_dev, rsp, DL_GET_VERSION_RSP_LEN_2, NCI_CMD_RSP_TIMEOUT_MS);
  61. if (ret <= 0) {
  62. pr_err("NxpDrv: %s: - nfc get version rsp error ret %d\n", __func__, ret);
  63. goto err;
  64. }
  65. if (rsp[0] == FW_MSG_CMD_RSP && ret >= DL_GET_VERSION_RSP_LEN_2) {
  66. nfc_dev->fw_major_version = rsp[FW_MAJOR_VER_OFFSET];
  67. if (rsp[FW_ROM_CODE_VER_OFFSET] == SN1XX_ROM_VER &&
  68. rsp[FW_MAJOR_VER_OFFSET] == SN1xx_MAJOR_VER)
  69. chip_type = CHIP_SN1XX;
  70. else if (rsp[FW_ROM_CODE_VER_OFFSET] == SN220_ROM_VER &&
  71. rsp[FW_MAJOR_VER_OFFSET] == SN220_MAJOR_VER)
  72. chip_type = CHIP_SN220;
  73. pr_debug("NxpDrv: %s:NFC Chip Type 0x%02x Rom Version 0x%02x FW Minor 0x%02x Major 0x%02x\n",
  74. __func__, rsp[GET_VERSION_RSP_CHIP_TYPE_OFFSET],
  75. rsp[FW_ROM_CODE_VER_OFFSET],
  76. rsp[GET_VERSION_RSP_MINOR_VERSION_OFFSET],
  77. rsp[FW_MAJOR_VER_OFFSET]);
  78. nfc_dev->nqx_info.info.chip_type = rsp[GET_VERSION_RSP_CHIP_TYPE_OFFSET];
  79. nfc_dev->nqx_info.info.rom_version = rsp[FW_ROM_CODE_VER_OFFSET];
  80. nfc_dev->nqx_info.info.fw_minor = rsp[GET_VERSION_RSP_MINOR_VERSION_OFFSET];
  81. nfc_dev->nqx_info.info.fw_major = rsp[FW_MAJOR_VER_OFFSET];
  82. }
  83. err:
  84. return chip_type;
  85. }
  86. /**
  87. * get_nfcc_session_state_dl() - gets the session state
  88. * @nfc_dev: nfc device data structure
  89. *
  90. * Performs get session command and determine
  91. * the nfcc state based on session status.
  92. *
  93. * @Return nfcc state based on session status.
  94. * NFC_STATE_FW_TEARED if sessionis not closed
  95. * NFC_STATE_FW_DWL if session closed
  96. * NFC_STATE_UNKNOWN in error cases.
  97. */
  98. enum nfc_state_flags get_nfcc_session_state_dl(struct nfc_dev *nfc_dev)
  99. {
  100. int ret = 0;
  101. uint8_t *cmd = nfc_dev->write_kbuf;
  102. uint8_t *rsp = nfc_dev->read_kbuf;
  103. enum nfc_state_flags nfc_state = NFC_STATE_UNKNOWN;
  104. *cmd++ = DL_CMD;
  105. *cmd++ = DL_GET_SESSION_STATE_CMD_PAYLOAD_LEN;
  106. *cmd++ = DL_GET_SESSION_CMD_ID;
  107. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  108. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  109. *cmd++ = DL_PAYLOAD_BYTE_ZERO;
  110. *cmd++ = DL_GET_SESSION_CMD_CRC_1;
  111. *cmd++ = DL_GET_SESSION_CMD_CRC_2;
  112. pr_debug("NxpDrv: %s:Sending GET_SESSION_STATE cmd of size = %d\n", __func__,
  113. DL_GET_SESSION_STATE_CMD_LEN);
  114. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, DL_GET_SESSION_STATE_CMD_LEN,
  115. MAX_RETRY_COUNT);
  116. if (ret <= 0) {
  117. pr_err("NxpDrv: %s: - nfc get session cmd error ret %d\n", __func__, ret);
  118. goto err;
  119. }
  120. memset(rsp, 0x00, DL_GET_SESSION_STATE_RSP_LEN);
  121. pr_debug("NxpDrv: %s:Reading response of GET_SESSION_STATE cmd\n", __func__);
  122. ret = nfc_dev->nfc_read(nfc_dev, rsp, DL_GET_SESSION_STATE_RSP_LEN, NCI_CMD_RSP_TIMEOUT_MS);
  123. if (ret <= 0) {
  124. pr_err("NxpDrv: %s: - nfc get session rsp error ret %d\n", __func__, ret);
  125. goto err;
  126. }
  127. if (rsp[0] != FW_MSG_CMD_RSP) {
  128. pr_err("NxpDrv: %s: - nfc invalid get session state rsp\n", __func__);
  129. goto err;
  130. }
  131. pr_debug("NxpDrv: Response bytes are %02x%02x%02x%02x%02x%02x%02x%02x\n",
  132. rsp[0], rsp[1], rsp[2], rsp[3], rsp[4], rsp[5], rsp[6], rsp[7]);
  133. /*verify fw in non-teared state */
  134. if (rsp[GET_SESSION_STS_OFF] != NFCC_SESSION_STS_CLOSED) {
  135. pr_err("NxpDrv: %s NFCC booted in FW teared state\n", __func__);
  136. nfc_state = NFC_STATE_FW_TEARED;
  137. } else {
  138. pr_info("NxpDrv: %s NFCC booted in FW DN mode\n", __func__);
  139. nfc_state = NFC_STATE_FW_DWL;
  140. }
  141. err:
  142. return nfc_state;
  143. }
  144. /**
  145. * get_nfcc_chip_type() - get nfcc chip type in nci mode.
  146. * @nfc_dev: nfc device data structure.
  147. *
  148. * Function to perform nci core reset and extract
  149. * chip type from the response.
  150. *
  151. * @Return: enum chip_types value
  152. *
  153. */
  154. static enum chip_types get_nfcc_chip_type(struct nfc_dev *nfc_dev)
  155. {
  156. int ret = 0;
  157. uint8_t major_version = 0;
  158. uint8_t rom_version = 0;
  159. uint8_t *cmd = nfc_dev->write_kbuf;
  160. uint8_t *rsp = nfc_dev->read_kbuf;
  161. enum chip_types chip_type = CHIP_UNKNOWN;
  162. *cmd++ = NCI_CMD;
  163. *cmd++ = NCI_CORE_RESET_CMD_OID;
  164. *cmd++ = NCI_CORE_RESET_CMD_PAYLOAD_LEN;
  165. *cmd++ = NCI_CORE_RESET_KEEP_CONFIG;
  166. pr_debug("NxpDrv: %s:Sending NCI Core Reset cmd of size = %d\n", __func__, NCI_RESET_CMD_LEN);
  167. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, NCI_RESET_CMD_LEN, NO_RETRY);
  168. if (ret <= 0) {
  169. pr_err("NxpDrv: %s: - nfc nci core reset cmd error ret %d\n", __func__, ret);
  170. goto err;
  171. }
  172. /* to flush out debug NTF this delay is required */
  173. usleep_range(NCI_RESET_RESP_READ_DELAY, NCI_RESET_RESP_READ_DELAY + 100);
  174. nfc_dev->nfc_enable_intr(nfc_dev);
  175. memset(rsp, 0x00, NCI_RESET_RSP_LEN);
  176. pr_debug("NxpDrv: %s:Reading NCI Core Reset rsp\n", __func__);
  177. ret = nfc_dev->nfc_read(nfc_dev, rsp, NCI_RESET_RSP_LEN, NCI_CMD_RSP_TIMEOUT_MS);
  178. if (ret <= 0) {
  179. pr_err("NxpDrv: %s: - nfc nci core reset rsp error ret %d\n", __func__, ret);
  180. goto err_disable_intr;
  181. }
  182. pr_debug("NxpDrv: %s: nci core reset response 0x%02x%02x%02x%02x\n",
  183. __func__, rsp[0], rsp[1], rsp[2], rsp[3]);
  184. if (rsp[0] != NCI_RSP) {
  185. /* reset response failed response*/
  186. pr_err("NxpDrv: %s invalid nci core reset response\n", __func__);
  187. goto err_disable_intr;
  188. }
  189. memset(rsp, 0x00, NCI_RESET_NTF_LEN);
  190. /* read nci rest response ntf */
  191. ret = nfc_dev->nfc_read(nfc_dev, rsp, NCI_RESET_NTF_LEN, NCI_CMD_RSP_TIMEOUT_MS);
  192. if (ret <= 0) {
  193. pr_err("NxpDrv: %s - nfc nci rest rsp ntf error status %d\n", __func__, ret);
  194. goto err_disable_intr;
  195. }
  196. if (rsp[0] == NCI_NTF) {
  197. /* read version info from NCI Reset Notification */
  198. rom_version = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] - 3];
  199. major_version = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] - 2];
  200. /* determine chip type based on version info */
  201. if (rom_version == SN1XX_ROM_VER && major_version == SN1xx_MAJOR_VER)
  202. chip_type = CHIP_SN1XX;
  203. else if (rom_version == SN220_ROM_VER && major_version == SN220_MAJOR_VER)
  204. chip_type = CHIP_SN220;
  205. pr_debug("NxpDrv: %s:NCI Core Reset ntf 0x%02x%02x%02x%02x\n",
  206. __func__, rsp[0], rsp[1], rsp[2], rsp[3]);
  207. nfc_dev->nqx_info.info.chip_type = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] -
  208. NFC_CHIP_TYPE_OFF];
  209. nfc_dev->nqx_info.info.rom_version = rom_version;
  210. nfc_dev->nqx_info.info.fw_major = major_version;
  211. nfc_dev->nqx_info.info.fw_minor = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] -
  212. NFC_FW_MINOR_OFF];
  213. }
  214. err_disable_intr:
  215. nfc_dev->nfc_disable_intr(nfc_dev);
  216. err:
  217. return chip_type;
  218. }
  219. /**
  220. * validate_download_gpio() - validate download gpio.
  221. * @nfc_dev: nfc_dev device data structure.
  222. * @chip_type: chip type of the platform.
  223. *
  224. * Validates dwnld gpio should configured for supported and
  225. * should not be configured for unsupported platform.
  226. *
  227. * @Return: true if gpio validation successful ortherwise
  228. * false if validation fails.
  229. */
  230. static bool validate_download_gpio(struct nfc_dev *nfc_dev, enum chip_types chip_type)
  231. {
  232. bool status = false;
  233. struct platform_gpio *nfc_gpio;
  234. if (nfc_dev == NULL) {
  235. pr_err("NxpDrv: %s nfc devices structure is null\n", __func__);
  236. return status;
  237. }
  238. nfc_gpio = &nfc_dev->configs.gpio;
  239. if (chip_type == CHIP_SN1XX) {
  240. /* gpio should be configured for SN1xx */
  241. status = gpio_is_valid(nfc_gpio->dwl_req);
  242. } else if (chip_type == CHIP_SN220) {
  243. /* gpio should not be configured for SN220 */
  244. set_valid_gpio(nfc_gpio->dwl_req, 0);
  245. gpio_free(nfc_gpio->dwl_req);
  246. nfc_gpio->dwl_req = -EINVAL;
  247. status = true;
  248. }
  249. return status;
  250. }
  251. /* Check for availability of NFC controller hardware */
  252. int nfcc_hw_check(struct nfc_dev *nfc_dev)
  253. {
  254. int ret = 0;
  255. enum nfc_state_flags nfc_state = NFC_STATE_UNKNOWN;
  256. enum chip_types chip_type = CHIP_UNKNOWN;
  257. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  258. /*get fw version in nci mode*/
  259. gpio_set_ven(nfc_dev, 1);
  260. gpio_set_ven(nfc_dev, 0);
  261. gpio_set_ven(nfc_dev, 1);
  262. chip_type = get_nfcc_chip_type(nfc_dev);
  263. /*get fw version in fw dwl mode*/
  264. if (chip_type == CHIP_UNKNOWN) {
  265. nfc_dev->nfc_enable_intr(nfc_dev);
  266. /*Chip is unknown, initially assume with fw dwl pin enabled*/
  267. set_valid_gpio(nfc_gpio->dwl_req, 1);
  268. gpio_set_ven(nfc_dev, 0);
  269. gpio_set_ven(nfc_dev, 1);
  270. chip_type = get_nfcc_chip_type_dl(nfc_dev);
  271. /*get the state of nfcc normal/teared in fw dwl mode*/
  272. } else {
  273. nfc_state = NFC_STATE_NCI;
  274. }
  275. /*validate gpio config required as per the chip*/
  276. if (!validate_download_gpio(nfc_dev, chip_type)) {
  277. pr_info("NxpDrv: %s gpio validation fail\n", __func__);
  278. ret = -ENXIO;
  279. goto err;
  280. }
  281. /*check whether the NFCC is in FW DN or Teared state*/
  282. if (nfc_state != NFC_STATE_NCI)
  283. nfc_state = get_nfcc_session_state_dl(nfc_dev);
  284. /*nfcc state specific operations */
  285. switch (nfc_state) {
  286. case NFC_STATE_FW_TEARED:
  287. pr_warn("NxpDrv: %s: - NFCC FW Teared State\n", __func__);
  288. break;
  289. case NFC_STATE_FW_DWL:
  290. case NFC_STATE_NCI:
  291. break;
  292. case NFC_STATE_UNKNOWN:
  293. default:
  294. ret = -ENXIO;
  295. pr_err("NxpDrv: %s: - NFCC HW not available\n", __func__);
  296. goto err;
  297. }
  298. nfc_dev->nfc_state = nfc_state;
  299. err:
  300. nfc_dev->nfc_disable_intr(nfc_dev);
  301. set_valid_gpio(nfc_gpio->dwl_req, 0);
  302. gpio_set_ven(nfc_dev, 0);
  303. gpio_set_ven(nfc_dev, 1);
  304. nfc_dev->nfc_ven_enabled = true;
  305. return ret;
  306. }