summaryrefslogtreecommitdiff
path: root/drivers/char/tpm/tpm_tis_synquacer.c
blob: e47bdd27270492f5176f2fcc7a549c17fc5d20be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020 Linaro Ltd.
 *
 * This device driver implements MMIO TPM on SynQuacer Platform.
 */
#include <linux/acpi.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/kernel.h>
#include "tpm.h"
#include "tpm_tis_core.h"

/*
 * irq > 0 means: use irq $irq;
 * irq = 0 means: autoprobe for an irq;
 * irq = -1 means: no irq support
 */
struct tpm_tis_synquacer_info {
	struct resource res;
	int irq;
};

struct tpm_tis_synquacer_phy {
	struct tpm_tis_data priv;
	void __iomem *iobase;
};

static inline struct tpm_tis_synquacer_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
{
	return container_of(data, struct tpm_tis_synquacer_phy, priv);
}

static int tpm_tis_synquacer_read_bytes(struct tpm_tis_data *data, u32 addr,
					u16 len, u8 *result)
{
	struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);

	while (len--)
		*result++ = ioread8(phy->iobase + addr);

	return 0;
}

static int tpm_tis_synquacer_write_bytes(struct tpm_tis_data *data, u32 addr,
					 u16 len, const u8 *value)
{
	struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);

	while (len--)
		iowrite8(*value++, phy->iobase + addr);

	return 0;
}

static int tpm_tis_synquacer_read16_bw(struct tpm_tis_data *data,
				       u32 addr, u16 *result)
{
	struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);

	/*
	 * Due to the limitation of SPI controller on SynQuacer,
	 * 16/32 bits access must be done in byte-wise and descending order.
	 */
	*result = (ioread8(phy->iobase + addr + 1) << 8) |
		  (ioread8(phy->iobase + addr));

	return 0;
}

static int tpm_tis_synquacer_read32_bw(struct tpm_tis_data *data,
				       u32 addr, u32 *result)
{
	struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);

	/*
	 * Due to the limitation of SPI controller on SynQuacer,
	 * 16/32 bits access must be done in byte-wise and descending order.
	 */
	*result = (ioread8(phy->iobase + addr + 3) << 24) |
		  (ioread8(phy->iobase + addr + 2) << 16) |
		  (ioread8(phy->iobase + addr + 1) << 8) |
		  (ioread8(phy->iobase + addr));

	return 0;
}

static int tpm_tis_synquacer_write32_bw(struct tpm_tis_data *data,
					u32 addr, u32 value)
{
	struct tpm_tis_synquacer_phy *phy = to_tpm_tis_tcg_phy(data);

	/*
	 * Due to the limitation of SPI controller on SynQuacer,
	 * 16/32 bits access must be done in byte-wise and descending order.
	 */
	iowrite8(value >> 24, phy->iobase + addr + 3);
	iowrite8(value >> 16, phy->iobase + addr + 2);
	iowrite8(value >> 8, phy->iobase + addr + 1);
	iowrite8(value, phy->iobase + addr);

	return 0;
}

static const struct tpm_tis_phy_ops tpm_tcg_bw = {
	.read_bytes	= tpm_tis_synquacer_read_bytes,
	.write_bytes	= tpm_tis_synquacer_write_bytes,
	.read16		= tpm_tis_synquacer_read16_bw,
	.read32		= tpm_tis_synquacer_read32_bw,
	.write32	= tpm_tis_synquacer_write32_bw,
};

static int tpm_tis_synquacer_init(struct device *dev,
				  struct tpm_tis_synquacer_info *tpm_info)
{
	struct tpm_tis_synquacer_phy *phy;

	phy = devm_kzalloc(dev, sizeof(struct tpm_tis_synquacer_phy), GFP_KERNEL);
	if (phy == NULL)
		return -ENOMEM;

	phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
	if (IS_ERR(phy->iobase))
		return PTR_ERR(phy->iobase);

	return tpm_tis_core_init(dev, &phy->priv, tpm_info->irq, &tpm_tcg_bw,
				 ACPI_HANDLE(dev));
}

static SIMPLE_DEV_PM_OPS(tpm_tis_synquacer_pm, tpm_pm_suspend, tpm_tis_resume);

static int tpm_tis_synquacer_probe(struct platform_device *pdev)
{
	struct tpm_tis_synquacer_info tpm_info = {};
	struct resource *res;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		dev_err(&pdev->dev, "no memory resource defined\n");
		return -ENODEV;
	}
	tpm_info.res = *res;

	tpm_info.irq = -1;

	return tpm_tis_synquacer_init(&pdev->dev, &tpm_info);
}

static int tpm_tis_synquacer_remove(struct platform_device *pdev)
{
	struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);

	tpm_chip_unregister(chip);
	tpm_tis_remove(chip);

	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id tis_synquacer_of_platform_match[] = {
	{.compatible = "socionext,synquacer-tpm-mmio"},
	{},
};
MODULE_DEVICE_TABLE(of, tis_synquacer_of_platform_match);
#endif

#ifdef CONFIG_ACPI
static const struct acpi_device_id tpm_synquacer_acpi_tbl[] = {
	{ "SCX0009" },
	{},
};
MODULE_DEVICE_TABLE(acpi, tpm_synquacer_acpi_tbl);
#endif

static struct platform_driver tis_synquacer_drv = {
	.probe = tpm_tis_synquacer_probe,
	.remove = tpm_tis_synquacer_remove,
	.driver = {
		.name		= "tpm_tis_synquacer",
		.pm		= &tpm_tis_synquacer_pm,
		.of_match_table = of_match_ptr(tis_synquacer_of_platform_match),
		.acpi_match_table = ACPI_PTR(tpm_synquacer_acpi_tbl),
	},
};

static int __init tpm_tis_synquacer_module_init(void)
{
	int rc;

	rc = platform_driver_register(&tis_synquacer_drv);
	if (rc)
		return rc;

	return 0;
}

static void __exit tpm_tis_synquacer_module_exit(void)
{
	platform_driver_unregister(&tis_synquacer_drv);
}

module_init(tpm_tis_synquacer_module_init);
module_exit(tpm_tis_synquacer_module_exit);
MODULE_DESCRIPTION("TPM MMIO Driver for Socionext SynQuacer platform");
MODULE_LICENSE("GPL");