sch56xx-common.c 15 KB

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