tpm_i2c_nuvoton.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /******************************************************************************
  2. * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501/NPCT6XX,
  3. * based on the TCG TPM Interface Spec version 1.2.
  4. * Specifications at www.trustedcomputinggroup.org
  5. *
  6. * Copyright (C) 2011, Nuvoton Technology Corporation.
  7. * Dan Morav <dan.morav@nuvoton.com>
  8. * Copyright (C) 2013, Obsidian Research Corp.
  9. * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see http://www.gnu.org/licenses/>.
  23. *
  24. * Nuvoton contact information: APC.Support@nuvoton.com
  25. *****************************************************************************/
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/wait.h>
  32. #include <linux/i2c.h>
  33. #include <linux/of_device.h>
  34. #include "tpm.h"
  35. /* I2C interface offsets */
  36. #define TPM_STS 0x00
  37. #define TPM_BURST_COUNT 0x01
  38. #define TPM_DATA_FIFO_W 0x20
  39. #define TPM_DATA_FIFO_R 0x40
  40. #define TPM_VID_DID_RID 0x60
  41. /* TPM command header size */
  42. #define TPM_HEADER_SIZE 10
  43. #define TPM_RETRY 5
  44. /*
  45. * I2C bus device maximum buffer size w/o counting I2C address or command
  46. * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
  47. */
  48. #define TPM_I2C_MAX_BUF_SIZE 32
  49. #define TPM_I2C_RETRY_COUNT 32
  50. #define TPM_I2C_BUS_DELAY 1000 /* usec */
  51. #define TPM_I2C_RETRY_DELAY_SHORT (2 * 1000) /* usec */
  52. #define TPM_I2C_RETRY_DELAY_LONG (10 * 1000) /* usec */
  53. #define TPM_I2C_DELAY_RANGE 300 /* usec */
  54. #define OF_IS_TPM2 ((void *)1)
  55. #define I2C_IS_TPM2 1
  56. struct priv_data {
  57. int irq;
  58. unsigned int intrs;
  59. wait_queue_head_t read_queue;
  60. };
  61. static s32 i2c_nuvoton_read_buf(struct i2c_client *client, u8 offset, u8 size,
  62. u8 *data)
  63. {
  64. s32 status;
  65. status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
  66. dev_dbg(&client->dev,
  67. "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
  68. offset, size, (int)size, data, status);
  69. return status;
  70. }
  71. static s32 i2c_nuvoton_write_buf(struct i2c_client *client, u8 offset, u8 size,
  72. u8 *data)
  73. {
  74. s32 status;
  75. status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
  76. dev_dbg(&client->dev,
  77. "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
  78. offset, size, (int)size, data, status);
  79. return status;
  80. }
  81. #define TPM_STS_VALID 0x80
  82. #define TPM_STS_COMMAND_READY 0x40
  83. #define TPM_STS_GO 0x20
  84. #define TPM_STS_DATA_AVAIL 0x10
  85. #define TPM_STS_EXPECT 0x08
  86. #define TPM_STS_RESPONSE_RETRY 0x02
  87. #define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
  88. #define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
  89. #define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
  90. /* read TPM_STS register */
  91. static u8 i2c_nuvoton_read_status(struct tpm_chip *chip)
  92. {
  93. struct i2c_client *client = to_i2c_client(chip->dev.parent);
  94. s32 status;
  95. u8 data;
  96. status = i2c_nuvoton_read_buf(client, TPM_STS, 1, &data);
  97. if (status <= 0) {
  98. dev_err(&chip->dev, "%s() error return %d\n", __func__,
  99. status);
  100. data = TPM_STS_ERR_VAL;
  101. }
  102. return data;
  103. }
  104. /* write byte to TPM_STS register */
  105. static s32 i2c_nuvoton_write_status(struct i2c_client *client, u8 data)
  106. {
  107. s32 status;
  108. int i;
  109. /* this causes the current command to be aborted */
  110. for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
  111. status = i2c_nuvoton_write_buf(client, TPM_STS, 1, &data);
  112. if (status < 0)
  113. usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
  114. + TPM_I2C_DELAY_RANGE);
  115. }
  116. return status;
  117. }
  118. /* write commandReady to TPM_STS register */
  119. static void i2c_nuvoton_ready(struct tpm_chip *chip)
  120. {
  121. struct i2c_client *client = to_i2c_client(chip->dev.parent);
  122. s32 status;
  123. /* this causes the current command to be aborted */
  124. status = i2c_nuvoton_write_status(client, TPM_STS_COMMAND_READY);
  125. if (status < 0)
  126. dev_err(&chip->dev,
  127. "%s() fail to write TPM_STS.commandReady\n", __func__);
  128. }
  129. /* read burstCount field from TPM_STS register
  130. * return -1 on fail to read */
  131. static int i2c_nuvoton_get_burstcount(struct i2c_client *client,
  132. struct tpm_chip *chip)
  133. {
  134. unsigned long stop = jiffies + chip->timeout_d;
  135. s32 status;
  136. int burst_count = -1;
  137. u8 data;
  138. /* wait for burstcount to be non-zero */
  139. do {
  140. /* in I2C burstCount is 1 byte */
  141. status = i2c_nuvoton_read_buf(client, TPM_BURST_COUNT, 1,
  142. &data);
  143. if (status > 0 && data > 0) {
  144. burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
  145. break;
  146. }
  147. usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
  148. + TPM_I2C_DELAY_RANGE);
  149. } while (time_before(jiffies, stop));
  150. return burst_count;
  151. }
  152. /*
  153. * WPCT301/NPCT501/NPCT6XX SINT# supports only dataAvail
  154. * any call to this function which is not waiting for dataAvail will
  155. * set queue to NULL to avoid waiting for interrupt
  156. */
  157. static bool i2c_nuvoton_check_status(struct tpm_chip *chip, u8 mask, u8 value)
  158. {
  159. u8 status = i2c_nuvoton_read_status(chip);
  160. return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
  161. }
  162. static int i2c_nuvoton_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
  163. u32 timeout, wait_queue_head_t *queue)
  164. {
  165. if ((chip->flags & TPM_CHIP_FLAG_IRQ) && queue) {
  166. s32 rc;
  167. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  168. unsigned int cur_intrs = priv->intrs;
  169. enable_irq(priv->irq);
  170. rc = wait_event_interruptible_timeout(*queue,
  171. cur_intrs != priv->intrs,
  172. timeout);
  173. if (rc > 0)
  174. return 0;
  175. /* At this point we know that the SINT pin is asserted, so we
  176. * do not need to do i2c_nuvoton_check_status */
  177. } else {
  178. unsigned long ten_msec, stop;
  179. bool status_valid;
  180. /* check current status */
  181. status_valid = i2c_nuvoton_check_status(chip, mask, value);
  182. if (status_valid)
  183. return 0;
  184. /* use polling to wait for the event */
  185. ten_msec = jiffies + usecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
  186. stop = jiffies + timeout;
  187. do {
  188. if (time_before(jiffies, ten_msec))
  189. usleep_range(TPM_I2C_RETRY_DELAY_SHORT,
  190. TPM_I2C_RETRY_DELAY_SHORT
  191. + TPM_I2C_DELAY_RANGE);
  192. else
  193. usleep_range(TPM_I2C_RETRY_DELAY_LONG,
  194. TPM_I2C_RETRY_DELAY_LONG
  195. + TPM_I2C_DELAY_RANGE);
  196. status_valid = i2c_nuvoton_check_status(chip, mask,
  197. value);
  198. if (status_valid)
  199. return 0;
  200. } while (time_before(jiffies, stop));
  201. }
  202. dev_err(&chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
  203. value);
  204. return -ETIMEDOUT;
  205. }
  206. /* wait for dataAvail field to be set in the TPM_STS register */
  207. static int i2c_nuvoton_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
  208. wait_queue_head_t *queue)
  209. {
  210. return i2c_nuvoton_wait_for_stat(chip,
  211. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  212. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  213. timeout, queue);
  214. }
  215. /* Read @count bytes into @buf from TPM_RD_FIFO register */
  216. static int i2c_nuvoton_recv_data(struct i2c_client *client,
  217. struct tpm_chip *chip, u8 *buf, size_t count)
  218. {
  219. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  220. s32 rc;
  221. int burst_count, bytes2read, size = 0;
  222. while (size < count &&
  223. i2c_nuvoton_wait_for_data_avail(chip,
  224. chip->timeout_c,
  225. &priv->read_queue) == 0) {
  226. burst_count = i2c_nuvoton_get_burstcount(client, chip);
  227. if (burst_count < 0) {
  228. dev_err(&chip->dev,
  229. "%s() fail to read burstCount=%d\n", __func__,
  230. burst_count);
  231. return -EIO;
  232. }
  233. bytes2read = min_t(size_t, burst_count, count - size);
  234. rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_R,
  235. bytes2read, &buf[size]);
  236. if (rc < 0) {
  237. dev_err(&chip->dev,
  238. "%s() fail on i2c_nuvoton_read_buf()=%d\n",
  239. __func__, rc);
  240. return -EIO;
  241. }
  242. dev_dbg(&chip->dev, "%s(%d):", __func__, bytes2read);
  243. size += bytes2read;
  244. }
  245. return size;
  246. }
  247. /* Read TPM command results */
  248. static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  249. {
  250. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  251. struct device *dev = chip->dev.parent;
  252. struct i2c_client *client = to_i2c_client(dev);
  253. s32 rc;
  254. int expected, status, burst_count, retries, size = 0;
  255. if (count < TPM_HEADER_SIZE) {
  256. i2c_nuvoton_ready(chip); /* return to idle */
  257. dev_err(dev, "%s() count < header size\n", __func__);
  258. return -EIO;
  259. }
  260. for (retries = 0; retries < TPM_RETRY; retries++) {
  261. if (retries > 0) {
  262. /* if this is not the first trial, set responseRetry */
  263. i2c_nuvoton_write_status(client,
  264. TPM_STS_RESPONSE_RETRY);
  265. }
  266. /*
  267. * read first available (> 10 bytes), including:
  268. * tag, paramsize, and result
  269. */
  270. status = i2c_nuvoton_wait_for_data_avail(
  271. chip, chip->timeout_c, &priv->read_queue);
  272. if (status != 0) {
  273. dev_err(dev, "%s() timeout on dataAvail\n", __func__);
  274. size = -ETIMEDOUT;
  275. continue;
  276. }
  277. burst_count = i2c_nuvoton_get_burstcount(client, chip);
  278. if (burst_count < 0) {
  279. dev_err(dev, "%s() fail to get burstCount\n", __func__);
  280. size = -EIO;
  281. continue;
  282. }
  283. size = i2c_nuvoton_recv_data(client, chip, buf,
  284. burst_count);
  285. if (size < TPM_HEADER_SIZE) {
  286. dev_err(dev, "%s() fail to read header\n", __func__);
  287. size = -EIO;
  288. continue;
  289. }
  290. /*
  291. * convert number of expected bytes field from big endian 32 bit
  292. * to machine native
  293. */
  294. expected = be32_to_cpu(*(__be32 *) (buf + 2));
  295. if (expected > count) {
  296. dev_err(dev, "%s() expected > count\n", __func__);
  297. size = -EIO;
  298. continue;
  299. }
  300. rc = i2c_nuvoton_recv_data(client, chip, &buf[size],
  301. expected - size);
  302. size += rc;
  303. if (rc < 0 || size < expected) {
  304. dev_err(dev, "%s() fail to read remainder of result\n",
  305. __func__);
  306. size = -EIO;
  307. continue;
  308. }
  309. if (i2c_nuvoton_wait_for_stat(
  310. chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
  311. TPM_STS_VALID, chip->timeout_c,
  312. NULL)) {
  313. dev_err(dev, "%s() error left over data\n", __func__);
  314. size = -ETIMEDOUT;
  315. continue;
  316. }
  317. break;
  318. }
  319. i2c_nuvoton_ready(chip);
  320. dev_dbg(&chip->dev, "%s() -> %d\n", __func__, size);
  321. return size;
  322. }
  323. /*
  324. * Send TPM command.
  325. *
  326. * If interrupts are used (signaled by an irq set in the vendor structure)
  327. * tpm.c can skip polling for the data to be available as the interrupt is
  328. * waited for here
  329. */
  330. static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
  331. {
  332. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  333. struct device *dev = chip->dev.parent;
  334. struct i2c_client *client = to_i2c_client(dev);
  335. u32 ordinal;
  336. size_t count = 0;
  337. int burst_count, bytes2write, retries, rc = -EIO;
  338. for (retries = 0; retries < TPM_RETRY; retries++) {
  339. i2c_nuvoton_ready(chip);
  340. if (i2c_nuvoton_wait_for_stat(chip, TPM_STS_COMMAND_READY,
  341. TPM_STS_COMMAND_READY,
  342. chip->timeout_b, NULL)) {
  343. dev_err(dev, "%s() timeout on commandReady\n",
  344. __func__);
  345. rc = -EIO;
  346. continue;
  347. }
  348. rc = 0;
  349. while (count < len - 1) {
  350. burst_count = i2c_nuvoton_get_burstcount(client,
  351. chip);
  352. if (burst_count < 0) {
  353. dev_err(dev, "%s() fail get burstCount\n",
  354. __func__);
  355. rc = -EIO;
  356. break;
  357. }
  358. bytes2write = min_t(size_t, burst_count,
  359. len - 1 - count);
  360. rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W,
  361. bytes2write, &buf[count]);
  362. if (rc < 0) {
  363. dev_err(dev, "%s() fail i2cWriteBuf\n",
  364. __func__);
  365. break;
  366. }
  367. dev_dbg(dev, "%s(%d):", __func__, bytes2write);
  368. count += bytes2write;
  369. rc = i2c_nuvoton_wait_for_stat(chip,
  370. TPM_STS_VALID |
  371. TPM_STS_EXPECT,
  372. TPM_STS_VALID |
  373. TPM_STS_EXPECT,
  374. chip->timeout_c,
  375. NULL);
  376. if (rc < 0) {
  377. dev_err(dev, "%s() timeout on Expect\n",
  378. __func__);
  379. rc = -ETIMEDOUT;
  380. break;
  381. }
  382. }
  383. if (rc < 0)
  384. continue;
  385. /* write last byte */
  386. rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W, 1,
  387. &buf[count]);
  388. if (rc < 0) {
  389. dev_err(dev, "%s() fail to write last byte\n",
  390. __func__);
  391. rc = -EIO;
  392. continue;
  393. }
  394. dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
  395. rc = i2c_nuvoton_wait_for_stat(chip,
  396. TPM_STS_VALID | TPM_STS_EXPECT,
  397. TPM_STS_VALID,
  398. chip->timeout_c, NULL);
  399. if (rc) {
  400. dev_err(dev, "%s() timeout on Expect to clear\n",
  401. __func__);
  402. rc = -ETIMEDOUT;
  403. continue;
  404. }
  405. break;
  406. }
  407. if (rc < 0) {
  408. /* retries == TPM_RETRY */
  409. i2c_nuvoton_ready(chip);
  410. return rc;
  411. }
  412. /* execute the TPM command */
  413. rc = i2c_nuvoton_write_status(client, TPM_STS_GO);
  414. if (rc < 0) {
  415. dev_err(dev, "%s() fail to write Go\n", __func__);
  416. i2c_nuvoton_ready(chip);
  417. return rc;
  418. }
  419. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  420. rc = i2c_nuvoton_wait_for_data_avail(chip,
  421. tpm_calc_ordinal_duration(chip,
  422. ordinal),
  423. &priv->read_queue);
  424. if (rc) {
  425. dev_err(dev, "%s() timeout command duration\n", __func__);
  426. i2c_nuvoton_ready(chip);
  427. return rc;
  428. }
  429. dev_dbg(dev, "%s() -> %zd\n", __func__, len);
  430. return len;
  431. }
  432. static bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
  433. {
  434. return (status == TPM_STS_COMMAND_READY);
  435. }
  436. static const struct tpm_class_ops tpm_i2c = {
  437. .flags = TPM_OPS_AUTO_STARTUP,
  438. .status = i2c_nuvoton_read_status,
  439. .recv = i2c_nuvoton_recv,
  440. .send = i2c_nuvoton_send,
  441. .cancel = i2c_nuvoton_ready,
  442. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  443. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  444. .req_canceled = i2c_nuvoton_req_canceled,
  445. };
  446. /* The only purpose for the handler is to signal to any waiting threads that
  447. * the interrupt is currently being asserted. The driver does not do any
  448. * processing triggered by interrupts, and the chip provides no way to mask at
  449. * the source (plus that would be slow over I2C). Run the IRQ as a one-shot,
  450. * this means it cannot be shared. */
  451. static irqreturn_t i2c_nuvoton_int_handler(int dummy, void *dev_id)
  452. {
  453. struct tpm_chip *chip = dev_id;
  454. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  455. priv->intrs++;
  456. wake_up(&priv->read_queue);
  457. disable_irq_nosync(priv->irq);
  458. return IRQ_HANDLED;
  459. }
  460. static int get_vid(struct i2c_client *client, u32 *res)
  461. {
  462. static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
  463. u32 temp;
  464. s32 rc;
  465. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  466. return -ENODEV;
  467. rc = i2c_nuvoton_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
  468. if (rc < 0)
  469. return rc;
  470. /* check WPCT301 values - ignore RID */
  471. if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
  472. /*
  473. * f/w rev 2.81 has an issue where the VID_DID_RID is not
  474. * reporting the right value. so give it another chance at
  475. * offset 0x20 (FIFO_W).
  476. */
  477. rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_W, 4,
  478. (u8 *) (&temp));
  479. if (rc < 0)
  480. return rc;
  481. /* check WPCT301 values - ignore RID */
  482. if (memcmp(&temp, vid_did_rid_value,
  483. sizeof(vid_did_rid_value)))
  484. return -ENODEV;
  485. }
  486. *res = temp;
  487. return 0;
  488. }
  489. static int i2c_nuvoton_probe(struct i2c_client *client,
  490. const struct i2c_device_id *id)
  491. {
  492. int rc;
  493. struct tpm_chip *chip;
  494. struct device *dev = &client->dev;
  495. struct priv_data *priv;
  496. u32 vid = 0;
  497. rc = get_vid(client, &vid);
  498. if (rc)
  499. return rc;
  500. dev_info(dev, "VID: %04X DID: %02X RID: %02X\n", (u16) vid,
  501. (u8) (vid >> 16), (u8) (vid >> 24));
  502. chip = tpmm_chip_alloc(dev, &tpm_i2c);
  503. if (IS_ERR(chip))
  504. return PTR_ERR(chip);
  505. priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
  506. if (!priv)
  507. return -ENOMEM;
  508. if (dev->of_node) {
  509. const struct of_device_id *of_id;
  510. of_id = of_match_device(dev->driver->of_match_table, dev);
  511. if (of_id && of_id->data == OF_IS_TPM2)
  512. chip->flags |= TPM_CHIP_FLAG_TPM2;
  513. } else
  514. if (id->driver_data == I2C_IS_TPM2)
  515. chip->flags |= TPM_CHIP_FLAG_TPM2;
  516. init_waitqueue_head(&priv->read_queue);
  517. /* Default timeouts */
  518. chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  519. chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
  520. chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  521. chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  522. dev_set_drvdata(&chip->dev, priv);
  523. /*
  524. * I2C intfcaps (interrupt capabilitieis) in the chip are hard coded to:
  525. * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
  526. * The IRQ should be set in the i2c_board_info (which is done
  527. * automatically in of_i2c_register_devices, for device tree users */
  528. priv->irq = client->irq;
  529. if (client->irq) {
  530. dev_dbg(dev, "%s() priv->irq\n", __func__);
  531. rc = devm_request_irq(dev, client->irq,
  532. i2c_nuvoton_int_handler,
  533. IRQF_TRIGGER_LOW,
  534. dev_name(&chip->dev),
  535. chip);
  536. if (rc) {
  537. dev_err(dev, "%s() Unable to request irq: %d for use\n",
  538. __func__, priv->irq);
  539. priv->irq = 0;
  540. } else {
  541. chip->flags |= TPM_CHIP_FLAG_IRQ;
  542. /* Clear any pending interrupt */
  543. i2c_nuvoton_ready(chip);
  544. /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
  545. rc = i2c_nuvoton_wait_for_stat(chip,
  546. TPM_STS_COMMAND_READY,
  547. TPM_STS_COMMAND_READY,
  548. chip->timeout_b,
  549. NULL);
  550. if (rc == 0) {
  551. /*
  552. * TIS is in ready state
  553. * write dummy byte to enter reception state
  554. * TPM_DATA_FIFO_W <- rc (0)
  555. */
  556. rc = i2c_nuvoton_write_buf(client,
  557. TPM_DATA_FIFO_W,
  558. 1, (u8 *) (&rc));
  559. if (rc < 0)
  560. return rc;
  561. /* TPM_STS <- 0x40 (commandReady) */
  562. i2c_nuvoton_ready(chip);
  563. } else {
  564. /*
  565. * timeout_b reached - command was
  566. * aborted. TIS should now be in idle state -
  567. * only TPM_STS_VALID should be set
  568. */
  569. if (i2c_nuvoton_read_status(chip) !=
  570. TPM_STS_VALID)
  571. return -EIO;
  572. }
  573. }
  574. }
  575. return tpm_chip_register(chip);
  576. }
  577. static int i2c_nuvoton_remove(struct i2c_client *client)
  578. {
  579. struct tpm_chip *chip = i2c_get_clientdata(client);
  580. tpm_chip_unregister(chip);
  581. return 0;
  582. }
  583. static const struct i2c_device_id i2c_nuvoton_id[] = {
  584. {"tpm_i2c_nuvoton"},
  585. {"tpm2_i2c_nuvoton", .driver_data = I2C_IS_TPM2},
  586. {}
  587. };
  588. MODULE_DEVICE_TABLE(i2c, i2c_nuvoton_id);
  589. #ifdef CONFIG_OF
  590. static const struct of_device_id i2c_nuvoton_of_match[] = {
  591. {.compatible = "nuvoton,npct501"},
  592. {.compatible = "winbond,wpct301"},
  593. {.compatible = "nuvoton,npct601", .data = OF_IS_TPM2},
  594. {},
  595. };
  596. MODULE_DEVICE_TABLE(of, i2c_nuvoton_of_match);
  597. #endif
  598. static SIMPLE_DEV_PM_OPS(i2c_nuvoton_pm_ops, tpm_pm_suspend, tpm_pm_resume);
  599. static struct i2c_driver i2c_nuvoton_driver = {
  600. .id_table = i2c_nuvoton_id,
  601. .probe = i2c_nuvoton_probe,
  602. .remove = i2c_nuvoton_remove,
  603. .driver = {
  604. .name = "tpm_i2c_nuvoton",
  605. .pm = &i2c_nuvoton_pm_ops,
  606. .of_match_table = of_match_ptr(i2c_nuvoton_of_match),
  607. },
  608. };
  609. module_i2c_driver(i2c_nuvoton_driver);
  610. MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
  611. MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
  612. MODULE_LICENSE("GPL");