sch56xx-common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /***************************************************************************
  2. * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/acpi.h>
  26. #include <linux/delay.h>
  27. #include <linux/fs.h>
  28. #include <linux/watchdog.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/kref.h>
  32. #include <linux/slab.h>
  33. #include "sch56xx-common.h"
  34. /* Insmod parameters */
  35. static int nowayout = WATCHDOG_NOWAYOUT;
  36. module_param(nowayout, int, 0);
  37. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  38. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  39. #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */
  40. #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
  41. #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
  42. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  43. #define SIO_REG_DEVID 0x20 /* Device ID */
  44. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  45. #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
  46. #define SIO_SCH5627_ID 0xC6 /* Chipset ID */
  47. #define SIO_SCH5636_ID 0xC7 /* Chipset ID */
  48. #define REGION_LENGTH 10
  49. #define SCH56XX_CMD_READ 0x02
  50. #define SCH56XX_CMD_WRITE 0x03
  51. /* Watchdog registers */
  52. #define SCH56XX_REG_WDOG_PRESET 0x58B
  53. #define SCH56XX_REG_WDOG_CONTROL 0x58C
  54. #define SCH56XX_WDOG_TIME_BASE_SEC 0x01
  55. #define SCH56XX_REG_WDOG_OUTPUT_ENABLE 0x58E
  56. #define SCH56XX_WDOG_OUTPUT_ENABLE 0x02
  57. struct sch56xx_watchdog_data {
  58. u16 addr;
  59. u32 revision;
  60. struct mutex *io_lock;
  61. struct mutex watchdog_lock;
  62. struct list_head list; /* member of the watchdog_data_list */
  63. struct kref kref;
  64. struct miscdevice watchdog_miscdev;
  65. unsigned long watchdog_is_open;
  66. char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
  67. char watchdog_expect_close;
  68. u8 watchdog_preset;
  69. u8 watchdog_control;
  70. u8 watchdog_output_enable;
  71. };
  72. static struct platform_device *sch56xx_pdev;
  73. /*
  74. * Somewhat ugly :( global data pointer list with all sch56xx devices, so that
  75. * we can find our device data as when using misc_register there is no other
  76. * method to get to ones device data from the open fop.
  77. */
  78. static LIST_HEAD(watchdog_data_list);
  79. /* Note this lock not only protect list access, but also data.kref access */
  80. static DEFINE_MUTEX(watchdog_data_mutex);
  81. /* Super I/O functions */
  82. static inline int superio_inb(int base, int reg)
  83. {
  84. outb(reg, base);
  85. return inb(base + 1);
  86. }
  87. static inline int superio_enter(int base)
  88. {
  89. /* Don't step on other drivers' I/O space by accident */
  90. if (!request_muxed_region(base, 2, "sch56xx")) {
  91. pr_err("I/O address 0x%04x already in use\n", base);
  92. return -EBUSY;
  93. }
  94. outb(SIO_UNLOCK_KEY, base);
  95. return 0;
  96. }
  97. static inline void superio_select(int base, int ld)
  98. {
  99. outb(SIO_REG_LDSEL, base);
  100. outb(ld, base + 1);
  101. }
  102. static inline void superio_exit(int base)
  103. {
  104. outb(SIO_LOCK_KEY, base);
  105. release_region(base, 2);
  106. }
  107. static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
  108. {
  109. u8 val;
  110. int i;
  111. /*
  112. * According to SMSC for the commands we use the maximum time for
  113. * the EM to respond is 15 ms, but testing shows in practice it
  114. * responds within 15-32 reads, so we first busy poll, and if
  115. * that fails sleep a bit and try again until we are way past
  116. * the 15 ms maximum response time.
  117. */
  118. const int max_busy_polls = 64;
  119. const int max_lazy_polls = 32;
  120. /* (Optional) Write-Clear the EC to Host Mailbox Register */
  121. val = inb(addr + 1);
  122. outb(val, addr + 1);
  123. /* Set Mailbox Address Pointer to first location in Region 1 */
  124. outb(0x00, addr + 2);
  125. outb(0x80, addr + 3);
  126. /* Write Request Packet Header */
  127. outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */
  128. outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */
  129. outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */
  130. /* Write Value field */
  131. if (cmd == SCH56XX_CMD_WRITE)
  132. outb(v, addr + 4);
  133. /* Write Address field */
  134. outb(reg & 0xff, addr + 6);
  135. outb(reg >> 8, addr + 7);
  136. /* Execute the Random Access Command */
  137. outb(0x01, addr); /* Write 01h to the Host-to-EC register */
  138. /* EM Interface Polling "Algorithm" */
  139. for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
  140. if (i >= max_busy_polls)
  141. msleep(1);
  142. /* Read Interrupt source Register */
  143. val = inb(addr + 8);
  144. /* Write Clear the interrupt source bits */
  145. if (val)
  146. outb(val, addr + 8);
  147. /* Command Completed ? */
  148. if (val & 0x01)
  149. break;
  150. }
  151. if (i == max_busy_polls + max_lazy_polls) {
  152. pr_err("Max retries exceeded reading virtual "
  153. "register 0x%04hx (%d)\n", reg, 1);
  154. return -EIO;
  155. }
  156. /*
  157. * According to SMSC we may need to retry this, but sofar I've always
  158. * seen this succeed in 1 try.
  159. */
  160. for (i = 0; i < max_busy_polls; i++) {
  161. /* Read EC-to-Host Register */
  162. val = inb(addr + 1);
  163. /* Command Completed ? */
  164. if (val == 0x01)
  165. break;
  166. if (i == 0)
  167. pr_warn("EC reports: 0x%02x reading virtual register "
  168. "0x%04hx\n", (unsigned int)val, reg);
  169. }
  170. if (i == max_busy_polls) {
  171. pr_err("Max retries exceeded reading virtual "
  172. "register 0x%04hx (%d)\n", reg, 2);
  173. return -EIO;
  174. }
  175. /*
  176. * According to the SMSC app note we should now do:
  177. *
  178. * Set Mailbox Address Pointer to first location in Region 1 *
  179. * outb(0x00, addr + 2);
  180. * outb(0x80, addr + 3);
  181. *
  182. * But if we do that things don't work, so let's not.
  183. */
  184. /* Read Value field */
  185. if (cmd == SCH56XX_CMD_READ)
  186. return inb(addr + 4);
  187. return 0;
  188. }
  189. int sch56xx_read_virtual_reg(u16 addr, u16 reg)
  190. {
  191. return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
  192. }
  193. EXPORT_SYMBOL(sch56xx_read_virtual_reg);
  194. int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
  195. {
  196. return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
  197. }
  198. EXPORT_SYMBOL(sch56xx_write_virtual_reg);
  199. int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
  200. {
  201. int lsb, msb;
  202. /* Read LSB first, this will cause the matching MSB to be latched */
  203. lsb = sch56xx_read_virtual_reg(addr, reg);
  204. if (lsb < 0)
  205. return lsb;
  206. msb = sch56xx_read_virtual_reg(addr, reg + 1);
  207. if (msb < 0)
  208. return msb;
  209. return lsb | (msb << 8);
  210. }
  211. EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
  212. int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
  213. int high_nibble)
  214. {
  215. int msb, lsn;
  216. /* Read MSB first, this will cause the matching LSN to be latched */
  217. msb = sch56xx_read_virtual_reg(addr, msb_reg);
  218. if (msb < 0)
  219. return msb;
  220. lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
  221. if (lsn < 0)
  222. return lsn;
  223. if (high_nibble)
  224. return (msb << 4) | (lsn >> 4);
  225. else
  226. return (msb << 4) | (lsn & 0x0f);
  227. }
  228. EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
  229. /*
  230. * Watchdog routines
  231. */
  232. /*
  233. * Release our data struct when the platform device has been released *and*
  234. * all references to our watchdog device are released.
  235. */
  236. static void sch56xx_watchdog_release_resources(struct kref *r)
  237. {
  238. struct sch56xx_watchdog_data *data =
  239. container_of(r, struct sch56xx_watchdog_data, kref);
  240. kfree(data);
  241. }
  242. static int watchdog_set_timeout(struct sch56xx_watchdog_data *data,
  243. int timeout)
  244. {
  245. int ret, resolution;
  246. u8 control;
  247. /* 1 second or 60 second resolution? */
  248. if (timeout <= 255)
  249. resolution = 1;
  250. else
  251. resolution = 60;
  252. if (timeout < resolution || timeout > (resolution * 255))
  253. return -EINVAL;
  254. mutex_lock(&data->watchdog_lock);
  255. if (!data->addr) {
  256. ret = -ENODEV;
  257. goto leave;
  258. }
  259. if (resolution == 1)
  260. control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC;
  261. else
  262. control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC;
  263. if (data->watchdog_control != control) {
  264. mutex_lock(data->io_lock);
  265. ret = sch56xx_write_virtual_reg(data->addr,
  266. SCH56XX_REG_WDOG_CONTROL,
  267. control);
  268. mutex_unlock(data->io_lock);
  269. if (ret)
  270. goto leave;
  271. data->watchdog_control = control;
  272. }
  273. /*
  274. * Remember new timeout value, but do not write as that (re)starts
  275. * the watchdog countdown.
  276. */
  277. data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
  278. ret = data->watchdog_preset * resolution;
  279. leave:
  280. mutex_unlock(&data->watchdog_lock);
  281. return ret;
  282. }
  283. static int watchdog_get_timeout(struct sch56xx_watchdog_data *data)
  284. {
  285. int timeout;
  286. mutex_lock(&data->watchdog_lock);
  287. if (data->watchdog_control & SCH56XX_WDOG_TIME_BASE_SEC)
  288. timeout = data->watchdog_preset;
  289. else
  290. timeout = data->watchdog_preset * 60;
  291. mutex_unlock(&data->watchdog_lock);
  292. return timeout;
  293. }
  294. static int watchdog_start(struct sch56xx_watchdog_data *data)
  295. {
  296. int ret;
  297. u8 val;
  298. mutex_lock(&data->watchdog_lock);
  299. if (!data->addr) {
  300. ret = -ENODEV;
  301. goto leave_unlock_watchdog;
  302. }
  303. /*
  304. * The sch56xx's watchdog cannot really be started / stopped
  305. * it is always running, but we can avoid the timer expiring
  306. * from causing a system reset by clearing the output enable bit.
  307. *
  308. * The sch56xx's watchdog will set the watchdog event bit, bit 0
  309. * of the second interrupt source register (at base-address + 9),
  310. * when the timer expires.
  311. *
  312. * This will only cause a system reset if the 0-1 flank happens when
  313. * output enable is true. Setting output enable after the flank will
  314. * not cause a reset, nor will the timer expiring a second time.
  315. * This means we must clear the watchdog event bit in case it is set.
  316. *
  317. * The timer may still be running (after a recent watchdog_stop) and
  318. * mere milliseconds away from expiring, so the timer must be reset
  319. * first!
  320. */
  321. mutex_lock(data->io_lock);
  322. /* 1. Reset the watchdog countdown counter */
  323. ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
  324. data->watchdog_preset);
  325. if (ret)
  326. goto leave;
  327. /* 2. Enable output (if not already enabled) */
  328. if (!(data->watchdog_output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
  329. val = data->watchdog_output_enable |
  330. SCH56XX_WDOG_OUTPUT_ENABLE;
  331. ret = sch56xx_write_virtual_reg(data->addr,
  332. SCH56XX_REG_WDOG_OUTPUT_ENABLE,
  333. val);
  334. if (ret)
  335. goto leave;
  336. data->watchdog_output_enable = val;
  337. }
  338. /* 3. Clear the watchdog event bit if set */
  339. val = inb(data->addr + 9);
  340. if (val & 0x01)
  341. outb(0x01, data->addr + 9);
  342. leave:
  343. mutex_unlock(data->io_lock);
  344. leave_unlock_watchdog:
  345. mutex_unlock(&data->watchdog_lock);
  346. return ret;
  347. }
  348. static int watchdog_trigger(struct sch56xx_watchdog_data *data)
  349. {
  350. int ret;
  351. mutex_lock(&data->watchdog_lock);
  352. if (!data->addr) {
  353. ret = -ENODEV;
  354. goto leave;
  355. }
  356. /* Reset the watchdog countdown counter */
  357. mutex_lock(data->io_lock);
  358. ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
  359. data->watchdog_preset);
  360. mutex_unlock(data->io_lock);
  361. leave:
  362. mutex_unlock(&data->watchdog_lock);
  363. return ret;
  364. }
  365. static int watchdog_stop_unlocked(struct sch56xx_watchdog_data *data)
  366. {
  367. int ret = 0;
  368. u8 val;
  369. if (!data->addr)
  370. return -ENODEV;
  371. if (data->watchdog_output_enable & SCH56XX_WDOG_OUTPUT_ENABLE) {
  372. val = data->watchdog_output_enable &
  373. ~SCH56XX_WDOG_OUTPUT_ENABLE;
  374. mutex_lock(data->io_lock);
  375. ret = sch56xx_write_virtual_reg(data->addr,
  376. SCH56XX_REG_WDOG_OUTPUT_ENABLE,
  377. val);
  378. mutex_unlock(data->io_lock);
  379. if (ret)
  380. return ret;
  381. data->watchdog_output_enable = val;
  382. }
  383. return ret;
  384. }
  385. static int watchdog_stop(struct sch56xx_watchdog_data *data)
  386. {
  387. int ret;
  388. mutex_lock(&data->watchdog_lock);
  389. ret = watchdog_stop_unlocked(data);
  390. mutex_unlock(&data->watchdog_lock);
  391. return ret;
  392. }
  393. static int watchdog_release(struct inode *inode, struct file *filp)
  394. {
  395. struct sch56xx_watchdog_data *data = filp->private_data;
  396. if (data->watchdog_expect_close) {
  397. watchdog_stop(data);
  398. data->watchdog_expect_close = 0;
  399. } else {
  400. watchdog_trigger(data);
  401. pr_crit("unexpected close, not stopping watchdog!\n");
  402. }
  403. clear_bit(0, &data->watchdog_is_open);
  404. mutex_lock(&watchdog_data_mutex);
  405. kref_put(&data->kref, sch56xx_watchdog_release_resources);
  406. mutex_unlock(&watchdog_data_mutex);
  407. return 0;
  408. }
  409. static int watchdog_open(struct inode *inode, struct file *filp)
  410. {
  411. struct sch56xx_watchdog_data *pos, *data = NULL;
  412. int ret, watchdog_is_open;
  413. /*
  414. * We get called from drivers/char/misc.c with misc_mtx hold, and we
  415. * call misc_register() from sch56xx_watchdog_probe() with
  416. * watchdog_data_mutex hold, as misc_register() takes the misc_mtx
  417. * lock, this is a possible deadlock, so we use mutex_trylock here.
  418. */
  419. if (!mutex_trylock(&watchdog_data_mutex))
  420. return -ERESTARTSYS;
  421. list_for_each_entry(pos, &watchdog_data_list, list) {
  422. if (pos->watchdog_miscdev.minor == iminor(inode)) {
  423. data = pos;
  424. break;
  425. }
  426. }
  427. /* Note we can never not have found data, so we don't check for this */
  428. watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
  429. if (!watchdog_is_open)
  430. kref_get(&data->kref);
  431. mutex_unlock(&watchdog_data_mutex);
  432. if (watchdog_is_open)
  433. return -EBUSY;
  434. filp->private_data = data;
  435. /* Start the watchdog */
  436. ret = watchdog_start(data);
  437. if (ret) {
  438. watchdog_release(inode, filp);
  439. return ret;
  440. }
  441. return nonseekable_open(inode, filp);
  442. }
  443. static ssize_t watchdog_write(struct file *filp, const char __user *buf,
  444. size_t count, loff_t *offset)
  445. {
  446. int ret;
  447. struct sch56xx_watchdog_data *data = filp->private_data;
  448. if (count) {
  449. if (!nowayout) {
  450. size_t i;
  451. /* Clear it in case it was set with a previous write */
  452. data->watchdog_expect_close = 0;
  453. for (i = 0; i != count; i++) {
  454. char c;
  455. if (get_user(c, buf + i))
  456. return -EFAULT;
  457. if (c == 'V')
  458. data->watchdog_expect_close = 1;
  459. }
  460. }
  461. ret = watchdog_trigger(data);
  462. if (ret)
  463. return ret;
  464. }
  465. return count;
  466. }
  467. static long watchdog_ioctl(struct file *filp, unsigned int cmd,
  468. unsigned long arg)
  469. {
  470. struct watchdog_info ident = {
  471. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
  472. .identity = "sch56xx watchdog"
  473. };
  474. int i, ret = 0;
  475. struct sch56xx_watchdog_data *data = filp->private_data;
  476. switch (cmd) {
  477. case WDIOC_GETSUPPORT:
  478. ident.firmware_version = data->revision;
  479. if (!nowayout)
  480. ident.options |= WDIOF_MAGICCLOSE;
  481. if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
  482. ret = -EFAULT;
  483. break;
  484. case WDIOC_GETSTATUS:
  485. case WDIOC_GETBOOTSTATUS:
  486. ret = put_user(0, (int __user *)arg);
  487. break;
  488. case WDIOC_KEEPALIVE:
  489. ret = watchdog_trigger(data);
  490. break;
  491. case WDIOC_GETTIMEOUT:
  492. i = watchdog_get_timeout(data);
  493. ret = put_user(i, (int __user *)arg);
  494. break;
  495. case WDIOC_SETTIMEOUT:
  496. if (get_user(i, (int __user *)arg)) {
  497. ret = -EFAULT;
  498. break;
  499. }
  500. ret = watchdog_set_timeout(data, i);
  501. if (ret >= 0)
  502. ret = put_user(ret, (int __user *)arg);
  503. break;
  504. case WDIOC_SETOPTIONS:
  505. if (get_user(i, (int __user *)arg)) {
  506. ret = -EFAULT;
  507. break;
  508. }
  509. if (i & WDIOS_DISABLECARD)
  510. ret = watchdog_stop(data);
  511. else if (i & WDIOS_ENABLECARD)
  512. ret = watchdog_trigger(data);
  513. else
  514. ret = -EINVAL;
  515. break;
  516. default:
  517. ret = -ENOTTY;
  518. }
  519. return ret;
  520. }
  521. static const struct file_operations watchdog_fops = {
  522. .owner = THIS_MODULE,
  523. .llseek = no_llseek,
  524. .open = watchdog_open,
  525. .release = watchdog_release,
  526. .write = watchdog_write,
  527. .unlocked_ioctl = watchdog_ioctl,
  528. };
  529. struct sch56xx_watchdog_data *sch56xx_watchdog_register(
  530. u16 addr, u32 revision, struct mutex *io_lock, int check_enabled)
  531. {
  532. struct sch56xx_watchdog_data *data;
  533. int i, err, control, output_enable;
  534. const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
  535. /* Cache the watchdog registers */
  536. mutex_lock(io_lock);
  537. control =
  538. sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL);
  539. output_enable =
  540. sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE);
  541. mutex_unlock(io_lock);
  542. if (control < 0)
  543. return NULL;
  544. if (output_enable < 0)
  545. return NULL;
  546. if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
  547. pr_warn("Watchdog not enabled by BIOS, not registering\n");
  548. return NULL;
  549. }
  550. data = kzalloc(sizeof(struct sch56xx_watchdog_data), GFP_KERNEL);
  551. if (!data)
  552. return NULL;
  553. data->addr = addr;
  554. data->revision = revision;
  555. data->io_lock = io_lock;
  556. data->watchdog_control = control;
  557. data->watchdog_output_enable = output_enable;
  558. mutex_init(&data->watchdog_lock);
  559. INIT_LIST_HEAD(&data->list);
  560. kref_init(&data->kref);
  561. err = watchdog_set_timeout(data, 60);
  562. if (err < 0)
  563. goto error;
  564. /*
  565. * We take the data_mutex lock early so that watchdog_open() cannot
  566. * run when misc_register() has completed, but we've not yet added
  567. * our data to the watchdog_data_list.
  568. */
  569. mutex_lock(&watchdog_data_mutex);
  570. for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
  571. /* Register our watchdog part */
  572. snprintf(data->watchdog_name, sizeof(data->watchdog_name),
  573. "watchdog%c", (i == 0) ? '\0' : ('0' + i));
  574. data->watchdog_miscdev.name = data->watchdog_name;
  575. data->watchdog_miscdev.fops = &watchdog_fops;
  576. data->watchdog_miscdev.minor = watchdog_minors[i];
  577. err = misc_register(&data->watchdog_miscdev);
  578. if (err == -EBUSY)
  579. continue;
  580. if (err)
  581. break;
  582. list_add(&data->list, &watchdog_data_list);
  583. pr_info("Registered /dev/%s chardev major 10, minor: %d\n",
  584. data->watchdog_name, watchdog_minors[i]);
  585. break;
  586. }
  587. mutex_unlock(&watchdog_data_mutex);
  588. if (err) {
  589. pr_err("Registering watchdog chardev: %d\n", err);
  590. goto error;
  591. }
  592. if (i == ARRAY_SIZE(watchdog_minors)) {
  593. pr_warn("Couldn't register watchdog (no free minor)\n");
  594. goto error;
  595. }
  596. return data;
  597. error:
  598. kfree(data);
  599. return NULL;
  600. }
  601. EXPORT_SYMBOL(sch56xx_watchdog_register);
  602. void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data *data)
  603. {
  604. mutex_lock(&watchdog_data_mutex);
  605. misc_deregister(&data->watchdog_miscdev);
  606. list_del(&data->list);
  607. mutex_unlock(&watchdog_data_mutex);
  608. mutex_lock(&data->watchdog_lock);
  609. if (data->watchdog_is_open) {
  610. pr_warn("platform device unregistered with watchdog "
  611. "open! Stopping watchdog.\n");
  612. watchdog_stop_unlocked(data);
  613. }
  614. /* Tell the wdog start/stop/trigger functions our dev is gone */
  615. data->addr = 0;
  616. data->io_lock = NULL;
  617. mutex_unlock(&data->watchdog_lock);
  618. mutex_lock(&watchdog_data_mutex);
  619. kref_put(&data->kref, sch56xx_watchdog_release_resources);
  620. mutex_unlock(&watchdog_data_mutex);
  621. }
  622. EXPORT_SYMBOL(sch56xx_watchdog_unregister);
  623. /*
  624. * platform dev find, add and remove functions
  625. */
  626. static int __init sch56xx_find(int sioaddr, unsigned short *address,
  627. const char **name)
  628. {
  629. u8 devid;
  630. int err;
  631. err = superio_enter(sioaddr);
  632. if (err)
  633. return err;
  634. devid = superio_inb(sioaddr, SIO_REG_DEVID);
  635. switch (devid) {
  636. case SIO_SCH5627_ID:
  637. *name = "sch5627";
  638. break;
  639. case SIO_SCH5636_ID:
  640. *name = "sch5636";
  641. break;
  642. default:
  643. pr_debug("Unsupported device id: 0x%02x\n",
  644. (unsigned int)devid);
  645. err = -ENODEV;
  646. goto exit;
  647. }
  648. superio_select(sioaddr, SIO_SCH56XX_LD_EM);
  649. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  650. pr_warn("Device not activated\n");
  651. err = -ENODEV;
  652. goto exit;
  653. }
  654. /*
  655. * Warning the order of the low / high byte is the other way around
  656. * as on most other superio devices!!
  657. */
  658. *address = superio_inb(sioaddr, SIO_REG_ADDR) |
  659. superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
  660. if (*address == 0) {
  661. pr_warn("Base address not set\n");
  662. err = -ENODEV;
  663. goto exit;
  664. }
  665. exit:
  666. superio_exit(sioaddr);
  667. return err;
  668. }
  669. static int __init sch56xx_device_add(unsigned short address, const char *name)
  670. {
  671. struct resource res = {
  672. .start = address,
  673. .end = address + REGION_LENGTH - 1,
  674. .flags = IORESOURCE_IO,
  675. };
  676. int err;
  677. sch56xx_pdev = platform_device_alloc(name, address);
  678. if (!sch56xx_pdev)
  679. return -ENOMEM;
  680. res.name = sch56xx_pdev->name;
  681. err = acpi_check_resource_conflict(&res);
  682. if (err)
  683. goto exit_device_put;
  684. err = platform_device_add_resources(sch56xx_pdev, &res, 1);
  685. if (err) {
  686. pr_err("Device resource addition failed\n");
  687. goto exit_device_put;
  688. }
  689. err = platform_device_add(sch56xx_pdev);
  690. if (err) {
  691. pr_err("Device addition failed\n");
  692. goto exit_device_put;
  693. }
  694. return 0;
  695. exit_device_put:
  696. platform_device_put(sch56xx_pdev);
  697. return err;
  698. }
  699. static int __init sch56xx_init(void)
  700. {
  701. int err;
  702. unsigned short address;
  703. const char *name;
  704. err = sch56xx_find(0x4e, &address, &name);
  705. if (err)
  706. err = sch56xx_find(0x2e, &address, &name);
  707. if (err)
  708. return err;
  709. return sch56xx_device_add(address, name);
  710. }
  711. static void __exit sch56xx_exit(void)
  712. {
  713. platform_device_unregister(sch56xx_pdev);
  714. }
  715. MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
  716. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  717. MODULE_LICENSE("GPL");
  718. module_init(sch56xx_init);
  719. module_exit(sch56xx_exit);