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
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>
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.
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.
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/>.
24 * Nuvoton contact information: APC.Support@nuvoton.com
25 *****************************************************************************/
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/wait.h>
33 #include <linux/i2c.h>
36 /* I2C interface offsets */
38 #define TPM_BURST_COUNT 0x01
39 #define TPM_DATA_FIFO_W 0x20
40 #define TPM_DATA_FIFO_R 0x40
41 #define TPM_VID_DID_RID 0x60
42 /* TPM command header size */
43 #define TPM_HEADER_SIZE 10
46 * I2C bus device maximum buffer size w/o counting I2C address or command
47 * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
49 #define TPM_I2C_MAX_BUF_SIZE 32
50 #define TPM_I2C_RETRY_COUNT 32
51 #define TPM_I2C_BUS_DELAY 1 /* msec */
52 #define TPM_I2C_RETRY_DELAY_SHORT 2 /* msec */
53 #define TPM_I2C_RETRY_DELAY_LONG 10 /* msec */
55 #define I2C_DRIVER_NAME "tpm_i2c_nuvoton"
61 static s32 i2c_nuvoton_read_buf(struct i2c_client *client, u8 offset, u8 size,
66 status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
68 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
69 offset, size, (int)size, data, status);
73 static s32 i2c_nuvoton_write_buf(struct i2c_client *client, u8 offset, u8 size,
78 status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
80 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
81 offset, size, (int)size, data, status);
85 #define TPM_STS_VALID 0x80
86 #define TPM_STS_COMMAND_READY 0x40
87 #define TPM_STS_GO 0x20
88 #define TPM_STS_DATA_AVAIL 0x10
89 #define TPM_STS_EXPECT 0x08
90 #define TPM_STS_RESPONSE_RETRY 0x02
91 #define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
93 #define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
94 #define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
96 /* read TPM_STS register */
97 static u8 i2c_nuvoton_read_status(struct tpm_chip *chip)
99 struct i2c_client *client = to_i2c_client(chip->dev.parent);
103 status = i2c_nuvoton_read_buf(client, TPM_STS, 1, &data);
105 dev_err(&chip->dev, "%s() error return %d\n", __func__,
107 data = TPM_STS_ERR_VAL;
113 /* write byte to TPM_STS register */
114 static s32 i2c_nuvoton_write_status(struct i2c_client *client, u8 data)
119 /* this causes the current command to be aborted */
120 for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
121 status = i2c_nuvoton_write_buf(client, TPM_STS, 1, &data);
122 msleep(TPM_I2C_BUS_DELAY);
127 /* write commandReady to TPM_STS register */
128 static void i2c_nuvoton_ready(struct tpm_chip *chip)
130 struct i2c_client *client = to_i2c_client(chip->dev.parent);
133 /* this causes the current command to be aborted */
134 status = i2c_nuvoton_write_status(client, TPM_STS_COMMAND_READY);
137 "%s() fail to write TPM_STS.commandReady\n", __func__);
140 /* read burstCount field from TPM_STS register
141 * return -1 on fail to read */
142 static int i2c_nuvoton_get_burstcount(struct i2c_client *client,
143 struct tpm_chip *chip)
145 unsigned long stop = jiffies + chip->vendor.timeout_d;
147 int burst_count = -1;
150 /* wait for burstcount to be non-zero */
152 /* in I2C burstCount is 1 byte */
153 status = i2c_nuvoton_read_buf(client, TPM_BURST_COUNT, 1,
155 if (status > 0 && data > 0) {
156 burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
159 msleep(TPM_I2C_BUS_DELAY);
160 } while (time_before(jiffies, stop));
166 * WPCT301/NPCT501 SINT# supports only dataAvail
167 * any call to this function which is not waiting for dataAvail will
168 * set queue to NULL to avoid waiting for interrupt
170 static bool i2c_nuvoton_check_status(struct tpm_chip *chip, u8 mask, u8 value)
172 u8 status = i2c_nuvoton_read_status(chip);
173 return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
176 static int i2c_nuvoton_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
177 u32 timeout, wait_queue_head_t *queue)
179 if (chip->vendor.irq && queue) {
181 struct priv_data *priv = chip->vendor.priv;
182 unsigned int cur_intrs = priv->intrs;
184 enable_irq(chip->vendor.irq);
185 rc = wait_event_interruptible_timeout(*queue,
186 cur_intrs != priv->intrs,
190 /* At this point we know that the SINT pin is asserted, so we
191 * do not need to do i2c_nuvoton_check_status */
193 unsigned long ten_msec, stop;
196 /* check current status */
197 status_valid = i2c_nuvoton_check_status(chip, mask, value);
201 /* use polling to wait for the event */
202 ten_msec = jiffies + msecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
203 stop = jiffies + timeout;
205 if (time_before(jiffies, ten_msec))
206 msleep(TPM_I2C_RETRY_DELAY_SHORT);
208 msleep(TPM_I2C_RETRY_DELAY_LONG);
209 status_valid = i2c_nuvoton_check_status(chip, mask,
213 } while (time_before(jiffies, stop));
215 dev_err(&chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
220 /* wait for dataAvail field to be set in the TPM_STS register */
221 static int i2c_nuvoton_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
222 wait_queue_head_t *queue)
224 return i2c_nuvoton_wait_for_stat(chip,
225 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
226 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
230 /* Read @count bytes into @buf from TPM_RD_FIFO register */
231 static int i2c_nuvoton_recv_data(struct i2c_client *client,
232 struct tpm_chip *chip, u8 *buf, size_t count)
235 int burst_count, bytes2read, size = 0;
237 while (size < count &&
238 i2c_nuvoton_wait_for_data_avail(chip,
239 chip->vendor.timeout_c,
240 &chip->vendor.read_queue) == 0) {
241 burst_count = i2c_nuvoton_get_burstcount(client, chip);
242 if (burst_count < 0) {
244 "%s() fail to read burstCount=%d\n", __func__,
248 bytes2read = min_t(size_t, burst_count, count - size);
249 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_R,
250 bytes2read, &buf[size]);
253 "%s() fail on i2c_nuvoton_read_buf()=%d\n",
257 dev_dbg(&chip->dev, "%s(%d):", __func__, bytes2read);
264 /* Read TPM command results */
265 static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
267 struct device *dev = chip->dev.parent;
268 struct i2c_client *client = to_i2c_client(dev);
276 if (count < TPM_HEADER_SIZE) {
277 i2c_nuvoton_ready(chip); /* return to idle */
278 dev_err(dev, "%s() count < header size\n", __func__);
281 for (retries = 0; retries < TPM_RETRY; retries++) {
283 /* if this is not the first trial, set responseRetry */
284 i2c_nuvoton_write_status(client,
285 TPM_STS_RESPONSE_RETRY);
288 * read first available (> 10 bytes), including:
289 * tag, paramsize, and result
291 status = i2c_nuvoton_wait_for_data_avail(
292 chip, chip->vendor.timeout_c, &chip->vendor.read_queue);
294 dev_err(dev, "%s() timeout on dataAvail\n", __func__);
298 burst_count = i2c_nuvoton_get_burstcount(client, chip);
299 if (burst_count < 0) {
300 dev_err(dev, "%s() fail to get burstCount\n", __func__);
304 size = i2c_nuvoton_recv_data(client, chip, buf,
306 if (size < TPM_HEADER_SIZE) {
307 dev_err(dev, "%s() fail to read header\n", __func__);
312 * convert number of expected bytes field from big endian 32 bit
315 expected = be32_to_cpu(*(__be32 *) (buf + 2));
316 if (expected > count || expected < size) {
317 dev_err(dev, "%s() expected > count\n", __func__);
321 rc = i2c_nuvoton_recv_data(client, chip, &buf[size],
324 if (rc < 0 || size < expected) {
325 dev_err(dev, "%s() fail to read remainder of result\n",
330 if (i2c_nuvoton_wait_for_stat(
331 chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
332 TPM_STS_VALID, chip->vendor.timeout_c,
334 dev_err(dev, "%s() error left over data\n", __func__);
340 i2c_nuvoton_ready(chip);
341 dev_dbg(&chip->dev, "%s() -> %d\n", __func__, size);
348 * If interrupts are used (signaled by an irq set in the vendor structure)
349 * tpm.c can skip polling for the data to be available as the interrupt is
352 static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
354 struct device *dev = chip->dev.parent;
355 struct i2c_client *client = to_i2c_client(dev);
358 int burst_count, bytes2write, retries, rc = -EIO;
360 for (retries = 0; retries < TPM_RETRY; retries++) {
361 i2c_nuvoton_ready(chip);
362 if (i2c_nuvoton_wait_for_stat(chip, TPM_STS_COMMAND_READY,
363 TPM_STS_COMMAND_READY,
364 chip->vendor.timeout_b, NULL)) {
365 dev_err(dev, "%s() timeout on commandReady\n",
371 while (count < len - 1) {
372 burst_count = i2c_nuvoton_get_burstcount(client,
374 if (burst_count < 0) {
375 dev_err(dev, "%s() fail get burstCount\n",
380 bytes2write = min_t(size_t, burst_count,
382 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W,
383 bytes2write, &buf[count]);
385 dev_err(dev, "%s() fail i2cWriteBuf\n",
389 dev_dbg(dev, "%s(%d):", __func__, bytes2write);
390 count += bytes2write;
391 rc = i2c_nuvoton_wait_for_stat(chip,
396 chip->vendor.timeout_c,
399 dev_err(dev, "%s() timeout on Expect\n",
408 /* write last byte */
409 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W, 1,
412 dev_err(dev, "%s() fail to write last byte\n",
417 dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
418 rc = i2c_nuvoton_wait_for_stat(chip,
419 TPM_STS_VALID | TPM_STS_EXPECT,
421 chip->vendor.timeout_c, NULL);
423 dev_err(dev, "%s() timeout on Expect to clear\n",
431 /* retries == TPM_RETRY */
432 i2c_nuvoton_ready(chip);
435 /* execute the TPM command */
436 rc = i2c_nuvoton_write_status(client, TPM_STS_GO);
438 dev_err(dev, "%s() fail to write Go\n", __func__);
439 i2c_nuvoton_ready(chip);
442 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
443 rc = i2c_nuvoton_wait_for_data_avail(chip,
444 tpm_calc_ordinal_duration(chip,
446 &chip->vendor.read_queue);
448 dev_err(dev, "%s() timeout command duration\n", __func__);
449 i2c_nuvoton_ready(chip);
453 dev_dbg(dev, "%s() -> %zd\n", __func__, len);
457 static bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
459 return (status == TPM_STS_COMMAND_READY);
462 static const struct tpm_class_ops tpm_i2c = {
463 .status = i2c_nuvoton_read_status,
464 .recv = i2c_nuvoton_recv,
465 .send = i2c_nuvoton_send,
466 .cancel = i2c_nuvoton_ready,
467 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
468 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
469 .req_canceled = i2c_nuvoton_req_canceled,
472 /* The only purpose for the handler is to signal to any waiting threads that
473 * the interrupt is currently being asserted. The driver does not do any
474 * processing triggered by interrupts, and the chip provides no way to mask at
475 * the source (plus that would be slow over I2C). Run the IRQ as a one-shot,
476 * this means it cannot be shared. */
477 static irqreturn_t i2c_nuvoton_int_handler(int dummy, void *dev_id)
479 struct tpm_chip *chip = dev_id;
480 struct priv_data *priv = chip->vendor.priv;
483 wake_up(&chip->vendor.read_queue);
484 disable_irq_nosync(chip->vendor.irq);
488 static int get_vid(struct i2c_client *client, u32 *res)
490 static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
494 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
496 rc = i2c_nuvoton_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
500 /* check WPCT301 values - ignore RID */
501 if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
503 * f/w rev 2.81 has an issue where the VID_DID_RID is not
504 * reporting the right value. so give it another chance at
505 * offset 0x20 (FIFO_W).
507 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_W, 4,
512 /* check WPCT301 values - ignore RID */
513 if (memcmp(&temp, vid_did_rid_value,
514 sizeof(vid_did_rid_value)))
522 static int i2c_nuvoton_probe(struct i2c_client *client,
523 const struct i2c_device_id *id)
526 struct tpm_chip *chip;
527 struct device *dev = &client->dev;
530 rc = get_vid(client, &vid);
534 dev_info(dev, "VID: %04X DID: %02X RID: %02X\n", (u16) vid,
535 (u8) (vid >> 16), (u8) (vid >> 24));
537 chip = tpmm_chip_alloc(dev, &tpm_i2c);
539 return PTR_ERR(chip);
541 chip->vendor.priv = devm_kzalloc(dev, sizeof(struct priv_data),
543 if (!chip->vendor.priv)
546 init_waitqueue_head(&chip->vendor.read_queue);
547 init_waitqueue_head(&chip->vendor.int_queue);
549 /* Default timeouts */
550 chip->vendor.timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
551 chip->vendor.timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
552 chip->vendor.timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
553 chip->vendor.timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
556 * I2C intfcaps (interrupt capabilitieis) in the chip are hard coded to:
557 * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
558 * The IRQ should be set in the i2c_board_info (which is done
559 * automatically in of_i2c_register_devices, for device tree users */
560 chip->vendor.irq = client->irq;
562 if (chip->vendor.irq) {
563 dev_dbg(dev, "%s() chip-vendor.irq\n", __func__);
564 rc = devm_request_irq(dev, chip->vendor.irq,
565 i2c_nuvoton_int_handler,
570 dev_err(dev, "%s() Unable to request irq: %d for use\n",
571 __func__, chip->vendor.irq);
572 chip->vendor.irq = 0;
574 /* Clear any pending interrupt */
575 i2c_nuvoton_ready(chip);
576 /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
577 rc = i2c_nuvoton_wait_for_stat(chip,
578 TPM_STS_COMMAND_READY,
579 TPM_STS_COMMAND_READY,
580 chip->vendor.timeout_b,
584 * TIS is in ready state
585 * write dummy byte to enter reception state
586 * TPM_DATA_FIFO_W <- rc (0)
588 rc = i2c_nuvoton_write_buf(client,
593 /* TPM_STS <- 0x40 (commandReady) */
594 i2c_nuvoton_ready(chip);
597 * timeout_b reached - command was
598 * aborted. TIS should now be in idle state -
599 * only TPM_STS_VALID should be set
601 if (i2c_nuvoton_read_status(chip) !=
608 if (tpm_get_timeouts(chip))
611 if (tpm_do_selftest(chip))
614 return tpm_chip_register(chip);
617 static int i2c_nuvoton_remove(struct i2c_client *client)
619 struct device *dev = &(client->dev);
620 struct tpm_chip *chip = dev_get_drvdata(dev);
621 tpm_chip_unregister(chip);
625 static const struct i2c_device_id i2c_nuvoton_id[] = {
626 {I2C_DRIVER_NAME, 0},
629 MODULE_DEVICE_TABLE(i2c, i2c_nuvoton_id);
632 static const struct of_device_id i2c_nuvoton_of_match[] = {
633 {.compatible = "nuvoton,npct501"},
634 {.compatible = "winbond,wpct301"},
637 MODULE_DEVICE_TABLE(of, i2c_nuvoton_of_match);
640 static SIMPLE_DEV_PM_OPS(i2c_nuvoton_pm_ops, tpm_pm_suspend, tpm_pm_resume);
642 static struct i2c_driver i2c_nuvoton_driver = {
643 .id_table = i2c_nuvoton_id,
644 .probe = i2c_nuvoton_probe,
645 .remove = i2c_nuvoton_remove,
647 .name = I2C_DRIVER_NAME,
648 .pm = &i2c_nuvoton_pm_ops,
649 .of_match_table = of_match_ptr(i2c_nuvoton_of_match),
653 module_i2c_driver(i2c_nuvoton_driver);
655 MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
656 MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
657 MODULE_LICENSE("GPL");