12e62c498SMarcus Folkesson // SPDX-License-Identifier: GPL-2.0+ 2a44a4553SMatthias Brugger /* 3a44a4553SMatthias Brugger * Mediatek Watchdog Driver 4a44a4553SMatthias Brugger * 5a44a4553SMatthias Brugger * Copyright (C) 2014 Matthias Brugger 6a44a4553SMatthias Brugger * 7a44a4553SMatthias Brugger * Matthias Brugger <matthias.bgg@gmail.com> 8a44a4553SMatthias Brugger * 9a44a4553SMatthias Brugger * Based on sunxi_wdt.c 10a44a4553SMatthias Brugger */ 11a44a4553SMatthias Brugger 12*9e5236e7Syong.liang #include <dt-bindings/reset-controller/mt2712-resets.h> 13c254e103Syong.liang #include <dt-bindings/reset-controller/mt8183-resets.h> 14c254e103Syong.liang #include <linux/delay.h> 15a44a4553SMatthias Brugger #include <linux/err.h> 16a44a4553SMatthias Brugger #include <linux/init.h> 17a44a4553SMatthias Brugger #include <linux/io.h> 18a44a4553SMatthias Brugger #include <linux/kernel.h> 19a44a4553SMatthias Brugger #include <linux/module.h> 20a44a4553SMatthias Brugger #include <linux/moduleparam.h> 21a44a4553SMatthias Brugger #include <linux/of.h> 22c254e103Syong.liang #include <linux/of_device.h> 23a44a4553SMatthias Brugger #include <linux/platform_device.h> 24c254e103Syong.liang #include <linux/reset-controller.h> 25a44a4553SMatthias Brugger #include <linux/types.h> 26a44a4553SMatthias Brugger #include <linux/watchdog.h> 27a44a4553SMatthias Brugger 28a44a4553SMatthias Brugger #define WDT_MAX_TIMEOUT 31 29a44a4553SMatthias Brugger #define WDT_MIN_TIMEOUT 1 30a44a4553SMatthias Brugger #define WDT_LENGTH_TIMEOUT(n) ((n) << 5) 31a44a4553SMatthias Brugger 32a44a4553SMatthias Brugger #define WDT_LENGTH 0x04 33a44a4553SMatthias Brugger #define WDT_LENGTH_KEY 0x8 34a44a4553SMatthias Brugger 35a44a4553SMatthias Brugger #define WDT_RST 0x08 36a44a4553SMatthias Brugger #define WDT_RST_RELOAD 0x1971 37a44a4553SMatthias Brugger 38a44a4553SMatthias Brugger #define WDT_MODE 0x00 39a44a4553SMatthias Brugger #define WDT_MODE_EN (1 << 0) 40a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_LOW (0 << 1) 41a44a4553SMatthias Brugger #define WDT_MODE_EXT_POL_HIGH (1 << 1) 42a44a4553SMatthias Brugger #define WDT_MODE_EXRST_EN (1 << 2) 43a44a4553SMatthias Brugger #define WDT_MODE_IRQ_EN (1 << 3) 44a44a4553SMatthias Brugger #define WDT_MODE_AUTO_START (1 << 4) 45a44a4553SMatthias Brugger #define WDT_MODE_DUAL_EN (1 << 6) 46a44a4553SMatthias Brugger #define WDT_MODE_KEY 0x22000000 47a44a4553SMatthias Brugger 48a44a4553SMatthias Brugger #define WDT_SWRST 0x14 49a44a4553SMatthias Brugger #define WDT_SWRST_KEY 0x1209 50a44a4553SMatthias Brugger 51c254e103Syong.liang #define WDT_SWSYSRST 0x18U 52c254e103Syong.liang #define WDT_SWSYS_RST_KEY 0x88000000 53c254e103Syong.liang 54a44a4553SMatthias Brugger #define DRV_NAME "mtk-wdt" 55a44a4553SMatthias Brugger #define DRV_VERSION "1.0" 56a44a4553SMatthias Brugger 57a44a4553SMatthias Brugger static bool nowayout = WATCHDOG_NOWAYOUT; 58b82e6953SMarcus Folkesson static unsigned int timeout; 59a44a4553SMatthias Brugger 60a44a4553SMatthias Brugger struct mtk_wdt_dev { 61a44a4553SMatthias Brugger struct watchdog_device wdt_dev; 62a44a4553SMatthias Brugger void __iomem *wdt_base; 63c254e103Syong.liang spinlock_t lock; /* protects WDT_SWSYSRST reg */ 64c254e103Syong.liang struct reset_controller_dev rcdev; 65a44a4553SMatthias Brugger }; 66a44a4553SMatthias Brugger 67c254e103Syong.liang struct mtk_wdt_data { 68c254e103Syong.liang int toprgu_sw_rst_num; 69c254e103Syong.liang }; 70c254e103Syong.liang 71*9e5236e7Syong.liang static const struct mtk_wdt_data mt2712_data = { 72*9e5236e7Syong.liang .toprgu_sw_rst_num = MT2712_TOPRGU_SW_RST_NUM, 73*9e5236e7Syong.liang }; 74*9e5236e7Syong.liang 75c254e103Syong.liang static const struct mtk_wdt_data mt8183_data = { 76c254e103Syong.liang .toprgu_sw_rst_num = MT8183_TOPRGU_SW_RST_NUM, 77c254e103Syong.liang }; 78c254e103Syong.liang 79c254e103Syong.liang static int toprgu_reset_update(struct reset_controller_dev *rcdev, 80c254e103Syong.liang unsigned long id, bool assert) 81c254e103Syong.liang { 82c254e103Syong.liang unsigned int tmp; 83c254e103Syong.liang unsigned long flags; 84c254e103Syong.liang struct mtk_wdt_dev *data = 85c254e103Syong.liang container_of(rcdev, struct mtk_wdt_dev, rcdev); 86c254e103Syong.liang 87c254e103Syong.liang spin_lock_irqsave(&data->lock, flags); 88c254e103Syong.liang 89c254e103Syong.liang tmp = readl(data->wdt_base + WDT_SWSYSRST); 90c254e103Syong.liang if (assert) 91c254e103Syong.liang tmp |= BIT(id); 92c254e103Syong.liang else 93c254e103Syong.liang tmp &= ~BIT(id); 94c254e103Syong.liang tmp |= WDT_SWSYS_RST_KEY; 95c254e103Syong.liang writel(tmp, data->wdt_base + WDT_SWSYSRST); 96c254e103Syong.liang 97c254e103Syong.liang spin_unlock_irqrestore(&data->lock, flags); 98c254e103Syong.liang 99c254e103Syong.liang return 0; 100c254e103Syong.liang } 101c254e103Syong.liang 102c254e103Syong.liang static int toprgu_reset_assert(struct reset_controller_dev *rcdev, 103c254e103Syong.liang unsigned long id) 104c254e103Syong.liang { 105c254e103Syong.liang return toprgu_reset_update(rcdev, id, true); 106c254e103Syong.liang } 107c254e103Syong.liang 108c254e103Syong.liang static int toprgu_reset_deassert(struct reset_controller_dev *rcdev, 109c254e103Syong.liang unsigned long id) 110c254e103Syong.liang { 111c254e103Syong.liang return toprgu_reset_update(rcdev, id, false); 112c254e103Syong.liang } 113c254e103Syong.liang 114c254e103Syong.liang static int toprgu_reset(struct reset_controller_dev *rcdev, 115c254e103Syong.liang unsigned long id) 116c254e103Syong.liang { 117c254e103Syong.liang int ret; 118c254e103Syong.liang 119c254e103Syong.liang ret = toprgu_reset_assert(rcdev, id); 120c254e103Syong.liang if (ret) 121c254e103Syong.liang return ret; 122c254e103Syong.liang 123c254e103Syong.liang return toprgu_reset_deassert(rcdev, id); 124c254e103Syong.liang } 125c254e103Syong.liang 126c254e103Syong.liang static const struct reset_control_ops toprgu_reset_ops = { 127c254e103Syong.liang .assert = toprgu_reset_assert, 128c254e103Syong.liang .deassert = toprgu_reset_deassert, 129c254e103Syong.liang .reset = toprgu_reset, 130c254e103Syong.liang }; 131c254e103Syong.liang 132c254e103Syong.liang static int toprgu_register_reset_controller(struct platform_device *pdev, 133c254e103Syong.liang int rst_num) 134c254e103Syong.liang { 135c254e103Syong.liang int ret; 136c254e103Syong.liang struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev); 137c254e103Syong.liang 138c254e103Syong.liang spin_lock_init(&mtk_wdt->lock); 139c254e103Syong.liang 140c254e103Syong.liang mtk_wdt->rcdev.owner = THIS_MODULE; 141c254e103Syong.liang mtk_wdt->rcdev.nr_resets = rst_num; 142c254e103Syong.liang mtk_wdt->rcdev.ops = &toprgu_reset_ops; 143c254e103Syong.liang mtk_wdt->rcdev.of_node = pdev->dev.of_node; 144c254e103Syong.liang ret = devm_reset_controller_register(&pdev->dev, &mtk_wdt->rcdev); 145c254e103Syong.liang if (ret != 0) 146c254e103Syong.liang dev_err(&pdev->dev, 147c254e103Syong.liang "couldn't register wdt reset controller: %d\n", ret); 148c254e103Syong.liang return ret; 149c254e103Syong.liang } 150c254e103Syong.liang 1514d8b229dSGuenter Roeck static int mtk_wdt_restart(struct watchdog_device *wdt_dev, 1524d8b229dSGuenter Roeck unsigned long action, void *data) 153a44a4553SMatthias Brugger { 154e86adc3fSDamien Riegel struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 155a44a4553SMatthias Brugger void __iomem *wdt_base; 156a44a4553SMatthias Brugger 157a44a4553SMatthias Brugger wdt_base = mtk_wdt->wdt_base; 158a44a4553SMatthias Brugger 159a44a4553SMatthias Brugger while (1) { 160a44a4553SMatthias Brugger writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST); 161a44a4553SMatthias Brugger mdelay(5); 162a44a4553SMatthias Brugger } 163a44a4553SMatthias Brugger 164e86adc3fSDamien Riegel return 0; 165a44a4553SMatthias Brugger } 166a44a4553SMatthias Brugger 167a44a4553SMatthias Brugger static int mtk_wdt_ping(struct watchdog_device *wdt_dev) 168a44a4553SMatthias Brugger { 169a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 170a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 171a44a4553SMatthias Brugger 172a44a4553SMatthias Brugger iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST); 173a44a4553SMatthias Brugger 174a44a4553SMatthias Brugger return 0; 175a44a4553SMatthias Brugger } 176a44a4553SMatthias Brugger 177a44a4553SMatthias Brugger static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev, 178a44a4553SMatthias Brugger unsigned int timeout) 179a44a4553SMatthias Brugger { 180a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 181a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 182a44a4553SMatthias Brugger u32 reg; 183a44a4553SMatthias Brugger 184a44a4553SMatthias Brugger wdt_dev->timeout = timeout; 185a44a4553SMatthias Brugger 186a44a4553SMatthias Brugger /* 187a44a4553SMatthias Brugger * One bit is the value of 512 ticks 188a44a4553SMatthias Brugger * The clock has 32 KHz 189a44a4553SMatthias Brugger */ 190a44a4553SMatthias Brugger reg = WDT_LENGTH_TIMEOUT(timeout << 6) | WDT_LENGTH_KEY; 191a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_LENGTH); 192a44a4553SMatthias Brugger 193a44a4553SMatthias Brugger mtk_wdt_ping(wdt_dev); 194a44a4553SMatthias Brugger 195a44a4553SMatthias Brugger return 0; 196a44a4553SMatthias Brugger } 197a44a4553SMatthias Brugger 198a44a4553SMatthias Brugger static int mtk_wdt_stop(struct watchdog_device *wdt_dev) 199a44a4553SMatthias Brugger { 200a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 201a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 202a44a4553SMatthias Brugger u32 reg; 203a44a4553SMatthias Brugger 204a44a4553SMatthias Brugger reg = readl(wdt_base + WDT_MODE); 205a44a4553SMatthias Brugger reg &= ~WDT_MODE_EN; 2065da2bf1aSNicolas Boichat reg |= WDT_MODE_KEY; 207a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_MODE); 208a44a4553SMatthias Brugger 209a44a4553SMatthias Brugger return 0; 210a44a4553SMatthias Brugger } 211a44a4553SMatthias Brugger 212a44a4553SMatthias Brugger static int mtk_wdt_start(struct watchdog_device *wdt_dev) 213a44a4553SMatthias Brugger { 214a44a4553SMatthias Brugger u32 reg; 215a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev); 216a44a4553SMatthias Brugger void __iomem *wdt_base = mtk_wdt->wdt_base; 2179ffd906dSDan Carpenter int ret; 218a44a4553SMatthias Brugger 219a44a4553SMatthias Brugger ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout); 220a44a4553SMatthias Brugger if (ret < 0) 221a44a4553SMatthias Brugger return ret; 222a44a4553SMatthias Brugger 223a44a4553SMatthias Brugger reg = ioread32(wdt_base + WDT_MODE); 224a44a4553SMatthias Brugger reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN); 225a44a4553SMatthias Brugger reg |= (WDT_MODE_EN | WDT_MODE_KEY); 226a44a4553SMatthias Brugger iowrite32(reg, wdt_base + WDT_MODE); 227a44a4553SMatthias Brugger 228a44a4553SMatthias Brugger return 0; 229a44a4553SMatthias Brugger } 230a44a4553SMatthias Brugger 231a44a4553SMatthias Brugger static const struct watchdog_info mtk_wdt_info = { 232a44a4553SMatthias Brugger .identity = DRV_NAME, 233a44a4553SMatthias Brugger .options = WDIOF_SETTIMEOUT | 234a44a4553SMatthias Brugger WDIOF_KEEPALIVEPING | 235a44a4553SMatthias Brugger WDIOF_MAGICCLOSE, 236a44a4553SMatthias Brugger }; 237a44a4553SMatthias Brugger 238a44a4553SMatthias Brugger static const struct watchdog_ops mtk_wdt_ops = { 239a44a4553SMatthias Brugger .owner = THIS_MODULE, 240a44a4553SMatthias Brugger .start = mtk_wdt_start, 241a44a4553SMatthias Brugger .stop = mtk_wdt_stop, 242a44a4553SMatthias Brugger .ping = mtk_wdt_ping, 243a44a4553SMatthias Brugger .set_timeout = mtk_wdt_set_timeout, 244e86adc3fSDamien Riegel .restart = mtk_wdt_restart, 245a44a4553SMatthias Brugger }; 246a44a4553SMatthias Brugger 247a44a4553SMatthias Brugger static int mtk_wdt_probe(struct platform_device *pdev) 248a44a4553SMatthias Brugger { 249a15f6e64SGuenter Roeck struct device *dev = &pdev->dev; 250a44a4553SMatthias Brugger struct mtk_wdt_dev *mtk_wdt; 251c254e103Syong.liang const struct mtk_wdt_data *wdt_data; 252a44a4553SMatthias Brugger int err; 253a44a4553SMatthias Brugger 254a15f6e64SGuenter Roeck mtk_wdt = devm_kzalloc(dev, sizeof(*mtk_wdt), GFP_KERNEL); 255a44a4553SMatthias Brugger if (!mtk_wdt) 256a44a4553SMatthias Brugger return -ENOMEM; 257a44a4553SMatthias Brugger 258a44a4553SMatthias Brugger platform_set_drvdata(pdev, mtk_wdt); 259a44a4553SMatthias Brugger 2600f0a6a28SGuenter Roeck mtk_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0); 261a44a4553SMatthias Brugger if (IS_ERR(mtk_wdt->wdt_base)) 262a44a4553SMatthias Brugger return PTR_ERR(mtk_wdt->wdt_base); 263a44a4553SMatthias Brugger 264a44a4553SMatthias Brugger mtk_wdt->wdt_dev.info = &mtk_wdt_info; 265a44a4553SMatthias Brugger mtk_wdt->wdt_dev.ops = &mtk_wdt_ops; 266a44a4553SMatthias Brugger mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT; 267a44a4553SMatthias Brugger mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT; 268a44a4553SMatthias Brugger mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT; 269a15f6e64SGuenter Roeck mtk_wdt->wdt_dev.parent = dev; 270a44a4553SMatthias Brugger 271a15f6e64SGuenter Roeck watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, dev); 272a44a4553SMatthias Brugger watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout); 273e86adc3fSDamien Riegel watchdog_set_restart_priority(&mtk_wdt->wdt_dev, 128); 274a44a4553SMatthias Brugger 275a44a4553SMatthias Brugger watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt); 276a44a4553SMatthias Brugger 277a44a4553SMatthias Brugger mtk_wdt_stop(&mtk_wdt->wdt_dev); 278a44a4553SMatthias Brugger 279a15f6e64SGuenter Roeck watchdog_stop_on_reboot(&mtk_wdt->wdt_dev); 280a15f6e64SGuenter Roeck err = devm_watchdog_register_device(dev, &mtk_wdt->wdt_dev); 281a44a4553SMatthias Brugger if (unlikely(err)) 282a44a4553SMatthias Brugger return err; 283a44a4553SMatthias Brugger 284a15f6e64SGuenter Roeck dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n", 285a44a4553SMatthias Brugger mtk_wdt->wdt_dev.timeout, nowayout); 286a44a4553SMatthias Brugger 287c254e103Syong.liang wdt_data = of_device_get_match_data(dev); 288c254e103Syong.liang if (wdt_data) { 289c254e103Syong.liang err = toprgu_register_reset_controller(pdev, 290c254e103Syong.liang wdt_data->toprgu_sw_rst_num); 291c254e103Syong.liang if (err) 292c254e103Syong.liang return err; 293c254e103Syong.liang } 294a44a4553SMatthias Brugger return 0; 295a44a4553SMatthias Brugger } 296a44a4553SMatthias Brugger 2979fab0692SGreta Zhang #ifdef CONFIG_PM_SLEEP 2989fab0692SGreta Zhang static int mtk_wdt_suspend(struct device *dev) 2999fab0692SGreta Zhang { 3009fab0692SGreta Zhang struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev); 3019fab0692SGreta Zhang 3029fab0692SGreta Zhang if (watchdog_active(&mtk_wdt->wdt_dev)) 3039fab0692SGreta Zhang mtk_wdt_stop(&mtk_wdt->wdt_dev); 3049fab0692SGreta Zhang 3059fab0692SGreta Zhang return 0; 3069fab0692SGreta Zhang } 3079fab0692SGreta Zhang 3089fab0692SGreta Zhang static int mtk_wdt_resume(struct device *dev) 3099fab0692SGreta Zhang { 3109fab0692SGreta Zhang struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev); 3119fab0692SGreta Zhang 3129fab0692SGreta Zhang if (watchdog_active(&mtk_wdt->wdt_dev)) { 3139fab0692SGreta Zhang mtk_wdt_start(&mtk_wdt->wdt_dev); 3149fab0692SGreta Zhang mtk_wdt_ping(&mtk_wdt->wdt_dev); 3159fab0692SGreta Zhang } 3169fab0692SGreta Zhang 3179fab0692SGreta Zhang return 0; 3189fab0692SGreta Zhang } 3199fab0692SGreta Zhang #endif 3209fab0692SGreta Zhang 321a44a4553SMatthias Brugger static const struct of_device_id mtk_wdt_dt_ids[] = { 322*9e5236e7Syong.liang { .compatible = "mediatek,mt2712-wdt", .data = &mt2712_data }, 323a44a4553SMatthias Brugger { .compatible = "mediatek,mt6589-wdt" }, 324c254e103Syong.liang { .compatible = "mediatek,mt8183-wdt", .data = &mt8183_data }, 325a44a4553SMatthias Brugger { /* sentinel */ } 326a44a4553SMatthias Brugger }; 327a44a4553SMatthias Brugger MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids); 328a44a4553SMatthias Brugger 3299fab0692SGreta Zhang static const struct dev_pm_ops mtk_wdt_pm_ops = { 3309fab0692SGreta Zhang SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend, 3319fab0692SGreta Zhang mtk_wdt_resume) 3329fab0692SGreta Zhang }; 3339fab0692SGreta Zhang 334a44a4553SMatthias Brugger static struct platform_driver mtk_wdt_driver = { 335a44a4553SMatthias Brugger .probe = mtk_wdt_probe, 336a44a4553SMatthias Brugger .driver = { 337a44a4553SMatthias Brugger .name = DRV_NAME, 3389fab0692SGreta Zhang .pm = &mtk_wdt_pm_ops, 339a44a4553SMatthias Brugger .of_match_table = mtk_wdt_dt_ids, 340a44a4553SMatthias Brugger }, 341a44a4553SMatthias Brugger }; 342a44a4553SMatthias Brugger 343a44a4553SMatthias Brugger module_platform_driver(mtk_wdt_driver); 344a44a4553SMatthias Brugger 345a44a4553SMatthias Brugger module_param(timeout, uint, 0); 346a44a4553SMatthias Brugger MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds"); 347a44a4553SMatthias Brugger 348a44a4553SMatthias Brugger module_param(nowayout, bool, 0); 349a44a4553SMatthias Brugger MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" 350a44a4553SMatthias Brugger __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 351a44a4553SMatthias Brugger 352a44a4553SMatthias Brugger MODULE_LICENSE("GPL"); 353a44a4553SMatthias Brugger MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>"); 354a44a4553SMatthias Brugger MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver"); 355a44a4553SMatthias Brugger MODULE_VERSION(DRV_VERSION); 356