tpm_i2c_nuvoton.c 19 KB

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