^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Watchdog driver for Atmel AT91RM9200 (Thunder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2003 SAN People (Pty) Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mfd/syscon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mfd/syscon/atmel-st.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define WDT_DEFAULT_TIME 5 /* seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define WDT_MAX_TIME 256 /* seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int wdt_time = WDT_DEFAULT_TIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static struct regmap *regmap_st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) module_param(wdt_time, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __MODULE_STRING(WDT_DEFAULT_TIME) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #ifdef CONFIG_WATCHDOG_NOWAYOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) "Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static unsigned long at91wdt_busy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* ......................................................................... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int at91rm9200_restart(struct notifier_block *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long mode, void *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Perform a hardware reset with the use of the Watchdog timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) regmap_write(regmap_st, AT91_ST_WDMR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) AT91_ST_RSTEN | AT91_ST_EXTEN | 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) mdelay(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) pr_emerg("Unable to restart system\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static struct notifier_block at91rm9200_restart_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .notifier_call = at91rm9200_restart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .priority = 192,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Disable the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static inline void at91_wdt_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Enable and reset the watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static inline void at91_wdt_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN | AT91_ST_RSTEN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) (((65536 * wdt_time) >> 8) & AT91_ST_WDV));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Reload the watchdog timer. (ie, pat the watchdog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline void at91_wdt_reload(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* ......................................................................... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Watchdog device is opened, and watchdog starts running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int at91_wdt_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (test_and_set_bit(0, &at91wdt_busy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) at91_wdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Close the watchdog device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int at91_wdt_close(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Disable the watchdog when file is closed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!nowayout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) at91_wdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) clear_bit(0, &at91wdt_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Change the watchdog time interval.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int at91_wdt_settimeout(int new_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Since WDV is a 16-bit counter, the maximum period is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * 65536 / 256 = 256 seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* Set new watchdog time. It will be used when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) at91_wdt_start() is called. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) wdt_time = new_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static const struct watchdog_info at91_wdt_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .identity = "at91 watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Handle commands from user-space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static long at91_wdt_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int new_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return copy_to_user(argp, &at91_wdt_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) sizeof(at91_wdt_info)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (get_user(new_value, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (new_value & WDIOS_DISABLECARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) at91_wdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (new_value & WDIOS_ENABLECARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) at91_wdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) at91_wdt_reload(); /* pat the watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (get_user(new_value, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (at91_wdt_settimeout(new_value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* Enable new time value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) at91_wdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Return current value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return put_user(wdt_time, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return put_user(wdt_time, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Pat the watchdog whenever device is written to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static ssize_t at91_wdt_write(struct file *file, const char *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) at91_wdt_reload(); /* pat the watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* ......................................................................... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static const struct file_operations at91wdt_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) .unlocked_ioctl = at91_wdt_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) .open = at91_wdt_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .release = at91_wdt_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) .write = at91_wdt_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static struct miscdevice at91wdt_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) .minor = WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) .name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) .fops = &at91wdt_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int at91wdt_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct device *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (at91wdt_miscdev.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) at91wdt_miscdev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) parent = dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) dev_err(dev, "no parent\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) regmap_st = syscon_node_to_regmap(parent->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (IS_ERR(regmap_st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) res = misc_register(&at91wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) res = register_restart_handler(&at91rm9200_restart_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) dev_warn(dev, "failed to register restart handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) pr_info("AT91 Watchdog Timer enabled (%d seconds%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) wdt_time, nowayout ? ", nowayout" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int at91wdt_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) res = unregister_restart_handler(&at91rm9200_restart_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev_warn(dev, "failed to unregister restart handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) misc_deregister(&at91wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) at91wdt_miscdev.parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static void at91wdt_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) at91_wdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) at91_wdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int at91wdt_resume(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (at91wdt_busy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) at91_wdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #define at91wdt_suspend NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) #define at91wdt_resume NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static const struct of_device_id at91_wdt_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) { .compatible = "atmel,at91rm9200-wdt" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static struct platform_driver at91wdt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .probe = at91wdt_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .remove = at91wdt_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) .shutdown = at91wdt_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .suspend = at91wdt_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) .resume = at91wdt_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) .name = "atmel_st_watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) .of_match_table = at91_wdt_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static int __init at91_wdt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /* Check that the heartbeat value is within range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if not reset to the default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (at91_wdt_settimeout(wdt_time)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) at91_wdt_settimeout(WDT_DEFAULT_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) pr_info("wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) wdt_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return platform_driver_register(&at91wdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static void __exit at91_wdt_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) platform_driver_unregister(&at91wdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) module_init(at91_wdt_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) module_exit(at91_wdt_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) MODULE_AUTHOR("Andrew Victor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) MODULE_DESCRIPTION("Watchdog driver for Atmel AT91RM9200");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) MODULE_ALIAS("platform:atmel_st_watchdog");