1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 IBM Corporation
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Dave Safford <safford@watson.ibm.com>
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Kylene Hall <kjhall@us.ibm.com>
11 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13 * Device driver for TCG/TCPA TPM (trusted platform module).
14 * Specifications at www.trustedcomputinggroup.org
18 #include "tpm_atmel.h"
20 /* write status bits */
21 enum tpm_atmel_write_status
{
22 ATML_STATUS_ABORT
= 0x01,
23 ATML_STATUS_LASTBYTE
= 0x04
25 /* read status bits */
26 enum tpm_atmel_read_status
{
27 ATML_STATUS_BUSY
= 0x01,
28 ATML_STATUS_DATA_AVAIL
= 0x02,
29 ATML_STATUS_REWRITE
= 0x04,
30 ATML_STATUS_READY
= 0x08
33 static int tpm_atml_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
35 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
36 u8 status
, *hdr
= buf
;
41 /* start reading header */
45 for (i
= 0; i
< 6; i
++) {
46 status
= ioread8(priv
->iobase
+ 1);
47 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
48 dev_err(&chip
->dev
, "error reading header\n");
51 *buf
++ = ioread8(priv
->iobase
);
54 /* size of the data received */
55 native_size
= (__force __be32
*) (hdr
+ 2);
56 size
= be32_to_cpu(*native_size
);
60 "Recv size(%d) less than available space\n", size
);
61 for (; i
< size
; i
++) { /* clear the waiting data anyway */
62 status
= ioread8(priv
->iobase
+ 1);
63 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
64 dev_err(&chip
->dev
, "error reading data\n");
71 /* read all the data available */
72 for (; i
< size
; i
++) {
73 status
= ioread8(priv
->iobase
+ 1);
74 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
75 dev_err(&chip
->dev
, "error reading data\n");
78 *buf
++ = ioread8(priv
->iobase
);
81 /* make sure data available is gone */
82 status
= ioread8(priv
->iobase
+ 1);
84 if (status
& ATML_STATUS_DATA_AVAIL
) {
85 dev_err(&chip
->dev
, "data available is stuck\n");
92 static int tpm_atml_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
94 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
97 dev_dbg(&chip
->dev
, "tpm_atml_send:\n");
98 for (i
= 0; i
< count
; i
++) {
99 dev_dbg(&chip
->dev
, "%d 0x%x(%d)\n", i
, buf
[i
], buf
[i
]);
100 iowrite8(buf
[i
], priv
->iobase
);
106 static void tpm_atml_cancel(struct tpm_chip
*chip
)
108 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
110 iowrite8(ATML_STATUS_ABORT
, priv
->iobase
+ 1);
113 static u8
tpm_atml_status(struct tpm_chip
*chip
)
115 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
117 return ioread8(priv
->iobase
+ 1);
120 static bool tpm_atml_req_canceled(struct tpm_chip
*chip
, u8 status
)
122 return (status
== ATML_STATUS_READY
);
125 static const struct tpm_class_ops tpm_atmel
= {
126 .recv
= tpm_atml_recv
,
127 .send
= tpm_atml_send
,
128 .cancel
= tpm_atml_cancel
,
129 .status
= tpm_atml_status
,
130 .req_complete_mask
= ATML_STATUS_BUSY
| ATML_STATUS_DATA_AVAIL
,
131 .req_complete_val
= ATML_STATUS_DATA_AVAIL
,
132 .req_canceled
= tpm_atml_req_canceled
,
135 static struct platform_device
*pdev
;
137 static void atml_plat_remove(void)
139 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
140 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
142 tpm_chip_unregister(chip
);
143 if (priv
->have_region
)
144 atmel_release_region(priv
->base
, priv
->region_size
);
145 atmel_put_base_addr(priv
->iobase
);
146 platform_device_unregister(pdev
);
149 static SIMPLE_DEV_PM_OPS(tpm_atml_pm
, tpm_pm_suspend
, tpm_pm_resume
);
151 static struct platform_driver atml_drv
= {
158 static int __init
init_atmel(void)
161 void __iomem
*iobase
= NULL
;
162 int have_region
, region_size
;
164 struct tpm_chip
*chip
;
165 struct tpm_atmel_priv
*priv
;
167 rc
= platform_driver_register(&atml_drv
);
171 if ((iobase
= atmel_get_base_addr(&base
, ®ion_size
)) == NULL
) {
177 (atmel_request_region
178 (base
, region_size
, "tpm_atmel0") == NULL
) ? 0 : 1;
180 pdev
= platform_device_register_simple("tpm_atmel", -1, NULL
, 0);
186 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
192 priv
->iobase
= iobase
;
194 priv
->have_region
= have_region
;
195 priv
->region_size
= region_size
;
197 chip
= tpmm_chip_alloc(&pdev
->dev
, &tpm_atmel
);
203 dev_set_drvdata(&chip
->dev
, priv
);
205 rc
= tpm_chip_register(chip
);
212 platform_device_unregister(pdev
);
214 atmel_put_base_addr(iobase
);
216 atmel_release_region(base
,
219 platform_driver_unregister(&atml_drv
);
223 static void __exit
cleanup_atmel(void)
225 platform_driver_unregister(&atml_drv
);
229 module_init(init_atmel
);
230 module_exit(cleanup_atmel
);
232 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
233 MODULE_DESCRIPTION("TPM Driver");
234 MODULE_VERSION("2.0");
235 MODULE_LICENSE("GPL");