Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Watchdog driver for Broadcom BCM2835
  4 *
  5 * "bcm2708_wdog" driver written by Luke Diamand that was obtained from
  6 * branch "rpi-3.6.y" of git://github.com/raspberrypi/linux.git was used
  7 * as a hardware reference for the Broadcom BCM2835 watchdog timer.
  8 *
  9 * Copyright (C) 2013 Lubomir Rintel <lkundrak@v3.sk>
 10 *
 
 
 
 
 11 */
 12
 13#include <linux/delay.h>
 
 14#include <linux/types.h>
 15#include <linux/mfd/bcm2835-pm.h>
 16#include <linux/module.h>
 17#include <linux/io.h>
 18#include <linux/watchdog.h>
 19#include <linux/platform_device.h>
 20#include <linux/of_address.h>
 21#include <linux/of_platform.h>
 22
 23#define PM_RSTC				0x1c
 24#define PM_RSTS				0x20
 25#define PM_WDOG				0x24
 26
 27#define PM_PASSWORD			0x5a000000
 28
 29#define PM_WDOG_TIME_SET		0x000fffff
 30#define PM_RSTC_WRCFG_CLR		0xffffffcf
 31#define PM_RSTS_HADWRH_SET		0x00000040
 32#define PM_RSTC_WRCFG_SET		0x00000030
 33#define PM_RSTC_WRCFG_FULL_RESET	0x00000020
 34#define PM_RSTC_RESET			0x00000102
 35
 36/*
 37 * The Raspberry Pi firmware uses the RSTS register to know which partition
 38 * to boot from. The partition value is spread into bits 0, 2, 4, 6, 8, 10.
 39 * Partition 63 is a special partition used by the firmware to indicate halt.
 40 */
 41#define PM_RSTS_RASPBERRYPI_HALT	0x555
 42
 43#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
 44#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
 45#define WDOG_TICKS_TO_MSECS(x) ((x) * 1000 >> 16)
 46
 47struct bcm2835_wdt {
 48	void __iomem		*base;
 49	spinlock_t		lock;
 
 50};
 51
 52static struct bcm2835_wdt *bcm2835_power_off_wdt;
 53
 54static unsigned int heartbeat;
 55static bool nowayout = WATCHDOG_NOWAYOUT;
 56
 57static bool bcm2835_wdt_is_running(struct bcm2835_wdt *wdt)
 58{
 59	uint32_t cur;
 60
 61	cur = readl(wdt->base + PM_RSTC);
 62
 63	return !!(cur & PM_RSTC_WRCFG_FULL_RESET);
 64}
 65
 66static int bcm2835_wdt_start(struct watchdog_device *wdog)
 67{
 68	struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
 69	uint32_t cur;
 70	unsigned long flags;
 71
 72	spin_lock_irqsave(&wdt->lock, flags);
 73
 74	writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
 75				PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
 76	cur = readl_relaxed(wdt->base + PM_RSTC);
 77	writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
 78		  PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
 79
 80	spin_unlock_irqrestore(&wdt->lock, flags);
 81
 82	return 0;
 83}
 84
 85static int bcm2835_wdt_stop(struct watchdog_device *wdog)
 86{
 87	struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
 88
 89	writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
 90	return 0;
 91}
 92
 93static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
 94{
 95	struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
 96
 97	uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
 98	return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
 99}
100
101static void __bcm2835_restart(struct bcm2835_wdt *wdt)
102{
103	u32 val;
104
105	/* use a timeout of 10 ticks (~150us) */
106	writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
107	val = readl_relaxed(wdt->base + PM_RSTC);
108	val &= PM_RSTC_WRCFG_CLR;
109	val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
110	writel_relaxed(val, wdt->base + PM_RSTC);
111
112	/* No sleeping, possibly atomic. */
113	mdelay(1);
114}
115
116static int bcm2835_restart(struct watchdog_device *wdog,
117			   unsigned long action, void *data)
118{
119	struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
120
121	__bcm2835_restart(wdt);
122
123	return 0;
124}
125
126static const struct watchdog_ops bcm2835_wdt_ops = {
127	.owner =	THIS_MODULE,
128	.start =	bcm2835_wdt_start,
129	.stop =		bcm2835_wdt_stop,
130	.get_timeleft =	bcm2835_wdt_get_timeleft,
131	.restart =	bcm2835_restart,
132};
133
134static const struct watchdog_info bcm2835_wdt_info = {
135	.options =	WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
136			WDIOF_KEEPALIVEPING,
137	.identity =	"Broadcom BCM2835 Watchdog timer",
138};
139
140static struct watchdog_device bcm2835_wdt_wdd = {
141	.info =		&bcm2835_wdt_info,
142	.ops =		&bcm2835_wdt_ops,
143	.min_timeout =	1,
144	.max_hw_heartbeat_ms =	WDOG_TICKS_TO_MSECS(PM_WDOG_TIME_SET),
145	.timeout =	WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
146};
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148/*
149 * We can't really power off, but if we do the normal reset scheme, and
150 * indicate to bootcode.bin not to reboot, then most of the chip will be
151 * powered off.
152 */
153static void bcm2835_power_off(void)
154{
155	struct bcm2835_wdt *wdt = bcm2835_power_off_wdt;
 
 
 
156	u32 val;
157
158	/*
159	 * We set the watchdog hard reset bit here to distinguish this reset
160	 * from the normal (full) reset. bootcode.bin will not reboot after a
161	 * hard reset.
162	 */
163	val = readl_relaxed(wdt->base + PM_RSTS);
164	val |= PM_PASSWORD | PM_RSTS_RASPBERRYPI_HALT;
165	writel_relaxed(val, wdt->base + PM_RSTS);
166
167	/* Continue with normal reset mechanism */
168	__bcm2835_restart(wdt);
169}
170
171static int bcm2835_wdt_probe(struct platform_device *pdev)
172{
173	struct bcm2835_pm *pm = dev_get_drvdata(pdev->dev.parent);
174	struct device *dev = &pdev->dev;
 
175	struct bcm2835_wdt *wdt;
176	int err;
177
178	wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
179	if (!wdt)
180		return -ENOMEM;
 
181
182	spin_lock_init(&wdt->lock);
183
184	wdt->base = pm->base;
 
 
 
 
185
186	watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
187	watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
188	watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
189	bcm2835_wdt_wdd.parent = dev;
190	if (bcm2835_wdt_is_running(wdt)) {
191		/*
192		 * The currently active timeout value (set by the
193		 * bootloader) may be different from the module
194		 * heartbeat parameter or the value in device
195		 * tree. But we just need to set WDOG_HW_RUNNING,
196		 * because then the framework will "immediately" ping
197		 * the device, updating the timeout.
198		 */
199		set_bit(WDOG_HW_RUNNING, &bcm2835_wdt_wdd.status);
200	}
201
202	watchdog_set_restart_priority(&bcm2835_wdt_wdd, 128);
203
204	watchdog_stop_on_reboot(&bcm2835_wdt_wdd);
205	err = devm_watchdog_register_device(dev, &bcm2835_wdt_wdd);
206	if (err)
207		return err;
208
209	if (of_device_is_system_power_controller(pdev->dev.parent->of_node)) {
210		if (!pm_power_off) {
211			pm_power_off = bcm2835_power_off;
212			bcm2835_power_off_wdt = wdt;
213		} else {
214			dev_info(dev, "Poweroff handler already present!\n");
215		}
216	}
217
 
 
 
 
 
 
218	dev_info(dev, "Broadcom BCM2835 watchdog timer");
219	return 0;
220}
221
222static void bcm2835_wdt_remove(struct platform_device *pdev)
223{
 
 
 
224	if (pm_power_off == bcm2835_power_off)
225		pm_power_off = NULL;
 
 
 
 
 
 
 
 
 
226}
227
 
 
 
 
 
 
228static struct platform_driver bcm2835_wdt_driver = {
229	.probe		= bcm2835_wdt_probe,
230	.remove_new	= bcm2835_wdt_remove,
 
231	.driver = {
232		.name =		"bcm2835-wdt",
 
233	},
234};
235module_platform_driver(bcm2835_wdt_driver);
236
237module_param(heartbeat, uint, 0);
238MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
239
240module_param(nowayout, bool, 0);
241MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
242				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
243
244MODULE_ALIAS("platform:bcm2835-wdt");
245MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
246MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
247MODULE_LICENSE("GPL");
v4.10.11
 
  1/*
  2 * Watchdog driver for Broadcom BCM2835
  3 *
  4 * "bcm2708_wdog" driver written by Luke Diamand that was obtained from
  5 * branch "rpi-3.6.y" of git://github.com/raspberrypi/linux.git was used
  6 * as a hardware reference for the Broadcom BCM2835 watchdog timer.
  7 *
  8 * Copyright (C) 2013 Lubomir Rintel <lkundrak@v3.sk>
  9 *
 10 * This program is free software; you can redistribute it and/or modify it
 11 * under the terms of the GNU General Public License as published by the
 12 * Free Software Foundation; either version 2 of the License, or (at your
 13 * option) any later version.
 14 */
 15
 16#include <linux/delay.h>
 17#include <linux/reboot.h>
 18#include <linux/types.h>
 
 19#include <linux/module.h>
 20#include <linux/io.h>
 21#include <linux/watchdog.h>
 22#include <linux/platform_device.h>
 23#include <linux/of_address.h>
 24#include <linux/of_platform.h>
 25
 26#define PM_RSTC				0x1c
 27#define PM_RSTS				0x20
 28#define PM_WDOG				0x24
 29
 30#define PM_PASSWORD			0x5a000000
 31
 32#define PM_WDOG_TIME_SET		0x000fffff
 33#define PM_RSTC_WRCFG_CLR		0xffffffcf
 34#define PM_RSTS_HADWRH_SET		0x00000040
 35#define PM_RSTC_WRCFG_SET		0x00000030
 36#define PM_RSTC_WRCFG_FULL_RESET	0x00000020
 37#define PM_RSTC_RESET			0x00000102
 38
 39/*
 40 * The Raspberry Pi firmware uses the RSTS register to know which partiton
 41 * to boot from. The partiton value is spread into bits 0, 2, 4, 6, 8, 10.
 42 * Partiton 63 is a special partition used by the firmware to indicate halt.
 43 */
 44#define PM_RSTS_RASPBERRYPI_HALT	0x555
 45
 46#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
 47#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
 
 48
 49struct bcm2835_wdt {
 50	void __iomem		*base;
 51	spinlock_t		lock;
 52	struct notifier_block	restart_handler;
 53};
 54
 
 
 55static unsigned int heartbeat;
 56static bool nowayout = WATCHDOG_NOWAYOUT;
 57
 58static bool bcm2835_wdt_is_running(struct bcm2835_wdt *wdt)
 59{
 60	uint32_t cur;
 61
 62	cur = readl(wdt->base + PM_RSTC);
 63
 64	return !!(cur & PM_RSTC_WRCFG_FULL_RESET);
 65}
 66
 67static int bcm2835_wdt_start(struct watchdog_device *wdog)
 68{
 69	struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
 70	uint32_t cur;
 71	unsigned long flags;
 72
 73	spin_lock_irqsave(&wdt->lock, flags);
 74
 75	writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
 76				PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
 77	cur = readl_relaxed(wdt->base + PM_RSTC);
 78	writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
 79		  PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
 80
 81	spin_unlock_irqrestore(&wdt->lock, flags);
 82
 83	return 0;
 84}
 85
 86static int bcm2835_wdt_stop(struct watchdog_device *wdog)
 87{
 88	struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
 89
 90	writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
 91	return 0;
 92}
 93
 94static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
 95{
 96	struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
 97
 98	uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
 99	return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
100}
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102static const struct watchdog_ops bcm2835_wdt_ops = {
103	.owner =	THIS_MODULE,
104	.start =	bcm2835_wdt_start,
105	.stop =		bcm2835_wdt_stop,
106	.get_timeleft =	bcm2835_wdt_get_timeleft,
 
107};
108
109static const struct watchdog_info bcm2835_wdt_info = {
110	.options =	WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
111			WDIOF_KEEPALIVEPING,
112	.identity =	"Broadcom BCM2835 Watchdog timer",
113};
114
115static struct watchdog_device bcm2835_wdt_wdd = {
116	.info =		&bcm2835_wdt_info,
117	.ops =		&bcm2835_wdt_ops,
118	.min_timeout =	1,
119	.max_timeout =	WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
120	.timeout =	WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
121};
122
123static int
124bcm2835_restart(struct notifier_block *this, unsigned long mode, void *cmd)
125{
126	struct bcm2835_wdt *wdt = container_of(this, struct bcm2835_wdt,
127					       restart_handler);
128	u32 val;
129
130	/* use a timeout of 10 ticks (~150us) */
131	writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
132	val = readl_relaxed(wdt->base + PM_RSTC);
133	val &= PM_RSTC_WRCFG_CLR;
134	val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
135	writel_relaxed(val, wdt->base + PM_RSTC);
136
137	/* No sleeping, possibly atomic. */
138	mdelay(1);
139
140	return 0;
141}
142
143/*
144 * We can't really power off, but if we do the normal reset scheme, and
145 * indicate to bootcode.bin not to reboot, then most of the chip will be
146 * powered off.
147 */
148static void bcm2835_power_off(void)
149{
150	struct device_node *np =
151		of_find_compatible_node(NULL, NULL, "brcm,bcm2835-pm-wdt");
152	struct platform_device *pdev = of_find_device_by_node(np);
153	struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
154	u32 val;
155
156	/*
157	 * We set the watchdog hard reset bit here to distinguish this reset
158	 * from the normal (full) reset. bootcode.bin will not reboot after a
159	 * hard reset.
160	 */
161	val = readl_relaxed(wdt->base + PM_RSTS);
162	val |= PM_PASSWORD | PM_RSTS_RASPBERRYPI_HALT;
163	writel_relaxed(val, wdt->base + PM_RSTS);
164
165	/* Continue with normal reset mechanism */
166	bcm2835_restart(&wdt->restart_handler, REBOOT_HARD, NULL);
167}
168
169static int bcm2835_wdt_probe(struct platform_device *pdev)
170{
 
171	struct device *dev = &pdev->dev;
172	struct device_node *np = dev->of_node;
173	struct bcm2835_wdt *wdt;
174	int err;
175
176	wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
177	if (!wdt)
178		return -ENOMEM;
179	platform_set_drvdata(pdev, wdt);
180
181	spin_lock_init(&wdt->lock);
182
183	wdt->base = of_iomap(np, 0);
184	if (!wdt->base) {
185		dev_err(dev, "Failed to remap watchdog regs");
186		return -ENODEV;
187	}
188
189	watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
190	watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
191	watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
192	bcm2835_wdt_wdd.parent = &pdev->dev;
193	if (bcm2835_wdt_is_running(wdt)) {
194		/*
195		 * The currently active timeout value (set by the
196		 * bootloader) may be different from the module
197		 * heartbeat parameter or the value in device
198		 * tree. But we just need to set WDOG_HW_RUNNING,
199		 * because then the framework will "immediately" ping
200		 * the device, updating the timeout.
201		 */
202		set_bit(WDOG_HW_RUNNING, &bcm2835_wdt_wdd.status);
203	}
204	err = watchdog_register_device(&bcm2835_wdt_wdd);
205	if (err) {
206		dev_err(dev, "Failed to register watchdog device");
207		iounmap(wdt->base);
 
 
208		return err;
 
 
 
 
 
 
 
 
209	}
210
211	wdt->restart_handler.notifier_call = bcm2835_restart;
212	wdt->restart_handler.priority = 128;
213	register_restart_handler(&wdt->restart_handler);
214	if (pm_power_off == NULL)
215		pm_power_off = bcm2835_power_off;
216
217	dev_info(dev, "Broadcom BCM2835 watchdog timer");
218	return 0;
219}
220
221static int bcm2835_wdt_remove(struct platform_device *pdev)
222{
223	struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
224
225	unregister_restart_handler(&wdt->restart_handler);
226	if (pm_power_off == bcm2835_power_off)
227		pm_power_off = NULL;
228	watchdog_unregister_device(&bcm2835_wdt_wdd);
229	iounmap(wdt->base);
230
231	return 0;
232}
233
234static void bcm2835_wdt_shutdown(struct platform_device *pdev)
235{
236	bcm2835_wdt_stop(&bcm2835_wdt_wdd);
237}
238
239static const struct of_device_id bcm2835_wdt_of_match[] = {
240	{ .compatible = "brcm,bcm2835-pm-wdt", },
241	{},
242};
243MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match);
244
245static struct platform_driver bcm2835_wdt_driver = {
246	.probe		= bcm2835_wdt_probe,
247	.remove		= bcm2835_wdt_remove,
248	.shutdown	= bcm2835_wdt_shutdown,
249	.driver = {
250		.name =		"bcm2835-wdt",
251		.of_match_table = bcm2835_wdt_of_match,
252	},
253};
254module_platform_driver(bcm2835_wdt_driver);
255
256module_param(heartbeat, uint, 0);
257MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
258
259module_param(nowayout, bool, 0);
260MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
261				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
262
 
263MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
264MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
265MODULE_LICENSE("GPL");