blob: c64a1bc65349cc3959c488034fbfd892e7ac04cf [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 *
Kent Yoder8e81cc12007-08-22 14:01:04 -070010 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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"
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080023#include "tpm_atmel.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/* write status bits */
Kylene Hall3122a882005-06-23 22:01:48 -070026enum tpm_atmel_write_status {
27 ATML_STATUS_ABORT = 0x01,
28 ATML_STATUS_LASTBYTE = 0x04
29};
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* read status bits */
Kylene Hall3122a882005-06-23 22:01:48 -070031enum 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Andrew Mortonb888c872005-10-30 15:03:28 -080038static int tpm_atml_recv(struct tpm_chip *chip, u8 *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
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++) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -070050 status = ioread8(chip->vendor.iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Kylene Jo Hall90612b32005-11-18 01:10:58 -080052 dev_err(chip->dev, "error reading header\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return -EIO;
54 }
Kylene Jo Hall90dda522006-04-22 02:37:15 -070055 *buf++ = ioread8(chip->vendor.iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
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) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080063 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 "Recv size(%d) less than available space\n", size);
65 for (; i < size; i++) { /* clear the waiting data anyway */
Kylene Jo Hall90dda522006-04-22 02:37:15 -070066 status = ioread8(chip->vendor.iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Kylene Jo Hall90612b32005-11-18 01:10:58 -080068 dev_err(chip->dev, "error reading data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -EIO;
70 }
71 }
72 return -EIO;
73 }
74
75 /* read all the data available */
76 for (; i < size; i++) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -070077 status = ioread8(chip->vendor.iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Kylene Jo Hall90612b32005-11-18 01:10:58 -080079 dev_err(chip->dev, "error reading data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EIO;
81 }
Kylene Jo Hall90dda522006-04-22 02:37:15 -070082 *buf++ = ioread8(chip->vendor.iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85 /* make sure data available is gone */
Kylene Jo Hall90dda522006-04-22 02:37:15 -070086 status = ioread8(chip->vendor.iobase + 1);
Kylene Jo Hall90612b32005-11-18 01:10:58 -080087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (status & ATML_STATUS_DATA_AVAIL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080089 dev_err(chip->dev, "data available is stuck\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EIO;
91 }
92
93 return size;
94}
95
Andrew Mortonb888c872005-10-30 15:03:28 -080096static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 int i;
99
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800100 dev_dbg(chip->dev, "tpm_atml_send:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 for (i = 0; i < count; i++) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800102 dev_dbg(chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]);
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700103 iowrite8(buf[i], chip->vendor.iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 return count;
107}
108
109static void tpm_atml_cancel(struct tpm_chip *chip)
110{
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700111 iowrite8(ATML_STATUS_ABORT, chip->vendor.iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800114static u8 tpm_atml_status(struct tpm_chip *chip)
115{
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700116 return ioread8(chip->vendor.iobase + 1);
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800117}
118
Arjan van de Ven62322d22006-07-03 00:24:21 -0700119static const struct file_operations atmel_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .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
Kylene Hall6659ca22005-06-23 22:02:00 -0700128static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
129static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
130static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
131static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
132
133static struct attribute* atmel_attrs[] = {
134 &dev_attr_pubek.attr,
135 &dev_attr_pcrs.attr,
136 &dev_attr_caps.attr,
137 &dev_attr_cancel.attr,
Randy Dunlap874ec332005-10-30 15:03:29 -0800138 NULL,
Kylene Hall6659ca22005-06-23 22:02:00 -0700139};
140
141static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };
142
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700143static const struct tpm_vendor_specific tpm_atmel = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .recv = tpm_atml_recv,
145 .send = tpm_atml_send,
146 .cancel = tpm_atml_cancel,
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800147 .status = tpm_atml_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
149 .req_complete_val = ATML_STATUS_DATA_AVAIL,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700150 .req_canceled = ATML_STATUS_READY,
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
Andrew Mortonb888c872005-10-30 15:03:28 -0800155static struct platform_device *pdev;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800156
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800157static void atml_plat_remove(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800159 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
160
Andrew Mortonb888c872005-10-30 15:03:28 -0800161 if (chip) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700162 if (chip->vendor.have_region)
163 atmel_release_region(chip->vendor.base,
164 chip->vendor.region_size);
165 atmel_put_base_addr(chip->vendor.iobase);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800166 tpm_remove_hardware(chip->dev);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800167 platform_device_unregister(pdev);
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800168 }
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800169}
170
Ming Lei7a192ec2009-02-06 23:40:12 +0800171static int tpm_atml_suspend(struct platform_device *dev, pm_message_t msg)
172{
173 return tpm_pm_suspend(&dev->dev, msg);
174}
175
176static int tpm_atml_resume(struct platform_device *dev)
177{
178 return tpm_pm_resume(&dev->dev);
179}
180static struct platform_driver atml_drv = {
181 .driver = {
182 .name = "tpm_atmel",
183 .owner = THIS_MODULE,
184 },
185 .suspend = tpm_atml_suspend,
186 .resume = tpm_atml_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
189static int __init init_atmel(void)
190{
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800191 int rc = 0;
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700192 void __iomem *iobase = NULL;
193 int have_region, region_size;
194 unsigned long base;
195 struct tpm_chip *chip;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800196
Ming Lei7a192ec2009-02-06 23:40:12 +0800197 rc = platform_driver_register(&atml_drv);
Jeff Garzikf33d9bd2006-10-11 01:21:51 -0700198 if (rc)
199 return rc;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800200
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700201 if ((iobase = atmel_get_base_addr(&base, &region_size)) == NULL) {
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800202 rc = -ENODEV;
203 goto err_unreg_drv;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800204 }
205
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700206 have_region =
Kylene Jo Hall90612b32005-11-18 01:10:58 -0800207 (atmel_request_region
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700208 (tpm_atmel.base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
209
Jeff Garzikf33d9bd2006-10-11 01:21:51 -0700210 pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
211 if (IS_ERR(pdev)) {
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800212 rc = PTR_ERR(pdev);
213 goto err_rel_reg;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800214 }
215
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700216 if (!(chip = tpm_register_hardware(&pdev->dev, &tpm_atmel))) {
217 rc = -ENODEV;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800218 goto err_unreg_dev;
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700219 }
220
221 chip->vendor.iobase = iobase;
222 chip->vendor.base = base;
223 chip->vendor.have_region = have_region;
224 chip->vendor.region_size = region_size;
225
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800226 return 0;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800227
228err_unreg_dev:
229 platform_device_unregister(pdev);
230err_rel_reg:
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700231 atmel_put_base_addr(iobase);
232 if (have_region)
233 atmel_release_region(base,
234 region_size);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800235err_unreg_drv:
Ming Lei7a192ec2009-02-06 23:40:12 +0800236 platform_driver_unregister(&atml_drv);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800237 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static void __exit cleanup_atmel(void)
241{
Ming Lei7a192ec2009-02-06 23:40:12 +0800242 platform_driver_unregister(&atml_drv);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800243 atml_plat_remove();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246module_init(init_atmel);
247module_exit(cleanup_atmel);
248
249MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
250MODULE_DESCRIPTION("TPM Driver");
251MODULE_VERSION("2.0");
252MODULE_LICENSE("GPL");