watchdog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/init.h>
  3. #include <linux/io.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/types.h>
  8. #include <linux/fs.h>
  9. #include <linux/cdev.h>
  10. #include <linux/device.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/timer.h>
  13. #define WATCHDOG_NAME "IT87 WDT"
  14. #define DEVICE_NAME "watchdog"
  15. #define CLASS_NAME "watchdog_class"
  16. /* Defaults for Module Parameter */
  17. #define DEFAULT_TIMEOUT 60
  18. #define DEFAULT_TESTMODE 0
  19. /* IO Ports */
  20. #define REG 0x4e
  21. #define VAL 0x4f
  22. /* Logical device Numbers LDN */
  23. #define GPIO 0x07
  24. /* Configuration Registers and Functions */
  25. #define LDNREG 0x07
  26. #define CHIPID 0x20
  27. #define CHIPREV 0x22
  28. /* Chip Id numbers */
  29. #define NO_DEV_ID 0xffff
  30. #define IT8607_ID 0x8607
  31. #define IT8620_ID 0x8620
  32. #define IT8622_ID 0x8622
  33. #define IT8625_ID 0x8625
  34. #define IT8628_ID 0x8628
  35. #define IT8655_ID 0x8655
  36. #define IT8665_ID 0x8665
  37. #define IT8686_ID 0x8686
  38. #define IT8702_ID 0x8702
  39. #define IT8705_ID 0x8705
  40. #define IT8712_ID 0x8712
  41. #define IT8716_ID 0x8716
  42. #define IT8718_ID 0x8718
  43. #define IT8720_ID 0x8720
  44. #define IT8721_ID 0x8721
  45. #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
  46. #define IT8728_ID 0x8728
  47. #define IT8772_ID 0x8772
  48. #define IT8783_ID 0x8783
  49. #define IT8784_ID 0x8784
  50. #define IT8786_ID 0x8786
  51. /* GPIO Configuration Registers LDN=0x07 */
  52. #define WDTCTRL 0x71
  53. #define WDTCFG 0x72
  54. #define WDTVALLSB 0x73
  55. #define WDTVALMSB 0x74
  56. /* GPIO Bits WDTCFG */
  57. #define WDT_TOV1 0x80
  58. #define WDT_KRST 0x40
  59. #define WDT_TOVE 0x20
  60. #define WDT_PWROK 0x10 /* not in it8721 */
  61. #define WDT_INT_MASK 0x0f
  62. static unsigned int max_units, chip_type;
  63. static unsigned int timeout = DEFAULT_TIMEOUT;
  64. static int testmode = DEFAULT_TESTMODE;
  65. module_param(timeout, int, 0);
  66. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
  67. __MODULE_STRING(DEFAULT_TIMEOUT));
  68. module_param(testmode, int, 0);
  69. MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
  70. __MODULE_STRING(DEFAULT_TESTMODE));
  71. /* Character device variables */
  72. static int major_number;
  73. static struct class *watchdog_class = NULL;
  74. static struct device *watchdog_device = NULL;
  75. /* Watchdog state: 0 = OFF, 1 = ON */
  76. static int watchdog_state = 0;
  77. static DEFINE_MUTEX(watchdog_mutex);
  78. /* Software timer for servicing watchdog */
  79. static struct timer_list watchdog_timer;
  80. extern struct kobject *vfiec_kobj;
  81. /* Superio Chip */
  82. static inline int superio_enter(void)
  83. {
  84. /*
  85. * Try to reserve REG and REG + 1 for exclusive access.
  86. */
  87. if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
  88. return -EBUSY;
  89. outb(0x87, REG);
  90. outb(0x01, REG);
  91. outb(0x55, REG);
  92. outb(0xaa, REG);
  93. return 0;
  94. }
  95. static inline void superio_exit(void)
  96. {
  97. outb(0x02, REG);
  98. outb(0x02, VAL);
  99. release_region(REG, 2);
  100. }
  101. static inline void superio_select(int ldn)
  102. {
  103. outb(LDNREG, REG);
  104. outb(ldn, VAL);
  105. }
  106. static inline int superio_inb(int reg)
  107. {
  108. outb(reg, REG);
  109. return inb(VAL);
  110. }
  111. static inline void superio_outb(int val, int reg)
  112. {
  113. outb(reg, REG);
  114. outb(val, VAL);
  115. }
  116. static inline int superio_inw(int reg)
  117. {
  118. int val;
  119. outb(reg++, REG);
  120. val = inb(VAL) << 8;
  121. outb(reg, REG);
  122. val |= inb(VAL);
  123. return val;
  124. }
  125. /* Internal function, should be called after superio_select(GPIO) */
  126. static void _wdt_update_timeout(unsigned int t)
  127. {
  128. unsigned char cfg = WDT_KRST;
  129. if (testmode)
  130. cfg = 0;
  131. if (t <= max_units)
  132. cfg |= WDT_TOV1;
  133. else
  134. t /= 60;
  135. if (chip_type != IT8721_ID)
  136. cfg |= WDT_PWROK;
  137. superio_outb(cfg, WDTCFG);
  138. superio_outb(t, WDTVALLSB);
  139. if (max_units > 255)
  140. superio_outb(t >> 8, WDTVALMSB);
  141. else
  142. superio_outb(0, WDTVALMSB);
  143. }
  144. static int wdt_update_timeout(unsigned int t)
  145. {
  146. int ret;
  147. ret = superio_enter();
  148. if (ret)
  149. return ret;
  150. superio_select(GPIO);
  151. _wdt_update_timeout(t);
  152. superio_exit();
  153. return 0;
  154. }
  155. static int wdt_round_time(int t)
  156. {
  157. t += 59;
  158. t -= t % 60;
  159. return t;
  160. }
  161. /* Software timer callback - service the watchdog */
  162. static void watchdog_timer_callback(struct timer_list *t)
  163. {
  164. /* Re-trigger the hardware watchdog by writing timeout again */
  165. wdt_update_timeout(timeout);
  166. /* Restart timer (periodic servicing, e.g., every half timeout) */
  167. mod_timer(&watchdog_timer, jiffies + (timeout * HZ / 2));
  168. }
  169. /* Enable watchdog - start hardware and software timer */
  170. static int watchdog_enable(void)
  171. {
  172. int ret;
  173. pr_info("Enabling watchdog timer\n");
  174. ret = wdt_update_timeout(timeout);
  175. if (ret) {
  176. pr_err("Failed to set watchdog timeout\n");
  177. return ret;
  178. }
  179. /* Start periodic software timer to service watchdog */
  180. timer_setup(&watchdog_timer, watchdog_timer_callback, 0);
  181. mod_timer(&watchdog_timer, jiffies + (timeout * HZ / 2));
  182. return 0;
  183. }
  184. /* Disable watchdog - stop hardware and software timer */
  185. static int watchdog_disable(void)
  186. {
  187. pr_info("Disabling watchdog timer\n");
  188. /* Stop and delete software timer */
  189. del_timer_sync(&watchdog_timer);
  190. /* Disable hardware watchdog by setting timeout to 0 */
  191. return wdt_update_timeout(0);
  192. }
  193. /* Parse user command: supports "ON", "OFF", "1", "0" */
  194. static int parse_watchdog_command(const char *buf, size_t count)
  195. {
  196. /* Skip whitespace */
  197. while (count > 0 && (*buf == ' ' || *buf == '\t' || *buf == '\n' || *buf == '\r')) {
  198. buf++;
  199. count--;
  200. }
  201. if (count == 0)
  202. return -1;
  203. /* Check for numeric value */
  204. if (count == 1 && (buf[0] == '0' || buf[0] == '1')) {
  205. return buf[0] - '0';
  206. }
  207. /* Check for "ON" or "OFF" (case insensitive) */
  208. if (count >= 2) {
  209. if ((buf[0] == 'O' || buf[0] == 'o') &&
  210. (buf[1] == 'N' || buf[1] == 'n')) {
  211. return 1;
  212. }
  213. if (count >= 3 &&
  214. (buf[0] == 'O' || buf[0] == 'o') &&
  215. (buf[1] == 'F' || buf[1] == 'f') &&
  216. (buf[2] == 'F' || buf[2] == 'f')) {
  217. return 0;
  218. }
  219. }
  220. return -1;
  221. }
  222. /* Character device file operations */
  223. static ssize_t watchdog_read(struct file *file, char __user *user_buf,
  224. size_t count, loff_t *ppos)
  225. {
  226. char status_buf[64];
  227. int len;
  228. if (*ppos > 0)
  229. return 0;
  230. mutex_lock(&watchdog_mutex);
  231. if (watchdog_state)
  232. len = snprintf(status_buf, sizeof(status_buf),
  233. "The watchdog is on\nThe args value is 1\n");
  234. else
  235. len = snprintf(status_buf, sizeof(status_buf),
  236. "The watchdog is off\nThe args value is 0\n");
  237. mutex_unlock(&watchdog_mutex);
  238. if (copy_to_user(user_buf, status_buf, len))
  239. return -EFAULT;
  240. *ppos += len;
  241. return len;
  242. }
  243. static ssize_t watchdog_write(struct file *file, const char __user *user_buf,
  244. size_t count, loff_t *ppos)
  245. {
  246. char buf[16];
  247. int cmd;
  248. int ret = count;
  249. if (count == 0 || count >= sizeof(buf))
  250. return -EINVAL;
  251. if (copy_from_user(buf, user_buf, count))
  252. return -EFAULT;
  253. buf[count] = '\0';
  254. cmd = parse_watchdog_command(buf, count);
  255. if (cmd < 0)
  256. return -EINVAL;
  257. mutex_lock(&watchdog_mutex);
  258. if (cmd == watchdog_state) {
  259. /* No state change */
  260. if (cmd)
  261. pr_info("Watchdog is already on\n");
  262. else
  263. pr_info("Watchdog is already off\n");
  264. } else {
  265. if (cmd) {
  266. /* Turn ON */
  267. if (watchdog_enable() == 0) {
  268. watchdog_state = 1;
  269. pr_info("Watchdog turned on successfully\n");
  270. } else {
  271. ret = -EIO;
  272. }
  273. } else {
  274. /* Turn OFF */
  275. if (watchdog_disable() == 0) {
  276. watchdog_state = 0;
  277. pr_info("Watchdog turned off successfully\n");
  278. } else {
  279. ret = -EIO;
  280. }
  281. }
  282. }
  283. mutex_unlock(&watchdog_mutex);
  284. return ret;
  285. }
  286. static int watchdog_open(struct inode *inode, struct file *file)
  287. {
  288. return 0;
  289. }
  290. static int watchdog_release(struct inode *inode, struct file *file)
  291. {
  292. return 0;
  293. }
  294. static const struct file_operations watchdog_fops = {
  295. .owner = THIS_MODULE,
  296. .read = watchdog_read,
  297. .write = watchdog_write,
  298. .open = watchdog_open,
  299. .release = watchdog_release,
  300. };
  301. static char *my_devnode(struct device *dev, umode_t *mode) {
  302. if (mode) {
  303. *mode = 0666;
  304. }
  305. return NULL;
  306. }
  307. static ssize_t watchdog_show(struct kobject *kobj, struct kobj_attribute *attr,
  308. char *buf)
  309. {
  310. return 0;
  311. }
  312. static ssize_t watchdog_store(struct kobject *kobj, struct kobj_attribute *attr,
  313. const char *buf, size_t count)
  314. {
  315. int cmd;
  316. int ret = count;
  317. // if (count == 0 || count >= sizeof(buf))
  318. // return -EINVAL;
  319. // if (strscpy(buf, user_buf, count) < 0)
  320. // return -EFAULT;
  321. // buf[count] = '\0';
  322. cmd = parse_watchdog_command(buf, count);
  323. if (cmd < 0)
  324. return -EINVAL;
  325. mutex_lock(&watchdog_mutex);
  326. if (cmd == watchdog_state) {
  327. /* No state change */
  328. if (cmd)
  329. pr_info("Watchdog is already on\n");
  330. else
  331. pr_info("Watchdog is already off\n");
  332. } else {
  333. if (cmd) {
  334. /* Turn ON */
  335. if (watchdog_enable() == 0) {
  336. watchdog_state = 1;
  337. pr_info("Watchdog turned on successfully\n");
  338. } else {
  339. ret = -EIO;
  340. }
  341. } else {
  342. /* Turn OFF */
  343. if (watchdog_disable() == 0) {
  344. watchdog_state = 0;
  345. pr_info("Watchdog turned off successfully\n");
  346. } else {
  347. ret = -EIO;
  348. }
  349. }
  350. }
  351. mutex_unlock(&watchdog_mutex);
  352. return ret;
  353. }
  354. static struct kobj_attribute watchdog_attr =
  355. __ATTR(watchdog, 0644, watchdog_show, watchdog_store);
  356. static struct attribute *watchdog_attrs[] = {
  357. &watchdog_attr.attr,
  358. NULL,
  359. };
  360. static struct attribute_group watchdog_attr_group = {
  361. .attrs = watchdog_attrs,
  362. };
  363. int watchdog_init(void)
  364. {
  365. u8 chip_rev;
  366. int rc;
  367. rc = sysfs_create_group(vfiec_kobj, &watchdog_attr_group);
  368. if (rc)
  369. {
  370. pr_err("Faiec_version to create sysfs group: %d\n", rc);
  371. return -1;
  372. }
  373. rc = superio_enter();
  374. if (rc)
  375. return rc;
  376. chip_type = superio_inw(CHIPID);
  377. chip_rev = superio_inb(CHIPREV) & 0x0f;
  378. superio_exit();
  379. switch (chip_type) {
  380. case IT8702_ID:
  381. max_units = 255;
  382. break;
  383. case IT8712_ID:
  384. max_units = (chip_rev < 8) ? 255 : 65535;
  385. break;
  386. case IT8716_ID:
  387. case IT8726_ID:
  388. max_units = 65535;
  389. break;
  390. case IT8607_ID:
  391. case IT8620_ID:
  392. case IT8622_ID:
  393. case IT8625_ID:
  394. case IT8628_ID:
  395. case IT8655_ID:
  396. case IT8665_ID:
  397. case IT8686_ID:
  398. case IT8718_ID:
  399. case IT8720_ID:
  400. case IT8721_ID:
  401. case IT8728_ID:
  402. case IT8772_ID:
  403. case IT8783_ID:
  404. case IT8784_ID:
  405. case IT8786_ID:
  406. max_units = 65535;
  407. break;
  408. case IT8705_ID:
  409. pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
  410. chip_type, chip_rev);
  411. return -ENODEV;
  412. case NO_DEV_ID:
  413. pr_err("no device\n");
  414. return -ENODEV;
  415. default:
  416. pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
  417. chip_type, chip_rev);
  418. return -ENODEV;
  419. }
  420. rc = superio_enter();
  421. if (rc)
  422. return rc;
  423. superio_select(GPIO);
  424. superio_outb(WDT_TOV1, WDTCFG);
  425. superio_outb(0x00, WDTCTRL);
  426. superio_exit();
  427. if (timeout < 1 || timeout > max_units * 60) {
  428. timeout = DEFAULT_TIMEOUT;
  429. pr_warn("Timeout value out of range, use default %d sec\n",
  430. DEFAULT_TIMEOUT);
  431. }
  432. if (timeout > max_units)
  433. timeout = wdt_round_time(timeout);
  434. /* Register character device */
  435. major_number = register_chrdev(0, DEVICE_NAME, &watchdog_fops);
  436. if (major_number < 0) {
  437. pr_err("Failed to register character device\n");
  438. return major_number;
  439. }
  440. /* Create device class */
  441. watchdog_class = class_create(THIS_MODULE, CLASS_NAME);
  442. if (IS_ERR(watchdog_class)) {
  443. unregister_chrdev(major_number, DEVICE_NAME);
  444. pr_err("Failed to create device class\n");
  445. return PTR_ERR(watchdog_class);
  446. }
  447. watchdog_class->devnode = my_devnode;
  448. /* Create device node /dev/watchdog */
  449. watchdog_device = device_create(watchdog_class, NULL,
  450. MKDEV(major_number, 0),
  451. NULL, DEVICE_NAME);
  452. if (IS_ERR(watchdog_device)) {
  453. class_destroy(watchdog_class);
  454. unregister_chrdev(major_number, DEVICE_NAME);
  455. pr_err("Failed to create device node\n");
  456. return PTR_ERR(watchdog_device);
  457. }
  458. /* Initialize state */
  459. watchdog_state = 0;
  460. /* Initialize timer */
  461. timer_setup(&watchdog_timer, watchdog_timer_callback, 0);
  462. pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (testmode=%d)\n",
  463. chip_type, chip_rev, timeout, testmode);
  464. pr_info("Device /dev/watchdog created. Default state: OFF\n");
  465. return 0;
  466. }
  467. void watchdog_exit(void)
  468. {
  469. /* Ensure watchdog is disabled before exit */
  470. if (watchdog_state) {
  471. watchdog_disable();
  472. watchdog_state = 0;
  473. }
  474. /* Delete timer */
  475. del_timer_sync(&watchdog_timer);
  476. /* Remove device */
  477. device_destroy(watchdog_class, MKDEV(major_number, 0));
  478. class_destroy(watchdog_class);
  479. unregister_chrdev(major_number, DEVICE_NAME);
  480. pr_info("Watchdog driver exited\n");
  481. }