xref: /openbmc/linux/drivers/watchdog/imgpdc_wdt.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
293937669SNaidu Tellapati /*
393937669SNaidu Tellapati  * Imagination Technologies PowerDown Controller Watchdog Timer.
493937669SNaidu Tellapati  *
593937669SNaidu Tellapati  * Copyright (c) 2014 Imagination Technologies Ltd.
693937669SNaidu Tellapati  *
793937669SNaidu Tellapati  * Based on drivers/watchdog/sunxi_wdt.c Copyright (c) 2013 Carlo Caione
893937669SNaidu Tellapati  *                                                     2012 Henrik Nordstrom
9c1f26387SEzequiel Garcia  *
10c1f26387SEzequiel Garcia  * Notes
11c1f26387SEzequiel Garcia  * -----
12c1f26387SEzequiel Garcia  * The timeout value is rounded to the next power of two clock cycles.
13c1f26387SEzequiel Garcia  * This is configured using the PDC_WDT_CONFIG register, according to this
14c1f26387SEzequiel Garcia  * formula:
15c1f26387SEzequiel Garcia  *
16c1f26387SEzequiel Garcia  *     timeout = 2^(delay + 1) clock cycles
17c1f26387SEzequiel Garcia  *
18c1f26387SEzequiel Garcia  * Where 'delay' is the value written in PDC_WDT_CONFIG register.
19c1f26387SEzequiel Garcia  *
20c1f26387SEzequiel Garcia  * Therefore, the hardware only allows to program watchdog timeouts, expressed
21c1f26387SEzequiel Garcia  * as a power of two number of watchdog clock cycles. The current implementation
22c1f26387SEzequiel Garcia  * guarantees that the actual watchdog timeout will be _at least_ the value
23c1f26387SEzequiel Garcia  * programmed in the imgpdg_wdt driver.
24c1f26387SEzequiel Garcia  *
25c1f26387SEzequiel Garcia  * The following table shows how the user-configured timeout relates
26c1f26387SEzequiel Garcia  * to the actual hardware timeout (watchdog clock @ 40000 Hz):
27c1f26387SEzequiel Garcia  *
28c1f26387SEzequiel Garcia  * input timeout | WD_DELAY | actual timeout
29c1f26387SEzequiel Garcia  * -----------------------------------
30c1f26387SEzequiel Garcia  *      10       |   18     |  13 seconds
31c1f26387SEzequiel Garcia  *      20       |   19     |  26 seconds
32c1f26387SEzequiel Garcia  *      30       |   20     |  52 seconds
33c1f26387SEzequiel Garcia  *      60       |   21     |  104 seconds
34c1f26387SEzequiel Garcia  *
35c1f26387SEzequiel Garcia  * Albeit coarse, this granularity would suffice most watchdog uses.
36c1f26387SEzequiel Garcia  * If the platform allows it, the user should be able to change the watchdog
37c1f26387SEzequiel Garcia  * clock rate and achieve a finer timeout granularity.
3893937669SNaidu Tellapati  */
3993937669SNaidu Tellapati 
4093937669SNaidu Tellapati #include <linux/clk.h>
4193937669SNaidu Tellapati #include <linux/io.h>
4293937669SNaidu Tellapati #include <linux/log2.h>
4393937669SNaidu Tellapati #include <linux/module.h>
44ac316725SRandy Dunlap #include <linux/mod_devicetable.h>
4593937669SNaidu Tellapati #include <linux/platform_device.h>
4693937669SNaidu Tellapati #include <linux/slab.h>
4793937669SNaidu Tellapati #include <linux/watchdog.h>
4893937669SNaidu Tellapati 
4993937669SNaidu Tellapati /* registers */
5093937669SNaidu Tellapati #define PDC_WDT_SOFT_RESET		0x00
5193937669SNaidu Tellapati #define PDC_WDT_CONFIG			0x04
5293937669SNaidu Tellapati   #define PDC_WDT_CONFIG_ENABLE		BIT(31)
5393937669SNaidu Tellapati   #define PDC_WDT_CONFIG_DELAY_MASK	0x1f
5493937669SNaidu Tellapati 
5593937669SNaidu Tellapati #define PDC_WDT_TICKLE1			0x08
5693937669SNaidu Tellapati #define PDC_WDT_TICKLE1_MAGIC		0xabcd1234
5793937669SNaidu Tellapati #define PDC_WDT_TICKLE2			0x0c
5893937669SNaidu Tellapati #define PDC_WDT_TICKLE2_MAGIC		0x4321dcba
5993937669SNaidu Tellapati 
6093937669SNaidu Tellapati #define PDC_WDT_TICKLE_STATUS_MASK	0x7
6193937669SNaidu Tellapati #define PDC_WDT_TICKLE_STATUS_SHIFT	0
6293937669SNaidu Tellapati #define PDC_WDT_TICKLE_STATUS_HRESET	0x0  /* Hard reset */
6393937669SNaidu Tellapati #define PDC_WDT_TICKLE_STATUS_TIMEOUT	0x1  /* Timeout */
6493937669SNaidu Tellapati #define PDC_WDT_TICKLE_STATUS_TICKLE	0x2  /* Tickled incorrectly */
6593937669SNaidu Tellapati #define PDC_WDT_TICKLE_STATUS_SRESET	0x3  /* Soft reset */
6693937669SNaidu Tellapati #define PDC_WDT_TICKLE_STATUS_USER	0x4  /* User reset */
6793937669SNaidu Tellapati 
6893937669SNaidu Tellapati /* Timeout values are in seconds */
6993937669SNaidu Tellapati #define PDC_WDT_MIN_TIMEOUT		1
7093937669SNaidu Tellapati #define PDC_WDT_DEF_TIMEOUT		64
7193937669SNaidu Tellapati 
727094e1ddSAndrew Bresticker static int heartbeat;
7393937669SNaidu Tellapati module_param(heartbeat, int, 0);
74ae6ee2fdSJames Hogan MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds "
7593937669SNaidu Tellapati 	"(default=" __MODULE_STRING(PDC_WDT_DEF_TIMEOUT) ")");
7693937669SNaidu Tellapati 
7793937669SNaidu Tellapati static bool nowayout = WATCHDOG_NOWAYOUT;
7893937669SNaidu Tellapati module_param(nowayout, bool, 0);
7993937669SNaidu Tellapati MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
8093937669SNaidu Tellapati 	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
8193937669SNaidu Tellapati 
8293937669SNaidu Tellapati struct pdc_wdt_dev {
8393937669SNaidu Tellapati 	struct watchdog_device wdt_dev;
8493937669SNaidu Tellapati 	struct clk *wdt_clk;
8593937669SNaidu Tellapati 	struct clk *sys_clk;
8693937669SNaidu Tellapati 	void __iomem *base;
8793937669SNaidu Tellapati };
8893937669SNaidu Tellapati 
pdc_wdt_keepalive(struct watchdog_device * wdt_dev)8993937669SNaidu Tellapati static int pdc_wdt_keepalive(struct watchdog_device *wdt_dev)
9093937669SNaidu Tellapati {
9193937669SNaidu Tellapati 	struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
9293937669SNaidu Tellapati 
9393937669SNaidu Tellapati 	writel(PDC_WDT_TICKLE1_MAGIC, wdt->base + PDC_WDT_TICKLE1);
9493937669SNaidu Tellapati 	writel(PDC_WDT_TICKLE2_MAGIC, wdt->base + PDC_WDT_TICKLE2);
9593937669SNaidu Tellapati 
9693937669SNaidu Tellapati 	return 0;
9793937669SNaidu Tellapati }
9893937669SNaidu Tellapati 
pdc_wdt_stop(struct watchdog_device * wdt_dev)9993937669SNaidu Tellapati static int pdc_wdt_stop(struct watchdog_device *wdt_dev)
10093937669SNaidu Tellapati {
10193937669SNaidu Tellapati 	unsigned int val;
10293937669SNaidu Tellapati 	struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
10393937669SNaidu Tellapati 
10493937669SNaidu Tellapati 	val = readl(wdt->base + PDC_WDT_CONFIG);
10593937669SNaidu Tellapati 	val &= ~PDC_WDT_CONFIG_ENABLE;
10693937669SNaidu Tellapati 	writel(val, wdt->base + PDC_WDT_CONFIG);
10793937669SNaidu Tellapati 
10893937669SNaidu Tellapati 	/* Must tickle to finish the stop */
10993937669SNaidu Tellapati 	pdc_wdt_keepalive(wdt_dev);
11093937669SNaidu Tellapati 
11193937669SNaidu Tellapati 	return 0;
11293937669SNaidu Tellapati }
11393937669SNaidu Tellapati 
__pdc_wdt_set_timeout(struct pdc_wdt_dev * wdt)1148aa453a5SAndrew Bresticker static void __pdc_wdt_set_timeout(struct pdc_wdt_dev *wdt)
1158aa453a5SAndrew Bresticker {
1168aa453a5SAndrew Bresticker 	unsigned long clk_rate = clk_get_rate(wdt->wdt_clk);
1178aa453a5SAndrew Bresticker 	unsigned int val;
1188aa453a5SAndrew Bresticker 
1198aa453a5SAndrew Bresticker 	val = readl(wdt->base + PDC_WDT_CONFIG) & ~PDC_WDT_CONFIG_DELAY_MASK;
1208aa453a5SAndrew Bresticker 	val |= order_base_2(wdt->wdt_dev.timeout * clk_rate) - 1;
1218aa453a5SAndrew Bresticker 	writel(val, wdt->base + PDC_WDT_CONFIG);
1228aa453a5SAndrew Bresticker }
1238aa453a5SAndrew Bresticker 
pdc_wdt_set_timeout(struct watchdog_device * wdt_dev,unsigned int new_timeout)12493937669SNaidu Tellapati static int pdc_wdt_set_timeout(struct watchdog_device *wdt_dev,
12593937669SNaidu Tellapati 			       unsigned int new_timeout)
12693937669SNaidu Tellapati {
12793937669SNaidu Tellapati 	struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
12893937669SNaidu Tellapati 
12993937669SNaidu Tellapati 	wdt->wdt_dev.timeout = new_timeout;
13093937669SNaidu Tellapati 
1318aa453a5SAndrew Bresticker 	__pdc_wdt_set_timeout(wdt);
13293937669SNaidu Tellapati 
13393937669SNaidu Tellapati 	return 0;
13493937669SNaidu Tellapati }
13593937669SNaidu Tellapati 
13693937669SNaidu Tellapati /* Start the watchdog timer (delay should already be set) */
pdc_wdt_start(struct watchdog_device * wdt_dev)13793937669SNaidu Tellapati static int pdc_wdt_start(struct watchdog_device *wdt_dev)
13893937669SNaidu Tellapati {
13993937669SNaidu Tellapati 	unsigned int val;
14093937669SNaidu Tellapati 	struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
14193937669SNaidu Tellapati 
1428aa453a5SAndrew Bresticker 	__pdc_wdt_set_timeout(wdt);
1438aa453a5SAndrew Bresticker 
14493937669SNaidu Tellapati 	val = readl(wdt->base + PDC_WDT_CONFIG);
14593937669SNaidu Tellapati 	val |= PDC_WDT_CONFIG_ENABLE;
14693937669SNaidu Tellapati 	writel(val, wdt->base + PDC_WDT_CONFIG);
14793937669SNaidu Tellapati 
14893937669SNaidu Tellapati 	return 0;
14993937669SNaidu Tellapati }
15093937669SNaidu Tellapati 
pdc_wdt_restart(struct watchdog_device * wdt_dev,unsigned long action,void * data)1514d8b229dSGuenter Roeck static int pdc_wdt_restart(struct watchdog_device *wdt_dev,
1524d8b229dSGuenter Roeck 			   unsigned long action, void *data)
1530f10d9c5SDamien Riegel {
1540f10d9c5SDamien Riegel 	struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
1550f10d9c5SDamien Riegel 
1560f10d9c5SDamien Riegel 	/* Assert SOFT_RESET */
1570f10d9c5SDamien Riegel 	writel(0x1, wdt->base + PDC_WDT_SOFT_RESET);
1580f10d9c5SDamien Riegel 
1590f10d9c5SDamien Riegel 	return 0;
1600f10d9c5SDamien Riegel }
1610f10d9c5SDamien Riegel 
1626c368932SBhumika Goyal static const struct watchdog_info pdc_wdt_info = {
16393937669SNaidu Tellapati 	.identity	= "IMG PDC Watchdog",
16493937669SNaidu Tellapati 	.options	= WDIOF_SETTIMEOUT |
16593937669SNaidu Tellapati 			  WDIOF_KEEPALIVEPING |
16693937669SNaidu Tellapati 			  WDIOF_MAGICCLOSE,
16793937669SNaidu Tellapati };
16893937669SNaidu Tellapati 
16993937669SNaidu Tellapati static const struct watchdog_ops pdc_wdt_ops = {
17093937669SNaidu Tellapati 	.owner		= THIS_MODULE,
17193937669SNaidu Tellapati 	.start		= pdc_wdt_start,
17293937669SNaidu Tellapati 	.stop		= pdc_wdt_stop,
17393937669SNaidu Tellapati 	.ping		= pdc_wdt_keepalive,
17493937669SNaidu Tellapati 	.set_timeout	= pdc_wdt_set_timeout,
1750f10d9c5SDamien Riegel 	.restart        = pdc_wdt_restart,
17693937669SNaidu Tellapati };
17793937669SNaidu Tellapati 
pdc_wdt_probe(struct platform_device * pdev)17893937669SNaidu Tellapati static int pdc_wdt_probe(struct platform_device *pdev)
17993937669SNaidu Tellapati {
1801f25cb28SGuenter Roeck 	struct device *dev = &pdev->dev;
181deb8d50eSEzequiel Garcia 	u64 div;
182*c4b8e92bSChristophe JAILLET 	int val;
18393937669SNaidu Tellapati 	unsigned long clk_rate;
18493937669SNaidu Tellapati 	struct pdc_wdt_dev *pdc_wdt;
18593937669SNaidu Tellapati 
1861f25cb28SGuenter Roeck 	pdc_wdt = devm_kzalloc(dev, sizeof(*pdc_wdt), GFP_KERNEL);
18793937669SNaidu Tellapati 	if (!pdc_wdt)
18893937669SNaidu Tellapati 		return -ENOMEM;
18993937669SNaidu Tellapati 
1900f0a6a28SGuenter Roeck 	pdc_wdt->base = devm_platform_ioremap_resource(pdev, 0);
19193937669SNaidu Tellapati 	if (IS_ERR(pdc_wdt->base))
19293937669SNaidu Tellapati 		return PTR_ERR(pdc_wdt->base);
19393937669SNaidu Tellapati 
194*c4b8e92bSChristophe JAILLET 	pdc_wdt->sys_clk = devm_clk_get_enabled(dev, "sys");
19593937669SNaidu Tellapati 	if (IS_ERR(pdc_wdt->sys_clk)) {
1961f25cb28SGuenter Roeck 		dev_err(dev, "failed to get the sys clock\n");
19793937669SNaidu Tellapati 		return PTR_ERR(pdc_wdt->sys_clk);
19893937669SNaidu Tellapati 	}
19993937669SNaidu Tellapati 
200*c4b8e92bSChristophe JAILLET 	pdc_wdt->wdt_clk = devm_clk_get_enabled(dev, "wdt");
20193937669SNaidu Tellapati 	if (IS_ERR(pdc_wdt->wdt_clk)) {
2021f25cb28SGuenter Roeck 		dev_err(dev, "failed to get the wdt clock\n");
20393937669SNaidu Tellapati 		return PTR_ERR(pdc_wdt->wdt_clk);
20493937669SNaidu Tellapati 	}
20593937669SNaidu Tellapati 
20693937669SNaidu Tellapati 	/* We use the clock rate to calculate the max timeout */
20793937669SNaidu Tellapati 	clk_rate = clk_get_rate(pdc_wdt->wdt_clk);
20893937669SNaidu Tellapati 	if (clk_rate == 0) {
2091f25cb28SGuenter Roeck 		dev_err(dev, "failed to get clock rate\n");
2101f25cb28SGuenter Roeck 		return -EINVAL;
21193937669SNaidu Tellapati 	}
21293937669SNaidu Tellapati 
21393937669SNaidu Tellapati 	if (order_base_2(clk_rate) > PDC_WDT_CONFIG_DELAY_MASK + 1) {
2141f25cb28SGuenter Roeck 		dev_err(dev, "invalid clock rate\n");
2151f25cb28SGuenter Roeck 		return -EINVAL;
21693937669SNaidu Tellapati 	}
21793937669SNaidu Tellapati 
21893937669SNaidu Tellapati 	if (order_base_2(clk_rate) == 0)
21993937669SNaidu Tellapati 		pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT + 1;
22093937669SNaidu Tellapati 	else
22193937669SNaidu Tellapati 		pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT;
22293937669SNaidu Tellapati 
22393937669SNaidu Tellapati 	pdc_wdt->wdt_dev.info = &pdc_wdt_info;
22493937669SNaidu Tellapati 	pdc_wdt->wdt_dev.ops = &pdc_wdt_ops;
225deb8d50eSEzequiel Garcia 
226deb8d50eSEzequiel Garcia 	div = 1ULL << (PDC_WDT_CONFIG_DELAY_MASK + 1);
227deb8d50eSEzequiel Garcia 	do_div(div, clk_rate);
228deb8d50eSEzequiel Garcia 	pdc_wdt->wdt_dev.max_timeout = div;
2297094e1ddSAndrew Bresticker 	pdc_wdt->wdt_dev.timeout = PDC_WDT_DEF_TIMEOUT;
2301f25cb28SGuenter Roeck 	pdc_wdt->wdt_dev.parent = dev;
231a629c08fSJames Hogan 	watchdog_set_drvdata(&pdc_wdt->wdt_dev, pdc_wdt);
23293937669SNaidu Tellapati 
2331f25cb28SGuenter Roeck 	watchdog_init_timeout(&pdc_wdt->wdt_dev, heartbeat, dev);
23493937669SNaidu Tellapati 
23593937669SNaidu Tellapati 	pdc_wdt_stop(&pdc_wdt->wdt_dev);
23693937669SNaidu Tellapati 
23793937669SNaidu Tellapati 	/* Find what caused the last reset */
23893937669SNaidu Tellapati 	val = readl(pdc_wdt->base + PDC_WDT_TICKLE1);
23993937669SNaidu Tellapati 	val = (val & PDC_WDT_TICKLE_STATUS_MASK) >> PDC_WDT_TICKLE_STATUS_SHIFT;
24093937669SNaidu Tellapati 	switch (val) {
24193937669SNaidu Tellapati 	case PDC_WDT_TICKLE_STATUS_TICKLE:
24293937669SNaidu Tellapati 	case PDC_WDT_TICKLE_STATUS_TIMEOUT:
24393937669SNaidu Tellapati 		pdc_wdt->wdt_dev.bootstatus |= WDIOF_CARDRESET;
2441f25cb28SGuenter Roeck 		dev_info(dev, "watchdog module last reset due to timeout\n");
24593937669SNaidu Tellapati 		break;
24693937669SNaidu Tellapati 	case PDC_WDT_TICKLE_STATUS_HRESET:
2471f25cb28SGuenter Roeck 		dev_info(dev,
24893937669SNaidu Tellapati 			 "watchdog module last reset due to hard reset\n");
24993937669SNaidu Tellapati 		break;
25093937669SNaidu Tellapati 	case PDC_WDT_TICKLE_STATUS_SRESET:
2511f25cb28SGuenter Roeck 		dev_info(dev,
25293937669SNaidu Tellapati 			 "watchdog module last reset due to soft reset\n");
25393937669SNaidu Tellapati 		break;
25493937669SNaidu Tellapati 	case PDC_WDT_TICKLE_STATUS_USER:
2551f25cb28SGuenter Roeck 		dev_info(dev,
25693937669SNaidu Tellapati 			 "watchdog module last reset due to user reset\n");
25793937669SNaidu Tellapati 		break;
25893937669SNaidu Tellapati 	default:
2591f25cb28SGuenter Roeck 		dev_info(dev, "contains an illegal status code (%08x)\n", val);
26093937669SNaidu Tellapati 		break;
26193937669SNaidu Tellapati 	}
26293937669SNaidu Tellapati 
26393937669SNaidu Tellapati 	watchdog_set_nowayout(&pdc_wdt->wdt_dev, nowayout);
2640f10d9c5SDamien Riegel 	watchdog_set_restart_priority(&pdc_wdt->wdt_dev, 128);
26593937669SNaidu Tellapati 
26693937669SNaidu Tellapati 	platform_set_drvdata(pdev, pdc_wdt);
26793937669SNaidu Tellapati 
2681f25cb28SGuenter Roeck 	watchdog_stop_on_reboot(&pdc_wdt->wdt_dev);
2691f25cb28SGuenter Roeck 	watchdog_stop_on_unregister(&pdc_wdt->wdt_dev);
2701f25cb28SGuenter Roeck 	return devm_watchdog_register_device(dev, &pdc_wdt->wdt_dev);
27193937669SNaidu Tellapati }
27293937669SNaidu Tellapati 
27393937669SNaidu Tellapati static const struct of_device_id pdc_wdt_match[] = {
27493937669SNaidu Tellapati 	{ .compatible = "img,pdc-wdt" },
27593937669SNaidu Tellapati 	{}
27693937669SNaidu Tellapati };
27793937669SNaidu Tellapati MODULE_DEVICE_TABLE(of, pdc_wdt_match);
27893937669SNaidu Tellapati 
27993937669SNaidu Tellapati static struct platform_driver pdc_wdt_driver = {
28093937669SNaidu Tellapati 	.driver = {
28193937669SNaidu Tellapati 		.name = "imgpdc-wdt",
28293937669SNaidu Tellapati 		.of_match_table	= pdc_wdt_match,
28393937669SNaidu Tellapati 	},
28493937669SNaidu Tellapati 	.probe = pdc_wdt_probe,
28593937669SNaidu Tellapati };
28693937669SNaidu Tellapati module_platform_driver(pdc_wdt_driver);
28793937669SNaidu Tellapati 
28893937669SNaidu Tellapati MODULE_AUTHOR("Jude Abraham <Jude.Abraham@imgtec.com>");
28993937669SNaidu Tellapati MODULE_AUTHOR("Naidu Tellapati <Naidu.Tellapati@imgtec.com>");
29093937669SNaidu Tellapati MODULE_DESCRIPTION("Imagination Technologies PDC Watchdog Timer Driver");
29193937669SNaidu Tellapati MODULE_LICENSE("GPL v2");
292