]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
spi: spi-uclass: Add new spi_get_bus_and_cs() implementation
authorPatrice Chotard <patrice.chotard@foss.st.com>
Wed, 30 Mar 2022 07:33:13 +0000 (09:33 +0200)
committerTom Rini <trini@konsulko.com>
Mon, 23 May 2022 13:33:10 +0000 (09:33 -0400)
Move legacy spi_get_bus_and_cs() code to _spi_get_bus_and_cs().

Add new spi_get_bus_and_cs() implementation which rely on DT
for speed and mode and don't need any drv_name nor dev_name
parameters. This will prepare the ground for next patch.

Update all callers to use _spi_get_bus_and_cs() to keep the
same behavior.

Signed-off-by: Patrice Chotard <patrice.chotard@foss.st.com>
Cc: Marek Behun <marek.behun@nic.cz>
Cc: Jagan Teki <jagan@amarulasolutions.com>
Cc: Vignesh R <vigneshr@ti.com>
Cc: Joe Hershberger <joe.hershberger@ni.com>
Cc: Ramon Fried <rfried.dev@gmail.com>
Cc: Lukasz Majewski <lukma@denx.de>
Cc: Marek Vasut <marex@denx.de>
Cc: Wolfgang Denk <wd@denx.de>
Cc: Simon Glass <sjg@chromium.org>
Cc: Stefan Roese <sr@denx.de>
Cc: "Pali Rohár" <pali@kernel.org>
Cc: Konstantin Porotchkin <kostap@marvell.com>
Cc: Igal Liberman <igall@marvell.com>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Pratyush Yadav <p.yadav@ti.com>
Cc: Sean Anderson <seanga2@gmail.com>
Cc: Anji J <anji.jagarlmudi@nxp.com>
Cc: Biwen Li <biwen.li@nxp.com>
Cc: Priyanka Jain <priyanka.jain@nxp.com>
Cc: Chaitanya Sakinam <chaitanya.sakinam@nxp.com>
board/CZ.NIC/turris_mox/turris_mox.c
cmd/spi.c
drivers/mtd/spi/sf-uclass.c
drivers/spi/spi-uclass.c
drivers/usb/gadget/max3420_udc.c
include/spi.h
test/dm/spi.c

index a4738b3a3ca3339a17de71fe96bf9df4684aac64..68bc315a9c129eaab0dc1bc6baadde96a3e53ec8 100644 (file)
@@ -149,9 +149,9 @@ static int mox_do_spi(u8 *in, u8 *out, size_t size)
        struct udevice *dev;
        int ret;
 
-       ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
-                                "spi_generic_drv", "moxtet@1", &dev,
-                                &slave);
+       ret = _spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
+                                 "spi_generic_drv", "moxtet@1", &dev,
+                                 &slave);
        if (ret)
                goto fail;
 
index 6dc32678da108b5cfe9782abcd2503cf659453cd..454ebe37d7547a1ac38e6275bee47729904c0461 100644 (file)
--- a/cmd/spi.c
+++ b/cmd/spi.c
@@ -46,8 +46,8 @@ static int do_spi_xfer(int bus, int cs)
        str = strdup(name);
        if (!str)
                return -ENOMEM;
-       ret = spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv",
-                                str, &dev, &slave);
+       ret = _spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv",
+                                 str, &dev, &slave);
        if (ret)
                return ret;
 #else
index 63d16291ff1e30068676e23cf1ecc638b324d875..b45ba54ebf6afa52abf5f65378a91f4ade05c5b5 100644 (file)
@@ -74,7 +74,7 @@ int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
        snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs);
        str = strdup(name);
 #endif
-       ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
+       ret = _spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
                                  "jedec_spi_nor", str, &bus, &slave);
        if (ret)
                return ret;
index f8ec312d715235669bf659bb7ead0188b6249ea1..f2791c4b88ecb0b143b77786bf15e691d3abe8bb 100644 (file)
@@ -340,9 +340,65 @@ int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
        return ret;
 }
 
-int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
-                      const char *drv_name, const char *dev_name,
-                      struct udevice **busp, struct spi_slave **devp)
+int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
+                      struct spi_slave **devp)
+{
+       struct udevice *bus, *dev;
+       struct dm_spi_bus *bus_data;
+       struct spi_slave *slave;
+       int ret;
+
+#if CONFIG_IS_ENABLED(OF_PLATDATA)
+       ret = uclass_first_device_err(UCLASS_SPI, &bus);
+#else
+       ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
+#endif
+       if (ret) {
+               log_err("Invalid bus %d (err=%d)\n", busnum, ret);
+               return ret;
+       }
+       ret = spi_find_chip_select(bus, cs, &dev);
+       if (ret) {
+               dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
+               return ret;
+       }
+
+       if (!device_active(dev)) {
+               struct spi_slave *slave;
+
+               ret = device_probe(dev);
+               if (ret)
+                       goto err;
+               slave = dev_get_parent_priv(dev);
+               slave->dev = dev;
+       }
+
+       slave = dev_get_parent_priv(dev);
+       bus_data = dev_get_uclass_priv(bus);
+
+       /*
+        * In case the operation speed is not yet established by
+        * dm_spi_claim_bus() ensure the bus is configured properly.
+        */
+       if (!bus_data->speed) {
+               ret = spi_claim_bus(slave);
+               if (ret)
+                       goto err;
+       }
+       *busp = bus;
+       *devp = slave;
+
+       return 0;
+
+err:
+       log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
+
+       return ret;
+}
+
+int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
+                       const char *drv_name, const char *dev_name,
+                       struct udevice **busp, struct spi_slave **devp)
 {
        struct udevice *bus, *dev;
        struct dm_spi_slave_plat *plat;
@@ -453,8 +509,8 @@ struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
        struct udevice *dev;
        int ret;
 
-       ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
-                                &slave);
+       ret = _spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
+                                 &slave);
        if (ret)
                return NULL;
 
index a16095f8927a131d478a47b9c153fd8e6e468b79..fa655c98dcc5c30f23262f0dfc23ae80c31fd73c 100644 (file)
@@ -830,8 +830,8 @@ static int max3420_udc_probe(struct udevice *dev)
        cs = slave_pdata->cs;
        speed = slave_pdata->max_hz;
        mode = slave_pdata->mode;
-       spi_get_bus_and_cs(busnum, cs, speed, mode, "spi_generic_drv",
-                          NULL, &spid, &udc->slave);
+       _spi_get_bus_and_cs(busnum, cs, speed, mode, false, "spi_generic_drv",
+                           NULL, &spid, &udc->slave);
 
        udc->dev = dev;
        udc->gadget.ep0 = &udc->ep[0].ep_usb;
index fa9ab12dbef4f47977ae143c4786b15d5ff9c13a..9a8c1fb260e64622e7d3d0ef99edadae81d4e518 100644 (file)
@@ -572,6 +572,23 @@ int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  * Given a bus number and chip select, this finds the corresponding bus
  * device and slave device.
  *
+ * @busnum:    SPI bus number
+ * @cs:                Chip select to look for
+ * @busp:      Returns bus device
+ * @devp:      Return slave device
+ * @return 0 if found, -ve on error
+ */
+int spi_get_bus_and_cs(int busnum, int cs,
+                      struct udevice **busp, struct spi_slave **devp);
+
+/**
+ * _spi_get_bus_and_cs() - Find and activate bus and slave devices by number
+ * As spi_flash_probe(), This is an old-style function. We should remove
+ * it when all SPI flash drivers use dm
+ *
+ * Given a bus number and chip select, this finds the corresponding bus
+ * device and slave device.
+ *
  * If no such slave exists, and drv_name is not NULL, then a new slave device
  * is automatically bound on this chip select with requested speed and mode.
  *
@@ -588,7 +605,7 @@ int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  * @devp:      Return slave device
  * Return: 0 if found, -ve on error
  */
-int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
+int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
                        const char *drv_name, const char *dev_name,
                        struct udevice **busp, struct spi_slave **devp);
 
index ee4ad3abaa3c26454d7b558bedcddcbbe23a30ba..7ab0820abbdb53684638e0a7e530a6bb82e6619b 100644 (file)
@@ -46,19 +46,19 @@ static int dm_test_spi_find(struct unit_test_state *uts)
 
        /* This finds nothing because we removed the device */
        ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
-       ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, speed, mode,
-                                               NULL, 0, &bus, &slave));
+       ut_asserteq(-ENODEV, _spi_get_bus_and_cs(busnum, cs, speed, mode,
+                                                NULL, 0, &bus, &slave));
 
        /*
         * This forces the device to be re-added, but there is no emulation
         * connected so the probe will fail. We require that bus is left
-        * alone on failure, and that the spi_get_bus_and_cs() does not add
+        * alone on failure, and that the _spi_get_bus_and_cs() does not add
         * a 'partially-inited' device.
         */
        ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
-       ut_asserteq(-ENOENT, spi_get_bus_and_cs(busnum, cs, speed, mode,
-                                               "jedec_spi_nor", "name", &bus,
-                                               &slave));
+       ut_asserteq(-ENOENT, _spi_get_bus_and_cs(busnum, cs, speed, mode,
+                                                "jedec_spi_nor", "name", &bus,
+                                                &slave));
        sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
        ut_assertok(spi_cs_info(bus, cs, &info));
        ut_asserteq_ptr(NULL, info.dev);
@@ -67,8 +67,8 @@ static int dm_test_spi_find(struct unit_test_state *uts)
        ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node,
                                         "name"));
        ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev));
-       ut_assertok(spi_get_bus_and_cs(busnum, cs, speed, mode,
-                                      "jedec_spi_nor", "name", &bus, &slave));
+       ut_assertok(_spi_get_bus_and_cs(busnum, cs, speed, mode,
+                                       "jedec_spi_nor", "name", &bus, &slave));
 
        ut_assertok(spi_cs_info(bus, cs, &info));
        ut_asserteq_ptr(info.dev, slave->dev);
@@ -76,8 +76,9 @@ static int dm_test_spi_find(struct unit_test_state *uts)
        /* We should be able to add something to another chip select */
        ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node,
                                         "name"));
-       ut_asserteq(-EINVAL, spi_get_bus_and_cs(busnum, cs_b, speed, mode,
-                                      "jedec_spi_nor", "name", &bus, &slave));
+       ut_asserteq(-EINVAL, _spi_get_bus_and_cs(busnum, cs_b, speed, mode,
+                                                "jedec_spi_nor", "name", &bus,
+                                                &slave));
        ut_asserteq(-EINVAL, spi_cs_info(bus, cs_b, &info));
        ut_asserteq_ptr(NULL, info.dev);
 
@@ -145,11 +146,11 @@ static int dm_test_spi_claim_bus(struct unit_test_state *uts)
        const int busnum = 0, cs_a = 0, cs_b = 1, mode = 0;
 
        /* Get spi slave on CS0 */
-       ut_assertok(spi_get_bus_and_cs(busnum, cs_a, 1000000, mode, NULL, 0,
-                                      &bus, &slave_a));
+       ut_assertok(_spi_get_bus_and_cs(busnum, cs_a, 1000000, mode, NULL, 0,
+                                       &bus, &slave_a));
        /* Get spi slave on CS1 */
-       ut_assertok(spi_get_bus_and_cs(busnum, cs_b, 1000000, mode, NULL, 0,
-                                      &bus, &slave_b));
+       ut_assertok(_spi_get_bus_and_cs(busnum, cs_b, 1000000, mode, NULL, 0,
+                                       &bus, &slave_b));
 
        /* Different max_hz, different mode. */
        ut_assert(slave_a->max_hz != slave_b->max_hz);
@@ -182,8 +183,8 @@ static int dm_test_spi_xfer(struct unit_test_state *uts)
        const char dout[5] = {0x9f};
        unsigned char din[5];
 
-       ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
-                                      &bus, &slave));
+       ut_assertok(_spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
+                                       &bus, &slave));
        ut_assertok(spi_claim_bus(slave));
        ut_assertok(spi_xfer(slave, 40, dout, din,
                             SPI_XFER_BEGIN | SPI_XFER_END));