Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1042219ybh; Sun, 19 Jul 2020 06:56:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyonjbqp91Ylj3w+awUIkKr5REU0mv79FW6phJfpWEFD/0I1bRe9GvFKYW57Sk6Uu0ksiOz X-Received: by 2002:a17:906:e25a:: with SMTP id gq26mr16338976ejb.152.1595167006598; Sun, 19 Jul 2020 06:56:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595167006; cv=none; d=google.com; s=arc-20160816; b=FyIWZfS6zkMCH9oVAfnn5myTE+q/URMrzinQYCc+K5OypiMxehGzP6cS6COaigcu5K r6G8lDsqYDlKc6rW4z16aLF5+hiCwXVBLKM8sNBJkmeD4L/NDiG/tk5MKwGBFhCHu76H Gd27nLe+iPKAquqwquDG4kQvgwt7QuQS/a8rGzyFiOjaSXzZsVhknWMq/fm+xOWYtevP wBBIIIV96K6hKG7r5mif3z+WhxeZ/rRKa9lesenmcJNRlVbJtOeOE4d0oDm1YOQQausT y/XZMzx1jSxxL8vMDHumcFWf0h22q66H+X5GobBmqVtQ5aK4N9ywn5zmsm64jEP8I2Rt 5oWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject:dkim-signature; bh=UgldB4wGcq1fUnLz6Vxctj8j+9cNDnU87erHnoDl4AA=; b=gEKgOihKjkIDnh1BIQpve2f+xh/yOtHNau8KKgZI2Imqy86+PiU7qqUXqylTzESVMt McbEok6znJDi+j9OamVJ5LEorQFVmMqA6zC6UiEh09uTKZBJayizpF70lK4PkhT58q8g WEMKacOBhVMy9/xrpTNRMX4zHm8RuCKh+nlDg4apaZ2yBDEGI3j3XVMP3bpWM6KbqCLk C2QFzfxdXv+vzjoDTvki+EWAggJPq/fkzV14gXWo6n8mL3MzxXY3DVHjINeQnt72AP/u IZu1C3zUzEJEisKmUVmUaXoyN7uGBcIkM4COkbMvJUWODl7T8RnqR0VaxdTVNkqY8JMY mfig== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=gFiQAMp9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w17si4444342edx.522.2020.07.19.06.56.23; Sun, 19 Jul 2020 06:56:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=gFiQAMp9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726413AbgGSN4O (ORCPT + 99 others); Sun, 19 Jul 2020 09:56:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725988AbgGSN4O (ORCPT ); Sun, 19 Jul 2020 09:56:14 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9688C0619D2; Sun, 19 Jul 2020 06:56:13 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id p1so7488117pls.4; Sun, 19 Jul 2020 06:56:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=UgldB4wGcq1fUnLz6Vxctj8j+9cNDnU87erHnoDl4AA=; b=gFiQAMp9vaE1h9NNuH0HtjbwSwKuZekC+BTsKF87Ln2lB4MIAu6ECudR9uTfRkoUw6 jF3U3tiqjUvL82jeDcI71Xcb66cSu3q6oFitOzeZYfArrVuZP667kxzXwAnxomzTGRPW j5Ptr+xtCXpx77erU9rQfNHQXThWYS2QQBEeMY/wsxjvgxKJEb91q3bJsqCKlAtAEos3 riNDai3QsANIcj8E4GtEqq2tMFgDpDJh/FLZQDi1eYnPK1qt6dYJ4uMLuA8mmhoKTR4+ ewiVS7pdhPuziesBncgWgYId3Hr3oa7JBgySeLT/farMiZTsVwwfZo8jfa4QMziGBjxn npXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=UgldB4wGcq1fUnLz6Vxctj8j+9cNDnU87erHnoDl4AA=; b=U26tCeE/Eg75pgWeQbMqtVERgJmZsvHxubXX4FlGbnLIIt1i5rP/EyIWQG2lQPoq6O XvG+mYsJBah4t6AcJV0GYxSMPUiO6WDeTK45fXxNYiXv9x2t08FnIYbK75JQ3AEFlYx5 NpGYNaSFUcpD2YopbZ2gZcDCwqikyxYmyz/3oFD0hVJsTeLrc8sGGHJFJP+TjiGIT0uI F9jSfGIfngswdOP6LeWTSpK2HN6BbRigAwGiyFrrLOtCEGNZGzBJ4ekcH7lgvKo+g8/P oF7GJwZPRNXkIW4XPkZgkuYH28gy+5uiAsDWjLnHkE079sZhi3QFJCV+UwKFuY/GFO3H 5fDA== X-Gm-Message-State: AOAM530X/tJgYFSI17Bgk5kjps02uUxZjkHncJY/kp3cI59iuT2Nkfyj vJIL3NGJWHnxzLlGBdQM+38= X-Received: by 2002:a17:902:8681:: with SMTP id g1mr14923298plo.303.1595166973340; Sun, 19 Jul 2020 06:56:13 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id n15sm8776089pjf.12.2020.07.19.06.56.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Jul 2020 06:56:12 -0700 (PDT) Subject: Re: [PATCHv4 3/4] watchdog: rti-wdt: attach to running watchdog during probe To: Tero Kristo , wim@linux-watchdog.org, linux-watchdog@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jan.kiszka@siemens.com References: <20200717132958.14304-1-t-kristo@ti.com> <20200717132958.14304-4-t-kristo@ti.com> From: Guenter Roeck Autocrypt: addr=linux@roeck-us.net; keydata= xsFNBE6H1WcBEACu6jIcw5kZ5dGeJ7E7B2uweQR/4FGxH10/H1O1+ApmcQ9i87XdZQiB9cpN RYHA7RCEK2dh6dDccykQk3bC90xXMPg+O3R+C/SkwcnUak1UZaeK/SwQbq/t0tkMzYDRxfJ7 nyFiKxUehbNF3r9qlJgPqONwX5vJy4/GvDHdddSCxV41P/ejsZ8PykxyJs98UWhF54tGRWFl 7i1xvaDB9lN5WTLRKSO7wICuLiSz5WZHXMkyF4d+/O5ll7yz/o/JxK5vO/sduYDIlFTvBZDh gzaEtNf5tQjsjG4io8E0Yq0ViobLkS2RTNZT8ICq/Jmvl0SpbHRvYwa2DhNsK0YjHFQBB0FX IdhdUEzNefcNcYvqigJpdICoP2e4yJSyflHFO4dr0OrdnGLe1Zi/8Xo/2+M1dSSEt196rXaC kwu2KgIgmkRBb3cp2vIBBIIowU8W3qC1+w+RdMUrZxKGWJ3juwcgveJlzMpMZNyM1jobSXZ0 VHGMNJ3MwXlrEFPXaYJgibcg6brM6wGfX/LBvc/haWw4yO24lT5eitm4UBdIy9pKkKmHHh7s jfZJkB5fWKVdoCv/omy6UyH6ykLOPFugl+hVL2Prf8xrXuZe1CMS7ID9Lc8FaL1ROIN/W8Vk BIsJMaWOhks//7d92Uf3EArDlDShwR2+D+AMon8NULuLBHiE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FKFOkynOs3pVbcbq1pxbhk3TRWCGRU5JolI4ohy/7JV1TVbjiDI HP/aVnm6NC8of26P40Pg8EdAhajZnHHjA7FrJXsy3cyIGqvg9os4rNkUWmrCfLLsZDHD8FnU mDW4+i+XlNFUPUYMrIKi9joBhu18ssf5i5Q= Message-ID: <51c30a7c-a0e3-fc0b-2550-64749480a5fb@roeck-us.net> Date: Sun, 19 Jul 2020 06:56:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200717132958.14304-4-t-kristo@ti.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/17/20 6:29 AM, Tero Kristo wrote: > If the RTI watchdog is running already during probe, the driver must > configure itself to match the HW. Window size and timeout is probed from > hardware, and the last keepalive ping is adjusted to match it also. > > Signed-off-by: Tero Kristo Reviewed-by: Guenter Roeck > --- > drivers/watchdog/rti_wdt.c | 112 +++++++++++++++++++++++++++++++++---- > 1 file changed, 102 insertions(+), 10 deletions(-) > > diff --git a/drivers/watchdog/rti_wdt.c b/drivers/watchdog/rti_wdt.c > index d456dd72d99a..7cbdc178ffe8 100644 > --- a/drivers/watchdog/rti_wdt.c > +++ b/drivers/watchdog/rti_wdt.c > @@ -35,7 +35,11 @@ > > #define RTIWWDRX_NMI 0xa > > -#define RTIWWDSIZE_50P 0x50 > +#define RTIWWDSIZE_50P 0x50 > +#define RTIWWDSIZE_25P 0x500 > +#define RTIWWDSIZE_12P5 0x5000 > +#define RTIWWDSIZE_6P25 0x50000 > +#define RTIWWDSIZE_3P125 0x500000 > > #define WDENABLE_KEY 0xa98559da > > @@ -48,7 +52,7 @@ > > #define DWDST BIT(1) > > -static int heartbeat; > +static int heartbeat = DEFAULT_HEARTBEAT; > > /* > * struct to hold data for each WDT device > @@ -79,11 +83,9 @@ static int rti_wdt_start(struct watchdog_device *wdd) > * be petted during the open window; not too early or not too late. > * The HW configuration options only allow for the open window size > * to be 50% or less than that; we obviouly want to configure the open > - * window as large as possible so we select the 50% option. To avoid > - * any glitches, we accommodate 5% safety margin also, so we setup > - * the min_hw_hearbeat at 55% of the timeout period. > + * window as large as possible so we select the 50% option. > */ > - wdd->min_hw_heartbeat_ms = 11 * wdd->timeout * 1000 / 20; > + wdd->min_hw_heartbeat_ms = 500 * wdd->timeout; > > /* Generate NMI when wdt expires */ > writel_relaxed(RTIWWDRX_NMI, wdt->base + RTIWWDRXCTRL); > @@ -110,7 +112,48 @@ static int rti_wdt_ping(struct watchdog_device *wdd) > return 0; > } > > -static unsigned int rti_wdt_get_timeleft(struct watchdog_device *wdd) > +static int rti_wdt_setup_hw_hb(struct watchdog_device *wdd, u32 wsize) > +{ > + /* > + * RTI only supports a windowed mode, where the watchdog can only > + * be petted during the open window; not too early or not too late. > + * The HW configuration options only allow for the open window size > + * to be 50% or less than that. > + */ > + switch (wsize) { > + case RTIWWDSIZE_50P: > + /* 50% open window => 50% min heartbeat */ > + wdd->min_hw_heartbeat_ms = 500 * heartbeat; > + break; > + > + case RTIWWDSIZE_25P: > + /* 25% open window => 75% min heartbeat */ > + wdd->min_hw_heartbeat_ms = 750 * heartbeat; > + break; > + > + case RTIWWDSIZE_12P5: > + /* 12.5% open window => 87.5% min heartbeat */ > + wdd->min_hw_heartbeat_ms = 875 * heartbeat; > + break; > + > + case RTIWWDSIZE_6P25: > + /* 6.5% open window => 93.5% min heartbeat */ > + wdd->min_hw_heartbeat_ms = 935 * heartbeat; > + break; > + > + case RTIWWDSIZE_3P125: > + /* 3.125% open window => 96.9% min heartbeat */ > + wdd->min_hw_heartbeat_ms = 969 * heartbeat; > + break; > + > + default: > + return -EINVAL; > + } > + > + return 0; > +} > + > +static unsigned int rti_wdt_get_timeleft_ms(struct watchdog_device *wdd) > { > u64 timer_counter; > u32 val; > @@ -123,11 +166,18 @@ static unsigned int rti_wdt_get_timeleft(struct watchdog_device *wdd) > > timer_counter = readl_relaxed(wdt->base + RTIDWDCNTR); > > + timer_counter *= 1000; > + > do_div(timer_counter, wdt->freq); > > return timer_counter; > } > > +static unsigned int rti_wdt_get_timeleft(struct watchdog_device *wdd) > +{ > + return rti_wdt_get_timeleft_ms(wdd) / 1000; > +} > + > static const struct watchdog_info rti_wdt_info = { > .options = WDIOF_KEEPALIVEPING, > .identity = "K3 RTI Watchdog", > @@ -148,6 +198,7 @@ static int rti_wdt_probe(struct platform_device *pdev) > struct watchdog_device *wdd; > struct rti_wdt_device *wdt; > struct clk *clk; > + u32 last_ping = 0; > > wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); > if (!wdt) > @@ -169,6 +220,14 @@ static int rti_wdt_probe(struct platform_device *pdev) > return -EINVAL; > } > > + /* > + * If watchdog is running at 32k clock, it is not accurate. > + * Adjust frequency down in this case so that we don't pet > + * the watchdog too often. > + */ > + if (wdt->freq < 32768) > + wdt->freq = wdt->freq * 9 / 10; > + > pm_runtime_enable(dev); > ret = pm_runtime_get_sync(dev); > if (ret) { > @@ -185,11 +244,8 @@ static int rti_wdt_probe(struct platform_device *pdev) > wdd->min_timeout = 1; > wdd->max_hw_heartbeat_ms = (WDT_PRELOAD_MAX << WDT_PRELOAD_SHIFT) / > wdt->freq * 1000; > - wdd->timeout = DEFAULT_HEARTBEAT; > wdd->parent = dev; > > - watchdog_init_timeout(wdd, heartbeat, dev); > - > watchdog_set_drvdata(wdd, wdt); > watchdog_set_nowayout(wdd, 1); > watchdog_set_restart_priority(wdd, 128); > @@ -201,12 +257,48 @@ static int rti_wdt_probe(struct platform_device *pdev) > goto err_iomap; > } > > + if (readl(wdt->base + RTIDWDCTRL) == WDENABLE_KEY) { > + u32 time_left_ms; > + u64 heartbeat_ms; > + u32 wsize; > + > + set_bit(WDOG_HW_RUNNING, &wdd->status); > + time_left_ms = rti_wdt_get_timeleft_ms(wdd); > + heartbeat_ms = readl(wdt->base + RTIDWDPRLD); > + heartbeat_ms <<= WDT_PRELOAD_SHIFT; > + heartbeat_ms *= 1000; > + do_div(heartbeat_ms, wdt->freq); > + if (heartbeat_ms != heartbeat * 1000) > + dev_warn(dev, "watchdog already running, ignoring heartbeat config!\n"); > + > + heartbeat = heartbeat_ms; > + heartbeat /= 1000; > + > + wsize = readl(wdt->base + RTIWWDSIZECTRL); > + ret = rti_wdt_setup_hw_hb(wdd, wsize); > + if (ret) { > + dev_err(dev, "bad window size.\n"); > + goto err_iomap; > + } > + > + last_ping = heartbeat_ms - time_left_ms; > + if (time_left_ms > heartbeat_ms) { > + dev_warn(dev, "time_left > heartbeat? Assuming last ping just before now.\n"); > + last_ping = 0; > + } > + } > + > + watchdog_init_timeout(wdd, heartbeat, dev); > + > ret = watchdog_register_device(wdd); > if (ret) { > dev_err(dev, "cannot register watchdog device\n"); > goto err_iomap; > } > > + if (last_ping) > + watchdog_set_last_hw_keepalive(wdd, last_ping); > + > return 0; > > err_iomap: >