Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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