pcwd.c 23 KB

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