| 1 | |
| 2 | |
| 3 | * Watchdog driver for Faraday Technology FTWDT010
|
| 4 | *
|
| 5 | * Copyright (C) 2017 Linus Walleij <[email protected]>
|
| 6 | *
|
| 7 | * Inspired by the out-of-tree drivers from OpenWRT:
|
| 8 | * Copyright (C) 2009 Paulius Zaleckas <[email protected]>
|
| 9 |
|
| 10 |
|
| 11 | #include <linux/bitops.h>
|
| 12 | #include <linux/init.h>
|
| 13 | #include <linux/interrupt.h>
|
| 14 | #include <linux/io.h>
|
| 15 | #include <linux/kernel.h>
|
| 16 | #include <linux/module.h>
|
| 17 | #include <linux/platform_device.h>
|
| 18 | #include <linux/slab.h>
|
| 19 | #include <linux/watchdog.h>
|
| 20 |
|
| 21 | #define FTWDT010_WDCOUNTER 0x0
|
| 22 | #define FTWDT010_WDLOAD 0x4
|
| 23 | #define FTWDT010_WDRESTART 0x8
|
| 24 | #define FTWDT010_WDCR 0xC
|
| 25 |
|
| 26 | #define WDRESTART_MAGIC 0x5AB9
|
| 27 |
|
| 28 | #define WDCR_CLOCK_5MHZ BIT(4)
|
| 29 | #define WDCR_WDEXT BIT(3)
|
| 30 | #define WDCR_WDINTR BIT(2)
|
| 31 | #define WDCR_SYS_RST BIT(1)
|
| 32 | #define WDCR_ENABLE BIT(0)
|
| 33 |
|
| 34 | #define WDT_CLOCK 5000000
|
| 35 |
|
| 36 | struct ftwdt010_wdt {
|
| 37 | struct watchdog_device wdd;
|
| 38 | struct device *dev;
|
| 39 | void __iomem *base;
|
| 40 | bool has_irq;
|
| 41 | };
|
| 42 |
|
| 43 | static inline
|
| 44 | struct ftwdt010_wdt *to_ftwdt010_wdt(struct watchdog_device *wdd)
|
| 45 | {
|
| 46 | return container_of(wdd, struct ftwdt010_wdt, wdd);
|
| 47 | }
|
| 48 |
|
| 49 | static void ftwdt010_enable(struct ftwdt010_wdt *gwdt,
|
| 50 | unsigned int timeout,
|
| 51 | bool need_irq)
|
| 52 | {
|
| 53 | u32 enable;
|
| 54 |
|
| 55 | writel(timeout * WDT_CLOCK, gwdt->base + FTWDT010_WDLOAD);
|
| 56 | writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
|
| 57 |
|
| 58 | enable = WDCR_CLOCK_5MHZ | WDCR_SYS_RST;
|
| 59 | writel(enable, gwdt->base + FTWDT010_WDCR);
|
| 60 | if (need_irq)
|
| 61 | enable |= WDCR_WDINTR;
|
| 62 | enable |= WDCR_ENABLE;
|
| 63 | writel(enable, gwdt->base + FTWDT010_WDCR);
|
| 64 | }
|
| 65 |
|
| 66 | static int ftwdt010_wdt_start(struct watchdog_device *wdd)
|
| 67 | {
|
| 68 | struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
|
| 69 |
|
| 70 | ftwdt010_enable(gwdt, wdd->timeout, gwdt->has_irq);
|
| 71 | return 0;
|
| 72 | }
|
| 73 |
|
| 74 | static int ftwdt010_wdt_stop(struct watchdog_device *wdd)
|
| 75 | {
|
| 76 | struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
|
| 77 |
|
| 78 | writel(0, gwdt->base + FTWDT010_WDCR);
|
| 79 |
|
| 80 | return 0;
|
| 81 | }
|
| 82 |
|
| 83 | static int ftwdt010_wdt_ping(struct watchdog_device *wdd)
|
| 84 | {
|
| 85 | struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
|
| 86 |
|
| 87 | writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
|
| 88 |
|
| 89 | return 0;
|
| 90 | }
|
| 91 |
|
| 92 | static int ftwdt010_wdt_set_timeout(struct watchdog_device *wdd,
|
| 93 | unsigned int timeout)
|
| 94 | {
|
| 95 | wdd->timeout = timeout;
|
| 96 | if (watchdog_active(wdd))
|
| 97 | ftwdt010_wdt_start(wdd);
|
| 98 |
|
| 99 | return 0;
|
| 100 | }
|
| 101 |
|
| 102 | static int ftwdt010_wdt_restart(struct watchdog_device *wdd,
|
| 103 | unsigned long action, void *data)
|
| 104 | {
|
| 105 | ftwdt010_enable(to_ftwdt010_wdt(wdd), 0, false);
|
| 106 | return 0;
|
| 107 | }
|
| 108 |
|
| 109 | static irqreturn_t ftwdt010_wdt_interrupt(int irq, void *data)
|
| 110 | {
|
| 111 | struct ftwdt010_wdt *gwdt = data;
|
| 112 |
|
| 113 | watchdog_notify_pretimeout(&gwdt->wdd);
|
| 114 |
|
| 115 | return IRQ_HANDLED;
|
| 116 | }
|
| 117 |
|
| 118 | static const struct watchdog_ops ftwdt010_wdt_ops = {
|
| 119 | .start = ftwdt010_wdt_start,
|
| 120 | .stop = ftwdt010_wdt_stop,
|
| 121 | .ping = ftwdt010_wdt_ping,
|
| 122 | .set_timeout = ftwdt010_wdt_set_timeout,
|
| 123 | .restart = ftwdt010_wdt_restart,
|
| 124 | .owner = THIS_MODULE,
|
| 125 | };
|
| 126 |
|
| 127 | static const struct watchdog_info ftwdt010_wdt_info = {
|
| 128 | .options = WDIOF_KEEPALIVEPING
|
| 129 | | WDIOF_MAGICCLOSE
|
| 130 | | WDIOF_SETTIMEOUT,
|
| 131 | .identity = KBUILD_MODNAME,
|
| 132 | };
|
| 133 |
|
| 134 |
|
| 135 | static int ftwdt010_wdt_probe(struct platform_device *pdev)
|
| 136 | {
|
| 137 | struct device *dev = &pdev->dev;
|
| 138 | struct ftwdt010_wdt *gwdt;
|
| 139 | unsigned int reg;
|
| 140 | int irq;
|
| 141 | int ret;
|
| 142 |
|
| 143 | gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
|
| 144 | if (!gwdt)
|
| 145 | return -ENOMEM;
|
| 146 |
|
| 147 | gwdt->base = devm_platform_ioremap_resource(pdev, 0);
|
| 148 | if (IS_ERR(gwdt->base))
|
| 149 | return PTR_ERR(gwdt->base);
|
| 150 |
|
| 151 | gwdt->dev = dev;
|
| 152 | gwdt->wdd.info = &ftwdt010_wdt_info;
|
| 153 | gwdt->wdd.ops = &ftwdt010_wdt_ops;
|
| 154 | gwdt->wdd.min_timeout = 1;
|
| 155 | gwdt->wdd.max_timeout = 0xFFFFFFFF / WDT_CLOCK;
|
| 156 | gwdt->wdd.parent = dev;
|
| 157 |
|
| 158 | |
| 159 | * If 'timeout-sec' unspecified in devicetree, assume a 13 second
|
| 160 | * default.
|
| 161 |
|
| 162 | gwdt->wdd.timeout = 13U;
|
| 163 | watchdog_init_timeout(&gwdt->wdd, 0, dev);
|
| 164 |
|
| 165 | reg = readw(gwdt->base + FTWDT010_WDCR);
|
| 166 | if (reg & WDCR_ENABLE) {
|
| 167 |
|
| 168 | reg &= ~WDCR_ENABLE;
|
| 169 | writel(reg, gwdt->base + FTWDT010_WDCR);
|
| 170 | }
|
| 171 |
|
| 172 | irq = platform_get_irq(pdev, 0);
|
| 173 | if (irq > 0) {
|
| 174 | ret = devm_request_irq(dev, irq, ftwdt010_wdt_interrupt, 0,
|
| 175 | "watchdog bark", gwdt);
|
| 176 | if (ret)
|
| 177 | return ret;
|
| 178 | gwdt->has_irq = true;
|
| 179 | }
|
| 180 |
|
| 181 | ret = devm_watchdog_register_device(dev, &gwdt->wdd);
|
| 182 | if (ret)
|
| 183 | return ret;
|
| 184 |
|
| 185 |
|
| 186 | platform_set_drvdata(pdev, gwdt);
|
| 187 | dev_info(dev, "FTWDT010 watchdog driver enabled\n");
|
| 188 |
|
| 189 | return 0;
|
| 190 | }
|
| 191 |
|
| 192 | static int __maybe_unused ftwdt010_wdt_suspend(struct device *dev)
|
| 193 | {
|
| 194 | struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
|
| 195 | unsigned int reg;
|
| 196 |
|
| 197 | reg = readw(gwdt->base + FTWDT010_WDCR);
|
| 198 | reg &= ~WDCR_ENABLE;
|
| 199 | writel(reg, gwdt->base + FTWDT010_WDCR);
|
| 200 |
|
| 201 | return 0;
|
| 202 | }
|
| 203 |
|
| 204 | static int __maybe_unused ftwdt010_wdt_resume(struct device *dev)
|
| 205 | {
|
| 206 | struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
|
| 207 | unsigned int reg;
|
| 208 |
|
| 209 | if (watchdog_active(&gwdt->wdd)) {
|
| 210 | reg = readw(gwdt->base + FTWDT010_WDCR);
|
| 211 | reg |= WDCR_ENABLE;
|
| 212 | writel(reg, gwdt->base + FTWDT010_WDCR);
|
| 213 | }
|
| 214 |
|
| 215 | return 0;
|
| 216 | }
|
| 217 |
|
| 218 | static const struct dev_pm_ops ftwdt010_wdt_dev_pm_ops = {
|
| 219 | SET_SYSTEM_SLEEP_PM_OPS(ftwdt010_wdt_suspend,
|
| 220 | ftwdt010_wdt_resume)
|
| 221 | };
|
| 222 |
|
| 223 | static const struct of_device_id ftwdt010_wdt_match[] = {
|
| 224 | { .compatible = "faraday,ftwdt010" },
|
| 225 | { .compatible = "cortina,gemini-watchdog" },
|
| 226 | {},
|
| 227 | };
|
| 228 | MODULE_DEVICE_TABLE(of, ftwdt010_wdt_match);
|
| 229 |
|
| 230 | static struct platform_driver ftwdt010_wdt_driver = {
|
| 231 | .probe = ftwdt010_wdt_probe,
|
| 232 | .driver = {
|
| 233 | .name = "ftwdt010-wdt",
|
| 234 | .of_match_table = ftwdt010_wdt_match,
|
| 235 | .pm = &ftwdt010_wdt_dev_pm_ops,
|
| 236 | },
|
| 237 | };
|
| 238 | module_platform_driver(ftwdt010_wdt_driver);
|
| 239 | MODULE_AUTHOR("Linus Walleij");
|
| 240 | MODULE_DESCRIPTION("Watchdog driver for Faraday Technology FTWDT010");
|
| 241 | MODULE_LICENSE("GPL");
|