pcwd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. * PC Watchdog Driver
  3. * by Ken Hollis (khollis@bitgate.com)
  4. *
  5. * Permission granted from Simon Machell (73244.1270@compuserve.com)
  6. * Written for the Linux Kernel, and GPLed by Ken Hollis
  7. *
  8. * 960107 Added request_region routines, modulized the whole thing.
  9. * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added
  10. * WD_TIMEOUT define.
  11. * 960216 Added eof marker on the file, and changed verbose messages.
  12. * 960716 Made functional and cosmetic changes to the source for
  13. * inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
  14. * 960717 Removed read/seek routines, replaced with ioctl. Also, added
  15. * check_region command due to Alan's suggestion.
  16. * 960821 Made changes to compile in newer 2.0.x kernels. Added
  17. * "cold reboot sense" entry.
  18. * 960825 Made a few changes to code, deleted some defines and made
  19. * typedefs to replace them. Made heartbeat reset only available
  20. * via ioctl, and removed the write routine.
  21. * 960828 Added new items for PC Watchdog Rev.C card.
  22. * 960829 Changed around all of the IOCTLs, added new features,
  23. * added watchdog disable/re-enable routines. Added firmware
  24. * version reporting. Added read routine for temperature.
  25. * Removed some extra defines, added an autodetect Revision
  26. * routine.
  27. * 961006 Revised some documentation, fixed some cosmetic bugs. Made
  28. * drivers to panic the system if it's overheating at bootup.
  29. * 961118 Changed some verbiage on some of the output, tidied up
  30. * code bits, and added compatibility to 2.1.x.
  31. * 970912 Enabled board on open and disable on close.
  32. * 971107 Took account of recent VFS changes (broke read).
  33. * 971210 Disable board on initialisation in case board already ticking.
  34. * 971222 Changed open/close for temperature handling
  35. * Michael Meskes <meskes@debian.org>.
  36. * 980112 Used minor numbers from include/linux/miscdevice.h
  37. * 990403 Clear reset status after reading control status register in
  38. * pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>]
  39. * 990605 Made changes to code to support Firmware 1.22a, added
  40. * fairly useless proc entry.
  41. * 990610 removed said useless proc code for the merge <alan>
  42. * 000403 Removed last traces of proc code. <davej>
  43. * 011214 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
  44. * Added timeout module option to override default
  45. */
  46. /*
  47. * A bells and whistles driver is available from http://www.pcwd.de/
  48. * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
  49. */
  50. #include <linux/config.h> /* For CONFIG_WATCHDOG_NOWAYOUT/... */
  51. #include <linux/module.h> /* For module specific items */
  52. #include <linux/moduleparam.h> /* For new moduleparam's */
  53. #include <linux/types.h> /* For standard types (like size_t) */
  54. #include <linux/errno.h> /* For the -ENODEV/... values */
  55. #include <linux/kernel.h> /* For printk/panic/... */
  56. #include <linux/delay.h> /* For mdelay function */
  57. #include <linux/timer.h> /* For timer related operations */
  58. #include <linux/jiffies.h> /* For jiffies stuff */
  59. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
  60. #include <linux/watchdog.h> /* For the watchdog specific items */
  61. #include <linux/notifier.h> /* For notifier support */
  62. #include <linux/reboot.h> /* For reboot_notifier stuff */
  63. #include <linux/init.h> /* For __init/__exit/... */
  64. #include <linux/fs.h> /* For file operations */
  65. #include <linux/ioport.h> /* For io-port access */
  66. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  67. #include <linux/sched.h> /* TASK_INTERRUPTIBLE, set_current_state() and friends */
  68. #include <linux/slab.h> /* For kmalloc */
  69. #include <asm/uaccess.h> /* For copy_to_user/put_user/... */
  70. #include <asm/io.h> /* For inb/outb/... */
  71. /* Module and version information */
  72. #define WD_VER "1.16 (03/01/2006)"
  73. #define PFX "pcwd: "
  74. /*
  75. * It should be noted that PCWD_REVISION_B was removed because A and B
  76. * are essentially the same types of card, with the exception that B
  77. * has temperature reporting. Since I didn't receive a Rev.B card,
  78. * the Rev.B card is not supported. (It's a good thing too, as they
  79. * are no longer in production.)
  80. */
  81. #define PCWD_REVISION_A 1
  82. #define PCWD_REVISION_C 2
  83. /*
  84. * These are the defines that describe the control status #1 bits for the
  85. * PC Watchdog card, revision A.
  86. */
  87. #define WD_WDRST 0x01 /* Previously reset state */
  88. #define WD_T110 0x02 /* Temperature overheat sense */
  89. #define WD_HRTBT 0x04 /* Heartbeat sense */
  90. #define WD_RLY2 0x08 /* External relay triggered */
  91. #define WD_SRLY2 0x80 /* Software external relay triggered */
  92. /*
  93. * These are the defines that describe the control status #1 bits for the
  94. * PC Watchdog card, revision C.
  95. */
  96. #define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
  97. #define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
  98. #define WD_REVC_TTRP 0x04 /* Temperature Trip status */
  99. /* max. time we give an ISA watchdog card to process a command */
  100. /* 500ms for each 4 bit response (according to spec.) */
  101. #define ISA_COMMAND_TIMEOUT 1000
  102. /* Watchdog's internal commands */
  103. #define CMD_ISA_IDLE 0x00
  104. #define CMD_ISA_VERSION_INTEGER 0x01
  105. #define CMD_ISA_VERSION_TENTH 0x02
  106. #define CMD_ISA_VERSION_HUNDRETH 0x03
  107. #define CMD_ISA_VERSION_MINOR 0x04
  108. #define CMD_ISA_SWITCH_SETTINGS 0x05
  109. #define CMD_ISA_DELAY_TIME_2SECS 0x0A
  110. #define CMD_ISA_DELAY_TIME_4SECS 0x0B
  111. #define CMD_ISA_DELAY_TIME_8SECS 0x0C
  112. /*
  113. * We are using an kernel timer to do the pinging of the watchdog
  114. * every ~500ms. We try to set the internal heartbeat of the
  115. * watchdog to 2 ms.
  116. */
  117. #define WDT_INTERVAL (HZ/2+1)
  118. /* We can only use 1 card due to the /dev/watchdog restriction */
  119. static int cards_found;
  120. /* internal variables */
  121. static atomic_t open_allowed = ATOMIC_INIT(1);
  122. static char expect_close;
  123. static int temp_panic;
  124. static struct { /* this is private data for each ISA-PC watchdog card */
  125. int revision; /* The card's revision */
  126. int supports_temp; /* Wether or not the card has a temperature device */
  127. int command_mode; /* Wether or not the card is in command mode */
  128. int boot_status; /* The card's boot status */
  129. int io_addr; /* The cards I/O address */
  130. spinlock_t io_lock; /* the lock for io operations */
  131. struct timer_list timer; /* The timer that pings the watchdog */
  132. unsigned long next_heartbeat; /* the next_heartbeat for the timer */
  133. } pcwd_private;
  134. /* module parameters */
  135. #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */
  136. static int heartbeat = WATCHDOG_HEARTBEAT;
  137. module_param(heartbeat, int, 0);
  138. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  139. static int nowayout = WATCHDOG_NOWAYOUT;
  140. module_param(nowayout, int, 0);
  141. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  142. /*
  143. * Internal functions
  144. */
  145. static int send_isa_command(int cmd)
  146. {
  147. int i;
  148. int control_status;
  149. int port0, last_port0; /* Double read for stabilising */
  150. /* The WCMD bit must be 1 and the command is only 4 bits in size */
  151. control_status = (cmd & 0x0F) | 0x80;
  152. outb_p(control_status, pcwd_private.io_addr + 2);
  153. udelay(ISA_COMMAND_TIMEOUT);
  154. port0 = inb_p(pcwd_private.io_addr);
  155. for (i = 0; i < 25; ++i) {
  156. last_port0 = port0;
  157. port0 = inb_p(pcwd_private.io_addr);
  158. if (port0 == last_port0)
  159. break; /* Data is stable */
  160. udelay (250);
  161. }
  162. return port0;
  163. }
  164. static int set_command_mode(void)
  165. {
  166. int i, found=0, count=0;
  167. /* Set the card into command mode */
  168. spin_lock(&pcwd_private.io_lock);
  169. while ((!found) && (count < 3)) {
  170. i = send_isa_command(CMD_ISA_IDLE);
  171. if (i == 0x00)
  172. found = 1;
  173. else if (i == 0xF3) {
  174. /* Card does not like what we've done to it */
  175. outb_p(0x00, pcwd_private.io_addr + 2);
  176. udelay(1200); /* Spec says wait 1ms */
  177. outb_p(0x00, pcwd_private.io_addr + 2);
  178. udelay(ISA_COMMAND_TIMEOUT);
  179. }
  180. count++;
  181. }
  182. spin_unlock(&pcwd_private.io_lock);
  183. pcwd_private.command_mode = found;
  184. return(found);
  185. }
  186. static void unset_command_mode(void)
  187. {
  188. /* Set the card into normal mode */
  189. spin_lock(&pcwd_private.io_lock);
  190. outb_p(0x00, pcwd_private.io_addr + 2);
  191. udelay(ISA_COMMAND_TIMEOUT);
  192. spin_unlock(&pcwd_private.io_lock);
  193. pcwd_private.command_mode = 0;
  194. }
  195. static void pcwd_timer_ping(unsigned long data)
  196. {
  197. int wdrst_stat;
  198. /* If we got a heartbeat pulse within the WDT_INTERVAL
  199. * we agree to ping the WDT */
  200. if(time_before(jiffies, pcwd_private.next_heartbeat)) {
  201. /* Ping the watchdog */
  202. spin_lock(&pcwd_private.io_lock);
  203. if (pcwd_private.revision == PCWD_REVISION_A) {
  204. /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
  205. wdrst_stat = inb_p(pcwd_private.io_addr);
  206. wdrst_stat &= 0x0F;
  207. wdrst_stat |= WD_WDRST;
  208. outb_p(wdrst_stat, pcwd_private.io_addr + 1);
  209. } else {
  210. /* Re-trigger watchdog by writing to port 0 */
  211. outb_p(0x00, pcwd_private.io_addr);
  212. }
  213. /* Re-set the timer interval */
  214. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  215. spin_unlock(&pcwd_private.io_lock);
  216. } else {
  217. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  218. }
  219. }
  220. static int pcwd_start(void)
  221. {
  222. int stat_reg;
  223. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  224. /* Start the timer */
  225. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  226. /* Enable the port */
  227. if (pcwd_private.revision == PCWD_REVISION_C) {
  228. spin_lock(&pcwd_private.io_lock);
  229. outb_p(0x00, pcwd_private.io_addr + 3);
  230. udelay(ISA_COMMAND_TIMEOUT);
  231. stat_reg = inb_p(pcwd_private.io_addr + 2);
  232. spin_unlock(&pcwd_private.io_lock);
  233. if (stat_reg & 0x10) {
  234. printk(KERN_INFO PFX "Could not start watchdog\n");
  235. return -EIO;
  236. }
  237. }
  238. return 0;
  239. }
  240. static int pcwd_stop(void)
  241. {
  242. int stat_reg;
  243. /* Stop the timer */
  244. del_timer(&pcwd_private.timer);
  245. /* Disable the board */
  246. if (pcwd_private.revision == PCWD_REVISION_C) {
  247. spin_lock(&pcwd_private.io_lock);
  248. outb_p(0xA5, pcwd_private.io_addr + 3);
  249. udelay(ISA_COMMAND_TIMEOUT);
  250. outb_p(0xA5, pcwd_private.io_addr + 3);
  251. udelay(ISA_COMMAND_TIMEOUT);
  252. stat_reg = inb_p(pcwd_private.io_addr + 2);
  253. spin_unlock(&pcwd_private.io_lock);
  254. if ((stat_reg & 0x10) == 0) {
  255. printk(KERN_INFO PFX "Could not stop watchdog\n");
  256. return -EIO;
  257. }
  258. }
  259. return 0;
  260. }
  261. static int pcwd_keepalive(void)
  262. {
  263. /* user land ping */
  264. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  265. return 0;
  266. }
  267. static int pcwd_set_heartbeat(int t)
  268. {
  269. if ((t < 2) || (t > 7200)) /* arbitrary upper limit */
  270. return -EINVAL;
  271. heartbeat = t;
  272. return 0;
  273. }
  274. static int pcwd_get_status(int *status)
  275. {
  276. int card_status;
  277. *status=0;
  278. spin_lock(&pcwd_private.io_lock);
  279. if (pcwd_private.revision == PCWD_REVISION_A)
  280. /* Rev A cards return status information from
  281. * the base register, which is used for the
  282. * temperature in other cards. */
  283. card_status = inb(pcwd_private.io_addr);
  284. else {
  285. /* Rev C cards return card status in the base
  286. * address + 1 register. And use different bits
  287. * to indicate a card initiated reset, and an
  288. * over-temperature condition. And the reboot
  289. * status can be reset. */
  290. card_status = inb(pcwd_private.io_addr + 1);
  291. }
  292. spin_unlock(&pcwd_private.io_lock);
  293. if (pcwd_private.revision == PCWD_REVISION_A) {
  294. if (card_status & WD_WDRST)
  295. *status |= WDIOF_CARDRESET;
  296. if (card_status & WD_T110) {
  297. *status |= WDIOF_OVERHEAT;
  298. if (temp_panic) {
  299. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  300. kernel_power_off();
  301. }
  302. }
  303. } else {
  304. if (card_status & WD_REVC_WTRP)
  305. *status |= WDIOF_CARDRESET;
  306. if (card_status & WD_REVC_TTRP) {
  307. *status |= WDIOF_OVERHEAT;
  308. if (temp_panic) {
  309. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  310. kernel_power_off();
  311. }
  312. }
  313. }
  314. return 0;
  315. }
  316. static int pcwd_clear_status(void)
  317. {
  318. if (pcwd_private.revision == PCWD_REVISION_C) {
  319. spin_lock(&pcwd_private.io_lock);
  320. outb_p(0x00, pcwd_private.io_addr + 1); /* clear reset status */
  321. spin_unlock(&pcwd_private.io_lock);
  322. }
  323. return 0;
  324. }
  325. static int pcwd_get_temperature(int *temperature)
  326. {
  327. /* check that port 0 gives temperature info and no command results */
  328. if (pcwd_private.command_mode)
  329. return -1;
  330. *temperature = 0;
  331. if (!pcwd_private.supports_temp)
  332. return -ENODEV;
  333. /*
  334. * Convert celsius to fahrenheit, since this was
  335. * the decided 'standard' for this return value.
  336. */
  337. spin_lock(&pcwd_private.io_lock);
  338. *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
  339. spin_unlock(&pcwd_private.io_lock);
  340. return 0;
  341. }
  342. /*
  343. * /dev/watchdog handling
  344. */
  345. static int pcwd_ioctl(struct inode *inode, struct file *file,
  346. unsigned int cmd, unsigned long arg)
  347. {
  348. int rv;
  349. int status;
  350. int temperature;
  351. int new_heartbeat;
  352. int __user *argp = (int __user *)arg;
  353. static struct watchdog_info ident = {
  354. .options = WDIOF_OVERHEAT |
  355. WDIOF_CARDRESET |
  356. WDIOF_KEEPALIVEPING |
  357. WDIOF_SETTIMEOUT |
  358. WDIOF_MAGICCLOSE,
  359. .firmware_version = 1,
  360. .identity = "PCWD",
  361. };
  362. switch(cmd) {
  363. default:
  364. return -ENOIOCTLCMD;
  365. case WDIOC_GETSUPPORT:
  366. if(copy_to_user(argp, &ident, sizeof(ident)))
  367. return -EFAULT;
  368. return 0;
  369. case WDIOC_GETSTATUS:
  370. pcwd_get_status(&status);
  371. return put_user(status, argp);
  372. case WDIOC_GETBOOTSTATUS:
  373. return put_user(pcwd_private.boot_status, argp);
  374. case WDIOC_GETTEMP:
  375. if (pcwd_get_temperature(&temperature))
  376. return -EFAULT;
  377. return put_user(temperature, argp);
  378. case WDIOC_SETOPTIONS:
  379. if (pcwd_private.revision == PCWD_REVISION_C)
  380. {
  381. if(copy_from_user(&rv, argp, sizeof(int)))
  382. return -EFAULT;
  383. if (rv & WDIOS_DISABLECARD)
  384. {
  385. return pcwd_stop();
  386. }
  387. if (rv & WDIOS_ENABLECARD)
  388. {
  389. return pcwd_start();
  390. }
  391. if (rv & WDIOS_TEMPPANIC)
  392. {
  393. temp_panic = 1;
  394. }
  395. }
  396. return -EINVAL;
  397. case WDIOC_KEEPALIVE:
  398. pcwd_keepalive();
  399. return 0;
  400. case WDIOC_SETTIMEOUT:
  401. if (get_user(new_heartbeat, argp))
  402. return -EFAULT;
  403. if (pcwd_set_heartbeat(new_heartbeat))
  404. return -EINVAL;
  405. pcwd_keepalive();
  406. /* Fall */
  407. case WDIOC_GETTIMEOUT:
  408. return put_user(heartbeat, argp);
  409. }
  410. return 0;
  411. }
  412. static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
  413. loff_t *ppos)
  414. {
  415. if (len) {
  416. if (!nowayout) {
  417. size_t i;
  418. /* In case it was set long ago */
  419. expect_close = 0;
  420. for (i = 0; i != len; i++) {
  421. char c;
  422. if (get_user(c, buf + i))
  423. return -EFAULT;
  424. if (c == 'V')
  425. expect_close = 42;
  426. }
  427. }
  428. pcwd_keepalive();
  429. }
  430. return len;
  431. }
  432. static int pcwd_open(struct inode *inode, struct file *file)
  433. {
  434. if (!atomic_dec_and_test(&open_allowed) ) {
  435. atomic_inc( &open_allowed );
  436. return -EBUSY;
  437. }
  438. if (nowayout)
  439. __module_get(THIS_MODULE);
  440. /* Activate */
  441. pcwd_start();
  442. pcwd_keepalive();
  443. return nonseekable_open(inode, file);
  444. }
  445. static int pcwd_close(struct inode *inode, struct file *file)
  446. {
  447. if (expect_close == 42) {
  448. pcwd_stop();
  449. } else {
  450. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  451. pcwd_keepalive();
  452. }
  453. expect_close = 0;
  454. atomic_inc( &open_allowed );
  455. return 0;
  456. }
  457. /*
  458. * /dev/temperature handling
  459. */
  460. static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
  461. loff_t *ppos)
  462. {
  463. int temperature;
  464. if (pcwd_get_temperature(&temperature))
  465. return -EFAULT;
  466. if (copy_to_user(buf, &temperature, 1))
  467. return -EFAULT;
  468. return 1;
  469. }
  470. static int pcwd_temp_open(struct inode *inode, struct file *file)
  471. {
  472. if (!pcwd_private.supports_temp)
  473. return -ENODEV;
  474. return nonseekable_open(inode, file);
  475. }
  476. static int pcwd_temp_close(struct inode *inode, struct file *file)
  477. {
  478. return 0;
  479. }
  480. /*
  481. * Notify system
  482. */
  483. static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  484. {
  485. if (code==SYS_DOWN || code==SYS_HALT) {
  486. /* Turn the WDT off */
  487. pcwd_stop();
  488. }
  489. return NOTIFY_DONE;
  490. }
  491. /*
  492. * Kernel Interfaces
  493. */
  494. static struct file_operations pcwd_fops = {
  495. .owner = THIS_MODULE,
  496. .llseek = no_llseek,
  497. .write = pcwd_write,
  498. .ioctl = pcwd_ioctl,
  499. .open = pcwd_open,
  500. .release = pcwd_close,
  501. };
  502. static struct miscdevice pcwd_miscdev = {
  503. .minor = WATCHDOG_MINOR,
  504. .name = "watchdog",
  505. .fops = &pcwd_fops,
  506. };
  507. static struct file_operations pcwd_temp_fops = {
  508. .owner = THIS_MODULE,
  509. .llseek = no_llseek,
  510. .read = pcwd_temp_read,
  511. .open = pcwd_temp_open,
  512. .release = pcwd_temp_close,
  513. };
  514. static struct miscdevice temp_miscdev = {
  515. .minor = TEMP_MINOR,
  516. .name = "temperature",
  517. .fops = &pcwd_temp_fops,
  518. };
  519. static struct notifier_block pcwd_notifier = {
  520. .notifier_call = pcwd_notify_sys,
  521. };
  522. /*
  523. * Init & exit routines
  524. */
  525. static inline void get_support(void)
  526. {
  527. if (inb(pcwd_private.io_addr) != 0xF0)
  528. pcwd_private.supports_temp = 1;
  529. }
  530. static inline int get_revision(void)
  531. {
  532. int r = PCWD_REVISION_C;
  533. spin_lock(&pcwd_private.io_lock);
  534. /* REV A cards use only 2 io ports; test
  535. * presumes a floating bus reads as 0xff. */
  536. if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
  537. (inb(pcwd_private.io_addr + 3) == 0xFF))
  538. r=PCWD_REVISION_A;
  539. spin_unlock(&pcwd_private.io_lock);
  540. return r;
  541. }
  542. static inline char *get_firmware(void)
  543. {
  544. int one, ten, hund, minor;
  545. char *ret;
  546. ret = kmalloc(6, GFP_KERNEL);
  547. if(ret == NULL)
  548. return NULL;
  549. if (set_command_mode()) {
  550. one = send_isa_command(CMD_ISA_VERSION_INTEGER);
  551. ten = send_isa_command(CMD_ISA_VERSION_TENTH);
  552. hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
  553. minor = send_isa_command(CMD_ISA_VERSION_MINOR);
  554. sprintf(ret, "%c.%c%c%c", one, ten, hund, minor);
  555. }
  556. else
  557. sprintf(ret, "ERROR");
  558. unset_command_mode();
  559. return(ret);
  560. }
  561. static inline int get_option_switches(void)
  562. {
  563. int rv=0;
  564. if (set_command_mode()) {
  565. /* Get switch settings */
  566. rv = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
  567. }
  568. unset_command_mode();
  569. return(rv);
  570. }
  571. static int __devinit pcwatchdog_init(int base_addr)
  572. {
  573. int ret;
  574. char *firmware;
  575. int option_switches;
  576. cards_found++;
  577. if (cards_found == 1)
  578. printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER);
  579. if (cards_found > 1) {
  580. printk(KERN_ERR PFX "This driver only supports 1 device\n");
  581. return -ENODEV;
  582. }
  583. if (base_addr == 0x0000) {
  584. printk(KERN_ERR PFX "No I/O-Address for card detected\n");
  585. return -ENODEV;
  586. }
  587. pcwd_private.io_addr = base_addr;
  588. /* Check card's revision */
  589. pcwd_private.revision = get_revision();
  590. if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
  591. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  592. pcwd_private.io_addr);
  593. pcwd_private.io_addr = 0x0000;
  594. return -EIO;
  595. }
  596. /* Initial variables */
  597. pcwd_private.supports_temp = 0;
  598. temp_panic = 0;
  599. pcwd_private.boot_status = 0x0000;
  600. /* get the boot_status */
  601. pcwd_get_status(&pcwd_private.boot_status);
  602. /* clear the "card caused reboot" flag */
  603. pcwd_clear_status();
  604. init_timer(&pcwd_private.timer);
  605. pcwd_private.timer.function = pcwd_timer_ping;
  606. pcwd_private.timer.data = 0;
  607. /* Disable the board */
  608. pcwd_stop();
  609. /* Check whether or not the card supports the temperature device */
  610. get_support();
  611. /* Get some extra info from the hardware (in command/debug/diag mode) */
  612. if (pcwd_private.revision == PCWD_REVISION_A)
  613. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
  614. else if (pcwd_private.revision == PCWD_REVISION_C) {
  615. firmware = get_firmware();
  616. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
  617. pcwd_private.io_addr, firmware);
  618. kfree(firmware);
  619. option_switches = get_option_switches();
  620. printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
  621. option_switches,
  622. ((option_switches & 0x10) ? "ON" : "OFF"),
  623. ((option_switches & 0x08) ? "ON" : "OFF"));
  624. /* Reprogram internal heartbeat to 2 seconds */
  625. if (set_command_mode()) {
  626. send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
  627. unset_command_mode();
  628. }
  629. } else {
  630. /* Should NEVER happen, unless get_revision() fails. */
  631. printk(KERN_INFO PFX "Unable to get revision\n");
  632. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  633. pcwd_private.io_addr = 0x0000;
  634. return -1;
  635. }
  636. if (pcwd_private.supports_temp)
  637. printk(KERN_INFO PFX "Temperature Option Detected\n");
  638. if (pcwd_private.boot_status & WDIOF_CARDRESET)
  639. printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
  640. if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
  641. printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
  642. printk(KERN_EMERG PFX "CPU Overheat\n");
  643. }
  644. if (pcwd_private.boot_status == 0)
  645. printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
  646. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  647. if (pcwd_set_heartbeat(heartbeat)) {
  648. pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
  649. printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n",
  650. WATCHDOG_HEARTBEAT);
  651. }
  652. ret = register_reboot_notifier(&pcwd_notifier);
  653. if (ret) {
  654. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  655. ret);
  656. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  657. pcwd_private.io_addr = 0x0000;
  658. return ret;
  659. }
  660. if (pcwd_private.supports_temp) {
  661. ret = misc_register(&temp_miscdev);
  662. if (ret) {
  663. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  664. TEMP_MINOR, ret);
  665. unregister_reboot_notifier(&pcwd_notifier);
  666. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  667. pcwd_private.io_addr = 0x0000;
  668. return ret;
  669. }
  670. }
  671. ret = misc_register(&pcwd_miscdev);
  672. if (ret) {
  673. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  674. WATCHDOG_MINOR, ret);
  675. if (pcwd_private.supports_temp)
  676. misc_deregister(&temp_miscdev);
  677. unregister_reboot_notifier(&pcwd_notifier);
  678. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  679. pcwd_private.io_addr = 0x0000;
  680. return ret;
  681. }
  682. printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  683. heartbeat, nowayout);
  684. return 0;
  685. }
  686. static void __devexit pcwatchdog_exit(void)
  687. {
  688. /* Disable the board */
  689. if (!nowayout)
  690. pcwd_stop();
  691. /* Deregister */
  692. misc_deregister(&pcwd_miscdev);
  693. if (pcwd_private.supports_temp)
  694. misc_deregister(&temp_miscdev);
  695. unregister_reboot_notifier(&pcwd_notifier);
  696. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  697. pcwd_private.io_addr = 0x0000;
  698. cards_found--;
  699. }
  700. /*
  701. * The ISA cards have a heartbeat bit in one of the registers, which
  702. * register is card dependent. The heartbeat bit is monitored, and if
  703. * found, is considered proof that a Berkshire card has been found.
  704. * The initial rate is once per second at board start up, then twice
  705. * per second for normal operation.
  706. */
  707. static int __init pcwd_checkcard(int base_addr)
  708. {
  709. int port0, last_port0; /* Reg 0, in case it's REV A */
  710. int port1, last_port1; /* Register 1 for REV C cards */
  711. int i;
  712. int retval;
  713. if (!request_region (base_addr, 4, "PCWD")) {
  714. printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
  715. return 0;
  716. }
  717. retval = 0;
  718. port0 = inb_p(base_addr); /* For REV A boards */
  719. port1 = inb_p(base_addr + 1); /* For REV C boards */
  720. if (port0 != 0xff || port1 != 0xff) {
  721. /* Not an 'ff' from a floating bus, so must be a card! */
  722. for (i = 0; i < 4; ++i) {
  723. msleep(500);
  724. last_port0 = port0;
  725. last_port1 = port1;
  726. port0 = inb_p(base_addr);
  727. port1 = inb_p(base_addr + 1);
  728. /* Has either hearbeat bit changed? */
  729. if ((port0 ^ last_port0) & WD_HRTBT ||
  730. (port1 ^ last_port1) & WD_REVC_HRBT) {
  731. retval = 1;
  732. break;
  733. }
  734. }
  735. }
  736. release_region (base_addr, 4);
  737. return retval;
  738. }
  739. /*
  740. * These are the auto-probe addresses available.
  741. *
  742. * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
  743. * Revision A has an address range of 2 addresses, while Revision C has 4.
  744. */
  745. static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
  746. static int __init pcwd_init_module(void)
  747. {
  748. int i, found = 0;
  749. spin_lock_init(&pcwd_private.io_lock);
  750. for (i = 0; pcwd_ioports[i] != 0; i++) {
  751. if (pcwd_checkcard(pcwd_ioports[i])) {
  752. if (!(pcwatchdog_init(pcwd_ioports[i])))
  753. found++;
  754. }
  755. }
  756. if (!found) {
  757. printk (KERN_INFO PFX "No card detected, or port not available\n");
  758. return -ENODEV;
  759. }
  760. return 0;
  761. }
  762. static void __exit pcwd_cleanup_module(void)
  763. {
  764. if (pcwd_private.io_addr)
  765. pcwatchdog_exit();
  766. return;
  767. }
  768. module_init(pcwd_init_module);
  769. module_exit(pcwd_cleanup_module);
  770. MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
  771. MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
  772. MODULE_LICENSE("GPL");
  773. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  774. MODULE_ALIAS_MISCDEV(TEMP_MINOR);