common.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /******************************************************************************
  2. * Copyright (C) 2019-2021 NXP
  3. * *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. ******************************************************************************/
  19. #ifndef _COMMON_H_
  20. #define _COMMON_H_
  21. #include <linux/types.h>
  22. #include <linux/version.h>
  23. #include <linux/semaphore.h>
  24. #include <linux/completion.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/cdev.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/gpio.h>
  29. #include "i2c_drv.h"
  30. #define DEV_COUNT 1 /* Max device count for this driver */
  31. #define CLASS_NAME "nfc" /* i2c device class */
  32. // NFC character device name, this will be in /dev/
  33. #define NFC_CHAR_DEV_NAME "pn553"
  34. // NCI packet details
  35. #define NCI_MSG_CMD 0x20
  36. #define NCI_MSG_RSP 0x40
  37. #define NCI_HDR_LEN 3
  38. #define NCI_PAYLOAD_IDX 3
  39. #define NCI_PAYLOAD_LEN_IDX 2
  40. /*Time to wait for first NCI rest response*/
  41. #define NCI_RESET_RESP_READ_DELAY (10000) // 10ms
  42. #define NCI_RESET_RESP_TIMEOUT (500) // 500ms
  43. // FW DNLD packet details
  44. #define FW_MSG_CMD_RSP 0x00
  45. #define FW_HDR_LEN 2
  46. #define FW_PAYLOAD_LEN_IDX 1
  47. #define FW_CRC_LEN 2
  48. #define FW_MIN_PAYLOAD_LEN 4
  49. #define MIN_NFC_DL_FRAME_SIZE 3
  50. #define NCI_RESET_CMD_LEN (4)
  51. #define NCI_RESET_RSP_LEN (4)
  52. #define NCI_RESET_NTF_LEN (13)
  53. #define DL_GET_VERSION_CMD_LEN (8)
  54. #define DL_GET_VERSION_RSP_LEN_1 (12)
  55. #define DL_GET_VERSION_RSP_LEN_2 (20)
  56. #define DL_RESET_CMD_LEN (8)
  57. #define DL_GET_SESSION_STATE_CMD_LEN (8)
  58. #define DL_GET_SESSION_STATE_RSP_LEN (8)
  59. #define GET_SESSION_STS_OFF (3)
  60. #define NFCC_SESSION_STS_CLOSED (0x0)
  61. #define MAX_NCI_PAYLOAD_LEN (255)
  62. #define MAX_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
  63. #define MAX_DL_PAYLOAD_LEN (550)
  64. #define MAX_DL_BUFFER_SIZE (FW_HDR_LEN + FW_CRC_LEN + MAX_DL_PAYLOAD_LEN)
  65. // Maximum retry count for standby writes
  66. #define MAX_RETRY_COUNT (3)
  67. // Retry count for normal write
  68. #define NO_RETRY (1)
  69. #define MAX_IRQ_WAIT_TIME (90)
  70. #define WAKEUP_SRC_TIMEOUT (2000)
  71. #define NCI_MAX_CMD_RSP_TIMEOUT (5000) //5s
  72. /*command response timeout*/
  73. #define NCI_CMD_RSP_TIMEOUT (2000) //2s
  74. /*Time to wait for NFCC to be ready again after any change in the GPIO*/
  75. #define NFC_GPIO_SET_WAIT_TIME_USEC (10000)
  76. /*Time to wait after soft reset via any NCI/DL cmd*/
  77. #define NFC_SOFT_RESET_WAIT_TIME_USEC (5000)
  78. /*Time to wait before retrying i2c writes*/
  79. #define WRITE_RETRY_WAIT_TIME_USEC (1000)
  80. /*Time to wait before retrying read for some specific usecases*/
  81. #define READ_RETRY_WAIT_TIME_USEC (3500)
  82. #define NFC_MAGIC 0xE9
  83. /*Ioctls*/
  84. // The type should be aligned with MW HAL definitions
  85. #define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, long)
  86. #define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, long)
  87. #define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, long)
  88. #define NFC_GET_PLATFORM_TYPE _IO(NFC_MAGIC, 0x04)
  89. #define NFC_GET_NFC_STATE _IO(NFC_MAGIC, 0x05)
  90. /* NFC HAL can call this ioctl to get the current IRQ state */
  91. #define NFC_GET_IRQ_STATE _IO(NFC_MAGIC, 0x06)
  92. #define DTS_IRQ_GPIO_STR "nxp,pn544-irq"
  93. #define DTS_VEN_GPIO_STR "nxp,pn544-ven"
  94. #define DTS_FWDN_GPIO_STR "nxp,pn544-fw-dwnld"
  95. enum nfcc_ioctl_request {
  96. /* NFC disable request with VEN LOW */
  97. NFC_POWER_OFF = 0,
  98. /* NFC enable request with VEN Toggle */
  99. NFC_POWER_ON,
  100. /* firmware download request with VEN Toggle */
  101. NFC_FW_DWL_VEN_TOGGLE,
  102. /* ISO reset request */
  103. NFC_ISO_RESET,
  104. /* request for firmware download gpio HIGH */
  105. NFC_FW_DWL_HIGH,
  106. /* VEN hard reset request */
  107. NFC_VEN_FORCED_HARD_RESET,
  108. /* request for firmware download gpio LOW */
  109. NFC_FW_DWL_LOW,
  110. };
  111. /*nfc platform interface type*/
  112. enum interface_flags {
  113. /*I2C physical IF for NFCC */
  114. PLATFORM_IF_I2C = 0,
  115. };
  116. /*nfc state flags*/
  117. enum nfc_state_flags {
  118. /*nfc in unknown state */
  119. NFC_STATE_UNKNOWN = 0,
  120. /*nfc in download mode */
  121. NFC_STATE_FW_DWL = 0x1,
  122. /*nfc booted in NCI mode */
  123. NFC_STATE_NCI = 0x2,
  124. /*nfc booted in Fw teared mode */
  125. NFC_STATE_FW_TEARED = 0x4,
  126. };
  127. /*
  128. * Power state for IBI handing, mainly needed to defer the IBI handling
  129. * for the IBI received in suspend state to do it later in resume call
  130. */
  131. enum pm_state_flags {
  132. PM_STATE_NORMAL = 0,
  133. PM_STATE_SUSPEND,
  134. PM_STATE_IBI_BEFORE_RESUME,
  135. };
  136. /* Enum for GPIO values*/
  137. enum gpio_values {
  138. GPIO_INPUT = 0x0,
  139. GPIO_OUTPUT = 0x1,
  140. GPIO_HIGH = 0x2,
  141. GPIO_OUTPUT_HIGH = 0x3,
  142. GPIO_IRQ = 0x4,
  143. };
  144. // NFC GPIO variables
  145. typedef struct platform_gpio {
  146. unsigned int irq;
  147. unsigned int ven;
  148. unsigned int dwl_req;
  149. } platform_gpio_t;
  150. // NFC Struct to get all the required configs from DTS*/
  151. typedef struct platform_configs {
  152. platform_gpio_t gpio;
  153. } platform_configs_t;
  154. //cold reset Features specific Parameters
  155. typedef struct cold_reset {
  156. bool rsp_pending; /*cmd rsp pending status */
  157. bool in_progress; /*for cold reset when gurad timer in progress */
  158. bool reset_protection; /*reset protection enabled/disabled */
  159. uint8_t status; /*status from response buffer */
  160. uint8_t rst_prot_src; /*reset protection source (SPI, NFC) */
  161. struct mutex sync_mutex;
  162. struct timer_list timer;
  163. wait_queue_head_t read_wq;
  164. } cold_reset_t;
  165. /* Device specific structure */
  166. typedef struct nfc_dev {
  167. wait_queue_head_t read_wq;
  168. struct mutex read_mutex;
  169. struct mutex ese_access_mutex;
  170. struct mutex dev_ref_mutex;
  171. unsigned int dev_ref_count;
  172. struct class *nfc_class;
  173. struct device *nfc_device;
  174. struct cdev c_dev;
  175. dev_t devno;
  176. /* Interface flag */
  177. uint8_t interface;
  178. /* nfc state flags */
  179. uint8_t nfc_state;
  180. /* NFC VEN pin state */
  181. bool nfc_ven_enabled;
  182. #if defined(RECOVERY_ENABLE)
  183. /* current firmware major version */
  184. uint8_t fw_major_version;
  185. /* NFC recovery Required */
  186. bool recovery_required;
  187. #endif
  188. union {
  189. i2c_dev_t i2c_dev;
  190. };
  191. platform_configs_t configs;
  192. cold_reset_t cold_reset;
  193. /*funtion pointers for the common i2c functionality */
  194. int (*nfc_read) (struct nfc_dev *dev, char *buf, size_t count, int timeout);
  195. int (*nfc_write) (struct nfc_dev *dev, const char *buf, const size_t count,
  196. int max_retry_cnt);
  197. int (*nfc_enable_intr) (struct nfc_dev *dev);
  198. int (*nfc_disable_intr) (struct nfc_dev *dev);
  199. int (*nfc_flush_readq) (struct nfc_dev *dev);
  200. } nfc_dev_t;
  201. int nfc_dev_open(struct inode *inode, struct file *filp);
  202. int nfc_dev_close(struct inode *inode, struct file *filp);
  203. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
  204. int nfc_parse_dt(struct device *dev, platform_configs_t *nfc_configs,
  205. uint8_t interface);
  206. int nfc_misc_register(nfc_dev_t *nfc_dev,
  207. const struct file_operations *nfc_fops, int count, char *devname,
  208. char *classname);
  209. void nfc_misc_unregister(nfc_dev_t *nfc_dev, int count);
  210. int configure_gpio(unsigned int gpio, int flag);
  211. void gpio_set_ven(nfc_dev_t *nfc_dev, int value);
  212. void gpio_free_all(nfc_dev_t *nfc_dev);
  213. int validate_nfc_state_nci(nfc_dev_t *nfc_dev);
  214. int nfcc_hw_check(nfc_dev_t *nfc_dev);
  215. void set_nfcc_state_from_rsp(nfc_dev_t *dev, const char *buf,
  216. const int count);
  217. void enable_dwnld_mode(nfc_dev_t* nfc_dev, bool value);
  218. #endif //_COMMON_H_