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 AFD2EC04FE1 for ; Tue, 1 Aug 2023 08:28:02 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D959E86B92; Tue, 1 Aug 2023 10:28:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1690878480; bh=MSzqAIYMhn9zxr1tbOH+G0E4sg8PsbvSEOzWCVqbjk8=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=geu37IAKkTB4aKNkFUEIFurmrxYXyQwLW0iutQTeM1Y3GKAFQuR38huNRcf0r7RbJ y4kxcBBCQwLkiQKCTdCWVLuRE2t/s0Jia1MYsZkgFliebU/n7yVHdpww0f4EeWEfGq 1PKSK1ljfTXDU4w5jkNlxXOBvCgUFAGHOgaChTkiZpiAx3cdufj+CJMGRYJtFnNDsO rb5ztkmxf3Z1/98ceuGTPC2KgaES/j33zRGIBwsHa6dDe21Yd2G6Vr+pQfy1QNRckW 6InEUUGWVnvOxNDQav33hWtTTWaDMNXMqkFP7ScDxUCIhagQRLFeam1VA3Zh9BUjK9 ooQZl6QDZ6Q2w== Received: by phobos.denx.de (Postfix, from userid 109) id 533E486BB0; Tue, 1 Aug 2023 10:27:59 +0200 (CEST) Received: from mout-u-107.mailbox.org (mout-u-107.mailbox.org [IPv6:2001:67c:2050:101:465::107]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 333A586B92 for ; Tue, 1 Aug 2023 10:27:57 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sr@denx.de Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4RFSsl2249z9sRv; Tue, 1 Aug 2023 10:27:55 +0200 (CEST) Message-ID: <9b9f0bc8-7dfd-c335-7806-032e09912644@denx.de> Date: Tue, 1 Aug 2023 10:27:53 +0200 MIME-Version: 1.0 Subject: Re: [PATCH] i2c: mvtwsi: reset controller if stuck in "bus error" state To: Sam Edwards , u-boot@lists.denx.de Cc: Heiko Schocher References: <20230725221305.49268-1-CFSworks@gmail.com> Content-Language: en-US From: Stefan Roese In-Reply-To: <20230725221305.49268-1-CFSworks@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 On 7/26/23 00:13, Sam Edwards wrote: > The MVTWSI controller can act either as a master or slave device. When > acting as a master, the FSM is driven by the CPU. As a slave, the FSM is > driven by the bus directly. In what is (apparently) a safety mechanism, > if the bus transitions our FSM in any improper way, the FSM goes to a > "bus error" state (0x00). I could find no documented or experimental way > to get the FSM out of this state, except for a controller reset. > > Since U-Boot only uses the MVTWSI controller as a bus master, this > feature only gets in the way: we do not care what happened on the bus > previously as long as the bus is ready for a new transaction. So, when > trying to start a new transaction, check for this state and reset the > controller if necessary. > > Note that this should not be confused with the "deblocking" technique > (used by the `i2c reset` command), which involves pulsing SCL repeatedly > if SDA is found to be held low, in an attempt to force the bus back to > an idle state. This patch only resets the controller in case something > else had previously upset it, and (in principle) results in no > externally-observable change in behavior. > > Signed-off-by: Sam Edwards Reviewed-by: Stefan Roese Thanks, Stefan > --- > drivers/i2c/mvtwsi.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 42 insertions(+) > > diff --git a/drivers/i2c/mvtwsi.c b/drivers/i2c/mvtwsi.c > index 93bbc6916e..14cdb0f663 100644 > --- a/drivers/i2c/mvtwsi.c > +++ b/drivers/i2c/mvtwsi.c > @@ -142,6 +142,8 @@ enum mvtwsi_ctrl_register_fields { > * code. > */ > enum mvstwsi_status_values { > + /* Protocol violation on bus; this is a terminal state */ > + MVTWSI_BUS_ERROR = 0x00, > /* START condition transmitted */ > MVTWSI_STATUS_START = 0x08, > /* Repeated START condition transmitted */ > @@ -525,6 +527,36 @@ static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed, > #endif > } > > +/* > + * __twsi_i2c_reinit() - Reset and reinitialize the I2C controller. > + * > + * This function should be called to get the MVTWSI controller out of the > + * "bus error" state. It saves and restores the baud and address registers. > + * > + * @twsi: The MVTWSI register structure to use. > + * @tick: The duration of a clock cycle at the current I2C speed. > + */ > +static void __twsi_i2c_reinit(struct mvtwsi_registers *twsi, uint tick) > +{ > + uint baud; > + uint slaveadd; > + > + /* Save baud, address registers */ > + baud = readl(&twsi->baudrate); > + slaveadd = readl(&twsi->slave_address); > + > + /* Reset controller */ > + twsi_reset(twsi); > + > + /* Restore baud, address registers */ > + writel(baud, &twsi->baudrate); > + writel(slaveadd, &twsi->slave_address); > + writel(0, &twsi->xtnd_slave_addr); > + > + /* Assert STOP, but don't care for the result */ > + (void) twsi_stop(twsi, tick); > +} > + > /* > * i2c_begin() - Start a I2C transaction. > * > @@ -621,6 +653,11 @@ static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip, > int stop_status; > int expected_start = MVTWSI_STATUS_START; > > + /* Check for (and clear) a bus error from a previous failed transaction > + * or another master on the same bus */ > + if (readl(&twsi->status) == MVTWSI_BUS_ERROR) > + __twsi_i2c_reinit(twsi, tick); > + > if (alen > 0) { > /* Begin i2c write to send the address bytes */ > status = i2c_begin(twsi, expected_start, (chip << 1), tick); > @@ -668,6 +705,11 @@ static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip, > { > int status, stop_status; > > + /* Check for (and clear) a bus error from a previous failed transaction > + * or another master on the same bus */ > + if (readl(&twsi->status) == MVTWSI_BUS_ERROR) > + __twsi_i2c_reinit(twsi, tick); > + > /* Begin i2c write to send first the address bytes, then the > * data bytes */ > status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick); Viele Grüße, Stefan Roese -- DENX Software Engineering GmbH, Managing Director: Erika Unter HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de