Skip to contentSkip to conversation

andes_spi: add andes_spi interface

PATCH New Macpaul Lin 3 participants 4 messages Apr 22, 2011 – Apr 23, 2011 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 andes_spi: add andes_spi interface

andes_spi is an spi interface developed by Andes Tech.

---
 drivers/spi/Makefile    |    1 +
 drivers/spi/andes_spi.c |  302 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/spi/andes_spi.h |  128 ++++++++++++++++++++
 3 files changed, 431 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/andes_spi.c
 create mode 100644 drivers/spi/andes_spi.h
drivers/spi/Makefile +1 -0
index d582fbb..8ee8f68 100644
@@ -26,6 +26,7 @@ include $(TOPDIR)/config.mk
 LIB	:= $(obj)libspi.o
 
 COBJS-$(CONFIG_ALTERA_SPI) += altera_spi.o
+COBJS-$(CONFIG_ANDES_SPI) += andes_spi.o
 COBJS-$(CONFIG_ATMEL_DATAFLASH_SPI) += atmel_dataflash_spi.o
 COBJS-$(CONFIG_ATMEL_SPI) += atmel_spi.o
 COBJS-$(CONFIG_BFIN_SPI) += bfin_spi.o
drivers/spi/andes_spi.c new file +302 -0

302 lines not on this page · raw message

drivers/spi/andes_spi.h new file +128 -0

128 lines not on this page · raw message

Signature · 1 line
1.7.3.5
#1 lore raw

PATCH 1/1 andes_spi: add andes_spi interface

drivers/spi/andes_spi.c line 37

Quoted 5 lines · Macpaul Lin
drivers/spi/andes_spi.c new file +5 -0
+#ifdef DEBUG
+#define debug(fmt, args...) ? ?printf(fmt, ##args)
+#else
+#define debug(fmt, args...)
+#endif

common.h already provides this for you

Review

Quoted 7 lines
(quoted diff) +7 -0
+void andes_spi_spit_en(struct andes_spi_slave *ds)
+{
+ ? ? ? debug("%s: dcr: %x, write value: %x\n",
+ ? ? ? ? ? ? ? ? ? ? ? __func__, readl(&ds->regs->dcr),
+ ? ? ? ? ? ? ? ? ? ? ? readl(&ds->regs->dcr) | ANDES_SPI_DCR_SPIT);
+ ? ? ? writel((readl(&ds->regs->dcr) | ANDES_SPI_DCR_SPIT), &ds->regs->dcr);
+}

putting io accessors into debug() is generally a bad idea. i'd suggest you store this in a local variable and then pass that to everything else. it'd also make the code cleaner. unsigned long dcr = readl(&ds->regs->dcr);

Quoted 6 lines
(quoted diff) +6 -0
+struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
+ ? ? ? ? ? ? ? ? ? ? ? unsigned int max_hz, unsigned int mode)
+{
+ ? ? ? struct andes_spi_slave ?*ds;
+
+ ? ? ? if (!spi_cs_is_valid(bus, cs))

Review

Quoted 10 lines
(quoted diff) +10 -0
+ ? ? ? ? ? ? ? return NULL;
+
+ ? ? ? ds = malloc(sizeof(*ds));
+ ? ? ? if (!ds)
+ ? ? ? ? ? ? ? return NULL;
+
+ ? ? ? ds->slave.bus = bus;
+ ? ? ? ds->slave.cs = cs;
+ ? ? ? ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE;
+ ? ? ? ds->freq = max_hz;

your spi controller has no frequency limit ? your spi_claim_bus indicates that there is ...

Review

Quoted 10 lines
(quoted diff) +9 -0
+static int andes_spi_read(struct spi_slave *slave, unsigned int len,
+ ? ? ? ? ? ? ? ? ? ? ? ? ? u8 *rxp, unsigned long flags)
+{
....
+ ? ? ? while (len > 0) {
+ ? ? ? ? ? ? ? debug(" ");
+ ? ? ? ? ? ? ? if (len / 4 > 0)
+ ? ? ? ? ? ? ? ? ? ? ? left = 4;
+ ? ? ? ? ? ? ? else
+ ? ? ? ? ? ? ? ? ? ? ? left = len;

seems like you want:
left = max(len, 4);

Quoted 8 lines

+ ? ? ? ? ? ? ? data = readl(&ds->regs->data);
+ ? ? ? ? ? ? ? for (i = 0; i < left; i++) {
+ ? ? ? ? ? ? ? ? ? ? ? debug("%02x ", data & 0xff);
+ ? ? ? ? ? ? ? ? ? ? ? *rxp++ = data;
+ ? ? ? ? ? ? ? ? ? ? ? data >>= 8;
+ ? ? ? ? ? ? ? ? ? ? ? len--;
+ ? ? ? ? ? ? ? }
+ ? ? ? }

looks to me like you write too much data ... if someone does a spi read of 1 byte, you still write out 4 ?

Quoted 6 lines
(quoted diff) +5 -0
+int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
+ ? ? ? ? ? ?const void *dout, void *din, unsigned long flags)
+{
...
+ ? ? ? /*
+ ? ? ? ?* It's not clear how non-8-bit-aligned transfers are supposed to be

Review

Quoted 10 lines
(quoted diff) +10 -0
+ ? ? ? ?* represented as a stream of bytes...this is a limitation of
+ ? ? ? ?* the current SPI interface - here we terminate on receiving such a
+ ? ? ? ?* transfer request.
+ ? ? ? ?*/
+
+ ? ? ? if (bitlen % 8) {
+ ? ? ? ? ? ? ? /* Errors always terminate an ongoing transfer */
+ ? ? ? ? ? ? ? flags |= SPI_XFER_END;
+ ? ? ? ? ? ? ? goto out;
+ ? ? ? }

this is correct behavior

drivers/spi/andes_spi.h

Quoted 2 lines
drivers/spi/andes_spi.h new file +0 -0

i cant think of any reason other code would want to include this ... so please put this header in drivers/spi/
-mike

#2 lore raw

PATCH 1/1 andes_spi: add andes_spi interface

Hi Mike,

Quoted 6 lines

+ ds->slave.bus = bus;

Quoted 3 lines
drivers/spi/andes_spi.c new file +3 -0
+       ds->slave.cs = cs;
+       ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE;
+       ds->freq = max_hz;

your spi controller has no frequency limit ? your spi_claim_bus

Quoted 3 lines

indicates that there is ...

Hey, it really strange but I couldn't help. ;p
According to the hardware datasheet, the only thing related to frequence is baudrate.
But, any value I set to baudrate was no affect.
It seems that something has done between the hardware itselves. I've asked the designer and he said the software coder should not care about this.

Quoted 5 lines
Quoted 5 lines
drivers/spi/andes_spi.c new file +4 -0
+static int andes_spi_read(struct spi_slave *slave, unsigned int len,
+                           u8 *rxp, unsigned long flags)
+{
....
+       while (len > 0) {
Quoted 9 lines

drivers/spi/andes_spi.c line 121

Quoted 5 lines
drivers/spi/andes_spi.c new file +5 -0
+               debug(" ");
+               if (len / 4 > 0)
+                       left = 4;
+               else
+                       left = len;

seems like you want:
left = max(len, 4);

No. Since the hardware is really designed so strange.
Although the data of SPI should be transfered byte-by-bytes. But this hardware requires you put the data 32bit (a register's length) once.
So, If you exactly have a data more then 4 bytes, you must have to divide them
into 4 bytes a chunk, and write them 4 bytes a time.
That's why left = max(len, 4) doesn't help. The value of left cannot larger than 4.

Quoted 9 lines

drivers/spi/andes_spi.c line 130

Quoted 8 lines
drivers/spi/andes_spi.c new file +8 -0
+               data = readl(&ds->regs->data);
+               for (i = 0; i < left; i++) {
+                       debug("%02x ", data & 0xff);
+                       *rxp++ = data;
+                       data >>= 8;
+                       len--;
+               }
+       }
Quoted 4 lines

looks to me like you write too much data ... if someone does a spi read of 1 byte, you still write out 4 ?

Same problem here. This SPI interface must read and write 4 byte a time. The hardware designer thinks they are really smart.
The hardware will strip the padding according to the data lenght in "WRITEthenREAD" command.

Thanks.
I'll fix the rest of those problem and send patch v2.

Signature · 2 lines
Best regards,
Macpaul Lin
#3 lore raw

PATCH 1/1 andes_spi: add andes_spi interface

Quoted 8 lines · Macpaul Lin
Quoted 7 lines
Quoted 4 lines

+ ? ? ? ds->slave.bus = bus;
+ ? ? ? ds->slave.cs = cs;
+ ? ? ? ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE;
+ ? ? ? ds->freq = max_hz;

your spi controller has no frequency limit ? ?your spi_claim_bus indicates that there is ...

Quoted 7 lines

Hey, it really strange but I couldn't help.? ;p
According to the hardware datasheet, the only thing related to frequence is baudrate.
But, any value I set to baudrate was no affect.
It seems that something has done between the hardware itselves. I've asked the designer and he said the software coder should not care about this.

unfortunately your designer is mistaken. software must be aware of it considering SPI slaves often have frequency limits, and board designs sometimes arent that great.

even looking at the baud setup code, there is math there that overflows.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
drivers/spi/andes_spi.c new file +4 -0
+static int andes_spi_read(struct spi_slave *slave, unsigned int len,
+ ? ? ? ? ? ? ? ? ? ? ? ? ? u8 *rxp, unsigned long flags)
+{
....
+ ? ? ? while (len > 0) {
Quoted 8 lines
Quoted 5 lines

+ ? ? ? ? ? ? ? debug(" ");
+ ? ? ? ? ? ? ? if (len / 4 > 0)
+ ? ? ? ? ? ? ? ? ? ? ? left = 4;
+ ? ? ? ? ? ? ? else
+ ? ? ? ? ? ? ? ? ? ? ? left = len;

seems like you want:
left = max(len, 4);

Quoted 1 line

No. Since the hardware is really designed so strange.

so you want:
left = min(len, 4);
-mike