Updated corresponding to - NFC_AR_00_6000_11.02.00_OpnSrc

This commit is contained in:
Nanjesh
2020-04-24 23:24:40 +05:30
parent cb2bb40fc5
commit 25c57d3a9e
16 changed files with 2259 additions and 2390 deletions

View File

@@ -1,29 +0,0 @@
#
# near field communication configuration
#
config NXP_NFC_I2C
tristate "NFC on I2C Interface"
depends on I2C
---help---
This selects Near field controller on I2C Interface.
If you want NFC support, you should say Y here and
also to your specific host controller driver.
config NXP_NFC_I3C
tristate "NFC on I3C Interface"
depends on I3C
---help---
This selects Near field controller on I3C Interface.
If you want NFC support, you should say Y here and
also to your specific host controller driver.
config NXP_NFC_SN110
bool "Nxp NFC sn110 Controller"
---help---
You'll have to say Y if your computer contains an sn110 i2C device that
you want to use under Linux.
You can say N here if you don't have any sn110 I2C connected to your computer.

View File

@@ -1,11 +0,0 @@
#
# Makefile for nfc devices
#
ifdef CONFIG_NXP_NFC_I2C
obj-y += common.o
else ifdef CONFIG_NXP_NFC_I3C
obj-y += common.o
endif
obj-$(CONFIG_NXP_NFC_I2C) += i2c_drv.o
obj-$(CONFIG_NXP_NFC_I3C) += i3c_drv.o
obj-$(CONFIG_NXP_NFC_SN110) += sn110.o

View File

@@ -1,667 +0,0 @@
/******************************************************************************
* 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 "../nfc/cold_reset.h"
#include "common.h"
nfc_dev_t *nfc_dev_platform = NULL;
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;
}
//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->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;
}
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);
return -EINVAL;
}
//required for old platform only
nfc_gpio->ese_pwr = of_get_named_gpio(np, DTS_ESE_GPIO_STR, 0);
if ((!gpio_is_valid(nfc_gpio->ese_pwr))) {
pr_err("nfc ese_pwr gpio invalid %d\n", nfc_gpio->ese_pwr);
nfc_gpio->ese_pwr = -EINVAL;
}
pr_info("%s: %d, %d, %d, %d\n", __func__,
nfc_gpio->irq, nfc_gpio->ven, nfc_gpio->dwl_req,
nfc_gpio->ese_pwr);
return 0;
}
void gpio_set_ven(nfc_dev_t *nfc_dev, int value)
{
if (nfc_dev->ven_policy == VEN_ALWAYS_ENABLED) {
value |= 1;
}
if (gpio_get_value(nfc_dev->gpio.ven) != value) {
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));
else
ret = gpio_direction_input(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.ese_pwr)) {
gpio_free(nfc_dev->gpio.ese_pwr);
}
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;
}
static void enable_interrupt(nfc_dev_t *nfc_dev)
{
if (nfc_dev->interface == PLATFORM_IF_I2C)
i2c_enable_irq(&nfc_dev->i2c_dev);
else {
#ifdef CONFIG_NXP_NFC_I3C
i3c_enable_ibi(&nfc_dev->i3c_dev);
#endif //CONFIG_NXP_NFC_I3C
}
}
static void disable_interrupt(nfc_dev_t *nfc_dev)
{
if (nfc_dev->interface == PLATFORM_IF_I2C)
i2c_disable_irq(&nfc_dev->i2c_dev);
else {
#ifdef CONFIG_NXP_NFC_I3C
i3c_disable_ibi(&nfc_dev->i3c_dev);
#endif //CONFIG_NXP_NFC_I3C
}
}
static int send_cold_reset_cmd(nfc_dev_t *nfc_dev)
{
int ret = 0;
char cmd[COLD_RESET_CMD_LEN];
cmd[0] = COLD_RESET_CMD_GID;
cmd[1] = COLD_RESET_OID;
cmd[2] = COLD_RESET_CMD_PAYLOAD_LEN;
if (nfc_dev->interface == PLATFORM_IF_I2C) {
ret = i2c_write(&nfc_dev->i2c_dev, cmd,
COLD_RESET_CMD_LEN, MAX_RETRY_COUNT);
} else {
//TODO: Handling Cold reset for I3C
//ret = i3c_write(nfc_dev->i3c_dev, cmd, COLD_RESET_CMD_LEN);
}
if (ret != COLD_RESET_CMD_LEN) {
pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
nfc_dev->cold_reset.timer_started = false;
return -EIO;
}
pr_info("%s: NxpNciX: %d > %02X%02X%02X \n", __func__, ret, cmd[0],
cmd[1], cmd[2]);
return ret;
}
void read_cold_reset_rsp(nfc_dev_t *nfc_dev, char *buf)
{
int ret = -1;
char rsp[COLD_RESET_RSP_LEN];
i2c_dev_t *i2c_dev = &nfc_dev->i2c_dev;
cold_reset_t *cold_reset = &nfc_dev->cold_reset;
cold_reset->status = -EIO;
/*
* read header also if NFC is disabled
* for enable case, will be taken care by nfc read thread
*/
if (!cold_reset->nfc_enabled) {
if (nfc_dev->interface == PLATFORM_IF_I2C) {
ret = i2c_read(i2c_dev, rsp, NCI_HDR_LEN);
} else {
//TODO: Handling Cold reset for I3C
//ret = i3c_read(i3c_dev, rsp, NCI_HDR_LEN);
}
if (ret != NCI_HDR_LEN) {
pr_err("%s: failure to read cold reset rsp header\n",
__func__);
return;
}
} else {
memcpy(rsp, buf, NCI_HDR_LEN);
}
if ((NCI_HDR_LEN + rsp[NCI_PAYLOAD_LEN_OFFSET]) != COLD_RESET_RSP_LEN) {
pr_err("%s: - invalid response for cold_reset\n", __func__);
return;
}
if (nfc_dev->interface == PLATFORM_IF_I2C) {
ret = i2c_read(i2c_dev, &rsp[NCI_PAYLOAD_IDX], rsp[2]);
} else {
//TODO:Handling Cold Reset for I3C
//ret = i3c_read(nfc_dev->i3c_dev, &rsp[NCI_PAYLOAD_IDX], rsp[2]);
}
if (ret != rsp[2]) {
pr_err("%s: failure to read cold reset rsp header\n", __func__);
return;
}
pr_info("%s NxpNciR : len = 4 > %02X%02X%02X%02X\n", __func__, rsp[0],
rsp[1], rsp[2], rsp[3]);
cold_reset->status = rsp[NCI_PAYLOAD_IDX];
}
#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__);
nfc_dev_platform->cold_reset.timer_started = false;
return;
}
static long start_ese_cold_reset_guard_timer(void)
{
long ret = -EINVAL;
if (timer_pending(&nfc_dev_platform->cold_reset.timer) == 1) {
pr_info("ese_cold_reset_guard_timer: delete pending timer \n");
/* delete timer if already pending */
del_timer(&nfc_dev_platform->cold_reset.timer);
}
nfc_dev_platform->cold_reset.timer_started = true;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
init_timer(&nfc_dev_platform->cold_reset.timer);
setup_timer( &nfc_dev_platform->cold_reset.timer, ese_cold_reset_gaurd_timer_callback, 0);
#else
timer_setup(&nfc_dev_platform->cold_reset.timer, ese_cold_reset_gaurd_timer_callback, 0);
#endif
ret = mod_timer(&nfc_dev_platform->cold_reset.timer,
jiffies + msecs_to_jiffies(ESE_COLD_RESET_GUARD_TIME));
return ret;
}
static int perform_ese_cold_reset(nfc_dev_t *nfc_dev,
ese_cold_reset_origin_t origin)
{
int ret = 0;
if (gpio_get_value(nfc_dev->gpio.dwl_req)) {
pr_err("FW download in-progress\n");
return -EBUSY;
}
if (!gpio_get_value(nfc_dev->gpio.ven)) {
pr_err("VEN LOW - NFCC powered off\n");
return -ENODEV;
}
mutex_lock(&nfc_dev->cold_reset.sync_mutex);
if (!nfc_dev->cold_reset.timer_started) {
ret = start_ese_cold_reset_guard_timer();
if (ret) {
pr_err("%s: Error in mod_timer\n", __func__);
mutex_unlock(&nfc_dev->cold_reset.sync_mutex);
return ret;
}
/* set default value for status as failure */
nfc_dev->cold_reset.status = -EIO;
ret = send_cold_reset_cmd(nfc_dev);
if (ret <= 0) {
pr_err("failed to send cold reset command\n");
mutex_unlock(&nfc_dev->cold_reset.sync_mutex);
return ret;
}
ret = 0;
nfc_dev->cold_reset.rsp_pending = true;
/* check if NFC is enabled */
if (nfc_dev->cold_reset.nfc_enabled) {
/* Pending read from NFC_HAL will read the cold reset rsp and signal read_wq */
if (!wait_event_interruptible_timeout
(nfc_dev->cold_reset.read_wq,
nfc_dev->cold_reset.rsp_pending == false,
msecs_to_jiffies(ESE_COLD_RESET_CMD_RSP_TIMEOUT)))
{
pr_err("%s:Cold Reset Response Timeout\n",
__func__);
}
} else {
/* Read data as NFC thread is not active */
enable_interrupt(nfc_dev);
read_cold_reset_rsp(nfc_dev, NULL);
nfc_dev->cold_reset.rsp_pending = false;
// TODO: Handling Cold reset for I3C
// ret = i3c_read(i3c_dev, rsp, NCI_HDR_LEN);
}
if (!ret) { /* wait for reboot guard timer */
if (wait_event_interruptible_timeout
(nfc_dev->cold_reset.read_wq, true,
msecs_to_jiffies(ESE_COLD_RESET_REBOOT_GUARD_TIME))
== 0) {
pr_err("%s: guard Timeout interrupted",
__func__);
}
}
}
mutex_unlock(&nfc_dev->cold_reset.sync_mutex);
if (ret == 0) /* success case */
ret = nfc_dev->cold_reset.status;
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 (IS_COLD_RESET_REQ(arg) &&
nfc_dev->interface == PLATFORM_IF_I2C) {
ret = perform_ese_cold_reset(nfc_dev, arg);
} else if (arg == ESE_POWER_STATE) {
// eSE power state
ret = gpio_get_value(nfc_dev->gpio.ven);
} else {
pr_err("%s bad arg %lu\n", __func__, arg);
ret = -ENOIOCTLCMD;
}
return ret;
}
EXPORT_SYMBOL(nfc_ese_pwr);
/*
* This function shall be called from SPI, UWB, NFC driver to perform eSE cold reset.
*/
int ese_cold_reset(ese_cold_reset_origin_t origin)
{
int ret = 0;
unsigned long arg;
pr_info("%s: Enter origin:%d", __func__, origin);
switch (origin) {
case ESE_COLD_RESET_SOURCE_SPI:
arg = ESE_COLD_RESET_SPI;
break;
case ESE_COLD_RESET_SOURCE_UWB:
arg = ESE_COLD_RESET_UWB;
break;
default:
pr_info("%s: Invalid argument", __func__);
return -EINVAL;
}
if (nfc_dev_platform == NULL)
return -ENODEV;
ret = nfc_ese_pwr(nfc_dev_platform, arg);
pr_info("%s:%d exit, Status:%d", __func__, origin, ret);
return ret;
}
EXPORT_SYMBOL(ese_cold_reset);
/*
* nfc_ioctl_power_states() - power control
* @filp: pointer to the file descriptor
* @arg: mode that we want to move to
*
* Device power control. Depending on the arg value, device moves to
* different states
* (arg = 0): NFC_ENABLE GPIO = 0, FW_DL GPIO = 0
* (arg = 1): NFC_ENABLE GPIO = 1, FW_DL GPIO = 0
* (arg = 2): FW_DL GPIO = 1
*
* 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.
*/
disable_interrupt(nfc_dev);
pr_debug("gpio firm disable\n");
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_set_value(nfc_dev->gpio.dwl_req, 0);
usleep_range(10000, 10100);
}
if (gpio_is_valid(nfc_dev->gpio.ese_pwr)) {
if (!gpio_get_value(nfc_dev->gpio.ese_pwr)) {
pr_debug("disabling ven\n");
gpio_set_ven(nfc_dev, 0);
} else {
pr_debug("keeping ven high\n");
}
} else {
pr_debug("ese_pwr invalid, set ven to low\n");
gpio_set_ven(nfc_dev, 0);
}
nfc_dev->nfc_ven_enabled = false;
} else if (arg == NFC_POWER_ON) {
enable_interrupt(nfc_dev);
pr_debug("gpio_set_value enable: %s:\n", __func__);
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_set_value(nfc_dev->gpio.dwl_req, 0);
usleep_range(10000, 10100);
}
gpio_set_ven(nfc_dev, 1);
nfc_dev->nfc_ven_enabled = true;
#ifdef CONFIG_NXP_NFC_I3C
if (nfc_dev->interface == PLATFORM_IF_I3C)
nfc_dev->i3c_dev.read_hdr = NCI_HDR_LEN;
#endif //CONFIG_NXP_NFC_I3C
} else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
/*
* We are switching to Dowload Mode, toggle the enable pin
* in order to set the NFCC in the new mode
*/
if (gpio_is_valid(nfc_dev->gpio.ese_pwr)) {
if (gpio_get_value(nfc_dev->gpio.ese_pwr)) {
pr_err
("FW download forbidden while ese is on\n");
return -EBUSY; /* Device or resource busy */
}
}
gpio_set_value(nfc_dev->gpio.ven, 1);
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_set_value(nfc_dev->gpio.dwl_req, 1);
usleep_range(10000, 10100);
}
if (nfc_dev->interface == PLATFORM_IF_I2C) {
gpio_set_value(nfc_dev->gpio.ven, 0);
usleep_range(10000, 10100);
}
gpio_set_value(nfc_dev->gpio.ven, 1);
usleep_range(10000, 10100);
} else if (arg == NFC_FW_DWL_HIGH) {
/*
* Setting firmware download gpio to HIGH
* before FW download start
*/
pr_debug("set fw gpio high\n");
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_set_value(nfc_dev->gpio.dwl_req, 1);
usleep_range(10000, 10100);
} else
pr_debug("gpio.dwl_req is invalid\n");
} else if (arg == NFC_VEN_FORCED_HARD_RESET
&& nfc_dev->interface == PLATFORM_IF_I2C) {
/*
* TODO: Enable Ven reset for I3C, after hot join integration
*/
gpio_set_value(nfc_dev->gpio.ven, 0);
usleep_range(10000, 10100);
gpio_set_value(nfc_dev->gpio.ven, 1);
usleep_range(10000, 10100);
pr_info("%s VEN forced reset done\n", __func__);
} else if (arg == NFC_FW_DWL_LOW) {
/*
* Setting firmware download gpio to LOW
* FW download finished
*/
pr_debug("set fw gpio LOW\n");
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_set_value(nfc_dev->gpio.dwl_req, 0);
usleep_range(10000, 10100);
} else {
pr_debug("gpio.dwl_req is invalid\n");
}
#ifdef CONFIG_NXP_NFC_I3C
if (nfc_dev->interface == PLATFORM_IF_I3C)
nfc_dev->i3c_dev.read_hdr = NCI_HDR_LEN;
#endif //CONFIG_NXP_NFC_I3C
#ifdef CONFIG_NXP_NFC_I3C
} else if (arg == NFC_FW_HDR_LEN) {
if (nfc_dev->interface == PLATFORM_IF_I3C)
nfc_dev->i3c_dev.read_hdr = FW_HDR_LEN;
#endif //CONFIG_NXP_NFC_I3C
} 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, 3);
break;
case NFC_GET_PLATFORM_TYPE:
ret = nfc_dev->interface;
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);
if (!nfc_dev)
return -ENODEV;
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) {
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_set_value(nfc_dev->gpio.dwl_req, 0);
usleep_range(10000, 10100);
}
nfc_dev->cold_reset.nfc_enabled = true;
enable_interrupt(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);
if (!nfc_dev)
return -ENODEV;
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) {
disable_interrupt(nfc_dev);
if (gpio_is_valid(nfc_dev->gpio.dwl_req)) {
gpio_set_value(nfc_dev->gpio.dwl_req, 0);
usleep_range(10000, 10100);
}
nfc_dev->cold_reset.nfc_enabled = false;
}
if (nfc_dev->dev_ref_count > 0)
nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
filp->private_data = NULL;
mutex_unlock(&nfc_dev->dev_ref_mutex);
return 0;
}

View File

@@ -1,229 +0,0 @@
/******************************************************************************
* 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>
#ifdef CONFIG_NXP_NFC_I2C
#include "i2c_drv.h"
#endif
#ifdef CONFIG_NXP_NFC_I3C
#include "i3c_drv.h"
#endif
#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"
#define NCI_HDR_LEN 3 /* HDR length of NCI packet */
#define NCI_PAYLOAD_LEN_OFFSET 2
#define NCI_PAYLOAD_IDX 3
#define MAX_NCI_PAYLOAD_LEN (255)
#define MAX_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
#define MAX_RETRY_COUNT (3)
#define NO_RETRY (1)
#define MAX_IRQ_WAIT_TIME (90)
#define WAKEUP_SRC_TIMEOUT (2000)
/* ESE_COLD_RESET MACROS */
#define COLD_RESET_CMD_LEN 3
#define COLD_RESET_RSP_LEN 4
#define COLD_RESET_CMD_GID 0x2F
#define COLD_RESET_CMD_PAYLOAD_LEN 0x00
#define COLD_RESET_RSP_GID 0x4F
#define COLD_RESET_OID 0x1E
/*
* ESE_RESET: Bit mask to check if ese_reset_guard timer is started (bit b7)
* */
#define ESE_COLD_RESET_GUARD_TIMER_MASK (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 ESE_COLD_RESET_CMD_RSP_TIMEOUT (2000) //2s
/*
* ESE_RESET: Guard time to reboot the JCOP
*/
#define ESE_COLD_RESET_REBOOT_GUARD_TIME (50) //50ms
/*
* ESE_RESET: Checks if eSE cold reset has been requested
*/
#define IS_COLD_RESET_REQ(arg) ((arg == ESE_COLD_RESET_NFC) || \
(arg == ESE_COLD_RESET_SPI) || (arg == ESE_COLD_RESET_UWB))
/*
* ESE_RESET: macro evaluates to 1 if eSE cold reset response is received
* */
#define IS_COLD_RESET_RSP(buf) ((COLD_RESET_RSP_GID == buf[0]) && (COLD_RESET_OID == buf[1]))
#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, 0x0B)
#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"
#define DTS_ESE_GPIO_STR "nxp,pn544-ese-pwr"
enum ese_ioctl_request {
/* eSE POWER ON */
ESE_POWER_ON = 0,
/* eSE POWER OFF */
ESE_POWER_OFF,
/* eSE POWER STATE */
ESE_POWER_STATE,
/* eSE COLD RESET */
ESE_COLD_RESET_NFC,
ESE_COLD_RESET_SPI,
ESE_COLD_RESET_UWB,
};
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,
/*for HDR size change in FW mode */
NFC_FW_HDR_LEN,
/* Cold reset request for eSE */
NFC_ESE_COLD_RST,
};
/*nfc platform interface type*/
enum interface_flags {
/*I2C physical IF for NFCC */
PLATFORM_IF_I2C = 0,
/*I3C physical IF for NFCC */
PLATFORM_IF_I3C,
};
/*nfc platform interface type*/
enum ven_policy_flags {
/*VEN usage in lagacy platform */
VEN_LEGACY = 0,
/*VEN reset only to recover from failure usecases */
VEN_ALWAYS_ENABLED,
};
/* 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;
unsigned int ese_pwr;
} platform_gpio_t;
//Features specific Parameters
typedef struct cold_reset {
wait_queue_head_t read_wq;
bool rsp_pending;
unsigned int ntf;
uint8_t status;
/* NFC device opened by MW */
bool nfc_enabled;
/* eSe cold reset guard timer is started */
bool timer_started;
struct mutex sync_mutex;
struct timer_list timer;
} 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;
/*store the ven functioning */
uint8_t ven_policy;
/* NFC VEN pin state */
bool nfc_ven_enabled;
union {
#ifdef CONFIG_NXP_NFC_I2C
i2c_dev_t i2c_dev;
#endif
#ifdef CONFIG_NXP_NFC_I3C
i3c_dev_t i3c_dev;
#endif
};
platform_gpio_t gpio;
cold_reset_t cold_reset;
} 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 read_cold_reset_rsp(nfc_dev_t *nfc_dev, char *buf);
void gpio_set_ven(nfc_dev_t *nfc_dev, int value);
void gpio_free_all(nfc_dev_t *nfc_dev);
#endif //_COMMON_H_

View File

@@ -1,465 +0,0 @@
/******************************************************************************
* 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 "../nfc/cold_reset.h"
#include "common.h"
#include "sn110.h"
extern nfc_dev_t *nfc_dev_platform;
/**
* i2c_disable_irq()
*
* Check if interrupt is disabled or not
* and disable interrupt
*
* Return: void
*/
void i2c_disable_irq(i2c_dev_t *i2c_dev)
{
unsigned long flags;
spin_lock_irqsave(&i2c_dev->irq_enabled_lock, flags);
if (i2c_dev->irq_enabled) {
disable_irq_nosync(i2c_dev->client->irq);
i2c_dev->irq_enabled = false;
}
spin_unlock_irqrestore(&i2c_dev->irq_enabled_lock, flags);
}
/**
* i2c_enable_irq()
*
* Check if interrupt is enabled or not
* and enable interrupt
*
* Return: void
*/
void i2c_enable_irq(i2c_dev_t *i2c_dev)
{
unsigned long flags;
spin_lock_irqsave(&i2c_dev->irq_enabled_lock, flags);
if (!i2c_dev->irq_enabled) {
i2c_dev->irq_enabled = true;
enable_irq(i2c_dev->client->irq);
}
spin_unlock_irqrestore(&i2c_dev->irq_enabled_lock, flags);
}
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(i2c_dev);
wake_up(&nfc_dev->read_wq);
return IRQ_HANDLED;
}
int i2c_read(i2c_dev_t *i2c_dev, char *buf, size_t count)
{
int ret;
pr_debug("%s : reading %zu bytes.\n", __func__, count);
/* Read data */
ret = i2c_master_recv(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;
}
/* delay for the slow nfc devices between susequent read operation */
usleep_range(1000, 1100);
err:
return ret;
}
int i2c_write(i2c_dev_t *i2c_dev, 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(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);
}
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(i2c_dev);
if (!gpio_get_value(nfc_dev->gpio.ven)) {
pr_info("%s: releasing read\n", __func__);
ret = -EIO;
goto err;
}
if (gpio_get_value(nfc_dev->gpio.irq))
break;
pr_warn("%s: spurious interrupt detected\n", __func__);
}
}
memset(tmp, 0x00, count);
/* Read data */
ret = i2c_read(i2c_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 && IS_COLD_RESET_RSP(tmp)) {
read_cold_reset_rsp(nfc_dev, tmp);
nfc_dev->cold_reset.rsp_pending = false;
wake_up_interruptible(&nfc_dev->cold_reset.read_wq);
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_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;
if (copy_from_user(tmp, buf, count)) {
pr_err("%s : failed to copy from user space\n", __func__);
return -EFAULT;
}
ret = i2c_write(i2c_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_platform = nfc_dev;
nfc_dev->interface = PLATFORM_IF_I2C;
nfc_dev->i2c_dev.client = client;
i2c_dev = &nfc_dev->i2c_dev;
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);
goto err;
}
ret = configure_gpio(nfc_gpio.ese_pwr, GPIO_OUTPUT);
if (ret) {
pr_err("%s: unable to request nfc ese pwr gpio [%d]\n",
__func__, nfc_gpio.ese_pwr);
}
nfc_dev->gpio.ven = nfc_gpio.ven;
nfc_dev->gpio.irq = nfc_gpio.irq;
nfc_dev->gpio.dwl_req = nfc_gpio.dwl_req;
nfc_dev->gpio.ese_pwr = nfc_gpio.ese_pwr;
/* init mutex and queues */
init_waitqueue_head(&nfc_dev->read_wq);
init_waitqueue_head(&nfc_dev->cold_reset.read_wq);
mutex_init(&nfc_dev->read_mutex);
mutex_init(&nfc_dev->dev_ref_mutex);
mutex_init(&nfc_dev->ese_access_mutex);
mutex_init(&nfc_dev->cold_reset.sync_mutex);
spin_lock_init(&i2c_dev->irq_enabled_lock);
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(i2c_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;
nfc_dev->cold_reset.rsp_pending = false;
nfc_dev->cold_reset.nfc_enabled = false;
/*call to platform specific probe */
ret = sn110_i2c_probe(nfc_dev);
if (ret != 0) {
pr_err("%s: probing platform failed\n", __func__);
goto err_request_free_irq;
};
pr_info("%s probing nfc i2c successfully", __func__);
return 0;
err_request_free_irq:
free_irq(client->irq, nfc_dev);
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);
if (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;
}
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);
if (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);
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");

View File

@@ -1,47 +0,0 @@
/******************************************************************************
* 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 _I2C_DRV_H_
#define _I2C_DRV_H_
#include <linux/i2c.h>
/*kept same as dts */
#define NFC_I2C_DRV_STR "nxp,pn544"
#define NFC_I2C_DEV_ID "pn553"
//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;
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);
void i2c_disable_irq(i2c_dev_t *i2c_dev);
void i2c_enable_irq(i2c_dev_t *i2c_dev);
int i2c_write(i2c_dev_t *i2c_dev, char *buf, size_t count, int max_retry_cnt);
int i2c_read(i2c_dev_t *i2c_dev, char *buf, size_t count);
#endif //_I2C_DRV_H_

View File

@@ -1,766 +0,0 @@
/******************************************************************************
* 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/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 "sn110.h"
/** @brief This API used to write I3C data to I3C device.
*
* @param dev the i3c_dev for the i3c device.
* @param buf the data to write
* @param count the number of bytes of data to be written.
* @return ret number of bytes written ,negative error core otherwise.
*/
ssize_t i3c_write(i3c_dev_t *i3c_dev, const char *buf, const size_t count,
int max_retry_cnt)
{
int ret = -EIO;
int retry_count = 0;
struct i3c_priv_xfer write_buf = {
.rnw = NFC_I3C_WRITE,
.len = count,
.data.out = buf
};
do {
ret = i3c_device_do_priv_xfers(i3c_dev->device, &write_buf,
(sizeof(write_buf) /
sizeof(struct i3c_priv_xfer)));
pr_debug("%s exit ret = %x\n", __func__, ret);
if (!ret) {
ret = count;
break;
}
pr_err("%s errno = %x\n", __func__, write_buf.err);
retry_count++;
usleep_range(1000, 1100);
} while (retry_count < max_retry_cnt);
return ret;
}
/** @brief This API used to read data from I3C device.
*
* @param dev the i3c_dev for the slave.
* @param buf the buffer to copy the data.
* @param count the number of bytes to be read.
*
* @return number of bytes read ,negative error code otherwise.
*/
ssize_t i3c_read(i3c_dev_t *i3c_dev, char *buf, size_t count)
{
int ret = -EIO;
struct i3c_priv_xfer read_buf = {
.rnw = NFC_I3C_READ,
.len = count,
.data.in = buf
};
ret = i3c_device_do_priv_xfers(i3c_dev->device, &read_buf,
(sizeof(read_buf) /
sizeof(struct i3c_priv_xfer)));
pr_debug("%s exit ret = %x\n", __func__, ret);
if (!ret)
ret = count;
else
pr_err("%s errno = %x\n", __func__, read_buf.err);
return ret;
}
/** @brief This API can be used to write data to nci buf.
* The API will overwrite the existing memory if
* it reaches the end of total allocated memory.
* @param dev the dev structure for driver.
* @param readbuf the buffer to be copied data from
* @param count the number of bytes to copy to nci_buffer.
* @return number of bytes copied to nci buffer , error code otherwise
*/
static ssize_t i3c_kbuf_store(i3c_dev_t *i3c_dev, const char *buf,
const size_t count)
{
size_t buf_offset = 0;
size_t requested_size = count;
size_t available_size = 0;
if (i3c_dev == NULL)
return -ENODEV;
else if (buf == NULL || count == 0)
return -EINVAL;
pr_debug("%s enter\n", __func__);
if (count > i3c_dev->buf.total_size) {
pr_err("%s No memory to copy the data\n", __func__);
return -ENOMEM;
}
pr_debug("%s:total_size %zx write_off = %x read_off = %x\n",
__func__, i3c_dev->buf.total_size, i3c_dev->buf.write_offset,
i3c_dev->buf.read_offset);
mutex_lock(&i3c_dev->nci_buf_mutex);
available_size = i3c_dev->buf.total_size - i3c_dev->buf.write_offset;
/*
* When available buffer is less than requested count,
* copy the data upto available memory.
* The remaining data is copied to the start of memory.
* The write offset is incremented by the remaining copied bytes
* from the beginning.
*/
if (requested_size > available_size) {
pr_warn
("%s:out of mem req_size = %zx avail = %zx\n",
__func__, requested_size, available_size);
memcpy(i3c_dev->buf.kbuf + i3c_dev->buf.write_offset,
buf + buf_offset, available_size);
requested_size = requested_size - available_size;
i3c_dev->buf.write_offset = 0;
buf_offset = available_size;
pr_debug("%s: requested_size = %zx available_size = %zx\n",
__func__, requested_size, available_size);
}
if (requested_size) {
memcpy(i3c_dev->buf.kbuf + i3c_dev->buf.write_offset,
buf + buf_offset, requested_size);
i3c_dev->buf.write_offset += requested_size;
if (i3c_dev->buf.write_offset == i3c_dev->buf.total_size)
i3c_dev->buf.write_offset = 0;
}
complete(&i3c_dev->read_cplt);
mutex_unlock(&i3c_dev->nci_buf_mutex);
pr_debug("%s: total bytes req_size = %zx avail_size = %zx\n",
__func__, requested_size, available_size);
return count;
}
/** @brief This API can be used to retrieve data from driver buffer.
*
* When data is not available, it waits for required data to be present.
* When data is present it copies the data into buffer reuested by read.
* @param dev the dev structure for driver.
* @param buf the buffer to copy the data.
* @param count the number of bytes to be read.
* @return number of bytes copied , error code for failures .
*/
static ssize_t i3c_nci_kbuf_retrieve(i3c_dev_t *i3c_dev, char *buf,
size_t count)
{
int ret = 0;
size_t requested_size = count;
size_t available_size = 0;
size_t copied_size = 0;
if (i3c_dev == NULL)
return -ENODEV;
else if (buf == NULL || count == 0)
return -EINVAL;
pr_debug("%s enter\n", __func__);
/*
* When the requested data count is more than available data to read,
* wait on completion till the requested bytes are available.
* If write offset is more than read offset and available data is
* more than requested count, copy the requested bytes directly and
* increment the read_offset.
* If read offset is more than write offset,
* available size is total_size size - read_offset
* and upto write offset from the beginning of buffer.
*/
do {
pr_debug("%s: read_offset = %x write_offset = %x\n", __func__,
i3c_dev->buf.read_offset, i3c_dev->buf.write_offset);
mutex_lock(&i3c_dev->nci_buf_mutex);
if (i3c_dev->buf.read_offset <= i3c_dev->buf.write_offset)
available_size =
i3c_dev->buf.write_offset -
i3c_dev->buf.read_offset;
else
available_size =
(i3c_dev->buf.total_size -
i3c_dev->buf.read_offset) +
i3c_dev->buf.write_offset;
mutex_unlock(&i3c_dev->nci_buf_mutex);
if (available_size >= requested_size)
break;
reinit_completion(&i3c_dev->read_cplt);
ret = wait_for_completion_interruptible(&i3c_dev->read_cplt);
if (ret != 0) {
pr_err("didnt get completion, interrupted!! ret %d\n",
ret);
return -EINVAL;
}
} while (available_size < requested_size);
mutex_lock(&i3c_dev->nci_buf_mutex);
if (i3c_dev->buf.write_offset >= i3c_dev->buf.read_offset +
requested_size) {
/*
* Write offset is more than read offset + count , copy the data
* directly and increment the read offset
*/
memcpy(buf, i3c_dev->buf.kbuf + i3c_dev->buf.read_offset,
requested_size);
i3c_dev->buf.read_offset += requested_size;
} else {
copied_size =
i3c_dev->buf.total_size - i3c_dev->buf.read_offset;
if (copied_size > requested_size)
copied_size = requested_size;
/*
* Read offset is more than write offset. Copy requested data
* from read_offset to the total size and increment the read
* offset. If requested data is still greater than zero,
* copy the data from beginning of buffer.
*/
memcpy(buf, i3c_dev->buf.kbuf + i3c_dev->buf.read_offset,
copied_size);
requested_size = requested_size - copied_size;
i3c_dev->buf.read_offset += copied_size;
if (requested_size) {
pr_debug("%s remaining copied bytes\n", __func__);
memcpy(buf + copied_size, i3c_dev->buf.kbuf,
requested_size);
i3c_dev->buf.read_offset = requested_size;
}
}
mutex_unlock(&i3c_dev->nci_buf_mutex);
pr_debug("%s , count = %zx exit\n", __func__, count);
return count;
}
/** @brief This API can be used to read data from I3C device from HAL layer.
*
* This read function is registered during probe.
* When data is not available, it waits for required data to be present.
* @param filp the device file handle opened by HAL.
* @param buf the buffer to read the data.
* @param count the number of bytes to be read.
* @param offset the offset in the buf.
* @return Number of bytes read from I3C device ,error code for failures.
*/
ssize_t nfc_i3c_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;
i3c_dev_t *i3c_dev = &nfc_dev->i3c_dev;
if (count > MAX_BUFFER_SIZE)
count = MAX_BUFFER_SIZE;
memset(tmp, 0x00, count);
pr_debug("%s : reading %zu bytes\n", __func__, count);
mutex_lock(&nfc_dev->read_mutex);
ret = i3c_nci_kbuf_retrieve(i3c_dev, tmp, count);
if (ret != count) {
pr_err("%s: buf read from I3C device returned error (%d)\n",
__func__, ret);
ret = -EIO;
} else if (copy_to_user(buf, tmp, ret)) {
pr_warn("%s : failed to copy to user space\n", __func__);
ret = -EFAULT;
}
mutex_unlock(&nfc_dev->read_mutex);
return ret;
}
/** @brief This API can be used to write data to I3C device.
*
* @param dev the i3c_dev for the slave.
* @param buf the buffer to copy the data.
* @param count the number of bytes to be read.
* @return ret count number of btes written, error code for failures.
*/
ssize_t nfc_i3c_dev_write(struct file *filp, const char __user *buf,
size_t count, loff_t *offset)
{
int ret;
char tmp[MAX_BUFFER_SIZE];
nfc_dev_t *nfc_dev = filp->private_data;
i3c_dev_t *i3c_dev = &nfc_dev->i3c_dev;
if (count > MAX_BUFFER_SIZE) {
pr_err("%s: requested writes size(%d) more then expected\n",
__func__, count);
return -ENOMEM;
}
if (copy_from_user(tmp, buf, count)) {
pr_err("%s : failed to copy from user space\n", __func__);
ret = PTR_ERR(tmp);
return ret;
}
ret = i3c_write(i3c_dev, tmp, count, NO_RETRY);
if (ret != count) {
pr_err("%s: failed to write %d\n", __func__, ret);
ret = -EIO;
}
pr_debug("%s : i3c-%d: NfcNciTx %x %x %x\n", __func__,
iminor(file_inode(filp)), tmp[0], tmp[1], tmp[2]);
pr_debug("%s : ret = %x\n", __func__, ret);
return ret;
}
/** @brief This API shall be called from workqueue queue from IBI handler.
* First it will read HDR byte from I3C chip.Based on the length byte
* it will read the next length bytes.Then it will write these bytes
* to nci write buf.
* @param work the work added into the workqueue.
*
* @return void
*/
static void i3c_workqueue_handler(struct work_struct *work)
{
int ret = 0;
int length_byte = 0;
unsigned char *tmp = NULL;
unsigned char hdr_len = NCI_HDR_LEN;
i3c_dev_t *i3c_dev = container_of(work, i3c_dev_t, work);
if (!i3c_dev) {
pr_err("%s: dev not found\n", __func__);
return;
}
hdr_len = i3c_dev->read_hdr;
tmp = i3c_dev->read_kbuf;
if (!tmp) {
pr_err("%s: No memory to copy read data\n", __func__);
return;
}
pr_info("%s: hdr_len = %d\n", __func__, hdr_len);
memset(tmp, 0x00, i3c_dev->read_kbuf_len);
ret = i3c_read(i3c_dev, tmp, hdr_len);
if (ret < 0) {
pr_err("%s: i3c_read returned error %d\n", __func__, ret);
return;
}
if (hdr_len == FW_HDR_LEN)
length_byte = tmp[hdr_len - 1] + FW_CRC_LEN;
else
length_byte = tmp[hdr_len - 1];
ret = i3c_read(i3c_dev, tmp + hdr_len, length_byte);
if (ret < 0) {
pr_err("%s: i3c_read returned error %d\n", __func__, ret);
i3c_kbuf_store(i3c_dev, tmp, hdr_len);
return;
}
i3c_kbuf_store(i3c_dev, tmp, hdr_len + length_byte);
}
/** @brief This API is used to handle IBI coming from the I3C device.
* This will add work into the workqueue , which will call workqueue
* handler to read data from I3C device.
* @param device I3C device.
* @param payload payload shall be NULL for NFC device.
* @return void.
*/
static void i3c_ibi_handler(struct i3c_device *device,
const struct i3c_ibi_payload *payload)
{
nfc_dev_t *nfc_dev = i3cdev_get_drvdata(device);
i3c_dev_t *i3c_dev = &nfc_dev->i3c_dev;
pr_debug("%s: Received read IBI request from slave", __func__);
if (device_may_wakeup(&device->dev))
pm_wakeup_event(&device->dev, WAKEUP_SRC_TIMEOUT);
if (atomic_read(&i3c_dev->pm_state) == PM_STATE_NORMAL) {
if (!queue_work(i3c_dev->wq, &i3c_dev->work))
pr_debug("%s: queue work success\n", __func__);
} else {
/*assume suspend state and expect only 1 IBI in suspend state */
atomic_set(&i3c_dev->pm_state, PM_STATE_IBI_BEFORE_RESUME);
}
}
/** @brief This API can be used to enable IBI from the I3C device.
* @param i3c_dev the i3c_dev for the slave.
* @return 0 on success, error code for failures.
*/
int i3c_enable_ibi(i3c_dev_t *i3c_dev)
{
int ret = 0;
int retry_count = 0;
if (!i3c_dev->ibi_enabled) {
do {
ret = i3c_device_enable_ibi(i3c_dev->device);
if (!ret) {
i3c_dev->ibi_enabled = true;
break;
}
pr_debug("en_ibi ret %d retrying..\n", ret);
retry_count++;
usleep_range(RETRY_WAIT_TIME_USEC,
(RETRY_WAIT_TIME_USEC + 100));
} while (retry_count < RETRY_COUNT_IBI);
} else {
pr_debug("%s: already enabled\n", __func__);
}
return ret;
}
/** @brief This API can be used to disable IBI from the I3C device.
* @param i3c_dev the i3c_dev for the slave.
* @return 0 on success, error code for failures.
*/
int i3c_disable_ibi(i3c_dev_t *i3c_dev)
{
int ret = 0;
int retry_count = 0;
if (i3c_dev->ibi_enabled) {
do {
ret = i3c_device_disable_ibi(i3c_dev->device);
if (!ret) {
i3c_dev->ibi_enabled = false;
break;
}
pr_debug("dis_ibi ret %d retrying..\n", ret);
retry_count++;
usleep_range(RETRY_WAIT_TIME_USEC,
(RETRY_WAIT_TIME_USEC + 100));
} while (retry_count < RETRY_COUNT_IBI);
} else {
pr_debug("%s: already disabled\n", __func__);
}
return ret;
}
/** @brief This API can be used to request IBI from the I3C device.
* This function will request IBI from master controller for the device
* and register ibi handler, enable IBI .
* @param i3c_dev the i3c_dev for the slave.
* @return 0 on success, error code for failures.
*/
static int i3c_request_ibi(i3c_dev_t *i3c_dev)
{
int ret = 0;
struct i3c_ibi_setup ibireq = {
.handler = i3c_ibi_handler,
.max_payload_len = MAX_IBI_PAYLOAD_LEN,
.num_slots = NUM_NFC_IBI_SLOT,
};
ret = i3c_device_request_ibi(i3c_dev->device, &ibireq);
pr_debug("%s Request IBI status = %d\n", __func__, ret);
return ret;
}
/** @brief This API can be used to create a workqueue for
* handling IBI request from I3C device.
*
* @param dev the i3c_dev for the slave.
* @return 0 on success, error code for failures.
*/
static int i3c_init_workqueue(i3c_dev_t *i3c_dev)
{
i3c_dev->wq = alloc_workqueue(I3C_WORKQUEUE_NAME, 0, 0);
if (!i3c_dev->wq)
return -ENOMEM;
INIT_WORK(&i3c_dev->work, i3c_workqueue_handler);
pr_debug("%s ibi workqueue created successfully\n", __func__);
return 0;
}
/** @brief This API can be used to set the NCI buf to zero.
*
* @param dev the dev for the driver.
* @return 0 on success, error code for failures.
*/
static int i3c_reset_nci_buf(i3c_dev_t *i3c_dev)
{
i3c_dev->buf.write_offset = 0;
i3c_dev->buf.read_offset = 0;
memset(i3c_dev->buf.kbuf, 0, i3c_dev->buf.total_size);
return 0;
}
static const struct file_operations nfc_i3c_dev_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.read = nfc_i3c_dev_read,
.write = nfc_i3c_dev_write,
.open = nfc_dev_open,
.release = nfc_dev_close,
.unlocked_ioctl = nfc_dev_ioctl,
};
/** @brief This API can be used to probe I3c device.
*
* @param device the i3c_dev for the slave.
* @return 0 on success, error code for failures.
*/
int nfc_i3c_dev_probe(struct i3c_device *device)
{
int ret = 0;
nfc_dev_t *nfc_dev = NULL;
platform_gpio_t nfc_gpio;
pr_debug("%s: enter\n", __func__);
/*retrive details of gpios from dt */
ret = nfc_parse_dt(&device->dev, &nfc_gpio, PLATFORM_IF_I3C);
if (ret) {
pr_err("%s : failed to parse dt\n", __func__);
goto err;
}
nfc_dev = kzalloc(sizeof(nfc_dev_t), GFP_KERNEL);
if (nfc_dev == NULL) {
ret = -ENOMEM;
goto err;
}
/*
* Memory allocation for the read from the I3C before
* storing it in Kbuf store
*/
nfc_dev->i3c_dev.read_hdr = NCI_HDR_LEN;
nfc_dev->i3c_dev.read_kbuf_len = MAX_BUFFER_SIZE;
nfc_dev->i3c_dev.read_kbuf = kzalloc(MAX_BUFFER_SIZE,
GFP_DMA | GFP_KERNEL);
if (!nfc_dev->i3c_dev.read_kbuf) {
ret = -ENOMEM;
goto err;
}
/*
* Kbuf memory for storing NCI/Firmware Mode Buffers before
* actual read from the user
*/
nfc_dev->i3c_dev.buf.kbuf = (char *)__get_free_pages(GFP_KERNEL, 0);
if (!nfc_dev->i3c_dev.buf.kbuf) {
ret = -ENOMEM;
goto err;
}
nfc_dev->i3c_dev.buf.total_size = PAGE_SIZE;
i3c_reset_nci_buf(&nfc_dev->i3c_dev);
nfc_dev->interface = PLATFORM_IF_I3C;
nfc_dev->i3c_dev.device = device;
ret = configure_gpio(nfc_gpio.ven, GPIO_OUTPUT_HIGH);
if (ret) {
pr_err("%s: unable to request nfc reset gpio [%d]\n",
__func__, nfc_gpio.ven);
goto err;
}
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);
goto err;
}
nfc_dev->gpio.ven = nfc_gpio.ven;
nfc_dev->gpio.irq = nfc_gpio.irq;
nfc_dev->gpio.dwl_req = -EINVAL;
nfc_dev->gpio.ese_pwr = -EINVAL;
/* init mutex and queues */
init_completion(&nfc_dev->i3c_dev.read_cplt);
mutex_init(&nfc_dev->i3c_dev.nci_buf_mutex);
mutex_init(&nfc_dev->dev_ref_mutex);
mutex_init(&nfc_dev->read_mutex);
ret = i3c_init_workqueue(&nfc_dev->i3c_dev);
if (ret) {
pr_err("%s: alloc workqueue failed\n", __func__);
goto err_mutex_destroy;
}
ret = nfc_misc_register(nfc_dev, &nfc_i3c_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;
}
i3cdev_set_drvdata(device, nfc_dev);
ret = i3c_request_ibi(&nfc_dev->i3c_dev);
if (ret) {
pr_err("%s: i3c_request_ibi failed\n", __func__);
goto err_nfc_misc_unregister;
}
atomic_set(&nfc_dev->i3c_dev.pm_state, PM_STATE_NORMAL);
device_init_wakeup(&device->dev, true);
device_set_wakeup_capable(&device->dev, true);
/*call to platform specific probe */
ret = sn110_i3c_probe(nfc_dev);
if (ret != 0) {
pr_err("%s: probing platform failed\n", __func__);
goto err_nfc_misc_unregister;
};
pr_info("%s probing nfc i3c successfully", __func__);
return 0;
err_nfc_misc_unregister:
nfc_misc_unregister(nfc_dev, DEV_COUNT);
err_mutex_destroy:
mutex_destroy(&nfc_dev->read_mutex);
mutex_destroy(&nfc_dev->dev_ref_mutex);
mutex_destroy(&nfc_dev->i3c_dev.nci_buf_mutex);
err:
gpio_free_all(nfc_dev);
if (nfc_dev->i3c_dev.buf.kbuf)
free_pages((unsigned long)nfc_dev->i3c_dev.buf.kbuf, 0);
if (nfc_dev->i3c_dev.read_kbuf)
kfree(nfc_dev->i3c_dev.read_kbuf);
if (nfc_dev)
kfree(nfc_dev);
pr_err("%s: probing not successful, check hardware\n", __func__);
return ret;
}
/** @brief This API is automatically called on shutdown or crash.
*
* @param device the i3c_dev for the slave.
* @return 0 on success, error code for failures.
*/
int nfc_i3c_dev_remove(struct i3c_device *device)
{
nfc_dev_t *nfc_dev = i3cdev_get_drvdata(device);
i3c_dev_t *i3c_dev = NULL;
if (!nfc_dev) {
pr_err("%s: device doesn't exist anymore\n", __func__);
return -ENODEV;
}
i3c_dev = &nfc_dev->i3c_dev;
i3c_device_disable_ibi(device);
i3c_device_free_ibi(device);
if (i3c_dev->wq)
destroy_workqueue(i3c_dev->wq);
nfc_misc_unregister(nfc_dev, DEV_COUNT);
mutex_destroy(&nfc_dev->read_mutex);
mutex_destroy(&nfc_dev->dev_ref_mutex);
mutex_destroy(&i3c_dev->nci_buf_mutex);
gpio_free_all(nfc_dev);
kfree(i3c_dev->read_kbuf);
free_pages((unsigned long)i3c_dev->buf.kbuf, 0);
kfree(nfc_dev);
return 0;
}
int nfc_i3c_dev_suspend(struct device *pdev)
{
struct i3c_device *device = dev_to_i3cdev(pdev);
nfc_dev_t *nfc_dev = i3cdev_get_drvdata(device);
pr_debug("%s: enter\n", __func__);
if (!nfc_dev) {
pr_err("%s: device doesn't exist anymore\n", __func__);
return -ENODEV;
}
if (device_may_wakeup(&device->dev) && nfc_dev->i3c_dev.ibi_enabled)
atomic_set(&nfc_dev->i3c_dev.pm_state, PM_STATE_SUSPEND);
return 0;
}
int nfc_i3c_dev_resume(struct device *pdev)
{
struct i3c_device *device = dev_to_i3cdev(pdev);
nfc_dev_t *nfc_dev = i3cdev_get_drvdata(device);
i3c_dev_t *i3c_dev = NULL;
pr_debug("%s: enter\n", __func__);
if (!nfc_dev) {
pr_err("%s: device doesn't exist anymore\n", __func__);
return -ENODEV;
}
i3c_dev = &nfc_dev->i3c_dev;
if (device_may_wakeup(&device->dev)) {
if (atomic_read(&i3c_dev->pm_state) ==
PM_STATE_IBI_BEFORE_RESUME) {
/*queue the deferered work to work queue */
if (!queue_work(i3c_dev->wq, &i3c_dev->work))
pr_debug("%s: Added workqueue successfully\n",
__func__);
}
atomic_set(&i3c_dev->pm_state, PM_STATE_NORMAL);
}
return 0;
}
static const struct i3c_device_id nfc_i3c_dev_id[] = {
I3C_DEVICE(NFC_I3C_MANU_ID, NFC_I3C_PART_ID, 0),
{},
};
static const struct of_device_id nfc_i3c_dev_match_table[] = {
{
.compatible = NFC_I3C_DRV_STR
},
{}
};
static const struct dev_pm_ops nfc_i3c_dev_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(nfc_i3c_dev_suspend, nfc_i3c_dev_resume)
};
static struct i3c_driver nfc_i3c_dev_driver = {
.id_table = nfc_i3c_dev_id,
.probe = nfc_i3c_dev_probe,
.remove = nfc_i3c_dev_remove,
.driver = {
.owner = THIS_MODULE,
.name = NFC_I3C_DRV_STR,
.pm = &nfc_i3c_dev_pm_ops,
.of_match_table = nfc_i3c_dev_match_table,
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
},
};
MODULE_DEVICE_TABLE(of, nfc_i3c_dev_match_table);
static int __init nfc_dev_i3c_init(void)
{
int ret = 0;
pr_info("Loading NXP NFC I3C driver\n");
ret = i3c_driver_register_with_owner(&nfc_i3c_dev_driver, THIS_MODULE);
pr_debug("NFC i3c driver register ret = %d\n", ret);
return ret;
}
module_init(nfc_dev_i3c_init);
static void __exit nfc_i3c_dev_exit(void)
{
pr_info("Unloading NXP NFC I3C driver\n");
i3c_driver_unregister(&nfc_i3c_dev_driver);
}
module_exit(nfc_i3c_dev_exit);
MODULE_DESCRIPTION("NXP NFC I3C driver");
MODULE_LICENSE("GPL");

View File

@@ -1,109 +0,0 @@
/******************************************************************************
* 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 _I3C_DRV_H_
#define _I3C_DRV_H_
#include <linux/i3c/master.h>
#include <linux/i3c/device.h>
//to to kept same as dt
#define NFC_I3C_DRV_STR "nxp,pn544_i3c"
#define NFC_I3C_MANU_ID (0x011B)
#define NFC_I3C_PART_ID (0)
//Byte indicating I3C Read
#define NFC_I3C_READ 1
//Byte indicating I3C Write
#define NFC_I3C_WRITE 0
// Maximum no of IBI slot
#define NUM_NFC_IBI_SLOT 1
// Maximum IBI payload length
#define MAX_IBI_PAYLOAD_LEN 0
// CRC len to be read
#define FW_CRC_LEN 2
// FW DNLD HDR length
#define FW_HDR_LEN 2
// Time to wait before retrying I3C writes, in micro seconds
#define RETRY_WAIT_TIME_USEC (2000)
// Retry count for enable/disable IBI CCC
#define RETRY_COUNT_IBI (3)
// I3C WorkQueue name
#define I3C_WORKQUEUE_NAME "i3c_workq"
/**
* struct nci_buf - NCI buffer used to store and retrieve read data from device.
* @read_offset: The offset pointing to data available to read in nci buf.
* @write_offset: The offset pointing to free buf available to write.
* @total_size: Size of nci buf.
* @kbuf: allocated nci buf.
*/
struct nci_buf {
unsigned int read_offset;
unsigned int write_offset;
size_t total_size;
char *kbuf;
};
/**
* struct i3c_dev Structure representing device and driver data.
* @i3c_device Structure to represent I3C device.
* @wq: NCI workqueue for handling IBI request.
* @work: Work added to workqueue to read data from IBI handler.
* @buf Driver buf store read data from device.Read call will
* fetch from this buffer.
* @nci_buf_mutex: mutex to protect NCI buf retrieve/store .
* @read_cplt: Completion to wait for read data to be available.
* @read_kbuf_len: Temp buf len to hold I3C data.
* @read_kbuf: Temp buf to hold I3C data.
* @read_hdr Header size for reads.
* @ibi_enabled: IBI enabled or not.
* @pm_state: PM state of NFC I3C device.
*/
typedef struct i3c_dev {
struct i3c_device *device;
struct workqueue_struct *wq;
struct work_struct work;
struct nci_buf buf;
struct mutex nci_buf_mutex;
struct completion read_cplt;
size_t read_kbuf_len;
char *read_kbuf;
unsigned char read_hdr;
bool ibi_enabled;
atomic_t pm_state;
} i3c_dev_t;
int nfc_i3c_dev_probe(struct i3c_device *device);
int nfc_i3c_dev_remove(struct i3c_device *device);
int nfc_i3c_dev_suspend(struct device *device);
int nfc_i3c_dev_resume(struct device *device);
int i3c_enable_ibi(i3c_dev_t *i3c_dev);
int i3c_disable_ibi(i3c_dev_t *i3c_dev);
ssize_t i3c_write(i3c_dev_t *i3c_dev, const char *buf, const size_t count,
int max_retry_cnt);
ssize_t i3c_read(i3c_dev_t *, char *buf, size_t count);
#endif //_I3C_DRV_H_

View File

@@ -1,38 +0,0 @@
/******************************************************************************
* 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/delay.h>
#include "common.h"
#include "sn110.h"
int sn110_i2c_probe(nfc_dev_t *nfc_dev)
{
pr_debug("%s: enter\n", __func__);
usleep_range(5000, 5100);
gpio_set_value(nfc_dev->gpio.ven, 1);
usleep_range(5000, 5100);
nfc_dev->ven_policy = VEN_ALWAYS_ENABLED;
return 0;
}
int sn110_i3c_probe(nfc_dev_t *nfc_dev)
{
pr_debug("%s: enter\n", __func__);
nfc_dev->ven_policy = VEN_ALWAYS_ENABLED;
return 0;
}

View File

@@ -1,23 +0,0 @@
/******************************************************************************
* 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 _NFC_SN110_H_
#define _NFC_SN110_H_
int sn110_i2c_probe(nfc_dev_t *nfc_dev);
int sn110_i3c_probe(nfc_dev_t *nfc_dev);
#endif //_NFC_SN110_H_

13
pn553-i2c/Kconfig Normal file
View File

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

9
pn553-i2c/Makefile Normal file
View File

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

343
pn553-i2c/cold_reset.c Normal file
View File

@@ -0,0 +1,343 @@
/******************************************************************************
* 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;
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(2000, 3000);
}
} 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);

View File

@@ -16,13 +16,24 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
******************************************************************************/ ******************************************************************************/
#ifndef _NFC_COMMON_H_
#define _NFC_COMMON_H_
#ifndef _NFC_COLD_RESET_H_ #define MSG_NFCC_RSP 0x40
#define _NFC_COLD_RESET_H_ #define MSG_PROP_GID 0x0F
#define ESE_CLD_RST_OID 0x1E
#define RST_PROTECTION_CMD_INDEX 0x03
#define RST_PROTECTION_OID 0x1F
#define RST_PROTECTION_ENABLED 0x08
typedef enum ese_cold_reset_origin { typedef enum ese_cold_reset_origin {
ESE_COLD_RESET_SOURCE_SPI, ESE_COLD_RESET_NOT_REQUESTED = 0x00,
ESE_COLD_RESET_SOURCE_UWB, ESE_COLD_RESET_SOURCE_NFC = 0x01,
} ese_cold_reset_origin_t; ESE_COLD_RESET_SOURCE_SPI = 0x02,
ESE_COLD_RESET_SOURCE_UWB = 0x04,
}ese_cold_reset_origin_t;
#endif /* NFC_COLD_RESET_H_ */ void ese_reset_resource_init(void);
void ese_reset_resource_destroy(void);
#endif /* _NFC_COMMON_H_ */

1611
pn553-i2c/pn553.c Normal file

File diff suppressed because it is too large Load Diff

266
pn553-i2c/pn553.h Normal file
View File

@@ -0,0 +1,266 @@
/*
* 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