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; 126a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_MODE); 127a44a4553SMatthias Brugger 128a44a4553SMatthias Brugger return 0; 129a44a4553SMatthias Brugger } 130a44a4553SMatthias Brugger 131a44a4553SMatthias Brugger static int mtk_wdt_start(struct watchdog_device *wdt_dev) 132a44a4553SMatthias Brugger { 133a44a4553SMatthias Brugger u32 reg; 134a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 135a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 136*9ffd906dSDan Carpenter int ret; 137a44a4553SMatthias Brugger 138a44a4553SMatthias Brugger ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout); 139a44a4553SMatthias Brugger if (ret < 0) 140a44a4553SMatthias Brugger return ret; 141a44a4553SMatthias Brugger 142a44a4553SMatthias Brugger reg = ioread32(wdt_base + WDT_MODE); 143a44a4553SMatthias Brugger reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN); 144a44a4553SMatthias Brugger reg |= (WDT_MODE_EN | WDT_MODE_KEY); 145a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_MODE); 146a44a4553SMatthias Brugger 147a44a4553SMatthias Brugger return 0; 148a44a4553SMatthias Brugger } 149a44a4553SMatthias Brugger 150a44a4553SMatthias Brugger static const struct watchdog_info mtk_wdt_info = { 151a44a4553SMatthias Brugger .identity = DRV_NAME, 152a44a4553SMatthias Brugger .options = WDIOF_SETTIMEOUT | 153a44a4553SMatthias Brugger WDIOF_KEEPALIVEPING | 154a44a4553SMatthias Brugger WDIOF_MAGICCLOSE, 155a44a4553SMatthias Brugger }; 156a44a4553SMatthias Brugger 157a44a4553SMatthias Brugger static const struct watchdog_ops mtk_wdt_ops = { 158a44a4553SMatthias Brugger .owner = THIS_MODULE, 159a44a4553SMatthias Brugger .start = mtk_wdt_start, 160a44a4553SMatthias Brugger .stop = mtk_wdt_stop, 161a44a4553SMatthias Brugger .ping = mtk_wdt_ping, 162a44a4553SMatthias Brugger .set_timeout = mtk_wdt_set_timeout, 163a44a4553SMatthias Brugger }; 164a44a4553SMatthias Brugger 165a44a4553SMatthias Brugger static int mtk_wdt_probe(struct platform_device *pdev) 166a44a4553SMatthias Brugger { 167a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt; 168a44a4553SMatthias Brugger struct resource *res; 169a44a4553SMatthias Brugger int err; 170a44a4553SMatthias Brugger 171a44a4553SMatthias Brugger mtk_wdt = devm_kzalloc(&pdev->dev, sizeof(*mtk_wdt), GFP_KERNEL); 172a44a4553SMatthias Brugger if (!mtk_wdt) 173a44a4553SMatthias Brugger return -ENOMEM; 174a44a4553SMatthias Brugger 175a44a4553SMatthias Brugger platform_set_drvdata(pdev, mtk_wdt); 176a44a4553SMatthias Brugger 177a44a4553SMatthias Brugger res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 178a44a4553SMatthias Brugger mtk_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res); 179a44a4553SMatthias Brugger if (IS_ERR(mtk_wdt->wdt_base)) 180a44a4553SMatthias Brugger return PTR_ERR(mtk_wdt->wdt_base); 181a44a4553SMatthias Brugger 182a44a4553SMatthias Brugger mtk_wdt->wdt_dev.info = &mtk_wdt_info; 183a44a4553SMatthias Brugger mtk_wdt->wdt_dev.ops = &mtk_wdt_ops; 184a44a4553SMatthias Brugger mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT; 185a44a4553SMatthias Brugger mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT; 186a44a4553SMatthias Brugger mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT; 187a44a4553SMatthias Brugger mtk_wdt->wdt_dev.parent = &pdev->dev; 188a44a4553SMatthias Brugger 189a44a4553SMatthias Brugger watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, &pdev->dev); 190a44a4553SMatthias Brugger watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout); 191a44a4553SMatthias Brugger 192a44a4553SMatthias Brugger watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt); 193a44a4553SMatthias Brugger 194a44a4553SMatthias Brugger mtk_wdt_stop(&mtk_wdt->wdt_dev); 195a44a4553SMatthias Brugger 196a44a4553SMatthias Brugger err = watchdog_register_device(&mtk_wdt->wdt_dev); 197a44a4553SMatthias Brugger if (unlikely(err)) 198a44a4553SMatthias Brugger return err; 199a44a4553SMatthias Brugger 200a44a4553SMatthias Brugger mtk_wdt->restart_handler.notifier_call = mtk_reset_handler; 201a44a4553SMatthias Brugger mtk_wdt->restart_handler.priority = 128; 202a44a4553SMatthias Brugger err = register_restart_handler(&mtk_wdt->restart_handler); 203a44a4553SMatthias Brugger if (err) 204a44a4553SMatthias Brugger dev_warn(&pdev->dev, 205a44a4553SMatthias Brugger "cannot register restart handler (err=%d)\n", err); 206a44a4553SMatthias Brugger 207a44a4553SMatthias Brugger dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n", 208a44a4553SMatthias Brugger mtk_wdt->wdt_dev.timeout, nowayout); 209a44a4553SMatthias Brugger 210a44a4553SMatthias Brugger return 0; 211a44a4553SMatthias Brugger } 212a44a4553SMatthias Brugger 213a44a4553SMatthias Brugger static int mtk_wdt_remove(struct platform_device *pdev) 214a44a4553SMatthias Brugger { 215a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev); 216a44a4553SMatthias Brugger 217a44a4553SMatthias Brugger unregister_restart_handler(&mtk_wdt->restart_handler); 218a44a4553SMatthias Brugger 219a44a4553SMatthias Brugger watchdog_unregister_device(&mtk_wdt->wdt_dev); 220a44a4553SMatthias Brugger 221a44a4553SMatthias Brugger return 0; 222a44a4553SMatthias Brugger } 223a44a4553SMatthias Brugger 224a44a4553SMatthias Brugger static const struct of_device_id mtk_wdt_dt_ids[] = { 225a44a4553SMatthias Brugger { .compatible = "mediatek,mt6589-wdt" }, 226a44a4553SMatthias Brugger { /* sentinel */ } 227a44a4553SMatthias Brugger }; 228a44a4553SMatthias Brugger MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids); 229a44a4553SMatthias Brugger 230a44a4553SMatthias Brugger static struct platform_driver mtk_wdt_driver = { 231a44a4553SMatthias Brugger .probe = mtk_wdt_probe, 232a44a4553SMatthias Brugger .remove = mtk_wdt_remove, 233a44a4553SMatthias Brugger .driver = { 234a44a4553SMatthias Brugger .name = DRV_NAME, 235a44a4553SMatthias Brugger .of_match_table = mtk_wdt_dt_ids, 236a44a4553SMatthias Brugger }, 237a44a4553SMatthias Brugger }; 238a44a4553SMatthias Brugger 239a44a4553SMatthias Brugger module_platform_driver(mtk_wdt_driver); 240a44a4553SMatthias Brugger 241a44a4553SMatthias Brugger module_param(timeout, uint, 0); 242a44a4553SMatthias Brugger MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds"); 243a44a4553SMatthias Brugger 244a44a4553SMatthias Brugger module_param(nowayout, bool, 0); 245a44a4553SMatthias Brugger MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" 246a44a4553SMatthias Brugger __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 247a44a4553SMatthias Brugger 248a44a4553SMatthias Brugger MODULE_LICENSE("GPL"); 249a44a4553SMatthias Brugger MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>"); 250a44a4553SMatthias Brugger MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver"); 251a44a4553SMatthias Brugger MODULE_VERSION(DRV_VERSION); 252