xref: /openbmc/linux/drivers/watchdog/aspeed_wdt.c (revision f814388063212c03339672772c2a1bd86b56d57c)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2efa859f7SJoel Stanley /*
3efa859f7SJoel Stanley  * Copyright 2016 IBM Corporation
4efa859f7SJoel Stanley  *
5efa859f7SJoel Stanley  * Joel Stanley <joel@jms.id.au>
6efa859f7SJoel Stanley  */
7efa859f7SJoel Stanley 
89ec0b7e0SEddie James #include <linux/bits.h>
9efa859f7SJoel Stanley #include <linux/delay.h>
109ec0b7e0SEddie James #include <linux/interrupt.h>
11efa859f7SJoel Stanley #include <linux/io.h>
12efa859f7SJoel Stanley #include <linux/kernel.h>
13a243cb93SChristophe JAILLET #include <linux/kstrtox.h>
14efa859f7SJoel Stanley #include <linux/module.h>
15efa859f7SJoel Stanley #include <linux/of.h>
169ec0b7e0SEddie James #include <linux/of_irq.h>
17efa859f7SJoel Stanley #include <linux/platform_device.h>
18efa859f7SJoel Stanley #include <linux/watchdog.h>
19efa859f7SJoel Stanley 
204ed1a6b6SEduardo Valentin static bool nowayout = WATCHDOG_NOWAYOUT;
214ed1a6b6SEduardo Valentin module_param(nowayout, bool, 0);
224ed1a6b6SEduardo Valentin MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
234ed1a6b6SEduardo Valentin 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
244ed1a6b6SEduardo Valentin 
259ec0b7e0SEddie James struct aspeed_wdt_config {
269ec0b7e0SEddie James 	u32 ext_pulse_width_mask;
279ec0b7e0SEddie James 	u32 irq_shift;
289ec0b7e0SEddie James 	u32 irq_mask;
299ec0b7e0SEddie James };
309ec0b7e0SEddie James 
31efa859f7SJoel Stanley struct aspeed_wdt {
32efa859f7SJoel Stanley 	struct watchdog_device	wdd;
33efa859f7SJoel Stanley 	void __iomem		*base;
34efa859f7SJoel Stanley 	u32			ctrl;
359ec0b7e0SEddie James 	const struct aspeed_wdt_config *cfg;
36012c0460SAndrew Jeffery };
37012c0460SAndrew Jeffery 
38012c0460SAndrew Jeffery static const struct aspeed_wdt_config ast2400_config = {
39012c0460SAndrew Jeffery 	.ext_pulse_width_mask = 0xff,
409ec0b7e0SEddie James 	.irq_shift = 0,
419ec0b7e0SEddie James 	.irq_mask = 0,
42012c0460SAndrew Jeffery };
43012c0460SAndrew Jeffery 
44012c0460SAndrew Jeffery static const struct aspeed_wdt_config ast2500_config = {
45012c0460SAndrew Jeffery 	.ext_pulse_width_mask = 0xfffff,
469ec0b7e0SEddie James 	.irq_shift = 12,
479ec0b7e0SEddie James 	.irq_mask = GENMASK(31, 12),
489ec0b7e0SEddie James };
499ec0b7e0SEddie James 
509ec0b7e0SEddie James static const struct aspeed_wdt_config ast2600_config = {
519ec0b7e0SEddie James 	.ext_pulse_width_mask = 0xfffff,
529ec0b7e0SEddie James 	.irq_shift = 0,
539ec0b7e0SEddie James 	.irq_mask = GENMASK(31, 10),
54012c0460SAndrew Jeffery };
55012c0460SAndrew Jeffery 
56efa859f7SJoel Stanley static const struct of_device_id aspeed_wdt_of_table[] = {
57012c0460SAndrew Jeffery 	{ .compatible = "aspeed,ast2400-wdt", .data = &ast2400_config },
58012c0460SAndrew Jeffery 	{ .compatible = "aspeed,ast2500-wdt", .data = &ast2500_config },
599ec0b7e0SEddie James 	{ .compatible = "aspeed,ast2600-wdt", .data = &ast2600_config },
60efa859f7SJoel Stanley 	{ },
61efa859f7SJoel Stanley };
62efa859f7SJoel Stanley MODULE_DEVICE_TABLE(of, aspeed_wdt_of_table);
63efa859f7SJoel Stanley 
64efa859f7SJoel Stanley #define WDT_STATUS		0x00
65efa859f7SJoel Stanley #define WDT_RELOAD_VALUE	0x04
66efa859f7SJoel Stanley #define WDT_RESTART		0x08
67efa859f7SJoel Stanley #define WDT_CTRL		0x0C
686ffa3402SMilton Miller #define   WDT_CTRL_BOOT_SECONDARY	BIT(7)
69efa859f7SJoel Stanley #define   WDT_CTRL_RESET_MODE_SOC	(0x00 << 5)
70efa859f7SJoel Stanley #define   WDT_CTRL_RESET_MODE_FULL_CHIP	(0x01 << 5)
71b7f0b8adSChristopher Bostic #define   WDT_CTRL_RESET_MODE_ARM_CPU	(0x10 << 5)
72efa859f7SJoel Stanley #define   WDT_CTRL_1MHZ_CLK		BIT(4)
73efa859f7SJoel Stanley #define   WDT_CTRL_WDT_EXT		BIT(3)
74efa859f7SJoel Stanley #define   WDT_CTRL_WDT_INTR		BIT(2)
75efa859f7SJoel Stanley #define   WDT_CTRL_RESET_SYSTEM		BIT(1)
76efa859f7SJoel Stanley #define   WDT_CTRL_ENABLE		BIT(0)
7749d4d277SEddie James #define WDT_TIMEOUT_STATUS	0x10
789ec0b7e0SEddie James #define   WDT_TIMEOUT_STATUS_IRQ		BIT(2)
7949d4d277SEddie James #define   WDT_TIMEOUT_STATUS_BOOT_SECONDARY	BIT(1)
803d9e89bdSIvan Mikhaylov #define WDT_CLEAR_TIMEOUT_STATUS	0x14
813d9e89bdSIvan Mikhaylov #define   WDT_CLEAR_TIMEOUT_AND_BOOT_CODE_SELECTION	BIT(0)
82*f8143880SZev Weiss #define WDT_RESET_MASK1		0x1c
83*f8143880SZev Weiss #define WDT_RESET_MASK2		0x20
84efa859f7SJoel Stanley 
85012c0460SAndrew Jeffery /*
86012c0460SAndrew Jeffery  * WDT_RESET_WIDTH controls the characteristics of the external pulse (if
87012c0460SAndrew Jeffery  * enabled), specifically:
88012c0460SAndrew Jeffery  *
89012c0460SAndrew Jeffery  * * Pulse duration
90012c0460SAndrew Jeffery  * * Drive mode: push-pull vs open-drain
91012c0460SAndrew Jeffery  * * Polarity: Active high or active low
92012c0460SAndrew Jeffery  *
93012c0460SAndrew Jeffery  * Pulse duration configuration is available on both the AST2400 and AST2500,
94012c0460SAndrew Jeffery  * though the field changes between SoCs:
95012c0460SAndrew Jeffery  *
96012c0460SAndrew Jeffery  * AST2400: Bits 7:0
97012c0460SAndrew Jeffery  * AST2500: Bits 19:0
98012c0460SAndrew Jeffery  *
99012c0460SAndrew Jeffery  * This difference is captured in struct aspeed_wdt_config.
100012c0460SAndrew Jeffery  *
101012c0460SAndrew Jeffery  * The AST2500 exposes the drive mode and polarity options, but not in a
102012c0460SAndrew Jeffery  * regular fashion. For read purposes, bit 31 represents active high or low,
103012c0460SAndrew Jeffery  * and bit 30 represents push-pull or open-drain. With respect to write, magic
104012c0460SAndrew Jeffery  * values need to be written to the top byte to change the state of the drive
105012c0460SAndrew Jeffery  * mode and polarity bits. Any other value written to the top byte has no
106012c0460SAndrew Jeffery  * effect on the state of the drive mode or polarity bits. However, the pulse
107012c0460SAndrew Jeffery  * width value must be preserved (as desired) if written.
108012c0460SAndrew Jeffery  */
109012c0460SAndrew Jeffery #define WDT_RESET_WIDTH		0x18
110012c0460SAndrew Jeffery #define   WDT_RESET_WIDTH_ACTIVE_HIGH	BIT(31)
111012c0460SAndrew Jeffery #define     WDT_ACTIVE_HIGH_MAGIC	(0xA5 << 24)
112012c0460SAndrew Jeffery #define     WDT_ACTIVE_LOW_MAGIC	(0x5A << 24)
113012c0460SAndrew Jeffery #define   WDT_RESET_WIDTH_PUSH_PULL	BIT(30)
114012c0460SAndrew Jeffery #define     WDT_PUSH_PULL_MAGIC		(0xA8 << 24)
115012c0460SAndrew Jeffery #define     WDT_OPEN_DRAIN_MAGIC	(0x8A << 24)
116012c0460SAndrew Jeffery 
117efa859f7SJoel Stanley #define WDT_RESTART_MAGIC	0x4755
118efa859f7SJoel Stanley 
119efa859f7SJoel Stanley /* 32 bits at 1MHz, in milliseconds */
120efa859f7SJoel Stanley #define WDT_MAX_TIMEOUT_MS	4294967
121efa859f7SJoel Stanley #define WDT_DEFAULT_TIMEOUT	30
122efa859f7SJoel Stanley #define WDT_RATE_1MHZ		1000000
123efa859f7SJoel Stanley 
to_aspeed_wdt(struct watchdog_device * wdd)124efa859f7SJoel Stanley static struct aspeed_wdt *to_aspeed_wdt(struct watchdog_device *wdd)
125efa859f7SJoel Stanley {
126efa859f7SJoel Stanley 	return container_of(wdd, struct aspeed_wdt, wdd);
127efa859f7SJoel Stanley }
128efa859f7SJoel Stanley 
aspeed_wdt_enable(struct aspeed_wdt * wdt,int count)129efa859f7SJoel Stanley static void aspeed_wdt_enable(struct aspeed_wdt *wdt, int count)
130efa859f7SJoel Stanley {
131efa859f7SJoel Stanley 	wdt->ctrl |= WDT_CTRL_ENABLE;
132efa859f7SJoel Stanley 
133efa859f7SJoel Stanley 	writel(0, wdt->base + WDT_CTRL);
134efa859f7SJoel Stanley 	writel(count, wdt->base + WDT_RELOAD_VALUE);
135efa859f7SJoel Stanley 	writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
136efa859f7SJoel Stanley 	writel(wdt->ctrl, wdt->base + WDT_CTRL);
137efa859f7SJoel Stanley }
138efa859f7SJoel Stanley 
aspeed_wdt_start(struct watchdog_device * wdd)139efa859f7SJoel Stanley static int aspeed_wdt_start(struct watchdog_device *wdd)
140efa859f7SJoel Stanley {
141efa859f7SJoel Stanley 	struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
142efa859f7SJoel Stanley 
143efa859f7SJoel Stanley 	aspeed_wdt_enable(wdt, wdd->timeout * WDT_RATE_1MHZ);
144efa859f7SJoel Stanley 
145efa859f7SJoel Stanley 	return 0;
146efa859f7SJoel Stanley }
147efa859f7SJoel Stanley 
aspeed_wdt_stop(struct watchdog_device * wdd)148efa859f7SJoel Stanley static int aspeed_wdt_stop(struct watchdog_device *wdd)
149efa859f7SJoel Stanley {
150efa859f7SJoel Stanley 	struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
151efa859f7SJoel Stanley 
152efa859f7SJoel Stanley 	wdt->ctrl &= ~WDT_CTRL_ENABLE;
153efa859f7SJoel Stanley 	writel(wdt->ctrl, wdt->base + WDT_CTRL);
154efa859f7SJoel Stanley 
155efa859f7SJoel Stanley 	return 0;
156efa859f7SJoel Stanley }
157efa859f7SJoel Stanley 
aspeed_wdt_ping(struct watchdog_device * wdd)158efa859f7SJoel Stanley static int aspeed_wdt_ping(struct watchdog_device *wdd)
159efa859f7SJoel Stanley {
160efa859f7SJoel Stanley 	struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
161efa859f7SJoel Stanley 
162efa859f7SJoel Stanley 	writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
163efa859f7SJoel Stanley 
164efa859f7SJoel Stanley 	return 0;
165efa859f7SJoel Stanley }
166efa859f7SJoel Stanley 
aspeed_wdt_set_timeout(struct watchdog_device * wdd,unsigned int timeout)167efa859f7SJoel Stanley static int aspeed_wdt_set_timeout(struct watchdog_device *wdd,
168efa859f7SJoel Stanley 				  unsigned int timeout)
169efa859f7SJoel Stanley {
170efa859f7SJoel Stanley 	struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
171efa859f7SJoel Stanley 	u32 actual;
172efa859f7SJoel Stanley 
173efa859f7SJoel Stanley 	wdd->timeout = timeout;
174efa859f7SJoel Stanley 
175e7dc481cSTao Ren 	actual = min(timeout, wdd->max_hw_heartbeat_ms / 1000);
176efa859f7SJoel Stanley 
177efa859f7SJoel Stanley 	writel(actual * WDT_RATE_1MHZ, wdt->base + WDT_RELOAD_VALUE);
178efa859f7SJoel Stanley 	writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
179efa859f7SJoel Stanley 
180efa859f7SJoel Stanley 	return 0;
181efa859f7SJoel Stanley }
182efa859f7SJoel Stanley 
aspeed_wdt_set_pretimeout(struct watchdog_device * wdd,unsigned int pretimeout)1839ec0b7e0SEddie James static int aspeed_wdt_set_pretimeout(struct watchdog_device *wdd,
1849ec0b7e0SEddie James 				     unsigned int pretimeout)
1859ec0b7e0SEddie James {
1869ec0b7e0SEddie James 	struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
1879ec0b7e0SEddie James 	u32 actual = pretimeout * WDT_RATE_1MHZ;
1889ec0b7e0SEddie James 	u32 s = wdt->cfg->irq_shift;
1899ec0b7e0SEddie James 	u32 m = wdt->cfg->irq_mask;
1909ec0b7e0SEddie James 
1919ec0b7e0SEddie James 	wdd->pretimeout = pretimeout;
1929ec0b7e0SEddie James 	wdt->ctrl &= ~m;
1939ec0b7e0SEddie James 	if (pretimeout)
1949ec0b7e0SEddie James 		wdt->ctrl |= ((actual << s) & m) | WDT_CTRL_WDT_INTR;
1959ec0b7e0SEddie James 	else
1969ec0b7e0SEddie James 		wdt->ctrl &= ~WDT_CTRL_WDT_INTR;
1979ec0b7e0SEddie James 
1989ec0b7e0SEddie James 	writel(wdt->ctrl, wdt->base + WDT_CTRL);
1999ec0b7e0SEddie James 
2009ec0b7e0SEddie James 	return 0;
2019ec0b7e0SEddie James }
2029ec0b7e0SEddie James 
aspeed_wdt_restart(struct watchdog_device * wdd,unsigned long action,void * data)203efa859f7SJoel Stanley static int aspeed_wdt_restart(struct watchdog_device *wdd,
204efa859f7SJoel Stanley 			      unsigned long action, void *data)
205efa859f7SJoel Stanley {
206efa859f7SJoel Stanley 	struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
207efa859f7SJoel Stanley 
2086ffa3402SMilton Miller 	wdt->ctrl &= ~WDT_CTRL_BOOT_SECONDARY;
209efa859f7SJoel Stanley 	aspeed_wdt_enable(wdt, 128 * WDT_RATE_1MHZ / 1000);
210efa859f7SJoel Stanley 
211efa859f7SJoel Stanley 	mdelay(1000);
212efa859f7SJoel Stanley 
213efa859f7SJoel Stanley 	return 0;
214efa859f7SJoel Stanley }
215efa859f7SJoel Stanley 
2163d9e89bdSIvan Mikhaylov /* access_cs0 shows if cs0 is accessible, hence the reverted bit */
access_cs0_show(struct device * dev,struct device_attribute * attr,char * buf)2173d9e89bdSIvan Mikhaylov static ssize_t access_cs0_show(struct device *dev,
2183d9e89bdSIvan Mikhaylov 			       struct device_attribute *attr, char *buf)
2193d9e89bdSIvan Mikhaylov {
2203d9e89bdSIvan Mikhaylov 	struct aspeed_wdt *wdt = dev_get_drvdata(dev);
2213d9e89bdSIvan Mikhaylov 	u32 status = readl(wdt->base + WDT_TIMEOUT_STATUS);
2223d9e89bdSIvan Mikhaylov 
2233bb21781SJuerg Haefliger 	return sysfs_emit(buf, "%u\n",
2243d9e89bdSIvan Mikhaylov 			  !(status & WDT_TIMEOUT_STATUS_BOOT_SECONDARY));
2253d9e89bdSIvan Mikhaylov }
2263d9e89bdSIvan Mikhaylov 
access_cs0_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2273d9e89bdSIvan Mikhaylov static ssize_t access_cs0_store(struct device *dev,
2283d9e89bdSIvan Mikhaylov 				struct device_attribute *attr, const char *buf,
2293d9e89bdSIvan Mikhaylov 				size_t size)
2303d9e89bdSIvan Mikhaylov {
2313d9e89bdSIvan Mikhaylov 	struct aspeed_wdt *wdt = dev_get_drvdata(dev);
2323d9e89bdSIvan Mikhaylov 	unsigned long val;
2333d9e89bdSIvan Mikhaylov 
2343d9e89bdSIvan Mikhaylov 	if (kstrtoul(buf, 10, &val))
2353d9e89bdSIvan Mikhaylov 		return -EINVAL;
2363d9e89bdSIvan Mikhaylov 
2373d9e89bdSIvan Mikhaylov 	if (val)
2383d9e89bdSIvan Mikhaylov 		writel(WDT_CLEAR_TIMEOUT_AND_BOOT_CODE_SELECTION,
2393d9e89bdSIvan Mikhaylov 		       wdt->base + WDT_CLEAR_TIMEOUT_STATUS);
2403d9e89bdSIvan Mikhaylov 
2413d9e89bdSIvan Mikhaylov 	return size;
2423d9e89bdSIvan Mikhaylov }
2433d9e89bdSIvan Mikhaylov 
2443d9e89bdSIvan Mikhaylov /*
2453d9e89bdSIvan Mikhaylov  * This attribute exists only if the system has booted from the alternate
2463d9e89bdSIvan Mikhaylov  * flash with 'alt-boot' option.
2473d9e89bdSIvan Mikhaylov  *
2483d9e89bdSIvan Mikhaylov  * At alternate flash the 'access_cs0' sysfs node provides:
2493d9e89bdSIvan Mikhaylov  *   ast2400: a way to get access to the primary SPI flash chip at CS0
2503d9e89bdSIvan Mikhaylov  *            after booting from the alternate chip at CS1.
2513d9e89bdSIvan Mikhaylov  *   ast2500: a way to restore the normal address mapping from
2523d9e89bdSIvan Mikhaylov  *            (CS0->CS1, CS1->CS0) to (CS0->CS0, CS1->CS1).
2533d9e89bdSIvan Mikhaylov  *
2543d9e89bdSIvan Mikhaylov  * Clearing the boot code selection and timeout counter also resets to the
2553d9e89bdSIvan Mikhaylov  * initial state the chip select line mapping. When the SoC is in normal
2563d9e89bdSIvan Mikhaylov  * mapping state (i.e. booted from CS0), clearing those bits does nothing for
2573d9e89bdSIvan Mikhaylov  * both versions of the SoC. For alternate boot mode (booted from CS1 due to
2583d9e89bdSIvan Mikhaylov  * wdt2 expiration) the behavior differs as described above.
2593d9e89bdSIvan Mikhaylov  *
2603d9e89bdSIvan Mikhaylov  * This option can be used with wdt2 (watchdog1) only.
2613d9e89bdSIvan Mikhaylov  */
2623d9e89bdSIvan Mikhaylov static DEVICE_ATTR_RW(access_cs0);
2633d9e89bdSIvan Mikhaylov 
2643d9e89bdSIvan Mikhaylov static struct attribute *bswitch_attrs[] = {
2653d9e89bdSIvan Mikhaylov 	&dev_attr_access_cs0.attr,
2663d9e89bdSIvan Mikhaylov 	NULL
2673d9e89bdSIvan Mikhaylov };
2683d9e89bdSIvan Mikhaylov ATTRIBUTE_GROUPS(bswitch);
2693d9e89bdSIvan Mikhaylov 
270efa859f7SJoel Stanley static const struct watchdog_ops aspeed_wdt_ops = {
271efa859f7SJoel Stanley 	.start		= aspeed_wdt_start,
272efa859f7SJoel Stanley 	.stop		= aspeed_wdt_stop,
273efa859f7SJoel Stanley 	.ping		= aspeed_wdt_ping,
274efa859f7SJoel Stanley 	.set_timeout	= aspeed_wdt_set_timeout,
2759ec0b7e0SEddie James 	.set_pretimeout = aspeed_wdt_set_pretimeout,
276efa859f7SJoel Stanley 	.restart	= aspeed_wdt_restart,
277efa859f7SJoel Stanley 	.owner		= THIS_MODULE,
278efa859f7SJoel Stanley };
279efa859f7SJoel Stanley 
280efa859f7SJoel Stanley static const struct watchdog_info aspeed_wdt_info = {
281efa859f7SJoel Stanley 	.options	= WDIOF_KEEPALIVEPING
282efa859f7SJoel Stanley 			| WDIOF_MAGICCLOSE
283efa859f7SJoel Stanley 			| WDIOF_SETTIMEOUT,
284efa859f7SJoel Stanley 	.identity	= KBUILD_MODNAME,
285efa859f7SJoel Stanley };
286efa859f7SJoel Stanley 
2879ec0b7e0SEddie James static const struct watchdog_info aspeed_wdt_pretimeout_info = {
2889ec0b7e0SEddie James 	.options	= WDIOF_KEEPALIVEPING
2899ec0b7e0SEddie James 			| WDIOF_PRETIMEOUT
2909ec0b7e0SEddie James 			| WDIOF_MAGICCLOSE
2919ec0b7e0SEddie James 			| WDIOF_SETTIMEOUT,
2929ec0b7e0SEddie James 	.identity	= KBUILD_MODNAME,
2939ec0b7e0SEddie James };
2949ec0b7e0SEddie James 
aspeed_wdt_irq(int irq,void * arg)2959ec0b7e0SEddie James static irqreturn_t aspeed_wdt_irq(int irq, void *arg)
2969ec0b7e0SEddie James {
2979ec0b7e0SEddie James 	struct watchdog_device *wdd = arg;
2989ec0b7e0SEddie James 	struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
2999ec0b7e0SEddie James 	u32 status = readl(wdt->base + WDT_TIMEOUT_STATUS);
3009ec0b7e0SEddie James 
3019ec0b7e0SEddie James 	if (status & WDT_TIMEOUT_STATUS_IRQ)
3029ec0b7e0SEddie James 		watchdog_notify_pretimeout(wdd);
3039ec0b7e0SEddie James 
3049ec0b7e0SEddie James 	return IRQ_HANDLED;
3059ec0b7e0SEddie James }
3069ec0b7e0SEddie James 
aspeed_wdt_probe(struct platform_device * pdev)307efa859f7SJoel Stanley static int aspeed_wdt_probe(struct platform_device *pdev)
308efa859f7SJoel Stanley {
309eda21ee9SGuenter Roeck 	struct device *dev = &pdev->dev;
310012c0460SAndrew Jeffery 	const struct of_device_id *ofdid;
311efa859f7SJoel Stanley 	struct aspeed_wdt *wdt;
312b7f0b8adSChristopher Bostic 	struct device_node *np;
313b7f0b8adSChristopher Bostic 	const char *reset_type;
314012c0460SAndrew Jeffery 	u32 duration;
31549d4d277SEddie James 	u32 status;
316efa859f7SJoel Stanley 	int ret;
317efa859f7SJoel Stanley 
318eda21ee9SGuenter Roeck 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
319efa859f7SJoel Stanley 	if (!wdt)
320efa859f7SJoel Stanley 		return -ENOMEM;
321efa859f7SJoel Stanley 
3229ec0b7e0SEddie James 	np = dev->of_node;
3239ec0b7e0SEddie James 
3249ec0b7e0SEddie James 	ofdid = of_match_node(aspeed_wdt_of_table, np);
3259ec0b7e0SEddie James 	if (!ofdid)
3269ec0b7e0SEddie James 		return -EINVAL;
3279ec0b7e0SEddie James 	wdt->cfg = ofdid->data;
3289ec0b7e0SEddie James 
3290f0a6a28SGuenter Roeck 	wdt->base = devm_platform_ioremap_resource(pdev, 0);
330efa859f7SJoel Stanley 	if (IS_ERR(wdt->base))
331efa859f7SJoel Stanley 		return PTR_ERR(wdt->base);
332efa859f7SJoel Stanley 
333efa859f7SJoel Stanley 	wdt->wdd.info = &aspeed_wdt_info;
3349ec0b7e0SEddie James 
3359ec0b7e0SEddie James 	if (wdt->cfg->irq_mask) {
3369ec0b7e0SEddie James 		int irq = platform_get_irq_optional(pdev, 0);
3379ec0b7e0SEddie James 
3389ec0b7e0SEddie James 		if (irq > 0) {
3399ec0b7e0SEddie James 			ret = devm_request_irq(dev, irq, aspeed_wdt_irq,
3409ec0b7e0SEddie James 					       IRQF_SHARED, dev_name(dev),
3419ec0b7e0SEddie James 					       wdt);
3429ec0b7e0SEddie James 			if (ret)
3439ec0b7e0SEddie James 				return ret;
3449ec0b7e0SEddie James 
3459ec0b7e0SEddie James 			wdt->wdd.info = &aspeed_wdt_pretimeout_info;
3469ec0b7e0SEddie James 		}
3479ec0b7e0SEddie James 	}
3489ec0b7e0SEddie James 
349efa859f7SJoel Stanley 	wdt->wdd.ops = &aspeed_wdt_ops;
350efa859f7SJoel Stanley 	wdt->wdd.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT_MS;
351eda21ee9SGuenter Roeck 	wdt->wdd.parent = dev;
352efa859f7SJoel Stanley 
353efa859f7SJoel Stanley 	wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
354eda21ee9SGuenter Roeck 	watchdog_init_timeout(&wdt->wdd, 0, dev);
355efa859f7SJoel Stanley 
3564ed1a6b6SEduardo Valentin 	watchdog_set_nowayout(&wdt->wdd, nowayout);
3574ed1a6b6SEduardo Valentin 
358c0457125SJoel Stanley 	/*
359c0457125SJoel Stanley 	 * On clock rates:
360c0457125SJoel Stanley 	 *  - ast2400 wdt can run at PCLK, or 1MHz
361c0457125SJoel Stanley 	 *  - ast2500 only runs at 1MHz, hard coding bit 4 to 1
362c0457125SJoel Stanley 	 *  - ast2600 always runs at 1MHz
363c0457125SJoel Stanley 	 *
364c0457125SJoel Stanley 	 * Set the ast2400 to run at 1MHz as it simplifies the driver.
365c0457125SJoel Stanley 	 */
366c0457125SJoel Stanley 	if (of_device_is_compatible(np, "aspeed,ast2400-wdt"))
367b7f0b8adSChristopher Bostic 		wdt->ctrl = WDT_CTRL_1MHZ_CLK;
368b7f0b8adSChristopher Bostic 
369efa859f7SJoel Stanley 	/*
370efa859f7SJoel Stanley 	 * Control reset on a per-device basis to ensure the
371b7f0b8adSChristopher Bostic 	 * host is not affected by a BMC reboot
372efa859f7SJoel Stanley 	 */
373b7f0b8adSChristopher Bostic 	ret = of_property_read_string(np, "aspeed,reset-type", &reset_type);
374b7f0b8adSChristopher Bostic 	if (ret) {
375b7f0b8adSChristopher Bostic 		wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC | WDT_CTRL_RESET_SYSTEM;
376b7f0b8adSChristopher Bostic 	} else {
377b7f0b8adSChristopher Bostic 		if (!strcmp(reset_type, "cpu"))
378d2fc8db6SMilton Miller 			wdt->ctrl |= WDT_CTRL_RESET_MODE_ARM_CPU |
379d2fc8db6SMilton Miller 				     WDT_CTRL_RESET_SYSTEM;
380b7f0b8adSChristopher Bostic 		else if (!strcmp(reset_type, "soc"))
381d2fc8db6SMilton Miller 			wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC |
382d2fc8db6SMilton Miller 				     WDT_CTRL_RESET_SYSTEM;
383b7f0b8adSChristopher Bostic 		else if (!strcmp(reset_type, "system"))
384d2fc8db6SMilton Miller 			wdt->ctrl |= WDT_CTRL_RESET_MODE_FULL_CHIP |
385d2fc8db6SMilton Miller 				     WDT_CTRL_RESET_SYSTEM;
386b7f0b8adSChristopher Bostic 		else if (strcmp(reset_type, "none"))
387b7f0b8adSChristopher Bostic 			return -EINVAL;
388b7f0b8adSChristopher Bostic 	}
389b7f0b8adSChristopher Bostic 	if (of_property_read_bool(np, "aspeed,external-signal"))
390b7f0b8adSChristopher Bostic 		wdt->ctrl |= WDT_CTRL_WDT_EXT;
3916ffa3402SMilton Miller 	if (of_property_read_bool(np, "aspeed,alt-boot"))
3926ffa3402SMilton Miller 		wdt->ctrl |= WDT_CTRL_BOOT_SECONDARY;
393b7f0b8adSChristopher Bostic 
394efa859f7SJoel Stanley 	if (readl(wdt->base + WDT_CTRL) & WDT_CTRL_ENABLE)  {
3959f3e13c7SAndrew Jeffery 		/*
3969f3e13c7SAndrew Jeffery 		 * The watchdog is running, but invoke aspeed_wdt_start() to
3979f3e13c7SAndrew Jeffery 		 * write wdt->ctrl to WDT_CTRL to ensure the watchdog's
3989f3e13c7SAndrew Jeffery 		 * configuration conforms to the driver's expectations.
3999f3e13c7SAndrew Jeffery 		 * Primarily, ensure we're using the 1MHz clock source.
4009f3e13c7SAndrew Jeffery 		 */
401efa859f7SJoel Stanley 		aspeed_wdt_start(&wdt->wdd);
402efa859f7SJoel Stanley 		set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
403efa859f7SJoel Stanley 	}
404efa859f7SJoel Stanley 
405b3528b48SRyan Chen 	if ((of_device_is_compatible(np, "aspeed,ast2500-wdt")) ||
406b3528b48SRyan Chen 		(of_device_is_compatible(np, "aspeed,ast2600-wdt"))) {
407*f8143880SZev Weiss 		u32 reset_mask[2];
408*f8143880SZev Weiss 		size_t nrstmask = of_device_is_compatible(np, "aspeed,ast2600-wdt") ? 2 : 1;
409012c0460SAndrew Jeffery 		u32 reg = readl(wdt->base + WDT_RESET_WIDTH);
410012c0460SAndrew Jeffery 
4119ec0b7e0SEddie James 		reg &= wdt->cfg->ext_pulse_width_mask;
412012c0460SAndrew Jeffery 		if (of_property_read_bool(np, "aspeed,ext-active-high"))
413012c0460SAndrew Jeffery 			reg |= WDT_ACTIVE_HIGH_MAGIC;
414012c0460SAndrew Jeffery 		else
415012c0460SAndrew Jeffery 			reg |= WDT_ACTIVE_LOW_MAGIC;
416012c0460SAndrew Jeffery 
417012c0460SAndrew Jeffery 		writel(reg, wdt->base + WDT_RESET_WIDTH);
41819f04459SChin-Ting Kuo 
4199ec0b7e0SEddie James 		reg &= wdt->cfg->ext_pulse_width_mask;
42019f04459SChin-Ting Kuo 		if (of_property_read_bool(np, "aspeed,ext-push-pull"))
42119f04459SChin-Ting Kuo 			reg |= WDT_PUSH_PULL_MAGIC;
42219f04459SChin-Ting Kuo 		else
42319f04459SChin-Ting Kuo 			reg |= WDT_OPEN_DRAIN_MAGIC;
42419f04459SChin-Ting Kuo 
42519f04459SChin-Ting Kuo 		writel(reg, wdt->base + WDT_RESET_WIDTH);
426*f8143880SZev Weiss 
427*f8143880SZev Weiss 		ret = of_property_read_u32_array(np, "aspeed,reset-mask", reset_mask, nrstmask);
428*f8143880SZev Weiss 		if (!ret) {
429*f8143880SZev Weiss 			writel(reset_mask[0], wdt->base + WDT_RESET_MASK1);
430*f8143880SZev Weiss 			if (nrstmask > 1)
431*f8143880SZev Weiss 				writel(reset_mask[1], wdt->base + WDT_RESET_MASK2);
432*f8143880SZev Weiss 		}
433012c0460SAndrew Jeffery 	}
434012c0460SAndrew Jeffery 
435012c0460SAndrew Jeffery 	if (!of_property_read_u32(np, "aspeed,ext-pulse-duration", &duration)) {
4369ec0b7e0SEddie James 		u32 max_duration = wdt->cfg->ext_pulse_width_mask + 1;
437012c0460SAndrew Jeffery 
438012c0460SAndrew Jeffery 		if (duration == 0 || duration > max_duration) {
439eda21ee9SGuenter Roeck 			dev_err(dev, "Invalid pulse duration: %uus\n",
440012c0460SAndrew Jeffery 				duration);
441012c0460SAndrew Jeffery 			duration = max(1U, min(max_duration, duration));
442eda21ee9SGuenter Roeck 			dev_info(dev, "Pulse duration set to %uus\n",
443012c0460SAndrew Jeffery 				 duration);
444012c0460SAndrew Jeffery 		}
445012c0460SAndrew Jeffery 
446012c0460SAndrew Jeffery 		/*
447012c0460SAndrew Jeffery 		 * The watchdog is always configured with a 1MHz source, so
448012c0460SAndrew Jeffery 		 * there is no need to scale the microsecond value. However we
449012c0460SAndrew Jeffery 		 * need to offset it - from the datasheet:
450012c0460SAndrew Jeffery 		 *
451012c0460SAndrew Jeffery 		 * "This register decides the asserting duration of wdt_ext and
452012c0460SAndrew Jeffery 		 * wdt_rstarm signal. The default value is 0xFF. It means the
453012c0460SAndrew Jeffery 		 * default asserting duration of wdt_ext and wdt_rstarm is
454012c0460SAndrew Jeffery 		 * 256us."
455012c0460SAndrew Jeffery 		 *
456012c0460SAndrew Jeffery 		 * This implies a value of 0 gives a 1us pulse.
457012c0460SAndrew Jeffery 		 */
458012c0460SAndrew Jeffery 		writel(duration - 1, wdt->base + WDT_RESET_WIDTH);
459012c0460SAndrew Jeffery 	}
460012c0460SAndrew Jeffery 
46149d4d277SEddie James 	status = readl(wdt->base + WDT_TIMEOUT_STATUS);
4623d9e89bdSIvan Mikhaylov 	if (status & WDT_TIMEOUT_STATUS_BOOT_SECONDARY) {
46349d4d277SEddie James 		wdt->wdd.bootstatus = WDIOF_CARDRESET;
46449d4d277SEddie James 
4653d9e89bdSIvan Mikhaylov 		if (of_device_is_compatible(np, "aspeed,ast2400-wdt") ||
4663d9e89bdSIvan Mikhaylov 		    of_device_is_compatible(np, "aspeed,ast2500-wdt"))
4673d9e89bdSIvan Mikhaylov 			wdt->wdd.groups = bswitch_groups;
4683d9e89bdSIvan Mikhaylov 	}
4693d9e89bdSIvan Mikhaylov 
4703d9e89bdSIvan Mikhaylov 	dev_set_drvdata(dev, wdt);
4713d9e89bdSIvan Mikhaylov 
4724ab05433SWolfram Sang 	return devm_watchdog_register_device(dev, &wdt->wdd);
473efa859f7SJoel Stanley }
474efa859f7SJoel Stanley 
475efa859f7SJoel Stanley static struct platform_driver aspeed_watchdog_driver = {
476efa859f7SJoel Stanley 	.probe = aspeed_wdt_probe,
477efa859f7SJoel Stanley 	.driver = {
478efa859f7SJoel Stanley 		.name = KBUILD_MODNAME,
479bfeaadbcSKrzysztof Kozlowski 		.of_match_table = aspeed_wdt_of_table,
480efa859f7SJoel Stanley 	},
481efa859f7SJoel Stanley };
482d4238aa4SAndrew Jeffery 
aspeed_wdt_init(void)483d4238aa4SAndrew Jeffery static int __init aspeed_wdt_init(void)
484d4238aa4SAndrew Jeffery {
485d4238aa4SAndrew Jeffery 	return platform_driver_register(&aspeed_watchdog_driver);
486d4238aa4SAndrew Jeffery }
487d4238aa4SAndrew Jeffery arch_initcall(aspeed_wdt_init);
488d4238aa4SAndrew Jeffery 
aspeed_wdt_exit(void)489d4238aa4SAndrew Jeffery static void __exit aspeed_wdt_exit(void)
490d4238aa4SAndrew Jeffery {
491d4238aa4SAndrew Jeffery 	platform_driver_unregister(&aspeed_watchdog_driver);
492d4238aa4SAndrew Jeffery }
493d4238aa4SAndrew Jeffery module_exit(aspeed_wdt_exit);
494efa859f7SJoel Stanley 
495efa859f7SJoel Stanley MODULE_DESCRIPTION("Aspeed Watchdog Driver");
496efa859f7SJoel Stanley MODULE_LICENSE("GPL");
497