blob: b48fd0871f1740ce424c0ad1b265fefcecbdee54 [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 Garciafc723852014-02-10 20:00:28 -030053};
54
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030055struct orion_watchdog {
56 struct watchdog_device wdt;
57 void __iomem *reg;
58 void __iomem *rstout;
59 unsigned long clk_rate;
60 struct clk *clk;
Ezequiel Garciafc723852014-02-10 20:00:28 -030061 const struct orion_watchdog_data *data;
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030062};
Sylver Bruneau22ac9232008-06-26 10:47:45 +020063
Ezequiel Garcia19242272014-02-10 20:00:29 -030064static int orion_wdt_clock_init(struct platform_device *pdev,
65 struct orion_watchdog *dev)
66{
67 int ret;
68
69 dev->clk = devm_clk_get(&pdev->dev, NULL);
70 if (IS_ERR(dev->clk))
71 return PTR_ERR(dev->clk);
72 ret = clk_prepare_enable(dev->clk);
73 if (ret)
74 return ret;
75
76 dev->clk_rate = clk_get_rate(dev->clk);
77 return 0;
78}
79
Axel Lin0dd6e482012-03-26 11:14:29 +080080static int orion_wdt_ping(struct watchdog_device *wdt_dev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010081{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030082 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010083 /* Reload watchdog duration */
Ezequiel Garciafc723852014-02-10 20:00:28 -030084 writel(dev->clk_rate * wdt_dev->timeout,
85 dev->reg + dev->data->wdt_counter_offset);
Axel Lin0dd6e482012-03-26 11:14:29 +080086 return 0;
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010087}
88
Axel Lin0dd6e482012-03-26 11:14:29 +080089static int orion_wdt_start(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020090{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -030091 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
92
Sylver Bruneau22ac9232008-06-26 10:47:45 +020093 /* Set watchdog duration */
Ezequiel Garciafc723852014-02-10 20:00:28 -030094 writel(dev->clk_rate * wdt_dev->timeout,
95 dev->reg + dev->data->wdt_counter_offset);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020096
Sylver Bruneau22ac9232008-06-26 10:47:45 +020097 /* Enable watchdog timer */
Ezequiel Garciafc723852014-02-10 20:00:28 -030098 atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit,
99 dev->data->wdt_enable_bit);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200100
101 /* Enable reset on watchdog */
Ezequiel Garciafc723852014-02-10 20:00:28 -0300102 atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit,
103 dev->data->rstout_enable_bit);
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300104
Axel Lin0dd6e482012-03-26 11:14:29 +0800105 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200106}
107
Axel Lin0dd6e482012-03-26 11:14:29 +0800108static int orion_wdt_stop(struct watchdog_device *wdt_dev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200109{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300110 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
111
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200112 /* Disable reset on watchdog */
Ezequiel Garciafc723852014-02-10 20:00:28 -0300113 atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit, 0);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200114
115 /* Disable watchdog timer */
Ezequiel Garciafc723852014-02-10 20:00:28 -0300116 atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit, 0);
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300117
Axel Lin0dd6e482012-03-26 11:14:29 +0800118 return 0;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000119}
120
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300121static int orion_wdt_enabled(struct orion_watchdog *dev)
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300122{
123 bool enabled, running;
124
Ezequiel Garciafc723852014-02-10 20:00:28 -0300125 enabled = readl(dev->rstout) & dev->data->rstout_enable_bit;
126 running = readl(dev->reg + TIMER_CTRL) & dev->data->wdt_enable_bit;
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300127
128 return enabled && running;
129}
130
Axel Lin0dd6e482012-03-26 11:14:29 +0800131static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000132{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300133 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
Ezequiel Garciafc723852014-02-10 20:00:28 -0300134 return readl(dev->reg + dev->data->wdt_counter_offset) / dev->clk_rate;
Axel Lin0dd6e482012-03-26 11:14:29 +0800135}
136
137static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
138 unsigned int timeout)
139{
140 wdt_dev->timeout = timeout;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000141 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200142}
143
Axel Lin0dd6e482012-03-26 11:14:29 +0800144static const struct watchdog_info orion_wdt_info = {
145 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
146 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200147};
148
Axel Lin0dd6e482012-03-26 11:14:29 +0800149static const struct watchdog_ops orion_wdt_ops = {
150 .owner = THIS_MODULE,
151 .start = orion_wdt_start,
152 .stop = orion_wdt_stop,
153 .ping = orion_wdt_ping,
154 .set_timeout = orion_wdt_set_timeout,
155 .get_timeleft = orion_wdt_get_timeleft,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200156};
157
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300158static irqreturn_t orion_wdt_irq(int irq, void *devid)
159{
160 panic("Watchdog Timeout");
161 return IRQ_HANDLED;
162}
163
Ezequiel Garcia868eb612014-02-10 20:00:25 -0300164/*
165 * The original devicetree binding for this driver specified only
166 * one memory resource, so in order to keep DT backwards compatibility
167 * we try to fallback to a hardcoded register address, if the resource
168 * is missing from the devicetree.
169 */
170static void __iomem *orion_wdt_ioremap_rstout(struct platform_device *pdev,
171 phys_addr_t internal_regs)
172{
173 struct resource *res;
174 phys_addr_t rstout;
175
176 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
177 if (res)
178 return devm_ioremap(&pdev->dev, res->start,
179 resource_size(res));
180
181 /* This workaround works only for "orion-wdt", DT-enabled */
182 if (!of_device_is_compatible(pdev->dev.of_node, "marvell,orion-wdt"))
183 return NULL;
184
185 rstout = internal_regs + ORION_RSTOUT_MASK_OFFSET;
186
187 WARN(1, FW_BUG "falling back to harcoded RSTOUT reg 0x%x\n", rstout);
188 return devm_ioremap(&pdev->dev, rstout, 0x4);
189}
190
Ezequiel Garciafc723852014-02-10 20:00:28 -0300191static const struct orion_watchdog_data orion_data = {
192 .rstout_enable_bit = BIT(1),
193 .wdt_enable_bit = BIT(4),
194 .wdt_counter_offset = 0x24,
Ezequiel Garcia19242272014-02-10 20:00:29 -0300195 .clock_init = orion_wdt_clock_init,
Ezequiel Garciafc723852014-02-10 20:00:28 -0300196};
197
198static const struct of_device_id orion_wdt_of_match_table[] = {
199 {
200 .compatible = "marvell,orion-wdt",
201 .data = &orion_data,
202 },
203 {},
204};
205MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
206
Bill Pemberton2d991a12012-11-19 13:21:41 -0500207static int orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800208{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300209 struct orion_watchdog *dev;
Ezequiel Garciafc723852014-02-10 20:00:28 -0300210 const struct of_device_id *match;
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300211 unsigned int wdt_max_duration; /* (seconds) */
Jason Coopera855a7c2012-03-15 00:33:26 +0000212 struct resource *res;
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300213 int ret, irq;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800214
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300215 dev = devm_kzalloc(&pdev->dev, sizeof(struct orion_watchdog),
216 GFP_KERNEL);
217 if (!dev)
218 return -ENOMEM;
219
Ezequiel Garciafc723852014-02-10 20:00:28 -0300220 match = of_match_device(orion_wdt_of_match_table, &pdev->dev);
221 if (!match)
222 /* Default legacy match */
223 match = &orion_wdt_of_match_table[0];
224
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300225 dev->wdt.info = &orion_wdt_info;
226 dev->wdt.ops = &orion_wdt_ops;
227 dev->wdt.min_timeout = 1;
Ezequiel Garciafc723852014-02-10 20:00:28 -0300228 dev->data = match->data;
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300229
Jason Coopera855a7c2012-03-15 00:33:26 +0000230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia19242272014-02-10 20:00:29 -0300231 if (!res)
232 return -ENODEV;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300233
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300234 dev->reg = devm_ioremap(&pdev->dev, res->start,
235 resource_size(res));
Ezequiel Garcia19242272014-02-10 20:00:29 -0300236 if (!dev->reg)
237 return -ENOMEM;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800238
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300239 dev->rstout = orion_wdt_ioremap_rstout(pdev, res->start &
240 INTERNAL_REGS_MASK);
Ezequiel Garcia19242272014-02-10 20:00:29 -0300241 if (!dev->rstout)
242 return -ENODEV;
243
244 ret = dev->data->clock_init(pdev, dev);
245 if (ret) {
246 dev_err(&pdev->dev, "cannot initialize clock\n");
247 return ret;
Ezequiel Garcia868eb612014-02-10 20:00:25 -0300248 }
249
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300250 wdt_max_duration = WDT_MAX_CYCLE_COUNT / dev->clk_rate;
Axel Lin0dd6e482012-03-26 11:14:29 +0800251
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300252 dev->wdt.timeout = wdt_max_duration;
253 dev->wdt.max_timeout = wdt_max_duration;
254 watchdog_init_timeout(&dev->wdt, heartbeat, &pdev->dev);
255
256 platform_set_drvdata(pdev, &dev->wdt);
257 watchdog_set_drvdata(&dev->wdt, dev);
Axel Lin0dd6e482012-03-26 11:14:29 +0800258
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300259 /*
260 * Let's make sure the watchdog is fully stopped, unless it's
261 * explicitly enabled. This may be the case if the module was
262 * removed and re-insterted, or if the bootloader explicitly
263 * set a running watchdog before booting the kernel.
264 */
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300265 if (!orion_wdt_enabled(dev))
266 orion_wdt_stop(&dev->wdt);
Ezequiel Garciad9d0c532014-02-10 20:00:23 -0300267
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300268 /* Request the IRQ only after the watchdog is disabled */
269 irq = platform_get_irq(pdev, 0);
270 if (irq > 0) {
271 /*
272 * Not all supported platforms specify an interrupt for the
273 * watchdog, so let's make it optional.
274 */
275 ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300276 pdev->name, dev);
Ezequiel Garciae97662e2014-02-10 20:00:24 -0300277 if (ret < 0) {
278 dev_err(&pdev->dev, "failed to request IRQ\n");
279 goto disable_clk;
280 }
281 }
282
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300283 watchdog_set_nowayout(&dev->wdt, nowayout);
284 ret = watchdog_register_device(&dev->wdt);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300285 if (ret)
286 goto disable_clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800287
Joe Perches27c766a2012-02-15 15:06:19 -0800288 pr_info("Initial timeout %d sec%s\n",
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300289 dev->wdt.timeout, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800290 return 0;
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300291
292disable_clk:
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300293 clk_disable_unprepare(dev->clk);
Ezequiel Garciabb02c662014-02-10 20:00:20 -0300294 return ret;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800295}
296
Bill Pemberton4b12b892012-11-19 13:26:24 -0500297static int orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200298{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300299 struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
300 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
301
302 watchdog_unregister_device(wdt_dev);
303 clk_disable_unprepare(dev->clk);
Axel Lin0dd6e482012-03-26 11:14:29 +0800304 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200305}
306
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400307static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100308{
Ezequiel Garciab89a9c42014-02-10 20:00:27 -0300309 struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
310 orion_wdt_stop(wdt_dev);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100311}
312
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400313static struct platform_driver orion_wdt_driver = {
314 .probe = orion_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500315 .remove = orion_wdt_remove,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400316 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800317 .driver = {
318 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400319 .name = "orion_wdt",
Sachin Kamat85eee812013-09-30 10:12:51 +0530320 .of_match_table = orion_wdt_of_match_table,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800321 },
322};
323
Axel Linb8ec6112011-11-29 13:56:27 +0800324module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200325
326MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400327MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200328
329module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100330MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200331
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100332module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100333MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
334 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200335
336MODULE_LICENSE("GPL");
Lubomir Rintelf3ea7332013-01-04 15:06:28 +0100337MODULE_ALIAS("platform:orion_wdt");