atmel-i2c.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2017, Microchip Technology Inc.
  4. * Author: Tudor Ambarus <[email protected]>
  5. */
  6. #ifndef __ATMEL_I2C_H__
  7. #define __ATMEL_I2C_H__
  8. #include <linux/hw_random.h>
  9. #include <linux/types.h>
  10. #define ATMEL_ECC_PRIORITY 300
  11. #define COMMAND 0x03 /* packet function */
  12. #define SLEEP_TOKEN 0x01
  13. #define WAKE_TOKEN_MAX_SIZE 8
  14. /* Definitions of Data and Command sizes */
  15. #define WORD_ADDR_SIZE 1
  16. #define COUNT_SIZE 1
  17. #define CRC_SIZE 2
  18. #define CMD_OVERHEAD_SIZE (COUNT_SIZE + CRC_SIZE)
  19. /* size in bytes of the n prime */
  20. #define ATMEL_ECC_NIST_P256_N_SIZE 32
  21. #define ATMEL_ECC_PUBKEY_SIZE (2 * ATMEL_ECC_NIST_P256_N_SIZE)
  22. #define STATUS_RSP_SIZE 4
  23. #define ECDH_RSP_SIZE (32 + CMD_OVERHEAD_SIZE)
  24. #define GENKEY_RSP_SIZE (ATMEL_ECC_PUBKEY_SIZE + \
  25. CMD_OVERHEAD_SIZE)
  26. #define READ_RSP_SIZE (4 + CMD_OVERHEAD_SIZE)
  27. #define RANDOM_RSP_SIZE (32 + CMD_OVERHEAD_SIZE)
  28. #define MAX_RSP_SIZE GENKEY_RSP_SIZE
  29. /**
  30. * atmel_i2c_cmd - structure used for communicating with the device.
  31. * @word_addr: indicates the function of the packet sent to the device. This
  32. * byte should have a value of COMMAND for normal operation.
  33. * @count : number of bytes to be transferred to (or from) the device.
  34. * @opcode : the command code.
  35. * @param1 : the first parameter; always present.
  36. * @param2 : the second parameter; always present.
  37. * @data : optional remaining input data. Includes a 2-byte CRC.
  38. * @rxsize : size of the data received from i2c client.
  39. * @msecs : command execution time in milliseconds
  40. */
  41. struct atmel_i2c_cmd {
  42. u8 word_addr;
  43. u8 count;
  44. u8 opcode;
  45. u8 param1;
  46. __le16 param2;
  47. u8 data[MAX_RSP_SIZE];
  48. u8 msecs;
  49. u16 rxsize;
  50. } __packed;
  51. /* Status/Error codes */
  52. #define STATUS_SIZE 0x04
  53. #define STATUS_NOERR 0x00
  54. #define STATUS_WAKE_SUCCESSFUL 0x11
  55. /* Definitions for eeprom organization */
  56. #define CONFIG_ZONE 0
  57. /* Definitions for Indexes common to all commands */
  58. #define RSP_DATA_IDX 1 /* buffer index of data in response */
  59. #define DATA_SLOT_2 2 /* used for ECDH private key */
  60. /* Definitions for the device lock state */
  61. #define DEVICE_LOCK_ADDR 0x15
  62. #define LOCK_VALUE_IDX (RSP_DATA_IDX + 2)
  63. #define LOCK_CONFIG_IDX (RSP_DATA_IDX + 3)
  64. /*
  65. * Wake High delay to data communication (microseconds). SDA should be stable
  66. * high for this entire duration.
  67. */
  68. #define TWHI_MIN 1500
  69. #define TWHI_MAX 1550
  70. /* Wake Low duration */
  71. #define TWLO_USEC 60
  72. /* Command execution time (milliseconds) */
  73. #define MAX_EXEC_TIME_ECDH 58
  74. #define MAX_EXEC_TIME_GENKEY 115
  75. #define MAX_EXEC_TIME_READ 1
  76. #define MAX_EXEC_TIME_RANDOM 50
  77. /* Command opcode */
  78. #define OPCODE_ECDH 0x43
  79. #define OPCODE_GENKEY 0x40
  80. #define OPCODE_READ 0x02
  81. #define OPCODE_RANDOM 0x1b
  82. /* Definitions for the READ Command */
  83. #define READ_COUNT 7
  84. /* Definitions for the RANDOM Command */
  85. #define RANDOM_COUNT 7
  86. /* Definitions for the GenKey Command */
  87. #define GENKEY_COUNT 7
  88. #define GENKEY_MODE_PRIVATE 0x04
  89. /* Definitions for the ECDH Command */
  90. #define ECDH_COUNT 71
  91. #define ECDH_PREFIX_MODE 0x00
  92. /* Used for binding tfm objects to i2c clients. */
  93. struct atmel_ecc_driver_data {
  94. struct list_head i2c_client_list;
  95. spinlock_t i2c_list_lock;
  96. } ____cacheline_aligned;
  97. /**
  98. * atmel_i2c_client_priv - i2c_client private data
  99. * @client : pointer to i2c client device
  100. * @i2c_client_list_node: part of i2c_client_list
  101. * @lock : lock for sending i2c commands
  102. * @wake_token : wake token array of zeros
  103. * @wake_token_sz : size in bytes of the wake_token
  104. * @tfm_count : number of active crypto transformations on i2c client
  105. *
  106. * Reads and writes from/to the i2c client are sequential. The first byte
  107. * transmitted to the device is treated as the byte size. Any attempt to send
  108. * more than this number of bytes will cause the device to not ACK those bytes.
  109. * After the host writes a single command byte to the input buffer, reads are
  110. * prohibited until after the device completes command execution. Use a mutex
  111. * when sending i2c commands.
  112. */
  113. struct atmel_i2c_client_priv {
  114. struct i2c_client *client;
  115. struct list_head i2c_client_list_node;
  116. struct mutex lock;
  117. u8 wake_token[WAKE_TOKEN_MAX_SIZE];
  118. size_t wake_token_sz;
  119. atomic_t tfm_count ____cacheline_aligned;
  120. struct hwrng hwrng;
  121. };
  122. /**
  123. * atmel_i2c_work_data - data structure representing the work
  124. * @ctx : transformation context.
  125. * @cbk : pointer to a callback function to be invoked upon completion of this
  126. * request. This has the form:
  127. * callback(struct atmel_i2c_work_data *work_data, void *areq, u8 status)
  128. * where:
  129. * @work_data: data structure representing the work
  130. * @areq : optional pointer to an argument passed with the original
  131. * request.
  132. * @status : status returned from the i2c client device or i2c error.
  133. * @areq: optional pointer to a user argument for use at callback time.
  134. * @work: describes the task to be executed.
  135. * @cmd : structure used for communicating with the device.
  136. */
  137. struct atmel_i2c_work_data {
  138. void *ctx;
  139. struct i2c_client *client;
  140. void (*cbk)(struct atmel_i2c_work_data *work_data, void *areq,
  141. int status);
  142. void *areq;
  143. struct work_struct work;
  144. struct atmel_i2c_cmd cmd;
  145. };
  146. int atmel_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  147. void atmel_i2c_enqueue(struct atmel_i2c_work_data *work_data,
  148. void (*cbk)(struct atmel_i2c_work_data *work_data,
  149. void *areq, int status),
  150. void *areq);
  151. void atmel_i2c_flush_queue(void);
  152. int atmel_i2c_send_receive(struct i2c_client *client, struct atmel_i2c_cmd *cmd);
  153. void atmel_i2c_init_read_cmd(struct atmel_i2c_cmd *cmd);
  154. void atmel_i2c_init_random_cmd(struct atmel_i2c_cmd *cmd);
  155. void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);
  156. int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
  157. struct scatterlist *pubkey);
  158. #endif /* __ATMEL_I2C_H__ */