tpm_atmel.c 5.34 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2004 IBM Corporation
 *
 * Authors:
 * Leendert van Doorn <leendert@watson.ibm.com>
 * Dave Safford <safford@watson.ibm.com>
 * Reiner Sailer <sailer@watson.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
 *
 * Device driver for TCG/TCPA TPM (trusted platform module).
 * Specifications at www.trustedcomputinggroup.org	 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 * 
 */

#include "tpm.h"
23
#include "tpm_atmel.h"
Linus Torvalds's avatar
Linus Torvalds committed
24 25

/* write status bits */
26 27 28 29
enum tpm_atmel_write_status {
	ATML_STATUS_ABORT = 0x01,
	ATML_STATUS_LASTBYTE = 0x04
};
Linus Torvalds's avatar
Linus Torvalds committed
30
/* read status bits */
31 32 33 34 35 36
enum tpm_atmel_read_status {
	ATML_STATUS_BUSY = 0x01,
	ATML_STATUS_DATA_AVAIL = 0x02,
	ATML_STATUS_REWRITE = 0x04,
	ATML_STATUS_READY = 0x08
};
Linus Torvalds's avatar
Linus Torvalds committed
37

Andrew Morton's avatar
Andrew Morton committed
38
static int tpm_atml_recv(struct tpm_chip *chip, u8 *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42 43 44 45 46 47 48 49
{
	u8 status, *hdr = buf;
	u32 size;
	int i;
	__be32 *native_size;

	/* start reading header */
	if (count < 6)
		return -EIO;

	for (i = 0; i < 6; i++) {
50
		status = ioread8(chip->vendor->iobase + 1);
Linus Torvalds's avatar
Linus Torvalds committed
51
		if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
52
			dev_err(chip->dev, "error reading header\n");
Linus Torvalds's avatar
Linus Torvalds committed
53 54
			return -EIO;
		}
55
		*buf++ = ioread8(chip->vendor->iobase);
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60 61 62
	}

	/* size of the data received */
	native_size = (__force __be32 *) (hdr + 2);
	size = be32_to_cpu(*native_size);

	if (count < size) {
63
		dev_err(chip->dev,
Linus Torvalds's avatar
Linus Torvalds committed
64 65
			"Recv size(%d) less than available space\n", size);
		for (; i < size; i++) {	/* clear the waiting data anyway */
66
			status = ioread8(chip->vendor->iobase + 1);
Linus Torvalds's avatar
Linus Torvalds committed
67
			if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
68
				dev_err(chip->dev, "error reading data\n");
Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72 73 74 75 76
				return -EIO;
			}
		}
		return -EIO;
	}

	/* read all the data available */
	for (; i < size; i++) {
77
		status = ioread8(chip->vendor->iobase + 1);
Linus Torvalds's avatar
Linus Torvalds committed
78
		if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
79
			dev_err(chip->dev, "error reading data\n");
Linus Torvalds's avatar
Linus Torvalds committed
80 81
			return -EIO;
		}
82
		*buf++ = ioread8(chip->vendor->iobase);
Linus Torvalds's avatar
Linus Torvalds committed
83 84 85
	}

	/* make sure data available is gone */
86 87
	status = ioread8(chip->vendor->iobase + 1);

Linus Torvalds's avatar
Linus Torvalds committed
88
	if (status & ATML_STATUS_DATA_AVAIL) {
89
		dev_err(chip->dev, "data available is stuck\n");
Linus Torvalds's avatar
Linus Torvalds committed
90 91 92 93 94 95
		return -EIO;
	}

	return size;
}

Andrew Morton's avatar
Andrew Morton committed
96
static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
97 98 99
{
	int i;

100
	dev_dbg(chip->dev, "tpm_atml_send:\n");
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
104 105 106 107 108 109 110
	}

	return count;
}

static void tpm_atml_cancel(struct tpm_chip *chip)
{
111
	iowrite8(ATML_STATUS_ABORT, chip->vendor->iobase + 1);
Linus Torvalds's avatar
Linus Torvalds committed
112 113
}

114 115
static u8 tpm_atml_status(struct tpm_chip *chip)
{
116
	return ioread8(chip->vendor->iobase + 1);
117 118
}

Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123 124 125 126 127
static struct file_operations atmel_ops = {
	.owner = THIS_MODULE,
	.llseek = no_llseek,
	.open = tpm_open,
	.read = tpm_read,
	.write = tpm_write,
	.release = tpm_release,
};

128 129 130 131 132 133 134 135 136 137
static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);

static struct attribute* atmel_attrs[] = {
	&dev_attr_pubek.attr,
	&dev_attr_pcrs.attr,
	&dev_attr_caps.attr,
	&dev_attr_cancel.attr,
138
	NULL,
139 140 141 142
};

static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };

Linus Torvalds's avatar
Linus Torvalds committed
143 144 145 146
static struct tpm_vendor_specific tpm_atmel = {
	.recv = tpm_atml_recv,
	.send = tpm_atml_send,
	.cancel = tpm_atml_cancel,
147
	.status = tpm_atml_status,
Linus Torvalds's avatar
Linus Torvalds committed
148 149
	.req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
	.req_complete_val = ATML_STATUS_DATA_AVAIL,
150
	.req_canceled = ATML_STATUS_READY,
151
	.attr_group = &atmel_attr_grp,
Linus Torvalds's avatar
Linus Torvalds committed
152 153 154
	.miscdev = { .fops = &atmel_ops, },
};

Andrew Morton's avatar
Andrew Morton committed
155
static struct platform_device *pdev;
156

157
static void atml_plat_remove(void)
158
{
159 160
	struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);

Andrew Morton's avatar
Andrew Morton committed
161
	if (chip) {
162
		if (chip->vendor->have_region)
163 164
			atmel_release_region(chip->vendor->base,
					     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 172 173 174 175 176 177 178 179
	}
}

static struct device_driver atml_drv = {
	.name = "tpm_atmel",
	.bus = &platform_bus_type,
	.owner = THIS_MODULE,
	.suspend = tpm_pm_suspend,
	.resume = tpm_pm_resume,
};

static int __init init_atmel(void)
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182
{
	int rc = 0;

183
	driver_register(&atml_drv);
Linus Torvalds's avatar
Linus Torvalds committed
184

185
	if ((tpm_atmel.iobase = atmel_get_base_addr(&tpm_atmel)) == NULL) {
186 187
		rc = -ENODEV;
		goto err_unreg_drv;
188
	}
Linus Torvalds's avatar
Linus Torvalds committed
189

190 191 192 193
	tpm_atmel.have_region =
	    (atmel_request_region
	     (tpm_atmel.base, tpm_atmel.region_size,
	      "tpm_atmel0") == NULL) ? 0 : 1;
Linus Torvalds's avatar
Linus Torvalds committed
194

195 196 197
	if (IS_ERR
	    (pdev =
	     platform_device_register_simple("tpm_atmel", -1, NULL, 0))) {
198 199
		rc = PTR_ERR(pdev);
		goto err_rel_reg;
200
	}
Linus Torvalds's avatar
Linus Torvalds committed
201

202 203
	if ((rc = tpm_register_hardware(&pdev->dev, &tpm_atmel)) < 0)
		goto err_unreg_dev;
204
	return 0;
205 206 207 208 209

err_unreg_dev:
	platform_device_unregister(pdev);
err_rel_reg:
	atmel_put_base_addr(&tpm_atmel);
210 211 212
	if (tpm_atmel.have_region)
		atmel_release_region(tpm_atmel.base,
				     tpm_atmel.region_size);
213 214 215
err_unreg_drv:
	driver_unregister(&atml_drv);
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219
}

static void __exit cleanup_atmel(void)
{
220
	driver_unregister(&atml_drv);
221
	atml_plat_remove();
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225 226 227 228 229 230
}

module_init(init_atmel);
module_exit(cleanup_atmel);

MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
MODULE_DESCRIPTION("TPM Driver");
MODULE_VERSION("2.0");
MODULE_LICENSE("GPL");