193 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * Copyright (c) 2017, Microchip Technology Inc.4 * Author: Tudor Ambarus5 */6 7#ifndef __ATMEL_I2C_H__8#define __ATMEL_I2C_H__9 10#include <linux/hw_random.h>11#include <linux/types.h>12 13#define ATMEL_ECC_PRIORITY 30014 15#define COMMAND 0x03 /* packet function */16#define SLEEP_TOKEN 0x0117#define WAKE_TOKEN_MAX_SIZE 818 19/* Definitions of Data and Command sizes */20#define WORD_ADDR_SIZE 121#define COUNT_SIZE 122#define CRC_SIZE 223#define CMD_OVERHEAD_SIZE (COUNT_SIZE + CRC_SIZE)24 25/* size in bytes of the n prime */26#define ATMEL_ECC_NIST_P256_N_SIZE 3227#define ATMEL_ECC_PUBKEY_SIZE (2 * ATMEL_ECC_NIST_P256_N_SIZE)28 29#define STATUS_RSP_SIZE 430#define ECDH_RSP_SIZE (32 + CMD_OVERHEAD_SIZE)31#define GENKEY_RSP_SIZE (ATMEL_ECC_PUBKEY_SIZE + \32 CMD_OVERHEAD_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_SIZE36 37/**38 * atmel_i2c_cmd - structure used for communicating with the device.39 * @word_addr: indicates the function of the packet sent to the device. This40 * 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 milliseconds48 */49struct atmel_i2c_cmd {50 u8 word_addr;51 u8 count;52 u8 opcode;53 u8 param1;54 __le16 param2;55 u8 data[MAX_RSP_SIZE];56 u8 msecs;57 u16 rxsize;58} __packed;59 60/* Status/Error codes */61#define STATUS_SIZE 0x0462#define STATUS_NOERR 0x0063#define STATUS_WAKE_SUCCESSFUL 0x1164 65/* Definitions for eeprom organization */66#define CONFIGURATION_ZONE 067#define OTP_ZONE 168 69/* Definitions for eeprom zone sizes */70#define OTP_ZONE_SIZE 6471 72/* Definitions for Indexes common to all commands */73#define RSP_DATA_IDX 1 /* buffer index of data in response */74#define DATA_SLOT_2 2 /* used for ECDH private key */75 76/* Definitions for the device lock state */77#define DEVICE_LOCK_ADDR 0x1578#define LOCK_VALUE_IDX (RSP_DATA_IDX + 2)79#define LOCK_CONFIG_IDX (RSP_DATA_IDX + 3)80 81/*82 * Wake High delay to data communication (microseconds). SDA should be stable83 * high for this entire duration.84 */85#define TWHI_MIN 150086#define TWHI_MAX 155087 88/* Wake Low duration */89#define TWLO_USEC 6090 91/* Command execution time (milliseconds) */92#define MAX_EXEC_TIME_ECDH 5893#define MAX_EXEC_TIME_GENKEY 11594#define MAX_EXEC_TIME_READ 195#define MAX_EXEC_TIME_RANDOM 5096 97/* Command opcode */98#define OPCODE_ECDH 0x4399#define OPCODE_GENKEY 0x40100#define OPCODE_READ 0x02101#define OPCODE_RANDOM 0x1b102 103/* Definitions for the READ Command */104#define READ_COUNT 7105 106/* Definitions for the RANDOM Command */107#define RANDOM_COUNT 7108 109/* Definitions for the GenKey Command */110#define GENKEY_COUNT 7111#define GENKEY_MODE_PRIVATE 0x04112 113/* Definitions for the ECDH Command */114#define ECDH_COUNT 71115#define ECDH_PREFIX_MODE 0x00116 117/* Used for binding tfm objects to i2c clients. */118struct atmel_ecc_driver_data {119 struct list_head i2c_client_list;120 spinlock_t i2c_list_lock;121} ____cacheline_aligned;122 123/**124 * atmel_i2c_client_priv - i2c_client private data125 * @client : pointer to i2c client device126 * @i2c_client_list_node: part of i2c_client_list127 * @lock : lock for sending i2c commands128 * @wake_token : wake token array of zeros129 * @wake_token_sz : size in bytes of the wake_token130 * @tfm_count : number of active crypto transformations on i2c client131 * @hwrng : hold the hardware generated rng132 *133 * Reads and writes from/to the i2c client are sequential. The first byte134 * transmitted to the device is treated as the byte size. Any attempt to send135 * more than this number of bytes will cause the device to not ACK those bytes.136 * After the host writes a single command byte to the input buffer, reads are137 * prohibited until after the device completes command execution. Use a mutex138 * when sending i2c commands.139 */140struct atmel_i2c_client_priv {141 struct i2c_client *client;142 struct list_head i2c_client_list_node;143 struct mutex lock;144 u8 wake_token[WAKE_TOKEN_MAX_SIZE];145 size_t wake_token_sz;146 atomic_t tfm_count ____cacheline_aligned;147 struct hwrng hwrng;148};149 150/**151 * atmel_i2c_work_data - data structure representing the work152 * @ctx : transformation context.153 * @cbk : pointer to a callback function to be invoked upon completion of this154 * request. This has the form:155 * callback(struct atmel_i2c_work_data *work_data, void *areq, u8 status)156 * where:157 * @work_data: data structure representing the work158 * @areq : optional pointer to an argument passed with the original159 * request.160 * @status : status returned from the i2c client device or i2c error.161 * @areq: optional pointer to a user argument for use at callback time.162 * @work: describes the task to be executed.163 * @cmd : structure used for communicating with the device.164 */165struct atmel_i2c_work_data {166 void *ctx;167 struct i2c_client *client;168 void (*cbk)(struct atmel_i2c_work_data *work_data, void *areq,169 int status);170 void *areq;171 struct work_struct work;172 struct atmel_i2c_cmd cmd;173};174 175int atmel_i2c_probe(struct i2c_client *client);176 177void atmel_i2c_enqueue(struct atmel_i2c_work_data *work_data,178 void (*cbk)(struct atmel_i2c_work_data *work_data,179 void *areq, int status),180 void *areq);181void atmel_i2c_flush_queue(void);182 183int atmel_i2c_send_receive(struct i2c_client *client, struct atmel_i2c_cmd *cmd);184 185void atmel_i2c_init_read_config_cmd(struct atmel_i2c_cmd *cmd);186int atmel_i2c_init_read_otp_cmd(struct atmel_i2c_cmd *cmd, u16 addr);187void atmel_i2c_init_random_cmd(struct atmel_i2c_cmd *cmd);188void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);189int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,190 struct scatterlist *pubkey);191 192#endif /* __ATMEL_I2C_H__ */193