tpm_i2c_infineon.c 18 KB

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