linux-watchdog.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] [PATCH] watchdog: Exar/MaxLinear XR28V38x driver
@ 2022-07-22  9:51 David Müller
  2022-07-22 15:08 ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: David Müller @ 2022-07-22  9:51 UTC (permalink / raw)
  To: linux-watchdog

Simple driver for the watchdog present in some Exar/MaxLinear UART chips.
Please see https://www.maxlinear.com/product/interface/uarts/lpc-uarts/xr28v384
for more info.

Signed-off-by: David Müller <d.mueller@elsoft.ch>
---
 drivers/watchdog/Kconfig    |  11 ++
 drivers/watchdog/Makefile   |   1 +
 drivers/watchdog/exar_wdt.c | 364 ++++++++++++++++++++++++++++++++++++
 3 files changed, 376 insertions(+)
 create mode 100644 drivers/watchdog/exar_wdt.c

diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index 32fd37698932..5849c066609c 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -1089,6 +1089,17 @@ config EBC_C384_WDT
 	  WinSystems EBC-C384 motherboard. The timeout may be configured via
 	  the timeout module parameter.
 
+config EXAR_WDT
+	tristate "Exar Watchdog Timer"
+	depends on X86
+	select WATCHDOG_CORE
+	help
+	  Enables watchdog timer support for the watchdog timer present
+	  in some Exar/MaxLinear UART chips like the XR28V38x.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called exar_wdt.
+
 config F71808E_WDT
 	tristate "Fintek F718xx, F818xx Super I/O Watchdog"
 	depends on X86
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index c324e9d820e9..31b9bdb47b4e 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -105,6 +105,7 @@ obj-$(CONFIG_ADVANTECH_WDT) += advantechwdt.o
 obj-$(CONFIG_ALIM1535_WDT) += alim1535_wdt.o
 obj-$(CONFIG_ALIM7101_WDT) += alim7101_wdt.o
 obj-$(CONFIG_EBC_C384_WDT) += ebc-c384_wdt.o
+obj-$(CONFIG_EXAR_WDT) += exar_wdt.o
 obj-$(CONFIG_F71808E_WDT) += f71808e_wdt.o
 obj-$(CONFIG_SP5100_TCO) += sp5100_tco.o
 obj-$(CONFIG_GEODE_WDT) += geodewdt.o
diff --git a/drivers/watchdog/exar_wdt.c b/drivers/watchdog/exar_wdt.c
new file mode 100644
index 000000000000..1adf25bf2bc6
--- /dev/null
+++ b/drivers/watchdog/exar_wdt.c
@@ -0,0 +1,364 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *	exar_wdt.c - Driver for the watchdog present in some
+ *		     Exar/MaxLinear UART chips like the XR28V38x.
+ *
+ *	(c) Copyright 2022 D. Müller <d.mueller@elsoft.ch>.
+ *
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+/* Includes */
+#include <linux/io.h>			/* For inb() / outb() */
+#include <linux/module.h>		/* For module specific items */
+#include <linux/platform_device.h>	/* For platform_driver framework */
+#include <linux/watchdog.h>		/* For the watchdog specific items */
+
+#define DRV_NAME	"exar_wdt"
+
+/* SIO definitions */
+static const unsigned short sio_config_ports[] = { 0x2e, 0x4e };
+/* there are 4 possible SIO enter keys */
+static const unsigned char sio_enter_keys[] = { 0x67, 0x77, 0x87, 0xA0 };
+#define EXAR_EXIT_KEY	0xAA
+
+#define EXAR_LDN	0x07
+#define EXAR_DID	0x20
+#define EXAR_VID	0x23
+#define EXAR_WDT	0x26
+#define EXAR_ACT	0x30
+#define EXAR_RTBASE	0x60
+
+/* WDT runtime registers */
+#define WDT_CTRL	0x00
+#define WDT_VAL		0x01
+/* the millisec feature is not supported */
+#define WDT_UNITS_SEC	BIT(1)
+#define WDT_UNITS_MIN	BIT(2)
+
+/* The devices private data */
+struct wdt_priv {
+	/* parent device */
+	struct platform_device *pdev;
+	/* the lock for WDT io operations */
+	spinlock_t io_lock;
+	/* the base address of the WDT runtime registers */
+	unsigned short rt_base;
+	/* pre-computed timeout values */
+	unsigned char unit;
+	unsigned char timeout;
+};
+
+/* Module parameters */
+#define WATCHDOG_TIMEOUT 60		/* 60 sec default timeout */
+static int timeout = WATCHDOG_TIMEOUT;	/* in seconds */
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+		 "Watchdog timeout in seconds. 1<=timeout<=15300, default="
+		 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout,
+		 "Watchdog cannot be stopped once started (default="
+		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+/* internal variables */
+static struct platform_device *exar_wdt_pdev;
+
+/* "Super-IO" functions */
+
+static int exar_sio_enter(const unsigned short config_port,
+			  const unsigned char key)
+{
+	if (!request_muxed_region(config_port, 2, DRV_NAME))
+		return -EBUSY;
+
+	/* write the ENTER-KEY twice */
+	outb(key, config_port);
+	outb(key, config_port);
+
+	return 0;
+}
+
+static void exar_sio_exit(const unsigned short config_port)
+{
+	outb(EXAR_EXIT_KEY, config_port);
+	release_region(config_port, 2);
+}
+
+static unsigned char exar_sio_read(const unsigned short config_port,
+				   const unsigned char reg)
+{
+	outb(reg, config_port);
+	return inb(config_port + 1);
+}
+
+static void exar_sio_write(const unsigned short config_port,
+			   const unsigned char reg, const unsigned char val)
+{
+	outb(reg, config_port);
+	outb(val, config_port + 1);
+}
+
+static unsigned short exar_sio_read16(const unsigned short config_port,
+				      const unsigned char reg)
+{
+	unsigned short val;
+
+	/* read the MSB */
+	val = exar_sio_read(config_port, reg);
+
+	/* read and merge in the LSB */
+	val = (val << 8) | exar_sio_read(config_port, reg + 1);
+
+	return val;
+}
+
+/* WDT internal functions */
+
+static void exar_wdt_arm(const struct wdt_priv *priv)
+{
+	/* write timeout value twice to arm watchdog */
+	outb(priv->timeout, priv->rt_base + WDT_VAL);
+	outb(priv->timeout, priv->rt_base + WDT_VAL);
+}
+
+static void exar_wdt_disarm(const struct wdt_priv *priv)
+{
+	outb(0xFF, priv->rt_base + WDT_VAL);
+	outb(0, priv->rt_base + WDT_VAL);
+}
+
+/* Watchdog Operations */
+
+static int exar_wdt_start(struct watchdog_device *wdog)
+{
+	struct wdt_priv *priv = watchdog_get_drvdata(wdog);
+
+	spin_lock(&priv->io_lock);
+
+	/* disable watchdog */
+	exar_wdt_disarm(priv);
+
+	/* program timeout unit */
+	outb(priv->unit, priv->rt_base + WDT_CTRL);
+	/* and arm watchdog */
+	exar_wdt_arm(priv);
+
+	spin_unlock(&priv->io_lock);
+	return 0;
+}
+
+static int exar_wdt_stop(struct watchdog_device *wdog)
+{
+	struct wdt_priv *priv = watchdog_get_drvdata(wdog);
+
+	spin_lock(&priv->io_lock);
+
+	/* disarm watchdog */
+	exar_wdt_disarm(priv);
+
+	spin_unlock(&priv->io_lock);
+	return 0;
+}
+
+static int exar_wdt_keepalive(struct watchdog_device *wdog)
+{
+	struct wdt_priv *priv = watchdog_get_drvdata(wdog);
+
+	spin_lock(&priv->io_lock);
+
+	/* reading the WDT_VAL reg will feed the watchdog */
+	inb(priv->rt_base + WDT_VAL);
+
+	spin_unlock(&priv->io_lock);
+	return 0;
+}
+
+static int exar_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
+{
+	struct wdt_priv *priv = watchdog_get_drvdata(wdog);
+	unsigned char timeout_unit = WDT_UNITS_SEC;
+
+	if (watchdog_timeout_invalid(wdog, t))
+		return -EINVAL;
+
+	/*
+	 * if new timeout is bigger then 255 seconds, change the
+	 * unit to minutes and round the timeout up to the next whole minute
+	 */
+	if (t > 255) {
+		timeout_unit = WDT_UNITS_MIN;
+		t = DIV_ROUND_UP(t, 60);
+	}
+
+	/* save for later use in exar_wdt_start() */
+	priv->unit = timeout_unit;
+	priv->timeout = t;
+
+	wdog->timeout = (timeout_unit == WDT_UNITS_MIN) ? t * 60 : t;
+
+	return 0;
+}
+
+static const struct watchdog_info exar_wdt_info = {
+	.options	= WDIOF_KEEPALIVEPING |
+			  WDIOF_SETTIMEOUT |
+			  WDIOF_MAGICCLOSE,
+	.identity	= "Exar/MaxLinear XR28V38x Watchdog",
+};
+
+static const struct watchdog_ops exar_wdt_ops = {
+	.owner		= THIS_MODULE,
+	.start		= exar_wdt_start,
+	.stop		= exar_wdt_stop,
+	.ping		= exar_wdt_keepalive,
+	.set_timeout	= exar_wdt_set_timeout,
+};
+
+static struct watchdog_device wdt_dev = {
+	.info = &exar_wdt_info,
+	.ops = &exar_wdt_ops,
+	.min_timeout = 1,
+	.max_timeout = 255 * 60,
+};
+
+static unsigned short __init exar_detect(const unsigned short config_port,
+					 const unsigned char key,
+					 unsigned short *rt_base)
+{
+	int ret;
+	unsigned short base = 0;
+	unsigned short vid, did;
+
+	ret = exar_sio_enter(config_port, key);
+	if (ret) {
+		pr_info("config space unavailable\n (err: %d", ret);
+		return 0;
+	}
+
+	vid = exar_sio_read16(config_port, EXAR_VID);
+	did = exar_sio_read16(config_port, EXAR_DID);
+
+	/* check for the vendor and device IDs we currently know about */
+	if (vid == 0x13A8 &&		/* vendor Exar/MaxLinar */
+	    (did == 0x0382 ||		/* UART XR28V382 */
+	     did == 0x0384)) {		/* UART XR28V384 */
+		/* set LD config reg to WDT device (8) */
+		exar_sio_write(config_port, EXAR_LDN, 8);
+		/* is device active? */
+		if (exar_sio_read(config_port, EXAR_ACT) == 0x01) {
+			/* get the WDT runtime registers base address*/
+			base = exar_sio_read16(config_port, EXAR_RTBASE);
+
+			/* set global WDT control to
+			 * assert WDTOUT / rearm by read
+			 */
+			exar_sio_write(config_port, EXAR_WDT, 0);
+		}
+	}
+
+	exar_sio_exit(config_port);
+
+	if (base) {
+		pr_debug("Found a XR28V%X WDT (conf: 0x%x / rt: 0x%04x)\n",
+			 did, config_port, base);
+		*rt_base = base;
+		return did;
+	}
+
+	return 0;
+}
+
+static int __init exar_wdt_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct wdt_priv *priv;
+	int ret, i, j;
+	unsigned short did = 0, rt_base = 0;
+
+	/* search for the first active Exar WDT on all possible locations */
+	for (i = 0; !did && (i < ARRAY_SIZE(sio_config_ports)); i++) {
+		for (j = 0; !did && (j < ARRAY_SIZE(sio_enter_keys)); j++)
+			did = exar_detect(sio_config_ports[i],
+					  sio_enter_keys[j],
+					  &rt_base);
+	}
+
+	if (!did)
+		return -ENODEV;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	spin_lock_init(&priv->io_lock);
+	priv->pdev = pdev;
+	priv->rt_base = rt_base;
+
+	if (watchdog_timeout_invalid(&wdt_dev, timeout))
+		/* invalid timeout specified, reset to default */
+		timeout = WATCHDOG_TIMEOUT;
+
+	watchdog_init_timeout(&wdt_dev, timeout, NULL);
+	watchdog_set_nowayout(&wdt_dev, nowayout);
+	watchdog_stop_on_reboot(&wdt_dev);
+	watchdog_stop_on_unregister(&wdt_dev);
+	watchdog_set_drvdata(&wdt_dev, priv);
+
+	if (!devm_request_region(dev,
+				 priv->rt_base + WDT_CTRL, 2, DRV_NAME)) {
+		dev_err(dev, "failed to request region 0x%04x-0x%04x.\n",
+			priv->rt_base + WDT_CTRL, priv->rt_base + WDT_VAL);
+		return -EBUSY;
+	}
+
+	exar_wdt_set_timeout(&wdt_dev, timeout);
+	/* Make sure that the watchdog is not running */
+	exar_wdt_stop(&wdt_dev);
+
+	ret = devm_watchdog_register_device(dev, &wdt_dev);
+	if (ret)
+		return ret;
+
+	dev_info(dev, "XR28V%X WDT initialized. timeout=%d sec (nowayout=%d)\n",
+		 did, timeout, nowayout);
+
+	return 0;
+}
+
+static struct platform_driver exar_wdt_driver = {
+	.driver = {
+		.name = DRV_NAME,
+	},
+};
+
+static int __init exar_wdt_init(void)
+{
+	int ret;
+
+	exar_wdt_pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
+	if (IS_ERR(exar_wdt_pdev))
+		return PTR_ERR(exar_wdt_pdev);
+
+	ret = platform_driver_probe(&exar_wdt_driver, exar_wdt_probe);
+	if (ret)
+		platform_device_unregister(exar_wdt_pdev);
+
+	return ret;
+}
+
+static void __exit exar_wdt_exit(void)
+{
+	platform_device_unregister(exar_wdt_pdev);
+	platform_driver_unregister(&exar_wdt_driver);
+}
+
+module_init(exar_wdt_init);
+module_exit(exar_wdt_exit);
+
+MODULE_AUTHOR("David Müller <d.mueller@elsoft.ch>");
+MODULE_DESCRIPTION("Exar/MaxLinear Watchdog Driver");
+MODULE_LICENSE("GPL");
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-25 17:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-22  9:51 [RFC] [PATCH] watchdog: Exar/MaxLinear XR28V38x driver David Müller
2022-07-22 15:08 ` Guenter Roeck
2022-07-25 14:24   ` David Müller (ELSOFT AG)
2022-07-25 17:49     ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).