Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[linux-2.6] / drivers / char / tpm / tpm_atmel.c
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  *
4  * Authors:
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>
9  *
10  * Maintained by: <tpmdd_devel@lists.sourceforge.net>
11  *
12  * Device driver for TCG/TCPA TPM (trusted platform module).
13  * Specifications at www.trustedcomputinggroup.org       
14  *
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
18  * License.
19  * 
20  */
21
22 #include "tpm.h"
23 #include "tpm_atmel.h"
24
25 /* write status bits */
26 enum tpm_atmel_write_status {
27         ATML_STATUS_ABORT = 0x01,
28         ATML_STATUS_LASTBYTE = 0x04
29 };
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
36 };
37
38 static int tpm_atml_recv(struct tpm_chip *chip, u8 *buf, size_t count)
39 {
40         u8 status, *hdr = buf;
41         u32 size;
42         int i;
43         __be32 *native_size;
44
45         /* start reading header */
46         if (count < 6)
47                 return -EIO;
48
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");
53                         return -EIO;
54                 }
55                 *buf++ = ioread8(chip->vendor->iobase);
56         }
57
58         /* size of the data received */
59         native_size = (__force __be32 *) (hdr + 2);
60         size = be32_to_cpu(*native_size);
61
62         if (count < size) {
63                 dev_err(chip->dev,
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");
69                                 return -EIO;
70                         }
71                 }
72                 return -EIO;
73         }
74
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");
80                         return -EIO;
81                 }
82                 *buf++ = ioread8(chip->vendor->iobase);
83         }
84
85         /* make sure data available is gone */
86         status = ioread8(chip->vendor->iobase + 1);
87
88         if (status & ATML_STATUS_DATA_AVAIL) {
89                 dev_err(chip->dev, "data available is stuck\n");
90                 return -EIO;
91         }
92
93         return size;
94 }
95
96 static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
97 {
98         int i;
99
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);
104         }
105
106         return count;
107 }
108
109 static void tpm_atml_cancel(struct tpm_chip *chip)
110 {
111         iowrite8(ATML_STATUS_ABORT, chip->vendor->iobase + 1);
112 }
113
114 static u8 tpm_atml_status(struct tpm_chip *chip)
115 {
116         return ioread8(chip->vendor->iobase + 1);
117 }
118
119 static struct file_operations atmel_ops = {
120         .owner = THIS_MODULE,
121         .llseek = no_llseek,
122         .open = tpm_open,
123         .read = tpm_read,
124         .write = tpm_write,
125         .release = tpm_release,
126 };
127
128 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
129 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
130 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
131 static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
132
133 static struct attribute* atmel_attrs[] = {
134         &dev_attr_pubek.attr,
135         &dev_attr_pcrs.attr,
136         &dev_attr_caps.attr,
137         &dev_attr_cancel.attr,
138         NULL,
139 };
140
141 static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };
142
143 static struct tpm_vendor_specific tpm_atmel = {
144         .recv = tpm_atml_recv,
145         .send = tpm_atml_send,
146         .cancel = tpm_atml_cancel,
147         .status = tpm_atml_status,
148         .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
149         .req_complete_val = ATML_STATUS_DATA_AVAIL,
150         .req_canceled = ATML_STATUS_READY,
151         .attr_group = &atmel_attr_grp,
152         .miscdev = { .fops = &atmel_ops, },
153 };
154
155 static struct platform_device *pdev;
156
157 static void atml_plat_remove(void)
158 {
159         struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
160
161         if (chip) {
162                 if (chip->vendor->have_region)
163                         atmel_release_region(chip->vendor->base,
164                                              chip->vendor->region_size);
165                 atmel_put_base_addr(chip->vendor);
166                 tpm_remove_hardware(chip->dev);
167                 platform_device_unregister(pdev);
168         }
169 }
170
171 static struct device_driver atml_drv = {
172         .name = "tpm_atmel",
173         .bus = &platform_bus_type,
174         .owner = THIS_MODULE,
175         .suspend = tpm_pm_suspend,
176         .resume = tpm_pm_resume,
177 };
178
179 static int __init init_atmel(void)
180 {
181         int rc = 0;
182
183         driver_register(&atml_drv);
184
185         if ((tpm_atmel.iobase = atmel_get_base_addr(&tpm_atmel)) == NULL) {
186                 rc = -ENODEV;
187                 goto err_unreg_drv;
188         }
189
190         tpm_atmel.have_region =
191             (atmel_request_region
192              (tpm_atmel.base, tpm_atmel.region_size,
193               "tpm_atmel0") == NULL) ? 0 : 1;
194
195         if (IS_ERR
196             (pdev =
197              platform_device_register_simple("tpm_atmel", -1, NULL, 0))) {
198                 rc = PTR_ERR(pdev);
199                 goto err_rel_reg;
200         }
201
202         if ((rc = tpm_register_hardware(&pdev->dev, &tpm_atmel)) < 0)
203                 goto err_unreg_dev;
204         return 0;
205
206 err_unreg_dev:
207         platform_device_unregister(pdev);
208 err_rel_reg:
209         atmel_put_base_addr(&tpm_atmel);
210         if (tpm_atmel.have_region)
211                 atmel_release_region(tpm_atmel.base,
212                                      tpm_atmel.region_size);
213 err_unreg_drv:
214         driver_unregister(&atml_drv);
215         return rc;
216 }
217
218 static void __exit cleanup_atmel(void)
219 {
220         driver_unregister(&atml_drv);
221         atml_plat_remove();
222 }
223
224 module_init(init_atmel);
225 module_exit(cleanup_atmel);
226
227 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
228 MODULE_DESCRIPTION("TPM Driver");
229 MODULE_VERSION("2.0");
230 MODULE_LICENSE("GPL");