Updated corresponding to - NFC_AR_00_6000_11.06.00

Этот коммит содержится в:
nxf24591
2020-11-20 18:32:04 +05:30
родитель 8c5402967f
Коммит bedce83629
12 изменённых файлов: 1600 добавлений и 2261 удалений

6
nfc/Makefile Обычный файл
Просмотреть файл

@@ -0,0 +1,6 @@
#
# 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

447
nfc/common.c Обычный файл
Просмотреть файл

@@ -0,0 +1,447 @@
/******************************************************************************
* Copyright (C) 2019-2020 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
*
******************************************************************************/
#include <linux/of_gpio.h>
#include <linux/of_device.h>
#include <linux/delay.h>
#include <linux/version.h>
#include "common.h"
#include "common_ese.h"
int nfc_parse_dt(struct device *dev, platform_gpio_t * nfc_gpio,
uint8_t interface)
{
struct device_node *np = dev->of_node;
if (!np) {
pr_err("nfc of_node NULL\n");
return -EINVAL;
}
nfc_gpio->irq = -EINVAL;
nfc_gpio->dwl_req = -EINVAL;
nfc_gpio->ven = -EINVAL;
//required for i2c based chips only
if (interface == PLATFORM_IF_I2C) {
nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
if ((!gpio_is_valid(nfc_gpio->irq))) {
pr_err("nfc irq gpio invalid %d\n", nfc_gpio->irq);
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))) {
pr_err("nfc ven gpio invalid %d\n", nfc_gpio->ven);
return -EINVAL;
}
pr_info("%s: %d, %d, %d, %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
nfc_gpio->dwl_req);
return 0;
}
void set_valid_gpio(int gpio, int value)
{
if (gpio_is_valid(gpio)) {
pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
gpio_set_value(gpio, value);
// hardware dependent delay
usleep_range(10000, 10100);
}
}
int get_valid_gpio(int gpio)
{
int value = -1;
if (gpio_is_valid(gpio)) {
value = gpio_get_value(gpio);
pr_debug("%s gpio %d value %d\n", __func__, gpio, value);
}
return value;
}
void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
{
if (gpio_get_value(nfc_dev->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);
// hardware dependent delay
usleep_range(10000, 10100);
}
}
int configure_gpio(unsigned int gpio, int flag)
{
int ret;
pr_debug("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
if (gpio_is_valid(gpio)) {
ret = gpio_request(gpio, "nfc_gpio");
if (ret) {
pr_err("%s: unable to request nfc gpio [%d]\n", __func__, gpio);
return ret;
}
/*set direction and value for output pin */
if (flag & GPIO_OUTPUT) {
ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
pr_debug("nfc o/p gpio %d level %d\n", gpio, gpio_get_value(gpio));
} else {
ret = gpio_direction_input(gpio);
pr_debug("nfc i/p gpio %d\n", gpio);
}
if (ret) {
pr_err("%s: unable to set direction for nfc gpio [%d]\n", __func__, gpio);
gpio_free(gpio);
return ret;
}
/*Consider value as control for input IRQ pin */
if (flag & GPIO_IRQ) {
ret = gpio_to_irq(gpio);
if (ret < 0) {
pr_err("%s: unable to set irq for nfc gpio [%d]\n", __func__, gpio);
gpio_free(gpio);
return ret;
}
pr_debug("%s: gpio_to_irq successful [%d]\n", __func__, gpio);
return ret;
}
} else {
pr_err("%s: invalid gpio\n", __func__);
ret = -EINVAL;
}
return ret;
}
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);
}
if (gpio_is_valid(nfc_dev->gpio.irq)) {
gpio_free(nfc_dev->gpio.irq);
}
if (gpio_is_valid(nfc_dev->gpio.ven)) {
gpio_free(nfc_dev->gpio.ven);
}
}
void nfc_misc_unregister(nfc_dev_t *nfc_dev, int count)
{
pr_debug("%s: entry\n", __func__);
device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
cdev_del(&nfc_dev->c_dev);
class_destroy(nfc_dev->nfc_class);
unregister_chrdev_region(nfc_dev->devno, count);
}
int nfc_misc_register(nfc_dev_t *nfc_dev,
const struct file_operations *nfc_fops,
int count, char *devname, char *classname)
{
int ret = 0;
ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
if (ret < 0) {
pr_err("%s: failed to alloc chrdev region ret %d\n", __func__, ret);
return ret;
}
nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
if (IS_ERR(nfc_dev->nfc_class)) {
ret = PTR_ERR(nfc_dev->nfc_class);
pr_err("%s: failed to register device class ret %d\n", __func__, ret);
unregister_chrdev_region(nfc_dev->devno, count);
return ret;
}
cdev_init(&nfc_dev->c_dev, nfc_fops);
ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
if (ret < 0) {
pr_err("%s: failed to add cdev ret %d\n", __func__, ret);
class_destroy(nfc_dev->nfc_class);
unregister_chrdev_region(nfc_dev->devno, count);
return ret;
}
nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
nfc_dev->devno, nfc_dev, devname);
if (IS_ERR(nfc_dev->nfc_device)) {
ret = PTR_ERR(nfc_dev->nfc_device);
pr_err("%s: failed to create the device ret %d\n", __func__, ret);
cdev_del(&nfc_dev->c_dev);
class_destroy(nfc_dev->nfc_class);
unregister_chrdev_region(nfc_dev->devno, count);
return ret;
}
return 0;
}
/*
* nfc_ioctl_power_states() - power control
* @nfc_dev: nfc device data structure
* @arg: mode that we want to move to
*
* Device power control. Depending on the arg value, device moves to
* different states, refer common.h for args
*
* Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
*/
static int nfc_ioctl_power_states(nfc_dev_t *nfc_dev, unsigned long arg)
{
int ret = 0;
if (arg == NFC_POWER_OFF) {
/*
* We are attempting a hardware reset so let us disable
* interrupts to avoid spurious notifications to upper
* layers.
*/
nfc_dev->nfc_disable_intr(nfc_dev);
set_valid_gpio(nfc_dev->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);
gpio_set_ven(nfc_dev, 1);
nfc_dev->nfc_ven_enabled = true;
} else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
/*
* We are switching to download Mode, toggle the enable pin
* 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) {
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);
} else if (arg == NFC_FW_DWL_HIGH) {
/*
* 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) {
nfc_dev->nfc_state = NFC_STATE_FW_DWL;
}
} else if (arg == NFC_VEN_FORCED_HARD_RESET) {
nfc_dev->nfc_disable_intr(nfc_dev);
gpio_set_ven(nfc_dev, 0);
gpio_set_ven(nfc_dev, 1);
nfc_dev->nfc_enable_intr(nfc_dev);
} else if (arg == NFC_FW_DWL_LOW) {
/*
* 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) {
nfc_dev->nfc_state = NFC_STATE_NCI;
}
} else {
pr_err("%s bad arg %lu\n", __func__, arg);
ret = -ENOIOCTLCMD;
}
return ret;
}
/** @brief IOCTL function to be used to set or get data from upper layer.
*
* @param pfile fil node for opened device.
* @cmd IOCTL type from upper layer.
* @arg IOCTL arg from upper layer.
*
* @return 0 on success, error code for failures.
*/
long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
int ret = 0;
struct nfc_dev *nfc_dev = pfile->private_data;
if (!nfc_dev)
return -ENODEV;
pr_debug("%s cmd = %x arg = %zx\n", __func__, cmd, arg);
switch (cmd) {
case NFC_SET_PWR:
ret = nfc_ioctl_power_states(nfc_dev, arg);
break;
case ESE_SET_PWR:
ret = nfc_ese_pwr(nfc_dev, arg);
break;
case ESE_GET_PWR:
ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
break;
case NFC_GET_PLATFORM_TYPE:
ret = nfc_dev->interface;
break;
case NFC_GET_NFC_STATE:
ret = nfc_dev->nfc_state;
pr_debug("nfc get state %d\n", ret);
break;
case NFC_GET_IRQ_STATE:
ret = 0;
ret = gpio_get_value(nfc_dev->gpio.irq);
break;
default:
pr_err("%s bad cmd %lu\n", __func__, arg);
ret = -ENOIOCTLCMD;
};
return ret;
}
int nfc_dev_open(struct inode *inode, struct file *filp)
{
nfc_dev_t *nfc_dev = container_of(inode->i_cdev, nfc_dev_t, c_dev);
pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
mutex_lock(&nfc_dev->dev_ref_mutex);
filp->private_data = nfc_dev;
if (nfc_dev->dev_ref_count == 0) {
set_valid_gpio(nfc_dev->gpio.dwl_req, 0);
nfc_dev->nfc_enable_intr(nfc_dev);
}
nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
mutex_unlock(&nfc_dev->dev_ref_mutex);
return 0;
}
int nfc_dev_close(struct inode *inode, struct file *filp)
{
nfc_dev_t *nfc_dev = container_of(inode->i_cdev, nfc_dev_t, c_dev);
pr_debug("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
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);
}
if (nfc_dev->dev_ref_count > 0)
nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
else {
nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS_NFC);
/* Uncomment below line incase of eSE calls flow is via NFC driver
* i.e. direct calls from SPI HAL to NFC driver*/
//nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS);
}
filp->private_data = NULL;
mutex_unlock(&nfc_dev->dev_ref_mutex);
return 0;
}
static int get_nfcc_boot_state(struct nfc_dev *nfc_dev)
{
int ret = 0;
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,
sizeof(get_version_cmd), MAX_RETRY_COUNT);
if (ret <= 0) {
pr_err("%s: - nfc get version cmd error ret %d\n", __func__, ret);
goto err;
}
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);
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)) {
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])) {
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__);
return NFC_STATE_NCI;
}
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);
goto err;
}
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);
if (ret <= 0) {
pr_err("%s: - nfc get session state rsp err %d\n", __func__, ret);
goto err;
}
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_debug("%s NFCC booted in teared fw state %d\n", __func__, __LINE__);
return NFC_STATE_FW_TEARED;
}
pr_debug("%s NFCC booted in FW DN mode %d\n", __func__, __LINE__);
return NFC_STATE_FW_DWL;
err:
pr_err("%s Unlikely NFCC not booted in FW DN mode %d\n", __func__, __LINE__);
return NFC_STATE_UNKNOWN;
}
int validate_nfc_state_nci(nfc_dev_t *nfc_dev)
{
if (!gpio_get_value(nfc_dev->gpio.ven)) {
pr_err("VEN LOW - NFCC powered off\n");
return -ENODEV;
} else {
if (get_valid_gpio(nfc_dev->gpio.dwl_req) == 1) {
pr_err("FW download in-progress\n");
return -EBUSY;
} else if (nfc_dev->nfc_state == NFC_STATE_FW_DWL) {
pr_err("FW download state \n");
return -EBUSY;
}
}
return 0;
}

211
nfc/common.h Обычный файл
Просмотреть файл

@@ -0,0 +1,211 @@
/******************************************************************************
* Copyright (C) 2019-2020 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
*
******************************************************************************/
#ifndef _COMMON_H_
#define _COMMON_H_
#include <linux/types.h>
#include <linux/version.h>
#include <linux/semaphore.h>
#include <linux/completion.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/spinlock.h>
#include <linux/gpio.h>
#include "i2c_drv.h"
#define DEV_COUNT 1 /* Max device count for this driver */
#define CLASS_NAME "nfc" /* i2c device class */
// NFC character device name, this will be in /dev/
#define NFC_CHAR_DEV_NAME "pn553"
// NCI packet details
#define NCI_MSG_CMD 0x20
#define NCI_MSG_RSP 0x40
#define NCI_HDR_LEN 3
#define NCI_PAYLOAD_IDX 3
#define NCI_PAYLOAD_LEN_IDX 2
// FW DNLD packet details
#define FW_HDR_LEN 2
#define FW_PAYLOAD_LEN_IDX 1
#define FW_CRC_LEN 2
#define MIN_NFC_DL_FRAME_SIZE 3
#define NCI_RESET_CMD_LEN (4)
#define NCI_RESET_RSP_LEN (4)
#define NCI_RESET_NTF_LEN (13)
#define DL_GET_VERSION_CMD_LEN (8)
#define DL_GET_VERSION_RSP_LEN_1 (12)
#define DL_GET_VERSION_RSP_LEN_2 (20)
#define DL_RESET_CMD_LEN (8)
#define DL_GET_SESSION_STATE_CMD_LEN (8)
#define DL_GET_SESSION_STATE_RSP_LEN (8)
#define GET_SESSION_STS_OFF (3)
#define NFCC_SESSION_STS_CLOSED (0x0)
#define MAX_NCI_PAYLOAD_LEN (255)
#define MAX_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
#define MAX_DL_PAYLOAD_LEN (550)
#define MAX_DL_BUFFER_SIZE (FW_HDR_LEN + FW_CRC_LEN + MAX_DL_PAYLOAD_LEN)
// Maximum retry count for standby writes
#define MAX_RETRY_COUNT (3)
// Retry count for normal write
#define NO_RETRY (1)
#define MAX_IRQ_WAIT_TIME (90)
#define WAKEUP_SRC_TIMEOUT (2000)
/*command response timeout*/
#define NCI_CMD_RSP_TIMEOUT (2000) //2s
#define NFC_MAGIC 0xE9
/*Ioctls*/
// The type should be aligned with MW HAL definitions
#define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, long)
#define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, long)
#define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, long)
#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 DTS_IRQ_GPIO_STR "nxp,pn544-irq"
#define DTS_VEN_GPIO_STR "nxp,pn544-ven"
#define DTS_FWDN_GPIO_STR "nxp,pn544-fw-dwnld"
enum nfcc_ioctl_request {
/* NFC disable request with VEN LOW */
NFC_POWER_OFF = 0,
/* NFC enable request with VEN Toggle */
NFC_POWER_ON,
/* firmware download request with VEN Toggle */
NFC_FW_DWL_VEN_TOGGLE,
/* ISO reset request */
NFC_ISO_RESET,
/* request for firmware download gpio HIGH */
NFC_FW_DWL_HIGH,
/* VEN hard reset request */
NFC_VEN_FORCED_HARD_RESET,
/* request for firmware download gpio LOW */
NFC_FW_DWL_LOW,
};
/*nfc platform interface type*/
enum interface_flags {
/*I2C physical IF for NFCC */
PLATFORM_IF_I2C = 0,
};
/*nfc state flags*/
enum nfc_state_flags {
/*nfc in unknown state */
NFC_STATE_UNKNOWN = 0,
/*nfc in download mode */
NFC_STATE_FW_DWL = 0x1,
/*nfc booted in NCI mode */
NFC_STATE_NCI = 0x2,
/*nfc booted in Fw teared mode */
NFC_STATE_FW_TEARED = 0x4,
};
/*
* Power state for IBI handing, mainly needed to defer the IBI handling
* for the IBI received in suspend state to do it later in resume call
*/
enum pm_state_flags {
PM_STATE_NORMAL = 0,
PM_STATE_SUSPEND,
PM_STATE_IBI_BEFORE_RESUME,
};
/* Enum for GPIO values*/
enum gpio_values {
GPIO_INPUT = 0x0,
GPIO_OUTPUT = 0x1,
GPIO_HIGH = 0x2,
GPIO_OUTPUT_HIGH = 0x3,
GPIO_IRQ = 0x4,
};
// NFC GPIO variables
typedef struct platform_gpio {
unsigned int irq;
unsigned int ven;
unsigned int dwl_req;
} platform_gpio_t;
//cold reset Features specific Parameters
typedef struct cold_reset {
bool rsp_pending; /*cmd rsp pending status */
bool in_progress; /*for cold reset when gurad timer in progress */
bool reset_protection; /*reset protection enabled/disabled */
uint8_t status; /*status from response buffer */
uint8_t rst_prot_src; /*reset protection source (SPI, NFC) */
struct mutex sync_mutex;
struct timer_list timer;
wait_queue_head_t read_wq;
} cold_reset_t;
/* Device specific structure */
typedef struct nfc_dev {
wait_queue_head_t read_wq;
struct mutex read_mutex;
struct mutex ese_access_mutex;
struct mutex dev_ref_mutex;
unsigned int dev_ref_count;
struct class *nfc_class;
struct device *nfc_device;
struct cdev c_dev;
dev_t devno;
/* Interface flag */
uint8_t interface;
/* nfc state flags */
uint8_t nfc_state;
/* NFC VEN pin state */
bool nfc_ven_enabled;
union {
i2c_dev_t i2c_dev;
};
platform_gpio_t gpio;
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_write) (struct nfc_dev *dev, const char *buf, const size_t count,
int max_retry_cnt);
int (*nfc_enable_intr) (struct nfc_dev *dev);
int (*nfc_disable_intr) (struct nfc_dev *dev);
int (*nfc_flush_readq) (struct nfc_dev *dev);
} nfc_dev_t;
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,
uint8_t interface);
int nfc_misc_register(nfc_dev_t *nfc_dev,
const struct file_operations *nfc_fops, int count, char *devname,
char *classname);
void nfc_misc_unregister(nfc_dev_t *nfc_dev, int count);
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);
#endif //_COMMON_H_

347
nfc/common_ese.c Обычный файл
Просмотреть файл

@@ -0,0 +1,347 @@
/******************************************************************************
* Copyright (C) 2020 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
*
******************************************************************************/
#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/version.h>
#include <linux/fs.h>
#include "common.h"
#include "common_ese.h"
static void cold_reset_gaurd_timer_callback(struct timer_list *t)
{
cold_reset_t *cold_reset = from_timer(cold_reset, t, timer);
pr_debug("%s: Enter\n", __func__);
cold_reset->in_progress = false;
return;
}
static long start_cold_reset_guard_timer(cold_reset_t *cold_reset)
{
long ret = -EINVAL;
if (timer_pending(&cold_reset->timer) == 1) {
pr_debug("ese_cold_reset_guard_timer: delete pending timer \n");
/* delete timer if already pending */
del_timer(&cold_reset->timer);
}
cold_reset->in_progress = true;
timer_setup(&cold_reset->timer, cold_reset_gaurd_timer_callback, 0);
ret = mod_timer(&cold_reset->timer,
jiffies + msecs_to_jiffies(ESE_CLD_RST_GUARD_TIME));
return ret;
}
static int send_cold_reset_protection_cmd(nfc_dev_t *nfc_dev, bool requestType)
{
int ret = 0;
int length = 0;
uint8_t *cmd = NULL;
uint8_t cld_rst_cmd[] = { NCI_PROP_MSG_CMD, CLD_RST_OID,
CLD_RST_PAYLOAD_SIZE
};
uint8_t rst_prot_cmd[] = { NCI_PROP_MSG_CMD, RST_PROT_OID,
RST_PROT_PAYLOAD_SIZE, 0x00
};
cold_reset_t *cold_reset = &nfc_dev->cold_reset;
if (requestType) {
length = sizeof(rst_prot_cmd);
rst_prot_cmd[NCI_PAYLOAD_IDX] = (!cold_reset->reset_protection) ? 1 : 0;
cmd = rst_prot_cmd;
} else {
length = sizeof(cld_rst_cmd);
cmd = cld_rst_cmd;
}
ret = nfc_dev->nfc_write(nfc_dev, cmd, length, MAX_RETRY_COUNT);
if (ret != length) {
pr_err("%s : nfc_write returned %d\n", __func__, ret);
return -EIO;
}
if (requestType) {
pr_debug("%s: NxpNciX: %d > %02X%02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
cmd[2], cmd[3]);
} else {
pr_debug("%s: NxpNciX: %d > %02X%02X%02X\n", __func__, ret, cmd[0], cmd[1],
cmd[2]);
}
return ret;
}
void wakeup_on_prop_rsp(nfc_dev_t *nfc_dev, uint8_t *buf)
{
cold_reset_t *cold_reset = &nfc_dev->cold_reset;
cold_reset->status = -EIO;
if ((NCI_HDR_LEN + buf[NCI_PAYLOAD_LEN_IDX]) != NCI_PROP_MSG_RSP_LEN) {
pr_err("%s: - invalid response for cold_reset/protection \n", __func__);
} else {
cold_reset->status = buf[NCI_PAYLOAD_IDX];
}
pr_debug("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__, buf[0], buf[1],
buf[2], buf[3]);
cold_reset->rsp_pending = false;
wake_up_interruptible(&cold_reset->read_wq);
}
static int validate_cold_reset_protection_request(cold_reset_t *cold_reset,
unsigned long arg)
{
if (!cold_reset->reset_protection) {
if (IS_RST_PROT_EN_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
pr_debug("%s:req - reset protection enable\n", __func__);
} else if (IS_CLD_RST_REQ(arg) && IS_SRC_VALID(arg)) {
pr_debug("%s:req - cold reset\n", __func__);
} else if (IS_RST_PROT_DIS_REQ(arg) && IS_SRC_VALID_PROT(arg)) {
pr_debug("%s:req - reset protection already disable\n", __func__);
return -EINVAL;
} else {
pr_err("%s:Operation not permitted \n", __func__);
return -EPERM;
}
} else {
if (IS_RST_PROT_DIS_REQ(arg)
&& IS_SRC(arg, cold_reset->rst_prot_src)) {
pr_debug("%s:req - disable reset protection from same src\n", __func__);
} else if (IS_CLD_RST_REQ(arg)
&& IS_SRC(arg, cold_reset->rst_prot_src)) {
pr_debug("%s:req - cold reset from same source\n", __func__);
} else if (IS_RST_PROT_EN_REQ(arg)
&& IS_SRC(arg, cold_reset->rst_prot_src)) {
pr_debug("%s:request - enable reset protection from same source\n", __func__);
} else {
pr_err("%s: Operation not permitted \n", __func__);
return -EPERM;
}
}
return 0;
}
static int perform_cold_reset_protection(nfc_dev_t *nfc_dev, unsigned long arg)
{
int ret = 0;
struct file filp;
cold_reset_t *cold_reset = &nfc_dev->cold_reset;
bool nfc_dev_opened = false;
/*check if NFCC not in the FW download or hard reset state */
ret = validate_nfc_state_nci(nfc_dev);
if (ret < 0) {
pr_err("%s: invalid cmd", __func__);
return ret;
}
/* check if NFC is enabled */
mutex_lock(&nfc_dev->dev_ref_mutex);
nfc_dev_opened = (nfc_dev->dev_ref_count > 0) ? true : false;
mutex_unlock(&nfc_dev->dev_ref_mutex);
mutex_lock(&cold_reset->sync_mutex);
/*check if NFCC not in the FW download or hard reset state */
ret = validate_cold_reset_protection_request(cold_reset, arg);
if (ret < 0) {
pr_err("%s: invalid cmd", __func__);
goto err;
}
/*check if cold reset already in progress */
if (IS_CLD_RST_REQ(arg) && cold_reset->in_progress) {
pr_err("%s: cold reset already in progress", __func__);
ret = -EBUSY;
goto err;
}
/* set default value for status as failure */
cold_reset->status = -EIO;
cold_reset->rsp_pending = true;
/*enable interrupt before sending cmd, when devnode not opened by HAL */
if (!nfc_dev_opened)
nfc_dev->nfc_enable_intr(nfc_dev);
ret = send_cold_reset_protection_cmd(nfc_dev, IS_RST_PROT_REQ(arg));
if (ret < 0) {
pr_err("failed to send cold reset/protection command\n");
cold_reset->rsp_pending = false;
goto err;
}
ret = 0;
/*start the cold reset guard timer */
if (IS_CLD_RST_REQ(arg)) {
/*Guard timer not needed when OSU over NFC*/
if(!(cold_reset->reset_protection && IS_SRC_NFC(arg))) {
ret = start_cold_reset_guard_timer(cold_reset);
if (ret) {
pr_err("%s: Error in mod_timer\n", __func__);
goto err;
}
}
}
do {
/* Read is pending from the HAL service which will complete the response */
if (nfc_dev_opened) {
if (!wait_event_interruptible_timeout
(cold_reset->read_wq,
cold_reset->rsp_pending == false,
msecs_to_jiffies(NCI_CMD_RSP_TIMEOUT))) {
pr_err("%s:cold reset/protection response timeout\n", __func__);
ret = -EAGAIN;
}
} 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)
}
} while (ret == -ERESTARTSYS || ret == -EFAULT);
if (ret == 0) { /* success case */
ret = cold_reset->status;
if (IS_RST_PROT_REQ(arg)) {
cold_reset->reset_protection = IS_RST_PROT_EN_REQ(arg);
cold_reset->rst_prot_src =
IS_RST_PROT_EN_REQ(arg) ? GET_SRC(arg) : SRC_NONE;
/* wait for reboot guard timer */
} else if (wait_event_interruptible_timeout
(cold_reset->read_wq, true,
msecs_to_jiffies(ESE_CLD_RST_REBOOT_GUARD_TIME)) ==
0) {
pr_info("%s: reboot guard timer timeout", __func__);
}
}
err:
mutex_unlock(&cold_reset->sync_mutex);
return ret;
}
/*
* Power management of the eSE
* eSE and NFCC both are powered using VEN gpio,
* VEN HIGH - eSE and NFCC both are powered on
* VEN LOW - eSE and NFCC both are power down
*/
int nfc_ese_pwr(nfc_dev_t *nfc_dev, unsigned long arg)
{
int ret = 0;
if (arg == ESE_POWER_ON) {
/**
* Let's store the NFC VEN pin state
* will check stored value in case of eSE power off request,
* to find out if NFC MW also sent request to set VEN HIGH
* 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);
if (!nfc_dev->nfc_ven_enabled) {
pr_debug("eSE HAL service setting ven HIGH\n");
gpio_set_ven(nfc_dev, 1);
} else {
pr_debug("ven already HIGH\n");
}
} else if (arg == ESE_POWER_OFF) {
if (!nfc_dev->nfc_ven_enabled) {
pr_debug("NFC not enabled, disabling ven\n");
gpio_set_ven(nfc_dev, 0);
} else {
pr_debug("keep ven high as NFC is enabled\n");
}
} else if (arg == ESE_POWER_STATE) {
// eSE get power state
ret = gpio_get_value(nfc_dev->gpio.ven);
} else if (IS_CLD_RST_REQ(arg) || IS_RST_PROT_REQ(arg)) {
ret = perform_cold_reset_protection(nfc_dev, arg);
} else {
pr_err("%s bad arg %lu\n", __func__, arg);
ret = -ENOIOCTLCMD;
}
return ret;
}
EXPORT_SYMBOL(nfc_ese_pwr);
#define ESE_LEGACY_INTERFACE
#ifdef ESE_LEGACY_INTERFACE
static nfc_dev_t *nfc_dev_legacy = NULL;
/******************************************************************************
* perform_ese_cold_reset() - It shall be called by others driver(not nfc/ese)
* to perform cold reset only
* @arg: request of cold reset from other drivers should be ESE_CLD_RST_OTHER
*
* Returns:- 0 in case of sucess and negative values in case of failure
*****************************************************************************/
int perform_ese_cold_reset(unsigned long arg)
{
int ret = 0;
if (nfc_dev_legacy) {
if (IS_CLD_RST_REQ(arg) && IS_SRC_OTHER(arg)) {
ret = nfc_ese_pwr(nfc_dev_legacy, arg);
} else {
pr_err("%s : Operation not permitted \n", __func__);
return -EPERM;
}
}
pr_debug("%s:%d exit, status:%lu", __func__, arg, ret);
return ret;
}
EXPORT_SYMBOL(perform_ese_cold_reset);
#endif //ESE_LEGACY_INTERFACE
void common_ese_on_hard_reset(nfc_dev_t *nfc_dev)
{
cold_reset_t *cold_reset = &nfc_dev->cold_reset;
cold_reset->rsp_pending = false;
cold_reset->in_progress = false;
if (timer_pending(&cold_reset->timer) == 1) {
del_timer(&cold_reset->timer);
}
}
void common_ese_init(nfc_dev_t *nfc_dev)
{
cold_reset_t *cold_reset = &nfc_dev->cold_reset;
cold_reset->reset_protection = false;
cold_reset->rst_prot_src = SRC_NONE;
init_waitqueue_head(&cold_reset->read_wq);
mutex_init(&cold_reset->sync_mutex);
common_ese_on_hard_reset(nfc_dev);
#ifdef ESE_LEGACY_INTERFACE
nfc_dev_legacy = nfc_dev;
#endif //ESE_LEGACY_INTERFACE
}
void common_ese_exit(nfc_dev_t *nfc_dev)
{
mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
#ifdef ESE_LEGACY_INTERFACE
nfc_dev_legacy = NULL;
#endif //ESE_LEGACY_INTERFACE
}

97
nfc/common_ese.h Обычный файл
Просмотреть файл

@@ -0,0 +1,97 @@
/******************************************************************************
* Copyright (C) 2020 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
*
******************************************************************************/
#ifndef _COMMON_ESE_H_
#define _COMMON_ESE_H_
#include "common.h"
/*nci prop msg 1st byte*/
#define NCI_PROP_MSG_GID 0x0F
#define NCI_PROP_MSG_CMD (NCI_MSG_CMD | NCI_PROP_MSG_GID)
#define NCI_PROP_MSG_RSP (NCI_MSG_RSP | NCI_PROP_MSG_GID)
/*nci prop msg 2nd byte*/
#define CLD_RST_OID 0x1E
#define RST_PROT_OID 0x1F
/*nci prop msg 3rd byte*/
#define CLD_RST_PAYLOAD_SIZE 0x00
#define RST_PROT_PAYLOAD_SIZE 0x01
/*nci prop msg response length*/
#define NCI_PROP_MSG_RSP_LEN 0x04
/*cold reset guard time to allow back to back cold reset after some time*/
#define ESE_CLD_RST_GUARD_TIME (3000) //3s
/*guard time to reboot after reset*/
#define ESE_CLD_RST_REBOOT_GUARD_TIME (50) //50ms
/*sources of reset protection and cold reset*/
typedef enum reset_source {
SRC_SPI = 0,
SRC_NFC = 0x10,
SRC_OTHER = 0x20,
SRC_NONE = 0x80,
} reset_source_t;
enum ese_ioctl_request {
ESE_POWER_ON = 0, /* eSE POWER ON */
ESE_POWER_OFF, /* eSE POWER OFF */
ESE_POWER_STATE, /* eSE GET POWER STATE */
/*ese reset requests from eSE service/hal/driver */
ESE_CLD_RST, /* eSE COLD RESET */
ESE_RST_PROT_EN, /* eSE RESET PROTECTION ENABLE */
ESE_RST_PROT_DIS, /* eSE RESET PROTECTION DISABLE */
/*similar ese reset requests from nfc service/hal/driver */
ESE_CLD_RST_NFC = ESE_CLD_RST | SRC_NFC,
ESE_RST_PROT_EN_NFC = ESE_RST_PROT_EN | SRC_NFC,
ESE_RST_PROT_DIS_NFC = ESE_RST_PROT_DIS | SRC_NFC,
/*similar ese reset requests from other service/hal/driver */
ESE_CLD_RST_OTHER = ESE_CLD_RST | SRC_OTHER,
};
#define GET_SRC(arg) (arg & 0xF0)
#define IS_SRC(arg, src) (GET_SRC(arg) == src)
#define IS_SRC_SPI(arg) IS_SRC(arg, SRC_SPI)
#define IS_SRC_NFC(arg) IS_SRC(arg, SRC_NFC)
#define IS_SRC_OTHER(arg) IS_SRC(arg, SRC_OTHER)
#define IS_SRC_VALID(arg) (IS_SRC_SPI(arg) || IS_SRC_NFC(arg) || \
IS_SRC_OTHER(arg))
#define IS_SRC_VALID_PROT(arg) (IS_SRC_SPI(arg) || IS_SRC_NFC(arg))
#define IS_RST(arg, type) ((arg & 0xF) == type)
#define IS_CLD_RST_REQ(arg) IS_RST(arg, ESE_CLD_RST)
#define IS_RST_PROT_EN_REQ(arg) IS_RST(arg, ESE_RST_PROT_EN)
#define IS_RST_PROT_DIS_REQ(arg) IS_RST(arg, ESE_RST_PROT_DIS)
#define IS_RST_PROT_REQ(arg) (IS_RST_PROT_EN_REQ(arg) || IS_RST_PROT_DIS_REQ(arg))
/* This macro evaluates to 1 if prop cmd response is received */
#define IS_PROP_CMD_RSP(buf) \
((NCI_PROP_MSG_RSP == buf[0]) && ((CLD_RST_OID == buf[1]) || \
(RST_PROT_OID == buf[1])))
void wakeup_on_prop_rsp(nfc_dev_t *nfc_dev, uint8_t *buf);
int nfc_ese_pwr(nfc_dev_t *nfc_dev, unsigned long arg);
void common_ese_on_hard_reset(nfc_dev_t *nfc_dev);
void common_ese_init(nfc_dev_t *nfc_dev);
void common_ese_exit(nfc_dev_t *nfc_dev);
#endif /* _COMMON_ESE_H_ */

467
nfc/i2c_drv.c Обычный файл
Просмотреть файл

@@ -0,0 +1,467 @@
/******************************************************************************
* Copyright (C) 2013-2020 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
*
******************************************************************************/
/*
* Copyright (C) 2010 Trusted Logic S.A.
*
* 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
*/
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include "common.h"
#include "common_ese.h"
/**
* i2c_disable_irq()
*
* Check if interrupt is disabled or not
* and disable interrupt
*
* Return: int
*/
int i2c_disable_irq(struct nfc_dev *dev)
{
unsigned long flags;
spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
if (dev->i2c_dev.irq_enabled) {
disable_irq_nosync(dev->i2c_dev.client->irq);
dev->i2c_dev.irq_enabled = false;
}
spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
return 0;
}
/**
* i2c_enable_irq()
*
* Check if interrupt is enabled or not
* and enable interrupt
*
* Return: int
*/
int i2c_enable_irq(struct nfc_dev *dev)
{
unsigned long flags;
spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
if (!dev->i2c_dev.irq_enabled) {
dev->i2c_dev.irq_enabled = true;
enable_irq(dev->i2c_dev.client->irq);
}
spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
return 0;
}
static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
{
nfc_dev_t *nfc_dev = dev_id;
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
if (device_may_wakeup(&i2c_dev->client->dev))
pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT);
i2c_disable_irq(nfc_dev);
wake_up(&nfc_dev->read_wq);
return IRQ_HANDLED;
}
int i2c_read(struct nfc_dev *dev, char *buf, size_t count)
{
int ret;
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;
}
int i2c_write(struct nfc_dev *dev, const char *buf, size_t count,
int max_retry_cnt)
{
int ret = -EINVAL;
int retry_cnt;
pr_debug("%s : writing %zu bytes.\n", __func__, count);
for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
ret = i2c_master_send(dev->i2c_dev.client, buf, count);
if (ret <= 0) {
pr_warn("%s: write failed, Maybe in Standby Mode - Retry(%d)\n", __func__,
retry_cnt);
usleep_range(1000, 1100);
} 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)
{
int ret;
char tmp[MAX_BUFFER_SIZE];
nfc_dev_t *nfc_dev = filp->private_data;
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
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;
}
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 (ret) {
pr_err("error wakeup of read wq\n");
goto err;
}
}
i2c_disable_irq(nfc_dev);
if (gpio_get_value(nfc_dev->gpio.irq))
break;
if (!gpio_get_value(nfc_dev->gpio.ven)) {
pr_info("%s: releasing read\n", __func__);
ret = -EIO;
goto err;
}
pr_warn("%s: spurious interrupt detected\n", __func__);
}
}
memset(tmp, 0x00, count);
/* Read data */
ret = i2c_read(nfc_dev, tmp, count);
if (ret <= 0) {
pr_err("%s: i2c_read returned %d\n", __func__, ret);
goto err;
}
/* check if it's response of cold reset command
* NFC HAL process shouldn't receive this data as
* command was sent by driver
*/
if (nfc_dev->cold_reset.rsp_pending) {
if (IS_PROP_CMD_RSP(tmp)) {
/* Read data */
ret =
i2c_read(nfc_dev, &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__);
goto err;
}
wakeup_on_prop_rsp(nfc_dev, tmp);
mutex_unlock(&nfc_dev->read_mutex);
/*
* NFC process doesn't know about cold reset command
* being sent as it was initiated by eSE process
* we shouldn't return any data to NFC process
*/
return 0;
}
}
if (copy_to_user(buf, tmp, ret)) {
pr_warn("%s : failed to copy to user space\n", __func__);
ret = -EFAULT;
}
err:
mutex_unlock(&nfc_dev->read_mutex);
return ret;
}
ssize_t nfc_i2c_dev_write(struct file * filp, const char __user *buf,
size_t count, loff_t * offset)
{
int ret;
char tmp[MAX_DL_BUFFER_SIZE];
nfc_dev_t *nfc_dev = filp->private_data;
if (count > MAX_DL_BUFFER_SIZE)
count = MAX_DL_BUFFER_SIZE;
if (copy_from_user(tmp, buf, count)) {
pr_err("%s : failed to copy from user space\n", __func__);
return -EFAULT;
}
ret = i2c_write(nfc_dev, tmp, count, NO_RETRY);
if (ret != count) {
pr_err("%s: failed to write %d\n", __func__, ret);
ret = -EIO;
}
return ret;
}
static const struct file_operations nfc_i2c_dev_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.read = nfc_i2c_dev_read,
.write = nfc_i2c_dev_write,
.open = nfc_dev_open,
.release = nfc_dev_close,
.unlocked_ioctl = nfc_dev_ioctl,
};
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;
pr_debug("%s: enter\n", __func__);
/*retrive details of gpios from dt */
ret = nfc_parse_dt(&client->dev, &nfc_gpio, PLATFORM_IF_I2C);
if (ret) {
pr_err("%s : failed to parse dt\n", __func__);
goto err;
}
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
pr_err("%s : need I2C_FUNC_I2C\n", __func__);
ret = -ENODEV;
goto err;
}
nfc_dev = kzalloc(sizeof(nfc_dev_t), GFP_KERNEL);
if (nfc_dev == NULL) {
ret = -ENOMEM;
goto err;
}
nfc_dev->interface = PLATFORM_IF_I2C;
nfc_dev->nfc_state = NFC_STATE_NCI;
nfc_dev->i2c_dev.client = client;
i2c_dev = &nfc_dev->i2c_dev;
nfc_dev->nfc_read = i2c_read;
nfc_dev->nfc_write = i2c_write;
nfc_dev->nfc_enable_intr = i2c_enable_irq;
nfc_dev->nfc_disable_intr = i2c_disable_irq;
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);
goto err;
}
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);
goto err;
}
client->irq = ret;
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);
}
/*copy the retrived gpio details from DT */
memcpy(&nfc_dev->gpio, &nfc_gpio, sizeof(struct platform_gpio));
/* init mutex and queues */
init_waitqueue_head(&nfc_dev->read_wq);
mutex_init(&nfc_dev->read_mutex);
mutex_init(&nfc_dev->dev_ref_mutex);
mutex_init(&nfc_dev->ese_access_mutex);
spin_lock_init(&i2c_dev->irq_enabled_lock);
common_ese_init(nfc_dev);
ret = nfc_misc_register(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT,
NFC_CHAR_DEV_NAME, CLASS_NAME);
if (ret) {
pr_err("%s: nfc_misc_register failed\n", __func__);
goto err_mutex_destroy;
}
/* interrupt initializations */
pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
i2c_dev->irq_enabled = true;
ret = request_irq(client->irq, i2c_irq_handler,
IRQF_TRIGGER_HIGH, client->name, nfc_dev);
if (ret) {
pr_err("%s: request_irq failed\n", __func__);
goto err_nfc_misc_unregister;
}
i2c_disable_irq(nfc_dev);
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:
nfc_misc_unregister(nfc_dev, DEV_COUNT);
err_mutex_destroy:
mutex_destroy(&nfc_dev->dev_ref_mutex);
mutex_destroy(&nfc_dev->read_mutex);
mutex_destroy(&nfc_dev->ese_access_mutex);
mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
err:
gpio_free_all(nfc_dev);
kfree(nfc_dev);
pr_err("%s: probing not successful, check hardware\n", __func__);
return ret;
}
int nfc_i2c_dev_remove(struct i2c_client *client)
{
int ret = 0;
nfc_dev_t *nfc_dev = NULL;
pr_info("%s: remove device\n", __func__);
nfc_dev = i2c_get_clientdata(client);
if (!nfc_dev) {
pr_err("%s: device doesn't exist anymore\n", __func__);
ret = -ENODEV;
return ret;
}
if (nfc_dev->dev_ref_count > 0) {
pr_err("%s: device already in use\n", __func__);
return -EBUSY;
}
device_init_wakeup(&client->dev, false);
free_irq(client->irq, nfc_dev);
nfc_misc_unregister(nfc_dev, DEV_COUNT);
mutex_destroy(&nfc_dev->read_mutex);
mutex_destroy(&nfc_dev->ese_access_mutex);
mutex_destroy(&nfc_dev->cold_reset.sync_mutex);
gpio_free_all(nfc_dev);
kfree(nfc_dev);
return ret;
}
int nfc_i2c_dev_suspend(struct device *device)
{
struct i2c_client *client = to_i2c_client(device);
nfc_dev_t *nfc_dev = i2c_get_clientdata(client);
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
if (!enable_irq_wake(client->irq))
i2c_dev->irq_wake_up = true;
}
return 0;
}
int nfc_i2c_dev_resume(struct device *device)
{
struct i2c_client *client = to_i2c_client(device);
nfc_dev_t *nfc_dev = i2c_get_clientdata(client);
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
if (!disable_irq_wake(client->irq))
i2c_dev->irq_wake_up = false;
}
return 0;
}
static const struct i2c_device_id nfc_i2c_dev_id[] = {
{NFC_I2C_DEV_ID, 0},
{}
};
static const struct of_device_id nfc_i2c_dev_match_table[] = {
{.compatible = NFC_I2C_DRV_STR,},
{}
};
static const struct dev_pm_ops nfc_i2c_dev_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(nfc_i2c_dev_suspend, nfc_i2c_dev_resume)
};
static struct i2c_driver nfc_i2c_dev_driver = {
.id_table = nfc_i2c_dev_id,
.probe = nfc_i2c_dev_probe,
.remove = nfc_i2c_dev_remove,
.driver = {
.name = NFC_I2C_DRV_STR,
.pm = &nfc_i2c_dev_pm_ops,
.of_match_table = nfc_i2c_dev_match_table,
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
},
};
MODULE_DEVICE_TABLE(of, nfc_i2c_dev_match_table);
static int __init nfc_i2c_dev_init(void)
{
int ret = 0;
pr_info("Loading NXP NFC I2C driver\n");
ret = i2c_add_driver(&nfc_i2c_dev_driver);
if (ret != 0)
pr_err("NFC I2C add driver error ret %d\n", ret);
return ret;
}
module_init(nfc_i2c_dev_init);
static void __exit nfc_i2c_dev_exit(void)
{
pr_info("Unloading NXP NFC I2C driver\n");
i2c_del_driver(&nfc_i2c_dev_driver);
}
module_exit(nfc_i2c_dev_exit);
MODULE_DESCRIPTION("NXP NFC I2C driver");
MODULE_LICENSE("GPL");

Просмотреть файл

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (C) 2020 NXP
* Copyright (C) 2019-2020 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
@@ -16,24 +16,30 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
******************************************************************************/
#ifndef _NFC_COMMON_H_
#define _NFC_COMMON_H_
#ifndef _I2C_DRV_H_
#define _I2C_DRV_H_
#include <linux/i2c.h>
#define MSG_NFCC_RSP 0x40
#define MSG_PROP_GID 0x0F
#define ESE_CLD_RST_OID 0x1E
#define RST_PROTECTION_CMD_INDEX 0x03
/*kept same as dts */
#define NFC_I2C_DRV_STR "nxp,pn544"
#define NFC_I2C_DEV_ID "pn553"
#define RST_PROTECTION_OID 0x1F
#define RST_PROTECTION_ENABLED 0x08
//Interface specific parameters
typedef struct i2c_dev {
struct i2c_client *client;
/*IRQ parameters */
bool irq_enabled;
spinlock_t irq_enabled_lock;
/* NFC_IRQ wake-up state */
bool irq_wake_up;
} i2c_dev_t;
typedef enum ese_cold_reset_origin {
ESE_COLD_RESET_NOT_REQUESTED = 0x00,
ESE_COLD_RESET_SOURCE_NFC = 0x01,
ESE_COLD_RESET_SOURCE_SPI = 0x02,
ESE_COLD_RESET_SOURCE_UWB = 0x04,
}ese_cold_reset_origin_t;
void ese_reset_resource_init(void);
void ese_reset_resource_destroy(void);
#endif /* _NFC_COMMON_H_ */
long nfc_i2c_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
int nfc_i2c_dev_probe(struct i2c_client *client,
const struct i2c_device_id *id);
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_

Просмотреть файл

@@ -1,13 +0,0 @@
#
# Nxp Nci protocol (I2C) devices
#
config NFC_PN553_DEVICES
bool "Nxp pn553 NCI protocol driver (I2C) devices"
default y
---help---
You'll have to say Y if your computer contains an I2C device that
you want to use under Linux.
You can say N here if you don't have any SPI connected to your computer.

Просмотреть файл

@@ -1,9 +0,0 @@
#
# Makefile for nfc devices
#
obj-$(CONFIG_NFC_PN553_DEVICES) += pn553.o
obj-$(CONFIG_NFC_PN553_DEVICES) += cold_reset.o
ccflags-$(CONFIG_NFC_PN553_DEVICES) := -DDEBUG

Просмотреть файл

@@ -1,343 +0,0 @@
/******************************************************************************
* Copyright (C) 2020 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
*
******************************************************************************/
#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/version.h>
#include <linux/fs.h>
#include "cold_reset.h"
#include "pn553.h"
/*ESE_COLD_RESET MACROS */
#define MAX_BUFFER_SIZE 512 /* copied from pn553.c */
#define MSG_NFCC_CMD 0x20
#define NCI_PROP_RST_RSP_SIZE 0x04
/* Evaluates to 1 If cold reset is in progress or the guard timer is still running */
#define IS_COLD_RESET_REQ_IN_PROGRESS(flags) \
(flags & (MASK_ESE_COLD_RESET | MASK_ESE_COLD_RESET_GUARD_TIMER))
#define IS_RESET_PROTECTION_ENABLED(flags) (flags & RST_PROTECTION_ENABLED)
#define IS_COLD_RESET_ALLOWED(flags, src) (!IS_COLD_RESET_REQ_IN_PROGRESS(flags) \
&& (!IS_RESET_PROTECTION_ENABLED(flags) || src == ESE_COLD_RESET_SOURCE_SPI))
static struct pn544_dev *pn544_dev;
struct mutex ese_cold_reset_sync_mutex;
struct mutex nci_send_cmd_mutex;
extern ssize_t pn544_dev_read(struct file *filp, char __user *buf,
size_t count, loff_t *offset);
static int8_t prop_nci_rsp[NCI_PROP_RST_RSP_SIZE];
static struct timer_list ese_cold_reset_timer;
static struct completion prop_cmd_resp_sema;
static struct completion ese_cold_reset_sema;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
static void ese_cold_reset_gaurd_timer_callback(unsigned long data);
#else
static void ese_cold_reset_gaurd_timer_callback(struct timer_list *unused);
#endif
static long start_ese_cold_reset_guard_timer(void);
extern struct pn544_dev * get_nfcc_dev_data(void);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
static void ese_cold_reset_gaurd_timer_callback(unsigned long data)
{
(void)data;
#else
static void ese_cold_reset_gaurd_timer_callback(struct timer_list *unused)
{
#endif
pr_info("%s: Enter\n",__func__);
pn544_dev->state_flags &= ~MASK_ESE_COLD_RESET_GUARD_TIMER;
return;
}
static long start_ese_cold_reset_guard_timer(void)
{
long ret = -EINVAL;
printk( KERN_INFO "starting ese_cold_reset_timer \n");
if(timer_pending(&ese_cold_reset_timer) == 1)
{
pr_info("ese_cold_reset_guard_timer: delete pending timer \n");
/* delete timer if already pending */
del_timer(&ese_cold_reset_timer);
}
pn544_dev->state_flags |= MASK_ESE_COLD_RESET_GUARD_TIMER;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
init_timer(&ese_cold_reset_timer);
setup_timer( &ese_cold_reset_timer, ese_cold_reset_gaurd_timer_callback, 0 );
#else
timer_setup(&ese_cold_reset_timer, ese_cold_reset_gaurd_timer_callback, 0);
#endif
ret = mod_timer(&ese_cold_reset_timer, jiffies + msecs_to_jiffies(ESE_COLD_RESET_GUARD_TIME));
if (ret)
printk( KERN_INFO "%s: Error in mod_timer\n",__func__);
return ret;
}
void ese_reset_resource_init(void) {
mutex_init(&ese_cold_reset_sync_mutex);
mutex_init(&nci_send_cmd_mutex);
}
void ese_reset_resource_destroy(void) {
mutex_destroy(&ese_cold_reset_sync_mutex);
mutex_destroy(&nci_send_cmd_mutex);
}
void rcv_prop_resp_status(const char * const buf)
{
int ret = -1;
char tmp[MAX_BUFFER_SIZE];
size_t rcount = 0;
memset(&prop_nci_rsp, 0, sizeof(prop_nci_rsp));
memcpy(prop_nci_rsp, buf, 3);
rcount = (size_t)prop_nci_rsp[2];
/* Read data: No need to wait for the interrupt */
ret = i2c_master_recv(pn544_dev->client, tmp, rcount);
if(ret == rcount){
prop_nci_rsp[3] = tmp[0];
pr_info("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__,prop_nci_rsp[0],
prop_nci_rsp[1],prop_nci_rsp[2],prop_nci_rsp[3]);
}else{
pr_err("%s : Failed to receive payload of the cold_rst_cmd\n",__func__);
prop_nci_rsp[3] = -EIO;
}
if(pn544_dev->state_flags &(P544_FLAG_NFC_ON)){
complete(&prop_cmd_resp_sema);
}
}
/******************************************************************************
* Function : send_nci_transceive
*
* Description : Common NCI command send utility function.
*
* Parameters : prop_cmd : Data to be sent to NFCC
* prop_cmd_size : Length of the data to be sent
*
* Returns : 0 (SUCCESS)/ (-1)otherwise
*****************************************************************************/
static int send_nci_transceive(uint8_t *prop_cmd, size_t prop_cmd_size) {
int ret = 0;
unsigned int loop=0x03;
struct file filp = {NULL};
int retry = 1;
pr_info("%s: Enter", __func__);
filp.private_data = pn544_dev;
if(pn544_dev->state_flags & P544_FLAG_FW_DNLD) {
/* If FW DNLD, Operation is not permitted */
pr_err("%s : Operation is not permitted during fwdnld\n", __func__);
return -ECANCELED;
}
mutex_lock(&nci_send_cmd_mutex);
init_completion(&prop_cmd_resp_sema);
/* write command to I2C line*/
do {
ret = i2c_master_send(pn544_dev->client, prop_cmd, prop_cmd_size);
if (ret == prop_cmd_size) {
break;
}
usleep_range(5000, 6000);
} while(loop--);
if(!loop && (ret != prop_cmd_size)) {
pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
mutex_unlock(&nci_send_cmd_mutex);
return -EREMOTEIO;
}
ret = 0x00;
do {
if(pn544_dev->state_flags & P544_FLAG_NFC_ON)/* NFC_ON */ {
/* Read is pending from the NFC service which will complete the prop_cmd_resp_sema */
if(wait_for_completion_timeout(&prop_cmd_resp_sema,
msecs_to_jiffies(NCI_CMD_RSP_TIMEOUT)) == 0){
pr_err("%s: Timeout", __func__);
ret = prop_nci_rsp[3] = -EAGAIN; // Failure case
}
} else { /* NFC_OFF */
/* call the pn544_dev_read() */
filp.f_flags &= ~O_NONBLOCK;
ret = pn544_dev_read(&filp, NULL,3, 0);
if(!ret)
break;
usleep_range(3500, 4000);
}
} while((retry-- >= 0) && ret == -ERESTARTSYS);
mutex_unlock(&nci_send_cmd_mutex);
if(0x00 == ret && prop_nci_rsp[3])
ret = -1 * prop_nci_rsp[3];
/* Return the status to the SPI/UWB Driver */
pr_info("%s: exit, Status:%d", __func__, ret);
return ret;
}
/******************************************************************************
* Function : do_reset_protection
*
* Description : It shall be called by SPI driver to enable/disable reset
* protection
*
* Parameters : Enable(TRUE)/Disable(FALSE)
*
* Returns :
* 0 : OK < Success case >
* -EPERM(-1) : REJECTED < NFCC rejects the cold reset cmd>
* -3 : FAILED < NFCC responds to cold reset cmd>
* -EIO(-5) : SYNTAX_ERROR < NFCC cmd framing is wrong >
* -6 : SEMANTIC_ERROR < NFCC rsp to cold reset cmd >
* -9 : INAVLID_PARAM < NFCC rsp to cold reset cmd >
* -EAGAIN(-11) : < 1. mod_timer(): temp error during kernel alloc >
* < 2. No rsp received from NFCC for cold reset cmd >
* -ENOMEM(-12) : < mod_timer(): failed to allocate memory >
* -EINVAL(-22) : < 1. cold rst req is received from unknown source >
* < 2. mod_timer(): invalid arg is passed>
* -EREMOTEIO(-121): < Reset cmd write failure over I2C >
* -ECANCELED(-125): < FW DWNLD is going on so driver canceled operation >
* -ERESTARTSYS(-512): < Userspace process is restarted during read operation >
*****************************************************************************/
int do_reset_protection(bool type) {
int ret = 0;
uint8_t prop_cmd[] = {0x2F, 0x1F, 0x01, 0x00};
pn544_dev = get_nfcc_dev_data();
pr_info("%s: Enter cmd type: %d", __func__, type);
prop_cmd[RST_PROTECTION_CMD_INDEX] = (type) ? 1 : 0;
if(type ) {
pn544_dev->state_flags |= RST_PROTECTION_ENABLED;
} else {
if(!(pn544_dev->state_flags & RST_PROTECTION_ENABLED)) {
return ret;
}
}
pr_info("%s: NxpNciX: %d > %02X%02X%02X%02X \n", __func__, ret,prop_cmd[0],
prop_cmd[1],prop_cmd[2],prop_cmd[3]);
ret = send_nci_transceive(prop_cmd, sizeof(prop_cmd));
if(ret) {
pr_err("%s : send_nci_command returned %d\n", __func__, ret);
}
if(!type) {
pn544_dev->state_flags &= ~RST_PROTECTION_ENABLED;
}
pr_info("%s: exit, Status:%d state_flag : %x ", __func__, ret,
pn544_dev->state_flags);
return ret;
}
EXPORT_SYMBOL(do_reset_protection);
/******************************************************************************
* Function : ese_cold_reset
*
* Description : It shall be called by NFC/SPI/UWB driver to perform driver to
* to driver eSE cold reset.
*
* Parameters : src Source of the cold reset request
*
* Returns :
* 0 : OK < Success case >
* -EPERM(-1) : REJECTED < Guard timer running>
* -3 : FAILED < NFCC responds to cold reset cmd>
* -EIO(-5) : SYNTAX_ERROR < NFCC cmd framing is wrong >
* -6 : SEMANTIC_ERROR < NFCC rsp to cold reset cmd >
* -9 : INAVLID_PARAM < NFCC rsp to cold reset cmd >
* -EAGAIN(-11) : < 1. mod_timer(): temp error during kernel alloc >
* < 2. No rsp received from NFCC for cold reset cmd >
* -ENOMEM(-12) : < mod_timer(): failed to allocate memory >
* -EBUSY(-16) : < eSE busy, in updater mode>
* -EINVAL(-22) : < 1. cold rst req is received from unknown source >
* < 2. mod_timer(): invalid arg is passed>
* -EREMOTEIO(-121): < Reset cmd write failure over I2C >
* -ECANCELED(-125): < FW DWNLD is going on so driver canceled operation >
* -ERESTARTSYS(-512): < Userspace process is restarted during read operation >
*****************************************************************************/
int ese_cold_reset(ese_cold_reset_origin_t src)
{
int ret = 0;
uint8_t ese_cld_reset[] = {0x2F, 0x1E, 0x00};
pr_info("%s: Enter origin:%d", __func__, src);
switch(src) {
case ESE_COLD_RESET_SOURCE_NFC:
case ESE_COLD_RESET_SOURCE_SPI:
case ESE_COLD_RESET_SOURCE_UWB:
break;
default:
pr_info("%s: Invalid argument", __func__);
return -EINVAL;
}
pn544_dev = get_nfcc_dev_data();
mutex_lock(&ese_cold_reset_sync_mutex);
if(IS_COLD_RESET_ALLOWED(pn544_dev->state_flags, src)) {
ret = start_ese_cold_reset_guard_timer();
if(ret) {
mutex_unlock(&ese_cold_reset_sync_mutex);
return ret; /* EAGAIN/EINVAL/ENOMEM*/
}
pn544_dev->state_flags |= src << ESE_COLD_RESET_ORIGIN_FLAGS_POS;
init_completion(&ese_cold_reset_sema);
pr_info("%s: NxpNciX: %d > %02X%02X%02X \n", __func__, ret,ese_cld_reset[0],
ese_cld_reset[1],ese_cld_reset[2]);
ret = send_nci_transceive(ese_cld_reset, sizeof(ese_cld_reset));
if(ret) {
pn544_dev->state_flags &= ~(MASK_ESE_COLD_RESET | MASK_ESE_COLD_RESET_GUARD_TIMER);
mutex_unlock(&ese_cold_reset_sync_mutex);
return ret;
}
/* wait for reboot guard timer*/
if(!ret && wait_for_completion_timeout(&ese_cold_reset_sema,
msecs_to_jiffies(ESE_COLD_RESET_REBOOT_GUARD_TIME)) == 0){
pr_info("%s: guard Timeout", __func__);
}
} else {
if(IS_RESET_PROTECTION_ENABLED(pn544_dev->state_flags)) {
pr_err("%s : Not allowed resource busy \n", __func__);
ret = -EBUSY;
}
else if(IS_COLD_RESET_REQ_IN_PROGRESS(pn544_dev->state_flags)) {
pr_err("%s : Operation not permitted \n", __func__);
ret = -EPERM;
}
else {
/*No Action required*/
}
}
pn544_dev->state_flags &= ~(src << ESE_COLD_RESET_ORIGIN_FLAGS_POS);
mutex_unlock(&ese_cold_reset_sync_mutex);
/* Return the status to the SPI/UWB Driver */
pr_info("%s:%d exit, Status:%d", __func__, src, ret);
return ret;
}
EXPORT_SYMBOL(ese_cold_reset);

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@@ -1,266 +0,0 @@
/*
* Copyright (C) 2010 Trusted Logic S.A.
*
* 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
*/
/******************************************************************************
*
* The original Work has been changed by NXP Semiconductors.
*
* Copyright (C) 2013-2020 NXP Semiconductors
* *
* 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
*
******************************************************************************/
#ifndef _PN553_H_
#define _PN553_H_
#include <linux/miscdevice.h>
#define PN544_MAGIC 0xE9
/*
* PN544 power control via ioctl
* PN544_SET_PWR(0): power off
* PN544_SET_PWR(1): power on
* PN544_SET_PWR(2): reset and power on with firmware download enabled
*/
#define PN544_SET_PWR _IOW(PN544_MAGIC, 0x01, long)
/*
* SPI Request NFCC to enable p61 power, only in param
* Only for SPI
* level 1 = Enable power
* level 0 = Disable power
* This also be used to perform eSE cold reset when
* argument value is 0x03
*/
#define P61_SET_SPI_PWR _IOW(PN544_MAGIC, 0x02, long)
/* SPI or DWP can call this ioctl to get the current
* power state of P61
*
*/
#define P61_GET_PWR_STATUS _IOR(PN544_MAGIC, 0x03, long)
/* DWP side this ioctl will be called
* level 1 = Wired access is enabled/ongoing
* level 0 = Wired access is disalbed/stopped
*/
#define P61_SET_WIRED_ACCESS _IOW(PN544_MAGIC, 0x04, long)
/*
NFC Init will call the ioctl to register the PID with the i2c driver
*/
#define P544_SET_NFC_SERVICE_PID _IOW(PN544_MAGIC, 0x05, long)
/*
NFC and SPI will call the ioctl to get the i2c/spi bus access
*/
#define P544_GET_ESE_ACCESS _IOW(PN544_MAGIC, 0x06, long)
/*
NFC and SPI will call the ioctl to update the power scheme
*/
#define P544_SET_POWER_SCHEME _IOW(PN544_MAGIC, 0x07, long)
/*
NFC will call the ioctl to release the svdd protection
*/
#define P544_REL_SVDD_WAIT _IOW(PN544_MAGIC, 0x08, long)
/* SPI or DWP can call this ioctl to get the current
* power state of P61
*
*/
#define PN544_SET_DWNLD_STATUS _IOW(PN544_MAGIC, 0x09, long)
/*
NFC will call the ioctl to release the dwp on/off protection
*/
#define P544_REL_DWPONOFF_WAIT _IOW(PN544_MAGIC, 0x0A, long)
/*
NFC will call the ioctl to start Secure Timer
*/
#define P544_SECURE_TIMER_SESSION _IOW(PN544_MAGIC, 0x0B, long)
#define MAX_ESE_ACCESS_TIME_OUT_MS 200 /*100 milliseconds*/
/*
NFC_ON: Driver is being used by the NFC service (bit b0)
*/
#define P544_FLAG_NFC_ON 0x01
/*
FW_DNLD: NFC_ON and FW download is going on (bit b1)
*/
#define P544_FLAG_FW_DNLD 0x02
/*
* VEN_RESET: NFC_ON and FW download with VEN reset (bit b2)
*/
#define P544_FLAG_NFC_VEN_RESET 0x04
/*
* ESE_RESET: Starting of flag positions for eSE cold reset origin
*/
#define ESE_COLD_RESET_ORIGIN_FLAGS_POS (4) //(bit b4)
#define ESE_COLD_RESET_ORIGIN_NFC_FLAG_POS (4) //(bit b4)
/*
* ESE_RESET: Mask for the flags used for Driver to driver cold reset
* b6, b5, b4 :
* 0 0 0 -> no request for ese_cold_reset
* 0 0 1 -> ese_cold_reset requested from NFC driver
* 0 1 0 -> ese_cold_reset requested from eSE driver
* 1 0 0 -> ese_cold_reset requested from UWB driver
*/
#define MASK_ESE_COLD_RESET (0x70)
/*
* ESE_RESET: Bit mask to check if ese_reset_guard timer is started (bit b7)
*/
#define MASK_ESE_COLD_RESET_GUARD_TIMER (0x80)
/*
* ESE_RESET: Guard time to allow eSE cold reset from the driver
*/
#define ESE_COLD_RESET_GUARD_TIME (3000) //3s
/*
* ESE_RESET: NCI command response timeout
*/
#define NCI_CMD_RSP_TIMEOUT (2000) //2s
/*
* ESE_RESET: Guard time to reboot the JCOP
*/
#define ESE_COLD_RESET_REBOOT_GUARD_TIME (50) //50ms
typedef enum p61_access_state{
P61_STATE_INVALID = 0x0000,
P61_STATE_IDLE = 0x0100, /* p61 is free to use */
P61_STATE_WIRED = 0x0200, /* p61 is being accessed by DWP (NFCC)*/
P61_STATE_SPI = 0x0400, /* P61 is being accessed by SPI */
P61_STATE_DWNLD = 0x0800, /* NFCC fw download is in progress */
P61_STATE_SPI_PRIO = 0x1000, /*Start of p61 access by SPI on priority*/
P61_STATE_SPI_PRIO_END = 0x2000, /*End of p61 access by SPI on priority*/
P61_STATE_SPI_END = 0x4000,
P61_STATE_JCP_DWNLD = 0x8000,/* JCOP downlad in progress */
P61_STATE_SECURE_MODE = 0x100000, /* secure mode state*/
P61_STATE_SPI_SVDD_SYNC_START = 0x0001, /*ESE_VDD Low req by SPI*/
P61_STATE_SPI_SVDD_SYNC_END = 0x0002, /*ESE_VDD is Low by SPI*/
P61_STATE_DWP_SVDD_SYNC_START = 0x0004, /*ESE_VDD Low req by Nfc*/
P61_STATE_DWP_SVDD_SYNC_END = 0x0008 /*ESE_VDD is Low by Nfc*/
}p61_access_state_t;
typedef enum chip_type_pwr_scheme{
PN67T_PWR_SCHEME = 0x01,
PN80T_LEGACY_PWR_SCHEME,
PN80T_EXT_PMU_SCHEME,
}chip_pwr_scheme_t;
typedef enum jcop_dwnld_state{
JCP_DWNLD_IDLE = P61_STATE_JCP_DWNLD, /* jcop dwnld is ongoing*/
JCP_DWNLD_INIT=0x8010, /* jcop dwonload init state*/
JCP_DWNLD_START=0x8020, /* download started */
JCP_SPI_DWNLD_COMPLETE=0x8040, /* jcop download complete in spi interface*/
JCP_DWP_DWNLD_COMPLETE=0x8080, /* jcop download complete */
} jcop_dwnld_state_t;
struct pn544_i2c_platform_data {
unsigned int irq_gpio;
unsigned int ven_gpio;
unsigned int firm_gpio;
unsigned int ese_pwr_gpio; /* gpio to give power to p61, only TEE should use this */
unsigned int iso_rst_gpio; /* gpio used for ISO hard reset P73*/
};
struct hw_type_info {
/*
* Response of get_version_cmd will be stored in data
* byte structure :
* byte 0-1 : Header
* byte 2 : Status
* byte 3 : Hardware Version
* byte 4 : ROM code
* byte 5 : 0x00 constant
* byte 6-7 : Protected data version
* byte 8-9 : Trim data version
* byte 10-11 : FW version
* byte 12-13 : CRC
* */
char data[20];
int len;
};
#define NEXUS5x 0
#define HWINFO 0
#if NEXUS5x
#undef ISO_RST
#else
#define ISO_RST
#endif
struct pn544_dev {
wait_queue_head_t read_wq;
struct mutex read_mutex;
struct i2c_client *client;
struct miscdevice pn544_device;
unsigned int ven_gpio;
unsigned int firm_gpio;
unsigned int irq_gpio;
unsigned int ese_pwr_gpio; /* gpio used by SPI to provide power to p61 via NFCC */
#ifdef ISO_RST
unsigned int iso_rst_gpio; /* ISO-RST pin gpio*/
#endif
struct mutex p61_state_mutex; /* used to make p61_current_state flag secure */
p61_access_state_t p61_current_state; /* stores the current P61 state */
bool nfc_ven_enabled; /* stores the VEN pin state powered by Nfc */
bool spi_ven_enabled; /* stores the VEN pin state powered by Spi */
bool irq_enabled;
spinlock_t irq_enabled_lock;
long nfc_service_pid; /*used to signal the nfc the nfc service */
chip_pwr_scheme_t chip_pwr_scheme;
unsigned int secure_timer_cnt;
struct workqueue_struct *pSecureTimerCbWq;
struct work_struct wq_task;
/* This byte represents different flags used during eSE cold reset request from
* Driver to driver
* Bit value Status Remark
* b0 : 1 -> NFC_ON Driver Open should set the flag
* 0 NFC_OFF Driver release should reset this flag
* b1 : 1 -> FWDNLD If FWDNLD is going on.
* 0 Normal operation
* b2 : 1 --> Ven reset has been requested
* b3 : reserved bit
* b6, b5, b4 :
* 0 0 0 -> no request for ese_cold_reset
* 0 0 1 -> ese_cold_reset requested from NFC driver
* 0 1 0 -> ese_cold_reset requested from eSE driver
* 0 1 1 -> ese_cold_reset requested from UWB driver
* Remaining combinations: Reserved for future use.
* These bits will be cleared once cold reset rsp is received.
* b7 : 1 --> The ese_cold reset guard time has is running
* It will be cleared by the Guard Timer Callback
* */
volatile uint8_t state_flags;
};
#endif