1 /* 2 * Watchdog driver for Broadcom BCM2835 3 * 4 * "bcm2708_wdog" driver written by Luke Diamand that was obtained from 5 * branch "rpi-3.6.y" of git://github.com/raspberrypi/linux.git was used 6 * as a hardware reference for the Broadcom BCM2835 watchdog timer. 7 * 8 * Copyright (C) 2013 Lubomir Rintel <lkundrak@v3.sk> 9 * 10 * This program is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU General Public License as published by the 12 * Free Software Foundation; either version 2 of the License, or (at your 13 * option) any later version. 14 */ 15 16 #include <linux/types.h> 17 #include <linux/module.h> 18 #include <linux/io.h> 19 #include <linux/watchdog.h> 20 #include <linux/platform_device.h> 21 #include <linux/of_address.h> 22 23 #define PM_RSTC 0x1c 24 #define PM_WDOG 0x24 25 26 #define PM_PASSWORD 0x5a000000 27 28 #define PM_WDOG_TIME_SET 0x000fffff 29 #define PM_RSTC_WRCFG_CLR 0xffffffcf 30 #define PM_RSTC_WRCFG_SET 0x00000030 31 #define PM_RSTC_WRCFG_FULL_RESET 0x00000020 32 #define PM_RSTC_RESET 0x00000102 33 34 #define SECS_TO_WDOG_TICKS(x) ((x) << 16) 35 #define WDOG_TICKS_TO_SECS(x) ((x) >> 16) 36 37 struct bcm2835_wdt { 38 void __iomem *base; 39 spinlock_t lock; 40 }; 41 42 static unsigned int heartbeat; 43 static bool nowayout = WATCHDOG_NOWAYOUT; 44 45 static int bcm2835_wdt_start(struct watchdog_device *wdog) 46 { 47 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog); 48 uint32_t cur; 49 unsigned long flags; 50 51 spin_lock_irqsave(&wdt->lock, flags); 52 53 writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) & 54 PM_WDOG_TIME_SET), wdt->base + PM_WDOG); 55 cur = readl_relaxed(wdt->base + PM_RSTC); 56 writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) | 57 PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC); 58 59 spin_unlock_irqrestore(&wdt->lock, flags); 60 61 return 0; 62 } 63 64 static int bcm2835_wdt_stop(struct watchdog_device *wdog) 65 { 66 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog); 67 68 writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC); 69 dev_info(wdog->dev, "Watchdog timer stopped"); 70 return 0; 71 } 72 73 static int bcm2835_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t) 74 { 75 wdog->timeout = t; 76 return 0; 77 } 78 79 static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog) 80 { 81 struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog); 82 83 uint32_t ret = readl_relaxed(wdt->base + PM_WDOG); 84 return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET); 85 } 86 87 static struct watchdog_ops bcm2835_wdt_ops = { 88 .owner = THIS_MODULE, 89 .start = bcm2835_wdt_start, 90 .stop = bcm2835_wdt_stop, 91 .set_timeout = bcm2835_wdt_set_timeout, 92 .get_timeleft = bcm2835_wdt_get_timeleft, 93 }; 94 95 static struct watchdog_info bcm2835_wdt_info = { 96 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | 97 WDIOF_KEEPALIVEPING, 98 .identity = "Broadcom BCM2835 Watchdog timer", 99 }; 100 101 static struct watchdog_device bcm2835_wdt_wdd = { 102 .info = &bcm2835_wdt_info, 103 .ops = &bcm2835_wdt_ops, 104 .min_timeout = 1, 105 .max_timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET), 106 .timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET), 107 }; 108 109 static int bcm2835_wdt_probe(struct platform_device *pdev) 110 { 111 struct device *dev = &pdev->dev; 112 struct device_node *np = dev->of_node; 113 struct bcm2835_wdt *wdt; 114 int err; 115 116 wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL); 117 if (!wdt) { 118 dev_err(dev, "Failed to allocate memory for watchdog device"); 119 return -ENOMEM; 120 } 121 platform_set_drvdata(pdev, wdt); 122 123 spin_lock_init(&wdt->lock); 124 125 wdt->base = of_iomap(np, 0); 126 if (!wdt->base) { 127 dev_err(dev, "Failed to remap watchdog regs"); 128 return -ENODEV; 129 } 130 131 watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt); 132 watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev); 133 watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout); 134 err = watchdog_register_device(&bcm2835_wdt_wdd); 135 if (err) { 136 dev_err(dev, "Failed to register watchdog device"); 137 iounmap(wdt->base); 138 return err; 139 } 140 141 dev_info(dev, "Broadcom BCM2835 watchdog timer"); 142 return 0; 143 } 144 145 static int bcm2835_wdt_remove(struct platform_device *pdev) 146 { 147 struct bcm2835_wdt *wdt = platform_get_drvdata(pdev); 148 149 watchdog_unregister_device(&bcm2835_wdt_wdd); 150 iounmap(wdt->base); 151 152 return 0; 153 } 154 155 static void bcm2835_wdt_shutdown(struct platform_device *pdev) 156 { 157 bcm2835_wdt_stop(&bcm2835_wdt_wdd); 158 } 159 160 static const struct of_device_id bcm2835_wdt_of_match[] = { 161 { .compatible = "brcm,bcm2835-pm-wdt", }, 162 {}, 163 }; 164 MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match); 165 166 static struct platform_driver bcm2835_wdt_driver = { 167 .probe = bcm2835_wdt_probe, 168 .remove = bcm2835_wdt_remove, 169 .shutdown = bcm2835_wdt_shutdown, 170 .driver = { 171 .name = "bcm2835-wdt", 172 .owner = THIS_MODULE, 173 .of_match_table = bcm2835_wdt_of_match, 174 }, 175 }; 176 module_platform_driver(bcm2835_wdt_driver); 177 178 module_param(heartbeat, uint, 0); 179 MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds"); 180 181 module_param(nowayout, bool, 0); 182 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" 183 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 184 185 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>"); 186 MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer"); 187 MODULE_LICENSE("GPL"); 188