2 * Copyright (C) 2004 IBM Corporation
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the
23 #include "tpm_atmel.h"
25 /* write status bits */
26 enum tpm_atmel_write_status
{
27 ATML_STATUS_ABORT
= 0x01,
28 ATML_STATUS_LASTBYTE
= 0x04
30 /* read status bits */
31 enum tpm_atmel_read_status
{
32 ATML_STATUS_BUSY
= 0x01,
33 ATML_STATUS_DATA_AVAIL
= 0x02,
34 ATML_STATUS_REWRITE
= 0x04,
35 ATML_STATUS_READY
= 0x08
38 static int tpm_atml_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
40 u8 status
, *hdr
= buf
;
45 /* start reading header */
49 for (i
= 0; i
< 6; i
++) {
50 status
= ioread8(chip
->vendor
.iobase
+ 1);
51 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
52 dev_err(chip
->dev
, "error reading header\n");
55 *buf
++ = ioread8(chip
->vendor
.iobase
);
58 /* size of the data received */
59 native_size
= (__force __be32
*) (hdr
+ 2);
60 size
= be32_to_cpu(*native_size
);
64 "Recv size(%d) less than available space\n", size
);
65 for (; i
< size
; i
++) { /* clear the waiting data anyway */
66 status
= ioread8(chip
->vendor
.iobase
+ 1);
67 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
68 dev_err(chip
->dev
, "error reading data\n");
75 /* read all the data available */
76 for (; i
< size
; i
++) {
77 status
= ioread8(chip
->vendor
.iobase
+ 1);
78 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
79 dev_err(chip
->dev
, "error reading data\n");
82 *buf
++ = ioread8(chip
->vendor
.iobase
);
85 /* make sure data available is gone */
86 status
= ioread8(chip
->vendor
.iobase
+ 1);
88 if (status
& ATML_STATUS_DATA_AVAIL
) {
89 dev_err(chip
->dev
, "data available is stuck\n");
96 static int tpm_atml_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
100 dev_dbg(chip
->dev
, "tpm_atml_send:\n");
101 for (i
= 0; i
< count
; i
++) {
102 dev_dbg(chip
->dev
, "%d 0x%x(%d)\n", i
, buf
[i
], buf
[i
]);
103 iowrite8(buf
[i
], chip
->vendor
.iobase
);
109 static void tpm_atml_cancel(struct tpm_chip
*chip
)
111 iowrite8(ATML_STATUS_ABORT
, chip
->vendor
.iobase
+ 1);
114 static u8
tpm_atml_status(struct tpm_chip
*chip
)
116 return ioread8(chip
->vendor
.iobase
+ 1);
119 static bool tpm_atml_req_canceled(struct tpm_chip
*chip
, u8 status
)
121 return (status
== ATML_STATUS_READY
);
124 static const struct file_operations atmel_ops
= {
125 .owner
= THIS_MODULE
,
130 .release
= tpm_release
,
133 static DEVICE_ATTR(pubek
, S_IRUGO
, tpm_show_pubek
, NULL
);
134 static DEVICE_ATTR(pcrs
, S_IRUGO
, tpm_show_pcrs
, NULL
);
135 static DEVICE_ATTR(caps
, S_IRUGO
, tpm_show_caps
, NULL
);
136 static DEVICE_ATTR(cancel
, S_IWUSR
|S_IWGRP
, NULL
, tpm_store_cancel
);
138 static struct attribute
* atmel_attrs
[] = {
139 &dev_attr_pubek
.attr
,
142 &dev_attr_cancel
.attr
,
146 static struct attribute_group atmel_attr_grp
= { .attrs
= atmel_attrs
};
148 static const struct tpm_vendor_specific tpm_atmel
= {
149 .recv
= tpm_atml_recv
,
150 .send
= tpm_atml_send
,
151 .cancel
= tpm_atml_cancel
,
152 .status
= tpm_atml_status
,
153 .req_complete_mask
= ATML_STATUS_BUSY
| ATML_STATUS_DATA_AVAIL
,
154 .req_complete_val
= ATML_STATUS_DATA_AVAIL
,
155 .req_canceled
= tpm_atml_req_canceled
,
156 .attr_group
= &atmel_attr_grp
,
157 .miscdev
= { .fops
= &atmel_ops
, },
160 static struct platform_device
*pdev
;
162 static void atml_plat_remove(void)
164 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
167 if (chip
->vendor
.have_region
)
168 atmel_release_region(chip
->vendor
.base
,
169 chip
->vendor
.region_size
);
170 atmel_put_base_addr(chip
->vendor
.iobase
);
171 tpm_remove_hardware(chip
->dev
);
172 platform_device_unregister(pdev
);
176 static SIMPLE_DEV_PM_OPS(tpm_atml_pm
, tpm_pm_suspend
, tpm_pm_resume
);
178 static struct platform_driver atml_drv
= {
181 .owner
= THIS_MODULE
,
186 static int __init
init_atmel(void)
189 void __iomem
*iobase
= NULL
;
190 int have_region
, region_size
;
192 struct tpm_chip
*chip
;
194 rc
= platform_driver_register(&atml_drv
);
198 if ((iobase
= atmel_get_base_addr(&base
, ®ion_size
)) == NULL
) {
204 (atmel_request_region
205 (tpm_atmel
.base
, region_size
, "tpm_atmel0") == NULL
) ? 0 : 1;
207 pdev
= platform_device_register_simple("tpm_atmel", -1, NULL
, 0);
213 if (!(chip
= tpm_register_hardware(&pdev
->dev
, &tpm_atmel
))) {
218 chip
->vendor
.iobase
= iobase
;
219 chip
->vendor
.base
= base
;
220 chip
->vendor
.have_region
= have_region
;
221 chip
->vendor
.region_size
= region_size
;
226 platform_device_unregister(pdev
);
228 atmel_put_base_addr(iobase
);
230 atmel_release_region(base
,
233 platform_driver_unregister(&atml_drv
);
237 static void __exit
cleanup_atmel(void)
239 platform_driver_unregister(&atml_drv
);
243 module_init(init_atmel
);
244 module_exit(cleanup_atmel
);
246 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
247 MODULE_DESCRIPTION("TPM Driver");
248 MODULE_VERSION("2.0");
249 MODULE_LICENSE("GPL");