Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E7C8EC7618D for ; Tue, 21 Mar 2023 09:50:27 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B625785BB5; Tue, 21 Mar 2023 10:50:24 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.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=gmail.com header.i=@gmail.com header.b="VIn6mx1M"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 717A285B86; Tue, 21 Mar 2023 10:50:22 +0100 (CET) Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3AA7685BB5 for ; Tue, 21 Mar 2023 10:50:18 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=al.kochet@gmail.com Received: by mail-lj1-x22a.google.com with SMTP id s20so7654756ljp.1 for ; Tue, 21 Mar 2023 02:50:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679392217; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=oe9LwoT6lqBlQr92ILRrtzCT1Q2lrAnK15B4NavIXgY=; b=VIn6mx1M4euLJdnlGCywS733PAddJgKKd7Rfgj/36muToDI3V+wrbCeC7XM2x1RMPr xpXQozyavpGy9PrHrjFY3IMAa+zbizTIoH3S3DF/+khG0ypS3cJMedG1q1gHQopV/Trt Ghc52E5p4ol52Cyc6JJrNgEX21Bo1dQEpBUk978Uqf/uPA/ndZlezlQADA5oDnNjCp26 SEB07v7I81tZOr5T12BF3gHpVw3KoX988O3lrvoo0CnJlJ0LSzJDRD64Mr8z8wi7+43t et4F3+BxOmgcB4CMNgPLqo7vbBnESLN6jPM5kFdNSTl/pp+0XTFmf3AzvDsk1X5BXlYj Jo+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679392217; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oe9LwoT6lqBlQr92ILRrtzCT1Q2lrAnK15B4NavIXgY=; b=Q69NXSEFsTahdSHZoMYPg+4/eOb8G+LjuPDP8RASNBvuGbrmWfdIAAfyaQgPlwR52w eRqvP+9mU8uQdefVzR6HFoll5mhV10m+W2s0Ds/8enC6C08yjiHN9E4p+4a0Wgzb7aPJ oPCINHN3TTPOChoISrt0bS3n6vCpG5qUR1o41YMJnJxMrzFpjT3kbYz1XmxvLt20yTms L9GIjFsSCit0RHxHsCqPfUP0JLArt2qAAEoboxurxPwfDQ54GHd/jOFLt/OK83S6cxLl 8GrX3Tx0ZhmPg+beNmFW2Zy/JBIuHep2uZlhqz2pSG6lgsV3I/kFLQnkzFcttr7yiAjo OaSA== X-Gm-Message-State: AO0yUKUE1CirCPWn5CrHxUA0yH5ZrtE/PRrj3zHFJP5cilOvmuAn2OJr h4GGrY0bVIj/l2TrCTIog9I= X-Google-Smtp-Source: AK7set8Dr3j8iVmUKyBWxXzPFjr1KflpOY5i0OJDfvy2UMoGZyjP80Z+GhFzyWL3kcRm2h8WthO8SQ== X-Received: by 2002:a2e:b16b:0:b0:295:a16f:c893 with SMTP id a11-20020a2eb16b000000b00295a16fc893mr644319ljm.18.1679392217290; Tue, 21 Mar 2023 02:50:17 -0700 (PDT) Received: from smtpclient.apple (37-145-211-71.broadband.corbina.ru. [37.145.211.71]) by smtp.gmail.com with ESMTPSA id q5-20020a19a405000000b004db513b017esm2115483lfc.45.2023.03.21.02.50.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Mar 2023 02:50:16 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.400.51.1.1\)) Subject: Re: [PATCH] i2c: correct I2C deblock logic From: Alexander Kochetkov In-Reply-To: Date: Tue, 21 Mar 2023 12:50:05 +0300 Cc: "hs@denx.de" , "marex@denx.de" , "u-boot@lists.denx.de" , dl-uboot-imx , "xypron.glpk@gmx.de" , Simon Glass Content-Transfer-Encoding: quoted-printable Message-Id: <3E609CD7-294F-43BD-9A98-281036A1D952@gmail.com> References: <20230210092718.1699222-1-haibo.chen@nxp.com> <86707c02-d9fb-ed67-5734-9724df15464f@denx.de> <6AB909C3-F277-4A52-AD67-65F59A748A57@gmail.com> To: Bough Chen X-Mailer: Apple Mail (2.3731.400.51.1.1) 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.8 at phobos.denx.de X-Virus-Status: Clean Hi Haibo. Nice catch! Agree with you patch. But may be fix problem in general? I suggest to add GPIOD_ACTIVE_LOW to the GPIOD_MASK_DIR mask to fix = problems 1, 2, 4. Not sure if that logically correct. What do you think? I grepped the code for =E2=80=98dm_gpio_set_dir_flags=E2=80=99 and found = another places affected by the bug. 1. u-boot/drivers/spi/mxc_spi.c: ret =3D dm_gpio_set_dir_flags(&mxcs->cs_gpios[i], GPIOD_IS_OUT | = GPIOD_ACTIVE_LOW); Here GPIOD_ACTIVE_LOW is passed to dm_gpio_set_dir_flags() and filtered = out by dm_gpio_set_dir_flags(). GPIOD_ACTIVE_LOW can be added to GPIOD_MASK_DIR mask to fix that. 2. u-boot/drivers/spi/mvebu_a3700_spi.c: ret =3D dm_gpio_set_dir_flags(&plat->cs_gpios[i], GPIOD_IS_OUT | = GPIOD_ACTIVE_LOW); Same as 1. 3. u-boot/drivers/phy/allwinner/phy-sun4i-usb.c: ret =3D dm_gpio_set_dir_flags(&phy->gpio_id_det, GPIOD_IS_IN | = GPIOD_PULL_UP); Here GPIOD_PULL_UP is passed to dm_gpio_set_dir_flags() and filtered out = by dm_gpio_set_dir_flags(). Looks, like we have to introduce dm_gpio_set_pull_flags() and use it = together with dm_gpio_set_dir_flags(). 4. u-boot/drivers/i2c/i2c-uclass.c dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | GPIOD_ACTIVE_LOW | = GPIOD_IS_OUT_ACTIVE); Same as 1. Your patch cover that bug. > 21 =D0=BC=D0=B0=D1=80=D1=82=D0=B0 2023 =D0=B3., =D0=B2 11:37, Bough = Chen =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0= ): >=20 >> -----Original Message----- >> From: Alexander Kochetkov >> Sent: 2023=E5=B9=B43=E6=9C=8820=E6=97=A5 16:03 >> To: hs@denx.de >> Cc: Bough Chen ; marex@denx.de; >> u-boot@lists.denx.de; dl-uboot-imx ; >> xypron.glpk@gmx.de; Simon Glass >> Subject: Re: [PATCH] i2c: correct I2C deblock logic >>=20 >> Hello! >>=20 >> The patch doesn=E2=80=99t add new functionality to the code. >> May be it makes code more readable. But in later case the patch = description >> should be corrected and Fixes tag removed. >=20 > Hi Alexander, >=20 > This patch not only make the code more readable, but also really fix a = bug. > In our current code, we will call i2c_gpio_set_pin(scl_pin, 1) and = then i2c_gpio_set_pin(scl_pin, 0). > After the i2c_gpio_set_pin(scl_pin, 0), the flags contain = GPIOD_ACTIVE_LOW. > Though for SDA, it will first call i2c_gpio_set_pin(sda_pin, 0) then = i2c_gpio_set_pin(sda_pin, 1), but=20 > i2c_gpio_set_pin-> dm_gpio_set_dir_flags-> dm_gpio_clrset_flags, it = only clear GPIOD_MASK_DIR, this has no impact with GPIOD_ACTIVE_LOW. So = this GPIOD_ACTIVE_LOW keep in flags. > Then call i2c_gpio_get_pin by using dm_gpio_get_value(pin) will get = the wrong data, let the function i2c_deblock_gpio_loop always return = -EREMOTEIO >=20 > Best Regards > Haibo Chen >=20 >>=20 >> The flag GPIOD_ACTIVE_LOW affects return value dm_gpio_get_value(). >> And return value doesn=E2=80=99t depends on the DTS settings. It = depends on the flag >> passed to dm_gpio_set_dir_flags(). Usually the code parses DTS and = provide the >> correct flag to the dm_gpio_set_dir_flags(). >>=20 >> So the patch adds flag GPIOD_ACTIVE_LOW to the = dm_gpio_set_dir_flags() and >> as expected this negates the return value of dm_gpio_get_value(). So = in order to >> keep the code correct the patch also fixes adds negotiation to >> dm_gpio_get_value(). >>=20 >> Alexander. >>=20 >>=20 >>=20 >>> 20 =D0=BC=D0=B0=D1=80=D1=82=D0=B0 2023 =D0=B3., =D0=B2 10:44, Heiko = Schocher =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0): >>>=20 >>> Hi! >>>=20 >>> On 13.03.23 03:55, Bough Chen wrote: >>>>> -----Original Message----- >>>>> From: Bough Chen >>>>> Sent: 2023=E5=B9=B42=E6=9C=8810=E6=97=A5 17:27 >>>>> To: hs@denx.de; al.kochet@gmail.com; marex@denx.de >>>>> Cc: u-boot@lists.denx.de; dl-uboot-imx ; >>>>> xypron.glpk@gmx.de; Bough Chen >>>>> Subject: [PATCH] i2c: correct I2C deblock logic >>>>>=20 >>>>> From: Haibo Chen >>>>>=20 >>>>> Current code use dm_gpio_get_value() to get SDA and SCL value, and >>>>> the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to >>>>> wait slave release SDA, the SDA are config as GPIOD_IS_IN, and >>>>> whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. >>>>> Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if >>>>> the SDA is in low level, then >>>>> dm_gpio_get_value() will return 1, current code logic will stop = the >>>>> SCL toggle wrongly, cause the I2C deblock not work as expect. >>>>>=20 >>>>> This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and >>>>> GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() = eaqual >>>>> to the physical voltage logic level. >>>>>=20 >>>>=20 >>>> Hi, >>>>=20 >>>> Any comments for this patch, just a reminder in case you miss it. >>>=20 >>> Indeed, I missed this patch, sorry! >>> Your explanation sounds reasonable to me, but I wonder if nobody >>> tapped into this problem... it would be good to have some test = reports >>> from others! Also added Simon, as he did a lot of work in this = space. >>>=20 >>> Thanks! >>>=20 >>> bye, >>> Heiko >>>>=20 >>>> Best Regards >>>> Haibo Chen >>>>=20 >>>>> Fixes: aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock") >>>>> Signed-off-by: Haibo Chen >>>>> --- >>>>> drivers/i2c/i2c-uclass.c | 5 +++-- >>>>> 1 file changed, 3 insertions(+), 2 deletions(-) >>>>>=20 >>>>> diff --git a/drivers/i2c/i2c-uclass.c b/drivers/i2c/i2c-uclass.c >>>>> index >>>>> 8d9a89ed89..4dc707c659 100644 >>>>> --- a/drivers/i2c/i2c-uclass.c >>>>> +++ b/drivers/i2c/i2c-uclass.c >>>>> @@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct >>>>> udevice >>>>> *dev) static void i2c_gpio_set_pin(struct gpio_desc *pin, int = bit) { >>>>> if (bit) >>>>> - dm_gpio_set_dir_flags(pin, GPIOD_IS_IN); >>>>> + dm_gpio_set_dir_flags(pin, GPIOD_IS_IN | >>>>> + GPIOD_ACTIVE_LOW); >>>>> else >>>>> dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | >>>>> GPIOD_ACTIVE_LOW | >>>>> @@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc >>>>> *pin, int >>>>> bit) >>>>>=20 >>>>> static int i2c_gpio_get_pin(struct gpio_desc *pin) { >>>>> - return dm_gpio_get_value(pin); >>>>> + return !dm_gpio_get_value(pin); >>>>> } >>>>>=20 >>>>> int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, >>>>> -- >>>>> 2.34.1 >>>>=20 >>>=20 >>> -- >>> DENX Software Engineering GmbH, Managing Director: Erika Unter >>> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, = Germany >>> Phone: +49-8142-66989-52 Fax: +49-8142-66989-80 Email: = hs@denx.de >=20