1975b7f0fSJoel Stanley // SPDX-License-Identifier: GPL-2.0
2975b7f0fSJoel Stanley // Copyright (c) 2018 Nuvoton Technology corporation.
3975b7f0fSJoel Stanley // Copyright (c) 2018 IBM Corp.
4975b7f0fSJoel Stanley
5975b7f0fSJoel Stanley #include <linux/bitops.h>
6*af084fdcSJonathan Neuschäfer #include <linux/clk.h>
7975b7f0fSJoel Stanley #include <linux/delay.h>
8975b7f0fSJoel Stanley #include <linux/interrupt.h>
9975b7f0fSJoel Stanley #include <linux/kernel.h>
10975b7f0fSJoel Stanley #include <linux/module.h>
11975b7f0fSJoel Stanley #include <linux/of_irq.h>
12975b7f0fSJoel Stanley #include <linux/platform_device.h>
13975b7f0fSJoel Stanley #include <linux/slab.h>
14975b7f0fSJoel Stanley #include <linux/watchdog.h>
15975b7f0fSJoel Stanley
16975b7f0fSJoel Stanley #define NPCM_WTCR 0x1C
17975b7f0fSJoel Stanley
18975b7f0fSJoel Stanley #define NPCM_WTCLK (BIT(10) | BIT(11)) /* Clock divider */
19975b7f0fSJoel Stanley #define NPCM_WTE BIT(7) /* Enable */
20975b7f0fSJoel Stanley #define NPCM_WTIE BIT(6) /* Enable irq */
21975b7f0fSJoel Stanley #define NPCM_WTIS (BIT(4) | BIT(5)) /* Interval selection */
22975b7f0fSJoel Stanley #define NPCM_WTIF BIT(3) /* Interrupt flag*/
23975b7f0fSJoel Stanley #define NPCM_WTRF BIT(2) /* Reset flag */
24975b7f0fSJoel Stanley #define NPCM_WTRE BIT(1) /* Reset enable */
25975b7f0fSJoel Stanley #define NPCM_WTR BIT(0) /* Reset counter */
26975b7f0fSJoel Stanley
27975b7f0fSJoel Stanley /*
28975b7f0fSJoel Stanley * Watchdog timeouts
29975b7f0fSJoel Stanley *
30975b7f0fSJoel Stanley * 170 msec: WTCLK=01 WTIS=00 VAL= 0x400
31975b7f0fSJoel Stanley * 670 msec: WTCLK=01 WTIS=01 VAL= 0x410
32975b7f0fSJoel Stanley * 1360 msec: WTCLK=10 WTIS=00 VAL= 0x800
33975b7f0fSJoel Stanley * 2700 msec: WTCLK=01 WTIS=10 VAL= 0x420
34975b7f0fSJoel Stanley * 5360 msec: WTCLK=10 WTIS=01 VAL= 0x810
35975b7f0fSJoel Stanley * 10700 msec: WTCLK=01 WTIS=11 VAL= 0x430
36975b7f0fSJoel Stanley * 21600 msec: WTCLK=10 WTIS=10 VAL= 0x820
37975b7f0fSJoel Stanley * 43000 msec: WTCLK=11 WTIS=00 VAL= 0xC00
38975b7f0fSJoel Stanley * 85600 msec: WTCLK=10 WTIS=11 VAL= 0x830
39975b7f0fSJoel Stanley * 172000 msec: WTCLK=11 WTIS=01 VAL= 0xC10
40975b7f0fSJoel Stanley * 687000 msec: WTCLK=11 WTIS=10 VAL= 0xC20
41975b7f0fSJoel Stanley * 2750000 msec: WTCLK=11 WTIS=11 VAL= 0xC30
42975b7f0fSJoel Stanley */
43975b7f0fSJoel Stanley
44975b7f0fSJoel Stanley struct npcm_wdt {
45975b7f0fSJoel Stanley struct watchdog_device wdd;
46975b7f0fSJoel Stanley void __iomem *reg;
47*af084fdcSJonathan Neuschäfer struct clk *clk;
48975b7f0fSJoel Stanley };
49975b7f0fSJoel Stanley
to_npcm_wdt(struct watchdog_device * wdd)50975b7f0fSJoel Stanley static inline struct npcm_wdt *to_npcm_wdt(struct watchdog_device *wdd)
51975b7f0fSJoel Stanley {
52975b7f0fSJoel Stanley return container_of(wdd, struct npcm_wdt, wdd);
53975b7f0fSJoel Stanley }
54975b7f0fSJoel Stanley
npcm_wdt_ping(struct watchdog_device * wdd)55975b7f0fSJoel Stanley static int npcm_wdt_ping(struct watchdog_device *wdd)
56975b7f0fSJoel Stanley {
57975b7f0fSJoel Stanley struct npcm_wdt *wdt = to_npcm_wdt(wdd);
58975b7f0fSJoel Stanley u32 val;
59975b7f0fSJoel Stanley
60975b7f0fSJoel Stanley val = readl(wdt->reg);
61975b7f0fSJoel Stanley writel(val | NPCM_WTR, wdt->reg);
62975b7f0fSJoel Stanley
63975b7f0fSJoel Stanley return 0;
64975b7f0fSJoel Stanley }
65975b7f0fSJoel Stanley
npcm_wdt_start(struct watchdog_device * wdd)66975b7f0fSJoel Stanley static int npcm_wdt_start(struct watchdog_device *wdd)
67975b7f0fSJoel Stanley {
68975b7f0fSJoel Stanley struct npcm_wdt *wdt = to_npcm_wdt(wdd);
69975b7f0fSJoel Stanley u32 val;
70975b7f0fSJoel Stanley
71*af084fdcSJonathan Neuschäfer if (wdt->clk)
72*af084fdcSJonathan Neuschäfer clk_prepare_enable(wdt->clk);
73*af084fdcSJonathan Neuschäfer
74975b7f0fSJoel Stanley if (wdd->timeout < 2)
75975b7f0fSJoel Stanley val = 0x800;
76975b7f0fSJoel Stanley else if (wdd->timeout < 3)
77975b7f0fSJoel Stanley val = 0x420;
78975b7f0fSJoel Stanley else if (wdd->timeout < 6)
79975b7f0fSJoel Stanley val = 0x810;
80975b7f0fSJoel Stanley else if (wdd->timeout < 11)
81975b7f0fSJoel Stanley val = 0x430;
82975b7f0fSJoel Stanley else if (wdd->timeout < 22)
83975b7f0fSJoel Stanley val = 0x820;
84975b7f0fSJoel Stanley else if (wdd->timeout < 44)
85975b7f0fSJoel Stanley val = 0xC00;
86975b7f0fSJoel Stanley else if (wdd->timeout < 87)
87975b7f0fSJoel Stanley val = 0x830;
88975b7f0fSJoel Stanley else if (wdd->timeout < 173)
89975b7f0fSJoel Stanley val = 0xC10;
90975b7f0fSJoel Stanley else if (wdd->timeout < 688)
91975b7f0fSJoel Stanley val = 0xC20;
92975b7f0fSJoel Stanley else
93975b7f0fSJoel Stanley val = 0xC30;
94975b7f0fSJoel Stanley
95975b7f0fSJoel Stanley val |= NPCM_WTRE | NPCM_WTE | NPCM_WTR | NPCM_WTIE;
96975b7f0fSJoel Stanley
97975b7f0fSJoel Stanley writel(val, wdt->reg);
98975b7f0fSJoel Stanley
99975b7f0fSJoel Stanley return 0;
100975b7f0fSJoel Stanley }
101975b7f0fSJoel Stanley
npcm_wdt_stop(struct watchdog_device * wdd)102975b7f0fSJoel Stanley static int npcm_wdt_stop(struct watchdog_device *wdd)
103975b7f0fSJoel Stanley {
104975b7f0fSJoel Stanley struct npcm_wdt *wdt = to_npcm_wdt(wdd);
105975b7f0fSJoel Stanley
106975b7f0fSJoel Stanley writel(0, wdt->reg);
107975b7f0fSJoel Stanley
108*af084fdcSJonathan Neuschäfer if (wdt->clk)
109*af084fdcSJonathan Neuschäfer clk_disable_unprepare(wdt->clk);
110*af084fdcSJonathan Neuschäfer
111975b7f0fSJoel Stanley return 0;
112975b7f0fSJoel Stanley }
113975b7f0fSJoel Stanley
npcm_wdt_set_timeout(struct watchdog_device * wdd,unsigned int timeout)114975b7f0fSJoel Stanley static int npcm_wdt_set_timeout(struct watchdog_device *wdd,
115975b7f0fSJoel Stanley unsigned int timeout)
116975b7f0fSJoel Stanley {
117975b7f0fSJoel Stanley if (timeout < 2)
118975b7f0fSJoel Stanley wdd->timeout = 1;
119975b7f0fSJoel Stanley else if (timeout < 3)
120975b7f0fSJoel Stanley wdd->timeout = 2;
121975b7f0fSJoel Stanley else if (timeout < 6)
122975b7f0fSJoel Stanley wdd->timeout = 5;
123975b7f0fSJoel Stanley else if (timeout < 11)
124975b7f0fSJoel Stanley wdd->timeout = 10;
125975b7f0fSJoel Stanley else if (timeout < 22)
126975b7f0fSJoel Stanley wdd->timeout = 21;
127975b7f0fSJoel Stanley else if (timeout < 44)
128975b7f0fSJoel Stanley wdd->timeout = 43;
129975b7f0fSJoel Stanley else if (timeout < 87)
130975b7f0fSJoel Stanley wdd->timeout = 86;
131975b7f0fSJoel Stanley else if (timeout < 173)
132975b7f0fSJoel Stanley wdd->timeout = 172;
133975b7f0fSJoel Stanley else if (timeout < 688)
134975b7f0fSJoel Stanley wdd->timeout = 687;
135975b7f0fSJoel Stanley else
136975b7f0fSJoel Stanley wdd->timeout = 2750;
137975b7f0fSJoel Stanley
138975b7f0fSJoel Stanley if (watchdog_active(wdd))
139975b7f0fSJoel Stanley npcm_wdt_start(wdd);
140975b7f0fSJoel Stanley
141975b7f0fSJoel Stanley return 0;
142975b7f0fSJoel Stanley }
143975b7f0fSJoel Stanley
npcm_wdt_interrupt(int irq,void * data)144975b7f0fSJoel Stanley static irqreturn_t npcm_wdt_interrupt(int irq, void *data)
145975b7f0fSJoel Stanley {
146975b7f0fSJoel Stanley struct npcm_wdt *wdt = data;
147975b7f0fSJoel Stanley
148975b7f0fSJoel Stanley watchdog_notify_pretimeout(&wdt->wdd);
149975b7f0fSJoel Stanley
150975b7f0fSJoel Stanley return IRQ_HANDLED;
151975b7f0fSJoel Stanley }
152975b7f0fSJoel Stanley
npcm_wdt_restart(struct watchdog_device * wdd,unsigned long action,void * data)153975b7f0fSJoel Stanley static int npcm_wdt_restart(struct watchdog_device *wdd,
154975b7f0fSJoel Stanley unsigned long action, void *data)
155975b7f0fSJoel Stanley {
156975b7f0fSJoel Stanley struct npcm_wdt *wdt = to_npcm_wdt(wdd);
157975b7f0fSJoel Stanley
158*af084fdcSJonathan Neuschäfer /* For reset, we start the WDT clock and leave it running. */
159*af084fdcSJonathan Neuschäfer if (wdt->clk)
160*af084fdcSJonathan Neuschäfer clk_prepare_enable(wdt->clk);
161*af084fdcSJonathan Neuschäfer
162975b7f0fSJoel Stanley writel(NPCM_WTR | NPCM_WTRE | NPCM_WTE, wdt->reg);
163975b7f0fSJoel Stanley udelay(1000);
164975b7f0fSJoel Stanley
165975b7f0fSJoel Stanley return 0;
166975b7f0fSJoel Stanley }
167975b7f0fSJoel Stanley
npcm_is_running(struct watchdog_device * wdd)168975b7f0fSJoel Stanley static bool npcm_is_running(struct watchdog_device *wdd)
169975b7f0fSJoel Stanley {
170975b7f0fSJoel Stanley struct npcm_wdt *wdt = to_npcm_wdt(wdd);
171975b7f0fSJoel Stanley
172975b7f0fSJoel Stanley return readl(wdt->reg) & NPCM_WTE;
173975b7f0fSJoel Stanley }
174975b7f0fSJoel Stanley
175975b7f0fSJoel Stanley static const struct watchdog_info npcm_wdt_info = {
176975b7f0fSJoel Stanley .identity = KBUILD_MODNAME,
177975b7f0fSJoel Stanley .options = WDIOF_SETTIMEOUT
178975b7f0fSJoel Stanley | WDIOF_KEEPALIVEPING
179975b7f0fSJoel Stanley | WDIOF_MAGICCLOSE,
180975b7f0fSJoel Stanley };
181975b7f0fSJoel Stanley
182975b7f0fSJoel Stanley static const struct watchdog_ops npcm_wdt_ops = {
183975b7f0fSJoel Stanley .owner = THIS_MODULE,
184975b7f0fSJoel Stanley .start = npcm_wdt_start,
185975b7f0fSJoel Stanley .stop = npcm_wdt_stop,
186975b7f0fSJoel Stanley .ping = npcm_wdt_ping,
187975b7f0fSJoel Stanley .set_timeout = npcm_wdt_set_timeout,
188975b7f0fSJoel Stanley .restart = npcm_wdt_restart,
189975b7f0fSJoel Stanley };
190975b7f0fSJoel Stanley
npcm_wdt_probe(struct platform_device * pdev)191975b7f0fSJoel Stanley static int npcm_wdt_probe(struct platform_device *pdev)
192975b7f0fSJoel Stanley {
193975b7f0fSJoel Stanley struct device *dev = &pdev->dev;
194975b7f0fSJoel Stanley struct npcm_wdt *wdt;
195975b7f0fSJoel Stanley int irq;
196975b7f0fSJoel Stanley int ret;
197975b7f0fSJoel Stanley
1987e6437e1SGuenter Roeck wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
199975b7f0fSJoel Stanley if (!wdt)
200975b7f0fSJoel Stanley return -ENOMEM;
201975b7f0fSJoel Stanley
2020f0a6a28SGuenter Roeck wdt->reg = devm_platform_ioremap_resource(pdev, 0);
203975b7f0fSJoel Stanley if (IS_ERR(wdt->reg))
204975b7f0fSJoel Stanley return PTR_ERR(wdt->reg);
205975b7f0fSJoel Stanley
206*af084fdcSJonathan Neuschäfer wdt->clk = devm_clk_get_optional(&pdev->dev, NULL);
207*af084fdcSJonathan Neuschäfer if (IS_ERR(wdt->clk))
208*af084fdcSJonathan Neuschäfer return PTR_ERR(wdt->clk);
209*af084fdcSJonathan Neuschäfer
210975b7f0fSJoel Stanley irq = platform_get_irq(pdev, 0);
211975b7f0fSJoel Stanley if (irq < 0)
212975b7f0fSJoel Stanley return irq;
213975b7f0fSJoel Stanley
214975b7f0fSJoel Stanley wdt->wdd.info = &npcm_wdt_info;
215975b7f0fSJoel Stanley wdt->wdd.ops = &npcm_wdt_ops;
216975b7f0fSJoel Stanley wdt->wdd.min_timeout = 1;
217975b7f0fSJoel Stanley wdt->wdd.max_timeout = 2750;
218975b7f0fSJoel Stanley wdt->wdd.parent = dev;
219975b7f0fSJoel Stanley
220975b7f0fSJoel Stanley wdt->wdd.timeout = 86;
221975b7f0fSJoel Stanley watchdog_init_timeout(&wdt->wdd, 0, dev);
222975b7f0fSJoel Stanley
223975b7f0fSJoel Stanley /* Ensure timeout is able to be represented by the hardware */
224975b7f0fSJoel Stanley npcm_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout);
225975b7f0fSJoel Stanley
226975b7f0fSJoel Stanley if (npcm_is_running(&wdt->wdd)) {
227975b7f0fSJoel Stanley /* Restart with the default or device-tree specified timeout */
228975b7f0fSJoel Stanley npcm_wdt_start(&wdt->wdd);
229975b7f0fSJoel Stanley set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
230975b7f0fSJoel Stanley }
231975b7f0fSJoel Stanley
2327e6437e1SGuenter Roeck ret = devm_request_irq(dev, irq, npcm_wdt_interrupt, 0, "watchdog",
2337e6437e1SGuenter Roeck wdt);
234975b7f0fSJoel Stanley if (ret)
235975b7f0fSJoel Stanley return ret;
236975b7f0fSJoel Stanley
237975b7f0fSJoel Stanley ret = devm_watchdog_register_device(dev, &wdt->wdd);
238ab9113d0SWolfram Sang if (ret)
239975b7f0fSJoel Stanley return ret;
240975b7f0fSJoel Stanley
241975b7f0fSJoel Stanley dev_info(dev, "NPCM watchdog driver enabled\n");
242975b7f0fSJoel Stanley
243975b7f0fSJoel Stanley return 0;
244975b7f0fSJoel Stanley }
245975b7f0fSJoel Stanley
246975b7f0fSJoel Stanley #ifdef CONFIG_OF
247975b7f0fSJoel Stanley static const struct of_device_id npcm_wdt_match[] = {
248328d1c1aSJonathan Neuschäfer {.compatible = "nuvoton,wpcm450-wdt"},
249975b7f0fSJoel Stanley {.compatible = "nuvoton,npcm750-wdt"},
250975b7f0fSJoel Stanley {},
251975b7f0fSJoel Stanley };
252975b7f0fSJoel Stanley MODULE_DEVICE_TABLE(of, npcm_wdt_match);
253975b7f0fSJoel Stanley #endif
254975b7f0fSJoel Stanley
255975b7f0fSJoel Stanley static struct platform_driver npcm_wdt_driver = {
256975b7f0fSJoel Stanley .probe = npcm_wdt_probe,
257975b7f0fSJoel Stanley .driver = {
258975b7f0fSJoel Stanley .name = "npcm-wdt",
259975b7f0fSJoel Stanley .of_match_table = of_match_ptr(npcm_wdt_match),
260975b7f0fSJoel Stanley },
261975b7f0fSJoel Stanley };
262975b7f0fSJoel Stanley module_platform_driver(npcm_wdt_driver);
263975b7f0fSJoel Stanley
264975b7f0fSJoel Stanley MODULE_AUTHOR("Joel Stanley");
265975b7f0fSJoel Stanley MODULE_DESCRIPTION("Watchdog driver for NPCM");
266975b7f0fSJoel Stanley MODULE_LICENSE("GPL v2");
267