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