sch56xx-common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. struct mutex *io_lock;
  60. struct kref kref;
  61. struct watchdog_info wdinfo;
  62. struct watchdog_device wddev;
  63. u8 watchdog_preset;
  64. u8 watchdog_control;
  65. u8 watchdog_output_enable;
  66. };
  67. static struct platform_device *sch56xx_pdev;
  68. /* Super I/O functions */
  69. static inline int superio_inb(int base, int reg)
  70. {
  71. outb(reg, base);
  72. return inb(base + 1);
  73. }
  74. static inline int superio_enter(int base)
  75. {
  76. /* Don't step on other drivers' I/O space by accident */
  77. if (!request_muxed_region(base, 2, "sch56xx")) {
  78. pr_err("I/O address 0x%04x already in use\n", base);
  79. return -EBUSY;
  80. }
  81. outb(SIO_UNLOCK_KEY, base);
  82. return 0;
  83. }
  84. static inline void superio_select(int base, int ld)
  85. {
  86. outb(SIO_REG_LDSEL, base);
  87. outb(ld, base + 1);
  88. }
  89. static inline void superio_exit(int base)
  90. {
  91. outb(SIO_LOCK_KEY, base);
  92. release_region(base, 2);
  93. }
  94. static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
  95. {
  96. u8 val;
  97. int i;
  98. /*
  99. * According to SMSC for the commands we use the maximum time for
  100. * the EM to respond is 15 ms, but testing shows in practice it
  101. * responds within 15-32 reads, so we first busy poll, and if
  102. * that fails sleep a bit and try again until we are way past
  103. * the 15 ms maximum response time.
  104. */
  105. const int max_busy_polls = 64;
  106. const int max_lazy_polls = 32;
  107. /* (Optional) Write-Clear the EC to Host Mailbox Register */
  108. val = inb(addr + 1);
  109. outb(val, addr + 1);
  110. /* Set Mailbox Address Pointer to first location in Region 1 */
  111. outb(0x00, addr + 2);
  112. outb(0x80, addr + 3);
  113. /* Write Request Packet Header */
  114. outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */
  115. outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */
  116. outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */
  117. /* Write Value field */
  118. if (cmd == SCH56XX_CMD_WRITE)
  119. outb(v, addr + 4);
  120. /* Write Address field */
  121. outb(reg & 0xff, addr + 6);
  122. outb(reg >> 8, addr + 7);
  123. /* Execute the Random Access Command */
  124. outb(0x01, addr); /* Write 01h to the Host-to-EC register */
  125. /* EM Interface Polling "Algorithm" */
  126. for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
  127. if (i >= max_busy_polls)
  128. msleep(1);
  129. /* Read Interrupt source Register */
  130. val = inb(addr + 8);
  131. /* Write Clear the interrupt source bits */
  132. if (val)
  133. outb(val, addr + 8);
  134. /* Command Completed ? */
  135. if (val & 0x01)
  136. break;
  137. }
  138. if (i == max_busy_polls + max_lazy_polls) {
  139. pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
  140. reg, 1);
  141. return -EIO;
  142. }
  143. /*
  144. * According to SMSC we may need to retry this, but sofar I've always
  145. * seen this succeed in 1 try.
  146. */
  147. for (i = 0; i < max_busy_polls; i++) {
  148. /* Read EC-to-Host Register */
  149. val = inb(addr + 1);
  150. /* Command Completed ? */
  151. if (val == 0x01)
  152. break;
  153. if (i == 0)
  154. pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
  155. (unsigned int)val, reg);
  156. }
  157. if (i == max_busy_polls) {
  158. pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
  159. reg, 2);
  160. return -EIO;
  161. }
  162. /*
  163. * According to the SMSC app note we should now do:
  164. *
  165. * Set Mailbox Address Pointer to first location in Region 1 *
  166. * outb(0x00, addr + 2);
  167. * outb(0x80, addr + 3);
  168. *
  169. * But if we do that things don't work, so let's not.
  170. */
  171. /* Read Value field */
  172. if (cmd == SCH56XX_CMD_READ)
  173. return inb(addr + 4);
  174. return 0;
  175. }
  176. int sch56xx_read_virtual_reg(u16 addr, u16 reg)
  177. {
  178. return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
  179. }
  180. EXPORT_SYMBOL(sch56xx_read_virtual_reg);
  181. int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
  182. {
  183. return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
  184. }
  185. EXPORT_SYMBOL(sch56xx_write_virtual_reg);
  186. int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
  187. {
  188. int lsb, msb;
  189. /* Read LSB first, this will cause the matching MSB to be latched */
  190. lsb = sch56xx_read_virtual_reg(addr, reg);
  191. if (lsb < 0)
  192. return lsb;
  193. msb = sch56xx_read_virtual_reg(addr, reg + 1);
  194. if (msb < 0)
  195. return msb;
  196. return lsb | (msb << 8);
  197. }
  198. EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
  199. int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
  200. int high_nibble)
  201. {
  202. int msb, lsn;
  203. /* Read MSB first, this will cause the matching LSN to be latched */
  204. msb = sch56xx_read_virtual_reg(addr, msb_reg);
  205. if (msb < 0)
  206. return msb;
  207. lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
  208. if (lsn < 0)
  209. return lsn;
  210. if (high_nibble)
  211. return (msb << 4) | (lsn >> 4);
  212. else
  213. return (msb << 4) | (lsn & 0x0f);
  214. }
  215. EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
  216. /*
  217. * Watchdog routines
  218. */
  219. /* Release our data struct when we're unregistered *and*
  220. all references to our watchdog device are released */
  221. static void watchdog_release_resources(struct kref *r)
  222. {
  223. struct sch56xx_watchdog_data *data =
  224. container_of(r, struct sch56xx_watchdog_data, kref);
  225. kfree(data);
  226. }
  227. static int watchdog_set_timeout(struct watchdog_device *wddev,
  228. unsigned int timeout)
  229. {
  230. struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
  231. unsigned int resolution;
  232. u8 control;
  233. int ret;
  234. /* 1 second or 60 second resolution? */
  235. if (timeout <= 255)
  236. resolution = 1;
  237. else
  238. resolution = 60;
  239. if (timeout < resolution || timeout > (resolution * 255))
  240. return -EINVAL;
  241. if (resolution == 1)
  242. control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC;
  243. else
  244. control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC;
  245. if (data->watchdog_control != control) {
  246. mutex_lock(data->io_lock);
  247. ret = sch56xx_write_virtual_reg(data->addr,
  248. SCH56XX_REG_WDOG_CONTROL,
  249. control);
  250. mutex_unlock(data->io_lock);
  251. if (ret)
  252. return ret;
  253. data->watchdog_control = control;
  254. }
  255. /*
  256. * Remember new timeout value, but do not write as that (re)starts
  257. * the watchdog countdown.
  258. */
  259. data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
  260. wddev->timeout = data->watchdog_preset * resolution;
  261. return 0;
  262. }
  263. static int watchdog_start(struct watchdog_device *wddev)
  264. {
  265. struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
  266. int ret;
  267. u8 val;
  268. /*
  269. * The sch56xx's watchdog cannot really be started / stopped
  270. * it is always running, but we can avoid the timer expiring
  271. * from causing a system reset by clearing the output enable bit.
  272. *
  273. * The sch56xx's watchdog will set the watchdog event bit, bit 0
  274. * of the second interrupt source register (at base-address + 9),
  275. * when the timer expires.
  276. *
  277. * This will only cause a system reset if the 0-1 flank happens when
  278. * output enable is true. Setting output enable after the flank will
  279. * not cause a reset, nor will the timer expiring a second time.
  280. * This means we must clear the watchdog event bit in case it is set.
  281. *
  282. * The timer may still be running (after a recent watchdog_stop) and
  283. * mere milliseconds away from expiring, so the timer must be reset
  284. * first!
  285. */
  286. mutex_lock(data->io_lock);
  287. /* 1. Reset the watchdog countdown counter */
  288. ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
  289. data->watchdog_preset);
  290. if (ret)
  291. goto leave;
  292. /* 2. Enable output */
  293. val = data->watchdog_output_enable | SCH56XX_WDOG_OUTPUT_ENABLE;
  294. ret = sch56xx_write_virtual_reg(data->addr,
  295. SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
  296. if (ret)
  297. goto leave;
  298. data->watchdog_output_enable = val;
  299. /* 3. Clear the watchdog event bit if set */
  300. val = inb(data->addr + 9);
  301. if (val & 0x01)
  302. outb(0x01, data->addr + 9);
  303. leave:
  304. mutex_unlock(data->io_lock);
  305. return ret;
  306. }
  307. static int watchdog_trigger(struct watchdog_device *wddev)
  308. {
  309. struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
  310. int ret;
  311. /* Reset the watchdog countdown counter */
  312. mutex_lock(data->io_lock);
  313. ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
  314. data->watchdog_preset);
  315. mutex_unlock(data->io_lock);
  316. return ret;
  317. }
  318. static int watchdog_stop(struct watchdog_device *wddev)
  319. {
  320. struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
  321. int ret = 0;
  322. u8 val;
  323. val = data->watchdog_output_enable & ~SCH56XX_WDOG_OUTPUT_ENABLE;
  324. mutex_lock(data->io_lock);
  325. ret = sch56xx_write_virtual_reg(data->addr,
  326. SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
  327. mutex_unlock(data->io_lock);
  328. if (ret)
  329. return ret;
  330. data->watchdog_output_enable = val;
  331. return 0;
  332. }
  333. static void watchdog_ref(struct watchdog_device *wddev)
  334. {
  335. struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
  336. kref_get(&data->kref);
  337. }
  338. static void watchdog_unref(struct watchdog_device *wddev)
  339. {
  340. struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
  341. kref_put(&data->kref, watchdog_release_resources);
  342. }
  343. static const struct watchdog_ops watchdog_ops = {
  344. .owner = THIS_MODULE,
  345. .start = watchdog_start,
  346. .stop = watchdog_stop,
  347. .ping = watchdog_trigger,
  348. .set_timeout = watchdog_set_timeout,
  349. .ref = watchdog_ref,
  350. .unref = watchdog_unref,
  351. };
  352. struct sch56xx_watchdog_data *sch56xx_watchdog_register(struct device *parent,
  353. u16 addr, u32 revision, struct mutex *io_lock, int check_enabled)
  354. {
  355. struct sch56xx_watchdog_data *data;
  356. int err, control, output_enable;
  357. /* Cache the watchdog registers */
  358. mutex_lock(io_lock);
  359. control =
  360. sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL);
  361. output_enable =
  362. sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE);
  363. mutex_unlock(io_lock);
  364. if (control < 0)
  365. return NULL;
  366. if (output_enable < 0)
  367. return NULL;
  368. if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
  369. pr_warn("Watchdog not enabled by BIOS, not registering\n");
  370. return NULL;
  371. }
  372. data = kzalloc(sizeof(struct sch56xx_watchdog_data), GFP_KERNEL);
  373. if (!data)
  374. return NULL;
  375. data->addr = addr;
  376. data->io_lock = io_lock;
  377. kref_init(&data->kref);
  378. strlcpy(data->wdinfo.identity, "sch56xx watchdog",
  379. sizeof(data->wdinfo.identity));
  380. data->wdinfo.firmware_version = revision;
  381. data->wdinfo.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT;
  382. if (!nowayout)
  383. data->wdinfo.options |= WDIOF_MAGICCLOSE;
  384. data->wddev.info = &data->wdinfo;
  385. data->wddev.ops = &watchdog_ops;
  386. data->wddev.parent = parent;
  387. data->wddev.timeout = 60;
  388. data->wddev.min_timeout = 1;
  389. data->wddev.max_timeout = 255 * 60;
  390. if (nowayout)
  391. set_bit(WDOG_NO_WAY_OUT, &data->wddev.status);
  392. if (output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)
  393. set_bit(WDOG_ACTIVE, &data->wddev.status);
  394. /* Since the watchdog uses a downcounter there is no register to read
  395. the BIOS set timeout from (if any was set at all) ->
  396. Choose a preset which will give us a 1 minute timeout */
  397. if (control & SCH56XX_WDOG_TIME_BASE_SEC)
  398. data->watchdog_preset = 60; /* seconds */
  399. else
  400. data->watchdog_preset = 1; /* minute */
  401. data->watchdog_control = control;
  402. data->watchdog_output_enable = output_enable;
  403. watchdog_set_drvdata(&data->wddev, data);
  404. err = watchdog_register_device(&data->wddev);
  405. if (err) {
  406. pr_err("Registering watchdog chardev: %d\n", err);
  407. kfree(data);
  408. return NULL;
  409. }
  410. return data;
  411. }
  412. EXPORT_SYMBOL(sch56xx_watchdog_register);
  413. void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data *data)
  414. {
  415. watchdog_unregister_device(&data->wddev);
  416. kref_put(&data->kref, watchdog_release_resources);
  417. /* Don't touch data after this it may have been free-ed! */
  418. }
  419. EXPORT_SYMBOL(sch56xx_watchdog_unregister);
  420. /*
  421. * platform dev find, add and remove functions
  422. */
  423. static int __init sch56xx_find(int sioaddr, const char **name)
  424. {
  425. u8 devid;
  426. unsigned short address;
  427. int err;
  428. err = superio_enter(sioaddr);
  429. if (err)
  430. return err;
  431. devid = superio_inb(sioaddr, SIO_REG_DEVID);
  432. switch (devid) {
  433. case SIO_SCH5627_ID:
  434. *name = "sch5627";
  435. break;
  436. case SIO_SCH5636_ID:
  437. *name = "sch5636";
  438. break;
  439. default:
  440. pr_debug("Unsupported device id: 0x%02x\n",
  441. (unsigned int)devid);
  442. err = -ENODEV;
  443. goto exit;
  444. }
  445. superio_select(sioaddr, SIO_SCH56XX_LD_EM);
  446. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  447. pr_warn("Device not activated\n");
  448. err = -ENODEV;
  449. goto exit;
  450. }
  451. /*
  452. * Warning the order of the low / high byte is the other way around
  453. * as on most other superio devices!!
  454. */
  455. address = superio_inb(sioaddr, SIO_REG_ADDR) |
  456. superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
  457. if (address == 0) {
  458. pr_warn("Base address not set\n");
  459. err = -ENODEV;
  460. goto exit;
  461. }
  462. err = address;
  463. exit:
  464. superio_exit(sioaddr);
  465. return err;
  466. }
  467. static int __init sch56xx_device_add(int address, const char *name)
  468. {
  469. struct resource res = {
  470. .start = address,
  471. .end = address + REGION_LENGTH - 1,
  472. .flags = IORESOURCE_IO,
  473. };
  474. int err;
  475. sch56xx_pdev = platform_device_alloc(name, address);
  476. if (!sch56xx_pdev)
  477. return -ENOMEM;
  478. res.name = sch56xx_pdev->name;
  479. err = acpi_check_resource_conflict(&res);
  480. if (err)
  481. goto exit_device_put;
  482. err = platform_device_add_resources(sch56xx_pdev, &res, 1);
  483. if (err) {
  484. pr_err("Device resource addition failed\n");
  485. goto exit_device_put;
  486. }
  487. err = platform_device_add(sch56xx_pdev);
  488. if (err) {
  489. pr_err("Device addition failed\n");
  490. goto exit_device_put;
  491. }
  492. return 0;
  493. exit_device_put:
  494. platform_device_put(sch56xx_pdev);
  495. return err;
  496. }
  497. static int __init sch56xx_init(void)
  498. {
  499. int address;
  500. const char *name = NULL;
  501. address = sch56xx_find(0x4e, &name);
  502. if (address < 0)
  503. address = sch56xx_find(0x2e, &name);
  504. if (address < 0)
  505. return address;
  506. return sch56xx_device_add(address, name);
  507. }
  508. static void __exit sch56xx_exit(void)
  509. {
  510. platform_device_unregister(sch56xx_pdev);
  511. }
  512. MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
  513. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  514. MODULE_LICENSE("GPL");
  515. module_init(sch56xx_init);
  516. module_exit(sch56xx_exit);