Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Microchip / Atmel ECC (I2C) driver.
0004  *
0005  * Copyright (c) 2017, Microchip Technology Inc.
0006  * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
0007  */
0008 
0009 #include <linux/bitrev.h>
0010 #include <linux/crc16.h>
0011 #include <linux/delay.h>
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/errno.h>
0015 #include <linux/i2c.h>
0016 #include <linux/init.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/scatterlist.h>
0020 #include <linux/slab.h>
0021 #include <linux/workqueue.h>
0022 #include "atmel-i2c.h"
0023 
0024 static const struct {
0025     u8 value;
0026     const char *error_text;
0027 } error_list[] = {
0028     { 0x01, "CheckMac or Verify miscompare" },
0029     { 0x03, "Parse Error" },
0030     { 0x05, "ECC Fault" },
0031     { 0x0F, "Execution Error" },
0032     { 0xEE, "Watchdog about to expire" },
0033     { 0xFF, "CRC or other communication error" },
0034 };
0035 
0036 /**
0037  * atmel_i2c_checksum() - Generate 16-bit CRC as required by ATMEL ECC.
0038  * CRC16 verification of the count, opcode, param1, param2 and data bytes.
0039  * The checksum is saved in little-endian format in the least significant
0040  * two bytes of the command. CRC polynomial is 0x8005 and the initial register
0041  * value should be zero.
0042  *
0043  * @cmd : structure used for communicating with the device.
0044  */
0045 static void atmel_i2c_checksum(struct atmel_i2c_cmd *cmd)
0046 {
0047     u8 *data = &cmd->count;
0048     size_t len = cmd->count - CRC_SIZE;
0049     __le16 *__crc16 = (__le16 *)(data + len);
0050 
0051     *__crc16 = cpu_to_le16(bitrev16(crc16(0, data, len)));
0052 }
0053 
0054 void atmel_i2c_init_read_cmd(struct atmel_i2c_cmd *cmd)
0055 {
0056     cmd->word_addr = COMMAND;
0057     cmd->opcode = OPCODE_READ;
0058     /*
0059      * Read the word from Configuration zone that contains the lock bytes
0060      * (UserExtra, Selector, LockValue, LockConfig).
0061      */
0062     cmd->param1 = CONFIG_ZONE;
0063     cmd->param2 = cpu_to_le16(DEVICE_LOCK_ADDR);
0064     cmd->count = READ_COUNT;
0065 
0066     atmel_i2c_checksum(cmd);
0067 
0068     cmd->msecs = MAX_EXEC_TIME_READ;
0069     cmd->rxsize = READ_RSP_SIZE;
0070 }
0071 EXPORT_SYMBOL(atmel_i2c_init_read_cmd);
0072 
0073 void atmel_i2c_init_random_cmd(struct atmel_i2c_cmd *cmd)
0074 {
0075     cmd->word_addr = COMMAND;
0076     cmd->opcode = OPCODE_RANDOM;
0077     cmd->param1 = 0;
0078     cmd->param2 = 0;
0079     cmd->count = RANDOM_COUNT;
0080 
0081     atmel_i2c_checksum(cmd);
0082 
0083     cmd->msecs = MAX_EXEC_TIME_RANDOM;
0084     cmd->rxsize = RANDOM_RSP_SIZE;
0085 }
0086 EXPORT_SYMBOL(atmel_i2c_init_random_cmd);
0087 
0088 void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid)
0089 {
0090     cmd->word_addr = COMMAND;
0091     cmd->count = GENKEY_COUNT;
0092     cmd->opcode = OPCODE_GENKEY;
0093     cmd->param1 = GENKEY_MODE_PRIVATE;
0094     /* a random private key will be generated and stored in slot keyID */
0095     cmd->param2 = cpu_to_le16(keyid);
0096 
0097     atmel_i2c_checksum(cmd);
0098 
0099     cmd->msecs = MAX_EXEC_TIME_GENKEY;
0100     cmd->rxsize = GENKEY_RSP_SIZE;
0101 }
0102 EXPORT_SYMBOL(atmel_i2c_init_genkey_cmd);
0103 
0104 int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
0105                 struct scatterlist *pubkey)
0106 {
0107     size_t copied;
0108 
0109     cmd->word_addr = COMMAND;
0110     cmd->count = ECDH_COUNT;
0111     cmd->opcode = OPCODE_ECDH;
0112     cmd->param1 = ECDH_PREFIX_MODE;
0113     /* private key slot */
0114     cmd->param2 = cpu_to_le16(DATA_SLOT_2);
0115 
0116     /*
0117      * The device only supports NIST P256 ECC keys. The public key size will
0118      * always be the same. Use a macro for the key size to avoid unnecessary
0119      * computations.
0120      */
0121     copied = sg_copy_to_buffer(pubkey,
0122                    sg_nents_for_len(pubkey,
0123                             ATMEL_ECC_PUBKEY_SIZE),
0124                    cmd->data, ATMEL_ECC_PUBKEY_SIZE);
0125     if (copied != ATMEL_ECC_PUBKEY_SIZE)
0126         return -EINVAL;
0127 
0128     atmel_i2c_checksum(cmd);
0129 
0130     cmd->msecs = MAX_EXEC_TIME_ECDH;
0131     cmd->rxsize = ECDH_RSP_SIZE;
0132 
0133     return 0;
0134 }
0135 EXPORT_SYMBOL(atmel_i2c_init_ecdh_cmd);
0136 
0137 /*
0138  * After wake and after execution of a command, there will be error, status, or
0139  * result bytes in the device's output register that can be retrieved by the
0140  * system. When the length of that group is four bytes, the codes returned are
0141  * detailed in error_list.
0142  */
0143 static int atmel_i2c_status(struct device *dev, u8 *status)
0144 {
0145     size_t err_list_len = ARRAY_SIZE(error_list);
0146     int i;
0147     u8 err_id = status[1];
0148 
0149     if (*status != STATUS_SIZE)
0150         return 0;
0151 
0152     if (err_id == STATUS_WAKE_SUCCESSFUL || err_id == STATUS_NOERR)
0153         return 0;
0154 
0155     for (i = 0; i < err_list_len; i++)
0156         if (error_list[i].value == err_id)
0157             break;
0158 
0159     /* if err_id is not in the error_list then ignore it */
0160     if (i != err_list_len) {
0161         dev_err(dev, "%02x: %s:\n", err_id, error_list[i].error_text);
0162         return err_id;
0163     }
0164 
0165     return 0;
0166 }
0167 
0168 static int atmel_i2c_wakeup(struct i2c_client *client)
0169 {
0170     struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
0171     u8 status[STATUS_RSP_SIZE];
0172     int ret;
0173 
0174     /*
0175      * The device ignores any levels or transitions on the SCL pin when the
0176      * device is idle, asleep or during waking up. Don't check for error
0177      * when waking up the device.
0178      */
0179     i2c_transfer_buffer_flags(client, i2c_priv->wake_token,
0180                 i2c_priv->wake_token_sz, I2C_M_IGNORE_NAK);
0181 
0182     /*
0183      * Wait to wake the device. Typical execution times for ecdh and genkey
0184      * are around tens of milliseconds. Delta is chosen to 50 microseconds.
0185      */
0186     usleep_range(TWHI_MIN, TWHI_MAX);
0187 
0188     ret = i2c_master_recv(client, status, STATUS_SIZE);
0189     if (ret < 0)
0190         return ret;
0191 
0192     return atmel_i2c_status(&client->dev, status);
0193 }
0194 
0195 static int atmel_i2c_sleep(struct i2c_client *client)
0196 {
0197     u8 sleep = SLEEP_TOKEN;
0198 
0199     return i2c_master_send(client, &sleep, 1);
0200 }
0201 
0202 /*
0203  * atmel_i2c_send_receive() - send a command to the device and receive its
0204  *                            response.
0205  * @client: i2c client device
0206  * @cmd   : structure used to communicate with the device
0207  *
0208  * After the device receives a Wake token, a watchdog counter starts within the
0209  * device. After the watchdog timer expires, the device enters sleep mode
0210  * regardless of whether some I/O transmission or command execution is in
0211  * progress. If a command is attempted when insufficient time remains prior to
0212  * watchdog timer execution, the device will return the watchdog timeout error
0213  * code without attempting to execute the command. There is no way to reset the
0214  * counter other than to put the device into sleep or idle mode and then
0215  * wake it up again.
0216  */
0217 int atmel_i2c_send_receive(struct i2c_client *client, struct atmel_i2c_cmd *cmd)
0218 {
0219     struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
0220     int ret;
0221 
0222     mutex_lock(&i2c_priv->lock);
0223 
0224     ret = atmel_i2c_wakeup(client);
0225     if (ret)
0226         goto err;
0227 
0228     /* send the command */
0229     ret = i2c_master_send(client, (u8 *)cmd, cmd->count + WORD_ADDR_SIZE);
0230     if (ret < 0)
0231         goto err;
0232 
0233     /* delay the appropriate amount of time for command to execute */
0234     msleep(cmd->msecs);
0235 
0236     /* receive the response */
0237     ret = i2c_master_recv(client, cmd->data, cmd->rxsize);
0238     if (ret < 0)
0239         goto err;
0240 
0241     /* put the device into low-power mode */
0242     ret = atmel_i2c_sleep(client);
0243     if (ret < 0)
0244         goto err;
0245 
0246     mutex_unlock(&i2c_priv->lock);
0247     return atmel_i2c_status(&client->dev, cmd->data);
0248 err:
0249     mutex_unlock(&i2c_priv->lock);
0250     return ret;
0251 }
0252 EXPORT_SYMBOL(atmel_i2c_send_receive);
0253 
0254 static void atmel_i2c_work_handler(struct work_struct *work)
0255 {
0256     struct atmel_i2c_work_data *work_data =
0257             container_of(work, struct atmel_i2c_work_data, work);
0258     struct atmel_i2c_cmd *cmd = &work_data->cmd;
0259     struct i2c_client *client = work_data->client;
0260     int status;
0261 
0262     status = atmel_i2c_send_receive(client, cmd);
0263     work_data->cbk(work_data, work_data->areq, status);
0264 }
0265 
0266 static struct workqueue_struct *atmel_wq;
0267 
0268 void atmel_i2c_enqueue(struct atmel_i2c_work_data *work_data,
0269                void (*cbk)(struct atmel_i2c_work_data *work_data,
0270                    void *areq, int status),
0271                void *areq)
0272 {
0273     work_data->cbk = (void *)cbk;
0274     work_data->areq = areq;
0275 
0276     INIT_WORK(&work_data->work, atmel_i2c_work_handler);
0277     queue_work(atmel_wq, &work_data->work);
0278 }
0279 EXPORT_SYMBOL(atmel_i2c_enqueue);
0280 
0281 void atmel_i2c_flush_queue(void)
0282 {
0283     flush_workqueue(atmel_wq);
0284 }
0285 EXPORT_SYMBOL(atmel_i2c_flush_queue);
0286 
0287 static inline size_t atmel_i2c_wake_token_sz(u32 bus_clk_rate)
0288 {
0289     u32 no_of_bits = DIV_ROUND_UP(TWLO_USEC * bus_clk_rate, USEC_PER_SEC);
0290 
0291     /* return the size of the wake_token in bytes */
0292     return DIV_ROUND_UP(no_of_bits, 8);
0293 }
0294 
0295 static int device_sanity_check(struct i2c_client *client)
0296 {
0297     struct atmel_i2c_cmd *cmd;
0298     int ret;
0299 
0300     cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
0301     if (!cmd)
0302         return -ENOMEM;
0303 
0304     atmel_i2c_init_read_cmd(cmd);
0305 
0306     ret = atmel_i2c_send_receive(client, cmd);
0307     if (ret)
0308         goto free_cmd;
0309 
0310     /*
0311      * It is vital that the Configuration, Data and OTP zones be locked
0312      * prior to release into the field of the system containing the device.
0313      * Failure to lock these zones may permit modification of any secret
0314      * keys and may lead to other security problems.
0315      */
0316     if (cmd->data[LOCK_CONFIG_IDX] || cmd->data[LOCK_VALUE_IDX]) {
0317         dev_err(&client->dev, "Configuration or Data and OTP zones are unlocked!\n");
0318         ret = -ENOTSUPP;
0319     }
0320 
0321     /* fall through */
0322 free_cmd:
0323     kfree(cmd);
0324     return ret;
0325 }
0326 
0327 int atmel_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
0328 {
0329     struct atmel_i2c_client_priv *i2c_priv;
0330     struct device *dev = &client->dev;
0331     int ret;
0332     u32 bus_clk_rate;
0333 
0334     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0335         dev_err(dev, "I2C_FUNC_I2C not supported\n");
0336         return -ENODEV;
0337     }
0338 
0339     bus_clk_rate = i2c_acpi_find_bus_speed(&client->adapter->dev);
0340     if (!bus_clk_rate) {
0341         ret = device_property_read_u32(&client->adapter->dev,
0342                            "clock-frequency", &bus_clk_rate);
0343         if (ret) {
0344             dev_err(dev, "failed to read clock-frequency property\n");
0345             return ret;
0346         }
0347     }
0348 
0349     if (bus_clk_rate > 1000000L) {
0350         dev_err(dev, "%u exceeds maximum supported clock frequency (1MHz)\n",
0351             bus_clk_rate);
0352         return -EINVAL;
0353     }
0354 
0355     i2c_priv = devm_kmalloc(dev, sizeof(*i2c_priv), GFP_KERNEL);
0356     if (!i2c_priv)
0357         return -ENOMEM;
0358 
0359     i2c_priv->client = client;
0360     mutex_init(&i2c_priv->lock);
0361 
0362     /*
0363      * WAKE_TOKEN_MAX_SIZE was calculated for the maximum bus_clk_rate -
0364      * 1MHz. The previous bus_clk_rate check ensures us that wake_token_sz
0365      * will always be smaller than or equal to WAKE_TOKEN_MAX_SIZE.
0366      */
0367     i2c_priv->wake_token_sz = atmel_i2c_wake_token_sz(bus_clk_rate);
0368 
0369     memset(i2c_priv->wake_token, 0, sizeof(i2c_priv->wake_token));
0370 
0371     atomic_set(&i2c_priv->tfm_count, 0);
0372 
0373     i2c_set_clientdata(client, i2c_priv);
0374 
0375     return device_sanity_check(client);
0376 }
0377 EXPORT_SYMBOL(atmel_i2c_probe);
0378 
0379 static int __init atmel_i2c_init(void)
0380 {
0381     atmel_wq = alloc_workqueue("atmel_wq", 0, 0);
0382     return atmel_wq ? 0 : -ENOMEM;
0383 }
0384 
0385 static void __exit atmel_i2c_exit(void)
0386 {
0387     destroy_workqueue(atmel_wq);
0388 }
0389 
0390 module_init(atmel_i2c_init);
0391 module_exit(atmel_i2c_exit);
0392 
0393 MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@microchip.com>");
0394 MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
0395 MODULE_LICENSE("GPL v2");