Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1272710imm; Fri, 14 Sep 2018 14:29:50 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYQeFBQRZcWCATK+aKKjyo+4kRv9edtzxV8HO5Gy0CPMWyQ4NryrjNQJdc2/jBZTor3b2n/ X-Received: by 2002:a17:902:561:: with SMTP id 88-v6mr13845137plf.320.1536960590925; Fri, 14 Sep 2018 14:29:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536960590; cv=none; d=google.com; s=arc-20160816; b=Lz791jXWrS2Fmn8lVPkYSoE1CYzH2vuZmZ1tltZoG0w+hb94Yz1AkkVx4QoMloSwIt y7i3GIRIHVBUsiXGM3Ac7o3M4R7LYoZRPTimfuKBWkvClsa6+kBdLmzsCnan/4olmtwR wtj9M3QkKmtDDm6Sstqg509LVyzcoMFFzTDPQus6DFN1avesx4HaUtOTEeQXmzS4xr3C SIXyxEC0kyoq14j2Q66DnJ7YC9Y0MOqoe/Qplf+py6WMsGsOHdZjvJTAC36y7gPfliIF MiiQMj7GZfYQ2NguQzDhVAlHQUgq23Zx2ERluQfs0zORdoUoeAh+hN0aRUDgC97Tsjqd qE1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=1w4T8MQ1D00c8c39poICnF9PjfT9QzjBedt6qyZsCFI=; b=BE+TIV/1IeKscxhb82CWukxaWn7URR2B+DQqnHvcTONI4Z/5CfErILi+Rmm4u65wad E8/BFPkv1PuMjKxuOn6SsJ4Q/20MZIhOReZ9SWKvl1/YxR2/pUp/vACs04dqhfdAJKMq l/AZibGRerRly1OAlzpbwUCwpm/blgz5NTxy41Pp/dwuoQQaU/kWwKMxKFVTPzrnouop k/I4ACXPezN9KoWXCdpRgwvEFtRNqkKlsztnzq0sj51hFOdcIeju7DTEVV/rqvx8eT4W 06ybiwxwsWzyoHNjEAklLOYOq63VAbInJb1EAh9xkLCF6U7BzpHrc31mXxcjzXh/tFi6 Ugcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=FNvTskKD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1-v6si7460521plc.168.2018.09.14.14.29.33; Fri, 14 Sep 2018 14:29:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=FNvTskKD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728184AbeIOCpe (ORCPT + 99 others); Fri, 14 Sep 2018 22:45:34 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:40609 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727749AbeIOCpe (ORCPT ); Fri, 14 Sep 2018 22:45:34 -0400 Received: by mail-pl1-f196.google.com with SMTP id s17-v6so4736322plp.7; Fri, 14 Sep 2018 14:29:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=1w4T8MQ1D00c8c39poICnF9PjfT9QzjBedt6qyZsCFI=; b=FNvTskKDIdiIGvH4m68wApgNWyjYJIPqPDzr9sA7KEjXDRTH6XBmnv+6SvMlQCJvPG oF06+Ninao0y1HaD4+r98UF7FmIX2EJsDkdBjFOndtfRKc6TwvM2HJAvm6AgVD+ABqZ/ XrvN7V8Y6TwchAr3LUJXvA7B20Ga/59/+F9zFUAtlAezrl4elZhrFjLqPoZqaOIYrRyn gdtxMQ1hColacxkvegSQ4VmssHCuiodRtRf3Y7JSgxJJhmC/FzqoVs0lp0xYMj5zYlOn ExH6bispvp6QBbBDye+3MP+wWujKzSxGe6H0MsmW6C3tGXicyDUEVIRlPeiGfX8pLJjO 8IJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=1w4T8MQ1D00c8c39poICnF9PjfT9QzjBedt6qyZsCFI=; b=PER8qzXN7Q3p+dZ0CWDl5jnV/IhFPMI1PCCqrFBB+f3ubBx2bWI5WyyUPxvQ1Qcd4x bhO2u0hiz/bYnw9vy15JEaSNjYN2S0E/TL+hGEex+WA6Xf/sgUmVtCqYCSZ2HKwRGKHV EP42p+VLfXElaTOkOdSt6amdNMTm5ypMqefx8H7h0JagvET60gDNRcbGHjiBkmYSHTmJ dgPJiiN2ofK3GwOilAtTE9Cd4N3Akin72DkJ391BEir7EnArjTefo9WwZti4cM8pjAxh UBmSqXyjiuj6G9FkDXGx7+DPK/KpDfcaQnLpQ6m6LB7WkZeTFeYUKm80yDEXAll3QiKD qoKQ== X-Gm-Message-State: APzg51B1z/566Hri6l+pA9GbPRVJGrBZPpIBtSz6M9noHIlEL10Ihyp5 +v9MpEPsa9doTWpc659qntU= X-Received: by 2002:a17:902:2006:: with SMTP id n6-v6mr13643488pla.325.1536960556726; Fri, 14 Sep 2018 14:29:16 -0700 (PDT) Received: from localhost (108-223-40-66.lightspeed.sntcca.sbcglobal.net. [108.223.40.66]) by smtp.gmail.com with ESMTPSA id u184-v6sm14697862pgd.46.2018.09.14.14.29.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Sep 2018 14:29:16 -0700 (PDT) Date: Fri, 14 Sep 2018 14:29:15 -0700 From: Guenter Roeck To: Romain Izard Cc: Nicolas Ferre , Alexandre Belloni , Wim Van Sebroeck , Marcus Folkesson , linux-arm-kernel@lists.infradead.org, linux-watchdog@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] watchdog: sama5d4: write the mode register in two steps Message-ID: <20180914212915.GA31857@roeck-us.net> References: <20180914101339.7382-1-romain.izard.pro@gmail.com> <20180914101339.7382-3-romain.izard.pro@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180914101339.7382-3-romain.izard.pro@gmail.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 14, 2018 at 12:13:39PM +0200, Romain Izard wrote: > The specification for SAMA5D2 and SAMA5D4 chips, that use this IP for > their watchdog timer, has the following advice regarding the Mode Register: > > "When setting the WDDIS bit, and while it is set, the fields WDV and WDD > must not be modified." > > I have observed on a board based on a SAMA5D2 chip that using any other > timeout duration than the default 16s in the device tree will reset the > board when the watchdog device is opened; this is probably due to ignoring > the aforementioned constraint. > > To fix this, read the content of the Mode Register before writing it, > and split the access into two parts if WDV or WDD need to be changed. > > Signed-off-by: Romain Izard That code now looks terribly complicated. Assuming it is indeed necessary, would it be possible to update the affected register in its own function ? Guenter > --- > drivers/watchdog/sama5d4_wdt.c | 27 ++++++++++++++++++++------- > 1 file changed, 20 insertions(+), 7 deletions(-) > > diff --git a/drivers/watchdog/sama5d4_wdt.c b/drivers/watchdog/sama5d4_wdt.c > index 1e93c1b0e3cf..1e05268ad94b 100644 > --- a/drivers/watchdog/sama5d4_wdt.c > +++ b/drivers/watchdog/sama5d4_wdt.c > @@ -46,7 +46,10 @@ MODULE_PARM_DESC(nowayout, > "Watchdog cannot be stopped once started (default=" > __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); > > -#define wdt_enabled (!(wdt->mr & AT91_WDT_WDDIS)) > +#define wdt_enabled(reg) (!((reg) & AT91_WDT_WDDIS)) > + > +#define wdt_different_counters(reg_a, reg_b) \ > + (((reg_a) ^ (reg_b)) & (AT91_WDT_WDV | AT91_WDT_WDD)) > > #define wdt_read(wdt, field) \ > readl_relaxed((wdt)->reg_base + (field)) > @@ -78,8 +81,11 @@ static void wdt_write_nosleep(struct sama5d4_wdt *wdt, u32 field, u32 val) > static int sama5d4_wdt_start(struct watchdog_device *wdd) > { > struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); > + u32 reg = wdt_read(wdt, AT91_WDT_MR); > > wdt->mr &= ~AT91_WDT_WDDIS; > + if (!wdt_enabled(reg) && wdt_different_counters(reg, wdt->mr)) > + wdt_write(wdt, AT91_WDT_MR, reg & ~AT91_WDT_WDDIS); > wdt_write(wdt, AT91_WDT_MR, wdt->mr); > > return 0; > @@ -88,8 +94,11 @@ static int sama5d4_wdt_start(struct watchdog_device *wdd) > static int sama5d4_wdt_stop(struct watchdog_device *wdd) > { > struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd); > + u32 reg = wdt_read(wdt, AT91_WDT_MR); > > wdt->mr |= AT91_WDT_WDDIS; > + if (wdt_enabled(reg) && wdt_different_counters(reg, wdt->mr)) > + wdt_write(wdt, AT91_WDT_MR, wdt->mr & ~AT91_WDT_WDDIS); > wdt_write(wdt, AT91_WDT_MR, wdt->mr); > > return 0; > @@ -122,7 +131,7 @@ static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd, > * If the watchdog is enabled, then the timeout can be updated. Else, > * wait that the user enables it. > */ > - if (wdt_enabled) > + if (wdt_enabled(wdt->mr)) > wdt_write(wdt, AT91_WDT_MR, wdt->mr & ~AT91_WDT_WDDIS); > > wdd->timeout = timeout; > @@ -186,13 +195,17 @@ static int sama5d4_wdt_init(struct sama5d4_wdt *wdt) > * If the watchdog is already running, we can safely update it. > * Else, we have to disable it properly. > */ > - if (wdt_enabled) { > + reg = wdt_read(wdt, AT91_WDT_MR); > + if (wdt_enabled(reg)) { > + if (!wdt_enabled(wdt->mr)) > + wdt_write_nosleep(wdt, AT91_WDT_MR, > + wdt->mr & ~AT91_WDT_WDDIS); > wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr); > - } else { > - reg = wdt_read(wdt, AT91_WDT_MR); > - if (!(reg & AT91_WDT_WDDIS)) > + } else if (wdt_enabled(wdt->mr)) { > + if (wdt_different_counters(reg, wdt->mr)) > wdt_write_nosleep(wdt, AT91_WDT_MR, > - reg | AT91_WDT_WDDIS); > + reg & ~AT91_WDT_WDDIS); > + wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr); > } > return 0; > } > -- > 2.17.1 >