0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/init.h>
0016 #include <linux/module.h>
0017 #include <linux/moduleparam.h>
0018 #include <linux/slab.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/wait.h>
0021 #include <linux/i2c.h>
0022 #include <linux/of_device.h>
0023 #include "tpm.h"
0024
0025
0026 #define TPM_STS 0x00
0027 #define TPM_BURST_COUNT 0x01
0028 #define TPM_DATA_FIFO_W 0x20
0029 #define TPM_DATA_FIFO_R 0x40
0030 #define TPM_VID_DID_RID 0x60
0031 #define TPM_I2C_RETRIES 5
0032
0033
0034
0035
0036 #define TPM_I2C_MAX_BUF_SIZE 32
0037 #define TPM_I2C_RETRY_COUNT 32
0038 #define TPM_I2C_BUS_DELAY 1000
0039 #define TPM_I2C_RETRY_DELAY_SHORT (2 * 1000)
0040 #define TPM_I2C_RETRY_DELAY_LONG (10 * 1000)
0041 #define TPM_I2C_DELAY_RANGE 300
0042
0043 #define OF_IS_TPM2 ((void *)1)
0044 #define I2C_IS_TPM2 1
0045
0046 struct priv_data {
0047 int irq;
0048 unsigned int intrs;
0049 wait_queue_head_t read_queue;
0050 };
0051
0052 static s32 i2c_nuvoton_read_buf(struct i2c_client *client, u8 offset, u8 size,
0053 u8 *data)
0054 {
0055 s32 status;
0056
0057 status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
0058 dev_dbg(&client->dev,
0059 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
0060 offset, size, (int)size, data, status);
0061 return status;
0062 }
0063
0064 static s32 i2c_nuvoton_write_buf(struct i2c_client *client, u8 offset, u8 size,
0065 u8 *data)
0066 {
0067 s32 status;
0068
0069 status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
0070 dev_dbg(&client->dev,
0071 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
0072 offset, size, (int)size, data, status);
0073 return status;
0074 }
0075
0076 #define TPM_STS_VALID 0x80
0077 #define TPM_STS_COMMAND_READY 0x40
0078 #define TPM_STS_GO 0x20
0079 #define TPM_STS_DATA_AVAIL 0x10
0080 #define TPM_STS_EXPECT 0x08
0081 #define TPM_STS_RESPONSE_RETRY 0x02
0082 #define TPM_STS_ERR_VAL 0x07
0083
0084 #define TPM_I2C_SHORT_TIMEOUT 750
0085 #define TPM_I2C_LONG_TIMEOUT 2000
0086
0087
0088 static u8 i2c_nuvoton_read_status(struct tpm_chip *chip)
0089 {
0090 struct i2c_client *client = to_i2c_client(chip->dev.parent);
0091 s32 status;
0092 u8 data;
0093
0094 status = i2c_nuvoton_read_buf(client, TPM_STS, 1, &data);
0095 if (status <= 0) {
0096 dev_err(&chip->dev, "%s() error return %d\n", __func__,
0097 status);
0098 data = TPM_STS_ERR_VAL;
0099 }
0100
0101 return data;
0102 }
0103
0104
0105 static s32 i2c_nuvoton_write_status(struct i2c_client *client, u8 data)
0106 {
0107 s32 status;
0108 int i;
0109
0110
0111 for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
0112 status = i2c_nuvoton_write_buf(client, TPM_STS, 1, &data);
0113 if (status < 0)
0114 usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
0115 + TPM_I2C_DELAY_RANGE);
0116 }
0117 return status;
0118 }
0119
0120
0121 static void i2c_nuvoton_ready(struct tpm_chip *chip)
0122 {
0123 struct i2c_client *client = to_i2c_client(chip->dev.parent);
0124 s32 status;
0125
0126
0127 status = i2c_nuvoton_write_status(client, TPM_STS_COMMAND_READY);
0128 if (status < 0)
0129 dev_err(&chip->dev,
0130 "%s() fail to write TPM_STS.commandReady\n", __func__);
0131 }
0132
0133
0134
0135 static int i2c_nuvoton_get_burstcount(struct i2c_client *client,
0136 struct tpm_chip *chip)
0137 {
0138 unsigned long stop = jiffies + chip->timeout_d;
0139 s32 status;
0140 int burst_count = -1;
0141 u8 data;
0142
0143
0144 do {
0145
0146 status = i2c_nuvoton_read_buf(client, TPM_BURST_COUNT, 1,
0147 &data);
0148 if (status > 0 && data > 0) {
0149 burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
0150 break;
0151 }
0152 usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
0153 + TPM_I2C_DELAY_RANGE);
0154 } while (time_before(jiffies, stop));
0155
0156 return burst_count;
0157 }
0158
0159
0160
0161
0162
0163
0164 static bool i2c_nuvoton_check_status(struct tpm_chip *chip, u8 mask, u8 value)
0165 {
0166 u8 status = i2c_nuvoton_read_status(chip);
0167 return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
0168 }
0169
0170 static int i2c_nuvoton_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
0171 u32 timeout, wait_queue_head_t *queue)
0172 {
0173 if ((chip->flags & TPM_CHIP_FLAG_IRQ) && queue) {
0174 s32 rc;
0175 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0176 unsigned int cur_intrs = priv->intrs;
0177
0178 enable_irq(priv->irq);
0179 rc = wait_event_interruptible_timeout(*queue,
0180 cur_intrs != priv->intrs,
0181 timeout);
0182 if (rc > 0)
0183 return 0;
0184
0185
0186 } else {
0187 unsigned long ten_msec, stop;
0188 bool status_valid;
0189
0190
0191 status_valid = i2c_nuvoton_check_status(chip, mask, value);
0192 if (status_valid)
0193 return 0;
0194
0195
0196 ten_msec = jiffies + usecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
0197 stop = jiffies + timeout;
0198 do {
0199 if (time_before(jiffies, ten_msec))
0200 usleep_range(TPM_I2C_RETRY_DELAY_SHORT,
0201 TPM_I2C_RETRY_DELAY_SHORT
0202 + TPM_I2C_DELAY_RANGE);
0203 else
0204 usleep_range(TPM_I2C_RETRY_DELAY_LONG,
0205 TPM_I2C_RETRY_DELAY_LONG
0206 + TPM_I2C_DELAY_RANGE);
0207 status_valid = i2c_nuvoton_check_status(chip, mask,
0208 value);
0209 if (status_valid)
0210 return 0;
0211 } while (time_before(jiffies, stop));
0212 }
0213 dev_err(&chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
0214 value);
0215 return -ETIMEDOUT;
0216 }
0217
0218
0219 static int i2c_nuvoton_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
0220 wait_queue_head_t *queue)
0221 {
0222 return i2c_nuvoton_wait_for_stat(chip,
0223 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0224 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0225 timeout, queue);
0226 }
0227
0228
0229 static int i2c_nuvoton_recv_data(struct i2c_client *client,
0230 struct tpm_chip *chip, u8 *buf, size_t count)
0231 {
0232 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0233 s32 rc;
0234 int burst_count, bytes2read, size = 0;
0235
0236 while (size < count &&
0237 i2c_nuvoton_wait_for_data_avail(chip,
0238 chip->timeout_c,
0239 &priv->read_queue) == 0) {
0240 burst_count = i2c_nuvoton_get_burstcount(client, chip);
0241 if (burst_count < 0) {
0242 dev_err(&chip->dev,
0243 "%s() fail to read burstCount=%d\n", __func__,
0244 burst_count);
0245 return -EIO;
0246 }
0247 bytes2read = min_t(size_t, burst_count, count - size);
0248 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_R,
0249 bytes2read, &buf[size]);
0250 if (rc < 0) {
0251 dev_err(&chip->dev,
0252 "%s() fail on i2c_nuvoton_read_buf()=%d\n",
0253 __func__, rc);
0254 return -EIO;
0255 }
0256 dev_dbg(&chip->dev, "%s(%d):", __func__, bytes2read);
0257 size += bytes2read;
0258 }
0259
0260 return size;
0261 }
0262
0263
0264 static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
0265 {
0266 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0267 struct device *dev = chip->dev.parent;
0268 struct i2c_client *client = to_i2c_client(dev);
0269 s32 rc;
0270 int status;
0271 int burst_count;
0272 int retries;
0273 int size = 0;
0274 u32 expected;
0275
0276 if (count < TPM_HEADER_SIZE) {
0277 i2c_nuvoton_ready(chip);
0278 dev_err(dev, "%s() count < header size\n", __func__);
0279 return -EIO;
0280 }
0281 for (retries = 0; retries < TPM_I2C_RETRIES; retries++) {
0282 if (retries > 0) {
0283
0284 i2c_nuvoton_write_status(client,
0285 TPM_STS_RESPONSE_RETRY);
0286 }
0287
0288
0289
0290
0291 status = i2c_nuvoton_wait_for_data_avail(
0292 chip, chip->timeout_c, &priv->read_queue);
0293 if (status != 0) {
0294 dev_err(dev, "%s() timeout on dataAvail\n", __func__);
0295 size = -ETIMEDOUT;
0296 continue;
0297 }
0298 burst_count = i2c_nuvoton_get_burstcount(client, chip);
0299 if (burst_count < 0) {
0300 dev_err(dev, "%s() fail to get burstCount\n", __func__);
0301 size = -EIO;
0302 continue;
0303 }
0304 size = i2c_nuvoton_recv_data(client, chip, buf,
0305 burst_count);
0306 if (size < TPM_HEADER_SIZE) {
0307 dev_err(dev, "%s() fail to read header\n", __func__);
0308 size = -EIO;
0309 continue;
0310 }
0311
0312
0313
0314
0315 expected = be32_to_cpu(*(__be32 *) (buf + 2));
0316 if (expected > count || expected < size) {
0317 dev_err(dev, "%s() expected > count\n", __func__);
0318 size = -EIO;
0319 continue;
0320 }
0321 rc = i2c_nuvoton_recv_data(client, chip, &buf[size],
0322 expected - size);
0323 size += rc;
0324 if (rc < 0 || size < expected) {
0325 dev_err(dev, "%s() fail to read remainder of result\n",
0326 __func__);
0327 size = -EIO;
0328 continue;
0329 }
0330 if (i2c_nuvoton_wait_for_stat(
0331 chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
0332 TPM_STS_VALID, chip->timeout_c,
0333 NULL)) {
0334 dev_err(dev, "%s() error left over data\n", __func__);
0335 size = -ETIMEDOUT;
0336 continue;
0337 }
0338 break;
0339 }
0340 i2c_nuvoton_ready(chip);
0341 dev_dbg(&chip->dev, "%s() -> %d\n", __func__, size);
0342 return size;
0343 }
0344
0345
0346
0347
0348
0349
0350
0351
0352 static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
0353 {
0354 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0355 struct device *dev = chip->dev.parent;
0356 struct i2c_client *client = to_i2c_client(dev);
0357 u32 ordinal;
0358 unsigned long duration;
0359 size_t count = 0;
0360 int burst_count, bytes2write, retries, rc = -EIO;
0361
0362 for (retries = 0; retries < TPM_RETRY; retries++) {
0363 i2c_nuvoton_ready(chip);
0364 if (i2c_nuvoton_wait_for_stat(chip, TPM_STS_COMMAND_READY,
0365 TPM_STS_COMMAND_READY,
0366 chip->timeout_b, NULL)) {
0367 dev_err(dev, "%s() timeout on commandReady\n",
0368 __func__);
0369 rc = -EIO;
0370 continue;
0371 }
0372 rc = 0;
0373 while (count < len - 1) {
0374 burst_count = i2c_nuvoton_get_burstcount(client,
0375 chip);
0376 if (burst_count < 0) {
0377 dev_err(dev, "%s() fail get burstCount\n",
0378 __func__);
0379 rc = -EIO;
0380 break;
0381 }
0382 bytes2write = min_t(size_t, burst_count,
0383 len - 1 - count);
0384 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W,
0385 bytes2write, &buf[count]);
0386 if (rc < 0) {
0387 dev_err(dev, "%s() fail i2cWriteBuf\n",
0388 __func__);
0389 break;
0390 }
0391 dev_dbg(dev, "%s(%d):", __func__, bytes2write);
0392 count += bytes2write;
0393 rc = i2c_nuvoton_wait_for_stat(chip,
0394 TPM_STS_VALID |
0395 TPM_STS_EXPECT,
0396 TPM_STS_VALID |
0397 TPM_STS_EXPECT,
0398 chip->timeout_c,
0399 NULL);
0400 if (rc < 0) {
0401 dev_err(dev, "%s() timeout on Expect\n",
0402 __func__);
0403 rc = -ETIMEDOUT;
0404 break;
0405 }
0406 }
0407 if (rc < 0)
0408 continue;
0409
0410
0411 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W, 1,
0412 &buf[count]);
0413 if (rc < 0) {
0414 dev_err(dev, "%s() fail to write last byte\n",
0415 __func__);
0416 rc = -EIO;
0417 continue;
0418 }
0419 dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
0420 rc = i2c_nuvoton_wait_for_stat(chip,
0421 TPM_STS_VALID | TPM_STS_EXPECT,
0422 TPM_STS_VALID,
0423 chip->timeout_c, NULL);
0424 if (rc) {
0425 dev_err(dev, "%s() timeout on Expect to clear\n",
0426 __func__);
0427 rc = -ETIMEDOUT;
0428 continue;
0429 }
0430 break;
0431 }
0432 if (rc < 0) {
0433
0434 i2c_nuvoton_ready(chip);
0435 return rc;
0436 }
0437
0438 rc = i2c_nuvoton_write_status(client, TPM_STS_GO);
0439 if (rc < 0) {
0440 dev_err(dev, "%s() fail to write Go\n", __func__);
0441 i2c_nuvoton_ready(chip);
0442 return rc;
0443 }
0444 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
0445 duration = tpm_calc_ordinal_duration(chip, ordinal);
0446
0447 rc = i2c_nuvoton_wait_for_data_avail(chip, duration, &priv->read_queue);
0448 if (rc) {
0449 dev_err(dev, "%s() timeout command duration %ld\n",
0450 __func__, duration);
0451 i2c_nuvoton_ready(chip);
0452 return rc;
0453 }
0454
0455 dev_dbg(dev, "%s() -> %zd\n", __func__, len);
0456 return 0;
0457 }
0458
0459 static bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
0460 {
0461 return (status == TPM_STS_COMMAND_READY);
0462 }
0463
0464 static const struct tpm_class_ops tpm_i2c = {
0465 .flags = TPM_OPS_AUTO_STARTUP,
0466 .status = i2c_nuvoton_read_status,
0467 .recv = i2c_nuvoton_recv,
0468 .send = i2c_nuvoton_send,
0469 .cancel = i2c_nuvoton_ready,
0470 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0471 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0472 .req_canceled = i2c_nuvoton_req_canceled,
0473 };
0474
0475
0476
0477
0478
0479
0480 static irqreturn_t i2c_nuvoton_int_handler(int dummy, void *dev_id)
0481 {
0482 struct tpm_chip *chip = dev_id;
0483 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0484
0485 priv->intrs++;
0486 wake_up(&priv->read_queue);
0487 disable_irq_nosync(priv->irq);
0488 return IRQ_HANDLED;
0489 }
0490
0491 static int get_vid(struct i2c_client *client, u32 *res)
0492 {
0493 static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
0494 u32 temp;
0495 s32 rc;
0496
0497 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0498 return -ENODEV;
0499 rc = i2c_nuvoton_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
0500 if (rc < 0)
0501 return rc;
0502
0503
0504 if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
0505
0506
0507
0508
0509
0510 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_W, 4,
0511 (u8 *) (&temp));
0512 if (rc < 0)
0513 return rc;
0514
0515
0516 if (memcmp(&temp, vid_did_rid_value,
0517 sizeof(vid_did_rid_value)))
0518 return -ENODEV;
0519 }
0520
0521 *res = temp;
0522 return 0;
0523 }
0524
0525 static int i2c_nuvoton_probe(struct i2c_client *client,
0526 const struct i2c_device_id *id)
0527 {
0528 int rc;
0529 struct tpm_chip *chip;
0530 struct device *dev = &client->dev;
0531 struct priv_data *priv;
0532 u32 vid = 0;
0533
0534 rc = get_vid(client, &vid);
0535 if (rc)
0536 return rc;
0537
0538 dev_info(dev, "VID: %04X DID: %02X RID: %02X\n", (u16) vid,
0539 (u8) (vid >> 16), (u8) (vid >> 24));
0540
0541 chip = tpmm_chip_alloc(dev, &tpm_i2c);
0542 if (IS_ERR(chip))
0543 return PTR_ERR(chip);
0544
0545 priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
0546 if (!priv)
0547 return -ENOMEM;
0548
0549 if (dev->of_node) {
0550 const struct of_device_id *of_id;
0551
0552 of_id = of_match_device(dev->driver->of_match_table, dev);
0553 if (of_id && of_id->data == OF_IS_TPM2)
0554 chip->flags |= TPM_CHIP_FLAG_TPM2;
0555 } else
0556 if (id->driver_data == I2C_IS_TPM2)
0557 chip->flags |= TPM_CHIP_FLAG_TPM2;
0558
0559 init_waitqueue_head(&priv->read_queue);
0560
0561
0562 chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0563 chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
0564 chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0565 chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0566
0567 dev_set_drvdata(&chip->dev, priv);
0568
0569
0570
0571
0572
0573
0574 priv->irq = client->irq;
0575 if (client->irq) {
0576 dev_dbg(dev, "%s() priv->irq\n", __func__);
0577 rc = devm_request_irq(dev, client->irq,
0578 i2c_nuvoton_int_handler,
0579 IRQF_TRIGGER_LOW,
0580 dev_name(&chip->dev),
0581 chip);
0582 if (rc) {
0583 dev_err(dev, "%s() Unable to request irq: %d for use\n",
0584 __func__, priv->irq);
0585 priv->irq = 0;
0586 } else {
0587 chip->flags |= TPM_CHIP_FLAG_IRQ;
0588
0589 i2c_nuvoton_ready(chip);
0590
0591 rc = i2c_nuvoton_wait_for_stat(chip,
0592 TPM_STS_COMMAND_READY,
0593 TPM_STS_COMMAND_READY,
0594 chip->timeout_b,
0595 NULL);
0596 if (rc == 0) {
0597
0598
0599
0600
0601
0602 rc = i2c_nuvoton_write_buf(client,
0603 TPM_DATA_FIFO_W,
0604 1, (u8 *) (&rc));
0605 if (rc < 0)
0606 return rc;
0607
0608 i2c_nuvoton_ready(chip);
0609 } else {
0610
0611
0612
0613
0614
0615 if (i2c_nuvoton_read_status(chip) !=
0616 TPM_STS_VALID)
0617 return -EIO;
0618 }
0619 }
0620 }
0621
0622 return tpm_chip_register(chip);
0623 }
0624
0625 static int i2c_nuvoton_remove(struct i2c_client *client)
0626 {
0627 struct tpm_chip *chip = i2c_get_clientdata(client);
0628
0629 tpm_chip_unregister(chip);
0630 return 0;
0631 }
0632
0633 static const struct i2c_device_id i2c_nuvoton_id[] = {
0634 {"tpm_i2c_nuvoton"},
0635 {"tpm2_i2c_nuvoton", .driver_data = I2C_IS_TPM2},
0636 {}
0637 };
0638 MODULE_DEVICE_TABLE(i2c, i2c_nuvoton_id);
0639
0640 #ifdef CONFIG_OF
0641 static const struct of_device_id i2c_nuvoton_of_match[] = {
0642 {.compatible = "nuvoton,npct501"},
0643 {.compatible = "winbond,wpct301"},
0644 {.compatible = "nuvoton,npct601", .data = OF_IS_TPM2},
0645 {},
0646 };
0647 MODULE_DEVICE_TABLE(of, i2c_nuvoton_of_match);
0648 #endif
0649
0650 static SIMPLE_DEV_PM_OPS(i2c_nuvoton_pm_ops, tpm_pm_suspend, tpm_pm_resume);
0651
0652 static struct i2c_driver i2c_nuvoton_driver = {
0653 .id_table = i2c_nuvoton_id,
0654 .probe = i2c_nuvoton_probe,
0655 .remove = i2c_nuvoton_remove,
0656 .driver = {
0657 .name = "tpm_i2c_nuvoton",
0658 .pm = &i2c_nuvoton_pm_ops,
0659 .of_match_table = of_match_ptr(i2c_nuvoton_of_match),
0660 },
0661 };
0662
0663 module_i2c_driver(i2c_nuvoton_driver);
0664
0665 MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
0666 MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
0667 MODULE_LICENSE("GPL");