2021-05-20 10:16:26

by Ralf Schlatterbeck

[permalink] [raw]
Subject: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

The current sun6i SPI implementation initializes the transfer too early,
resulting in SCK going high before the transfer. When using an additional
(gpio) chipselect with sun6i, the chipselect is asserted at a time when
clock is high, making the SPI transfer fail.

This is due to SUN6I_GBL_CTL_BUS_ENABLE being written into
SUN6I_GBL_CTL_REG at an early stage. Moving that to the transfer
function, hence, right before the transfer starts, mitigates that
problem.

Signed-off-by: Ralf Schlatterbeck <[email protected]>
Signed-off-by: Mirko Vogt <mirko-dev|[email protected]>
---
For oscilloscope screenshots with/without the patch, see my blog post
https://blog.runtux.com/posts/2019/04/18/
or the discussion in the armbian forum at
https://forum.armbian.com/topic/4330-spi-gpio-chip-select-support/
(my logo there is a penguin).

drivers/spi/spi-sun6i.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
index cc8401980125..2db075c87f51 100644
--- a/drivers/spi/spi-sun6i.c
+++ b/drivers/spi/spi-sun6i.c
@@ -379,6 +379,10 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
}

sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
+ /* Finally enable the bus - doing so before might raise SCK to HIGH */
+ sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
+ sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG)
+ | SUN6I_GBL_CTL_BUS_ENABLE);

/* Setup the transfer now... */
if (sspi->tx_buf)
@@ -504,7 +508,7 @@ static int sun6i_spi_runtime_resume(struct device *dev)
}

sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
- SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
+ SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);

return 0;

--
2.20.1


2021-05-21 20:21:55

by Andre Przywara

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

On Thu, 20 May 2021 12:06:56 +0200
Ralf Schlatterbeck <[email protected]> wrote:

Hi Ralf,

many thanks for taking care of upstreaming this!

> The current sun6i SPI implementation initializes the transfer too early,
> resulting in SCK going high before the transfer. When using an additional
> (gpio) chipselect with sun6i, the chipselect is asserted at a time when
> clock is high, making the SPI transfer fail.
>
> This is due to SUN6I_GBL_CTL_BUS_ENABLE being written into
> SUN6I_GBL_CTL_REG at an early stage. Moving that to the transfer
> function, hence, right before the transfer starts, mitigates that
> problem.
>
> Signed-off-by: Ralf Schlatterbeck <[email protected]>
> Signed-off-by: Mirko Vogt <mirko-dev|[email protected]>

So if I get your last email correctly, your intention was to make this
Mirko's patch, which you just send?
In this case the authorship should stay with him (git commit --amend
--author "..."), and you add your S-o-b (his first, your's next).
If you use git format-patch & git send-email, it will take care of
formatting this correctly (inserting a "From" line into the body).

As for the technical part: I know as much about SPI to have quite some
respect for the subtleties of the various modes. But your second
oscilloscope shot looks much better, and matches the timing diagram in
the manual (SS is the first to go down).
So I trust you that's the right move.

One formatting thing below:

> ---
> For oscilloscope screenshots with/without the patch, see my blog post
> https://blog.runtux.com/posts/2019/04/18/
> or the discussion in the armbian forum at
> https://forum.armbian.com/topic/4330-spi-gpio-chip-select-support/
> (my logo there is a penguin).
>
> drivers/spi/spi-sun6i.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
> index cc8401980125..2db075c87f51 100644
> --- a/drivers/spi/spi-sun6i.c
> +++ b/drivers/spi/spi-sun6i.c
> @@ -379,6 +379,10 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
> }
>
> sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
> + /* Finally enable the bus - doing so before might raise SCK to HIGH */
> + sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
> + sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG)
> + | SUN6I_GBL_CTL_BUS_ENABLE);

The "|" on the new line looks a bit odd. What about you utilise "reg"
here, as the other access do, to make this more readable?
reg = sun6i_spi_read(...);
reg |= ...
sun6i_spi_write(..., reg);

Cheers,
Andre

>
> /* Setup the transfer now... */
> if (sspi->tx_buf)
> @@ -504,7 +508,7 @@ static int sun6i_spi_runtime_resume(struct device *dev)
> }
>
> sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
> - SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
> + SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
>
> return 0;
>

2021-05-21 20:30:49

by Ralf Schlatterbeck

[permalink] [raw]
Subject: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

From: Mirko Vogt <mirko-dev|[email protected]>

The current sun6i SPI implementation initializes the transfer too early,
resulting in SCK going high before the transfer. When using an additional
(gpio) chipselect with sun6i, the chipselect is asserted at a time when
clock is high, making the SPI transfer fail.

This is due to SUN6I_GBL_CTL_BUS_ENABLE being written into
SUN6I_GBL_CTL_REG at an early stage. Moving that to the transfer
function, hence, right before the transfer starts, mitigates that
problem.

Signed-off-by: Mirko Vogt <mirko-dev|[email protected]>
Signed-off-by: Ralf Schlatterbeck <[email protected]>
---
Updated patch with suggested improvements by Andre Przywara
For oscilloscope screenshots with/without the patch, see my blog post
https://blog.runtux.com/posts/2019/04/18/
or the discussion in the armbian forum at
https://forum.armbian.com/topic/4330-spi-gpio-chip-select-support/
(my logo there is a penguin).

drivers/spi/spi-sun6i.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
index cc8401980125..23ad052528db 100644
--- a/drivers/spi/spi-sun6i.c
+++ b/drivers/spi/spi-sun6i.c
@@ -379,6 +379,10 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
}

sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
+ /* Finally enable the bus - doing so before might raise SCK to HIGH */
+ reg = sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG);
+ reg |= SUN6I_GBL_CTL_BUS_ENABLE;
+ sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG, reg);

/* Setup the transfer now... */
if (sspi->tx_buf)
@@ -504,7 +508,7 @@ static int sun6i_spi_runtime_resume(struct device *dev)
}

sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
- SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
+ SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);

return 0;

--
2.20.1

2021-05-21 20:31:49

by Ralf Schlatterbeck

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

On Fri, May 21, 2021 at 05:30:11PM +0100, Andre Przywara wrote:
> On Thu, 20 May 2021 12:06:56 +0200
> Ralf Schlatterbeck <[email protected]> wrote:
> So if I get your last email correctly, your intention was to make this
> Mirko's patch, which you just send?

Yes.

> In this case the authorship should stay with him (git commit --amend
> --author "..."), and you add your S-o-b (his first, your's next).
> If you use git format-patch & git send-email, it will take care of
> formatting this correctly (inserting a "From" line into the body).

I was using format-patch but editing the result. Would a 'Date' line in
the body also be OK? The patch is really from 2019...

I have it correctly in my git but was unaware that additional header
lines in the body are ok.

> One formatting thing below:
> > sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
> > + /* Finally enable the bus - doing so before might raise SCK to HIGH */
> > + sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
> > + sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG)
> > + | SUN6I_GBL_CTL_BUS_ENABLE);
>
> The "|" on the new line looks a bit odd. What about you utilise "reg"
> here, as the other access do, to make this more readable?
> reg = sun6i_spi_read(...);
> reg |= ...
> sun6i_spi_write(..., reg);

I've fixed this and will resubmit.

Thanks,
Ralf

2021-05-24 12:34:45

by Andre Przywara

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

On Fri, 21 May 2021 22:19:13 +0200
Ralf Schlatterbeck <[email protected]> wrote:

Hi,

> From: Mirko Vogt <mirko-dev|[email protected]>
>
> The current sun6i SPI implementation initializes the transfer too early,
> resulting in SCK going high before the transfer. When using an additional
> (gpio) chipselect with sun6i, the chipselect is asserted at a time when
> clock is high, making the SPI transfer fail.
>
> This is due to SUN6I_GBL_CTL_BUS_ENABLE being written into
> SUN6I_GBL_CTL_REG at an early stage. Moving that to the transfer
> function, hence, right before the transfer starts, mitigates that
> problem.
>
> Signed-off-by: Mirko Vogt <mirko-dev|[email protected]>
> Signed-off-by: Ralf Schlatterbeck <[email protected]>

Looks good to me now, thanks for the changes!

Reviewed-by: Andre Przywara <[email protected]>

Some comments for future contributions (should not hold back that
patch, I think):
- Single patch set series don't bother to have a "1/1" after the
"PATCH".
- You are expected to increase the version number when you send a new
version, to show that *this* is better than the previous post and
this version should be merged. Otherwise the maintainer might pick
the wrong version. "git format-patch -v2" and "git send-email" will
automatically take care of this.

> ---
> Updated patch with suggested improvements by Andre Przywara
> For oscilloscope screenshots with/without the patch, see my blog post
> https://blog.runtux.com/posts/2019/04/18/
> or the discussion in the armbian forum at
> https://forum.armbian.com/topic/4330-spi-gpio-chip-select-support/
> (my logo there is a penguin).

Please keep in mind that text after the dashes doesn't make it in it
repo, so this information would be lost there. Also, in general links in
commit messages are somewhat frowned upon, since they tend to 404
sooner or later. So ideally you can put a condensed version of your
findings into the commit message? Don't worry if it grows long, it is
not uncommon to have a 2 page commit message for a one-liner patch.

Cheers,
Andre



>
> drivers/spi/spi-sun6i.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
> index cc8401980125..23ad052528db 100644
> --- a/drivers/spi/spi-sun6i.c
> +++ b/drivers/spi/spi-sun6i.c
> @@ -379,6 +379,10 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
> }
>
> sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
> + /* Finally enable the bus - doing so before might raise SCK to HIGH */
> + reg = sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG);
> + reg |= SUN6I_GBL_CTL_BUS_ENABLE;
> + sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG, reg);
>
> /* Setup the transfer now... */
> if (sspi->tx_buf)
> @@ -504,7 +508,7 @@ static int sun6i_spi_runtime_resume(struct device *dev)
> }
>
> sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
> - SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
> + SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
>
> return 0;
>

2021-05-24 16:26:35

by Ralf Schlatterbeck

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

On Mon, May 24, 2021 at 01:33:01PM +0100, Andre Przywara wrote:
> - Single patch set series don't bother to have a "1/1" after the
> "PATCH".
> - You are expected to increase the version number when you send a new
> version, to show that *this* is better than the previous post and
> this version should be merged. Otherwise the maintainer might pick
> the wrong version. "git format-patch -v2" and "git send-email" will
> automatically take care of this.

OK I'll keep that in mind.

> Please keep in mind that text after the dashes doesn't make it in it
> repo, so this information would be lost there. Also, in general links in
> commit messages are somewhat frowned upon, since they tend to 404
> sooner or later. So ideally you can put a condensed version of your
> findings into the commit message? Don't worry if it grows long, it is
> not uncommon to have a 2 page commit message for a one-liner patch.

This was intentional, the commit message is only the upper part.
The Links were meant to give a lot of details why the patch fixes
something. I think the commit message already explains the condensed
findings. I've put this additional explanation text after the '---' to
not need an additional Patch 0/1 email ;-)

Thanks!
Ralf
--
Dr. Ralf Schlatterbeck Tel: +43/2243/26465-16
Open Source Consulting www: http://www.runtux.com
Reichergasse 131, A-3411 Weidling email: [email protected]

2021-05-25 14:39:11

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

On Fri, May 21, 2021 at 10:19:13PM +0200, Ralf Schlatterbeck wrote:
> From: Mirko Vogt <mirko-dev|[email protected]>
>
> The current sun6i SPI implementation initializes the transfer too early,
> resulting in SCK going high before the transfer. When using an additional
> (gpio) chipselect with sun6i, the chipselect is asserted at a time when
> clock is high, making the SPI transfer fail.

Please don't send new patches in reply to old threads, this makes
it hard to follow what is going on and creates confusion as to
what the current version of things is and means things can easily
get missed if the earlier thread gets discarded - this goes along
with the thing Andre mentioned about flagging the version of the
patch in the subject line.


Attachments:
(No filename) (756.00 B)
signature.asc (499.00 B)
Download all attachments

2021-05-27 20:48:34

by Ralf Schlatterbeck

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

On Tue, May 25, 2021 at 03:35:51PM +0100, Mark Brown wrote:
> On Fri, May 21, 2021 at 10:19:13PM +0200, Ralf Schlatterbeck wrote:
> > From: Mirko Vogt <mirko-dev|[email protected]>
> >
> > The current sun6i SPI implementation initializes the transfer too early,
> > resulting in SCK going high before the transfer. When using an additional
> > (gpio) chipselect with sun6i, the chipselect is asserted at a time when
> > clock is high, making the SPI transfer fail.
>
> Please don't send new patches in reply to old threads, this makes
> it hard to follow what is going on and creates confusion as to
> what the current version of things is and means things can easily
> get missed if the earlier thread gets discarded - this goes along
> with the thing Andre mentioned about flagging the version of the
> patch in the subject line.

OK, let me know if I should repost in new thread with increased version
number in the subject.

Thanks,
Ralf
--
Dr. Ralf Schlatterbeck Tel: +43/2243/26465-16
Open Source Consulting www: http://www.runtux.com
Reichergasse 131, A-3411 Weidling email: [email protected]

2021-06-05 14:23:16

by Mirko Vogt

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

On 5/27/21 1:39 PM, Ralf Schlatterbeck wrote
> OK, let me know if I should repost in new thread with increased version
> number in the subjec
Assuming this wasn't answered yet: was this just a comment for further
patches and things are going their way or should it indeed be resent as
new thread/version with respective changes applied?
Can I do something to help / resolve this?

Have a nice weekend

mirko

2021-06-14 14:51:02

by Ralf Schlatterbeck

[permalink] [raw]
Subject: Re: [PATCH 1/1] spi-sun6i: Fix chipselect/clock bug

I've reposted as v2 in a new thread.
--
Ralf Schlatterbeck