Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * STMicroelectronics TPM Linux driver for TPM ST33ZP24
0004  * Copyright (C) 2009 - 2016 STMicroelectronics
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/fs.h>
0009 #include <linux/kernel.h>
0010 #include <linux/delay.h>
0011 #include <linux/wait.h>
0012 #include <linux/freezer.h>
0013 #include <linux/string.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/gpio.h>
0016 #include <linux/sched.h>
0017 #include <linux/uaccess.h>
0018 #include <linux/io.h>
0019 #include <linux/slab.h>
0020 
0021 #include "../tpm.h"
0022 #include "st33zp24.h"
0023 
0024 #define TPM_ACCESS          0x0
0025 #define TPM_STS             0x18
0026 #define TPM_DATA_FIFO           0x24
0027 #define TPM_INTF_CAPABILITY     0x14
0028 #define TPM_INT_STATUS          0x10
0029 #define TPM_INT_ENABLE          0x08
0030 
0031 #define LOCALITY0           0
0032 
0033 enum st33zp24_access {
0034     TPM_ACCESS_VALID = 0x80,
0035     TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
0036     TPM_ACCESS_REQUEST_PENDING = 0x04,
0037     TPM_ACCESS_REQUEST_USE = 0x02,
0038 };
0039 
0040 enum st33zp24_status {
0041     TPM_STS_VALID = 0x80,
0042     TPM_STS_COMMAND_READY = 0x40,
0043     TPM_STS_GO = 0x20,
0044     TPM_STS_DATA_AVAIL = 0x10,
0045     TPM_STS_DATA_EXPECT = 0x08,
0046 };
0047 
0048 enum st33zp24_int_flags {
0049     TPM_GLOBAL_INT_ENABLE = 0x80,
0050     TPM_INTF_CMD_READY_INT = 0x080,
0051     TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
0052     TPM_INTF_WAKE_UP_READY_INT = 0x020,
0053     TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
0054     TPM_INTF_STS_VALID_INT = 0x002,
0055     TPM_INTF_DATA_AVAIL_INT = 0x001,
0056 };
0057 
0058 enum tis_defaults {
0059     TIS_SHORT_TIMEOUT = 750,
0060     TIS_LONG_TIMEOUT = 2000,
0061 };
0062 
0063 /*
0064  * clear the pending interrupt.
0065  */
0066 static u8 clear_interruption(struct st33zp24_dev *tpm_dev)
0067 {
0068     u8 interrupt;
0069 
0070     tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
0071     tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
0072     return interrupt;
0073 }
0074 
0075 /*
0076  * cancel the current command execution or set STS to COMMAND READY.
0077  */
0078 static void st33zp24_cancel(struct tpm_chip *chip)
0079 {
0080     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0081     u8 data;
0082 
0083     data = TPM_STS_COMMAND_READY;
0084     tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
0085 }
0086 
0087 /*
0088  * return the TPM_STS register
0089  */
0090 static u8 st33zp24_status(struct tpm_chip *chip)
0091 {
0092     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0093     u8 data;
0094 
0095     tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
0096     return data;
0097 }
0098 
0099 /*
0100  * if the locality is active
0101  */
0102 static bool check_locality(struct tpm_chip *chip)
0103 {
0104     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0105     u8 data;
0106     u8 status;
0107 
0108     status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
0109     if (status && (data &
0110         (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
0111         (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
0112         return true;
0113 
0114     return false;
0115 }
0116 
0117 static int request_locality(struct tpm_chip *chip)
0118 {
0119     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0120     unsigned long stop;
0121     long ret;
0122     u8 data;
0123 
0124     if (check_locality(chip))
0125         return tpm_dev->locality;
0126 
0127     data = TPM_ACCESS_REQUEST_USE;
0128     ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
0129     if (ret < 0)
0130         return ret;
0131 
0132     stop = jiffies + chip->timeout_a;
0133 
0134     /* Request locality is usually effective after the request */
0135     do {
0136         if (check_locality(chip))
0137             return tpm_dev->locality;
0138         msleep(TPM_TIMEOUT);
0139     } while (time_before(jiffies, stop));
0140 
0141     /* could not get locality */
0142     return -EACCES;
0143 }
0144 
0145 static void release_locality(struct tpm_chip *chip)
0146 {
0147     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0148     u8 data;
0149 
0150     data = TPM_ACCESS_ACTIVE_LOCALITY;
0151 
0152     tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
0153 }
0154 
0155 /*
0156  * get_burstcount return the burstcount value
0157  */
0158 static int get_burstcount(struct tpm_chip *chip)
0159 {
0160     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0161     unsigned long stop;
0162     int burstcnt, status;
0163     u8 temp;
0164 
0165     stop = jiffies + chip->timeout_d;
0166     do {
0167         status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1,
0168                         &temp, 1);
0169         if (status < 0)
0170             return -EBUSY;
0171 
0172         burstcnt = temp;
0173         status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2,
0174                         &temp, 1);
0175         if (status < 0)
0176             return -EBUSY;
0177 
0178         burstcnt |= temp << 8;
0179         if (burstcnt)
0180             return burstcnt;
0181         msleep(TPM_TIMEOUT);
0182     } while (time_before(jiffies, stop));
0183     return -EBUSY;
0184 }
0185 
0186 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
0187                 bool check_cancel, bool *canceled)
0188 {
0189     u8 status = chip->ops->status(chip);
0190 
0191     *canceled = false;
0192     if ((status & mask) == mask)
0193         return true;
0194     if (check_cancel && chip->ops->req_canceled(chip, status)) {
0195         *canceled = true;
0196         return true;
0197     }
0198     return false;
0199 }
0200 
0201 /*
0202  * wait for a TPM_STS value
0203  */
0204 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
0205             wait_queue_head_t *queue, bool check_cancel)
0206 {
0207     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0208     unsigned long stop;
0209     int ret = 0;
0210     bool canceled = false;
0211     bool condition;
0212     u32 cur_intrs;
0213     u8 status;
0214 
0215     /* check current status */
0216     status = st33zp24_status(chip);
0217     if ((status & mask) == mask)
0218         return 0;
0219 
0220     stop = jiffies + timeout;
0221 
0222     if (chip->flags & TPM_CHIP_FLAG_IRQ) {
0223         cur_intrs = tpm_dev->intrs;
0224         clear_interruption(tpm_dev);
0225         enable_irq(tpm_dev->irq);
0226 
0227         do {
0228             if (ret == -ERESTARTSYS && freezing(current))
0229                 clear_thread_flag(TIF_SIGPENDING);
0230 
0231             timeout = stop - jiffies;
0232             if ((long) timeout <= 0)
0233                 return -1;
0234 
0235             ret = wait_event_interruptible_timeout(*queue,
0236                         cur_intrs != tpm_dev->intrs,
0237                         timeout);
0238             clear_interruption(tpm_dev);
0239             condition = wait_for_tpm_stat_cond(chip, mask,
0240                         check_cancel, &canceled);
0241             if (ret >= 0 && condition) {
0242                 if (canceled)
0243                     return -ECANCELED;
0244                 return 0;
0245             }
0246         } while (ret == -ERESTARTSYS && freezing(current));
0247 
0248         disable_irq_nosync(tpm_dev->irq);
0249 
0250     } else {
0251         do {
0252             msleep(TPM_TIMEOUT);
0253             status = chip->ops->status(chip);
0254             if ((status & mask) == mask)
0255                 return 0;
0256         } while (time_before(jiffies, stop));
0257     }
0258 
0259     return -ETIME;
0260 }
0261 
0262 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
0263 {
0264     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0265     int size = 0, burstcnt, len, ret;
0266 
0267     while (size < count &&
0268            wait_for_stat(chip,
0269                  TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0270                  chip->timeout_c,
0271                  &tpm_dev->read_queue, true) == 0) {
0272         burstcnt = get_burstcount(chip);
0273         if (burstcnt < 0)
0274             return burstcnt;
0275         len = min_t(int, burstcnt, count - size);
0276         ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO,
0277                      buf + size, len);
0278         if (ret < 0)
0279             return ret;
0280 
0281         size += len;
0282     }
0283     return size;
0284 }
0285 
0286 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
0287 {
0288     struct tpm_chip *chip = dev_id;
0289     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0290 
0291     tpm_dev->intrs++;
0292     wake_up_interruptible(&tpm_dev->read_queue);
0293     disable_irq_nosync(tpm_dev->irq);
0294 
0295     return IRQ_HANDLED;
0296 }
0297 
0298 /*
0299  * send TPM commands through the I2C bus.
0300  */
0301 static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf,
0302              size_t len)
0303 {
0304     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0305     u32 status, i, size, ordinal;
0306     int burstcnt = 0;
0307     int ret;
0308     u8 data;
0309 
0310     if (len < TPM_HEADER_SIZE)
0311         return -EBUSY;
0312 
0313     ret = request_locality(chip);
0314     if (ret < 0)
0315         return ret;
0316 
0317     status = st33zp24_status(chip);
0318     if ((status & TPM_STS_COMMAND_READY) == 0) {
0319         st33zp24_cancel(chip);
0320         if (wait_for_stat
0321             (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
0322              &tpm_dev->read_queue, false) < 0) {
0323             ret = -ETIME;
0324             goto out_err;
0325         }
0326     }
0327 
0328     for (i = 0; i < len - 1;) {
0329         burstcnt = get_burstcount(chip);
0330         if (burstcnt < 0)
0331             return burstcnt;
0332         size = min_t(int, len - i - 1, burstcnt);
0333         ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
0334                      buf + i, size);
0335         if (ret < 0)
0336             goto out_err;
0337 
0338         i += size;
0339     }
0340 
0341     status = st33zp24_status(chip);
0342     if ((status & TPM_STS_DATA_EXPECT) == 0) {
0343         ret = -EIO;
0344         goto out_err;
0345     }
0346 
0347     ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
0348                  buf + len - 1, 1);
0349     if (ret < 0)
0350         goto out_err;
0351 
0352     status = st33zp24_status(chip);
0353     if ((status & TPM_STS_DATA_EXPECT) != 0) {
0354         ret = -EIO;
0355         goto out_err;
0356     }
0357 
0358     data = TPM_STS_GO;
0359     ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
0360     if (ret < 0)
0361         goto out_err;
0362 
0363     if (chip->flags & TPM_CHIP_FLAG_IRQ) {
0364         ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
0365 
0366         ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0367                 tpm_calc_ordinal_duration(chip, ordinal),
0368                 &tpm_dev->read_queue, false);
0369         if (ret < 0)
0370             goto out_err;
0371     }
0372 
0373     return 0;
0374 out_err:
0375     st33zp24_cancel(chip);
0376     release_locality(chip);
0377     return ret;
0378 }
0379 
0380 static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
0381                 size_t count)
0382 {
0383     int size = 0;
0384     u32 expected;
0385 
0386     if (!chip)
0387         return -EBUSY;
0388 
0389     if (count < TPM_HEADER_SIZE) {
0390         size = -EIO;
0391         goto out;
0392     }
0393 
0394     size = recv_data(chip, buf, TPM_HEADER_SIZE);
0395     if (size < TPM_HEADER_SIZE) {
0396         dev_err(&chip->dev, "Unable to read header\n");
0397         goto out;
0398     }
0399 
0400     expected = be32_to_cpu(*(__be32 *)(buf + 2));
0401     if (expected > count || expected < TPM_HEADER_SIZE) {
0402         size = -EIO;
0403         goto out;
0404     }
0405 
0406     size += recv_data(chip, &buf[TPM_HEADER_SIZE],
0407             expected - TPM_HEADER_SIZE);
0408     if (size < expected) {
0409         dev_err(&chip->dev, "Unable to read remainder of result\n");
0410         size = -ETIME;
0411     }
0412 
0413 out:
0414     st33zp24_cancel(chip);
0415     release_locality(chip);
0416     return size;
0417 }
0418 
0419 static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
0420 {
0421     return (status == TPM_STS_COMMAND_READY);
0422 }
0423 
0424 static const struct tpm_class_ops st33zp24_tpm = {
0425     .flags = TPM_OPS_AUTO_STARTUP,
0426     .send = st33zp24_send,
0427     .recv = st33zp24_recv,
0428     .cancel = st33zp24_cancel,
0429     .status = st33zp24_status,
0430     .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0431     .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
0432     .req_canceled = st33zp24_req_canceled,
0433 };
0434 
0435 /*
0436  * initialize the TPM device
0437  */
0438 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
0439            struct device *dev, int irq, int io_lpcpd)
0440 {
0441     int ret;
0442     u8 intmask = 0;
0443     struct tpm_chip *chip;
0444     struct st33zp24_dev *tpm_dev;
0445 
0446     chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
0447     if (IS_ERR(chip))
0448         return PTR_ERR(chip);
0449 
0450     tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
0451                    GFP_KERNEL);
0452     if (!tpm_dev)
0453         return -ENOMEM;
0454 
0455     tpm_dev->phy_id = phy_id;
0456     tpm_dev->ops = ops;
0457     dev_set_drvdata(&chip->dev, tpm_dev);
0458 
0459     chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
0460     chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
0461     chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
0462     chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
0463 
0464     tpm_dev->locality = LOCALITY0;
0465 
0466     if (irq) {
0467         /* INTERRUPT Setup */
0468         init_waitqueue_head(&tpm_dev->read_queue);
0469         tpm_dev->intrs = 0;
0470 
0471         if (request_locality(chip) != LOCALITY0) {
0472             ret = -ENODEV;
0473             goto _tpm_clean_answer;
0474         }
0475 
0476         clear_interruption(tpm_dev);
0477         ret = devm_request_irq(dev, irq, tpm_ioserirq_handler,
0478                 IRQF_TRIGGER_HIGH, "TPM SERIRQ management",
0479                 chip);
0480         if (ret < 0) {
0481             dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
0482                 irq);
0483             goto _tpm_clean_answer;
0484         }
0485 
0486         intmask |= TPM_INTF_CMD_READY_INT
0487             |  TPM_INTF_STS_VALID_INT
0488             |  TPM_INTF_DATA_AVAIL_INT;
0489 
0490         ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE,
0491                      &intmask, 1);
0492         if (ret < 0)
0493             goto _tpm_clean_answer;
0494 
0495         intmask = TPM_GLOBAL_INT_ENABLE;
0496         ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3),
0497                      &intmask, 1);
0498         if (ret < 0)
0499             goto _tpm_clean_answer;
0500 
0501         tpm_dev->irq = irq;
0502         chip->flags |= TPM_CHIP_FLAG_IRQ;
0503 
0504         disable_irq_nosync(tpm_dev->irq);
0505     }
0506 
0507     return tpm_chip_register(chip);
0508 _tpm_clean_answer:
0509     dev_info(&chip->dev, "TPM initialization fail\n");
0510     return ret;
0511 }
0512 EXPORT_SYMBOL(st33zp24_probe);
0513 
0514 void st33zp24_remove(struct tpm_chip *chip)
0515 {
0516     tpm_chip_unregister(chip);
0517 }
0518 EXPORT_SYMBOL(st33zp24_remove);
0519 
0520 #ifdef CONFIG_PM_SLEEP
0521 int st33zp24_pm_suspend(struct device *dev)
0522 {
0523     struct tpm_chip *chip = dev_get_drvdata(dev);
0524     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0525 
0526     int ret = 0;
0527 
0528     if (gpio_is_valid(tpm_dev->io_lpcpd))
0529         gpio_set_value(tpm_dev->io_lpcpd, 0);
0530     else
0531         ret = tpm_pm_suspend(dev);
0532 
0533     return ret;
0534 }
0535 EXPORT_SYMBOL(st33zp24_pm_suspend);
0536 
0537 int st33zp24_pm_resume(struct device *dev)
0538 {
0539     struct tpm_chip *chip = dev_get_drvdata(dev);
0540     struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
0541     int ret = 0;
0542 
0543     if (gpio_is_valid(tpm_dev->io_lpcpd)) {
0544         gpio_set_value(tpm_dev->io_lpcpd, 1);
0545         ret = wait_for_stat(chip,
0546                 TPM_STS_VALID, chip->timeout_b,
0547                 &tpm_dev->read_queue, false);
0548     } else {
0549         ret = tpm_pm_resume(dev);
0550         if (!ret)
0551             tpm1_do_selftest(chip);
0552     }
0553     return ret;
0554 }
0555 EXPORT_SYMBOL(st33zp24_pm_resume);
0556 #endif
0557 
0558 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
0559 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
0560 MODULE_VERSION("1.3.0");
0561 MODULE_LICENSE("GPL");