1a44a4553SMatthias Brugger /* 2a44a4553SMatthias Brugger * Mediatek Watchdog Driver 3a44a4553SMatthias Brugger * 4a44a4553SMatthias Brugger * Copyright (C) 2014 Matthias Brugger 5a44a4553SMatthias Brugger * 6a44a4553SMatthias Brugger * Matthias Brugger <matthias.bgg@gmail.com> 7a44a4553SMatthias Brugger * 8a44a4553SMatthias Brugger * This program is free software; you can redistribute it and/or modify 9a44a4553SMatthias Brugger * it under the terms of the GNU General Public License as published by 10a44a4553SMatthias Brugger * the Free Software Foundation; either version 2 of the License, or 11a44a4553SMatthias Brugger * (at your option) any later version. 12a44a4553SMatthias Brugger * 13a44a4553SMatthias Brugger * This program is distributed in the hope that it will be useful, 14a44a4553SMatthias Brugger * but WITHOUT ANY WARRANTY; without even the implied warranty of 15a44a4553SMatthias Brugger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16a44a4553SMatthias Brugger * GNU General Public License for more details. 17a44a4553SMatthias Brugger * 18a44a4553SMatthias Brugger * Based on sunxi_wdt.c 19a44a4553SMatthias Brugger */ 20a44a4553SMatthias Brugger 21a44a4553SMatthias Brugger #include <linux/err.h> 22a44a4553SMatthias Brugger #include <linux/init.h> 23a44a4553SMatthias Brugger #include <linux/io.h> 24a44a4553SMatthias Brugger #include <linux/kernel.h> 25a44a4553SMatthias Brugger #include <linux/module.h> 26a44a4553SMatthias Brugger #include <linux/moduleparam.h> 27a44a4553SMatthias Brugger #include <linux/of.h> 28a44a4553SMatthias Brugger #include <linux/platform_device.h> 29a44a4553SMatthias Brugger #include <linux/types.h> 30a44a4553SMatthias Brugger #include <linux/watchdog.h> 31a44a4553SMatthias Brugger #include <linux/notifier.h> 32a44a4553SMatthias Brugger #include <linux/reboot.h> 33a44a4553SMatthias Brugger #include <linux/delay.h> 34a44a4553SMatthias Brugger 35a44a4553SMatthias Brugger #define WDT_MAX_TIMEOUT 31 36a44a4553SMatthias Brugger #define WDT_MIN_TIMEOUT 1 37a44a4553SMatthias Brugger #define WDT_LENGTH_TIMEOUT(n) ((n) << 5) 38a44a4553SMatthias Brugger 39a44a4553SMatthias Brugger #define WDT_LENGTH 0x04 40a44a4553SMatthias Brugger #define WDT_LENGTH_KEY 0x8 41a44a4553SMatthias Brugger 42a44a4553SMatthias Brugger #define WDT_RST 0x08 43a44a4553SMatthias Brugger #define WDT_RST_RELOAD 0x1971 44a44a4553SMatthias Brugger 45a44a4553SMatthias Brugger #define WDT_MODE 0x00 46a44a4553SMatthias Brugger #define WDT_MODE_EN (1 << 0) 47a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_LOW (0 << 1) 48a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_HIGH (1 << 1) 49a44a4553SMatthias Brugger #define WDT_MODE_EXRST_EN (1 << 2) 50a44a4553SMatthias Brugger #define WDT_MODE_IRQ_EN (1 << 3) 51a44a4553SMatthias Brugger #define WDT_MODE_AUTO_START (1 << 4) 52a44a4553SMatthias Brugger #define WDT_MODE_DUAL_EN (1 << 6) 53a44a4553SMatthias Brugger #define WDT_MODE_KEY 0x22000000 54a44a4553SMatthias Brugger 55a44a4553SMatthias Brugger #define WDT_SWRST 0x14 56a44a4553SMatthias Brugger #define WDT_SWRST_KEY 0x1209 57a44a4553SMatthias Brugger 58a44a4553SMatthias Brugger #define DRV_NAME "mtk-wdt" 59a44a4553SMatthias Brugger #define DRV_VERSION "1.0" 60a44a4553SMatthias Brugger 61a44a4553SMatthias Brugger static bool nowayout = WATCHDOG_NOWAYOUT; 62a44a4553SMatthias Brugger static unsigned int timeout = WDT_MAX_TIMEOUT; 63a44a4553SMatthias Brugger 64a44a4553SMatthias Brugger struct mtk_wdt_dev { 65a44a4553SMatthias Brugger struct watchdog_device wdt_dev; 66a44a4553SMatthias Brugger void __iomem *wdt_base; 67a44a4553SMatthias Brugger struct notifier_block restart_handler; 68a44a4553SMatthias Brugger }; 69a44a4553SMatthias Brugger 70a44a4553SMatthias Brugger static int mtk_reset_handler(struct notifier_block *this, unsigned long mode, 71a44a4553SMatthias Brugger void *cmd) 72a44a4553SMatthias Brugger { 73a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt; 74a44a4553SMatthias Brugger void __iomem *wdt_base; 75a44a4553SMatthias Brugger 76a44a4553SMatthias Brugger mtk_wdt = container_of(this, struct mtk_wdt_dev, restart_handler); 77a44a4553SMatthias Brugger wdt_base = mtk_wdt->wdt_base; 78a44a4553SMatthias Brugger 79a44a4553SMatthias Brugger while (1) { 80a44a4553SMatthias Brugger writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST); 81a44a4553SMatthias Brugger mdelay(5); 82a44a4553SMatthias Brugger } 83a44a4553SMatthias Brugger 84a44a4553SMatthias Brugger return NOTIFY_DONE; 85a44a4553SMatthias Brugger } 86a44a4553SMatthias Brugger 87a44a4553SMatthias Brugger static int mtk_wdt_ping(struct watchdog_device *wdt_dev) 88a44a4553SMatthias Brugger { 89a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 90a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 91a44a4553SMatthias Brugger 92a44a4553SMatthias Brugger iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST); 93a44a4553SMatthias Brugger 94a44a4553SMatthias Brugger return 0; 95a44a4553SMatthias Brugger } 96a44a4553SMatthias Brugger 97a44a4553SMatthias Brugger static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev, 98a44a4553SMatthias Brugger unsigned int timeout) 99a44a4553SMatthias Brugger { 100a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 101a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 102a44a4553SMatthias Brugger u32 reg; 103a44a4553SMatthias Brugger 104a44a4553SMatthias Brugger wdt_dev->timeout = timeout; 105a44a4553SMatthias Brugger 106a44a4553SMatthias Brugger /* 107a44a4553SMatthias Brugger * One bit is the value of 512 ticks 108a44a4553SMatthias Brugger * The clock has 32 KHz 109a44a4553SMatthias Brugger */ 110a44a4553SMatthias Brugger reg = WDT_LENGTH_TIMEOUT(timeout << 6) | WDT_LENGTH_KEY; 111a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_LENGTH); 112a44a4553SMatthias Brugger 113a44a4553SMatthias Brugger mtk_wdt_ping(wdt_dev); 114a44a4553SMatthias Brugger 115a44a4553SMatthias Brugger return 0; 116a44a4553SMatthias Brugger } 117a44a4553SMatthias Brugger 118a44a4553SMatthias Brugger static int mtk_wdt_stop(struct watchdog_device *wdt_dev) 119a44a4553SMatthias Brugger { 120a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 121a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 122a44a4553SMatthias Brugger u32 reg; 123a44a4553SMatthias Brugger 124a44a4553SMatthias Brugger reg = readl(wdt_base + WDT_MODE); 125a44a4553SMatthias Brugger reg &= ~WDT_MODE_EN; 126*5da2bf1aSNicolas Boichat reg |= WDT_MODE_KEY; 127a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_MODE); 128a44a4553SMatthias Brugger 129a44a4553SMatthias Brugger return 0; 130a44a4553SMatthias Brugger } 131a44a4553SMatthias Brugger 132a44a4553SMatthias Brugger static int mtk_wdt_start(struct watchdog_device *wdt_dev) 133a44a4553SMatthias Brugger { 134a44a4553SMatthias Brugger u32 reg; 135a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 136a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 1379ffd906dSDan Carpenter int ret; 138a44a4553SMatthias Brugger 139a44a4553SMatthias Brugger ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout); 140a44a4553SMatthias Brugger if (ret < 0) 141a44a4553SMatthias Brugger return ret; 142a44a4553SMatthias Brugger 143a44a4553SMatthias Brugger reg = ioread32(wdt_base + WDT_MODE); 144a44a4553SMatthias Brugger reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN); 145a44a4553SMatthias Brugger reg |= (WDT_MODE_EN | WDT_MODE_KEY); 146a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_MODE); 147a44a4553SMatthias Brugger 148a44a4553SMatthias Brugger return 0; 149a44a4553SMatthias Brugger } 150a44a4553SMatthias Brugger 151a44a4553SMatthias Brugger static const struct watchdog_info mtk_wdt_info = { 152a44a4553SMatthias Brugger .identity = DRV_NAME, 153a44a4553SMatthias Brugger .options = WDIOF_SETTIMEOUT | 154a44a4553SMatthias Brugger WDIOF_KEEPALIVEPING | 155a44a4553SMatthias Brugger WDIOF_MAGICCLOSE, 156a44a4553SMatthias Brugger }; 157a44a4553SMatthias Brugger 158a44a4553SMatthias Brugger static const struct watchdog_ops mtk_wdt_ops = { 159a44a4553SMatthias Brugger .owner = THIS_MODULE, 160a44a4553SMatthias Brugger .start = mtk_wdt_start, 161a44a4553SMatthias Brugger .stop = mtk_wdt_stop, 162a44a4553SMatthias Brugger .ping = mtk_wdt_ping, 163a44a4553SMatthias Brugger .set_timeout = mtk_wdt_set_timeout, 164a44a4553SMatthias Brugger }; 165a44a4553SMatthias Brugger 166a44a4553SMatthias Brugger static int mtk_wdt_probe(struct platform_device *pdev) 167a44a4553SMatthias Brugger { 168a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt; 169a44a4553SMatthias Brugger struct resource *res; 170a44a4553SMatthias Brugger int err; 171a44a4553SMatthias Brugger 172a44a4553SMatthias Brugger mtk_wdt = devm_kzalloc(&pdev->dev, sizeof(*mtk_wdt), GFP_KERNEL); 173a44a4553SMatthias Brugger if (!mtk_wdt) 174a44a4553SMatthias Brugger return -ENOMEM; 175a44a4553SMatthias Brugger 176a44a4553SMatthias Brugger platform_set_drvdata(pdev, mtk_wdt); 177a44a4553SMatthias Brugger 178a44a4553SMatthias Brugger res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 179a44a4553SMatthias Brugger mtk_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res); 180a44a4553SMatthias Brugger if (IS_ERR(mtk_wdt->wdt_base)) 181a44a4553SMatthias Brugger return PTR_ERR(mtk_wdt->wdt_base); 182a44a4553SMatthias Brugger 183a44a4553SMatthias Brugger mtk_wdt->wdt_dev.info = &mtk_wdt_info; 184a44a4553SMatthias Brugger mtk_wdt->wdt_dev.ops = &mtk_wdt_ops; 185a44a4553SMatthias Brugger mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT; 186a44a4553SMatthias Brugger mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT; 187a44a4553SMatthias Brugger mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT; 188a44a4553SMatthias Brugger mtk_wdt->wdt_dev.parent = &pdev->dev; 189a44a4553SMatthias Brugger 190a44a4553SMatthias Brugger watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, &pdev->dev); 191a44a4553SMatthias Brugger watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout); 192a44a4553SMatthias Brugger 193a44a4553SMatthias Brugger watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt); 194a44a4553SMatthias Brugger 195a44a4553SMatthias Brugger mtk_wdt_stop(&mtk_wdt->wdt_dev); 196a44a4553SMatthias Brugger 197a44a4553SMatthias Brugger err = watchdog_register_device(&mtk_wdt->wdt_dev); 198a44a4553SMatthias Brugger if (unlikely(err)) 199a44a4553SMatthias Brugger return err; 200a44a4553SMatthias Brugger 201a44a4553SMatthias Brugger mtk_wdt->restart_handler.notifier_call = mtk_reset_handler; 202a44a4553SMatthias Brugger mtk_wdt->restart_handler.priority = 128; 203a44a4553SMatthias Brugger err = register_restart_handler(&mtk_wdt->restart_handler); 204a44a4553SMatthias Brugger if (err) 205a44a4553SMatthias Brugger dev_warn(&pdev->dev, 206a44a4553SMatthias Brugger "cannot register restart handler (err=%d)\n", err); 207a44a4553SMatthias Brugger 208a44a4553SMatthias Brugger dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n", 209a44a4553SMatthias Brugger mtk_wdt->wdt_dev.timeout, nowayout); 210a44a4553SMatthias Brugger 211a44a4553SMatthias Brugger return 0; 212a44a4553SMatthias Brugger } 213a44a4553SMatthias Brugger 2145724485bSGreta Zhang static void mtk_wdt_shutdown(struct platform_device *pdev) 2155724485bSGreta Zhang { 2165724485bSGreta Zhang struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev); 2175724485bSGreta Zhang 2185724485bSGreta Zhang if (watchdog_active(&mtk_wdt->wdt_dev)) 2195724485bSGreta Zhang mtk_wdt_stop(&mtk_wdt->wdt_dev); 2205724485bSGreta Zhang } 2215724485bSGreta Zhang 222a44a4553SMatthias Brugger static int mtk_wdt_remove(struct platform_device *pdev) 223a44a4553SMatthias Brugger { 224a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev); 225a44a4553SMatthias Brugger 226a44a4553SMatthias Brugger unregister_restart_handler(&mtk_wdt->restart_handler); 227a44a4553SMatthias Brugger 228a44a4553SMatthias Brugger watchdog_unregister_device(&mtk_wdt->wdt_dev); 229a44a4553SMatthias Brugger 230a44a4553SMatthias Brugger return 0; 231a44a4553SMatthias Brugger } 232a44a4553SMatthias Brugger 2339fab0692SGreta Zhang #ifdef CONFIG_PM_SLEEP 2349fab0692SGreta Zhang static int mtk_wdt_suspend(struct device *dev) 2359fab0692SGreta Zhang { 2369fab0692SGreta Zhang struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev); 2379fab0692SGreta Zhang 2389fab0692SGreta Zhang if (watchdog_active(&mtk_wdt->wdt_dev)) 2399fab0692SGreta Zhang mtk_wdt_stop(&mtk_wdt->wdt_dev); 2409fab0692SGreta Zhang 2419fab0692SGreta Zhang return 0; 2429fab0692SGreta Zhang } 2439fab0692SGreta Zhang 2449fab0692SGreta Zhang static int mtk_wdt_resume(struct device *dev) 2459fab0692SGreta Zhang { 2469fab0692SGreta Zhang struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev); 2479fab0692SGreta Zhang 2489fab0692SGreta Zhang if (watchdog_active(&mtk_wdt->wdt_dev)) { 2499fab0692SGreta Zhang mtk_wdt_start(&mtk_wdt->wdt_dev); 2509fab0692SGreta Zhang mtk_wdt_ping(&mtk_wdt->wdt_dev); 2519fab0692SGreta Zhang } 2529fab0692SGreta Zhang 2539fab0692SGreta Zhang return 0; 2549fab0692SGreta Zhang } 2559fab0692SGreta Zhang #endif 2569fab0692SGreta Zhang 257a44a4553SMatthias Brugger static const struct of_device_id mtk_wdt_dt_ids[] = { 258a44a4553SMatthias Brugger { .compatible = "mediatek,mt6589-wdt" }, 259a44a4553SMatthias Brugger { /* sentinel */ } 260a44a4553SMatthias Brugger }; 261a44a4553SMatthias Brugger MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids); 262a44a4553SMatthias Brugger 2639fab0692SGreta Zhang static const struct dev_pm_ops mtk_wdt_pm_ops = { 2649fab0692SGreta Zhang SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend, 2659fab0692SGreta Zhang mtk_wdt_resume) 2669fab0692SGreta Zhang }; 2679fab0692SGreta Zhang 268a44a4553SMatthias Brugger static struct platform_driver mtk_wdt_driver = { 269a44a4553SMatthias Brugger .probe = mtk_wdt_probe, 270a44a4553SMatthias Brugger .remove = mtk_wdt_remove, 2715724485bSGreta Zhang .shutdown = mtk_wdt_shutdown, 272a44a4553SMatthias Brugger .driver = { 273a44a4553SMatthias Brugger .name = DRV_NAME, 2749fab0692SGreta Zhang .pm = &mtk_wdt_pm_ops, 275a44a4553SMatthias Brugger .of_match_table = mtk_wdt_dt_ids, 276a44a4553SMatthias Brugger }, 277a44a4553SMatthias Brugger }; 278a44a4553SMatthias Brugger 279a44a4553SMatthias Brugger module_platform_driver(mtk_wdt_driver); 280a44a4553SMatthias Brugger 281a44a4553SMatthias Brugger module_param(timeout, uint, 0); 282a44a4553SMatthias Brugger MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds"); 283a44a4553SMatthias Brugger 284a44a4553SMatthias Brugger module_param(nowayout, bool, 0); 285a44a4553SMatthias Brugger MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" 286a44a4553SMatthias Brugger __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 287a44a4553SMatthias Brugger 288a44a4553SMatthias Brugger MODULE_LICENSE("GPL"); 289a44a4553SMatthias Brugger MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>"); 290a44a4553SMatthias Brugger MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver"); 291a44a4553SMatthias Brugger MODULE_VERSION(DRV_VERSION); 292