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