tpm_i2c_infineon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright (C) 2012,2013 Infineon Technologies
  3. *
  4. * Authors:
  5. * Peter Huewe <peter.huewe@infineon.com>
  6. *
  7. * Device driver for TCG/TCPA TPM (trusted platform module).
  8. * Specifications at www.trustedcomputinggroup.org
  9. *
  10. * This device driver implements the TPM interface as defined in
  11. * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
  12. * Infineon I2C Protocol Stack Specification v0.20.
  13. *
  14. * It is based on the original tpm_tis device driver from Leendert van
  15. * Dorn and Kyleen Hall.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation, version 2 of the
  20. * License.
  21. *
  22. *
  23. */
  24. #include <linux/init.h>
  25. #include <linux/i2c.h>
  26. #include <linux/module.h>
  27. #include <linux/wait.h>
  28. #include "tpm.h"
  29. /* max. buffer size supported by our TPM */
  30. #define TPM_BUFSIZE 1260
  31. /* max. number of iterations after I2C NAK */
  32. #define MAX_COUNT 3
  33. #define SLEEP_DURATION_LOW 55
  34. #define SLEEP_DURATION_HI 65
  35. /* max. number of iterations after I2C NAK for 'long' commands
  36. * we need this especially for sending TPM_READY, since the cleanup after the
  37. * transtion to the ready state may take some time, but it is unpredictable
  38. * how long it will take.
  39. */
  40. #define MAX_COUNT_LONG 50
  41. #define SLEEP_DURATION_LONG_LOW 200
  42. #define SLEEP_DURATION_LONG_HI 220
  43. /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */
  44. #define SLEEP_DURATION_RESET_LOW 2400
  45. #define SLEEP_DURATION_RESET_HI 2600
  46. /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */
  47. #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000)
  48. #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000)
  49. /* expected value for DIDVID register */
  50. #define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L
  51. #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
  52. enum i2c_chip_type {
  53. SLB9635,
  54. SLB9645,
  55. UNKNOWN,
  56. };
  57. /* Structure to store I2C TPM specific stuff */
  58. struct tpm_inf_dev {
  59. struct i2c_client *client;
  60. u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */
  61. struct tpm_chip *chip;
  62. enum i2c_chip_type chip_type;
  63. };
  64. static struct tpm_inf_dev tpm_dev;
  65. /*
  66. * iic_tpm_read() - read from TPM register
  67. * @addr: register address to read from
  68. * @buffer: provided by caller
  69. * @len: number of bytes to read
  70. *
  71. * Read len bytes from TPM register and put them into
  72. * buffer (little-endian format, i.e. first byte is put into buffer[0]).
  73. *
  74. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  75. * values have to be swapped.
  76. *
  77. * NOTE: We can't unfortunately use the combined read/write functions
  78. * provided by the i2c core as the TPM currently does not support the
  79. * repeated start condition and due to it's special requirements.
  80. * The i2c_smbus* functions do not work for this chip.
  81. *
  82. * Return -EIO on error, 0 on success.
  83. */
  84. static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
  85. {
  86. struct i2c_msg msg1 = {
  87. .addr = tpm_dev.client->addr,
  88. .len = 1,
  89. .buf = &addr
  90. };
  91. struct i2c_msg msg2 = {
  92. .addr = tpm_dev.client->addr,
  93. .flags = I2C_M_RD,
  94. .len = len,
  95. .buf = buffer
  96. };
  97. struct i2c_msg msgs[] = {msg1, msg2};
  98. int rc = 0;
  99. int count;
  100. /* Lock the adapter for the duration of the whole sequence. */
  101. if (!tpm_dev.client->adapter->algo->master_xfer)
  102. return -EOPNOTSUPP;
  103. i2c_lock_adapter(tpm_dev.client->adapter);
  104. if (tpm_dev.chip_type == SLB9645) {
  105. /* use a combined read for newer chips
  106. * unfortunately the smbus functions are not suitable due to
  107. * the 32 byte limit of the smbus.
  108. * retries should usually not be needed, but are kept just to
  109. * be on the safe side.
  110. */
  111. for (count = 0; count < MAX_COUNT; count++) {
  112. rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2);
  113. if (rc > 0)
  114. break; /* break here to skip sleep */
  115. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  116. }
  117. } else {
  118. /* slb9635 protocol should work in all cases */
  119. for (count = 0; count < MAX_COUNT; count++) {
  120. rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
  121. if (rc > 0)
  122. break; /* break here to skip sleep */
  123. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  124. }
  125. if (rc <= 0)
  126. goto out;
  127. /* After the TPM has successfully received the register address
  128. * it needs some time, thus we're sleeping here again, before
  129. * retrieving the data
  130. */
  131. for (count = 0; count < MAX_COUNT; count++) {
  132. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  133. rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1);
  134. if (rc > 0)
  135. break;
  136. }
  137. }
  138. out:
  139. i2c_unlock_adapter(tpm_dev.client->adapter);
  140. /* take care of 'guard time' */
  141. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  142. /* __i2c_transfer returns the number of successfully transferred
  143. * messages.
  144. * So rc should be greater than 0 here otherwise we have an error.
  145. */
  146. if (rc <= 0)
  147. return -EIO;
  148. return 0;
  149. }
  150. static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
  151. unsigned int sleep_low,
  152. unsigned int sleep_hi, u8 max_count)
  153. {
  154. int rc = -EIO;
  155. int count;
  156. struct i2c_msg msg1 = {
  157. .addr = tpm_dev.client->addr,
  158. .len = len + 1,
  159. .buf = tpm_dev.buf
  160. };
  161. if (len > TPM_BUFSIZE)
  162. return -EINVAL;
  163. if (!tpm_dev.client->adapter->algo->master_xfer)
  164. return -EOPNOTSUPP;
  165. i2c_lock_adapter(tpm_dev.client->adapter);
  166. /* prepend the 'register address' to the buffer */
  167. tpm_dev.buf[0] = addr;
  168. memcpy(&(tpm_dev.buf[1]), buffer, len);
  169. /*
  170. * NOTE: We have to use these special mechanisms here and unfortunately
  171. * cannot rely on the standard behavior of i2c_transfer.
  172. * Even for newer chips the smbus functions are not
  173. * suitable due to the 32 byte limit of the smbus.
  174. */
  175. for (count = 0; count < max_count; count++) {
  176. rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
  177. if (rc > 0)
  178. break;
  179. usleep_range(sleep_low, sleep_hi);
  180. }
  181. i2c_unlock_adapter(tpm_dev.client->adapter);
  182. /* take care of 'guard time' */
  183. usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
  184. /* __i2c_transfer returns the number of successfully transferred
  185. * messages.
  186. * So rc should be greater than 0 here otherwise we have an error.
  187. */
  188. if (rc <= 0)
  189. return -EIO;
  190. return 0;
  191. }
  192. /*
  193. * iic_tpm_write() - write to TPM register
  194. * @addr: register address to write to
  195. * @buffer: containing data to be written
  196. * @len: number of bytes to write
  197. *
  198. * Write len bytes from provided buffer to TPM register (little
  199. * endian format, i.e. buffer[0] is written as first byte).
  200. *
  201. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  202. * values have to be swapped.
  203. *
  204. * NOTE: use this function instead of the iic_tpm_write_generic function.
  205. *
  206. * Return -EIO on error, 0 on success
  207. */
  208. static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
  209. {
  210. return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW,
  211. SLEEP_DURATION_HI, MAX_COUNT);
  212. }
  213. /*
  214. * This function is needed especially for the cleanup situation after
  215. * sending TPM_READY
  216. * */
  217. static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
  218. {
  219. return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW,
  220. SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG);
  221. }
  222. enum tis_access {
  223. TPM_ACCESS_VALID = 0x80,
  224. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  225. TPM_ACCESS_REQUEST_PENDING = 0x04,
  226. TPM_ACCESS_REQUEST_USE = 0x02,
  227. };
  228. enum tis_status {
  229. TPM_STS_VALID = 0x80,
  230. TPM_STS_COMMAND_READY = 0x40,
  231. TPM_STS_GO = 0x20,
  232. TPM_STS_DATA_AVAIL = 0x10,
  233. TPM_STS_DATA_EXPECT = 0x08,
  234. };
  235. enum tis_defaults {
  236. TIS_SHORT_TIMEOUT = 750, /* ms */
  237. TIS_LONG_TIMEOUT = 2000, /* 2 sec */
  238. };
  239. #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
  240. #define TPM_STS(l) (0x0001 | ((l) << 4))
  241. #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
  242. #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
  243. static int check_locality(struct tpm_chip *chip, int loc)
  244. {
  245. u8 buf;
  246. int rc;
  247. rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
  248. if (rc < 0)
  249. return rc;
  250. if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  251. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
  252. chip->vendor.locality = loc;
  253. return loc;
  254. }
  255. return -EIO;
  256. }
  257. /* implementation similar to tpm_tis */
  258. static void release_locality(struct tpm_chip *chip, int loc, int force)
  259. {
  260. u8 buf;
  261. if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
  262. return;
  263. if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
  264. (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
  265. buf = TPM_ACCESS_ACTIVE_LOCALITY;
  266. iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
  267. }
  268. }
  269. static int request_locality(struct tpm_chip *chip, int loc)
  270. {
  271. unsigned long stop;
  272. u8 buf = TPM_ACCESS_REQUEST_USE;
  273. if (check_locality(chip, loc) >= 0)
  274. return loc;
  275. iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
  276. /* wait for burstcount */
  277. stop = jiffies + chip->vendor.timeout_a;
  278. do {
  279. if (check_locality(chip, loc) >= 0)
  280. return loc;
  281. usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
  282. } while (time_before(jiffies, stop));
  283. return -ETIME;
  284. }
  285. static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
  286. {
  287. /* NOTE: since I2C read may fail, return 0 in this case --> time-out */
  288. u8 buf = 0xFF;
  289. u8 i = 0;
  290. do {
  291. if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
  292. return 0;
  293. i++;
  294. /* if locallity is set STS should not be 0xFF */
  295. } while ((buf == 0xFF) && i < 10);
  296. return buf;
  297. }
  298. static void tpm_tis_i2c_ready(struct tpm_chip *chip)
  299. {
  300. /* this causes the current command to be aborted */
  301. u8 buf = TPM_STS_COMMAND_READY;
  302. iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
  303. }
  304. static ssize_t get_burstcount(struct tpm_chip *chip)
  305. {
  306. unsigned long stop;
  307. ssize_t burstcnt;
  308. u8 buf[3];
  309. /* wait for burstcount */
  310. /* which timeout value, spec has 2 answers (c & d) */
  311. stop = jiffies + chip->vendor.timeout_d;
  312. do {
  313. /* Note: STS is little endian */
  314. if (iic_tpm_read(TPM_STS(chip->vendor.locality)+1, buf, 3) < 0)
  315. burstcnt = 0;
  316. else
  317. burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
  318. if (burstcnt)
  319. return burstcnt;
  320. usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
  321. } while (time_before(jiffies, stop));
  322. return -EBUSY;
  323. }
  324. static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  325. int *status)
  326. {
  327. unsigned long stop;
  328. /* check current status */
  329. *status = tpm_tis_i2c_status(chip);
  330. if ((*status != 0xFF) && (*status & mask) == mask)
  331. return 0;
  332. stop = jiffies + timeout;
  333. do {
  334. /* since we just checked the status, give the TPM some time */
  335. usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
  336. *status = tpm_tis_i2c_status(chip);
  337. if ((*status & mask) == mask)
  338. return 0;
  339. } while (time_before(jiffies, stop));
  340. return -ETIME;
  341. }
  342. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  343. {
  344. size_t size = 0;
  345. ssize_t burstcnt;
  346. u8 retries = 0;
  347. int rc;
  348. while (size < count) {
  349. burstcnt = get_burstcount(chip);
  350. /* burstcnt < 0 = TPM is busy */
  351. if (burstcnt < 0)
  352. return burstcnt;
  353. /* limit received data to max. left */
  354. if (burstcnt > (count - size))
  355. burstcnt = count - size;
  356. rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
  357. &(buf[size]), burstcnt);
  358. if (rc == 0)
  359. size += burstcnt;
  360. else if (rc < 0)
  361. retries++;
  362. /* avoid endless loop in case of broken HW */
  363. if (retries > MAX_COUNT_LONG)
  364. return -EIO;
  365. }
  366. return size;
  367. }
  368. static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  369. {
  370. int size = 0;
  371. int expected, status;
  372. if (count < TPM_HEADER_SIZE) {
  373. size = -EIO;
  374. goto out;
  375. }
  376. /* read first 10 bytes, including tag, paramsize, and result */
  377. size = recv_data(chip, buf, TPM_HEADER_SIZE);
  378. if (size < TPM_HEADER_SIZE) {
  379. dev_err(chip->dev, "Unable to read header\n");
  380. goto out;
  381. }
  382. expected = be32_to_cpu(*(__be32 *)(buf + 2));
  383. if ((size_t) expected > count) {
  384. size = -EIO;
  385. goto out;
  386. }
  387. size += recv_data(chip, &buf[TPM_HEADER_SIZE],
  388. expected - TPM_HEADER_SIZE);
  389. if (size < expected) {
  390. dev_err(chip->dev, "Unable to read remainder of result\n");
  391. size = -ETIME;
  392. goto out;
  393. }
  394. wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
  395. if (status & TPM_STS_DATA_AVAIL) { /* retry? */
  396. dev_err(chip->dev, "Error left over data\n");
  397. size = -EIO;
  398. goto out;
  399. }
  400. out:
  401. tpm_tis_i2c_ready(chip);
  402. /* The TPM needs some time to clean up here,
  403. * so we sleep rather than keeping the bus busy
  404. */
  405. usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
  406. release_locality(chip, chip->vendor.locality, 0);
  407. return size;
  408. }
  409. static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
  410. {
  411. int rc, status;
  412. ssize_t burstcnt;
  413. size_t count = 0;
  414. u8 retries = 0;
  415. u8 sts = TPM_STS_GO;
  416. if (len > TPM_BUFSIZE)
  417. return -E2BIG; /* command is too long for our tpm, sorry */
  418. if (request_locality(chip, 0) < 0)
  419. return -EBUSY;
  420. status = tpm_tis_i2c_status(chip);
  421. if ((status & TPM_STS_COMMAND_READY) == 0) {
  422. tpm_tis_i2c_ready(chip);
  423. if (wait_for_stat
  424. (chip, TPM_STS_COMMAND_READY,
  425. chip->vendor.timeout_b, &status) < 0) {
  426. rc = -ETIME;
  427. goto out_err;
  428. }
  429. }
  430. while (count < len - 1) {
  431. burstcnt = get_burstcount(chip);
  432. /* burstcnt < 0 = TPM is busy */
  433. if (burstcnt < 0)
  434. return burstcnt;
  435. if (burstcnt > (len - 1 - count))
  436. burstcnt = len - 1 - count;
  437. rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
  438. &(buf[count]), burstcnt);
  439. if (rc == 0)
  440. count += burstcnt;
  441. else if (rc < 0)
  442. retries++;
  443. /* avoid endless loop in case of broken HW */
  444. if (retries > MAX_COUNT_LONG) {
  445. rc = -EIO;
  446. goto out_err;
  447. }
  448. wait_for_stat(chip, TPM_STS_VALID,
  449. chip->vendor.timeout_c, &status);
  450. if ((status & TPM_STS_DATA_EXPECT) == 0) {
  451. rc = -EIO;
  452. goto out_err;
  453. }
  454. }
  455. /* write last byte */
  456. iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
  457. wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
  458. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  459. rc = -EIO;
  460. goto out_err;
  461. }
  462. /* go and do it */
  463. iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
  464. return len;
  465. out_err:
  466. tpm_tis_i2c_ready(chip);
  467. /* The TPM needs some time to clean up here,
  468. * so we sleep rather than keeping the bus busy
  469. */
  470. usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
  471. release_locality(chip, chip->vendor.locality, 0);
  472. return rc;
  473. }
  474. static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
  475. {
  476. return (status == TPM_STS_COMMAND_READY);
  477. }
  478. static const struct file_operations tis_ops = {
  479. .owner = THIS_MODULE,
  480. .llseek = no_llseek,
  481. .open = tpm_open,
  482. .read = tpm_read,
  483. .write = tpm_write,
  484. .release = tpm_release,
  485. };
  486. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  487. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  488. static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
  489. static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
  490. static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
  491. static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
  492. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
  493. static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
  494. static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
  495. static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
  496. static struct attribute *tis_attrs[] = {
  497. &dev_attr_pubek.attr,
  498. &dev_attr_pcrs.attr,
  499. &dev_attr_enabled.attr,
  500. &dev_attr_active.attr,
  501. &dev_attr_owned.attr,
  502. &dev_attr_temp_deactivated.attr,
  503. &dev_attr_caps.attr,
  504. &dev_attr_cancel.attr,
  505. &dev_attr_durations.attr,
  506. &dev_attr_timeouts.attr,
  507. NULL,
  508. };
  509. static struct attribute_group tis_attr_grp = {
  510. .attrs = tis_attrs
  511. };
  512. static struct tpm_vendor_specific tpm_tis_i2c = {
  513. .status = tpm_tis_i2c_status,
  514. .recv = tpm_tis_i2c_recv,
  515. .send = tpm_tis_i2c_send,
  516. .cancel = tpm_tis_i2c_ready,
  517. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  518. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  519. .req_canceled = tpm_tis_i2c_req_canceled,
  520. .attr_group = &tis_attr_grp,
  521. .miscdev.fops = &tis_ops,
  522. };
  523. static int tpm_tis_i2c_init(struct device *dev)
  524. {
  525. u32 vendor;
  526. int rc = 0;
  527. struct tpm_chip *chip;
  528. chip = tpm_register_hardware(dev, &tpm_tis_i2c);
  529. if (!chip) {
  530. dev_err(dev, "could not register hardware\n");
  531. rc = -ENODEV;
  532. goto out_err;
  533. }
  534. /* Disable interrupts */
  535. chip->vendor.irq = 0;
  536. /* Default timeouts */
  537. chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  538. chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  539. chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  540. chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  541. if (request_locality(chip, 0) != 0) {
  542. dev_err(dev, "could not request locality\n");
  543. rc = -ENODEV;
  544. goto out_vendor;
  545. }
  546. /* read four bytes from DID_VID register */
  547. if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) {
  548. dev_err(dev, "could not read vendor id\n");
  549. rc = -EIO;
  550. goto out_release;
  551. }
  552. if (vendor == TPM_TIS_I2C_DID_VID_9645) {
  553. tpm_dev.chip_type = SLB9645;
  554. } else if (vendor == TPM_TIS_I2C_DID_VID_9635) {
  555. tpm_dev.chip_type = SLB9635;
  556. } else {
  557. dev_err(dev, "vendor id did not match! ID was %08x\n", vendor);
  558. rc = -ENODEV;
  559. goto out_release;
  560. }
  561. dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
  562. INIT_LIST_HEAD(&chip->vendor.list);
  563. tpm_dev.chip = chip;
  564. tpm_get_timeouts(chip);
  565. tpm_do_selftest(chip);
  566. return 0;
  567. out_release:
  568. release_locality(chip, chip->vendor.locality, 1);
  569. out_vendor:
  570. /* close file handles */
  571. tpm_dev_vendor_release(chip);
  572. /* remove hardware */
  573. tpm_remove_hardware(chip->dev);
  574. /* reset these pointers, otherwise we oops */
  575. chip->dev->release = NULL;
  576. chip->release = NULL;
  577. tpm_dev.client = NULL;
  578. dev_set_drvdata(chip->dev, chip);
  579. out_err:
  580. return rc;
  581. }
  582. static const struct i2c_device_id tpm_tis_i2c_table[] = {
  583. {"tpm_i2c_infineon", 0},
  584. {"slb9635tt", 0},
  585. {"slb9645tt", 1},
  586. {},
  587. };
  588. MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table);
  589. #ifdef CONFIG_OF
  590. static const struct of_device_id tpm_tis_i2c_of_match[] = {
  591. {
  592. .name = "tpm_i2c_infineon",
  593. .type = "tpm",
  594. .compatible = "infineon,tpm_i2c_infineon",
  595. .data = (void *)0
  596. },
  597. {
  598. .name = "slb9635tt",
  599. .type = "tpm",
  600. .compatible = "infineon,slb9635tt",
  601. .data = (void *)0
  602. },
  603. {
  604. .name = "slb9645tt",
  605. .type = "tpm",
  606. .compatible = "infineon,slb9645tt",
  607. .data = (void *)1
  608. },
  609. {},
  610. };
  611. MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match);
  612. #endif
  613. static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume);
  614. static int tpm_tis_i2c_probe(struct i2c_client *client,
  615. const struct i2c_device_id *id)
  616. {
  617. int rc;
  618. struct device *dev = &(client->dev);
  619. if (tpm_dev.client != NULL) {
  620. dev_err(dev, "This driver only supports one client at a time\n");
  621. return -EBUSY; /* We only support one client */
  622. }
  623. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  624. dev_err(dev, "no algorithms associated to the i2c bus\n");
  625. return -ENODEV;
  626. }
  627. tpm_dev.client = client;
  628. rc = tpm_tis_i2c_init(&client->dev);
  629. if (rc != 0) {
  630. tpm_dev.client = NULL;
  631. rc = -ENODEV;
  632. }
  633. return rc;
  634. }
  635. static int tpm_tis_i2c_remove(struct i2c_client *client)
  636. {
  637. struct tpm_chip *chip = tpm_dev.chip;
  638. release_locality(chip, chip->vendor.locality, 1);
  639. /* close file handles */
  640. tpm_dev_vendor_release(chip);
  641. /* remove hardware */
  642. tpm_remove_hardware(chip->dev);
  643. /* reset these pointers, otherwise we oops */
  644. chip->dev->release = NULL;
  645. chip->release = NULL;
  646. tpm_dev.client = NULL;
  647. dev_set_drvdata(chip->dev, chip);
  648. return 0;
  649. }
  650. static struct i2c_driver tpm_tis_i2c_driver = {
  651. .id_table = tpm_tis_i2c_table,
  652. .probe = tpm_tis_i2c_probe,
  653. .remove = tpm_tis_i2c_remove,
  654. .driver = {
  655. .name = "tpm_i2c_infineon",
  656. .owner = THIS_MODULE,
  657. .pm = &tpm_tis_i2c_ops,
  658. .of_match_table = of_match_ptr(tpm_tis_i2c_of_match),
  659. },
  660. };
  661. module_i2c_driver(tpm_tis_i2c_driver);
  662. MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>");
  663. MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
  664. MODULE_VERSION("2.2.0");
  665. MODULE_LICENSE("GPL");