tpm_tis_synquacer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Linaro Ltd.
  4. *
  5. * This device driver implements MMIO TPM on SynQuacer Platform.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/of.h>
  12. #include <linux/kernel.h>
  13. #include "tpm.h"
  14. #include "tpm_tis_core.h"
  15. /*
  16. * irq > 0 means: use irq $irq;
  17. * irq = 0 means: autoprobe for an irq;
  18. * irq = -1 means: no irq support
  19. */
  20. struct tpm_tis_synquacer_info {
  21. struct resource res;
  22. int irq;
  23. };
  24. struct tpm_tis_synquacer_phy {
  25. struct tpm_tis_data priv;
  26. void __iomem *iobase;
  27. };
  28. static inline struct tpm_tis_synquacer_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
  29. {
  30. return container_of(data, struct tpm_tis_synquacer_phy, priv);
  31. }
  32. static int tpm_tis_synquacer_read_bytes(struct tpm_tis_data *data, u32 addr,
  33. u16 len, u8 *result,
  34. enum tpm_tis_io_mode io_mode)
  35. {
  36. struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);
  37. switch (io_mode) {
  38. case TPM_TIS_PHYS_8:
  39. while (len--)
  40. *result++ = ioread8(phy->iobase + addr);
  41. break;
  42. case TPM_TIS_PHYS_16:
  43. result[1] = ioread8(phy->iobase + addr + 1);
  44. result[0] = ioread8(phy->iobase + addr);
  45. break;
  46. case TPM_TIS_PHYS_32:
  47. result[3] = ioread8(phy->iobase + addr + 3);
  48. result[2] = ioread8(phy->iobase + addr + 2);
  49. result[1] = ioread8(phy->iobase + addr + 1);
  50. result[0] = ioread8(phy->iobase + addr);
  51. break;
  52. }
  53. return 0;
  54. }
  55. static int tpm_tis_synquacer_write_bytes(struct tpm_tis_data *data, u32 addr,
  56. u16 len, const u8 *value,
  57. enum tpm_tis_io_mode io_mode)
  58. {
  59. struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);
  60. switch (io_mode) {
  61. case TPM_TIS_PHYS_8:
  62. while (len--)
  63. iowrite8(*value++, phy->iobase + addr);
  64. break;
  65. case TPM_TIS_PHYS_16:
  66. return -EINVAL;
  67. case TPM_TIS_PHYS_32:
  68. /*
  69. * Due to the limitation of SPI controller on SynQuacer,
  70. * 16/32 bits access must be done in byte-wise and descending order.
  71. */
  72. iowrite8(value[3], phy->iobase + addr + 3);
  73. iowrite8(value[2], phy->iobase + addr + 2);
  74. iowrite8(value[1], phy->iobase + addr + 1);
  75. iowrite8(value[0], phy->iobase + addr);
  76. break;
  77. }
  78. return 0;
  79. }
  80. static const struct tpm_tis_phy_ops tpm_tcg_bw = {
  81. .read_bytes = tpm_tis_synquacer_read_bytes,
  82. .write_bytes = tpm_tis_synquacer_write_bytes,
  83. };
  84. static int tpm_tis_synquacer_init(struct device *dev,
  85. struct tpm_tis_synquacer_info *tpm_info)
  86. {
  87. struct tpm_tis_synquacer_phy *phy;
  88. phy = devm_kzalloc(dev, sizeof(struct tpm_tis_synquacer_phy), GFP_KERNEL);
  89. if (phy == NULL)
  90. return -ENOMEM;
  91. phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
  92. if (IS_ERR(phy->iobase))
  93. return PTR_ERR(phy->iobase);
  94. return tpm_tis_core_init(dev, &phy->priv, tpm_info->irq, &tpm_tcg_bw,
  95. ACPI_HANDLE(dev));
  96. }
  97. static SIMPLE_DEV_PM_OPS(tpm_tis_synquacer_pm, tpm_pm_suspend, tpm_tis_resume);
  98. static int tpm_tis_synquacer_probe(struct platform_device *pdev)
  99. {
  100. struct tpm_tis_synquacer_info tpm_info = {};
  101. struct resource *res;
  102. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  103. if (res == NULL) {
  104. dev_err(&pdev->dev, "no memory resource defined\n");
  105. return -ENODEV;
  106. }
  107. tpm_info.res = *res;
  108. tpm_info.irq = -1;
  109. return tpm_tis_synquacer_init(&pdev->dev, &tpm_info);
  110. }
  111. static void tpm_tis_synquacer_remove(struct platform_device *pdev)
  112. {
  113. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  114. tpm_chip_unregister(chip);
  115. tpm_tis_remove(chip);
  116. }
  117. #ifdef CONFIG_OF
  118. static const struct of_device_id tis_synquacer_of_platform_match[] = {
  119. {.compatible = "socionext,synquacer-tpm-mmio"},
  120. {},
  121. };
  122. MODULE_DEVICE_TABLE(of, tis_synquacer_of_platform_match);
  123. #endif
  124. #ifdef CONFIG_ACPI
  125. static const struct acpi_device_id tpm_synquacer_acpi_tbl[] = {
  126. { "SCX0009" },
  127. {},
  128. };
  129. MODULE_DEVICE_TABLE(acpi, tpm_synquacer_acpi_tbl);
  130. #endif
  131. static struct platform_driver tis_synquacer_drv = {
  132. .probe = tpm_tis_synquacer_probe,
  133. .remove_new = tpm_tis_synquacer_remove,
  134. .driver = {
  135. .name = "tpm_tis_synquacer",
  136. .pm = &tpm_tis_synquacer_pm,
  137. .of_match_table = of_match_ptr(tis_synquacer_of_platform_match),
  138. .acpi_match_table = ACPI_PTR(tpm_synquacer_acpi_tbl),
  139. },
  140. };
  141. module_platform_driver(tis_synquacer_drv);
  142. MODULE_DESCRIPTION("TPM MMIO Driver for Socionext SynQuacer platform");
  143. MODULE_LICENSE("GPL");