orion_wdt.c 8.6 KB
Newer Older
1
/*
2
 * drivers/watchdog/orion_wdt.c
3
 *
4
 * Watchdog driver for Orion/Kirkwood processors
5 6 7 8 9 10 11 12
 *
 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
 *
 * This file is licensed under  the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

13 14
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

15 16 17 18
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/kernel.h>
19
#include <linux/platform_device.h>
20 21
#include <linux/watchdog.h>
#include <linux/init.h>
22
#include <linux/interrupt.h>
23
#include <linux/io.h>
24
#include <linux/clk.h>
25
#include <linux/err.h>
26
#include <linux/of.h>
27
#include <linux/of_device.h>
28

29 30 31 32 33 34
/* RSTOUT mask register physical address for Orion5x, Kirkwood and Dove */
#define ORION_RSTOUT_MASK_OFFSET	0x20108

/* Internal registers can be configured at any 1 MiB aligned address */
#define INTERNAL_REGS_MASK		~(SZ_1M - 1)

35 36 37
/*
 * Watchdog timer block registers.
 */
38
#define TIMER_CTRL		0x0000
39

40
#define WDT_MAX_CYCLE_COUNT	0xffffffff
41

W
Wim Van Sebroeck 已提交
42
static bool nowayout = WATCHDOG_NOWAYOUT;
43
static int heartbeat = -1;		/* module parameter (seconds) */
44

45 46
struct orion_watchdog;

47 48 49 50
struct orion_watchdog_data {
	int wdt_counter_offset;
	int wdt_enable_bit;
	int rstout_enable_bit;
51 52
	int (*clock_init)(struct platform_device *,
			  struct orion_watchdog *);
53 54
};

55 56 57 58 59 60
struct orion_watchdog {
	struct watchdog_device wdt;
	void __iomem *reg;
	void __iomem *rstout;
	unsigned long clk_rate;
	struct clk *clk;
61
	const struct orion_watchdog_data *data;
62
};
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static int orion_wdt_clock_init(struct platform_device *pdev,
				struct orion_watchdog *dev)
{
	int ret;

	dev->clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(dev->clk))
		return PTR_ERR(dev->clk);
	ret = clk_prepare_enable(dev->clk);
	if (ret)
		return ret;

	dev->clk_rate = clk_get_rate(dev->clk);
	return 0;
}

80
static int orion_wdt_ping(struct watchdog_device *wdt_dev)
81
{
82
	struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
83
	/* Reload watchdog duration */
84 85
	writel(dev->clk_rate * wdt_dev->timeout,
	       dev->reg + dev->data->wdt_counter_offset);
86
	return 0;
87 88
}

89
static int orion_wdt_start(struct watchdog_device *wdt_dev)
90
{
91 92
	struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);

93
	/* Set watchdog duration */
94 95
	writel(dev->clk_rate * wdt_dev->timeout,
	       dev->reg + dev->data->wdt_counter_offset);
96 97

	/* Enable watchdog timer */
98 99
	atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit,
						dev->data->wdt_enable_bit);
100 101

	/* Enable reset on watchdog */
102 103
	atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit,
				      dev->data->rstout_enable_bit);
104

105
	return 0;
106 107
}

108
static int orion_wdt_stop(struct watchdog_device *wdt_dev)
109
{
110 111
	struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);

112
	/* Disable reset on watchdog */
113
	atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit, 0);
114 115

	/* Disable watchdog timer */
116
	atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit, 0);
117

118
	return 0;
119 120
}

121
static int orion_wdt_enabled(struct orion_watchdog *dev)
122 123 124
{
	bool enabled, running;

125 126
	enabled = readl(dev->rstout) & dev->data->rstout_enable_bit;
	running = readl(dev->reg + TIMER_CTRL) & dev->data->wdt_enable_bit;
127 128 129 130

	return enabled && running;
}

131
static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
132
{
133
	struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
134
	return readl(dev->reg + dev->data->wdt_counter_offset) / dev->clk_rate;
135 136
}

137 138
static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
				 unsigned int timeout)
139
{
140
	wdt_dev->timeout = timeout;
141 142 143
	return 0;
}

144 145 146
static const struct watchdog_info orion_wdt_info = {
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
	.identity = "Orion Watchdog",
147 148
};

149 150 151 152 153 154 155
static const struct watchdog_ops orion_wdt_ops = {
	.owner = THIS_MODULE,
	.start = orion_wdt_start,
	.stop = orion_wdt_stop,
	.ping = orion_wdt_ping,
	.set_timeout = orion_wdt_set_timeout,
	.get_timeleft = orion_wdt_get_timeleft,
156 157
};

158 159 160 161 162 163
static irqreturn_t orion_wdt_irq(int irq, void *devid)
{
	panic("Watchdog Timeout");
	return IRQ_HANDLED;
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/*
 * The original devicetree binding for this driver specified only
 * one memory resource, so in order to keep DT backwards compatibility
 * we try to fallback to a hardcoded register address, if the resource
 * is missing from the devicetree.
 */
static void __iomem *orion_wdt_ioremap_rstout(struct platform_device *pdev,
					      phys_addr_t internal_regs)
{
	struct resource *res;
	phys_addr_t rstout;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (res)
		return devm_ioremap(&pdev->dev, res->start,
				    resource_size(res));

	/* This workaround works only for "orion-wdt", DT-enabled */
	if (!of_device_is_compatible(pdev->dev.of_node, "marvell,orion-wdt"))
		return NULL;

	rstout = internal_regs + ORION_RSTOUT_MASK_OFFSET;

	WARN(1, FW_BUG "falling back to harcoded RSTOUT reg 0x%x\n", rstout);
	return devm_ioremap(&pdev->dev, rstout, 0x4);
}

191 192 193 194
static const struct orion_watchdog_data orion_data = {
	.rstout_enable_bit = BIT(1),
	.wdt_enable_bit = BIT(4),
	.wdt_counter_offset = 0x24,
195
	.clock_init = orion_wdt_clock_init,
196 197 198 199 200 201 202 203 204 205 206
};

static const struct of_device_id orion_wdt_of_match_table[] = {
	{
		.compatible = "marvell,orion-wdt",
		.data = &orion_data,
	},
	{},
};
MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);

B
Bill Pemberton 已提交
207
static int orion_wdt_probe(struct platform_device *pdev)
208
{
209
	struct orion_watchdog *dev;
210
	const struct of_device_id *match;
211
	unsigned int wdt_max_duration;	/* (seconds) */
212
	struct resource *res;
213
	int ret, irq;
214

215 216 217 218 219
	dev = devm_kzalloc(&pdev->dev, sizeof(struct orion_watchdog),
			   GFP_KERNEL);
	if (!dev)
		return -ENOMEM;

220 221 222 223 224
	match = of_match_device(orion_wdt_of_match_table, &pdev->dev);
	if (!match)
		/* Default legacy match */
		match = &orion_wdt_of_match_table[0];

225 226 227
	dev->wdt.info = &orion_wdt_info;
	dev->wdt.ops = &orion_wdt_ops;
	dev->wdt.min_timeout = 1;
228
	dev->data = match->data;
229

230
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 232
	if (!res)
		return -ENODEV;
233

234 235
	dev->reg = devm_ioremap(&pdev->dev, res->start,
			       resource_size(res));
236 237
	if (!dev->reg)
		return -ENOMEM;
238

239 240
	dev->rstout = orion_wdt_ioremap_rstout(pdev, res->start &
						     INTERNAL_REGS_MASK);
241 242 243 244 245 246 247
	if (!dev->rstout)
		return -ENODEV;

	ret = dev->data->clock_init(pdev, dev);
	if (ret) {
		dev_err(&pdev->dev, "cannot initialize clock\n");
		return ret;
248 249
	}

250 251 252 253 254
	wdt_max_duration = WDT_MAX_CYCLE_COUNT / dev->clk_rate;

	dev->wdt.timeout = wdt_max_duration;
	dev->wdt.max_timeout = wdt_max_duration;
	watchdog_init_timeout(&dev->wdt, heartbeat, &pdev->dev);
255

256 257
	platform_set_drvdata(pdev, &dev->wdt);
	watchdog_set_drvdata(&dev->wdt, dev);
258

259 260 261 262 263 264
	/*
	 * Let's make sure the watchdog is fully stopped, unless it's
	 * explicitly enabled. This may be the case if the module was
	 * removed and re-insterted, or if the bootloader explicitly
	 * set a running watchdog before booting the kernel.
	 */
265 266
	if (!orion_wdt_enabled(dev))
		orion_wdt_stop(&dev->wdt);
267

268 269 270 271 272 273 274 275
	/* Request the IRQ only after the watchdog is disabled */
	irq = platform_get_irq(pdev, 0);
	if (irq > 0) {
		/*
		 * Not all supported platforms specify an interrupt for the
		 * watchdog, so let's make it optional.
		 */
		ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
276
				       pdev->name, dev);
277 278 279 280 281 282
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to request IRQ\n");
			goto disable_clk;
		}
	}

283 284
	watchdog_set_nowayout(&dev->wdt, nowayout);
	ret = watchdog_register_device(&dev->wdt);
285 286
	if (ret)
		goto disable_clk;
287

288
	pr_info("Initial timeout %d sec%s\n",
289
		dev->wdt.timeout, nowayout ? ", nowayout" : "");
290
	return 0;
291 292

disable_clk:
293
	clk_disable_unprepare(dev->clk);
294
	return ret;
295 296
}

B
Bill Pemberton 已提交
297
static int orion_wdt_remove(struct platform_device *pdev)
298
{
299 300 301 302 303
	struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
	struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);

	watchdog_unregister_device(wdt_dev);
	clk_disable_unprepare(dev->clk);
304
	return 0;
305 306
}

307
static void orion_wdt_shutdown(struct platform_device *pdev)
308
{
309 310
	struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
	orion_wdt_stop(wdt_dev);
311 312
}

313 314
static struct platform_driver orion_wdt_driver = {
	.probe		= orion_wdt_probe,
315
	.remove		= orion_wdt_remove,
316
	.shutdown	= orion_wdt_shutdown,
317 318
	.driver		= {
		.owner	= THIS_MODULE,
319
		.name	= "orion_wdt",
320
		.of_match_table = orion_wdt_of_match_table,
321 322 323
	},
};

324
module_platform_driver(orion_wdt_driver);
325 326

MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
327
MODULE_DESCRIPTION("Orion Processor Watchdog");
328 329

module_param(heartbeat, int, 0);
330
MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
331

W
Wim Van Sebroeck 已提交
332
module_param(nowayout, bool, 0);
333 334
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
335 336

MODULE_LICENSE("GPL");
337
MODULE_ALIAS("platform:orion_wdt");