tpm2_tis_mmio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * driver for mmio TCG/TIS TPM (trusted platform module).
  4. *
  5. * Specifications at www.trustedcomputinggroup.org
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <tpm-v2.h>
  11. #include <linux/bitops.h>
  12. #include <linux/compiler.h>
  13. #include <linux/delay.h>
  14. #include <linux/errno.h>
  15. #include <linux/types.h>
  16. #include <linux/io.h>
  17. #include <linux/unaligned/be_byteshift.h>
  18. #include "tpm_tis.h"
  19. #include "tpm_internal.h"
  20. /**
  21. * struct tpm_tis_chip_data - Information about an MMIO TPM
  22. * @pcr_count: Number of PCR per bank
  23. * @pcr_select_min: Minimum size in bytes of the pcrSelect array
  24. * @iobase: Base address
  25. */
  26. struct tpm_tis_chip_data {
  27. unsigned int pcr_count;
  28. unsigned int pcr_select_min;
  29. void __iomem *iobase;
  30. };
  31. static int mmio_read_bytes(struct udevice *dev, u32 addr, u16 len,
  32. u8 *result)
  33. {
  34. struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
  35. while (len--)
  36. *result++ = ioread8(drv_data->iobase + addr);
  37. return 0;
  38. }
  39. static int mmio_write_bytes(struct udevice *dev, u32 addr, u16 len,
  40. const u8 *value)
  41. {
  42. struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
  43. while (len--)
  44. iowrite8(*value++, drv_data->iobase + addr);
  45. return 0;
  46. }
  47. static int mmio_read32(struct udevice *dev, u32 addr, u32 *result)
  48. {
  49. struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
  50. *result = ioread32(drv_data->iobase + addr);
  51. return 0;
  52. }
  53. static int mmio_write32(struct udevice *dev, u32 addr, u32 value)
  54. {
  55. struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
  56. iowrite32(value, drv_data->iobase + addr);
  57. return 0;
  58. }
  59. static struct tpm_tis_phy_ops phy_ops = {
  60. .read_bytes = mmio_read_bytes,
  61. .write_bytes = mmio_write_bytes,
  62. .read32 = mmio_read32,
  63. .write32 = mmio_write32,
  64. };
  65. static int tpm_tis_probe(struct udevice *dev)
  66. {
  67. struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
  68. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  69. int ret = 0;
  70. fdt_addr_t ioaddr;
  71. u64 sz;
  72. ioaddr = dev_read_addr(dev);
  73. if (ioaddr == FDT_ADDR_T_NONE)
  74. return log_msg_ret("ioaddr", -EINVAL);
  75. ret = dev_read_u64(dev, "reg", &sz);
  76. if (ret)
  77. return -EINVAL;
  78. drv_data->iobase = ioremap(ioaddr, sz);
  79. tpm_tis_ops_register(dev, &phy_ops);
  80. ret = tpm_tis_init(dev);
  81. if (ret)
  82. goto iounmap;
  83. priv->pcr_count = drv_data->pcr_count;
  84. priv->pcr_select_min = drv_data->pcr_select_min;
  85. /*
  86. * Although the driver probably works with a TPMv1 our Kconfig
  87. * limits the driver to TPMv2 only
  88. */
  89. priv->version = TPM_V2;
  90. return ret;
  91. iounmap:
  92. iounmap(drv_data->iobase);
  93. return -EINVAL;
  94. }
  95. static int tpm_tis_remove(struct udevice *dev)
  96. {
  97. struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
  98. int ret;
  99. ret = tpm_tis_cleanup(dev);
  100. iounmap(drv_data->iobase);
  101. return ret;
  102. }
  103. static const struct tpm_ops tpm_tis_ops = {
  104. .open = tpm_tis_open,
  105. .close = tpm_tis_close,
  106. .get_desc = tpm_tis_get_desc,
  107. .send = tpm_tis_send,
  108. .recv = tpm_tis_recv,
  109. .cleanup = tpm_tis_cleanup,
  110. };
  111. static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
  112. .pcr_count = 24,
  113. .pcr_select_min = 3,
  114. };
  115. static const struct udevice_id tpm_tis_ids[] = {
  116. {
  117. .compatible = "tcg,tpm-tis-mmio",
  118. .data = (ulong)&tpm_tis_std_chip_data,
  119. },
  120. { }
  121. };
  122. U_BOOT_DRIVER(tpm_tis_mmio) = {
  123. .name = "tpm_tis_mmio",
  124. .id = UCLASS_TPM,
  125. .of_match = tpm_tis_ids,
  126. .ops = &tpm_tis_ops,
  127. .probe = tpm_tis_probe,
  128. .remove = tpm_tis_remove,
  129. .priv_auto = sizeof(struct tpm_chip),
  130. };