12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b0d8a082SPurna Chandra Mandal /*
3b0d8a082SPurna Chandra Mandal * PIC32 deadman timer driver
4b0d8a082SPurna Chandra Mandal *
5b0d8a082SPurna Chandra Mandal * Purna Chandra Mandal <purna.mandal@microchip.com>
6b0d8a082SPurna Chandra Mandal * Copyright (c) 2016, Microchip Technology Inc.
7b0d8a082SPurna Chandra Mandal */
8b0d8a082SPurna Chandra Mandal #include <linux/clk.h>
9b0d8a082SPurna Chandra Mandal #include <linux/device.h>
10b0d8a082SPurna Chandra Mandal #include <linux/err.h>
11b0d8a082SPurna Chandra Mandal #include <linux/io.h>
12b0d8a082SPurna Chandra Mandal #include <linux/kernel.h>
13b0d8a082SPurna Chandra Mandal #include <linux/module.h>
14b0d8a082SPurna Chandra Mandal #include <linux/of.h>
15b0d8a082SPurna Chandra Mandal #include <linux/platform_device.h>
16b0d8a082SPurna Chandra Mandal #include <linux/pm.h>
17b0d8a082SPurna Chandra Mandal #include <linux/watchdog.h>
18b0d8a082SPurna Chandra Mandal
19b0d8a082SPurna Chandra Mandal #include <asm/mach-pic32/pic32.h>
20b0d8a082SPurna Chandra Mandal
21b0d8a082SPurna Chandra Mandal /* Deadman Timer Regs */
22b0d8a082SPurna Chandra Mandal #define DMTCON_REG 0x00
23b0d8a082SPurna Chandra Mandal #define DMTPRECLR_REG 0x10
24b0d8a082SPurna Chandra Mandal #define DMTCLR_REG 0x20
25b0d8a082SPurna Chandra Mandal #define DMTSTAT_REG 0x30
26b0d8a082SPurna Chandra Mandal #define DMTCNT_REG 0x40
27b0d8a082SPurna Chandra Mandal #define DMTPSCNT_REG 0x60
28b0d8a082SPurna Chandra Mandal #define DMTPSINTV_REG 0x70
29b0d8a082SPurna Chandra Mandal
30b0d8a082SPurna Chandra Mandal /* Deadman Timer Regs fields */
31b0d8a082SPurna Chandra Mandal #define DMT_ON BIT(15)
32b0d8a082SPurna Chandra Mandal #define DMT_STEP1_KEY BIT(6)
33b0d8a082SPurna Chandra Mandal #define DMT_STEP2_KEY BIT(3)
34b0d8a082SPurna Chandra Mandal #define DMTSTAT_WINOPN BIT(0)
35b0d8a082SPurna Chandra Mandal #define DMTSTAT_EVENT BIT(5)
36b0d8a082SPurna Chandra Mandal #define DMTSTAT_BAD2 BIT(6)
37b0d8a082SPurna Chandra Mandal #define DMTSTAT_BAD1 BIT(7)
38b0d8a082SPurna Chandra Mandal
39b0d8a082SPurna Chandra Mandal /* Reset Control Register fields for watchdog */
40b0d8a082SPurna Chandra Mandal #define RESETCON_DMT_TIMEOUT BIT(5)
41b0d8a082SPurna Chandra Mandal
42b0d8a082SPurna Chandra Mandal struct pic32_dmt {
43b0d8a082SPurna Chandra Mandal void __iomem *regs;
44b0d8a082SPurna Chandra Mandal struct clk *clk;
45b0d8a082SPurna Chandra Mandal };
46b0d8a082SPurna Chandra Mandal
dmt_enable(struct pic32_dmt * dmt)47b0d8a082SPurna Chandra Mandal static inline void dmt_enable(struct pic32_dmt *dmt)
48b0d8a082SPurna Chandra Mandal {
49b0d8a082SPurna Chandra Mandal writel(DMT_ON, PIC32_SET(dmt->regs + DMTCON_REG));
50b0d8a082SPurna Chandra Mandal }
51b0d8a082SPurna Chandra Mandal
dmt_disable(struct pic32_dmt * dmt)52b0d8a082SPurna Chandra Mandal static inline void dmt_disable(struct pic32_dmt *dmt)
53b0d8a082SPurna Chandra Mandal {
54b0d8a082SPurna Chandra Mandal writel(DMT_ON, PIC32_CLR(dmt->regs + DMTCON_REG));
55b0d8a082SPurna Chandra Mandal /*
56b0d8a082SPurna Chandra Mandal * Cannot touch registers in the CPU cycle following clearing the
57b0d8a082SPurna Chandra Mandal * ON bit.
58b0d8a082SPurna Chandra Mandal */
59b0d8a082SPurna Chandra Mandal nop();
60b0d8a082SPurna Chandra Mandal }
61b0d8a082SPurna Chandra Mandal
dmt_bad_status(struct pic32_dmt * dmt)62b0d8a082SPurna Chandra Mandal static inline int dmt_bad_status(struct pic32_dmt *dmt)
63b0d8a082SPurna Chandra Mandal {
64b0d8a082SPurna Chandra Mandal u32 val;
65b0d8a082SPurna Chandra Mandal
66b0d8a082SPurna Chandra Mandal val = readl(dmt->regs + DMTSTAT_REG);
67b0d8a082SPurna Chandra Mandal val &= (DMTSTAT_BAD1 | DMTSTAT_BAD2 | DMTSTAT_EVENT);
68b0d8a082SPurna Chandra Mandal if (val)
69b0d8a082SPurna Chandra Mandal return -EAGAIN;
70b0d8a082SPurna Chandra Mandal
71b0d8a082SPurna Chandra Mandal return 0;
72b0d8a082SPurna Chandra Mandal }
73b0d8a082SPurna Chandra Mandal
dmt_keepalive(struct pic32_dmt * dmt)74b0d8a082SPurna Chandra Mandal static inline int dmt_keepalive(struct pic32_dmt *dmt)
75b0d8a082SPurna Chandra Mandal {
76b0d8a082SPurna Chandra Mandal u32 v;
77b0d8a082SPurna Chandra Mandal u32 timeout = 500;
78b0d8a082SPurna Chandra Mandal
79b0d8a082SPurna Chandra Mandal /* set pre-clear key */
80b0d8a082SPurna Chandra Mandal writel(DMT_STEP1_KEY << 8, dmt->regs + DMTPRECLR_REG);
81b0d8a082SPurna Chandra Mandal
82b0d8a082SPurna Chandra Mandal /* wait for DMT window to open */
83b0d8a082SPurna Chandra Mandal while (--timeout) {
84b0d8a082SPurna Chandra Mandal v = readl(dmt->regs + DMTSTAT_REG) & DMTSTAT_WINOPN;
85b0d8a082SPurna Chandra Mandal if (v == DMTSTAT_WINOPN)
86b0d8a082SPurna Chandra Mandal break;
87b0d8a082SPurna Chandra Mandal }
88b0d8a082SPurna Chandra Mandal
89b0d8a082SPurna Chandra Mandal /* apply key2 */
90b0d8a082SPurna Chandra Mandal writel(DMT_STEP2_KEY, dmt->regs + DMTCLR_REG);
91b0d8a082SPurna Chandra Mandal
92b0d8a082SPurna Chandra Mandal /* check whether keys are latched correctly */
93b0d8a082SPurna Chandra Mandal return dmt_bad_status(dmt);
94b0d8a082SPurna Chandra Mandal }
95b0d8a082SPurna Chandra Mandal
pic32_dmt_get_timeout_secs(struct pic32_dmt * dmt)96b0d8a082SPurna Chandra Mandal static inline u32 pic32_dmt_get_timeout_secs(struct pic32_dmt *dmt)
97b0d8a082SPurna Chandra Mandal {
98b0d8a082SPurna Chandra Mandal unsigned long rate;
99b0d8a082SPurna Chandra Mandal
100b0d8a082SPurna Chandra Mandal rate = clk_get_rate(dmt->clk);
101b0d8a082SPurna Chandra Mandal if (rate)
102b0d8a082SPurna Chandra Mandal return readl(dmt->regs + DMTPSCNT_REG) / rate;
103b0d8a082SPurna Chandra Mandal
104b0d8a082SPurna Chandra Mandal return 0;
105b0d8a082SPurna Chandra Mandal }
106b0d8a082SPurna Chandra Mandal
pic32_dmt_bootstatus(struct pic32_dmt * dmt)107b0d8a082SPurna Chandra Mandal static inline u32 pic32_dmt_bootstatus(struct pic32_dmt *dmt)
108b0d8a082SPurna Chandra Mandal {
109b0d8a082SPurna Chandra Mandal u32 v;
110b0d8a082SPurna Chandra Mandal void __iomem *rst_base;
111b0d8a082SPurna Chandra Mandal
112b0d8a082SPurna Chandra Mandal rst_base = ioremap(PIC32_BASE_RESET, 0x10);
113b0d8a082SPurna Chandra Mandal if (!rst_base)
114b0d8a082SPurna Chandra Mandal return 0;
115b0d8a082SPurna Chandra Mandal
116b0d8a082SPurna Chandra Mandal v = readl(rst_base);
117b0d8a082SPurna Chandra Mandal
118b0d8a082SPurna Chandra Mandal writel(RESETCON_DMT_TIMEOUT, PIC32_CLR(rst_base));
119b0d8a082SPurna Chandra Mandal
120b0d8a082SPurna Chandra Mandal iounmap(rst_base);
121b0d8a082SPurna Chandra Mandal return v & RESETCON_DMT_TIMEOUT;
122b0d8a082SPurna Chandra Mandal }
123b0d8a082SPurna Chandra Mandal
pic32_dmt_start(struct watchdog_device * wdd)124b0d8a082SPurna Chandra Mandal static int pic32_dmt_start(struct watchdog_device *wdd)
125b0d8a082SPurna Chandra Mandal {
126b0d8a082SPurna Chandra Mandal struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
127b0d8a082SPurna Chandra Mandal
128b0d8a082SPurna Chandra Mandal dmt_enable(dmt);
129b0d8a082SPurna Chandra Mandal return dmt_keepalive(dmt);
130b0d8a082SPurna Chandra Mandal }
131b0d8a082SPurna Chandra Mandal
pic32_dmt_stop(struct watchdog_device * wdd)132b0d8a082SPurna Chandra Mandal static int pic32_dmt_stop(struct watchdog_device *wdd)
133b0d8a082SPurna Chandra Mandal {
134b0d8a082SPurna Chandra Mandal struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
135b0d8a082SPurna Chandra Mandal
136b0d8a082SPurna Chandra Mandal dmt_disable(dmt);
137b0d8a082SPurna Chandra Mandal
138b0d8a082SPurna Chandra Mandal return 0;
139b0d8a082SPurna Chandra Mandal }
140b0d8a082SPurna Chandra Mandal
pic32_dmt_ping(struct watchdog_device * wdd)141b0d8a082SPurna Chandra Mandal static int pic32_dmt_ping(struct watchdog_device *wdd)
142b0d8a082SPurna Chandra Mandal {
143b0d8a082SPurna Chandra Mandal struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
144b0d8a082SPurna Chandra Mandal
145b0d8a082SPurna Chandra Mandal return dmt_keepalive(dmt);
146b0d8a082SPurna Chandra Mandal }
147b0d8a082SPurna Chandra Mandal
148b0d8a082SPurna Chandra Mandal static const struct watchdog_ops pic32_dmt_fops = {
149b0d8a082SPurna Chandra Mandal .owner = THIS_MODULE,
150b0d8a082SPurna Chandra Mandal .start = pic32_dmt_start,
151b0d8a082SPurna Chandra Mandal .stop = pic32_dmt_stop,
152b0d8a082SPurna Chandra Mandal .ping = pic32_dmt_ping,
153b0d8a082SPurna Chandra Mandal };
154b0d8a082SPurna Chandra Mandal
155b0d8a082SPurna Chandra Mandal static const struct watchdog_info pic32_dmt_ident = {
156b0d8a082SPurna Chandra Mandal .options = WDIOF_KEEPALIVEPING |
157b0d8a082SPurna Chandra Mandal WDIOF_MAGICCLOSE,
158b0d8a082SPurna Chandra Mandal .identity = "PIC32 Deadman Timer",
159b0d8a082SPurna Chandra Mandal };
160b0d8a082SPurna Chandra Mandal
161b0d8a082SPurna Chandra Mandal static struct watchdog_device pic32_dmt_wdd = {
162b0d8a082SPurna Chandra Mandal .info = &pic32_dmt_ident,
163b0d8a082SPurna Chandra Mandal .ops = &pic32_dmt_fops,
164b0d8a082SPurna Chandra Mandal };
165b0d8a082SPurna Chandra Mandal
pic32_dmt_probe(struct platform_device * pdev)166b0d8a082SPurna Chandra Mandal static int pic32_dmt_probe(struct platform_device *pdev)
167b0d8a082SPurna Chandra Mandal {
168a02b3d7cSGuenter Roeck struct device *dev = &pdev->dev;
169b0d8a082SPurna Chandra Mandal int ret;
170b0d8a082SPurna Chandra Mandal struct pic32_dmt *dmt;
171b0d8a082SPurna Chandra Mandal struct watchdog_device *wdd = &pic32_dmt_wdd;
172b0d8a082SPurna Chandra Mandal
173a02b3d7cSGuenter Roeck dmt = devm_kzalloc(dev, sizeof(*dmt), GFP_KERNEL);
174fafdbabaSWei Yongjun if (!dmt)
175fafdbabaSWei Yongjun return -ENOMEM;
176b0d8a082SPurna Chandra Mandal
1770f0a6a28SGuenter Roeck dmt->regs = devm_platform_ioremap_resource(pdev, 0);
178b0d8a082SPurna Chandra Mandal if (IS_ERR(dmt->regs))
179b0d8a082SPurna Chandra Mandal return PTR_ERR(dmt->regs);
180b0d8a082SPurna Chandra Mandal
181*e0912ea8SChristophe JAILLET dmt->clk = devm_clk_get_enabled(dev, NULL);
182b0d8a082SPurna Chandra Mandal if (IS_ERR(dmt->clk)) {
183a02b3d7cSGuenter Roeck dev_err(dev, "clk not found\n");
184b0d8a082SPurna Chandra Mandal return PTR_ERR(dmt->clk);
185b0d8a082SPurna Chandra Mandal }
186b0d8a082SPurna Chandra Mandal
187b0d8a082SPurna Chandra Mandal wdd->timeout = pic32_dmt_get_timeout_secs(dmt);
188b0d8a082SPurna Chandra Mandal if (!wdd->timeout) {
189a02b3d7cSGuenter Roeck dev_err(dev, "failed to read watchdog register timeout\n");
190a02b3d7cSGuenter Roeck return -EINVAL;
191b0d8a082SPurna Chandra Mandal }
192b0d8a082SPurna Chandra Mandal
193a02b3d7cSGuenter Roeck dev_info(dev, "timeout %d\n", wdd->timeout);
194b0d8a082SPurna Chandra Mandal
195b0d8a082SPurna Chandra Mandal wdd->bootstatus = pic32_dmt_bootstatus(dmt) ? WDIOF_CARDRESET : 0;
196b0d8a082SPurna Chandra Mandal
197b0d8a082SPurna Chandra Mandal watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
198b0d8a082SPurna Chandra Mandal watchdog_set_drvdata(wdd, dmt);
199b0d8a082SPurna Chandra Mandal
200a02b3d7cSGuenter Roeck ret = devm_watchdog_register_device(dev, wdd);
201888ca35dSWolfram Sang if (ret)
202b0d8a082SPurna Chandra Mandal return ret;
203b0d8a082SPurna Chandra Mandal
204a02b3d7cSGuenter Roeck platform_set_drvdata(pdev, wdd);
205b0d8a082SPurna Chandra Mandal return 0;
206b0d8a082SPurna Chandra Mandal }
207b0d8a082SPurna Chandra Mandal
208b0d8a082SPurna Chandra Mandal static const struct of_device_id pic32_dmt_of_ids[] = {
209b0d8a082SPurna Chandra Mandal { .compatible = "microchip,pic32mzda-dmt",},
210b0d8a082SPurna Chandra Mandal { /* sentinel */ }
211b0d8a082SPurna Chandra Mandal };
212b0d8a082SPurna Chandra Mandal MODULE_DEVICE_TABLE(of, pic32_dmt_of_ids);
213b0d8a082SPurna Chandra Mandal
214b0d8a082SPurna Chandra Mandal static struct platform_driver pic32_dmt_driver = {
215b0d8a082SPurna Chandra Mandal .probe = pic32_dmt_probe,
216b0d8a082SPurna Chandra Mandal .driver = {
217b0d8a082SPurna Chandra Mandal .name = "pic32-dmt",
218b0d8a082SPurna Chandra Mandal .of_match_table = of_match_ptr(pic32_dmt_of_ids),
219b0d8a082SPurna Chandra Mandal }
220b0d8a082SPurna Chandra Mandal };
221b0d8a082SPurna Chandra Mandal
222b0d8a082SPurna Chandra Mandal module_platform_driver(pic32_dmt_driver);
223b0d8a082SPurna Chandra Mandal
224b0d8a082SPurna Chandra Mandal MODULE_AUTHOR("Purna Chandra Mandal <purna.mandal@microchip.com>");
225b0d8a082SPurna Chandra Mandal MODULE_DESCRIPTION("Microchip PIC32 DMT Driver");
226b0d8a082SPurna Chandra Mandal MODULE_LICENSE("GPL");
227