xref: /openbmc/linux/drivers/watchdog/rti_wdt.c (revision 8711071e9700b67045fe5518161d63f7a03e3c9e)
12d63908bSTero Kristo // SPDX-License-Identifier: GPL-2.0
22d63908bSTero Kristo /*
32d63908bSTero Kristo  * Watchdog driver for the K3 RTI module
42d63908bSTero Kristo  *
52d63908bSTero Kristo  * (c) Copyright 2019-2020 Texas Instruments Inc.
62d63908bSTero Kristo  * All rights reserved.
72d63908bSTero Kristo  */
82d63908bSTero Kristo 
92d63908bSTero Kristo #include <linux/clk.h>
102d63908bSTero Kristo #include <linux/device.h>
112d63908bSTero Kristo #include <linux/err.h>
122d63908bSTero Kristo #include <linux/io.h>
132d63908bSTero Kristo #include <linux/kernel.h>
142d63908bSTero Kristo #include <linux/mod_devicetable.h>
152d63908bSTero Kristo #include <linux/module.h>
162d63908bSTero Kristo #include <linux/moduleparam.h>
172d63908bSTero Kristo #include <linux/platform_device.h>
182d63908bSTero Kristo #include <linux/pm_runtime.h>
192d63908bSTero Kristo #include <linux/types.h>
202d63908bSTero Kristo #include <linux/watchdog.h>
212d63908bSTero Kristo 
222d63908bSTero Kristo #define DEFAULT_HEARTBEAT 60
232d63908bSTero Kristo 
242d63908bSTero Kristo /* Max heartbeat is calculated at 32kHz source clock */
252d63908bSTero Kristo #define MAX_HEARTBEAT	1000
262d63908bSTero Kristo 
272d63908bSTero Kristo /* Timer register set definition */
282d63908bSTero Kristo #define RTIDWDCTRL	0x90
292d63908bSTero Kristo #define RTIDWDPRLD	0x94
302d63908bSTero Kristo #define RTIWDSTATUS	0x98
312d63908bSTero Kristo #define RTIWDKEY	0x9c
322d63908bSTero Kristo #define RTIDWDCNTR	0xa0
332d63908bSTero Kristo #define RTIWWDRXCTRL	0xa4
342d63908bSTero Kristo #define RTIWWDSIZECTRL	0xa8
352d63908bSTero Kristo 
362d63908bSTero Kristo #define RTIWWDRX_NMI	0xa
372d63908bSTero Kristo 
382d63908bSTero Kristo #define RTIWWDSIZE_50P		0x50
395527483fSTero Kristo #define RTIWWDSIZE_25P		0x500
405527483fSTero Kristo #define RTIWWDSIZE_12P5		0x5000
415527483fSTero Kristo #define RTIWWDSIZE_6P25		0x50000
425527483fSTero Kristo #define RTIWWDSIZE_3P125	0x500000
432d63908bSTero Kristo 
442d63908bSTero Kristo #define WDENABLE_KEY	0xa98559da
452d63908bSTero Kristo 
462d63908bSTero Kristo #define WDKEY_SEQ0		0xe51a
472d63908bSTero Kristo #define WDKEY_SEQ1		0xa35c
482d63908bSTero Kristo 
492d63908bSTero Kristo #define WDT_PRELOAD_SHIFT	13
502d63908bSTero Kristo 
512d63908bSTero Kristo #define WDT_PRELOAD_MAX		0xfff
522d63908bSTero Kristo 
532d63908bSTero Kristo #define DWDST			BIT(1)
542d63908bSTero Kristo 
555527483fSTero Kristo static int heartbeat = DEFAULT_HEARTBEAT;
562d63908bSTero Kristo 
572d63908bSTero Kristo /*
582d63908bSTero Kristo  * struct to hold data for each WDT device
592d63908bSTero Kristo  * @base - base io address of WD device
602d63908bSTero Kristo  * @freq - source clock frequency of WDT
612d63908bSTero Kristo  * @wdd  - hold watchdog device as is in WDT core
622d63908bSTero Kristo  */
632d63908bSTero Kristo struct rti_wdt_device {
642d63908bSTero Kristo 	void __iomem		*base;
652d63908bSTero Kristo 	unsigned long		freq;
662d63908bSTero Kristo 	struct watchdog_device	wdd;
672d63908bSTero Kristo };
682d63908bSTero Kristo 
692d63908bSTero Kristo static int rti_wdt_start(struct watchdog_device *wdd)
702d63908bSTero Kristo {
712d63908bSTero Kristo 	u32 timer_margin;
722d63908bSTero Kristo 	struct rti_wdt_device *wdt = watchdog_get_drvdata(wdd);
732d63908bSTero Kristo 
742d63908bSTero Kristo 	/* set timeout period */
752d63908bSTero Kristo 	timer_margin = (u64)wdd->timeout * wdt->freq;
762d63908bSTero Kristo 	timer_margin >>= WDT_PRELOAD_SHIFT;
772d63908bSTero Kristo 	if (timer_margin > WDT_PRELOAD_MAX)
782d63908bSTero Kristo 		timer_margin = WDT_PRELOAD_MAX;
792d63908bSTero Kristo 	writel_relaxed(timer_margin, wdt->base + RTIDWDPRLD);
802d63908bSTero Kristo 
812d63908bSTero Kristo 	/*
822d63908bSTero Kristo 	 * RTI only supports a windowed mode, where the watchdog can only
832d63908bSTero Kristo 	 * be petted during the open window; not too early or not too late.
842d63908bSTero Kristo 	 * The HW configuration options only allow for the open window size
852d63908bSTero Kristo 	 * to be 50% or less than that; we obviouly want to configure the open
865527483fSTero Kristo 	 * window as large as possible so we select the 50% option.
872d63908bSTero Kristo 	 */
885527483fSTero Kristo 	wdd->min_hw_heartbeat_ms = 500 * wdd->timeout;
892d63908bSTero Kristo 
902d63908bSTero Kristo 	/* Generate NMI when wdt expires */
912d63908bSTero Kristo 	writel_relaxed(RTIWWDRX_NMI, wdt->base + RTIWWDRXCTRL);
922d63908bSTero Kristo 
932d63908bSTero Kristo 	/* Open window size 50%; this is the largest window size available */
942d63908bSTero Kristo 	writel_relaxed(RTIWWDSIZE_50P, wdt->base + RTIWWDSIZECTRL);
952d63908bSTero Kristo 
962d63908bSTero Kristo 	readl_relaxed(wdt->base + RTIWWDSIZECTRL);
972d63908bSTero Kristo 
982d63908bSTero Kristo 	/* enable watchdog */
992d63908bSTero Kristo 	writel_relaxed(WDENABLE_KEY, wdt->base + RTIDWDCTRL);
1002d63908bSTero Kristo 	return 0;
1012d63908bSTero Kristo }
1022d63908bSTero Kristo 
1032d63908bSTero Kristo static int rti_wdt_ping(struct watchdog_device *wdd)
1042d63908bSTero Kristo {
1052d63908bSTero Kristo 	struct rti_wdt_device *wdt = watchdog_get_drvdata(wdd);
1062d63908bSTero Kristo 
1072d63908bSTero Kristo 	/* put watchdog in service state */
1082d63908bSTero Kristo 	writel_relaxed(WDKEY_SEQ0, wdt->base + RTIWDKEY);
1092d63908bSTero Kristo 	/* put watchdog in active state */
1102d63908bSTero Kristo 	writel_relaxed(WDKEY_SEQ1, wdt->base + RTIWDKEY);
1112d63908bSTero Kristo 
1122d63908bSTero Kristo 	return 0;
1132d63908bSTero Kristo }
1142d63908bSTero Kristo 
1155527483fSTero Kristo static int rti_wdt_setup_hw_hb(struct watchdog_device *wdd, u32 wsize)
1165527483fSTero Kristo {
1175527483fSTero Kristo 	/*
1185527483fSTero Kristo 	 * RTI only supports a windowed mode, where the watchdog can only
1195527483fSTero Kristo 	 * be petted during the open window; not too early or not too late.
1205527483fSTero Kristo 	 * The HW configuration options only allow for the open window size
1215527483fSTero Kristo 	 * to be 50% or less than that.
1225527483fSTero Kristo 	 */
1235527483fSTero Kristo 	switch (wsize) {
1245527483fSTero Kristo 	case RTIWWDSIZE_50P:
1255527483fSTero Kristo 		/* 50% open window => 50% min heartbeat */
1265527483fSTero Kristo 		wdd->min_hw_heartbeat_ms = 500 * heartbeat;
1275527483fSTero Kristo 		break;
1285527483fSTero Kristo 
1295527483fSTero Kristo 	case RTIWWDSIZE_25P:
1305527483fSTero Kristo 		/* 25% open window => 75% min heartbeat */
1315527483fSTero Kristo 		wdd->min_hw_heartbeat_ms = 750 * heartbeat;
1325527483fSTero Kristo 		break;
1335527483fSTero Kristo 
1345527483fSTero Kristo 	case RTIWWDSIZE_12P5:
1355527483fSTero Kristo 		/* 12.5% open window => 87.5% min heartbeat */
1365527483fSTero Kristo 		wdd->min_hw_heartbeat_ms = 875 * heartbeat;
1375527483fSTero Kristo 		break;
1385527483fSTero Kristo 
1395527483fSTero Kristo 	case RTIWWDSIZE_6P25:
1405527483fSTero Kristo 		/* 6.5% open window => 93.5% min heartbeat */
1415527483fSTero Kristo 		wdd->min_hw_heartbeat_ms = 935 * heartbeat;
1425527483fSTero Kristo 		break;
1435527483fSTero Kristo 
1445527483fSTero Kristo 	case RTIWWDSIZE_3P125:
1455527483fSTero Kristo 		/* 3.125% open window => 96.9% min heartbeat */
1465527483fSTero Kristo 		wdd->min_hw_heartbeat_ms = 969 * heartbeat;
1475527483fSTero Kristo 		break;
1485527483fSTero Kristo 
1495527483fSTero Kristo 	default:
1505527483fSTero Kristo 		return -EINVAL;
1515527483fSTero Kristo 	}
1525527483fSTero Kristo 
1535527483fSTero Kristo 	return 0;
1545527483fSTero Kristo }
1555527483fSTero Kristo 
1565527483fSTero Kristo static unsigned int rti_wdt_get_timeleft_ms(struct watchdog_device *wdd)
1572d63908bSTero Kristo {
1582d63908bSTero Kristo 	u64 timer_counter;
1592d63908bSTero Kristo 	u32 val;
1602d63908bSTero Kristo 	struct rti_wdt_device *wdt = watchdog_get_drvdata(wdd);
1612d63908bSTero Kristo 
1622d63908bSTero Kristo 	/* if timeout has occurred then return 0 */
1632d63908bSTero Kristo 	val = readl_relaxed(wdt->base + RTIWDSTATUS);
1642d63908bSTero Kristo 	if (val & DWDST)
1652d63908bSTero Kristo 		return 0;
1662d63908bSTero Kristo 
1672d63908bSTero Kristo 	timer_counter = readl_relaxed(wdt->base + RTIDWDCNTR);
1682d63908bSTero Kristo 
1695527483fSTero Kristo 	timer_counter *= 1000;
1705527483fSTero Kristo 
1712d63908bSTero Kristo 	do_div(timer_counter, wdt->freq);
1722d63908bSTero Kristo 
1732d63908bSTero Kristo 	return timer_counter;
1742d63908bSTero Kristo }
1752d63908bSTero Kristo 
1765527483fSTero Kristo static unsigned int rti_wdt_get_timeleft(struct watchdog_device *wdd)
1775527483fSTero Kristo {
1785527483fSTero Kristo 	return rti_wdt_get_timeleft_ms(wdd) / 1000;
1795527483fSTero Kristo }
1805527483fSTero Kristo 
1812d63908bSTero Kristo static const struct watchdog_info rti_wdt_info = {
1822d63908bSTero Kristo 	.options = WDIOF_KEEPALIVEPING,
1832d63908bSTero Kristo 	.identity = "K3 RTI Watchdog",
1842d63908bSTero Kristo };
1852d63908bSTero Kristo 
1862d63908bSTero Kristo static const struct watchdog_ops rti_wdt_ops = {
1872d63908bSTero Kristo 	.owner		= THIS_MODULE,
1882d63908bSTero Kristo 	.start		= rti_wdt_start,
1892d63908bSTero Kristo 	.ping		= rti_wdt_ping,
1902d63908bSTero Kristo 	.get_timeleft	= rti_wdt_get_timeleft,
1912d63908bSTero Kristo };
1922d63908bSTero Kristo 
1932d63908bSTero Kristo static int rti_wdt_probe(struct platform_device *pdev)
1942d63908bSTero Kristo {
1952d63908bSTero Kristo 	int ret = 0;
1962d63908bSTero Kristo 	struct device *dev = &pdev->dev;
1972d63908bSTero Kristo 	struct resource *wdt_mem;
1982d63908bSTero Kristo 	struct watchdog_device *wdd;
1992d63908bSTero Kristo 	struct rti_wdt_device *wdt;
2002d63908bSTero Kristo 	struct clk *clk;
2015527483fSTero Kristo 	u32 last_ping = 0;
2022d63908bSTero Kristo 
2032d63908bSTero Kristo 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
2042d63908bSTero Kristo 	if (!wdt)
2052d63908bSTero Kristo 		return -ENOMEM;
2062d63908bSTero Kristo 
2072d63908bSTero Kristo 	clk = clk_get(dev, NULL);
208d342951bSKrzysztof Kozlowski 	if (IS_ERR(clk))
209d342951bSKrzysztof Kozlowski 		return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
2102d63908bSTero Kristo 
2112d63908bSTero Kristo 	wdt->freq = clk_get_rate(clk);
2122d63908bSTero Kristo 
2132d63908bSTero Kristo 	clk_put(clk);
2142d63908bSTero Kristo 
2152d63908bSTero Kristo 	if (!wdt->freq) {
2162d63908bSTero Kristo 		dev_err(dev, "Failed to get fck rate.\n");
2172d63908bSTero Kristo 		return -EINVAL;
2182d63908bSTero Kristo 	}
2192d63908bSTero Kristo 
2205527483fSTero Kristo 	/*
2215527483fSTero Kristo 	 * If watchdog is running at 32k clock, it is not accurate.
2225527483fSTero Kristo 	 * Adjust frequency down in this case so that we don't pet
2235527483fSTero Kristo 	 * the watchdog too often.
2245527483fSTero Kristo 	 */
2255527483fSTero Kristo 	if (wdt->freq < 32768)
2265527483fSTero Kristo 		wdt->freq = wdt->freq * 9 / 10;
2275527483fSTero Kristo 
2282d63908bSTero Kristo 	pm_runtime_enable(dev);
2292d63908bSTero Kristo 	ret = pm_runtime_get_sync(dev);
230*8711071eSZhang Qilong 	if (ret) {
231*8711071eSZhang Qilong 		pm_runtime_put_noidle(dev);
232d342951bSKrzysztof Kozlowski 		return dev_err_probe(dev, ret, "runtime pm failed\n");
233*8711071eSZhang Qilong 	}
2342d63908bSTero Kristo 
2352d63908bSTero Kristo 	platform_set_drvdata(pdev, wdt);
2362d63908bSTero Kristo 
2372d63908bSTero Kristo 	wdd = &wdt->wdd;
2382d63908bSTero Kristo 	wdd->info = &rti_wdt_info;
2392d63908bSTero Kristo 	wdd->ops = &rti_wdt_ops;
2402d63908bSTero Kristo 	wdd->min_timeout = 1;
2412d63908bSTero Kristo 	wdd->max_hw_heartbeat_ms = (WDT_PRELOAD_MAX << WDT_PRELOAD_SHIFT) /
2422d63908bSTero Kristo 		wdt->freq * 1000;
2432d63908bSTero Kristo 	wdd->parent = dev;
2442d63908bSTero Kristo 
2452d63908bSTero Kristo 	watchdog_set_drvdata(wdd, wdt);
2462d63908bSTero Kristo 	watchdog_set_nowayout(wdd, 1);
2472d63908bSTero Kristo 	watchdog_set_restart_priority(wdd, 128);
2482d63908bSTero Kristo 
2492d63908bSTero Kristo 	wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2502d63908bSTero Kristo 	wdt->base = devm_ioremap_resource(dev, wdt_mem);
2512d63908bSTero Kristo 	if (IS_ERR(wdt->base)) {
2522d63908bSTero Kristo 		ret = PTR_ERR(wdt->base);
2532d63908bSTero Kristo 		goto err_iomap;
2542d63908bSTero Kristo 	}
2552d63908bSTero Kristo 
2565527483fSTero Kristo 	if (readl(wdt->base + RTIDWDCTRL) == WDENABLE_KEY) {
2575527483fSTero Kristo 		u32 time_left_ms;
2585527483fSTero Kristo 		u64 heartbeat_ms;
2595527483fSTero Kristo 		u32 wsize;
2605527483fSTero Kristo 
2615527483fSTero Kristo 		set_bit(WDOG_HW_RUNNING, &wdd->status);
2625527483fSTero Kristo 		time_left_ms = rti_wdt_get_timeleft_ms(wdd);
2635527483fSTero Kristo 		heartbeat_ms = readl(wdt->base + RTIDWDPRLD);
2645527483fSTero Kristo 		heartbeat_ms <<= WDT_PRELOAD_SHIFT;
2655527483fSTero Kristo 		heartbeat_ms *= 1000;
2665527483fSTero Kristo 		do_div(heartbeat_ms, wdt->freq);
2675527483fSTero Kristo 		if (heartbeat_ms != heartbeat * 1000)
2685527483fSTero Kristo 			dev_warn(dev, "watchdog already running, ignoring heartbeat config!\n");
2695527483fSTero Kristo 
2705527483fSTero Kristo 		heartbeat = heartbeat_ms;
2715527483fSTero Kristo 		heartbeat /= 1000;
2725527483fSTero Kristo 
2735527483fSTero Kristo 		wsize = readl(wdt->base + RTIWWDSIZECTRL);
2745527483fSTero Kristo 		ret = rti_wdt_setup_hw_hb(wdd, wsize);
2755527483fSTero Kristo 		if (ret) {
2765527483fSTero Kristo 			dev_err(dev, "bad window size.\n");
2775527483fSTero Kristo 			goto err_iomap;
2785527483fSTero Kristo 		}
2795527483fSTero Kristo 
2805527483fSTero Kristo 		last_ping = heartbeat_ms - time_left_ms;
2815527483fSTero Kristo 		if (time_left_ms > heartbeat_ms) {
2825527483fSTero Kristo 			dev_warn(dev, "time_left > heartbeat? Assuming last ping just before now.\n");
2835527483fSTero Kristo 			last_ping = 0;
2845527483fSTero Kristo 		}
2855527483fSTero Kristo 	}
2865527483fSTero Kristo 
2875527483fSTero Kristo 	watchdog_init_timeout(wdd, heartbeat, dev);
2885527483fSTero Kristo 
2892d63908bSTero Kristo 	ret = watchdog_register_device(wdd);
2902d63908bSTero Kristo 	if (ret) {
2912d63908bSTero Kristo 		dev_err(dev, "cannot register watchdog device\n");
2922d63908bSTero Kristo 		goto err_iomap;
2932d63908bSTero Kristo 	}
2942d63908bSTero Kristo 
2955527483fSTero Kristo 	if (last_ping)
2965527483fSTero Kristo 		watchdog_set_last_hw_keepalive(wdd, last_ping);
2975527483fSTero Kristo 
2982d63908bSTero Kristo 	return 0;
2992d63908bSTero Kristo 
3002d63908bSTero Kristo err_iomap:
3012d63908bSTero Kristo 	pm_runtime_put_sync(&pdev->dev);
302d5b29c2cSTero Kristo 	pm_runtime_disable(&pdev->dev);
3032d63908bSTero Kristo 
3042d63908bSTero Kristo 	return ret;
3052d63908bSTero Kristo }
3062d63908bSTero Kristo 
3072d63908bSTero Kristo static int rti_wdt_remove(struct platform_device *pdev)
3082d63908bSTero Kristo {
3092d63908bSTero Kristo 	struct rti_wdt_device *wdt = platform_get_drvdata(pdev);
3102d63908bSTero Kristo 
3112d63908bSTero Kristo 	watchdog_unregister_device(&wdt->wdd);
3122d63908bSTero Kristo 	pm_runtime_put(&pdev->dev);
313d5b29c2cSTero Kristo 	pm_runtime_disable(&pdev->dev);
3142d63908bSTero Kristo 
3152d63908bSTero Kristo 	return 0;
3162d63908bSTero Kristo }
3172d63908bSTero Kristo 
3182d63908bSTero Kristo static const struct of_device_id rti_wdt_of_match[] = {
3192d63908bSTero Kristo 	{ .compatible = "ti,j7-rti-wdt", },
3202d63908bSTero Kristo 	{},
3212d63908bSTero Kristo };
3222d63908bSTero Kristo MODULE_DEVICE_TABLE(of, rti_wdt_of_match);
3232d63908bSTero Kristo 
3242d63908bSTero Kristo static struct platform_driver rti_wdt_driver = {
3252d63908bSTero Kristo 	.driver = {
3262d63908bSTero Kristo 		.name = "rti-wdt",
3272d63908bSTero Kristo 		.of_match_table = rti_wdt_of_match,
3282d63908bSTero Kristo 	},
3292d63908bSTero Kristo 	.probe = rti_wdt_probe,
3302d63908bSTero Kristo 	.remove = rti_wdt_remove,
3312d63908bSTero Kristo };
3322d63908bSTero Kristo 
3332d63908bSTero Kristo module_platform_driver(rti_wdt_driver);
3342d63908bSTero Kristo 
3352d63908bSTero Kristo MODULE_AUTHOR("Tero Kristo <t-kristo@ti.com>");
3362d63908bSTero Kristo MODULE_DESCRIPTION("K3 RTI Watchdog Driver");
3372d63908bSTero Kristo 
3382d63908bSTero Kristo module_param(heartbeat, int, 0);
3392d63908bSTero Kristo MODULE_PARM_DESC(heartbeat,
3402d63908bSTero Kristo 		 "Watchdog heartbeat period in seconds from 1 to "
3412d63908bSTero Kristo 		 __MODULE_STRING(MAX_HEARTBEAT) ", default "
3422d63908bSTero Kristo 		 __MODULE_STRING(DEFAULT_HEARTBEAT));
3432d63908bSTero Kristo 
3442d63908bSTero Kristo MODULE_LICENSE("GPL");
3452d63908bSTero Kristo MODULE_ALIAS("platform:rti-wdt");
346