linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] spi: atmel: Add GCLK support and simplify MR update
@ 2025-08-05 10:25 Manikandan Muralidharan
  2025-08-05 10:25 ` [PATCH v3 1/3] spi: atmel: simplify MR register update in cs_activate() Manikandan Muralidharan
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Manikandan Muralidharan @ 2025-08-05 10:25 UTC (permalink / raw)
  To: broonie, robh, krzk+dt, conor+dt, nicolas.ferre,
	alexandre.belloni, claudiu.beznea, ryan.wanner, tudor.ambarus,
	linux-spi, devicetree, linux-arm-kernel, linux-kernel
  Cc: manikandan.m

This patch series introduces support for using the Generic Clock (GCLK)
as a clock source in the Atmel SPI controller, and also includes a small
cleanup to simplify MR register handling in cs_activate().

On platforms like the SAM9X7 SoC, the SPI peripheral clock can run at
266 MHz. This can exceed the maximum value supported by the Serial Clock
Baud Rate (SCBR) divider, leading to SPI transfer failures. To address
this, the Atmel SPI controller can use an alternate clock source — the
Generic Clock (GCLK) — for SPCK generation.

--------
changes in v3:
- Add cover letter
- 2/3 - add explanation for the spi_gclk addition

changes in v2:
- 1/3 - Fixed mail threading
--------

Manikandan Muralidharan (3):
  spi: atmel: simplify MR register update in cs_activate()
  spi: dt-bindings: atmel,at91rm9200-spi: Add support for optional
    'spi_gclk' clock
  spi: atmel: Add support for handling GCLK as a clock source

 .../bindings/spi/atmel,at91rm9200-spi.yaml    | 11 ++-
 drivers/spi/spi-atmel.c                       | 78 ++++++++++++++-----
 2 files changed, 66 insertions(+), 23 deletions(-)


base-commit: 7e161a991ea71e6ec526abc8f40c6852ebe3d946
-- 
2.25.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/3] spi: atmel: simplify MR register update in cs_activate()
  2025-08-05 10:25 [PATCH v3 0/3] spi: atmel: Add GCLK support and simplify MR update Manikandan Muralidharan
@ 2025-08-05 10:25 ` Manikandan Muralidharan
  2025-08-05 10:25 ` [PATCH v3 2/3] spi: dt-bindings: atmel,at91rm9200-spi: Add support for optional 'spi_gclk' clock Manikandan Muralidharan
  2025-08-05 10:25 ` [PATCH v3 3/3] spi: atmel: Add support for handling GCLK as a clock source Manikandan Muralidharan
  2 siblings, 0 replies; 5+ messages in thread
From: Manikandan Muralidharan @ 2025-08-05 10:25 UTC (permalink / raw)
  To: broonie, robh, krzk+dt, conor+dt, nicolas.ferre,
	alexandre.belloni, claudiu.beznea, ryan.wanner, tudor.ambarus,
	linux-spi, devicetree, linux-arm-kernel, linux-kernel
  Cc: manikandan.m

simplified the MR register configuration by updating only the PCS field
using SPI_BFINS() instead of rewriting the entire register.
Avoids code duplication.

Signed-off-by: Manikandan Muralidharan <manikandan.m@microchip.com>
---
 drivers/spi/spi-atmel.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c
index 89a6b46cd319..409f544d8983 100644
--- a/drivers/spi/spi-atmel.c
+++ b/drivers/spi/spi-atmel.c
@@ -397,20 +397,10 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
 		 * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
 		 */
 		spi_writel(as, CSR0, asd->csr);
-		if (as->caps.has_wdrbt) {
-			spi_writel(as, MR,
-					SPI_BF(PCS, ~(0x01 << chip_select))
-					| SPI_BIT(WDRBT)
-					| SPI_BIT(MODFDIS)
-					| SPI_BIT(MSTR));
-		} else {
-			spi_writel(as, MR,
-					SPI_BF(PCS, ~(0x01 << chip_select))
-					| SPI_BIT(MODFDIS)
-					| SPI_BIT(MSTR));
-		}
 
 		mr = spi_readl(as, MR);
+		mr = SPI_BFINS(PCS, ~(0x01 << chip_select), mr);
+		spi_writel(as, MR, mr);
 
 		/*
 		 * Ensures the clock polarity is valid before we actually
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/3] spi: dt-bindings: atmel,at91rm9200-spi: Add support for optional 'spi_gclk' clock
  2025-08-05 10:25 [PATCH v3 0/3] spi: atmel: Add GCLK support and simplify MR update Manikandan Muralidharan
  2025-08-05 10:25 ` [PATCH v3 1/3] spi: atmel: simplify MR register update in cs_activate() Manikandan Muralidharan
@ 2025-08-05 10:25 ` Manikandan Muralidharan
  2025-08-05 18:00   ` Rob Herring
  2025-08-05 10:25 ` [PATCH v3 3/3] spi: atmel: Add support for handling GCLK as a clock source Manikandan Muralidharan
  2 siblings, 1 reply; 5+ messages in thread
From: Manikandan Muralidharan @ 2025-08-05 10:25 UTC (permalink / raw)
  To: broonie, robh, krzk+dt, conor+dt, nicolas.ferre,
	alexandre.belloni, claudiu.beznea, ryan.wanner, tudor.ambarus,
	linux-spi, devicetree, linux-arm-kernel, linux-kernel
  Cc: manikandan.m

The Atmel SPI controller supports both the peripheral clock and the
Generic Clock (GCLK) as sources for SPCK generation. On platforms like
the SAM9X7 SoC, the peripheral clock can reach frequencies up to
266 MHz, which may exceed the maximum value supported by the Serial
Clock Baud Rate (SCBR) divider, leading to SPI transfer failures. In such
cases, the GCLK can be used as an alternative source for SPCK generation"

This patch updates the Atmel SPI DT binding to support an optional
programmable SPI generic clock, specified as 'spi_gclk', in addition to
the required 'spi_clk'.

Signed-off-by: Manikandan Muralidharan <manikandan.m@microchip.com>
---
 .../devicetree/bindings/spi/atmel,at91rm9200-spi.yaml | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml b/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml
index d29772994cf5..11885d0cc209 100644
--- a/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml
+++ b/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml
@@ -31,11 +31,16 @@ properties:
     maxItems: 1
 
   clock-names:
-    contains:
-      const: spi_clk
+    items:
+      - const: spi_clk
+      - const: spi_gclk
+    minItems: 1
 
   clocks:
-    maxItems: 1
+    items:
+      - description: Peripheral Bus clock
+      - description: Programmable Generic clock
+    minItems: 1
 
   dmas:
     items:
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 3/3] spi: atmel: Add support for handling GCLK as a clock source
  2025-08-05 10:25 [PATCH v3 0/3] spi: atmel: Add GCLK support and simplify MR update Manikandan Muralidharan
  2025-08-05 10:25 ` [PATCH v3 1/3] spi: atmel: simplify MR register update in cs_activate() Manikandan Muralidharan
  2025-08-05 10:25 ` [PATCH v3 2/3] spi: dt-bindings: atmel,at91rm9200-spi: Add support for optional 'spi_gclk' clock Manikandan Muralidharan
@ 2025-08-05 10:25 ` Manikandan Muralidharan
  2 siblings, 0 replies; 5+ messages in thread
From: Manikandan Muralidharan @ 2025-08-05 10:25 UTC (permalink / raw)
  To: broonie, robh, krzk+dt, conor+dt, nicolas.ferre,
	alexandre.belloni, claudiu.beznea, ryan.wanner, tudor.ambarus,
	linux-spi, devicetree, linux-arm-kernel, linux-kernel
  Cc: manikandan.m

The SPI peripheral clock is typically used to derive the serial
clock (SPCK) via the FLEX_SPI_CSRx.SCBR field. However, on platforms
like the SAM9X7 SoC, where the peripheral clock can reach up to 266 MHz,
this may exceed the SCBR limit, causing SPI transfers to fail.
This patch adds support for using the SPI Generic Clock (GCLK) as an
alternative and more flexible clock source for SPCK generation.
The FLEX_SPI_MR.BRSRCCLK bit is updated accordingly to select between the
peripheral clock and GCLK.

Signed-off-by: Manikandan Muralidharan <manikandan.m@microchip.com>
---
 drivers/spi/spi-atmel.c | 64 +++++++++++++++++++++++++++++++++++------
 1 file changed, 56 insertions(+), 8 deletions(-)

diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c
index 409f544d8983..89977bff76d2 100644
--- a/drivers/spi/spi-atmel.c
+++ b/drivers/spi/spi-atmel.c
@@ -256,6 +256,7 @@ struct atmel_spi {
 	void __iomem		*regs;
 	int			irq;
 	struct clk		*clk;
+	struct clk		*gclk;
 	struct platform_device	*pdev;
 	unsigned long		spi_clk;
 
@@ -1480,6 +1481,8 @@ static void atmel_get_caps(struct atmel_spi *as)
 
 static void atmel_spi_init(struct atmel_spi *as)
 {
+	u32 mr = 0;
+
 	spi_writel(as, CR, SPI_BIT(SWRST));
 	spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
 
@@ -1487,12 +1490,17 @@ static void atmel_spi_init(struct atmel_spi *as)
 	if (as->fifo_size)
 		spi_writel(as, CR, SPI_BIT(FIFOEN));
 
-	if (as->caps.has_wdrbt) {
-		spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
-				| SPI_BIT(MSTR));
-	} else {
-		spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
-	}
+	/*
+	 * If GCLK is selected as the source clock for the bit rate generation
+	 * Enable the BRSRCCLK/FDIV/DIV32 bit
+	 */
+	if (as->gclk)
+		mr |= SPI_BIT(FDIV);
+
+	if (as->caps.has_wdrbt)
+		mr |= SPI_BIT(WDRBT);
+
+	spi_writel(as, MR, mr | SPI_BIT(MODFDIS) | SPI_BIT(MSTR));
 
 	if (as->use_pdc)
 		spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
@@ -1555,6 +1563,11 @@ static int atmel_spi_probe(struct platform_device *pdev)
 	as->phybase = regs->start;
 	as->irq = irq;
 	as->clk = clk;
+	as->gclk = devm_clk_get_optional(&pdev->dev, "spi_gclk");
+	if (IS_ERR(as->gclk)) {
+		ret = PTR_ERR(as->gclk);
+		goto out_unmap_regs;
+	}
 
 	init_completion(&as->xfer_completion);
 
@@ -1615,7 +1628,19 @@ static int atmel_spi_probe(struct platform_device *pdev)
 	if (ret)
 		goto out_free_irq;
 
-	as->spi_clk = clk_get_rate(clk);
+	/*
+	 * In cases where the peripheral clock is higher,the FLEX_SPI_CSRx.SCBR
+	 * exceeds the threshold (SCBR ≤ 255), the GCLK is used as the source clock
+	 * for the SPCK (SPI Serial Clock) bit rate generation
+	 */
+	if (as->gclk) {
+		ret = clk_prepare_enable(as->gclk);
+		if (ret)
+			goto out_disable_clk;
+		as->spi_clk = clk_get_rate(as->gclk);
+	} else {
+		as->spi_clk = clk_get_rate(clk);
+	}
 
 	as->fifo_size = 0;
 	if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
@@ -1650,6 +1675,8 @@ static int atmel_spi_probe(struct platform_device *pdev)
 
 	spi_writel(as, CR, SPI_BIT(SWRST));
 	spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
+	clk_disable_unprepare(as->gclk);
+out_disable_clk:
 	clk_disable_unprepare(clk);
 out_free_irq:
 out_unmap_regs:
@@ -1685,6 +1712,8 @@ static void atmel_spi_remove(struct platform_device *pdev)
 	spin_unlock_irq(&as->lock);
 
 	clk_disable_unprepare(as->clk);
+	if (as->gclk)
+		clk_disable_unprepare(as->gclk);
 
 	pm_runtime_put_noidle(&pdev->dev);
 	pm_runtime_disable(&pdev->dev);
@@ -1696,6 +1725,8 @@ static int atmel_spi_runtime_suspend(struct device *dev)
 	struct atmel_spi *as = spi_controller_get_devdata(host);
 
 	clk_disable_unprepare(as->clk);
+	if (as->gclk)
+		clk_disable_unprepare(as->gclk);
 	pinctrl_pm_select_sleep_state(dev);
 
 	return 0;
@@ -1705,10 +1736,20 @@ static int atmel_spi_runtime_resume(struct device *dev)
 {
 	struct spi_controller *host = dev_get_drvdata(dev);
 	struct atmel_spi *as = spi_controller_get_devdata(host);
+	int ret;
 
 	pinctrl_pm_select_default_state(dev);
 
-	return clk_prepare_enable(as->clk);
+	ret = clk_prepare_enable(as->clk);
+	if (ret)
+		return ret;
+	if (as->gclk) {
+		ret = clk_prepare_enable(as->gclk);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
 }
 
 static int atmel_spi_suspend(struct device *dev)
@@ -1736,10 +1777,17 @@ static int atmel_spi_resume(struct device *dev)
 	ret = clk_prepare_enable(as->clk);
 	if (ret)
 		return ret;
+	if (as->gclk) {
+		ret = clk_prepare_enable(as->gclk);
+		if (ret)
+			return ret;
+	}
 
 	atmel_spi_init(as);
 
 	clk_disable_unprepare(as->clk);
+	if (as->gclk)
+		clk_disable_unprepare(as->gclk);
 
 	if (!pm_runtime_suspended(dev)) {
 		ret = atmel_spi_runtime_resume(dev);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 2/3] spi: dt-bindings: atmel,at91rm9200-spi: Add support for optional 'spi_gclk' clock
  2025-08-05 10:25 ` [PATCH v3 2/3] spi: dt-bindings: atmel,at91rm9200-spi: Add support for optional 'spi_gclk' clock Manikandan Muralidharan
@ 2025-08-05 18:00   ` Rob Herring
  0 siblings, 0 replies; 5+ messages in thread
From: Rob Herring @ 2025-08-05 18:00 UTC (permalink / raw)
  To: Manikandan Muralidharan
  Cc: broonie, krzk+dt, conor+dt, nicolas.ferre, alexandre.belloni,
	claudiu.beznea, ryan.wanner, tudor.ambarus, linux-spi, devicetree,
	linux-arm-kernel, linux-kernel

On Tue, Aug 05, 2025 at 03:55:09PM +0530, Manikandan Muralidharan wrote:
> The Atmel SPI controller supports both the peripheral clock and the
> Generic Clock (GCLK) as sources for SPCK generation. On platforms like
> the SAM9X7 SoC, the peripheral clock can reach frequencies up to
> 266 MHz, which may exceed the maximum value supported by the Serial
> Clock Baud Rate (SCBR) divider, leading to SPI transfer failures. In such
> cases, the GCLK can be used as an alternative source for SPCK generation"
> 
> This patch updates the Atmel SPI DT binding to support an optional
> programmable SPI generic clock, specified as 'spi_gclk', in addition to
> the required 'spi_clk'.
> 
> Signed-off-by: Manikandan Muralidharan <manikandan.m@microchip.com>
> ---
>  .../devicetree/bindings/spi/atmel,at91rm9200-spi.yaml | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml b/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml
> index d29772994cf5..11885d0cc209 100644
> --- a/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml
> +++ b/Documentation/devicetree/bindings/spi/atmel,at91rm9200-spi.yaml
> @@ -31,11 +31,16 @@ properties:
>      maxItems: 1
>  
>    clock-names:
> -    contains:
> -      const: spi_clk
> +    items:
> +      - const: spi_clk
> +      - const: spi_gclk

Well, "spi_clk" was a terrible name as it is completely redundant as 
this is the "spi" block and everything in the list is a "clk". So don't 
continue it and just do "gclk".

> +    minItems: 1
>  
>    clocks:
> -    maxItems: 1
> +    items:
> +      - description: Peripheral Bus clock
> +      - description: Programmable Generic clock
> +    minItems: 1
>  
>    dmas:
>      items:
> -- 
> 2.25.1
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-05 18:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-05 10:25 [PATCH v3 0/3] spi: atmel: Add GCLK support and simplify MR update Manikandan Muralidharan
2025-08-05 10:25 ` [PATCH v3 1/3] spi: atmel: simplify MR register update in cs_activate() Manikandan Muralidharan
2025-08-05 10:25 ` [PATCH v3 2/3] spi: dt-bindings: atmel,at91rm9200-spi: Add support for optional 'spi_gclk' clock Manikandan Muralidharan
2025-08-05 18:00   ` Rob Herring
2025-08-05 10:25 ` [PATCH v3 3/3] spi: atmel: Add support for handling GCLK as a clock source Manikandan Muralidharan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).