blob: a9100ad3d0307082de9f098300e6ba48cd8ace79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
24/* Atmel definitions */
Kylene Halle1a23c62005-06-23 22:02:06 -070025enum tpm_atmel_addr {
26 TPM_ATMEL_BASE_ADDR_LO = 0x08,
27 TPM_ATMEL_BASE_ADDR_HI = 0x09
Kylene Hall3122a882005-06-23 22:01:48 -070028};
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* write status bits */
Kylene Hall3122a882005-06-23 22:01:48 -070031enum tpm_atmel_write_status {
32 ATML_STATUS_ABORT = 0x01,
33 ATML_STATUS_LASTBYTE = 0x04
34};
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* read status bits */
Kylene Hall3122a882005-06-23 22:01:48 -070036enum tpm_atmel_read_status {
37 ATML_STATUS_BUSY = 0x01,
38 ATML_STATUS_DATA_AVAIL = 0x02,
39 ATML_STATUS_REWRITE = 0x04,
40 ATML_STATUS_READY = 0x08
41};
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static int tpm_atml_recv(struct tpm_chip *chip, u8 * buf, size_t count)
44{
45 u8 status, *hdr = buf;
46 u32 size;
47 int i;
48 __be32 *native_size;
49
50 /* start reading header */
51 if (count < 6)
52 return -EIO;
53
54 for (i = 0; i < 6; i++) {
55 status = inb(chip->vendor->base + 1);
56 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080057 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 "error reading header\n");
59 return -EIO;
60 }
61 *buf++ = inb(chip->vendor->base);
62 }
63
64 /* size of the data received */
65 native_size = (__force __be32 *) (hdr + 2);
66 size = be32_to_cpu(*native_size);
67
68 if (count < size) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080069 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 "Recv size(%d) less than available space\n", size);
71 for (; i < size; i++) { /* clear the waiting data anyway */
72 status = inb(chip->vendor->base + 1);
73 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080074 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 "error reading data\n");
76 return -EIO;
77 }
78 }
79 return -EIO;
80 }
81
82 /* read all the data available */
83 for (; i < size; i++) {
84 status = inb(chip->vendor->base + 1);
85 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080086 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 "error reading data\n");
88 return -EIO;
89 }
90 *buf++ = inb(chip->vendor->base);
91 }
92
93 /* make sure data available is gone */
94 status = inb(chip->vendor->base + 1);
95 if (status & ATML_STATUS_DATA_AVAIL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080096 dev_err(chip->dev, "data available is stuck\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EIO;
98 }
99
100 return size;
101}
102
103static int tpm_atml_send(struct tpm_chip *chip, u8 * buf, size_t count)
104{
105 int i;
106
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800107 dev_dbg(chip->dev, "tpm_atml_send:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 for (i = 0; i < count; i++) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800109 dev_dbg(chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 outb(buf[i], chip->vendor->base);
111 }
112
113 return count;
114}
115
116static void tpm_atml_cancel(struct tpm_chip *chip)
117{
118 outb(ATML_STATUS_ABORT, chip->vendor->base + 1);
119}
120
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800121static u8 tpm_atml_status(struct tpm_chip *chip)
122{
123 return inb(chip->vendor->base + 1);
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static struct file_operations atmel_ops = {
127 .owner = THIS_MODULE,
128 .llseek = no_llseek,
129 .open = tpm_open,
130 .read = tpm_read,
131 .write = tpm_write,
132 .release = tpm_release,
133};
134
Kylene Hall6659ca22005-06-23 22:02:00 -0700135static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
136static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
137static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
138static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
139
140static struct attribute* atmel_attrs[] = {
141 &dev_attr_pubek.attr,
142 &dev_attr_pcrs.attr,
143 &dev_attr_caps.attr,
144 &dev_attr_cancel.attr,
145 0,
146};
147
148static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static struct tpm_vendor_specific tpm_atmel = {
151 .recv = tpm_atml_recv,
152 .send = tpm_atml_send,
153 .cancel = tpm_atml_cancel,
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800154 .status = tpm_atml_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
156 .req_complete_val = ATML_STATUS_DATA_AVAIL,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700157 .req_canceled = ATML_STATUS_READY,
Kylene Hall6659ca22005-06-23 22:02:00 -0700158 .attr_group = &atmel_attr_grp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .miscdev = { .fops = &atmel_ops, },
160};
161
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800162static struct platform_device *pdev = NULL;
163
164static void __devexit tpm_atml_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800166 struct tpm_chip *chip = dev_get_drvdata(dev);
167 if ( chip ) {
168 release_region(chip->vendor->base, 2);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800169 tpm_remove_hardware(chip->dev);
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800170 }
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800171}
172
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800173static struct device_driver atml_drv = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .name = "tpm_atmel",
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800175 .bus = &platform_bus_type,
176 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .suspend = tpm_pm_suspend,
178 .resume = tpm_pm_resume,
179};
180
181static int __init init_atmel(void)
182{
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800183 int rc = 0;
184 int lo, hi;
185
186 driver_register(&atml_drv);
187
188 lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO);
189 hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI);
190
191 tpm_atmel.base = (hi<<8)|lo;
192
193 /* verify that it is an Atmel part */
194 if (tpm_read_index(TPM_ADDR, 4) != 'A' || tpm_read_index(TPM_ADDR, 5) != 'T'
195 || tpm_read_index(TPM_ADDR, 6) != 'M' || tpm_read_index(TPM_ADDR, 7) != 'L') {
196 return -ENODEV;
197 }
198
199 /* verify chip version number is 1.1 */
200 if ( (tpm_read_index(TPM_ADDR, 0x00) != 0x01) ||
201 (tpm_read_index(TPM_ADDR, 0x01) != 0x01 ))
202 return -ENODEV;
203
204 pdev = kmalloc(sizeof(struct platform_device), GFP_KERNEL);
205 if ( !pdev )
206 return -ENOMEM;
207
208 memset(pdev, 0, sizeof(struct platform_device));
209
210 pdev->name = "tpm_atmel0";
211 pdev->id = -1;
212 pdev->num_resources = 0;
213 pdev->dev.release = tpm_atml_remove;
214 pdev->dev.driver = &atml_drv;
215
216 if ((rc=platform_device_register(pdev)) < 0) {
217 kfree(pdev);
218 pdev = NULL;
219 return rc;
220 }
221
222 if (request_region(tpm_atmel.base, 2, "tpm_atmel0") == NULL ) {
223 platform_device_unregister(pdev);
224 kfree(pdev);
225 pdev = NULL;
226 return -EBUSY;
227 }
228
229 if ((rc = tpm_register_hardware(&pdev->dev, &tpm_atmel)) < 0) {
230 release_region(tpm_atmel.base, 2);
231 platform_device_unregister(pdev);
232 kfree(pdev);
233 pdev = NULL;
234 return rc;
235 }
236
237 dev_info(&pdev->dev, "Atmel TPM 1.1, Base Address: 0x%x\n", tpm_atmel.base);
238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static void __exit cleanup_atmel(void)
242{
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800243 if (pdev) {
244 tpm_atml_remove(&pdev->dev);
245 platform_device_unregister(pdev);
246 kfree(pdev);
247 pdev = NULL;
248 }
249
250 driver_unregister(&atml_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253module_init(init_atmel);
254module_exit(cleanup_atmel);
255
256MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
257MODULE_DESCRIPTION("TPM Driver");
258MODULE_VERSION("2.0");
259MODULE_LICENSE("GPL");