From patchwork Wed May 18 06:46:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrice CHOTARD X-Patchwork-Id: 1632628 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=foss.st.com header.i=@foss.st.com header.a=rsa-sha256 header.s=selector1 header.b=lCC85Buq; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4L33TK0wVGz9sBF for ; Wed, 18 May 2022 16:47:49 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1ECD38428B; Wed, 18 May 2022 08:47:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=foss.st.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=foss.st.com header.i=@foss.st.com header.b="lCC85Buq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 81E0F83C96; Wed, 18 May 2022 08:47:15 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D6A5B839AA for ; Wed, 18 May 2022 08:47:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=foss.st.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=prvs=11372af201=patrice.chotard@foss.st.com Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24HNSjdZ021302; Wed, 18 May 2022 08:46:53 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=selector1; bh=Xo3spSWcUUSN8TmRfwQKYnoEdR7AWhbjTbvv2sgRtl0=; b=lCC85BuqP+cD3LoS9rLV26AyiWOJgfPlTRcSZSjVQOpwgVPRLCYbi96V4j8QMTVJXyVA GKwW6hwc4HFhwMuD69Sm0MmtqAQnuFj3uP/Zn3sSnBQcvcPL7WRYMmziRkccptQ1UqAR 4MBhHZdvbQmLWMu9/67pbx81Sp/ZhI5bDZmjSo8Kw6XBOpFT/3y5EiYg/rjV19HD2lPy ZHFBJPFgB57fbXL8NViVQPWvmzAQEc4HiRkDbWW7GXD1IsW/fJMz4M9EphfbgznbXKnu JAl4ZI4wpOJR78CumEE2A167kLdKKydoIdQ9BDQeVy2JVoXZ3l49+wixLaMW6I0ytq/R TQ== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3g23ahq0xx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 May 2022 08:46:53 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 0A2D410002A; Wed, 18 May 2022 08:46:51 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id EFD8220FA5B; Wed, 18 May 2022 08:46:50 +0200 (CEST) Received: from localhost (10.75.127.44) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2308.20; Wed, 18 May 2022 08:46:50 +0200 From: Patrice Chotard To: CC: Patrice CHOTARD , Patrick DELAUNAY , U-Boot STM32 , Marek Behun , Jagan Teki , Vignesh R , Joe Hershberger , Ramon Fried , Lukasz Majewski , Marek Vasut , Wolfgang Denk , Simon Glass , Stefan Roese , =?utf-8?q?Pali_R?= =?utf-8?q?oh=C3=A1r?= , Konstantin Porotchkin , Igal Liberman , Bin Meng , Pratyush Yadav , Sean Anderson , Anji J , Biwen Li , Priyanka Jain , Chaitanya Sakinam Subject: [RESEND PATCH v4 1/3] spi: spi-uclass: Add new spi_get_bus_and_cs() implementation Date: Wed, 18 May 2022 08:46:46 +0200 Message-ID: <20220518064648.1843664-2-patrice.chotard@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220518064648.1843664-1-patrice.chotard@foss.st.com> References: <20220518064648.1843664-1-patrice.chotard@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.44] X-ClientProxiedBy: SFHDAG2NODE2.st.com (10.75.127.5) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-18_02,2022-05-17_02,2022-02-23_01 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean 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 Cc: Marek Behun Cc: Jagan Teki Cc: Vignesh R Cc: Joe Hershberger Cc: Ramon Fried Cc: Lukasz Majewski Cc: Marek Vasut Cc: Wolfgang Denk Cc: Simon Glass Cc: Stefan Roese Cc: "Pali Rohár" Cc: Konstantin Porotchkin Cc: Igal Liberman Cc: Bin Meng Cc: Pratyush Yadav Cc: Sean Anderson Cc: Anji J Cc: Biwen Li Cc: Priyanka Jain Cc: Chaitanya Sakinam --- (no changes since v1) board/CZ.NIC/turris_mox/turris_mox.c | 6 +-- cmd/spi.c | 4 +- drivers/mtd/spi/sf-uclass.c | 2 +- drivers/spi/spi-uclass.c | 66 +++++++++++++++++++++++++--- drivers/usb/gadget/max3420_udc.c | 4 +- include/spi.h | 19 +++++++- test/dm/spi.c | 33 +++++++------- 7 files changed, 104 insertions(+), 30 deletions(-) diff --git a/board/CZ.NIC/turris_mox/turris_mox.c b/board/CZ.NIC/turris_mox/turris_mox.c index a4738b3a3c..68bc315a9c 100644 --- a/board/CZ.NIC/turris_mox/turris_mox.c +++ b/board/CZ.NIC/turris_mox/turris_mox.c @@ -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; diff --git a/cmd/spi.c b/cmd/spi.c index 6dc32678da..454ebe37d7 100644 --- 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 diff --git a/drivers/mtd/spi/sf-uclass.c b/drivers/mtd/spi/sf-uclass.c index 63d16291ff..b45ba54ebf 100644 --- a/drivers/mtd/spi/sf-uclass.c +++ b/drivers/mtd/spi/sf-uclass.c @@ -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; diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c index f8ec312d71..f2791c4b88 100644 --- a/drivers/spi/spi-uclass.c +++ b/drivers/spi/spi-uclass.c @@ -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; diff --git a/drivers/usb/gadget/max3420_udc.c b/drivers/usb/gadget/max3420_udc.c index a16095f892..fa655c98dc 100644 --- a/drivers/usb/gadget/max3420_udc.c +++ b/drivers/usb/gadget/max3420_udc.c @@ -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; diff --git a/include/spi.h b/include/spi.h index fa9ab12dbe..9a8c1fb260 100644 --- a/include/spi.h +++ b/include/spi.h @@ -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); diff --git a/test/dm/spi.c b/test/dm/spi.c index ee4ad3abaa..7ab0820abb 100644 --- a/test/dm/spi.c +++ b/test/dm/spi.c @@ -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));