* Request to submit driver for Nuvoton TPM I2C device (WPCT301)
@ 2010-07-14 9:31 Dan.Morav
2010-07-14 10:16 ` Ohad Ben-Cohen
` (2 more replies)
0 siblings, 3 replies; 14+ messages in thread
From: Dan.Morav @ 2010-07-14 9:31 UTC (permalink / raw)
To: linux-kernel; +Cc: debora, srajiv, m.selhorst, Dan.Morav, Michal.Schramm
[-- Attachment #1: Type: text/plain, Size: 2247 bytes --]
Hello,
I'd like to submit a new TPM driver.
Attached is a new driver for Nuvoton TPM with I2C interface.
- tpm_i2c.c is the driver code
- tpm.h and Makefile are added only for reference and stand alone compilation
The driver should be part of the drivers/char/tpm.
The driver uses standard Linux I2C bus driver interface to access the I2C bus.
The driver uses standard Linux TPM driver API.
The driver was tested on Linux kernel v2.6.31.
Please advice if any further information is required.
Regards,
Dan.
_________________________
Dan Morav
Advanced PC
Nuvoton Technology Israel Ltd.
Office +972.9.970.2388
eMail: dan.morav@nuvoton.com
===========================================================================================
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
===========================================================================================
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
[-- Attachment #2: TPM_I2C_0.9.1.zip --]
[-- Type: application/x-zip-compressed, Size: 21718 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Request to submit driver for Nuvoton TPM I2C device (WPCT301)
2010-07-14 9:31 Request to submit driver for Nuvoton TPM I2C device (WPCT301) Dan.Morav
@ 2010-07-14 10:16 ` Ohad Ben-Cohen
2010-07-14 10:57 ` Marcel Selhorst
2010-08-09 9:24 ` [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35 Dan.Morav
2 siblings, 0 replies; 14+ messages in thread
From: Ohad Ben-Cohen @ 2010-07-14 10:16 UTC (permalink / raw)
To: Dan.Morav
Cc: linux-kernel, debora, srajiv, m.selhorst, Michal.Schramm, Greg KH
Hi Dan,
On Wed, Jul 14, 2010 at 12:31 PM, <Dan.Morav@nuvoton.com> wrote:
> Hello,
>
> I'd like to submit a new TPM driver.
Thank you for submitting a new driver.
However please note that your code is licensed under GPLv3:
* Nuvoton TPM I2C Device Driver Interface for WPCT301,
* based on the TCG TPM Interface Spec version 1.2.
* Specifications at www.trustedcomputinggroup.org
*
* Copyright (C) 2010, Nuvoton Technology Corporation.
*
* 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, either version 3 of the License, or
* (at your option) any later version.
The Linux kernel is licensed under GPLv2 which is not compatible with
GPLv3, so please (consider to) relicense to GPLv2 and only then
resubmit.
Also please check out Documentation/SubmittingPatches for guidance how
to submit code to the Linux kernel (no attachments please, instead use
plain in-line patches text).
Thanks,
Ohad.
>
> Attached is a new driver for Nuvoton TPM with I2C interface.
> - tpm_i2c.c is the driver code
> - tpm.h and Makefile are added only for reference and stand alone compilation
>
> The driver should be part of the drivers/char/tpm.
> The driver uses standard Linux I2C bus driver interface to access the I2C bus.
> The driver uses standard Linux TPM driver API.
> The driver was tested on Linux kernel v2.6.31.
>
> Please advice if any further information is required.
>
> Regards,
> Dan.
> _________________________
> Dan Morav
> Advanced PC
> Nuvoton Technology Israel Ltd.
> Office +972.9.970.2388
> eMail: dan.morav@nuvoton.com
>
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Request to submit driver for Nuvoton TPM I2C device (WPCT301)
2010-07-14 9:31 Request to submit driver for Nuvoton TPM I2C device (WPCT301) Dan.Morav
2010-07-14 10:16 ` Ohad Ben-Cohen
@ 2010-07-14 10:57 ` Marcel Selhorst
2010-07-14 15:25 ` Randy Dunlap
2010-08-09 9:24 ` [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35 Dan.Morav
2 siblings, 1 reply; 14+ messages in thread
From: Marcel Selhorst @ 2010-07-14 10:57 UTC (permalink / raw)
To: Dan.Morav; +Cc: linux-kernel, debora, srajiv, Michal.Schramm
Hi Dan,
thanks for the driver. When submitting the patch, could you please add some
information, where / on what platforms this chip can be found?
Also, could you please rename your driver to tpm_nuvoton_i2c.c (or
similar)? There are other I2C-TPM device drivers from other vendors
currently in the process of being included and we need to distinguish
between these.
I had a very quick look over your code, could you please put your #defines
on top of the file? And please provide a complete patch (including KConfig
/ Makefile) against Linux instead of sending individual files.
Thanks,
Marcel
Am 14.07.2010 11:31, schrieb Dan.Morav@nuvoton.com:
> Hello,
>
> I'd like to submit a new TPM driver.
>
> Attached is a new driver for Nuvoton TPM with I2C interface.
> - tpm_i2c.c is the driver code
> - tpm.h and Makefile are added only for reference and stand alone compilation
>
> The driver should be part of the drivers/char/tpm.
> The driver uses standard Linux I2C bus driver interface to access the I2C bus.
> The driver uses standard Linux TPM driver API.
> The driver was tested on Linux kernel v2.6.31.
>
> Please advice if any further information is required.
>
> Regards,
> Dan.
> _________________________
> Dan Morav
> Advanced PC
> Nuvoton Technology Israel Ltd.
> Office +972.9.970.2388
> eMail: dan.morav@nuvoton.com
>
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
--
Sirrix AG security technologies -- http://www.sirrix.com
Dipl.-Ing. Marcel Selhorst eMail: m.selhorst@sirrix.com
Tel: +49 (234) 610071-126 Fax: +49 (234) 610071-526
Tel: +49 (681) 95986-126 Fax: +49 (681) 95986-526
Get my public key from keyserver, KeyId: 0x7C9821CC
Fingerprint 4138 E617 E62E 79D3 E663 BE5A 14E7 1CD8 7C98 21CC
Vorstand: Ammar Alkassar (Vors.), Christian Stueble
Vorsitzender des Aufsichtsrates: Prof. Dr. Kai Rannenberg
Sitz der Gesellschaft: Homburg/Saar, HRB 3857 Amtsgericht Saarbruecken
This message may contain confidential and/or privileged information.
If you are not the addressee, you must not use, copy, disclose or
take any action based on this message or any information herein.
If you have received this message in error, please advise the sender
immediately by reply e-mail and delete this message.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Request to submit driver for Nuvoton TPM I2C device (WPCT301)
2010-07-14 10:57 ` Marcel Selhorst
@ 2010-07-14 15:25 ` Randy Dunlap
0 siblings, 0 replies; 14+ messages in thread
From: Randy Dunlap @ 2010-07-14 15:25 UTC (permalink / raw)
To: Marcel Selhorst; +Cc: Dan.Morav, linux-kernel, debora, srajiv, Michal.Schramm
On Wed, 14 Jul 2010 12:57:20 +0200 Marcel Selhorst wrote:
> Hi Dan,
>
> thanks for the driver. When submitting the patch, could you please add some
> information, where / on what platforms this chip can be found?
>
> Also, could you please rename your driver to tpm_nuvoton_i2c.c (or
> similar)? There are other I2C-TPM device drivers from other vendors
> currently in the process of being included and we need to distinguish
> between these.
>
> I had a very quick look over your code, could you please put your #defines
> on top of the file? And please provide a complete patch (including KConfig
> / Makefile) against Linux instead of sending individual files.
It should also be submitted as a patch to the current kernel.
This matters for the Kconfig / Makefile changes.
> Thanks,
> Marcel
>
> Am 14.07.2010 11:31, schrieb Dan.Morav@nuvoton.com:
> > Hello,
> >
> > I'd like to submit a new TPM driver.
> >
> > Attached is a new driver for Nuvoton TPM with I2C interface.
> > - tpm_i2c.c is the driver code
> > - tpm.h and Makefile are added only for reference and stand alone compilation
> >
> > The driver should be part of the drivers/char/tpm.
> > The driver uses standard Linux I2C bus driver interface to access the I2C bus.
> > The driver uses standard Linux TPM driver API.
> > The driver was tested on Linux kernel v2.6.31.
> >
> > Please advice if any further information is required.
> >
> > Regards,
> > Dan.
> > _________________________
> > Dan Morav
> > Advanced PC
> > Nuvoton Technology Israel Ltd.
> > Office +972.9.970.2388
> > eMail: dan.morav@nuvoton.com
> >
> >
> > ===========================================================================================
> > The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
> >
> > ===========================================================================================
> > The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
>
> --
> Sirrix AG security technologies -- http://www.sirrix.com
> Dipl.-Ing. Marcel Selhorst eMail: m.selhorst@sirrix.com
> Tel: +49 (234) 610071-126 Fax: +49 (234) 610071-526
> Tel: +49 (681) 95986-126 Fax: +49 (681) 95986-526
> Get my public key from keyserver, KeyId: 0x7C9821CC
> Fingerprint 4138 E617 E62E 79D3 E663 BE5A 14E7 1CD8 7C98 21CC
>
> Vorstand: Ammar Alkassar (Vors.), Christian Stueble
> Vorsitzender des Aufsichtsrates: Prof. Dr. Kai Rannenberg
> Sitz der Gesellschaft: Homburg/Saar, HRB 3857 Amtsgericht Saarbruecken
>
> This message may contain confidential and/or privileged information.
> If you are not the addressee, you must not use, copy, disclose or
> take any action based on this message or any information herein.
> If you have received this message in error, please advise the sender
> immediately by reply e-mail and delete this message.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35
2010-07-14 9:31 Request to submit driver for Nuvoton TPM I2C device (WPCT301) Dan.Morav
2010-07-14 10:16 ` Ohad Ben-Cohen
2010-07-14 10:57 ` Marcel Selhorst
@ 2010-08-09 9:24 ` Dan.Morav
2010-08-17 12:59 ` Rajiv Andrade
` (2 more replies)
2 siblings, 3 replies; 14+ messages in thread
From: Dan.Morav @ 2010-08-09 9:24 UTC (permalink / raw)
To: linux-kernel; +Cc: debora, srajiv, m.selhorst, Michal.Schramm
Hi,
Below are the additions for WPCT301 Nuvoton Technology TPM with I2C interface device driver.
This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
This driver tested on Linux kernel version 2.6.31, on an x86 platform. Added the required I2C API changes applied in 2.6.33 to enable compilation.
Regards,
Dan.
_________________________
Dan Morav
Advanced PC
Nuvoton Technology Israel Ltd.
Office +972.9.970.2388
eMail: dan.morav@nuvoton.com
diff -uN linux-2.6.35/drivers/char/tpm/Kconfig linux/drivers/char/tpm/Kconfig
--- linux-2.6.35/drivers/char/tpm/Kconfig 2010-08-02 01:11:14.000000000 +0300
+++ linux/drivers/char/tpm/Kconfig 2010-08-08 23:08:32.017438534 +0300
@@ -33,6 +33,15 @@
from within Linux. To compile this driver as a module, choose
M here; the module will be called tpm_tis.
+config TCG_NUVOTON_I2C
+ tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
+ depends on I2C
+ ---help---
+ If you have a TPM security chip with I2C interface from
+ Nuvoton Technology Corp. say Yes and it will be accessible
+ from within Linux. To compile this driver as a module, choose
+ M here; the module will be called tpm_nuvoton_i2c.
+
config TCG_NSC
tristate "National Semiconductor TPM Interface"
---help---
diff -uN linux-2.6.35/drivers/char/tpm/Makefile linux/drivers/char/tpm/Makefile
--- linux-2.6.35/drivers/char/tpm/Makefile 2010-08-02 01:11:14.000000000 +0300
+++ linux/drivers/char/tpm/Makefile 2010-08-08 11:56:30.721438699 +0300
@@ -9,3 +9,4 @@
obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
+obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
diff -uN linux-2.6.35/drivers/char/tpm/tpm_nuvoton_i2c.c linux/drivers/char/tpm/tpm_nuvoton_i2c.c
--- linux-2.6.35/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
+++ linux/drivers/char/tpm/tpm_nuvoton_i2c.c 2010-08-08 20:29:24.469438926 +0300
@@ -0,0 +1,748 @@
+/******************************************************************************
+ * Nuvoton TPM I2C Device Driver Interface for WPCT301,
+ * based on the TCG TPM Interface Spec version 1.2.
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * Copyright (C) 2010, Nuvoton Technology Corporation.
+ * dan.morav@nuvoton.com
+ *
+ * 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, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/>.
+ *
+ * Nuvoton contact information: APC.Support@nuvoton.com
+ *****************************************************************************/
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/wait.h>
+#include <linux/i2c.h>
+#include <linux/version.h>
+#include "tpm.h"
+
+/* I2C interface defintions */
+#define TPM_I2C_ADDR 0x57
+/* I2C interface offsets */
+#define TPM_STS 0x00
+#define TPM_DATA_FIFO_W 0x20
+#define TPM_DATA_FIFO_R 0x40
+#define TPM_VID_DID_RID 0x60
+/* I2C class required */
+#define I2C_CLASS_ALL (I2C_CLASS_HWMON | I2C_CLASS_SPD)
+/* TPM command header size */
+#define TPM_HEADER_SIZE 10
+/*
+ * I2C bus device maximum buffer size
+ * w/o counting I2C address or command
+ * i.e. max size required for I2C write is 34
+ * = I2C addr, command, 32 bytes data
+ */
+#define I2C_MAX_BUF 32
+
+/* I2C Addresses to scan */
+static const unsigned short normal_i2c[] = { TPM_I2C_ADDR, I2C_CLIENT_END };
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
+I2C_CLIENT_INSMOD_1(NuvotonTPM);
+#endif
+
+static struct tpm_chip *chip = NULL;
+static struct i2c_client *ti2c_i2c_client = NULL;
+
+static u8 i2cRead8(u8 offset)
+{
+ s32 status;
+ u8 result;
+
+ /* dev_info(chip->dev, "in i2cRead8(%0x)\n", offset); */
+ if (ti2c_i2c_client == NULL) {
+ dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
+ return 0xff;
+ }
+ status = i2c_smbus_read_i2c_block_data(
+ ti2c_i2c_client,
+ offset,
+ 1,
+ &result);
+ /*
+ * dev_info(
+ * chip->dev,
+ * "i2cRead8(%0x)=%0x, sts=%d\n", offset, result, status);
+ */
+
+ return result;
+}
+
+static u32 i2cRead32(u8 offset)
+{
+ s32 status;
+ u32 result;
+
+ /* dev_info(chip->dev, "in i2cRead32(%0x)\n", offset); */
+ if (ti2c_i2c_client == NULL) {
+ dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
+ return 0xffffffff;
+ }
+ status = i2c_smbus_read_i2c_block_data(
+ ti2c_i2c_client,
+ offset,
+ 4,
+ (u8*)&result);
+ /*
+ * dev_info(
+ * chip->dev,
+ * "i2cRead32(%0x)=%04x, sts=%d\n", offset, result, status);
+ */
+
+ return result;
+}
+
+static void i2cReadBuf(u8 offset, u8 size, u8 *data)
+{
+ s32 status;
+
+ /* dev_info(chip->dev, "in i2cReadBuf(%0x, %0x)\n", offset, size); */
+ if (ti2c_i2c_client == NULL) {
+ dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
+ return;
+ }
+ status = i2c_smbus_read_i2c_block_data(
+ ti2c_i2c_client,
+ offset,
+ size,
+ data);
+ /*
+ * dev_info(
+ * chip->dev,
+ * "i2cRead32(%0x)=%04x, sts=%d\n", offset, result, status);
+ */
+
+ return;
+}
+
+static void i2cWrite8(u8 data, u8 offset)
+{
+ s32 status;
+
+ /*
+ * dev_info(
+ * chip->dev,
+ * "in i2cWrite8(data=%0x, offset=%0x)\n", data, offset);
+ */
+ if (ti2c_i2c_client == NULL)
+ {
+ dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
+ return;
+ }
+ status = i2c_smbus_write_i2c_block_data(
+ ti2c_i2c_client,
+ offset,
+ 1,
+ &data);
+ /*
+ * dev_info(
+ * chip->dev,
+ * "i2cWrite8(%0x, %0x), sts=%d\n", data, offset, status);
+ */
+}
+
+static void i2cWriteBuf(u8 offset, u8 size, u8 *data)
+{
+ s32 status;
+
+ /*
+ * dev_info(
+ * chip->dev,
+ * "in i2cWriteBuf(offset=%0x, size=%0x)\n", offset, size);
+ */
+ if (ti2c_i2c_client == NULL)
+ {
+ dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
+ return;
+ }
+ status = i2c_smbus_write_i2c_block_data(
+ ti2c_i2c_client,
+ offset,
+ size,
+ data);
+ /*
+ * dev_info(
+ * chip->dev,
+ * "i2cWriteBuf(offset=%0x, size=%0x), sts=%d\n", offset, size, status);
+ */
+}
+
+static int ti2c_detect(
+ struct i2c_client *client,
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
+ int kind,
+#endif
+ struct i2c_board_info *info)
+{
+ struct i2c_adapter *adapter = client->adapter;
+
+ dev_info(chip->dev, "In detect function\n");
+ if (client->addr != TPM_I2C_ADDR)
+ {
+ dev_err(chip->dev, "Wrong I2C addr %x\n", client->addr);
+ return -ENODEV;
+ }
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
+ return -ENODEV;
+
+ strlcpy(info->type, "ti2c", I2C_NAME_SIZE);
+ dev_info(chip->dev, "End detect function\n");
+
+ return 0;
+}
+
+static const u8 ti2c_VidDidRidValue[] = {0x50, 0x10, 0xfe, 0x00};
+
+static int ti2c_i2c_prob(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ u32 vidDidRid;
+
+ dev_info(chip->dev, "in ti2c_i2c_prob\n");
+
+ if (client->addr != TPM_I2C_ADDR)
+ {
+ return -1; /* Error */
+ }
+
+ ti2c_i2c_client = client; /* Save a pointer to the i2c client struct
+ * we need it during the interrupt */
+
+ vidDidRid = i2cRead32(TPM_VID_DID_RID);
+ dev_info(chip->dev, "VID: %04X DID: %02X RID: %02X\n",
+ (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
+
+ /* check WPCT301 values */
+ if (memcmp((void*)&vidDidRid, ti2c_VidDidRidValue, sizeof(u32)))
+ {
+ /*
+ * f/w rev 2.81 has an issue where the VID_DID_RID is not reporting
+ * the right value. so give it another chance at offset 0x20 (FIFO_W)
+ */
+ vidDidRid = i2cRead32(TPM_DATA_FIFO_W);
+ dev_info(chip->dev, "VID: %04X DID: %02X RID: %02X\n",
+ (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
+
+ /* check WPCT301 values */
+ if (memcmp((void*)&vidDidRid, ti2c_VidDidRidValue, sizeof(u32)))
+ {
+ ti2c_i2c_client = NULL;
+ dev_err(chip->dev, "WPCT301 not found\n");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int ti2c_i2c_remove(struct i2c_client *client)
+{
+ dev_info(chip->dev, "in ti2c_i2c_remove\n");
+ kfree(i2c_get_clientdata(client));
+ return 0;
+}
+
+static const struct i2c_device_id ti2c_i2c_id[] = { { "ti2c", 0 }, {} };
+
+static struct i2c_driver ti2c_driver = {
+ .driver = {
+ .name= "ti2c",
+ },
+ .probe = ti2c_i2c_prob,
+ .remove = ti2c_i2c_remove,
+ .id_table = ti2c_i2c_id,
+ .class = I2C_CLASS_ALL,
+ .detect = ti2c_detect,
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
+ .address_data = &addr_data,
+#else
+ .address_list = normal_i2c,
+#endif
+};
+
+enum ti2c_status {
+ TPM_STS_VALID = 0x80,
+ TPM_STS_COMMAND_READY = 0x40,
+ TPM_STS_GO = 0x20,
+ TPM_STS_DATA_AVAIL = 0x10,
+ TPM_STS_DATA_EXPECT = 0x08,
+};
+
+enum ti2c_defaults {
+ TI2C_MEM_BASE = 0xAE,
+ TI2C_MEM_LEN = 0x0100,
+ TI2C_SHORT_TIMEOUT = 750, /* ms */
+ TI2C_LONG_TIMEOUT = 2000, /* 2 sec */
+};
+
+static u8 tpm_ti2c_status(struct tpm_chip *chip)
+{
+ return i2cRead8(TPM_STS);
+}
+
+static void tpm_ti2c_ready(struct tpm_chip *chip)
+{
+ /* this causes the current command to be aborted */
+ i2cWrite8(TPM_STS_COMMAND_READY, TPM_STS);
+}
+
+static int get_burstcount(struct tpm_chip *chip)
+{
+ unsigned long stop;
+ int burstcnt;
+
+ /* wait for burstcount */
+ /* which timeout value, spec has 2 answers (c & d) */
+ stop = jiffies + chip->vendor.timeout_d;
+ do { /* in I2C burstCount is 1 byte */
+ burstcnt = i2cRead8(TPM_STS + 1);
+ if (burstcnt)
+ return burstcnt > I2C_MAX_BUF ? I2C_MAX_BUF : burstcnt;
+ msleep(TPM_TIMEOUT);
+ } while (time_before(jiffies, stop));
+ return -EBUSY;
+}
+
+/*
+ * WPCT301 SINT# supports only dataAvail
+ * any call to this function which is not waiting for dataAvail will
+ * set queue to NULL to avoid waiting for interrupt
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+ wait_queue_head_t *queue)
+{
+ unsigned long stop;
+ long rc;
+ u8 status;
+
+ /* check current status */
+ status = tpm_ti2c_status(chip);
+ if ((status & mask) == mask)
+ return 0;
+
+ if ((chip->vendor.irq) && (queue != NULL)) {
+ rc = wait_event_interruptible_timeout(*queue,
+ ((tpm_ti2c_status(chip) & mask) == mask),
+ timeout);
+ if (rc > 0)
+ return 0;
+ } else {
+ stop = jiffies + timeout;
+ do {
+ msleep(TPM_TIMEOUT);
+ status = tpm_ti2c_status(chip);
+ if ((status & mask) == mask)
+ return 0;
+ } while (time_before(jiffies, stop));
+ }
+ return -ETIME;
+}
+
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+ int size = 0, burstcnt, bytes2read;
+
+ while ((size < count) &&
+ wait_for_stat(
+ chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ chip->vendor.timeout_c,
+ &chip->vendor.read_queue) == 0)
+ {
+ burstcnt = get_burstcount(chip);
+ /*
+ * for (; burstcnt > 0 && size < count; burstcnt--)
+ * buf[size++] = i2cRead8(TPM_DATA_FIFO_R);
+ */
+ bytes2read = burstcnt < (count - size) ? burstcnt : (count - size);
+ i2cReadBuf(TPM_DATA_FIFO_R, bytes2read, &buf[size]);
+ size += bytes2read;
+ }
+ return size;
+}
+
+static int tpm_ti2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+ int size = 0;
+ int expected, status;
+
+ if (count < TPM_HEADER_SIZE) {
+ size = -EIO;
+ goto out;
+ }
+
+ /* read first available (> 10 bytes), including:
+ * tag, paramsize, and result */
+ status = wait_for_stat(
+ chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ chip->vendor.timeout_c,
+ &chip->vendor.read_queue);
+ if (status != 0) {
+ size = -EIO;
+ goto out;
+ }
+ size = get_burstcount(chip);
+ if ((size = recv_data(chip, buf, size)) < TPM_HEADER_SIZE) {
+ dev_err(chip->dev, "Unable to read header\n");
+ goto out;
+ }
+
+ expected = be32_to_cpu(*(__be32 *) (buf + 2));
+ if (expected > count) {
+ size = -EIO;
+ goto out;
+ }
+
+ if ((size += recv_data(chip, &buf[size], expected - size)) < expected) {
+ dev_err(chip->dev, "Unable to read remainder of result\n");
+ size = -ETIME;
+ goto out;
+ }
+
+ /* for WPCT301 both stsValid and dataAvail set at the same time */
+ wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
+ status = tpm_ti2c_status(chip);
+ if (status & TPM_STS_DATA_AVAIL) { /* retry? */
+ dev_err(chip->dev, "Error left over data\n");
+ size = -EIO;
+ goto out;
+ }
+
+out:
+ tpm_ti2c_ready(chip);
+ return size;
+}
+
+/*
+ * If interrupts are used (signaled by an irq set in the vendor structure)
+ * tpm.c can skip polling for the data to be available as the interrupt is
+ * waited for here
+ */
+static int tpm_ti2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
+{
+ int rc, status, burstcnt, bytes2write;
+ size_t count = 0;
+ u32 ordinal;
+
+ status = tpm_ti2c_status(chip);
+ if ((status & TPM_STS_COMMAND_READY) == 0) {
+ tpm_ti2c_ready(chip);
+ rc = wait_for_stat(
+ chip,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b,
+ NULL);
+ if (rc == -ETIME)
+ goto out_err;
+ }
+
+ while (count < len - 1) {
+ burstcnt = get_burstcount(chip);
+ /*
+ * for (; burstcnt > 0 && count < len - 1; burstcnt--) {
+ * i2cWrite8(buf[count], TPM_DATA_FIFO_W);
+ * count++;
+ * }
+ */
+ bytes2write = (len -1 -count) < burstcnt ? (len -1 -count) : burstcnt;
+ i2cWriteBuf(TPM_DATA_FIFO_W, bytes2write, &buf[count]);
+ count += bytes2write;
+
+ wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
+ status = tpm_ti2c_status(chip);
+ if ((status & TPM_STS_DATA_EXPECT) == 0) {
+ rc = -EIO;
+ goto out_err;
+ }
+ }
+
+ /* write last byte */
+ i2cWrite8(buf[count], TPM_DATA_FIFO_W);
+ wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
+ status = tpm_ti2c_status(chip);
+ if ((status & TPM_STS_DATA_EXPECT) != 0) {
+ rc = -EIO;
+ goto out_err;
+ }
+
+ /* go and do it */
+ i2cWrite8(TPM_STS_GO, TPM_STS);
+
+ ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+ rc = wait_for_stat(
+ chip,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ tpm_calc_ordinal_duration(chip, ordinal),
+ &chip->vendor.read_queue);
+ if (rc == -ETIME)
+ goto out_err;
+
+ return len;
+
+out_err:
+ tpm_ti2c_ready(chip);
+ return rc;
+}
+
+static struct file_operations ti2c_ops = {
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .open = tpm_open,
+ .read = tpm_read,
+ .write = tpm_write,
+ .release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
+ NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *ti2c_attrs[] = {
+ &dev_attr_pubek.attr,
+ &dev_attr_pcrs.attr,
+ &dev_attr_enabled.attr,
+ &dev_attr_active.attr,
+ &dev_attr_owned.attr,
+ &dev_attr_temp_deactivated.attr,
+ &dev_attr_caps.attr,
+ &dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group ti2c_attr_grp = {
+ .attrs = ti2c_attrs
+};
+
+static struct tpm_vendor_specific tpm_ti2c = {
+ .status = tpm_ti2c_status,
+ .recv = tpm_ti2c_recv,
+ .send = tpm_ti2c_send,
+ .cancel = tpm_ti2c_ready,
+ .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_canceled = TPM_STS_COMMAND_READY,
+ .attr_group = &ti2c_attr_grp,
+ .miscdev = {
+ .fops = &ti2c_ops,},
+};
+
+static irqreturn_t ti2c_int_handler(int dummy, void *dev_id)
+{
+ struct tpm_chip *chip = dev_id;
+
+ /* in I2C interrupt is triggeted only on dataAvail */
+ wake_up_interruptible(&chip->vendor.read_queue);
+
+ /*
+ * consider disabling the interrupt in the host
+ * in case of interrupt storm as TPM SINT# is level interrupt
+ * which is cleared only when dataAvail is cleared
+ */
+
+ return IRQ_HANDLED;
+}
+
+static int tpm_ti2c_init(struct device *dev, unsigned long start,
+ unsigned long len)
+{
+ u32 vendor;
+ int rc;
+
+ dev_info(dev, "in tpm_ti2c_init\n");
+ if (!start)
+ start = TI2C_MEM_BASE;
+ if (!len)
+ len = TI2C_MEM_LEN;
+
+ chip = tpm_register_hardware(dev, &tpm_ti2c);
+ if (chip == NULL)
+ {
+ dev_err(dev, "error in tpm_register_hardware\n");
+ return -ENODEV;
+ }
+
+ rc = i2c_add_driver(&ti2c_driver);
+ if (rc != 0)
+ {
+ dev_err(dev, "error registering i2c\n");
+ goto out_err;
+ }
+
+ if (ti2c_i2c_client == NULL)
+ {
+ dev_err(dev, "error allocating i2c\n");
+ rc = -ENODEV;
+ goto out_err1;
+ }
+
+ /* Default timeouts */
+ chip->vendor.timeout_a = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_b = msecs_to_jiffies(TI2C_LONG_TIMEOUT);
+ chip->vendor.timeout_c = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_d = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
+
+ vendor = i2cRead32(TPM_VID_DID_RID);
+ dev_info(dev,
+ "1.2 TPM (device-id 0x%X, rev-id %d)\n",
+ vendor >> 16, (u16)vendor);
+
+ /*
+ * Figure out the capabilities
+ * I2C hard coded:
+ * intfcaps = TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT;
+ */
+ dev_dbg(dev, "TPM interface capabilities:\n");
+ dev_dbg(dev, "\tInterrupt Level Low\n");
+ dev_dbg(dev, "\tData Avail Int Support\n");
+
+ /* INTERRUPT Setup */
+ init_waitqueue_head(&chip->vendor.read_queue);
+ init_waitqueue_head(&chip->vendor.int_queue);
+
+ /*
+ * I2C hard coded:
+ * intmask = TPM_INTF_DATA_AVAIL_INT;
+ */
+ if (chip->vendor.irq) {
+ if (request_irq(
+ chip->vendor.irq,
+ ti2c_int_handler,
+ IRQF_PROBE_SHARED,
+ chip->vendor.miscdev.name,
+ chip) != 0)
+ {
+ dev_info(dev,
+ "Unable to request irq: %d for use\n", chip->vendor.irq);
+ chip->vendor.irq = 0;
+ } else {
+ /*
+ * Clear all existing
+ * consider adding code that will make sure TIS is in idle state
+ * and that dataAvail is off in the TPM_STS:
+ * - TPM_STS <- 0x40 (commandReady)
+ */
+ tpm_ti2c_ready(chip);
+ /* - wait for TPM_STS == 0xA0 (stsValid, commandReady) */
+ rc = wait_for_stat(
+ chip,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b,
+ NULL);
+ if (rc == 0)
+ {
+ /*
+ * TIS is in ready state
+ * write dummy byte to enter reception state
+ * TPM_DATA_FIFO_W <- 0x00
+ */
+ i2cWrite8(0x00, TPM_DATA_FIFO_W);
+ /* TPM_STS <- 0x40 (commandReady) */
+ tpm_ti2c_ready(chip);
+ } else {
+ /*
+ * if timeout_b is reached then command was aborted,
+ * TIS in idle state
+ */
+ if (tpm_ti2c_status(chip) != TPM_STS_VALID) {
+ rc = -EIO;
+ goto out_err1;
+ }
+ }
+ }
+ }
+
+ /* uncomment in case BIOS does not send TPM_StartUp(ST_CLEAR) command */
+ /* tpm_startup_clear(chip); */
+ tpm_get_timeouts(chip);
+ tpm_continue_selftest(chip);
+
+ return 0;
+
+out_err1:
+ i2c_del_driver(&ti2c_driver);
+out_err:
+ tpm_remove_hardware(chip->dev);
+ return rc;
+}
+
+static int tpm_ti2c_suspend(struct platform_device *dev, pm_message_t msg)
+{
+ return tpm_pm_suspend(&dev->dev, msg);
+}
+
+static int tpm_ti2c_resume(struct platform_device *dev)
+{
+ return tpm_pm_resume(&dev->dev);
+}
+
+static struct platform_driver ti2c_drv = {
+ .driver = {
+ .name = "tpm_ti2c",
+ .owner = THIS_MODULE,
+ },
+ .suspend = tpm_ti2c_suspend,
+ .resume = tpm_ti2c_resume,
+};
+
+static struct platform_device *pdev;
+
+static int __init init_ti2c(void)
+{
+ int rc;
+
+ rc = platform_driver_register(&ti2c_drv);
+ if (rc < 0)
+ return rc;
+
+ pdev = platform_device_register_simple("tpm_ti2c", -1, NULL, 0);
+ if (IS_ERR(pdev))
+ return PTR_ERR(pdev);
+
+ rc = tpm_ti2c_init(&pdev->dev, 0, 0);
+ if(rc != 0) {
+ platform_device_unregister(pdev);
+ platform_driver_unregister(&ti2c_drv);
+ }
+
+ return rc;
+}
+
+static void __exit cleanup_ti2c(void)
+{
+ dev_info(chip->dev,"in cleanup_ti2c\n");
+
+ i2c_del_driver(&ti2c_driver);
+ tpm_remove_hardware(chip->dev);
+ if (chip->vendor.irq)
+ free_irq(chip->vendor.irq, chip);
+ platform_device_unregister(pdev);
+ platform_driver_unregister(&ti2c_drv);
+}
+
+module_init(init_ti2c);
+module_exit(cleanup_ti2c);
+MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
+MODULE_DESCRIPTION("Nuvton TPM I2C Driver");
+MODULE_VERSION("0.9.2");
+MODULE_LICENSE("GPL");
===========================================================================================
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
===========================================================================================
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35
2010-08-09 9:24 ` [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35 Dan.Morav
@ 2010-08-17 12:59 ` Rajiv Andrade
2010-08-17 15:03 ` Wolfram Sang
2011-07-25 7:25 ` [PATCH] TPM Nuvoton I2C driver, kernel 3.0 Dan.Morav
2 siblings, 0 replies; 14+ messages in thread
From: Rajiv Andrade @ 2010-08-17 12:59 UTC (permalink / raw)
To: <Dan.Morav@nuvoton.com>
Cc: linux-kernel, Debora Velarde, jmorris, m.selhorst, Michal.Schramm
Acked-by: Rajiv Andrade <srajiv@linux.vnet.ibm.com>
On Aug 9, 2010, at 6:24 AM, <Dan.Morav@nuvoton.com> <Dan.Morav@nuvoton.com> wrote:
> Hi,
>
> Below are the additions for WPCT301 Nuvoton Technology TPM with I2C interface device driver.
> This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
> This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
> This driver tested on Linux kernel version 2.6.31, on an x86 platform. Added the required I2C API changes applied in 2.6.33 to enable compilation.
>
> Regards,
> Dan.
> _________________________
> Dan Morav
> Advanced PC
> Nuvoton Technology Israel Ltd.
> Office +972.9.970.2388
> eMail: dan.morav@nuvoton.com
>
> diff -uN linux-2.6.35/drivers/char/tpm/Kconfig linux/drivers/char/tpm/Kconfig
> --- linux-2.6.35/drivers/char/tpm/Kconfig 2010-08-02 01:11:14.000000000 +0300
> +++ linux/drivers/char/tpm/Kconfig 2010-08-08 23:08:32.017438534 +0300
> @@ -33,6 +33,15 @@
> from within Linux. To compile this driver as a module, choose
> M here; the module will be called tpm_tis.
>
> +config TCG_NUVOTON_I2C
> + tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
> + depends on I2C
> + ---help---
> + If you have a TPM security chip with I2C interface from
> + Nuvoton Technology Corp. say Yes and it will be accessible
> + from within Linux. To compile this driver as a module, choose
> + M here; the module will be called tpm_nuvoton_i2c.
> +
> config TCG_NSC
> tristate "National Semiconductor TPM Interface"
> ---help---
> diff -uN linux-2.6.35/drivers/char/tpm/Makefile linux/drivers/char/tpm/Makefile
> --- linux-2.6.35/drivers/char/tpm/Makefile 2010-08-02 01:11:14.000000000 +0300
> +++ linux/drivers/char/tpm/Makefile 2010-08-08 11:56:30.721438699 +0300
> @@ -9,3 +9,4 @@
> obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
> obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
> obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
> +obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
> diff -uN linux-2.6.35/drivers/char/tpm/tpm_nuvoton_i2c.c linux/drivers/char/tpm/tpm_nuvoton_i2c.c
> --- linux-2.6.35/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
> +++ linux/drivers/char/tpm/tpm_nuvoton_i2c.c 2010-08-08 20:29:24.469438926 +0300
> @@ -0,0 +1,748 @@
> +/******************************************************************************
> + * Nuvoton TPM I2C Device Driver Interface for WPCT301,
> + * based on the TCG TPM Interface Spec version 1.2.
> + * Specifications at www.trustedcomputinggroup.org
> + *
> + * Copyright (C) 2010, Nuvoton Technology Corporation.
> + * dan.morav@nuvoton.com
> + *
> + * 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, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program. If not, see http://www.gnu.org/licenses/>.
> + *
> + * Nuvoton contact information: APC.Support@nuvoton.com
> + *****************************************************************************/
> +
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/moduleparam.h>
> +#include <linux/slab.h>
> +#include <linux/interrupt.h>
> +#include <linux/wait.h>
> +#include <linux/i2c.h>
> +#include <linux/version.h>
> +#include "tpm.h"
> +
> +/* I2C interface defintions */
> +#define TPM_I2C_ADDR 0x57
> +/* I2C interface offsets */
> +#define TPM_STS 0x00
> +#define TPM_DATA_FIFO_W 0x20
> +#define TPM_DATA_FIFO_R 0x40
> +#define TPM_VID_DID_RID 0x60
> +/* I2C class required */
> +#define I2C_CLASS_ALL (I2C_CLASS_HWMON | I2C_CLASS_SPD)
> +/* TPM command header size */
> +#define TPM_HEADER_SIZE 10
> +/*
> + * I2C bus device maximum buffer size
> + * w/o counting I2C address or command
> + * i.e. max size required for I2C write is 34
> + * = I2C addr, command, 32 bytes data
> + */
> +#define I2C_MAX_BUF 32
> +
> +/* I2C Addresses to scan */
> +static const unsigned short normal_i2c[] = { TPM_I2C_ADDR, I2C_CLIENT_END };
> +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
> +I2C_CLIENT_INSMOD_1(NuvotonTPM);
> +#endif
> +
> +static struct tpm_chip *chip = NULL;
> +static struct i2c_client *ti2c_i2c_client = NULL;
> +
> +static u8 i2cRead8(u8 offset)
> +{
> + s32 status;
> + u8 result;
> +
> + /* dev_info(chip->dev, "in i2cRead8(%0x)\n", offset); */
> + if (ti2c_i2c_client == NULL) {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return 0xff;
> + }
> + status = i2c_smbus_read_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + 1,
> + &result);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cRead8(%0x)=%0x, sts=%d\n", offset, result, status);
> + */
> +
> + return result;
> +}
> +
> +static u32 i2cRead32(u8 offset)
> +{
> + s32 status;
> + u32 result;
> +
> + /* dev_info(chip->dev, "in i2cRead32(%0x)\n", offset); */
> + if (ti2c_i2c_client == NULL) {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return 0xffffffff;
> + }
> + status = i2c_smbus_read_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + 4,
> + (u8*)&result);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cRead32(%0x)=%04x, sts=%d\n", offset, result, status);
> + */
> +
> + return result;
> +}
> +
> +static void i2cReadBuf(u8 offset, u8 size, u8 *data)
> +{
> + s32 status;
> +
> + /* dev_info(chip->dev, "in i2cReadBuf(%0x, %0x)\n", offset, size); */
> + if (ti2c_i2c_client == NULL) {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return;
> + }
> + status = i2c_smbus_read_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + size,
> + data);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cRead32(%0x)=%04x, sts=%d\n", offset, result, status);
> + */
> +
> + return;
> +}
> +
> +static void i2cWrite8(u8 data, u8 offset)
> +{
> + s32 status;
> +
> + /*
> + * dev_info(
> + * chip->dev,
> + * "in i2cWrite8(data=%0x, offset=%0x)\n", data, offset);
> + */
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return;
> + }
> + status = i2c_smbus_write_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + 1,
> + &data);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cWrite8(%0x, %0x), sts=%d\n", data, offset, status);
> + */
> +}
> +
> +static void i2cWriteBuf(u8 offset, u8 size, u8 *data)
> +{
> + s32 status;
> +
> + /*
> + * dev_info(
> + * chip->dev,
> + * "in i2cWriteBuf(offset=%0x, size=%0x)\n", offset, size);
> + */
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return;
> + }
> + status = i2c_smbus_write_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + size,
> + data);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cWriteBuf(offset=%0x, size=%0x), sts=%d\n", offset, size, status);
> + */
> +}
> +
> +static int ti2c_detect(
> + struct i2c_client *client,
> +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
> + int kind,
> +#endif
> + struct i2c_board_info *info)
> +{
> + struct i2c_adapter *adapter = client->adapter;
> +
> + dev_info(chip->dev, "In detect function\n");
> + if (client->addr != TPM_I2C_ADDR)
> + {
> + dev_err(chip->dev, "Wrong I2C addr %x\n", client->addr);
> + return -ENODEV;
> + }
> +
> + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
> + return -ENODEV;
> +
> + strlcpy(info->type, "ti2c", I2C_NAME_SIZE);
> + dev_info(chip->dev, "End detect function\n");
> +
> + return 0;
> +}
> +
> +static const u8 ti2c_VidDidRidValue[] = {0x50, 0x10, 0xfe, 0x00};
> +
> +static int ti2c_i2c_prob(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + u32 vidDidRid;
> +
> + dev_info(chip->dev, "in ti2c_i2c_prob\n");
> +
> + if (client->addr != TPM_I2C_ADDR)
> + {
> + return -1; /* Error */
> + }
> +
> + ti2c_i2c_client = client; /* Save a pointer to the i2c client struct
> + * we need it during the interrupt */
> +
> + vidDidRid = i2cRead32(TPM_VID_DID_RID);
> + dev_info(chip->dev, "VID: %04X DID: %02X RID: %02X\n",
> + (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
> +
> + /* check WPCT301 values */
> + if (memcmp((void*)&vidDidRid, ti2c_VidDidRidValue, sizeof(u32)))
> + {
> + /*
> + * f/w rev 2.81 has an issue where the VID_DID_RID is not reporting
> + * the right value. so give it another chance at offset 0x20 (FIFO_W)
> + */
> + vidDidRid = i2cRead32(TPM_DATA_FIFO_W);
> + dev_info(chip->dev, "VID: %04X DID: %02X RID: %02X\n",
> + (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
> +
> + /* check WPCT301 values */
> + if (memcmp((void*)&vidDidRid, ti2c_VidDidRidValue, sizeof(u32)))
> + {
> + ti2c_i2c_client = NULL;
> + dev_err(chip->dev, "WPCT301 not found\n");
> + return -1;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static int ti2c_i2c_remove(struct i2c_client *client)
> +{
> + dev_info(chip->dev, "in ti2c_i2c_remove\n");
> + kfree(i2c_get_clientdata(client));
> + return 0;
> +}
> +
> +static const struct i2c_device_id ti2c_i2c_id[] = { { "ti2c", 0 }, {} };
> +
> +static struct i2c_driver ti2c_driver = {
> + .driver = {
> + .name= "ti2c",
> + },
> + .probe = ti2c_i2c_prob,
> + .remove = ti2c_i2c_remove,
> + .id_table = ti2c_i2c_id,
> + .class = I2C_CLASS_ALL,
> + .detect = ti2c_detect,
> +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
> + .address_data = &addr_data,
> +#else
> + .address_list = normal_i2c,
> +#endif
> +};
> +
> +enum ti2c_status {
> + TPM_STS_VALID = 0x80,
> + TPM_STS_COMMAND_READY = 0x40,
> + TPM_STS_GO = 0x20,
> + TPM_STS_DATA_AVAIL = 0x10,
> + TPM_STS_DATA_EXPECT = 0x08,
> +};
> +
> +enum ti2c_defaults {
> + TI2C_MEM_BASE = 0xAE,
> + TI2C_MEM_LEN = 0x0100,
> + TI2C_SHORT_TIMEOUT = 750, /* ms */
> + TI2C_LONG_TIMEOUT = 2000, /* 2 sec */
> +};
> +
> +static u8 tpm_ti2c_status(struct tpm_chip *chip)
> +{
> + return i2cRead8(TPM_STS);
> +}
> +
> +static void tpm_ti2c_ready(struct tpm_chip *chip)
> +{
> + /* this causes the current command to be aborted */
> + i2cWrite8(TPM_STS_COMMAND_READY, TPM_STS);
> +}
> +
> +static int get_burstcount(struct tpm_chip *chip)
> +{
> + unsigned long stop;
> + int burstcnt;
> +
> + /* wait for burstcount */
> + /* which timeout value, spec has 2 answers (c & d) */
> + stop = jiffies + chip->vendor.timeout_d;
> + do { /* in I2C burstCount is 1 byte */
> + burstcnt = i2cRead8(TPM_STS + 1);
> + if (burstcnt)
> + return burstcnt > I2C_MAX_BUF ? I2C_MAX_BUF : burstcnt;
> + msleep(TPM_TIMEOUT);
> + } while (time_before(jiffies, stop));
> + return -EBUSY;
> +}
> +
> +/*
> + * WPCT301 SINT# supports only dataAvail
> + * any call to this function which is not waiting for dataAvail will
> + * set queue to NULL to avoid waiting for interrupt
> + */
> +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
> + wait_queue_head_t *queue)
> +{
> + unsigned long stop;
> + long rc;
> + u8 status;
> +
> + /* check current status */
> + status = tpm_ti2c_status(chip);
> + if ((status & mask) == mask)
> + return 0;
> +
> + if ((chip->vendor.irq) && (queue != NULL)) {
> + rc = wait_event_interruptible_timeout(*queue,
> + ((tpm_ti2c_status(chip) & mask) == mask),
> + timeout);
> + if (rc > 0)
> + return 0;
> + } else {
> + stop = jiffies + timeout;
> + do {
> + msleep(TPM_TIMEOUT);
> + status = tpm_ti2c_status(chip);
> + if ((status & mask) == mask)
> + return 0;
> + } while (time_before(jiffies, stop));
> + }
> + return -ETIME;
> +}
> +
> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> + int size = 0, burstcnt, bytes2read;
> +
> + while ((size < count) &&
> + wait_for_stat(
> + chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + chip->vendor.timeout_c,
> + &chip->vendor.read_queue) == 0)
> + {
> + burstcnt = get_burstcount(chip);
> + /*
> + * for (; burstcnt > 0 && size < count; burstcnt--)
> + * buf[size++] = i2cRead8(TPM_DATA_FIFO_R);
> + */
> + bytes2read = burstcnt < (count - size) ? burstcnt : (count - size);
> + i2cReadBuf(TPM_DATA_FIFO_R, bytes2read, &buf[size]);
> + size += bytes2read;
> + }
> + return size;
> +}
> +
> +static int tpm_ti2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> + int size = 0;
> + int expected, status;
> +
> + if (count < TPM_HEADER_SIZE) {
> + size = -EIO;
> + goto out;
> + }
> +
> + /* read first available (> 10 bytes), including:
> + * tag, paramsize, and result */
> + status = wait_for_stat(
> + chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + chip->vendor.timeout_c,
> + &chip->vendor.read_queue);
> + if (status != 0) {
> + size = -EIO;
> + goto out;
> + }
> + size = get_burstcount(chip);
> + if ((size = recv_data(chip, buf, size)) < TPM_HEADER_SIZE) {
> + dev_err(chip->dev, "Unable to read header\n");
> + goto out;
> + }
> +
> + expected = be32_to_cpu(*(__be32 *) (buf + 2));
> + if (expected > count) {
> + size = -EIO;
> + goto out;
> + }
> +
> + if ((size += recv_data(chip, &buf[size], expected - size)) < expected) {
> + dev_err(chip->dev, "Unable to read remainder of result\n");
> + size = -ETIME;
> + goto out;
> + }
> +
> + /* for WPCT301 both stsValid and dataAvail set at the same time */
> + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
> + status = tpm_ti2c_status(chip);
> + if (status & TPM_STS_DATA_AVAIL) { /* retry? */
> + dev_err(chip->dev, "Error left over data\n");
> + size = -EIO;
> + goto out;
> + }
> +
> +out:
> + tpm_ti2c_ready(chip);
> + return size;
> +}
> +
> +/*
> + * If interrupts are used (signaled by an irq set in the vendor structure)
> + * tpm.c can skip polling for the data to be available as the interrupt is
> + * waited for here
> + */
> +static int tpm_ti2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
> +{
> + int rc, status, burstcnt, bytes2write;
> + size_t count = 0;
> + u32 ordinal;
> +
> + status = tpm_ti2c_status(chip);
> + if ((status & TPM_STS_COMMAND_READY) == 0) {
> + tpm_ti2c_ready(chip);
> + rc = wait_for_stat(
> + chip,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b,
> + NULL);
> + if (rc == -ETIME)
> + goto out_err;
> + }
> +
> + while (count < len - 1) {
> + burstcnt = get_burstcount(chip);
> + /*
> + * for (; burstcnt > 0 && count < len - 1; burstcnt--) {
> + * i2cWrite8(buf[count], TPM_DATA_FIFO_W);
> + * count++;
> + * }
> + */
> + bytes2write = (len -1 -count) < burstcnt ? (len -1 -count) : burstcnt;
> + i2cWriteBuf(TPM_DATA_FIFO_W, bytes2write, &buf[count]);
> + count += bytes2write;
> +
> + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
> + status = tpm_ti2c_status(chip);
> + if ((status & TPM_STS_DATA_EXPECT) == 0) {
> + rc = -EIO;
> + goto out_err;
> + }
> + }
> +
> + /* write last byte */
> + i2cWrite8(buf[count], TPM_DATA_FIFO_W);
> + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
> + status = tpm_ti2c_status(chip);
> + if ((status & TPM_STS_DATA_EXPECT) != 0) {
> + rc = -EIO;
> + goto out_err;
> + }
> +
> + /* go and do it */
> + i2cWrite8(TPM_STS_GO, TPM_STS);
> +
> + ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
> + rc = wait_for_stat(
> + chip,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + tpm_calc_ordinal_duration(chip, ordinal),
> + &chip->vendor.read_queue);
> + if (rc == -ETIME)
> + goto out_err;
> +
> + return len;
> +
> +out_err:
> + tpm_ti2c_ready(chip);
> + return rc;
> +}
> +
> +static struct file_operations ti2c_ops = {
> + .owner = THIS_MODULE,
> + .llseek = no_llseek,
> + .open = tpm_open,
> + .read = tpm_read,
> + .write = tpm_write,
> + .release = tpm_release,
> +};
> +
> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
> +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
> +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
> +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
> +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
> +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
> + NULL);
> +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
> +
> +static struct attribute *ti2c_attrs[] = {
> + &dev_attr_pubek.attr,
> + &dev_attr_pcrs.attr,
> + &dev_attr_enabled.attr,
> + &dev_attr_active.attr,
> + &dev_attr_owned.attr,
> + &dev_attr_temp_deactivated.attr,
> + &dev_attr_caps.attr,
> + &dev_attr_cancel.attr, NULL,
> +};
> +
> +static struct attribute_group ti2c_attr_grp = {
> + .attrs = ti2c_attrs
> +};
> +
> +static struct tpm_vendor_specific tpm_ti2c = {
> + .status = tpm_ti2c_status,
> + .recv = tpm_ti2c_recv,
> + .send = tpm_ti2c_send,
> + .cancel = tpm_ti2c_ready,
> + .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_canceled = TPM_STS_COMMAND_READY,
> + .attr_group = &ti2c_attr_grp,
> + .miscdev = {
> + .fops = &ti2c_ops,},
> +};
> +
> +static irqreturn_t ti2c_int_handler(int dummy, void *dev_id)
> +{
> + struct tpm_chip *chip = dev_id;
> +
> + /* in I2C interrupt is triggeted only on dataAvail */
> + wake_up_interruptible(&chip->vendor.read_queue);
> +
> + /*
> + * consider disabling the interrupt in the host
> + * in case of interrupt storm as TPM SINT# is level interrupt
> + * which is cleared only when dataAvail is cleared
> + */
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int tpm_ti2c_init(struct device *dev, unsigned long start,
> + unsigned long len)
> +{
> + u32 vendor;
> + int rc;
> +
> + dev_info(dev, "in tpm_ti2c_init\n");
> + if (!start)
> + start = TI2C_MEM_BASE;
> + if (!len)
> + len = TI2C_MEM_LEN;
> +
> + chip = tpm_register_hardware(dev, &tpm_ti2c);
> + if (chip == NULL)
> + {
> + dev_err(dev, "error in tpm_register_hardware\n");
> + return -ENODEV;
> + }
> +
> + rc = i2c_add_driver(&ti2c_driver);
> + if (rc != 0)
> + {
> + dev_err(dev, "error registering i2c\n");
> + goto out_err;
> + }
> +
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(dev, "error allocating i2c\n");
> + rc = -ENODEV;
> + goto out_err1;
> + }
> +
> + /* Default timeouts */
> + chip->vendor.timeout_a = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_b = msecs_to_jiffies(TI2C_LONG_TIMEOUT);
> + chip->vendor.timeout_c = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_d = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> +
> + vendor = i2cRead32(TPM_VID_DID_RID);
> + dev_info(dev,
> + "1.2 TPM (device-id 0x%X, rev-id %d)\n",
> + vendor >> 16, (u16)vendor);
> +
> + /*
> + * Figure out the capabilities
> + * I2C hard coded:
> + * intfcaps = TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT;
> + */
> + dev_dbg(dev, "TPM interface capabilities:\n");
> + dev_dbg(dev, "\tInterrupt Level Low\n");
> + dev_dbg(dev, "\tData Avail Int Support\n");
> +
> + /* INTERRUPT Setup */
> + init_waitqueue_head(&chip->vendor.read_queue);
> + init_waitqueue_head(&chip->vendor.int_queue);
> +
> + /*
> + * I2C hard coded:
> + * intmask = TPM_INTF_DATA_AVAIL_INT;
> + */
> + if (chip->vendor.irq) {
> + if (request_irq(
> + chip->vendor.irq,
> + ti2c_int_handler,
> + IRQF_PROBE_SHARED,
> + chip->vendor.miscdev.name,
> + chip) != 0)
> + {
> + dev_info(dev,
> + "Unable to request irq: %d for use\n", chip->vendor.irq);
> + chip->vendor.irq = 0;
> + } else {
> + /*
> + * Clear all existing
> + * consider adding code that will make sure TIS is in idle state
> + * and that dataAvail is off in the TPM_STS:
> + * - TPM_STS <- 0x40 (commandReady)
> + */
> + tpm_ti2c_ready(chip);
> + /* - wait for TPM_STS == 0xA0 (stsValid, commandReady) */
> + rc = wait_for_stat(
> + chip,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b,
> + NULL);
> + if (rc == 0)
> + {
> + /*
> + * TIS is in ready state
> + * write dummy byte to enter reception state
> + * TPM_DATA_FIFO_W <- 0x00
> + */
> + i2cWrite8(0x00, TPM_DATA_FIFO_W);
> + /* TPM_STS <- 0x40 (commandReady) */
> + tpm_ti2c_ready(chip);
> + } else {
> + /*
> + * if timeout_b is reached then command was aborted,
> + * TIS in idle state
> + */
> + if (tpm_ti2c_status(chip) != TPM_STS_VALID) {
> + rc = -EIO;
> + goto out_err1;
> + }
> + }
> + }
> + }
> +
> + /* uncomment in case BIOS does not send TPM_StartUp(ST_CLEAR) command */
> + /* tpm_startup_clear(chip); */
> + tpm_get_timeouts(chip);
> + tpm_continue_selftest(chip);
> +
> + return 0;
> +
> +out_err1:
> + i2c_del_driver(&ti2c_driver);
> +out_err:
> + tpm_remove_hardware(chip->dev);
> + return rc;
> +}
> +
> +static int tpm_ti2c_suspend(struct platform_device *dev, pm_message_t msg)
> +{
> + return tpm_pm_suspend(&dev->dev, msg);
> +}
> +
> +static int tpm_ti2c_resume(struct platform_device *dev)
> +{
> + return tpm_pm_resume(&dev->dev);
> +}
> +
> +static struct platform_driver ti2c_drv = {
> + .driver = {
> + .name = "tpm_ti2c",
> + .owner = THIS_MODULE,
> + },
> + .suspend = tpm_ti2c_suspend,
> + .resume = tpm_ti2c_resume,
> +};
> +
> +static struct platform_device *pdev;
> +
> +static int __init init_ti2c(void)
> +{
> + int rc;
> +
> + rc = platform_driver_register(&ti2c_drv);
> + if (rc < 0)
> + return rc;
> +
> + pdev = platform_device_register_simple("tpm_ti2c", -1, NULL, 0);
> + if (IS_ERR(pdev))
> + return PTR_ERR(pdev);
> +
> + rc = tpm_ti2c_init(&pdev->dev, 0, 0);
> + if(rc != 0) {
> + platform_device_unregister(pdev);
> + platform_driver_unregister(&ti2c_drv);
> + }
> +
> + return rc;
> +}
> +
> +static void __exit cleanup_ti2c(void)
> +{
> + dev_info(chip->dev,"in cleanup_ti2c\n");
> +
> + i2c_del_driver(&ti2c_driver);
> + tpm_remove_hardware(chip->dev);
> + if (chip->vendor.irq)
> + free_irq(chip->vendor.irq, chip);
> + platform_device_unregister(pdev);
> + platform_driver_unregister(&ti2c_drv);
> +}
> +
> +module_init(init_ti2c);
> +module_exit(cleanup_ti2c);
> +MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
> +MODULE_DESCRIPTION("Nuvton TPM I2C Driver");
> +MODULE_VERSION("0.9.2");
> +MODULE_LICENSE("GPL");
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
>
>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35
2010-08-09 9:24 ` [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35 Dan.Morav
2010-08-17 12:59 ` Rajiv Andrade
@ 2010-08-17 15:03 ` Wolfram Sang
2011-07-25 7:25 ` [PATCH] TPM Nuvoton I2C driver, kernel 3.0 Dan.Morav
2 siblings, 0 replies; 14+ messages in thread
From: Wolfram Sang @ 2010-08-17 15:03 UTC (permalink / raw)
To: Dan.Morav; +Cc: linux-kernel, debora, srajiv, m.selhorst, Michal.Schramm
[-- Attachment #1: Type: text/plain, Size: 29157 bytes --]
Hello Dan,
On Mon, Aug 09, 2010 at 12:24:01PM +0300, Dan.Morav@nuvoton.com wrote:
> Hi,
>
> Below are the additions for WPCT301 Nuvoton Technology TPM with I2C interface device driver.
> This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
> This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
> This driver tested on Linux kernel version 2.6.31, on an x86 platform. Added the required I2C API changes applied in 2.6.33 to enable compilation.
If you want to have it included in the mainline kernel, it should be
tested against a recent kernel, optimum would be 2.6.36-rc1.
>
> Regards,
> Dan.
> _________________________
> Dan Morav
> Advanced PC
> Nuvoton Technology Israel Ltd.
> Office +972.9.970.2388
> eMail: dan.morav@nuvoton.com
>
> diff -uN linux-2.6.35/drivers/char/tpm/Kconfig linux/drivers/char/tpm/Kconfig
> --- linux-2.6.35/drivers/char/tpm/Kconfig 2010-08-02 01:11:14.000000000 +0300
> +++ linux/drivers/char/tpm/Kconfig 2010-08-08 23:08:32.017438534 +0300
> @@ -33,6 +33,15 @@
> from within Linux. To compile this driver as a module, choose
> M here; the module will be called tpm_tis.
>
> +config TCG_NUVOTON_I2C
> + tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
> + depends on I2C
> + ---help---
> + If you have a TPM security chip with I2C interface from
> + Nuvoton Technology Corp. say Yes and it will be accessible
> + from within Linux. To compile this driver as a module, choose
> + M here; the module will be called tpm_nuvoton_i2c.
> +
> config TCG_NSC
> tristate "National Semiconductor TPM Interface"
> ---help---
> diff -uN linux-2.6.35/drivers/char/tpm/Makefile linux/drivers/char/tpm/Makefile
> --- linux-2.6.35/drivers/char/tpm/Makefile 2010-08-02 01:11:14.000000000 +0300
> +++ linux/drivers/char/tpm/Makefile 2010-08-08 11:56:30.721438699 +0300
> @@ -9,3 +9,4 @@
> obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
> obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
> obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
> +obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
> diff -uN linux-2.6.35/drivers/char/tpm/tpm_nuvoton_i2c.c linux/drivers/char/tpm/tpm_nuvoton_i2c.c
> --- linux-2.6.35/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
> +++ linux/drivers/char/tpm/tpm_nuvoton_i2c.c 2010-08-08 20:29:24.469438926 +0300
> @@ -0,0 +1,748 @@
> +/******************************************************************************
> + * Nuvoton TPM I2C Device Driver Interface for WPCT301,
> + * based on the TCG TPM Interface Spec version 1.2.
> + * Specifications at www.trustedcomputinggroup.org
> + *
> + * Copyright (C) 2010, Nuvoton Technology Corporation.
> + * dan.morav@nuvoton.com
> + *
> + * 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, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program. If not, see http://www.gnu.org/licenses/>.
> + *
> + * Nuvoton contact information: APC.Support@nuvoton.com
> + *****************************************************************************/
> +
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/moduleparam.h>
> +#include <linux/slab.h>
> +#include <linux/interrupt.h>
> +#include <linux/wait.h>
> +#include <linux/i2c.h>
> +#include <linux/version.h>
> +#include "tpm.h"
> +
> +/* I2C interface defintions */
> +#define TPM_I2C_ADDR 0x57
> +/* I2C interface offsets */
> +#define TPM_STS 0x00
> +#define TPM_DATA_FIFO_W 0x20
> +#define TPM_DATA_FIFO_R 0x40
> +#define TPM_VID_DID_RID 0x60
> +/* I2C class required */
> +#define I2C_CLASS_ALL (I2C_CLASS_HWMON | I2C_CLASS_SPD)
> +/* TPM command header size */
> +#define TPM_HEADER_SIZE 10
> +/*
> + * I2C bus device maximum buffer size
> + * w/o counting I2C address or command
> + * i.e. max size required for I2C write is 34
> + * = I2C addr, command, 32 bytes data
> + */
> +#define I2C_MAX_BUF 32
> +
> +/* I2C Addresses to scan */
> +static const unsigned short normal_i2c[] = { TPM_I2C_ADDR, I2C_CLIENT_END };
> +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
> +I2C_CLIENT_INSMOD_1(NuvotonTPM);
> +#endif
No workarounds for older kernels in upstream drivers, please.
> +
> +static struct tpm_chip *chip = NULL;
> +static struct i2c_client *ti2c_i2c_client = NULL;
static? so you can have only one instance?
> +
> +static u8 i2cRead8(u8 offset)
the function name does not match the others in style, should be more
like 'i2c_read8'
> +{
> + s32 status;
indentation is wrong. should be one tab.
please make sure you used the checkpatch-script. and please read
CodingStyle and SubmittingPatches in the Documentation/ -folder
> + u8 result;
> +
> + /* dev_info(chip->dev, "in i2cRead8(%0x)\n", offset); */
those need to go, also all later occurences
> + if (ti2c_i2c_client == NULL) {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return 0xff;
> + }
> + status = i2c_smbus_read_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + 1,
> + &result);
This could be in one or two lines, also all similar occurences
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cRead8(%0x)=%0x, sts=%d\n", offset, result, status);
> + */
> +
> + return result;
> +}
> +
> +static u32 i2cRead32(u8 offset)
> +{
> + s32 status;
> + u32 result;
> +
> + /* dev_info(chip->dev, "in i2cRead32(%0x)\n", offset); */
> + if (ti2c_i2c_client == NULL) {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return 0xffffffff;
> + }
> + status = i2c_smbus_read_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + 4,
> + (u8*)&result);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cRead32(%0x)=%04x, sts=%d\n", offset, result, status);
> + */
> +
> + return result;
> +}
> +
> +static void i2cReadBuf(u8 offset, u8 size, u8 *data)
> +{
> + s32 status;
> +
> + /* dev_info(chip->dev, "in i2cReadBuf(%0x, %0x)\n", offset, size); */
> + if (ti2c_i2c_client == NULL) {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return;
> + }
> + status = i2c_smbus_read_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + size,
> + data);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cRead32(%0x)=%04x, sts=%d\n", offset, result, status);
> + */
> +
> + return;
You can drop plain 'return' at the end of a function.
> +}
> +
> +static void i2cWrite8(u8 data, u8 offset)
> +{
> + s32 status;
> +
> + /*
> + * dev_info(
> + * chip->dev,
> + * "in i2cWrite8(data=%0x, offset=%0x)\n", data, offset);
> + */
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return;
> + }
> + status = i2c_smbus_write_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + 1,
> + &data);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cWrite8(%0x, %0x), sts=%d\n", data, offset, status);
> + */
> +}
> +
> +static void i2cWriteBuf(u8 offset, u8 size, u8 *data)
> +{
> + s32 status;
> +
> + /*
> + * dev_info(
> + * chip->dev,
> + * "in i2cWriteBuf(offset=%0x, size=%0x)\n", offset, size);
> + */
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(chip->dev, "ti2c_i2c_client is NULL\n");
> + return;
> + }
> + status = i2c_smbus_write_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + size,
> + data);
> + /*
> + * dev_info(
> + * chip->dev,
> + * "i2cWriteBuf(offset=%0x, size=%0x), sts=%d\n", offset, size, status);
> + */
> +}
> +
> +static int ti2c_detect(
> + struct i2c_client *client,
> +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
> + int kind,
> +#endif
drop this again...
> + struct i2c_board_info *info)
> +{
> + struct i2c_adapter *adapter = client->adapter;
> +
> + dev_info(chip->dev, "In detect function\n");
> + if (client->addr != TPM_I2C_ADDR)
> + {
> + dev_err(chip->dev, "Wrong I2C addr %x\n", client->addr);
> + return -ENODEV;
> + }
> +
> + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
> + return -ENODEV;
> +
> + strlcpy(info->type, "ti2c", I2C_NAME_SIZE);
> + dev_info(chip->dev, "End detect function\n");
> +
> + return 0;
> +}
> +
> +static const u8 ti2c_VidDidRidValue[] = {0x50, 0x10, 0xfe, 0x00};
> +
> +static int ti2c_i2c_prob(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + u32 vidDidRid;
> +
> + dev_info(chip->dev, "in ti2c_i2c_prob\n");
> +
> + if (client->addr != TPM_I2C_ADDR)
> + {
> + return -1; /* Error */
-ESOMETHING?
Well, I'll stop here. Please read the documentation I mentioned above
and adjust the driver accordingly. Once it matches most of the formal
requirements, reviewing the internal workings of the driver will be much
easier.
Of course, thank you for contributing this driver in the first place!
Regards,
Wolfram
> + }
> +
> + ti2c_i2c_client = client; /* Save a pointer to the i2c client struct
> + * we need it during the interrupt */
> +
> + vidDidRid = i2cRead32(TPM_VID_DID_RID);
> + dev_info(chip->dev, "VID: %04X DID: %02X RID: %02X\n",
> + (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
> +
> + /* check WPCT301 values */
> + if (memcmp((void*)&vidDidRid, ti2c_VidDidRidValue, sizeof(u32)))
> + {
> + /*
> + * f/w rev 2.81 has an issue where the VID_DID_RID is not reporting
> + * the right value. so give it another chance at offset 0x20 (FIFO_W)
> + */
> + vidDidRid = i2cRead32(TPM_DATA_FIFO_W);
> + dev_info(chip->dev, "VID: %04X DID: %02X RID: %02X\n",
> + (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
> +
> + /* check WPCT301 values */
> + if (memcmp((void*)&vidDidRid, ti2c_VidDidRidValue, sizeof(u32)))
> + {
> + ti2c_i2c_client = NULL;
> + dev_err(chip->dev, "WPCT301 not found\n");
> + return -1;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static int ti2c_i2c_remove(struct i2c_client *client)
> +{
> + dev_info(chip->dev, "in ti2c_i2c_remove\n");
> + kfree(i2c_get_clientdata(client));
> + return 0;
> +}
> +
> +static const struct i2c_device_id ti2c_i2c_id[] = { { "ti2c", 0 }, {} };
> +
> +static struct i2c_driver ti2c_driver = {
> + .driver = {
> + .name= "ti2c",
> + },
> + .probe = ti2c_i2c_prob,
> + .remove = ti2c_i2c_remove,
> + .id_table = ti2c_i2c_id,
> + .class = I2C_CLASS_ALL,
> + .detect = ti2c_detect,
> +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
> + .address_data = &addr_data,
> +#else
> + .address_list = normal_i2c,
> +#endif
> +};
> +
> +enum ti2c_status {
> + TPM_STS_VALID = 0x80,
> + TPM_STS_COMMAND_READY = 0x40,
> + TPM_STS_GO = 0x20,
> + TPM_STS_DATA_AVAIL = 0x10,
> + TPM_STS_DATA_EXPECT = 0x08,
> +};
> +
> +enum ti2c_defaults {
> + TI2C_MEM_BASE = 0xAE,
> + TI2C_MEM_LEN = 0x0100,
> + TI2C_SHORT_TIMEOUT = 750, /* ms */
> + TI2C_LONG_TIMEOUT = 2000, /* 2 sec */
> +};
> +
> +static u8 tpm_ti2c_status(struct tpm_chip *chip)
> +{
> + return i2cRead8(TPM_STS);
> +}
> +
> +static void tpm_ti2c_ready(struct tpm_chip *chip)
> +{
> + /* this causes the current command to be aborted */
> + i2cWrite8(TPM_STS_COMMAND_READY, TPM_STS);
> +}
> +
> +static int get_burstcount(struct tpm_chip *chip)
> +{
> + unsigned long stop;
> + int burstcnt;
> +
> + /* wait for burstcount */
> + /* which timeout value, spec has 2 answers (c & d) */
> + stop = jiffies + chip->vendor.timeout_d;
> + do { /* in I2C burstCount is 1 byte */
> + burstcnt = i2cRead8(TPM_STS + 1);
> + if (burstcnt)
> + return burstcnt > I2C_MAX_BUF ? I2C_MAX_BUF : burstcnt;
> + msleep(TPM_TIMEOUT);
> + } while (time_before(jiffies, stop));
> + return -EBUSY;
> +}
> +
> +/*
> + * WPCT301 SINT# supports only dataAvail
> + * any call to this function which is not waiting for dataAvail will
> + * set queue to NULL to avoid waiting for interrupt
> + */
> +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
> + wait_queue_head_t *queue)
> +{
> + unsigned long stop;
> + long rc;
> + u8 status;
> +
> + /* check current status */
> + status = tpm_ti2c_status(chip);
> + if ((status & mask) == mask)
> + return 0;
> +
> + if ((chip->vendor.irq) && (queue != NULL)) {
> + rc = wait_event_interruptible_timeout(*queue,
> + ((tpm_ti2c_status(chip) & mask) == mask),
> + timeout);
> + if (rc > 0)
> + return 0;
> + } else {
> + stop = jiffies + timeout;
> + do {
> + msleep(TPM_TIMEOUT);
> + status = tpm_ti2c_status(chip);
> + if ((status & mask) == mask)
> + return 0;
> + } while (time_before(jiffies, stop));
> + }
> + return -ETIME;
> +}
> +
> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> + int size = 0, burstcnt, bytes2read;
> +
> + while ((size < count) &&
> + wait_for_stat(
> + chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + chip->vendor.timeout_c,
> + &chip->vendor.read_queue) == 0)
> + {
> + burstcnt = get_burstcount(chip);
> + /*
> + * for (; burstcnt > 0 && size < count; burstcnt--)
> + * buf[size++] = i2cRead8(TPM_DATA_FIFO_R);
> + */
> + bytes2read = burstcnt < (count - size) ? burstcnt : (count - size);
> + i2cReadBuf(TPM_DATA_FIFO_R, bytes2read, &buf[size]);
> + size += bytes2read;
> + }
> + return size;
> +}
> +
> +static int tpm_ti2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> + int size = 0;
> + int expected, status;
> +
> + if (count < TPM_HEADER_SIZE) {
> + size = -EIO;
> + goto out;
> + }
> +
> + /* read first available (> 10 bytes), including:
> + * tag, paramsize, and result */
> + status = wait_for_stat(
> + chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + chip->vendor.timeout_c,
> + &chip->vendor.read_queue);
> + if (status != 0) {
> + size = -EIO;
> + goto out;
> + }
> + size = get_burstcount(chip);
> + if ((size = recv_data(chip, buf, size)) < TPM_HEADER_SIZE) {
> + dev_err(chip->dev, "Unable to read header\n");
> + goto out;
> + }
> +
> + expected = be32_to_cpu(*(__be32 *) (buf + 2));
> + if (expected > count) {
> + size = -EIO;
> + goto out;
> + }
> +
> + if ((size += recv_data(chip, &buf[size], expected - size)) < expected) {
> + dev_err(chip->dev, "Unable to read remainder of result\n");
> + size = -ETIME;
> + goto out;
> + }
> +
> + /* for WPCT301 both stsValid and dataAvail set at the same time */
> + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
> + status = tpm_ti2c_status(chip);
> + if (status & TPM_STS_DATA_AVAIL) { /* retry? */
> + dev_err(chip->dev, "Error left over data\n");
> + size = -EIO;
> + goto out;
> + }
> +
> +out:
> + tpm_ti2c_ready(chip);
> + return size;
> +}
> +
> +/*
> + * If interrupts are used (signaled by an irq set in the vendor structure)
> + * tpm.c can skip polling for the data to be available as the interrupt is
> + * waited for here
> + */
> +static int tpm_ti2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
> +{
> + int rc, status, burstcnt, bytes2write;
> + size_t count = 0;
> + u32 ordinal;
> +
> + status = tpm_ti2c_status(chip);
> + if ((status & TPM_STS_COMMAND_READY) == 0) {
> + tpm_ti2c_ready(chip);
> + rc = wait_for_stat(
> + chip,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b,
> + NULL);
> + if (rc == -ETIME)
> + goto out_err;
> + }
> +
> + while (count < len - 1) {
> + burstcnt = get_burstcount(chip);
> + /*
> + * for (; burstcnt > 0 && count < len - 1; burstcnt--) {
> + * i2cWrite8(buf[count], TPM_DATA_FIFO_W);
> + * count++;
> + * }
> + */
> + bytes2write = (len -1 -count) < burstcnt ? (len -1 -count) : burstcnt;
> + i2cWriteBuf(TPM_DATA_FIFO_W, bytes2write, &buf[count]);
> + count += bytes2write;
> +
> + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
> + status = tpm_ti2c_status(chip);
> + if ((status & TPM_STS_DATA_EXPECT) == 0) {
> + rc = -EIO;
> + goto out_err;
> + }
> + }
> +
> + /* write last byte */
> + i2cWrite8(buf[count], TPM_DATA_FIFO_W);
> + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, NULL);
> + status = tpm_ti2c_status(chip);
> + if ((status & TPM_STS_DATA_EXPECT) != 0) {
> + rc = -EIO;
> + goto out_err;
> + }
> +
> + /* go and do it */
> + i2cWrite8(TPM_STS_GO, TPM_STS);
> +
> + ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
> + rc = wait_for_stat(
> + chip,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + tpm_calc_ordinal_duration(chip, ordinal),
> + &chip->vendor.read_queue);
> + if (rc == -ETIME)
> + goto out_err;
> +
> + return len;
> +
> +out_err:
> + tpm_ti2c_ready(chip);
> + return rc;
> +}
> +
> +static struct file_operations ti2c_ops = {
> + .owner = THIS_MODULE,
> + .llseek = no_llseek,
> + .open = tpm_open,
> + .read = tpm_read,
> + .write = tpm_write,
> + .release = tpm_release,
> +};
> +
> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
> +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
> +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
> +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
> +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
> +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
> + NULL);
> +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
> +
> +static struct attribute *ti2c_attrs[] = {
> + &dev_attr_pubek.attr,
> + &dev_attr_pcrs.attr,
> + &dev_attr_enabled.attr,
> + &dev_attr_active.attr,
> + &dev_attr_owned.attr,
> + &dev_attr_temp_deactivated.attr,
> + &dev_attr_caps.attr,
> + &dev_attr_cancel.attr, NULL,
> +};
> +
> +static struct attribute_group ti2c_attr_grp = {
> + .attrs = ti2c_attrs
> +};
> +
> +static struct tpm_vendor_specific tpm_ti2c = {
> + .status = tpm_ti2c_status,
> + .recv = tpm_ti2c_recv,
> + .send = tpm_ti2c_send,
> + .cancel = tpm_ti2c_ready,
> + .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_canceled = TPM_STS_COMMAND_READY,
> + .attr_group = &ti2c_attr_grp,
> + .miscdev = {
> + .fops = &ti2c_ops,},
> +};
> +
> +static irqreturn_t ti2c_int_handler(int dummy, void *dev_id)
> +{
> + struct tpm_chip *chip = dev_id;
> +
> + /* in I2C interrupt is triggeted only on dataAvail */
> + wake_up_interruptible(&chip->vendor.read_queue);
> +
> + /*
> + * consider disabling the interrupt in the host
> + * in case of interrupt storm as TPM SINT# is level interrupt
> + * which is cleared only when dataAvail is cleared
> + */
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int tpm_ti2c_init(struct device *dev, unsigned long start,
> + unsigned long len)
> +{
> + u32 vendor;
> + int rc;
> +
> + dev_info(dev, "in tpm_ti2c_init\n");
> + if (!start)
> + start = TI2C_MEM_BASE;
> + if (!len)
> + len = TI2C_MEM_LEN;
> +
> + chip = tpm_register_hardware(dev, &tpm_ti2c);
> + if (chip == NULL)
> + {
> + dev_err(dev, "error in tpm_register_hardware\n");
> + return -ENODEV;
> + }
> +
> + rc = i2c_add_driver(&ti2c_driver);
> + if (rc != 0)
> + {
> + dev_err(dev, "error registering i2c\n");
> + goto out_err;
> + }
> +
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(dev, "error allocating i2c\n");
> + rc = -ENODEV;
> + goto out_err1;
> + }
> +
> + /* Default timeouts */
> + chip->vendor.timeout_a = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_b = msecs_to_jiffies(TI2C_LONG_TIMEOUT);
> + chip->vendor.timeout_c = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_d = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> +
> + vendor = i2cRead32(TPM_VID_DID_RID);
> + dev_info(dev,
> + "1.2 TPM (device-id 0x%X, rev-id %d)\n",
> + vendor >> 16, (u16)vendor);
> +
> + /*
> + * Figure out the capabilities
> + * I2C hard coded:
> + * intfcaps = TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT;
> + */
> + dev_dbg(dev, "TPM interface capabilities:\n");
> + dev_dbg(dev, "\tInterrupt Level Low\n");
> + dev_dbg(dev, "\tData Avail Int Support\n");
> +
> + /* INTERRUPT Setup */
> + init_waitqueue_head(&chip->vendor.read_queue);
> + init_waitqueue_head(&chip->vendor.int_queue);
> +
> + /*
> + * I2C hard coded:
> + * intmask = TPM_INTF_DATA_AVAIL_INT;
> + */
> + if (chip->vendor.irq) {
> + if (request_irq(
> + chip->vendor.irq,
> + ti2c_int_handler,
> + IRQF_PROBE_SHARED,
> + chip->vendor.miscdev.name,
> + chip) != 0)
> + {
> + dev_info(dev,
> + "Unable to request irq: %d for use\n", chip->vendor.irq);
> + chip->vendor.irq = 0;
> + } else {
> + /*
> + * Clear all existing
> + * consider adding code that will make sure TIS is in idle state
> + * and that dataAvail is off in the TPM_STS:
> + * - TPM_STS <- 0x40 (commandReady)
> + */
> + tpm_ti2c_ready(chip);
> + /* - wait for TPM_STS == 0xA0 (stsValid, commandReady) */
> + rc = wait_for_stat(
> + chip,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b,
> + NULL);
> + if (rc == 0)
> + {
> + /*
> + * TIS is in ready state
> + * write dummy byte to enter reception state
> + * TPM_DATA_FIFO_W <- 0x00
> + */
> + i2cWrite8(0x00, TPM_DATA_FIFO_W);
> + /* TPM_STS <- 0x40 (commandReady) */
> + tpm_ti2c_ready(chip);
> + } else {
> + /*
> + * if timeout_b is reached then command was aborted,
> + * TIS in idle state
> + */
> + if (tpm_ti2c_status(chip) != TPM_STS_VALID) {
> + rc = -EIO;
> + goto out_err1;
> + }
> + }
> + }
> + }
> +
> + /* uncomment in case BIOS does not send TPM_StartUp(ST_CLEAR) command */
> + /* tpm_startup_clear(chip); */
> + tpm_get_timeouts(chip);
> + tpm_continue_selftest(chip);
> +
> + return 0;
> +
> +out_err1:
> + i2c_del_driver(&ti2c_driver);
> +out_err:
> + tpm_remove_hardware(chip->dev);
> + return rc;
> +}
> +
> +static int tpm_ti2c_suspend(struct platform_device *dev, pm_message_t msg)
> +{
> + return tpm_pm_suspend(&dev->dev, msg);
> +}
> +
> +static int tpm_ti2c_resume(struct platform_device *dev)
> +{
> + return tpm_pm_resume(&dev->dev);
> +}
> +
> +static struct platform_driver ti2c_drv = {
> + .driver = {
> + .name = "tpm_ti2c",
> + .owner = THIS_MODULE,
> + },
> + .suspend = tpm_ti2c_suspend,
> + .resume = tpm_ti2c_resume,
> +};
> +
> +static struct platform_device *pdev;
> +
> +static int __init init_ti2c(void)
> +{
> + int rc;
> +
> + rc = platform_driver_register(&ti2c_drv);
> + if (rc < 0)
> + return rc;
> +
> + pdev = platform_device_register_simple("tpm_ti2c", -1, NULL, 0);
> + if (IS_ERR(pdev))
> + return PTR_ERR(pdev);
> +
> + rc = tpm_ti2c_init(&pdev->dev, 0, 0);
> + if(rc != 0) {
> + platform_device_unregister(pdev);
> + platform_driver_unregister(&ti2c_drv);
> + }
> +
> + return rc;
> +}
> +
> +static void __exit cleanup_ti2c(void)
> +{
> + dev_info(chip->dev,"in cleanup_ti2c\n");
> +
> + i2c_del_driver(&ti2c_driver);
> + tpm_remove_hardware(chip->dev);
> + if (chip->vendor.irq)
> + free_irq(chip->vendor.irq, chip);
> + platform_device_unregister(pdev);
> + platform_driver_unregister(&ti2c_drv);
> +}
> +
> +module_init(init_ti2c);
> +module_exit(cleanup_ti2c);
> +MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
> +MODULE_DESCRIPTION("Nuvton TPM I2C Driver");
> +MODULE_VERSION("0.9.2");
> +MODULE_LICENSE("GPL");
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Pengutronix e.K. | Wolfram Sang |
Industrial Linux Solutions | http://www.pengutronix.de/ |
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH] TPM Nuvoton I2C driver, kernel 3.0
2010-08-09 9:24 ` [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35 Dan.Morav
2010-08-17 12:59 ` Rajiv Andrade
2010-08-17 15:03 ` Wolfram Sang
@ 2011-07-25 7:25 ` Dan.Morav
2011-07-26 0:50 ` Ryan Mallon
` (3 more replies)
2 siblings, 4 replies; 14+ messages in thread
From: Dan.Morav @ 2011-07-25 7:25 UTC (permalink / raw)
To: linux-kernel; +Cc: debora, srajiv, m.selhorst, Leonid.Azriel, Guy.Pavlov
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 31351 bytes --]
Hi,
Below are the additions for WPCT301/NPCT501 Nuvoton Technology TPM with I2C interface device driver.
This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
This driver tested on Linux kernel version 3.0-rc6 on an x86 based platform and Linux kernel version 2.6.36.3 on an ARM platform.
Regards,
Dan.
_________________________
Dan Morav
Advanced PC
Nuvoton Technology Israel Ltd.
Office +972.9.970.2233
eMail: dan.morav@nuvoton.com
diff -uN linux-3.0-rc6/drivers/char/tpm/Kconfig linux/drivers/char/tpm/Kconfig
--- linux-3.0-rc6/drivers/char/tpm/Kconfig 2011-07-25 10:07:31.519844000 +0300
+++ linux/drivers/char/tpm/Kconfig 2011-07-25 10:07:46.999567000 +0300
@@ -60,4 +60,13 @@
Further information on this driver and the supported hardware
can be found at http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
+config TCG_NUVOTON_I2C
+ tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
+ depends on I2C
+ ---help---
+ If you have a TPM security chip with I2C interface from
+ Nuvoton Technology Corp. say Yes and it will be accessible
+ from within Linux. To compile this driver as a module, choose
+ M here; the module will be called tpm_nuvoton_i2c.
+
endif # TCG_TPM
diff -uN linux-3.0-rc6/drivers/char/tpm/Makefile linux/drivers/char/tpm/Makefile
--- linux-3.0-rc6/drivers/char/tpm/Makefile 2011-07-25 10:07:31.533850000 +0300
+++ linux/drivers/char/tpm/Makefile 2011-07-25 10:07:47.018567000 +0300
@@ -9,3 +9,4 @@
obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
+obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
diff -uN linux-3.0-rc6/drivers/char/tpm/tpm_nuvoton_i2c.c linux/drivers/char/tpm/tpm_nuvoton_i2c.c
--- linux-3.0-rc6/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
+++ linux/drivers/char/tpm/tpm_nuvoton_i2c.c 2011-07-25 10:07:47.198570000 +0300
@@ -0,0 +1,914 @@
+/******************************************************************************
+ * Nuvoton TPM I2C Device Driver Interface for WPCT301,
+ * based on the TCG TPM Interface Spec version 1.2.
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * Copyright (C) 2011, Nuvoton Technology Corporation.
+ * dan.morav@nuvoton.com
+ *
+ * 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, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/>.
+ *
+ * Nuvoton contact information: APC.Support@nuvoton.com
+ *****************************************************************************/
+
+/*
+ * #define DEBUG 1
+ * #define TI2C_DBG 1
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/wait.h>
+#include <linux/i2c.h>
+#include <linux/version.h>
+#include "tpm.h"
+
+/* I2C interface defintions */
+#define TPM_I2C_ADDR 0x57
+/* I2C interface offsets */
+#define TPM_STS 0x00
+#define TPM_DATA_FIFO_W 0x20
+#define TPM_DATA_FIFO_R 0x40
+#define TPM_VID_DID_RID 0x60
+/* I2C class required */
+#define I2C_CLASS_ALL (I2C_CLASS_HWMON | I2C_CLASS_SPD)
+/* TPM command header size */
+#define TPM_HEADER_SIZE 10
+#define TPM_RETRY 5
+/*
+ * I2C bus device maximum buffer size
+ * w/o counting I2C address or command
+ * i.e. max size required for I2C write is 34
+ * = I2C addr, command, 32 bytes data
+ */
+#define I2C_MAX_BUF_SIZE 32
+#define I2C_RETRY_COUNT 32
+#define I2C_BUS_DELAY 1 /* msec */
+#define I2C_RETRY_DELAY_SHORT 2 /* msec */
+#define I2C_RETRY_DELAY_LONG 10 /* msec */
+
+#define TPM_STS_ERR_VAL 0x07 // bit2...bit0 reads always 0
+
+/* I2C Addresses to scan */
+static const unsigned short normal_i2c[] = { TPM_I2C_ADDR, I2C_CLIENT_END };
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
+I2C_CLIENT_INSMOD_1(NuvotonTPM);
+#endif
+
+static struct tpm_chip *chip = NULL;
+static struct i2c_client *ti2c_i2c_client = NULL;
+
+static s32 i2cReadBuf(u8 offset, u8 size, u8 *data)
+{
+ s32 status;
+ int busyRetry = I2C_RETRY_COUNT;
+ int nackRetry = I2C_RETRY_COUNT;
+ int unknRetry = I2C_RETRY_COUNT;
+#ifdef TI2C_DBG
+ int i;
+ char str[I2C_MAX_BUF_SIZE*4];
+#endif
+
+ if (ti2c_i2c_client == NULL) {
+ dev_err(chip->dev, "i2cReadBuf() ti2c_i2c_client is NULL\n");
+ return -ENODEV;
+ }
+
+ do {
+ status = i2c_smbus_read_i2c_block_data(
+ ti2c_i2c_client,
+ offset,
+ size,
+ data);
+ if (status == -EBUSY) {
+ if (busyRetry-- < 0) {
+ dev_err(chip->dev, "i2cReadBuf() bus is BUSY\n");
+ break;
+ }
+ msleep(I2C_BUS_DELAY);
+ } else if (status == -ETIME) {
+ if (nackRetry-- < 0) {
+ dev_err(chip->dev, "i2cReadBuf() NACK from TPM\n");
+ break;
+ }
+ msleep(I2C_BUS_DELAY);
+ } else if (status < 0) {
+ if (unknRetry-- < 0) {
+ dev_err(
+ chip->dev,
+ "i2cReadBuf() unexpected error. status=%d\n",
+ status);
+ break;
+ }
+ msleep(I2C_BUS_DELAY);
+ }
+ } while (status < 0);
+#ifdef TI2C_DBG
+ if (status >= 0) {
+ for (i = 0; i < size; i++)
+ sprintf(str +i*3, "%02x ", data[i]);
+ dev_dbg(chip->dev,
+ "i2cReadBuf(%0x)-> sts=%d\n\t%s\n", offset, status, str);
+ }
+#endif
+ return status;
+}
+
+static s32 i2cWriteBuf(u8 offset, u8 size, u8 *data)
+{
+ s32 status;
+ int busyRetry = I2C_RETRY_COUNT;
+ int nackRetry = I2C_RETRY_COUNT;
+ int unknRetry = I2C_RETRY_COUNT;
+#ifdef TI2C_DBG
+ int i;
+ char str[I2C_MAX_BUF_SIZE*4];
+#endif
+
+ if (ti2c_i2c_client == NULL)
+ {
+ dev_err(chip->dev, "i2cWriteBuf() ti2c_i2c_client is NULL\n");
+ return -ENODEV;
+ }
+ do {
+ status = i2c_smbus_write_i2c_block_data(
+ ti2c_i2c_client,
+ offset,
+ size,
+ data);
+ if (status == -EBUSY) {
+ if (busyRetry-- < 0) {
+ dev_err(chip->dev, "i2cWriteBuf() bus is BUSY\n");
+ break;
+ }
+ msleep(I2C_BUS_DELAY);
+ } else if (status == -ETIME) {
+ if (nackRetry-- < 0) {
+ dev_err(chip->dev, "i2cWriteBuf() NACK from TPM\n");
+ break;
+ }
+ msleep(I2C_BUS_DELAY);
+ } else if (status < 0) {
+ if (unknRetry-- < 0) {
+ dev_err(
+ chip->dev,
+ "i2cReadBuf() unexpected error. status=%d\n",
+ status);
+ break;
+ }
+ msleep(I2C_BUS_DELAY);
+ }
+ } while (status < 0);
+#ifdef TI2C_DBG
+ if (status >= 0) {
+ for (i = 0; i < size; i++)
+ sprintf(str +i*3, "%02x ", data[i]);
+ dev_dbg(chip->dev,
+ "i2cWriteBuf(offset=%0x, size=%0x)-> sts=%d\n\t%s\n",
+ offset, size, status, str);
+ }
+#endif
+ return status;
+}
+
+static int ti2c_detect(
+ struct i2c_client *client,
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
+ int kind,
+#endif
+ struct i2c_board_info *info)
+{
+ struct i2c_adapter *adapter = client->adapter;
+
+ dev_dbg(chip->dev, "ti2c_detect() In detect function\n");
+ if (client->addr != TPM_I2C_ADDR)
+ {
+ dev_err(chip->dev, "ti2c_detect() Wrong I2C addr %x\n", client->addr);
+ return -ENODEV;
+ }
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
+ return -ENODEV;
+
+ strlcpy(info->type, "ti2c", I2C_NAME_SIZE);
+ dev_dbg(chip->dev, "ti2c_detect() End detect function\n");
+
+ return 0;
+}
+
+static const u8 ti2c_VidDidRidValue[] = {0x50, 0x10, 0xfe, 0x00};
+
+static int ti2c_i2c_prob(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ u32 vidDidRid;
+ s32 rc;
+
+ dev_dbg(chip->dev, "ti2c_i2c_prob() in ti2c_i2c_prob\n");
+
+ if (client->addr != TPM_I2C_ADDR)
+ {
+ dev_err(chip->dev, "ti2c_i2c_prob() client-> addr != TPM_I2C_ADDR\n");
+ return -1; /* Error */
+ }
+
+ ti2c_i2c_client = client; /* Save a pointer to the i2c client struct
+ * we need it during the interrupt */
+
+ rc = i2cReadBuf(TPM_VID_DID_RID, 4, (u8 *)(&vidDidRid));
+ if (rc < 0) {
+ dev_err(chip->dev,
+ "ti2c_i2c_prob() fail to read VID/DID/RID. status=%d\n",
+ rc);
+ return -1;
+ }
+ dev_dbg(chip->dev, "ti2c_i2c_prob() VID: %04X DID: %02X RID: %02X\n",
+ (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
+
+ /* check WPCT301/NPCT350/NPCT550 values - ignore RID */
+ if (memcmp((void*)&vidDidRid,
+ ti2c_VidDidRidValue,
+ sizeof(ti2c_VidDidRidValue)-1))
+ {
+ /*
+ * f/w rev 2.81 has an issue where the VID_DID_RID is not reporting
+ * the right value. so give it another chance at offset 0x20 (FIFO_W)
+ */
+ rc = i2cReadBuf(TPM_DATA_FIFO_W, 4, (u8 *)(&vidDidRid));
+ if (rc < 0) {
+ dev_err(chip->dev,
+ "ti2c_i2c_prob() fail to read VID/DID/RID. status=%d\n",
+ rc);
+ return -1;
+ }
+ dev_dbg(chip->dev, "ti2c_i2c_prob() VID: %04X DID: %02X RID: %02X\n",
+ (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
+
+ /* check WPCT301/NPCT350/NPCT550 values - ignore RID*/
+ if (memcmp((void*)&vidDidRid,
+ ti2c_VidDidRidValue,
+ sizeof(ti2c_VidDidRidValue)-1))
+ {
+ ti2c_i2c_client = NULL;
+ dev_err(chip->dev, "ti2c_i2c_prob() WPCT301/NPCT501 not found\n");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int ti2c_i2c_remove(struct i2c_client *client)
+{
+ dev_info(chip->dev, "in ti2c_i2c_remove\n");
+ kfree(i2c_get_clientdata(client));
+ return 0;
+}
+
+static const struct i2c_device_id ti2c_i2c_id[] = { { "ti2c", 0 }, {} };
+
+static struct i2c_driver ti2c_driver = {
+ .driver = {
+ .name= "ti2c",
+ },
+ .probe = ti2c_i2c_prob,
+ .remove = ti2c_i2c_remove,
+ .id_table = ti2c_i2c_id,
+ .class = I2C_CLASS_ALL,
+ .detect = ti2c_detect,
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,32)
+ .address_data = &addr_data,
+#else
+ .address_list = normal_i2c,
+#endif
+};
+
+enum ti2c_status {
+ TPM_STS_VALID = 0x80,
+ TPM_STS_COMMAND_READY = 0x40,
+ TPM_STS_GO = 0x20,
+ TPM_STS_DATA_AVAIL = 0x10,
+ TPM_STS_EXPECT = 0x08,
+ TPM_STS_RESPONSE_RETRY = 0x02,
+};
+
+enum ti2c_defaults {
+ TI2C_MEM_BASE = 0xAE,
+ TI2C_MEM_LEN = 0x0100,
+ TI2C_SHORT_TIMEOUT = 750, /* ms */
+ TI2C_LONG_TIMEOUT = 2000, /* 2 sec */
+};
+
+static u8 tpm_ti2c_status(struct tpm_chip *chip)
+{
+ s32 status;
+ u8 data;
+
+ status = i2cReadBuf(TPM_STS, 1, &data);
+ if (status < 0) {
+ dev_err(chip->dev,
+ "tpm_ti2c_status() error return %#02x \n",
+ status);
+ data = TPM_STS_ERR_VAL;
+ }
+
+ return data;
+}
+
+static void tpm_ti2c_ready(struct tpm_chip *chip)
+{
+ s32 status;
+ u8 data = TPM_STS_COMMAND_READY;
+
+ status = i2cWriteBuf(TPM_STS, 1, &data);
+ if (status < 0) {
+ dev_err(chip->dev,
+ "tpm_ti2c_ready() fail on i2cWriteBuf() status=%d\n",
+ status);
+ }
+}
+
+static int get_burstcount(struct tpm_chip *chip)
+{
+ s32 status;
+ unsigned long stop;
+ int burstcnt = -1;
+ u8 data;
+
+ /* wait for burstcount */
+ /* which timeout value, spec has 2 answers (c & d) */
+ stop = jiffies + chip->vendor.timeout_d;
+ do { /* in I2C burstCount is 1 byte */
+ status = i2cReadBuf(TPM_STS +1, 1, &data);
+ if (status < 0) {
+ dev_err(chip->dev, "get_burstcount() error. \n");
+ break;
+ } else if (data > 0) {
+ burstcnt = data > I2C_MAX_BUF_SIZE ? I2C_MAX_BUF_SIZE : data;
+ break;
+ }
+ msleep(I2C_BUS_DELAY);
+ } while (time_before(jiffies, stop));
+ dev_dbg(chip->dev, "get_burstcount() -> timeout\n");
+
+ return burstcnt;
+}
+
+/*
+ * WPCT301/NPCT501 SINT# supports only dataAvail
+ * any call to this function which is not waiting for dataAvail will
+ * set queue to NULL to avoid waiting for interrupt
+ */
+static int wait_for_stat(
+ struct tpm_chip *chip,
+ u8 mask,
+ u8 value,
+ unsigned long timeout,
+ wait_queue_head_t *queue)
+{
+ unsigned long tenMsec, stop;
+ long rc;
+ u8 status;
+
+ /* check current status */
+ status = tpm_ti2c_status(chip);
+ if ((status != TPM_STS_ERR_VAL) && ((status & mask) == value)) {
+ return 0;
+ }
+
+ if ((chip->vendor.irq) && (queue != NULL)) {
+ /* use interrupt to wait for the event */
+ rc = wait_event_interruptible_timeout(
+ *queue,
+ ((status = tpm_ti2c_status(chip)) != TPM_STS_ERR_VAL) &&
+ ((status & mask) == value),
+ timeout);
+ if (rc > 0) {
+ return 0;
+ }
+ } else {
+ /* use polling to wait for the event */
+ tenMsec = jiffies + msecs_to_jiffies(I2C_RETRY_DELAY_LONG);
+ stop = jiffies + timeout;
+ do {
+ if (time_before(jiffies, tenMsec)) {
+ msleep(I2C_RETRY_DELAY_SHORT);
+ } else {
+ msleep(I2C_RETRY_DELAY_LONG);
+ }
+ status = tpm_ti2c_status(chip);
+ if ((status != TPM_STS_ERR_VAL) && ((status & mask) == value)) {
+ return 0;
+ }
+ } while (time_before(jiffies, stop));
+ }
+ dev_err(chip->dev, "wait_for_stat(%02x, %02x) -> timeout\n", mask, value);
+ return -ETIME;
+}
+
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+ s32 rc;
+ int size = 0, burstcnt, bytes2read;
+#ifdef DEBUG
+ int i;
+ char str[I2C_MAX_BUF_SIZE*4];
+#endif
+ while ((size < count) &&
+ (wait_for_stat(
+ chip,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ chip->vendor.timeout_c,
+ NULL) == 0))
+ {
+ burstcnt = get_burstcount(chip);
+ if (burstcnt < 0) {
+ dev_err(chip->dev,
+ "recv_data() fail to read burstCount=%d\n",
+ burstcnt);
+ return -EIO;
+ }
+ /*
+ * for (; burstcnt > 0 && size < count; burstcnt--)
+ * buf[size++] = i2cRead8(TPM_DATA_FIFO_R);
+ */
+ bytes2read = burstcnt < (count - size) ? burstcnt : (count - size);
+ rc = i2cReadBuf(TPM_DATA_FIFO_R, bytes2read, &buf[size]);
+ if (rc < 0) {
+ dev_err(chip->dev, "recv_data() fail on i2cReadBuf=%d\n", rc);
+ return -EIO;
+ }
+#ifdef DEBUG
+ for (i = 0; i < bytes2read; i++)
+ sprintf(str +i*3, "%02x ", buf[size+i]);
+ dev_dbg(chip->dev, "recv_data(%d):\n\t%s\n", bytes2read, str);
+#endif
+ size += bytes2read;
+ }
+ return size;
+}
+
+static int tpm_ti2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+ s32 rc;
+ int size = 0;
+ int expected, status, burstcnt;
+ int retries = TPM_RETRY;
+ u8 data = TPM_STS_RESPONSE_RETRY;
+
+ if (count < TPM_HEADER_SIZE) {
+ tpm_ti2c_ready(chip); /* return to idle */
+ dev_err(chip->dev, "tpm_ti2c_recv() count < header size\n");
+ return -EIO;
+ }
+
+ do {
+ if (retries < TPM_RETRY) {
+ /* if this is not the first trial, set responseRetry */
+ i2cWriteBuf(TPM_STS, 1, &data);
+ }
+ /* read first available (> 10 bytes), including:
+ * tag, paramsize, and result */
+ status = wait_for_stat(
+ chip,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ chip->vendor.timeout_c,
+ &chip->vendor.read_queue);
+ if (status != 0) {
+ dev_err(chip->dev, "tpm_ti2c_recv() wait for dataAvail\n");
+ size = -ETIME;
+ retries--;
+ continue;
+ }
+ burstcnt = get_burstcount(chip);
+ if (burstcnt < 0) {
+ dev_err(chip->dev, "tpm_ti2c_recv() Unable to get burstCount\n");
+ size = -EIO;
+ retries --;
+ continue;
+ }
+ if ((size = recv_data(chip, buf, burstcnt)) < TPM_HEADER_SIZE) {
+ dev_err(chip->dev, "tpm_ti2c_recv() Unable to read header\n");
+ size = -EIO;
+ retries--;
+ continue;
+ }
+ expected = be32_to_cpu(*(__be32 *) (buf + 2));
+ if (expected > count) {
+ dev_err(chip->dev, "tpm_ti2c_recv() expected > count\n");
+ size = -EIO;
+ retries--;
+ continue;
+ }
+ rc = recv_data(chip, &buf[size], expected - size);
+ if ((rc < 0) || ((size += rc) < expected)) {
+ dev_err(chip->dev,
+ "tpm_ti2c_recv() Unable to read remainder of result\n");
+ size = -EIO;
+ retries--;
+ continue;
+ }
+ if (wait_for_stat(
+ chip,
+ TPM_STS_VALID | TPM_STS_DATA_AVAIL,
+ TPM_STS_VALID,
+ chip->vendor.timeout_c,
+ NULL)) { /* retry? */
+ dev_err(chip->dev, "tpm_ti2c_recv() Error left over data\n");
+ size = -ETIME;
+ retries--;
+ continue;
+ }
+ break;
+ }while (retries > 0);
+ tpm_ti2c_ready(chip);
+ dev_dbg(chip->dev, "tpm_ti2c_recv() -> %d\n", size);
+ return size;
+}
+
+/*
+ * If interrupts are used (signaled by an irq set in the vendor structure)
+ * tpm.c can skip polling for the data to be available as the interrupt is
+ * waited for here
+ */
+static int tpm_ti2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
+{
+ int rc, burstcnt, bytes2write, retries = TPM_RETRY;
+ size_t count = 0;
+ u32 ordinal;
+ u8 data;
+#ifdef DEBUG
+ int i;
+ char str[I2C_MAX_BUF_SIZE*4];
+#endif
+
+ do {
+ tpm_ti2c_ready(chip);
+ if (wait_for_stat(
+ chip,
+ TPM_STS_COMMAND_READY,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b,
+ NULL)) {
+ dev_err(chip->dev, "tpm_ti2c_send() timeout on commandReady\n");
+ return -ETIME;
+ }
+ rc = 0;
+ while (count < len - 1)
+ {
+ burstcnt = get_burstcount(chip);
+ if (burstcnt < 0) {
+ dev_err(chip->dev, "tpm_ti2c_send() fail to get burstCount\n");
+ rc = -EIO;
+ break;
+ }
+ /*
+ * for (; burstcnt > 0 && count < len - 1; burstcnt--) {
+ * i2cWrite8(buf[count], TPM_DATA_FIFO_W);
+ * count++;
+ * }
+ */
+ bytes2write =
+ (len -1 -count) < burstcnt ? (len -1 -count) : burstcnt;
+ rc = i2cWriteBuf(TPM_DATA_FIFO_W, bytes2write, &buf[count]);
+ if (rc < 0) {
+ dev_err(chip->dev, "tpm_ti2c_send() fail on i2cWriteBug\n");
+ break;
+ }
+ #ifdef DEBUG
+ for (i = 0; i < bytes2write; i++)
+ sprintf(str +i*3, "%02x ", buf[count+i]);
+ dev_dbg(chip->dev, "tpm_ti2c_send(%d):\n\t%s\n", bytes2write, str);
+ #endif
+ count += bytes2write;
+ if (wait_for_stat(
+ chip,
+ TPM_STS_VALID | TPM_STS_EXPECT,
+ TPM_STS_VALID | TPM_STS_EXPECT,
+ chip->vendor.timeout_c,
+ NULL) < 0) {
+ dev_err(chip->dev, "tpm_ti2c_send() timeout on Expect\n");
+ rc = -ETIME;
+ break;
+ }
+ }
+ if (rc < 0) {
+ retries--;
+ continue;
+ }
+
+ /* write last byte */
+ rc = i2cWriteBuf(TPM_DATA_FIFO_W, 1, &buf[count]);
+ if (rc < 0) {
+ dev_err(chip->dev, "tpm_ti2c_send() fail to write last byte\n");
+ rc = -EIO;
+ retries--;
+ continue;
+ }
+ dev_dbg(chip->dev, "tpm_ti2c_send(last):\n\t%02x\n", buf[count]);
+ if (wait_for_stat(
+ chip,
+ TPM_STS_VALID | TPM_STS_EXPECT,
+ TPM_STS_VALID,
+ chip->vendor.timeout_c,
+ NULL)) {
+ dev_err(chip->dev, "tpm_ti2c_send() timeout on Expect to clear\n");
+ rc = -ETIME;
+ retries--;
+ continue;
+ }
+ break;
+ }while (retries > 0);
+ if (rc < 0) { /* retries == 0 */
+ tpm_ti2c_ready(chip);
+ return rc;
+ }
+ /* go and do it */
+ data = TPM_STS_GO;
+ rc = i2cWriteBuf(TPM_STS, 1, &data);
+ if (rc < 0) {
+ dev_err(chip->dev, "tpm_ti2c_send() fail to write Go\n");
+ tpm_ti2c_ready(chip);
+ return rc;
+ }
+ ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+ if (wait_for_stat(
+ chip,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ tpm_calc_ordinal_duration(chip, ordinal),
+ &chip->vendor.read_queue)) {
+ dev_err(chip->dev, "tpm_ti2c_send() timeout command duration\n");
+ tpm_ti2c_ready(chip);
+ return rc;
+ }
+
+ dev_dbg(chip->dev, "tpm_ti2c_send() -> %d\n", len);
+ return len;
+}
+
+static struct file_operations ti2c_ops = {
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .open = tpm_open,
+ .read = tpm_read,
+ .write = tpm_write,
+ .release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
+ NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *ti2c_attrs[] = {
+ &dev_attr_pubek.attr,
+ &dev_attr_pcrs.attr,
+ &dev_attr_enabled.attr,
+ &dev_attr_active.attr,
+ &dev_attr_owned.attr,
+ &dev_attr_temp_deactivated.attr,
+ &dev_attr_caps.attr,
+ &dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group ti2c_attr_grp = {
+ .attrs = ti2c_attrs
+};
+
+static struct tpm_vendor_specific tpm_ti2c = {
+ .status = tpm_ti2c_status,
+ .recv = tpm_ti2c_recv,
+ .send = tpm_ti2c_send,
+ .cancel = tpm_ti2c_ready,
+ .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_canceled = TPM_STS_COMMAND_READY,
+ .attr_group = &ti2c_attr_grp,
+ .miscdev = {
+ .fops = &ti2c_ops,},
+};
+
+static irqreturn_t ti2c_int_handler(int dummy, void *dev_id)
+{
+ struct tpm_chip *chip = dev_id;
+
+ /* in I2C interrupt is triggeted only on dataAvail */
+ wake_up_interruptible(&chip->vendor.read_queue);
+
+ /*
+ * consider disabling the interrupt in the host
+ * in case of interrupt storm as TPM SINT# is level interrupt
+ * which is cleared only when dataAvail is cleared
+ */
+
+ return IRQ_HANDLED;
+}
+
+static int tpm_ti2c_init(
+ struct device *dev,
+ unsigned long start,
+ unsigned long len)
+{
+ int rc;
+
+ dev_dbg(dev, "tpm_ti2c_init()\n");
+ if (!start)
+ start = TI2C_MEM_BASE;
+ if (!len)
+ len = TI2C_MEM_LEN;
+
+ chip = tpm_register_hardware(dev, &tpm_ti2c);
+ if (chip == NULL)
+ {
+ dev_err(dev, "tpm_ti2c_init() error in tpm_register_hardware\n");
+ return -ENODEV;
+ }
+
+ rc = i2c_add_driver(&ti2c_driver);
+ if (rc != 0)
+ {
+ dev_err(dev, "tpm_ti2c_init() error registering i2c\n");
+ goto out_err;
+ }
+
+ if (ti2c_i2c_client == NULL)
+ {
+ dev_err(dev, "tpm_ti2c_init() error allocating i2c\n");
+ rc = -ENODEV;
+ goto out_err1;
+ }
+
+ /* Default timeouts */
+ chip->vendor.timeout_a = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_b = msecs_to_jiffies(TI2C_LONG_TIMEOUT);
+ chip->vendor.timeout_c = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_d = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
+
+ /*
+ * Figure out the capabilities
+ * I2C hard coded:
+ * intfcaps = TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT;
+ */
+ dev_dbg(dev, "tpm_ti2c_init() TPM interface capabilities:\n");
+ dev_dbg(dev, "\tInterrupt Level Low\n");
+ dev_dbg(dev, "\tData Avail Int Support\n");
+
+ /* INTERRUPT Setup */
+ init_waitqueue_head(&chip->vendor.read_queue);
+ init_waitqueue_head(&chip->vendor.int_queue);
+
+ /*
+ * I2C hard coded:
+ * intmask = TPM_INTF_DATA_AVAIL_INT;
+ */
+ if (chip->vendor.irq) {
+ dev_dbg(dev, "tpm_ti2c_init() chip-vendor.irq\n");
+ if (request_irq(
+ chip->vendor.irq,
+ ti2c_int_handler,
+ IRQF_PROBE_SHARED,
+ chip->vendor.miscdev.name,
+ chip) != 0)
+ {
+ dev_err(
+ dev,
+ "tpm_ti2c_init() Unable to request irq: %d for use\n",
+ chip->vendor.irq);
+ chip->vendor.irq = 0;
+ } else {
+ /*
+ * Clear all existing
+ * consider adding code that will make sure TIS is in idle state
+ * and that dataAvail is off in the TPM_STS:
+ * - TPM_STS <- 0x40 (commandReady)
+ */
+ tpm_ti2c_ready(chip);
+ /* - wait for TPM_STS == 0xA0 (stsValid, commandReady) */
+ rc = wait_for_stat(
+ chip,
+ TPM_STS_COMMAND_READY,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b,
+ NULL);
+ if (rc == 0)
+ {
+ /*
+ * TIS is in ready state
+ * write dummy byte to enter reception state
+ * TPM_DATA_FIFO_W <- rc (0)
+ */
+ rc = i2cWriteBuf(TPM_DATA_FIFO_W, 1, (u8*)(&rc));
+ if (rc < 0) {
+ goto out_err1;
+ }
+ /* TPM_STS <- 0x40 (commandReady) */
+ tpm_ti2c_ready(chip);
+ } else {
+ /*
+ * if timeout_b is reached then command was aborted,
+ * TIS in idle state
+ */
+ if (tpm_ti2c_status(chip) != TPM_STS_VALID) {
+ rc = -EIO;
+ goto out_err1;
+ }
+ }
+ }
+ }
+
+ /* uncomment in case BIOS does not send TPM_StartUp(ST_CLEAR) command */
+ /* tpm_startup_clear(chip); */
+ tpm_get_timeouts(chip);
+ tpm_continue_selftest(chip);
+
+ return 0;
+
+out_err1:
+ i2c_del_driver(&ti2c_driver);
+out_err:
+ tpm_remove_hardware(chip->dev);
+ return rc;
+}
+
+static int tpm_ti2c_suspend(struct platform_device *dev, pm_message_t msg)
+{
+ return tpm_pm_suspend(&dev->dev, msg);
+}
+
+static int tpm_ti2c_resume(struct platform_device *dev)
+{
+ return tpm_pm_resume(&dev->dev);
+}
+
+static struct platform_driver ti2c_drv = {
+ .driver = {
+ .name = "tpm_ti2c",
+ .owner = THIS_MODULE,
+ },
+ .suspend = tpm_ti2c_suspend,
+ .resume = tpm_ti2c_resume,
+};
+
+static struct platform_device *pdev;
+
+static int __init init_ti2c(void)
+{
+ int rc;
+
+ rc = platform_driver_register(&ti2c_drv);
+ if (rc < 0)
+ return rc;
+
+ pdev = platform_device_register_simple("tpm_ti2c", -1, NULL, 0);
+ if (IS_ERR(pdev))
+ return PTR_ERR(pdev);
+
+ rc = tpm_ti2c_init(&pdev->dev, 0, 0);
+ if(rc != 0) {
+ platform_device_unregister(pdev);
+ platform_driver_unregister(&ti2c_drv);
+ }
+
+ return rc;
+}
+
+static void __exit cleanup_ti2c(void)
+{
+ dev_info(chip->dev,"in cleanup_ti2c\n");
+
+ i2c_del_driver(&ti2c_driver);
+ tpm_remove_hardware(chip->dev);
+ if (chip->vendor.irq)
+ free_irq(chip->vendor.irq, chip);
+ platform_device_unregister(pdev);
+ platform_driver_unregister(&ti2c_drv);
+}
+
+module_init(init_ti2c);
+module_exit(cleanup_ti2c);
+MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
+MODULE_DESCRIPTION("Nuvton TPM I2C Driver");
+MODULE_VERSION("0.9.5.0");
+MODULE_LICENSE("GPL");
===========================================================================================
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
ÿôèº{.nÇ+·®+%Ëÿ±éݶ\x17¥wÿº{.nÇ+·¥{±þG«éÿ{ayº\x1dÊÚë,j\a¢f£¢·hïêÿêçz_è®\x03(éÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?¨èÚ&£ø§~á¶iOæ¬z·vØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?I¥
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] TPM Nuvoton I2C driver, kernel 3.0
2011-07-25 7:25 ` [PATCH] TPM Nuvoton I2C driver, kernel 3.0 Dan.Morav
@ 2011-07-26 0:50 ` Ryan Mallon
2011-07-26 14:14 ` Wolfram Sang
` (2 subsequent siblings)
3 siblings, 0 replies; 14+ messages in thread
From: Ryan Mallon @ 2011-07-26 0:50 UTC (permalink / raw)
To: Dan.Morav
Cc: linux-kernel, debora, srajiv, m.selhorst, Leonid.Azriel, Guy.Pavlov
On 25/07/11 17:25, Dan.Morav@nuvoton.com wrote:
> Hi,
>
> Below are the additions for WPCT301/NPCT501 Nuvoton Technology TPM with I2C interface device driver.
> This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
> This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
> This driver tested on Linux kernel version 3.0-rc6 on an x86 based platform and Linux kernel version 2.6.36.3 on an ARM platform.
>
Hi Dan,
There are lots of style problems with this patch. You should run it
through scripts/checkpatch.pl which should point most of them out. Some
more comments inline below (mostly issues pointed out occur several times).
~Ryan
> diff -uN linux-3.0-rc6/drivers/char/tpm/Kconfig linux/drivers/char/tpm/Kconfig
> --- linux-3.0-rc6/drivers/char/tpm/Kconfig 2011-07-25 10:07:31.519844000 +0300
> +++ linux/drivers/char/tpm/Kconfig 2011-07-25 10:07:46.999567000 +0300
> @@ -60,4 +60,13 @@
> Further information on this driver and the supported hardware
> can be found at http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
>
> +config TCG_NUVOTON_I2C
> + tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
> + depends on I2C
> + ---help---
> + If you have a TPM security chip with I2C interface from
> + Nuvoton Technology Corp. say Yes and it will be accessible
> + from within Linux. To compile this driver as a module, choose
> + M here; the module will be called tpm_nuvoton_i2c.
> +
> endif # TCG_TPM
> diff -uN linux-3.0-rc6/drivers/char/tpm/Makefile linux/drivers/char/tpm/Makefile
> --- linux-3.0-rc6/drivers/char/tpm/Makefile 2011-07-25 10:07:31.533850000 +0300
> +++ linux/drivers/char/tpm/Makefile 2011-07-25 10:07:47.018567000 +0300
> @@ -9,3 +9,4 @@
> obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
> obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
> obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
> +obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
> diff -uN linux-3.0-rc6/drivers/char/tpm/tpm_nuvoton_i2c.c linux/drivers/char/tpm/tpm_nuvoton_i2c.c
> --- linux-3.0-rc6/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
> +++ linux/drivers/char/tpm/tpm_nuvoton_i2c.c 2011-07-25 10:07:47.198570000 +0300
> @@ -0,0 +1,914 @@
> +/******************************************************************************
> + * Nuvoton TPM I2C Device Driver Interface for WPCT301,
> + * based on the TCG TPM Interface Spec version 1.2.
> + * Specifications at www.trustedcomputinggroup.org
> + *
> + * Copyright (C) 2011, Nuvoton Technology Corporation.
> + * dan.morav@nuvoton.com
> + *
> + * 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, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program. If not, see http://www.gnu.org/licenses/>.
> + *
> + * Nuvoton contact information: APC.Support@nuvoton.com
> + *****************************************************************************/
> +
> +/*
> + * #define DEBUG 1
> + * #define TI2C_DBG 1
> + */
Remove this comment. Also, you just need to define them, they don't need
a value.
> +
> +#include<linux/init.h>
> +#include<linux/module.h>
> +#include<linux/moduleparam.h>
> +#include<linux/slab.h>
> +#include<linux/interrupt.h>
> +#include<linux/wait.h>
> +#include<linux/i2c.h>
> +#include<linux/version.h>
> +#include "tpm.h"
> +
> +/* I2C interface defintions */
> +#define TPM_I2C_ADDR 0x57
> +/* I2C interface offsets */
> +#define TPM_STS 0x00
> +#define TPM_DATA_FIFO_W 0x20
> +#define TPM_DATA_FIFO_R 0x40
> +#define TPM_VID_DID_RID 0x60
> +/* I2C class required */
> +#define I2C_CLASS_ALL (I2C_CLASS_HWMON | I2C_CLASS_SPD)
> +/* TPM command header size */
> +#define TPM_HEADER_SIZE 10
> +#define TPM_RETRY 5
> +/*
> + * I2C bus device maximum buffer size
> + * w/o counting I2C address or command
> + * i.e. max size required for I2C write is 34
> + * = I2C addr, command, 32 bytes data
> + */
> +#define I2C_MAX_BUF_SIZE 32
> +#define I2C_RETRY_COUNT 32
> +#define I2C_BUS_DELAY 1 /* msec */
> +#define I2C_RETRY_DELAY_SHORT 2 /* msec */
> +#define I2C_RETRY_DELAY_LONG 10 /* msec */
> +
> +#define TPM_STS_ERR_VAL 0x07 // bit2...bit0 reads always 0
/* */ Style comments please.
> +/* I2C Addresses to scan */
> +static const unsigned short normal_i2c[] = { TPM_I2C_ADDR, I2C_CLIENT_END };
> +#if LINUX_VERSION_CODE<= KERNEL_VERSION(2,6,32)
> +I2C_CLIENT_INSMOD_1(NuvotonTPM);
> +#endif
Don't use LINUX_VERSION_CODE tests in mainline code. This is no longer
the correct way to register I2C devices. Please see how other mainline
i2c drivers work.
> +
> +static struct tpm_chip *chip = NULL;
> +static struct i2c_client *ti2c_i2c_client = NULL;
> +
> +static s32 i2cReadBuf(u8 offset, u8 size, u8 *data)
Don't use camel case names. These functions should also be passed a
struct i2c_client * pointer rather than using a global.
> +{
> + s32 status;
Tabs, not spaces for indentation.
> + int busyRetry = I2C_RETRY_COUNT;
> + int nackRetry = I2C_RETRY_COUNT;
> + int unknRetry = I2C_RETRY_COUNT;
> +#ifdef TI2C_DBG
> + int i;
> + char str[I2C_MAX_BUF_SIZE*4];
> +#endif
> +
> + if (ti2c_i2c_client == NULL) {
> + dev_err(chip->dev, "i2cReadBuf() ti2c_i2c_client is NULL\n");
> + return -ENODEV;
> + }
Don't think you need this test. If the probe fails then the device won't
get registered.
> +
> + do {
> + status = i2c_smbus_read_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + size,
> + data);
> + if (status == -EBUSY) {
> + if (busyRetry--< 0) {
> + dev_err(chip->dev, "i2cReadBuf() bus is BUSY\n");
> + break;
> + }
> + msleep(I2C_BUS_DELAY);
> + } else if (status == -ETIME) {
> + if (nackRetry--< 0) {
> + dev_err(chip->dev, "i2cReadBuf() NACK from TPM\n");
> + break;
> + }
> + msleep(I2C_BUS_DELAY);
> + } else if (status< 0) {
> + if (unknRetry--< 0) {
> + dev_err(
> + chip->dev,
> + "i2cReadBuf() unexpected error. status=%d\n",
> + status);
> + break;
> + }
> + msleep(I2C_BUS_DELAY);
> + }
> + } while (status< 0);
I don't think you need all this retry stuff. The i2c core will handle
this for you. Just call
i2c_smbus_read_i2c_block_data and return the error code if it fails.
> +#ifdef TI2C_DBG
> + if (status>= 0) {
> + for (i = 0; i< size; i++)
> + sprintf(str +i*3, "%02x ", data[i]);
> + dev_dbg(chip->dev,
> + "i2cReadBuf(%0x)-> sts=%d\n\t%s\n", offset, status, str);
> + }
> +#endif
Is this really needed? You should also look at the print_hex_dump
functions, which can probably wrap this into a single function call with
KERN_DEBUG level.
> + return status;
> +}
> +
> +static s32 i2cWriteBuf(u8 offset, u8 size, u8 *data)
> +{
> + s32 status;
> + int busyRetry = I2C_RETRY_COUNT;
> + int nackRetry = I2C_RETRY_COUNT;
> + int unknRetry = I2C_RETRY_COUNT;
> +#ifdef TI2C_DBG
> + int i;
> + char str[I2C_MAX_BUF_SIZE*4];
> +#endif
> +
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(chip->dev, "i2cWriteBuf() ti2c_i2c_client is NULL\n");
> + return -ENODEV;
> + }
> + do {
> + status = i2c_smbus_write_i2c_block_data(
> + ti2c_i2c_client,
> + offset,
> + size,
> + data);
> + if (status == -EBUSY) {
> + if (busyRetry--< 0) {
> + dev_err(chip->dev, "i2cWriteBuf() bus is BUSY\n");
> + break;
> + }
> + msleep(I2C_BUS_DELAY);
> + } else if (status == -ETIME) {
> + if (nackRetry--< 0) {
> + dev_err(chip->dev, "i2cWriteBuf() NACK from TPM\n");
> + break;
> + }
> + msleep(I2C_BUS_DELAY);
> + } else if (status< 0) {
> + if (unknRetry--< 0) {
> + dev_err(
> + chip->dev,
> + "i2cReadBuf() unexpected error. status=%d\n",
> + status);
> + break;
> + }
> + msleep(I2C_BUS_DELAY);
> + }
> + } while (status< 0);
> +#ifdef TI2C_DBG
> + if (status>= 0) {
> + for (i = 0; i< size; i++)
> + sprintf(str +i*3, "%02x ", data[i]);
> + dev_dbg(chip->dev,
> + "i2cWriteBuf(offset=%0x, size=%0x)-> sts=%d\n\t%s\n",
> + offset, size, status, str);
> + }
> +#endif
> + return status;
> +}
> +
> +static int ti2c_detect(
> + struct i2c_client *client,
> +#if LINUX_VERSION_CODE<= KERNEL_VERSION(2,6,32)
> + int kind,
> +#endif
> + struct i2c_board_info *info)
> +{
> + struct i2c_adapter *adapter = client->adapter;
> +
> + dev_dbg(chip->dev, "ti2c_detect() In detect function\n");
> + if (client->addr != TPM_I2C_ADDR)
> + {
> + dev_err(chip->dev, "ti2c_detect() Wrong I2C addr %x\n", client->addr);
> + return -ENODEV;
> + }
> +
> + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
> + return -ENODEV;
> +
> + strlcpy(info->type, "ti2c", I2C_NAME_SIZE);
> + dev_dbg(chip->dev, "ti2c_detect() End detect function\n");
> +
> + return 0;
> +}
> +
> +static const u8 ti2c_VidDidRidValue[] = {0x50, 0x10, 0xfe, 0x00};
> +
> +static int ti2c_i2c_prob(struct i2c_client *client,
> + const struct i2c_device_id *id)
ti2c_i2c_probe.
> +{
> + u32 vidDidRid;
> + s32 rc;
> +
> + dev_dbg(chip->dev, "ti2c_i2c_prob() in ti2c_i2c_prob\n");
> +
> + if (client->addr != TPM_I2C_ADDR)
> + {
Braces go on the same lines as the if.
> + dev_err(chip->dev, "ti2c_i2c_prob() client-> addr != TPM_I2C_ADDR\n");
> + return -1; /* Error */
Use proper errno codes. -1 is EPERM.
> + }
> +
> + ti2c_i2c_client = client; /* Save a pointer to the i2c client struct
> + * we need it during the interrupt */
Typically i2c drivers allocate some custom chip structure and then do:
i2c_set_clientdata(client, chip);
Which means that you don't need a global pointer for the device, which
in turn means you can have more than one of them.
> +
> + rc = i2cReadBuf(TPM_VID_DID_RID, 4, (u8 *)(&vidDidRid));
> + if (rc< 0) {
> + dev_err(chip->dev,
> + "ti2c_i2c_prob() fail to read VID/DID/RID. status=%d\n",
> + rc);
> + return -1;
Proper errno code.
> + }
> + dev_dbg(chip->dev, "ti2c_i2c_prob() VID: %04X DID: %02X RID: %02X\n",
> + (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
Don't think all the casting is necessary.
> +
> + /* check WPCT301/NPCT350/NPCT550 values - ignore RID */
> + if (memcmp((void*)&vidDidRid,
> + ti2c_VidDidRidValue,
> + sizeof(ti2c_VidDidRidValue)-1))
Drop the void * cast.
> + {
> + /*
> + * f/w rev 2.81 has an issue where the VID_DID_RID is not reporting
> + * the right value. so give it another chance at offset 0x20 (FIFO_W)
> + */
> + rc = i2cReadBuf(TPM_DATA_FIFO_W, 4, (u8 *)(&vidDidRid));
> + if (rc< 0) {
> + dev_err(chip->dev,
> + "ti2c_i2c_prob() fail to read VID/DID/RID. status=%d\n",
> + rc);
> + return -1;
> + }
> + dev_dbg(chip->dev, "ti2c_i2c_prob() VID: %04X DID: %02X RID: %02X\n",
> + (u16)vidDidRid, (u8)(vidDidRid>>16), (u8)(vidDidRid>>24));
> +
> + /* check WPCT301/NPCT350/NPCT550 values - ignore RID*/
> + if (memcmp((void*)&vidDidRid,
> + ti2c_VidDidRidValue,
> + sizeof(ti2c_VidDidRidValue)-1))
> + {
> + ti2c_i2c_client = NULL;
> + dev_err(chip->dev, "ti2c_i2c_prob() WPCT301/NPCT501 not found\n");
> + return -1;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static int ti2c_i2c_remove(struct i2c_client *client)
> +{
> + dev_info(chip->dev, "in ti2c_i2c_remove\n");
> + kfree(i2c_get_clientdata(client));
> + return 0;
> +}
> +
> +static const struct i2c_device_id ti2c_i2c_id[] = { { "ti2c", 0 }, {} };
> +
> +static struct i2c_driver ti2c_driver = {
> + .driver = {
> + .name= "ti2c",
> + },
> + .probe = ti2c_i2c_prob,
> + .remove = ti2c_i2c_remove,
> + .id_table = ti2c_i2c_id,
> + .class = I2C_CLASS_ALL,
> + .detect = ti2c_detect,
> +#if LINUX_VERSION_CODE<= KERNEL_VERSION(2,6,32)
> + .address_data =&addr_data,
> +#else
> + .address_list = normal_i2c,
> +#endif
> +};
> +
> +enum ti2c_status {
> + TPM_STS_VALID = 0x80,
> + TPM_STS_COMMAND_READY = 0x40,
> + TPM_STS_GO = 0x20,
> + TPM_STS_DATA_AVAIL = 0x10,
> + TPM_STS_EXPECT = 0x08,
> + TPM_STS_RESPONSE_RETRY = 0x02,
> +};
> +
> +enum ti2c_defaults {
> + TI2C_MEM_BASE = 0xAE,
> + TI2C_MEM_LEN = 0x0100,
> + TI2C_SHORT_TIMEOUT = 750, /* ms */
> + TI2C_LONG_TIMEOUT = 2000, /* 2 sec */
> +};
This should not be an enum.
> +
> +static u8 tpm_ti2c_status(struct tpm_chip *chip)
> +{
> + s32 status;
> + u8 data;
> +
> + status = i2cReadBuf(TPM_STS, 1,&data);
> + if (status< 0) {
> + dev_err(chip->dev,
> + "tpm_ti2c_status() error return %#02x \n",
> + status);
> + data = TPM_STS_ERR_VAL;
> + }
> +
> + return data;
> +}
> +
> +static void tpm_ti2c_ready(struct tpm_chip *chip)
> +{
This should return an error so the caller can determine if the chip is
ready.
> + s32 status;
> + u8 data = TPM_STS_COMMAND_READY;
> +
> + status = i2cWriteBuf(TPM_STS, 1,&data);
> + if (status< 0) {
> + dev_err(chip->dev,
> + "tpm_ti2c_ready() fail on i2cWriteBuf() status=%d\n",
> + status);
> + }
> +}
> +
> +static int get_burstcount(struct tpm_chip *chip)
> +{
> + s32 status;
> + unsigned long stop;
> + int burstcnt = -1;
> + u8 data;
> +
> + /* wait for burstcount */
> + /* which timeout value, spec has 2 answers (c& d) */
This comment is no particularly helpful. What are c & d?
> + stop = jiffies + chip->vendor.timeout_d;
> + do { /* in I2C burstCount is 1 byte */
> + status = i2cReadBuf(TPM_STS +1, 1,&data);
Why TPM_STS + 1?
> + if (status< 0) {
> + dev_err(chip->dev, "get_burstcount() error. \n");
> + break;
> + } else if (data> 0) {
> + burstcnt = data> I2C_MAX_BUF_SIZE ? I2C_MAX_BUF_SIZE : data;
burstcnt = max(I2C_MAX_BUF_SIZE, data);
> + break;
> + }
> + msleep(I2C_BUS_DELAY);
> + } while (time_before(jiffies, stop));
> + dev_dbg(chip->dev, "get_burstcount() -> timeout\n");
This will always print?
> +
> + return burstcnt;
> +}
> +
> +/*
> + * WPCT301/NPCT501 SINT# supports only dataAvail
> + * any call to this function which is not waiting for dataAvail will
> + * set queue to NULL to avoid waiting for interrupt
> + */
> +static int wait_for_stat(
> + struct tpm_chip *chip,
> + u8 mask,
> + u8 value,
> + unsigned long timeout,
> + wait_queue_head_t *queue)
> +{
> + unsigned long tenMsec, stop;
> + long rc;
> + u8 status;
> +
> + /* check current status */
> + status = tpm_ti2c_status(chip);
> + if ((status != TPM_STS_ERR_VAL)&& ((status& mask) == value)) {
You don't need the inner parens around each clause.
> + return 0;
> + }
> +
> + if ((chip->vendor.irq)&& (queue != NULL)) {
!queue is preferable to queue != NULL. Also drop the inner parens.
> + /* use interrupt to wait for the event */
> + rc = wait_event_interruptible_timeout(
> + *queue,
> + ((status = tpm_ti2c_status(chip)) != TPM_STS_ERR_VAL)&&
Don't assign things inside the arguments to a function. It's really hard
to see, especially when its multiline line like this.
> + ((status& mask) == value),
> + timeout);
> + if (rc> 0) {
> + return 0;
> + }
> + } else {
> + /* use polling to wait for the event */
> + tenMsec = jiffies + msecs_to_jiffies(I2C_RETRY_DELAY_LONG);
> + stop = jiffies + timeout;
> + do {
> + if (time_before(jiffies, tenMsec)) {
> + msleep(I2C_RETRY_DELAY_SHORT);
> + } else {
> + msleep(I2C_RETRY_DELAY_LONG);
> + }
> + status = tpm_ti2c_status(chip);
> + if ((status != TPM_STS_ERR_VAL)&& ((status& mask) == value)) {
> + return 0;
> + }
> + } while (time_before(jiffies, stop));
> + }
> + dev_err(chip->dev, "wait_for_stat(%02x, %02x) -> timeout\n", mask, value);
> + return -ETIME;
> +}
> +
> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> + s32 rc;
> + int size = 0, burstcnt, bytes2read;
> +#ifdef DEBUG
> + int i;
> + char str[I2C_MAX_BUF_SIZE*4];
> +#endif
> + while ((size< count)&&
> + (wait_for_stat(
> + chip,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + chip->vendor.timeout_c,
> + NULL) == 0))
> + {
> + burstcnt = get_burstcount(chip);
> + if (burstcnt< 0) {
> + dev_err(chip->dev,
> + "recv_data() fail to read burstCount=%d\n",
> + burstcnt);
> + return -EIO;
> + }
> + /*
> + * for (; burstcnt> 0&& size< count; burstcnt--)
> + * buf[size++] = i2cRead8(TPM_DATA_FIFO_R);
> + */
Why is this commented out? Fix it or remove it.
> + bytes2read = burstcnt< (count - size) ? burstcnt : (count - size);
bytes2read = min(burst_count, count - size); ?
> + rc = i2cReadBuf(TPM_DATA_FIFO_R, bytes2read,&buf[size]);
> + if (rc< 0) {
> + dev_err(chip->dev, "recv_data() fail on i2cReadBuf=%d\n", rc);
> + return -EIO;
> + }
> +#ifdef DEBUG
> + for (i = 0; i< bytes2read; i++)
> + sprintf(str +i*3, "%02x ", buf[size+i]);
> + dev_dbg(chip->dev, "recv_data(%d):\n\t%s\n", bytes2read, str);
> +#endif
> + size += bytes2read;
> + }
> + return size;
> +}
> +
> +static int tpm_ti2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> + s32 rc;
> + int size = 0;
> + int expected, status, burstcnt;
> + int retries = TPM_RETRY;
> + u8 data = TPM_STS_RESPONSE_RETRY;
> +
> + if (count< TPM_HEADER_SIZE) {
> + tpm_ti2c_ready(chip); /* return to idle */
> + dev_err(chip->dev, "tpm_ti2c_recv() count< header size\n");
> + return -EIO;
> + }
> +
> + do {
> + if (retries< TPM_RETRY) {
> + /* if this is not the first trial, set responseRetry */
> + i2cWriteBuf(TPM_STS, 1,&data);
> + }
> + /* read first available (> 10 bytes), including:
> + * tag, paramsize, and result */
> + status = wait_for_stat(
> + chip,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + chip->vendor.timeout_c,
> +&chip->vendor.read_queue);
> + if (status != 0) {
> + dev_err(chip->dev, "tpm_ti2c_recv() wait for dataAvail\n");
> + size = -ETIME;
> + retries--;
> + continue;
> + }
> + burstcnt = get_burstcount(chip);
> + if (burstcnt< 0) {
> + dev_err(chip->dev, "tpm_ti2c_recv() Unable to get burstCount\n");
> + size = -EIO;
> + retries --;
> + continue;
> + }
> + if ((size = recv_data(chip, buf, burstcnt))< TPM_HEADER_SIZE) {
> + dev_err(chip->dev, "tpm_ti2c_recv() Unable to read header\n");
> + size = -EIO;
> + retries--;
> + continue;
> + }
> + expected = be32_to_cpu(*(__be32 *) (buf + 2));
> + if (expected> count) {
> + dev_err(chip->dev, "tpm_ti2c_recv() expected> count\n");
> + size = -EIO;
> + retries--;
> + continue;
> + }
> + rc = recv_data(chip,&buf[size], expected - size);
> + if ((rc< 0) || ((size += rc)< expected)) {
> + dev_err(chip->dev,
> + "tpm_ti2c_recv() Unable to read remainder of result\n");
> + size = -EIO;
> + retries--;
> + continue;
> + }
> + if (wait_for_stat(
> + chip,
> + TPM_STS_VALID | TPM_STS_DATA_AVAIL,
> + TPM_STS_VALID,
> + chip->vendor.timeout_c,
> + NULL)) { /* retry? */
> + dev_err(chip->dev, "tpm_ti2c_recv() Error left over data\n");
> + size = -ETIME;
> + retries--;
> + continue;
> + }
> + break;
> + }while (retries> 0);
Again, get rid of all the retry stuff.
> + tpm_ti2c_ready(chip);
Should probably return an error if it is not ready? Also, would it be
better to do the ready tests at the beginning of each operation rather
than the end? That way if the device is not ready you can return an
appropriate error, and you will probably spend less time waiting since
the waits will happen between operations.
> + dev_dbg(chip->dev, "tpm_ti2c_recv() -> %d\n", size);
> + return size;
> +}
> +
> +/*
> + * If interrupts are used (signaled by an irq set in the vendor structure)
> + * tpm.c can skip polling for the data to be available as the interrupt is
> + * waited for here
> + */
> +static int tpm_ti2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
> +{
> + int rc, burstcnt, bytes2write, retries = TPM_RETRY;
> + size_t count = 0;
> + u32 ordinal;
> + u8 data;
> +#ifdef DEBUG
> + int i;
> + char str[I2C_MAX_BUF_SIZE*4];
> +#endif
> +
> + do {
> + tpm_ti2c_ready(chip);
> + if (wait_for_stat(
> + chip,
> + TPM_STS_COMMAND_READY,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b,
> + NULL)) {
> + dev_err(chip->dev, "tpm_ti2c_send() timeout on commandReady\n");
> + return -ETIME;
> + }
> + rc = 0;
> + while (count< len - 1)
> + {
> + burstcnt = get_burstcount(chip);
> + if (burstcnt< 0) {
> + dev_err(chip->dev, "tpm_ti2c_send() fail to get burstCount\n");
> + rc = -EIO;
> + break;
> + }
> + /*
> + * for (; burstcnt> 0&& count< len - 1; burstcnt--) {
> + * i2cWrite8(buf[count], TPM_DATA_FIFO_W);
> + * count++;
> + * }
> + */
Remove this.
> + bytes2write =
> + (len -1 -count)< burstcnt ? (len -1 -count) : burstcnt;
> + rc = i2cWriteBuf(TPM_DATA_FIFO_W, bytes2write,&buf[count]);
> + if (rc< 0) {
> + dev_err(chip->dev, "tpm_ti2c_send() fail on i2cWriteBug\n");
> + break;
> + }
> + #ifdef DEBUG
> + for (i = 0; i< bytes2write; i++)
> + sprintf(str +i*3, "%02x ", buf[count+i]);
> + dev_dbg(chip->dev, "tpm_ti2c_send(%d):\n\t%s\n", bytes2write, str);
> + #endif
> + count += bytes2write;
> + if (wait_for_stat(
> + chip,
> + TPM_STS_VALID | TPM_STS_EXPECT,
> + TPM_STS_VALID | TPM_STS_EXPECT,
> + chip->vendor.timeout_c,
> + NULL)< 0) {
> + dev_err(chip->dev, "tpm_ti2c_send() timeout on Expect\n");
> + rc = -ETIME;
> + break;
> + }
> + }
> + if (rc< 0) {
> + retries--;
> + continue;
> + }
> +
> + /* write last byte */
> + rc = i2cWriteBuf(TPM_DATA_FIFO_W, 1,&buf[count]);
> + if (rc< 0) {
> + dev_err(chip->dev, "tpm_ti2c_send() fail to write last byte\n");
> + rc = -EIO;
> + retries--;
> + continue;
> + }
> + dev_dbg(chip->dev, "tpm_ti2c_send(last):\n\t%02x\n", buf[count]);
> + if (wait_for_stat(
> + chip,
> + TPM_STS_VALID | TPM_STS_EXPECT,
> + TPM_STS_VALID,
> + chip->vendor.timeout_c,
> + NULL)) {
> + dev_err(chip->dev, "tpm_ti2c_send() timeout on Expect to clear\n");
> + rc = -ETIME;
> + retries--;
> + continue;
> + }
> + break;
> + }while (retries> 0);
> + if (rc< 0) { /* retries == 0 */
> + tpm_ti2c_ready(chip);
> + return rc;
> + }
> + /* go and do it */
> + data = TPM_STS_GO;
> + rc = i2cWriteBuf(TPM_STS, 1,&data);
> + if (rc< 0) {
> + dev_err(chip->dev, "tpm_ti2c_send() fail to write Go\n");
> + tpm_ti2c_ready(chip);
> + return rc;
> + }
> + ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
This line looks a bit magic.
> + if (wait_for_stat(
> + chip,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + tpm_calc_ordinal_duration(chip, ordinal),
> +&chip->vendor.read_queue)) {
This wait_for_stat pattern gets used a few times. Can it wrapped up in a
helper function to make the call sites a bit simpler?
> + dev_err(chip->dev, "tpm_ti2c_send() timeout command duration\n");
> + tpm_ti2c_ready(chip);
> + return rc;
> + }
> +
> + dev_dbg(chip->dev, "tpm_ti2c_send() -> %d\n", len);
> + return len;
> +}
> +
> +static struct file_operations ti2c_ops = {
> + .owner = THIS_MODULE,
> + .llseek = no_llseek,
> + .open = tpm_open,
> + .read = tpm_read,
> + .write = tpm_write,
> + .release = tpm_release,
> +};
> +
> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
> +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
> +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
> +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
> +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
> +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
> + NULL);
> +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
> +static struct attribute *ti2c_attrs[] = {
> +&dev_attr_pubek.attr,
> +&dev_attr_pcrs.attr,
> +&dev_attr_enabled.attr,
> +&dev_attr_active.attr,
> +&dev_attr_owned.attr,
> +&dev_attr_temp_deactivated.attr,
> +&dev_attr_caps.attr,
> +&dev_attr_cancel.attr, NULL,
> +};
> +
> +static struct attribute_group ti2c_attr_grp = {
> + .attrs = ti2c_attrs
> +};
> +
> +static struct tpm_vendor_specific tpm_ti2c = {
> + .status = tpm_ti2c_status,
> + .recv = tpm_ti2c_recv,
> + .send = tpm_ti2c_send,
> + .cancel = tpm_ti2c_ready,
> + .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_canceled = TPM_STS_COMMAND_READY,
> + .attr_group =&ti2c_attr_grp,
> + .miscdev = {
> + .fops =&ti2c_ops,},
> +};
> +
> +static irqreturn_t ti2c_int_handler(int dummy, void *dev_id)
> +{
> + struct tpm_chip *chip = dev_id;
> +
> + /* in I2C interrupt is triggeted only on dataAvail */
> + wake_up_interruptible(&chip->vendor.read_queue);
> +
> + /*
> + * consider disabling the interrupt in the host
> + * in case of interrupt storm as TPM SINT# is level interrupt
> + * which is cleared only when dataAvail is cleared
> + */
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int tpm_ti2c_init(
> + struct device *dev,
> + unsigned long start,
> + unsigned long len)
> +{
> + int rc;
> +
> + dev_dbg(dev, "tpm_ti2c_init()\n");
> + if (!start)
> + start = TI2C_MEM_BASE;
> + if (!len)
> + len = TI2C_MEM_LEN;
> +
> + chip = tpm_register_hardware(dev,&tpm_ti2c);
> + if (chip == NULL)
> + {
> + dev_err(dev, "tpm_ti2c_init() error in tpm_register_hardware\n");
> + return -ENODEV;
> + }
> +
> + rc = i2c_add_driver(&ti2c_driver);
> + if (rc != 0)
> + {
> + dev_err(dev, "tpm_ti2c_init() error registering i2c\n");
> + goto out_err;
> + }
> +
> + if (ti2c_i2c_client == NULL)
> + {
> + dev_err(dev, "tpm_ti2c_init() error allocating i2c\n");
> + rc = -ENODEV;
> + goto out_err1;
> + }
> +
> + /* Default timeouts */
> + chip->vendor.timeout_a = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_b = msecs_to_jiffies(TI2C_LONG_TIMEOUT);
> + chip->vendor.timeout_c = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_d = msecs_to_jiffies(TI2C_SHORT_TIMEOUT);
> +
> + /*
> + * Figure out the capabilities
> + * I2C hard coded:
> + * intfcaps = TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT;
> + */
> + dev_dbg(dev, "tpm_ti2c_init() TPM interface capabilities:\n");
> + dev_dbg(dev, "\tInterrupt Level Low\n");
> + dev_dbg(dev, "\tData Avail Int Support\n");
> +
> + /* INTERRUPT Setup */
> + init_waitqueue_head(&chip->vendor.read_queue);
> + init_waitqueue_head(&chip->vendor.int_queue);
> +
> + /*
> + * I2C hard coded:
> + * intmask = TPM_INTF_DATA_AVAIL_INT;
> + */
> + if (chip->vendor.irq) {
> + dev_dbg(dev, "tpm_ti2c_init() chip-vendor.irq\n");
> + if (request_irq(
> + chip->vendor.irq,
> + ti2c_int_handler,
> + IRQF_PROBE_SHARED,
> + chip->vendor.miscdev.name,
> + chip) != 0)
You don't need to split each argument onto its own line. This should fit
in 2 or 3 lines tops. It is preferable to write it like this also:
rc = request_irq(...);
if (rc) {
...
}
> + {
> + dev_err(
> + dev,
> + "tpm_ti2c_init() Unable to request irq: %d for use\n",
> + chip->vendor.irq);
> + chip->vendor.irq = 0;
> + } else {
> + /*
> + * Clear all existing
> + * consider adding code that will make sure TIS is in idle state
> + * and that dataAvail is off in the TPM_STS:
> + * - TPM_STS<- 0x40 (commandReady)
> + */
> + tpm_ti2c_ready(chip);
> + /* - wait for TPM_STS == 0xA0 (stsValid, commandReady) */
> + rc = wait_for_stat(
> + chip,
> + TPM_STS_COMMAND_READY,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b,
> + NULL);
> + if (rc == 0)
> + {
> + /*
> + * TIS is in ready state
> + * write dummy byte to enter reception state
> + * TPM_DATA_FIFO_W<- rc (0)
> + */
> + rc = i2cWriteBuf(TPM_DATA_FIFO_W, 1, (u8*)(&rc));
> + if (rc< 0) {
> + goto out_err1;
> + }
> + /* TPM_STS<- 0x40 (commandReady) */
> + tpm_ti2c_ready(chip);
> + } else {
> + /*
> + * if timeout_b is reached then command was aborted,
> + * TIS in idle state
> + */
> + if (tpm_ti2c_status(chip) != TPM_STS_VALID) {
> + rc = -EIO;
> + goto out_err1;
> + }
> + }
> + }
> + }
> +
> + /* uncomment in case BIOS does not send TPM_StartUp(ST_CLEAR) command */
> + /* tpm_startup_clear(chip); */
> + tpm_get_timeouts(chip);
> + tpm_continue_selftest(chip);
> +
> + return 0;
> +
> +out_err1:
> + i2c_del_driver(&ti2c_driver);
> +out_err:
> + tpm_remove_hardware(chip->dev);
> + return rc;
> +}
> +
> +static int tpm_ti2c_suspend(struct platform_device *dev, pm_message_t msg)
> +{
> + return tpm_pm_suspend(&dev->dev, msg);
> +}
> +
> +static int tpm_ti2c_resume(struct platform_device *dev)
> +{
> + return tpm_pm_resume(&dev->dev);
> +}
> +
> +static struct platform_driver ti2c_drv = {
> + .driver = {
> + .name = "tpm_ti2c",
> + .owner = THIS_MODULE,
> + },
> + .suspend = tpm_ti2c_suspend,
> + .resume = tpm_ti2c_resume,
> +};
> +
> +static struct platform_device *pdev;
> +
> +static int __init init_ti2c(void)
> +{
> + int rc;
> +
> + rc = platform_driver_register(&ti2c_drv);
> + if (rc< 0)
> + return rc;
> +
> + pdev = platform_device_register_simple("tpm_ti2c", -1, NULL, 0);
> + if (IS_ERR(pdev))
> + return PTR_ERR(pdev);
> +
> + rc = tpm_ti2c_init(&pdev->dev, 0, 0);
Why does the tpm_ti2c_init function take optional start/len arguments if
you always pass them as zero?
> + if(rc != 0) {
> + platform_device_unregister(pdev);
> + platform_driver_unregister(&ti2c_drv);
> + }
> +
> + return rc;
> +}
> +
> +static void __exit cleanup_ti2c(void)
> +{
> + dev_info(chip->dev,"in cleanup_ti2c\n");
> +
> + i2c_del_driver(&ti2c_driver);
> + tpm_remove_hardware(chip->dev);
> + if (chip->vendor.irq)
> + free_irq(chip->vendor.irq, chip);
> + platform_device_unregister(pdev);
> + platform_driver_unregister(&ti2c_drv);
> +}
This should be an i2c driver, not a platform driver. It looks like as is
you will end up with three different devices registered?
> +
> +module_init(init_ti2c);
> +module_exit(cleanup_ti2c);
> +MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
> +MODULE_DESCRIPTION("Nuvton TPM I2C Driver");
> +MODULE_VERSION("0.9.5.0");
Do you really need this?
> +MODULE_LICENSE("GPL");
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] TPM Nuvoton I2C driver, kernel 3.0
2011-07-25 7:25 ` [PATCH] TPM Nuvoton I2C driver, kernel 3.0 Dan.Morav
2011-07-26 0:50 ` Ryan Mallon
@ 2011-07-26 14:14 ` Wolfram Sang
2011-07-26 18:45 ` Stefan Berger
2011-08-17 11:11 ` Dan.Morav
3 siblings, 0 replies; 14+ messages in thread
From: Wolfram Sang @ 2011-07-26 14:14 UTC (permalink / raw)
To: Dan.Morav
Cc: linux-kernel, debora, srajiv, m.selhorst, Leonid.Azriel, Guy.Pavlov
[-- Attachment #1: Type: text/plain, Size: 784 bytes --]
On Mon, Jul 25, 2011 at 10:25:30AM +0300, Dan.Morav@nuvoton.com wrote:
> Below are the additions for WPCT301/NPCT501 Nuvoton Technology TPM with I2C
> interface device driver. This driver uses Linux I2C bus driver to interface
> with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux
> TPM interface. This driver requires an I2C bus driver and TPM driver (tpm.ko)
> to be loaded prior to its loading. This driver tested on Linux kernel version
> 3.0-rc6 on an x86 based platform and Linux kernel version 2.6.36.3 on an ARM
> platform.
NACK. Please address the issues I mentioned last time.
--
Pengutronix e.K. | Wolfram Sang |
Industrial Linux Solutions | http://www.pengutronix.de/ |
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] TPM Nuvoton I2C driver, kernel 3.0
2011-07-25 7:25 ` [PATCH] TPM Nuvoton I2C driver, kernel 3.0 Dan.Morav
2011-07-26 0:50 ` Ryan Mallon
2011-07-26 14:14 ` Wolfram Sang
@ 2011-07-26 18:45 ` Stefan Berger
2011-08-17 11:11 ` Dan.Morav
3 siblings, 0 replies; 14+ messages in thread
From: Stefan Berger @ 2011-07-26 18:45 UTC (permalink / raw)
To: Dan.Morav
Cc: linux-kernel, debora, srajiv, m.selhorst, Leonid.Azriel, Guy.Pavlov
On 07/25/2011 03:25 AM, Dan.Morav@nuvoton.com wrote:
> Hi,
>
> Below are the additions for WPCT301/NPCT501 Nuvoton Technology TPM with I2C interface device driver.
> This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
> This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
> This driver tested on Linux kernel version 3.0-rc6 on an x86 based platform and Linux kernel version 2.6.36.3 on an ARM platform.
>
> Regards,
> Dan.
> ___
> +
> +static struct platform_driver ti2c_drv = {
> + .driver = {
> + .name = "tpm_ti2c",
> + .owner = THIS_MODULE,
> + },
> + .suspend = tpm_ti2c_suspend,
> + .resume = tpm_ti2c_resume,
indentation
Stefan
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH] TPM Nuvoton I2C driver, kernel 3.0
2011-07-25 7:25 ` [PATCH] TPM Nuvoton I2C driver, kernel 3.0 Dan.Morav
` (2 preceding siblings ...)
2011-07-26 18:45 ` Stefan Berger
@ 2011-08-17 11:11 ` Dan.Morav
2011-08-24 2:20 ` Ryan Mallon
2011-09-07 7:25 ` Dan.Morav
3 siblings, 2 replies; 14+ messages in thread
From: Dan.Morav @ 2011-08-17 11:11 UTC (permalink / raw)
To: linux-kernel; +Cc: debora, srajiv, m.selhorst
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 30639 bytes --]
Hi,
Below are the additions for WPCT301/NPCT501 Nuvoton Technology TPM with I2C interface device driver.
This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
This driver tested on Linux kernel version 3.0-rc7 on an x86 based platform.
Regards,
Dan.
Signed-off-by: Dan Morav <dmorav@nuvoton.com>
--- linux-3.0-rc7/drivers/char/tpm/Kconfig 2011-07-12 02:51:52.000000000 +0300
+++ linux-3.0/drivers/char/tpm/Kconfig 2011-08-17 13:15:28.062485000 +0300
@@ -60,4 +60,13 @@ config TCG_INFINEON
Further information on this driver and the supported hardware
can be found at http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
+config TCG_NUVOTON_I2C
+ tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
+ depends on I2C
+ ---help---
+ If you have a TPM security chip with I2C interface from
+ Nuvoton Technology Corp. say Yes and it will be accessible
+ from within Linux. To compile this driver as a module, choose
+ M here; the module will be called tpm_nuvoton_i2c.
+
endif # TCG_TPM
--- linux-3.0-rc7/drivers/char/tpm/Makefile 2011-07-12 02:51:52.000000000 +0300
+++ linux-3.0/drivers/char/tpm/Makefile 2011-05-02 10:15:28.000000000 +0300
@@ -9,3 +9,4 @@ obj-$(CONFIG_TCG_TIS) += tpm_tis.o
obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
+obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
--- linux-3.0-rc7/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
+++ linux-3.0/drivers/char/tpm/tpm_nuvoton_i2c.c 2011-08-17 12:12:05.351045000 +0300
@@ -0,0 +1,733 @@
+/******************************************************************************
+ * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501,
+ * based on the TCG TPM Interface Spec version 1.2.
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * Copyright (C) 2011, Nuvoton Technology Corporation.
+ * dan.morav@nuvoton.com
+ *
+ * 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, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/>.
+ *
+ * Nuvoton contact information: APC.Support@nuvoton.com
+ *****************************************************************************/
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/wait.h>
+#include <linux/i2c.h>
+#include "tpm.h"
+
+/* I2C interface offsets */
+#define TPM_STS 0x00
+#define TPM_BURST_COUNT 0x01
+#define TPM_DATA_FIFO_W 0x20
+#define TPM_DATA_FIFO_R 0x40
+#define TPM_VID_DID_RID 0x60
+ /* TPM command header size */
+#define TPM_HEADER_SIZE 10
+#define TPM_RETRY 5
+/*
+ * I2C bus device maximum buffer size w/o counting I2C address or command
+ * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
+ */
+#define TPM_I2C_MAX_BUF_SIZE 32
+#define TPM_I2C_RETRY_COUNT 32
+#define TPM_I2C_BUS_DELAY 1 /* msec */
+#define TPM_I2C_RETRY_DELAY_SHORT 2 /* msec */
+#define TPM_I2C_RETRY_DELAY_LONG 10 /* msec */
+
+#ifdef DEBUG
+#define TPM_I2C_PRINT_BUF(string, buf, size) \
+ print_hex_dump(KERN_DEBUG, (string), DUMP_PREFIX_NONE, 16, 1, (buf), \
+ (size), false);
+#else
+#define TPM_I2C_PRINT_BUF(string, buf, size)
+#endif
+
+static s32 tpm_i2c_read_buf(struct i2c_client *client, u8 offset, u8 size,
+ u8 *data)
+{
+ s32 status;
+
+ status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
+
+ if (status > 0) {
+ dev_dbg(&(client->dev),
+ "tpm_i2c_read_buf(%0x)-> sts=%d:", offset, status);
+ TPM_I2C_PRINT_BUF("data: ", data, size);
+ }
+
+ return status;
+}
+
+static s32 tpm_i2c_write_buf(struct i2c_client *client, u8 offset, u8 size,
+ u8 *data)
+{
+ s32 status;
+
+ status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
+
+ if (status >= 0) {
+ dev_dbg(&(client->dev),
+ "tpm_i2c_write_buf(offset=%0x, size=%0x)-> sts=%d:",
+ offset, size, status);
+ TPM_I2C_PRINT_BUF("data: ", data, size);
+ }
+
+ return status;
+}
+
+#define TPM_STS_VALID 0x80
+#define TPM_STS_COMMAND_READY 0x40
+#define TPM_STS_GO 0x20
+#define TPM_STS_DATA_AVAIL 0x10
+#define TPM_STS_EXPECT 0x08
+#define TPM_STS_RESPONSE_RETRY 0x02
+#define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
+
+#define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
+#define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
+
+/* read TPM_STS register */
+static u8 tpm_i2c_status(struct tpm_chip *chip)
+{
+ struct i2c_client *tpm_i2c_client = to_i2c_client(chip->dev);
+ s32 status;
+ u8 data;
+
+ status = tpm_i2c_read_buf(tpm_i2c_client, TPM_STS, 1, &data);
+ if (status <= 0) {
+ dev_err(chip->dev, "tpm_i2c_status() error return %#02x\n",
+ status);
+ data = TPM_STS_ERR_VAL;
+ }
+
+ return data;
+}
+
+/* write byte to TPM_STS register */
+static s32 tpm_i2c_write_status(struct i2c_client *tpm_i2c_client, u8 data)
+{
+ s32 status;
+ int i;
+
+ /* this causes the current command to be aborted */
+ for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
+ status = tpm_i2c_write_buf(tpm_i2c_client, TPM_STS, 1, &data);
+ msleep(TPM_I2C_BUS_DELAY);
+ }
+ return status;
+}
+
+/* write commandReady to TPM_STS register */
+static void tpm_i2c_ready(struct tpm_chip *chip)
+{
+ struct i2c_client *tpm_i2c_client = to_i2c_client(chip->dev);
+ s32 status;
+
+ /* this causes the current command to be aborted */
+ status = tpm_i2c_write_status(tpm_i2c_client, TPM_STS_COMMAND_READY);
+ if (status < 0)
+ dev_err(chip->dev,
+ "tpm_i2c_ready() fail to write TPM_STS.commandReady\n");
+}
+
+/* read burstCount field from TPM_STS register */
+/* return -1 on fail to read */
+static int tpm_i2c_get_burstcount(struct i2c_client *tpm_i2c_client,
+ struct tpm_chip *chip)
+{
+ unsigned long stop = jiffies + chip->vendor.timeout_d;
+ s32 status;
+ int burst_count = -1;
+ u8 data;
+
+ /* wait for burstcount to be non-zero */
+ do { /* in I2C burstCount is 1 byte */
+ status = tpm_i2c_read_buf(tpm_i2c_client, TPM_BURST_COUNT, 1,
+ &data);
+ if (status > 0 && data > 0) {
+ burst_count = min((u8)TPM_I2C_MAX_BUF_SIZE, data);
+ break;
+ }
+ msleep(TPM_I2C_BUS_DELAY);
+ } while (time_before(jiffies, stop));
+
+ return burst_count;
+}
+
+/*
+ * WPCT301/NPCT501 SINT# supports only dataAvail
+ * any call to this function which is not waiting for dataAvail will
+ * set queue to NULL to avoid waiting for interrupt
+ */
+static bool tpm_i2c_check_status(struct tpm_chip *chip, u8 mask, u8 value)
+{
+ u8 status = tpm_i2c_status(chip);
+ return status != TPM_STS_ERR_VAL && (status & mask) == (value);
+}
+
+static int tpm_i2c_wait4stat(struct tpm_chip *chip, u8 mask, u8 value,
+ u32 timeout, wait_queue_head_t *queue)
+{
+ unsigned long ten_msec;
+ unsigned long stop;
+ bool status_valid;
+ s32 rc;
+
+ /* check current status */
+ status_valid = tpm_i2c_check_status(chip, mask, value);
+ if (status_valid)
+ return 0;
+
+ if (chip->vendor.irq && !queue) {
+ /* use interrupt to wait for the event */
+ rc = wait_event_interruptible_timeout(*queue,
+ tpm_i2c_check_status(chip, mask, value),
+ timeout);
+ if (rc > 0)
+ return 0;
+ } else {
+ /* use polling to wait for the event */
+ ten_msec = jiffies + msecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
+ stop = jiffies + timeout;
+ do {
+ if (time_before(jiffies, ten_msec))
+ msleep(TPM_I2C_RETRY_DELAY_SHORT);
+ else
+ msleep(TPM_I2C_RETRY_DELAY_LONG);
+ status_valid = tpm_i2c_check_status(chip, mask, value);
+ if (status_valid)
+ return 0;
+ } while (time_before(jiffies, stop));
+ }
+ dev_err(chip->dev, "tpm_i2c_wait4stat(%02x, %02x) -> timeout\n", mask,
+ value);
+ return -ETIME;
+}
+
+/* wait for dataAvail field to be set in the TPM_STS register */
+static int tpm_i2c_wait4data_avail(struct tpm_chip *chip, u32 timeout,
+ wait_queue_head_t *queue)
+{
+ return tpm_i2c_wait4stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID, timeout,
+ queue);
+}
+
+/* Read @count bytes into @buf from TPM_RD_FIFO register */
+static int recv_data(struct i2c_client *tpm_i2c_client, struct tpm_chip *chip,
+ u8 *buf, size_t count)
+{
+ s32 rc;
+ int size = 0;
+ int burst_count;
+ int bytes2read;
+
+ while ((size < count) && (tpm_i2c_wait4data_avail(chip,
+ chip->vendor.timeout_c, NULL) == 0)) {
+ burst_count = tpm_i2c_get_burstcount(tpm_i2c_client, chip);
+ if (burst_count < 0) {
+ dev_err(chip->dev,
+ "recv_data() fail to read burstCount=%d\n",
+ burst_count);
+ return -EIO;
+ }
+ bytes2read = min((size_t)burst_count, count - size);
+ rc = tpm_i2c_read_buf(tpm_i2c_client, TPM_DATA_FIFO_R,
+ bytes2read, &buf[size]);
+ if (rc < 0) {
+ dev_err(chip->dev,
+ "recv_data() fail on tpm_i2c_read_buf()=%d\n",
+ rc);
+ return -EIO;
+ }
+ dev_dbg(chip->dev, "recv_data(%d):", bytes2read);
+ size += bytes2read;
+ }
+
+ return size;
+}
+
+/* Read TPM command results */
+static int tpm_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+ struct device *dev = chip->dev;
+ struct i2c_client *tpm_i2c_client = to_i2c_client(dev);
+ s32 rc;
+ int size = 0;
+ int expected;
+ int status;
+ int burst_count;
+ int retries = TPM_RETRY;
+
+ if (count < TPM_HEADER_SIZE) {
+ tpm_i2c_ready(chip); /* return to idle */
+ dev_err(dev, "tpm_i2c_recv() count < header size\n");
+ return -EIO;
+ }
+
+ do {
+ if (retries < TPM_RETRY) {
+ /* if this is not the first trial, set responseRetry */
+ tpm_i2c_write_status(tpm_i2c_client,
+ TPM_STS_RESPONSE_RETRY);
+ }
+ /*
+ * read first available (> 10 bytes), including:
+ * tag, paramsize, and result
+ */
+ status = tpm_i2c_wait4data_avail(chip, chip->vendor.timeout_c,
+ &chip->vendor.read_queue);
+ if (status != 0) {
+ dev_err(dev, "tpm_i2c_recv() timeout on dataAvail\n");
+ size = -ETIME;
+ retries--;
+ continue;
+ }
+ burst_count = tpm_i2c_get_burstcount(tpm_i2c_client, chip);
+ if (burst_count < 0) {
+ dev_err(dev, "tpm_i2c_recv() fail to get burstCount\n");
+ size = -EIO;
+ retries--;
+ continue;
+ }
+ size = recv_data(tpm_i2c_client, chip, buf, burst_count);
+ if (size < TPM_HEADER_SIZE) {
+ dev_err(dev, "tpm_i2c_recv() fail to read header\n");
+ size = -EIO;
+ retries--;
+ continue;
+ }
+ /*
+ * convert number of expected bytes field from big endian 32 bit
+ * to machine native
+ */
+ expected = be32_to_cpu(*(__be32 *)(buf + 2));
+ if (expected > count) {
+ dev_err(dev, "tpm_i2c_recv() expected > count\n");
+ size = -EIO;
+ retries--;
+ continue;
+ }
+ rc = recv_data(tpm_i2c_client, chip, &buf[size], expected-size);
+ size += rc;
+ if ((rc < 0) || (size < expected)) {
+ dev_err(dev, "tpm_i2c_recv() fail to read remainder"
+ "of result\n");
+ size = -EIO;
+ retries--;
+ continue;
+ }
+ if (tpm_i2c_wait4stat(chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
+ TPM_STS_VALID, chip->vendor.timeout_c,
+ NULL)) {
+ dev_err(dev, "tpm_i2c_recv() error left over data\n");
+ size = -ETIME;
+ retries--;
+ continue;
+ }
+ break;
+ } while (retries > 0);
+ tpm_i2c_ready(chip);
+ dev_dbg(chip->dev, "tpm_i2c_recv() -> %d\n", size);
+ return size;
+}
+
+/*
+ * Send TPM command.
+ *
+ * If interrupts are used (signaled by an irq set in the vendor structure)
+ * tpm.c can skip polling for the data to be available as the interrupt is
+ * waited for here
+ */
+static int tpm_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
+{
+ struct device *dev = chip->dev;
+ struct i2c_client *tpm_i2c_client = to_i2c_client(dev);
+ int rc = -EIO;
+ int burst_count;
+ int bytes2write;
+ int retries = TPM_RETRY;
+ size_t count = 0;
+ u32 ordinal;
+
+ do {
+ tpm_i2c_ready(chip);
+ if (tpm_i2c_wait4stat(chip, TPM_STS_COMMAND_READY,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b, NULL)) {
+ dev_err(dev,
+ "tpm_i2c_send() timeout on commandReady\n");
+ retries--;
+ rc = -EIO;
+ continue;
+ }
+ rc = 0;
+ while (count < len - 1) {
+ burst_count = tpm_i2c_get_burstcount(tpm_i2c_client,
+ chip);
+ if (burst_count < 0) {
+ dev_err(dev,
+ "tpm_i2c_send() fail get burstCount\n");
+ rc = -EIO;
+ break;
+ }
+ bytes2write = min((size_t)burst_count, len - 1 - count);
+ rc = tpm_i2c_write_buf(tpm_i2c_client, TPM_DATA_FIFO_W,
+ bytes2write, &buf[count]);
+ if (rc < 0) {
+ dev_err(dev,
+ "tpm_i2c_send() fail i2cWriteBuf\n");
+ break;
+ }
+ dev_dbg(dev, "tpm_i2c_send(%d):", bytes2write);
+ count += bytes2write;
+ rc = tpm_i2c_wait4stat(chip,
+ TPM_STS_VALID | TPM_STS_EXPECT,
+ TPM_STS_VALID | TPM_STS_EXPECT,
+ chip->vendor.timeout_c, NULL);
+ if (rc < 0) {
+ dev_err(dev,
+ "tpm_i2c_send() timeout on Expect\n");
+ rc = -ETIME;
+ break;
+ }
+ }
+ if (rc < 0) {
+ retries--;
+ continue;
+ }
+
+ /* write last byte */
+ rc = tpm_i2c_write_buf(tpm_i2c_client, TPM_DATA_FIFO_W, 1,
+ &buf[count]);
+ if (rc < 0) {
+ dev_err(dev, "tpm_i2c_send() fail to write last byte\n");
+ rc = -EIO;
+ retries--;
+ continue;
+ }
+ dev_dbg(dev, "tpm_i2c_send(last): %02x", buf[count]);
+ rc = tpm_i2c_wait4stat(chip, TPM_STS_VALID | TPM_STS_EXPECT,
+ TPM_STS_VALID, chip->vendor.timeout_c,
+ NULL);
+ if (rc) {
+ dev_err(dev,
+ "tpm_i2c_send() timeout on Expect to clear\n");
+ rc = -ETIME;
+ retries--;
+ continue;
+ }
+ break;
+ } while (retries > 0);
+ if (rc < 0) { /* retries == 0 */
+ tpm_i2c_ready(chip);
+ return rc;
+ }
+ /* go and do it */
+ rc = tpm_i2c_write_status(tpm_i2c_client, TPM_STS_GO);
+ if (rc < 0) {
+ dev_err(dev, "tpm_i2c_send() fail to write Go\n");
+ tpm_i2c_ready(chip);
+ return rc;
+ }
+ /* convert ordinal field from big endian 32 bit to machine native */
+ ordinal = be32_to_cpu(*((__be32 *)(buf + 6)));
+ rc = tpm_i2c_wait4data_avail(chip,
+ tpm_calc_ordinal_duration(chip, ordinal),
+ &chip->vendor.read_queue);
+ if (rc) {
+ dev_err(dev, "tpm_i2c_send() timeout command duration\n");
+ tpm_i2c_ready(chip);
+ return rc;
+ }
+
+ dev_dbg(dev, "tpm_i2c_send() -> %d\n", len);
+ return len;
+}
+
+static const struct file_operations tpm_i2c_ops = {
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .open = tpm_open,
+ .read = tpm_read,
+ .write = tpm_write,
+ .release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *tpm_i2c_attrs[] = {
+ &dev_attr_pubek.attr,
+ &dev_attr_pcrs.attr,
+ &dev_attr_enabled.attr,
+ &dev_attr_active.attr,
+ &dev_attr_owned.attr,
+ &dev_attr_temp_deactivated.attr,
+ &dev_attr_caps.attr,
+ &dev_attr_cancel.attr,
+ NULL,
+};
+
+static struct attribute_group tpm_i2c_attr_grp = {
+ .attrs = tpm_i2c_attrs
+};
+
+static struct tpm_vendor_specific tpm_i2c = {
+ .status = tpm_i2c_status,
+ .recv = tpm_i2c_recv,
+ .send = tpm_i2c_send,
+ .cancel = tpm_i2c_ready,
+ .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_canceled = TPM_STS_COMMAND_READY,
+ .attr_group = &tpm_i2c_attr_grp,
+ .miscdev = {
+ .fops = &tpm_i2c_ops,
+ },
+};
+
+static irqreturn_t tpm_i2c_int_handler(int dummy, void *dev_id)
+{
+ struct tpm_chip *chip = dev_id;
+
+ /* in I2C interrupt is triggeted only on dataAvail */
+ wake_up_interruptible(&chip->vendor.read_queue);
+
+ /*
+ * consider disabling the interrupt in the host
+ * in case of interrupt storm as TPM SINT# is level interrupt
+ * which is cleared only when dataAvail is cleared
+ */
+ return IRQ_HANDLED;
+}
+
+static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe, 0x00 };
+#define VID_DID_RID_SIZE sizeof(vid_did_rid_value)
+
+static int tpm_i2c_detect(struct i2c_client *client,
+ struct i2c_board_info *info)
+{
+ struct i2c_adapter *adapter = client->adapter;
+ struct device *dev = &(client->dev);
+ u32 temp;
+ s32 rc;
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
+ return -ENODEV;
+
+ rc = tpm_i2c_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)(&temp));
+ if (rc < 0) {
+ dev_err(dev, "tpm_i2c_detect() fail read VID/DID/RID => %d\n",
+ rc);
+ return -ENODEV;
+ }
+ dev_dbg(dev, "tpm_i2c_detect() VID: %04X DID: %02X RID: %02X\n", temp,
+ temp>>16, temp>>24);
+
+ /* check WPCT301 values - ignore RID */
+ if (memcmp(&temp, vid_did_rid_value, VID_DID_RID_SIZE-1)) {
+ /*
+ * f/w rev 2.81 has an issue where the VID_DID_RID is not
+ * reporting the right value. so give it another chance at
+ * offset 0x20 (FIFO_W).
+ */
+ rc = tpm_i2c_read_buf(client, TPM_DATA_FIFO_W, 4,
+ (u8 *)(&temp));
+ if (rc < 0) {
+ dev_err(dev, "tpm_i2c_detect() fail to read"
+ "VID/DID/RID. status=%d\n", rc);
+ return -ENODEV;
+ }
+ dev_dbg(dev,
+ "tpm_i2c_detect() VID: %04X DID: %02X RID: %02X\n",
+ temp, temp>>16, temp>>24);
+
+ /* check WPCT301 values - ignore RID*/
+ if (memcmp(&temp, vid_did_rid_value, VID_DID_RID_SIZE-1)) {
+ dev_err(dev,
+ "tpm_i2c_detect() WPCT301/NPCT501 not found\n");
+ return -ENODEV;
+ }
+ }
+
+ strlcpy(info->type, "tpm_i2c", I2C_NAME_SIZE);
+ dev_info(dev, "tpm_i2c VID: %04X DID: %02X RID: %02X\n", temp, temp>>16,
+ temp>>24);
+
+ return 0;
+}
+
+static int tpm_i2c_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ int rc;
+ struct tpm_chip *chip;
+ struct device *dev = &(client->dev);
+ void (*release)(struct device *dev) = dev->release;
+
+ chip = tpm_register_hardware(dev, &tpm_i2c);
+ if (!chip) {
+ dev_err(dev, "tpm_i2c_probe() error in tpm_register_hardware\n");
+ return -ENODEV;
+ }
+ /*
+ * restore dev->release function as it is modified by tpm_register_dev.
+ * save the assigned release function in chip->release for backup.
+ */
+ chip->release = dev->release;
+ dev->release = release;
+
+ /* Default timeouts */
+ chip->vendor.timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
+ chip->vendor.timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
+
+ /*
+ * I2C intfcaps (interrupt capabilitieis) are hard coded to:
+ * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
+ */
+
+ /* INTERRUPT Setup */
+ init_waitqueue_head(&chip->vendor.read_queue);
+ init_waitqueue_head(&chip->vendor.int_queue);
+
+ /* I2C intmask is hard coded to TPM_INTF_DATA_AVAIL_INT */
+ if (chip->vendor.irq) {
+ dev_dbg(dev, "tpm_i2c_probe() chip-vendor.irq\n");
+ rc = request_irq(chip->vendor.irq, tpm_i2c_int_handler,
+ IRQF_PROBE_SHARED, chip->vendor.miscdev.name, chip);
+ if (rc) {
+ dev_err(dev, "tpm_i2c_probe() Unable to request irq:"
+ "%d for use\n", chip->vendor.irq);
+ chip->vendor.irq = 0;
+ } else {
+ /* Clear any pending interrupt */
+ tpm_i2c_ready(chip);
+ /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
+ rc = tpm_i2c_wait4stat(chip, TPM_STS_COMMAND_READY,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b, NULL);
+ if (rc == 0) {
+ /*
+ * TIS is in ready state
+ * write dummy byte to enter reception state
+ * TPM_DATA_FIFO_W <- rc (0)
+ */
+ rc = tpm_i2c_write_buf(client, TPM_DATA_FIFO_W,
+ 1, (u8 *)(&rc));
+ if (rc < 0)
+ goto out_err;
+ /* TPM_STS <- 0x40 (commandReady) */
+ tpm_i2c_ready(chip);
+ } else {
+ /*
+ * timeout_b reached - command was
+ * aborted. TIS should now be in idle state -
+ * only TPM_STS_VALID should be set
+ */
+ if (tpm_i2c_status(chip) != TPM_STS_VALID) {
+ rc = -EIO;
+ goto out_err;
+ }
+ }
+ }
+ }
+
+ /* uncomment in case BIOS/booter does not send TPM_StartUp command */
+ /* tpm_startup_clear(chip); */
+ tpm_get_timeouts(chip);
+ tpm_continue_selftest(chip);
+
+ return 0;
+
+out_err:
+ tpm_remove_hardware(chip->dev);
+ return rc;
+}
+
+static int tpm_i2c_remove(struct i2c_client *client)
+{
+ struct device *dev = &(client->dev);
+ struct tpm_chip *chip = dev_get_drvdata(dev);
+
+ if (chip) {
+ tpm_dev_vendor_release(chip);
+ if (chip->vendor.irq)
+ free_irq(chip->vendor.irq, chip);
+ }
+ tpm_remove_hardware(dev);
+ kfree(chip);
+ return 0;
+}
+
+static int tpm_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
+{
+ return tpm_pm_suspend(&client->dev, mesg);
+}
+
+static int tpm_i2c_resume(struct i2c_client *client)
+{
+ return tpm_pm_resume(&client->dev);
+}
+
+static const struct i2c_device_id tpm_i2c_id[] = { { "tpm_i2c", 0 }, {} };
+
+/* I2C Addresses to scan */
+static const u16 normal_i2c[] = { 0x17, 0x57, I2C_CLIENT_END };
+
+
+#define I2C_CLASS_ANY 0xffff
+
+static struct i2c_driver tpm_i2c_driver = {
+ .driver = {
+ .name = "tpm_i2c",
+ },
+ .probe = tpm_i2c_probe,
+ .remove = tpm_i2c_remove,
+ .id_table = tpm_i2c_id,
+ .detect = tpm_i2c_detect,
+ .address_list = normal_i2c,
+ .suspend = tpm_i2c_suspend,
+ .resume = tpm_i2c_resume,
+ .class = I2C_CLASS_ANY,
+};
+
+static int __init tpm_i2c_init(void)
+{
+ return i2c_add_driver(&tpm_i2c_driver);
+}
+
+static void __exit tpm_i2c_exit(void)
+{
+ i2c_del_driver(&tpm_i2c_driver);
+}
+
+module_init(tpm_i2c_init);
+module_exit(tpm_i2c_exit);
+
+MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
+MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
+MODULE_LICENSE("GPL");
===========================================================================================
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
ÿôèº{.nÇ+·®+%Ëÿ±éݶ\x17¥wÿº{.nÇ+·¥{±þG«éÿ{ayº\x1dÊÚë,j\a¢f£¢·hïêÿêçz_è®\x03(éÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?¨èÚ&£ø§~á¶iOæ¬z·vØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?I¥
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH] TPM Nuvoton I2C driver, kernel 3.0
2011-08-17 11:11 ` Dan.Morav
@ 2011-08-24 2:20 ` Ryan Mallon
2011-09-07 7:25 ` Dan.Morav
1 sibling, 0 replies; 14+ messages in thread
From: Ryan Mallon @ 2011-08-24 2:20 UTC (permalink / raw)
To: Dan.Morav; +Cc: linux-kernel, debora, srajiv, m.selhorst
On 17/08/11 21:11, Dan.Morav@nuvoton.com wrote:
> Hi,
>
> Below are the additions for WPCT301/NPCT501 Nuvoton Technology TPM with I2C interface device driver.
> This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
> This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
> This driver tested on Linux kernel version 3.0-rc7 on an x86 based platform.
>
> Regards,
> Dan.
Hi Dan,
A few comments below.
~Ryan
> Signed-off-by: Dan Morav<dmorav@nuvoton.com>
>
> --- linux-3.0-rc7/drivers/char/tpm/Kconfig 2011-07-12 02:51:52.000000000 +0300
> +++ linux-3.0/drivers/char/tpm/Kconfig 2011-08-17 13:15:28.062485000 +0300
> @@ -60,4 +60,13 @@ config TCG_INFINEON
> Further information on this driver and the supported hardware
> can be found at http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
>
> +config TCG_NUVOTON_I2C
> + tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
> + depends on I2C
> + ---help---
> + If you have a TPM security chip with I2C interface from
> + Nuvoton Technology Corp. say Yes and it will be accessible
> + from within Linux. To compile this driver as a module, choose
> + M here; the module will be called tpm_nuvoton_i2c.
> +
> endif # TCG_TPM
> --- linux-3.0-rc7/drivers/char/tpm/Makefile 2011-07-12 02:51:52.000000000 +0300
> +++ linux-3.0/drivers/char/tpm/Makefile 2011-05-02 10:15:28.000000000 +0300
> @@ -9,3 +9,4 @@ obj-$(CONFIG_TCG_TIS) += tpm_tis.o
> obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
> obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
> obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
> +obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
> --- linux-3.0-rc7/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
> +++ linux-3.0/drivers/char/tpm/tpm_nuvoton_i2c.c 2011-08-17 12:12:05.351045000 +0300
> @@ -0,0 +1,733 @@
> +/******************************************************************************
> + * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501,
> + * based on the TCG TPM Interface Spec version 1.2.
> + * Specifications at www.trustedcomputinggroup.org
> + *
> + * Copyright (C) 2011, Nuvoton Technology Corporation.
> + * dan.morav@nuvoton.com
> + *
> + * 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, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program. If not, see http://www.gnu.org/licenses/>.
> + *
> + * Nuvoton contact information: APC.Support@nuvoton.com
> + *****************************************************************************/
Nitpick: Blank line between header comment and first include.
> +#include<linux/init.h>
> +#include<linux/module.h>
> +#include<linux/moduleparam.h>
> +#include<linux/slab.h>
> +#include<linux/interrupt.h>
> +#include<linux/wait.h>
> +#include<linux/i2c.h>
> +#include "tpm.h"
> +
> +/* I2C interface offsets */
> +#define TPM_STS 0x00
> +#define TPM_BURST_COUNT 0x01
> +#define TPM_DATA_FIFO_W 0x20
> +#define TPM_DATA_FIFO_R 0x40
> +#define TPM_VID_DID_RID 0x60
> + /* TPM command header size */
> +#define TPM_HEADER_SIZE 10
> +#define TPM_RETRY 5
> +/*
> + * I2C bus device maximum buffer size w/o counting I2C address or command
> + * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
> + */
> +#define TPM_I2C_MAX_BUF_SIZE 32
> +#define TPM_I2C_RETRY_COUNT 32
> +#define TPM_I2C_BUS_DELAY 1 /* msec */
> +#define TPM_I2C_RETRY_DELAY_SHORT 2 /* msec */
> +#define TPM_I2C_RETRY_DELAY_LONG 10 /* msec */
> +
> +#ifdef DEBUG
> +#define TPM_I2C_PRINT_BUF(string, buf, size) \
> + print_hex_dump(KERN_DEBUG, (string), DUMP_PREFIX_NONE, 16, 1, (buf), \
> + (size), false);
> +#else
> +#define TPM_I2C_PRINT_BUF(string, buf, size)
> +#endif
This can be a static inline and should have a name which reflects that
it is a debug function. The name should also be lower case. I had
thought there was already a debug version of print_hex_dump, but maybe I
am wrong.
> +
> +static s32 tpm_i2c_read_buf(struct i2c_client *client, u8 offset, u8 size,
> + u8 *data)
> +{
> + s32 status;
> +
> + status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
> +
> + if (status> 0) {
Nitpick: No blank line between an assignment and the if check.
> + dev_dbg(&(client->dev),
> + "tpm_i2c_read_buf(%0x)-> sts=%d:", offset, status);
> + TPM_I2C_PRINT_BUF("data: ", data, size);
> + }
> +
> + return status;
> +}
> +
> +static s32 tpm_i2c_write_buf(struct i2c_client *client, u8 offset, u8 size,
> + u8 *data)
> +{
> + s32 status;
> +
> + status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
> +
> + if (status>= 0) {
> + dev_dbg(&(client->dev),
> + "tpm_i2c_write_buf(offset=%0x, size=%0x)-> sts=%d:",
> + offset, size, status);
> + TPM_I2C_PRINT_BUF("data: ", data, size);
> + }
> +
> + return status;
> +}
> +
> +#define TPM_STS_VALID 0x80
> +#define TPM_STS_COMMAND_READY 0x40
> +#define TPM_STS_GO 0x20
> +#define TPM_STS_DATA_AVAIL 0x10
> +#define TPM_STS_EXPECT 0x08
> +#define TPM_STS_RESPONSE_RETRY 0x02
> +#define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
> +
> +#define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
> +#define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
> +
> +/* read TPM_STS register */
> +static u8 tpm_i2c_status(struct tpm_chip *chip)
> +{
> + struct i2c_client *tpm_i2c_client = to_i2c_client(chip->dev);
> + s32 status;
> + u8 data;
> +
> + status = tpm_i2c_read_buf(tpm_i2c_client, TPM_STS, 1,&data);
> + if (status<= 0) {
> + dev_err(chip->dev, "tpm_i2c_status() error return %#02x\n",
> + status);
> + data = TPM_STS_ERR_VAL;
> + }
> +
> + return data;
> +}
> +
> +/* write byte to TPM_STS register */
> +static s32 tpm_i2c_write_status(struct i2c_client *tpm_i2c_client, u8 data)
> +{
> + s32 status;
> + int i;
> +
> + /* this causes the current command to be aborted */
> + for (i = 0, status = -1; i< TPM_I2C_RETRY_COUNT&& status< 0; i++) {
> + status = tpm_i2c_write_buf(tpm_i2c_client, TPM_STS, 1,&data);
> + msleep(TPM_I2C_BUS_DELAY);
If the hardware is broken/not responding this could potentially loop
forever.
> + }
> + return status;
> +}
> +
> +/* write commandReady to TPM_STS register */
> +static void tpm_i2c_ready(struct tpm_chip *chip)
> +{
> + struct i2c_client *tpm_i2c_client = to_i2c_client(chip->dev);
> + s32 status;
> +
> + /* this causes the current command to be aborted */
> + status = tpm_i2c_write_status(tpm_i2c_client, TPM_STS_COMMAND_READY);
> + if (status< 0)
> + dev_err(chip->dev,
> + "tpm_i2c_ready() fail to write TPM_STS.commandReady\n");
> +}
> +
> +/* read burstCount field from TPM_STS register */
> +/* return -1 on fail to read */
> +static int tpm_i2c_get_burstcount(struct i2c_client *tpm_i2c_client,
> + struct tpm_chip *chip)
> +{
> + unsigned long stop = jiffies + chip->vendor.timeout_d;
> + s32 status;
> + int burst_count = -1;
> + u8 data;
> +
> + /* wait for burstcount to be non-zero */
> + do { /* in I2C burstCount is 1 byte */
Nitpick: Move this comment onto a newline.
> + status = tpm_i2c_read_buf(tpm_i2c_client, TPM_BURST_COUNT, 1,
> +&data);
> + if (status> 0&& data> 0) {
> + burst_count = min((u8)TPM_I2C_MAX_BUF_SIZE, data);
Use min_t instead of casting.
> + break;
> + }
> + msleep(TPM_I2C_BUS_DELAY);
> + } while (time_before(jiffies, stop));
> +
> + return burst_count;
> +}
> +
> +/*
> + * WPCT301/NPCT501 SINT# supports only dataAvail
> + * any call to this function which is not waiting for dataAvail will
> + * set queue to NULL to avoid waiting for interrupt
> + */
> +static bool tpm_i2c_check_status(struct tpm_chip *chip, u8 mask, u8 value)
> +{
> + u8 status = tpm_i2c_status(chip);
> + return status != TPM_STS_ERR_VAL&& (status& mask) == (value);
Don't need the parens around value. Would maybe rewrite as:
return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
To make it a bit more readable.
> +}
> +
> +static int tpm_i2c_wait4stat(struct tpm_chip *chip, u8 mask, u8 value,
> + u32 timeout, wait_queue_head_t *queue)
wait_for_stat.
> +{
> + unsigned long ten_msec;
> + unsigned long stop;
> + bool status_valid;
> + s32 rc;
> +
> + /* check current status */
> + status_valid = tpm_i2c_check_status(chip, mask, value);
> + if (status_valid)
> + return 0;
> +
> + if (chip->vendor.irq&& !queue) {
> + /* use interrupt to wait for the event */
> + rc = wait_event_interruptible_timeout(*queue,
> + tpm_i2c_check_status(chip, mask, value),
> + timeout);
> + if (rc> 0)
> + return 0;
> + } else {
> + /* use polling to wait for the event */
> + ten_msec = jiffies + msecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
> + stop = jiffies + timeout;
> + do {
> + if (time_before(jiffies, ten_msec))
> + msleep(TPM_I2C_RETRY_DELAY_SHORT);
> + else
> + msleep(TPM_I2C_RETRY_DELAY_LONG);
Why two different delays?
> + status_valid = tpm_i2c_check_status(chip, mask, value);
> + if (status_valid)
> + return 0;
> + } while (time_before(jiffies, stop));
> + }
> + dev_err(chip->dev, "tpm_i2c_wait4stat(%02x, %02x) -> timeout\n", mask,
> + value);
You should use __func__ if you want to print out function names.
> + return -ETIME;
ETIMEDOUT appears to be more common.
> +}
> +
> +/* wait for dataAvail field to be set in the TPM_STS register */
> +static int tpm_i2c_wait4data_avail(struct tpm_chip *chip, u32 timeout,
> + wait_queue_head_t *queue)
> +{
> + return tpm_i2c_wait4stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + TPM_STS_DATA_AVAIL | TPM_STS_VALID, timeout,
> + queue);
> +}
> +
> +/* Read @count bytes into @buf from TPM_RD_FIFO register */
> +static int recv_data(struct i2c_client *tpm_i2c_client, struct tpm_chip *chip,
> + u8 *buf, size_t count)
> +{
> + s32 rc;
> + int size = 0;
> + int burst_count;
> + int bytes2read;
Nitpick: Put variables with a common type on the same line.
> +
> + while ((size< count)&& (tpm_i2c_wait4data_avail(chip,
> + chip->vendor.timeout_c, NULL) == 0)) {
Here you don't need the parens around the individual clauses.
> + burst_count = tpm_i2c_get_burstcount(tpm_i2c_client, chip);
> + if (burst_count< 0) {
> + dev_err(chip->dev,
> + "recv_data() fail to read burstCount=%d\n",
> + burst_count);
> + return -EIO;
> + }
> + bytes2read = min((size_t)burst_count, count - size);
min_t
> + rc = tpm_i2c_read_buf(tpm_i2c_client, TPM_DATA_FIFO_R,
> + bytes2read,&buf[size]);
> + if (rc< 0) {
> + dev_err(chip->dev,
> + "recv_data() fail on tpm_i2c_read_buf()=%d\n",
> + rc);
> + return -EIO;
> + }
> + dev_dbg(chip->dev, "recv_data(%d):", bytes2read);
> + size += bytes2read;
> + }
> +
> + return size;
> +}
> +
> +/* Read TPM command results */
> +static int tpm_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> + struct device *dev = chip->dev;
> + struct i2c_client *tpm_i2c_client = to_i2c_client(dev);
> + s32 rc;
> + int size = 0;
> + int expected;
> + int status;
> + int burst_count;
> + int retries = TPM_RETRY;
> +
> + if (count< TPM_HEADER_SIZE) {
> + tpm_i2c_ready(chip); /* return to idle */
> + dev_err(dev, "tpm_i2c_recv() count< header size\n");
> + return -EIO;
> + }
> +
> + do {
for (retries = 0; retries < TPM_RETRY; retries++)
?
> + if (retries< TPM_RETRY) {
> + /* if this is not the first trial, set responseRetry */
> + tpm_i2c_write_status(tpm_i2c_client,
> + TPM_STS_RESPONSE_RETRY);
> + }
> + /*
> + * read first available (> 10 bytes), including:
> + * tag, paramsize, and result
> + */
> + status = tpm_i2c_wait4data_avail(chip, chip->vendor.timeout_c,
> +&chip->vendor.read_queue);
> + if (status != 0) {
> + dev_err(dev, "tpm_i2c_recv() timeout on dataAvail\n");
> + size = -ETIME;
> + retries--;
> + continue;
> + }
> + burst_count = tpm_i2c_get_burstcount(tpm_i2c_client, chip);
> + if (burst_count< 0) {
> + dev_err(dev, "tpm_i2c_recv() fail to get burstCount\n");
> + size = -EIO;
> + retries--;
> + continue;
> + }
> + size = recv_data(tpm_i2c_client, chip, buf, burst_count);
> + if (size< TPM_HEADER_SIZE) {
> + dev_err(dev, "tpm_i2c_recv() fail to read header\n");
> + size = -EIO;
> + retries--;
> + continue;
> + }
> + /*
> + * convert number of expected bytes field from big endian 32 bit
> + * to machine native
> + */
> + expected = be32_to_cpu(*(__be32 *)(buf + 2));
> + if (expected> count) {
> + dev_err(dev, "tpm_i2c_recv() expected> count\n");
> + size = -EIO;
> + retries--;
> + continue;
> + }
> + rc = recv_data(tpm_i2c_client, chip,&buf[size], expected-size);
> + size += rc;
> + if ((rc< 0) || (size< expected)) {
Don't need the internal parens.
> + dev_err(dev, "tpm_i2c_recv() fail to read remainder"
> + "of result\n");
> + size = -EIO;
> + retries--;
> + continue;
> + }
> + if (tpm_i2c_wait4stat(chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
> + TPM_STS_VALID, chip->vendor.timeout_c,
> + NULL)) {
> + dev_err(dev, "tpm_i2c_recv() error left over data\n");
> + size = -ETIME;
> + retries--;
> + continue;
> + }
> + break;
> + } while (retries> 0);
> + tpm_i2c_ready(chip);
> + dev_dbg(chip->dev, "tpm_i2c_recv() -> %d\n", size);
> + return size;
> +}
> +
> +/*
> + * Send TPM command.
> + *
> + * If interrupts are used (signaled by an irq set in the vendor structure)
> + * tpm.c can skip polling for the data to be available as the interrupt is
> + * waited for here
> + */
> +static int tpm_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
> +{
> + struct device *dev = chip->dev;
> + struct i2c_client *tpm_i2c_client = to_i2c_client(dev);
> + int rc = -EIO;
> + int burst_count;
> + int bytes2write;
> + int retries = TPM_RETRY;
> + size_t count = 0;
> + u32 ordinal;
> +
> + do {
Same here, just do a for loop for the retries?
> + tpm_i2c_ready(chip);
> + if (tpm_i2c_wait4stat(chip, TPM_STS_COMMAND_READY,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b, NULL)) {
> + dev_err(dev,
> + "tpm_i2c_send() timeout on commandReady\n");
> + retries--;
> + rc = -EIO;
> + continue;
> + }
> + rc = 0;
> + while (count< len - 1) {
> + burst_count = tpm_i2c_get_burstcount(tpm_i2c_client,
> + chip);
> + if (burst_count< 0) {
> + dev_err(dev,
> + "tpm_i2c_send() fail get burstCount\n");
> + rc = -EIO;
> + break;
> + }
> + bytes2write = min((size_t)burst_count, len - 1 - count);
> + rc = tpm_i2c_write_buf(tpm_i2c_client, TPM_DATA_FIFO_W,
> + bytes2write,&buf[count]);
> + if (rc< 0) {
> + dev_err(dev,
> + "tpm_i2c_send() fail i2cWriteBuf\n");
> + break;
> + }
> + dev_dbg(dev, "tpm_i2c_send(%d):", bytes2write);
> + count += bytes2write;
> + rc = tpm_i2c_wait4stat(chip,
> + TPM_STS_VALID | TPM_STS_EXPECT,
> + TPM_STS_VALID | TPM_STS_EXPECT,
> + chip->vendor.timeout_c, NULL);
> + if (rc< 0) {
> + dev_err(dev,
> + "tpm_i2c_send() timeout on Expect\n");
> + rc = -ETIME;
> + break;
> + }
> + }
> + if (rc< 0) {
> + retries--;
> + continue;
> + }
> +
> + /* write last byte */
> + rc = tpm_i2c_write_buf(tpm_i2c_client, TPM_DATA_FIFO_W, 1,
> +&buf[count]);
> + if (rc< 0) {
> + dev_err(dev, "tpm_i2c_send() fail to write last byte\n");
> + rc = -EIO;
> + retries--;
> + continue;
> + }
> + dev_dbg(dev, "tpm_i2c_send(last): %02x", buf[count]);
> + rc = tpm_i2c_wait4stat(chip, TPM_STS_VALID | TPM_STS_EXPECT,
> + TPM_STS_VALID, chip->vendor.timeout_c,
> + NULL);
> + if (rc) {
> + dev_err(dev,
> + "tpm_i2c_send() timeout on Expect to clear\n");
> + rc = -ETIME;
> + retries--;
> + continue;
> + }
> + break;
> + } while (retries> 0);
> + if (rc< 0) { /* retries == 0 */
Comment on newline.
> + tpm_i2c_ready(chip);
> + return rc;
> + }
> + /* go and do it */
Go and do what? :-)
> + rc = tpm_i2c_write_status(tpm_i2c_client, TPM_STS_GO);
> + if (rc< 0) {
> + dev_err(dev, "tpm_i2c_send() fail to write Go\n");
> + tpm_i2c_ready(chip);
> + return rc;
> + }
> + /* convert ordinal field from big endian 32 bit to machine native */
The line of code below says exactly the same thing that this comment
says. Remove the comment.
> + ordinal = be32_to_cpu(*((__be32 *)(buf + 6)));
> + rc = tpm_i2c_wait4data_avail(chip,
> + tpm_calc_ordinal_duration(chip, ordinal),
> +&chip->vendor.read_queue);
> + if (rc) {
> + dev_err(dev, "tpm_i2c_send() timeout command duration\n");
> + tpm_i2c_ready(chip);
> + return rc;
> + }
> +
> + dev_dbg(dev, "tpm_i2c_send() -> %d\n", len);
> + return len;
> +}
> +
> +static const struct file_operations tpm_i2c_ops = {
> + .owner = THIS_MODULE,
> + .llseek = no_llseek,
> + .open = tpm_open,
> + .read = tpm_read,
> + .write = tpm_write,
> + .release = tpm_release,
> +};
> +
> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
> +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
> +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
> +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
> +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
> +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
> +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
> +
> +static struct attribute *tpm_i2c_attrs[] = {
> +&dev_attr_pubek.attr,
> +&dev_attr_pcrs.attr,
> +&dev_attr_enabled.attr,
> +&dev_attr_active.attr,
> +&dev_attr_owned.attr,
> +&dev_attr_temp_deactivated.attr,
> +&dev_attr_caps.attr,
> +&dev_attr_cancel.attr,
> + NULL,
> +};
> +
> +static struct attribute_group tpm_i2c_attr_grp = {
> + .attrs = tpm_i2c_attrs
> +};
> +
> +static struct tpm_vendor_specific tpm_i2c = {
> + .status = tpm_i2c_status,
> + .recv = tpm_i2c_recv,
> + .send = tpm_i2c_send,
> + .cancel = tpm_i2c_ready,
> + .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> + .req_canceled = TPM_STS_COMMAND_READY,
> + .attr_group =&tpm_i2c_attr_grp,
> + .miscdev = {
> + .fops =&tpm_i2c_ops,
> + },
> +};
> +
> +static irqreturn_t tpm_i2c_int_handler(int dummy, void *dev_id)
> +{
> + struct tpm_chip *chip = dev_id;
> +
> + /* in I2C interrupt is triggeted only on dataAvail */
> + wake_up_interruptible(&chip->vendor.read_queue);
> +
> + /*
> + * consider disabling the interrupt in the host
> + * in case of interrupt storm as TPM SINT# is level interrupt
> + * which is cleared only when dataAvail is cleared
> + */
> + return IRQ_HANDLED;
> +}
> +
> +static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe, 0x00 };
> +#define VID_DID_RID_SIZE sizeof(vid_did_rid_value)
> +
> +static int tpm_i2c_detect(struct i2c_client *client,
> + struct i2c_board_info *info)
> +{
> + struct i2c_adapter *adapter = client->adapter;
> + struct device *dev =&(client->dev);
> + u32 temp;
> + s32 rc;
> +
> + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
> + return -ENODEV;
> +
> + rc = tpm_i2c_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)(&temp));
Don't need parens around &temp.
> + if (rc< 0) {
> + dev_err(dev, "tpm_i2c_detect() fail read VID/DID/RID => %d\n",
> + rc);
> + return -ENODEV;
> + }
> + dev_dbg(dev, "tpm_i2c_detect() VID: %04X DID: %02X RID: %02X\n", temp,
> + temp>>16, temp>>24);
Nitpick: space on either side of >> operator.
> +
> + /* check WPCT301 values - ignore RID */
> + if (memcmp(&temp, vid_did_rid_value, VID_DID_RID_SIZE-1)) {
Spaces on either side of operators.
Why do you have VID_DID_RID_SIZE - 1 everywhere. Can't you just drop the
0x00 from the end so it is only three bytes and just do:
if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value)) {
> + /*
> + * f/w rev 2.81 has an issue where the VID_DID_RID is not
> + * reporting the right value. so give it another chance at
> + * offset 0x20 (FIFO_W).
> + */
> + rc = tpm_i2c_read_buf(client, TPM_DATA_FIFO_W, 4,
> + (u8 *)(&temp));
> + if (rc< 0) {
> + dev_err(dev, "tpm_i2c_detect() fail to read"
> + "VID/DID/RID. status=%d\n", rc);
> + return -ENODEV;
> + }
> + dev_dbg(dev,
> + "tpm_i2c_detect() VID: %04X DID: %02X RID: %02X\n",
> + temp, temp>>16, temp>>24);
> +
> + /* check WPCT301 values - ignore RID*/
> + if (memcmp(&temp, vid_did_rid_value, VID_DID_RID_SIZE-1)) {
> + dev_err(dev,
> + "tpm_i2c_detect() WPCT301/NPCT501 not found\n");
> + return -ENODEV;
> + }
> + }
> +
> + strlcpy(info->type, "tpm_i2c", I2C_NAME_SIZE);
> + dev_info(dev, "tpm_i2c VID: %04X DID: %02X RID: %02X\n", temp, temp>>16,
> + temp>>24);
> +
> + return 0;
> +}
> +
> +static int tpm_i2c_probe(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + int rc;
> + struct tpm_chip *chip;
> + struct device *dev =&(client->dev);
Don't need the parens.
> + void (*release)(struct device *dev) = dev->release;
> +
> + chip = tpm_register_hardware(dev,&tpm_i2c);
> + if (!chip) {
> + dev_err(dev, "tpm_i2c_probe() error in tpm_register_hardware\n");
> + return -ENODEV;
> + }
> + /*
> + * restore dev->release function as it is modified by tpm_register_dev.
> + * save the assigned release function in chip->release for backup.
> + */
> + chip->release = dev->release;
> + dev->release = release;
Indentation got borked here. It also looks like you are using spaces
instead of tabs for indentation?
Reassigned the dev->release function looks odd. Is this the correct
thing to do?
> +
> + /* Default timeouts */
> + chip->vendor.timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
> + chip->vendor.timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
> + chip->vendor.timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
> +
> + /*
> + * I2C intfcaps (interrupt capabilitieis) are hard coded to:
> + * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
> + */
> +
> + /* INTERRUPT Setup */
> + init_waitqueue_head(&chip->vendor.read_queue);
> + init_waitqueue_head(&chip->vendor.int_queue);
Should this go inside the if (chip->vendor.irq) block if it is related
to interrupts?
> +
> + /* I2C intmask is hard coded to TPM_INTF_DATA_AVAIL_INT */
> + if (chip->vendor.irq) {
> + dev_dbg(dev, "tpm_i2c_probe() chip-vendor.irq\n");
> + rc = request_irq(chip->vendor.irq, tpm_i2c_int_handler,
> + IRQF_PROBE_SHARED, chip->vendor.miscdev.name, chip);
> + if (rc) {
> + dev_err(dev, "tpm_i2c_probe() Unable to request irq:"
> + "%d for use\n", chip->vendor.irq);
> + chip->vendor.irq = 0;
> + } else {
> + /* Clear any pending interrupt */
I think you can get an interrupt at this point. Are you ready for it?
> + tpm_i2c_ready(chip);
> + /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
> + rc = tpm_i2c_wait4stat(chip, TPM_STS_COMMAND_READY,
> + TPM_STS_COMMAND_READY,
> + chip->vendor.timeout_b, NULL);
> + if (rc == 0) {
> + /*
> + * TIS is in ready state
> + * write dummy byte to enter reception state
> + * TPM_DATA_FIFO_W<- rc (0)
> + */
> + rc = tpm_i2c_write_buf(client, TPM_DATA_FIFO_W,
> + 1, (u8 *)(&rc));
> + if (rc< 0)
> + goto out_err;
> + /* TPM_STS<- 0x40 (commandReady) */
> + tpm_i2c_ready(chip);
> + } else {
> + /*
> + * timeout_b reached - command was
> + * aborted. TIS should now be in idle state -
> + * only TPM_STS_VALID should be set
> + */
> + if (tpm_i2c_status(chip) != TPM_STS_VALID) {
> + rc = -EIO;
> + goto out_err;
> + }
> + }
> + }
> + }
> +
> + /* uncomment in case BIOS/booter does not send TPM_StartUp command */
> + /* tpm_startup_clear(chip); */
Is this a common problem? Should it be a command line option to the module?
> + tpm_get_timeouts(chip);
> + tpm_continue_selftest(chip);
> +
> + return 0;
> +
> +out_err:
> + tpm_remove_hardware(chip->dev);
> + return rc;
> +}
> +
> +static int tpm_i2c_remove(struct i2c_client *client)
> +{
> + struct device *dev =&(client->dev);
> + struct tpm_chip *chip = dev_get_drvdata(dev);
> +
> + if (chip) {
Can chip be NULL here?
> + tpm_dev_vendor_release(chip);
> + if (chip->vendor.irq)
> + free_irq(chip->vendor.irq, chip);
> + }
> + tpm_remove_hardware(dev);
> + kfree(chip);
> + return 0;
> +}
> +
> +static int tpm_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
> +{
> + return tpm_pm_suspend(&client->dev, mesg);
> +}
> +
> +static int tpm_i2c_resume(struct i2c_client *client)
> +{
> + return tpm_pm_resume(&client->dev);
> +}
> +
> +static const struct i2c_device_id tpm_i2c_id[] = { { "tpm_i2c", 0 }, {} };
> +
> +/* I2C Addresses to scan */
> +static const u16 normal_i2c[] = { 0x17, 0x57, I2C_CLIENT_END };
I though this style of i2c registration was deprecated? I could be wrong.
> +
> +
> +#define I2C_CLASS_ANY 0xffff
What is this for?
> +static struct i2c_driver tpm_i2c_driver = {
> + .driver = {
> + .name = "tpm_i2c",
> + },
> + .probe = tpm_i2c_probe,
> + .remove = tpm_i2c_remove,
> + .id_table = tpm_i2c_id,
> + .detect = tpm_i2c_detect,
> + .address_list = normal_i2c,
> + .suspend = tpm_i2c_suspend,
> + .resume = tpm_i2c_resume,
> + .class = I2C_CLASS_ANY,
> +};
> +
> +static int __init tpm_i2c_init(void)
> +{
> + return i2c_add_driver(&tpm_i2c_driver);
> +}
> +
> +static void __exit tpm_i2c_exit(void)
> +{
> + i2c_del_driver(&tpm_i2c_driver);
> +}
> +
> +module_init(tpm_i2c_init);
> +module_exit(tpm_i2c_exit);
> +
> +MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
> +MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
> +MODULE_LICENSE("GPL");
>
>
> ===========================================================================================
> The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
> N�����r��y���b�X��ǧv�^�){.n�+����{����zX��\x17��ܨ}���Ơz�&j:+v���\a����zZ+��+zf���h���~����i���z�\x1e�w���?����&�)ߢ^[f��^jǫy�m��@A�a��\x7f�\f0��h�\x0f�i\x7f
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH] TPM Nuvoton I2C driver, kernel 3.0
2011-08-17 11:11 ` Dan.Morav
2011-08-24 2:20 ` Ryan Mallon
@ 2011-09-07 7:25 ` Dan.Morav
1 sibling, 0 replies; 14+ messages in thread
From: Dan.Morav @ 2011-09-07 7:25 UTC (permalink / raw)
To: linux-kernel; +Cc: debora, srajiv, m.selhorst
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 31051 bytes --]
Hi,
Below are the additions for WPCT301/NPCT501 Nuvoton Technology TPM with I2C interface device driver.
This driver uses Linux I2C bus driver to interface with the I2C bus and Linux TPM driver (tpm.ko), to export the standard Linux TPM interface.
This driver requires an I2C bus driver and TPM driver (tpm.ko) to be loaded prior to its loading.
This driver tested on Linux kernel version 3.0-rc7 on an x86 based platform.
Regards,
Dan.
Signed-off-by: Dan Morav <dmorav@nuvoton.com>
diff -uprN linux-3.0-rc7/drivers/char/tpm/Kconfig linux/drivers/char/tpm/Kconfig
--- linux-3.0-rc7/drivers/char/tpm/Kconfig 2011-07-12 02:51:52.000000000 +0300
+++ linux/drivers/char/tpm/Kconfig 2011-08-17 13:15:28.062485000 +0300
@@ -60,4 +60,13 @@ config TCG_INFINEON
Further information on this driver and the supported hardware
can be found at http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
+config TCG_NUVOTON_I2C
+ tristate "Nuvoton Technology Corp. TPM 1.2 I2C Interface"
+ depends on I2C
+ ---help---
+ If you have a TPM security chip with I2C interface from
+ Nuvoton Technology Corp. say Yes and it will be accessible
+ from within Linux. To compile this driver as a module, choose
+ M here; the module will be called tpm_nuvoton_i2c.
+
endif # TCG_TPM
diff -uprN linux-3.0-rc7/drivers/char/tpm/Makefile linux/drivers/char/tpm/Makefile
--- linux-3.0-rc7/drivers/char/tpm/Makefile 2011-07-12 02:51:52.000000000 +0300
+++ linux/drivers/char/tpm/Makefile 2011-05-02 10:15:28.000000000 +0300
@@ -9,3 +9,4 @@ obj-$(CONFIG_TCG_TIS) += tpm_tis.o
obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
+obj-$(CONFIG_TCG_NUVOTON_I2C) += tpm_nuvoton_i2c.o
diff -uprN linux-3.0-rc7/drivers/char/tpm/tpm_nuvoton_i2c.c linux/drivers/char/tpm/tpm_nuvoton_i2c.c
--- linux-3.0-rc7/drivers/char/tpm/tpm_nuvoton_i2c.c 1970-01-01 02:00:00.000000000 +0200
+++ linux/drivers/char/tpm/tpm_nuvoton_i2c.c 2011-09-07 10:03:33.384833000 +0300
@@ -0,0 +1,719 @@
+/******************************************************************************
+ * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501,
+ * based on the TCG TPM Interface Spec version 1.2.
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * Copyright (C) 2011, Nuvoton Technology Corporation.
+ * dan.morav@nuvoton.com
+ *
+ * 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, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/>.
+ *
+ * Nuvoton contact information: APC.Support@nuvoton.com
+ *****************************************************************************/
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/wait.h>
+#include <linux/i2c.h>
+#include "tpm.h"
+
+/* I2C interface offsets */
+#define TPM_STS 0x00
+#define TPM_BURST_COUNT 0x01
+#define TPM_DATA_FIFO_W 0x20
+#define TPM_DATA_FIFO_R 0x40
+#define TPM_VID_DID_RID 0x60
+/* TPM command header size */
+#define TPM_HEADER_SIZE 10
+#define TPM_RETRY 5
+/*
+ * I2C bus device maximum buffer size w/o counting I2C address or command
+ * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
+ */
+#define TPM_I2C_MAX_BUF_SIZE 32
+#define TPM_I2C_RETRY_COUNT 32
+#define TPM_I2C_BUS_DELAY 1 /* msec */
+#define TPM_I2C_RETRY_DELAY_SHORT 2 /* msec */
+#define TPM_I2C_RETRY_DELAY_LONG 10 /* msec */
+
+static inline void debug_hex_dump(const char *prefix_str, const void *buf,
+ size_t size)
+{
+#ifdef DEBUG
+ print_hex_dump(KERN_DEBUG, prefix_str, DUMP_PREFIX_NONE, 16, 1, buf,
+ size, false);
+#endif
+}
+
+static s32 tpm_i2c_read_buf(struct i2c_client *client, u8 offset, u8 size,
+ u8 *data)
+{
+ s32 status;
+
+ status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
+ if (status > 0) {
+ dev_dbg(&(client->dev),
+ "%s(%0x)-> sts=%d:", __func__, offset, status);
+ debug_hex_dump("data: ", data, size);
+ }
+
+ return status;
+}
+
+static s32 tpm_i2c_write_buf(struct i2c_client *client, u8 offset, u8 size,
+ u8 *data)
+{
+ s32 status;
+
+ status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
+ if (status >= 0) {
+ dev_dbg(&(client->dev),
+ "%s(offset=%0x, size=%0x)-> sts=%d:", __func__, offset,
+ size, status);
+ debug_hex_dump("data: ", data, size);
+ }
+
+ return status;
+}
+
+#define TPM_STS_VALID 0x80
+#define TPM_STS_COMMAND_READY 0x40
+#define TPM_STS_GO 0x20
+#define TPM_STS_DATA_AVAIL 0x10
+#define TPM_STS_EXPECT 0x08
+#define TPM_STS_RESPONSE_RETRY 0x02
+#define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
+
+#define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
+#define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
+
+/* read TPM_STS register */
+static u8 tpm_i2c_read_status(struct tpm_chip *chip)
+{
+ struct i2c_client *tpm_i2c_client = to_i2c_client(chip->dev);
+ s32 status;
+ u8 data;
+
+ status = tpm_i2c_read_buf(tpm_i2c_client, TPM_STS, 1, &data);
+ if (status <= 0) {
+ dev_err(chip->dev, "%s() error return %#02x\n", __func__,
+ status);
+ data = TPM_STS_ERR_VAL;
+ }
+
+ return data;
+}
+
+/* write byte to TPM_STS register */
+static s32 tpm_i2c_write_status(struct i2c_client *tpm_i2c_client, u8 data)
+{
+ s32 status;
+ int i;
+
+ /* this causes the current command to be aborted */
+ for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
+ status = tpm_i2c_write_buf(tpm_i2c_client, TPM_STS, 1, &data);
+ msleep(TPM_I2C_BUS_DELAY);
+ }
+ return status;
+}
+
+/* write commandReady to TPM_STS register */
+static void tpm_i2c_ready(struct tpm_chip *chip)
+{
+ struct i2c_client *tpm_i2c_client = to_i2c_client(chip->dev);
+ s32 status;
+
+ /* this causes the current command to be aborted */
+ status = tpm_i2c_write_status(tpm_i2c_client, TPM_STS_COMMAND_READY);
+ if (status < 0)
+ dev_err(chip->dev,
+ "%s() fail to write TPM_STS.commandReady\n", __func__);
+}
+
+/* read burstCount field from TPM_STS register */
+/* return -1 on fail to read */
+static int tpm_i2c_get_burstcount(struct i2c_client *tpm_i2c_client,
+ struct tpm_chip *chip)
+{
+ unsigned long stop = jiffies + chip->vendor.timeout_d;
+ s32 status;
+ int burst_count = -1;
+ u8 data;
+
+ /* wait for burstcount to be non-zero */
+ do {
+ /* in I2C burstCount is 1 byte */
+ status = tpm_i2c_read_buf(tpm_i2c_client, TPM_BURST_COUNT, 1,
+ &data);
+ if (status > 0 && data > 0) {
+ burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
+ break;
+ }
+ msleep(TPM_I2C_BUS_DELAY);
+ } while (time_before(jiffies, stop));
+
+ return burst_count;
+}
+
+/*
+ * WPCT301/NPCT501 SINT# supports only dataAvail
+ * any call to this function which is not waiting for dataAvail will
+ * set queue to NULL to avoid waiting for interrupt
+ */
+static bool tpm_i2c_check_status(struct tpm_chip *chip, u8 mask, u8 value)
+{
+ u8 status = tpm_i2c_read_status(chip);
+ return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
+}
+
+static int tpm_i2c_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
+ u32 timeout, wait_queue_head_t *queue)
+{
+ unsigned long ten_msec, stop;
+ bool status_valid;
+ s32 rc;
+
+ /* check current status */
+ status_valid = tpm_i2c_check_status(chip, mask, value);
+ if (status_valid)
+ return 0;
+ if (chip->vendor.irq && !queue) {
+ /* use interrupt to wait for the event */
+ rc = wait_event_interruptible_timeout(*queue,
+ tpm_i2c_check_status(chip,
+ mask,
+ value),
+ timeout);
+ if (rc > 0)
+ return 0;
+ } else {
+ /* use polling to wait for the event */
+ ten_msec = jiffies + msecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
+ stop = jiffies + timeout;
+ do {
+ if (time_before(jiffies, ten_msec))
+ msleep(TPM_I2C_RETRY_DELAY_SHORT);
+ else
+ msleep(TPM_I2C_RETRY_DELAY_LONG);
+ status_valid = tpm_i2c_check_status(chip, mask, value);
+ if (status_valid)
+ return 0;
+ } while (time_before(jiffies, stop));
+ }
+ dev_err(chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
+ value);
+ return -ETIMEDOUT;
+}
+
+/* wait for dataAvail field to be set in the TPM_STS register */
+static int tpm_i2c_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
+ wait_queue_head_t *queue)
+{
+ return tpm_i2c_wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ timeout, queue);
+}
+
+/* Read @count bytes into @buf from TPM_RD_FIFO register */
+static int tpm_i2c_recv_data(struct i2c_client *tpm_i2c_client,
+ struct tpm_chip *chip, u8 * buf, size_t count)
+{
+ s32 rc;
+ int burst_count, bytes2read, size = 0;
+
+ while (size < count && tpm_i2c_wait_for_data_avail(chip,
+ chip->vendor.timeout_c,
+ NULL) == 0) {
+ burst_count = tpm_i2c_get_burstcount(tpm_i2c_client, chip);
+ if (burst_count < 0) {
+ dev_err(chip->dev,
+ "%s() fail to read burstCount=%d\n", __func__,
+ burst_count);
+ return -EIO;
+ }
+ bytes2read = min_t(size_t, burst_count, count - size);
+ rc = tpm_i2c_read_buf(tpm_i2c_client, TPM_DATA_FIFO_R,
+ bytes2read, &buf[size]);
+ if (rc < 0) {
+ dev_err(chip->dev,
+ "%s() fail on tpm_i2c_read_buf()=%d\n",
+ __func__, rc);
+ return -EIO;
+ }
+ dev_dbg(chip->dev, "%s(%d):", __func__, bytes2read);
+ size += bytes2read;
+ }
+
+ return size;
+}
+
+/* Read TPM command results */
+static int tpm_i2c_recv(struct tpm_chip *chip, u8 * buf, size_t count)
+{
+ struct device *dev = chip->dev;
+ struct i2c_client *tpm_i2c_client = to_i2c_client(dev);
+ s32 rc;
+ int expected, status, burst_count, retries, size = 0;
+
+ if (count < TPM_HEADER_SIZE) {
+ tpm_i2c_ready(chip); /* return to idle */
+ dev_err(dev, "%s() count < header size\n", __func__);
+ return -EIO;
+ }
+ for (retries = 0; retries < TPM_RETRY; retries++) {
+ if (retries > 0) {
+ /* if this is not the first trial, set responseRetry */
+ tpm_i2c_write_status(tpm_i2c_client,
+ TPM_STS_RESPONSE_RETRY);
+ }
+ /*
+ * read first available (> 10 bytes), including:
+ * tag, paramsize, and result
+ */
+ status = tpm_i2c_wait_for_data_avail(chip,
+ chip->vendor.timeout_c,
+ &chip->vendor.read_queue);
+ if (status != 0) {
+ dev_err(dev, "%s() timeout on dataAvail\n", __func__);
+ size = -ETIMEDOUT;
+ continue;
+ }
+ burst_count = tpm_i2c_get_burstcount(tpm_i2c_client, chip);
+ if (burst_count < 0) {
+ dev_err(dev, "%s() fail to get burstCount\n", __func__);
+ size = -EIO;
+ continue;
+ }
+ size = tpm_i2c_recv_data(tpm_i2c_client, chip, buf,
+ burst_count);
+ if (size < TPM_HEADER_SIZE) {
+ dev_err(dev, "%s() fail to read header\n", __func__);
+ size = -EIO;
+ continue;
+ }
+ /*
+ * convert number of expected bytes field from big endian 32 bit
+ * to machine native
+ */
+ expected = be32_to_cpu(*(__be32 *) (buf + 2));
+ if (expected > count) {
+ dev_err(dev, "%s() expected > count\n", __func__);
+ size = -EIO;
+ continue;
+ }
+ rc = tpm_i2c_recv_data(tpm_i2c_client, chip, &buf[size],
+ expected - size);
+ size += rc;
+ if (rc < 0 || size < expected) {
+ dev_err(dev, "%s() fail to read remainder of result\n",
+ __func__);
+ size = -EIO;
+ continue;
+ }
+ if (tpm_i2c_wait_for_stat(chip,
+ TPM_STS_VALID | TPM_STS_DATA_AVAIL,
+ TPM_STS_VALID, chip->vendor.timeout_c,
+ NULL)) {
+ dev_err(dev, "%s() error left over data\n", __func__);
+ size = -ETIMEDOUT;
+ continue;
+ }
+ break;
+ }
+ tpm_i2c_ready(chip);
+ dev_dbg(chip->dev, "%s() -> %d\n", __func__, size);
+ return size;
+}
+
+/*
+ * Send TPM command.
+ *
+ * If interrupts are used (signaled by an irq set in the vendor structure)
+ * tpm.c can skip polling for the data to be available as the interrupt is
+ * waited for here
+ */
+static int tpm_i2c_send(struct tpm_chip *chip, u8 * buf, size_t len)
+{
+ struct device *dev = chip->dev;
+ struct i2c_client *tpm_i2c_client = to_i2c_client(dev);
+ u32 ordinal;
+ size_t count = 0;
+ int burst_count, bytes2write, retries, rc = -EIO;
+
+ for (retries = 0; retries < TPM_RETRY; retries++) {
+ tpm_i2c_ready(chip);
+ if (tpm_i2c_wait_for_stat(chip, TPM_STS_COMMAND_READY,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b, NULL)) {
+ dev_err(dev, "%s() timeout on commandReady\n",
+ __func__);
+ rc = -EIO;
+ continue;
+ }
+ rc = 0;
+ while (count < len - 1) {
+ burst_count = tpm_i2c_get_burstcount(tpm_i2c_client,
+ chip);
+ if (burst_count < 0) {
+ dev_err(dev, "%s() fail get burstCount\n",
+ __func__);
+ rc = -EIO;
+ break;
+ }
+ bytes2write = min_t(size_t, burst_count,
+ len - 1 - count);
+ rc = tpm_i2c_write_buf(tpm_i2c_client, TPM_DATA_FIFO_W,
+ bytes2write, &buf[count]);
+ if (rc < 0) {
+ dev_err(dev, "%s() fail i2cWriteBuf\n",
+ __func__);
+ break;
+ }
+ dev_dbg(dev, "%s(%d):", __func__, bytes2write);
+ count += bytes2write;
+ rc = tpm_i2c_wait_for_stat(chip,
+ TPM_STS_VALID |
+ TPM_STS_EXPECT,
+ TPM_STS_VALID |
+ TPM_STS_EXPECT,
+ chip->vendor.timeout_c,
+ NULL);
+ if (rc < 0) {
+ dev_err(dev, "%s() timeout on Expect\n",
+ __func__);
+ rc = -ETIMEDOUT;
+ break;
+ }
+ }
+ if (rc < 0)
+ continue;
+
+ /* write last byte */
+ rc = tpm_i2c_write_buf(tpm_i2c_client, TPM_DATA_FIFO_W, 1,
+ &buf[count]);
+ if (rc < 0) {
+ dev_err(dev, "%s() fail to write last byte\n",
+ __func__);
+ rc = -EIO;
+ continue;
+ }
+ dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
+ rc = tpm_i2c_wait_for_stat(chip, TPM_STS_VALID | TPM_STS_EXPECT,
+ TPM_STS_VALID,
+ chip->vendor.timeout_c, NULL);
+ if (rc) {
+ dev_err(dev, "%s() timeout on Expect to clear\n",
+ __func__);
+ rc = -ETIMEDOUT;
+ continue;
+ }
+ break;
+ }
+ if (rc < 0) {
+ /* retries == TPM_RETRY */
+ tpm_i2c_ready(chip);
+ return rc;
+ }
+ /* execute the TPM command */
+ rc = tpm_i2c_write_status(tpm_i2c_client, TPM_STS_GO);
+ if (rc < 0) {
+ dev_err(dev, "%s() fail to write Go\n", __func__);
+ tpm_i2c_ready(chip);
+ return rc;
+ }
+ ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+ rc = tpm_i2c_wait_for_data_avail(chip,
+ tpm_calc_ordinal_duration(chip,
+ ordinal),
+ &chip->vendor.read_queue);
+ if (rc) {
+ dev_err(dev, "%s() timeout command duration\n", __func__);
+ tpm_i2c_ready(chip);
+ return rc;
+ }
+
+ dev_dbg(dev, "%s() -> %d\n", __func__, len);
+ return len;
+}
+
+static const struct file_operations tpm_i2c_ops = {
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .open = tpm_open,
+ .read = tpm_read,
+ .write = tpm_write,
+ .release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *tpm_i2c_attrs[] = {
+ &dev_attr_pubek.attr,
+ &dev_attr_pcrs.attr,
+ &dev_attr_enabled.attr,
+ &dev_attr_active.attr,
+ &dev_attr_owned.attr,
+ &dev_attr_temp_deactivated.attr,
+ &dev_attr_caps.attr,
+ &dev_attr_cancel.attr,
+ NULL,
+};
+
+static struct attribute_group tpm_i2c_attr_grp = {
+ .attrs = tpm_i2c_attrs
+};
+
+static struct tpm_vendor_specific tpm_i2c = {
+ .status = tpm_i2c_read_status,
+ .recv = tpm_i2c_recv,
+ .send = tpm_i2c_send,
+ .cancel = tpm_i2c_ready,
+ .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+ .req_canceled = TPM_STS_COMMAND_READY,
+ .attr_group = &tpm_i2c_attr_grp,
+ .miscdev = {
+ .fops = &tpm_i2c_ops,
+ },
+};
+
+static irqreturn_t tpm_i2c_int_handler(int dummy, void *dev_id)
+{
+ struct tpm_chip *chip = dev_id;
+
+ /* in I2C interrupt is triggeted only on dataAvail */
+ wake_up_interruptible(&chip->vendor.read_queue);
+
+ /*
+ * consider disabling the interrupt in the host
+ * in case of interrupt storm as TPM SINT# is level interrupt
+ * which is cleared only when dataAvail is cleared
+ */
+ return IRQ_HANDLED;
+}
+
+static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
+
+static int tpm_i2c_detect(struct i2c_client *client,
+ struct i2c_board_info *info)
+{
+ struct i2c_adapter *adapter = client->adapter;
+ struct device *dev = &(client->dev);
+ u32 temp;
+ s32 rc;
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
+ return -ENODEV;
+ rc = tpm_i2c_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
+ if (rc < 0) {
+ dev_err(dev, "%s() fail read VID/DID/RID => %d\n", __func__,
+ rc);
+ return -ENODEV;
+ }
+ dev_dbg(dev, "%s() VID: %04X DID: %02X RID: %02X\n", __func__,
+ (u16) temp, (u8) (temp >> 16), (u8) (temp >> 24));
+ /* check WPCT301 values - ignore RID */
+ if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
+ /*
+ * f/w rev 2.81 has an issue where the VID_DID_RID is not
+ * reporting the right value. so give it another chance at
+ * offset 0x20 (FIFO_W).
+ */
+ rc = tpm_i2c_read_buf(client, TPM_DATA_FIFO_W, 4,
+ (u8 *) (&temp));
+ if (rc < 0) {
+ dev_err(dev, "%s() fail to read VID/DID/RID. "
+ "status=%d\n", __func__, rc);
+ return -ENODEV;
+ }
+ dev_dbg(dev, "%s() VID: %04X DID: %02X RID: %02X\n", __func__,
+ (u16) temp, (u8) (temp >> 16), (u8) (temp >> 24));
+
+ /* check WPCT301 values - ignore RID */
+ if (memcmp(&temp, vid_did_rid_value,
+ sizeof(vid_did_rid_value))) {
+ dev_err(dev, "%s() WPCT301/NPCT501 not found\n",
+ __func__);
+ return -ENODEV;
+ }
+ }
+
+ strlcpy(info->type, "tpm_i2c", I2C_NAME_SIZE);
+ dev_info(dev, "tpm_i2c VID: %04X DID: %02X RID: %02X\n", (u16) temp,
+ (u8) (temp >> 16), (u8) (temp >> 24));
+
+ return 0;
+}
+
+static int tpm_i2c_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ int rc;
+ struct tpm_chip *chip;
+ struct device *dev = &client->dev;
+ void (*release) (struct device *dev) = dev->release;
+
+ chip = tpm_register_hardware(dev, &tpm_i2c);
+ if (!chip) {
+ dev_err(dev, "%s() error in tpm_register_hardware\n", __func__);
+ return -ENODEV;
+ }
+ /*
+ * restore dev->release function as it is modified by tpm_register_dev.
+ * save the assigned release function in chip->release for backup.
+ */
+ chip->release = dev->release;
+ dev->release = release;
+
+ /* Default timeouts */
+ chip->vendor.timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
+ chip->vendor.timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
+ chip->vendor.timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
+
+ /*
+ * I2C intfcaps (interrupt capabilitieis) are hard coded to:
+ * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
+ */
+
+ /* INTERRUPT Setup */
+ init_waitqueue_head(&chip->vendor.read_queue);
+ init_waitqueue_head(&chip->vendor.int_queue);
+
+ /* I2C intmask is hard coded to TPM_INTF_DATA_AVAIL_INT */
+ if (chip->vendor.irq) {
+ dev_dbg(dev, "%s() chip-vendor.irq\n", __func__);
+ rc = request_irq(chip->vendor.irq, tpm_i2c_int_handler,
+ IRQF_PROBE_SHARED, chip->vendor.miscdev.name,
+ chip);
+ if (rc) {
+ dev_err(dev, "%s() Unable to request irq: %d for use\n",
+ __func__, chip->vendor.irq);
+ chip->vendor.irq = 0;
+ } else {
+ /* Clear any pending interrupt */
+ tpm_i2c_ready(chip);
+ /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
+ rc = tpm_i2c_wait_for_stat(chip, TPM_STS_COMMAND_READY,
+ TPM_STS_COMMAND_READY,
+ chip->vendor.timeout_b,
+ NULL);
+ if (rc == 0) {
+ /*
+ * TIS is in ready state
+ * write dummy byte to enter reception state
+ * TPM_DATA_FIFO_W <- rc (0)
+ */
+ rc = tpm_i2c_write_buf(client, TPM_DATA_FIFO_W,
+ 1, (u8 *) (&rc));
+ if (rc < 0)
+ goto out_err;
+ /* TPM_STS <- 0x40 (commandReady) */
+ tpm_i2c_ready(chip);
+ } else {
+ /*
+ * timeout_b reached - command was
+ * aborted. TIS should now be in idle state -
+ * only TPM_STS_VALID should be set
+ */
+ if (tpm_i2c_read_status(chip) !=
+ TPM_STS_VALID) {
+ rc = -EIO;
+ goto out_err;
+ }
+ }
+ }
+ }
+
+ tpm_get_timeouts(chip);
+ tpm_continue_selftest(chip);
+
+ return 0;
+
+out_err:
+ tpm_remove_hardware(chip->dev);
+ return rc;
+}
+
+static int tpm_i2c_remove(struct i2c_client *client)
+{
+ struct device *dev = &(client->dev);
+ struct tpm_chip *chip = dev_get_drvdata(dev);
+
+ if (chip) {
+ tpm_dev_vendor_release(chip);
+ if (chip->vendor.irq)
+ free_irq(chip->vendor.irq, chip);
+ }
+ tpm_remove_hardware(dev);
+ kfree(chip);
+ return 0;
+}
+
+static int tpm_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
+{
+ return tpm_pm_suspend(&client->dev, mesg);
+}
+
+static int tpm_i2c_resume(struct i2c_client *client)
+{
+ return tpm_pm_resume(&client->dev);
+}
+
+static const struct i2c_device_id tpm_i2c_id[] = { {"tpm_i2c", 0}, {} };
+
+/* I2C Addresses to scan */
+static const u16 normal_i2c[] = { 0x17, 0x57, I2C_CLIENT_END };
+
+static struct i2c_driver tpm_i2c_driver = {
+ .driver = {
+ .name = "tpm_i2c",
+ },
+ .probe = tpm_i2c_probe,
+ .remove = tpm_i2c_remove,
+ .id_table = tpm_i2c_id,
+ .detect = tpm_i2c_detect,
+ .address_list = normal_i2c,
+ .suspend = tpm_i2c_suspend,
+ .resume = tpm_i2c_resume,
+ .class = UINT_MAX,
+};
+
+static int __init tpm_i2c_init(void)
+{
+ return i2c_add_driver(&tpm_i2c_driver);
+}
+
+static void __exit tpm_i2c_exit(void)
+{
+ i2c_del_driver(&tpm_i2c_driver);
+}
+
+module_init(tpm_i2c_init);
+module_exit(tpm_i2c_exit);
+
+MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
+MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
+MODULE_LICENSE("GPL");
===========================================================================================
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
ÿôèº{.nÇ+·®+%Ëÿ±éݶ\x17¥wÿº{.nÇ+·¥{±þG«éÿ{ayº\x1dÊÚë,j\a¢f£¢·hïêÿêçz_è®\x03(éÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?¨èÚ&£ø§~á¶iOæ¬z·vØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?I¥
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2011-09-07 16:40 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-14 9:31 Request to submit driver for Nuvoton TPM I2C device (WPCT301) Dan.Morav
2010-07-14 10:16 ` Ohad Ben-Cohen
2010-07-14 10:57 ` Marcel Selhorst
2010-07-14 15:25 ` Randy Dunlap
2010-08-09 9:24 ` [PATCH] TPM Nuvoton I2C driver, kernel 2.6.35 Dan.Morav
2010-08-17 12:59 ` Rajiv Andrade
2010-08-17 15:03 ` Wolfram Sang
2011-07-25 7:25 ` [PATCH] TPM Nuvoton I2C driver, kernel 3.0 Dan.Morav
2011-07-26 0:50 ` Ryan Mallon
2011-07-26 14:14 ` Wolfram Sang
2011-07-26 18:45 ` Stefan Berger
2011-08-17 11:11 ` Dan.Morav
2011-08-24 2:20 ` Ryan Mallon
2011-09-07 7:25 ` Dan.Morav
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.