Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / char / tpm / tpm_atmel.c
blob54a6750a67578114b12b6be5a684ed9ac349ca33
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2004 IBM Corporation
5 * Authors:
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Dave Safford <safford@watson.ibm.com>
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Kylene Hall <kjhall@us.ibm.com>
11 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13 * Device driver for TCG/TCPA TPM (trusted platform module).
14 * Specifications at www.trustedcomputinggroup.org
17 #include "tpm.h"
18 #include "tpm_atmel.h"
20 /* write status bits */
21 enum tpm_atmel_write_status {
22 ATML_STATUS_ABORT = 0x01,
23 ATML_STATUS_LASTBYTE = 0x04
25 /* read status bits */
26 enum tpm_atmel_read_status {
27 ATML_STATUS_BUSY = 0x01,
28 ATML_STATUS_DATA_AVAIL = 0x02,
29 ATML_STATUS_REWRITE = 0x04,
30 ATML_STATUS_READY = 0x08
33 static int tpm_atml_recv(struct tpm_chip *chip, u8 *buf, size_t count)
35 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
36 u8 status, *hdr = buf;
37 u32 size;
38 int i;
39 __be32 *native_size;
41 /* start reading header */
42 if (count < 6)
43 return -EIO;
45 for (i = 0; i < 6; i++) {
46 status = ioread8(priv->iobase + 1);
47 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
48 dev_err(&chip->dev, "error reading header\n");
49 return -EIO;
51 *buf++ = ioread8(priv->iobase);
54 /* size of the data received */
55 native_size = (__force __be32 *) (hdr + 2);
56 size = be32_to_cpu(*native_size);
58 if (count < size) {
59 dev_err(&chip->dev,
60 "Recv size(%d) less than available space\n", size);
61 for (; i < size; i++) { /* clear the waiting data anyway */
62 status = ioread8(priv->iobase + 1);
63 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
64 dev_err(&chip->dev, "error reading data\n");
65 return -EIO;
68 return -EIO;
71 /* read all the data available */
72 for (; i < size; i++) {
73 status = ioread8(priv->iobase + 1);
74 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
75 dev_err(&chip->dev, "error reading data\n");
76 return -EIO;
78 *buf++ = ioread8(priv->iobase);
81 /* make sure data available is gone */
82 status = ioread8(priv->iobase + 1);
84 if (status & ATML_STATUS_DATA_AVAIL) {
85 dev_err(&chip->dev, "data available is stuck\n");
86 return -EIO;
89 return size;
92 static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
94 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
95 int i;
97 dev_dbg(&chip->dev, "tpm_atml_send:\n");
98 for (i = 0; i < count; i++) {
99 dev_dbg(&chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]);
100 iowrite8(buf[i], priv->iobase);
103 return 0;
106 static void tpm_atml_cancel(struct tpm_chip *chip)
108 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
110 iowrite8(ATML_STATUS_ABORT, priv->iobase + 1);
113 static u8 tpm_atml_status(struct tpm_chip *chip)
115 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
117 return ioread8(priv->iobase + 1);
120 static bool tpm_atml_req_canceled(struct tpm_chip *chip, u8 status)
122 return (status == ATML_STATUS_READY);
125 static const struct tpm_class_ops tpm_atmel = {
126 .recv = tpm_atml_recv,
127 .send = tpm_atml_send,
128 .cancel = tpm_atml_cancel,
129 .status = tpm_atml_status,
130 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
131 .req_complete_val = ATML_STATUS_DATA_AVAIL,
132 .req_canceled = tpm_atml_req_canceled,
135 static struct platform_device *pdev;
137 static void atml_plat_remove(void)
139 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
140 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
142 tpm_chip_unregister(chip);
143 if (priv->have_region)
144 atmel_release_region(priv->base, priv->region_size);
145 atmel_put_base_addr(priv->iobase);
146 platform_device_unregister(pdev);
149 static SIMPLE_DEV_PM_OPS(tpm_atml_pm, tpm_pm_suspend, tpm_pm_resume);
151 static struct platform_driver atml_drv = {
152 .driver = {
153 .name = "tpm_atmel",
154 .pm = &tpm_atml_pm,
158 static int __init init_atmel(void)
160 int rc = 0;
161 void __iomem *iobase = NULL;
162 int have_region, region_size;
163 unsigned long base;
164 struct tpm_chip *chip;
165 struct tpm_atmel_priv *priv;
167 rc = platform_driver_register(&atml_drv);
168 if (rc)
169 return rc;
171 if ((iobase = atmel_get_base_addr(&base, &region_size)) == NULL) {
172 rc = -ENODEV;
173 goto err_unreg_drv;
176 have_region =
177 (atmel_request_region
178 (base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
180 pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
181 if (IS_ERR(pdev)) {
182 rc = PTR_ERR(pdev);
183 goto err_rel_reg;
186 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
187 if (!priv) {
188 rc = -ENOMEM;
189 goto err_unreg_dev;
192 priv->iobase = iobase;
193 priv->base = base;
194 priv->have_region = have_region;
195 priv->region_size = region_size;
197 chip = tpmm_chip_alloc(&pdev->dev, &tpm_atmel);
198 if (IS_ERR(chip)) {
199 rc = PTR_ERR(chip);
200 goto err_unreg_dev;
203 dev_set_drvdata(&chip->dev, priv);
205 rc = tpm_chip_register(chip);
206 if (rc)
207 goto err_unreg_dev;
209 return 0;
211 err_unreg_dev:
212 platform_device_unregister(pdev);
213 err_rel_reg:
214 atmel_put_base_addr(iobase);
215 if (have_region)
216 atmel_release_region(base,
217 region_size);
218 err_unreg_drv:
219 platform_driver_unregister(&atml_drv);
220 return rc;
223 static void __exit cleanup_atmel(void)
225 platform_driver_unregister(&atml_drv);
226 atml_plat_remove();
229 module_init(init_atmel);
230 module_exit(cleanup_atmel);
232 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
233 MODULE_DESCRIPTION("TPM Driver");
234 MODULE_VERSION("2.0");
235 MODULE_LICENSE("GPL");