2020-10-06 17:55:25

by Sagar Shrikant Kadam

[permalink] [raw]
Subject: [PATCH 0/1] fix i2c polling mode workaround for FU540-C000 SoC

The polling mode workaround for the FU540-C000 on HiFive Unleashed A00
board was added earlier. The logic for this seems to work only in case
the interrupt property was missing/not added into the i2c0 device node.

Here we address this issue by identifying the SOC based on compatibility
string and set the master xfer's to polling mode if it's the FU540-C000
SoC.

The fix has been tested on Linux 5.9.0-rc8 with a PMOD based RTCC sensor
connected to I2C pins J1 header of the board. Log for reference

# uname -a
Linux buildroot 5.9.0-rc8-00001-gf806864 #2 SMP Tue Oct 6 09:51:24 PDT 2020 riscv64 GNU/Linux
#
# i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 6f
70: -- -- -- -- -- -- -- --
# i2cget 0 0x57 0 b -y
0xa9
# i2cset 0 0x57 0 0xa5 b -y
# i2cget 0 0x57 0 b -y
0xa5
# i2cset 0 0x57 0 0x5a b -y
# i2cget 0 0x57 0 b -y
0x5a
# i2cget 0 0x6f 0 b -y
0x00
# i2cset 0 0x6f 0 0x5a b -y
# i2cget 0 0x6f 0 b -y
0x5a

Without the fix here, it's observed that "i2cdetect -y 0" turns the
system unresponsive, with CPU stall messages.

Sagar Shrikant Kadam (1):
i2c: ocores: fix polling mode workaround on FU540-C000 SoC

drivers/i2c/busses/i2c-ocores.c | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)

--
2.7.4


2020-10-06 17:56:17

by Sagar Shrikant Kadam

[permalink] [raw]
Subject: [PATCH 1/1] i2c: ocores: fix polling mode workaround on FU540-C000 SoC

The FU540-C000 has a broken IRQ and support was added earlier
so that it will operate in polling mode, but seems to work only
in case interrupts property is missing from the i2c0 dt-node.
This should not be the case and the driver should handle polling
mode with the interrupt property present in i2c0 node of the
device tree.
So check if it's the FU540-C000 soc and enable polling mode master
xfers, as the IRQ for this chip is broken.

Fixes commit c45d4ba86731 ("i2c: ocores: add polling mode workaround
for Sifive FU540-C000 SoC")

Signed-off-by: Sagar Shrikant Kadam <[email protected]>
---
drivers/i2c/busses/i2c-ocores.c | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c
index f5fc75b..4405244 100644
--- a/drivers/i2c/busses/i2c-ocores.c
+++ b/drivers/i2c/busses/i2c-ocores.c
@@ -686,17 +686,21 @@ static int ocores_i2c_probe(struct platform_device *pdev)

init_waitqueue_head(&i2c->wait);

+ /*
+ * Set OCORES_FLAG_BROKEN_IRQ to enable workaround for
+ * FU540-C000 SoC in polling mode.
+ * Since the SoC does have interrupt it's dt has the interrupt
+ * defined but it should be bypassed in driver as this SoC has
+ * a broken IRQ, hence update the master_xfer to use polling
+ * transfers.
+ */
+ match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
+ if (match && (long)match->data == TYPE_SIFIVE_REV0)
+ i2c->flags |= OCORES_FLAG_BROKEN_IRQ;
+
irq = platform_get_irq(pdev, 0);
- if (irq == -ENXIO) {
+ if (i2c->flags == OCORES_FLAG_BROKEN_IRQ || irq == -ENXIO) {
ocores_algorithm.master_xfer = ocores_xfer_polling;
-
- /*
- * Set in OCORES_FLAG_BROKEN_IRQ to enable workaround for
- * FU540-C000 SoC in polling mode.
- */
- match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
- if (match && (long)match->data == TYPE_SIFIVE_REV0)
- i2c->flags |= OCORES_FLAG_BROKEN_IRQ;
} else {
if (irq < 0)
return irq;
--
2.7.4

2020-10-07 17:08:22

by Peter Korsgaard

[permalink] [raw]
Subject: Re: [PATCH 1/1] i2c: ocores: fix polling mode workaround on FU540-C000 SoC

>>>>> "Sagar" == Sagar Shrikant Kadam <[email protected]> writes:

> The FU540-C000 has a broken IRQ and support was added earlier
> so that it will operate in polling mode, but seems to work only
> in case interrupts property is missing from the i2c0 dt-node.
> This should not be the case and the driver should handle polling
> mode with the interrupt property present in i2c0 node of the
> device tree.
> So check if it's the FU540-C000 soc and enable polling mode master
> xfers, as the IRQ for this chip is broken.

> Fixes commit c45d4ba86731 ("i2c: ocores: add polling mode workaround
> for Sifive FU540-C000 SoC")

> Signed-off-by: Sagar Shrikant Kadam <[email protected]>
> ---
> drivers/i2c/busses/i2c-ocores.c | 22 +++++++++++++---------
> 1 file changed, 13 insertions(+), 9 deletions(-)

> diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c
> index f5fc75b..4405244 100644
> --- a/drivers/i2c/busses/i2c-ocores.c
> +++ b/drivers/i2c/busses/i2c-ocores.c
> @@ -686,17 +686,21 @@ static int ocores_i2c_probe(struct platform_device *pdev)

> init_waitqueue_head(&i2c->wait);

> + /*
> + * Set OCORES_FLAG_BROKEN_IRQ to enable workaround for
> + * FU540-C000 SoC in polling mode.
> + * Since the SoC does have interrupt it's dt has the interrupt
> + * defined but it should be bypassed in driver as this SoC has
> + * a broken IRQ, hence update the master_xfer to use polling
> + * transfers.
> + */
> + match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
> + if (match && (long)match->data == TYPE_SIFIVE_REV0)
> + i2c->flags |= OCORES_FLAG_BROKEN_IRQ;
> +
> irq = platform_get_irq(pdev, 0);
> - if (irq == -ENXIO) {
> + if (i2c->flags == OCORES_FLAG_BROKEN_IRQ || irq == -ENXIO) {

NIT: flags is a bitmask, so i2c->flags & OCORES_FLAG_BROKEN_IRQ would be
better, even if there currently doesn't exist any other flags.

TYPE_SIFIVE_REV0 is also set for two compatibles:

{
.compatible = "sifive,fu540-c000-i2c",
.data = (void *)TYPE_SIFIVE_REV0,
},
{
.compatible = "sifive,i2c0",
.data = (void *)TYPE_SIFIVE_REV0,
},

Are both affected by this issue? if not, we will need to extend the code
to handle them differently.

Other than that, it looks OK to me.

--
Bye, Peter Korsgaard

2020-10-08 13:57:36

by Sagar Kadam

[permalink] [raw]
Subject: RE: [PATCH 1/1] i2c: ocores: fix polling mode workaround on FU540-C000 SoC

Hello Peter,

> -----Original Message-----
> From: Peter Korsgaard <[email protected]> On Behalf Of Peter Korsgaard
> Sent: Wednesday, October 7, 2020 5:10 PM
> To: Sagar Kadam <[email protected]>
> Cc: [email protected]; [email protected]; linux-
> [email protected]; [email protected]; Paul Walmsley ( Sifive)
> <[email protected]>; [email protected];
> [email protected]
> Subject: Re: [PATCH 1/1] i2c: ocores: fix polling mode workaround on FU540-
> C000 SoC
>
> [External Email] Do not click links or attachments unless you recognize the
> sender and know the content is safe
>
> >>>>> "Sagar" == Sagar Shrikant Kadam <[email protected]> writes:
>
> > The FU540-C000 has a broken IRQ and support was added earlier
> > so that it will operate in polling mode, but seems to work only
> > in case interrupts property is missing from the i2c0 dt-node.
> > This should not be the case and the driver should handle polling
> > mode with the interrupt property present in i2c0 node of the
> > device tree.
> > So check if it's the FU540-C000 soc and enable polling mode master
> > xfers, as the IRQ for this chip is broken.
>
> > Fixes commit c45d4ba86731 ("i2c: ocores: add polling mode workaround
> > for Sifive FU540-C000 SoC")
>
> > Signed-off-by: Sagar Shrikant Kadam <[email protected]>
> > ---
> > drivers/i2c/busses/i2c-ocores.c | 22 +++++++++++++---------
> > 1 file changed, 13 insertions(+), 9 deletions(-)
>
> > diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-
> ocores.c
> > index f5fc75b..4405244 100644
> > --- a/drivers/i2c/busses/i2c-ocores.c
> > +++ b/drivers/i2c/busses/i2c-ocores.c
> > @@ -686,17 +686,21 @@ static int ocores_i2c_probe(struct
> platform_device *pdev)
>
> > init_waitqueue_head(&i2c->wait);
>
> > + /*
> > + * Set OCORES_FLAG_BROKEN_IRQ to enable workaround for
> > + * FU540-C000 SoC in polling mode.
> > + * Since the SoC does have interrupt it's dt has the interrupt
> > + * defined but it should be bypassed in driver as this SoC has
> > + * a broken IRQ, hence update the master_xfer to use polling
> > + * transfers.
> > + */
> > + match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
> > + if (match && (long)match->data == TYPE_SIFIVE_REV0)
> > + i2c->flags |= OCORES_FLAG_BROKEN_IRQ;
> > +
> > irq = platform_get_irq(pdev, 0);
> > - if (irq == -ENXIO) {
> > + if (i2c->flags == OCORES_FLAG_BROKEN_IRQ || irq == -ENXIO) {
>
> NIT: flags is a bitmask, so i2c->flags & OCORES_FLAG_BROKEN_IRQ would be
> better, even if there currently doesn't exist any other flags.
>
Thanks for your suggestions. I will apply the bitmask, this will be better.

> TYPE_SIFIVE_REV0 is also set for two compatibles:
>
> {
> .compatible = "sifive,fu540-c000-i2c",
> .data = (void *)TYPE_SIFIVE_REV0,
> },
> {
> .compatible = "sifive,i2c0",
> .data = (void *)TYPE_SIFIVE_REV0,
> },
>
> Are both affected by this issue? if not, we will need to extend the code
> to handle them differently.
>

No, this issue is present in FU540-C000 SoC i.e SoC- specific, and so I can check
for the soc-compatible string as follows:

- match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
- if (match && (long)match->data == TYPE_SIFIVE_REV0)
+ if (of_device_is_compatible(pdev->dev.of_node,
+ "sifive,fu540-c000-i2c"))

Please let me know if this is okay.

Thanks & BR,
Sagar

> Other than that, it looks OK to me.
>
> --
> Bye, Peter Korsgaard

2020-10-08 16:07:03

by Peter Korsgaard

[permalink] [raw]
Subject: Re: [PATCH 1/1] i2c: ocores: fix polling mode workaround on FU540-C000 SoC

>>>>> "Sagar" == Sagar Kadam <[email protected]> writes:

> Hello Peter,
>> Are both affected by this issue? if not, we will need to extend the code
>> to handle them differently.
>>

> No, this issue is present in FU540-C000 SoC i.e SoC- specific, and so I can check
> for the soc-compatible string as follows:

> - match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
> - if (match && (long)match->data == TYPE_SIFIVE_REV0)
> + if (of_device_is_compatible(pdev->dev.of_node,
> + "sifive,fu540-c000-i2c"))

> Please let me know if this is okay.

Yes, that sounds sensible. Thanks.

--
Bye, Peter Korsgaard