atmel-i2c.c 11 KB

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