Updated corresponding to - NFC_AR_00_02.00_6000_12.51.00_OpnSrc

This commit is contained in:
nxf35421
2021-02-12 22:49:38 +05:30
parent bedce83629
commit 2ec82a2bfd
10 changed files with 2023 additions and 133 deletions

View File

@@ -2,5 +2,5 @@
# Makefile for nfc devices
#
obj-$(CONFIG_NXP_NFC_I2C) += pn553_i2c.o
pn553_i2c-objs := common.o common_ese.o i2c_drv.o
#ccflags-y := -DDEBUG
pn553_i2c-objs := common.o common_ese.o i2c_drv.o recovery_seq.o recovery_fw.o
ccflags-y += -DRECOVERY_ENABLE -UDEBUG

View File

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (C) 2019-2020 NXP
* Copyright (C) 2019-2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -23,10 +23,15 @@
#include "common.h"
#include "common_ese.h"
int nfc_parse_dt(struct device *dev, platform_gpio_t * nfc_gpio,
#if defined(RECOVERY_ENABLE)
#include "recovery_seq.h"
#endif
int nfc_parse_dt(struct device *dev, platform_configs_t *nfc_configs,
uint8_t interface)
{
struct device_node *np = dev->of_node;
platform_gpio_t *nfc_gpio = &nfc_configs->gpio;
if (!np) {
pr_err("nfc of_node NULL\n");
@@ -45,11 +50,6 @@ int nfc_parse_dt(struct device *dev, platform_gpio_t * nfc_gpio,
return -EINVAL;
}
pr_info("%s: irq %d\n", __func__, nfc_gpio->irq);
nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
if ((!gpio_is_valid(nfc_gpio->dwl_req))) {
pr_err("nfc dwl_req gpio invalid %d\n", nfc_gpio->dwl_req);
}
}
nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
if ((!gpio_is_valid(nfc_gpio->ven))) {
@@ -57,6 +57,11 @@ int nfc_parse_dt(struct device *dev, platform_gpio_t * nfc_gpio,
return -EINVAL;
}
nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
if ((!gpio_is_valid(nfc_gpio->dwl_req))) {
pr_warn("nfc dwl_req gpio invalid %d\n", nfc_gpio->dwl_req);
}
pr_info("%s: %d, %d, %d, %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
nfc_gpio->dwl_req);
return 0;
@@ -68,7 +73,8 @@ void set_valid_gpio(int gpio, int value)
pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
gpio_set_value(gpio, value);
// hardware dependent delay
usleep_range(10000, 10100);
usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
NFC_GPIO_SET_WAIT_TIME_USEC + 100);
}
}
@@ -84,15 +90,17 @@ int get_valid_gpio(int gpio)
void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
{
if (gpio_get_value(nfc_dev->gpio.ven) != value) {
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
if (gpio_get_value(nfc_gpio->ven) != value) {
pr_debug("%s: gpio_set_ven %d\n", __func__, value);
/*reset on change in level from high to low */
if (value) {
common_ese_on_hard_reset(nfc_dev);
}
gpio_set_value(nfc_dev->gpio.ven, value);
gpio_set_value(nfc_gpio->ven, value);
// hardware dependent delay
usleep_range(10000, 10100);
usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
NFC_GPIO_SET_WAIT_TIME_USEC + 100);
}
}
@@ -140,14 +148,15 @@ int configure_gpio(unsigned int gpio, int flag)
void gpio_free_all(nfc_dev_t *nfc_dev)
{
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_free(nfc_dev->gpio.dwl_req);
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
if (gpio_is_valid(nfc_gpio->dwl_req)) {
gpio_free(nfc_gpio->dwl_req);
}
if (gpio_is_valid(nfc_dev->gpio.irq)) {
gpio_free(nfc_dev->gpio.irq);
if (gpio_is_valid(nfc_gpio->irq)) {
gpio_free(nfc_gpio->irq);
}
if (gpio_is_valid(nfc_dev->gpio.ven)) {
gpio_free(nfc_dev->gpio.ven);
if (gpio_is_valid(nfc_gpio->ven)) {
gpio_free(nfc_gpio->ven);
}
}
@@ -211,6 +220,7 @@ int nfc_misc_register(nfc_dev_t *nfc_dev,
static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
{
int ret = 0;
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
if (arg == NFC_POWER_OFF) {
/*
* We are attempting a hardware reset so let us disable
@@ -218,12 +228,12 @@ static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
* layers.
*/
nfc_dev->nfc_disable_intr(nfc_dev);
set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
set_valid_gpio(nfc_gpio->dwl_req, 0);
gpio_set_ven(nfc_dev, 0);
nfc_dev->nfc_ven_enabled = false;
} else if (arg == NFC_POWER_ON) {
nfc_dev->nfc_enable_intr(nfc_dev);
set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
set_valid_gpio(nfc_gpio->dwl_req, 0);
gpio_set_ven(nfc_dev, 1);
nfc_dev->nfc_ven_enabled = true;
@@ -233,10 +243,8 @@ static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
* in order to set the NFCC in the new mode
*/
nfc_dev->nfc_disable_intr(nfc_dev);
set_valid_gpio(nfc_dev->gpio.dwl_req, 1);
if (nfc_dev->interface == PLATFORM_IF_I2C) {
set_valid_gpio(nfc_gpio->dwl_req, 1);
nfc_dev->nfc_state = NFC_STATE_FW_DWL;
}
gpio_set_ven(nfc_dev, 0);
gpio_set_ven(nfc_dev, 1);
nfc_dev->nfc_enable_intr(nfc_dev);
@@ -245,10 +253,8 @@ static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
* Setting firmware download gpio to HIGH
* before FW download start
*/
set_valid_gpio(nfc_dev->gpio.dwl_req, 1);
if (nfc_dev->interface == PLATFORM_IF_I2C) {
set_valid_gpio(nfc_gpio->dwl_req, 1);
nfc_dev->nfc_state = NFC_STATE_FW_DWL;
}
} else if (arg == NFC_VEN_FORCED_HARD_RESET) {
nfc_dev->nfc_disable_intr(nfc_dev);
@@ -260,10 +266,8 @@ static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
* Setting firmware download gpio to LOW
* FW download finished
*/
set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
if (nfc_dev->interface == PLATFORM_IF_I2C) {
set_valid_gpio(nfc_gpio->dwl_req, 0);
nfc_dev->nfc_state = NFC_STATE_NCI;
}
} else {
pr_err("%s bad arg %lu\n", __func__, arg);
ret = -ENOIOCTLCMD;
@@ -305,8 +309,7 @@ long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
pr_debug("nfc get state %d\n", ret);
break;
case NFC_GET_IRQ_STATE:
ret = 0;
ret = gpio_get_value(nfc_dev->gpio.irq);
ret = gpio_get_value(nfc_dev->configs.gpio.irq);
break;
default:
pr_err("%s bad cmd %lu\n", __func__, arg);
@@ -325,7 +328,7 @@ int nfc_dev_open(struct inode *inode, struct file *filp)
filp->private_data = nfc_dev;
if (nfc_dev->dev_ref_count == 0) {
set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
nfc_dev->nfc_enable_intr(nfc_dev);
}
@@ -341,7 +344,7 @@ int nfc_dev_close(struct inode *inode, struct file *filp)
mutex_lock(&nfc_dev->dev_ref_mutex);
if (nfc_dev->dev_ref_count == 1) {
nfc_dev->nfc_disable_intr(nfc_dev);
set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
}
if (nfc_dev->dev_ref_count > 0)
nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
@@ -363,14 +366,6 @@ static int get_nfcc_boot_state(struct nfc_dev *nfc_dev)
char get_version_cmd[] = { 0x00, 0x04, 0xF1, 0x00, 0x00, 0x00, 0x6E, 0xEF };
char get_session_state_cmd[] = { 0x00, 0x04, 0xF2, 0x00, 0x00, 0x00, 0xF5, 0x33 };
char rsp_buf[MAX_BUFFER_SIZE];
/*clearing any data in the kbuf store */
do {
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE);
if (ret < 0) {
pr_err("%s: - nfc read ret %d\n", __func__, ret);
}
pr_info("%s: - nfc read ret %d\n", __func__, ret);
} while (ret > 0);
pr_debug("%s:Sending GET_VERSION cmd\n", __func__);
ret = nfc_dev->nfc_write(nfc_dev, get_version_cmd,
@@ -381,18 +376,23 @@ static int get_nfcc_boot_state(struct nfc_dev *nfc_dev)
}
memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
pr_debug("%s:Reading response of GET_VERSION cmd\n", __func__);
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE);
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, DL_GET_VERSION_RSP_LEN_2, NCI_CMD_RSP_TIMEOUT);
if (ret <= 0) {
pr_err("%s: - nfc get version rsp error ret %d\n", __func__, ret);
goto err;
} else if (rsp_buf[0] == 0x0
&& ret == (FW_HDR_LEN + rsp_buf[FW_PAYLOAD_LEN_IDX] + FW_CRC_LEN)
&& (ret == DL_GET_VERSION_RSP_LEN_1 || ret == DL_GET_VERSION_RSP_LEN_2)) {
} else if (rsp_buf[0] == FW_MSG_CMD_RSP
&& ret >= DL_GET_VERSION_RSP_LEN_2) {
#if defined(RECOVERY_ENABLE)
nfc_dev->fw_major_version = rsp_buf[FW_MAJOR_VER_OFFSET];
/* recvoery neeeded only for SN1xx */
if(rsp_buf[FW_ROM_CODE_VER_OFFSET] == RECOVERY_FW_SUPPORTED_ROM_VER &&
nfc_dev->fw_major_version == RECOVERY_FW_SUPPORTED_MAJOR_VER)
nfc_dev->recovery_required = true;
#endif
pr_info("%s:NFC chip_type 0x%02x rom_version 0x%02x fw_minor 0x%02x fw_major 0x%02x\n",
__func__, rsp_buf[3], rsp_buf[4], rsp_buf[6], rsp_buf[7]);
} else if (rsp_buf[0] != 0x0
&& ret == (NCI_HDR_LEN + rsp_buf[NCI_PAYLOAD_LEN_IDX])) {
} else if (rsp_buf[0] != FW_MSG_CMD_RSP
&& ret >= (NCI_HDR_LEN + rsp_buf[NCI_PAYLOAD_LEN_IDX])) {
pr_info("%s:NFC response bytes 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3], rsp_buf[3]);
pr_debug("%s NFCC booted in NCI mode %d\n", __func__, __LINE__);
@@ -409,7 +409,7 @@ static int get_nfcc_boot_state(struct nfc_dev *nfc_dev)
}
memset(rsp_buf, 0x00, DL_GET_SESSION_STATE_RSP_LEN);
pr_debug("%s:Reading response of GET_SESSION_STATE cmd\n", __func__);
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, DL_GET_SESSION_STATE_RSP_LEN);
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, DL_GET_SESSION_STATE_RSP_LEN, NCI_CMD_RSP_TIMEOUT);
if (ret <= 0) {
pr_err("%s: - nfc get session state rsp err %d\n", __func__, ret);
goto err;
@@ -431,11 +431,12 @@ err:
int validate_nfc_state_nci(nfc_dev_t *nfc_dev)
{
if (!gpio_get_value(nfc_dev->gpio.ven)) {
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
if (!gpio_get_value(nfc_gpio->ven)) {
pr_err("VEN LOW - NFCC powered off\n");
return -ENODEV;
} else {
if (get_valid_gpio(nfc_dev->gpio.dwl_req) == 1) {
if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
pr_err("FW download in-progress\n");
return -EBUSY;
} else if (nfc_dev->nfc_state == NFC_STATE_FW_DWL) {
@@ -445,3 +446,162 @@ int validate_nfc_state_nci(nfc_dev_t *nfc_dev)
}
return 0;
}
static int set_nfcc_nci_state(struct nfc_dev *nfc_dev)
{
int ret = 0;
char dl_reset_cmd[] = { 0x00, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x18, 0x5B };
pr_debug("%s:Sending DL_RESET to boot in NCI mode\n", __func__);
ret = nfc_dev->nfc_write(nfc_dev, dl_reset_cmd,
sizeof(dl_reset_cmd), MAX_RETRY_COUNT);
if (ret <= 0) {
pr_err("%s: nfc dl reset cmd err ret %d\n", __func__, ret);
goto err;
}
usleep_range(NFC_SOFT_RESET_WAIT_TIME_USEC,
NFC_SOFT_RESET_WAIT_TIME_USEC + 100);
pr_debug("%s NFCC booted in NCI mode %d\n", __func__, __LINE__);
return 0;
err:
pr_err("%s Unlikely NFCC not booted in NCI mode %d\n", __func__, __LINE__);
return -1;
}
static bool do_nci_reset(nfc_dev_t *nfc_dev) {
const uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00};
char rsp_buf[MAX_BUFFER_SIZE];
int status = 0;
if (NULL == nfc_dev) {
pr_err("%s invalid params ", __func__);
return false;
}
pr_debug("%s Entry \n", __func__);
gpio_set_ven(nfc_dev, 0);
gpio_set_ven(nfc_dev, 1);
pr_debug(" %s send core reset cmd \n", __func__);
status = nfc_dev->nfc_write(nfc_dev, cmd_reset_nci,
sizeof(cmd_reset_nci), NO_RETRY);
if (status <= 0) {
pr_err(" %s: nfc nci core reset cmd err status %d\n", __func__, status);
return false;
}
usleep_range(NCI_RESET_RESP_READ_DELAY, NCI_RESET_RESP_READ_DELAY + 100);
nfc_dev->nfc_enable_intr(nfc_dev);
pr_debug(" %s Reading response of NCI reset \n", __func__);
memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
status = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE, NCI_RESET_RESP_TIMEOUT);
if (status <= 0) {
pr_err(" %s - nfc nci rest rsp error status %d\n", __func__, status);
nfc_dev->nfc_disable_intr(nfc_dev);
return false;
}
pr_debug(" %s: nci core reset response 0x%02x 0x%02x 0x%02x 0x%02x \n",
__func__, rsp_buf[0], rsp_buf[1],rsp_buf[2], rsp_buf[3]);
if(rsp_buf[0] != NCI_MSG_RSP) {
/* reset response failed response*/
pr_err("%s invalid nci core reset response");
nfc_dev->nfc_disable_intr(nfc_dev);
return false;
}
memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
/* read nci rest response ntf */
status = nfc_dev->nfc_read(nfc_dev, rsp_buf, MAX_BUFFER_SIZE, NCI_CMD_RSP_TIMEOUT);
if (status <= 0) {
pr_err("%s - nfc nci rest rsp ntf error status %d\n"
, __func__, status);
}
pr_debug(" %s:NFC NCI Reset Response ntf 0x%02x 0x%02x 0x%02x 0x%02x \n",
__func__, rsp_buf[0], rsp_buf[1],rsp_buf[2], rsp_buf[3]);
nfc_dev->nfc_disable_intr(nfc_dev);
gpio_set_ven(nfc_dev, 0);
gpio_set_ven(nfc_dev, 1);
return true;
}
/* Check for availability of NFC controller hardware */
int nfcc_hw_check(struct nfc_dev *nfc_dev)
{
int ret = 0;
uint8_t nfc_state = NFC_STATE_UNKNOWN;
if(do_nci_reset(nfc_dev)) {
pr_info("%s recovery not required", __func__);
return ret;
}
nfc_dev->nfc_enable_intr(nfc_dev);
/*set download mode for i2c products with dwl pin */
enable_dwnld_mode(nfc_dev, true);
nfc_state = get_nfcc_boot_state(nfc_dev);
switch (nfc_state) {
case NFC_STATE_FW_DWL:
usleep_range(NFC_GPIO_SET_WAIT_TIME_USEC,
NFC_GPIO_SET_WAIT_TIME_USEC + 100);
if (set_nfcc_nci_state(nfc_dev)) {
pr_debug("%s: - NFCC DL Reset Fails\n", __func__);
} else {
nfc_state = NFC_STATE_NCI;
/*set NCI mode for i2c products with dwl pin */
enable_dwnld_mode(nfc_dev, false);
}
/* fall-through */
case NFC_STATE_NCI:
nfc_dev->nfc_ven_enabled = true;
pr_debug("%s: - NFCC HW detected\n", __func__);
break;
case NFC_STATE_FW_TEARED:
pr_warn("%s: - NFCC FW Teared State\n", __func__);
#if defined(RECOVERY_ENABLE)
if(nfc_dev->recovery_required &&
(do_recovery(nfc_dev) == STATUS_SUCCESS)) {
pr_debug("%s: - NFCC HW detected\n", __func__);
}
#endif
nfc_dev->nfc_ven_enabled = true;
break;
case NFC_STATE_UNKNOWN:
default:
ret = -ENXIO;
pr_debug("%s: - NFCC HW not available\n", __func__);
};
nfc_dev->nfc_disable_intr(nfc_dev);
if (nfc_state == NFC_STATE_FW_TEARED) {
nfc_state = NFC_STATE_FW_DWL;
}
nfc_dev->nfc_state = nfc_state;
return ret;
}
void enable_dwnld_mode(nfc_dev_t* nfc_dev, bool value) {
if(nfc_dev != NULL) {
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
if (get_valid_gpio(nfc_gpio->dwl_req) != -1) {
set_valid_gpio(nfc_gpio->dwl_req, value);
gpio_set_ven(nfc_dev, 0);
gpio_set_ven(nfc_dev, 1);
}
}
}
void set_nfcc_state_from_rsp(struct nfc_dev *dev, const char *buf,
const int count)
{
int packet_size = 0;
if (buf[0] == FW_MSG_CMD_RSP && buf[1] >= FW_MIN_PAYLOAD_LEN) {
packet_size = FW_HDR_LEN + buf[FW_PAYLOAD_LEN_IDX] + FW_CRC_LEN;
if (packet_size == count && dev->nfc_state == NFC_STATE_NCI)
dev->nfc_state = NFC_STATE_FW_DWL;
} else {
packet_size = NCI_HDR_LEN + buf[NCI_PAYLOAD_LEN_IDX];
if (packet_size == count && dev->nfc_state == NFC_STATE_FW_DWL)
dev->nfc_state = NFC_STATE_NCI;
}
if (count != packet_size) {
pr_err("%s: Unlikely mismatch in packet size received (%d/%d)/\n", __func__,
packet_size, count);
}
}

View File

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (C) 2019-2020 NXP
* Copyright (C) 2019-2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -41,11 +41,16 @@
#define NCI_HDR_LEN 3
#define NCI_PAYLOAD_IDX 3
#define NCI_PAYLOAD_LEN_IDX 2
/*Time to wait for first NCI rest response*/
#define NCI_RESET_RESP_READ_DELAY (10000) // 10ms
#define NCI_RESET_RESP_TIMEOUT (500) // 500ms
// FW DNLD packet details
#define FW_MSG_CMD_RSP 0x00
#define FW_HDR_LEN 2
#define FW_PAYLOAD_LEN_IDX 1
#define FW_CRC_LEN 2
#define FW_MIN_PAYLOAD_LEN 4
#define MIN_NFC_DL_FRAME_SIZE 3
#define NCI_RESET_CMD_LEN (4)
@@ -72,9 +77,17 @@
#define MAX_IRQ_WAIT_TIME (90)
#define WAKEUP_SRC_TIMEOUT (2000)
#define NCI_MAX_CMD_RSP_TIMEOUT (5000) //5s
/*command response timeout*/
#define NCI_CMD_RSP_TIMEOUT (2000) //2s
/*Time to wait for NFCC to be ready again after any change in the GPIO*/
#define NFC_GPIO_SET_WAIT_TIME_USEC (10000)
/*Time to wait after soft reset via any NCI/DL cmd*/
#define NFC_SOFT_RESET_WAIT_TIME_USEC (5000)
/*Time to wait before retrying i2c writes*/
#define WRITE_RETRY_WAIT_TIME_USEC (1000)
/*Time to wait before retrying read for some specific usecases*/
#define READ_RETRY_WAIT_TIME_USEC (3500)
#define NFC_MAGIC 0xE9
/*Ioctls*/
@@ -85,7 +98,7 @@
#define NFC_GET_PLATFORM_TYPE _IO(NFC_MAGIC, 0x04)
#define NFC_GET_NFC_STATE _IO(NFC_MAGIC, 0x05)
/* NFC HAL can call this ioctl to get the current IRQ state */
#define NFC_GET_IRQ_STATE _IOW(NFC_MAGIC, 0x06, long)
#define NFC_GET_IRQ_STATE _IO(NFC_MAGIC, 0x06)
#define DTS_IRQ_GPIO_STR "nxp,pn544-irq"
#define DTS_VEN_GPIO_STR "nxp,pn544-ven"
@@ -151,6 +164,11 @@ typedef struct platform_gpio {
unsigned int dwl_req;
} platform_gpio_t;
// NFC Struct to get all the required configs from DTS*/
typedef struct platform_configs {
platform_gpio_t gpio;
} platform_configs_t;
//cold reset Features specific Parameters
typedef struct cold_reset {
bool rsp_pending; /*cmd rsp pending status */
@@ -180,14 +198,20 @@ typedef struct nfc_dev {
uint8_t nfc_state;
/* NFC VEN pin state */
bool nfc_ven_enabled;
#if defined(RECOVERY_ENABLE)
/* current firmware major version */
uint8_t fw_major_version;
/* NFC recovery Required */
bool recovery_required;
#endif
union {
i2c_dev_t i2c_dev;
};
platform_gpio_t gpio;
platform_configs_t configs;
cold_reset_t cold_reset;
/*funtion pointers for the common i2c functionality */
int (*nfc_read) (struct nfc_dev *dev, char *buf, size_t count);
int (*nfc_read) (struct nfc_dev *dev, char *buf, size_t count, int timeout);
int (*nfc_write) (struct nfc_dev *dev, const char *buf, const size_t count,
int max_retry_cnt);
int (*nfc_enable_intr) (struct nfc_dev *dev);
@@ -198,7 +222,7 @@ typedef struct nfc_dev {
int nfc_dev_open(struct inode *inode, struct file *filp);
int nfc_dev_close(struct inode *inode, struct file *filp);
long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
int nfc_parse_dt(struct device *dev, platform_gpio_t *nfc_gpio,
int nfc_parse_dt(struct device *dev, platform_configs_t *nfc_configs,
uint8_t interface);
int nfc_misc_register(nfc_dev_t *nfc_dev,
const struct file_operations *nfc_fops, int count, char *devname,
@@ -208,4 +232,8 @@ int configure_gpio(unsigned int gpio, int flag);
void gpio_set_ven(nfc_dev_t *nfc_dev, int value);
void gpio_free_all(nfc_dev_t *nfc_dev);
int validate_nfc_state_nci(nfc_dev_t *nfc_dev);
int nfcc_hw_check(nfc_dev_t *nfc_dev);
void set_nfcc_state_from_rsp(nfc_dev_t *dev, const char *buf,
const int count);
void enable_dwnld_mode(nfc_dev_t* nfc_dev, bool value);
#endif //_COMMON_H_

View File

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (C) 2020 NXP
* Copyright (C) 2020-2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -143,6 +143,7 @@ static int perform_cold_reset_protection(nfc_dev_t *nfc_dev, unsigned long arg)
{
int ret = 0;
struct file filp;
uint8_t cld_rst_rsp[MAX_BUFFER_SIZE];
cold_reset_t *cold_reset = &nfc_dev->cold_reset;
bool nfc_dev_opened = false;
@@ -212,13 +213,12 @@ static int perform_cold_reset_protection(nfc_dev_t *nfc_dev, unsigned long arg)
} else {
/* Read data as NFC thread is not active */
filp.private_data = nfc_dev;
#if IS_ENABLED(CONFIG_NXP_NFC_I2C)
if (nfc_dev->interface == PLATFORM_IF_I2C) {
filp.f_flags &= ~O_NONBLOCK;
ret = nfc_i2c_dev_read(&filp, NULL, 3, 0);
usleep_range(3500, 4000);
}
#endif //IS_ENABLED(CONFIG_NXP_NFC_I2C)
ret = nfc_dev->nfc_read(nfc_dev, cld_rst_rsp, 3, NCI_CMD_RSP_TIMEOUT);
if (!ret)
break;
usleep_range(READ_RETRY_WAIT_TIME_USEC,
READ_RETRY_WAIT_TIME_USEC + 500);
}
} while (ret == -ERESTARTSYS || ret == -EFAULT);
@@ -250,6 +250,7 @@ err:
int nfc_ese_pwr(nfc_dev_t *nfc_dev, unsigned long arg)
{
int ret = 0;
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
if (arg == ESE_POWER_ON) {
/**
* Let's store the NFC VEN pin state
@@ -258,7 +259,7 @@ int nfc_ese_pwr(nfc_dev_t *nfc_dev, unsigned long arg)
* VEN state will remain HIGH if NFC is enabled otherwise
* it will be set as LOW
*/
nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_dev->gpio.ven);
nfc_dev->nfc_ven_enabled = gpio_get_value(nfc_gpio->ven);
if (!nfc_dev->nfc_ven_enabled) {
pr_debug("eSE HAL service setting ven HIGH\n");
gpio_set_ven(nfc_dev, 1);
@@ -274,7 +275,7 @@ int nfc_ese_pwr(nfc_dev_t *nfc_dev, unsigned long arg)
}
} else if (arg == ESE_POWER_STATE) {
// eSE get power state
ret = gpio_get_value(nfc_dev->gpio.ven);
ret = gpio_get_value(nfc_gpio->ven);
} else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
ret = perform_cold_reset_protection(nfc_dev, arg);
} else {

View File

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (C) 2020 NXP
* Copyright (C) 2020-2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View File

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (C) 2013-2020 NXP
* Copyright (C) 2013-2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -43,6 +43,8 @@
#include <linux/slab.h>
#include "common.h"
#include "common_ese.h"
static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, int timeout);
/**
* i2c_disable_irq()
*
@@ -100,23 +102,14 @@ static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
return IRQ_HANDLED;
}
int i2c_read(struct nfc_dev *dev, char *buf, size_t count)
{
int ret;
int i2c_read(struct nfc_dev *dev, char *buf, size_t count, int timeout){
pr_debug("%s : reading %zu bytes.\n", __func__, count);
/* Read data */
ret = i2c_master_recv(dev->i2c_dev.client, buf, count);
if (ret <= 0) {
pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
goto err;
}
if (ret > count) {
pr_err("%s: received too many bytes from i2c (%d)\n",
__func__, ret);
ret = -EIO;
}
err:
return ret;
if(timeout > NCI_MAX_CMD_RSP_TIMEOUT)
timeout = NCI_MAX_CMD_RSP_TIMEOUT;
return i2c_read_internal(dev, buf, count, timeout);
}
int i2c_write(struct nfc_dev *dev, const char *buf, size_t count,
@@ -132,53 +125,58 @@ int i2c_write(struct nfc_dev *dev, const char *buf, size_t count,
if (ret <= 0) {
pr_warn("%s: write failed, Maybe in Standby Mode - Retry(%d)\n", __func__,
retry_cnt);
usleep_range(1000, 1100);
usleep_range(WRITE_RETRY_WAIT_TIME_USEC,
WRITE_RETRY_WAIT_TIME_USEC + 100);
} else if (ret == count)
break;
}
return ret;
}
ssize_t nfc_i2c_dev_read(struct file * filp, char __user *buf,
size_t count, loff_t * offset)
static ssize_t i2c_read_internal(struct nfc_dev *dev, char *buf, size_t count, int timeout)
{
int ret;
char tmp[MAX_BUFFER_SIZE];
nfc_dev_t *nfc_dev = filp->private_data;
nfc_dev_t *nfc_dev = dev;
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
platform_gpio_t *nfc_gpio = &nfc_dev->configs.gpio;
if (count > MAX_BUFFER_SIZE)
count = MAX_BUFFER_SIZE;
pr_debug("%s : reading %zu bytes.\n", __func__, count);
mutex_lock(&nfc_dev->read_mutex);
if (!gpio_get_value(nfc_dev->gpio.irq)) {
if (filp->f_flags & O_NONBLOCK) {
pr_err(":f_falg has O_NONBLOCK. EAGAIN\n");
ret = -EAGAIN;
goto err;
}
if (!gpio_get_value(nfc_gpio->irq)) {
while (1) {
ret = 0;
if (!i2c_dev->irq_enabled) {
i2c_dev->irq_enabled = true;
enable_irq(i2c_dev->client->irq);
}
if (!gpio_get_value(nfc_dev->gpio.irq)) {
ret = wait_event_interruptible(nfc_dev->read_wq,
!i2c_dev->
irq_enabled);
if (!gpio_get_value(nfc_gpio->irq)) {
if(timeout) {
ret = wait_event_interruptible_timeout(nfc_dev->read_wq,
!i2c_dev-> irq_enabled, msecs_to_jiffies(timeout));
if (ret) {
pr_err("error wakeup of read wq\n");
if (ret <= 0) {
pr_err("%s timeout/error in read\n", __func__);
goto err;
}
} else {
ret = wait_event_interruptible(nfc_dev->read_wq,
!i2c_dev->irq_enabled);
if (ret) {
pr_err("%s error wakeup of read wq\n", __func__);
goto err;
}
}
}
i2c_disable_irq(nfc_dev);
if (gpio_get_value(nfc_dev->gpio.irq))
if (gpio_get_value(nfc_gpio->irq))
break;
if (!gpio_get_value(nfc_dev->gpio.ven)) {
if (!gpio_get_value(nfc_gpio->ven)) {
pr_info("%s: releasing read\n", __func__);
ret = -EIO;
goto err;
@@ -189,7 +187,7 @@ ssize_t nfc_i2c_dev_read(struct file * filp, char __user *buf,
memset(tmp, 0x00, count);
/* Read data */
ret = i2c_read(nfc_dev, tmp, count);
ret = i2c_master_recv(nfc_dev->i2c_dev.client, tmp, count);
if (ret <= 0) {
pr_err("%s: i2c_read returned %d\n", __func__, ret);
goto err;
@@ -201,8 +199,7 @@ ssize_t nfc_i2c_dev_read(struct file * filp, char __user *buf,
if (nfc_dev->cold_reset.rsp_pending) {
if (IS_PROP_CMD_RSP(tmp)) {
/* Read data */
ret =
i2c_read(nfc_dev, &tmp[NCI_PAYLOAD_IDX],
ret = i2c_master_recv(nfc_dev->i2c_dev.client, &tmp[NCI_PAYLOAD_IDX],
tmp[NCI_PAYLOAD_LEN_IDX]);
if (ret <= 0) {
pr_err("%s: failure to read prop cold reset/protection rsp header\n", __func__);
@@ -228,6 +225,17 @@ err:
return ret;
}
ssize_t nfc_i2c_dev_read(struct file * filp, char __user *buf,
size_t count, loff_t * offset)
{
pr_debug("%s : reading %zu bytes.\n", __func__, count);
if (filp->f_flags & O_NONBLOCK) {
pr_err(":f_falg has O_NONBLOCK. EAGAIN\n");
return -EAGAIN;
}
return i2c_read_internal((nfc_dev_t *)filp->private_data, buf, count, 0);
}
ssize_t nfc_i2c_dev_write(struct file * filp, const char __user *buf,
size_t count, loff_t * offset)
{
@@ -266,10 +274,11 @@ int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
int ret = 0;
nfc_dev_t *nfc_dev = NULL;
i2c_dev_t *i2c_dev = NULL;
platform_gpio_t nfc_gpio;
platform_configs_t nfc_configs;
platform_gpio_t *nfc_gpio = &nfc_configs.gpio;
pr_debug("%s: enter\n", __func__);
/*retrive details of gpios from dt */
ret = nfc_parse_dt(&client->dev, &nfc_gpio, PLATFORM_IF_I2C);
ret = nfc_parse_dt(&client->dev, &nfc_configs, PLATFORM_IF_I2C);
if (ret) {
pr_err("%s : failed to parse dt\n", __func__);
goto err;
@@ -293,26 +302,30 @@ int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
nfc_dev->nfc_write = i2c_write;
nfc_dev->nfc_enable_intr = i2c_enable_irq;
nfc_dev->nfc_disable_intr = i2c_disable_irq;
#if defined(RECOVERY_ENABLE)
nfc_dev->recovery_required = false;
nfc_dev->fw_major_version = 0;
#endif
ret = configure_gpio(nfc_gpio.ven, GPIO_OUTPUT);
ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
if (ret) {
pr_err("%s: unable to request nfc reset gpio [%d]\n", __func__, nfc_gpio.ven);
pr_err("%s: unable to request nfc reset gpio [%d]\n", __func__, nfc_gpio->ven);
goto err;
}
ret = configure_gpio(nfc_gpio.irq, GPIO_IRQ);
ret = configure_gpio(nfc_gpio->irq, GPIO_IRQ);
if (ret <= 0) {
pr_err("%s: unable to request nfc irq gpio [%d]\n", __func__, nfc_gpio.irq);
pr_err("%s: unable to request nfc irq gpio [%d]\n", __func__, nfc_gpio->irq);
goto err;
}
client->irq = ret;
ret = configure_gpio(nfc_gpio.dwl_req, GPIO_OUTPUT);
ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
if (ret) {
pr_err("%s: unable to request nfc firm downl gpio [%d]\n", __func__,
nfc_gpio.dwl_req);
nfc_gpio->dwl_req);
}
/*copy the retrived gpio details from DT */
memcpy(&nfc_dev->gpio, &nfc_gpio, sizeof(struct platform_gpio));
memcpy(&nfc_dev->configs, &nfc_configs, sizeof(struct platform_configs));
/* init mutex and queues */
init_waitqueue_head(&nfc_dev->read_wq);
@@ -337,16 +350,17 @@ int nfc_i2c_dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
goto err_nfc_misc_unregister;
}
i2c_disable_irq(nfc_dev);
ret = nfcc_hw_check(nfc_dev);
if (ret || nfc_dev->nfc_state == NFC_STATE_UNKNOWN) {
pr_err("nfc hw check failed ret %d\n", ret);
}
device_init_wakeup(&client->dev, true);
device_set_wakeup_capable(&client->dev, true);
i2c_set_clientdata(client, nfc_dev);
i2c_dev->irq_wake_up = false;
//reset nfc
usleep_range(10000, 10100);
gpio_set_value(nfc_dev->gpio.ven, 1);
usleep_range(10000, 10100);
pr_info("%s probing nfc i2c successfully", __func__);
return 0;
err_nfc_misc_unregister:
@@ -357,8 +371,10 @@ err_mutex_destroy:
mutex_destroy(&nfc_dev->ese_access_mutex);
mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
err:
if (nfc_dev) {
gpio_free_all(nfc_dev);
kfree(nfc_dev);
}
pr_err("%s: probing not successful, check hardware\n", __func__);
return ret;
}

View File

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (C) 2019-2020 NXP
* Copyright (C) 2019-2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -40,6 +40,5 @@ int nfc_i2c_dev_probe(struct i2c_client *client,
int nfc_i2c_dev_remove(struct i2c_client *client);
int nfc_i2c_dev_suspend(struct device *device);
int nfc_i2c_dev_resume(struct device *device);
ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf, size_t count,
loff_t * offset);
#endif //_I2C_DRV_H_

1325
nfc/recovery_fw.c Normal file

File diff suppressed because it is too large Load Diff

281
nfc/recovery_seq.c Normal file
View File

@@ -0,0 +1,281 @@
/******************************************************************************
* Copyright (C) 2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
******************************************************************************/
#if defined(RECOVERY_ENABLE)
#include "recovery_seq.h"
recovery_info_t g_recovery_info;
recovery_frame_t g_recovery_frame;
extern const uint32_t gphDnldNfc_DlSeqSz; /* Recovery user buffer size */
extern const uint8_t gphDnldNfc_DlSequence[]; /* Recovery user buffer */
/** @brief Function to calculate crc value.
*
* @param pbuffer: input buffer for crc calculation.
* dwLength: length of input buffer
* @return calculated uint16_t crc valueof input buffer.
*/
static uint16_t calcCrc16(uint8_t* pbuffer, uint32_t dwLength) {
uint32_t i = 0;
uint16_t crc_new = 0;
uint16_t crc = DL_INVALID_CRC_VALUE;
if(NULL == pbuffer) {
pr_err("%s, invalid params", __func__);
return crc;
}
for (i = 0; i < dwLength; i++) {
crc_new = (uint8_t)(crc >> MSB_POS) | (crc << MSB_POS );
crc_new ^= pbuffer[i];
crc_new ^= (uint8_t)(crc_new & DL_CRC_MASK) >> 4;
crc_new ^= crc_new << 12;
crc_new ^= (crc_new & DL_CRC_MASK) << 5;
crc = crc_new;
}
return crc;
}
/** @brief Function to build command frame for recover.
*
* @return status code of recovery_status_t type.
*/
static recovery_status_t build_cmd_frame() {
uint16_t len = 0;
uint16_t wCrc = 0;
uint16_t writeOffset = 0;
pr_debug(" %s Entry", __func__);
if(gphDnldNfc_DlSeqSz == 0) {
pr_err(" %s invalid params", __func__);
return STATUS_FAILED;
}
memset(g_recovery_frame.p_buffer, 0x00, MAX_FRAME_SIZE);
g_recovery_frame.len = 0;
if(g_recovery_info.bFrameSegmented == false) {
len = gphDnldNfc_DlSequence[g_recovery_info.currentReadOffset];
len <<= MSB_POS;
len |= gphDnldNfc_DlSequence[g_recovery_info.currentReadOffset + 1];
} else {
/* last frame was segmented frame
* read length reamaining length */
len = g_recovery_info.wRemChunkBytes;
}
if(len > MAX_DATA_SIZE) {
/* set remaining chunk */
g_recovery_info.wRemChunkBytes = (len - MAX_DATA_SIZE);
len = MAX_DATA_SIZE;
/* set chunk bit to write in header */
len = DL_SET_HDR_FRAGBIT(len);
g_recovery_frame.p_buffer[writeOffset++] = (len >> MSB_POS) & SHIFT_MASK;
g_recovery_frame.p_buffer[writeOffset++] = len & SHIFT_MASK;
/* clear chunk bit for length variable */
len = DL_CLR_HDR_FRAGBIT(len);
/* first chunk of segmented frame*/
if(!g_recovery_info.bFrameSegmented) {
/* ignore header from user buffer */
g_recovery_info.currentReadOffset += FW_HDR_LEN;
g_recovery_info.remBytes -= FW_HDR_LEN;
}
g_recovery_frame.len += FW_HDR_LEN;
g_recovery_info.bFrameSegmented = true;
} else {
/* last chunk of segmented frame */
if(g_recovery_info.bFrameSegmented) {
/* write header with user chunk length */
g_recovery_frame.p_buffer[writeOffset++] = (len >> MSB_POS) & SHIFT_MASK;
g_recovery_frame.p_buffer[writeOffset++] = len & SHIFT_MASK;
g_recovery_frame.len += FW_HDR_LEN;
} else {
/* normal Frame with in supported size increase
* len to read header from user data */
len += FW_HDR_LEN;
}
g_recovery_info.wRemChunkBytes = 0;
g_recovery_info.bFrameSegmented = false;
}
if(((writeOffset + len) > MAX_FRAME_SIZE) ||
((g_recovery_info.currentReadOffset + len) > gphDnldNfc_DlSeqSz)) {
pr_err("%s frame offsets out of bound",__func__);
return STATUS_FAILED;
}
memcpy(&g_recovery_frame.p_buffer[writeOffset],
&gphDnldNfc_DlSequence[g_recovery_info.currentReadOffset], len);
g_recovery_info.currentReadOffset += len;
g_recovery_frame.len += len;
writeOffset += len;
g_recovery_info.remBytes -= len;
wCrc = calcCrc16(g_recovery_frame.p_buffer,
g_recovery_frame.len);
g_recovery_frame.p_buffer[writeOffset++] = (wCrc >> MSB_POS) & SHIFT_MASK;
g_recovery_frame.p_buffer[writeOffset++] = wCrc & SHIFT_MASK;
g_recovery_frame.len += FW_CRC_LEN;
return STATUS_SUCCESS;
}
/** @brief Function to tramsmit recovery frame.
* @param nfc_dev nfc driver object.
* @return status code of recovery_status_t type.
*/
static recovery_status_t transmit(nfc_dev_t* nfc_dev) {
char rsp_buf[MAX_BUFFER_SIZE];
int ret = 0;
int frame_resp_len = 0;
uint16_t respCRC = 0;
uint16_t respCRCOffset = 0;
pr_debug("%s Entry", __func__);
if(NULL == nfc_dev || g_recovery_frame.len <= 0) {
pr_err("%s invalid Params ", __func__);
return STATUS_FAILED;
}
ret = nfc_dev->nfc_write(nfc_dev, g_recovery_frame.p_buffer,
g_recovery_frame.len, MAX_RETRY_COUNT);
if (ret <= 0) {
pr_err(" %s: Write recovery frame error %d\n", __func__, ret);
return STATUS_FAILED;
}
pr_debug(" %s Reading response \n", __func__);
memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, FW_HDR_LEN, NCI_CMD_RSP_TIMEOUT);
if (ret < FW_HDR_LEN) {
pr_err(" %s - Read recovery frame response error ret %d\n", __func__, ret);
return STATUS_FAILED;
}
if( rsp_buf[0] != FW_MSG_CMD_RSP ||
rsp_buf[DL_FRAME_RESP_LEN_OFFSET] != DL_FRAME_RESP_LEN) {
pr_err("%s, invalid response", __func__);
return STATUS_FAILED;
}
frame_resp_len = rsp_buf[DL_FRAME_RESP_LEN_OFFSET] + FW_CRC_LEN;
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf + FW_HDR_LEN, frame_resp_len, NCI_CMD_RSP_TIMEOUT);
if (ret < frame_resp_len) {
pr_err(" %s - Read recovery frame response error ret %d\n", __func__, ret);
return STATUS_FAILED;
}
pr_debug(" %s: recovery frame Response 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
__func__, rsp_buf[0], rsp_buf[1],rsp_buf[2], rsp_buf[3], rsp_buf[4], rsp_buf[5]);
respCRCOffset = FW_HDR_LEN + rsp_buf[DL_FRAME_RESP_LEN_OFFSET];
respCRC = rsp_buf[respCRCOffset++];
respCRC <<= MSB_POS;
respCRC |= rsp_buf[respCRCOffset];
if(respCRC != calcCrc16(rsp_buf, DL_FRAME_RESP_LEN + FW_HDR_LEN)) {
pr_err("%s, invalid response crc", __func__);
return STATUS_FAILED;
}
if(g_recovery_info.bFrameSegmented &&
(rsp_buf[DL_FRAME_RESP_STAT_OFFSET] != DL_SEGMENTED_FRAME_RESP_STAT1
&& rsp_buf[DL_FRAME_RESP_STAT_OFFSET] != DL_SEGMENTED_FRAME_RESP_STAT2)) {
pr_err("%s, invalid stat flag in chunk response", __func__);
return STATUS_FAILED;
}
if(!g_recovery_info.bFrameSegmented &&
rsp_buf[DL_FRAME_RESP_STAT_OFFSET] != DL_NON_SEGMENTED_FRAME_RESP_STAT) {
pr_err("%s, invalid stat flag in response", __func__);
return STATUS_FAILED;
}
return STATUS_SUCCESS;
}
/** @brief Function to detect the fw state and print.
* @param nfc_dev nfc driver object.
* @return no return.
*/
static void detect_fw_state(nfc_dev_t* nfc_dev) {
const char get_session_state_cmd[] = { 0x00, 0x04, 0xF2, 0x00, 0x00, 0x00, 0xF5, 0x33 };
char rsp_buf[MAX_BUFFER_SIZE];
int ret = 0;
pr_debug("%s:Sending GET_SESSION_STATE cmd \n", __func__);
ret = nfc_dev->nfc_write(nfc_dev, get_session_state_cmd,
sizeof(get_session_state_cmd), MAX_RETRY_COUNT);
if (ret <= 0) {
pr_err("%s: - nfc get session state cmd err ret %d\n", __func__, ret);
return;
}
memset(rsp_buf, 0x00, DL_GET_SESSION_STATE_RSP_LEN);
pr_debug("%s: Reading response of GET_SESSION_STATE cmd\n", __func__);
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, DL_GET_SESSION_STATE_RSP_LEN, NCI_CMD_RSP_TIMEOUT);
if (ret <= 0) {
pr_err("%s: - nfc get session state rsp err %d\n", __func__, ret);
return;
}
pr_debug("Response bytes are %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x",
rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3], rsp_buf[4], rsp_buf[5],
rsp_buf[6], rsp_buf[7]);
/*verify fw in non-teared state */
if (rsp_buf[GET_SESSION_STS_OFF] != NFCC_SESSION_STS_CLOSED) {
pr_err("%s NFCC is in teared state %d\n", __func__, __LINE__);
} else {
pr_info("%s NFCC is in recoverd state %d\n", __func__, __LINE__);
}
}
/** @brief Function to check input version with recovery fw version.
* @param fw_major_version: input major_version to check.
* @return true if input major_version matches with recovery fw major version
* otherwise returns false.
*/
static bool check_major_version(uint8_t fw_major_version) {
if(gphDnldNfc_DlSeqSz < RECOVERY_FW_MJ_VER_OFFSET) {
/* Recovery data corrupted */
pr_err("%s Not able to extract major version from recovery fw\n", __func__);
return false;
}
return (fw_major_version == gphDnldNfc_DlSequence[RECOVERY_FW_MJ_VER_OFFSET]);
}
/** @brief Function to recover the nfcc.
* @param nfc_dev nfc driver object.
* @return status code of type recovery_status_t.
*/
recovery_status_t do_recovery(nfc_dev_t *nfc_dev) {
recovery_status_t status = STATUS_SUCCESS;
g_recovery_info.remBytes = gphDnldNfc_DlSeqSz;
g_recovery_info.currentReadOffset = 0;
g_recovery_info.bFrameSegmented = false;
g_recovery_info.wRemChunkBytes = 0;
pr_debug("%s Entry", __func__);
if(NULL == nfc_dev) {
pr_err("%s invalid params ", __func__);
return STATUS_FAILED;
}
if(!nfc_dev->recovery_required
|| !(check_major_version(nfc_dev->fw_major_version))) {
pr_err("%s recovery not required or unsupported version", __func__);
status = STATUS_FAILED;
goto EXIT_RECOVERY;
}
while(g_recovery_info.remBytes > 0) {
status = build_cmd_frame();
if(status != STATUS_SUCCESS) {
pr_err(" %s Unable to create recovery frame");
break;
}
status = transmit(nfc_dev);
if(status != STATUS_SUCCESS) {
pr_err(" %s Unable to send recovery frame");
break;
}
}
EXIT_RECOVERY:
detect_fw_state(nfc_dev);
/*set NCI mode for i2c products with dwl pin */
enable_dwnld_mode(nfc_dev, false);
pr_info("%s Recovery done status %d", __func__, status);
return status;
}
#endif

80
nfc/recovery_seq.h Normal file
View File

@@ -0,0 +1,80 @@
/******************************************************************************
* Copyright (C) 2021 NXP
* *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
******************************************************************************/
#if defined(RECOVERY_ENABLE)
#ifndef __RECOVERY_SEQ_H_
#define __RECOVERY_SEQ_H_
#include "common.h"
#define MAX_FRAME_SIZE 0x22A /* support for 256(0x100) & 554(0x22A) frame size*/
#define MAX_DATA_SIZE \
(MAX_FRAME_SIZE - FW_CRC_LEN - FW_HDR_LEN)
#define FW_ROM_CODE_VER_OFFSET 4
#define FW_MAJOR_VER_OFFSET 7
#define RECOVERY_FW_SUPPORTED_ROM_VER 0x01
#define RECOVERY_FW_SUPPORTED_MAJOR_VER 0x10
#define RECOVERY_FW_MJ_VER_OFFSET 5
#define DL_SET_HDR_FRAGBIT(n) \
((n) | (1 << 10)) /* Header chunk bit set macro */
#define DL_CLR_HDR_FRAGBIT(n) \
((n) & ~(1U << 10)) /* Header chunk bit clear macro */
#define DL_FRAME_RESP_LEN 0x04
#define DL_FRAME_RESP_LEN_OFFSET 1
#define DL_FRAME_RESP_STAT_OFFSET 2
#define DL_SEGMENTED_FRAME_RESP_STAT1 0x2D
#define DL_SEGMENTED_FRAME_RESP_STAT2 0x2E
#define DL_NON_SEGMENTED_FRAME_RESP_STAT 0x00
#define DL_INVALID_CRC_VALUE 0xffffU
#define DL_CRC_MASK 0xff
#define SHIFT_MASK 0x00FF
#define MSB_POS 8
/* Data buffer for frame to write */
typedef struct recovery_frame {
uint32_t len;
uint8_t p_buffer[MAX_FRAME_SIZE];
} recovery_frame_t;
/* Contains Info about user buffer and last data frame */
typedef struct recovery_info {
uint32_t currentReadOffset; /* current offset within the user buffer to read/write */
uint32_t remBytes; /* Remaining bytes to write */
uint16_t wRemChunkBytes; /* Remaining bytes within the chunked frame */
bool bFrameSegmented; /* Indicates the current frame is segmented */
} recovery_info_t;
/* indicates the error codes for nfc recovery module */
typedef enum recovery_status {
STATUS_SUCCESS = 0x00,
STATUS_FAILED = 0x01,
} recovery_status_t;
/** @brief Function to recover the nfcc.
* @param nfc_dev nfc driver object.
* @return status code of type recovery_status_t.
*/
recovery_status_t do_recovery(nfc_dev_t *nfc_dev);
#endif// end __RECOVERY_SEQ_H_
#endif