common.c 20 KB

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