123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #include "common.h"
- #include "common_nxp.h"
- static enum chip_types get_nfcc_chip_type_dl(struct nfc_dev *nfc_dev)
- {
- int ret = 0;
- uint8_t *cmd = nfc_dev->write_kbuf;
- uint8_t *rsp = nfc_dev->read_kbuf;
- enum chip_types chip_type = CHIP_UNKNOWN;
- *cmd++ = DL_CMD;
- *cmd++ = DL_GET_VERSION_CMD_PAYLOAD_LEN;
- *cmd++ = DL_GET_VERSION_CMD_ID;
- *cmd++ = DL_PAYLOAD_BYTE_ZERO;
- *cmd++ = DL_PAYLOAD_BYTE_ZERO;
- *cmd++ = DL_PAYLOAD_BYTE_ZERO;
- *cmd++ = DL_GET_VERSION_CMD_CRC_1;
- *cmd++ = DL_GET_VERSION_CMD_CRC_2;
- pr_debug("NxpDrv: %s:Sending GET_VERSION cmd of size = %d\n", __func__, DL_GET_VERSION_CMD_LEN);
- ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, DL_GET_VERSION_CMD_LEN,
- MAX_RETRY_COUNT);
- if (ret <= 0) {
- pr_err("NxpDrv: %s: - nfc get version cmd error ret %d\n", __func__, ret);
- goto err;
- }
- memset(rsp, 0x00, DL_GET_VERSION_RSP_LEN_2);
- pr_debug("NxpDrv: %s:Reading response of GET_VERSION cmd\n", __func__);
- ret = nfc_dev->nfc_read(nfc_dev, rsp, DL_GET_VERSION_RSP_LEN_2, NCI_CMD_RSP_TIMEOUT_MS);
- if (ret <= 0) {
- pr_err("NxpDrv: %s: - nfc get version rsp error ret %d\n", __func__, ret);
- goto err;
- }
- if (rsp[0] == FW_MSG_CMD_RSP && ret >= DL_GET_VERSION_RSP_LEN_2) {
- nfc_dev->fw_major_version = rsp[FW_MAJOR_VER_OFFSET];
- if (rsp[FW_ROM_CODE_VER_OFFSET] == SN1XX_ROM_VER &&
- rsp[FW_MAJOR_VER_OFFSET] == SN1xx_MAJOR_VER)
- chip_type = CHIP_SN1XX;
- else if (rsp[FW_ROM_CODE_VER_OFFSET] == SN220_ROM_VER &&
- rsp[FW_MAJOR_VER_OFFSET] == SN220_MAJOR_VER)
- chip_type = CHIP_SN220;
- pr_debug("NxpDrv: %s:NFC Chip Type 0x%02x Rom Version 0x%02x FW Minor 0x%02x Major 0x%02x\n",
- __func__, rsp[GET_VERSION_RSP_CHIP_TYPE_OFFSET],
- rsp[FW_ROM_CODE_VER_OFFSET],
- rsp[GET_VERSION_RSP_MINOR_VERSION_OFFSET],
- rsp[FW_MAJOR_VER_OFFSET]);
- nfc_dev->nqx_info.info.chip_type = rsp[GET_VERSION_RSP_CHIP_TYPE_OFFSET];
- nfc_dev->nqx_info.info.rom_version = rsp[FW_ROM_CODE_VER_OFFSET];
- nfc_dev->nqx_info.info.fw_minor = rsp[GET_VERSION_RSP_MINOR_VERSION_OFFSET];
- nfc_dev->nqx_info.info.fw_major = rsp[FW_MAJOR_VER_OFFSET];
- }
- err:
- return chip_type;
- }
- enum nfc_state_flags get_nfcc_session_state_dl(struct nfc_dev *nfc_dev)
- {
- int ret = 0;
- uint8_t *cmd = nfc_dev->write_kbuf;
- uint8_t *rsp = nfc_dev->read_kbuf;
- enum nfc_state_flags nfc_state = NFC_STATE_UNKNOWN;
- *cmd++ = DL_CMD;
- *cmd++ = DL_GET_SESSION_STATE_CMD_PAYLOAD_LEN;
- *cmd++ = DL_GET_SESSION_CMD_ID;
- *cmd++ = DL_PAYLOAD_BYTE_ZERO;
- *cmd++ = DL_PAYLOAD_BYTE_ZERO;
- *cmd++ = DL_PAYLOAD_BYTE_ZERO;
- *cmd++ = DL_GET_SESSION_CMD_CRC_1;
- *cmd++ = DL_GET_SESSION_CMD_CRC_2;
- pr_debug("NxpDrv: %s:Sending GET_SESSION_STATE cmd of size = %d\n", __func__,
- DL_GET_SESSION_STATE_CMD_LEN);
- ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, DL_GET_SESSION_STATE_CMD_LEN,
- MAX_RETRY_COUNT);
- if (ret <= 0) {
- pr_err("NxpDrv: %s: - nfc get session cmd error ret %d\n", __func__, ret);
- goto err;
- }
- memset(rsp, 0x00, DL_GET_SESSION_STATE_RSP_LEN);
- pr_debug("NxpDrv: %s:Reading response of GET_SESSION_STATE cmd\n", __func__);
- ret = nfc_dev->nfc_read(nfc_dev, rsp, DL_GET_SESSION_STATE_RSP_LEN, NCI_CMD_RSP_TIMEOUT_MS);
- if (ret <= 0) {
- pr_err("NxpDrv: %s: - nfc get session rsp error ret %d\n", __func__, ret);
- goto err;
- }
- if (rsp[0] != FW_MSG_CMD_RSP) {
- pr_err("NxpDrv: %s: - nfc invalid get session state rsp\n", __func__);
- goto err;
- }
- pr_debug("NxpDrv: Response bytes are %02x%02x%02x%02x%02x%02x%02x%02x\n",
- rsp[0], rsp[1], rsp[2], rsp[3], rsp[4], rsp[5], rsp[6], rsp[7]);
-
- if (rsp[GET_SESSION_STS_OFF] != NFCC_SESSION_STS_CLOSED) {
- pr_err("NxpDrv: %s NFCC booted in FW teared state\n", __func__);
- nfc_state = NFC_STATE_FW_TEARED;
- } else {
- pr_info("NxpDrv: %s NFCC booted in FW DN mode\n", __func__);
- nfc_state = NFC_STATE_FW_DWL;
- }
- err:
- return nfc_state;
- }
- static enum chip_types get_nfcc_chip_type(struct nfc_dev *nfc_dev)
- {
- int ret = 0;
- uint8_t major_version = 0;
- uint8_t rom_version = 0;
- uint8_t *cmd = nfc_dev->write_kbuf;
- uint8_t *rsp = nfc_dev->read_kbuf;
- enum chip_types chip_type = CHIP_UNKNOWN;
- *cmd++ = NCI_CMD;
- *cmd++ = NCI_CORE_RESET_CMD_OID;
- *cmd++ = NCI_CORE_RESET_CMD_PAYLOAD_LEN;
- *cmd++ = NCI_CORE_RESET_KEEP_CONFIG;
- pr_debug("NxpDrv: %s:Sending NCI Core Reset cmd of size = %d\n", __func__, NCI_RESET_CMD_LEN);
- ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, NCI_RESET_CMD_LEN, NO_RETRY);
- if (ret <= 0) {
- pr_err("NxpDrv: %s: - nfc nci core reset cmd error ret %d\n", __func__, ret);
- goto err;
- }
-
- usleep_range(NCI_RESET_RESP_READ_DELAY, NCI_RESET_RESP_READ_DELAY + 100);
- nfc_dev->nfc_enable_intr(nfc_dev);
- memset(rsp, 0x00, NCI_RESET_RSP_LEN);
- pr_debug("NxpDrv: %s:Reading NCI Core Reset rsp\n", __func__);
- ret = nfc_dev->nfc_read(nfc_dev, rsp, NCI_RESET_RSP_LEN, NCI_CMD_RSP_TIMEOUT_MS);
- if (ret <= 0) {
- pr_err("NxpDrv: %s: - nfc nci core reset rsp error ret %d\n", __func__, ret);
- goto err_disable_intr;
- }
- pr_debug("NxpDrv: %s: nci core reset response 0x%02x%02x%02x%02x\n",
- __func__, rsp[0], rsp[1], rsp[2], rsp[3]);
- if (rsp[0] != NCI_RSP) {
-
- pr_err("NxpDrv: %s invalid nci core reset response\n", __func__);
- goto err_disable_intr;
- }
- memset(rsp, 0x00, NCI_RESET_NTF_LEN);
-
- ret = nfc_dev->nfc_read(nfc_dev, rsp, NCI_RESET_NTF_LEN, NCI_CMD_RSP_TIMEOUT_MS);
- if (ret <= 0) {
- pr_err("NxpDrv: %s - nfc nci rest rsp ntf error status %d\n", __func__, ret);
- goto err_disable_intr;
- }
- if (rsp[0] == NCI_NTF) {
-
- rom_version = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] - 3];
- major_version = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] - 2];
-
- if (rom_version == SN1XX_ROM_VER && major_version == SN1xx_MAJOR_VER)
- chip_type = CHIP_SN1XX;
- else if (rom_version == SN220_ROM_VER && major_version == SN220_MAJOR_VER)
- chip_type = CHIP_SN220;
- pr_debug("NxpDrv: %s:NCI Core Reset ntf 0x%02x%02x%02x%02x\n",
- __func__, rsp[0], rsp[1], rsp[2], rsp[3]);
- nfc_dev->nqx_info.info.chip_type = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] -
- NFC_CHIP_TYPE_OFF];
- nfc_dev->nqx_info.info.rom_version = rom_version;
- nfc_dev->nqx_info.info.fw_major = major_version;
- nfc_dev->nqx_info.info.fw_minor = rsp[NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_IDX] -
- NFC_FW_MINOR_OFF];
- }
- err_disable_intr:
- nfc_dev->nfc_disable_intr(nfc_dev);
- err:
- return chip_type;
- }
- static bool validate_download_gpio(struct nfc_dev *nfc_dev, enum chip_types chip_type)
- {
- bool status = false;
- struct platform_gpio *nfc_gpio;
- if (nfc_dev == NULL) {
- pr_err("NxpDrv: %s nfc devices structure is null\n", __func__);
- return status;
- }
- nfc_gpio = &nfc_dev->configs.gpio;
- if (chip_type == CHIP_SN1XX) {
-
- status = gpio_is_valid(nfc_gpio->dwl_req);
- } else if (chip_type == CHIP_SN220) {
-
- set_valid_gpio(nfc_gpio->dwl_req, 0);
- gpio_free(nfc_gpio->dwl_req);
- nfc_gpio->dwl_req = -EINVAL;
- status = true;
- }
- return status;
- }
- int nfcc_hw_check(struct nfc_dev *nfc_dev)
- {
- int ret = 0;
- enum nfc_state_flags nfc_state = NFC_STATE_UNKNOWN;
- enum chip_types chip_type = CHIP_UNKNOWN;
- struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
-
- gpio_set_ven(nfc_dev, 1);
- gpio_set_ven(nfc_dev, 0);
- gpio_set_ven(nfc_dev, 1);
- chip_type = get_nfcc_chip_type(nfc_dev);
-
- if (chip_type == CHIP_UNKNOWN) {
- nfc_dev->nfc_enable_intr(nfc_dev);
-
- set_valid_gpio(nfc_gpio->dwl_req, 1);
- gpio_set_ven(nfc_dev, 0);
- gpio_set_ven(nfc_dev, 1);
- chip_type = get_nfcc_chip_type_dl(nfc_dev);
-
- } else {
- nfc_state = NFC_STATE_NCI;
- }
-
- if (!validate_download_gpio(nfc_dev, chip_type)) {
- pr_info("NxpDrv: %s gpio validation fail\n", __func__);
- ret = -ENXIO;
- goto err;
- }
-
- if (nfc_state != NFC_STATE_NCI)
- nfc_state = get_nfcc_session_state_dl(nfc_dev);
-
- switch (nfc_state) {
- case NFC_STATE_FW_TEARED:
- pr_warn("NxpDrv: %s: - NFCC FW Teared State\n", __func__);
- break;
- case NFC_STATE_FW_DWL:
- case NFC_STATE_NCI:
- break;
- case NFC_STATE_UNKNOWN:
- default:
- ret = -ENXIO;
- pr_err("NxpDrv: %s: - NFCC HW not available\n", __func__);
- goto err;
- }
- nfc_dev->nfc_state = nfc_state;
- err:
- nfc_dev->nfc_disable_intr(nfc_dev);
- set_valid_gpio(nfc_gpio->dwl_req, 0);
- gpio_set_ven(nfc_dev, 0);
- gpio_set_ven(nfc_dev, 1);
- nfc_dev->nfc_ven_enabled = true;
- return ret;
- }
|