xref: /linux/drivers/watchdog/cadence_wdt.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
12e62c498SMarcus Folkesson // SPDX-License-Identifier: GPL-2.0+
258bf0164SHarini Katakam /*
358bf0164SHarini Katakam  * Cadence WDT driver - Used by Xilinx Zynq
458bf0164SHarini Katakam  *
558bf0164SHarini Katakam  * Copyright (C) 2010 - 2014 Xilinx, Inc.
658bf0164SHarini Katakam  *
758bf0164SHarini Katakam  */
858bf0164SHarini Katakam 
958bf0164SHarini Katakam #include <linux/clk.h>
1058bf0164SHarini Katakam #include <linux/init.h>
1158bf0164SHarini Katakam #include <linux/interrupt.h>
1258bf0164SHarini Katakam #include <linux/io.h>
1358bf0164SHarini Katakam #include <linux/irq.h>
1458bf0164SHarini Katakam #include <linux/kernel.h>
1558bf0164SHarini Katakam #include <linux/module.h>
1658bf0164SHarini Katakam #include <linux/of.h>
1758bf0164SHarini Katakam #include <linux/platform_device.h>
1858bf0164SHarini Katakam #include <linux/watchdog.h>
1958bf0164SHarini Katakam 
2058bf0164SHarini Katakam #define CDNS_WDT_DEFAULT_TIMEOUT	10
2158bf0164SHarini Katakam /* Supports 1 - 516 sec */
2258bf0164SHarini Katakam #define CDNS_WDT_MIN_TIMEOUT	1
2358bf0164SHarini Katakam #define CDNS_WDT_MAX_TIMEOUT	516
2458bf0164SHarini Katakam 
2558bf0164SHarini Katakam /* Restart key */
2658bf0164SHarini Katakam #define CDNS_WDT_RESTART_KEY 0x00001999
2758bf0164SHarini Katakam 
2858bf0164SHarini Katakam /* Counter register access key */
2958bf0164SHarini Katakam #define CDNS_WDT_REGISTER_ACCESS_KEY 0x00920000
3058bf0164SHarini Katakam 
3158bf0164SHarini Katakam /* Counter value divisor */
3258bf0164SHarini Katakam #define CDNS_WDT_COUNTER_VALUE_DIVISOR 0x1000
3358bf0164SHarini Katakam 
3458bf0164SHarini Katakam /* Clock prescaler value and selection */
3558bf0164SHarini Katakam #define CDNS_WDT_PRESCALE_64	64
3658bf0164SHarini Katakam #define CDNS_WDT_PRESCALE_512	512
3758bf0164SHarini Katakam #define CDNS_WDT_PRESCALE_4096	4096
3858bf0164SHarini Katakam #define CDNS_WDT_PRESCALE_SELECT_64	1
3958bf0164SHarini Katakam #define CDNS_WDT_PRESCALE_SELECT_512	2
4058bf0164SHarini Katakam #define CDNS_WDT_PRESCALE_SELECT_4096	3
4158bf0164SHarini Katakam 
4258bf0164SHarini Katakam /* Input clock frequency */
4358bf0164SHarini Katakam #define CDNS_WDT_CLK_10MHZ	10000000
4458bf0164SHarini Katakam #define CDNS_WDT_CLK_75MHZ	75000000
4558bf0164SHarini Katakam 
4658bf0164SHarini Katakam /* Counter maximum value */
4758bf0164SHarini Katakam #define CDNS_WDT_COUNTER_MAX 0xFFF
4858bf0164SHarini Katakam 
490ddad77bSTomas Melin static int wdt_timeout;
5058bf0164SHarini Katakam static int nowayout = WATCHDOG_NOWAYOUT;
5158bf0164SHarini Katakam 
52db202f4dSMichal Simek module_param(wdt_timeout, int, 0644);
5358bf0164SHarini Katakam MODULE_PARM_DESC(wdt_timeout,
5458bf0164SHarini Katakam 		 "Watchdog time in seconds. (default="
5558bf0164SHarini Katakam 		 __MODULE_STRING(CDNS_WDT_DEFAULT_TIMEOUT) ")");
5658bf0164SHarini Katakam 
57db202f4dSMichal Simek module_param(nowayout, int, 0644);
5858bf0164SHarini Katakam MODULE_PARM_DESC(nowayout,
5958bf0164SHarini Katakam 		 "Watchdog cannot be stopped once started (default="
6058bf0164SHarini Katakam 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
6158bf0164SHarini Katakam 
6258bf0164SHarini Katakam /**
6358bf0164SHarini Katakam  * struct cdns_wdt - Watchdog device structure
6458bf0164SHarini Katakam  * @regs: baseaddress of device
6558bf0164SHarini Katakam  * @rst: reset flag
6658bf0164SHarini Katakam  * @clk: struct clk * of a clock source
6758bf0164SHarini Katakam  * @prescaler: for saving prescaler value
6858bf0164SHarini Katakam  * @ctrl_clksel: counter clock prescaler selection
6958bf0164SHarini Katakam  * @io_lock: spinlock for IO register access
7058bf0164SHarini Katakam  * @cdns_wdt_device: watchdog device structure
7158bf0164SHarini Katakam  *
7258bf0164SHarini Katakam  * Structure containing parameters specific to cadence watchdog.
7358bf0164SHarini Katakam  */
7458bf0164SHarini Katakam struct cdns_wdt {
7558bf0164SHarini Katakam 	void __iomem		*regs;
7658bf0164SHarini Katakam 	bool			rst;
7758bf0164SHarini Katakam 	struct clk		*clk;
7858bf0164SHarini Katakam 	u32			prescaler;
7958bf0164SHarini Katakam 	u32			ctrl_clksel;
8058bf0164SHarini Katakam 	spinlock_t		io_lock;
8158bf0164SHarini Katakam 	struct watchdog_device	cdns_wdt_device;
8258bf0164SHarini Katakam };
8358bf0164SHarini Katakam 
8458bf0164SHarini Katakam /* Write access to Registers */
cdns_wdt_writereg(struct cdns_wdt * wdt,u32 offset,u32 val)8558bf0164SHarini Katakam static inline void cdns_wdt_writereg(struct cdns_wdt *wdt, u32 offset, u32 val)
8658bf0164SHarini Katakam {
8758bf0164SHarini Katakam 	writel_relaxed(val, wdt->regs + offset);
8858bf0164SHarini Katakam }
8958bf0164SHarini Katakam 
9058bf0164SHarini Katakam /*************************Register Map**************************************/
9158bf0164SHarini Katakam 
9258bf0164SHarini Katakam /* Register Offsets for the WDT */
9358bf0164SHarini Katakam #define CDNS_WDT_ZMR_OFFSET	0x0	/* Zero Mode Register */
9458bf0164SHarini Katakam #define CDNS_WDT_CCR_OFFSET	0x4	/* Counter Control Register */
9558bf0164SHarini Katakam #define CDNS_WDT_RESTART_OFFSET	0x8	/* Restart Register */
9658bf0164SHarini Katakam #define CDNS_WDT_SR_OFFSET	0xC	/* Status Register */
9758bf0164SHarini Katakam 
9858bf0164SHarini Katakam /*
9958bf0164SHarini Katakam  * Zero Mode Register - This register controls how the time out is indicated
10058bf0164SHarini Katakam  * and also contains the access code to allow writes to the register (0xABC).
10158bf0164SHarini Katakam  */
10258bf0164SHarini Katakam #define CDNS_WDT_ZMR_WDEN_MASK	0x00000001 /* Enable the WDT */
10358bf0164SHarini Katakam #define CDNS_WDT_ZMR_RSTEN_MASK	0x00000002 /* Enable the reset output */
10458bf0164SHarini Katakam #define CDNS_WDT_ZMR_IRQEN_MASK	0x00000004 /* Enable IRQ output */
10558bf0164SHarini Katakam #define CDNS_WDT_ZMR_RSTLEN_16	0x00000030 /* Reset pulse of 16 pclk cycles */
10658bf0164SHarini Katakam #define CDNS_WDT_ZMR_ZKEY_VAL	0x00ABC000 /* Access key, 0xABC << 12 */
10758bf0164SHarini Katakam /*
10858bf0164SHarini Katakam  * Counter Control register - This register controls how fast the timer runs
10958bf0164SHarini Katakam  * and the reset value and also contains the access code to allow writes to
11058bf0164SHarini Katakam  * the register.
11158bf0164SHarini Katakam  */
11258bf0164SHarini Katakam #define CDNS_WDT_CCR_CRV_MASK	0x00003FFC /* Counter reset value */
11358bf0164SHarini Katakam 
11458bf0164SHarini Katakam /**
11558bf0164SHarini Katakam  * cdns_wdt_stop - Stop the watchdog.
11658bf0164SHarini Katakam  *
11758bf0164SHarini Katakam  * @wdd: watchdog device
11858bf0164SHarini Katakam  *
11958bf0164SHarini Katakam  * Read the contents of the ZMR register, clear the WDEN bit
12058bf0164SHarini Katakam  * in the register and set the access key for successful write.
12158bf0164SHarini Katakam  *
12258bf0164SHarini Katakam  * Return: always 0
12358bf0164SHarini Katakam  */
cdns_wdt_stop(struct watchdog_device * wdd)12458bf0164SHarini Katakam static int cdns_wdt_stop(struct watchdog_device *wdd)
12558bf0164SHarini Katakam {
12658bf0164SHarini Katakam 	struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
12758bf0164SHarini Katakam 
12858bf0164SHarini Katakam 	spin_lock(&wdt->io_lock);
12958bf0164SHarini Katakam 	cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET,
13058bf0164SHarini Katakam 			  CDNS_WDT_ZMR_ZKEY_VAL & (~CDNS_WDT_ZMR_WDEN_MASK));
13158bf0164SHarini Katakam 	spin_unlock(&wdt->io_lock);
13258bf0164SHarini Katakam 
13358bf0164SHarini Katakam 	return 0;
13458bf0164SHarini Katakam }
13558bf0164SHarini Katakam 
13658bf0164SHarini Katakam /**
13758bf0164SHarini Katakam  * cdns_wdt_reload - Reload the watchdog timer (i.e. pat the watchdog).
13858bf0164SHarini Katakam  *
13958bf0164SHarini Katakam  * @wdd: watchdog device
14058bf0164SHarini Katakam  *
14158bf0164SHarini Katakam  * Write the restart key value (0x00001999) to the restart register.
14258bf0164SHarini Katakam  *
14358bf0164SHarini Katakam  * Return: always 0
14458bf0164SHarini Katakam  */
cdns_wdt_reload(struct watchdog_device * wdd)14558bf0164SHarini Katakam static int cdns_wdt_reload(struct watchdog_device *wdd)
14658bf0164SHarini Katakam {
14758bf0164SHarini Katakam 	struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
14858bf0164SHarini Katakam 
14958bf0164SHarini Katakam 	spin_lock(&wdt->io_lock);
15058bf0164SHarini Katakam 	cdns_wdt_writereg(wdt, CDNS_WDT_RESTART_OFFSET,
15158bf0164SHarini Katakam 			  CDNS_WDT_RESTART_KEY);
15258bf0164SHarini Katakam 	spin_unlock(&wdt->io_lock);
15358bf0164SHarini Katakam 
15458bf0164SHarini Katakam 	return 0;
15558bf0164SHarini Katakam }
15658bf0164SHarini Katakam 
15758bf0164SHarini Katakam /**
15858bf0164SHarini Katakam  * cdns_wdt_start - Enable and start the watchdog.
15958bf0164SHarini Katakam  *
16058bf0164SHarini Katakam  * @wdd: watchdog device
16158bf0164SHarini Katakam  *
16258bf0164SHarini Katakam  * The counter value is calculated according to the formula:
16358bf0164SHarini Katakam  *		calculated count = (timeout * clock) / prescaler + 1.
16458bf0164SHarini Katakam  * The calculated count is divided by 0x1000 to obtain the field value
16558bf0164SHarini Katakam  * to write to counter control register.
16658bf0164SHarini Katakam  * Clears the contents of prescaler and counter reset value. Sets the
16758bf0164SHarini Katakam  * prescaler to 4096 and the calculated count and access key
16858bf0164SHarini Katakam  * to write to CCR Register.
16958bf0164SHarini Katakam  * Sets the WDT (WDEN bit) and either the Reset signal(RSTEN bit)
17058bf0164SHarini Katakam  * or Interrupt signal(IRQEN) with a specified cycles and the access
17158bf0164SHarini Katakam  * key to write to ZMR Register.
17258bf0164SHarini Katakam  *
17358bf0164SHarini Katakam  * Return: always 0
17458bf0164SHarini Katakam  */
cdns_wdt_start(struct watchdog_device * wdd)17558bf0164SHarini Katakam static int cdns_wdt_start(struct watchdog_device *wdd)
17658bf0164SHarini Katakam {
17758bf0164SHarini Katakam 	struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
17858bf0164SHarini Katakam 	unsigned int data = 0;
17958bf0164SHarini Katakam 	unsigned short count;
18058bf0164SHarini Katakam 	unsigned long clock_f = clk_get_rate(wdt->clk);
18158bf0164SHarini Katakam 
18258bf0164SHarini Katakam 	/*
18358bf0164SHarini Katakam 	 * Counter value divisor to obtain the value of
18458bf0164SHarini Katakam 	 * counter reset to be written to control register.
18558bf0164SHarini Katakam 	 */
18658bf0164SHarini Katakam 	count = (wdd->timeout * (clock_f / wdt->prescaler)) /
18758bf0164SHarini Katakam 		 CDNS_WDT_COUNTER_VALUE_DIVISOR + 1;
18858bf0164SHarini Katakam 
18958bf0164SHarini Katakam 	if (count > CDNS_WDT_COUNTER_MAX)
19058bf0164SHarini Katakam 		count = CDNS_WDT_COUNTER_MAX;
19158bf0164SHarini Katakam 
19258bf0164SHarini Katakam 	spin_lock(&wdt->io_lock);
19358bf0164SHarini Katakam 	cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET,
19458bf0164SHarini Katakam 			  CDNS_WDT_ZMR_ZKEY_VAL);
19558bf0164SHarini Katakam 
19658bf0164SHarini Katakam 	count = (count << 2) & CDNS_WDT_CCR_CRV_MASK;
19758bf0164SHarini Katakam 
19858bf0164SHarini Katakam 	/* Write counter access key first to be able write to register */
19958bf0164SHarini Katakam 	data = count | CDNS_WDT_REGISTER_ACCESS_KEY | wdt->ctrl_clksel;
20058bf0164SHarini Katakam 	cdns_wdt_writereg(wdt, CDNS_WDT_CCR_OFFSET, data);
20158bf0164SHarini Katakam 	data = CDNS_WDT_ZMR_WDEN_MASK | CDNS_WDT_ZMR_RSTLEN_16 |
20258bf0164SHarini Katakam 	       CDNS_WDT_ZMR_ZKEY_VAL;
20358bf0164SHarini Katakam 
20458bf0164SHarini Katakam 	/* Reset on timeout if specified in device tree. */
20558bf0164SHarini Katakam 	if (wdt->rst) {
20658bf0164SHarini Katakam 		data |= CDNS_WDT_ZMR_RSTEN_MASK;
20758bf0164SHarini Katakam 		data &= ~CDNS_WDT_ZMR_IRQEN_MASK;
20858bf0164SHarini Katakam 	} else {
20958bf0164SHarini Katakam 		data &= ~CDNS_WDT_ZMR_RSTEN_MASK;
21058bf0164SHarini Katakam 		data |= CDNS_WDT_ZMR_IRQEN_MASK;
21158bf0164SHarini Katakam 	}
21258bf0164SHarini Katakam 	cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET, data);
21358bf0164SHarini Katakam 	cdns_wdt_writereg(wdt, CDNS_WDT_RESTART_OFFSET,
21458bf0164SHarini Katakam 			  CDNS_WDT_RESTART_KEY);
21558bf0164SHarini Katakam 	spin_unlock(&wdt->io_lock);
21658bf0164SHarini Katakam 
21758bf0164SHarini Katakam 	return 0;
21858bf0164SHarini Katakam }
21958bf0164SHarini Katakam 
22058bf0164SHarini Katakam /**
22158bf0164SHarini Katakam  * cdns_wdt_settimeout - Set a new timeout value for the watchdog device.
22258bf0164SHarini Katakam  *
22358bf0164SHarini Katakam  * @wdd: watchdog device
22458bf0164SHarini Katakam  * @new_time: new timeout value that needs to be set
22558bf0164SHarini Katakam  * Return: 0 on success
22658bf0164SHarini Katakam  *
22758bf0164SHarini Katakam  * Update the watchdog_device timeout with new value which is used when
22858bf0164SHarini Katakam  * cdns_wdt_start is called.
22958bf0164SHarini Katakam  */
cdns_wdt_settimeout(struct watchdog_device * wdd,unsigned int new_time)23058bf0164SHarini Katakam static int cdns_wdt_settimeout(struct watchdog_device *wdd,
23158bf0164SHarini Katakam 			       unsigned int new_time)
23258bf0164SHarini Katakam {
23358bf0164SHarini Katakam 	wdd->timeout = new_time;
23458bf0164SHarini Katakam 
23558bf0164SHarini Katakam 	return cdns_wdt_start(wdd);
23658bf0164SHarini Katakam }
23758bf0164SHarini Katakam 
23858bf0164SHarini Katakam /**
23958bf0164SHarini Katakam  * cdns_wdt_irq_handler - Notifies of watchdog timeout.
24058bf0164SHarini Katakam  *
24158bf0164SHarini Katakam  * @irq: interrupt number
24258bf0164SHarini Katakam  * @dev_id: pointer to a platform device structure
24358bf0164SHarini Katakam  * Return: IRQ_HANDLED
24458bf0164SHarini Katakam  *
24558bf0164SHarini Katakam  * The handler is invoked when the watchdog times out and a
24658bf0164SHarini Katakam  * reset on timeout has not been enabled.
24758bf0164SHarini Katakam  */
cdns_wdt_irq_handler(int irq,void * dev_id)24858bf0164SHarini Katakam static irqreturn_t cdns_wdt_irq_handler(int irq, void *dev_id)
24958bf0164SHarini Katakam {
25058bf0164SHarini Katakam 	struct platform_device *pdev = dev_id;
25158bf0164SHarini Katakam 
25258bf0164SHarini Katakam 	dev_info(&pdev->dev,
25358bf0164SHarini Katakam 		 "Watchdog timed out. Internal reset not enabled\n");
25458bf0164SHarini Katakam 
25558bf0164SHarini Katakam 	return IRQ_HANDLED;
25658bf0164SHarini Katakam }
25758bf0164SHarini Katakam 
25858bf0164SHarini Katakam /*
25958bf0164SHarini Katakam  * Info structure used to indicate the features supported by the device
26058bf0164SHarini Katakam  * to the upper layers. This is defined in watchdog.h header file.
26158bf0164SHarini Katakam  */
2626c368932SBhumika Goyal static const struct watchdog_info cdns_wdt_info = {
26358bf0164SHarini Katakam 	.identity	= "cdns_wdt watchdog",
26458bf0164SHarini Katakam 	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
26558bf0164SHarini Katakam 			  WDIOF_MAGICCLOSE,
26658bf0164SHarini Katakam };
26758bf0164SHarini Katakam 
26858bf0164SHarini Katakam /* Watchdog Core Ops */
26985f15cfcSJulia Lawall static const struct watchdog_ops cdns_wdt_ops = {
27058bf0164SHarini Katakam 	.owner = THIS_MODULE,
27158bf0164SHarini Katakam 	.start = cdns_wdt_start,
27258bf0164SHarini Katakam 	.stop = cdns_wdt_stop,
27358bf0164SHarini Katakam 	.ping = cdns_wdt_reload,
27458bf0164SHarini Katakam 	.set_timeout = cdns_wdt_settimeout,
27558bf0164SHarini Katakam };
27658bf0164SHarini Katakam 
27758bf0164SHarini Katakam /************************Platform Operations*****************************/
27858bf0164SHarini Katakam /**
27958bf0164SHarini Katakam  * cdns_wdt_probe - Probe call for the device.
28058bf0164SHarini Katakam  *
28158bf0164SHarini Katakam  * @pdev: handle to the platform device structure.
28258bf0164SHarini Katakam  * Return: 0 on success, negative error otherwise.
28358bf0164SHarini Katakam  *
28458bf0164SHarini Katakam  * It does all the memory allocation and registration for the device.
28558bf0164SHarini Katakam  */
cdns_wdt_probe(struct platform_device * pdev)28658bf0164SHarini Katakam static int cdns_wdt_probe(struct platform_device *pdev)
28758bf0164SHarini Katakam {
288605d8c4fSGuenter Roeck 	struct device *dev = &pdev->dev;
28958bf0164SHarini Katakam 	int ret, irq;
29058bf0164SHarini Katakam 	unsigned long clock_f;
29158bf0164SHarini Katakam 	struct cdns_wdt *wdt;
29258bf0164SHarini Katakam 	struct watchdog_device *cdns_wdt_device;
29358bf0164SHarini Katakam 
294605d8c4fSGuenter Roeck 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
29558bf0164SHarini Katakam 	if (!wdt)
29658bf0164SHarini Katakam 		return -ENOMEM;
29758bf0164SHarini Katakam 
29858bf0164SHarini Katakam 	cdns_wdt_device = &wdt->cdns_wdt_device;
29958bf0164SHarini Katakam 	cdns_wdt_device->info = &cdns_wdt_info;
30058bf0164SHarini Katakam 	cdns_wdt_device->ops = &cdns_wdt_ops;
30158bf0164SHarini Katakam 	cdns_wdt_device->timeout = CDNS_WDT_DEFAULT_TIMEOUT;
30258bf0164SHarini Katakam 	cdns_wdt_device->min_timeout = CDNS_WDT_MIN_TIMEOUT;
30358bf0164SHarini Katakam 	cdns_wdt_device->max_timeout = CDNS_WDT_MAX_TIMEOUT;
30458bf0164SHarini Katakam 
3050f0a6a28SGuenter Roeck 	wdt->regs = devm_platform_ioremap_resource(pdev, 0);
30658bf0164SHarini Katakam 	if (IS_ERR(wdt->regs))
30758bf0164SHarini Katakam 		return PTR_ERR(wdt->regs);
30858bf0164SHarini Katakam 
30958bf0164SHarini Katakam 	/* Register the interrupt */
310605d8c4fSGuenter Roeck 	wdt->rst = of_property_read_bool(dev->of_node, "reset-on-timeout");
31158bf0164SHarini Katakam 	irq = platform_get_irq(pdev, 0);
31258bf0164SHarini Katakam 	if (!wdt->rst && irq >= 0) {
313605d8c4fSGuenter Roeck 		ret = devm_request_irq(dev, irq, cdns_wdt_irq_handler, 0,
31458bf0164SHarini Katakam 				       pdev->name, pdev);
31558bf0164SHarini Katakam 		if (ret) {
316605d8c4fSGuenter Roeck 			dev_err(dev,
31758bf0164SHarini Katakam 				"cannot register interrupt handler err=%d\n",
31858bf0164SHarini Katakam 				ret);
31958bf0164SHarini Katakam 			return ret;
32058bf0164SHarini Katakam 		}
32158bf0164SHarini Katakam 	}
32258bf0164SHarini Katakam 
32358bf0164SHarini Katakam 	/* Initialize the members of cdns_wdt structure */
324605d8c4fSGuenter Roeck 	cdns_wdt_device->parent = dev;
32558bf0164SHarini Katakam 
326c7d30d42SWolfram Sang 	watchdog_init_timeout(cdns_wdt_device, wdt_timeout, dev);
32758bf0164SHarini Katakam 	watchdog_set_nowayout(cdns_wdt_device, nowayout);
3285e1f976fSDamien Riegel 	watchdog_stop_on_reboot(cdns_wdt_device);
32958bf0164SHarini Katakam 	watchdog_set_drvdata(cdns_wdt_device, wdt);
33058bf0164SHarini Katakam 
331*616a2fe3SChristophe JAILLET 	wdt->clk = devm_clk_get_enabled(dev, NULL);
332dab11221SKrzysztof Kozlowski 	if (IS_ERR(wdt->clk))
333dab11221SKrzysztof Kozlowski 		return dev_err_probe(dev, PTR_ERR(wdt->clk),
334dab11221SKrzysztof Kozlowski 				     "input clock not found\n");
33558bf0164SHarini Katakam 
33658bf0164SHarini Katakam 	clock_f = clk_get_rate(wdt->clk);
33758bf0164SHarini Katakam 	if (clock_f <= CDNS_WDT_CLK_75MHZ) {
33858bf0164SHarini Katakam 		wdt->prescaler = CDNS_WDT_PRESCALE_512;
33958bf0164SHarini Katakam 		wdt->ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_512;
34058bf0164SHarini Katakam 	} else {
34158bf0164SHarini Katakam 		wdt->prescaler = CDNS_WDT_PRESCALE_4096;
34258bf0164SHarini Katakam 		wdt->ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_4096;
34358bf0164SHarini Katakam 	}
34458bf0164SHarini Katakam 
34558bf0164SHarini Katakam 	spin_lock_init(&wdt->io_lock);
34658bf0164SHarini Katakam 
347605d8c4fSGuenter Roeck 	watchdog_stop_on_reboot(cdns_wdt_device);
348605d8c4fSGuenter Roeck 	watchdog_stop_on_unregister(cdns_wdt_device);
349605d8c4fSGuenter Roeck 	ret = devm_watchdog_register_device(dev, cdns_wdt_device);
35076ed828bSWolfram Sang 	if (ret)
351605d8c4fSGuenter Roeck 		return ret;
35258bf0164SHarini Katakam 	platform_set_drvdata(pdev, wdt);
35358bf0164SHarini Katakam 
354b1301b90SSrinivas Neeli 	dev_info(dev, "Xilinx Watchdog Timer with timeout %ds%s\n",
355b1301b90SSrinivas Neeli 		 cdns_wdt_device->timeout, nowayout ? ", nowayout" : "");
35658bf0164SHarini Katakam 
35758bf0164SHarini Katakam 	return 0;
35858bf0164SHarini Katakam }
35958bf0164SHarini Katakam 
36058bf0164SHarini Katakam /**
36158bf0164SHarini Katakam  * cdns_wdt_suspend - Stop the device.
36258bf0164SHarini Katakam  *
36358bf0164SHarini Katakam  * @dev: handle to the device structure.
36458bf0164SHarini Katakam  * Return: 0 always.
36558bf0164SHarini Katakam  */
cdns_wdt_suspend(struct device * dev)36658bf0164SHarini Katakam static int __maybe_unused cdns_wdt_suspend(struct device *dev)
36758bf0164SHarini Katakam {
36820745634SWolfram Sang 	struct cdns_wdt *wdt = dev_get_drvdata(dev);
36958bf0164SHarini Katakam 
370eadc4fe1SShubhrajyoti Datta 	if (watchdog_active(&wdt->cdns_wdt_device)) {
37158bf0164SHarini Katakam 		cdns_wdt_stop(&wdt->cdns_wdt_device);
37258bf0164SHarini Katakam 		clk_disable_unprepare(wdt->clk);
373eadc4fe1SShubhrajyoti Datta 	}
37458bf0164SHarini Katakam 
37558bf0164SHarini Katakam 	return 0;
37658bf0164SHarini Katakam }
37758bf0164SHarini Katakam 
37858bf0164SHarini Katakam /**
37958bf0164SHarini Katakam  * cdns_wdt_resume - Resume the device.
38058bf0164SHarini Katakam  *
38158bf0164SHarini Katakam  * @dev: handle to the device structure.
38258bf0164SHarini Katakam  * Return: 0 on success, errno otherwise.
38358bf0164SHarini Katakam  */
cdns_wdt_resume(struct device * dev)38458bf0164SHarini Katakam static int __maybe_unused cdns_wdt_resume(struct device *dev)
38558bf0164SHarini Katakam {
38658bf0164SHarini Katakam 	int ret;
38720745634SWolfram Sang 	struct cdns_wdt *wdt = dev_get_drvdata(dev);
38858bf0164SHarini Katakam 
389eadc4fe1SShubhrajyoti Datta 	if (watchdog_active(&wdt->cdns_wdt_device)) {
39058bf0164SHarini Katakam 		ret = clk_prepare_enable(wdt->clk);
39158bf0164SHarini Katakam 		if (ret) {
39258bf0164SHarini Katakam 			dev_err(dev, "unable to enable clock\n");
39358bf0164SHarini Katakam 			return ret;
39458bf0164SHarini Katakam 		}
39558bf0164SHarini Katakam 		cdns_wdt_start(&wdt->cdns_wdt_device);
396eadc4fe1SShubhrajyoti Datta 	}
39758bf0164SHarini Katakam 
39858bf0164SHarini Katakam 	return 0;
39958bf0164SHarini Katakam }
40058bf0164SHarini Katakam 
40158bf0164SHarini Katakam static SIMPLE_DEV_PM_OPS(cdns_wdt_pm_ops, cdns_wdt_suspend, cdns_wdt_resume);
40258bf0164SHarini Katakam 
403011e29e7SArvind Yadav static const struct of_device_id cdns_wdt_of_match[] = {
40458bf0164SHarini Katakam 	{ .compatible = "cdns,wdt-r1p2", },
40558bf0164SHarini Katakam 	{ /* end of table */ }
40658bf0164SHarini Katakam };
40758bf0164SHarini Katakam MODULE_DEVICE_TABLE(of, cdns_wdt_of_match);
40858bf0164SHarini Katakam 
40958bf0164SHarini Katakam /* Driver Structure */
41058bf0164SHarini Katakam static struct platform_driver cdns_wdt_driver = {
41158bf0164SHarini Katakam 	.probe		= cdns_wdt_probe,
41258bf0164SHarini Katakam 	.driver		= {
41358bf0164SHarini Katakam 		.name	= "cdns-wdt",
41458bf0164SHarini Katakam 		.of_match_table = cdns_wdt_of_match,
41558bf0164SHarini Katakam 		.pm	= &cdns_wdt_pm_ops,
41658bf0164SHarini Katakam 	},
41758bf0164SHarini Katakam };
41858bf0164SHarini Katakam 
41958bf0164SHarini Katakam module_platform_driver(cdns_wdt_driver);
42058bf0164SHarini Katakam 
42158bf0164SHarini Katakam MODULE_AUTHOR("Xilinx, Inc.");
42258bf0164SHarini Katakam MODULE_DESCRIPTION("Watchdog driver for Cadence WDT");
42358bf0164SHarini Katakam MODULE_LICENSE("GPL");
424