0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010
0011 #include <linux/bitops.h>
0012 #include <linux/delay.h>
0013 #include <linux/errno.h>
0014 #include <linux/fs.h>
0015 #include <linux/init.h>
0016 #include <linux/io.h>
0017 #include <linux/kernel.h>
0018 #include <linux/mfd/syscon.h>
0019 #include <linux/mfd/syscon/atmel-st.h>
0020 #include <linux/miscdevice.h>
0021 #include <linux/module.h>
0022 #include <linux/moduleparam.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/reboot.h>
0025 #include <linux/regmap.h>
0026 #include <linux/types.h>
0027 #include <linux/watchdog.h>
0028 #include <linux/uaccess.h>
0029 #include <linux/of.h>
0030 #include <linux/of_device.h>
0031
0032 #define WDT_DEFAULT_TIME 5
0033 #define WDT_MAX_TIME 256
0034
0035 static int wdt_time = WDT_DEFAULT_TIME;
0036 static bool nowayout = WATCHDOG_NOWAYOUT;
0037 static struct regmap *regmap_st;
0038
0039 module_param(wdt_time, int, 0);
0040 MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
0041 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
0042
0043 #ifdef CONFIG_WATCHDOG_NOWAYOUT
0044 module_param(nowayout, bool, 0);
0045 MODULE_PARM_DESC(nowayout,
0046 "Watchdog cannot be stopped once started (default="
0047 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0048 #endif
0049
0050
0051 static unsigned long at91wdt_busy;
0052
0053
0054
0055 static int at91rm9200_restart(struct notifier_block *this,
0056 unsigned long mode, void *cmd)
0057 {
0058
0059
0060
0061 regmap_write(regmap_st, AT91_ST_WDMR,
0062 AT91_ST_RSTEN | AT91_ST_EXTEN | 1);
0063 regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
0064
0065 mdelay(2000);
0066
0067 pr_emerg("Unable to restart system\n");
0068 return NOTIFY_DONE;
0069 }
0070
0071 static struct notifier_block at91rm9200_restart_nb = {
0072 .notifier_call = at91rm9200_restart,
0073 .priority = 192,
0074 };
0075
0076
0077
0078
0079 static inline void at91_wdt_stop(void)
0080 {
0081 regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN);
0082 }
0083
0084
0085
0086
0087 static inline void at91_wdt_start(void)
0088 {
0089 regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN | AT91_ST_RSTEN |
0090 (((65536 * wdt_time) >> 8) & AT91_ST_WDV));
0091 regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
0092 }
0093
0094
0095
0096
0097 static inline void at91_wdt_reload(void)
0098 {
0099 regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
0100 }
0101
0102
0103
0104
0105
0106
0107 static int at91_wdt_open(struct inode *inode, struct file *file)
0108 {
0109 if (test_and_set_bit(0, &at91wdt_busy))
0110 return -EBUSY;
0111
0112 at91_wdt_start();
0113 return stream_open(inode, file);
0114 }
0115
0116
0117
0118
0119
0120
0121 static int at91_wdt_close(struct inode *inode, struct file *file)
0122 {
0123
0124 if (!nowayout)
0125 at91_wdt_stop();
0126
0127 clear_bit(0, &at91wdt_busy);
0128 return 0;
0129 }
0130
0131
0132
0133
0134 static int at91_wdt_settimeout(int new_time)
0135 {
0136
0137
0138
0139
0140
0141
0142 if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
0143 return -EINVAL;
0144
0145
0146
0147 wdt_time = new_time;
0148 return 0;
0149 }
0150
0151 static const struct watchdog_info at91_wdt_info = {
0152 .identity = "at91 watchdog",
0153 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
0154 };
0155
0156
0157
0158
0159 static long at91_wdt_ioctl(struct file *file,
0160 unsigned int cmd, unsigned long arg)
0161 {
0162 void __user *argp = (void __user *)arg;
0163 int __user *p = argp;
0164 int new_value;
0165
0166 switch (cmd) {
0167 case WDIOC_GETSUPPORT:
0168 return copy_to_user(argp, &at91_wdt_info,
0169 sizeof(at91_wdt_info)) ? -EFAULT : 0;
0170 case WDIOC_GETSTATUS:
0171 case WDIOC_GETBOOTSTATUS:
0172 return put_user(0, p);
0173 case WDIOC_SETOPTIONS:
0174 if (get_user(new_value, p))
0175 return -EFAULT;
0176 if (new_value & WDIOS_DISABLECARD)
0177 at91_wdt_stop();
0178 if (new_value & WDIOS_ENABLECARD)
0179 at91_wdt_start();
0180 return 0;
0181 case WDIOC_KEEPALIVE:
0182 at91_wdt_reload();
0183 return 0;
0184 case WDIOC_SETTIMEOUT:
0185 if (get_user(new_value, p))
0186 return -EFAULT;
0187 if (at91_wdt_settimeout(new_value))
0188 return -EINVAL;
0189
0190 at91_wdt_start();
0191
0192 return put_user(wdt_time, p);
0193 case WDIOC_GETTIMEOUT:
0194 return put_user(wdt_time, p);
0195 default:
0196 return -ENOTTY;
0197 }
0198 }
0199
0200
0201
0202
0203 static ssize_t at91_wdt_write(struct file *file, const char *data,
0204 size_t len, loff_t *ppos)
0205 {
0206 at91_wdt_reload();
0207 return len;
0208 }
0209
0210
0211
0212 static const struct file_operations at91wdt_fops = {
0213 .owner = THIS_MODULE,
0214 .llseek = no_llseek,
0215 .unlocked_ioctl = at91_wdt_ioctl,
0216 .compat_ioctl = compat_ptr_ioctl,
0217 .open = at91_wdt_open,
0218 .release = at91_wdt_close,
0219 .write = at91_wdt_write,
0220 };
0221
0222 static struct miscdevice at91wdt_miscdev = {
0223 .minor = WATCHDOG_MINOR,
0224 .name = "watchdog",
0225 .fops = &at91wdt_fops,
0226 };
0227
0228 static int at91wdt_probe(struct platform_device *pdev)
0229 {
0230 struct device *dev = &pdev->dev;
0231 struct device *parent;
0232 int res;
0233
0234 if (at91wdt_miscdev.parent)
0235 return -EBUSY;
0236 at91wdt_miscdev.parent = &pdev->dev;
0237
0238 parent = dev->parent;
0239 if (!parent) {
0240 dev_err(dev, "no parent\n");
0241 return -ENODEV;
0242 }
0243
0244 regmap_st = syscon_node_to_regmap(parent->of_node);
0245 if (IS_ERR(regmap_st))
0246 return -ENODEV;
0247
0248 res = misc_register(&at91wdt_miscdev);
0249 if (res)
0250 return res;
0251
0252 res = register_restart_handler(&at91rm9200_restart_nb);
0253 if (res)
0254 dev_warn(dev, "failed to register restart handler\n");
0255
0256 pr_info("AT91 Watchdog Timer enabled (%d seconds%s)\n",
0257 wdt_time, nowayout ? ", nowayout" : "");
0258 return 0;
0259 }
0260
0261 static int at91wdt_remove(struct platform_device *pdev)
0262 {
0263 struct device *dev = &pdev->dev;
0264 int res;
0265
0266 res = unregister_restart_handler(&at91rm9200_restart_nb);
0267 if (res)
0268 dev_warn(dev, "failed to unregister restart handler\n");
0269
0270 misc_deregister(&at91wdt_miscdev);
0271 at91wdt_miscdev.parent = NULL;
0272
0273 return res;
0274 }
0275
0276 static void at91wdt_shutdown(struct platform_device *pdev)
0277 {
0278 at91_wdt_stop();
0279 }
0280
0281 #ifdef CONFIG_PM
0282
0283 static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message)
0284 {
0285 at91_wdt_stop();
0286 return 0;
0287 }
0288
0289 static int at91wdt_resume(struct platform_device *pdev)
0290 {
0291 if (at91wdt_busy)
0292 at91_wdt_start();
0293 return 0;
0294 }
0295
0296 #else
0297 #define at91wdt_suspend NULL
0298 #define at91wdt_resume NULL
0299 #endif
0300
0301 static const struct of_device_id at91_wdt_dt_ids[] = {
0302 { .compatible = "atmel,at91rm9200-wdt" },
0303 { }
0304 };
0305 MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
0306
0307 static struct platform_driver at91wdt_driver = {
0308 .probe = at91wdt_probe,
0309 .remove = at91wdt_remove,
0310 .shutdown = at91wdt_shutdown,
0311 .suspend = at91wdt_suspend,
0312 .resume = at91wdt_resume,
0313 .driver = {
0314 .name = "atmel_st_watchdog",
0315 .of_match_table = at91_wdt_dt_ids,
0316 },
0317 };
0318
0319 static int __init at91_wdt_init(void)
0320 {
0321
0322
0323 if (at91_wdt_settimeout(wdt_time)) {
0324 at91_wdt_settimeout(WDT_DEFAULT_TIME);
0325 pr_info("wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
0326 wdt_time);
0327 }
0328
0329 return platform_driver_register(&at91wdt_driver);
0330 }
0331
0332 static void __exit at91_wdt_exit(void)
0333 {
0334 platform_driver_unregister(&at91wdt_driver);
0335 }
0336
0337 module_init(at91_wdt_init);
0338 module_exit(at91_wdt_exit);
0339
0340 MODULE_AUTHOR("Andrew Victor");
0341 MODULE_DESCRIPTION("Watchdog driver for Atmel AT91RM9200");
0342 MODULE_LICENSE("GPL");
0343 MODULE_ALIAS("platform:atmel_st_watchdog");