[U-Boot,v2,9/9] spi, mpc8xx: migrate to DM_SPI
diff mbox series

Message ID 75ce63553f69fdfb76f611e54f042ff692ca855d.1542749104.git.christophe.leroy@c-s.fr
State Accepted
Delegated to: Tom Rini
Headers show
Series
  • [U-Boot,v2,1/9] powerpc, mpc8xx: clear top of stack
Related show

Commit Message

Christophe Leroy Nov. 21, 2018, 8:51 a.m. UTC
Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
---
 drivers/spi/mpc8xx_spi.c | 179 ++++++++---------------------------------------
 1 file changed, 30 insertions(+), 149 deletions(-)

Comments

Jagan Teki Nov. 21, 2018, 9:09 a.m. UTC | #1
On Wed, Nov 21, 2018 at 2:21 PM Christophe Leroy
<christophe.leroy@c-s.fr> wrote:
>
> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
> ---
>  drivers/spi/mpc8xx_spi.c | 179 ++++++++---------------------------------------

Update Kconfig to move the config definition in DM_SPI area.

>
>         /* Setting tx bd status and data length */
> +       out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
>         out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
>         out_be16(&tbdf->cbd_datlen, count);
>
>         /* Setting rx bd status and data length */
> +       out_be32(&rbdf->cbd_bufaddr, (ulong)din);
>         out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
>         out_be16(&rbdf->cbd_datlen, 0);  /* rx length has no significance */
>
> @@ -333,3 +197,20 @@ ssize_t spi_xfer(size_t count)
>
>         return count;
>  }
> +
> +static const struct dm_spi_ops mpc8xx_spi_ops = {
> +       .xfer           = mpc8xx_spi_xfer,
> +};
> +
> +static const struct udevice_id mpc8xx_spi_ids[] = {
> +       { .compatible = "fsl,mpc8xx-spi" },

Is this binding from Linux?
Jagan Teki Nov. 21, 2018, 10:18 a.m. UTC | #2
On Wed, Nov 21, 2018 at 2:39 PM Jagan Teki <jagan@amarulasolutions.com> wrote:
>
> On Wed, Nov 21, 2018 at 2:21 PM Christophe Leroy
> <christophe.leroy@c-s.fr> wrote:
> >
> > Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
> > ---
> >  drivers/spi/mpc8xx_spi.c | 179 ++++++++---------------------------------------

How did you handle this eeprom calls?

cmd/built-in.o: In function `eeprom_init':
~/u-boot-dev/cmd/eeprom.c:71: undefined reference to `spi_init_f'
cmd/built-in.o: In function `eeprom_rw_block':
~/u-boot-dev/cmd/eeprom.c:135: undefined reference to `spi_read'
~/u-boot-dev/cmd/eeprom.c:137: undefined reference to `spi_write'
Christophe Leroy Nov. 29, 2018, 8:21 a.m. UTC | #3
Le 21/11/2018 à 11:18, Jagan Teki a écrit :
> On Wed, Nov 21, 2018 at 2:39 PM Jagan Teki <jagan@amarulasolutions.com> wrote:
>>
>> On Wed, Nov 21, 2018 at 2:21 PM Christophe Leroy
>> <christophe.leroy@c-s.fr> wrote:
>>>
>>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>>> ---
>>>   drivers/spi/mpc8xx_spi.c | 179 ++++++++---------------------------------------
> 
> How did you handle this eeprom calls?
> 
> cmd/built-in.o: In function `eeprom_init':
> ~/u-boot-dev/cmd/eeprom.c:71: undefined reference to `spi_init_f'
> cmd/built-in.o: In function `eeprom_rw_block':
> ~/u-boot-dev/cmd/eeprom.c:135: undefined reference to `spi_read'
> ~/u-boot-dev/cmd/eeprom.c:137: undefined reference to `spi_write'
> 

Oops, didn't see that. Indeed the HW I have doesn't have eeprom, I 
tested the SPI driver with thermal monitoring.

I see you sent a patch fixing it, thanks.

Christophe
Christophe Leroy Nov. 29, 2018, 8:23 a.m. UTC | #4
Le 21/11/2018 à 10:09, Jagan Teki a écrit :
> On Wed, Nov 21, 2018 at 2:21 PM Christophe Leroy
> <christophe.leroy@c-s.fr> wrote:
>>
>> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
>> ---
>>   drivers/spi/mpc8xx_spi.c | 179 ++++++++---------------------------------------
> 
> Update Kconfig to move the config definition in DM_SPI area.

You did it already it seems, thanks.

> 
>>
>>          /* Setting tx bd status and data length */
>> +       out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
>>          out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
>>          out_be16(&tbdf->cbd_datlen, count);
>>
>>          /* Setting rx bd status and data length */
>> +       out_be32(&rbdf->cbd_bufaddr, (ulong)din);
>>          out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
>>          out_be16(&rbdf->cbd_datlen, 0);  /* rx length has no significance */
>>
>> @@ -333,3 +197,20 @@ ssize_t spi_xfer(size_t count)
>>
>>          return count;
>>   }
>> +
>> +static const struct dm_spi_ops mpc8xx_spi_ops = {
>> +       .xfer           = mpc8xx_spi_xfer,
>> +};
>> +
>> +static const struct udevice_id mpc8xx_spi_ids[] = {
>> +       { .compatible = "fsl,mpc8xx-spi" },
> 
> Is this binding from Linux?
> 

No, on Linux we have a common driver for 8xx and most of 8xxx.
But the Uboot 8xxx SPI driver is too light to easily include 8xx so it 
was easier to port the existing 8xx driver to DM at the moment.

When the 8xxx implements QE mode SPI for the 83xx, then I'll be able to 
amend it to also support 8xx.

Christophe
Tom Rini Dec. 3, 2018, 10:50 p.m. UTC | #5
On Wed, Nov 21, 2018 at 08:51:57AM +0000, Christophe Leroy wrote:

> Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>

Applied to u-boot/master, thanks!

Patch
diff mbox series

diff --git a/drivers/spi/mpc8xx_spi.c b/drivers/spi/mpc8xx_spi.c
index 285fd4d2cc0..b020ce2b9d7 100644
--- a/drivers/spi/mpc8xx_spi.c
+++ b/drivers/spi/mpc8xx_spi.c
@@ -17,64 +17,19 @@ 
  */
 
 #include <common.h>
+#include <dm.h>
 #include <mpc8xx.h>
+#include <spi.h>
+
 #include <asm/cpm_8xx.h>
-#include <linux/ctype.h>
-#include <malloc.h>
-#include <post.h>
-#include <serial.h>
-
-#define SPI_EEPROM_WREN		0x06
-#define SPI_EEPROM_RDSR		0x05
-#define SPI_EEPROM_READ		0x03
-#define SPI_EEPROM_WRITE	0x02
-
-/* ---------------------------------------------------------------
- * Offset for initial SPI buffers in DPRAM:
- * We need a 520 byte scratch DPRAM area to use at an early stage.
- * It is used between the two initialization calls (spi_init_f()
- * and spi_init_r()).
- * The value 0xb00 makes it far enough from the start of the data
- * area (as well as from the stack pointer).
- * --------------------------------------------------------------- */
-#ifndef	CONFIG_SYS_SPI_INIT_OFFSET
-#define	CONFIG_SYS_SPI_INIT_OFFSET	0xB00
-#endif
+#include <asm/io.h>
 
 #define CPM_SPI_BASE_RX	CPM_SPI_BASE
 #define CPM_SPI_BASE_TX	(CPM_SPI_BASE + sizeof(cbd_t))
 
-/* -------------------
- * Function prototypes
- * ------------------- */
-ssize_t spi_xfer(size_t);
-
-/* -------------------
- * Variables
- * ------------------- */
-
 #define MAX_BUFFER	0x104
 
-/* ----------------------------------------------------------------------
- * Initially we place the RX and TX buffers at a fixed location in DPRAM!
- * ---------------------------------------------------------------------- */
-static uchar *rxbuf =
-	(uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
-			[CONFIG_SYS_SPI_INIT_OFFSET];
-static uchar *txbuf =
-	(uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
-			[CONFIG_SYS_SPI_INIT_OFFSET+MAX_BUFFER];
-
-/* **************************************************************************
- *
- *  Function:    spi_init_f
- *
- *  Description: Init SPI-Controller (ROM part)
- *
- *  return:      ---
- *
- * *********************************************************************** */
-void spi_init_f(void)
+static int mpc8xx_spi_probe(struct udevice *dev)
 {
 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
 	cpm8xx_t __iomem *cp = &immr->im_cpm;
@@ -180,117 +135,24 @@  void spi_init_f(void)
 	clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
 	clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
 
-	/* Set the bd's rx and tx buffer address pointers */
-	out_be32(&rbdf->cbd_bufaddr, (ulong)rxbuf);
-	out_be32(&tbdf->cbd_bufaddr, (ulong)txbuf);
-
 /* 10 + 11 */
 	out_8(&cp->cp_spim, 0);			/* Mask  all SPI events */
 	out_8(&cp->cp_spie, SPI_EMASK);		/* Clear all SPI events	*/
 
-	return;
+	return 0;
 }
 
-/* **************************************************************************
- *
- *  Function:    spi_init_r
- *
- *  Description: Init SPI-Controller (RAM part) -
- *		 The malloc engine is ready and we can move our buffers to
- *		 normal RAM
- *
- *  return:      ---
- *
- * *********************************************************************** */
-void spi_init_r(void)
+static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
+			    const void *dout, void *din, unsigned long flags)
 {
 	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
 	cpm8xx_t __iomem *cp = &immr->im_cpm;
-	spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
-	cbd_t __iomem *tbdf, *rbdf;
-
-	/* Disable relocation */
-	out_be16(&spi->spi_rpbase, 0);
-
-	/* tx and rx buffer descriptors */
-	tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
-	rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
-
-	/* Allocate memory for RX and TX buffers */
-	rxbuf = (uchar *)malloc(MAX_BUFFER);
-	txbuf = (uchar *)malloc(MAX_BUFFER);
-
-	out_be32(&rbdf->cbd_bufaddr, (ulong)rxbuf);
-	out_be32(&tbdf->cbd_bufaddr, (ulong)txbuf);
-
-	return;
-}
-
-/****************************************************************************
- *  Function:    spi_write
- **************************************************************************** */
-ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
-{
-	int i;
-
-	memset(rxbuf, 0, MAX_BUFFER);
-	memset(txbuf, 0, MAX_BUFFER);
-	*txbuf = SPI_EEPROM_WREN;		/* write enable		*/
-	spi_xfer(1);
-	memcpy(txbuf, addr, alen);
-	*txbuf = SPI_EEPROM_WRITE;		/* WRITE memory array	*/
-	memcpy(alen + txbuf, buffer, len);
-	spi_xfer(alen + len);
-						/* ignore received data	*/
-	for (i = 0; i < 1000; i++) {
-		*txbuf = SPI_EEPROM_RDSR;	/* read status		*/
-		txbuf[1] = 0;
-		spi_xfer(2);
-		if (!(rxbuf[1] & 1))
-			break;
-		udelay(1000);
-	}
-	if (i >= 1000)
-		printf("*** spi_write: Time out while writing!\n");
-
-	return len;
-}
-
-/****************************************************************************
- *  Function:    spi_read
- **************************************************************************** */
-ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
-{
-	memset(rxbuf, 0, MAX_BUFFER);
-	memset(txbuf, 0, MAX_BUFFER);
-	memcpy(txbuf, addr, alen);
-	*txbuf = SPI_EEPROM_READ;		/* READ memory array	*/
-
-	/*
-	 * There is a bug in 860T (?) that cuts the last byte of input
-	 * if we're reading into DPRAM. The solution we choose here is
-	 * to always read len+1 bytes (we have one extra byte at the
-	 * end of the buffer).
-	 */
-	spi_xfer(alen + len + 1);
-	memcpy(buffer, alen + rxbuf, len);
-
-	return len;
-}
-
-/****************************************************************************
- *  Function:    spi_xfer
- **************************************************************************** */
-ssize_t spi_xfer(size_t count)
-{
-	immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
-	cpm8xx_t __iomem *cp = &immr->im_cpm;
-	spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
 	cbd_t __iomem *tbdf, *rbdf;
 	int tm;
+	size_t count = (bitlen + 7) / 8;
 
-	/* Disable relocation */
-	out_be16(&spi->spi_rpbase, 0);
+	if (count > MAX_BUFFER)
+		return -EINVAL;
 
 	tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
 	rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
@@ -299,10 +161,12 @@  ssize_t spi_xfer(size_t count)
 	clrbits_be32(&cp->cp_pbdat, 0x0001);
 
 	/* Setting tx bd status and data length */
+	out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
 	out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
 	out_be16(&tbdf->cbd_datlen, count);
 
 	/* Setting rx bd status and data length */
+	out_be32(&rbdf->cbd_bufaddr, (ulong)din);
 	out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
 	out_be16(&rbdf->cbd_datlen, 0);	 /* rx length has no significance */
 
@@ -333,3 +197,20 @@  ssize_t spi_xfer(size_t count)
 
 	return count;
 }
+
+static const struct dm_spi_ops mpc8xx_spi_ops = {
+	.xfer		= mpc8xx_spi_xfer,
+};
+
+static const struct udevice_id mpc8xx_spi_ids[] = {
+	{ .compatible = "fsl,mpc8xx-spi" },
+	{ }
+};
+
+U_BOOT_DRIVER(mpc8xx_spi) = {
+	.name	= "mpc8xx_spi",
+	.id	= UCLASS_SPI,
+	.of_match = mpc8xx_spi_ids,
+	.ops	= &mpc8xx_spi_ops,
+	.probe	= mpc8xx_spi_probe,
+};