tpm_i2c_infineon.c 18 KB

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