pcwd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 (06/12/2004)"
  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 struct timer_list timer;
  124. static unsigned long next_heartbeat;
  125. static int temp_panic;
  126. static int revision; /* The card's revision */
  127. static int supports_temp; /* Wether or not the card has a temperature device */
  128. static int command_mode; /* Wether or not the card is in command mode */
  129. static int initial_status; /* The card's boot status */
  130. static int current_readport; /* The cards I/O address */
  131. static spinlock_t io_lock;
  132. /* module parameters */
  133. #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */
  134. static int heartbeat = WATCHDOG_HEARTBEAT;
  135. module_param(heartbeat, int, 0);
  136. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  137. static int nowayout = WATCHDOG_NOWAYOUT;
  138. module_param(nowayout, int, 0);
  139. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  140. /*
  141. * Internal functions
  142. */
  143. static int send_isa_command(int cmd)
  144. {
  145. int i;
  146. int control_status;
  147. int port0, last_port0; /* Double read for stabilising */
  148. /* The WCMD bit must be 1 and the command is only 4 bits in size */
  149. control_status = (cmd & 0x0F) | 0x80;
  150. outb_p(control_status, current_readport + 2);
  151. udelay(ISA_COMMAND_TIMEOUT);
  152. port0 = inb_p(current_readport);
  153. for (i = 0; i < 25; ++i) {
  154. last_port0 = port0;
  155. port0 = inb_p(current_readport);
  156. if (port0 == last_port0)
  157. break; /* Data is stable */
  158. udelay (250);
  159. }
  160. return port0;
  161. }
  162. static int set_command_mode(void)
  163. {
  164. int i, found=0, count=0;
  165. /* Set the card into command mode */
  166. spin_lock(&io_lock);
  167. while ((!found) && (count < 3)) {
  168. i = send_isa_command(CMD_ISA_IDLE);
  169. if (i == 0x00)
  170. found = 1;
  171. else if (i == 0xF3) {
  172. /* Card does not like what we've done to it */
  173. outb_p(0x00, current_readport + 2);
  174. udelay(1200); /* Spec says wait 1ms */
  175. outb_p(0x00, current_readport + 2);
  176. udelay(ISA_COMMAND_TIMEOUT);
  177. }
  178. count++;
  179. }
  180. spin_unlock(&io_lock);
  181. command_mode = found;
  182. return(found);
  183. }
  184. static void unset_command_mode(void)
  185. {
  186. /* Set the card into normal mode */
  187. spin_lock(&io_lock);
  188. outb_p(0x00, current_readport + 2);
  189. udelay(ISA_COMMAND_TIMEOUT);
  190. spin_unlock(&io_lock);
  191. command_mode = 0;
  192. }
  193. static void pcwd_timer_ping(unsigned long data)
  194. {
  195. int wdrst_stat;
  196. /* If we got a heartbeat pulse within the WDT_INTERVAL
  197. * we agree to ping the WDT */
  198. if(time_before(jiffies, next_heartbeat)) {
  199. /* Ping the watchdog */
  200. spin_lock(&io_lock);
  201. if (revision == PCWD_REVISION_A) {
  202. /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
  203. wdrst_stat = inb_p(current_readport);
  204. wdrst_stat &= 0x0F;
  205. wdrst_stat |= WD_WDRST;
  206. outb_p(wdrst_stat, current_readport + 1);
  207. } else {
  208. /* Re-trigger watchdog by writing to port 0 */
  209. outb_p(0x00, current_readport);
  210. }
  211. /* Re-set the timer interval */
  212. mod_timer(&timer, jiffies + WDT_INTERVAL);
  213. spin_unlock(&io_lock);
  214. } else {
  215. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  216. }
  217. }
  218. static int pcwd_start(void)
  219. {
  220. int stat_reg;
  221. next_heartbeat = jiffies + (heartbeat * HZ);
  222. /* Start the timer */
  223. mod_timer(&timer, jiffies + WDT_INTERVAL);
  224. /* Enable the port */
  225. if (revision == PCWD_REVISION_C) {
  226. spin_lock(&io_lock);
  227. outb_p(0x00, current_readport + 3);
  228. udelay(ISA_COMMAND_TIMEOUT);
  229. stat_reg = inb_p(current_readport + 2);
  230. spin_unlock(&io_lock);
  231. if (stat_reg & 0x10) {
  232. printk(KERN_INFO PFX "Could not start watchdog\n");
  233. return -EIO;
  234. }
  235. }
  236. return 0;
  237. }
  238. static int pcwd_stop(void)
  239. {
  240. int stat_reg;
  241. /* Stop the timer */
  242. del_timer(&timer);
  243. /* Disable the board */
  244. if (revision == PCWD_REVISION_C) {
  245. spin_lock(&io_lock);
  246. outb_p(0xA5, current_readport + 3);
  247. udelay(ISA_COMMAND_TIMEOUT);
  248. outb_p(0xA5, current_readport + 3);
  249. udelay(ISA_COMMAND_TIMEOUT);
  250. stat_reg = inb_p(current_readport + 2);
  251. spin_unlock(&io_lock);
  252. if ((stat_reg & 0x10) == 0) {
  253. printk(KERN_INFO PFX "Could not stop watchdog\n");
  254. return -EIO;
  255. }
  256. }
  257. return 0;
  258. }
  259. static int pcwd_keepalive(void)
  260. {
  261. /* user land ping */
  262. next_heartbeat = jiffies + (heartbeat * HZ);
  263. return 0;
  264. }
  265. static int pcwd_set_heartbeat(int t)
  266. {
  267. if ((t < 2) || (t > 7200)) /* arbitrary upper limit */
  268. return -EINVAL;
  269. heartbeat = t;
  270. return 0;
  271. }
  272. static int pcwd_get_status(int *status)
  273. {
  274. int card_status;
  275. *status=0;
  276. spin_lock(&io_lock);
  277. if (revision == PCWD_REVISION_A)
  278. /* Rev A cards return status information from
  279. * the base register, which is used for the
  280. * temperature in other cards. */
  281. card_status = inb(current_readport);
  282. else {
  283. /* Rev C cards return card status in the base
  284. * address + 1 register. And use different bits
  285. * to indicate a card initiated reset, and an
  286. * over-temperature condition. And the reboot
  287. * status can be reset. */
  288. card_status = inb(current_readport + 1);
  289. }
  290. spin_unlock(&io_lock);
  291. if (revision == PCWD_REVISION_A) {
  292. if (card_status & WD_WDRST)
  293. *status |= WDIOF_CARDRESET;
  294. if (card_status & WD_T110) {
  295. *status |= WDIOF_OVERHEAT;
  296. if (temp_panic) {
  297. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  298. kernel_power_off();
  299. }
  300. }
  301. } else {
  302. if (card_status & WD_REVC_WTRP)
  303. *status |= WDIOF_CARDRESET;
  304. if (card_status & WD_REVC_TTRP) {
  305. *status |= WDIOF_OVERHEAT;
  306. if (temp_panic) {
  307. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  308. kernel_power_off();
  309. }
  310. }
  311. }
  312. return 0;
  313. }
  314. static int pcwd_clear_status(void)
  315. {
  316. if (revision == PCWD_REVISION_C) {
  317. spin_lock(&io_lock);
  318. outb_p(0x00, current_readport + 1); /* clear reset status */
  319. spin_unlock(&io_lock);
  320. }
  321. return 0;
  322. }
  323. static int pcwd_get_temperature(int *temperature)
  324. {
  325. /* check that port 0 gives temperature info and no command results */
  326. if (command_mode)
  327. return -1;
  328. *temperature = 0;
  329. if (!supports_temp)
  330. return -ENODEV;
  331. /*
  332. * Convert celsius to fahrenheit, since this was
  333. * the decided 'standard' for this return value.
  334. */
  335. spin_lock(&io_lock);
  336. *temperature = ((inb(current_readport)) * 9 / 5) + 32;
  337. spin_unlock(&io_lock);
  338. return 0;
  339. }
  340. /*
  341. * /dev/watchdog handling
  342. */
  343. static int pcwd_ioctl(struct inode *inode, struct file *file,
  344. unsigned int cmd, unsigned long arg)
  345. {
  346. int rv;
  347. int status;
  348. int temperature;
  349. int new_heartbeat;
  350. int __user *argp = (int __user *)arg;
  351. static struct watchdog_info ident = {
  352. .options = WDIOF_OVERHEAT |
  353. WDIOF_CARDRESET |
  354. WDIOF_KEEPALIVEPING |
  355. WDIOF_SETTIMEOUT |
  356. WDIOF_MAGICCLOSE,
  357. .firmware_version = 1,
  358. .identity = "PCWD",
  359. };
  360. switch(cmd) {
  361. default:
  362. return -ENOIOCTLCMD;
  363. case WDIOC_GETSUPPORT:
  364. if(copy_to_user(argp, &ident, sizeof(ident)))
  365. return -EFAULT;
  366. return 0;
  367. case WDIOC_GETSTATUS:
  368. pcwd_get_status(&status);
  369. return put_user(status, argp);
  370. case WDIOC_GETBOOTSTATUS:
  371. return put_user(initial_status, argp);
  372. case WDIOC_GETTEMP:
  373. if (pcwd_get_temperature(&temperature))
  374. return -EFAULT;
  375. return put_user(temperature, argp);
  376. case WDIOC_SETOPTIONS:
  377. if (revision == PCWD_REVISION_C)
  378. {
  379. if(copy_from_user(&rv, argp, sizeof(int)))
  380. return -EFAULT;
  381. if (rv & WDIOS_DISABLECARD)
  382. {
  383. return pcwd_stop();
  384. }
  385. if (rv & WDIOS_ENABLECARD)
  386. {
  387. return pcwd_start();
  388. }
  389. if (rv & WDIOS_TEMPPANIC)
  390. {
  391. temp_panic = 1;
  392. }
  393. }
  394. return -EINVAL;
  395. case WDIOC_KEEPALIVE:
  396. pcwd_keepalive();
  397. return 0;
  398. case WDIOC_SETTIMEOUT:
  399. if (get_user(new_heartbeat, argp))
  400. return -EFAULT;
  401. if (pcwd_set_heartbeat(new_heartbeat))
  402. return -EINVAL;
  403. pcwd_keepalive();
  404. /* Fall */
  405. case WDIOC_GETTIMEOUT:
  406. return put_user(heartbeat, argp);
  407. }
  408. return 0;
  409. }
  410. static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
  411. loff_t *ppos)
  412. {
  413. if (len) {
  414. if (!nowayout) {
  415. size_t i;
  416. /* In case it was set long ago */
  417. expect_close = 0;
  418. for (i = 0; i != len; i++) {
  419. char c;
  420. if (get_user(c, buf + i))
  421. return -EFAULT;
  422. if (c == 'V')
  423. expect_close = 42;
  424. }
  425. }
  426. pcwd_keepalive();
  427. }
  428. return len;
  429. }
  430. static int pcwd_open(struct inode *inode, struct file *file)
  431. {
  432. if (!atomic_dec_and_test(&open_allowed) ) {
  433. atomic_inc( &open_allowed );
  434. return -EBUSY;
  435. }
  436. if (nowayout)
  437. __module_get(THIS_MODULE);
  438. /* Activate */
  439. pcwd_start();
  440. pcwd_keepalive();
  441. return nonseekable_open(inode, file);
  442. }
  443. static int pcwd_close(struct inode *inode, struct file *file)
  444. {
  445. if (expect_close == 42) {
  446. pcwd_stop();
  447. } else {
  448. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  449. pcwd_keepalive();
  450. }
  451. expect_close = 0;
  452. atomic_inc( &open_allowed );
  453. return 0;
  454. }
  455. /*
  456. * /dev/temperature handling
  457. */
  458. static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
  459. loff_t *ppos)
  460. {
  461. int temperature;
  462. if (pcwd_get_temperature(&temperature))
  463. return -EFAULT;
  464. if (copy_to_user(buf, &temperature, 1))
  465. return -EFAULT;
  466. return 1;
  467. }
  468. static int pcwd_temp_open(struct inode *inode, struct file *file)
  469. {
  470. if (!supports_temp)
  471. return -ENODEV;
  472. return nonseekable_open(inode, file);
  473. }
  474. static int pcwd_temp_close(struct inode *inode, struct file *file)
  475. {
  476. return 0;
  477. }
  478. /*
  479. * Notify system
  480. */
  481. static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  482. {
  483. if (code==SYS_DOWN || code==SYS_HALT) {
  484. /* Turn the WDT off */
  485. pcwd_stop();
  486. }
  487. return NOTIFY_DONE;
  488. }
  489. /*
  490. * Kernel Interfaces
  491. */
  492. static struct file_operations pcwd_fops = {
  493. .owner = THIS_MODULE,
  494. .llseek = no_llseek,
  495. .write = pcwd_write,
  496. .ioctl = pcwd_ioctl,
  497. .open = pcwd_open,
  498. .release = pcwd_close,
  499. };
  500. static struct miscdevice pcwd_miscdev = {
  501. .minor = WATCHDOG_MINOR,
  502. .name = "watchdog",
  503. .fops = &pcwd_fops,
  504. };
  505. static struct file_operations pcwd_temp_fops = {
  506. .owner = THIS_MODULE,
  507. .llseek = no_llseek,
  508. .read = pcwd_temp_read,
  509. .open = pcwd_temp_open,
  510. .release = pcwd_temp_close,
  511. };
  512. static struct miscdevice temp_miscdev = {
  513. .minor = TEMP_MINOR,
  514. .name = "temperature",
  515. .fops = &pcwd_temp_fops,
  516. };
  517. static struct notifier_block pcwd_notifier = {
  518. .notifier_call = pcwd_notify_sys,
  519. };
  520. /*
  521. * Init & exit routines
  522. */
  523. static inline void get_support(void)
  524. {
  525. if (inb(current_readport) != 0xF0)
  526. supports_temp = 1;
  527. }
  528. static inline int get_revision(void)
  529. {
  530. int r = PCWD_REVISION_C;
  531. spin_lock(&io_lock);
  532. /* REV A cards use only 2 io ports; test
  533. * presumes a floating bus reads as 0xff. */
  534. if ((inb(current_readport + 2) == 0xFF) ||
  535. (inb(current_readport + 3) == 0xFF))
  536. r=PCWD_REVISION_A;
  537. spin_unlock(&io_lock);
  538. return r;
  539. }
  540. static inline char *get_firmware(void)
  541. {
  542. int one, ten, hund, minor;
  543. char *ret;
  544. ret = kmalloc(6, GFP_KERNEL);
  545. if(ret == NULL)
  546. return NULL;
  547. if (set_command_mode()) {
  548. one = send_isa_command(CMD_ISA_VERSION_INTEGER);
  549. ten = send_isa_command(CMD_ISA_VERSION_TENTH);
  550. hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
  551. minor = send_isa_command(CMD_ISA_VERSION_MINOR);
  552. sprintf(ret, "%c.%c%c%c", one, ten, hund, minor);
  553. }
  554. else
  555. sprintf(ret, "ERROR");
  556. unset_command_mode();
  557. return(ret);
  558. }
  559. static inline int get_option_switches(void)
  560. {
  561. int rv=0;
  562. if (set_command_mode()) {
  563. /* Get switch settings */
  564. rv = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
  565. }
  566. unset_command_mode();
  567. return(rv);
  568. }
  569. static int __devinit pcwatchdog_init(int base_addr)
  570. {
  571. int ret;
  572. char *firmware;
  573. int option_switches;
  574. cards_found++;
  575. if (cards_found == 1)
  576. printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER);
  577. if (cards_found > 1) {
  578. printk(KERN_ERR PFX "This driver only supports 1 device\n");
  579. return -ENODEV;
  580. }
  581. if (base_addr == 0x0000) {
  582. printk(KERN_ERR PFX "No I/O-Address for card detected\n");
  583. return -ENODEV;
  584. }
  585. current_readport = base_addr;
  586. /* Check card's revision */
  587. revision = get_revision();
  588. if (!request_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
  589. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  590. current_readport);
  591. current_readport = 0x0000;
  592. return -EIO;
  593. }
  594. /* Initial variables */
  595. supports_temp = 0;
  596. temp_panic = 0;
  597. initial_status = 0x0000;
  598. /* get the boot_status */
  599. pcwd_get_status(&initial_status);
  600. /* clear the "card caused reboot" flag */
  601. pcwd_clear_status();
  602. init_timer(&timer);
  603. timer.function = pcwd_timer_ping;
  604. timer.data = 0;
  605. /* Disable the board */
  606. pcwd_stop();
  607. /* Check whether or not the card supports the temperature device */
  608. get_support();
  609. /* Get some extra info from the hardware (in command/debug/diag mode) */
  610. if (revision == PCWD_REVISION_A)
  611. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", current_readport);
  612. else if (revision == PCWD_REVISION_C) {
  613. firmware = get_firmware();
  614. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
  615. current_readport, firmware);
  616. kfree(firmware);
  617. option_switches = get_option_switches();
  618. printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
  619. option_switches,
  620. ((option_switches & 0x10) ? "ON" : "OFF"),
  621. ((option_switches & 0x08) ? "ON" : "OFF"));
  622. /* Reprogram internal heartbeat to 2 seconds */
  623. if (set_command_mode()) {
  624. send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
  625. unset_command_mode();
  626. }
  627. } else {
  628. /* Should NEVER happen, unless get_revision() fails. */
  629. printk(KERN_INFO PFX "Unable to get revision\n");
  630. release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4);
  631. current_readport = 0x0000;
  632. return -1;
  633. }
  634. if (supports_temp)
  635. printk(KERN_INFO PFX "Temperature Option Detected\n");
  636. if (initial_status & WDIOF_CARDRESET)
  637. printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
  638. if (initial_status & WDIOF_OVERHEAT) {
  639. printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
  640. printk(KERN_EMERG PFX "CPU Overheat\n");
  641. }
  642. if (initial_status == 0)
  643. printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
  644. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  645. if (pcwd_set_heartbeat(heartbeat)) {
  646. pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
  647. printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n",
  648. WATCHDOG_HEARTBEAT);
  649. }
  650. ret = register_reboot_notifier(&pcwd_notifier);
  651. if (ret) {
  652. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  653. ret);
  654. release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4);
  655. current_readport = 0x0000;
  656. return ret;
  657. }
  658. if (supports_temp) {
  659. ret = misc_register(&temp_miscdev);
  660. if (ret) {
  661. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  662. TEMP_MINOR, ret);
  663. unregister_reboot_notifier(&pcwd_notifier);
  664. release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4);
  665. current_readport = 0x0000;
  666. return ret;
  667. }
  668. }
  669. ret = misc_register(&pcwd_miscdev);
  670. if (ret) {
  671. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  672. WATCHDOG_MINOR, ret);
  673. if (supports_temp)
  674. misc_deregister(&temp_miscdev);
  675. unregister_reboot_notifier(&pcwd_notifier);
  676. release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4);
  677. current_readport = 0x0000;
  678. return ret;
  679. }
  680. printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  681. heartbeat, nowayout);
  682. return 0;
  683. }
  684. static void __devexit pcwatchdog_exit(void)
  685. {
  686. /* Disable the board */
  687. if (!nowayout)
  688. pcwd_stop();
  689. /* Deregister */
  690. misc_deregister(&pcwd_miscdev);
  691. if (supports_temp)
  692. misc_deregister(&temp_miscdev);
  693. unregister_reboot_notifier(&pcwd_notifier);
  694. release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4);
  695. current_readport = 0x0000;
  696. cards_found--;
  697. }
  698. /*
  699. * The ISA cards have a heartbeat bit in one of the registers, which
  700. * register is card dependent. The heartbeat bit is monitored, and if
  701. * found, is considered proof that a Berkshire card has been found.
  702. * The initial rate is once per second at board start up, then twice
  703. * per second for normal operation.
  704. */
  705. static int __init pcwd_checkcard(int base_addr)
  706. {
  707. int port0, last_port0; /* Reg 0, in case it's REV A */
  708. int port1, last_port1; /* Register 1 for REV C cards */
  709. int i;
  710. int retval;
  711. if (!request_region (base_addr, 4, "PCWD")) {
  712. printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
  713. return 0;
  714. }
  715. retval = 0;
  716. port0 = inb_p(base_addr); /* For REV A boards */
  717. port1 = inb_p(base_addr + 1); /* For REV C boards */
  718. if (port0 != 0xff || port1 != 0xff) {
  719. /* Not an 'ff' from a floating bus, so must be a card! */
  720. for (i = 0; i < 4; ++i) {
  721. msleep(500);
  722. last_port0 = port0;
  723. last_port1 = port1;
  724. port0 = inb_p(base_addr);
  725. port1 = inb_p(base_addr + 1);
  726. /* Has either hearbeat bit changed? */
  727. if ((port0 ^ last_port0) & WD_HRTBT ||
  728. (port1 ^ last_port1) & WD_REVC_HRBT) {
  729. retval = 1;
  730. break;
  731. }
  732. }
  733. }
  734. release_region (base_addr, 4);
  735. return retval;
  736. }
  737. /*
  738. * These are the auto-probe addresses available.
  739. *
  740. * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
  741. * Revision A has an address range of 2 addresses, while Revision C has 4.
  742. */
  743. static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
  744. static int __init pcwd_init_module(void)
  745. {
  746. int i, found = 0;
  747. spin_lock_init(&io_lock);
  748. for (i = 0; pcwd_ioports[i] != 0; i++) {
  749. if (pcwd_checkcard(pcwd_ioports[i])) {
  750. if (!(pcwatchdog_init(pcwd_ioports[i])))
  751. found++;
  752. }
  753. }
  754. if (!found) {
  755. printk (KERN_INFO PFX "No card detected, or port not available\n");
  756. return -ENODEV;
  757. }
  758. return 0;
  759. }
  760. static void __exit pcwd_cleanup_module(void)
  761. {
  762. if (current_readport)
  763. pcwatchdog_exit();
  764. return;
  765. }
  766. module_init(pcwd_init_module);
  767. module_exit(pcwd_cleanup_module);
  768. MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
  769. MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
  770. MODULE_LICENSE("GPL");
  771. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  772. MODULE_ALIAS_MISCDEV(TEMP_MINOR);