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, unsigned long action,
52 			  void *data)
53 {
54 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
55 
56 	dc_wdt_set(wdt, 1);
57 	/* wait for reset to assert... */
58 	mdelay(500);
59 
60 	return 0;
61 }
62 
63 static int dc_wdt_start(struct watchdog_device *wdog)
64 {
65 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
66 
67 	dc_wdt_set(wdt, wdog->timeout * clk_get_rate(wdt->clk));
68 
69 	return 0;
70 }
71 
72 static int dc_wdt_stop(struct watchdog_device *wdog)
73 {
74 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
75 
76 	writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
77 
78 	return 0;
79 }
80 
81 static int dc_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
82 {
83 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
84 
85 	dc_wdt_set(wdt, t * clk_get_rate(wdt->clk));
86 	wdog->timeout = t;
87 
88 	return 0;
89 }
90 
91 static unsigned int dc_wdt_get_timeleft(struct watchdog_device *wdog)
92 {
93 	struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
94 	uint32_t count = readl_relaxed(wdt->base + TIMER_A_COUNT);
95 
96 	return count / clk_get_rate(wdt->clk);
97 }
98 
99 static const struct watchdog_ops dc_wdt_ops = {
100 	.owner		= THIS_MODULE,
101 	.start		= dc_wdt_start,
102 	.stop		= dc_wdt_stop,
103 	.set_timeout	= dc_wdt_set_timeout,
104 	.get_timeleft	= dc_wdt_get_timeleft,
105 	.restart        = dc_wdt_restart,
106 };
107 
108 static const struct watchdog_info dc_wdt_info = {
109 	.options	= WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE
110 			| WDIOF_KEEPALIVEPING,
111 	.identity	= "Conexant Digicolor Watchdog",
112 };
113 
114 static struct watchdog_device dc_wdt_wdd = {
115 	.info		= &dc_wdt_info,
116 	.ops		= &dc_wdt_ops,
117 	.min_timeout	= 1,
118 };
119 
120 static int dc_wdt_probe(struct platform_device *pdev)
121 {
122 	struct resource *res;
123 	struct device *dev = &pdev->dev;
124 	struct dc_wdt *wdt;
125 	int ret;
126 
127 	wdt = devm_kzalloc(dev, sizeof(struct dc_wdt), GFP_KERNEL);
128 	if (!wdt)
129 		return -ENOMEM;
130 
131 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 	wdt->base = devm_ioremap_resource(dev, res);
133 	if (IS_ERR(wdt->base))
134 		return PTR_ERR(wdt->base);
135 
136 	wdt->clk = devm_clk_get(dev, NULL);
137 	if (IS_ERR(wdt->clk))
138 		return PTR_ERR(wdt->clk);
139 	dc_wdt_wdd.max_timeout = U32_MAX / clk_get_rate(wdt->clk);
140 	dc_wdt_wdd.timeout = dc_wdt_wdd.max_timeout;
141 	dc_wdt_wdd.parent = dev;
142 
143 	spin_lock_init(&wdt->lock);
144 
145 	watchdog_set_drvdata(&dc_wdt_wdd, wdt);
146 	watchdog_set_restart_priority(&dc_wdt_wdd, 128);
147 	watchdog_init_timeout(&dc_wdt_wdd, timeout, dev);
148 	watchdog_stop_on_reboot(&dc_wdt_wdd);
149 	ret = devm_watchdog_register_device(dev, &dc_wdt_wdd);
150 	if (ret) {
151 		dev_err(dev, "Failed to register watchdog device");
152 		return ret;
153 	}
154 
155 	return 0;
156 }
157 
158 static const struct of_device_id dc_wdt_of_match[] = {
159 	{ .compatible = "cnxt,cx92755-wdt", },
160 	{},
161 };
162 MODULE_DEVICE_TABLE(of, dc_wdt_of_match);
163 
164 static struct platform_driver dc_wdt_driver = {
165 	.probe		= dc_wdt_probe,
166 	.driver = {
167 		.name =		"digicolor-wdt",
168 		.of_match_table = dc_wdt_of_match,
169 	},
170 };
171 module_platform_driver(dc_wdt_driver);
172 
173 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
174 MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
175 MODULE_LICENSE("GPL");
176