Skip to content

Commit

Permalink
arch/arm64/lspi: improve spi initialization
Browse files Browse the repository at this point in the history
Hardware initialization is based refcount, not
spi enable bit and add interface to unitialize bus

Signed-off-by: Jouni Ukkonen <[email protected]>
  • Loading branch information
joukkone committed Nov 19, 2024
1 parent 7a149ae commit 7e85f5b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
102 changes: 53 additions & 49 deletions arch/arm64/src/imx9/imx9_lpspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ struct imx9_lpspidev_s
sem_t txsem; /* Wait for TX DMA to complete */
void *txbuf; /* Driver DMA safe buffer for TX */
void *rxbuf; /* Driver DMA safe buffer for RX */
int refcount; /* SPIn initialization counter */
#endif
};

Expand Down Expand Up @@ -1553,6 +1554,10 @@ static void imx9_lpspi_bus_initialize(struct imx9_lpspidev_s *priv)
{
uint32_t reg = 0;

/* Make sure bus is disabled */

imx9_lpspi_modifyreg32(priv, IMX9_LPSPI_CR_OFFSET, LPSPI_CR_MEN, 0);

/* Enable power and reset the peripheral */

imx9_lpspi_clock_enable(priv);
Expand Down Expand Up @@ -1802,8 +1807,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI1 pins: SCK, MISO, and MOSI */

Expand All @@ -1817,10 +1821,6 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI1_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI1_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
Expand All @@ -1834,8 +1834,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI2 pins: SCK, MISO, and MOSI */

Expand All @@ -1849,10 +1848,6 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI2_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI2_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
Expand All @@ -1866,8 +1861,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI3 pins: SCK, MISO, and MOSI */

Expand All @@ -1881,10 +1875,6 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI3_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI3_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
Expand All @@ -1898,8 +1888,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI4 pins: SCK, MISO, and MOSI */

Expand All @@ -1913,10 +1902,6 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI4_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI4_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
Expand All @@ -1930,8 +1915,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI5 pins: SCK, MISO, and MOSI */

Expand All @@ -1945,10 +1929,6 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI5_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI5_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
Expand All @@ -1962,8 +1942,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI6 pins: SCK, MISO, and MOSI */

Expand All @@ -1977,10 +1956,6 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI6_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI6_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
Expand All @@ -1994,8 +1969,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI7 pins: SCK, MISO, and MOSI */

Expand All @@ -2009,10 +1983,6 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI7_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI7_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
Expand All @@ -2026,8 +1996,7 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)

/* Only configure if the bus is not already configured */

if ((imx9_lpspi_getreg32(priv, IMX9_LPSPI_CR_OFFSET)
& LPSPI_CR_MEN) == 0)
if (priv->refcount == 0)
{
/* Configure SPI6 pins: SCK, MISO, and MOSI */

Expand All @@ -2041,18 +2010,25 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#if defined(GPIO_LPSPI8_DC) && defined(CONFIG_SPI_CMDDATA)
imx9_iomux_configure(GPIO_LPSPI8_DC);
#endif

/* Set up default configuration: Master, 8-bit, etc. */

imx9_lpspi_bus_initialize(priv);
}
}
else
#endif
{
leave_critical_section(flags);
spierr("ERROR: Unsupported SPI bus: %d\n", bus);
return NULL;
}

/* Set up default configuration: Master, 8-bit, etc. */

if (priv->refcount == 0)
{
imx9_lpspi_bus_initialize(priv);
}

priv->refcount++;

#ifdef CONFIG_IMX9_LPSPI_DMA
if (priv->rxch && priv->txch)
{
Expand Down Expand Up @@ -2084,8 +2060,36 @@ struct spi_dev_s *imx9_lpspibus_initialize(int bus)
#endif

leave_critical_section(flags);

return (struct spi_dev_s *)priv;
}

/****************************************************************************
* Name: imx9_lpspibus_uninitialize
*
* Description:
* Unitialize the selected SPI bus
*
* Input Parameters:
* dev - Device-specific state data
*
* Returned Value:
* None
*
****************************************************************************/

void imx9_lpspi_uninitialize(struct spi_dev_s *dev)
{
struct imx9_lpspidev_s *priv = (struct imx9_lpspidev_s *)dev;

if (priv->refcount > 0)
{
priv->refcount--;
if (priv->refcount == 0)
{
imx9_lpspi_modifyreg32(priv,
IMX9_LPSPI_CR_OFFSET, LPSPI_CR_MEN, 0);
}
}
}

#endif /* CONFIG_IMX9_LPSPI */
16 changes: 16 additions & 0 deletions arch/arm64/src/imx9/imx9_lpspi.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ struct spi_dev_s; /* Forward reference */

struct spi_dev_s *imx9_lpspibus_initialize(int bus);

/****************************************************************************
* Name: imx9_lpspibus_uninitialize
*
* Description:
* Unitialize the selected SPI bus if refcount is 1
*
* Input Parameters:
* dev - Device-specific state data
*
* Returned Value:
* None
*
****************************************************************************/

void imx9_lpspi_uninitialize(struct spi_dev_s *dev);

/****************************************************************************
* Name: imx9_lpspi1/2/...select and imx9_lpspi1/2/...status
*
Expand Down

0 comments on commit 7e85f5b

Please sign in to comment.