xref: /openbmc/linux/drivers/crypto/atmel-i2c.c (revision d58fa987)
1c34a3201SArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2c34a3201SArd Biesheuvel /*
3c34a3201SArd Biesheuvel  * Microchip / Atmel ECC (I2C) driver.
4c34a3201SArd Biesheuvel  *
5c34a3201SArd Biesheuvel  * Copyright (c) 2017, Microchip Technology Inc.
6c34a3201SArd Biesheuvel  * Author: Tudor Ambarus
7c34a3201SArd Biesheuvel  */
8c34a3201SArd Biesheuvel 
9c34a3201SArd Biesheuvel #include <linux/bitrev.h>
10c34a3201SArd Biesheuvel #include <linux/crc16.h>
11c34a3201SArd Biesheuvel #include <linux/delay.h>
12c34a3201SArd Biesheuvel #include <linux/device.h>
13c34a3201SArd Biesheuvel #include <linux/err.h>
14c34a3201SArd Biesheuvel #include <linux/errno.h>
15c34a3201SArd Biesheuvel #include <linux/i2c.h>
16c34a3201SArd Biesheuvel #include <linux/init.h>
17c34a3201SArd Biesheuvel #include <linux/kernel.h>
18c34a3201SArd Biesheuvel #include <linux/module.h>
19c34a3201SArd Biesheuvel #include <linux/scatterlist.h>
20c34a3201SArd Biesheuvel #include <linux/slab.h>
21c34a3201SArd Biesheuvel #include <linux/workqueue.h>
22c34a3201SArd Biesheuvel #include "atmel-i2c.h"
23c34a3201SArd Biesheuvel 
2425e9960cSYueHaibing static const struct {
2525e9960cSYueHaibing 	u8 value;
2625e9960cSYueHaibing 	const char *error_text;
2725e9960cSYueHaibing } error_list[] = {
2825e9960cSYueHaibing 	{ 0x01, "CheckMac or Verify miscompare" },
2925e9960cSYueHaibing 	{ 0x03, "Parse Error" },
3025e9960cSYueHaibing 	{ 0x05, "ECC Fault" },
3125e9960cSYueHaibing 	{ 0x0F, "Execution Error" },
3225e9960cSYueHaibing 	{ 0xEE, "Watchdog about to expire" },
3325e9960cSYueHaibing 	{ 0xFF, "CRC or other communication error" },
3425e9960cSYueHaibing };
3525e9960cSYueHaibing 
36c34a3201SArd Biesheuvel /**
37c34a3201SArd Biesheuvel  * atmel_i2c_checksum() - Generate 16-bit CRC as required by ATMEL ECC.
38c34a3201SArd Biesheuvel  * CRC16 verification of the count, opcode, param1, param2 and data bytes.
39c34a3201SArd Biesheuvel  * The checksum is saved in little-endian format in the least significant
40c34a3201SArd Biesheuvel  * two bytes of the command. CRC polynomial is 0x8005 and the initial register
41c34a3201SArd Biesheuvel  * value should be zero.
42c34a3201SArd Biesheuvel  *
43c34a3201SArd Biesheuvel  * @cmd : structure used for communicating with the device.
44c34a3201SArd Biesheuvel  */
atmel_i2c_checksum(struct atmel_i2c_cmd * cmd)45c34a3201SArd Biesheuvel static void atmel_i2c_checksum(struct atmel_i2c_cmd *cmd)
46c34a3201SArd Biesheuvel {
47c34a3201SArd Biesheuvel 	u8 *data = &cmd->count;
48c34a3201SArd Biesheuvel 	size_t len = cmd->count - CRC_SIZE;
4949d22167SHerbert Xu 	__le16 *__crc16 = (__le16 *)(data + len);
50c34a3201SArd Biesheuvel 
51c34a3201SArd Biesheuvel 	*__crc16 = cpu_to_le16(bitrev16(crc16(0, data, len)));
52c34a3201SArd Biesheuvel }
53c34a3201SArd Biesheuvel 
atmel_i2c_init_read_cmd(struct atmel_i2c_cmd * cmd)54c34a3201SArd Biesheuvel void atmel_i2c_init_read_cmd(struct atmel_i2c_cmd *cmd)
55c34a3201SArd Biesheuvel {
56c34a3201SArd Biesheuvel 	cmd->word_addr = COMMAND;
57c34a3201SArd Biesheuvel 	cmd->opcode = OPCODE_READ;
58c34a3201SArd Biesheuvel 	/*
59c34a3201SArd Biesheuvel 	 * Read the word from Configuration zone that contains the lock bytes
60c34a3201SArd Biesheuvel 	 * (UserExtra, Selector, LockValue, LockConfig).
61c34a3201SArd Biesheuvel 	 */
62b0f4f746SLukas Bulwahn 	cmd->param1 = CONFIGURATION_ZONE;
6349d22167SHerbert Xu 	cmd->param2 = cpu_to_le16(DEVICE_LOCK_ADDR);
64c34a3201SArd Biesheuvel 	cmd->count = READ_COUNT;
65c34a3201SArd Biesheuvel 
66c34a3201SArd Biesheuvel 	atmel_i2c_checksum(cmd);
67c34a3201SArd Biesheuvel 
68c34a3201SArd Biesheuvel 	cmd->msecs = MAX_EXEC_TIME_READ;
69c34a3201SArd Biesheuvel 	cmd->rxsize = READ_RSP_SIZE;
70c34a3201SArd Biesheuvel }
71c34a3201SArd Biesheuvel EXPORT_SYMBOL(atmel_i2c_init_read_cmd);
72c34a3201SArd Biesheuvel 
atmel_i2c_init_random_cmd(struct atmel_i2c_cmd * cmd)73da001fb6SArd Biesheuvel void atmel_i2c_init_random_cmd(struct atmel_i2c_cmd *cmd)
74da001fb6SArd Biesheuvel {
75da001fb6SArd Biesheuvel 	cmd->word_addr = COMMAND;
76da001fb6SArd Biesheuvel 	cmd->opcode = OPCODE_RANDOM;
77da001fb6SArd Biesheuvel 	cmd->param1 = 0;
78da001fb6SArd Biesheuvel 	cmd->param2 = 0;
79da001fb6SArd Biesheuvel 	cmd->count = RANDOM_COUNT;
80da001fb6SArd Biesheuvel 
81da001fb6SArd Biesheuvel 	atmel_i2c_checksum(cmd);
82da001fb6SArd Biesheuvel 
83da001fb6SArd Biesheuvel 	cmd->msecs = MAX_EXEC_TIME_RANDOM;
84da001fb6SArd Biesheuvel 	cmd->rxsize = RANDOM_RSP_SIZE;
85da001fb6SArd Biesheuvel }
86da001fb6SArd Biesheuvel EXPORT_SYMBOL(atmel_i2c_init_random_cmd);
87da001fb6SArd Biesheuvel 
atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd * cmd,u16 keyid)88c34a3201SArd Biesheuvel void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid)
89c34a3201SArd Biesheuvel {
90c34a3201SArd Biesheuvel 	cmd->word_addr = COMMAND;
91c34a3201SArd Biesheuvel 	cmd->count = GENKEY_COUNT;
92c34a3201SArd Biesheuvel 	cmd->opcode = OPCODE_GENKEY;
93c34a3201SArd Biesheuvel 	cmd->param1 = GENKEY_MODE_PRIVATE;
94c34a3201SArd Biesheuvel 	/* a random private key will be generated and stored in slot keyID */
95c34a3201SArd Biesheuvel 	cmd->param2 = cpu_to_le16(keyid);
96c34a3201SArd Biesheuvel 
97c34a3201SArd Biesheuvel 	atmel_i2c_checksum(cmd);
98c34a3201SArd Biesheuvel 
99c34a3201SArd Biesheuvel 	cmd->msecs = MAX_EXEC_TIME_GENKEY;
100c34a3201SArd Biesheuvel 	cmd->rxsize = GENKEY_RSP_SIZE;
101c34a3201SArd Biesheuvel }
102c34a3201SArd Biesheuvel EXPORT_SYMBOL(atmel_i2c_init_genkey_cmd);
103c34a3201SArd Biesheuvel 
atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd * cmd,struct scatterlist * pubkey)104c34a3201SArd Biesheuvel int atmel_i2c_init_ecdh_cmd(struct atmel_i2c_cmd *cmd,
105c34a3201SArd Biesheuvel 			    struct scatterlist *pubkey)
106c34a3201SArd Biesheuvel {
107c34a3201SArd Biesheuvel 	size_t copied;
108c34a3201SArd Biesheuvel 
109c34a3201SArd Biesheuvel 	cmd->word_addr = COMMAND;
110c34a3201SArd Biesheuvel 	cmd->count = ECDH_COUNT;
111c34a3201SArd Biesheuvel 	cmd->opcode = OPCODE_ECDH;
112c34a3201SArd Biesheuvel 	cmd->param1 = ECDH_PREFIX_MODE;
113c34a3201SArd Biesheuvel 	/* private key slot */
114c34a3201SArd Biesheuvel 	cmd->param2 = cpu_to_le16(DATA_SLOT_2);
115c34a3201SArd Biesheuvel 
116c34a3201SArd Biesheuvel 	/*
117c34a3201SArd Biesheuvel 	 * The device only supports NIST P256 ECC keys. The public key size will
118c34a3201SArd Biesheuvel 	 * always be the same. Use a macro for the key size to avoid unnecessary
119c34a3201SArd Biesheuvel 	 * computations.
120c34a3201SArd Biesheuvel 	 */
121c34a3201SArd Biesheuvel 	copied = sg_copy_to_buffer(pubkey,
122c34a3201SArd Biesheuvel 				   sg_nents_for_len(pubkey,
123c34a3201SArd Biesheuvel 						    ATMEL_ECC_PUBKEY_SIZE),
124c34a3201SArd Biesheuvel 				   cmd->data, ATMEL_ECC_PUBKEY_SIZE);
125c34a3201SArd Biesheuvel 	if (copied != ATMEL_ECC_PUBKEY_SIZE)
126c34a3201SArd Biesheuvel 		return -EINVAL;
127c34a3201SArd Biesheuvel 
128c34a3201SArd Biesheuvel 	atmel_i2c_checksum(cmd);
129c34a3201SArd Biesheuvel 
130c34a3201SArd Biesheuvel 	cmd->msecs = MAX_EXEC_TIME_ECDH;
131c34a3201SArd Biesheuvel 	cmd->rxsize = ECDH_RSP_SIZE;
132c34a3201SArd Biesheuvel 
133c34a3201SArd Biesheuvel 	return 0;
134c34a3201SArd Biesheuvel }
135c34a3201SArd Biesheuvel EXPORT_SYMBOL(atmel_i2c_init_ecdh_cmd);
136c34a3201SArd Biesheuvel 
137c34a3201SArd Biesheuvel /*
138c34a3201SArd Biesheuvel  * After wake and after execution of a command, there will be error, status, or
139c34a3201SArd Biesheuvel  * result bytes in the device's output register that can be retrieved by the
140c34a3201SArd Biesheuvel  * system. When the length of that group is four bytes, the codes returned are
141c34a3201SArd Biesheuvel  * detailed in error_list.
142c34a3201SArd Biesheuvel  */
atmel_i2c_status(struct device * dev,u8 * status)143c34a3201SArd Biesheuvel static int atmel_i2c_status(struct device *dev, u8 *status)
144c34a3201SArd Biesheuvel {
145c34a3201SArd Biesheuvel 	size_t err_list_len = ARRAY_SIZE(error_list);
146c34a3201SArd Biesheuvel 	int i;
147c34a3201SArd Biesheuvel 	u8 err_id = status[1];
148c34a3201SArd Biesheuvel 
149c34a3201SArd Biesheuvel 	if (*status != STATUS_SIZE)
150c34a3201SArd Biesheuvel 		return 0;
151c34a3201SArd Biesheuvel 
152c34a3201SArd Biesheuvel 	if (err_id == STATUS_WAKE_SUCCESSFUL || err_id == STATUS_NOERR)
153c34a3201SArd Biesheuvel 		return 0;
154c34a3201SArd Biesheuvel 
155c34a3201SArd Biesheuvel 	for (i = 0; i < err_list_len; i++)
156c34a3201SArd Biesheuvel 		if (error_list[i].value == err_id)
157c34a3201SArd Biesheuvel 			break;
158c34a3201SArd Biesheuvel 
159c34a3201SArd Biesheuvel 	/* if err_id is not in the error_list then ignore it */
160c34a3201SArd Biesheuvel 	if (i != err_list_len) {
161c34a3201SArd Biesheuvel 		dev_err(dev, "%02x: %s:\n", err_id, error_list[i].error_text);
162c34a3201SArd Biesheuvel 		return err_id;
163c34a3201SArd Biesheuvel 	}
164c34a3201SArd Biesheuvel 
165c34a3201SArd Biesheuvel 	return 0;
166c34a3201SArd Biesheuvel }
167c34a3201SArd Biesheuvel 
atmel_i2c_wakeup(struct i2c_client * client)168c34a3201SArd Biesheuvel static int atmel_i2c_wakeup(struct i2c_client *client)
169c34a3201SArd Biesheuvel {
170c34a3201SArd Biesheuvel 	struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
171c34a3201SArd Biesheuvel 	u8 status[STATUS_RSP_SIZE];
172c34a3201SArd Biesheuvel 	int ret;
173c34a3201SArd Biesheuvel 
174c34a3201SArd Biesheuvel 	/*
175c34a3201SArd Biesheuvel 	 * The device ignores any levels or transitions on the SCL pin when the
176c34a3201SArd Biesheuvel 	 * device is idle, asleep or during waking up. Don't check for error
177c34a3201SArd Biesheuvel 	 * when waking up the device.
178c34a3201SArd Biesheuvel 	 */
1792638268fSJianhui Zhao 	i2c_transfer_buffer_flags(client, i2c_priv->wake_token,
1802638268fSJianhui Zhao 				i2c_priv->wake_token_sz, I2C_M_IGNORE_NAK);
181c34a3201SArd Biesheuvel 
182c34a3201SArd Biesheuvel 	/*
183c34a3201SArd Biesheuvel 	 * Wait to wake the device. Typical execution times for ecdh and genkey
184c34a3201SArd Biesheuvel 	 * are around tens of milliseconds. Delta is chosen to 50 microseconds.
185c34a3201SArd Biesheuvel 	 */
186c34a3201SArd Biesheuvel 	usleep_range(TWHI_MIN, TWHI_MAX);
187c34a3201SArd Biesheuvel 
188c34a3201SArd Biesheuvel 	ret = i2c_master_recv(client, status, STATUS_SIZE);
189c34a3201SArd Biesheuvel 	if (ret < 0)
190c34a3201SArd Biesheuvel 		return ret;
191c34a3201SArd Biesheuvel 
192c34a3201SArd Biesheuvel 	return atmel_i2c_status(&client->dev, status);
193c34a3201SArd Biesheuvel }
194c34a3201SArd Biesheuvel 
atmel_i2c_sleep(struct i2c_client * client)195c34a3201SArd Biesheuvel static int atmel_i2c_sleep(struct i2c_client *client)
196c34a3201SArd Biesheuvel {
197c34a3201SArd Biesheuvel 	u8 sleep = SLEEP_TOKEN;
198c34a3201SArd Biesheuvel 
199c34a3201SArd Biesheuvel 	return i2c_master_send(client, &sleep, 1);
200c34a3201SArd Biesheuvel }
201c34a3201SArd Biesheuvel 
202c34a3201SArd Biesheuvel /*
203c34a3201SArd Biesheuvel  * atmel_i2c_send_receive() - send a command to the device and receive its
204c34a3201SArd Biesheuvel  *                            response.
205c34a3201SArd Biesheuvel  * @client: i2c client device
206c34a3201SArd Biesheuvel  * @cmd   : structure used to communicate with the device
207c34a3201SArd Biesheuvel  *
208c34a3201SArd Biesheuvel  * After the device receives a Wake token, a watchdog counter starts within the
209c34a3201SArd Biesheuvel  * device. After the watchdog timer expires, the device enters sleep mode
210c34a3201SArd Biesheuvel  * regardless of whether some I/O transmission or command execution is in
211c34a3201SArd Biesheuvel  * progress. If a command is attempted when insufficient time remains prior to
212c34a3201SArd Biesheuvel  * watchdog timer execution, the device will return the watchdog timeout error
213c34a3201SArd Biesheuvel  * code without attempting to execute the command. There is no way to reset the
214c34a3201SArd Biesheuvel  * counter other than to put the device into sleep or idle mode and then
215c34a3201SArd Biesheuvel  * wake it up again.
216c34a3201SArd Biesheuvel  */
atmel_i2c_send_receive(struct i2c_client * client,struct atmel_i2c_cmd * cmd)217c34a3201SArd Biesheuvel int atmel_i2c_send_receive(struct i2c_client *client, struct atmel_i2c_cmd *cmd)
218c34a3201SArd Biesheuvel {
219c34a3201SArd Biesheuvel 	struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
220c34a3201SArd Biesheuvel 	int ret;
221c34a3201SArd Biesheuvel 
222c34a3201SArd Biesheuvel 	mutex_lock(&i2c_priv->lock);
223c34a3201SArd Biesheuvel 
224c34a3201SArd Biesheuvel 	ret = atmel_i2c_wakeup(client);
225c34a3201SArd Biesheuvel 	if (ret)
226c34a3201SArd Biesheuvel 		goto err;
227c34a3201SArd Biesheuvel 
228c34a3201SArd Biesheuvel 	/* send the command */
229c34a3201SArd Biesheuvel 	ret = i2c_master_send(client, (u8 *)cmd, cmd->count + WORD_ADDR_SIZE);
230c34a3201SArd Biesheuvel 	if (ret < 0)
231c34a3201SArd Biesheuvel 		goto err;
232c34a3201SArd Biesheuvel 
233c34a3201SArd Biesheuvel 	/* delay the appropriate amount of time for command to execute */
234c34a3201SArd Biesheuvel 	msleep(cmd->msecs);
235c34a3201SArd Biesheuvel 
236c34a3201SArd Biesheuvel 	/* receive the response */
237c34a3201SArd Biesheuvel 	ret = i2c_master_recv(client, cmd->data, cmd->rxsize);
238c34a3201SArd Biesheuvel 	if (ret < 0)
239c34a3201SArd Biesheuvel 		goto err;
240c34a3201SArd Biesheuvel 
241c34a3201SArd Biesheuvel 	/* put the device into low-power mode */
242c34a3201SArd Biesheuvel 	ret = atmel_i2c_sleep(client);
243c34a3201SArd Biesheuvel 	if (ret < 0)
244c34a3201SArd Biesheuvel 		goto err;
245c34a3201SArd Biesheuvel 
246c34a3201SArd Biesheuvel 	mutex_unlock(&i2c_priv->lock);
247c34a3201SArd Biesheuvel 	return atmel_i2c_status(&client->dev, cmd->data);
248c34a3201SArd Biesheuvel err:
249c34a3201SArd Biesheuvel 	mutex_unlock(&i2c_priv->lock);
250c34a3201SArd Biesheuvel 	return ret;
251c34a3201SArd Biesheuvel }
252c34a3201SArd Biesheuvel EXPORT_SYMBOL(atmel_i2c_send_receive);
253c34a3201SArd Biesheuvel 
atmel_i2c_work_handler(struct work_struct * work)254c34a3201SArd Biesheuvel static void atmel_i2c_work_handler(struct work_struct *work)
255c34a3201SArd Biesheuvel {
256c34a3201SArd Biesheuvel 	struct atmel_i2c_work_data *work_data =
257c34a3201SArd Biesheuvel 			container_of(work, struct atmel_i2c_work_data, work);
258c34a3201SArd Biesheuvel 	struct atmel_i2c_cmd *cmd = &work_data->cmd;
259c34a3201SArd Biesheuvel 	struct i2c_client *client = work_data->client;
260c34a3201SArd Biesheuvel 	int status;
261c34a3201SArd Biesheuvel 
262c34a3201SArd Biesheuvel 	status = atmel_i2c_send_receive(client, cmd);
263c34a3201SArd Biesheuvel 	work_data->cbk(work_data, work_data->areq, status);
264c34a3201SArd Biesheuvel }
265c34a3201SArd Biesheuvel 
2660a2f4b57STetsuo Handa static struct workqueue_struct *atmel_wq;
2670a2f4b57STetsuo Handa 
atmel_i2c_enqueue(struct atmel_i2c_work_data * work_data,void (* cbk)(struct atmel_i2c_work_data * work_data,void * areq,int status),void * areq)268c34a3201SArd Biesheuvel void atmel_i2c_enqueue(struct atmel_i2c_work_data *work_data,
269c34a3201SArd Biesheuvel 		       void (*cbk)(struct atmel_i2c_work_data *work_data,
270c34a3201SArd Biesheuvel 				   void *areq, int status),
271c34a3201SArd Biesheuvel 		       void *areq)
272c34a3201SArd Biesheuvel {
273c34a3201SArd Biesheuvel 	work_data->cbk = (void *)cbk;
274c34a3201SArd Biesheuvel 	work_data->areq = areq;
275c34a3201SArd Biesheuvel 
276c34a3201SArd Biesheuvel 	INIT_WORK(&work_data->work, atmel_i2c_work_handler);
2770a2f4b57STetsuo Handa 	queue_work(atmel_wq, &work_data->work);
278c34a3201SArd Biesheuvel }
279c34a3201SArd Biesheuvel EXPORT_SYMBOL(atmel_i2c_enqueue);
280c34a3201SArd Biesheuvel 
atmel_i2c_flush_queue(void)2810a2f4b57STetsuo Handa void atmel_i2c_flush_queue(void)
2820a2f4b57STetsuo Handa {
2830a2f4b57STetsuo Handa 	flush_workqueue(atmel_wq);
2840a2f4b57STetsuo Handa }
2850a2f4b57STetsuo Handa EXPORT_SYMBOL(atmel_i2c_flush_queue);
2860a2f4b57STetsuo Handa 
atmel_i2c_wake_token_sz(u32 bus_clk_rate)287c34a3201SArd Biesheuvel static inline size_t atmel_i2c_wake_token_sz(u32 bus_clk_rate)
288c34a3201SArd Biesheuvel {
289c34a3201SArd Biesheuvel 	u32 no_of_bits = DIV_ROUND_UP(TWLO_USEC * bus_clk_rate, USEC_PER_SEC);
290c34a3201SArd Biesheuvel 
291c34a3201SArd Biesheuvel 	/* return the size of the wake_token in bytes */
292c34a3201SArd Biesheuvel 	return DIV_ROUND_UP(no_of_bits, 8);
293c34a3201SArd Biesheuvel }
294c34a3201SArd Biesheuvel 
device_sanity_check(struct i2c_client * client)295c34a3201SArd Biesheuvel static int device_sanity_check(struct i2c_client *client)
296c34a3201SArd Biesheuvel {
297c34a3201SArd Biesheuvel 	struct atmel_i2c_cmd *cmd;
298c34a3201SArd Biesheuvel 	int ret;
299c34a3201SArd Biesheuvel 
300c34a3201SArd Biesheuvel 	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
301c34a3201SArd Biesheuvel 	if (!cmd)
302c34a3201SArd Biesheuvel 		return -ENOMEM;
303c34a3201SArd Biesheuvel 
304c34a3201SArd Biesheuvel 	atmel_i2c_init_read_cmd(cmd);
305c34a3201SArd Biesheuvel 
306c34a3201SArd Biesheuvel 	ret = atmel_i2c_send_receive(client, cmd);
307c34a3201SArd Biesheuvel 	if (ret)
308c34a3201SArd Biesheuvel 		goto free_cmd;
309c34a3201SArd Biesheuvel 
310c34a3201SArd Biesheuvel 	/*
311c34a3201SArd Biesheuvel 	 * It is vital that the Configuration, Data and OTP zones be locked
312c34a3201SArd Biesheuvel 	 * prior to release into the field of the system containing the device.
313c34a3201SArd Biesheuvel 	 * Failure to lock these zones may permit modification of any secret
314c34a3201SArd Biesheuvel 	 * keys and may lead to other security problems.
315c34a3201SArd Biesheuvel 	 */
316c34a3201SArd Biesheuvel 	if (cmd->data[LOCK_CONFIG_IDX] || cmd->data[LOCK_VALUE_IDX]) {
317c34a3201SArd Biesheuvel 		dev_err(&client->dev, "Configuration or Data and OTP zones are unlocked!\n");
318c34a3201SArd Biesheuvel 		ret = -ENOTSUPP;
319c34a3201SArd Biesheuvel 	}
320c34a3201SArd Biesheuvel 
321c34a3201SArd Biesheuvel 	/* fall through */
322c34a3201SArd Biesheuvel free_cmd:
323c34a3201SArd Biesheuvel 	kfree(cmd);
324c34a3201SArd Biesheuvel 	return ret;
325c34a3201SArd Biesheuvel }
326c34a3201SArd Biesheuvel 
atmel_i2c_probe(struct i2c_client * client)327*d58fa987SUwe Kleine-König int atmel_i2c_probe(struct i2c_client *client)
328c34a3201SArd Biesheuvel {
329c34a3201SArd Biesheuvel 	struct atmel_i2c_client_priv *i2c_priv;
330c34a3201SArd Biesheuvel 	struct device *dev = &client->dev;
331c34a3201SArd Biesheuvel 	int ret;
332c34a3201SArd Biesheuvel 	u32 bus_clk_rate;
333c34a3201SArd Biesheuvel 
334c34a3201SArd Biesheuvel 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
335c34a3201SArd Biesheuvel 		dev_err(dev, "I2C_FUNC_I2C not supported\n");
336c34a3201SArd Biesheuvel 		return -ENODEV;
337c34a3201SArd Biesheuvel 	}
338c34a3201SArd Biesheuvel 
339c34a3201SArd Biesheuvel 	bus_clk_rate = i2c_acpi_find_bus_speed(&client->adapter->dev);
340c34a3201SArd Biesheuvel 	if (!bus_clk_rate) {
341c34a3201SArd Biesheuvel 		ret = device_property_read_u32(&client->adapter->dev,
342c34a3201SArd Biesheuvel 					       "clock-frequency", &bus_clk_rate);
343c34a3201SArd Biesheuvel 		if (ret) {
344c34a3201SArd Biesheuvel 			dev_err(dev, "failed to read clock-frequency property\n");
345c34a3201SArd Biesheuvel 			return ret;
346c34a3201SArd Biesheuvel 		}
347c34a3201SArd Biesheuvel 	}
348c34a3201SArd Biesheuvel 
349c34a3201SArd Biesheuvel 	if (bus_clk_rate > 1000000L) {
3503eb75fc7SKai Ye 		dev_err(dev, "%u exceeds maximum supported clock frequency (1MHz)\n",
351c34a3201SArd Biesheuvel 			bus_clk_rate);
352c34a3201SArd Biesheuvel 		return -EINVAL;
353c34a3201SArd Biesheuvel 	}
354c34a3201SArd Biesheuvel 
355c34a3201SArd Biesheuvel 	i2c_priv = devm_kmalloc(dev, sizeof(*i2c_priv), GFP_KERNEL);
356c34a3201SArd Biesheuvel 	if (!i2c_priv)
357c34a3201SArd Biesheuvel 		return -ENOMEM;
358c34a3201SArd Biesheuvel 
359c34a3201SArd Biesheuvel 	i2c_priv->client = client;
360c34a3201SArd Biesheuvel 	mutex_init(&i2c_priv->lock);
361c34a3201SArd Biesheuvel 
362c34a3201SArd Biesheuvel 	/*
363c34a3201SArd Biesheuvel 	 * WAKE_TOKEN_MAX_SIZE was calculated for the maximum bus_clk_rate -
364c34a3201SArd Biesheuvel 	 * 1MHz. The previous bus_clk_rate check ensures us that wake_token_sz
365c34a3201SArd Biesheuvel 	 * will always be smaller than or equal to WAKE_TOKEN_MAX_SIZE.
366c34a3201SArd Biesheuvel 	 */
367c34a3201SArd Biesheuvel 	i2c_priv->wake_token_sz = atmel_i2c_wake_token_sz(bus_clk_rate);
368c34a3201SArd Biesheuvel 
369c34a3201SArd Biesheuvel 	memset(i2c_priv->wake_token, 0, sizeof(i2c_priv->wake_token));
370c34a3201SArd Biesheuvel 
371c34a3201SArd Biesheuvel 	atomic_set(&i2c_priv->tfm_count, 0);
372c34a3201SArd Biesheuvel 
373c34a3201SArd Biesheuvel 	i2c_set_clientdata(client, i2c_priv);
374c34a3201SArd Biesheuvel 
37559f71498SUwe Kleine-König 	return device_sanity_check(client);
376c34a3201SArd Biesheuvel }
377c34a3201SArd Biesheuvel EXPORT_SYMBOL(atmel_i2c_probe);
378c34a3201SArd Biesheuvel 
atmel_i2c_init(void)3790a2f4b57STetsuo Handa static int __init atmel_i2c_init(void)
3800a2f4b57STetsuo Handa {
3810a2f4b57STetsuo Handa 	atmel_wq = alloc_workqueue("atmel_wq", 0, 0);
3820a2f4b57STetsuo Handa 	return atmel_wq ? 0 : -ENOMEM;
3830a2f4b57STetsuo Handa }
3840a2f4b57STetsuo Handa 
atmel_i2c_exit(void)3850a2f4b57STetsuo Handa static void __exit atmel_i2c_exit(void)
3860a2f4b57STetsuo Handa {
3870a2f4b57STetsuo Handa 	destroy_workqueue(atmel_wq);
3880a2f4b57STetsuo Handa }
3890a2f4b57STetsuo Handa 
3900a2f4b57STetsuo Handa module_init(atmel_i2c_init);
3910a2f4b57STetsuo Handa module_exit(atmel_i2c_exit);
3920a2f4b57STetsuo Handa 
393c34a3201SArd Biesheuvel MODULE_AUTHOR("Tudor Ambarus");
394c34a3201SArd Biesheuvel MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
395c34a3201SArd Biesheuvel MODULE_LICENSE("GPL v2");
396