/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (c) 2017, Microchip Technology Inc.3* Author: Tudor Ambarus4*/56#ifndef __ATMEL_I2C_H__7#define __ATMEL_I2C_H__89#include <linux/hw_random.h>10#include <linux/types.h>1112#define ATMEL_ECC_PRIORITY 3001314#define COMMAND 0x03 /* packet function */15#define SLEEP_TOKEN 0x0116#define WAKE_TOKEN_MAX_SIZE 81718/* Definitions of Data and Command sizes */19#define WORD_ADDR_SIZE 120#define COUNT_SIZE 121#define CRC_SIZE 222#define CMD_OVERHEAD_SIZE (COUNT_SIZE + CRC_SIZE)2324/* size in bytes of the n prime */25#define ATMEL_ECC_NIST_P256_N_SIZE 3226#define ATMEL_ECC_PUBKEY_SIZE (2 * ATMEL_ECC_NIST_P256_N_SIZE)2728#define STATUS_RSP_SIZE 429#define ECDH_RSP_SIZE (32 + CMD_OVERHEAD_SIZE)30#define GENKEY_RSP_SIZE (ATMEL_ECC_PUBKEY_SIZE + \31CMD_OVERHEAD_SIZE)32#define READ_RSP_SIZE (4 + CMD_OVERHEAD_SIZE)33#define RANDOM_RSP_SIZE (32 + CMD_OVERHEAD_SIZE)34#define MAX_RSP_SIZE GENKEY_RSP_SIZE3536/**37* atmel_i2c_cmd - structure used for communicating with the device.38* @word_addr: indicates the function of the packet sent to the device. This39* byte should have a value of COMMAND for normal operation.40* @count : number of bytes to be transferred to (or from) the device.41* @opcode : the command code.42* @param1 : the first parameter; always present.43* @param2 : the second parameter; always present.44* @data : optional remaining input data. Includes a 2-byte CRC.45* @rxsize : size of the data received from i2c client.46* @msecs : command execution time in milliseconds47*/48struct atmel_i2c_cmd {49u8 word_addr;50u8 count;51u8 opcode;52u8 param1;53__le16 param2;54u8 data[MAX_RSP_SIZE];55u8 msecs;56u16 rxsize;57} __packed;5859/* Status/Error codes */60#define STATUS_SIZE 0x0461#define STATUS_NOERR 0x0062#define STATUS_WAKE_SUCCESSFUL 0x116364/* Definitions for eeprom organization */65#define CONFIGURATION_ZONE 066#define OTP_ZONE 16768/* Definitions for eeprom zone sizes */69#define OTP_ZONE_SIZE 647071/* Definitions for Indexes common to all commands */72#define RSP_DATA_IDX 1 /* buffer index of data in response */73#define DATA_SLOT_2 2 /* used for ECDH private key */7475/* Definitions for the device lock state */76#define DEVICE_LOCK_ADDR 0x1577#define LOCK_VALUE_IDX (RSP_DATA_IDX + 2)78#define LOCK_CONFIG_IDX (RSP_DATA_IDX + 3)7980/*81* Wake High delay to data communication (microseconds). SDA should be stable82* high for this entire duration.83*/84#define TWHI_MIN 150085#define TWHI_MAX 15508687/* Wake Low duration */88#define TWLO_USEC 608990/* Command execution time (milliseconds) */91#define MAX_EXEC_TIME_ECDH 5892#define MAX_EXEC_TIME_GENKEY 11593#define MAX_EXEC_TIME_READ 194#define MAX_EXEC_TIME_RANDOM 509596/* Command opcode */97#define OPCODE_ECDH 0x4398#define OPCODE_GENKEY 0x4099#define OPCODE_READ 0x02100#define OPCODE_RANDOM 0x1b101102/* Definitions for the READ Command */103#define READ_COUNT 7104105/* Definitions for the RANDOM Command */106#define RANDOM_COUNT 7107108/* Definitions for the GenKey Command */109#define GENKEY_COUNT 7110#define GENKEY_MODE_PRIVATE 0x04111112/* Definitions for the ECDH Command */113#define ECDH_COUNT 71114#define ECDH_PREFIX_MODE 0x00115116/* Used for binding tfm objects to i2c clients. */117struct atmel_ecc_driver_data {118struct list_head i2c_client_list;119spinlock_t i2c_list_lock;120} ____cacheline_aligned;121122/**123* atmel_i2c_client_priv - i2c_client private data124* @client : pointer to i2c client device125* @i2c_client_list_node: part of i2c_client_list126* @lock : lock for sending i2c commands127* @wake_token : wake token array of zeros128* @wake_token_sz : size in bytes of the wake_token129* @tfm_count : number of active crypto transformations on i2c client130* @hwrng : hold the hardware generated rng131*132* Reads and writes from/to the i2c client are sequential. The first byte133* transmitted to the device is treated as the byte size. Any attempt to send134* more than this number of bytes will cause the device to not ACK those bytes.135* After the host writes a single command byte to the input buffer, reads are136* prohibited until after the device completes command execution. Use a mutex137* when sending i2c commands.138*/139struct atmel_i2c_client_priv {140struct i2c_client *client;141struct list_head i2c_client_list_node;142struct mutex lock;143u8 wake_token[WAKE_TOKEN_MAX_SIZE];144size_t wake_token_sz;145atomic_t tfm_count ____cacheline_aligned;146struct hwrng hwrng;147};148149/**150* atmel_i2c_work_data - data structure representing the work151* @ctx : transformation context.152* @cbk : pointer to a callback function to be invoked upon completion of this153* request. This has the form:154* callback(struct atmel_i2c_work_data *work_data, void *areq, u8 status)155* where:156* @work_data: data structure representing the work157* @areq : optional pointer to an argument passed with the original158* request.159* @status : status returned from the i2c client device or i2c error.160* @areq: optional pointer to a user argument for use at callback time.161* @work: describes the task to be executed.162* @cmd : structure used for communicating with the device.163*/164struct atmel_i2c_work_data {165void *ctx;166struct i2c_client *client;167void (*cbk)(struct atmel_i2c_work_data *work_data, void *areq,168int status);169void *areq;170struct work_struct work;171struct atmel_i2c_cmd cmd;172};173174int atmel_i2c_probe(struct i2c_client *client);175176void atmel_i2c_enqueue(struct atmel_i2c_work_data *work_data,177void (*cbk)(struct atmel_i2c_work_data *work_data,178void *areq, int status),179void *areq);180void atmel_i2c_flush_queue(void);181182int atmel_i2c_send_receive(struct i2c_client *client, struct atmel_i2c_cmd *cmd);183184void atmel_i2c_init_read_config_cmd(struct atmel_i2c_cmd *cmd);185int atmel_i2c_init_read_otp_cmd(struct atmel_i2c_cmd *cmd, u16 addr);186void atmel_i2c_init_random_cmd(struct atmel_i2c_cmd *cmd);187void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);188int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,189struct scatterlist *pubkey);190191#endif /* __ATMEL_I2C_H__ */192193194