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
22 #include <linux/platform_device.h>
25 /* Atmel definitions */
27 TPM_ATMEL_BASE_ADDR_LO
= 0x08,
28 TPM_ATMEL_BASE_ADDR_HI
= 0x09
31 /* write status bits */
32 enum tpm_atmel_write_status
{
33 ATML_STATUS_ABORT
= 0x01,
34 ATML_STATUS_LASTBYTE
= 0x04
36 /* read status bits */
37 enum tpm_atmel_read_status
{
38 ATML_STATUS_BUSY
= 0x01,
39 ATML_STATUS_DATA_AVAIL
= 0x02,
40 ATML_STATUS_REWRITE
= 0x04,
41 ATML_STATUS_READY
= 0x08
44 static int tpm_atml_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
46 u8 status
, *hdr
= buf
;
51 /* start reading header */
55 for (i
= 0; i
< 6; i
++) {
56 status
= inb(chip
->vendor
->base
+ 1);
57 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
59 "error reading header\n");
62 *buf
++ = inb(chip
->vendor
->base
);
65 /* size of the data received */
66 native_size
= (__force __be32
*) (hdr
+ 2);
67 size
= be32_to_cpu(*native_size
);
71 "Recv size(%d) less than available space\n", size
);
72 for (; i
< size
; i
++) { /* clear the waiting data anyway */
73 status
= inb(chip
->vendor
->base
+ 1);
74 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
76 "error reading data\n");
83 /* read all the data available */
84 for (; i
< size
; i
++) {
85 status
= inb(chip
->vendor
->base
+ 1);
86 if ((status
& ATML_STATUS_DATA_AVAIL
) == 0) {
88 "error reading data\n");
91 *buf
++ = inb(chip
->vendor
->base
);
94 /* make sure data available is gone */
95 status
= inb(chip
->vendor
->base
+ 1);
96 if (status
& ATML_STATUS_DATA_AVAIL
) {
97 dev_err(chip
->dev
, "data available is stuck\n");
104 static int tpm_atml_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
108 dev_dbg(chip
->dev
, "tpm_atml_send:\n");
109 for (i
= 0; i
< count
; i
++) {
110 dev_dbg(chip
->dev
, "%d 0x%x(%d)\n", i
, buf
[i
], buf
[i
]);
111 outb(buf
[i
], chip
->vendor
->base
);
117 static void tpm_atml_cancel(struct tpm_chip
*chip
)
119 outb(ATML_STATUS_ABORT
, chip
->vendor
->base
+ 1);
122 static u8
tpm_atml_status(struct tpm_chip
*chip
)
124 return inb(chip
->vendor
->base
+ 1);
127 static struct file_operations atmel_ops
= {
128 .owner
= THIS_MODULE
,
133 .release
= tpm_release
,
136 static DEVICE_ATTR(pubek
, S_IRUGO
, tpm_show_pubek
, NULL
);
137 static DEVICE_ATTR(pcrs
, S_IRUGO
, tpm_show_pcrs
, NULL
);
138 static DEVICE_ATTR(caps
, S_IRUGO
, tpm_show_caps
, NULL
);
139 static DEVICE_ATTR(cancel
, S_IWUSR
|S_IWGRP
, NULL
, tpm_store_cancel
);
141 static struct attribute
* atmel_attrs
[] = {
142 &dev_attr_pubek
.attr
,
145 &dev_attr_cancel
.attr
,
149 static struct attribute_group atmel_attr_grp
= { .attrs
= atmel_attrs
};
151 static struct tpm_vendor_specific tpm_atmel
= {
152 .recv
= tpm_atml_recv
,
153 .send
= tpm_atml_send
,
154 .cancel
= tpm_atml_cancel
,
155 .status
= tpm_atml_status
,
156 .req_complete_mask
= ATML_STATUS_BUSY
| ATML_STATUS_DATA_AVAIL
,
157 .req_complete_val
= ATML_STATUS_DATA_AVAIL
,
158 .req_canceled
= ATML_STATUS_READY
,
159 .attr_group
= &atmel_attr_grp
,
160 .miscdev
= { .fops
= &atmel_ops
, },
163 static struct platform_device
*pdev
;
165 static void __devexit
tpm_atml_remove(struct device
*dev
)
167 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
169 release_region(chip
->vendor
->base
, 2);
170 tpm_remove_hardware(chip
->dev
);
174 static struct device_driver atml_drv
= {
176 .bus
= &platform_bus_type
,
177 .owner
= THIS_MODULE
,
178 .suspend
= tpm_pm_suspend
,
179 .resume
= tpm_pm_resume
,
182 static int __init
init_atmel(void)
187 driver_register(&atml_drv
);
189 lo
= tpm_read_index(TPM_ADDR
, TPM_ATMEL_BASE_ADDR_LO
);
190 hi
= tpm_read_index(TPM_ADDR
, TPM_ATMEL_BASE_ADDR_HI
);
192 tpm_atmel
.base
= (hi
<<8)|lo
;
194 /* verify that it is an Atmel part */
195 if (tpm_read_index(TPM_ADDR
, 4) != 'A' || tpm_read_index(TPM_ADDR
, 5) != 'T'
196 || tpm_read_index(TPM_ADDR
, 6) != 'M' || tpm_read_index(TPM_ADDR
, 7) != 'L') {
200 /* verify chip version number is 1.1 */
201 if ( (tpm_read_index(TPM_ADDR
, 0x00) != 0x01) ||
202 (tpm_read_index(TPM_ADDR
, 0x01) != 0x01 ))
205 pdev
= kzalloc(sizeof(struct platform_device
), GFP_KERNEL
);
209 pdev
->name
= "tpm_atmel0";
211 pdev
->num_resources
= 0;
212 pdev
->dev
.release
= tpm_atml_remove
;
213 pdev
->dev
.driver
= &atml_drv
;
215 if ((rc
= platform_device_register(pdev
)) < 0) {
221 if (request_region(tpm_atmel
.base
, 2, "tpm_atmel0") == NULL
) {
222 platform_device_unregister(pdev
);
228 if ((rc
= tpm_register_hardware(&pdev
->dev
, &tpm_atmel
)) < 0) {
229 release_region(tpm_atmel
.base
, 2);
230 platform_device_unregister(pdev
);
236 dev_info(&pdev
->dev
, "Atmel TPM 1.1, Base Address: 0x%x\n",
241 static void __exit
cleanup_atmel(void)
244 tpm_atml_remove(&pdev
->dev
);
245 platform_device_unregister(pdev
);
250 driver_unregister(&atml_drv
);
253 module_init(init_atmel
);
254 module_exit(cleanup_atmel
);
256 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
257 MODULE_DESCRIPTION("TPM Driver");
258 MODULE_VERSION("2.0");
259 MODULE_LICENSE("GPL");