1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Linaro Ltd.
5 * This device driver implements MMIO TPM on SynQuacer Platform.
7 #include <linux/acpi.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #include <linux/of_device.h>
13 #include <linux/kernel.h>
15 #include "tpm_tis_core.h"
18 * irq > 0 means: use irq $irq;
19 * irq = 0 means: autoprobe for an irq;
20 * irq = -1 means: no irq support
22 struct tpm_tis_synquacer_info
{
27 struct tpm_tis_synquacer_phy
{
28 struct tpm_tis_data priv
;
32 static inline struct tpm_tis_synquacer_phy
*to_tpm_tis_tcg_phy(struct tpm_tis_data
*data
)
34 return container_of(data
, struct tpm_tis_synquacer_phy
, priv
);
37 static int tpm_tis_synquacer_read_bytes(struct tpm_tis_data
*data
, u32 addr
,
40 struct tpm_tis_synquacer_phy
*phy
= to_tpm_tis_tcg_phy(data
);
43 *result
++ = ioread8(phy
->iobase
+ addr
);
48 static int tpm_tis_synquacer_write_bytes(struct tpm_tis_data
*data
, u32 addr
,
49 u16 len
, const u8
*value
)
51 struct tpm_tis_synquacer_phy
*phy
= to_tpm_tis_tcg_phy(data
);
54 iowrite8(*value
++, phy
->iobase
+ addr
);
59 static int tpm_tis_synquacer_read16_bw(struct tpm_tis_data
*data
,
60 u32 addr
, u16
*result
)
62 struct tpm_tis_synquacer_phy
*phy
= to_tpm_tis_tcg_phy(data
);
65 * Due to the limitation of SPI controller on SynQuacer,
66 * 16/32 bits access must be done in byte-wise and descending order.
68 *result
= (ioread8(phy
->iobase
+ addr
+ 1) << 8) |
69 (ioread8(phy
->iobase
+ addr
));
74 static int tpm_tis_synquacer_read32_bw(struct tpm_tis_data
*data
,
75 u32 addr
, u32
*result
)
77 struct tpm_tis_synquacer_phy
*phy
= to_tpm_tis_tcg_phy(data
);
80 * Due to the limitation of SPI controller on SynQuacer,
81 * 16/32 bits access must be done in byte-wise and descending order.
83 *result
= (ioread8(phy
->iobase
+ addr
+ 3) << 24) |
84 (ioread8(phy
->iobase
+ addr
+ 2) << 16) |
85 (ioread8(phy
->iobase
+ addr
+ 1) << 8) |
86 (ioread8(phy
->iobase
+ addr
));
91 static int tpm_tis_synquacer_write32_bw(struct tpm_tis_data
*data
,
94 struct tpm_tis_synquacer_phy
*phy
= to_tpm_tis_tcg_phy(data
);
97 * Due to the limitation of SPI controller on SynQuacer,
98 * 16/32 bits access must be done in byte-wise and descending order.
100 iowrite8(value
>> 24, phy
->iobase
+ addr
+ 3);
101 iowrite8(value
>> 16, phy
->iobase
+ addr
+ 2);
102 iowrite8(value
>> 8, phy
->iobase
+ addr
+ 1);
103 iowrite8(value
, phy
->iobase
+ addr
);
108 static const struct tpm_tis_phy_ops tpm_tcg_bw
= {
109 .read_bytes
= tpm_tis_synquacer_read_bytes
,
110 .write_bytes
= tpm_tis_synquacer_write_bytes
,
111 .read16
= tpm_tis_synquacer_read16_bw
,
112 .read32
= tpm_tis_synquacer_read32_bw
,
113 .write32
= tpm_tis_synquacer_write32_bw
,
116 static int tpm_tis_synquacer_init(struct device
*dev
,
117 struct tpm_tis_synquacer_info
*tpm_info
)
119 struct tpm_tis_synquacer_phy
*phy
;
121 phy
= devm_kzalloc(dev
, sizeof(struct tpm_tis_synquacer_phy
), GFP_KERNEL
);
125 phy
->iobase
= devm_ioremap_resource(dev
, &tpm_info
->res
);
126 if (IS_ERR(phy
->iobase
))
127 return PTR_ERR(phy
->iobase
);
129 return tpm_tis_core_init(dev
, &phy
->priv
, tpm_info
->irq
, &tpm_tcg_bw
,
133 static SIMPLE_DEV_PM_OPS(tpm_tis_synquacer_pm
, tpm_pm_suspend
, tpm_tis_resume
);
135 static int tpm_tis_synquacer_probe(struct platform_device
*pdev
)
137 struct tpm_tis_synquacer_info tpm_info
= {};
138 struct resource
*res
;
140 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
142 dev_err(&pdev
->dev
, "no memory resource defined\n");
149 return tpm_tis_synquacer_init(&pdev
->dev
, &tpm_info
);
152 static int tpm_tis_synquacer_remove(struct platform_device
*pdev
)
154 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
156 tpm_chip_unregister(chip
);
157 tpm_tis_remove(chip
);
163 static const struct of_device_id tis_synquacer_of_platform_match
[] = {
164 {.compatible
= "socionext,synquacer-tpm-mmio"},
167 MODULE_DEVICE_TABLE(of
, tis_synquacer_of_platform_match
);
171 static const struct acpi_device_id tpm_synquacer_acpi_tbl
[] = {
175 MODULE_DEVICE_TABLE(acpi
, tpm_synquacer_acpi_tbl
);
178 static struct platform_driver tis_synquacer_drv
= {
179 .probe
= tpm_tis_synquacer_probe
,
180 .remove
= tpm_tis_synquacer_remove
,
182 .name
= "tpm_tis_synquacer",
183 .pm
= &tpm_tis_synquacer_pm
,
184 .of_match_table
= of_match_ptr(tis_synquacer_of_platform_match
),
185 .acpi_match_table
= ACPI_PTR(tpm_synquacer_acpi_tbl
),
189 static int __init
tpm_tis_synquacer_module_init(void)
193 rc
= platform_driver_register(&tis_synquacer_drv
);
200 static void __exit
tpm_tis_synquacer_module_exit(void)
202 platform_driver_unregister(&tis_synquacer_drv
);
205 module_init(tpm_tis_synquacer_module_init
);
206 module_exit(tpm_tis_synquacer_module_exit
);
207 MODULE_DESCRIPTION("TPM MMIO Driver for Socionext SynQuacer platform");
208 MODULE_LICENSE("GPL");