staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / drivers / crypto / atmel-i2c.c
blobdc876fab2882f3f88f858b5c56c419df5c23b42c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Microchip / Atmel ECC (I2C) driver.
5 * Copyright (c) 2017, Microchip Technology Inc.
6 * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
7 */
9 #include <linux/bitrev.h>
10 #include <linux/crc16.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 #include "atmel-i2c.h"
24 /**
25 * atmel_i2c_checksum() - Generate 16-bit CRC as required by ATMEL ECC.
26 * CRC16 verification of the count, opcode, param1, param2 and data bytes.
27 * The checksum is saved in little-endian format in the least significant
28 * two bytes of the command. CRC polynomial is 0x8005 and the initial register
29 * value should be zero.
31 * @cmd : structure used for communicating with the device.
33 static void atmel_i2c_checksum(struct atmel_i2c_cmd *cmd)
35 u8 *data = &cmd->count;
36 size_t len = cmd->count - CRC_SIZE;
37 __le16 *__crc16 = (__le16 *)(data + len);
39 *__crc16 = cpu_to_le16(bitrev16(crc16(0, data, len)));
42 void atmel_i2c_init_read_cmd(struct atmel_i2c_cmd *cmd)
44 cmd->word_addr = COMMAND;
45 cmd->opcode = OPCODE_READ;
47 * Read the word from Configuration zone that contains the lock bytes
48 * (UserExtra, Selector, LockValue, LockConfig).
50 cmd->param1 = CONFIG_ZONE;
51 cmd->param2 = cpu_to_le16(DEVICE_LOCK_ADDR);
52 cmd->count = READ_COUNT;
54 atmel_i2c_checksum(cmd);
56 cmd->msecs = MAX_EXEC_TIME_READ;
57 cmd->rxsize = READ_RSP_SIZE;
59 EXPORT_SYMBOL(atmel_i2c_init_read_cmd);
61 void atmel_i2c_init_random_cmd(struct atmel_i2c_cmd *cmd)
63 cmd->word_addr = COMMAND;
64 cmd->opcode = OPCODE_RANDOM;
65 cmd->param1 = 0;
66 cmd->param2 = 0;
67 cmd->count = RANDOM_COUNT;
69 atmel_i2c_checksum(cmd);
71 cmd->msecs = MAX_EXEC_TIME_RANDOM;
72 cmd->rxsize = RANDOM_RSP_SIZE;
74 EXPORT_SYMBOL(atmel_i2c_init_random_cmd);
76 void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid)
78 cmd->word_addr = COMMAND;
79 cmd->count = GENKEY_COUNT;
80 cmd->opcode = OPCODE_GENKEY;
81 cmd->param1 = GENKEY_MODE_PRIVATE;
82 /* a random private key will be generated and stored in slot keyID */
83 cmd->param2 = cpu_to_le16(keyid);
85 atmel_i2c_checksum(cmd);
87 cmd->msecs = MAX_EXEC_TIME_GENKEY;
88 cmd->rxsize = GENKEY_RSP_SIZE;
90 EXPORT_SYMBOL(atmel_i2c_init_genkey_cmd);
92 int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
93 struct scatterlist *pubkey)
95 size_t copied;
97 cmd->word_addr = COMMAND;
98 cmd->count = ECDH_COUNT;
99 cmd->opcode = OPCODE_ECDH;
100 cmd->param1 = ECDH_PREFIX_MODE;
101 /* private key slot */
102 cmd->param2 = cpu_to_le16(DATA_SLOT_2);
105 * The device only supports NIST P256 ECC keys. The public key size will
106 * always be the same. Use a macro for the key size to avoid unnecessary
107 * computations.
109 copied = sg_copy_to_buffer(pubkey,
110 sg_nents_for_len(pubkey,
111 ATMEL_ECC_PUBKEY_SIZE),
112 cmd->data, ATMEL_ECC_PUBKEY_SIZE);
113 if (copied != ATMEL_ECC_PUBKEY_SIZE)
114 return -EINVAL;
116 atmel_i2c_checksum(cmd);
118 cmd->msecs = MAX_EXEC_TIME_ECDH;
119 cmd->rxsize = ECDH_RSP_SIZE;
121 return 0;
123 EXPORT_SYMBOL(atmel_i2c_init_ecdh_cmd);
126 * After wake and after execution of a command, there will be error, status, or
127 * result bytes in the device's output register that can be retrieved by the
128 * system. When the length of that group is four bytes, the codes returned are
129 * detailed in error_list.
131 static int atmel_i2c_status(struct device *dev, u8 *status)
133 size_t err_list_len = ARRAY_SIZE(error_list);
134 int i;
135 u8 err_id = status[1];
137 if (*status != STATUS_SIZE)
138 return 0;
140 if (err_id == STATUS_WAKE_SUCCESSFUL || err_id == STATUS_NOERR)
141 return 0;
143 for (i = 0; i < err_list_len; i++)
144 if (error_list[i].value == err_id)
145 break;
147 /* if err_id is not in the error_list then ignore it */
148 if (i != err_list_len) {
149 dev_err(dev, "%02x: %s:\n", err_id, error_list[i].error_text);
150 return err_id;
153 return 0;
156 static int atmel_i2c_wakeup(struct i2c_client *client)
158 struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
159 u8 status[STATUS_RSP_SIZE];
160 int ret;
163 * The device ignores any levels or transitions on the SCL pin when the
164 * device is idle, asleep or during waking up. Don't check for error
165 * when waking up the device.
167 i2c_master_send(client, i2c_priv->wake_token, i2c_priv->wake_token_sz);
170 * Wait to wake the device. Typical execution times for ecdh and genkey
171 * are around tens of milliseconds. Delta is chosen to 50 microseconds.
173 usleep_range(TWHI_MIN, TWHI_MAX);
175 ret = i2c_master_recv(client, status, STATUS_SIZE);
176 if (ret < 0)
177 return ret;
179 return atmel_i2c_status(&client->dev, status);
182 static int atmel_i2c_sleep(struct i2c_client *client)
184 u8 sleep = SLEEP_TOKEN;
186 return i2c_master_send(client, &sleep, 1);
190 * atmel_i2c_send_receive() - send a command to the device and receive its
191 * response.
192 * @client: i2c client device
193 * @cmd : structure used to communicate with the device
195 * After the device receives a Wake token, a watchdog counter starts within the
196 * device. After the watchdog timer expires, the device enters sleep mode
197 * regardless of whether some I/O transmission or command execution is in
198 * progress. If a command is attempted when insufficient time remains prior to
199 * watchdog timer execution, the device will return the watchdog timeout error
200 * code without attempting to execute the command. There is no way to reset the
201 * counter other than to put the device into sleep or idle mode and then
202 * wake it up again.
204 int atmel_i2c_send_receive(struct i2c_client *client, struct atmel_i2c_cmd *cmd)
206 struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
207 int ret;
209 mutex_lock(&i2c_priv->lock);
211 ret = atmel_i2c_wakeup(client);
212 if (ret)
213 goto err;
215 /* send the command */
216 ret = i2c_master_send(client, (u8 *)cmd, cmd->count + WORD_ADDR_SIZE);
217 if (ret < 0)
218 goto err;
220 /* delay the appropriate amount of time for command to execute */
221 msleep(cmd->msecs);
223 /* receive the response */
224 ret = i2c_master_recv(client, cmd->data, cmd->rxsize);
225 if (ret < 0)
226 goto err;
228 /* put the device into low-power mode */
229 ret = atmel_i2c_sleep(client);
230 if (ret < 0)
231 goto err;
233 mutex_unlock(&i2c_priv->lock);
234 return atmel_i2c_status(&client->dev, cmd->data);
235 err:
236 mutex_unlock(&i2c_priv->lock);
237 return ret;
239 EXPORT_SYMBOL(atmel_i2c_send_receive);
241 static void atmel_i2c_work_handler(struct work_struct *work)
243 struct atmel_i2c_work_data *work_data =
244 container_of(work, struct atmel_i2c_work_data, work);
245 struct atmel_i2c_cmd *cmd = &work_data->cmd;
246 struct i2c_client *client = work_data->client;
247 int status;
249 status = atmel_i2c_send_receive(client, cmd);
250 work_data->cbk(work_data, work_data->areq, status);
253 void atmel_i2c_enqueue(struct atmel_i2c_work_data *work_data,
254 void (*cbk)(struct atmel_i2c_work_data *work_data,
255 void *areq, int status),
256 void *areq)
258 work_data->cbk = (void *)cbk;
259 work_data->areq = areq;
261 INIT_WORK(&work_data->work, atmel_i2c_work_handler);
262 schedule_work(&work_data->work);
264 EXPORT_SYMBOL(atmel_i2c_enqueue);
266 static inline size_t atmel_i2c_wake_token_sz(u32 bus_clk_rate)
268 u32 no_of_bits = DIV_ROUND_UP(TWLO_USEC * bus_clk_rate, USEC_PER_SEC);
270 /* return the size of the wake_token in bytes */
271 return DIV_ROUND_UP(no_of_bits, 8);
274 static int device_sanity_check(struct i2c_client *client)
276 struct atmel_i2c_cmd *cmd;
277 int ret;
279 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
280 if (!cmd)
281 return -ENOMEM;
283 atmel_i2c_init_read_cmd(cmd);
285 ret = atmel_i2c_send_receive(client, cmd);
286 if (ret)
287 goto free_cmd;
290 * It is vital that the Configuration, Data and OTP zones be locked
291 * prior to release into the field of the system containing the device.
292 * Failure to lock these zones may permit modification of any secret
293 * keys and may lead to other security problems.
295 if (cmd->data[LOCK_CONFIG_IDX] || cmd->data[LOCK_VALUE_IDX]) {
296 dev_err(&client->dev, "Configuration or Data and OTP zones are unlocked!\n");
297 ret = -ENOTSUPP;
300 /* fall through */
301 free_cmd:
302 kfree(cmd);
303 return ret;
306 int atmel_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
308 struct atmel_i2c_client_priv *i2c_priv;
309 struct device *dev = &client->dev;
310 int ret;
311 u32 bus_clk_rate;
313 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
314 dev_err(dev, "I2C_FUNC_I2C not supported\n");
315 return -ENODEV;
318 bus_clk_rate = i2c_acpi_find_bus_speed(&client->adapter->dev);
319 if (!bus_clk_rate) {
320 ret = device_property_read_u32(&client->adapter->dev,
321 "clock-frequency", &bus_clk_rate);
322 if (ret) {
323 dev_err(dev, "failed to read clock-frequency property\n");
324 return ret;
328 if (bus_clk_rate > 1000000L) {
329 dev_err(dev, "%d exceeds maximum supported clock frequency (1MHz)\n",
330 bus_clk_rate);
331 return -EINVAL;
334 i2c_priv = devm_kmalloc(dev, sizeof(*i2c_priv), GFP_KERNEL);
335 if (!i2c_priv)
336 return -ENOMEM;
338 i2c_priv->client = client;
339 mutex_init(&i2c_priv->lock);
342 * WAKE_TOKEN_MAX_SIZE was calculated for the maximum bus_clk_rate -
343 * 1MHz. The previous bus_clk_rate check ensures us that wake_token_sz
344 * will always be smaller than or equal to WAKE_TOKEN_MAX_SIZE.
346 i2c_priv->wake_token_sz = atmel_i2c_wake_token_sz(bus_clk_rate);
348 memset(i2c_priv->wake_token, 0, sizeof(i2c_priv->wake_token));
350 atomic_set(&i2c_priv->tfm_count, 0);
352 i2c_set_clientdata(client, i2c_priv);
354 ret = device_sanity_check(client);
355 if (ret)
356 return ret;
358 return 0;
360 EXPORT_SYMBOL(atmel_i2c_probe);
362 MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@microchip.com>");
363 MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
364 MODULE_LICENSE("GPL v2");