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
19 struct tpm_atmel_priv
{
26 #define atmel_getb(chip, offset) inb(atmel_get_priv(chip)->base + (offset))
27 #define atmel_putb(val, chip, offset) \
28 outb(val, atmel_get_priv(chip)->base + (offset))
29 #define atmel_request_region request_region
30 #define atmel_release_region release_region
31 /* Atmel definitions */
33 TPM_ATMEL_BASE_ADDR_LO
= 0x08,
34 TPM_ATMEL_BASE_ADDR_HI
= 0x09
37 static inline int tpm_read_index(int base
, int index
)
40 return inb(base
+ 1) & 0xFF;
43 /* Verify this is a 1.1 Atmel TPM */
44 static int atmel_verify_tpm11(void)
46 /* verify that it is an Atmel part */
47 if (tpm_read_index(TPM_ADDR
, 4) != 'A' ||
48 tpm_read_index(TPM_ADDR
, 5) != 'T' ||
49 tpm_read_index(TPM_ADDR
, 6) != 'M' ||
50 tpm_read_index(TPM_ADDR
, 7) != 'L')
53 /* query chip for its version number */
54 if (tpm_read_index(TPM_ADDR
, 0x00) != 1 ||
55 tpm_read_index(TPM_ADDR
, 0x01) != 1)
58 /* This is an atmel supported part */
62 /* Determine where to talk to device */
63 static void __iomem
*atmel_get_base_addr(unsigned long *base
, int *region_size
)
67 if (atmel_verify_tpm11() != 0)
70 lo
= tpm_read_index(TPM_ADDR
, TPM_ATMEL_BASE_ADDR_LO
);
71 hi
= tpm_read_index(TPM_ADDR
, TPM_ATMEL_BASE_ADDR_HI
);
73 *base
= (hi
<< 8) | lo
;
76 return ioport_map(*base
, *region_size
);
79 /* write status bits */
80 enum tpm_atmel_write_status
{
81 ATML_STATUS_ABORT
= 0x01,
82 ATML_STATUS_LASTBYTE
= 0x04
84 /* read status bits */
85 enum tpm_atmel_read_status
{
86 ATML_STATUS_BUSY
= 0x01,
87 ATML_STATUS_DATA_AVAIL
= 0x02,
88 ATML_STATUS_REWRITE
= 0x04,
89 ATML_STATUS_READY
= 0x08
92 static int tpm_atml_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
94 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
95 u8 status
, *hdr
= buf
;
100 /* start reading header */
104 for (i
= 0; i
< 6; i
++) {
105 status
= ioread8(priv
->iobase
+ 1);
106 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
107 dev_err(&chip
->dev
, "error reading header\n");
110 *buf
++ = ioread8(priv
->iobase
);
113 /* size of the data received */
114 native_size
= (__force __be32
*) (hdr
+ 2);
115 size
= be32_to_cpu(*native_size
);
119 "Recv size(%d) less than available space\n", size
);
120 for (; i
< size
; i
++) { /* clear the waiting data anyway */
121 status
= ioread8(priv
->iobase
+ 1);
122 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
123 dev_err(&chip
->dev
, "error reading data\n");
130 /* read all the data available */
131 for (; i
< size
; i
++) {
132 status
= ioread8(priv
->iobase
+ 1);
133 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
134 dev_err(&chip
->dev
, "error reading data\n");
137 *buf
++ = ioread8(priv
->iobase
);
140 /* make sure data available is gone */
141 status
= ioread8(priv
->iobase
+ 1);
143 if (status
& ATML_STATUS_DATA_AVAIL
) {
144 dev_err(&chip
->dev
, "data available is stuck\n");
151 static int tpm_atml_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
153 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
156 dev_dbg(&chip
->dev
, "tpm_atml_send:\n");
157 for (i
= 0; i
< count
; i
++) {
158 dev_dbg(&chip
->dev
, "%d 0x%x(%d)\n", i
, buf
[i
], buf
[i
]);
159 iowrite8(buf
[i
], priv
->iobase
);
165 static void tpm_atml_cancel(struct tpm_chip
*chip
)
167 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
169 iowrite8(ATML_STATUS_ABORT
, priv
->iobase
+ 1);
172 static u8
tpm_atml_status(struct tpm_chip
*chip
)
174 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
176 return ioread8(priv
->iobase
+ 1);
179 static bool tpm_atml_req_canceled(struct tpm_chip
*chip
, u8 status
)
181 return (status
== ATML_STATUS_READY
);
184 static const struct tpm_class_ops tpm_atmel
= {
185 .recv
= tpm_atml_recv
,
186 .send
= tpm_atml_send
,
187 .cancel
= tpm_atml_cancel
,
188 .status
= tpm_atml_status
,
189 .req_complete_mask
= ATML_STATUS_BUSY
| ATML_STATUS_DATA_AVAIL
,
190 .req_complete_val
= ATML_STATUS_DATA_AVAIL
,
191 .req_canceled
= tpm_atml_req_canceled
,
194 static struct platform_device
*pdev
;
196 static void atml_plat_remove(void)
198 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
199 struct tpm_atmel_priv
*priv
= dev_get_drvdata(&chip
->dev
);
201 tpm_chip_unregister(chip
);
202 if (priv
->have_region
)
203 atmel_release_region(priv
->base
, priv
->region_size
);
204 platform_device_unregister(pdev
);
207 static SIMPLE_DEV_PM_OPS(tpm_atml_pm
, tpm_pm_suspend
, tpm_pm_resume
);
209 static struct platform_driver atml_drv
= {
216 static int __init
init_atmel(void)
219 void __iomem
*iobase
= NULL
;
220 int have_region
, region_size
;
222 struct tpm_chip
*chip
;
223 struct tpm_atmel_priv
*priv
;
225 rc
= platform_driver_register(&atml_drv
);
229 if ((iobase
= atmel_get_base_addr(&base
, ®ion_size
)) == NULL
) {
235 (atmel_request_region
236 (base
, region_size
, "tpm_atmel0") == NULL
) ? 0 : 1;
238 pdev
= platform_device_register_simple("tpm_atmel", -1, NULL
, 0);
244 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
250 priv
->iobase
= iobase
;
252 priv
->have_region
= have_region
;
253 priv
->region_size
= region_size
;
255 chip
= tpmm_chip_alloc(&pdev
->dev
, &tpm_atmel
);
261 dev_set_drvdata(&chip
->dev
, priv
);
263 rc
= tpm_chip_register(chip
);
270 platform_device_unregister(pdev
);
273 atmel_release_region(base
,
276 platform_driver_unregister(&atml_drv
);
280 static void __exit
cleanup_atmel(void)
282 platform_driver_unregister(&atml_drv
);
286 module_init(init_atmel
);
287 module_exit(cleanup_atmel
);
289 MODULE_AUTHOR("Leendert van Doorn <leendert@watson.ibm.com>");
290 MODULE_DESCRIPTION("TPM Driver");
291 MODULE_VERSION("2.0");
292 MODULE_LICENSE("GPL");