]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/spi/bfin_spi.c
Merge branch 'master' of git://git.denx.de/u-boot-video
[karo-tx-uboot.git] / drivers / spi / bfin_spi.c
index 4e008a79b6ad79e57242b407a9ce0e3d1bbd6a18..a9a4d92c3e719b0a1f215393ac7fc22de6cae8cf 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Driver for Blackfin On-Chip SPI device
  *
- * Copyright (c) 2005-2008 Analog Devices Inc.
+ * Copyright (c) 2005-2010 Analog Devices Inc.
  *
  * Licensed under the GPL-2 or later.
  */
@@ -13,6 +13,7 @@
 #include <spi.h>
 
 #include <asm/blackfin.h>
+#include <asm/gpio.h>
 #include <asm/portmux.h>
 #include <asm/mach-common/bits/spi.h>
 
@@ -34,48 +35,66 @@ MAKE_SPI_FUNC(SPI_BAUD, 0x14)
 
 #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
 
-__attribute__((weak))
+#define gpio_cs(cs) ((cs) - MAX_CTRL_CS)
+#ifdef CONFIG_BFIN_SPI_GPIO_CS
+# define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS)
+#else
+# define is_gpio_cs(cs) 0
+#endif
+
 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
 {
-#if defined(__ADSPBF538__) || defined(__ADSPBF539__)
-       /* The SPI1/SPI2 buses are weird ... only 1 CS */
-       if (bus > 0 && cs != 1)
-               return 0;
-#endif
-       return (cs >= 1 && cs <= 7);
+       if (is_gpio_cs(cs))
+               return gpio_is_valid(gpio_cs(cs));
+       else
+               return (cs >= 1 && cs <= MAX_CTRL_CS);
 }
 
-__attribute__((weak))
 void spi_cs_activate(struct spi_slave *slave)
 {
        struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
-       write_SPI_FLG(bss,
-               (read_SPI_FLG(bss) &
-               ~((!bss->flg << 8) << slave->cs)) |
-               (1 << slave->cs));
+
+       if (is_gpio_cs(slave->cs)) {
+               unsigned int cs = gpio_cs(slave->cs);
+               gpio_set_value(cs, bss->flg);
+               debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
+       } else {
+               write_SPI_FLG(bss,
+                       (read_SPI_FLG(bss) &
+                       ~((!bss->flg << 8) << slave->cs)) |
+                       (1 << slave->cs));
+               debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
+       }
+
        SSYNC();
-       debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
 }
 
-__attribute__((weak))
 void spi_cs_deactivate(struct spi_slave *slave)
 {
        struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
-       u16 flg;
-
-       /* make sure we force the cs to deassert rather than let the
-        * pin float back up.  otherwise, exact timings may not be
-        * met some of the time leading to random behavior (ugh).
-        */
-       flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
-       write_SPI_FLG(bss, flg);
-       SSYNC();
-       debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
 
-       flg &= ~(1 << slave->cs);
-       write_SPI_FLG(bss, flg);
+       if (is_gpio_cs(slave->cs)) {
+               unsigned int cs = gpio_cs(slave->cs);
+               gpio_set_value(cs, !bss->flg);
+               debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
+       } else {
+               u16 flg;
+
+               /* make sure we force the cs to deassert rather than let the
+                * pin float back up.  otherwise, exact timings may not be
+                * met some of the time leading to random behavior (ugh).
+                */
+               flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
+               write_SPI_FLG(bss, flg);
+               SSYNC();
+               debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
+
+               flg &= ~(1 << slave->cs);
+               write_SPI_FLG(bss, flg);
+               debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
+       }
+
        SSYNC();
-       debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
 }
 
 void spi_init()
@@ -118,13 +137,29 @@ static const unsigned short cs_pins[][7] = {
 #endif
 };
 
+void spi_set_speed(struct spi_slave *slave, uint hz)
+{
+       struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
+       ulong sclk;
+       u32 baud;
+
+       sclk = get_sclk();
+       baud = sclk / (2 * hz);
+       /* baud should be rounded up */
+       if (sclk % (2 * hz))
+               baud += 1;
+       if (baud < 2)
+               baud = 2;
+       else if (baud > (u16)-1)
+               baud = -1;
+       bss->baud = baud;
+}
+
 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
                unsigned int max_hz, unsigned int mode)
 {
        struct bfin_spi_slave *bss;
-       ulong sclk;
        u32 mmr_base;
-       u32 baud;
 
        if (!spi_cs_is_valid(bus, cs))
                return NULL;
@@ -146,32 +181,20 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
                default: return NULL;
        }
 
-       sclk = get_sclk();
-       baud = sclk / (2 * max_hz);
-       /* baud should be rounded up */
-       if (sclk % (2 * max_hz))
-               baud += 1;
-       if (baud < 2)
-               baud = 2;
-       else if (baud > (u16)-1)
-               baud = -1;
-
-       bss = malloc(sizeof(*bss));
+       bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs);
        if (!bss)
                return NULL;
 
-       bss->slave.bus = bus;
-       bss->slave.cs = cs;
        bss->mmr_base = (void *)mmr_base;
        bss->ctl = SPE | MSTR | TDBR_CORE;
        if (mode & SPI_CPHA) bss->ctl |= CPHA;
        if (mode & SPI_CPOL) bss->ctl |= CPOL;
        if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
-       bss->baud = baud;
        bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
+       spi_set_speed(&bss->slave, max_hz);
 
        debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
-               bus, cs, mmr_base, bss->ctl, baud, bss->flg);
+               bus, cs, mmr_base, bss->ctl, bss->baud, bss->flg);
 
        return &bss->slave;
 }
@@ -188,7 +211,13 @@ int spi_claim_bus(struct spi_slave *slave)
 
        debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
 
-       pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
+       if (is_gpio_cs(slave->cs)) {
+               unsigned int cs = gpio_cs(slave->cs);
+               gpio_request(cs, "bfin-spi");
+               gpio_direction_output(cs, !bss->flg);
+               pins[slave->bus][0] = P_DONTCARE;
+       } else
+               pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
        peripheral_request_list(pins[slave->bus], "bfin-spi");
 
        write_SPI_CTL(bss, bss->ctl);
@@ -205,6 +234,8 @@ void spi_release_bus(struct spi_slave *slave)
        debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
 
        peripheral_free_list(pins[slave->bus]);
+       if (is_gpio_cs(slave->cs))
+               gpio_free(gpio_cs(slave->cs));
 
        write_SPI_CTL(bss, 0);
        SSYNC();
@@ -214,6 +245,35 @@ void spi_release_bus(struct spi_slave *slave)
 # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
 #endif
 
+static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
+                       uint bytes)
+{
+       /* discard invalid data and clear RXS */
+       read_SPI_RDBR(bss);
+       /* todo: take advantage of hardware fifos  */
+       while (bytes--) {
+               u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
+               debug("%s: tx:%x ", __func__, value);
+               write_SPI_TDBR(bss, value);
+               SSYNC();
+               while ((read_SPI_STAT(bss) & TXS))
+                       if (ctrlc())
+                               return -1;
+               while (!(read_SPI_STAT(bss) & SPIF))
+                       if (ctrlc())
+                               return -1;
+               while (!(read_SPI_STAT(bss) & RXS))
+                       if (ctrlc())
+                               return -1;
+               value = read_SPI_RDBR(bss);
+               if (rx)
+                       *rx++ = value;
+               debug("rx:%x\n", value);
+       }
+
+       return 0;
+}
+
 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
                void *din, unsigned long flags)
 {
@@ -238,32 +298,7 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
        if (flags & SPI_XFER_BEGIN)
                spi_cs_activate(slave);
 
-       /* todo: take advantage of hardware fifos and setup RX dma */
-       while (bytes--) {
-               u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
-               debug("%s: tx:%x ", __func__, value);
-               write_SPI_TDBR(bss, value);
-               SSYNC();
-               while ((read_SPI_STAT(bss) & TXS))
-                       if (ctrlc()) {
-                               ret = -1;
-                               goto done;
-                       }
-               while (!(read_SPI_STAT(bss) & SPIF))
-                       if (ctrlc()) {
-                               ret = -1;
-                               goto done;
-                       }
-               while (!(read_SPI_STAT(bss) & RXS))
-                       if (ctrlc()) {
-                               ret = -1;
-                               goto done;
-                       }
-               value = read_SPI_RDBR(bss);
-               if (rx)
-                       *rx++ = value;
-               debug("rx:%x\n", value);
-       }
+       ret = spi_pio_xfer(bss, tx, rx, bytes);
 
  done:
        if (flags & SPI_XFER_END)