diff mbox series

[RFC,1/2] tpm: tpm_tis_spi: Ensure SPI mode 0

Message ID 20240906060947.4844-2-wahrenst@gmx.net (mailing list archive)
State New
Headers show
Series tpm: Minor improvements | expand

Commit Message

Stefan Wahren Sept. 6, 2024, 6:09 a.m. UTC
According to TCG PC Client Platform TPM Profile (PTP) Specification
only SPI mode 0 is supported. In order to ensure the SPI controller
supports the necessary settings, call spi_setup() and bail out
as soon as possible in error case.

This change should affect all supported TPM SPI devices, because
tpm_tis_spi_probe is either called direct or indirectly.

Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
---
 drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

--
2.34.1

Comments

Jarkko Sakkinen Sept. 6, 2024, 9:47 a.m. UTC | #1
On Fri Sep 6, 2024 at 9:09 AM EEST, Stefan Wahren wrote:
> According to TCG PC Client Platform TPM Profile (PTP) Specification
> only SPI mode 0 is supported. In order to ensure the SPI controller
> supports the necessary settings, call spi_setup() and bail out
> as soon as possible in error case.
>
> This change should affect all supported TPM SPI devices, because
> tpm_tis_spi_probe is either called direct or indirectly.
>
> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
> ---
>  drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
>
> diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
> index 61b42c83ced8..e62d297b040e 100644
> --- a/drivers/char/tpm/tpm_tis_spi_main.c
> +++ b/drivers/char/tpm/tpm_tis_spi_main.c
> @@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
>  int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
>  		     int irq, const struct tpm_tis_phy_ops *phy_ops)
>  {
> +	int ret;
> +
> +	spi->mode &= ~SPI_MODE_X_MASK;
> +	spi->mode |= SPI_MODE_0;
> +
> +	ret = spi_setup(spi);
> +	if (ret < 0) {
> +		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
> +		return ret;
> +	}
> +
>  	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
>  	if (!phy->iobuf)
>  		return -ENOMEM;
> --
> 2.34.1


Why?

BR, Jarkko
Stefan Wahren Sept. 6, 2024, 2:46 p.m. UTC | #2
Am 06.09.24 um 11:47 schrieb Jarkko Sakkinen:
> On Fri Sep 6, 2024 at 9:09 AM EEST, Stefan Wahren wrote:
>> According to TCG PC Client Platform TPM Profile (PTP) Specification
>> only SPI mode 0 is supported. In order to ensure the SPI controller
>> supports the necessary settings, call spi_setup() and bail out
>> as soon as possible in error case.
>>
>> This change should affect all supported TPM SPI devices, because
>> tpm_tis_spi_probe is either called direct or indirectly.
>>
>> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
>> ---
>>   drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
>>   1 file changed, 11 insertions(+)
>>
>> diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
>> index 61b42c83ced8..e62d297b040e 100644
>> --- a/drivers/char/tpm/tpm_tis_spi_main.c
>> +++ b/drivers/char/tpm/tpm_tis_spi_main.c
>> @@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
>>   int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
>>   		     int irq, const struct tpm_tis_phy_ops *phy_ops)
>>   {
>> +	int ret;
>> +
>> +	spi->mode &= ~SPI_MODE_X_MASK;
>> +	spi->mode |= SPI_MODE_0;
>> +
>> +	ret = spi_setup(spi);
>> +	if (ret < 0) {
>> +		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
>> +		return ret;
>> +	}
>> +
>>   	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
>>   	if (!phy->iobuf)
>>   		return -ENOMEM;
>> --
>> 2.34.1
>
> Why?
SPI protocol driver usually call spi_setup to verify that the SPI
controller accept the settings (SPI mode, bit, clock rate ...). Bailing
out early is more helpful for debugging issues.
>
> BR, Jarkko
Jarkko Sakkinen Sept. 9, 2024, 5:05 p.m. UTC | #3
On Fri Sep 6, 2024 at 5:46 PM EEST, Stefan Wahren wrote:
> Am 06.09.24 um 11:47 schrieb Jarkko Sakkinen:
> > On Fri Sep 6, 2024 at 9:09 AM EEST, Stefan Wahren wrote:
> >> According to TCG PC Client Platform TPM Profile (PTP) Specification
> >> only SPI mode 0 is supported. In order to ensure the SPI controller
> >> supports the necessary settings, call spi_setup() and bail out
> >> as soon as possible in error case.
> >>
> >> This change should affect all supported TPM SPI devices, because
> >> tpm_tis_spi_probe is either called direct or indirectly.
> >>
> >> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
> >> ---
> >>   drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
> >>   1 file changed, 11 insertions(+)
> >>
> >> diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
> >> index 61b42c83ced8..e62d297b040e 100644
> >> --- a/drivers/char/tpm/tpm_tis_spi_main.c
> >> +++ b/drivers/char/tpm/tpm_tis_spi_main.c
> >> @@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
> >>   int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
> >>   		     int irq, const struct tpm_tis_phy_ops *phy_ops)
> >>   {
> >> +	int ret;
> >> +
> >> +	spi->mode &= ~SPI_MODE_X_MASK;
> >> +	spi->mode |= SPI_MODE_0;
> >> +
> >> +	ret = spi_setup(spi);
> >> +	if (ret < 0) {
> >> +		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
> >> +		return ret;
> >> +	}
> >> +
> >>   	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
> >>   	if (!phy->iobuf)
> >>   		return -ENOMEM;
> >> --
> >> 2.34.1
> >
> > Why?
> SPI protocol driver usually call spi_setup to verify that the SPI
> controller accept the settings (SPI mode, bit, clock rate ...). Bailing
> out early is more helpful for debugging issues.

What problem has this patch solved for you? I think it makes the code
only less robust and more error prone.

BR, Jarkko
Stefan Wahren Sept. 9, 2024, 5:16 p.m. UTC | #4
Hi Jarkko,

Am 09.09.24 um 19:05 schrieb Jarkko Sakkinen:
> On Fri Sep 6, 2024 at 5:46 PM EEST, Stefan Wahren wrote:
>> Am 06.09.24 um 11:47 schrieb Jarkko Sakkinen:
>>> On Fri Sep 6, 2024 at 9:09 AM EEST, Stefan Wahren wrote:
>>>> According to TCG PC Client Platform TPM Profile (PTP) Specification
>>>> only SPI mode 0 is supported. In order to ensure the SPI controller
>>>> supports the necessary settings, call spi_setup() and bail out
>>>> as soon as possible in error case.
>>>>
>>>> This change should affect all supported TPM SPI devices, because
>>>> tpm_tis_spi_probe is either called direct or indirectly.
>>>>
>>>> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
>>>> ---
>>>>    drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
>>>>    1 file changed, 11 insertions(+)
>>>>
>>>> diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
>>>> index 61b42c83ced8..e62d297b040e 100644
>>>> --- a/drivers/char/tpm/tpm_tis_spi_main.c
>>>> +++ b/drivers/char/tpm/tpm_tis_spi_main.c
>>>> @@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
>>>>    int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
>>>>    		     int irq, const struct tpm_tis_phy_ops *phy_ops)
>>>>    {
>>>> +	int ret;
>>>> +
>>>> +	spi->mode &= ~SPI_MODE_X_MASK;
>>>> +	spi->mode |= SPI_MODE_0;
>>>> +
>>>> +	ret = spi_setup(spi);
>>>> +	if (ret < 0) {
>>>> +		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
>>>> +		return ret;
>>>> +	}
>>>> +
>>>>    	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
>>>>    	if (!phy->iobuf)
>>>>    		return -ENOMEM;
>>>> --
>>>> 2.34.1
>>> Why?
>> SPI protocol driver usually call spi_setup to verify that the SPI
>> controller accept the settings (SPI mode, bit, clock rate ...). Bailing
>> out early is more helpful for debugging issues.
> What problem has this patch solved for you? I think it makes the code
> only less robust and more error prone.
this is not a fix for an issue i was experiencing.

It is just an improvement to catch possible invalid settings which has
been passed via DT device for example or the SPI controller doesn't
support SPI mode 0.
>
> BR, Jarkko
diff mbox series

Patch

diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c
index 61b42c83ced8..e62d297b040e 100644
--- a/drivers/char/tpm/tpm_tis_spi_main.c
+++ b/drivers/char/tpm/tpm_tis_spi_main.c
@@ -248,6 +248,17 @@  static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
+	int ret;
+
+	spi->mode &= ~SPI_MODE_X_MASK;
+	spi->mode |= SPI_MODE_0;
+
+	ret = spi_setup(spi);
+	if (ret < 0) {
+		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
+		return ret;
+	}
+
 	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
 	if (!phy->iobuf)
 		return -ENOMEM;