spi: cadence-qspi: Add direct mode support
[oweals/u-boot.git] / drivers / spi / spi-mem.c
index af9aef009a73d2169410cedb0c704900d27a2e42..7788ab995344332a1280b196c52de652dd55de5a 100644 (file)
@@ -201,7 +201,6 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
        unsigned int pos = 0;
        const u8 *tx_buf = NULL;
        u8 *rx_buf = NULL;
-       u8 *op_buf;
        int op_len;
        u32 flag;
        int ret;
@@ -210,7 +209,11 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
        if (!spi_mem_supports_op(slave, op))
                return -ENOTSUPP;
 
-       if (ops->mem_ops) {
+       ret = spi_claim_bus(slave);
+       if (ret < 0)
+               return ret;
+
+       if (ops->mem_ops && ops->mem_ops->exec_op) {
 #ifndef __UBOOT__
                /*
                 * Flush the message queue before executing our SPI memory
@@ -232,6 +235,7 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
                mutex_lock(&ctlr->io_mutex);
 #endif
                ret = ops->mem_ops->exec_op(slave, op);
+
 #ifndef __UBOOT__
                mutex_unlock(&ctlr->io_mutex);
                mutex_unlock(&ctlr->bus_lock_mutex);
@@ -245,8 +249,10 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
                 * read path) and expect the core to use the regular SPI
                 * interface in other cases.
                 */
-               if (!ret || ret != -ENOTSUPP)
+               if (!ret || ret != -ENOTSUPP) {
+                       spi_release_bus(slave);
                        return ret;
+               }
        }
 
 #ifndef __UBOOT__
@@ -323,15 +329,6 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
                return -EIO;
 #else
 
-       /* U-Boot does not support parallel SPI data lanes */
-       if ((op->cmd.buswidth != 1) ||
-           (op->addr.nbytes && op->addr.buswidth != 1) ||
-           (op->dummy.nbytes && op->dummy.buswidth != 1) ||
-           (op->data.nbytes && op->data.buswidth != 1)) {
-               printf("Dual/Quad raw SPI transfers not supported\n");
-               return -ENOTSUPP;
-       }
-
        if (op->data.nbytes) {
                if (op->data.dir == SPI_MEM_DATA_IN)
                        rx_buf = op->data.buf.in;
@@ -340,11 +337,17 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
        }
 
        op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
-       op_buf = calloc(1, op_len);
 
-       ret = spi_claim_bus(slave);
-       if (ret < 0)
-               return ret;
+       /*
+        * Avoid using malloc() here so that we can use this code in SPL where
+        * simple malloc may be used. That implementation does not allow free()
+        * so repeated calls to this code can exhaust the space.
+        *
+        * The value of op_len is small, since it does not include the actual
+        * data being sent, only the op-code and address. In fact, it should be
+        * possible to just use a small fixed value here instead of op_len.
+        */
+       u8 op_buf[op_len];
 
        op_buf[pos++] = op->cmd.opcode;
 
@@ -388,8 +391,6 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
                debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
        debug("[ret %d]\n", ret);
 
-       free(op_buf);
-
        if (ret < 0)
                return ret;
 #endif /* __UBOOT__ */
@@ -421,6 +422,27 @@ int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
        if (ops->mem_ops && ops->mem_ops->adjust_op_size)
                return ops->mem_ops->adjust_op_size(slave, op);
 
+       if (!ops->mem_ops || !ops->mem_ops->exec_op) {
+               unsigned int len;
+
+               len = sizeof(op->cmd.opcode) + op->addr.nbytes +
+                       op->dummy.nbytes;
+               if (slave->max_write_size && len > slave->max_write_size)
+                       return -EINVAL;
+
+               if (op->data.dir == SPI_MEM_DATA_IN) {
+                       if (slave->max_read_size)
+                               op->data.nbytes = min(op->data.nbytes,
+                                             slave->max_read_size);
+               } else if (slave->max_write_size) {
+                       op->data.nbytes = min(op->data.nbytes,
+                                             slave->max_write_size - len);
+               }
+
+               if (!op->data.nbytes)
+                       return -EINVAL;
+       }
+
        return 0;
 }
 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);