sch56xx-common.c 15 KB

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