Carlo Caione | d00680e | 2013-07-30 21:20:46 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * sunxi Watchdog Driver |
| 3 | * |
| 4 | * Copyright (c) 2013 Carlo Caione |
| 5 | * 2012 Henrik Nordstrom |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * Based on xen_wdt.c |
| 13 | * (c) Copyright 2010 Novell, Inc. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/watchdog.h> |
| 27 | |
| 28 | #define WDT_MAX_TIMEOUT 16 |
| 29 | #define WDT_MIN_TIMEOUT 1 |
| 30 | #define WDT_MODE_TIMEOUT(n) ((n) << 3) |
| 31 | #define WDT_TIMEOUT_MASK WDT_MODE_TIMEOUT(0x0F) |
| 32 | |
| 33 | #define WDT_CTRL 0x00 |
| 34 | #define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1)) |
| 35 | |
| 36 | #define WDT_MODE 0x04 |
| 37 | #define WDT_MODE_EN (1 << 0) |
| 38 | #define WDT_MODE_RST_EN (1 << 1) |
| 39 | |
| 40 | #define DRV_NAME "sunxi-wdt" |
| 41 | #define DRV_VERSION "1.0" |
| 42 | |
| 43 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 44 | static unsigned int timeout = WDT_MAX_TIMEOUT; |
| 45 | |
| 46 | struct sunxi_wdt_dev { |
| 47 | struct watchdog_device wdt_dev; |
| 48 | void __iomem *wdt_base; |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * wdt_timeout_map maps the watchdog timer interval value in seconds to |
| 53 | * the value of the register WDT_MODE bit 3:6 |
| 54 | * |
| 55 | * [timeout seconds] = register value |
| 56 | * |
| 57 | */ |
| 58 | |
| 59 | static const int wdt_timeout_map[] = { |
| 60 | [1] = 0b0001, /* 1s */ |
| 61 | [2] = 0b0010, /* 2s */ |
| 62 | [3] = 0b0011, /* 3s */ |
| 63 | [4] = 0b0100, /* 4s */ |
| 64 | [5] = 0b0101, /* 5s */ |
| 65 | [6] = 0b0110, /* 6s */ |
| 66 | [8] = 0b0111, /* 8s */ |
| 67 | [10] = 0b1000, /* 10s */ |
| 68 | [12] = 0b1001, /* 12s */ |
| 69 | [14] = 0b1010, /* 14s */ |
| 70 | [16] = 0b1011, /* 16s */ |
| 71 | }; |
| 72 | |
| 73 | static int sunxi_wdt_ping(struct watchdog_device *wdt_dev) |
| 74 | { |
| 75 | struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev); |
| 76 | void __iomem *wdt_base = sunxi_wdt->wdt_base; |
| 77 | |
| 78 | iowrite32(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev, |
| 84 | unsigned int timeout) |
| 85 | { |
| 86 | struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev); |
| 87 | void __iomem *wdt_base = sunxi_wdt->wdt_base; |
| 88 | u32 reg; |
| 89 | |
| 90 | if (wdt_timeout_map[timeout] == 0) |
| 91 | timeout++; |
| 92 | |
| 93 | sunxi_wdt->wdt_dev.timeout = timeout; |
| 94 | |
| 95 | reg = ioread32(wdt_base + WDT_MODE); |
| 96 | reg &= ~WDT_TIMEOUT_MASK; |
| 97 | reg |= WDT_MODE_TIMEOUT(wdt_timeout_map[timeout]); |
| 98 | iowrite32(reg, wdt_base + WDT_MODE); |
| 99 | |
| 100 | sunxi_wdt_ping(wdt_dev); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int sunxi_wdt_stop(struct watchdog_device *wdt_dev) |
| 106 | { |
| 107 | struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev); |
| 108 | void __iomem *wdt_base = sunxi_wdt->wdt_base; |
| 109 | |
| 110 | iowrite32(0, wdt_base + WDT_MODE); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int sunxi_wdt_start(struct watchdog_device *wdt_dev) |
| 116 | { |
| 117 | u32 reg; |
| 118 | struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev); |
| 119 | void __iomem *wdt_base = sunxi_wdt->wdt_base; |
| 120 | int ret; |
| 121 | |
| 122 | ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev, |
| 123 | sunxi_wdt->wdt_dev.timeout); |
| 124 | if (ret < 0) |
| 125 | return ret; |
| 126 | |
| 127 | reg = ioread32(wdt_base + WDT_MODE); |
| 128 | reg |= (WDT_MODE_RST_EN | WDT_MODE_EN); |
| 129 | iowrite32(reg, wdt_base + WDT_MODE); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static const struct watchdog_info sunxi_wdt_info = { |
| 135 | .identity = DRV_NAME, |
| 136 | .options = WDIOF_SETTIMEOUT | |
| 137 | WDIOF_KEEPALIVEPING | |
| 138 | WDIOF_MAGICCLOSE, |
| 139 | }; |
| 140 | |
| 141 | static const struct watchdog_ops sunxi_wdt_ops = { |
| 142 | .owner = THIS_MODULE, |
| 143 | .start = sunxi_wdt_start, |
| 144 | .stop = sunxi_wdt_stop, |
| 145 | .ping = sunxi_wdt_ping, |
| 146 | .set_timeout = sunxi_wdt_set_timeout, |
| 147 | }; |
| 148 | |
| 149 | static int __init sunxi_wdt_probe(struct platform_device *pdev) |
| 150 | { |
| 151 | struct sunxi_wdt_dev *sunxi_wdt; |
| 152 | struct resource *res; |
| 153 | int err; |
| 154 | |
| 155 | sunxi_wdt = devm_kzalloc(&pdev->dev, sizeof(*sunxi_wdt), GFP_KERNEL); |
| 156 | if (!sunxi_wdt) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | platform_set_drvdata(pdev, sunxi_wdt); |
| 160 | |
| 161 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 162 | sunxi_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res); |
| 163 | if (IS_ERR(sunxi_wdt->wdt_base)) |
| 164 | return PTR_ERR(sunxi_wdt->wdt_base); |
| 165 | |
| 166 | sunxi_wdt->wdt_dev.info = &sunxi_wdt_info; |
| 167 | sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops; |
| 168 | sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT; |
| 169 | sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT; |
| 170 | sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT; |
| 171 | sunxi_wdt->wdt_dev.parent = &pdev->dev; |
| 172 | |
| 173 | watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, &pdev->dev); |
| 174 | watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout); |
| 175 | |
| 176 | watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt); |
| 177 | |
| 178 | sunxi_wdt_stop(&sunxi_wdt->wdt_dev); |
| 179 | |
| 180 | err = watchdog_register_device(&sunxi_wdt->wdt_dev); |
| 181 | if (unlikely(err)) |
| 182 | return err; |
| 183 | |
| 184 | dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)", |
| 185 | sunxi_wdt->wdt_dev.timeout, nowayout); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int __exit sunxi_wdt_remove(struct platform_device *pdev) |
| 191 | { |
| 192 | struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev); |
| 193 | |
| 194 | watchdog_unregister_device(&sunxi_wdt->wdt_dev); |
| 195 | watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static void sunxi_wdt_shutdown(struct platform_device *pdev) |
| 201 | { |
| 202 | struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev); |
| 203 | |
| 204 | sunxi_wdt_stop(&sunxi_wdt->wdt_dev); |
| 205 | } |
| 206 | |
| 207 | static const struct of_device_id sunxi_wdt_dt_ids[] = { |
| 208 | { .compatible = "allwinner,sun4i-wdt" }, |
| 209 | { /* sentinel */ } |
| 210 | }; |
| 211 | MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids); |
| 212 | |
| 213 | static struct platform_driver sunxi_wdt_driver = { |
| 214 | .probe = sunxi_wdt_probe, |
| 215 | .remove = sunxi_wdt_remove, |
| 216 | .shutdown = sunxi_wdt_shutdown, |
| 217 | .driver = { |
| 218 | .owner = THIS_MODULE, |
| 219 | .name = DRV_NAME, |
| 220 | .of_match_table = of_match_ptr(sunxi_wdt_dt_ids) |
| 221 | }, |
| 222 | }; |
| 223 | |
| 224 | module_platform_driver(sunxi_wdt_driver); |
| 225 | |
| 226 | module_param(timeout, uint, 0); |
| 227 | MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds"); |
| 228 | |
| 229 | module_param(nowayout, bool, 0); |
| 230 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " |
| 231 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 232 | |
| 233 | MODULE_LICENSE("GPL"); |
| 234 | MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>"); |
| 235 | MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>"); |
| 236 | MODULE_DESCRIPTION("sunxi WatchDog Timer Driver"); |
| 237 | MODULE_VERSION(DRV_VERSION); |