tpm_i2c_nuvoton.c 20 KB

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