Merge branch '2020-05-18-reduce-size-of-common.h'
[oweals/u-boot.git] / drivers / spi / sandbox_spi.c
index e717424db83a9ff7dcc15bc57074c5722b66ff11..b0a46c886815925027c5c3cf5a790083f1ebcd16 100644 (file)
@@ -8,20 +8,21 @@
  * Licensed under the GPL-2 or later.
  */
 
+#define LOG_CATEGORY UCLASS_SPI
+
 #include <common.h>
 #include <dm.h>
+#include <log.h>
 #include <malloc.h>
 #include <spi.h>
 #include <spi_flash.h>
 #include <os.h>
 
-#include <asm/errno.h>
+#include <linux/errno.h>
 #include <asm/spi.h>
 #include <asm/state.h>
 #include <dm/device-internal.h>
 
-DECLARE_GLOBAL_DATA_PTR;
-
 #ifndef CONFIG_SPI_IDLE_VAL
 # define CONFIG_SPI_IDLE_VAL 0xFF
 #endif
@@ -58,7 +59,6 @@ static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
        struct udevice *emul;
        uint bytes = bitlen / 8, i;
        int ret;
-       u8 *tx = (void *)dout, *rx = din;
        uint busnum, cs;
 
        if (bitlen == 0)
@@ -89,37 +89,16 @@ static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
        if (ret)
                return ret;
 
-       /* make sure rx/tx buffers are full so clients can assume */
-       if (!tx) {
-               debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
-               tx = malloc(bytes);
-               if (!tx) {
-                       debug("sandbox_spi: Out of memory\n");
-                       return -ENOMEM;
-               }
-       }
-       if (!rx) {
-               debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
-               rx = malloc(bytes);
-               if (!rx) {
-                       debug("sandbox_spi: Out of memory\n");
-                       return -ENOMEM;
-               }
-       }
-
        ops = spi_emul_get_ops(emul);
        ret = ops->xfer(emul, bitlen, dout, din, flags);
 
-       debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
-             ret, ret ? "bad" : "good");
-       for (i = 0; i < bytes; ++i)
-               debug(" %u:%02x", i, rx[i]);
-       debug("\n");
-
-       if (tx != dout)
-               free(tx);
-       if (rx != din)
-               free(rx);
+       log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
+                   ret, ret ? "bad" : "good");
+       if (din) {
+               for (i = 0; i < bytes; ++i)
+                       log_content(" %u:%02x", i, ((u8 *)din)[i]);
+       }
+       log_content("\n");
 
        return ret;
 }
@@ -139,7 +118,17 @@ static int sandbox_cs_info(struct udevice *bus, uint cs,
 {
        /* Always allow activity on CS 0 */
        if (cs >= 1)
-               return -ENODEV;
+               return -EINVAL;
+
+       return 0;
+}
+
+static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
+                               uint *map_sizep, uint *offsetp)
+{
+       *map_basep = 0x1000;
+       *map_sizep = 0x2000;
+       *offsetp = 0x100;
 
        return 0;
 }
@@ -149,6 +138,7 @@ static const struct dm_spi_ops sandbox_spi_ops = {
        .set_speed      = sandbox_spi_set_speed,
        .set_mode       = sandbox_spi_set_mode,
        .cs_info        = sandbox_cs_info,
+       .get_mmap       = sandbox_spi_get_mmap,
 };
 
 static const struct udevice_id sandbox_spi_ids[] = {
@@ -160,6 +150,5 @@ U_BOOT_DRIVER(spi_sandbox) = {
        .name   = "spi_sandbox",
        .id     = UCLASS_SPI,
        .of_match = sandbox_spi_ids,
-       .per_child_auto_alloc_size      = sizeof(struct spi_slave),
        .ops    = &sandbox_spi_ops,
 };