Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / char / tpm / tpm_atmel.c
blob54a0360a3c95ea5bfed9507143c49b7debe744a9
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"
19 struct tpm_atmel_priv {
20 int region_size;
21 int have_region;
22 unsigned long base;
23 void __iomem *iobase;
26 #define atmel_getb(chip, offset) inb(atmel_get_priv(chip)->base + (offset))
27 #define atmel_putb(val, chip, offset) \
28 outb(val, atmel_get_priv(chip)->base + (offset))
29 #define atmel_request_region request_region
30 #define atmel_release_region release_region
31 /* Atmel definitions */
32 enum tpm_atmel_addr {
33 TPM_ATMEL_BASE_ADDR_LO = 0x08,
34 TPM_ATMEL_BASE_ADDR_HI = 0x09
37 static inline int tpm_read_index(int base, int index)
39 outb(index, base);
40 return inb(base + 1) & 0xFF;
43 /* Verify this is a 1.1 Atmel TPM */
44 static int atmel_verify_tpm11(void)
46 /* verify that it is an Atmel part */
47 if (tpm_read_index(TPM_ADDR, 4) != 'A' ||
48 tpm_read_index(TPM_ADDR, 5) != 'T' ||
49 tpm_read_index(TPM_ADDR, 6) != 'M' ||
50 tpm_read_index(TPM_ADDR, 7) != 'L')
51 return 1;
53 /* query chip for its version number */
54 if (tpm_read_index(TPM_ADDR, 0x00) != 1 ||
55 tpm_read_index(TPM_ADDR, 0x01) != 1)
56 return 1;
58 /* This is an atmel supported part */
59 return 0;
62 /* Determine where to talk to device */
63 static void __iomem *atmel_get_base_addr(unsigned long *base, int *region_size)
65 int lo, hi;
67 if (atmel_verify_tpm11() != 0)
68 return NULL;
70 lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO);
71 hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI);
73 *base = (hi << 8) | lo;
74 *region_size = 2;
76 return ioport_map(*base, *region_size);
79 /* write status bits */
80 enum tpm_atmel_write_status {
81 ATML_STATUS_ABORT = 0x01,
82 ATML_STATUS_LASTBYTE = 0x04
84 /* read status bits */
85 enum tpm_atmel_read_status {
86 ATML_STATUS_BUSY = 0x01,
87 ATML_STATUS_DATA_AVAIL = 0x02,
88 ATML_STATUS_REWRITE = 0x04,
89 ATML_STATUS_READY = 0x08
92 static int tpm_atml_recv(struct tpm_chip *chip, u8 *buf, size_t count)
94 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
95 u8 status, *hdr = buf;
96 u32 size;
97 int i;
98 __be32 *native_size;
100 /* start reading header */
101 if (count < 6)
102 return -EIO;
104 for (i = 0; i < 6; i++) {
105 status = ioread8(priv->iobase + 1);
106 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
107 dev_err(&chip->dev, "error reading header\n");
108 return -EIO;
110 *buf++ = ioread8(priv->iobase);
113 /* size of the data received */
114 native_size = (__force __be32 *) (hdr + 2);
115 size = be32_to_cpu(*native_size);
117 if (count < size) {
118 dev_err(&chip->dev,
119 "Recv size(%d) less than available space\n", size);
120 for (; i < size; i++) { /* clear the waiting data anyway */
121 status = ioread8(priv->iobase + 1);
122 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
123 dev_err(&chip->dev, "error reading data\n");
124 return -EIO;
127 return -EIO;
130 /* read all the data available */
131 for (; i < size; i++) {
132 status = ioread8(priv->iobase + 1);
133 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
134 dev_err(&chip->dev, "error reading data\n");
135 return -EIO;
137 *buf++ = ioread8(priv->iobase);
140 /* make sure data available is gone */
141 status = ioread8(priv->iobase + 1);
143 if (status & ATML_STATUS_DATA_AVAIL) {
144 dev_err(&chip->dev, "data available is stuck\n");
145 return -EIO;
148 return size;
151 static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
153 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
154 int i;
156 dev_dbg(&chip->dev, "tpm_atml_send:\n");
157 for (i = 0; i < count; i++) {
158 dev_dbg(&chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]);
159 iowrite8(buf[i], priv->iobase);
162 return 0;
165 static void tpm_atml_cancel(struct tpm_chip *chip)
167 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
169 iowrite8(ATML_STATUS_ABORT, priv->iobase + 1);
172 static u8 tpm_atml_status(struct tpm_chip *chip)
174 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
176 return ioread8(priv->iobase + 1);
179 static bool tpm_atml_req_canceled(struct tpm_chip *chip, u8 status)
181 return (status == ATML_STATUS_READY);
184 static const struct tpm_class_ops tpm_atmel = {
185 .recv = tpm_atml_recv,
186 .send = tpm_atml_send,
187 .cancel = tpm_atml_cancel,
188 .status = tpm_atml_status,
189 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
190 .req_complete_val = ATML_STATUS_DATA_AVAIL,
191 .req_canceled = tpm_atml_req_canceled,
194 static struct platform_device *pdev;
196 static void atml_plat_remove(void)
198 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
199 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
201 tpm_chip_unregister(chip);
202 if (priv->have_region)
203 atmel_release_region(priv->base, priv->region_size);
204 platform_device_unregister(pdev);
207 static SIMPLE_DEV_PM_OPS(tpm_atml_pm, tpm_pm_suspend, tpm_pm_resume);
209 static struct platform_driver atml_drv = {
210 .driver = {
211 .name = "tpm_atmel",
212 .pm = &tpm_atml_pm,
216 static int __init init_atmel(void)
218 int rc = 0;
219 void __iomem *iobase = NULL;
220 int have_region, region_size;
221 unsigned long base;
222 struct tpm_chip *chip;
223 struct tpm_atmel_priv *priv;
225 rc = platform_driver_register(&atml_drv);
226 if (rc)
227 return rc;
229 if ((iobase = atmel_get_base_addr(&base, &region_size)) == NULL) {
230 rc = -ENODEV;
231 goto err_unreg_drv;
234 have_region =
235 (atmel_request_region
236 (base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
238 pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
239 if (IS_ERR(pdev)) {
240 rc = PTR_ERR(pdev);
241 goto err_rel_reg;
244 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
245 if (!priv) {
246 rc = -ENOMEM;
247 goto err_unreg_dev;
250 priv->iobase = iobase;
251 priv->base = base;
252 priv->have_region = have_region;
253 priv->region_size = region_size;
255 chip = tpmm_chip_alloc(&pdev->dev, &tpm_atmel);
256 if (IS_ERR(chip)) {
257 rc = PTR_ERR(chip);
258 goto err_unreg_dev;
261 dev_set_drvdata(&chip->dev, priv);
263 rc = tpm_chip_register(chip);
264 if (rc)
265 goto err_unreg_dev;
267 return 0;
269 err_unreg_dev:
270 platform_device_unregister(pdev);
271 err_rel_reg:
272 if (have_region)
273 atmel_release_region(base,
274 region_size);
275 err_unreg_drv:
276 platform_driver_unregister(&atml_drv);
277 return rc;
280 static void __exit cleanup_atmel(void)
282 platform_driver_unregister(&atml_drv);
283 atml_plat_remove();
286 module_init(init_atmel);
287 module_exit(cleanup_atmel);
289 MODULE_AUTHOR("Leendert van Doorn <leendert@watson.ibm.com>");
290 MODULE_DESCRIPTION("TPM Driver");
291 MODULE_VERSION("2.0");
292 MODULE_LICENSE("GPL");