16f702fceSAtsushi Nemoto /* 26f702fceSAtsushi Nemoto * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs 36f702fceSAtsushi Nemoto * 46f702fceSAtsushi Nemoto * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp> 56f702fceSAtsushi Nemoto * 66f702fceSAtsushi Nemoto * This program is free software; you can redistribute it and/or modify 76f702fceSAtsushi Nemoto * it under the terms of the GNU General Public License version 2 as 86f702fceSAtsushi Nemoto * published by the Free Software Foundation. 96f702fceSAtsushi Nemoto */ 1027c766aaSJoe Perches 1127c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 1227c766aaSJoe Perches 136f702fceSAtsushi Nemoto #include <linux/module.h> 146f702fceSAtsushi Nemoto #include <linux/moduleparam.h> 156f702fceSAtsushi Nemoto #include <linux/types.h> 166f702fceSAtsushi Nemoto #include <linux/watchdog.h> 176f702fceSAtsushi Nemoto #include <linux/init.h> 186f702fceSAtsushi Nemoto #include <linux/platform_device.h> 196f702fceSAtsushi Nemoto #include <linux/clk.h> 206f702fceSAtsushi Nemoto #include <linux/err.h> 216f702fceSAtsushi Nemoto #include <linux/io.h> 226f702fceSAtsushi Nemoto #include <asm/txx9tmr.h> 236f702fceSAtsushi Nemoto 24b92c803eSWim Van Sebroeck #define WD_TIMER_CCD 7 /* 1/256 */ 25b92c803eSWim Van Sebroeck #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD)) 26b92c803eSWim Van Sebroeck #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK) 276f702fceSAtsushi Nemoto #define TIMER_MARGIN 60 /* Default is 60 seconds */ 286f702fceSAtsushi Nemoto 29b92c803eSWim Van Sebroeck static unsigned int timeout = TIMER_MARGIN; /* in seconds */ 30b92c803eSWim Van Sebroeck module_param(timeout, uint, 0); 316f702fceSAtsushi Nemoto MODULE_PARM_DESC(timeout, 326f702fceSAtsushi Nemoto "Watchdog timeout in seconds. " 336f702fceSAtsushi Nemoto "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), " 346f702fceSAtsushi Nemoto "default=" __MODULE_STRING(TIMER_MARGIN) ")"); 356f702fceSAtsushi Nemoto 3686a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT; 3786a1e189SWim Van Sebroeck module_param(nowayout, bool, 0); 386f702fceSAtsushi Nemoto MODULE_PARM_DESC(nowayout, 396f702fceSAtsushi Nemoto "Watchdog cannot be stopped once started " 406f702fceSAtsushi Nemoto "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 416f702fceSAtsushi Nemoto 426f702fceSAtsushi Nemoto static struct txx9_tmr_reg __iomem *txx9wdt_reg; 436f702fceSAtsushi Nemoto static struct clk *txx9_imclk; 44f8494e06SAdrian Bunk static DEFINE_SPINLOCK(txx9_lock); 456f702fceSAtsushi Nemoto 46d6245842SAxel Lin static int txx9wdt_ping(struct watchdog_device *wdt_dev) 476f702fceSAtsushi Nemoto { 488dc244f7SAlan Cox spin_lock(&txx9_lock); 496f702fceSAtsushi Nemoto __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); 508dc244f7SAlan Cox spin_unlock(&txx9_lock); 51d6245842SAxel Lin return 0; 526f702fceSAtsushi Nemoto } 536f702fceSAtsushi Nemoto 54d6245842SAxel Lin static int txx9wdt_start(struct watchdog_device *wdt_dev) 556f702fceSAtsushi Nemoto { 568dc244f7SAlan Cox spin_lock(&txx9_lock); 57b92c803eSWim Van Sebroeck __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra); 586f702fceSAtsushi Nemoto __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr); 596f702fceSAtsushi Nemoto __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */ 606f702fceSAtsushi Nemoto __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG, 616f702fceSAtsushi Nemoto &txx9wdt_reg->tcr); 626f702fceSAtsushi Nemoto __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); 638dc244f7SAlan Cox spin_unlock(&txx9_lock); 64d6245842SAxel Lin return 0; 656f702fceSAtsushi Nemoto } 666f702fceSAtsushi Nemoto 67d6245842SAxel Lin static int txx9wdt_stop(struct watchdog_device *wdt_dev) 686f702fceSAtsushi Nemoto { 698dc244f7SAlan Cox spin_lock(&txx9_lock); 706f702fceSAtsushi Nemoto __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr); 716f702fceSAtsushi Nemoto __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE, 726f702fceSAtsushi Nemoto &txx9wdt_reg->tcr); 738dc244f7SAlan Cox spin_unlock(&txx9_lock); 746f702fceSAtsushi Nemoto return 0; 756f702fceSAtsushi Nemoto } 766f702fceSAtsushi Nemoto 77d6245842SAxel Lin static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev, 78d6245842SAxel Lin unsigned int new_timeout) 796f702fceSAtsushi Nemoto { 80b92c803eSWim Van Sebroeck wdt_dev->timeout = new_timeout; 81d6245842SAxel Lin txx9wdt_stop(wdt_dev); 82d6245842SAxel Lin txx9wdt_start(wdt_dev); 83d6245842SAxel Lin return 0; 846f702fceSAtsushi Nemoto } 856f702fceSAtsushi Nemoto 86d6245842SAxel Lin static const struct watchdog_info txx9wdt_info = { 87d6245842SAxel Lin .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, 886f702fceSAtsushi Nemoto .identity = "Hardware Watchdog for TXx9", 896f702fceSAtsushi Nemoto }; 906f702fceSAtsushi Nemoto 91d6245842SAxel Lin static const struct watchdog_ops txx9wdt_ops = { 926f702fceSAtsushi Nemoto .owner = THIS_MODULE, 93d6245842SAxel Lin .start = txx9wdt_start, 94d6245842SAxel Lin .stop = txx9wdt_stop, 95d6245842SAxel Lin .ping = txx9wdt_ping, 96d6245842SAxel Lin .set_timeout = txx9wdt_set_timeout, 976f702fceSAtsushi Nemoto }; 986f702fceSAtsushi Nemoto 99d6245842SAxel Lin static struct watchdog_device txx9wdt = { 100d6245842SAxel Lin .info = &txx9wdt_info, 101d6245842SAxel Lin .ops = &txx9wdt_ops, 1026f702fceSAtsushi Nemoto }; 1036f702fceSAtsushi Nemoto 1046f702fceSAtsushi Nemoto static int __init txx9wdt_probe(struct platform_device *dev) 1056f702fceSAtsushi Nemoto { 1066f702fceSAtsushi Nemoto int ret; 1076f702fceSAtsushi Nemoto 1086f702fceSAtsushi Nemoto txx9_imclk = clk_get(NULL, "imbus_clk"); 1096f702fceSAtsushi Nemoto if (IS_ERR(txx9_imclk)) { 1106f702fceSAtsushi Nemoto ret = PTR_ERR(txx9_imclk); 1116f702fceSAtsushi Nemoto txx9_imclk = NULL; 1126f702fceSAtsushi Nemoto goto exit; 1136f702fceSAtsushi Nemoto } 114bfb1f46fSGeert Uytterhoeven ret = clk_prepare_enable(txx9_imclk); 1156f702fceSAtsushi Nemoto if (ret) { 1166f702fceSAtsushi Nemoto clk_put(txx9_imclk); 1176f702fceSAtsushi Nemoto txx9_imclk = NULL; 1186f702fceSAtsushi Nemoto goto exit; 1196f702fceSAtsushi Nemoto } 1206f702fceSAtsushi Nemoto 121*0f0a6a28SGuenter Roeck txx9wdt_reg = devm_platform_ioremap_resource(dev, 0); 1224c271bb6SThierry Reding if (IS_ERR(txx9wdt_reg)) { 1234c271bb6SThierry Reding ret = PTR_ERR(txx9wdt_reg); 1246f702fceSAtsushi Nemoto goto exit; 1256f702fceSAtsushi Nemoto } 1266f702fceSAtsushi Nemoto 127b92c803eSWim Van Sebroeck if (timeout < 1 || timeout > WD_MAX_TIMEOUT) 128b92c803eSWim Van Sebroeck timeout = TIMER_MARGIN; 129b92c803eSWim Van Sebroeck txx9wdt.timeout = timeout; 130d6245842SAxel Lin txx9wdt.min_timeout = 1; 131d6245842SAxel Lin txx9wdt.max_timeout = WD_MAX_TIMEOUT; 1326551881cSPratyush Anand txx9wdt.parent = &dev->dev; 133d6245842SAxel Lin watchdog_set_nowayout(&txx9wdt, nowayout); 134d6245842SAxel Lin 135d6245842SAxel Lin ret = watchdog_register_device(&txx9wdt); 136d6245842SAxel Lin if (ret) 137d6245842SAxel Lin goto exit; 138d6245842SAxel Lin 13927c766aaSJoe Perches pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n", 1406f702fceSAtsushi Nemoto timeout, WD_MAX_TIMEOUT, nowayout); 1416f702fceSAtsushi Nemoto 1426f702fceSAtsushi Nemoto return 0; 1436f702fceSAtsushi Nemoto exit: 1446f702fceSAtsushi Nemoto if (txx9_imclk) { 145bfb1f46fSGeert Uytterhoeven clk_disable_unprepare(txx9_imclk); 1466f702fceSAtsushi Nemoto clk_put(txx9_imclk); 1476f702fceSAtsushi Nemoto } 1486f702fceSAtsushi Nemoto return ret; 1496f702fceSAtsushi Nemoto } 1506f702fceSAtsushi Nemoto 1516f702fceSAtsushi Nemoto static int __exit txx9wdt_remove(struct platform_device *dev) 1526f702fceSAtsushi Nemoto { 153d6245842SAxel Lin watchdog_unregister_device(&txx9wdt); 154bfb1f46fSGeert Uytterhoeven clk_disable_unprepare(txx9_imclk); 1556f702fceSAtsushi Nemoto clk_put(txx9_imclk); 1566f702fceSAtsushi Nemoto return 0; 1576f702fceSAtsushi Nemoto } 1586f702fceSAtsushi Nemoto 159168b5251SWim Van Sebroeck static void txx9wdt_shutdown(struct platform_device *dev) 160168b5251SWim Van Sebroeck { 161d6245842SAxel Lin txx9wdt_stop(&txx9wdt); 162168b5251SWim Van Sebroeck } 163168b5251SWim Van Sebroeck 1646f702fceSAtsushi Nemoto static struct platform_driver txx9wdt_driver = { 1656f702fceSAtsushi Nemoto .remove = __exit_p(txx9wdt_remove), 166168b5251SWim Van Sebroeck .shutdown = txx9wdt_shutdown, 1676f702fceSAtsushi Nemoto .driver = { 1686f702fceSAtsushi Nemoto .name = "txx9wdt", 1696f702fceSAtsushi Nemoto }, 1706f702fceSAtsushi Nemoto }; 1716f702fceSAtsushi Nemoto 1721cb9204cSFabio Porcedda module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe); 1736f702fceSAtsushi Nemoto 1746f702fceSAtsushi Nemoto MODULE_DESCRIPTION("TXx9 Watchdog Driver"); 1756f702fceSAtsushi Nemoto MODULE_LICENSE("GPL"); 176f37d193cSKay Sievers MODULE_ALIAS("platform:txx9wdt"); 177