Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (c) 2017, Microchip Technology Inc.
0004  * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
0005  */
0006 
0007 #ifndef __ATMEL_I2C_H__
0008 #define __ATMEL_I2C_H__
0009 
0010 #include <linux/hw_random.h>
0011 #include <linux/types.h>
0012 
0013 #define ATMEL_ECC_PRIORITY      300
0014 
0015 #define COMMAND             0x03 /* packet function */
0016 #define SLEEP_TOKEN         0x01
0017 #define WAKE_TOKEN_MAX_SIZE     8
0018 
0019 /* Definitions of Data and Command sizes */
0020 #define WORD_ADDR_SIZE          1
0021 #define COUNT_SIZE          1
0022 #define CRC_SIZE            2
0023 #define CMD_OVERHEAD_SIZE       (COUNT_SIZE + CRC_SIZE)
0024 
0025 /* size in bytes of the n prime */
0026 #define ATMEL_ECC_NIST_P256_N_SIZE  32
0027 #define ATMEL_ECC_PUBKEY_SIZE       (2 * ATMEL_ECC_NIST_P256_N_SIZE)
0028 
0029 #define STATUS_RSP_SIZE         4
0030 #define ECDH_RSP_SIZE           (32 + CMD_OVERHEAD_SIZE)
0031 #define GENKEY_RSP_SIZE         (ATMEL_ECC_PUBKEY_SIZE + \
0032                      CMD_OVERHEAD_SIZE)
0033 #define READ_RSP_SIZE           (4 + CMD_OVERHEAD_SIZE)
0034 #define RANDOM_RSP_SIZE         (32 + CMD_OVERHEAD_SIZE)
0035 #define MAX_RSP_SIZE            GENKEY_RSP_SIZE
0036 
0037 /**
0038  * atmel_i2c_cmd - structure used for communicating with the device.
0039  * @word_addr: indicates the function of the packet sent to the device. This
0040  *             byte should have a value of COMMAND for normal operation.
0041  * @count    : number of bytes to be transferred to (or from) the device.
0042  * @opcode   : the command code.
0043  * @param1   : the first parameter; always present.
0044  * @param2   : the second parameter; always present.
0045  * @data     : optional remaining input data. Includes a 2-byte CRC.
0046  * @rxsize   : size of the data received from i2c client.
0047  * @msecs    : command execution time in milliseconds
0048  */
0049 struct atmel_i2c_cmd {
0050     u8 word_addr;
0051     u8 count;
0052     u8 opcode;
0053     u8 param1;
0054     __le16 param2;
0055     u8 data[MAX_RSP_SIZE];
0056     u8 msecs;
0057     u16 rxsize;
0058 } __packed;
0059 
0060 /* Status/Error codes */
0061 #define STATUS_SIZE         0x04
0062 #define STATUS_NOERR            0x00
0063 #define STATUS_WAKE_SUCCESSFUL      0x11
0064 
0065 /* Definitions for eeprom organization */
0066 #define CONFIG_ZONE         0
0067 
0068 /* Definitions for Indexes common to all commands */
0069 #define RSP_DATA_IDX            1 /* buffer index of data in response */
0070 #define DATA_SLOT_2         2 /* used for ECDH private key */
0071 
0072 /* Definitions for the device lock state */
0073 #define DEVICE_LOCK_ADDR        0x15
0074 #define LOCK_VALUE_IDX          (RSP_DATA_IDX + 2)
0075 #define LOCK_CONFIG_IDX         (RSP_DATA_IDX + 3)
0076 
0077 /*
0078  * Wake High delay to data communication (microseconds). SDA should be stable
0079  * high for this entire duration.
0080  */
0081 #define TWHI_MIN            1500
0082 #define TWHI_MAX            1550
0083 
0084 /* Wake Low duration */
0085 #define TWLO_USEC           60
0086 
0087 /* Command execution time (milliseconds) */
0088 #define MAX_EXEC_TIME_ECDH      58
0089 #define MAX_EXEC_TIME_GENKEY        115
0090 #define MAX_EXEC_TIME_READ      1
0091 #define MAX_EXEC_TIME_RANDOM        50
0092 
0093 /* Command opcode */
0094 #define OPCODE_ECDH         0x43
0095 #define OPCODE_GENKEY           0x40
0096 #define OPCODE_READ         0x02
0097 #define OPCODE_RANDOM           0x1b
0098 
0099 /* Definitions for the READ Command */
0100 #define READ_COUNT          7
0101 
0102 /* Definitions for the RANDOM Command */
0103 #define RANDOM_COUNT            7
0104 
0105 /* Definitions for the GenKey Command */
0106 #define GENKEY_COUNT            7
0107 #define GENKEY_MODE_PRIVATE     0x04
0108 
0109 /* Definitions for the ECDH Command */
0110 #define ECDH_COUNT          71
0111 #define ECDH_PREFIX_MODE        0x00
0112 
0113 /* Used for binding tfm objects to i2c clients. */
0114 struct atmel_ecc_driver_data {
0115     struct list_head i2c_client_list;
0116     spinlock_t i2c_list_lock;
0117 } ____cacheline_aligned;
0118 
0119 /**
0120  * atmel_i2c_client_priv - i2c_client private data
0121  * @client              : pointer to i2c client device
0122  * @i2c_client_list_node: part of i2c_client_list
0123  * @lock                : lock for sending i2c commands
0124  * @wake_token          : wake token array of zeros
0125  * @wake_token_sz       : size in bytes of the wake_token
0126  * @tfm_count           : number of active crypto transformations on i2c client
0127  *
0128  * Reads and writes from/to the i2c client are sequential. The first byte
0129  * transmitted to the device is treated as the byte size. Any attempt to send
0130  * more than this number of bytes will cause the device to not ACK those bytes.
0131  * After the host writes a single command byte to the input buffer, reads are
0132  * prohibited until after the device completes command execution. Use a mutex
0133  * when sending i2c commands.
0134  */
0135 struct atmel_i2c_client_priv {
0136     struct i2c_client *client;
0137     struct list_head i2c_client_list_node;
0138     struct mutex lock;
0139     u8 wake_token[WAKE_TOKEN_MAX_SIZE];
0140     size_t wake_token_sz;
0141     atomic_t tfm_count ____cacheline_aligned;
0142     struct hwrng hwrng;
0143 };
0144 
0145 /**
0146  * atmel_i2c_work_data - data structure representing the work
0147  * @ctx : transformation context.
0148  * @cbk : pointer to a callback function to be invoked upon completion of this
0149  *        request. This has the form:
0150  *        callback(struct atmel_i2c_work_data *work_data, void *areq, u8 status)
0151  *        where:
0152  *        @work_data: data structure representing the work
0153  *        @areq     : optional pointer to an argument passed with the original
0154  *                    request.
0155  *        @status   : status returned from the i2c client device or i2c error.
0156  * @areq: optional pointer to a user argument for use at callback time.
0157  * @work: describes the task to be executed.
0158  * @cmd : structure used for communicating with the device.
0159  */
0160 struct atmel_i2c_work_data {
0161     void *ctx;
0162     struct i2c_client *client;
0163     void (*cbk)(struct atmel_i2c_work_data *work_data, void *areq,
0164             int status);
0165     void *areq;
0166     struct work_struct work;
0167     struct atmel_i2c_cmd cmd;
0168 };
0169 
0170 int atmel_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
0171 
0172 void atmel_i2c_enqueue(struct atmel_i2c_work_data *work_data,
0173                void (*cbk)(struct atmel_i2c_work_data *work_data,
0174                    void *areq, int status),
0175                void *areq);
0176 void atmel_i2c_flush_queue(void);
0177 
0178 int atmel_i2c_send_receive(struct i2c_client *client, struct atmel_i2c_cmd *cmd);
0179 
0180 void atmel_i2c_init_read_cmd(struct atmel_i2c_cmd *cmd);
0181 void atmel_i2c_init_random_cmd(struct atmel_i2c_cmd *cmd);
0182 void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);
0183 int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
0184                 struct scatterlist *pubkey);
0185 
0186 #endif /* __ATMEL_I2C_H__ */