pcwd.c 23 KB

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