common.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2022 NXP
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. ******************************************************************************/
  20. /*
  21. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  22. *
  23. *****************************************************************************/
  24. #ifndef _COMMON_H_
  25. #define _COMMON_H_
  26. #include <linux/cdev.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/delay.h>
  29. #include <linux/ipc_logging.h>
  30. #include <linux/clk.h>
  31. #include <nfcinfo.h>
  32. #include "i2c_drv.h"
  33. #include "ese_cold_reset.h"
  34. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  35. /*secure library headers*/
  36. #include "smcinvoke.h"
  37. #include "smcinvoke_object.h"
  38. #include "IClientEnv.h"
  39. #endif
  40. /* Max device count for this driver */
  41. #define DEV_COUNT 1
  42. /* i2c device class */
  43. #define CLASS_NAME "qti-nfc"
  44. /* NFC character device name, this will be in /dev/ */
  45. #define NFC_CHAR_DEV_NAME "nq-nci"
  46. /* NCI packet details */
  47. #define NCI_CMD (0x20)
  48. #define NCI_RSP (0x40)
  49. #define NCI_NTF (0x60)
  50. #define NCI_HDR_LEN (3)
  51. #define NCI_HDR_IDX (0)
  52. #define DL_CMD 0x00
  53. #define DL_PAYLOAD_BYTE_ZERO 0x00
  54. #define NCI_HDR_OID_IDX (1)
  55. #define NCI_PAYLOAD_IDX (3)
  56. #define NCI_PAYLOAD_LEN_IDX (2)
  57. /*Time to wait for first NCI rest response*/
  58. #define NCI_RESET_RESP_READ_DELAY (10000) // 10ms
  59. #define NCI_RESET_RESP_TIMEOUT (500) // 500ms
  60. // FW DNLD packet details
  61. #define FW_MSG_CMD_RSP 0x00
  62. #define DL_HDR_LEN (2)
  63. #define DL_CRC_LEN (2)
  64. #define NCI_RSP_PKT_TYPE (0x40)
  65. #define MAX_NCI_PAYLOAD_LEN (255)
  66. #define MAX_NCI_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
  67. /*
  68. * From MW 11.04 buffer size increased to support
  69. * frame size of 554 in FW download mode
  70. * Frame len(2) + Frame Header(6) + DATA(512) + HASH(32) + CRC(2) + RFU(4)
  71. */
  72. #define MAX_DL_PAYLOAD_LEN (550)
  73. #define MAX_DL_BUFFER_SIZE (DL_HDR_LEN + DL_CRC_LEN + \
  74. MAX_DL_PAYLOAD_LEN)
  75. /* Retry count for normal write */
  76. #define NO_RETRY (1)
  77. /* Maximum retry count for standby writes */
  78. #define MAX_RETRY_COUNT (3)
  79. #define MAX_WRITE_IRQ_COUNT (5)
  80. #define MAX_IRQ_WAIT_TIME (90)
  81. #define WAKEUP_SRC_TIMEOUT (100)
  82. /* command response timeout */
  83. #define NCI_CMD_RSP_TIMEOUT_MS (2000)
  84. /* Time to wait for NFCC to be ready again after any change in the GPIO */
  85. #define NFC_GPIO_SET_WAIT_TIME_US (10000)
  86. /* Time to wait before retrying writes */
  87. #define WRITE_RETRY_WAIT_TIME_US (3000)
  88. /* Time to wait before retrying read for some specific usecases */
  89. #define READ_RETRY_WAIT_TIME_US (3500)
  90. #define NFC_MAGIC (0xE9)
  91. // Ioctls
  92. // The type should be aligned with MW HAL definitions
  93. #define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, uint32_t)
  94. #define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, uint32_t)
  95. #define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, uint32_t)
  96. #define NFC_SET_RESET_READ_PENDING _IOW(NFC_MAGIC, 0x04, uint32_t)
  97. #define NFC_GET_GPIO_STATUS _IOR(NFC_MAGIC, 0x05, uint32_t)
  98. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  99. #define NFC_SECURE_ZONE _IOW(NFC_MAGIC, 0x0A, uint32_t)
  100. #endif
  101. #define DTS_IRQ_GPIO_STR "qcom,sn-irq"
  102. #define DTS_VEN_GPIO_STR "qcom,sn-ven"
  103. #define DTS_FWDN_GPIO_STR "qcom,sn-firm"
  104. #define DTS_CLKREQ_GPIO_STR "qcom,sn-clkreq"
  105. #define DTS_SZONE_STR "qcom,sn-szone"
  106. #define NFC_LDO_SUPPLY_DT_NAME "qcom,sn-vdd-1p8"
  107. #define NFC_LDO_SUPPLY_NAME "qcom,sn-vdd-1p8-supply"
  108. #define NFC_LDO_VOL_DT_NAME "qcom,sn-vdd-1p8-voltage"
  109. #define NFC_LDO_CUR_DT_NAME "qcom,sn-vdd-1p8-current"
  110. //as per SN1x0 datasheet
  111. #define NFC_VDDIO_MIN 1650000 //in uV
  112. #define NFC_VDDIO_MAX 1950000 //in uV
  113. #define NFC_CURRENT_MAX 157000 //in uA
  114. /* Each GPIO occupies consecutive two bits */
  115. #define GPIO_POS_SHIFT_VAL 2
  116. /* Two bits to indicate GPIO status (Invalid(-2), Set(1) or Reset(0)) */
  117. #define GPIO_STATUS_MASK_BITS 3
  118. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  119. //NFC ID for registration with secure libraries
  120. #define HW_STATE_UID 0x108
  121. #define HW_OP_GET_STATE 1
  122. #define HW_NFC_UID 0x506
  123. #define FEATURE_NOT_SUPPORTED 12
  124. #define PERIPHERAL_NOT_FOUND 10
  125. #endif
  126. #define NUM_OF_IPC_LOG_PAGES (2)
  127. #define PKT_MAX_LEN (4) // no of max bytes to print for cmd/resp
  128. #define GET_IPCLOG_MAX_PKT_LEN(c) ((c > PKT_MAX_LEN) ? PKT_MAX_LEN : c)
  129. #define NFCLOG_IPC(nfc_dev, log_to_dmesg, x...) \
  130. do { \
  131. ipc_log_string(nfc_dev->ipcl, x); \
  132. if (log_to_dmesg) { \
  133. if (nfc_dev->nfc_device) \
  134. dev_err((nfc_dev->nfc_device), x); \
  135. else \
  136. pr_err(x); \
  137. } \
  138. } while (0)
  139. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  140. static struct semaphore sem_eSE_pwr_off;
  141. static bool chk_eSE_pwr_off;
  142. #endif
  143. enum ese_ioctl_request {
  144. /* eSE POWER ON */
  145. ESE_POWER_ON = 0,
  146. /* eSE POWER OFF */
  147. ESE_POWER_OFF,
  148. /* eSE POWER STATE */
  149. ESE_POWER_STATE
  150. };
  151. enum nfcc_ioctl_request {
  152. /* NFC disable request with VEN LOW */
  153. NFC_POWER_OFF = 0,
  154. /* NFC enable request with VEN Toggle */
  155. NFC_POWER_ON,
  156. /* firmware download request with VEN Toggle */
  157. NFC_FW_DWL_VEN_TOGGLE,
  158. /* ISO reset request */
  159. NFC_ISO_RESET,
  160. /* request for firmware download gpio HIGH */
  161. NFC_FW_DWL_HIGH,
  162. /* VEN hard reset request */
  163. NFC_VEN_FORCED_HARD_RESET,
  164. /* request for firmware download gpio LOW */
  165. NFC_FW_DWL_LOW,
  166. /* NFC enable without VEN gpio modification */
  167. NFC_ENABLE,
  168. /* NFC disable without VEN gpio modification */
  169. NFC_DISABLE,
  170. };
  171. enum nfc_read_pending {
  172. NFC_RESET_READ_PENDING,
  173. NFC_SET_READ_PENDING,
  174. };
  175. /* nfc platform interface type */
  176. enum interface_flags {
  177. /* I2C physical IF for NFCC */
  178. PLATFORM_IF_I2C = 0,
  179. };
  180. /* nfc state flags */
  181. enum nfc_state_flags {
  182. /* nfc in unknown state */
  183. NFC_STATE_UNKNOWN = 0,
  184. /* nfc in download mode */
  185. NFC_STATE_FW_DWL = 0x1,
  186. /* nfc booted in NCI mode */
  187. NFC_STATE_NCI = 0x2,
  188. /* nfc booted in Fw teared mode */
  189. NFC_STATE_FW_TEARED = 0x4,
  190. };
  191. /*
  192. * Power state for IBI handing, mainly needed to defer the IBI handling
  193. * for the IBI received in suspend state to do it later in resume call
  194. */
  195. enum pm_state_flags {
  196. PM_STATE_NORMAL = 0,
  197. PM_STATE_SUSPEND,
  198. PM_STATE_IBI_BEFORE_RESUME,
  199. };
  200. /* Enum for GPIO values */
  201. enum gpio_values {
  202. GPIO_INPUT = 0x0,
  203. GPIO_OUTPUT = 0x1,
  204. GPIO_HIGH = 0x2,
  205. GPIO_OUTPUT_HIGH = 0x3,
  206. GPIO_IRQ = 0x4,
  207. };
  208. /* NFC GPIO variables */
  209. struct platform_gpio {
  210. unsigned int irq;
  211. unsigned int ven;
  212. unsigned int clkreq;
  213. unsigned int dwl_req;
  214. };
  215. // NFC LDO entries from DT
  216. struct platform_ldo {
  217. int vdd_levels[2];
  218. int max_current;
  219. };
  220. /* NFC Struct to get all the required configs from DTS */
  221. struct platform_configs {
  222. struct platform_gpio gpio;
  223. struct platform_ldo ldo;
  224. const char *szone;
  225. #ifdef NFC_SECURE_PERIPHERAL_ENABLED
  226. bool CNSS_NFC_HW_SECURE_ENABLE;
  227. #endif
  228. };
  229. /* Device specific structure */
  230. struct nfc_dev {
  231. wait_queue_head_t read_wq;
  232. struct mutex read_mutex;
  233. struct mutex write_mutex;
  234. uint8_t *read_kbuf;
  235. uint8_t *write_kbuf;
  236. struct mutex dev_ref_mutex;
  237. unsigned int dev_ref_count;
  238. struct class *nfc_class;
  239. struct device *nfc_device;
  240. struct cdev c_dev;
  241. dev_t devno;
  242. /* Interface flag */
  243. uint8_t interface;
  244. /* nfc state flags */
  245. uint8_t nfc_state;
  246. /* NFC VEN pin state */
  247. bool nfc_ven_enabled;
  248. /* current firmware major version */
  249. uint8_t fw_major_version;
  250. bool is_vreg_enabled;
  251. bool is_ese_session_active;
  252. bool release_read;
  253. union {
  254. struct i2c_dev i2c_dev;
  255. };
  256. struct platform_configs configs;
  257. struct cold_reset cold_reset;
  258. struct regulator *reg;
  259. /* read buffer*/
  260. size_t kbuflen;
  261. u8 *kbuf;
  262. union nqx_uinfo nqx_info;
  263. /*secure zone state*/
  264. bool secure_zone;
  265. void *ipcl;
  266. /* function pointers for the common i2c functionality */
  267. int (*nfc_read)(struct nfc_dev *dev, char *buf, size_t count,
  268. int timeout);
  269. int (*nfc_write)(struct nfc_dev *dev, const char *buf,
  270. const size_t count, int max_retry_cnt);
  271. int (*nfc_enable_intr)(struct nfc_dev *dev);
  272. int (*nfc_disable_intr)(struct nfc_dev *dev);
  273. };
  274. int nfc_dev_open(struct inode *inode, struct file *filp);
  275. int nfc_dev_flush(struct file *pfile, fl_owner_t id);
  276. int nfc_dev_close(struct inode *inode, struct file *filp);
  277. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  278. unsigned long arg);
  279. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
  280. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  281. uint8_t interface);
  282. int nfc_misc_register(struct nfc_dev *nfc_dev,
  283. const struct file_operations *nfc_fops, int count,
  284. char *devname, char *classname);
  285. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count);
  286. int configure_gpio(unsigned int gpio, int flag);
  287. void gpio_set_ven(struct nfc_dev *nfc_dev, int value);
  288. void set_valid_gpio(int gpio, int value);
  289. int nfcc_hw_check(struct nfc_dev *nfc_dev);
  290. unsigned int nfc_ioctl_nfcc_info(struct file *, unsigned long);
  291. void gpio_free_all(struct nfc_dev *nfc_dev);
  292. int nfc_ldo_config(struct device *dev, struct nfc_dev *nfc_dev);
  293. int nfc_ldo_vote(struct nfc_dev *nfc_dev);
  294. int nfc_ese_pwr(struct nfc_dev *nfc_dev, unsigned long arg);
  295. int nfc_ldo_unvote(struct nfc_dev *nfc_dev);
  296. int is_nfc_data_available_for_read(struct nfc_dev *nfc_dev);
  297. int validate_nfc_state_nci(struct nfc_dev *nfc_dev);
  298. int nfc_post_init(struct nfc_dev *nfc_dev);
  299. int nfc_dynamic_protection_ioctl(struct nfc_dev *nfc_dev, unsigned long sec_zone_trans);
  300. bool nfc_hw_secure_check(void);
  301. #endif /* _COMMON_H_ */