From: Andre Przywara <andre.przywara@arm.com>
To: Maksim Kiselev <bigunclemax@gmail.com>
Cc: Icenowy Zheng <icenowy@aosc.io>, Mark Brown <broonie@kernel.org>,
Rob Herring <robh+dt@kernel.org>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
Conor Dooley <conor+dt@kernel.org>, Chen-Yu Tsai <wens@csie.org>,
Jernej Skrabec <jernej.skrabec@gmail.com>,
Samuel Holland <samuel@sholland.org>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Cristian Ciocaltea <cristian.ciocaltea@collabora.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Maxime Ripard <mripard@kernel.org>,
linux-spi@vger.kernel.org, devicetree@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-riscv@lists.infradead.org
Subject: Re: [PATCH v3 3/5] spi: sun6i: add quirk for in-controller clock divider
Date: Sun, 7 May 2023 10:51:50 +0100 [thread overview]
Message-ID: <20230507105150.31643b87@slackpad.lan> (raw)
In-Reply-To: <20230506232616.1792109-4-bigunclemax@gmail.com>
On Sun, 7 May 2023 02:26:06 +0300
Maksim Kiselev <bigunclemax@gmail.com> wrote:
Hi Maksim,
thanks for the quick turnaround and the changes.
> Previously SPI controllers in Allwinner SoCs has a clock divider inside.
> However now the clock divider is removed and to set the transfer clock
> rate it's only needed to set the SPI module clock to the target value
> and configure a proper work mode.
>
> According to the datasheet there are three work modes:
>
> | SPI Sample Mode | SDM(bit13) | SDC(bit11) | Run Clock |
> |-------------------------|------------|------------|-----------|
> | normal sample | 1 | 0 | <= 24 MHz |
> | delay half cycle sample | 0 | 0 | <= 40 MHz |
> | delay one cycle sample | 0 | 1 | >= 80 MHz |
>
> Add a quirk for this kind of SPI controllers.
>
> Co-developed-by: Icenowy Zheng <icenowy@aosc.io>
> Signed-off-by: Maksim Kiselev <bigunclemax@gmail.com>
> ---
> drivers/spi/spi-sun6i.c | 92 +++++++++++++++++++++++++++--------------
> 1 file changed, 62 insertions(+), 30 deletions(-)
>
> diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
> index 01a01cd86db5..1e9e9a8159d9 100644
> --- a/drivers/spi/spi-sun6i.c
> +++ b/drivers/spi/spi-sun6i.c
> @@ -42,7 +42,9 @@
> #define SUN6I_TFR_CTL_CS_MANUAL BIT(6)
> #define SUN6I_TFR_CTL_CS_LEVEL BIT(7)
> #define SUN6I_TFR_CTL_DHB BIT(8)
> +#define SUN6I_TFR_CTL_SDC BIT(11)
> #define SUN6I_TFR_CTL_FBS BIT(12)
> +#define SUN6I_TFR_CTL_SDM BIT(13)
> #define SUN6I_TFR_CTL_XCH BIT(31)
>
> #define SUN6I_INT_CTL_REG 0x10
> @@ -87,6 +89,7 @@
>
> struct sun6i_spi_cfg {
> unsigned long fifo_depth;
> + bool has_clk_ctl;
> };
>
> struct sun6i_spi {
> @@ -260,7 +263,7 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
> struct spi_transfer *tfr)
> {
> struct sun6i_spi *sspi = spi_master_get_devdata(master);
> - unsigned int mclk_rate, div, div_cdr1, div_cdr2, timeout;
> + unsigned int div, div_cdr1, div_cdr2, timeout;
> unsigned int start, end, tx_time;
> unsigned int trig_level;
> unsigned int tx_len = 0, rx_len = 0;
> @@ -350,39 +353,66 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
>
> sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
>
> - /* Ensure that we have a parent clock fast enough */
> - mclk_rate = clk_get_rate(sspi->mclk);
> - if (mclk_rate < (2 * tfr->speed_hz)) {
> - clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
> - mclk_rate = clk_get_rate(sspi->mclk);
> - }
> + if (sspi->cfg->has_clk_ctl) {
> + unsigned int mclk_rate = clk_get_rate(sspi->mclk);
>
> - /*
> - * Setup clock divider.
> - *
> - * We have two choices there. Either we can use the clock
> - * divide rate 1, which is calculated thanks to this formula:
> - * SPI_CLK = MOD_CLK / (2 ^ cdr)
> - * Or we can use CDR2, which is calculated with the formula:
> - * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
> - * Wether we use the former or the latter is set through the
> - * DRS bit.
> - *
> - * First try CDR2, and if we can't reach the expected
> - * frequency, fall back to CDR1.
> - */
> - div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz);
> - div_cdr2 = DIV_ROUND_UP(div_cdr1, 2);
> - if (div_cdr2 <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
> - reg = SUN6I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN6I_CLK_CTL_DRS;
> - tfr->effective_speed_hz = mclk_rate / (2 * div_cdr2);
> + /* Ensure that we have a parent clock fast enough */
> + if (mclk_rate < (2 * tfr->speed_hz)) {
> + clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
> + mclk_rate = clk_get_rate(sspi->mclk);
> + }
> +
> + /*
> + * Setup clock divider.
> + *
> + * We have two choices there. Either we can use the clock
> + * divide rate 1, which is calculated thanks to this formula:
> + * SPI_CLK = MOD_CLK / (2 ^ cdr)
> + * Or we can use CDR2, which is calculated with the formula:
> + * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
> + * Whether we use the former or the latter is set through the
> + * DRS bit.
> + *
> + * First try CDR2, and if we can't reach the expected
> + * frequency, fall back to CDR1.
> + */
> + div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz);
> + div_cdr2 = DIV_ROUND_UP(div_cdr1, 2);
> + if (div_cdr2 <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
> + reg = SUN6I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN6I_CLK_CTL_DRS;
> + tfr->effective_speed_hz = mclk_rate / (2 * div_cdr2);
> + } else {
> + div = min(SUN6I_CLK_CTL_CDR1_MASK, order_base_2(div_cdr1));
> + reg = SUN6I_CLK_CTL_CDR1(div);
> + tfr->effective_speed_hz = mclk_rate / (1 << div);
> + }
> +
> + sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
> } else {
> - div = min(SUN6I_CLK_CTL_CDR1_MASK, order_base_2(div_cdr1));
> - reg = SUN6I_CLK_CTL_CDR1(div);
> - tfr->effective_speed_hz = mclk_rate / (1 << div);
> + clk_set_rate(sspi->mclk, tfr->speed_hz);
> + tfr->effective_speed_hz = clk_get_rate(sspi->mclk);
> +
> + /*
> + * Configure work mode.
> + *
> + * There are three work modes depending on the controller clock
> + * frequency:
> + * - normal sample mode : CLK <= 24MHz SDM=1 SDC=0
> + * - delay half-cycle sample mode : CLK <= 40MHz SDM=0 SDC=0
> + * - delay one-cycle sample mode : CLK >= 80MHz SDM=0 SDC=1
> + */
> + reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
> +
> + if (tfr->effective_speed_hz <= 24000000)
> + reg |= SUN6I_TFR_CTL_SDM;
> + else if (tfr->effective_speed_hz >= 80000000)
> + reg |= SUN6I_TFR_CTL_SDC;
This case assumes that the SDM bit is zero already. I think just
masking both bits off above, right after the read, is the easiest, then
you can also lose the else branch below entirely.
Cheers,
Andre
> + else
> + reg &= ~(SUN6I_TFR_CTL_SDM | SUN6I_TFR_CTL_SDC);
> +
> + sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
> }
>
> - 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;
> @@ -701,10 +731,12 @@ static void sun6i_spi_remove(struct platform_device *pdev)
>
> static const struct sun6i_spi_cfg sun6i_a31_spi_cfg = {
> .fifo_depth = SUN6I_FIFO_DEPTH,
> + .has_clk_ctl = true,
> };
>
> static const struct sun6i_spi_cfg sun8i_h3_spi_cfg = {
> .fifo_depth = SUN8I_FIFO_DEPTH,
> + .has_clk_ctl = true,
> };
>
> static const struct of_device_id sun6i_spi_match[] = {
next prev parent reply other threads:[~2023-05-07 9:52 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-06 23:26 [PATCH v3 0/5] Allwinner R329/D1/R528/T113s SPI support Maksim Kiselev
2023-05-06 23:26 ` [PATCH v3 1/5] dt-bindings: spi: sun6i: add DT bindings for Allwinner R329/D1/R528/T113s SPI Maksim Kiselev
2023-05-07 7:43 ` Krzysztof Kozlowski
[not found] ` <835082fe07b77db8598aebabe98a74c2c5ac47d1.camel@aosc.io>
2023-05-07 7:43 ` Krzysztof Kozlowski
2023-05-07 9:50 ` Andre Przywara
2023-05-06 23:26 ` [PATCH v3 2/5] spi: sun6i: change OF match data to a struct Maksim Kiselev
2023-05-08 9:47 ` David Laight
2023-05-10 8:33 ` Maxim Kiselev
2023-05-10 8:55 ` David Laight
2023-05-10 10:13 ` Andre Przywara
2023-05-06 23:26 ` [PATCH v3 3/5] spi: sun6i: add quirk for in-controller clock divider Maksim Kiselev
2023-05-07 9:51 ` Andre Przywara [this message]
2023-05-07 15:09 ` Maxim Kiselev
2023-05-06 23:26 ` [PATCH v3 4/5] spi: sun6i: add support for R329/D1/R528/T113s SPI controllers Maksim Kiselev
2023-05-07 9:52 ` Andre Przywara
2023-05-06 23:26 ` [PATCH v3 5/5] riscv: dts: allwinner: d1: Add SPI controllers node Maksim Kiselev
2023-05-07 9:52 ` Andre Przywara
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230507105150.31643b87@slackpad.lan \
--to=andre.przywara@arm.com \
--cc=aou@eecs.berkeley.edu \
--cc=bigunclemax@gmail.com \
--cc=broonie@kernel.org \
--cc=conor+dt@kernel.org \
--cc=cristian.ciocaltea@collabora.com \
--cc=devicetree@vger.kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=icenowy@aosc.io \
--cc=jernej.skrabec@gmail.com \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-spi@vger.kernel.org \
--cc=linux-sunxi@lists.linux.dev \
--cc=mripard@kernel.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=robh+dt@kernel.org \
--cc=samuel@sholland.org \
--cc=wens@csie.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox