]> git.karo-electronics.de Git - karo-tx-uboot.git/blobdiff - cpu/nios/spi.c
imported U-Boot Release 2009.08 from Freescale BSP L2.6.31_10.08.01
[karo-tx-uboot.git] / cpu / nios / spi.c
index f37146b7939b75093376f8f86755015cd3a53d1a..89f9797faa23c193607f99b7f47c656e1ed1d1da 100755 (executable)
 #include <nios-io.h>
 #include <spi.h>
 
-#if !defined(CFG_NIOS_SPIBASE)
-#error "*** CFG_NIOS_SPIBASE not defined ***"
+#if !defined(CONFIG_SYS_NIOS_SPIBASE)
+#error "*** CONFIG_SYS_NIOS_SPIBASE not defined ***"
 #endif
 
-#if !defined(CFG_NIOS_SPIBITS)
-#error "*** CFG_NIOS_SPIBITS not defined ***"
+#if !defined(CONFIG_SYS_NIOS_SPIBITS)
+#error "*** CONFIG_SYS_NIOS_SPIBITS not defined ***"
 #endif
 
-#if (CFG_NIOS_SPIBITS != 8) && (CFG_NIOS_SPIBITS != 16)
-#error "*** CFG_NIOS_SPIBITS should be either 8 or 16 ***"
+#if (CONFIG_SYS_NIOS_SPIBITS != 8) && (CONFIG_SYS_NIOS_SPIBITS != 16)
+#error "*** CONFIG_SYS_NIOS_SPIBITS should be either 8 or 16 ***"
 #endif
 
-static nios_spi_t      *spi    = (nios_spi_t *)CFG_NIOS_SPIBASE;
+static nios_spi_t      *spi    = (nios_spi_t *)CONFIG_SYS_NIOS_SPIBASE;
 
 /* Warning:
  * You cannot enable DEBUG for early system initalization, i. e. when
@@ -63,10 +63,10 @@ static char quickhex (int i)
        return hex_digit[i];
 }
 
-static void memdump (void *pv, int num)
+static void memdump (const void *pv, int num)
 {
        int i;
-       unsigned char *pc = (unsigned char *) pv;
+       const unsigned char *pc = (const unsigned char *) pv;
 
        for (i = 0; i < num; i++)
                printf ("%c%c ", quickhex (pc[i] >> 4), quickhex (pc[i] & 0x0f));
@@ -83,74 +83,111 @@ static void memdump (void *pv, int num)
 #endif  /* DEBUG */
 
 
+struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
+               unsigned int max_hz, unsigned int mode)
+{
+       struct spi_slave *slave;
+
+       if (!spi_cs_is_valid(bus, cs))
+               return NULL;
+
+       slave = malloc(sizeof(struct spi_slave));
+       if (!slave)
+               return NULL;
+
+       slave->bus = bus;
+       slave->cs = cs;
+
+       /* TODO: Add support for different modes and speeds */
+
+       return slave;
+}
+
+void spi_free_slave(struct spi_slave *slave)
+{
+       free(slave);
+}
+
+int spi_claim_bus(struct spi_slave *slave)
+{
+       return 0;
+}
+
+void spi_release_bus(struct spi_slave *slave)
+{
+
+}
+
 /*
  * SPI transfer:
  *
  * See include/spi.h and http://www.altera.com/literature/ds/ds_nios_spi.pdf
  * for more informations.
  */
-int spi_xfer(spi_chipsel_type chipsel, int bitlen, uchar *dout, uchar *din)
+int spi_xfer(struct spi_slave *slave, int bitlen, const void *dout,
+               void *din, unsigned long flags)
 {
+       const u8 *txd = dout;
+       u8 *rxd = din;
        int j;
 
-       DPRINT(("spi_xfer: chipsel %08X dout %08X din %08X bitlen %d\n",
-               (int)chipsel, *(uint *)dout, *(uint *)din, bitlen));
+       DPRINT(("spi_xfer: slave %u:%u dout %08X din %08X bitlen %d\n",
+               slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen));
 
-       memdump((void*)dout, (bitlen + 7) / 8);
+       memdump(dout, (bitlen + 7) / 8);
 
-       if(chipsel != NULL) {
-               chipsel(1);     /* select the target chip */
-       }
+       if (flags & SPI_XFER_BEGIN)
+               spi_cs_activate(slave);
 
-       if (bitlen > CFG_NIOS_SPIBITS) {        /* leave chip select active */
+       if (!(flags & SPI_XFER_END) || bitlen > CONFIG_SYS_NIOS_SPIBITS) {
+               /* leave chip select active */
                spi->control |= NIOS_SPI_SSO;
        }
 
        for (   j = 0;                          /* count each byte in */
                j < ((bitlen + 7) / 8);         /* dout[] and din[] */
 
-#if    (CFG_NIOS_SPIBITS == 8)
+#if    (CONFIG_SYS_NIOS_SPIBITS == 8)
                j++) {
 
                while ((spi->status & NIOS_SPI_TRDY) == 0)
                        ;
-               spi->txdata = (unsigned)(dout[j]);
+               spi->txdata = (unsigned)(txd[j]);
 
                while ((spi->status & NIOS_SPI_RRDY) == 0)
                        ;
-               din[j] = (unsigned char)(spi->rxdata & 0xff);
+               rxd[j] = (unsigned char)(spi->rxdata & 0xff);
 
-#elif  (CFG_NIOS_SPIBITS == 16)
+#elif  (CONFIG_SYS_NIOS_SPIBITS == 16)
                j++, j++) {
 
                while ((spi->status & NIOS_SPI_TRDY) == 0)
                        ;
                if ((j+1) < ((bitlen + 7) / 8))
-                       spi->txdata = (unsigned)((dout[j] << 8) | dout[j+1]);
+                       spi->txdata = (unsigned)((txd[j] << 8) | txd[j+1]);
                else
-                       spi->txdata = (unsigned)(dout[j] << 8);
+                       spi->txdata = (unsigned)(txd[j] << 8);
 
                while ((spi->status & NIOS_SPI_RRDY) == 0)
                        ;
-               din[j] = (unsigned char)((spi->rxdata >> 8) & 0xff);
+               rxd[j] = (unsigned char)((spi->rxdata >> 8) & 0xff);
                if ((j+1) < ((bitlen + 7) / 8))
-                       din[j+1] = (unsigned char)(spi->rxdata & 0xff);
+                       rxd[j+1] = (unsigned char)(spi->rxdata & 0xff);
 
 #else
-#error "*** unsupported value of CFG_NIOS_SPIBITS ***"
+#error "*** unsupported value of CONFIG_SYS_NIOS_SPIBITS ***"
 #endif
 
        }
 
-       if (bitlen > CFG_NIOS_SPIBITS) {
+       if (bitlen > CONFIG_SYS_NIOS_SPIBITS && (flags & SPI_XFER_END)) {
                spi->control &= ~NIOS_SPI_SSO;
        }
 
-       if(chipsel != NULL) {
-               chipsel(0);     /* deselect the target chip */
-       }
+       if (flags & SPI_XFER_END)
+               spi_cs_deactivate(slave);
 
-       memdump((void*)din, (bitlen + 7) / 8);
+       memdump(din, (bitlen + 7) / 8);
 
        return 0;
 }