From patchwork Mon Feb 1 06:02:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AceLan Kao X-Patchwork-Id: 1434040 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=aW6G/0we; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DTcmV51Klz9tk5; Mon, 1 Feb 2021 17:02:34 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1l6SIB-0006jp-Iy; Mon, 01 Feb 2021 06:02:31 +0000 Received: from mail-pg1-f181.google.com ([209.85.215.181]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l6SI7-0006hB-JE for kernel-team@lists.ubuntu.com; Mon, 01 Feb 2021 06:02:27 +0000 Received: by mail-pg1-f181.google.com with SMTP id r38so11320696pgk.13 for ; Sun, 31 Jan 2021 22:02:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EVd+FMKvy+XMAJfhCMNvC8S6Zt3DNSr0iPYZ/TiuFfw=; b=aW6G/0wen9tTtLAkRIairO1/EmYRYnJUJamEuHQD07v8hijDVNnPchH3+99UjDPAV5 3p5tKebPH4/H5mYykvUQRj3+qVpSI2corwbyj3fOIEjwsYwV4MlQHezkUez09MWluPnI 7MdGaX/aDlFg7isE0acmE1oomN44j7uq4knsRq1kmCsMYALb0RsLENCtoNl8uallQHia GFs+weRuZM79WggfBGHHv2hzYao1W6Taj9KKwwDw83zJbfaVXgR1ExWi8arkBXczAh// 74kW5IwVBheRqEd6YhAZvBz4d9yXz0oAAf9dKISNGlyaZW7Rzv6YqnuZjbCyo1zW6qEK 5msQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=EVd+FMKvy+XMAJfhCMNvC8S6Zt3DNSr0iPYZ/TiuFfw=; b=pbrp4hyu7sAFBXy25gIykwtGYHQI3Gvb3Nebqht8oEC3FFWk5amy/dfUfS6JcNn6I/ iYo8ZFfUvTmluln61PQVyaA3nUrcdm88j/FumaiqSNwEOr0wik6Ygg0Ch2RG4d9qJBGM StnHO9CmzpeuFImClhLDT9dfXxU5UE7ZCNuSmLGND4FBGmm2kMZWiujmanYZYOpE5nSh JLap/P0u3WRzrjCEoFqEbAR75SafW1Z0C8QVzRBNjNhkzfMkSAQBvrYWDyKhILUU0b16 5TBGS1kW23KWdxRWaxUY6mvwFqq7mN8LFkY6scrkQPaOsEeIlVF+0TtYpMvDwDJ8hVvf jyzA== X-Gm-Message-State: AOAM530h89NYIYkSZZghM5i0qtC6ImCJDn65+ugmYu5uUhnBIMHomxtM Ji5BvmwFGjfGtNC7wN98EszHC+dTOnc= X-Google-Smtp-Source: ABdhPJxy9daX4AGDpp8VdZcYyqk0Ohvb9QfMxS45MMXtnpdtosTjpz4N/dvbJK22qzPzb+vAHVrqMw== X-Received: by 2002:a65:654e:: with SMTP id a14mr15740090pgw.265.1612159345597; Sun, 31 Jan 2021 22:02:25 -0800 (PST) Received: from localhost (61-220-137-34.HINET-IP.hinet.net. [61.220.137.34]) by smtp.gmail.com with ESMTPSA id j16sm14126568pjj.18.2021.01.31.22.02.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Jan 2021 22:02:25 -0800 (PST) From: AceLan Kao To: kernel-team@lists.ubuntu.com Subject: [PATCH v4 6/7][SRU][Focal] UBUNTU: ODM: watchdog: ahc1ec0-wdt: Add sub-device watchdog for Advantech embedded controller Date: Mon, 1 Feb 2021 14:02:04 +0800 Message-Id: <20210201060205.355796-7-acelan.kao@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210201060205.355796-1-acelan.kao@canonical.com> References: <20210201060205.355796-1-acelan.kao@canonical.com> MIME-Version: 1.0 Received-SPF: pass client-ip=209.85.215.181; envelope-from=acelan@gmail.com; helo=mail-pg1-f181.google.com X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Campion Kang BugLink: https://bugs.launchpad.net/bugs/1902672 This is one of sub-device driver for Advantech embedded controller AHC1EC0. This driver provide watchdog functionality for Advantech related applications to restart the system. Changed since V5: - remove unnecessary header files - bug fixed: reboot halt if watchdog enabled - Kconfig: add "AHC1EC0" string to clearly define the EC name Signed-off-by: Campion Kang [Add ODM dependency] Signed-off-by: Chia-Lin Kao (AceLan) --- drivers/watchdog/Kconfig | 12 ++ drivers/watchdog/Makefile | 1 + drivers/watchdog/ahc1ec0-wdt.c | 261 +++++++++++++++++++++++++++++++++ 3 files changed, 274 insertions(+) create mode 100644 drivers/watchdog/ahc1ec0-wdt.c diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index 1aa42e879e63..8e4d7be6ffdb 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -1597,6 +1597,18 @@ config NIC7018_WDT To compile this driver as a module, choose M here: the module will be called nic7018_wdt. +config AHC1EC0_WDT + tristate "Advantech AHC1EC0 Watchdog Function" + depends on MFD_AHC1EC0 + depends on UBUNTU_ODM_DRIVERS + help + This is sub-device for Advantech AHC1EC0 embedded controller. + + This driver provide watchdog functionality for Advantech related + applications to restart the system. + To compile thie driver as a module, choose M here: the module will be + called ahc1ec0-wdt. + # M68K Architecture config M54xx_WATCHDOG diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index 2ee352bf3372..ab25b0aae43f 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -142,6 +142,7 @@ obj-$(CONFIG_INTEL_MID_WATCHDOG) += intel-mid_wdt.o obj-$(CONFIG_INTEL_MEI_WDT) += mei_wdt.o obj-$(CONFIG_NI903X_WDT) += ni903x_wdt.o obj-$(CONFIG_NIC7018_WDT) += nic7018_wdt.o +obj-$(CONFIG_AHC1EC0_WDT) += ahc1ec0-wdt.o obj-$(CONFIG_MLX_WDT) += mlx_wdt.o # M68K Architecture diff --git a/drivers/watchdog/ahc1ec0-wdt.c b/drivers/watchdog/ahc1ec0-wdt.c new file mode 100644 index 000000000000..4497b6106b24 --- /dev/null +++ b/drivers/watchdog/ahc1ec0-wdt.c @@ -0,0 +1,261 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Watchdog Driver for Advantech Embedded Controller chip AHC1EC0 + * + * Copyright 2020, Advantech IIoT Group + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DRV_NAME "ahc1ec0-wdt" + +struct ec_wdt_data { + struct watchdog_device wdtdev; + struct adv_ec_platform_data *adv_ec_data; + struct notifier_block reboot_nb; + struct mutex lock_ioctl; + int is_enable; + int current_timeout; +}; + +#define EC_WDT_MIN_TIMEOUT 1 /* The watchdog devices minimum timeout value (in seconds). */ +#define EC_WDT_MAX_TIMEOUT 600 /* The watchdog devices maximum timeout value (in seconds) */ +#define EC_WDT_DEFAULT_TIMEOUT 45 + +static int set_delay(struct adv_ec_platform_data *adv_ec_data, unsigned short delay_timeout_in_ms) +{ + if (write_hw_ram(adv_ec_data, EC_RESET_DELAY_TIME_L, delay_timeout_in_ms & 0x00FF)) { + pr_err("Failed to set Watchdog Retset Time Low byte."); + return -EINVAL; + } + + if (write_hw_ram(adv_ec_data, EC_RESET_DELAY_TIME_H, (delay_timeout_in_ms & 0xFF00) >> 8)) { + pr_err("Failed to set Watchdog Retset Time Hight byte."); + return -EINVAL; + } + + return 0; +} + +static int advwdt_set_heartbeat(unsigned long t) +{ + if (t < 1 || t > 6553) { + pr_err("%s: the input timeout is out of range.", __func__); + pr_err("Please choose valid data between 1 ~ 6553."); + return -EINVAL; + } + + return (t * 10); +} + +/* Notifier for system down */ +static int advwdt_notify_sys(struct notifier_block *nb, unsigned long code, void *data) +{ + if (code == SYS_DOWN || code == SYS_HALT) { + struct ec_wdt_data *ec_wdt_data; + + ec_wdt_data = container_of(nb, struct ec_wdt_data, reboot_nb); + if (!ec_wdt_data) + return NOTIFY_BAD; + + /* Turn the WDT off */ + if (write_hwram_command(ec_wdt_data->adv_ec_data, EC_WDT_STOP)) { + pr_err("Failed to set Watchdog stop."); + return -EINVAL; + } + ec_wdt_data->is_enable = 0; + pr_info("%s: notify sys shutdown", __func__); + } + + return NOTIFY_DONE; +} + +static int ec_wdt_start(struct watchdog_device *wdd) +{ + int ret; + int timeout, timeout_in_ms; + struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd); + struct adv_ec_platform_data *adv_ec_data; + + dev_dbg(wdd->parent, "%s\n", __func__); + + adv_ec_data = ec_wdt_data->adv_ec_data; + timeout = wdd->timeout; /* The watchdog devices timeout value (in seconds). */ + + mutex_lock(&ec_wdt_data->lock_ioctl); + + timeout_in_ms = advwdt_set_heartbeat(timeout); + if (timeout_in_ms < 0) { + mutex_unlock(&ec_wdt_data->lock_ioctl); + return timeout_in_ms; + } + + ret = set_delay(adv_ec_data, (unsigned short)(timeout_in_ms-1)); + if (ret) { + dev_err(wdd->parent, "Failed to set Watchdog delay (ret=%x).\n", ret); + mutex_unlock(&ec_wdt_data->lock_ioctl); + return ret; + } + ret = write_hwram_command(adv_ec_data, EC_WDT_STOP); + ret = write_hwram_command(adv_ec_data, EC_WDT_START); + if (ret) { + dev_err(wdd->parent, "Failed to set Watchdog start (ret=%x).\n", ret); + mutex_unlock(&ec_wdt_data->lock_ioctl); + return ret; + } + ec_wdt_data->is_enable = 1; + ec_wdt_data->current_timeout = timeout_in_ms/10; + + mutex_unlock(&ec_wdt_data->lock_ioctl); + return 0; +} + +static int ec_wdt_stop(struct watchdog_device *wdd) +{ + int ret; + struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd); + struct adv_ec_platform_data *adv_ec_data; + + dev_dbg(wdd->parent, "%s\n", __func__); + + adv_ec_data = ec_wdt_data->adv_ec_data; + + mutex_lock(&ec_wdt_data->lock_ioctl); + ret = write_hwram_command(adv_ec_data, EC_WDT_STOP); + mutex_unlock(&ec_wdt_data->lock_ioctl); + if (ret) + pr_err("Failed to set Watchdog stop."); + else + ec_wdt_data->is_enable = 0; + + return ret; +} + +static int ec_wdt_ping(struct watchdog_device *wdd) +{ + int ret; + struct ec_wdt_data *ec_wdt_data = watchdog_get_drvdata(wdd); + struct adv_ec_platform_data *adv_ec_data; + + dev_dbg(wdd->parent, "%s\n", __func__); + + adv_ec_data = ec_wdt_data->adv_ec_data; + + mutex_lock(&ec_wdt_data->lock_ioctl); + ret = write_hwram_command(adv_ec_data, EC_WDT_RESET); + mutex_unlock(&ec_wdt_data->lock_ioctl); + if (ret) { + pr_err("Failed to set Watchdog reset."); + return -EINVAL; + } + + return 0; +} + +static int ec_wdt_set_timeout(struct watchdog_device *wdd, + unsigned int timeout) +{ + dev_dbg(wdd->parent, "%s, timeout=%d\n", __func__, timeout); + + wdd->timeout = timeout; + + if (watchdog_active(wdd)) + return ec_wdt_start(wdd); + + return 0; +} + +static const struct watchdog_info ec_watchdog_info = { + .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, + .identity = "AHC1EC0 Watchdog", +}; + +static const struct watchdog_ops ec_watchdog_ops = { + .owner = THIS_MODULE, + .start = ec_wdt_start, + .stop = ec_wdt_stop, + .ping = ec_wdt_ping, + .set_timeout = ec_wdt_set_timeout, +}; + +static int adv_ec_wdt_probe(struct platform_device *pdev) +{ + int ret; + struct device *dev = &pdev->dev; + struct adv_ec_platform_data *adv_ec_data; + struct ec_wdt_data *ec_wdt_data; + struct watchdog_device *wdd; + + dev_info(dev, "watchdog probe start\n"); + + adv_ec_data = dev_get_drvdata(dev->parent); + if (!adv_ec_data) + return -EINVAL; + + ec_wdt_data = devm_kzalloc(dev, sizeof(struct ec_wdt_data), GFP_KERNEL); + if (!ec_wdt_data) + return -ENOMEM; + + mutex_init(&ec_wdt_data->lock_ioctl); + + ec_wdt_data->adv_ec_data = adv_ec_data; + wdd = &ec_wdt_data->wdtdev; + + watchdog_init_timeout(&ec_wdt_data->wdtdev, 0, dev); + + //watchdog_set_nowayout(&ec_wdt_data->wdtdev, WATCHDOG_NOWAYOUT); + watchdog_set_drvdata(&ec_wdt_data->wdtdev, ec_wdt_data); + platform_set_drvdata(pdev, ec_wdt_data); + + wdd->info = &ec_watchdog_info; + wdd->ops = &ec_watchdog_ops; + wdd->min_timeout = EC_WDT_MIN_TIMEOUT; + wdd->max_timeout = EC_WDT_MAX_TIMEOUT; + wdd->parent = dev; + + ec_wdt_data->wdtdev.timeout = EC_WDT_DEFAULT_TIMEOUT; + ec_wdt_data->is_enable = 0; + ec_wdt_data->current_timeout = EC_WDT_DEFAULT_TIMEOUT; + + watchdog_stop_on_unregister(wdd); + + ec_wdt_data->reboot_nb.notifier_call = advwdt_notify_sys; + ret = devm_register_reboot_notifier(dev, &ec_wdt_data->reboot_nb); + if (ret) { + dev_err(dev, "watchdog%d: Cannot register reboot notifier (%d)\n", + wdd->id, ret); + return ret; + } + + ret = devm_watchdog_register_device(dev, wdd); + if (ret != 0) + dev_err(dev, "watchdog_register_device() failed: %d\n", + ret); + + dev_info(dev, "watchdog register success\n"); + + return 0; +} + +static struct platform_driver adv_wdt_drv = { + .driver = { + .name = DRV_NAME, + }, + .probe = adv_ec_wdt_probe, +}; +module_platform_driver(adv_wdt_drv); + +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:" DRV_NAME); +MODULE_DESCRIPTION("Advantech Embedded Controller Watchdog Driver."); +MODULE_AUTHOR("Campion Kang "); +MODULE_VERSION("1.0");