xref: /linux/drivers/char/tpm/tpm_tis_synquacer.c (revision 2ccf8c76b4fb38deb25af3830aa9489b6a374621)
1d5ae56a4SMasahisa Kojima // SPDX-License-Identifier: GPL-2.0
2d5ae56a4SMasahisa Kojima /*
3d5ae56a4SMasahisa Kojima  * Copyright (C) 2020 Linaro Ltd.
4d5ae56a4SMasahisa Kojima  *
5d5ae56a4SMasahisa Kojima  * This device driver implements MMIO TPM on SynQuacer Platform.
6d5ae56a4SMasahisa Kojima  */
7d5ae56a4SMasahisa Kojima #include <linux/acpi.h>
8d5ae56a4SMasahisa Kojima #include <linux/init.h>
9d5ae56a4SMasahisa Kojima #include <linux/module.h>
10d5ae56a4SMasahisa Kojima #include <linux/slab.h>
11d5ae56a4SMasahisa Kojima #include <linux/of.h>
12d5ae56a4SMasahisa Kojima #include <linux/of_device.h>
13d5ae56a4SMasahisa Kojima #include <linux/kernel.h>
14d5ae56a4SMasahisa Kojima #include "tpm.h"
15d5ae56a4SMasahisa Kojima #include "tpm_tis_core.h"
16d5ae56a4SMasahisa Kojima 
17d5ae56a4SMasahisa Kojima /*
18d5ae56a4SMasahisa Kojima  * irq > 0 means: use irq $irq;
19d5ae56a4SMasahisa Kojima  * irq = 0 means: autoprobe for an irq;
20d5ae56a4SMasahisa Kojima  * irq = -1 means: no irq support
21d5ae56a4SMasahisa Kojima  */
22d5ae56a4SMasahisa Kojima struct tpm_tis_synquacer_info {
23d5ae56a4SMasahisa Kojima 	struct resource res;
24d5ae56a4SMasahisa Kojima 	int irq;
25d5ae56a4SMasahisa Kojima };
26d5ae56a4SMasahisa Kojima 
27d5ae56a4SMasahisa Kojima struct tpm_tis_synquacer_phy {
28d5ae56a4SMasahisa Kojima 	struct tpm_tis_data priv;
29d5ae56a4SMasahisa Kojima 	void __iomem *iobase;
30d5ae56a4SMasahisa Kojima };
31d5ae56a4SMasahisa Kojima 
32d5ae56a4SMasahisa Kojima static inline struct tpm_tis_synquacer_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
33d5ae56a4SMasahisa Kojima {
34d5ae56a4SMasahisa Kojima 	return container_of(data, struct tpm_tis_synquacer_phy, priv);
35d5ae56a4SMasahisa Kojima }
36d5ae56a4SMasahisa Kojima 
37d5ae56a4SMasahisa Kojima static int tpm_tis_synquacer_read_bytes(struct tpm_tis_data *data, u32 addr,
386422cbd3SJohannes Holland 					u16 len, u8 *result,
396422cbd3SJohannes Holland 					enum tpm_tis_io_mode io_mode)
40d5ae56a4SMasahisa Kojima {
41d5ae56a4SMasahisa Kojima 	struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);
426422cbd3SJohannes Holland 	switch (io_mode) {
436422cbd3SJohannes Holland 	case TPM_TIS_PHYS_8:
44d5ae56a4SMasahisa Kojima 		while (len--)
45d5ae56a4SMasahisa Kojima 			*result++ = ioread8(phy->iobase + addr);
466422cbd3SJohannes Holland 		break;
476422cbd3SJohannes Holland 	case TPM_TIS_PHYS_16:
486422cbd3SJohannes Holland 		result[1] = ioread8(phy->iobase + addr + 1);
496422cbd3SJohannes Holland 		result[0] = ioread8(phy->iobase + addr);
506422cbd3SJohannes Holland 		break;
516422cbd3SJohannes Holland 	case TPM_TIS_PHYS_32:
526422cbd3SJohannes Holland 		result[3] = ioread8(phy->iobase + addr + 3);
536422cbd3SJohannes Holland 		result[2] = ioread8(phy->iobase + addr + 2);
546422cbd3SJohannes Holland 		result[1] = ioread8(phy->iobase + addr + 1);
556422cbd3SJohannes Holland 		result[0] = ioread8(phy->iobase + addr);
566422cbd3SJohannes Holland 		break;
576422cbd3SJohannes Holland 	}
58d5ae56a4SMasahisa Kojima 
59d5ae56a4SMasahisa Kojima 	return 0;
60d5ae56a4SMasahisa Kojima }
61d5ae56a4SMasahisa Kojima 
62d5ae56a4SMasahisa Kojima static int tpm_tis_synquacer_write_bytes(struct tpm_tis_data *data, u32 addr,
636422cbd3SJohannes Holland 					 u16 len, const u8 *value,
646422cbd3SJohannes Holland 					 enum tpm_tis_io_mode io_mode)
65d5ae56a4SMasahisa Kojima {
66d5ae56a4SMasahisa Kojima 	struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);
676422cbd3SJohannes Holland 	switch (io_mode) {
686422cbd3SJohannes Holland 	case TPM_TIS_PHYS_8:
69d5ae56a4SMasahisa Kojima 		while (len--)
70d5ae56a4SMasahisa Kojima 			iowrite8(*value++, phy->iobase + addr);
716422cbd3SJohannes Holland 		break;
726422cbd3SJohannes Holland 	case TPM_TIS_PHYS_16:
736422cbd3SJohannes Holland 		return -EINVAL;
746422cbd3SJohannes Holland 	case TPM_TIS_PHYS_32:
75d5ae56a4SMasahisa Kojima 		/*
76d5ae56a4SMasahisa Kojima 		 * Due to the limitation of SPI controller on SynQuacer,
77d5ae56a4SMasahisa Kojima 		 * 16/32 bits access must be done in byte-wise and descending order.
78d5ae56a4SMasahisa Kojima 		 */
796422cbd3SJohannes Holland 		iowrite8(value[3], phy->iobase + addr + 3);
806422cbd3SJohannes Holland 		iowrite8(value[2], phy->iobase + addr + 2);
816422cbd3SJohannes Holland 		iowrite8(value[1], phy->iobase + addr + 1);
826422cbd3SJohannes Holland 		iowrite8(value[0], phy->iobase + addr);
836422cbd3SJohannes Holland 		break;
84d5ae56a4SMasahisa Kojima 	}
85d5ae56a4SMasahisa Kojima 
86d5ae56a4SMasahisa Kojima 	return 0;
87d5ae56a4SMasahisa Kojima }
88d5ae56a4SMasahisa Kojima 
89d5ae56a4SMasahisa Kojima static const struct tpm_tis_phy_ops tpm_tcg_bw = {
90d5ae56a4SMasahisa Kojima 	.read_bytes	= tpm_tis_synquacer_read_bytes,
91d5ae56a4SMasahisa Kojima 	.write_bytes	= tpm_tis_synquacer_write_bytes,
92d5ae56a4SMasahisa Kojima };
93d5ae56a4SMasahisa Kojima 
94d5ae56a4SMasahisa Kojima static int tpm_tis_synquacer_init(struct device *dev,
95d5ae56a4SMasahisa Kojima 				  struct tpm_tis_synquacer_info *tpm_info)
96d5ae56a4SMasahisa Kojima {
97d5ae56a4SMasahisa Kojima 	struct tpm_tis_synquacer_phy *phy;
98d5ae56a4SMasahisa Kojima 
99d5ae56a4SMasahisa Kojima 	phy = devm_kzalloc(dev, sizeof(struct tpm_tis_synquacer_phy), GFP_KERNEL);
100d5ae56a4SMasahisa Kojima 	if (phy == NULL)
101d5ae56a4SMasahisa Kojima 		return -ENOMEM;
102d5ae56a4SMasahisa Kojima 
103d5ae56a4SMasahisa Kojima 	phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
104d5ae56a4SMasahisa Kojima 	if (IS_ERR(phy->iobase))
105d5ae56a4SMasahisa Kojima 		return PTR_ERR(phy->iobase);
106d5ae56a4SMasahisa Kojima 
107d5ae56a4SMasahisa Kojima 	return tpm_tis_core_init(dev, &phy->priv, tpm_info->irq, &tpm_tcg_bw,
108d5ae56a4SMasahisa Kojima 				 ACPI_HANDLE(dev));
109d5ae56a4SMasahisa Kojima }
110d5ae56a4SMasahisa Kojima 
111d5ae56a4SMasahisa Kojima static SIMPLE_DEV_PM_OPS(tpm_tis_synquacer_pm, tpm_pm_suspend, tpm_tis_resume);
112d5ae56a4SMasahisa Kojima 
113d5ae56a4SMasahisa Kojima static int tpm_tis_synquacer_probe(struct platform_device *pdev)
114d5ae56a4SMasahisa Kojima {
115d5ae56a4SMasahisa Kojima 	struct tpm_tis_synquacer_info tpm_info = {};
116d5ae56a4SMasahisa Kojima 	struct resource *res;
117d5ae56a4SMasahisa Kojima 
118d5ae56a4SMasahisa Kojima 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
119d5ae56a4SMasahisa Kojima 	if (res == NULL) {
120d5ae56a4SMasahisa Kojima 		dev_err(&pdev->dev, "no memory resource defined\n");
121d5ae56a4SMasahisa Kojima 		return -ENODEV;
122d5ae56a4SMasahisa Kojima 	}
123d5ae56a4SMasahisa Kojima 	tpm_info.res = *res;
124d5ae56a4SMasahisa Kojima 
125d5ae56a4SMasahisa Kojima 	tpm_info.irq = -1;
126d5ae56a4SMasahisa Kojima 
127d5ae56a4SMasahisa Kojima 	return tpm_tis_synquacer_init(&pdev->dev, &tpm_info);
128d5ae56a4SMasahisa Kojima }
129d5ae56a4SMasahisa Kojima 
1307b69ef62SUwe Kleine-König static void tpm_tis_synquacer_remove(struct platform_device *pdev)
131d5ae56a4SMasahisa Kojima {
132d5ae56a4SMasahisa Kojima 	struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
133d5ae56a4SMasahisa Kojima 
134d5ae56a4SMasahisa Kojima 	tpm_chip_unregister(chip);
135d5ae56a4SMasahisa Kojima 	tpm_tis_remove(chip);
136d5ae56a4SMasahisa Kojima }
137d5ae56a4SMasahisa Kojima 
138d5ae56a4SMasahisa Kojima #ifdef CONFIG_OF
139d5ae56a4SMasahisa Kojima static const struct of_device_id tis_synquacer_of_platform_match[] = {
140d5ae56a4SMasahisa Kojima 	{.compatible = "socionext,synquacer-tpm-mmio"},
141d5ae56a4SMasahisa Kojima 	{},
142d5ae56a4SMasahisa Kojima };
143d5ae56a4SMasahisa Kojima MODULE_DEVICE_TABLE(of, tis_synquacer_of_platform_match);
144d5ae56a4SMasahisa Kojima #endif
145d5ae56a4SMasahisa Kojima 
146d5ae56a4SMasahisa Kojima #ifdef CONFIG_ACPI
147d5ae56a4SMasahisa Kojima static const struct acpi_device_id tpm_synquacer_acpi_tbl[] = {
148d5ae56a4SMasahisa Kojima 	{ "SCX0009" },
149d5ae56a4SMasahisa Kojima 	{},
150d5ae56a4SMasahisa Kojima };
151d5ae56a4SMasahisa Kojima MODULE_DEVICE_TABLE(acpi, tpm_synquacer_acpi_tbl);
152d5ae56a4SMasahisa Kojima #endif
153d5ae56a4SMasahisa Kojima 
154d5ae56a4SMasahisa Kojima static struct platform_driver tis_synquacer_drv = {
155d5ae56a4SMasahisa Kojima 	.probe = tpm_tis_synquacer_probe,
1567b69ef62SUwe Kleine-König 	.remove_new = tpm_tis_synquacer_remove,
157d5ae56a4SMasahisa Kojima 	.driver = {
158d5ae56a4SMasahisa Kojima 		.name		= "tpm_tis_synquacer",
159d5ae56a4SMasahisa Kojima 		.pm		= &tpm_tis_synquacer_pm,
160d5ae56a4SMasahisa Kojima 		.of_match_table = of_match_ptr(tis_synquacer_of_platform_match),
161d5ae56a4SMasahisa Kojima 		.acpi_match_table = ACPI_PTR(tpm_synquacer_acpi_tbl),
162d5ae56a4SMasahisa Kojima 	},
163d5ae56a4SMasahisa Kojima };
164d5ae56a4SMasahisa Kojima 
165*2ccf8c76SLi Zetao module_platform_driver(tis_synquacer_drv);
166d5ae56a4SMasahisa Kojima 
167d5ae56a4SMasahisa Kojima MODULE_DESCRIPTION("TPM MMIO Driver for Socionext SynQuacer platform");
168d5ae56a4SMasahisa Kojima MODULE_LICENSE("GPL");
169