blob: 392529785b40b20bd3dbcb4d38c6d4f34ad28ad3 [file] [log] [blame]
Sylver Bruneau22ac9232008-06-26 10:47:45 +02001/*
Nicolas Pitre3b937a72009-06-01 13:56:02 -04002 * drivers/watchdog/orion_wdt.c
Sylver Bruneau22ac9232008-06-26 10:47:45 +02003 *
Nicolas Pitre3b937a72009-06-01 13:56:02 -04004 * Watchdog driver for Orion/Kirkwood processors
Sylver Bruneau22ac9232008-06-26 10:47:45 +02005 *
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
Joe Perches27c766a2012-02-15 15:06:19 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Sylver Bruneau22ac9232008-06-26 10:47:45 +020015#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080019#include <linux/platform_device.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020020#include <linux/watchdog.h>
21#include <linux/init.h>
Ezequiel Garciae97662e2014-02-10 20:00:24 -030022#include <linux/interrupt.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020023#include <linux/io.h>
Andrew Lunn4f04be62012-03-04 16:57:31 +010024#include <linux/clk.h>
Axel Lin0dd6e482012-03-26 11:14:29 +080025#include <linux/err.h>
Andrew Lunn1e7bad02012-06-10 15:20:06 +020026#include <linux/of.h>
Ezequiel Garciafc723852014-02-10 20:00:28 -030027#include <linux/of_device.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020028
Ezequiel Garcia868eb612014-02-10 20:00:25 -030029/* RSTOUT mask register physical address for Orion5x, Kirkwood and Dove */
30#define ORION_RSTOUT_MASK_OFFSET 0x20108
31
32/* Internal registers can be configured at any 1 MiB aligned address */
33#define INTERNAL_REGS_MASK ~(SZ_1M - 1)
34
Sylver Bruneau22ac9232008-06-26 10:47:45 +020035/*
36 * Watchdog timer block registers.
37 */
Jason Coopera855a7c2012-03-15 00:33:26 +000038#define TIMER_CTRL 0x0000
Sylver Bruneau22ac9232008-06-26 10:47:45 +020039
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080040#define WDT_MAX_CYCLE_COUNT 0xffffffff
Sylver Bruneau22ac9232008-06-26 10:47:45 +020041
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010042static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080043static int heartbeat = -1; /* module parameter (seconds) */
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030044
Ezequiel Garcia19242272014-02-10 20:00:29 -030045struct orion_watchdog;
46
Ezequiel Garciafc723852014-02-10 20:00:28 -030047struct orion_watchdog_data {
48 int wdt_counter_offset;
49 int wdt_enable_bit;
50 int rstout_enable_bit;
Ezequiel Garcia19242272014-02-10 20:00:29 -030051 int (*clock_init)(struct platform_device *,
52 struct orion_watchdog *);
Ezequiel Garcia490d8e32014-02-10 20:00:30 -030053 int (*start)(struct watchdog_device *);
Ezequiel Garciafc723852014-02-10 20:00:28 -030054};
55
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030056struct orion_watchdog {
57 struct watchdog_device wdt;
58 void __iomem *reg;
59 void __iomem *rstout;
60 unsigned long clk_rate;
61 struct clk *clk;
Ezequiel Garciafc723852014-02-10 20:00:28 -030062 const struct orion_watchdog_data *data;
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030063};
Sylver Bruneau22ac9232008-06-26 10:47:45 +020064
Ezequiel Garcia19242272014-02-10 20:00:29 -030065static int orion_wdt_clock_init(struct platform_device *pdev,
66 struct orion_watchdog *dev)
67{
68 int ret;
69
70 dev->clk = devm_clk_get(&pdev->dev, NULL);
71 if (IS_ERR(dev->clk))
72 return PTR_ERR(dev->clk);
73 ret = clk_prepare_enable(dev->clk);
74 if (ret)
75 return ret;
76
77 dev->clk_rate = clk_get_rate(dev->clk);
78 return 0;
79}
80
Axel Lin0dd6e482012-03-26 11:14:29 +080081static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010082{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030083 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010084 /* Reload watchdog duration */
Ezequiel Garciafc723852014-02-10 20:00:28 -030085 writel(dev->clk_rate * wdt_dev->timeout,
86 dev->reg + dev->data->wdt_counter_offset);
Axel Lin0dd6e482012-03-26 11:14:29 +080087 return 0;
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010088}
89
Ezequiel Garcia490d8e32014-02-10 20:00:30 -030090static int orion_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020091{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030092 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
93
Sylver Bruneau22ac9232008-06-26 10:47:45 +020094 /* Set watchdog duration */
Ezequiel Garciafc723852014-02-10 20:00:28 -030095 writel(dev->clk_rate * wdt_dev->timeout,
96 dev->reg + dev->data->wdt_counter_offset);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020097
Sylver Bruneau22ac9232008-06-26 10:47:45 +020098 /* Enable watchdog timer */
Ezequiel Garciafc723852014-02-10 20:00:28 -030099 atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit,
100 dev->data->wdt_enable_bit);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200101
102 /* Enable reset on watchdog */
Ezequiel Garciafc723852014-02-10 20:00:28 -0300103 atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit,
104 dev->data->rstout_enable_bit);
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300105
Axel Lin0dd6e482012-03-26 11:14:29 +0800106 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200107}
108
Ezequiel Garcia490d8e32014-02-10 20:00:30 -0300109static int orion_wdt_start(struct watchdog_device *wdt_dev)
110{
111 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
112
113 /* There are some per-SoC quirks to handle */
114 return dev->data->start(wdt_dev);
115}
116
Axel Lin0dd6e482012-03-26 11:14:29 +0800117static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200118{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300119 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
120
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200121 /* Disable reset on watchdog */
Ezequiel Garciafc723852014-02-10 20:00:28 -0300122 atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit, 0);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200123
124 /* Disable watchdog timer */
Ezequiel Garciafc723852014-02-10 20:00:28 -0300125 atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit, 0);
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300126
Axel Lin0dd6e482012-03-26 11:14:29 +0800127 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000128}
129
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300130static int orion_wdt_enabled(struct orion_watchdog *dev)
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300131{
132 bool enabled, running;
133
Ezequiel Garciafc723852014-02-10 20:00:28 -0300134 enabled = readl(dev->rstout) & dev->data->rstout_enable_bit;
135 running = readl(dev->reg + TIMER_CTRL) & dev->data->wdt_enable_bit;
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300136
137 return enabled && running;
138}
139
Axel Lin0dd6e482012-03-26 11:14:29 +0800140static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000141{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300142 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
Ezequiel Garciafc723852014-02-10 20:00:28 -0300143 return readl(dev->reg + dev->data->wdt_counter_offset) / dev->clk_rate;
Axel Lin0dd6e482012-03-26 11:14:29 +0800144}
145
146static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
147 unsigned int timeout)
148{
149 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000150 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200151}
152
Axel Lin0dd6e482012-03-26 11:14:29 +0800153static const struct watchdog_info orion_wdt_info = {
154 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
155 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200156};
157
Axel Lin0dd6e482012-03-26 11:14:29 +0800158static const struct watchdog_ops orion_wdt_ops = {
159 .owner = THIS_MODULE,
160 .start = orion_wdt_start,
161 .stop = orion_wdt_stop,
162 .ping = orion_wdt_ping,
163 .set_timeout = orion_wdt_set_timeout,
164 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200165};
166
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300167static irqreturn_t orion_wdt_irq(int irq, void *devid)
168{
169 panic("Watchdog Timeout");
170 return IRQ_HANDLED;
171}
172
Ezequiel Garcia868eb612014-02-10 20:00:25 -0300173/*
174 * The original devicetree binding for this driver specified only
175 * one memory resource, so in order to keep DT backwards compatibility
176 * we try to fallback to a hardcoded register address, if the resource
177 * is missing from the devicetree.
178 */
179static void __iomem *orion_wdt_ioremap_rstout(struct platform_device *pdev,
180 phys_addr_t internal_regs)
181{
182 struct resource *res;
183 phys_addr_t rstout;
184
185 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
186 if (res)
187 return devm_ioremap(&pdev->dev, res->start,
188 resource_size(res));
189
190 /* This workaround works only for "orion-wdt", DT-enabled */
191 if (!of_device_is_compatible(pdev->dev.of_node, "marvell,orion-wdt"))
192 return NULL;
193
194 rstout = internal_regs + ORION_RSTOUT_MASK_OFFSET;
195
196 WARN(1, FW_BUG "falling back to harcoded RSTOUT reg 0x%x\n", rstout);
197 return devm_ioremap(&pdev->dev, rstout, 0x4);
198}
199
Ezequiel Garciafc723852014-02-10 20:00:28 -0300200static const struct orion_watchdog_data orion_data = {
201 .rstout_enable_bit = BIT(1),
202 .wdt_enable_bit = BIT(4),
203 .wdt_counter_offset = 0x24,
Ezequiel Garcia19242272014-02-10 20:00:29 -0300204 .clock_init = orion_wdt_clock_init,
Ezequiel Garcia490d8e32014-02-10 20:00:30 -0300205 .start = orion_start,
Ezequiel Garciafc723852014-02-10 20:00:28 -0300206};
207
208static const struct of_device_id orion_wdt_of_match_table[] = {
209 {
210 .compatible = "marvell,orion-wdt",
211 .data = &orion_data,
212 },
213 {},
214};
215MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
216
Bill Pemberton2d991a12012-11-19 13:21:41 -0500217static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800218{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300219 struct orion_watchdog *dev;
Ezequiel Garciafc723852014-02-10 20:00:28 -0300220 const struct of_device_id *match;
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300221 unsigned int wdt_max_duration; /* (seconds) */
Jason Coopera855a7c2012-03-15 00:33:26 +0000222 struct resource *res;
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300223 int ret, irq;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800224
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300225 dev = devm_kzalloc(&pdev->dev, sizeof(struct orion_watchdog),
226 GFP_KERNEL);
227 if (!dev)
228 return -ENOMEM;
229
Ezequiel Garciafc723852014-02-10 20:00:28 -0300230 match = of_match_device(orion_wdt_of_match_table, &pdev->dev);
231 if (!match)
232 /* Default legacy match */
233 match = &orion_wdt_of_match_table[0];
234
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300235 dev->wdt.info = &orion_wdt_info;
236 dev->wdt.ops = &orion_wdt_ops;
237 dev->wdt.min_timeout = 1;
Ezequiel Garciafc723852014-02-10 20:00:28 -0300238 dev->data = match->data;
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300239
Jason Coopera855a7c2012-03-15 00:33:26 +0000240 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia19242272014-02-10 20:00:29 -0300241 if (!res)
242 return -ENODEV;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300243
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300244 dev->reg = devm_ioremap(&pdev->dev, res->start,
245 resource_size(res));
Ezequiel Garcia19242272014-02-10 20:00:29 -0300246 if (!dev->reg)
247 return -ENOMEM;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800248
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300249 dev->rstout = orion_wdt_ioremap_rstout(pdev, res->start &
250 INTERNAL_REGS_MASK);
Ezequiel Garcia19242272014-02-10 20:00:29 -0300251 if (!dev->rstout)
252 return -ENODEV;
253
254 ret = dev->data->clock_init(pdev, dev);
255 if (ret) {
256 dev_err(&pdev->dev, "cannot initialize clock\n");
257 return ret;
Ezequiel Garcia868eb612014-02-10 20:00:25 -0300258 }
259
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300260 wdt_max_duration = WDT_MAX_CYCLE_COUNT / dev->clk_rate;
Axel Lin0dd6e482012-03-26 11:14:29 +0800261
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300262 dev->wdt.timeout = wdt_max_duration;
263 dev->wdt.max_timeout = wdt_max_duration;
264 watchdog_init_timeout(&dev->wdt, heartbeat, &pdev->dev);
265
266 platform_set_drvdata(pdev, &dev->wdt);
267 watchdog_set_drvdata(&dev->wdt, dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800268
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300269 /*
270 * Let's make sure the watchdog is fully stopped, unless it's
271 * explicitly enabled. This may be the case if the module was
272 * removed and re-insterted, or if the bootloader explicitly
273 * set a running watchdog before booting the kernel.
274 */
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300275 if (!orion_wdt_enabled(dev))
276 orion_wdt_stop(&dev->wdt);
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300277
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300278 /* Request the IRQ only after the watchdog is disabled */
279 irq = platform_get_irq(pdev, 0);
280 if (irq > 0) {
281 /*
282 * Not all supported platforms specify an interrupt for the
283 * watchdog, so let's make it optional.
284 */
285 ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300286 pdev->name, dev);
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300287 if (ret < 0) {
288 dev_err(&pdev->dev, "failed to request IRQ\n");
289 goto disable_clk;
290 }
291 }
292
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300293 watchdog_set_nowayout(&dev->wdt, nowayout);
294 ret = watchdog_register_device(&dev->wdt);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300295 if (ret)
296 goto disable_clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800297
Joe Perches27c766a2012-02-15 15:06:19 -0800298 pr_info("Initial timeout %d sec%s\n",
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300299 dev->wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800300 return 0;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300301
302disable_clk:
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300303 clk_disable_unprepare(dev->clk);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300304 return ret;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800305}
306
Bill Pemberton4b12b892012-11-19 13:26:24 -0500307static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200308{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300309 struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
310 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
311
312 watchdog_unregister_device(wdt_dev);
313 clk_disable_unprepare(dev->clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800314 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200315}
316
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400317static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100318{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300319 struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
320 orion_wdt_stop(wdt_dev);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100321}
322
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400323static struct platform_driver orion_wdt_driver = {
324 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500325 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400326 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800327 .driver = {
328 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400329 .name = "orion_wdt",
Sachin Kamat85eee812013-09-30 10:12:51 +0530330 .of_match_table = orion_wdt_of_match_table,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800331 },
332};
333
Axel Linb8ec6112011-11-29 13:56:27 +0800334module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200335
336MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400337MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200338
339module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100340MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200341
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100342module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100343MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
344 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200345
346MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100347MODULE_ALIAS("platform:orion_wdt");