]> git.sur5r.net Git - u-boot/blobdiff - drivers/spi/omap3_spi.c
spi: omap3: fix set_speed and set_mode dm callbacks
[u-boot] / drivers / spi / omap3_spi.c
index 98ee6d3d03ff7b85153065dd7f07d6548370139a..766436ea5f9845bd3be4b5186df1d93c6ba0a5a2 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2016 Jagan Teki <jteki@openedev.com>
  *                   Christophe Ricard <christophe.ricard@gmail.com>
@@ -13,8 +14,6 @@
  * Copyright (C) 2005, 2006 Nokia Corporation
  *
  * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -35,6 +34,12 @@ DECLARE_GLOBAL_DATA_PTR;
 #define OMAP3_MCSPI4_BASE      0x480BA000
 #endif
 
+#define OMAP4_MCSPI_REG_OFFSET 0x100
+
+struct omap2_mcspi_platform_config {
+       unsigned int regs_offset;
+};
+
 /* per-register bitmasks */
 #define OMAP3_MCSPI_SYSCONFIG_SMARTIDLE (2 << 3)
 #define OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
@@ -103,6 +108,9 @@ struct mcspi {
 };
 
 struct omap3_spi_priv {
+#ifndef CONFIG_DM_SPI
+       struct spi_slave slave;
+#endif
        struct mcspi *regs;
        unsigned int cs;
        unsigned int freq;
@@ -447,23 +455,13 @@ static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
        conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
 
        writel(conf, &priv->regs->modulctrl);
-
-       _omap3_spi_set_mode(priv);
-       _omap3_spi_set_speed(priv);
 }
 
 #ifndef CONFIG_DM_SPI
 
-struct omap3_spi_slave {
-       struct spi_slave         slave;
-       struct omap3_spi_priv   spi_priv;
-};
-
-struct omap3_spi_priv *priv;
-
-static inline struct omap3_spi_slave *to_omap3_spi(struct spi_slave *slave)
+static inline struct omap3_spi_priv *to_omap3_spi(struct spi_slave *slave)
 {
-       return container_of(slave, struct omap3_spi_slave, slave);
+       return container_of(slave, struct omap3_spi_priv, slave);
 }
 
 void spi_init(void)
@@ -473,13 +471,15 @@ void spi_init(void)
 
 void spi_free_slave(struct spi_slave *slave)
 {
-       struct omap3_spi_slave *ds = to_omap3_spi(slave);
+       struct omap3_spi_priv *priv = to_omap3_spi(slave);
 
-       free(ds);
+       free(priv);
 }
 
 int spi_claim_bus(struct spi_slave *slave)
 {
+       struct omap3_spi_priv *priv = to_omap3_spi(slave);
+
        _omap3_spi_claim_bus(priv);
        _omap3_spi_set_wordlen(priv);
        _omap3_spi_set_mode(priv);
@@ -490,6 +490,8 @@ int spi_claim_bus(struct spi_slave *slave)
 
 void spi_release_bus(struct spi_slave *slave)
 {
+       struct omap3_spi_priv *priv = to_omap3_spi(slave);
+
        /* Reset the SPI hardware */
        spi_reset(priv->regs);
 }
@@ -497,7 +499,7 @@ void spi_release_bus(struct spi_slave *slave)
 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
                                     unsigned int max_hz, unsigned int mode)
 {
-       struct omap3_spi_slave *ds;
+       struct omap3_spi_priv *priv;
        struct mcspi *regs;
 
        /*
@@ -542,7 +544,8 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
        }
 
        if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
-               printf("SPI error: unsupported frequency %i Hz. Max frequency is 48 Mhz\n", max_hz);
+               printf("SPI error: unsupported frequency %i Hz. Max frequency is 48 MHz\n",
+                      max_hz);
                return NULL;
        }
 
@@ -551,29 +554,32 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
                return NULL;
        }
 
-       ds = spi_alloc_slave(struct omap3_spi_slave, bus, cs);
-       if (!ds) {
+       priv = spi_alloc_slave(struct omap3_spi_priv, bus, cs);
+       if (!priv) {
                printf("SPI error: malloc of SPI structure failed\n");
                return NULL;
        }
 
-       priv = &ds->spi_priv;
-
        priv->regs = regs;
        priv->cs = cs;
        priv->freq = max_hz;
        priv->mode = mode;
-       priv->wordlen = ds->slave.wordlen;
-#ifdef CONFIG_OMAP3_SPI_D0_D1_SWAPPED
+       priv->wordlen = priv->slave.wordlen;
+#if 0
+       /* Please migrate to DM_SPI support for this feature. */
        priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
 #endif
 
-       return &ds->slave;
+       return &priv->slave;
 }
 
 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
             const void *dout, void *din, unsigned long flags)
-{ return _spi_xfer(priv, bitlen, dout, din, flags); }
+{
+       struct omap3_spi_priv *priv = to_omap3_spi(slave);
+
+       return _spi_xfer(priv, bitlen, dout, din, flags);
+}
 
 #else
 
@@ -584,8 +590,8 @@ static int omap3_spi_claim_bus(struct udevice *dev)
        struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
 
        priv->cs = slave_plat->cs;
-       priv->mode = slave_plat->mode;
        priv->freq = slave_plat->max_hz;
+
        _omap3_spi_claim_bus(priv);
 
        return 0;
@@ -619,11 +625,16 @@ static int omap3_spi_probe(struct udevice *dev)
 {
        struct omap3_spi_priv *priv = dev_get_priv(dev);
        const void *blob = gd->fdt_blob;
-       int node = dev->of_offset;
+       int node = dev_of_offset(dev);
+
+       struct omap2_mcspi_platform_config* data =
+               (struct omap2_mcspi_platform_config*)dev_get_driver_data(dev);
 
-       priv->regs = (struct mcspi *)dev_get_addr(dev);
-       priv->pin_dir = fdtdec_get_uint(blob, node, "ti,pindir-d0-out-d1-in",
-                                           MCSPI_PINDIR_D0_IN_D1_OUT);
+       priv->regs = (struct mcspi *)(devfdt_get_addr(dev) + data->regs_offset);
+       if (fdtdec_get_bool(blob, node, "ti,pindir-d0-out-d1-in"))
+               priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
+       else
+               priv->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
        priv->wordlen = SPI_DEFAULT_WORDLEN;
        return 0;
 }
@@ -637,13 +648,25 @@ static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
        return _spi_xfer(priv, bitlen, dout, din, flags);
 }
 
-static int omap3_spi_set_speed(struct udevice *bus, unsigned int speed)
+static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
 {
+
+       struct omap3_spi_priv *priv = dev_get_priv(dev);
+
+       priv->freq = speed;
+       _omap3_spi_set_speed(priv);
+
        return 0;
 }
 
-static int omap3_spi_set_mode(struct udevice *bus, uint mode)
+static int omap3_spi_set_mode(struct udevice *dev, uint mode)
 {
+       struct omap3_spi_priv *priv = dev_get_priv(dev);
+
+       priv->mode = mode;
+
+       _omap3_spi_set_mode(priv);
+
        return 0;
 }
 
@@ -660,9 +683,17 @@ static const struct dm_spi_ops omap3_spi_ops = {
         */
 };
 
+static struct omap2_mcspi_platform_config omap2_pdata = {
+       .regs_offset = 0,
+};
+
+static struct omap2_mcspi_platform_config omap4_pdata = {
+       .regs_offset = OMAP4_MCSPI_REG_OFFSET,
+};
+
 static const struct udevice_id omap3_spi_ids[] = {
-       { .compatible = "ti,omap2-mcspi" },
-       { .compatible = "ti,omap4-mcspi" },
+       { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
+       { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
        { }
 };
 
@@ -673,6 +704,5 @@ U_BOOT_DRIVER(omap3_spi) = {
        .probe = omap3_spi_probe,
        .ops    = &omap3_spi_ops,
        .priv_auto_alloc_size = sizeof(struct omap3_spi_priv),
-       .probe = omap3_spi_probe,
 };
 #endif