xref: /openbmc/linux/drivers/watchdog/bcm7038_wdt.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
12e62c498SMarcus Folkesson // SPDX-License-Identifier: GPL-2.0+
27a3629feSJustin Chen /*
37a3629feSJustin Chen  * Copyright (C) 2015 Broadcom Corporation
47a3629feSJustin Chen  *
57a3629feSJustin Chen  */
67a3629feSJustin Chen 
77a3629feSJustin Chen #include <linux/clk.h>
87a3629feSJustin Chen #include <linux/init.h>
97a3629feSJustin Chen #include <linux/io.h>
107a3629feSJustin Chen #include <linux/module.h>
117a3629feSJustin Chen #include <linux/of.h>
127a3629feSJustin Chen #include <linux/platform_device.h>
13d6b9c679SFlorian Fainelli #include <linux/platform_data/bcm7038_wdt.h>
147a3629feSJustin Chen #include <linux/pm.h>
157a3629feSJustin Chen #include <linux/watchdog.h>
167a3629feSJustin Chen 
177a3629feSJustin Chen #define WDT_START_1		0xff00
187a3629feSJustin Chen #define WDT_START_2		0x00ff
197a3629feSJustin Chen #define WDT_STOP_1		0xee00
207a3629feSJustin Chen #define WDT_STOP_2		0x00ee
217a3629feSJustin Chen 
227a3629feSJustin Chen #define WDT_TIMEOUT_REG		0x0
237a3629feSJustin Chen #define WDT_CMD_REG		0x4
247a3629feSJustin Chen 
257a3629feSJustin Chen #define WDT_MIN_TIMEOUT		1 /* seconds */
267a3629feSJustin Chen #define WDT_DEFAULT_TIMEOUT	30 /* seconds */
277a3629feSJustin Chen #define WDT_DEFAULT_RATE	27000000
287a3629feSJustin Chen 
297a3629feSJustin Chen struct bcm7038_watchdog {
307a3629feSJustin Chen 	void __iomem		*base;
317a3629feSJustin Chen 	struct watchdog_device	wdd;
327a3629feSJustin Chen 	u32			rate;
337a3629feSJustin Chen 	struct clk		*clk;
347a3629feSJustin Chen };
357a3629feSJustin Chen 
367a3629feSJustin Chen static bool nowayout = WATCHDOG_NOWAYOUT;
377a3629feSJustin Chen 
bcm7038_wdt_write(u32 value,void __iomem * addr)38e379c219SÁlvaro Fernández Rojas static inline void bcm7038_wdt_write(u32 value, void __iomem *addr)
39e379c219SÁlvaro Fernández Rojas {
40e379c219SÁlvaro Fernández Rojas 	/* MIPS chips strapped for BE will automagically configure the
41e379c219SÁlvaro Fernández Rojas 	 * peripheral registers for CPU-native byte order.
42e379c219SÁlvaro Fernández Rojas 	 */
43e379c219SÁlvaro Fernández Rojas 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
44e379c219SÁlvaro Fernández Rojas 		__raw_writel(value, addr);
45e379c219SÁlvaro Fernández Rojas 	else
46e379c219SÁlvaro Fernández Rojas 		writel_relaxed(value, addr);
47e379c219SÁlvaro Fernández Rojas }
48e379c219SÁlvaro Fernández Rojas 
bcm7038_wdt_read(void __iomem * addr)49e379c219SÁlvaro Fernández Rojas static inline u32 bcm7038_wdt_read(void __iomem *addr)
50e379c219SÁlvaro Fernández Rojas {
51e379c219SÁlvaro Fernández Rojas 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
52e379c219SÁlvaro Fernández Rojas 		return __raw_readl(addr);
53e379c219SÁlvaro Fernández Rojas 	else
54e379c219SÁlvaro Fernández Rojas 		return readl_relaxed(addr);
55e379c219SÁlvaro Fernández Rojas }
56e379c219SÁlvaro Fernández Rojas 
bcm7038_wdt_set_timeout_reg(struct watchdog_device * wdog)577a3629feSJustin Chen static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
587a3629feSJustin Chen {
597a3629feSJustin Chen 	struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
607a3629feSJustin Chen 	u32 timeout;
617a3629feSJustin Chen 
627a3629feSJustin Chen 	timeout = wdt->rate * wdog->timeout;
637a3629feSJustin Chen 
64e379c219SÁlvaro Fernández Rojas 	bcm7038_wdt_write(timeout, wdt->base + WDT_TIMEOUT_REG);
657a3629feSJustin Chen }
667a3629feSJustin Chen 
bcm7038_wdt_ping(struct watchdog_device * wdog)677a3629feSJustin Chen static int bcm7038_wdt_ping(struct watchdog_device *wdog)
687a3629feSJustin Chen {
697a3629feSJustin Chen 	struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
707a3629feSJustin Chen 
71e379c219SÁlvaro Fernández Rojas 	bcm7038_wdt_write(WDT_START_1, wdt->base + WDT_CMD_REG);
72e379c219SÁlvaro Fernández Rojas 	bcm7038_wdt_write(WDT_START_2, wdt->base + WDT_CMD_REG);
737a3629feSJustin Chen 
747a3629feSJustin Chen 	return 0;
757a3629feSJustin Chen }
767a3629feSJustin Chen 
bcm7038_wdt_start(struct watchdog_device * wdog)777a3629feSJustin Chen static int bcm7038_wdt_start(struct watchdog_device *wdog)
787a3629feSJustin Chen {
797a3629feSJustin Chen 	bcm7038_wdt_set_timeout_reg(wdog);
807a3629feSJustin Chen 	bcm7038_wdt_ping(wdog);
817a3629feSJustin Chen 
827a3629feSJustin Chen 	return 0;
837a3629feSJustin Chen }
847a3629feSJustin Chen 
bcm7038_wdt_stop(struct watchdog_device * wdog)857a3629feSJustin Chen static int bcm7038_wdt_stop(struct watchdog_device *wdog)
867a3629feSJustin Chen {
877a3629feSJustin Chen 	struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
887a3629feSJustin Chen 
89e379c219SÁlvaro Fernández Rojas 	bcm7038_wdt_write(WDT_STOP_1, wdt->base + WDT_CMD_REG);
90e379c219SÁlvaro Fernández Rojas 	bcm7038_wdt_write(WDT_STOP_2, wdt->base + WDT_CMD_REG);
917a3629feSJustin Chen 
927a3629feSJustin Chen 	return 0;
937a3629feSJustin Chen }
947a3629feSJustin Chen 
bcm7038_wdt_set_timeout(struct watchdog_device * wdog,unsigned int t)957a3629feSJustin Chen static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
967a3629feSJustin Chen 				   unsigned int t)
977a3629feSJustin Chen {
987a3629feSJustin Chen 	/* Can't modify timeout value if watchdog timer is running */
997a3629feSJustin Chen 	bcm7038_wdt_stop(wdog);
1007a3629feSJustin Chen 	wdog->timeout = t;
1017a3629feSJustin Chen 	bcm7038_wdt_start(wdog);
1027a3629feSJustin Chen 
1037a3629feSJustin Chen 	return 0;
1047a3629feSJustin Chen }
1057a3629feSJustin Chen 
bcm7038_wdt_get_timeleft(struct watchdog_device * wdog)1067a3629feSJustin Chen static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
1077a3629feSJustin Chen {
1087a3629feSJustin Chen 	struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
1097a3629feSJustin Chen 	u32 time_left;
1107a3629feSJustin Chen 
111e379c219SÁlvaro Fernández Rojas 	time_left = bcm7038_wdt_read(wdt->base + WDT_CMD_REG);
1127a3629feSJustin Chen 
1137a3629feSJustin Chen 	return time_left / wdt->rate;
1147a3629feSJustin Chen }
1157a3629feSJustin Chen 
1166c368932SBhumika Goyal static const struct watchdog_info bcm7038_wdt_info = {
1177a3629feSJustin Chen 	.identity	= "Broadcom BCM7038 Watchdog Timer",
1187a3629feSJustin Chen 	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
1197a3629feSJustin Chen 				WDIOF_MAGICCLOSE
1207a3629feSJustin Chen };
1217a3629feSJustin Chen 
12285f15cfcSJulia Lawall static const struct watchdog_ops bcm7038_wdt_ops = {
1237a3629feSJustin Chen 	.owner		= THIS_MODULE,
1247a3629feSJustin Chen 	.start		= bcm7038_wdt_start,
1257a3629feSJustin Chen 	.stop		= bcm7038_wdt_stop,
1267a3629feSJustin Chen 	.set_timeout	= bcm7038_wdt_set_timeout,
1277a3629feSJustin Chen 	.get_timeleft	= bcm7038_wdt_get_timeleft,
1287a3629feSJustin Chen };
1297a3629feSJustin Chen 
bcm7038_wdt_probe(struct platform_device * pdev)1307a3629feSJustin Chen static int bcm7038_wdt_probe(struct platform_device *pdev)
1317a3629feSJustin Chen {
132d6b9c679SFlorian Fainelli 	struct bcm7038_wdt_platform_data *pdata = pdev->dev.platform_data;
1337a3629feSJustin Chen 	struct device *dev = &pdev->dev;
1347a3629feSJustin Chen 	struct bcm7038_watchdog *wdt;
135d6b9c679SFlorian Fainelli 	const char *clk_name = NULL;
1367a3629feSJustin Chen 	int err;
1377a3629feSJustin Chen 
1387a3629feSJustin Chen 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
1397a3629feSJustin Chen 	if (!wdt)
1407a3629feSJustin Chen 		return -ENOMEM;
1417a3629feSJustin Chen 
1427a3629feSJustin Chen 	platform_set_drvdata(pdev, wdt);
1437a3629feSJustin Chen 
1440f0a6a28SGuenter Roeck 	wdt->base = devm_platform_ioremap_resource(pdev, 0);
1457a3629feSJustin Chen 	if (IS_ERR(wdt->base))
1467a3629feSJustin Chen 		return PTR_ERR(wdt->base);
1477a3629feSJustin Chen 
148d6b9c679SFlorian Fainelli 	if (pdata && pdata->clk_name)
149d6b9c679SFlorian Fainelli 		clk_name = pdata->clk_name;
150d6b9c679SFlorian Fainelli 
151*be80ae3dSChristophe JAILLET 	wdt->clk = devm_clk_get_enabled(dev, clk_name);
1527a3629feSJustin Chen 	/* If unable to get clock, use default frequency */
1537a3629feSJustin Chen 	if (!IS_ERR(wdt->clk)) {
1547a3629feSJustin Chen 		wdt->rate = clk_get_rate(wdt->clk);
1557a3629feSJustin Chen 		/* Prevent divide-by-zero exception */
1567a3629feSJustin Chen 		if (!wdt->rate)
1577a3629feSJustin Chen 			wdt->rate = WDT_DEFAULT_RATE;
1587a3629feSJustin Chen 	} else {
1597a3629feSJustin Chen 		wdt->rate = WDT_DEFAULT_RATE;
1607a3629feSJustin Chen 		wdt->clk = NULL;
1617a3629feSJustin Chen 	}
1627a3629feSJustin Chen 
1637a3629feSJustin Chen 	wdt->wdd.info		= &bcm7038_wdt_info;
1647a3629feSJustin Chen 	wdt->wdd.ops		= &bcm7038_wdt_ops;
1657a3629feSJustin Chen 	wdt->wdd.min_timeout	= WDT_MIN_TIMEOUT;
1667a3629feSJustin Chen 	wdt->wdd.timeout	= WDT_DEFAULT_TIMEOUT;
1677a3629feSJustin Chen 	wdt->wdd.max_timeout	= 0xffffffff / wdt->rate;
1687a3629feSJustin Chen 	wdt->wdd.parent		= dev;
1697a3629feSJustin Chen 	watchdog_set_drvdata(&wdt->wdd, wdt);
1707a3629feSJustin Chen 
17169656dcdSGuenter Roeck 	watchdog_stop_on_reboot(&wdt->wdd);
17269656dcdSGuenter Roeck 	watchdog_stop_on_unregister(&wdt->wdd);
17369656dcdSGuenter Roeck 	err = devm_watchdog_register_device(dev, &wdt->wdd);
174f5429895SWolfram Sang 	if (err)
1757a3629feSJustin Chen 		return err;
1767a3629feSJustin Chen 
1777a3629feSJustin Chen 	dev_info(dev, "Registered BCM7038 Watchdog\n");
1787a3629feSJustin Chen 
1797a3629feSJustin Chen 	return 0;
1807a3629feSJustin Chen }
1817a3629feSJustin Chen 
bcm7038_wdt_suspend(struct device * dev)1827a3629feSJustin Chen static int bcm7038_wdt_suspend(struct device *dev)
1837a3629feSJustin Chen {
1847a3629feSJustin Chen 	struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
1857a3629feSJustin Chen 
1867a3629feSJustin Chen 	if (watchdog_active(&wdt->wdd))
1877a3629feSJustin Chen 		return bcm7038_wdt_stop(&wdt->wdd);
1887a3629feSJustin Chen 
1897a3629feSJustin Chen 	return 0;
1907a3629feSJustin Chen }
1917a3629feSJustin Chen 
bcm7038_wdt_resume(struct device * dev)1927a3629feSJustin Chen static int bcm7038_wdt_resume(struct device *dev)
1937a3629feSJustin Chen {
1947a3629feSJustin Chen 	struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
1957a3629feSJustin Chen 
1967a3629feSJustin Chen 	if (watchdog_active(&wdt->wdd))
1977a3629feSJustin Chen 		return bcm7038_wdt_start(&wdt->wdd);
1987a3629feSJustin Chen 
1997a3629feSJustin Chen 	return 0;
2007a3629feSJustin Chen }
2017a3629feSJustin Chen 
2023b956c0aSPaul Cercueil static DEFINE_SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops,
2033b956c0aSPaul Cercueil 				bcm7038_wdt_suspend, bcm7038_wdt_resume);
2047a3629feSJustin Chen 
2057a3629feSJustin Chen static const struct of_device_id bcm7038_wdt_match[] = {
2062dd441f1SRafał Miłecki 	{ .compatible = "brcm,bcm6345-wdt" },
2077a3629feSJustin Chen 	{ .compatible = "brcm,bcm7038-wdt" },
2087a3629feSJustin Chen 	{},
2097a3629feSJustin Chen };
21057d77c62SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
2117a3629feSJustin Chen 
212e764faefSFlorian Fainelli static const struct platform_device_id bcm7038_wdt_devtype[] = {
213e764faefSFlorian Fainelli 	{ .name = "bcm63xx-wdt" },
214e764faefSFlorian Fainelli 	{ /* sentinel */ },
215e764faefSFlorian Fainelli };
216e764faefSFlorian Fainelli MODULE_DEVICE_TABLE(platform, bcm7038_wdt_devtype);
217e764faefSFlorian Fainelli 
2187a3629feSJustin Chen static struct platform_driver bcm7038_wdt_driver = {
2197a3629feSJustin Chen 	.probe		= bcm7038_wdt_probe,
220e764faefSFlorian Fainelli 	.id_table	= bcm7038_wdt_devtype,
2217a3629feSJustin Chen 	.driver		= {
2227a3629feSJustin Chen 		.name		= "bcm7038-wdt",
2237a3629feSJustin Chen 		.of_match_table	= bcm7038_wdt_match,
2243b956c0aSPaul Cercueil 		.pm		= pm_sleep_ptr(&bcm7038_wdt_pm_ops),
2257a3629feSJustin Chen 	}
2267a3629feSJustin Chen };
2277a3629feSJustin Chen module_platform_driver(bcm7038_wdt_driver);
2287a3629feSJustin Chen 
2297a3629feSJustin Chen module_param(nowayout, bool, 0);
2307a3629feSJustin Chen MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
2317a3629feSJustin Chen 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
2322e62c498SMarcus Folkesson MODULE_LICENSE("GPL");
2337a3629feSJustin Chen MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
2347a3629feSJustin Chen MODULE_AUTHOR("Justin Chen");
235