blob: c707ab64792245c1be04b55d7a5eaaa0bb7e8c53 [file] [log] [blame]
Joel Stanleyefa859f2016-05-18 17:51:00 +09301/*
2 * Copyright 2016 IBM Corporation
3 *
4 * Joel Stanley <joel@jms.id.au>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/delay.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/platform_device.h>
18#include <linux/watchdog.h>
19
20struct aspeed_wdt {
21 struct watchdog_device wdd;
22 void __iomem *base;
23 u32 ctrl;
24};
25
26static const struct of_device_id aspeed_wdt_of_table[] = {
27 { .compatible = "aspeed,ast2400-wdt" },
28 { .compatible = "aspeed,ast2500-wdt" },
29 { },
30};
31MODULE_DEVICE_TABLE(of, aspeed_wdt_of_table);
32
33#define WDT_STATUS 0x00
34#define WDT_RELOAD_VALUE 0x04
35#define WDT_RESTART 0x08
36#define WDT_CTRL 0x0C
37#define WDT_CTRL_RESET_MODE_SOC (0x00 << 5)
38#define WDT_CTRL_RESET_MODE_FULL_CHIP (0x01 << 5)
Christopher Bosticb7f0b8a2017-07-17 14:25:39 -050039#define WDT_CTRL_RESET_MODE_ARM_CPU (0x10 << 5)
Joel Stanleyefa859f2016-05-18 17:51:00 +093040#define WDT_CTRL_1MHZ_CLK BIT(4)
41#define WDT_CTRL_WDT_EXT BIT(3)
42#define WDT_CTRL_WDT_INTR BIT(2)
43#define WDT_CTRL_RESET_SYSTEM BIT(1)
44#define WDT_CTRL_ENABLE BIT(0)
45
46#define WDT_RESTART_MAGIC 0x4755
47
48/* 32 bits at 1MHz, in milliseconds */
49#define WDT_MAX_TIMEOUT_MS 4294967
50#define WDT_DEFAULT_TIMEOUT 30
51#define WDT_RATE_1MHZ 1000000
52
53static struct aspeed_wdt *to_aspeed_wdt(struct watchdog_device *wdd)
54{
55 return container_of(wdd, struct aspeed_wdt, wdd);
56}
57
58static void aspeed_wdt_enable(struct aspeed_wdt *wdt, int count)
59{
60 wdt->ctrl |= WDT_CTRL_ENABLE;
61
62 writel(0, wdt->base + WDT_CTRL);
63 writel(count, wdt->base + WDT_RELOAD_VALUE);
64 writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
65 writel(wdt->ctrl, wdt->base + WDT_CTRL);
66}
67
68static int aspeed_wdt_start(struct watchdog_device *wdd)
69{
70 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
71
72 aspeed_wdt_enable(wdt, wdd->timeout * WDT_RATE_1MHZ);
73
74 return 0;
75}
76
77static int aspeed_wdt_stop(struct watchdog_device *wdd)
78{
79 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
80
81 wdt->ctrl &= ~WDT_CTRL_ENABLE;
82 writel(wdt->ctrl, wdt->base + WDT_CTRL);
83
84 return 0;
85}
86
87static int aspeed_wdt_ping(struct watchdog_device *wdd)
88{
89 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
90
91 writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
92
93 return 0;
94}
95
96static int aspeed_wdt_set_timeout(struct watchdog_device *wdd,
97 unsigned int timeout)
98{
99 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
100 u32 actual;
101
102 wdd->timeout = timeout;
103
104 actual = min(timeout, wdd->max_hw_heartbeat_ms * 1000);
105
106 writel(actual * WDT_RATE_1MHZ, wdt->base + WDT_RELOAD_VALUE);
107 writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
108
109 return 0;
110}
111
112static int aspeed_wdt_restart(struct watchdog_device *wdd,
113 unsigned long action, void *data)
114{
115 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
116
117 aspeed_wdt_enable(wdt, 128 * WDT_RATE_1MHZ / 1000);
118
119 mdelay(1000);
120
121 return 0;
122}
123
124static const struct watchdog_ops aspeed_wdt_ops = {
125 .start = aspeed_wdt_start,
126 .stop = aspeed_wdt_stop,
127 .ping = aspeed_wdt_ping,
128 .set_timeout = aspeed_wdt_set_timeout,
129 .restart = aspeed_wdt_restart,
130 .owner = THIS_MODULE,
131};
132
133static const struct watchdog_info aspeed_wdt_info = {
134 .options = WDIOF_KEEPALIVEPING
135 | WDIOF_MAGICCLOSE
136 | WDIOF_SETTIMEOUT,
137 .identity = KBUILD_MODNAME,
138};
139
Joel Stanleyefa859f2016-05-18 17:51:00 +0930140static int aspeed_wdt_probe(struct platform_device *pdev)
141{
142 struct aspeed_wdt *wdt;
143 struct resource *res;
Christopher Bosticb7f0b8a2017-07-17 14:25:39 -0500144 struct device_node *np;
145 const char *reset_type;
Joel Stanleyefa859f2016-05-18 17:51:00 +0930146 int ret;
147
148 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
149 if (!wdt)
150 return -ENOMEM;
151
152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
153 wdt->base = devm_ioremap_resource(&pdev->dev, res);
154 if (IS_ERR(wdt->base))
155 return PTR_ERR(wdt->base);
156
157 /*
158 * The ast2400 wdt can run at PCLK, or 1MHz. The ast2500 only
159 * runs at 1MHz. We chose to always run at 1MHz, as there's no
160 * good reason to have a faster watchdog counter.
161 */
162 wdt->wdd.info = &aspeed_wdt_info;
163 wdt->wdd.ops = &aspeed_wdt_ops;
164 wdt->wdd.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT_MS;
165 wdt->wdd.parent = &pdev->dev;
166
167 wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
168 watchdog_init_timeout(&wdt->wdd, 0, &pdev->dev);
169
Christopher Bosticb7f0b8a2017-07-17 14:25:39 -0500170 wdt->ctrl = WDT_CTRL_1MHZ_CLK;
171
Joel Stanleyefa859f2016-05-18 17:51:00 +0930172 /*
173 * Control reset on a per-device basis to ensure the
Christopher Bosticb7f0b8a2017-07-17 14:25:39 -0500174 * host is not affected by a BMC reboot
Joel Stanleyefa859f2016-05-18 17:51:00 +0930175 */
Christopher Bosticb7f0b8a2017-07-17 14:25:39 -0500176 np = pdev->dev.of_node;
177 ret = of_property_read_string(np, "aspeed,reset-type", &reset_type);
178 if (ret) {
179 wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC | WDT_CTRL_RESET_SYSTEM;
180 } else {
181 if (!strcmp(reset_type, "cpu"))
182 wdt->ctrl |= WDT_CTRL_RESET_MODE_ARM_CPU;
183 else if (!strcmp(reset_type, "soc"))
184 wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC;
185 else if (!strcmp(reset_type, "system"))
186 wdt->ctrl |= WDT_CTRL_RESET_SYSTEM;
187 else if (strcmp(reset_type, "none"))
188 return -EINVAL;
189 }
190 if (of_property_read_bool(np, "aspeed,external-signal"))
191 wdt->ctrl |= WDT_CTRL_WDT_EXT;
192
193 writel(wdt->ctrl, wdt->base + WDT_CTRL);
Joel Stanleyefa859f2016-05-18 17:51:00 +0930194
195 if (readl(wdt->base + WDT_CTRL) & WDT_CTRL_ENABLE) {
196 aspeed_wdt_start(&wdt->wdd);
197 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
198 }
199
Guenter Roeck7db16342017-01-10 15:21:44 -0800200 ret = devm_watchdog_register_device(&pdev->dev, &wdt->wdd);
Joel Stanleyefa859f2016-05-18 17:51:00 +0930201 if (ret) {
202 dev_err(&pdev->dev, "failed to register\n");
203 return ret;
204 }
205
Joel Stanleyefa859f2016-05-18 17:51:00 +0930206 return 0;
207}
208
209static struct platform_driver aspeed_watchdog_driver = {
210 .probe = aspeed_wdt_probe,
Joel Stanleyefa859f2016-05-18 17:51:00 +0930211 .driver = {
212 .name = KBUILD_MODNAME,
213 .of_match_table = of_match_ptr(aspeed_wdt_of_table),
214 },
215};
216module_platform_driver(aspeed_watchdog_driver);
217
218MODULE_DESCRIPTION("Aspeed Watchdog Driver");
219MODULE_LICENSE("GPL");