tpm_i2c_infineon.c 18 KB

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