xref: /linux/drivers/char/tpm/tpm_atmel.c (revision c25141062a82ae8bddced1b3ce2b57a1c0efabe0)
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 	struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
41 	u8 status, *hdr = buf;
42 	u32 size;
43 	int i;
44 	__be32 *native_size;
45 
46 	/* start reading header */
47 	if (count < 6)
48 		return -EIO;
49 
50 	for (i = 0; i < 6; i++) {
51 		status = ioread8(priv->iobase + 1);
52 		if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
53 			dev_err(&chip->dev, "error reading header\n");
54 			return -EIO;
55 		}
56 		*buf++ = ioread8(priv->iobase);
57 	}
58 
59 	/* size of the data received */
60 	native_size = (__force __be32 *) (hdr + 2);
61 	size = be32_to_cpu(*native_size);
62 
63 	if (count < size) {
64 		dev_err(&chip->dev,
65 			"Recv size(%d) less than available space\n", size);
66 		for (; i < size; i++) {	/* clear the waiting data anyway */
67 			status = ioread8(priv->iobase + 1);
68 			if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
69 				dev_err(&chip->dev, "error reading data\n");
70 				return -EIO;
71 			}
72 		}
73 		return -EIO;
74 	}
75 
76 	/* read all the data available */
77 	for (; i < size; i++) {
78 		status = ioread8(priv->iobase + 1);
79 		if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
80 			dev_err(&chip->dev, "error reading data\n");
81 			return -EIO;
82 		}
83 		*buf++ = ioread8(priv->iobase);
84 	}
85 
86 	/* make sure data available is gone */
87 	status = ioread8(priv->iobase + 1);
88 
89 	if (status & ATML_STATUS_DATA_AVAIL) {
90 		dev_err(&chip->dev, "data available is stuck\n");
91 		return -EIO;
92 	}
93 
94 	return size;
95 }
96 
97 static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
98 {
99 	struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
100 	int i;
101 
102 	dev_dbg(&chip->dev, "tpm_atml_send:\n");
103 	for (i = 0; i < count; i++) {
104 		dev_dbg(&chip->dev, "%d 0x%x(%d)\n",  i, buf[i], buf[i]);
105 		iowrite8(buf[i], priv->iobase);
106 	}
107 
108 	return count;
109 }
110 
111 static void tpm_atml_cancel(struct tpm_chip *chip)
112 {
113 	struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
114 
115 	iowrite8(ATML_STATUS_ABORT, priv->iobase + 1);
116 }
117 
118 static u8 tpm_atml_status(struct tpm_chip *chip)
119 {
120 	struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
121 
122 	return ioread8(priv->iobase + 1);
123 }
124 
125 static bool tpm_atml_req_canceled(struct tpm_chip *chip, u8 status)
126 {
127 	return (status == ATML_STATUS_READY);
128 }
129 
130 static const struct tpm_class_ops tpm_atmel = {
131 	.recv = tpm_atml_recv,
132 	.send = tpm_atml_send,
133 	.cancel = tpm_atml_cancel,
134 	.status = tpm_atml_status,
135 	.req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
136 	.req_complete_val = ATML_STATUS_DATA_AVAIL,
137 	.req_canceled = tpm_atml_req_canceled,
138 };
139 
140 static struct platform_device *pdev;
141 
142 static void atml_plat_remove(void)
143 {
144 	struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
145 	struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
146 
147 	tpm_chip_unregister(chip);
148 	if (priv->have_region)
149 		atmel_release_region(priv->base, priv->region_size);
150 	atmel_put_base_addr(priv->iobase);
151 	platform_device_unregister(pdev);
152 }
153 
154 static SIMPLE_DEV_PM_OPS(tpm_atml_pm, tpm_pm_suspend, tpm_pm_resume);
155 
156 static struct platform_driver atml_drv = {
157 	.driver = {
158 		.name = "tpm_atmel",
159 		.pm		= &tpm_atml_pm,
160 	},
161 };
162 
163 static int __init init_atmel(void)
164 {
165 	int rc = 0;
166 	void __iomem *iobase = NULL;
167 	int have_region, region_size;
168 	unsigned long base;
169 	struct  tpm_chip *chip;
170 	struct tpm_atmel_priv *priv;
171 
172 	rc = platform_driver_register(&atml_drv);
173 	if (rc)
174 		return rc;
175 
176 	if ((iobase = atmel_get_base_addr(&base, &region_size)) == NULL) {
177 		rc = -ENODEV;
178 		goto err_unreg_drv;
179 	}
180 
181 	have_region =
182 	    (atmel_request_region
183 	     (base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
184 
185 	pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
186 	if (IS_ERR(pdev)) {
187 		rc = PTR_ERR(pdev);
188 		goto err_rel_reg;
189 	}
190 
191 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
192 	if (!priv) {
193 		rc = -ENOMEM;
194 		goto err_unreg_dev;
195 	}
196 
197 	priv->iobase = iobase;
198 	priv->base = base;
199 	priv->have_region = have_region;
200 	priv->region_size = region_size;
201 
202 	chip = tpmm_chip_alloc(&pdev->dev, &tpm_atmel);
203 	if (IS_ERR(chip)) {
204 		rc = PTR_ERR(chip);
205 		goto err_unreg_dev;
206 	}
207 
208 	dev_set_drvdata(&chip->dev, priv);
209 
210 	rc = tpm_chip_register(chip);
211 	if (rc)
212 		goto err_unreg_dev;
213 
214 	return 0;
215 
216 err_unreg_dev:
217 	platform_device_unregister(pdev);
218 err_rel_reg:
219 	atmel_put_base_addr(iobase);
220 	if (have_region)
221 		atmel_release_region(base,
222 				     region_size);
223 err_unreg_drv:
224 	platform_driver_unregister(&atml_drv);
225 	return rc;
226 }
227 
228 static void __exit cleanup_atmel(void)
229 {
230 	platform_driver_unregister(&atml_drv);
231 	atml_plat_remove();
232 }
233 
234 module_init(init_atmel);
235 module_exit(cleanup_atmel);
236 
237 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
238 MODULE_DESCRIPTION("TPM Driver");
239 MODULE_VERSION("2.0");
240 MODULE_LICENSE("GPL");
241