Marcus Folkesson | 2e62c49 | 2018-03-16 16:14:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Broadcom Corporation |
| 4 | * |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/platform_device.h> |
Florian Fainelli | d6b9c67 | 2021-11-12 14:46:32 -0800 | [diff] [blame] | 13 | #include <linux/platform_data/bcm7038_wdt.h> |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 14 | #include <linux/pm.h> |
| 15 | #include <linux/watchdog.h> |
| 16 | |
| 17 | #define WDT_START_1 0xff00 |
| 18 | #define WDT_START_2 0x00ff |
| 19 | #define WDT_STOP_1 0xee00 |
| 20 | #define WDT_STOP_2 0x00ee |
| 21 | |
| 22 | #define WDT_TIMEOUT_REG 0x0 |
| 23 | #define WDT_CMD_REG 0x4 |
| 24 | |
| 25 | #define WDT_MIN_TIMEOUT 1 /* seconds */ |
| 26 | #define WDT_DEFAULT_TIMEOUT 30 /* seconds */ |
| 27 | #define WDT_DEFAULT_RATE 27000000 |
| 28 | |
| 29 | struct bcm7038_watchdog { |
| 30 | void __iomem *base; |
| 31 | struct watchdog_device wdd; |
| 32 | u32 rate; |
| 33 | struct clk *clk; |
| 34 | }; |
| 35 | |
| 36 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 37 | |
Álvaro Fernández Rojas | e379c21 | 2021-02-23 09:00:42 +0100 | [diff] [blame] | 38 | static inline void bcm7038_wdt_write(u32 value, void __iomem *addr) |
| 39 | { |
| 40 | /* MIPS chips strapped for BE will automagically configure the |
| 41 | * peripheral registers for CPU-native byte order. |
| 42 | */ |
| 43 | if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
| 44 | __raw_writel(value, addr); |
| 45 | else |
| 46 | writel_relaxed(value, addr); |
| 47 | } |
| 48 | |
| 49 | static inline u32 bcm7038_wdt_read(void __iomem *addr) |
| 50 | { |
| 51 | if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
| 52 | return __raw_readl(addr); |
| 53 | else |
| 54 | return readl_relaxed(addr); |
| 55 | } |
| 56 | |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 57 | static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog) |
| 58 | { |
| 59 | struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog); |
| 60 | u32 timeout; |
| 61 | |
| 62 | timeout = wdt->rate * wdog->timeout; |
| 63 | |
Álvaro Fernández Rojas | e379c21 | 2021-02-23 09:00:42 +0100 | [diff] [blame] | 64 | bcm7038_wdt_write(timeout, wdt->base + WDT_TIMEOUT_REG); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static int bcm7038_wdt_ping(struct watchdog_device *wdog) |
| 68 | { |
| 69 | struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog); |
| 70 | |
Álvaro Fernández Rojas | e379c21 | 2021-02-23 09:00:42 +0100 | [diff] [blame] | 71 | bcm7038_wdt_write(WDT_START_1, wdt->base + WDT_CMD_REG); |
| 72 | bcm7038_wdt_write(WDT_START_2, wdt->base + WDT_CMD_REG); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int bcm7038_wdt_start(struct watchdog_device *wdog) |
| 78 | { |
| 79 | bcm7038_wdt_set_timeout_reg(wdog); |
| 80 | bcm7038_wdt_ping(wdog); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int bcm7038_wdt_stop(struct watchdog_device *wdog) |
| 86 | { |
| 87 | struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog); |
| 88 | |
Álvaro Fernández Rojas | e379c21 | 2021-02-23 09:00:42 +0100 | [diff] [blame] | 89 | bcm7038_wdt_write(WDT_STOP_1, wdt->base + WDT_CMD_REG); |
| 90 | bcm7038_wdt_write(WDT_STOP_2, wdt->base + WDT_CMD_REG); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog, |
| 96 | unsigned int t) |
| 97 | { |
| 98 | /* Can't modify timeout value if watchdog timer is running */ |
| 99 | bcm7038_wdt_stop(wdog); |
| 100 | wdog->timeout = t; |
| 101 | bcm7038_wdt_start(wdog); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog) |
| 107 | { |
| 108 | struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog); |
| 109 | u32 time_left; |
| 110 | |
Álvaro Fernández Rojas | e379c21 | 2021-02-23 09:00:42 +0100 | [diff] [blame] | 111 | time_left = bcm7038_wdt_read(wdt->base + WDT_CMD_REG); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 112 | |
| 113 | return time_left / wdt->rate; |
| 114 | } |
| 115 | |
Bhumika Goyal | 6c36893 | 2016-12-26 22:35:11 +0530 | [diff] [blame] | 116 | static const struct watchdog_info bcm7038_wdt_info = { |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 117 | .identity = "Broadcom BCM7038 Watchdog Timer", |
| 118 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
| 119 | WDIOF_MAGICCLOSE |
| 120 | }; |
| 121 | |
Julia Lawall | 85f15cf | 2016-09-01 19:35:26 +0200 | [diff] [blame] | 122 | static const struct watchdog_ops bcm7038_wdt_ops = { |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 123 | .owner = THIS_MODULE, |
| 124 | .start = bcm7038_wdt_start, |
| 125 | .stop = bcm7038_wdt_stop, |
| 126 | .set_timeout = bcm7038_wdt_set_timeout, |
| 127 | .get_timeleft = bcm7038_wdt_get_timeleft, |
| 128 | }; |
| 129 | |
Guenter Roeck | 69656dc | 2019-04-08 12:38:30 -0700 | [diff] [blame] | 130 | static void bcm7038_clk_disable_unprepare(void *data) |
| 131 | { |
| 132 | clk_disable_unprepare(data); |
| 133 | } |
| 134 | |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 135 | static int bcm7038_wdt_probe(struct platform_device *pdev) |
| 136 | { |
Florian Fainelli | d6b9c67 | 2021-11-12 14:46:32 -0800 | [diff] [blame] | 137 | struct bcm7038_wdt_platform_data *pdata = pdev->dev.platform_data; |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 138 | struct device *dev = &pdev->dev; |
| 139 | struct bcm7038_watchdog *wdt; |
Florian Fainelli | d6b9c67 | 2021-11-12 14:46:32 -0800 | [diff] [blame] | 140 | const char *clk_name = NULL; |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 141 | int err; |
| 142 | |
| 143 | wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); |
| 144 | if (!wdt) |
| 145 | return -ENOMEM; |
| 146 | |
| 147 | platform_set_drvdata(pdev, wdt); |
| 148 | |
Guenter Roeck | 0f0a6a2 | 2019-04-02 12:01:53 -0700 | [diff] [blame] | 149 | wdt->base = devm_platform_ioremap_resource(pdev, 0); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 150 | if (IS_ERR(wdt->base)) |
| 151 | return PTR_ERR(wdt->base); |
| 152 | |
Florian Fainelli | d6b9c67 | 2021-11-12 14:46:32 -0800 | [diff] [blame] | 153 | if (pdata && pdata->clk_name) |
| 154 | clk_name = pdata->clk_name; |
| 155 | |
| 156 | wdt->clk = devm_clk_get(dev, clk_name); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 157 | /* If unable to get clock, use default frequency */ |
| 158 | if (!IS_ERR(wdt->clk)) { |
Fabio Estevam | dd0a18c | 2017-07-22 17:22:59 -0300 | [diff] [blame] | 159 | err = clk_prepare_enable(wdt->clk); |
| 160 | if (err) |
| 161 | return err; |
Guenter Roeck | 69656dc | 2019-04-08 12:38:30 -0700 | [diff] [blame] | 162 | err = devm_add_action_or_reset(dev, |
| 163 | bcm7038_clk_disable_unprepare, |
| 164 | wdt->clk); |
| 165 | if (err) |
| 166 | return err; |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 167 | wdt->rate = clk_get_rate(wdt->clk); |
| 168 | /* Prevent divide-by-zero exception */ |
| 169 | if (!wdt->rate) |
| 170 | wdt->rate = WDT_DEFAULT_RATE; |
| 171 | } else { |
| 172 | wdt->rate = WDT_DEFAULT_RATE; |
| 173 | wdt->clk = NULL; |
| 174 | } |
| 175 | |
| 176 | wdt->wdd.info = &bcm7038_wdt_info; |
| 177 | wdt->wdd.ops = &bcm7038_wdt_ops; |
| 178 | wdt->wdd.min_timeout = WDT_MIN_TIMEOUT; |
| 179 | wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT; |
| 180 | wdt->wdd.max_timeout = 0xffffffff / wdt->rate; |
| 181 | wdt->wdd.parent = dev; |
| 182 | watchdog_set_drvdata(&wdt->wdd, wdt); |
| 183 | |
Guenter Roeck | 69656dc | 2019-04-08 12:38:30 -0700 | [diff] [blame] | 184 | watchdog_stop_on_reboot(&wdt->wdd); |
| 185 | watchdog_stop_on_unregister(&wdt->wdd); |
| 186 | err = devm_watchdog_register_device(dev, &wdt->wdd); |
Wolfram Sang | f542989 | 2019-05-18 23:27:20 +0200 | [diff] [blame] | 187 | if (err) |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 188 | return err; |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 189 | |
| 190 | dev_info(dev, "Registered BCM7038 Watchdog\n"); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 195 | #ifdef CONFIG_PM_SLEEP |
| 196 | static int bcm7038_wdt_suspend(struct device *dev) |
| 197 | { |
| 198 | struct bcm7038_watchdog *wdt = dev_get_drvdata(dev); |
| 199 | |
| 200 | if (watchdog_active(&wdt->wdd)) |
| 201 | return bcm7038_wdt_stop(&wdt->wdd); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int bcm7038_wdt_resume(struct device *dev) |
| 207 | { |
| 208 | struct bcm7038_watchdog *wdt = dev_get_drvdata(dev); |
| 209 | |
| 210 | if (watchdog_active(&wdt->wdd)) |
| 211 | return bcm7038_wdt_start(&wdt->wdd); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend, |
| 218 | bcm7038_wdt_resume); |
| 219 | |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 220 | static const struct of_device_id bcm7038_wdt_match[] = { |
| 221 | { .compatible = "brcm,bcm7038-wdt" }, |
| 222 | {}, |
| 223 | }; |
Javier Martinez Canillas | 57d77c6 | 2016-10-14 12:23:50 -0300 | [diff] [blame] | 224 | MODULE_DEVICE_TABLE(of, bcm7038_wdt_match); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 225 | |
Florian Fainelli | e764fae | 2021-11-12 14:46:34 -0800 | [diff] [blame] | 226 | static const struct platform_device_id bcm7038_wdt_devtype[] = { |
| 227 | { .name = "bcm63xx-wdt" }, |
| 228 | { /* sentinel */ }, |
| 229 | }; |
| 230 | MODULE_DEVICE_TABLE(platform, bcm7038_wdt_devtype); |
| 231 | |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 232 | static struct platform_driver bcm7038_wdt_driver = { |
| 233 | .probe = bcm7038_wdt_probe, |
Florian Fainelli | e764fae | 2021-11-12 14:46:34 -0800 | [diff] [blame] | 234 | .id_table = bcm7038_wdt_devtype, |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 235 | .driver = { |
| 236 | .name = "bcm7038-wdt", |
| 237 | .of_match_table = bcm7038_wdt_match, |
| 238 | .pm = &bcm7038_wdt_pm_ops, |
| 239 | } |
| 240 | }; |
| 241 | module_platform_driver(bcm7038_wdt_driver); |
| 242 | |
| 243 | module_param(nowayout, bool, 0); |
| 244 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 245 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Marcus Folkesson | 2e62c49 | 2018-03-16 16:14:11 +0100 | [diff] [blame] | 246 | MODULE_LICENSE("GPL"); |
Justin Chen | 7a3629f | 2015-08-31 11:02:43 -0700 | [diff] [blame] | 247 | MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog"); |
| 248 | MODULE_AUTHOR("Justin Chen"); |