Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2019 NXP. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/clk.h> |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 7 | #include <linux/io.h> |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 8 | #include <linux/iopoll.h> |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/reboot.h> |
| 14 | #include <linux/watchdog.h> |
| 15 | |
| 16 | #define WDOG_CS 0x0 |
| 17 | #define WDOG_CS_CMD32EN BIT(13) |
| 18 | #define WDOG_CS_ULK BIT(11) |
| 19 | #define WDOG_CS_RCS BIT(10) |
Fabio Estevam | eccb7fe | 2019-10-29 14:40:37 -0300 | [diff] [blame] | 20 | #define LPO_CLK 0x1 |
| 21 | #define LPO_CLK_SHIFT 8 |
| 22 | #define WDOG_CS_CLK (LPO_CLK << LPO_CLK_SHIFT) |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 23 | #define WDOG_CS_EN BIT(7) |
| 24 | #define WDOG_CS_UPDATE BIT(5) |
Anson Huang | 0cfbe17 | 2020-07-31 07:03:33 +0800 | [diff] [blame] | 25 | #define WDOG_CS_WAIT BIT(1) |
| 26 | #define WDOG_CS_STOP BIT(0) |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 27 | |
| 28 | #define WDOG_CNT 0x4 |
| 29 | #define WDOG_TOVAL 0x8 |
| 30 | |
| 31 | #define REFRESH_SEQ0 0xA602 |
| 32 | #define REFRESH_SEQ1 0xB480 |
| 33 | #define REFRESH ((REFRESH_SEQ1 << 16) | REFRESH_SEQ0) |
| 34 | |
| 35 | #define UNLOCK_SEQ0 0xC520 |
| 36 | #define UNLOCK_SEQ1 0xD928 |
| 37 | #define UNLOCK ((UNLOCK_SEQ1 << 16) | UNLOCK_SEQ0) |
| 38 | |
| 39 | #define DEFAULT_TIMEOUT 60 |
| 40 | #define MAX_TIMEOUT 128 |
| 41 | #define WDOG_CLOCK_RATE 1000 |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 42 | #define WDOG_WAIT_TIMEOUT 20 |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 43 | |
| 44 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 45 | module_param(nowayout, bool, 0000); |
| 46 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 47 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 48 | |
| 49 | struct imx7ulp_wdt_device { |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 50 | struct watchdog_device wdd; |
| 51 | void __iomem *base; |
| 52 | struct clk *clk; |
| 53 | }; |
| 54 | |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 55 | static int imx7ulp_wdt_wait(void __iomem *base, u32 mask) |
| 56 | { |
| 57 | u32 val = readl(base + WDOG_CS); |
| 58 | |
| 59 | if (!(val & mask) && readl_poll_timeout_atomic(base + WDOG_CS, val, |
| 60 | val & mask, 0, |
| 61 | WDOG_WAIT_TIMEOUT)) |
| 62 | return -ETIMEDOUT; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int imx7ulp_wdt_enable(struct watchdog_device *wdog, bool enable) |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 68 | { |
Fabio Estevam | 747d88a | 2019-10-29 14:40:34 -0300 | [diff] [blame] | 69 | struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog); |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 70 | |
Fabio Estevam | 747d88a | 2019-10-29 14:40:34 -0300 | [diff] [blame] | 71 | u32 val = readl(wdt->base + WDOG_CS); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 72 | int ret; |
Fabio Estevam | 747d88a | 2019-10-29 14:40:34 -0300 | [diff] [blame] | 73 | |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 74 | local_irq_disable(); |
Fabio Estevam | 747d88a | 2019-10-29 14:40:34 -0300 | [diff] [blame] | 75 | writel(UNLOCK, wdt->base + WDOG_CNT); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 76 | ret = imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK); |
| 77 | if (ret) |
| 78 | goto enable_out; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 79 | if (enable) |
Fabio Estevam | 747d88a | 2019-10-29 14:40:34 -0300 | [diff] [blame] | 80 | writel(val | WDOG_CS_EN, wdt->base + WDOG_CS); |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 81 | else |
Fabio Estevam | 747d88a | 2019-10-29 14:40:34 -0300 | [diff] [blame] | 82 | writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 83 | imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS); |
| 84 | |
| 85 | enable_out: |
| 86 | local_irq_enable(); |
| 87 | |
| 88 | return ret; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Fabio Estevam | c37e358 | 2019-10-29 14:40:36 -0300 | [diff] [blame] | 91 | static bool imx7ulp_wdt_is_enabled(void __iomem *base) |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 92 | { |
| 93 | u32 val = readl(base + WDOG_CS); |
| 94 | |
| 95 | return val & WDOG_CS_EN; |
| 96 | } |
| 97 | |
| 98 | static int imx7ulp_wdt_ping(struct watchdog_device *wdog) |
| 99 | { |
| 100 | struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog); |
| 101 | |
| 102 | writel(REFRESH, wdt->base + WDOG_CNT); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int imx7ulp_wdt_start(struct watchdog_device *wdog) |
| 108 | { |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 109 | return imx7ulp_wdt_enable(wdog, true); |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static int imx7ulp_wdt_stop(struct watchdog_device *wdog) |
| 113 | { |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 114 | return imx7ulp_wdt_enable(wdog, false); |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int imx7ulp_wdt_set_timeout(struct watchdog_device *wdog, |
| 118 | unsigned int timeout) |
| 119 | { |
| 120 | struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog); |
| 121 | u32 val = WDOG_CLOCK_RATE * timeout; |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 122 | int ret; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 123 | |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 124 | local_irq_disable(); |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 125 | writel(UNLOCK, wdt->base + WDOG_CNT); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 126 | ret = imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK); |
| 127 | if (ret) |
| 128 | goto timeout_out; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 129 | writel(val, wdt->base + WDOG_TOVAL); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 130 | imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS); |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 131 | |
| 132 | wdog->timeout = timeout; |
| 133 | |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 134 | timeout_out: |
| 135 | local_irq_enable(); |
| 136 | |
| 137 | return ret; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Fabio Estevam | 6083ab7 | 2019-10-29 14:40:33 -0300 | [diff] [blame] | 140 | static int imx7ulp_wdt_restart(struct watchdog_device *wdog, |
| 141 | unsigned long action, void *data) |
| 142 | { |
| 143 | struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 144 | int ret; |
Fabio Estevam | 6083ab7 | 2019-10-29 14:40:33 -0300 | [diff] [blame] | 145 | |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 146 | ret = imx7ulp_wdt_enable(wdog, true); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | ret = imx7ulp_wdt_set_timeout(&wdt->wdd, 1); |
| 151 | if (ret) |
| 152 | return ret; |
Fabio Estevam | 6083ab7 | 2019-10-29 14:40:33 -0300 | [diff] [blame] | 153 | |
| 154 | /* wait for wdog to fire */ |
| 155 | while (true) |
| 156 | ; |
| 157 | |
| 158 | return NOTIFY_DONE; |
| 159 | } |
| 160 | |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 161 | static const struct watchdog_ops imx7ulp_wdt_ops = { |
| 162 | .owner = THIS_MODULE, |
| 163 | .start = imx7ulp_wdt_start, |
| 164 | .stop = imx7ulp_wdt_stop, |
| 165 | .ping = imx7ulp_wdt_ping, |
| 166 | .set_timeout = imx7ulp_wdt_set_timeout, |
Fabio Estevam | 6083ab7 | 2019-10-29 14:40:33 -0300 | [diff] [blame] | 167 | .restart = imx7ulp_wdt_restart, |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | static const struct watchdog_info imx7ulp_wdt_info = { |
| 171 | .identity = "i.MX7ULP watchdog timer", |
| 172 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
| 173 | WDIOF_MAGICCLOSE, |
| 174 | }; |
| 175 | |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 176 | static int imx7ulp_wdt_init(void __iomem *base, unsigned int timeout) |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 177 | { |
| 178 | u32 val; |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 179 | int ret; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 180 | |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 181 | local_irq_disable(); |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 182 | /* unlock the wdog for reconfiguration */ |
| 183 | writel_relaxed(UNLOCK_SEQ0, base + WDOG_CNT); |
| 184 | writel_relaxed(UNLOCK_SEQ1, base + WDOG_CNT); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 185 | ret = imx7ulp_wdt_wait(base, WDOG_CS_ULK); |
| 186 | if (ret) |
| 187 | goto init_out; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 188 | |
| 189 | /* set an initial timeout value in TOVAL */ |
| 190 | writel(timeout, base + WDOG_TOVAL); |
| 191 | /* enable 32bit command sequence and reconfigure */ |
Anson Huang | 0cfbe17 | 2020-07-31 07:03:33 +0800 | [diff] [blame] | 192 | val = WDOG_CS_CMD32EN | WDOG_CS_CLK | WDOG_CS_UPDATE | |
| 193 | WDOG_CS_WAIT | WDOG_CS_STOP; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 194 | writel(val, base + WDOG_CS); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 195 | imx7ulp_wdt_wait(base, WDOG_CS_RCS); |
| 196 | |
| 197 | init_out: |
| 198 | local_irq_enable(); |
| 199 | |
| 200 | return ret; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void imx7ulp_wdt_action(void *data) |
| 204 | { |
| 205 | clk_disable_unprepare(data); |
| 206 | } |
| 207 | |
| 208 | static int imx7ulp_wdt_probe(struct platform_device *pdev) |
| 209 | { |
| 210 | struct imx7ulp_wdt_device *imx7ulp_wdt; |
| 211 | struct device *dev = &pdev->dev; |
| 212 | struct watchdog_device *wdog; |
| 213 | int ret; |
| 214 | |
| 215 | imx7ulp_wdt = devm_kzalloc(dev, sizeof(*imx7ulp_wdt), GFP_KERNEL); |
| 216 | if (!imx7ulp_wdt) |
| 217 | return -ENOMEM; |
| 218 | |
| 219 | platform_set_drvdata(pdev, imx7ulp_wdt); |
| 220 | |
| 221 | imx7ulp_wdt->base = devm_platform_ioremap_resource(pdev, 0); |
| 222 | if (IS_ERR(imx7ulp_wdt->base)) |
| 223 | return PTR_ERR(imx7ulp_wdt->base); |
| 224 | |
| 225 | imx7ulp_wdt->clk = devm_clk_get(dev, NULL); |
| 226 | if (IS_ERR(imx7ulp_wdt->clk)) { |
| 227 | dev_err(dev, "Failed to get watchdog clock\n"); |
| 228 | return PTR_ERR(imx7ulp_wdt->clk); |
| 229 | } |
| 230 | |
| 231 | ret = clk_prepare_enable(imx7ulp_wdt->clk); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | ret = devm_add_action_or_reset(dev, imx7ulp_wdt_action, imx7ulp_wdt->clk); |
| 236 | if (ret) |
| 237 | return ret; |
| 238 | |
| 239 | wdog = &imx7ulp_wdt->wdd; |
| 240 | wdog->info = &imx7ulp_wdt_info; |
| 241 | wdog->ops = &imx7ulp_wdt_ops; |
| 242 | wdog->min_timeout = 1; |
| 243 | wdog->max_timeout = MAX_TIMEOUT; |
| 244 | wdog->parent = dev; |
| 245 | wdog->timeout = DEFAULT_TIMEOUT; |
| 246 | |
| 247 | watchdog_init_timeout(wdog, 0, dev); |
| 248 | watchdog_stop_on_reboot(wdog); |
| 249 | watchdog_stop_on_unregister(wdog); |
| 250 | watchdog_set_drvdata(wdog, imx7ulp_wdt); |
Anson Huang | 7439494 | 2020-07-31 07:03:32 +0800 | [diff] [blame] | 251 | ret = imx7ulp_wdt_init(imx7ulp_wdt->base, wdog->timeout * WDOG_CLOCK_RATE); |
| 252 | if (ret) |
| 253 | return ret; |
Anson Huang | 41b630f | 2019-08-28 09:35:01 -0400 | [diff] [blame] | 254 | |
| 255 | return devm_watchdog_register_device(dev, wdog); |
| 256 | } |
| 257 | |
| 258 | static int __maybe_unused imx7ulp_wdt_suspend(struct device *dev) |
| 259 | { |
| 260 | struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev); |
| 261 | |
| 262 | if (watchdog_active(&imx7ulp_wdt->wdd)) |
| 263 | imx7ulp_wdt_stop(&imx7ulp_wdt->wdd); |
| 264 | |
| 265 | clk_disable_unprepare(imx7ulp_wdt->clk); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int __maybe_unused imx7ulp_wdt_resume(struct device *dev) |
| 271 | { |
| 272 | struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev); |
| 273 | u32 timeout = imx7ulp_wdt->wdd.timeout * WDOG_CLOCK_RATE; |
| 274 | int ret; |
| 275 | |
| 276 | ret = clk_prepare_enable(imx7ulp_wdt->clk); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | |
| 280 | if (imx7ulp_wdt_is_enabled(imx7ulp_wdt->base)) |
| 281 | imx7ulp_wdt_init(imx7ulp_wdt->base, timeout); |
| 282 | |
| 283 | if (watchdog_active(&imx7ulp_wdt->wdd)) |
| 284 | imx7ulp_wdt_start(&imx7ulp_wdt->wdd); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static SIMPLE_DEV_PM_OPS(imx7ulp_wdt_pm_ops, imx7ulp_wdt_suspend, |
| 290 | imx7ulp_wdt_resume); |
| 291 | |
| 292 | static const struct of_device_id imx7ulp_wdt_dt_ids[] = { |
| 293 | { .compatible = "fsl,imx7ulp-wdt", }, |
| 294 | { /* sentinel */ } |
| 295 | }; |
| 296 | MODULE_DEVICE_TABLE(of, imx7ulp_wdt_dt_ids); |
| 297 | |
| 298 | static struct platform_driver imx7ulp_wdt_driver = { |
| 299 | .probe = imx7ulp_wdt_probe, |
| 300 | .driver = { |
| 301 | .name = "imx7ulp-wdt", |
| 302 | .pm = &imx7ulp_wdt_pm_ops, |
| 303 | .of_match_table = imx7ulp_wdt_dt_ids, |
| 304 | }, |
| 305 | }; |
| 306 | module_platform_driver(imx7ulp_wdt_driver); |
| 307 | |
| 308 | MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>"); |
| 309 | MODULE_DESCRIPTION("Freescale i.MX7ULP watchdog driver"); |
| 310 | MODULE_LICENSE("GPL v2"); |