blob: 07abfb7143b105350d3ebaa353867ce17836a7a7 [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 Hall3122a882005-06-23 22:01:48 -070025enum tpm_atmel_addr{
26 TPM_ATML_BASE = 0x400
27};
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/* write status bits */
Kylene Hall3122a882005-06-23 22:01:48 -070030enum tpm_atmel_write_status {
31 ATML_STATUS_ABORT = 0x01,
32 ATML_STATUS_LASTBYTE = 0x04
33};
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* read status bits */
Kylene Hall3122a882005-06-23 22:01:48 -070035enum tpm_atmel_read_status {
36 ATML_STATUS_BUSY = 0x01,
37 ATML_STATUS_DATA_AVAIL = 0x02,
38 ATML_STATUS_REWRITE = 0x04,
39 ATML_STATUS_READY = 0x08
40};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static int tpm_atml_recv(struct tpm_chip *chip, u8 * buf, size_t count)
43{
44 u8 status, *hdr = buf;
45 u32 size;
46 int i;
47 __be32 *native_size;
48
49 /* start reading header */
50 if (count < 6)
51 return -EIO;
52
53 for (i = 0; i < 6; i++) {
54 status = inb(chip->vendor->base + 1);
55 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
56 dev_err(&chip->pci_dev->dev,
57 "error reading header\n");
58 return -EIO;
59 }
60 *buf++ = inb(chip->vendor->base);
61 }
62
63 /* size of the data received */
64 native_size = (__force __be32 *) (hdr + 2);
65 size = be32_to_cpu(*native_size);
66
67 if (count < size) {
68 dev_err(&chip->pci_dev->dev,
69 "Recv size(%d) less than available space\n", size);
70 for (; i < size; i++) { /* clear the waiting data anyway */
71 status = inb(chip->vendor->base + 1);
72 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
73 dev_err(&chip->pci_dev->dev,
74 "error reading data\n");
75 return -EIO;
76 }
77 }
78 return -EIO;
79 }
80
81 /* read all the data available */
82 for (; i < size; i++) {
83 status = inb(chip->vendor->base + 1);
84 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
85 dev_err(&chip->pci_dev->dev,
86 "error reading data\n");
87 return -EIO;
88 }
89 *buf++ = inb(chip->vendor->base);
90 }
91
92 /* make sure data available is gone */
93 status = inb(chip->vendor->base + 1);
94 if (status & ATML_STATUS_DATA_AVAIL) {
95 dev_err(&chip->pci_dev->dev, "data available is stuck\n");
96 return -EIO;
97 }
98
99 return size;
100}
101
102static int tpm_atml_send(struct tpm_chip *chip, u8 * buf, size_t count)
103{
104 int i;
105
106 dev_dbg(&chip->pci_dev->dev, "tpm_atml_send: ");
107 for (i = 0; i < count; i++) {
108 dev_dbg(&chip->pci_dev->dev, "0x%x(%d) ", buf[i], buf[i]);
109 outb(buf[i], chip->vendor->base);
110 }
111
112 return count;
113}
114
115static void tpm_atml_cancel(struct tpm_chip *chip)
116{
117 outb(ATML_STATUS_ABORT, chip->vendor->base + 1);
118}
119
120static struct file_operations atmel_ops = {
121 .owner = THIS_MODULE,
122 .llseek = no_llseek,
123 .open = tpm_open,
124 .read = tpm_read,
125 .write = tpm_write,
126 .release = tpm_release,
127};
128
Kylene Hall6659ca22005-06-23 22:02:00 -0700129static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
130static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
131static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
132static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
133
134static struct attribute* atmel_attrs[] = {
135 &dev_attr_pubek.attr,
136 &dev_attr_pcrs.attr,
137 &dev_attr_caps.attr,
138 &dev_attr_cancel.attr,
139 0,
140};
141
142static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static struct tpm_vendor_specific tpm_atmel = {
145 .recv = tpm_atml_recv,
146 .send = tpm_atml_send,
147 .cancel = tpm_atml_cancel,
148 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
149 .req_complete_val = ATML_STATUS_DATA_AVAIL,
150 .base = TPM_ATML_BASE,
Kylene Hall6659ca22005-06-23 22:02:00 -0700151 .attr_group = &atmel_attr_grp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .miscdev = { .fops = &atmel_ops, },
153};
154
155static int __devinit tpm_atml_init(struct pci_dev *pci_dev,
156 const struct pci_device_id *pci_id)
157{
158 u8 version[4];
159 int rc = 0;
160
161 if (pci_enable_device(pci_dev))
162 return -EIO;
163
164 if (tpm_lpc_bus_init(pci_dev, TPM_ATML_BASE)) {
165 rc = -ENODEV;
166 goto out_err;
167 }
168
169 /* verify that it is an Atmel part */
170 if (tpm_read_index(4) != 'A' || tpm_read_index(5) != 'T'
171 || tpm_read_index(6) != 'M' || tpm_read_index(7) != 'L') {
172 rc = -ENODEV;
173 goto out_err;
174 }
175
176 /* query chip for its version number */
177 if ((version[0] = tpm_read_index(0x00)) != 0xFF) {
178 version[1] = tpm_read_index(0x01);
179 version[2] = tpm_read_index(0x02);
180 version[3] = tpm_read_index(0x03);
181 } else {
182 dev_info(&pci_dev->dev, "version query failed\n");
183 rc = -ENODEV;
184 goto out_err;
185 }
186
187 if ((rc = tpm_register_hardware(pci_dev, &tpm_atmel)) < 0)
188 goto out_err;
189
190 dev_info(&pci_dev->dev,
191 "Atmel TPM version %d.%d.%d.%d\n", version[0], version[1],
192 version[2], version[3]);
193
194 return 0;
195out_err:
196 pci_disable_device(pci_dev);
197 return rc;
198}
199
200static struct pci_device_id tpm_pci_tbl[] __devinitdata = {
201 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0)},
202 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12)},
203 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0)},
204 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12)},
205 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0)},
206 {PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_LPC)},
207 {0,}
208};
209
210MODULE_DEVICE_TABLE(pci, tpm_pci_tbl);
211
212static struct pci_driver atmel_pci_driver = {
213 .name = "tpm_atmel",
214 .id_table = tpm_pci_tbl,
215 .probe = tpm_atml_init,
216 .remove = __devexit_p(tpm_remove),
217 .suspend = tpm_pm_suspend,
218 .resume = tpm_pm_resume,
219};
220
221static int __init init_atmel(void)
222{
223 return pci_register_driver(&atmel_pci_driver);
224}
225
226static void __exit cleanup_atmel(void)
227{
228 pci_unregister_driver(&atmel_pci_driver);
229}
230
231module_init(init_atmel);
232module_exit(cleanup_atmel);
233
234MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
235MODULE_DESCRIPTION("TPM Driver");
236MODULE_VERSION("2.0");
237MODULE_LICENSE("GPL");