common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /******************************************************************************
  2. * Copyright (C) 2019-2021 NXP
  3. * *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. ******************************************************************************/
  19. #include <linux/of_gpio.h>
  20. #include <linux/of_device.h>
  21. #include <linux/delay.h>
  22. #include <linux/version.h>
  23. #include "common.h"
  24. #include "common_ese.h"
  25. #if defined(RECOVERY_ENABLE)
  26. #include "recovery_seq.h"
  27. #endif
  28. int nfc_parse_dt(struct device *dev, platform_configs_t *nfc_configs,
  29. uint8_t interface)
  30. {
  31. struct device_node *np = dev->of_node;
  32. platform_gpio_t *nfc_gpio = &nfc_configs->gpio;
  33. if (!np) {
  34. pr_err("nfc of_node NULL\n");
  35. return -EINVAL;
  36. }
  37. nfc_gpio->irq = -EINVAL;
  38. nfc_gpio->dwl_req = -EINVAL;
  39. nfc_gpio->ven = -EINVAL;
  40. //required for i2c based chips only
  41. if (interface == PLATFORM_IF_I2C) {
  42. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  43. if ((!gpio_is_valid(nfc_gpio->irq))) {
  44. pr_err("nfc irq gpio invalid %d\n", nfc_gpio->irq);
  45. return -EINVAL;
  46. }
  47. pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
  48. }
  49. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  50. if ((!gpio_is_valid(nfc_gpio->ven))) {
  51. pr_err("nfc ven gpio invalid %d\n", nfc_gpio->ven);
  52. return -EINVAL;
  53. }
  54. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  55. if ((!gpio_is_valid(nfc_gpio->dwl_req))) {
  56. pr_warn("nfc dwl_req gpio invalid %d\n", nfc_gpio->dwl_req);
  57. }
  58. pr_info("%s: %d, %d, %d, %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  59. nfc_gpio->dwl_req);
  60. return 0;
  61. }
  62. void set_valid_gpio(int gpio, int value)
  63. {
  64. if (gpio_is_valid(gpio)) {
  65. pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
  66. gpio_set_value(gpio, value);
  67. // hardware dependent delay
  68. usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
  69. NFC_GPIO_SET_WAIT_TIME_USEC + 100);
  70. }
  71. }
  72. int get_valid_gpio(int gpio)
  73. {
  74. int value = -1;
  75. if (gpio_is_valid(gpio)) {
  76. value = gpio_get_value(gpio);
  77. pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
  78. }
  79. return value;
  80. }
  81. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  82. {
  83. platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
  84. if (gpio_get_value(nfc_gpio->ven) != value) {
  85. pr_debug("%s: gpio_set_ven %d\n", __func__, value);
  86. /*reset on change in level from high to low */
  87. if (value) {
  88. common_ese_on_hard_reset(nfc_dev);
  89. }
  90. gpio_set_value(nfc_gpio->ven, value);
  91. // hardware dependent delay
  92. usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
  93. NFC_GPIO_SET_WAIT_TIME_USEC + 100);
  94. }
  95. }
  96. int configure_gpio(unsigned int gpio, int flag)
  97. {
  98. int ret;
  99. pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  100. if (gpio_is_valid(gpio)) {
  101. ret = gpio_request(gpio, "nfc_gpio");
  102. if (ret) {
  103. pr_err("%s: unable to request nfc gpio [%d]\n", __func__, gpio);
  104. return ret;
  105. }
  106. /*set direction and value for output pin */
  107. if (flag & GPIO_OUTPUT) {
  108. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  109. pr_debug("nfc o/p gpio %d level %d\n", gpio, gpio_get_value(gpio));
  110. } else {
  111. ret = gpio_direction_input(gpio);
  112. pr_debug("nfc i/p gpio %d\n", gpio);
  113. }
  114. if (ret) {
  115. pr_err("%s: unable to set direction for nfc gpio [%d]\n", __func__, gpio);
  116. gpio_free(gpio);
  117. return ret;
  118. }
  119. /*Consider value as control for input IRQ pin */
  120. if (flag & GPIO_IRQ) {
  121. ret = gpio_to_irq(gpio);
  122. if (ret < 0) {
  123. pr_err("%s: unable to set irq for nfc gpio [%d]\n", __func__, gpio);
  124. gpio_free(gpio);
  125. return ret;
  126. }
  127. pr_debug("%s: gpio_to_irq successful [%d]\n", __func__, gpio);
  128. return ret;
  129. }
  130. } else {
  131. pr_err("%s: invalid gpio\n", __func__);
  132. ret = -EINVAL;
  133. }
  134. return ret;
  135. }
  136. void gpio_free_all(nfc_dev_t *nfc_dev)
  137. {
  138. platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
  139. if (gpio_is_valid(nfc_gpio->dwl_req)) {
  140. gpio_free(nfc_gpio->dwl_req);
  141. }
  142. if (gpio_is_valid(nfc_gpio->irq)) {
  143. gpio_free(nfc_gpio->irq);
  144. }
  145. if (gpio_is_valid(nfc_gpio->ven)) {
  146. gpio_free(nfc_gpio->ven);
  147. }
  148. }
  149. void nfc_misc_unregister(nfc_dev_t *nfc_dev, int count)
  150. {
  151. pr_debug("%s: entry\n", __func__);
  152. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  153. cdev_del(&nfc_dev->c_dev);
  154. class_destroy(nfc_dev->nfc_class);
  155. unregister_chrdev_region(nfc_dev->devno, count);
  156. }
  157. int nfc_misc_register(nfc_dev_t *nfc_dev,
  158. const struct file_operations *nfc_fops,
  159. int count, char *devname, char *classname)
  160. {
  161. int ret = 0;
  162. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  163. if (ret < 0) {
  164. pr_err("%s: failed to alloc chrdev region ret %d\n", __func__, ret);
  165. return ret;
  166. }
  167. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  168. if (IS_ERR(nfc_dev->nfc_class)) {
  169. ret = PTR_ERR(nfc_dev->nfc_class);
  170. pr_err("%s: failed to register device class ret %d\n", __func__, ret);
  171. unregister_chrdev_region(nfc_dev->devno, count);
  172. return ret;
  173. }
  174. cdev_init(&nfc_dev->c_dev, nfc_fops);
  175. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  176. if (ret < 0) {
  177. pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
  178. class_destroy(nfc_dev->nfc_class);
  179. unregister_chrdev_region(nfc_dev->devno, count);
  180. return ret;
  181. }
  182. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  183. nfc_dev->devno, nfc_dev, devname);
  184. if (IS_ERR(nfc_dev->nfc_device)) {
  185. ret = PTR_ERR(nfc_dev->nfc_device);
  186. pr_err("%s: failed to create the device ret %d\n", __func__, ret);
  187. cdev_del(&nfc_dev->c_dev);
  188. class_destroy(nfc_dev->nfc_class);
  189. unregister_chrdev_region(nfc_dev->devno, count);
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. /*
  195. * nfc_ioctl_power_states() - power control
  196. * @nfc_dev: nfc device data structure
  197. * @arg: mode that we want to move to
  198. *
  199. * Device power control. Depending on the arg value, device moves to
  200. * different states, refer common.h for args
  201. *
  202. * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
  203. */
  204. static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
  205. {
  206. int ret = 0;
  207. platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
  208. if (arg == NFC_POWER_OFF) {
  209. /*
  210. * We are attempting a hardware reset so let us disable
  211. * interrupts to avoid spurious notifications to upper
  212. * layers.
  213. */
  214. nfc_dev->nfc_disable_intr(nfc_dev);
  215. set_valid_gpio(nfc_gpio->dwl_req, 0);
  216. gpio_set_ven(nfc_dev, 0);
  217. nfc_dev->nfc_ven_enabled = false;
  218. } else if (arg == NFC_POWER_ON) {
  219. nfc_dev->nfc_enable_intr(nfc_dev);
  220. set_valid_gpio(nfc_gpio->dwl_req, 0);
  221. gpio_set_ven(nfc_dev, 1);
  222. nfc_dev->nfc_ven_enabled = true;
  223. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  224. /*
  225. * We are switching to download Mode, toggle the enable pin
  226. * in order to set the NFCC in the new mode
  227. */
  228. nfc_dev->nfc_disable_intr(nfc_dev);
  229. set_valid_gpio(nfc_gpio->dwl_req, 1);
  230. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  231. gpio_set_ven(nfc_dev, 0);
  232. gpio_set_ven(nfc_dev, 1);
  233. nfc_dev->nfc_enable_intr(nfc_dev);
  234. } else if (arg == NFC_FW_DWL_HIGH) {
  235. /*
  236. * Setting firmware download gpio to HIGH
  237. * before FW download start
  238. */
  239. set_valid_gpio(nfc_gpio->dwl_req, 1);
  240. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  241. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  242. nfc_dev->nfc_disable_intr(nfc_dev);
  243. gpio_set_ven(nfc_dev, 0);
  244. gpio_set_ven(nfc_dev, 1);
  245. nfc_dev->nfc_enable_intr(nfc_dev);
  246. } else if (arg == NFC_FW_DWL_LOW) {
  247. /*
  248. * Setting firmware download gpio to LOW
  249. * FW download finished
  250. */
  251. set_valid_gpio(nfc_gpio->dwl_req, 0);
  252. nfc_dev->nfc_state = NFC_STATE_NCI;
  253. } else {
  254. pr_err("%s bad arg %lu\n", __func__, arg);
  255. ret = -ENOIOCTLCMD;
  256. }
  257. return ret;
  258. }
  259. /** @brief IOCTL function to be used to set or get data from upper layer.
  260. *
  261. * @param pfile fil node for opened device.
  262. * @cmd IOCTL type from upper layer.
  263. * @arg IOCTL arg from upper layer.
  264. *
  265. * @return 0 on success, error code for failures.
  266. */
  267. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  268. {
  269. int ret = 0;
  270. struct nfc_dev *nfc_dev = pfile->private_data;
  271. if (!nfc_dev)
  272. return -ENODEV;
  273. pr_debug("%s cmd = %x arg = %zx\n", __func__, cmd, arg);
  274. switch (cmd) {
  275. case NFC_SET_PWR:
  276. ret = nfc_ioctl_power_states(nfc_dev, arg);
  277. break;
  278. case ESE_SET_PWR:
  279. ret = nfc_ese_pwr(nfc_dev, arg);
  280. break;
  281. case ESE_GET_PWR:
  282. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  283. break;
  284. case NFC_GET_PLATFORM_TYPE:
  285. ret = nfc_dev->interface;
  286. break;
  287. case NFC_GET_NFC_STATE:
  288. ret = nfc_dev->nfc_state;
  289. pr_debug("nfc get state %d\n", ret);
  290. break;
  291. case NFC_GET_IRQ_STATE:
  292. ret = gpio_get_value(nfc_dev->configs.gpio.irq);
  293. break;
  294. default:
  295. pr_err("%s bad cmd %lu\n", __func__, arg);
  296. ret = -ENOIOCTLCMD;
  297. };
  298. return ret;
  299. }
  300. int nfc_dev_open(struct inode *inode, struct file *filp)
  301. {
  302. nfc_dev_t *nfc_dev = container_of(inode->i_cdev, nfc_dev_t, c_dev);
  303. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  304. mutex_lock(&nfc_dev->dev_ref_mutex);
  305. filp->private_data = nfc_dev;
  306. if (nfc_dev->dev_ref_count == 0) {
  307. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  308. nfc_dev->nfc_enable_intr(nfc_dev);
  309. }
  310. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  311. mutex_unlock(&nfc_dev->dev_ref_mutex);
  312. return 0;
  313. }
  314. int nfc_dev_close(struct inode *inode, struct file *filp)
  315. {
  316. nfc_dev_t *nfc_dev = container_of(inode->i_cdev, nfc_dev_t, c_dev);
  317. pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  318. mutex_lock(&nfc_dev->dev_ref_mutex);
  319. if (nfc_dev->dev_ref_count == 1) {
  320. nfc_dev->nfc_disable_intr(nfc_dev);
  321. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  322. }
  323. if (nfc_dev->dev_ref_count > 0)
  324. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  325. else {
  326. nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS_NFC);
  327. /* Uncomment below line incase of eSE calls flow is via NFC driver
  328. * i.e. direct calls from SPI HAL to NFC driver*/
  329. //nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS);
  330. }
  331. filp->private_data = NULL;
  332. mutex_unlock(&nfc_dev->dev_ref_mutex);
  333. return 0;
  334. }
  335. static int get_nfcc_boot_state(struct nfc_dev *nfc_dev)
  336. {
  337. int ret = 0;
  338. char get_version_cmd[] = { 0x00, 0x04, 0xF1, 0x00, 0x00, 0x00, 0x6E, 0xEF };
  339. char get_session_state_cmd[] = { 0x00, 0x04, 0xF2, 0x00, 0x00, 0x00, 0xF5, 0x33 };
  340. char rsp_buf[MAX_BUFFER_SIZE];
  341. pr_debug("%s:Sending GET_VERSION cmd\n", __func__);
  342. ret = nfc_dev->nfc_write(nfc_dev, get_version_cmd,
  343. sizeof(get_version_cmd), MAX_RETRY_COUNT);
  344. if (ret <= 0) {
  345. pr_err("%s: - nfc get version cmd error ret %d\n", __func__, ret);
  346. goto err;
  347. }
  348. memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
  349. pr_debug("%s:Reading response of GET_VERSION cmd\n", __func__);
  350. ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, DL_GET_VERSION_RSP_LEN_2, NCI_CMD_RSP_TIMEOUT);
  351. if (ret <= 0) {
  352. pr_err("%s: - nfc get version rsp error ret %d\n", __func__, ret);
  353. goto err;
  354. } else if (rsp_buf[0] == FW_MSG_CMD_RSP
  355. && ret >= DL_GET_VERSION_RSP_LEN_2) {
  356. #if defined(RECOVERY_ENABLE)
  357. nfc_dev->fw_major_version = rsp_buf[FW_MAJOR_VER_OFFSET];
  358. /* recvoery neeeded only for SN1xx */
  359. if(rsp_buf[FW_ROM_CODE_VER_OFFSET] == RECOVERY_FW_SUPPORTED_ROM_VER &&
  360. nfc_dev->fw_major_version == RECOVERY_FW_SUPPORTED_MAJOR_VER)
  361. nfc_dev->recovery_required = true;
  362. #endif
  363. pr_info("%s:NFC chip_type 0x%02x rom_version 0x%02x fw_minor 0x%02x fw_major 0x%02x\n",
  364. __func__, rsp_buf[3], rsp_buf[4], rsp_buf[6], rsp_buf[7]);
  365. } else if (rsp_buf[0] != FW_MSG_CMD_RSP
  366. && ret >= (NCI_HDR_LEN + rsp_buf[NCI_PAYLOAD_LEN_IDX])) {
  367. pr_info("%s:NFC response bytes 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
  368. rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3], rsp_buf[3]);
  369. pr_debug("%s NFCC booted in NCI mode %d\n", __func__, __LINE__);
  370. return NFC_STATE_NCI;
  371. }
  372. pr_debug("%s:Sending GET_SESSION_STATE cmd \n", __func__);
  373. ret = nfc_dev->nfc_write(nfc_dev, get_session_state_cmd,
  374. sizeof(get_session_state_cmd),
  375. MAX_RETRY_COUNT);
  376. if (ret <= 0) {
  377. pr_err("%s: - nfc get session state cmd err ret %d\n", __func__, ret);
  378. goto err;
  379. }
  380. memset(rsp_buf, 0x00, DL_GET_SESSION_STATE_RSP_LEN);
  381. pr_debug("%s:Reading response of GET_SESSION_STATE cmd\n", __func__);
  382. ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, DL_GET_SESSION_STATE_RSP_LEN, NCI_CMD_RSP_TIMEOUT);
  383. if (ret <= 0) {
  384. pr_err("%s: - nfc get session state rsp err %d\n", __func__, ret);
  385. goto err;
  386. }
  387. pr_debug("Response bytes are %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x",
  388. rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3], rsp_buf[4], rsp_buf[5],
  389. rsp_buf[6], rsp_buf[7]);
  390. /*verify fw in non-teared state */
  391. if (rsp_buf[GET_SESSION_STS_OFF] != NFCC_SESSION_STS_CLOSED) {
  392. pr_debug("%s NFCC booted in teared fw state %d\n", __func__, __LINE__);
  393. return NFC_STATE_FW_TEARED;
  394. }
  395. pr_debug("%s NFCC booted in FW DN mode %d\n", __func__, __LINE__);
  396. return NFC_STATE_FW_DWL;
  397. err:
  398. pr_err("%s Unlikely NFCC not booted in FW DN mode %d\n", __func__, __LINE__);
  399. return NFC_STATE_UNKNOWN;
  400. }
  401. int validate_nfc_state_nci(nfc_dev_t *nfc_dev)
  402. {
  403. platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
  404. if (!gpio_get_value(nfc_gpio->ven)) {
  405. pr_err("VEN LOW - NFCC powered off\n");
  406. return -ENODEV;
  407. } else {
  408. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  409. pr_err("FW download in-progress\n");
  410. return -EBUSY;
  411. } else if (nfc_dev->nfc_state == NFC_STATE_FW_DWL) {
  412. pr_err("FW download state \n");
  413. return -EBUSY;
  414. }
  415. }
  416. return 0;
  417. }
  418. static int set_nfcc_nci_state(struct nfc_dev *nfc_dev)
  419. {
  420. int ret = 0;
  421. char dl_reset_cmd[] = { 0x00, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x18, 0x5B };
  422. pr_debug("%s:Sending DL_RESET to boot in NCI mode\n", __func__);
  423. ret = nfc_dev->nfc_write(nfc_dev, dl_reset_cmd,
  424. sizeof(dl_reset_cmd), MAX_RETRY_COUNT);
  425. if (ret <= 0) {
  426. pr_err("%s: nfc dl reset cmd err ret %d\n", __func__, ret);
  427. goto err;
  428. }
  429. usleep_range(NFC_SOFT_RESET_WAIT_TIME_USEC,
  430. NFC_SOFT_RESET_WAIT_TIME_USEC + 100);
  431. pr_debug("%s NFCC booted in NCI mode %d\n", __func__, __LINE__);
  432. return 0;
  433. err:
  434. pr_err("%s Unlikely NFCC not booted in NCI mode %d\n", __func__, __LINE__);
  435. return -1;
  436. }
  437. static bool do_nci_reset(nfc_dev_t *nfc_dev) {
  438. const uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00};
  439. char rsp_buf[MAX_BUFFER_SIZE];
  440. int status = 0;
  441. if (NULL == nfc_dev) {
  442. pr_err("%s invalid params ", __func__);
  443. return false;
  444. }
  445. pr_debug("%s Entry \n", __func__);
  446. gpio_set_ven(nfc_dev, 0);
  447. gpio_set_ven(nfc_dev, 1);
  448. pr_debug(" %s send core reset cmd \n", __func__);
  449. status = nfc_dev->nfc_write(nfc_dev, cmd_reset_nci,
  450. sizeof(cmd_reset_nci), NO_RETRY);
  451. if (status <= 0) {
  452. pr_err(" %s: nfc nci core reset cmd err status %d\n", __func__, status);
  453. return false;
  454. }
  455. usleep_range(NCI_RESET_RESP_READ_DELAY, NCI_RESET_RESP_READ_DELAY + 100);
  456. nfc_dev->nfc_enable_intr(nfc_dev);
  457. pr_debug(" %s Reading response of NCI reset \n", __func__);
  458. memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
  459. status = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE, NCI_RESET_RESP_TIMEOUT);
  460. if (status <= 0) {
  461. pr_err(" %s - nfc nci rest rsp error status %d\n", __func__, status);
  462. nfc_dev->nfc_disable_intr(nfc_dev);
  463. return false;
  464. }
  465. pr_debug(" %s: nci core reset response 0x%02x 0x%02x 0x%02x 0x%02x \n",
  466. __func__, rsp_buf[0], rsp_buf[1],rsp_buf[2], rsp_buf[3]);
  467. if(rsp_buf[0] != NCI_MSG_RSP) {
  468. /* reset response failed response*/
  469. pr_err("%s invalid nci core reset response");
  470. nfc_dev->nfc_disable_intr(nfc_dev);
  471. return false;
  472. }
  473. memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
  474. /* read nci rest response ntf */
  475. status = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE, NCI_CMD_RSP_TIMEOUT);
  476. if (status <= 0) {
  477. pr_err("%s - nfc nci rest rsp ntf error status %d\n"
  478. , __func__, status);
  479. }
  480. pr_debug(" %s:NFC NCI Reset Response ntf 0x%02x 0x%02x 0x%02x 0x%02x \n",
  481. __func__, rsp_buf[0], rsp_buf[1],rsp_buf[2], rsp_buf[3]);
  482. nfc_dev->nfc_disable_intr(nfc_dev);
  483. gpio_set_ven(nfc_dev, 0);
  484. gpio_set_ven(nfc_dev, 1);
  485. return true;
  486. }
  487. /* Check for availability of NFC controller hardware */
  488. int nfcc_hw_check(struct nfc_dev *nfc_dev)
  489. {
  490. int ret = 0;
  491. uint8_t nfc_state = NFC_STATE_UNKNOWN;
  492. if(do_nci_reset(nfc_dev)) {
  493. pr_info("%s recovery not required", __func__);
  494. return ret;
  495. }
  496. nfc_dev->nfc_enable_intr(nfc_dev);
  497. /*set download mode for i2c products with dwl pin */
  498. enable_dwnld_mode(nfc_dev, true);
  499. nfc_state = get_nfcc_boot_state(nfc_dev);
  500. switch (nfc_state) {
  501. case NFC_STATE_FW_DWL:
  502. usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
  503. NFC_GPIO_SET_WAIT_TIME_USEC + 100);
  504. if (set_nfcc_nci_state(nfc_dev)) {
  505. pr_debug("%s: - NFCC DL Reset Fails\n", __func__);
  506. } else {
  507. nfc_state = NFC_STATE_NCI;
  508. /*set NCI mode for i2c products with dwl pin */
  509. enable_dwnld_mode(nfc_dev, false);
  510. }
  511. /* fall-through */
  512. case NFC_STATE_NCI:
  513. nfc_dev->nfc_ven_enabled = true;
  514. pr_debug("%s: - NFCC HW detected\n", __func__);
  515. break;
  516. case NFC_STATE_FW_TEARED:
  517. pr_warn("%s: - NFCC FW Teared State\n", __func__);
  518. #if defined(RECOVERY_ENABLE)
  519. if(nfc_dev->recovery_required &&
  520. (do_recovery(nfc_dev) == STATUS_SUCCESS)) {
  521. pr_debug("%s: - NFCC HW detected\n", __func__);
  522. }
  523. #endif
  524. nfc_dev->nfc_ven_enabled = true;
  525. break;
  526. case NFC_STATE_UNKNOWN:
  527. default:
  528. ret = -ENXIO;
  529. pr_debug("%s: - NFCC HW not available\n", __func__);
  530. };
  531. nfc_dev->nfc_disable_intr(nfc_dev);
  532. if (nfc_state == NFC_STATE_FW_TEARED) {
  533. nfc_state = NFC_STATE_FW_DWL;
  534. }
  535. nfc_dev->nfc_state = nfc_state;
  536. return ret;
  537. }
  538. void enable_dwnld_mode(nfc_dev_t* nfc_dev, bool value) {
  539. if(nfc_dev != NULL) {
  540. platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
  541. if (get_valid_gpio(nfc_gpio->dwl_req) != -1) {
  542. set_valid_gpio(nfc_gpio->dwl_req, value);
  543. gpio_set_ven(nfc_dev, 0);
  544. gpio_set_ven(nfc_dev, 1);
  545. }
  546. }
  547. }
  548. void set_nfcc_state_from_rsp(struct nfc_dev *dev, const char *buf,
  549. const int count)
  550. {
  551. int packet_size = 0;
  552. if (buf[0] == FW_MSG_CMD_RSP && buf[1] >= FW_MIN_PAYLOAD_LEN) {
  553. packet_size = FW_HDR_LEN + buf[FW_PAYLOAD_LEN_IDX] + FW_CRC_LEN;
  554. if (packet_size == count && dev->nfc_state == NFC_STATE_NCI)
  555. dev->nfc_state = NFC_STATE_FW_DWL;
  556. } else {
  557. packet_size = NCI_HDR_LEN + buf[NCI_PAYLOAD_LEN_IDX];
  558. if (packet_size == count && dev->nfc_state == NFC_STATE_FW_DWL)
  559. dev->nfc_state = NFC_STATE_NCI;
  560. }
  561. if (count != packet_size) {
  562. pr_err("%s: Unlikely mismatch in packet size received (%d/%d)/\n", __func__,
  563. packet_size, count);
  564. }
  565. }