common.h 9.6 KB

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