tpm_i2c_nuvoton.c 18 KB

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