1e9800b79SDaniel Palmer // SPDX-License-Identifier: GPL-2.0
2e9800b79SDaniel Palmer /*
3e9800b79SDaniel Palmer * MStar WDT driver
4e9800b79SDaniel Palmer *
5e9800b79SDaniel Palmer * Copyright (C) 2019 - 2021 Daniel Palmer
6e9800b79SDaniel Palmer * Copyright (C) 2021 Romain Perier
7e9800b79SDaniel Palmer *
8e9800b79SDaniel Palmer */
9e9800b79SDaniel Palmer
10e9800b79SDaniel Palmer #include <linux/clk.h>
11e9800b79SDaniel Palmer #include <linux/io.h>
12e9800b79SDaniel Palmer #include <linux/mod_devicetable.h>
13e9800b79SDaniel Palmer #include <linux/module.h>
14e9800b79SDaniel Palmer #include <linux/platform_device.h>
15e9800b79SDaniel Palmer #include <linux/watchdog.h>
16e9800b79SDaniel Palmer
17e9800b79SDaniel Palmer #define REG_WDT_CLR 0x0
18e9800b79SDaniel Palmer #define REG_WDT_MAX_PRD_L 0x10
19e9800b79SDaniel Palmer #define REG_WDT_MAX_PRD_H 0x14
20e9800b79SDaniel Palmer
21e9800b79SDaniel Palmer #define MSC313E_WDT_MIN_TIMEOUT 1
22e9800b79SDaniel Palmer #define MSC313E_WDT_DEFAULT_TIMEOUT 30
23e9800b79SDaniel Palmer
24e9800b79SDaniel Palmer static unsigned int timeout;
25e9800b79SDaniel Palmer
26e9800b79SDaniel Palmer module_param(timeout, int, 0);
27e9800b79SDaniel Palmer MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
28e9800b79SDaniel Palmer
29e9800b79SDaniel Palmer struct msc313e_wdt_priv {
30e9800b79SDaniel Palmer void __iomem *base;
31e9800b79SDaniel Palmer struct watchdog_device wdev;
32e9800b79SDaniel Palmer struct clk *clk;
33e9800b79SDaniel Palmer };
34e9800b79SDaniel Palmer
msc313e_wdt_start(struct watchdog_device * wdev)35e9800b79SDaniel Palmer static int msc313e_wdt_start(struct watchdog_device *wdev)
36e9800b79SDaniel Palmer {
37e9800b79SDaniel Palmer struct msc313e_wdt_priv *priv = watchdog_get_drvdata(wdev);
38e9800b79SDaniel Palmer u32 timeout;
39e9800b79SDaniel Palmer int err;
40e9800b79SDaniel Palmer
41e9800b79SDaniel Palmer err = clk_prepare_enable(priv->clk);
42e9800b79SDaniel Palmer if (err)
43e9800b79SDaniel Palmer return err;
44e9800b79SDaniel Palmer
45e9800b79SDaniel Palmer timeout = wdev->timeout * clk_get_rate(priv->clk);
46e9800b79SDaniel Palmer writew(timeout & 0xffff, priv->base + REG_WDT_MAX_PRD_L);
47e9800b79SDaniel Palmer writew((timeout >> 16) & 0xffff, priv->base + REG_WDT_MAX_PRD_H);
48e9800b79SDaniel Palmer writew(1, priv->base + REG_WDT_CLR);
49e9800b79SDaniel Palmer return 0;
50e9800b79SDaniel Palmer }
51e9800b79SDaniel Palmer
msc313e_wdt_ping(struct watchdog_device * wdev)52e9800b79SDaniel Palmer static int msc313e_wdt_ping(struct watchdog_device *wdev)
53e9800b79SDaniel Palmer {
54e9800b79SDaniel Palmer struct msc313e_wdt_priv *priv = watchdog_get_drvdata(wdev);
55e9800b79SDaniel Palmer
56e9800b79SDaniel Palmer writew(1, priv->base + REG_WDT_CLR);
57e9800b79SDaniel Palmer return 0;
58e9800b79SDaniel Palmer }
59e9800b79SDaniel Palmer
msc313e_wdt_stop(struct watchdog_device * wdev)60e9800b79SDaniel Palmer static int msc313e_wdt_stop(struct watchdog_device *wdev)
61e9800b79SDaniel Palmer {
62e9800b79SDaniel Palmer struct msc313e_wdt_priv *priv = watchdog_get_drvdata(wdev);
63e9800b79SDaniel Palmer
64e9800b79SDaniel Palmer writew(0, priv->base + REG_WDT_MAX_PRD_L);
65e9800b79SDaniel Palmer writew(0, priv->base + REG_WDT_MAX_PRD_H);
66e9800b79SDaniel Palmer writew(0, priv->base + REG_WDT_CLR);
67e9800b79SDaniel Palmer clk_disable_unprepare(priv->clk);
68e9800b79SDaniel Palmer return 0;
69e9800b79SDaniel Palmer }
70e9800b79SDaniel Palmer
msc313e_wdt_settimeout(struct watchdog_device * wdev,unsigned int new_time)71e9800b79SDaniel Palmer static int msc313e_wdt_settimeout(struct watchdog_device *wdev, unsigned int new_time)
72e9800b79SDaniel Palmer {
73e9800b79SDaniel Palmer wdev->timeout = new_time;
74e9800b79SDaniel Palmer
75e9800b79SDaniel Palmer return msc313e_wdt_start(wdev);
76e9800b79SDaniel Palmer }
77e9800b79SDaniel Palmer
78e9800b79SDaniel Palmer static const struct watchdog_info msc313e_wdt_ident = {
79e9800b79SDaniel Palmer .identity = "MSC313e watchdog",
80e9800b79SDaniel Palmer .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
81e9800b79SDaniel Palmer };
82e9800b79SDaniel Palmer
83e9800b79SDaniel Palmer static const struct watchdog_ops msc313e_wdt_ops = {
84e9800b79SDaniel Palmer .owner = THIS_MODULE,
85e9800b79SDaniel Palmer .start = msc313e_wdt_start,
86e9800b79SDaniel Palmer .stop = msc313e_wdt_stop,
87e9800b79SDaniel Palmer .ping = msc313e_wdt_ping,
88e9800b79SDaniel Palmer .set_timeout = msc313e_wdt_settimeout,
89e9800b79SDaniel Palmer };
90e9800b79SDaniel Palmer
91e9800b79SDaniel Palmer static const struct of_device_id msc313e_wdt_of_match[] = {
92e9800b79SDaniel Palmer { .compatible = "mstar,msc313e-wdt", },
93e9800b79SDaniel Palmer { /* sentinel */ }
94e9800b79SDaniel Palmer };
95e9800b79SDaniel Palmer MODULE_DEVICE_TABLE(of, msc313e_wdt_of_match);
96e9800b79SDaniel Palmer
msc313e_wdt_probe(struct platform_device * pdev)97e9800b79SDaniel Palmer static int msc313e_wdt_probe(struct platform_device *pdev)
98e9800b79SDaniel Palmer {
99e9800b79SDaniel Palmer struct device *dev = &pdev->dev;
100e9800b79SDaniel Palmer struct msc313e_wdt_priv *priv;
101e9800b79SDaniel Palmer
102e9800b79SDaniel Palmer priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
103e9800b79SDaniel Palmer if (!priv)
104e9800b79SDaniel Palmer return -ENOMEM;
105e9800b79SDaniel Palmer
106e9800b79SDaniel Palmer priv->base = devm_platform_ioremap_resource(pdev, 0);
107e9800b79SDaniel Palmer if (IS_ERR(priv->base))
108e9800b79SDaniel Palmer return PTR_ERR(priv->base);
109e9800b79SDaniel Palmer
110e9800b79SDaniel Palmer priv->clk = devm_clk_get(dev, NULL);
111e9800b79SDaniel Palmer if (IS_ERR(priv->clk)) {
112e9800b79SDaniel Palmer dev_err(dev, "No input clock\n");
113e9800b79SDaniel Palmer return PTR_ERR(priv->clk);
114e9800b79SDaniel Palmer }
115e9800b79SDaniel Palmer
116e9800b79SDaniel Palmer priv->wdev.info = &msc313e_wdt_ident,
117e9800b79SDaniel Palmer priv->wdev.ops = &msc313e_wdt_ops,
118e9800b79SDaniel Palmer priv->wdev.parent = dev;
119e9800b79SDaniel Palmer priv->wdev.min_timeout = MSC313E_WDT_MIN_TIMEOUT;
120e9800b79SDaniel Palmer priv->wdev.max_timeout = U32_MAX / clk_get_rate(priv->clk);
121e9800b79SDaniel Palmer priv->wdev.timeout = MSC313E_WDT_DEFAULT_TIMEOUT;
122e9800b79SDaniel Palmer
123*ffd264bdSDaniel Palmer /* If the period is non-zero the WDT is running */
124*ffd264bdSDaniel Palmer if (readw(priv->base + REG_WDT_MAX_PRD_L) | (readw(priv->base + REG_WDT_MAX_PRD_H) << 16))
125*ffd264bdSDaniel Palmer set_bit(WDOG_HW_RUNNING, &priv->wdev.status);
126*ffd264bdSDaniel Palmer
127e9800b79SDaniel Palmer watchdog_set_drvdata(&priv->wdev, priv);
128e9800b79SDaniel Palmer
129e9800b79SDaniel Palmer watchdog_init_timeout(&priv->wdev, timeout, dev);
130e9800b79SDaniel Palmer watchdog_stop_on_reboot(&priv->wdev);
131e9800b79SDaniel Palmer watchdog_stop_on_unregister(&priv->wdev);
132e9800b79SDaniel Palmer
133e9800b79SDaniel Palmer return devm_watchdog_register_device(dev, &priv->wdev);
134e9800b79SDaniel Palmer }
135e9800b79SDaniel Palmer
msc313e_wdt_suspend(struct device * dev)136e9800b79SDaniel Palmer static int __maybe_unused msc313e_wdt_suspend(struct device *dev)
137e9800b79SDaniel Palmer {
138e9800b79SDaniel Palmer struct msc313e_wdt_priv *priv = dev_get_drvdata(dev);
139e9800b79SDaniel Palmer
140e9800b79SDaniel Palmer if (watchdog_active(&priv->wdev))
141e9800b79SDaniel Palmer msc313e_wdt_stop(&priv->wdev);
142e9800b79SDaniel Palmer
143e9800b79SDaniel Palmer return 0;
144e9800b79SDaniel Palmer }
145e9800b79SDaniel Palmer
msc313e_wdt_resume(struct device * dev)146e9800b79SDaniel Palmer static int __maybe_unused msc313e_wdt_resume(struct device *dev)
147e9800b79SDaniel Palmer {
148e9800b79SDaniel Palmer struct msc313e_wdt_priv *priv = dev_get_drvdata(dev);
149e9800b79SDaniel Palmer
150e9800b79SDaniel Palmer if (watchdog_active(&priv->wdev))
151e9800b79SDaniel Palmer msc313e_wdt_start(&priv->wdev);
152e9800b79SDaniel Palmer
153e9800b79SDaniel Palmer return 0;
154e9800b79SDaniel Palmer }
155e9800b79SDaniel Palmer
156e9800b79SDaniel Palmer static SIMPLE_DEV_PM_OPS(msc313e_wdt_pm_ops, msc313e_wdt_suspend, msc313e_wdt_resume);
157e9800b79SDaniel Palmer
158e9800b79SDaniel Palmer static struct platform_driver msc313e_wdt_driver = {
159e9800b79SDaniel Palmer .driver = {
160e9800b79SDaniel Palmer .name = "msc313e-wdt",
161e9800b79SDaniel Palmer .of_match_table = msc313e_wdt_of_match,
162e9800b79SDaniel Palmer .pm = &msc313e_wdt_pm_ops,
163e9800b79SDaniel Palmer },
164e9800b79SDaniel Palmer .probe = msc313e_wdt_probe,
165e9800b79SDaniel Palmer };
166e9800b79SDaniel Palmer module_platform_driver(msc313e_wdt_driver);
167e9800b79SDaniel Palmer
168e9800b79SDaniel Palmer MODULE_AUTHOR("Daniel Palmer <daniel@thingy.jp>");
169e9800b79SDaniel Palmer MODULE_DESCRIPTION("Watchdog driver for MStar MSC313e");
170e9800b79SDaniel Palmer MODULE_LICENSE("GPL v2");
171