watchdog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. memset(status_buf, 0, sizeof(status_buf));
  238. if( watchdog_state == 1)
  239. {
  240. status_buf[0] = '1';
  241. }
  242. else
  243. {
  244. status_buf[0] = '0';
  245. }
  246. len = 1;
  247. mutex_unlock(&watchdog_mutex);
  248. if (copy_to_user(user_buf, status_buf, len))
  249. return -EFAULT;
  250. // *ppos += len;
  251. return len;
  252. }
  253. static ssize_t watchdog_write(struct file *file, const char __user *user_buf,
  254. size_t count, loff_t *ppos)
  255. {
  256. char buf[16];
  257. int cmd;
  258. int ret = count;
  259. if (count == 0 || count >= sizeof(buf))
  260. return -EINVAL;
  261. if (copy_from_user(buf, user_buf, count))
  262. return -EFAULT;
  263. buf[count] = '\0';
  264. cmd = parse_watchdog_command(buf, count);
  265. if (cmd < 0)
  266. return -EINVAL;
  267. mutex_lock(&watchdog_mutex);
  268. if (cmd == watchdog_state) {
  269. /* No state change */
  270. if (cmd)
  271. pr_info("Watchdog is already on\n");
  272. else
  273. pr_info("Watchdog is already off\n");
  274. } else {
  275. if (cmd) {
  276. /* Turn ON */
  277. if (watchdog_enable() == 0) {
  278. watchdog_state = 1;
  279. pr_info("Watchdog turned on successfully\n");
  280. } else {
  281. ret = -EIO;
  282. }
  283. } else {
  284. /* Turn OFF */
  285. if (watchdog_disable() == 0) {
  286. watchdog_state = 0;
  287. pr_info("Watchdog turned off successfully\n");
  288. } else {
  289. ret = -EIO;
  290. }
  291. }
  292. }
  293. mutex_unlock(&watchdog_mutex);
  294. return ret;
  295. }
  296. static int watchdog_open(struct inode *inode, struct file *file)
  297. {
  298. return 0;
  299. }
  300. static int watchdog_release(struct inode *inode, struct file *file)
  301. {
  302. return 0;
  303. }
  304. static const struct file_operations watchdog_fops = {
  305. .owner = THIS_MODULE,
  306. .read = watchdog_read,
  307. .write = watchdog_write,
  308. .open = watchdog_open,
  309. .release = watchdog_release,
  310. };
  311. static char *my_devnode(struct device *dev, umode_t *mode) {
  312. if (mode) {
  313. *mode = 0666;
  314. }
  315. return NULL;
  316. }
  317. static ssize_t watchdog_show(struct kobject *kobj, struct kobj_attribute *attr,
  318. char *buf)
  319. {
  320. return 0;
  321. }
  322. static ssize_t watchdog_store(struct kobject *kobj, struct kobj_attribute *attr,
  323. const char *buf, size_t count)
  324. {
  325. int cmd;
  326. int ret = count;
  327. // if (count == 0 || count >= sizeof(buf))
  328. // return -EINVAL;
  329. // if (strscpy(buf, user_buf, count) < 0)
  330. // return -EFAULT;
  331. // buf[count] = '\0';
  332. cmd = parse_watchdog_command(buf, count);
  333. if (cmd < 0)
  334. return -EINVAL;
  335. mutex_lock(&watchdog_mutex);
  336. if (cmd == watchdog_state) {
  337. /* No state change */
  338. if (cmd)
  339. pr_info("Watchdog is already on\n");
  340. else
  341. pr_info("Watchdog is already off\n");
  342. } else {
  343. if (cmd) {
  344. /* Turn ON */
  345. if (watchdog_enable() == 0) {
  346. watchdog_state = 1;
  347. pr_info("Watchdog turned on successfully\n");
  348. } else {
  349. ret = -EIO;
  350. }
  351. } else {
  352. /* Turn OFF */
  353. if (watchdog_disable() == 0) {
  354. watchdog_state = 0;
  355. pr_info("Watchdog turned off successfully\n");
  356. } else {
  357. ret = -EIO;
  358. }
  359. }
  360. }
  361. mutex_unlock(&watchdog_mutex);
  362. return ret;
  363. }
  364. static struct kobj_attribute watchdog_attr =
  365. __ATTR(watchdog, 0644, watchdog_show, watchdog_store);
  366. static struct attribute *watchdog_attrs[] = {
  367. &watchdog_attr.attr,
  368. NULL,
  369. };
  370. static struct attribute_group watchdog_attr_group = {
  371. .attrs = watchdog_attrs,
  372. };
  373. int watchdog_init(void)
  374. {
  375. u8 chip_rev;
  376. int rc;
  377. rc = sysfs_create_group(vfiec_kobj, &watchdog_attr_group);
  378. if (rc)
  379. {
  380. pr_err("Faiec_version to create sysfs group: %d\n", rc);
  381. return -1;
  382. }
  383. rc = superio_enter();
  384. if (rc)
  385. return rc;
  386. chip_type = superio_inw(CHIPID);
  387. chip_rev = superio_inb(CHIPREV) & 0x0f;
  388. superio_exit();
  389. switch (chip_type) {
  390. case IT8702_ID:
  391. max_units = 255;
  392. break;
  393. case IT8712_ID:
  394. max_units = (chip_rev < 8) ? 255 : 65535;
  395. break;
  396. case IT8716_ID:
  397. case IT8726_ID:
  398. max_units = 65535;
  399. break;
  400. case IT8607_ID:
  401. case IT8620_ID:
  402. case IT8622_ID:
  403. case IT8625_ID:
  404. case IT8628_ID:
  405. case IT8655_ID:
  406. case IT8665_ID:
  407. case IT8686_ID:
  408. case IT8718_ID:
  409. case IT8720_ID:
  410. case IT8721_ID:
  411. case IT8728_ID:
  412. case IT8772_ID:
  413. case IT8783_ID:
  414. case IT8784_ID:
  415. case IT8786_ID:
  416. max_units = 65535;
  417. break;
  418. case IT8705_ID:
  419. pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
  420. chip_type, chip_rev);
  421. return -ENODEV;
  422. case NO_DEV_ID:
  423. pr_err("no device\n");
  424. return -ENODEV;
  425. default:
  426. pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
  427. chip_type, chip_rev);
  428. return -ENODEV;
  429. }
  430. rc = superio_enter();
  431. if (rc)
  432. return rc;
  433. superio_select(GPIO);
  434. superio_outb(WDT_TOV1, WDTCFG);
  435. superio_outb(0x00, WDTCTRL);
  436. superio_exit();
  437. if (timeout < 1 || timeout > max_units * 60) {
  438. timeout = DEFAULT_TIMEOUT;
  439. pr_warn("Timeout value out of range, use default %d sec\n",
  440. DEFAULT_TIMEOUT);
  441. }
  442. if (timeout > max_units)
  443. timeout = wdt_round_time(timeout);
  444. /* Register character device */
  445. major_number = register_chrdev(0, DEVICE_NAME, &watchdog_fops);
  446. if (major_number < 0) {
  447. pr_err("Failed to register character device\n");
  448. return major_number;
  449. }
  450. /* Create device class */
  451. watchdog_class = class_create(THIS_MODULE, CLASS_NAME);
  452. if (IS_ERR(watchdog_class)) {
  453. unregister_chrdev(major_number, DEVICE_NAME);
  454. pr_err("Failed to create device class\n");
  455. return PTR_ERR(watchdog_class);
  456. }
  457. watchdog_class->devnode = my_devnode;
  458. /* Create device node /dev/watchdog */
  459. watchdog_device = device_create(watchdog_class, NULL,
  460. MKDEV(major_number, 0),
  461. NULL, DEVICE_NAME);
  462. if (IS_ERR(watchdog_device)) {
  463. class_destroy(watchdog_class);
  464. unregister_chrdev(major_number, DEVICE_NAME);
  465. pr_err("Failed to create device node\n");
  466. return PTR_ERR(watchdog_device);
  467. }
  468. /* Initialize state */
  469. watchdog_state = 0;
  470. /* Initialize timer */
  471. timer_setup(&watchdog_timer, watchdog_timer_callback, 0);
  472. pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (testmode=%d)\n",
  473. chip_type, chip_rev, timeout, testmode);
  474. pr_info("Device /dev/watchdog created. Default state: OFF\n");
  475. return 0;
  476. }
  477. void watchdog_exit(void)
  478. {
  479. /* Ensure watchdog is disabled before exit */
  480. if (watchdog_state) {
  481. watchdog_disable();
  482. watchdog_state = 0;
  483. }
  484. /* Delete timer */
  485. del_timer_sync(&watchdog_timer);
  486. sysfs_remove_group(vfiec_kobj, &watchdog_attr_group);
  487. /* Remove device */
  488. device_destroy(watchdog_class, MKDEV(major_number, 0));
  489. class_destroy(watchdog_class);
  490. unregister_chrdev(major_number, DEVICE_NAME);
  491. pr_info("Watchdog driver exited\n");
  492. }