1 /*
2  * Watchdog driver for Conexant Digicolor
3  *
4  * Copyright (C) 2015 Paradox Innovation Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  */
11 
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/io.h>
15 #include <linux/delay.h>
16 #include <linux/clk.h>
17 #include <linux/watchdog.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_address.h>
20 
21 #define TIMER_A_CONTROL		0
22 #define TIMER_A_COUNT		4
23 
24 #define TIMER_A_ENABLE_COUNT	BIT(0)
25 #define TIMER_A_ENABLE_WATCHDOG	BIT(1)
26 
27 struct dc_wdt {
28 	void __iomem		*base;
29 	struct clk		*clk;
30 	spinlock_t		lock;
31 };
32 
33 static unsigned timeout;
34 module_param(timeout, uint, 0);
35 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
36 
37 static void dc_wdt_set(struct dc_wdt *wdt, u32 ticks)
38 {
39 	unsigned long flags;
40 
41 	spin_lock_irqsave(&wdt->lock, flags);
42 
43 	writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
44 	writel_relaxed(ticks, wdt->base + TIMER_A_COUNT);
45 	writel_relaxed(TIMER_A_ENABLE_COUNT | TIMER_A_ENABLE_WATCHDOG,
46 		       wdt->base + TIMER_A_CONTROL);
47 
48 	spin_unlock_irqrestore(&wdt->lock, flags);
49 }
50 
51 static int dc_wdt_restart(struct watchdog_device *wdog)
52 {
53 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
54 
55 	dc_wdt_set(wdt, 1);
56 	/* wait for reset to assert... */
57 	mdelay(500);
58 
59 	return 0;
60 }
61 
62 static int dc_wdt_start(struct watchdog_device *wdog)
63 {
64 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
65 
66 	dc_wdt_set(wdt, wdog->timeout * clk_get_rate(wdt->clk));
67 
68 	return 0;
69 }
70 
71 static int dc_wdt_stop(struct watchdog_device *wdog)
72 {
73 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
74 
75 	writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
76 
77 	return 0;
78 }
79 
80 static int dc_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
81 {
82 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
83 
84 	dc_wdt_set(wdt, t * clk_get_rate(wdt->clk));
85 	wdog->timeout = t;
86 
87 	return 0;
88 }
89 
90 static unsigned int dc_wdt_get_timeleft(struct watchdog_device *wdog)
91 {
92 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
93 	uint32_t count = readl_relaxed(wdt->base + TIMER_A_COUNT);
94 
95 	return count / clk_get_rate(wdt->clk);
96 }
97 
98 static struct watchdog_ops dc_wdt_ops = {
99 	.owner		= THIS_MODULE,
100 	.start		= dc_wdt_start,
101 	.stop		= dc_wdt_stop,
102 	.set_timeout	= dc_wdt_set_timeout,
103 	.get_timeleft	= dc_wdt_get_timeleft,
104 	.restart        = dc_wdt_restart,
105 };
106 
107 static struct watchdog_info dc_wdt_info = {
108 	.options	= WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE
109 			| WDIOF_KEEPALIVEPING,
110 	.identity	= "Conexant Digicolor Watchdog",
111 };
112 
113 static struct watchdog_device dc_wdt_wdd = {
114 	.info		= &dc_wdt_info,
115 	.ops		= &dc_wdt_ops,
116 	.min_timeout	= 1,
117 };
118 
119 static int dc_wdt_probe(struct platform_device *pdev)
120 {
121 	struct device *dev = &pdev->dev;
122 	struct device_node *np = dev->of_node;
123 	struct dc_wdt *wdt;
124 	int ret;
125 
126 	wdt = devm_kzalloc(dev, sizeof(struct dc_wdt), GFP_KERNEL);
127 	if (!wdt)
128 		return -ENOMEM;
129 	platform_set_drvdata(pdev, wdt);
130 
131 	wdt->base = of_iomap(np, 0);
132 	if (!wdt->base) {
133 		dev_err(dev, "Failed to remap watchdog regs");
134 		return -ENODEV;
135 	}
136 
137 	wdt->clk = devm_clk_get(&pdev->dev, NULL);
138 	if (IS_ERR(wdt->clk)) {
139 		ret = PTR_ERR(wdt->clk);
140 		goto err_iounmap;
141 	}
142 	dc_wdt_wdd.max_timeout = U32_MAX / clk_get_rate(wdt->clk);
143 	dc_wdt_wdd.timeout = dc_wdt_wdd.max_timeout;
144 	dc_wdt_wdd.parent = &pdev->dev;
145 
146 	spin_lock_init(&wdt->lock);
147 
148 	watchdog_set_drvdata(&dc_wdt_wdd, wdt);
149 	watchdog_set_restart_priority(&dc_wdt_wdd, 128);
150 	watchdog_init_timeout(&dc_wdt_wdd, timeout, dev);
151 	ret = watchdog_register_device(&dc_wdt_wdd);
152 	if (ret) {
153 		dev_err(dev, "Failed to register watchdog device");
154 		goto err_iounmap;
155 	}
156 
157 	return 0;
158 
159 err_iounmap:
160 	iounmap(wdt->base);
161 	return ret;
162 }
163 
164 static int dc_wdt_remove(struct platform_device *pdev)
165 {
166 	struct dc_wdt *wdt = platform_get_drvdata(pdev);
167 
168 	watchdog_unregister_device(&dc_wdt_wdd);
169 	iounmap(wdt->base);
170 
171 	return 0;
172 }
173 
174 static void dc_wdt_shutdown(struct platform_device *pdev)
175 {
176 	dc_wdt_stop(&dc_wdt_wdd);
177 }
178 
179 static const struct of_device_id dc_wdt_of_match[] = {
180 	{ .compatible = "cnxt,cx92755-wdt", },
181 	{},
182 };
183 MODULE_DEVICE_TABLE(of, dc_wdt_of_match);
184 
185 static struct platform_driver dc_wdt_driver = {
186 	.probe		= dc_wdt_probe,
187 	.remove		= dc_wdt_remove,
188 	.shutdown	= dc_wdt_shutdown,
189 	.driver = {
190 		.name =		"digicolor-wdt",
191 		.of_match_table = dc_wdt_of_match,
192 	},
193 };
194 module_platform_driver(dc_wdt_driver);
195 
196 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
197 MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
198 MODULE_LICENSE("GPL");
199