Merge tag 'u-boot-imx-20200107' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[oweals/u-boot.git] / drivers / spi / designware_spi.c
index 06f777461ecf1d4e3964998a201fc35f6be610fb..66ff8eeccdebde3da7980b2006a9d8fe5c70d04c 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Designware master SPI core controller driver
  *
@@ -6,24 +7,21 @@
  * Very loosely based on the Linux driver:
  * drivers/spi/spi-dw.c, which is:
  * Copyright (c) 2009, Intel Corporation.
- *
- * SPDX-License-Identifier:    GPL-2.0
  */
 
-#include <asm-generic/gpio.h>
 #include <common.h>
+#include <asm-generic/gpio.h>
 #include <clk.h>
 #include <dm.h>
 #include <errno.h>
 #include <malloc.h>
 #include <spi.h>
 #include <fdtdec.h>
+#include <reset.h>
 #include <linux/compat.h>
 #include <linux/iopoll.h>
 #include <asm/io.h>
 
-DECLARE_GLOBAL_DATA_PTR;
-
 /* Register offsets */
 #define DW_SPI_CTRL0                   0x00
 #define DW_SPI_CTRL1                   0x04
@@ -112,31 +110,23 @@ struct dw_spi_priv {
        void *tx_end;
        void *rx;
        void *rx_end;
+
+       struct reset_ctl_bulk   resets;
 };
 
-static inline u32 dw_readl(struct dw_spi_priv *priv, u32 offset)
+static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
 {
        return __raw_readl(priv->regs + offset);
 }
 
-static inline void dw_writel(struct dw_spi_priv *priv, u32 offset, u32 val)
+static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
 {
        __raw_writel(val, priv->regs + offset);
 }
 
-static inline u16 dw_readw(struct dw_spi_priv *priv, u32 offset)
-{
-       return __raw_readw(priv->regs + offset);
-}
-
-static inline void dw_writew(struct dw_spi_priv *priv, u32 offset, u16 val)
-{
-       __raw_writew(val, priv->regs + offset);
-}
-
 static int request_gpio_cs(struct udevice *bus)
 {
-#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
+#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
        struct dw_spi_priv *priv = dev_get_priv(bus);
        int ret;
 
@@ -163,14 +153,12 @@ static int request_gpio_cs(struct udevice *bus)
 static int dw_spi_ofdata_to_platdata(struct udevice *bus)
 {
        struct dw_spi_platdata *plat = bus->platdata;
-       const void *blob = gd->fdt_blob;
-       int node = dev_of_offset(bus);
 
        plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
 
        /* Use 500KHz as a suitable default */
-       plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
-                                       500000);
+       plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
+                                              500000);
        debug("%s: regs=%p max-frequency=%d\n", __func__, plat->regs,
              plat->frequency);
 
@@ -179,14 +167,14 @@ static int dw_spi_ofdata_to_platdata(struct udevice *bus)
 
 static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
 {
-       dw_writel(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
+       dw_write(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
 }
 
 /* Restart the controller, disable all interrupts, clean rx fifo */
 static void spi_hw_init(struct dw_spi_priv *priv)
 {
        spi_enable_chip(priv, 0);
-       dw_writel(priv, DW_SPI_IMR, 0xff);
+       dw_write(priv, DW_SPI_IMR, 0xff);
        spi_enable_chip(priv, 1);
 
        /*
@@ -197,13 +185,13 @@ static void spi_hw_init(struct dw_spi_priv *priv)
                u32 fifo;
 
                for (fifo = 1; fifo < 256; fifo++) {
-                       dw_writew(priv, DW_SPI_TXFLTR, fifo);
-                       if (fifo != dw_readw(priv, DW_SPI_TXFLTR))
+                       dw_write(priv, DW_SPI_TXFLTR, fifo);
+                       if (fifo != dw_read(priv, DW_SPI_TXFLTR))
                                break;
                }
 
                priv->fifo_len = (fifo == 1) ? 0 : fifo;
-               dw_writew(priv, DW_SPI_TXFLTR, 0);
+               dw_write(priv, DW_SPI_TXFLTR, 0);
        }
        debug("%s: fifo_len=%d\n", __func__, priv->fifo_len);
 }
@@ -242,6 +230,34 @@ err_rate:
        return -EINVAL;
 }
 
+static int dw_spi_reset(struct udevice *bus)
+{
+       int ret;
+       struct dw_spi_priv *priv = dev_get_priv(bus);
+
+       ret = reset_get_bulk(bus, &priv->resets);
+       if (ret) {
+               /*
+                * Return 0 if error due to !CONFIG_DM_RESET and reset
+                * DT property is not present.
+                */
+               if (ret == -ENOENT || ret == -ENOTSUPP)
+                       return 0;
+
+               dev_warn(bus, "Can't get reset: %d\n", ret);
+               return ret;
+       }
+
+       ret = reset_deassert_bulk(&priv->resets);
+       if (ret) {
+               reset_release_bulk(&priv->resets);
+               dev_err(bus, "Failed to reset: %d\n", ret);
+               return ret;
+       }
+
+       return 0;
+}
+
 static int dw_spi_probe(struct udevice *bus)
 {
        struct dw_spi_platdata *plat = dev_get_platdata(bus);
@@ -255,6 +271,10 @@ static int dw_spi_probe(struct udevice *bus)
        if (ret)
                return ret;
 
+       ret = dw_spi_reset(bus);
+       if (ret)
+               return ret;
+
        /* Currently only bits_per_word == 8 supported */
        priv->bits_per_word = 8;
 
@@ -272,7 +292,7 @@ static inline u32 tx_max(struct dw_spi_priv *priv)
        u32 tx_left, tx_room, rxtx_gap;
 
        tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
-       tx_room = priv->fifo_len - dw_readw(priv, DW_SPI_TXFLR);
+       tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
 
        /*
         * Another concern is about the tx/rx mismatch, we
@@ -293,7 +313,7 @@ static inline u32 rx_max(struct dw_spi_priv *priv)
 {
        u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
 
-       return min_t(u32, rx_left, dw_readw(priv, DW_SPI_RXFLR));
+       return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
 }
 
 static void dw_writer(struct dw_spi_priv *priv)
@@ -309,7 +329,7 @@ static void dw_writer(struct dw_spi_priv *priv)
                        else
                                txw = *(u16 *)(priv->tx);
                }
-               dw_writew(priv, DW_SPI_DR, txw);
+               dw_write(priv, DW_SPI_DR, txw);
                debug("%s: tx=0x%02x\n", __func__, txw);
                priv->tx += priv->bits_per_word >> 3;
        }
@@ -321,7 +341,7 @@ static void dw_reader(struct dw_spi_priv *priv)
        u16 rxw;
 
        while (max--) {
-               rxw = dw_readw(priv, DW_SPI_DR);
+               rxw = dw_read(priv, DW_SPI_DR);
                debug("%s: rx=0x%02x\n", __func__, rxw);
 
                /* Care about rx if the transfer's original "rx" is not null */
@@ -345,9 +365,15 @@ static int poll_transfer(struct dw_spi_priv *priv)
        return 0;
 }
 
-static void external_cs_manage(struct udevice *dev, bool on)
+/*
+ * We define external_cs_manage function as 'weak' as some targets
+ * (like MSCC Ocelot) don't control the external CS pin using a GPIO
+ * controller. These SoCs use specific registers to control by
+ * software the SPI pins (and especially the CS).
+ */
+__weak void external_cs_manage(struct udevice *dev, bool on)
 {
-#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
+#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
        struct dw_spi_priv *priv = dev_get_priv(dev->parent);
 
        if (!dm_gpio_is_valid(&priv->cs_gpio))
@@ -410,8 +436,8 @@ static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
 
        debug("%s: cr0=%08x\n", __func__, cr0);
        /* Reprogram cr0 only if changed */
-       if (dw_readw(priv, DW_SPI_CTRL0) != cr0)
-               dw_writew(priv, DW_SPI_CTRL0, cr0);
+       if (dw_read(priv, DW_SPI_CTRL0) != cr0)
+               dw_write(priv, DW_SPI_CTRL0, cr0);
 
        /*
         * Configure the desired SS (slave select 0...3) in the controller
@@ -419,7 +445,7 @@ static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
         * automatically. So no cs_activate() etc is needed in this driver.
         */
        cs = spi_chip_select(dev);
-       dw_writel(priv, DW_SPI_SER, 1 << cs);
+       dw_write(priv, DW_SPI_SER, 1 << cs);
 
        /* Enable controller after writing control registers */
        spi_enable_chip(priv, 1);
@@ -435,7 +461,7 @@ static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
         * in the beginning of new transfer.
         */
        if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
-                              !(val & SR_TF_EMPT) || (val & SR_BUSY),
+                              (val & SR_TF_EMPT) && !(val & SR_BUSY),
                               RX_TIMEOUT * 1000)) {
                ret = -ETIMEDOUT;
        }
@@ -462,7 +488,7 @@ static int dw_spi_set_speed(struct udevice *bus, uint speed)
        /* clk_div doesn't support odd number */
        clk_div = priv->bus_clk_rate / speed;
        clk_div = (clk_div + 1) & 0xfffe;
-       dw_writel(priv, DW_SPI_BAUDR, clk_div);
+       dw_write(priv, DW_SPI_BAUDR, clk_div);
 
        /* Enable controller after writing control registers */
        spi_enable_chip(priv, 1);
@@ -489,6 +515,27 @@ static int dw_spi_set_mode(struct udevice *bus, uint mode)
        return 0;
 }
 
+static int dw_spi_remove(struct udevice *bus)
+{
+       struct dw_spi_priv *priv = dev_get_priv(bus);
+       int ret;
+
+       ret = reset_release_bulk(&priv->resets);
+       if (ret)
+               return ret;
+
+#if CONFIG_IS_ENABLED(CLK)
+       ret = clk_disable(&priv->clk);
+       if (ret)
+               return ret;
+
+       ret = clk_free(&priv->clk);
+       if (ret)
+               return ret;
+#endif
+       return 0;
+}
+
 static const struct dm_spi_ops dw_spi_ops = {
        .xfer           = dw_spi_xfer,
        .set_speed      = dw_spi_set_speed,
@@ -513,4 +560,5 @@ U_BOOT_DRIVER(dw_spi) = {
        .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
        .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
        .probe = dw_spi_probe,
+       .remove = dw_spi_remove,
 };