blob: 0d322ab11faabcbae304f9611665873900778962 [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{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +020040 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 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++) {
Christophe Ricard4eea7032016-03-31 22:56:55 +020051 status = ioread8(priv->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -050053 dev_err(&chip->dev, "error reading header\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return -EIO;
55 }
Christophe Ricard4eea7032016-03-31 22:56:55 +020056 *buf++ = ioread8(priv->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
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) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -050064 dev_err(&chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 "Recv size(%d) less than available space\n", size);
66 for (; i < size; i++) { /* clear the waiting data anyway */
Christophe Ricard4eea7032016-03-31 22:56:55 +020067 status = ioread8(priv->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -050069 dev_err(&chip->dev, "error reading data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -EIO;
71 }
72 }
73 return -EIO;
74 }
75
76 /* read all the data available */
77 for (; i < size; i++) {
Christophe Ricard4eea7032016-03-31 22:56:55 +020078 status = ioread8(priv->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -050080 dev_err(&chip->dev, "error reading data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EIO;
82 }
Christophe Ricard4eea7032016-03-31 22:56:55 +020083 *buf++ = ioread8(priv->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 /* make sure data available is gone */
Christophe Ricard4eea7032016-03-31 22:56:55 +020087 status = ioread8(priv->iobase + 1);
Kylene Jo Hall90612b32005-11-18 01:10:58 -080088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (status & ATML_STATUS_DATA_AVAIL) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -050090 dev_err(&chip->dev, "data available is stuck\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -EIO;
92 }
93
94 return size;
95}
96
Andrew Mortonb888c872005-10-30 15:03:28 -080097static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +020099 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int i;
101
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500102 dev_dbg(&chip->dev, "tpm_atml_send:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 for (i = 0; i < count; i++) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500104 dev_dbg(&chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]);
Christophe Ricard4eea7032016-03-31 22:56:55 +0200105 iowrite8(buf[i], priv->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
108 return count;
109}
110
111static void tpm_atml_cancel(struct tpm_chip *chip)
112{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200113 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
Christophe Ricard4eea7032016-03-31 22:56:55 +0200114
115 iowrite8(ATML_STATUS_ABORT, priv->iobase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800118static u8 tpm_atml_status(struct tpm_chip *chip)
119{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200120 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
Christophe Ricard4eea7032016-03-31 22:56:55 +0200121
122 return ioread8(priv->iobase + 1);
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800123}
124
Stefan Berger1f866052013-01-22 13:52:35 -0600125static bool tpm_atml_req_canceled(struct tpm_chip *chip, u8 status)
126{
127 return (status == ATML_STATUS_READY);
128}
129
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -0700130static const struct tpm_class_ops tpm_atmel = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .recv = tpm_atml_recv,
132 .send = tpm_atml_send,
133 .cancel = tpm_atml_cancel,
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -0800134 .status = tpm_atml_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
136 .req_complete_val = ATML_STATUS_DATA_AVAIL,
Stefan Berger1f866052013-01-22 13:52:35 -0600137 .req_canceled = tpm_atml_req_canceled,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Andrew Mortonb888c872005-10-30 15:03:28 -0800140static struct platform_device *pdev;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800141
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800142static void atml_plat_remove(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800144 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200145 struct tpm_atmel_priv *priv = dev_get_drvdata(&chip->dev);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800146
Andrew Mortonb888c872005-10-30 15:03:28 -0800147 if (chip) {
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800148 tpm_chip_unregister(chip);
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +0200149 if (priv->have_region)
Jarkko Sakkinenee177982016-03-23 08:16:09 +0200150 atmel_release_region(priv->base, priv->region_size);
Christophe Ricard4eea7032016-03-31 22:56:55 +0200151 atmel_put_base_addr(priv->iobase);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800152 platform_device_unregister(pdev);
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800153 }
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800154}
155
Rafael J. Wysocki8324be02012-07-06 19:09:13 +0200156static SIMPLE_DEV_PM_OPS(tpm_atml_pm, tpm_pm_suspend, tpm_pm_resume);
Ming Lei7a192ec2009-02-06 23:40:12 +0800157
Ming Lei7a192ec2009-02-06 23:40:12 +0800158static struct platform_driver atml_drv = {
159 .driver = {
160 .name = "tpm_atmel",
Rafael J. Wysocki8324be02012-07-06 19:09:13 +0200161 .pm = &tpm_atml_pm,
Ming Lei7a192ec2009-02-06 23:40:12 +0800162 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
165static int __init init_atmel(void)
166{
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800167 int rc = 0;
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700168 void __iomem *iobase = NULL;
169 int have_region, region_size;
170 unsigned long base;
171 struct tpm_chip *chip;
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +0200172 struct tpm_atmel_priv *priv;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800173
Ming Lei7a192ec2009-02-06 23:40:12 +0800174 rc = platform_driver_register(&atml_drv);
Jeff Garzikf33d9bd2006-10-11 01:21:51 -0700175 if (rc)
176 return rc;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800177
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700178 if ((iobase = atmel_get_base_addr(&base, &region_size)) == NULL) {
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800179 rc = -ENODEV;
180 goto err_unreg_drv;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800181 }
182
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700183 have_region =
Kylene Jo Hall90612b32005-11-18 01:10:58 -0800184 (atmel_request_region
Jason Gunthorpe1e6e0972013-09-14 17:36:29 -0600185 (base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700186
Jeff Garzikf33d9bd2006-10-11 01:21:51 -0700187 pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
188 if (IS_ERR(pdev)) {
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800189 rc = PTR_ERR(pdev);
190 goto err_rel_reg;
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800191 }
192
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +0200193 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
194 if (!priv) {
195 rc = -ENOMEM;
196 goto err_unreg_dev;
197 }
198
Christophe Ricard4eea7032016-03-31 22:56:55 +0200199 priv->iobase = iobase;
Jarkko Sakkinenee177982016-03-23 08:16:09 +0200200 priv->base = base;
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +0200201 priv->have_region = have_region;
202 priv->region_size = region_size;
203
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800204 chip = tpmm_chip_alloc(&pdev->dev, &tpm_atmel);
205 if (IS_ERR(chip)) {
206 rc = PTR_ERR(chip);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800207 goto err_unreg_dev;
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700208 }
209
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200210 dev_set_drvdata(&chip->dev, priv);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700211
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800212 rc = tpm_chip_register(chip);
213 if (rc)
214 goto err_unreg_dev;
215
Kylene Jo Hall682e97a2005-10-30 15:03:25 -0800216 return 0;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800217
218err_unreg_dev:
219 platform_device_unregister(pdev);
220err_rel_reg:
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700221 atmel_put_base_addr(iobase);
222 if (have_region)
223 atmel_release_region(base,
224 region_size);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800225err_unreg_drv:
Ming Lei7a192ec2009-02-06 23:40:12 +0800226 platform_driver_unregister(&atml_drv);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800227 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static void __exit cleanup_atmel(void)
231{
Ming Lei7a192ec2009-02-06 23:40:12 +0800232 platform_driver_unregister(&atml_drv);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800233 atml_plat_remove();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236module_init(init_atmel);
237module_exit(cleanup_atmel);
238
239MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
240MODULE_DESCRIPTION("TPM Driver");
241MODULE_VERSION("2.0");
242MODULE_LICENSE("GPL");