From 25c57d3a9ed89449979b811ed02c8d2b6bdf7b76 Mon Sep 17 00:00:00 2001 From: Nanjesh Date: Fri, 24 Apr 2020 23:24:40 +0530 Subject: [PATCH] Updated corresponding to - NFC_AR_00_6000_11.02.00_OpnSrc --- nfc/Kconfig | 29 - nfc/Makefile | 11 - nfc/common.c | 667 ------------- nfc/common.h | 229 ----- nfc/i2c_drv.c | 465 --------- nfc/i2c_drv.h | 47 - nfc/i3c_drv.c | 766 --------------- nfc/i3c_drv.h | 109 --- nfc/sn110.c | 38 - nfc/sn110.h | 23 - pn553-i2c/Kconfig | 13 + pn553-i2c/Makefile | 9 + pn553-i2c/cold_reset.c | 343 +++++++ {nfc => pn553-i2c}/cold_reset.h | 23 +- pn553-i2c/pn553.c | 1611 +++++++++++++++++++++++++++++++ pn553-i2c/pn553.h | 266 +++++ 16 files changed, 2259 insertions(+), 2390 deletions(-) delete mode 100644 nfc/Kconfig delete mode 100644 nfc/Makefile delete mode 100644 nfc/common.c delete mode 100644 nfc/common.h delete mode 100644 nfc/i2c_drv.c delete mode 100644 nfc/i2c_drv.h delete mode 100644 nfc/i3c_drv.c delete mode 100644 nfc/i3c_drv.h delete mode 100644 nfc/sn110.c delete mode 100644 nfc/sn110.h create mode 100644 pn553-i2c/Kconfig create mode 100644 pn553-i2c/Makefile create mode 100644 pn553-i2c/cold_reset.c rename {nfc => pn553-i2c}/cold_reset.h (62%) create mode 100644 pn553-i2c/pn553.c create mode 100644 pn553-i2c/pn553.h diff --git a/nfc/Kconfig b/nfc/Kconfig deleted file mode 100644 index f87a2b7818..0000000000 --- a/nfc/Kconfig +++ /dev/null @@ -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. diff --git a/nfc/Makefile b/nfc/Makefile deleted file mode 100644 index 8dc087ee68..0000000000 --- a/nfc/Makefile +++ /dev/null @@ -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 diff --git a/nfc/common.c b/nfc/common.c deleted file mode 100644 index 6c3f5c2a80..0000000000 --- a/nfc/common.c +++ /dev/null @@ -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 -#include -#include -#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; -} diff --git a/nfc/common.h b/nfc/common.h deleted file mode 100644 index 49f6e2b49b..0000000000 --- a/nfc/common.h +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include - -#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_ diff --git a/nfc/i2c_drv.c b/nfc/i2c_drv.c deleted file mode 100644 index 5d1e1393c5..0000000000 --- a/nfc/i2c_drv.c +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#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"); diff --git a/nfc/i2c_drv.h b/nfc/i2c_drv.h deleted file mode 100644 index eef57dc8d3..0000000000 --- a/nfc/i2c_drv.h +++ /dev/null @@ -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 - -/*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_ diff --git a/nfc/i3c_drv.c b/nfc/i3c_drv.c deleted file mode 100644 index 063308c268..0000000000 --- a/nfc/i3c_drv.c +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#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"); diff --git a/nfc/i3c_drv.h b/nfc/i3c_drv.h deleted file mode 100644 index 5ed2e77ca5..0000000000 --- a/nfc/i3c_drv.h +++ /dev/null @@ -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 -#include - -//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_ diff --git a/nfc/sn110.c b/nfc/sn110.c deleted file mode 100644 index 605ccf12a8..0000000000 --- a/nfc/sn110.c +++ /dev/null @@ -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 -#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; -} diff --git a/nfc/sn110.h b/nfc/sn110.h deleted file mode 100644 index 93de1bebfb..0000000000 --- a/nfc/sn110.h +++ /dev/null @@ -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_ diff --git a/pn553-i2c/Kconfig b/pn553-i2c/Kconfig new file mode 100644 index 0000000000..878e4a8169 --- /dev/null +++ b/pn553-i2c/Kconfig @@ -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. + diff --git a/pn553-i2c/Makefile b/pn553-i2c/Makefile new file mode 100644 index 0000000000..c5a3cd2a41 --- /dev/null +++ b/pn553-i2c/Makefile @@ -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 + diff --git a/pn553-i2c/cold_reset.c b/pn553-i2c/cold_reset.c new file mode 100644 index 0000000000..bc0b176731 --- /dev/null +++ b/pn553-i2c/cold_reset.c @@ -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 +#include +#include +#include +#include +#include +#include +#include +#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); diff --git a/nfc/cold_reset.h b/pn553-i2c/cold_reset.h similarity index 62% rename from nfc/cold_reset.h rename to pn553-i2c/cold_reset.h index 35afe748dc..59ceb374e3 100644 --- a/nfc/cold_reset.h +++ b/pn553-i2c/cold_reset.h @@ -16,13 +16,24 @@ * 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 _NFC_COLD_RESET_H_ +#define MSG_NFCC_RSP 0x40 +#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 { - ESE_COLD_RESET_SOURCE_SPI, - ESE_COLD_RESET_SOURCE_UWB, -} ese_cold_reset_origin_t; + 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; -#endif /* NFC_COLD_RESET_H_ */ +void ese_reset_resource_init(void); +void ese_reset_resource_destroy(void); +#endif /* _NFC_COMMON_H_ */ diff --git a/pn553-i2c/pn553.c b/pn553-i2c/pn553.c new file mode 100644 index 0000000000..0579655aab --- /dev/null +++ b/pn553-i2c/pn553.c @@ -0,0 +1,1611 @@ +/* + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 + * + ******************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +/* HiKey Compilation fix */ +#define HiKey_620_COMPILATION_FIX 1 +#ifndef HiKey_620_COMPILATION_FIX +#include +#endif + +#include +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) +#include +#include +#endif + +#include +#include "pn553.h" +#include "cold_reset.h" + +#define DRAGON_NFC 1 +#define SIG_NFC 44 +#define MAX_BUFFER_SIZE 512 +#define MAX_SECURE_SESSIONS 1 + +/* This macro evaluates to 1 if the cold reset is requested by driver(SPI/UWB). */ +#define IS_PROP_CMD_REQUESTED(flags) (flags & (MASK_ESE_COLD_RESET | RST_PROTECTION_ENABLED)) +/* This macro evaluates to 1 if eSE cold reset response is received */ +#define IS_PROP_RSP(buf) \ + (((MSG_NFCC_RSP | MSG_PROP_GID) == buf[0]) && ((ESE_CLD_RST_OID == buf[1]) || \ + (RST_PROTECTION_OID == buf[1]) )) + +/* VEN is kept ON all the time if you define the macro VEN_ALWAYS_ON. +Used for SN100 usecases */ +#define VEN_ALWAYS_ON +/* Macro added to disable SVDD power toggling */ +/* #define JCOP_4X_VALIDATION */ + + +/* HiKey Compilation fix */ +#ifndef HiKey_620_COMPILATION_FIX +struct wake_lock nfc_wake_lock; +#if HWINFO +struct hw_type_info hw_info; +#endif +static bool sIsWakeLocked = false; +#endif +static struct pn544_dev *pn544_dev; +static struct semaphore ese_access_sema; +static struct semaphore svdd_sync_onoff_sema; +static struct completion dwp_onoff_sema; +static struct timer_list secure_timer; +static void release_ese_lock(p61_access_state_t p61_current_state); +int get_ese_lock(p61_access_state_t p61_current_state, int timeout); +static long set_jcop_download_state(unsigned long arg); +static long start_seccure_timer(unsigned long timer_value); +static long secure_timer_operation(struct pn544_dev *pn544_dev, unsigned long arg); +extern void rcv_prop_resp_status(const char * const buf); +extern long ese_cold_reset(ese_cold_reset_origin_t src); +extern void ese_reset_resource_init(void); +extern void ese_reset_resource_destroy(void); +#if HWINFO +static void check_hw_info(void); +#endif +#define SECURE_TIMER_WORK_QUEUE "SecTimerCbWq" + +struct pn544_dev * get_nfcc_dev_data(void) { + return pn544_dev; +} +static void pn544_disable_irq(struct pn544_dev *pn544_dev) +{ + unsigned long flags; + + spin_lock_irqsave(&pn544_dev->irq_enabled_lock, flags); + if (pn544_dev->irq_enabled) { + disable_irq_nosync(pn544_dev->client->irq); + disable_irq_wake(pn544_dev->client->irq); + pn544_dev->irq_enabled = false; + } + spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags); +} + +static int pn544_dev_release(struct inode *inode, struct file *filp) { + pn544_dev->state_flags &= ~(P544_FLAG_NFC_VEN_RESET|P544_FLAG_NFC_ON|P544_FLAG_FW_DNLD); + if (pn544_dev->firm_gpio) + gpio_set_value(pn544_dev->firm_gpio, 0); + pr_info(KERN_ALERT "Exit %s: NFC driver release nfc hal \n", __func__); + return 0; +} +static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id) +{ + struct pn544_dev *pn544_dev = dev_id; + + pn544_disable_irq(pn544_dev); + /* HiKey Compilation fix */ + #ifndef HiKey_620_COMPILATION_FIX + if (sIsWakeLocked == false) + { + wake_lock(&nfc_wake_lock); + sIsWakeLocked = true; + } else { + pr_debug("%s already wake locked!\n", __func__); + } + #endif + /* Wake up waiting readers */ + wake_up(&pn544_dev->read_wq); + + + return IRQ_HANDLED; +} + +ssize_t pn544_dev_read(struct file *filp, char __user *buf, + size_t count, loff_t *offset) +{ + struct pn544_dev *pn544_dev = filp->private_data; + char tmp[MAX_BUFFER_SIZE]; + int ret; + + if (count > MAX_BUFFER_SIZE) + count = MAX_BUFFER_SIZE; + + //pr_debug("%s : reading %zu bytes.\n", __func__, count); + + mutex_lock(&pn544_dev->read_mutex); + + if (!gpio_get_value(pn544_dev->irq_gpio)) { + if (filp->f_flags & O_NONBLOCK) { + ret = -EAGAIN; + goto fail; + } + + while (1) { + pn544_dev->irq_enabled = true; + enable_irq(pn544_dev->client->irq); + enable_irq_wake(pn544_dev->client->irq); + ret = wait_event_interruptible( + pn544_dev->read_wq, + !pn544_dev->irq_enabled); + + pn544_disable_irq(pn544_dev); + + if (ret) + goto fail; + if(pn544_dev->state_flags & P544_FLAG_NFC_VEN_RESET) { + pr_warning("%s: releasing read \n", __func__); + pn544_dev->state_flags &= ~P544_FLAG_NFC_VEN_RESET; + ret = -EL3RST; + goto fail; + } + if (gpio_get_value(pn544_dev->irq_gpio)) + break; + + pr_warning("%s: spurious interrupt detected\n", __func__); + } + } + + /* Read data */ + ret = i2c_master_recv(pn544_dev->client, tmp, count); + #ifndef HiKey_620_COMPILATION_FIX + /* HiKey Compilation fix */ + if (sIsWakeLocked == true) { + wake_unlock(&nfc_wake_lock); + sIsWakeLocked = false; + } + #endif + + + /* If ese cold reset has been requested then read the response */ + if(IS_PROP_CMD_REQUESTED(pn544_dev->state_flags) && IS_PROP_RSP(tmp)) { + rcv_prop_resp_status(tmp); + /* Request is from driver, consume the response */ + mutex_unlock(&pn544_dev->read_mutex); + return 0; + } + mutex_unlock(&pn544_dev->read_mutex); + + /* pn544 seems to be slow in handling I2C read requests + * so add 1ms delay after recv operation */ +#if !NEXUS5x + udelay(1000); +#endif + + if (ret < 0) { + pr_err("%s: i2c_master_recv returned %d\n", __func__, ret); + return ret; + } + if (ret > count) { + pr_err("%s: received too many bytes from i2c (%d)\n", + __func__, ret); + return -EIO; + } + if (copy_to_user(buf, tmp, ret)) { + pr_warning("%s : failed to copy to user space\n", __func__); + return -EFAULT; + } + return ret; + + fail: + mutex_unlock(&pn544_dev->read_mutex); + return ret; +} + +static ssize_t pn544_dev_write(struct file *filp, const char __user *buf, + size_t count, loff_t *offset) +{ + struct pn544_dev *pn544_dev; + char tmp[MAX_BUFFER_SIZE]; + int ret; + + pn544_dev = filp->private_data; + + 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; + } + + //pr_debug("%s : writing %zu bytes.\n", __func__, count); + /* Write data */ + ret = i2c_master_send(pn544_dev->client, tmp, count); + if (ret != count) { + pr_err("%s : i2c_master_send returned %d\n", __func__, ret); + ret = -EIO; + } + /* pn544 seems to be slow in handling I2C write requests + * so add 1ms delay after I2C send oparation */ + udelay(1000); + + return ret; +} + +static void p61_update_access_state(struct pn544_dev *pn544_dev, p61_access_state_t current_state, bool set) +{ + pr_info("%s: Enter current_state = %x\n", __func__, pn544_dev->p61_current_state); + if (current_state) + { + if(set){ + if(pn544_dev->p61_current_state == P61_STATE_IDLE) + pn544_dev->p61_current_state = P61_STATE_INVALID; + pn544_dev->p61_current_state |= current_state; + } + else{ + pn544_dev->p61_current_state ^= current_state; + if(!pn544_dev->p61_current_state) + pn544_dev->p61_current_state = P61_STATE_IDLE; + } + } + pr_info("%s: Exit current_state = %x\n", __func__, pn544_dev->p61_current_state); +} + +static void p61_get_access_state(struct pn544_dev *pn544_dev, p61_access_state_t *current_state) +{ + + if (current_state == NULL) { + //*current_state = P61_STATE_INVALID; + pr_err("%s : invalid state of p61_access_state_t current state \n", __func__); + } else { + *current_state = pn544_dev->p61_current_state; + } +} +static void p61_access_lock(struct pn544_dev *pn544_dev) +{ + mutex_lock(&pn544_dev->p61_state_mutex); +} +static void p61_access_unlock(struct pn544_dev *pn544_dev) +{ + mutex_unlock(&pn544_dev->p61_state_mutex); +} + +static int signal_handler(p61_access_state_t state, long nfc_pid) +{ + struct siginfo sinfo; + pid_t pid; + struct task_struct *task; + int sigret = 0, ret = 0; + pr_info("%s: Enter\n", __func__); + + if(nfc_pid == 0) + { + pr_info("nfc_pid is clear don't call signal_handler.\n"); + } + else + { + memset(&sinfo, 0, sizeof(struct siginfo)); + sinfo.si_signo = SIG_NFC; + sinfo.si_code = SI_QUEUE; + sinfo.si_int = state; + pid = nfc_pid; + + task = pid_task(find_vpid(pid), PIDTYPE_PID); + if(task) + { + pr_info("%s.\n", task->comm); + sigret = force_sig_info(SIG_NFC, &sinfo, task); + if(sigret < 0){ + pr_info("send_sig_info failed..... sigret %d.\n", sigret); + ret = -1; + //msleep(60); + } + } + else{ + pr_info("finding task from PID failed\r\n"); + ret = -1; + } + } + pr_info("%s: Exit ret = %d\n", __func__, ret); + return ret; +} +static void svdd_sync_onoff(long nfc_service_pid, p61_access_state_t origin) +{ + int timeout = 100; //100 ms timeout + unsigned long tempJ = msecs_to_jiffies(timeout); + pr_info("%s: Enter nfc_service_pid: %ld\n", __func__, nfc_service_pid); + if(nfc_service_pid) + { + if (0 == signal_handler(origin, nfc_service_pid)) + { + sema_init(&svdd_sync_onoff_sema, 0); + pr_info("Waiting for svdd protection response"); + if(down_timeout(&svdd_sync_onoff_sema, tempJ) != 0) + { + pr_info("svdd wait protection: Timeout"); + } + pr_info("svdd wait protection : released"); + } + } +} +static int release_svdd_wait(void) +{ + pr_info("%s: Enter \n", __func__); + up(&svdd_sync_onoff_sema); + return 0; +} + +static void dwp_OnOff(long nfc_service_pid, p61_access_state_t origin) +{ + int timeout = 100; //100 ms timeout + unsigned long tempJ = msecs_to_jiffies(timeout); + if(nfc_service_pid) + { + if (0 == signal_handler(origin, nfc_service_pid)) + { + init_completion(&dwp_onoff_sema); + if(wait_for_completion_timeout(&dwp_onoff_sema, tempJ) != 0) + { + pr_info("Dwp On/off wait protection: Timeout"); + } + pr_info("Dwp On/Off wait protection : released"); + } + } +} +static int release_dwpOnOff_wait(void) +{ + pr_info("%s: Enter \n", __func__); + complete(&dwp_onoff_sema); + return 0; +} + +static int pn544_dev_open(struct inode *inode, struct file *filp) +{ + struct pn544_dev *pn544_dev = container_of(filp->private_data, + struct pn544_dev, + pn544_device); + + filp->private_data = pn544_dev; + pn544_dev->state_flags |= (P544_FLAG_NFC_ON); + pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode)); + + return 0; +} + +static int set_nfc_pid(unsigned long arg) +{ + pr_info("%s : The NFC Service PID is %ld\n", __func__, arg); + pn544_dev->nfc_service_pid = arg; + return 0; +} + +long pn544_dev_ioctl(struct file *filp, unsigned int cmd, + unsigned long arg) +{ + /* Free pass autobahn area, not protected. Use it carefullly. START */ + switch(cmd) + { + case P544_GET_ESE_ACCESS: + return get_ese_lock(P61_STATE_WIRED, arg); + break; + case P544_REL_SVDD_WAIT: + return release_svdd_wait(); + break; + case P544_SET_NFC_SERVICE_PID: + return set_nfc_pid(arg); + break; + case P544_REL_DWPONOFF_WAIT: + return release_dwpOnOff_wait(); + break; + default: + break; + } + /* Free pass autobahn area, not protected. Use it carefullly. END */ + + p61_access_lock(pn544_dev); + switch (cmd) { + case PN544_SET_PWR: + { + p61_access_state_t current_state = P61_STATE_INVALID; + p61_get_access_state(pn544_dev, ¤t_state); + if (arg == 2) { + if (current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO) && (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME)) + { + /* NFCC fw/download should not be allowed if p61 is used + * by SPI + */ + pr_info("%s NFCC should not be allowed to reset/FW download \n", __func__); + p61_access_unlock(pn544_dev); + return -EBUSY; /* Device or resource busy */ + } + pn544_dev->nfc_ven_enabled = true; + if ((pn544_dev->spi_ven_enabled == false && !(pn544_dev->secure_timer_cnt)) + || (pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME)) + { + /* power on with firmware download (requires hw reset) + */ + pr_info("%s power on with firmware\n", __func__); + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + if (pn544_dev->firm_gpio) { + p61_update_access_state(pn544_dev, P61_STATE_DWNLD, true); + gpio_set_value(pn544_dev->firm_gpio, 1); + pn544_dev->state_flags |= (P544_FLAG_FW_DNLD); + } + + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 0); + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + } + } else if (arg == 1) { + /* power on */ + if (pn544_dev->firm_gpio) { + if ((current_state & (P61_STATE_WIRED|P61_STATE_SPI|P61_STATE_SPI_PRIO))== 0){ + p61_update_access_state(pn544_dev, P61_STATE_IDLE, true); + } + if(current_state & P61_STATE_DWNLD){ + p61_update_access_state(pn544_dev, P61_STATE_DWNLD, false); + } + gpio_set_value(pn544_dev->firm_gpio, 0); + pn544_dev->state_flags &= ~(P544_FLAG_FW_DNLD); + } + + pn544_dev->nfc_ven_enabled = true; + #ifndef VEN_ALWAYS_ON + if (pn544_dev->spi_ven_enabled == false || (pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME)) { + gpio_set_value(pn544_dev->ven_gpio, 1); + } + #endif + } else if (arg == 0) { + /* power off */ + if (pn544_dev->firm_gpio) { + if ((current_state & (P61_STATE_WIRED|P61_STATE_SPI|P61_STATE_SPI_PRIO))== 0){ + p61_update_access_state(pn544_dev, P61_STATE_IDLE, true); + } + gpio_set_value(pn544_dev->firm_gpio, 0); + } + + pn544_dev->nfc_ven_enabled = false; + /* Don't change Ven state if spi made it high */ + #ifndef VEN_ALWAYS_ON + if ((pn544_dev->spi_ven_enabled == false && !(pn544_dev->secure_timer_cnt)) + || (pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME)) { + gpio_set_value(pn544_dev->ven_gpio, 0); + } + #endif + /* HiKey Compilation fix */ + #ifndef HiKey_620_COMPILATION_FIX + if (sIsWakeLocked == true) { + wake_unlock(&nfc_wake_lock); + sIsWakeLocked = false; + } + #endif + } else if (arg == 3) { + /*NFC Service called ISO-RST*/ + p61_access_state_t current_state = P61_STATE_INVALID; + p61_get_access_state(pn544_dev, ¤t_state); + if(current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) { + p61_access_unlock(pn544_dev); + return -EPERM; /* Operation not permitted */ + } + if(current_state & P61_STATE_WIRED) { + p61_update_access_state(pn544_dev, P61_STATE_WIRED, false); + } +#ifdef ISO_RST + gpio_set_value(pn544_dev->iso_rst_gpio, 0); + msleep(50); + gpio_set_value(pn544_dev->iso_rst_gpio, 1); + msleep(50); + pr_info("%s ISO RESET from DWP DONE\n", __func__); +#endif + } else if (arg == 4) { + pr_info("%s FW dwldioctl called from NFC \n", __func__); + /*NFC Service called FW dwnld*/ + if (pn544_dev->firm_gpio) { + p61_update_access_state(pn544_dev, P61_STATE_DWNLD, true); + gpio_set_value(pn544_dev->firm_gpio, 1); + pn544_dev->state_flags |= (P544_FLAG_FW_DNLD); + msleep(10); + } + } else if (arg == 5) { + pn544_dev->state_flags |= P544_FLAG_NFC_VEN_RESET; + pn544_disable_irq(pn544_dev); + wake_up(&pn544_dev->read_wq); + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 0); + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + pr_info("%s VEN reset DONE >>>>>>>\n", __func__); + } else if (arg == 6) { + if (pn544_dev->firm_gpio) { + gpio_set_value(pn544_dev->firm_gpio, 0); + pn544_dev->state_flags &= ~(P544_FLAG_FW_DNLD); + } + pr_info("%s FW GPIO set to 0x00 >>>>>>>\n", __func__); + }else { + pr_err("%s bad arg %lu\n", __func__, arg); + /* changed the p61 state to idle*/ + p61_access_unlock(pn544_dev); + return -EINVAL; + } + } + break; + case P61_SET_SPI_PWR: + { + p61_access_state_t current_state = P61_STATE_INVALID; + p61_get_access_state(pn544_dev, ¤t_state); + if (arg == 1) { + pr_info("%s : PN61_SET_SPI_PWR - power on ese\n", __func__); + if ((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0) + { + p61_update_access_state(pn544_dev, P61_STATE_SPI, true); + /*To handle triple mode protection signal + NFC service when SPI session started*/ + if (!(current_state & P61_STATE_JCP_DWNLD)){ + if(pn544_dev->nfc_service_pid){ + pr_info("nfc service pid %s ---- %ld", __func__, pn544_dev->nfc_service_pid); + /*signal_handler(P61_STATE_SPI, pn544_dev->nfc_service_pid);*/ + dwp_OnOff(pn544_dev->nfc_service_pid, P61_STATE_SPI); + } + else{ + pr_info(" invalid nfc service pid....signalling failed%s ---- %ld", __func__, pn544_dev->nfc_service_pid); + } + } + pn544_dev->spi_ven_enabled = true; + + if(pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME) + break; + #ifndef VEN_ALWAYS_ON + if (pn544_dev->nfc_ven_enabled == false) + { + /* provide power to NFCC if, NFC service not provided */ + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + } + #endif + /* pull the gpio to high once NFCC is power on*/ + gpio_set_value(pn544_dev->ese_pwr_gpio, 1); + + /* Delay (10ms) after SVDD_PWR_ON to allow JCOP to bootup (5ms jcop boot time + 5ms guard time) */ + usleep_range(10000, 12000); + + } else { + pr_info("%s : PN61_SET_SPI_PWR - power on ese failed \n", __func__); + p61_access_unlock(pn544_dev); + return -EBUSY; /* Device or resource busy */ + } + } else if (arg == 0) { + pr_info("%s : PN61_SET_SPI_PWR - power off ese\n", __func__); + if(current_state & P61_STATE_SPI_PRIO){ + p61_update_access_state(pn544_dev, P61_STATE_SPI_PRIO, false); + if (!(current_state & P61_STATE_JCP_DWNLD)) + { + if(pn544_dev->nfc_service_pid){ + pr_info("nfc service pid %s ---- %ld", __func__, pn544_dev->nfc_service_pid); + if(!(current_state & P61_STATE_WIRED)) + { + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START | + P61_STATE_SPI_PRIO_END); + }else { + signal_handler(P61_STATE_SPI_PRIO_END, pn544_dev->nfc_service_pid); + } + } + else{ + pr_info(" invalid nfc service pid....signalling failed%s ---- %ld", __func__, pn544_dev->nfc_service_pid); + } + } else if (!(current_state & P61_STATE_WIRED)) { + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START); + } + pn544_dev->spi_ven_enabled = false; + + if(pn544_dev->chip_pwr_scheme == PN80T_EXT_PMU_SCHEME) + break; + + /* if secure timer is running, Delay the SPI close by 25ms after sending End of Apdu to enable eSE go into DPD + gracefully (20ms after EOS + 5ms DPD settlement time) */ + if(pn544_dev->secure_timer_cnt) + usleep_range(25000, 30000); + + if (!(current_state & P61_STATE_WIRED) && !(pn544_dev->secure_timer_cnt)) + { +#ifndef JCOP_4X_VALIDATION + gpio_set_value(pn544_dev->ese_pwr_gpio, 0); + /* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */ + usleep_range(2500, 3000); +#endif + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END); + } +#ifndef JCOP_4X_VALIDATION + #ifndef VEN_ALWAYS_ON + if ((pn544_dev->nfc_ven_enabled == false) && !(pn544_dev->secure_timer_cnt)) { + gpio_set_value(pn544_dev->ven_gpio, 0); + msleep(10); + } + #endif +#endif + }else if(current_state & P61_STATE_SPI){ + p61_update_access_state(pn544_dev, P61_STATE_SPI, false); + if (!(current_state & P61_STATE_WIRED) && + (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME) && + !(current_state & P61_STATE_JCP_DWNLD)) + { + if(pn544_dev->nfc_service_pid){ + pr_info("nfc service pid %s ---- %ld", __func__, pn544_dev->nfc_service_pid); + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START | P61_STATE_SPI_END); + } + else{ + pr_info(" invalid nfc service pid....signalling failed%s ---- %ld", __func__, pn544_dev->nfc_service_pid); + } + /* if secure timer is running, Delay the SPI close by 25ms after sending End of Apdu to enable eSE go into DPD + gracefully (20ms after EOS + 5ms DPD settlement time) */ + if(pn544_dev->secure_timer_cnt) + usleep_range(25000, 30000); + + if (!(pn544_dev->secure_timer_cnt)) { +#ifndef JCOP_4X_VALIDATION + gpio_set_value(pn544_dev->ese_pwr_gpio, 0); + /* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */ + usleep_range(2500, 3000); +#endif + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END); + } + } + /*If JCOP3.2 or 3.3 for handling triple mode + protection signal NFC service */ + else + { + if (!(current_state & P61_STATE_JCP_DWNLD)) + { + if(pn544_dev->nfc_service_pid){ + pr_info("nfc service pid %s ---- %ld", __func__, pn544_dev->nfc_service_pid); + if(pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME) + { + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START | P61_STATE_SPI_END); + } else { + signal_handler(P61_STATE_SPI_END, pn544_dev->nfc_service_pid); + } + } + else{ + pr_info(" invalid nfc service pid....signalling failed%s ---- %ld", __func__, pn544_dev->nfc_service_pid); + } + } else if (pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME) { + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START); + } + if(pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME) + { +#ifndef JCOP_4X_VALIDATION + gpio_set_value(pn544_dev->ese_pwr_gpio, 0); +#endif + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END); + pr_info("PN80T legacy ese_pwr_gpio off %s", __func__); + } + } + pn544_dev->spi_ven_enabled = false; +#ifndef VEN_ALWAYS_ON + if (pn544_dev->nfc_ven_enabled == false && (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME) + && !(pn544_dev->secure_timer_cnt)) { + gpio_set_value(pn544_dev->ven_gpio, 0); + msleep(10); + } +#endif + } else { + pr_err("%s : PN61_SET_SPI_PWR - failed, current_state = %x \n", + __func__, pn544_dev->p61_current_state); + p61_access_unlock(pn544_dev); + return -EPERM; /* Operation not permitted */ + } + }else if (arg == 2) { + pr_info("%s : PN61_SET_SPI_PWR - reset\n", __func__); + if (current_state & (P61_STATE_IDLE|P61_STATE_SPI|P61_STATE_SPI_PRIO)) { + if (pn544_dev->spi_ven_enabled == false) + { + pn544_dev->spi_ven_enabled = true; + #ifndef VEN_ALWAYS_ON + if ((pn544_dev->nfc_ven_enabled == false) && (pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME)) { + /* provide power to NFCC if, NFC service not provided */ + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + } + #endif + } + if(pn544_dev->chip_pwr_scheme != PN80T_EXT_PMU_SCHEME && !(pn544_dev->secure_timer_cnt)) + { + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START); +#ifndef JCOP_4X_VALIDATION + gpio_set_value(pn544_dev->ese_pwr_gpio, 0); +#endif + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END); + msleep(10); + if(!gpio_get_value(pn544_dev->ese_pwr_gpio)) + gpio_set_value(pn544_dev->ese_pwr_gpio, 1); + msleep(10); + } + } else { + pr_info("%s : PN61_SET_SPI_PWR - reset failed \n", __func__); + p61_access_unlock(pn544_dev); + return -EBUSY; /* Device or resource busy */ + } + }else if (arg == 3) { + int ret = ese_cold_reset(ESE_COLD_RESET_SOURCE_NFC); + p61_access_unlock(pn544_dev); + return ret; + }else if (arg == 4) { + if (current_state & P61_STATE_SPI_PRIO) + { + pr_info("%s : PN61_SET_SPI_PWR - Prio Session Ending...\n", __func__); + p61_update_access_state(pn544_dev, P61_STATE_SPI_PRIO, false); + /*after SPI prio timeout, the state is changing from SPI prio to SPI */ + p61_update_access_state(pn544_dev, P61_STATE_SPI, true); + if (current_state & P61_STATE_WIRED) + { + if(pn544_dev->nfc_service_pid){ + pr_info("nfc service pid %s ---- %ld", __func__, pn544_dev->nfc_service_pid); + signal_handler(P61_STATE_SPI_PRIO_END, pn544_dev->nfc_service_pid); + } + else{ + pr_info(" invalid nfc service pid....signalling failed%s ---- %ld", __func__, pn544_dev->nfc_service_pid); + } + } + } + else + { + pr_info("%s : PN61_SET_SPI_PWR - Prio Session End failed \n", __func__); + p61_access_unlock(pn544_dev); + return -EBADRQC; /* Device or resource busy */ + } + } else if(arg == 5){ + release_ese_lock(P61_STATE_SPI); + } else if (arg == 6) { + /*SPI Service called ISO-RST*/ + p61_access_state_t current_state = P61_STATE_INVALID; + p61_get_access_state(pn544_dev, ¤t_state); + if(current_state & P61_STATE_WIRED) { + p61_access_unlock(pn544_dev); + return -EPERM; /* Operation not permitted */ + } + if(current_state & P61_STATE_SPI) { + p61_update_access_state(pn544_dev, P61_STATE_SPI, false); + }else if(current_state & P61_STATE_SPI_PRIO) { + p61_update_access_state(pn544_dev, P61_STATE_SPI_PRIO, false); + } +#ifdef ISO_RST + gpio_set_value(pn544_dev->iso_rst_gpio, 0); + msleep(50); + gpio_set_value(pn544_dev->iso_rst_gpio, 1); + msleep(50); + pr_info("%s ISO RESET from SPI DONE\n", __func__); +#endif + } + else { + pr_info("%s bad ese pwr arg %lu\n", __func__, arg); + p61_access_unlock(pn544_dev); + return -EBADRQC; /* Invalid request code */ + } + } + break; + + case P61_GET_PWR_STATUS: + { + p61_access_state_t current_state = P61_STATE_INVALID; + p61_get_access_state(pn544_dev, ¤t_state); + pr_info("%s: P61_GET_PWR_STATUS = %x",__func__, current_state); + put_user(current_state, (int __user *)arg); + } + break; + + case PN544_SET_DWNLD_STATUS: + { + long ret; + ret = set_jcop_download_state(arg); + if(ret < 0) + { + p61_access_unlock(pn544_dev); + return ret; + } + } + break; + + case P61_SET_WIRED_ACCESS: + { + p61_access_state_t current_state = P61_STATE_INVALID; + p61_get_access_state(pn544_dev, ¤t_state); + if (arg == 1) + { + if (current_state) + { + pr_info("%s : P61_SET_WIRED_ACCESS - enabling\n", __func__); + p61_update_access_state(pn544_dev, P61_STATE_WIRED, true); + if (current_state & P61_STATE_SPI_PRIO) + { + if(pn544_dev->nfc_service_pid){ + pr_info("nfc service pid %s ---- %ld", __func__, pn544_dev->nfc_service_pid); + signal_handler(P61_STATE_SPI_PRIO, pn544_dev->nfc_service_pid); + } + else{ + pr_info(" invalid nfc service pid....signalling failed%s ---- %ld", __func__, pn544_dev->nfc_service_pid); + } + } + if((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0 && (pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME)) + gpio_set_value(pn544_dev->ese_pwr_gpio, 1); + } else { + pr_info("%s : P61_SET_WIRED_ACCESS - enabling failed \n", __func__); + p61_access_unlock(pn544_dev); + return -EBUSY; /* Device or resource busy */ + } + } else if (arg == 0) { + pr_info("%s : P61_SET_WIRED_ACCESS - disabling \n", __func__); + if (current_state & P61_STATE_WIRED){ + p61_update_access_state(pn544_dev, P61_STATE_WIRED, false); + if((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0 && (pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME)) + { + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START); + gpio_set_value(pn544_dev->ese_pwr_gpio, 0); + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END); + } + } else { + pr_err("%s : P61_SET_WIRED_ACCESS - failed, current_state = %x \n", + __func__, pn544_dev->p61_current_state); + p61_access_unlock(pn544_dev); + return -EPERM; /* Operation not permitted */ + } + } + else if(arg == 2) + { + pr_info("%s : P61_ESE_GPIO_LOW \n", __func__); + if(pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME) + { + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_START); + gpio_set_value(pn544_dev->ese_pwr_gpio, 0); + svdd_sync_onoff(pn544_dev->nfc_service_pid, P61_STATE_SPI_SVDD_SYNC_END); + } + } + else if(arg == 3) + { + pr_info("%s : P61_ESE_GPIO_HIGH \n", __func__); + if(pn544_dev->chip_pwr_scheme == PN67T_PWR_SCHEME) + gpio_set_value(pn544_dev->ese_pwr_gpio, 1); + } + else if(arg == 4) + { + release_ese_lock(P61_STATE_WIRED); + } + else { + pr_info("%s P61_SET_WIRED_ACCESS - bad arg %lu\n", __func__, arg); + p61_access_unlock(pn544_dev); + return -EBADRQC; /* Invalid request code */ + } + } + break; + case P544_SET_POWER_SCHEME: + { + if(arg == PN67T_PWR_SCHEME) + { + pn544_dev->chip_pwr_scheme = PN67T_PWR_SCHEME; + pr_info("%s : The power scheme is set to PN67T legacy \n", __func__); + } + else if(arg == PN80T_LEGACY_PWR_SCHEME) + { + pn544_dev->chip_pwr_scheme = PN80T_LEGACY_PWR_SCHEME; + pr_info("%s : The power scheme is set to PN80T_LEGACY_PWR_SCHEME,\n", __func__); + } + else if(arg == PN80T_EXT_PMU_SCHEME) + { + pn544_dev->chip_pwr_scheme = PN80T_EXT_PMU_SCHEME; + pr_info("%s : The power scheme is set to PN80T_EXT_PMU_SCHEME,\n", __func__); + } + else + { + pr_info("%s : The power scheme is invalid,\n", __func__); + } + } + break; + case P544_SECURE_TIMER_SESSION: + { + secure_timer_operation(pn544_dev, arg); + } + break; + default: + pr_err("%s bad ioctl %u\n", __func__, cmd); + p61_access_unlock(pn544_dev); + return -EINVAL; + } + p61_access_unlock(pn544_dev); + return 0; +} +EXPORT_SYMBOL(pn544_dev_ioctl); + +static void secure_timer_workqueue(struct work_struct *Wq) +{ + p61_access_state_t current_state = P61_STATE_INVALID; + printk( KERN_INFO "secure_timer_callback: called (%lu).\n", jiffies); + /* Locking the critical section: ESE_PWR_OFF to allow eSE to shutdown peacefully :: START */ + get_ese_lock(P61_STATE_WIRED, MAX_ESE_ACCESS_TIME_OUT_MS); + p61_update_access_state(pn544_dev, P61_STATE_SECURE_MODE, false); + p61_get_access_state(pn544_dev, ¤t_state); + + if((current_state & (P61_STATE_SPI|P61_STATE_SPI_PRIO)) == 0) + { + printk( KERN_INFO "secure_timer_callback: make se_pwer_gpio low, state = %d", current_state); + gpio_set_value(pn544_dev->ese_pwr_gpio, 0); + /* Delay (2.5ms) after SVDD_PWR_OFF for the shutdown settlement time */ + usleep_range(2500, 3000); + #ifndef VEN_ALWAYS_ON + if(pn544_dev->nfc_service_pid == 0x00) + { + gpio_set_value(pn544_dev->ven_gpio, 0); + printk( KERN_INFO "secure_timer_callback :make ven_gpio low, state = %d", current_state); + } + #endif + } + pn544_dev->secure_timer_cnt = 0; + /* Locking the critical section: ESE_PWR_OFF to allow eSE to shutdown peacefully :: END */ + release_ese_lock(P61_STATE_WIRED); + return; +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) +static void secure_timer_callback( unsigned long data ) +{ +(void)data; +#else +static void secure_timer_callback(struct timer_list *unused) +{ +#endif + /* Flush and push the timer callback event to the bottom half(work queue) + to be executed later, at a safer time */ + flush_workqueue(pn544_dev->pSecureTimerCbWq); + queue_work(pn544_dev->pSecureTimerCbWq, &pn544_dev->wq_task); + return; +} + +static long start_seccure_timer(unsigned long timer_value) +{ + long ret = -EINVAL; + pr_info("start_seccure_timer: enter\n"); + /* Delete the timer if timer pending */ + if(timer_pending(&secure_timer) == 1) + { + pr_info("start_seccure_timer: delete pending timer \n"); + /* delete timer if already pending */ + del_timer(&secure_timer); + } + /* Start the timer if timer value is non-zero */ + if(timer_value) + { +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) + init_timer(&secure_timer); + setup_timer( &secure_timer, secure_timer_callback, 0 ); +#else + timer_setup(&secure_timer, secure_timer_callback, 0); +#endif + pr_info("start_seccure_timer: timeout %lums (%lu)\n",timer_value, jiffies ); + ret = mod_timer( &secure_timer, jiffies + msecs_to_jiffies(timer_value)); + if (ret) + pr_info("start_seccure_timer: Error in mod_timer\n"); + } + return ret; +} + +static long secure_timer_operation(struct pn544_dev *pn544_dev, unsigned long arg) +{ + long ret = -EINVAL; + unsigned long timer_value = arg; + + printk( KERN_INFO "secure_timer_operation, %d\n",pn544_dev->chip_pwr_scheme); + if(pn544_dev->chip_pwr_scheme == PN80T_LEGACY_PWR_SCHEME) + { + ret = start_seccure_timer(timer_value); + if(!ret) + { + pn544_dev->secure_timer_cnt = 1; + p61_update_access_state(pn544_dev, P61_STATE_SECURE_MODE, true); + } + else + { + pn544_dev->secure_timer_cnt = 0; + p61_update_access_state(pn544_dev, P61_STATE_SECURE_MODE, false); + pr_info("%s :Secure timer reset \n", __func__); + } + } + else + { + pr_info("%s :Secure timer session not applicable \n", __func__); + } + return ret; +} + +static long set_jcop_download_state(unsigned long arg) +{ + p61_access_state_t current_state = P61_STATE_INVALID; + long ret = 0; + p61_get_access_state(pn544_dev, ¤t_state); + pr_info("%s:Enter PN544_SET_DWNLD_STATUS:JCOP Dwnld state arg = %ld",__func__, arg); + if(arg == JCP_DWNLD_INIT) + { + if(pn544_dev->nfc_service_pid) + { + pr_info("nfc service pid %s ---- %ld", __func__, pn544_dev->nfc_service_pid); + signal_handler(JCP_DWNLD_INIT, pn544_dev->nfc_service_pid); + } + else + { + if (current_state & P61_STATE_JCP_DWNLD) + { + ret = -EINVAL; + } + else + { + p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, true); + } + } + } + else if (arg == JCP_DWNLD_START) + { + if (current_state & P61_STATE_JCP_DWNLD) + { + ret = -EINVAL; + } + else + { + p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, true); + } + } + else if (arg == JCP_SPI_DWNLD_COMPLETE) + { + if(pn544_dev->nfc_service_pid) + { + signal_handler(JCP_DWP_DWNLD_COMPLETE, pn544_dev->nfc_service_pid); + } + p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, false); + } + else if (arg == JCP_DWP_DWNLD_COMPLETE) + { + p61_update_access_state(pn544_dev, P61_STATE_JCP_DWNLD, false); + } + else + { + pr_info("%s bad ese pwr arg %lu\n", __func__, arg); + p61_access_unlock(pn544_dev); + return -EBADRQC; /* Invalid request code */ + } + pr_info("%s: PN544_SET_DWNLD_STATUS = %x",__func__, current_state); + + return ret; +} + +int get_ese_lock(p61_access_state_t p61_current_state, int timeout) +{ + unsigned long tempJ = msecs_to_jiffies(timeout); + if(down_timeout(&ese_access_sema, tempJ) != 0) + { + printk("get_ese_lock: timeout p61_current_state = %d\n", p61_current_state); + return -EBUSY; + } + return 0; +} +EXPORT_SYMBOL(get_ese_lock); + +static void release_ese_lock(p61_access_state_t p61_current_state) +{ + up(&ese_access_sema); +} + + +static const struct file_operations pn544_dev_fops = { + .owner = THIS_MODULE, + .llseek = no_llseek, + .read = pn544_dev_read, + .write = pn544_dev_write, + .open = pn544_dev_open, + .release = pn544_dev_release, + .unlocked_ioctl = pn544_dev_ioctl, +}; +#if DRAGON_NFC +static int pn544_parse_dt(struct device *dev, + struct pn544_i2c_platform_data *data) +{ + struct device_node *np = dev->of_node; + int errorno = 0; + +#if !NEXUS5x + data->irq_gpio = of_get_named_gpio(np, "nxp,pn544-irq", 0); + if ((!gpio_is_valid(data->irq_gpio))) + return -EINVAL; + + data->ven_gpio = of_get_named_gpio(np, "nxp,pn544-ven", 0); + if ((!gpio_is_valid(data->ven_gpio))) + return -EINVAL; + + data->firm_gpio = of_get_named_gpio(np, "nxp,pn544-fw-dwnld", 0); + if ((!gpio_is_valid(data->firm_gpio))) + return -EINVAL; + + data->ese_pwr_gpio = of_get_named_gpio(np, "nxp,pn544-ese-pwr", 0); + if ((!gpio_is_valid(data->ese_pwr_gpio))) + return -EINVAL; + data->iso_rst_gpio = of_get_named_gpio(np, "nxp,pn544-iso-pwr-rst", 0); + if ((!gpio_is_valid(data->iso_rst_gpio))) + return -EINVAL; +#else + data->ven_gpio = of_get_named_gpio_flags(np, + "nxp,gpio_ven", 0, NULL); + data->firm_gpio = of_get_named_gpio_flags(np, + "nxp,gpio_mode", 0, NULL); + data->irq_gpio = of_get_named_gpio_flags(np, + "nxp,gpio_irq", 0, NULL); +#endif + pr_info("%s: %d, %d, %d, %d, %d error:%d\n", __func__, + data->irq_gpio, data->ven_gpio, data->firm_gpio, data->iso_rst_gpio, + data->ese_pwr_gpio, errorno); + + return errorno; +} +#endif + +static int pn544_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + int ret; + struct pn544_i2c_platform_data *platform_data; + //struct pn544_dev *pn544_dev; + +#if !DRAGON_NFC + platform_data = client->dev.platform_data; +#else + struct device_node *node = client->dev.of_node; + + if (node) { + platform_data = devm_kzalloc(&client->dev, + sizeof(struct pn544_i2c_platform_data), GFP_KERNEL); + if (!platform_data) { + dev_err(&client->dev, + "nfc-nci probe: Failed to allocate memory\n"); + return -ENOMEM; + } + ret = pn544_parse_dt(&client->dev, platform_data); + if (ret) + { + pr_info("%s pn544_parse_dt failed", __func__); + } + client->irq = gpio_to_irq(platform_data->irq_gpio); + if (client->irq < 0) + { + pr_info("%s gpio to irq failed", __func__); + } + } else { + platform_data = client->dev.platform_data; + } +#endif + if (platform_data == NULL) { + pr_err("%s : nfc probe fail\n", __func__); + return -ENODEV; + } + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { + pr_err("%s : need I2C_FUNC_I2C\n", __func__); + return -ENODEV; + } +#if !DRAGON_NFC + ret = gpio_request(platform_data->irq_gpio, "nfc_int"); + if (ret) + return -ENODEV; + ret = gpio_request(platform_data->ven_gpio, "nfc_ven"); + if (ret) + goto err_ven; + ret = gpio_request(platform_data->ese_pwr_gpio, "nfc_ese_pwr"); + if (ret) + goto err_ese_pwr; + if (platform_data->firm_gpio) { + ret = gpio_request(platform_data->firm_gpio, "nfc_firm"); + if (ret) + goto err_firm; + } +#ifdef ISO_RST + if(platform_data->iso_rst_gpio) { + ret = gpio_request(platform_data->iso_rst_gpio, "nfc_iso_rst"); + if (ret) + goto err_iso_rst; + } +#endif +#endif + pn544_dev = kzalloc(sizeof(*pn544_dev), GFP_KERNEL); + if (pn544_dev == NULL) { + dev_err(&client->dev, + "failed to allocate memory for module data\n"); + ret = -ENOMEM; + goto err_exit; + } + + pn544_dev->irq_gpio = platform_data->irq_gpio; + pn544_dev->ven_gpio = platform_data->ven_gpio; + pn544_dev->firm_gpio = platform_data->firm_gpio; + pn544_dev->ese_pwr_gpio = platform_data->ese_pwr_gpio; +#ifdef ISO_RST + pn544_dev->iso_rst_gpio = platform_data->iso_rst_gpio; +#endif + pn544_dev->p61_current_state = P61_STATE_IDLE; + pn544_dev->nfc_ven_enabled = false; + pn544_dev->spi_ven_enabled = false; + pn544_dev->chip_pwr_scheme = PN67T_PWR_SCHEME; + pn544_dev->client = client; + pn544_dev->secure_timer_cnt = 0; + + pn544_dev->state_flags = 0x00; + ret = gpio_direction_input(pn544_dev->irq_gpio); + if (ret < 0) { + pr_err("%s :not able to set irq_gpio as input\n", __func__); + goto err_ven; + } + #ifndef VEN_ALWAYS_ON + ret = gpio_direction_output(pn544_dev->ven_gpio, 0); + if (ret < 0) { + pr_err("%s : not able to set ven_gpio as output\n", __func__); + goto err_firm; + } + #else + ret = gpio_direction_output(pn544_dev->ven_gpio, 1); + if (ret < 0) { + pr_err("%s : not able to set ven_gpio as output\n", __func__); + goto err_firm; + } + #endif + ret = gpio_direction_output(pn544_dev->ese_pwr_gpio, 0); + if (ret < 0) { + pr_err("%s : not able to set ese_pwr gpio as output\n", __func__); + goto err_ese_pwr; + } + if (platform_data->firm_gpio) { + ret = gpio_direction_output(pn544_dev->firm_gpio, 0); + if (ret < 0) { + pr_err("%s : not able to set firm_gpio as output\n", + __func__); + goto err_exit; + } + } +#ifdef ISO_RST + ret = gpio_direction_output(pn544_dev->iso_rst_gpio, 0); + if (ret < 0) { + pr_err("%s : not able to set iso rst gpio as output\n", __func__); + goto err_iso_rst; + } +#endif + /* init mutex and queues */ + ese_reset_resource_init(); + init_waitqueue_head(&pn544_dev->read_wq); + mutex_init(&pn544_dev->read_mutex); + sema_init(&ese_access_sema, 1); + mutex_init(&pn544_dev->p61_state_mutex); + spin_lock_init(&pn544_dev->irq_enabled_lock); + pn544_dev->pSecureTimerCbWq = create_workqueue(SECURE_TIMER_WORK_QUEUE); + INIT_WORK(&pn544_dev->wq_task, secure_timer_workqueue); + pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR; + pn544_dev->pn544_device.name = "pn553"; + pn544_dev->pn544_device.fops = &pn544_dev_fops; + + ret = misc_register(&pn544_dev->pn544_device); + if (ret) { + pr_err("%s : misc_register failed\n", __FILE__); + goto err_misc_register; + } + /* HiKey Compilation fix */ + #ifndef HiKey_620_COMPILATION_FIX + wake_lock_init(&nfc_wake_lock, WAKE_LOCK_SUSPEND, "NFCWAKE"); + #endif +#ifdef ISO_RST + /* Setting ISO RESET pin high to power ESE during init */ + gpio_set_value(pn544_dev->iso_rst_gpio, 1); +#endif + /* request irq. the irq is set whenever the chip has data available + * for reading. it is cleared when all data has been read. + */ + pr_info("%s : requesting IRQ %d\n", __func__, client->irq); + pn544_dev->irq_enabled = true; + ret = request_irq(client->irq, pn544_dev_irq_handler, + IRQF_TRIGGER_HIGH, client->name, pn544_dev); + if (ret) { + dev_err(&client->dev, "request_irq failed\n"); + goto err_request_irq_failed; + } + enable_irq_wake(pn544_dev->client->irq); + pn544_disable_irq(pn544_dev); + i2c_set_clientdata(client, pn544_dev); +#ifdef VEN_ALWAYS_ON + msleep(5); /* VBAT--> VDDIO(HIGH) + Guardtime of min 5ms --> VEN(HIGH) */ + /* VEN toggle(reset) to proceed */ + gpio_set_value(pn544_dev->ven_gpio, 0); + msleep(5); + gpio_set_value(pn544_dev->ven_gpio, 1); +#endif + +#if HWINFO + /* + * This function is used only if + * hardware info is required during probe*/ + check_hw_info(); +#endif + + return 0; + + err_request_irq_failed: + misc_deregister(&pn544_dev->pn544_device); + err_misc_register: + ese_reset_resource_destroy(); + mutex_destroy(&pn544_dev->read_mutex); + mutex_destroy(&pn544_dev->p61_state_mutex); + kfree(pn544_dev); + err_exit: + if (pn544_dev->firm_gpio) + gpio_free(platform_data->firm_gpio); + err_firm: + gpio_free(platform_data->ese_pwr_gpio); + err_ese_pwr: + gpio_free(platform_data->ven_gpio); + err_ven: + gpio_free(platform_data->irq_gpio); +#ifdef ISO_RST + err_iso_rst: + gpio_free(platform_data->iso_rst_gpio); +#endif + return ret; +} + +static int pn544_remove(struct i2c_client *client) +{ + struct pn544_dev *pn544_dev; + + pn544_dev = i2c_get_clientdata(client); + free_irq(client->irq, pn544_dev); + misc_deregister(&pn544_dev->pn544_device); + mutex_destroy(&pn544_dev->read_mutex); + mutex_destroy(&pn544_dev->p61_state_mutex); + gpio_free(pn544_dev->irq_gpio); + gpio_free(pn544_dev->ven_gpio); + gpio_free(pn544_dev->ese_pwr_gpio); + destroy_workqueue(pn544_dev->pSecureTimerCbWq); +#ifdef ISO_RST + gpio_free(pn544_dev->iso_rst_gpio); +#endif + pn544_dev->p61_current_state = P61_STATE_INVALID; + pn544_dev->nfc_ven_enabled = false; + pn544_dev->spi_ven_enabled = false; + ese_reset_resource_destroy(); + + if (pn544_dev->firm_gpio) + gpio_free(pn544_dev->firm_gpio); + kfree(pn544_dev); + + return 0; +} + +static const struct i2c_device_id pn544_id[] = { +#if NEXUS5x + { "pn548", 0 }, +#else + { "pn544", 0 }, +#endif + { } +}; +#if DRAGON_NFC +static struct of_device_id pn544_i2c_dt_match[] = { + { +#if NEXUS5x + .compatible = "nxp,pn548", +#else + .compatible = "nxp,pn544", +#endif + }, + {} +}; +#endif +static struct i2c_driver pn544_driver = { + .id_table = pn544_id, + .probe = pn544_probe, + .remove = pn544_remove, + .driver = { + .owner = THIS_MODULE, +#if NEXUS5x + .name = "pn548", +#else + .name = "pn544", +#endif +#if DRAGON_NFC + .of_match_table = pn544_i2c_dt_match, +#endif + }, +}; +#if HWINFO +/****************************************************************************** + * Function check_hw_info + * + * Description This function is called during pn544_probe to retrieve + * HW info. + * Useful get HW information in case of previous FW download is + * interrupted and core reset is not allowed. + * This function checks if core reset is allowed, if not + * sets DWNLD_REQ(firm_gpio) , ven reset and sends firmware + * get version command. + * In response HW information will be received. + * + * Returns None + * + ******************************************************************************/ +static void check_hw_info() { + char read_data[20]; + int ret, get_version_len = 8, retry_count = 0; + static uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00}; + char get_version_cmd[] = + {0x00, 0x04, 0xF1, 0x00, 0x00, 0x00, 0x6E, 0xEF}; + + pr_info("%s :Enter\n", __func__); + + /* + * Ven Reset before sending core Reset + * This is to check core reset is allowed or not. + * If not allowed then previous FW download is interrupted in between + * */ + pr_info("%s :Ven Reset \n", __func__); + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 0); + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + ret = i2c_master_send(pn544_dev->client, cmd_reset_nci, 4); + + if (ret == 4) { + pr_info("%s : core reset write success\n", __func__); + } else { + + /* + * Core reset failed. + * set the DWNLD_REQ , do ven reset + * send firmware download info command + * */ + pr_err("%s : write failed\n", __func__); + pr_info("%s power on with firmware\n", __func__); + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + if (pn544_dev->firm_gpio) { + p61_update_access_state(pn544_dev, P61_STATE_DWNLD, true); + gpio_set_value(pn544_dev->firm_gpio, 1); + } + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 0); + msleep(10); + gpio_set_value(pn544_dev->ven_gpio, 1); + msleep(10); + ret = i2c_master_send(pn544_dev->client, get_version_cmd, get_version_len); + if (ret != get_version_len) { + ret = -EIO; + pr_err("%s : write_failed \n", __func__); + } + else { + pr_info("%s :data sent\n", __func__); + } + + ret = 0; + + while (retry_count < 10) { + + /* + * Wait for read interrupt + * If spurious interrupt is received retry again + * */ + pn544_dev->irq_enabled = true; + enable_irq(pn544_dev->client->irq); + enable_irq_wake(pn544_dev->client->irq); + ret = wait_event_interruptible( + pn544_dev->read_wq, + !pn544_dev->irq_enabled); + + pn544_disable_irq(pn544_dev); + + if (gpio_get_value(pn544_dev->irq_gpio)) + break; + + pr_warning("%s: spurious interrupt detected\n", __func__); + retry_count ++; + } + + if(ret) { + return; + } + + /* + * Read response data and copy into hw_type_info + * */ + ret = i2c_master_recv(pn544_dev->client, read_data, 14); + + if(ret) { + memcpy(hw_info.data, read_data, ret); + hw_info.len = ret; + pr_info("%s :data received len : %d\n", __func__,hw_info.len); + } + else { + pr_err("%s :Read Failed\n", __func__); + } + } +} +#endif +/* + * module load/unload record keeping + */ + +static int __init pn544_dev_init(void) +{ + pr_info("Loading pn544 driver\n"); + return i2c_add_driver(&pn544_driver); +} +module_init(pn544_dev_init); + +static void __exit pn544_dev_exit(void) +{ + pr_info("Unloading pn544 driver\n"); + i2c_del_driver(&pn544_driver); +} +module_exit(pn544_dev_exit); + +MODULE_AUTHOR("Sylvain Fonteneau"); +MODULE_DESCRIPTION("NFC PN544 driver"); +MODULE_LICENSE("GPL"); diff --git a/pn553-i2c/pn553.h b/pn553-i2c/pn553.h new file mode 100644 index 0000000000..d196474f3e --- /dev/null +++ b/pn553-i2c/pn553.h @@ -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 + +#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