1 /***************************************************************************
2 * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com> *
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. *
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. *
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 ***************************************************************************/
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/platform_device.h>
25 #include <linux/err.h>
27 #include <linux/acpi.h>
28 #include <linux/delay.h>
30 #include <linux/watchdog.h>
31 #include <linux/miscdevice.h>
32 #include <linux/uaccess.h>
33 #include <linux/kref.h>
34 #include <linux/slab.h>
35 #include "sch56xx-common.h"
37 /* Insmod parameters */
38 static int nowayout = WATCHDOG_NOWAYOUT;
39 module_param(nowayout, int, 0);
40 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
41 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
43 #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */
44 #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
45 #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
47 #define SIO_REG_LDSEL 0x07 /* Logical device select */
48 #define SIO_REG_DEVID 0x20 /* Device ID */
49 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
50 #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
52 #define SIO_SCH5627_ID 0xC6 /* Chipset ID */
53 #define SIO_SCH5636_ID 0xC7 /* Chipset ID */
55 #define REGION_LENGTH 10
57 #define SCH56XX_CMD_READ 0x02
58 #define SCH56XX_CMD_WRITE 0x03
60 /* Watchdog registers */
61 #define SCH56XX_REG_WDOG_PRESET 0x58B
62 #define SCH56XX_REG_WDOG_CONTROL 0x58C
63 #define SCH56XX_WDOG_TIME_BASE_SEC 0x01
64 #define SCH56XX_REG_WDOG_OUTPUT_ENABLE 0x58E
65 #define SCH56XX_WDOG_OUTPUT_ENABLE 0x02
67 struct sch56xx_watchdog_data {
69 struct mutex *io_lock;
71 struct watchdog_info wdinfo;
72 struct watchdog_device wddev;
75 u8 watchdog_output_enable;
78 static struct platform_device *sch56xx_pdev;
80 /* Super I/O functions */
81 static inline int superio_inb(int base, int reg)
87 static inline int superio_enter(int base)
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);
95 outb(SIO_UNLOCK_KEY, base);
100 static inline void superio_select(int base, int ld)
102 outb(SIO_REG_LDSEL, base);
106 static inline void superio_exit(int base)
108 outb(SIO_LOCK_KEY, base);
109 release_region(base, 2);
112 static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
117 * According to SMSC for the commands we use the maximum time for
118 * the EM to respond is 15 ms, but testing shows in practice it
119 * responds within 15-32 reads, so we first busy poll, and if
120 * that fails sleep a bit and try again until we are way past
121 * the 15 ms maximum response time.
123 const int max_busy_polls = 64;
124 const int max_lazy_polls = 32;
126 /* (Optional) Write-Clear the EC to Host Mailbox Register */
130 /* Set Mailbox Address Pointer to first location in Region 1 */
131 outb(0x00, addr + 2);
132 outb(0x80, addr + 3);
134 /* Write Request Packet Header */
135 outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */
136 outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */
137 outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */
139 /* Write Value field */
140 if (cmd == SCH56XX_CMD_WRITE)
143 /* Write Address field */
144 outb(reg & 0xff, addr + 6);
145 outb(reg >> 8, addr + 7);
147 /* Execute the Random Access Command */
148 outb(0x01, addr); /* Write 01h to the Host-to-EC register */
150 /* EM Interface Polling "Algorithm" */
151 for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
152 if (i >= max_busy_polls)
154 /* Read Interrupt source Register */
156 /* Write Clear the interrupt source bits */
159 /* Command Completed ? */
163 if (i == max_busy_polls + max_lazy_polls) {
164 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
170 * According to SMSC we may need to retry this, but sofar I've always
171 * seen this succeed in 1 try.
173 for (i = 0; i < max_busy_polls; i++) {
174 /* Read EC-to-Host Register */
176 /* Command Completed ? */
181 pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
182 (unsigned int)val, reg);
184 if (i == max_busy_polls) {
185 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
191 * According to the SMSC app note we should now do:
193 * Set Mailbox Address Pointer to first location in Region 1 *
194 * outb(0x00, addr + 2);
195 * outb(0x80, addr + 3);
197 * But if we do that things don't work, so let's not.
200 /* Read Value field */
201 if (cmd == SCH56XX_CMD_READ)
202 return inb(addr + 4);
207 int sch56xx_read_virtual_reg(u16 addr, u16 reg)
209 return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
211 EXPORT_SYMBOL(sch56xx_read_virtual_reg);
213 int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
215 return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
217 EXPORT_SYMBOL(sch56xx_write_virtual_reg);
219 int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
223 /* Read LSB first, this will cause the matching MSB to be latched */
224 lsb = sch56xx_read_virtual_reg(addr, reg);
228 msb = sch56xx_read_virtual_reg(addr, reg + 1);
232 return lsb | (msb << 8);
234 EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
236 int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
241 /* Read MSB first, this will cause the matching LSN to be latched */
242 msb = sch56xx_read_virtual_reg(addr, msb_reg);
246 lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
251 return (msb << 4) | (lsn >> 4);
253 return (msb << 4) | (lsn & 0x0f);
255 EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
261 /* Release our data struct when we're unregistered *and*
262 all references to our watchdog device are released */
263 static void watchdog_release_resources(struct kref *r)
265 struct sch56xx_watchdog_data *data =
266 container_of(r, struct sch56xx_watchdog_data, kref);
270 static int watchdog_set_timeout(struct watchdog_device *wddev,
271 unsigned int timeout)
273 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
274 unsigned int resolution;
278 /* 1 second or 60 second resolution? */
284 if (timeout < resolution || timeout > (resolution * 255))
288 control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC;
290 control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC;
292 if (data->watchdog_control != control) {
293 mutex_lock(data->io_lock);
294 ret = sch56xx_write_virtual_reg(data->addr,
295 SCH56XX_REG_WDOG_CONTROL,
297 mutex_unlock(data->io_lock);
301 data->watchdog_control = control;
305 * Remember new timeout value, but do not write as that (re)starts
306 * the watchdog countdown.
308 data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
309 wddev->timeout = data->watchdog_preset * resolution;
314 static int watchdog_start(struct watchdog_device *wddev)
316 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
321 * The sch56xx's watchdog cannot really be started / stopped
322 * it is always running, but we can avoid the timer expiring
323 * from causing a system reset by clearing the output enable bit.
325 * The sch56xx's watchdog will set the watchdog event bit, bit 0
326 * of the second interrupt source register (at base-address + 9),
327 * when the timer expires.
329 * This will only cause a system reset if the 0-1 flank happens when
330 * output enable is true. Setting output enable after the flank will
331 * not cause a reset, nor will the timer expiring a second time.
332 * This means we must clear the watchdog event bit in case it is set.
334 * The timer may still be running (after a recent watchdog_stop) and
335 * mere milliseconds away from expiring, so the timer must be reset
339 mutex_lock(data->io_lock);
341 /* 1. Reset the watchdog countdown counter */
342 ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
343 data->watchdog_preset);
347 /* 2. Enable output */
348 val = data->watchdog_output_enable | SCH56XX_WDOG_OUTPUT_ENABLE;
349 ret = sch56xx_write_virtual_reg(data->addr,
350 SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
354 data->watchdog_output_enable = val;
356 /* 3. Clear the watchdog event bit if set */
357 val = inb(data->addr + 9);
359 outb(0x01, data->addr + 9);
362 mutex_unlock(data->io_lock);
366 static int watchdog_trigger(struct watchdog_device *wddev)
368 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
371 /* Reset the watchdog countdown counter */
372 mutex_lock(data->io_lock);
373 ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
374 data->watchdog_preset);
375 mutex_unlock(data->io_lock);
380 static int watchdog_stop(struct watchdog_device *wddev)
382 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
386 val = data->watchdog_output_enable & ~SCH56XX_WDOG_OUTPUT_ENABLE;
387 mutex_lock(data->io_lock);
388 ret = sch56xx_write_virtual_reg(data->addr,
389 SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
390 mutex_unlock(data->io_lock);
394 data->watchdog_output_enable = val;
398 static void watchdog_ref(struct watchdog_device *wddev)
400 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
402 kref_get(&data->kref);
405 static void watchdog_unref(struct watchdog_device *wddev)
407 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
409 kref_put(&data->kref, watchdog_release_resources);
412 static const struct watchdog_ops watchdog_ops = {
413 .owner = THIS_MODULE,
414 .start = watchdog_start,
415 .stop = watchdog_stop,
416 .ping = watchdog_trigger,
417 .set_timeout = watchdog_set_timeout,
419 .unref = watchdog_unref,
422 struct sch56xx_watchdog_data *sch56xx_watchdog_register(struct device *parent,
423 u16 addr, u32 revision, struct mutex *io_lock, int check_enabled)
425 struct sch56xx_watchdog_data *data;
426 int err, control, output_enable;
428 /* Cache the watchdog registers */
431 sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL);
433 sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE);
434 mutex_unlock(io_lock);
438 if (output_enable < 0)
440 if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
441 pr_warn("Watchdog not enabled by BIOS, not registering\n");
445 data = kzalloc(sizeof(struct sch56xx_watchdog_data), GFP_KERNEL);
450 data->io_lock = io_lock;
451 kref_init(&data->kref);
453 strlcpy(data->wdinfo.identity, "sch56xx watchdog",
454 sizeof(data->wdinfo.identity));
455 data->wdinfo.firmware_version = revision;
456 data->wdinfo.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT;
458 data->wdinfo.options |= WDIOF_MAGICCLOSE;
460 data->wddev.info = &data->wdinfo;
461 data->wddev.ops = &watchdog_ops;
462 data->wddev.parent = parent;
463 data->wddev.timeout = 60;
464 data->wddev.min_timeout = 1;
465 data->wddev.max_timeout = 255 * 60;
467 set_bit(WDOG_NO_WAY_OUT, &data->wddev.status);
468 if (output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)
469 set_bit(WDOG_ACTIVE, &data->wddev.status);
471 /* Since the watchdog uses a downcounter there is no register to read
472 the BIOS set timeout from (if any was set at all) ->
473 Choose a preset which will give us a 1 minute timeout */
474 if (control & SCH56XX_WDOG_TIME_BASE_SEC)
475 data->watchdog_preset = 60; /* seconds */
477 data->watchdog_preset = 1; /* minute */
479 data->watchdog_control = control;
480 data->watchdog_output_enable = output_enable;
482 watchdog_set_drvdata(&data->wddev, data);
483 err = watchdog_register_device(&data->wddev);
485 pr_err("Registering watchdog chardev: %d\n", err);
492 EXPORT_SYMBOL(sch56xx_watchdog_register);
494 void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data *data)
496 watchdog_unregister_device(&data->wddev);
497 kref_put(&data->kref, watchdog_release_resources);
498 /* Don't touch data after this it may have been free-ed! */
500 EXPORT_SYMBOL(sch56xx_watchdog_unregister);
503 * platform dev find, add and remove functions
506 static int __init sch56xx_find(int sioaddr, const char **name)
509 unsigned short address;
512 err = superio_enter(sioaddr);
516 devid = superio_inb(sioaddr, SIO_REG_DEVID);
525 pr_debug("Unsupported device id: 0x%02x\n",
526 (unsigned int)devid);
531 superio_select(sioaddr, SIO_SCH56XX_LD_EM);
533 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
534 pr_warn("Device not activated\n");
540 * Warning the order of the low / high byte is the other way around
541 * as on most other superio devices!!
543 address = superio_inb(sioaddr, SIO_REG_ADDR) |
544 superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
546 pr_warn("Base address not set\n");
553 superio_exit(sioaddr);
557 static int __init sch56xx_device_add(int address, const char *name)
559 struct resource res = {
561 .end = address + REGION_LENGTH - 1,
562 .flags = IORESOURCE_IO,
566 sch56xx_pdev = platform_device_alloc(name, address);
570 res.name = sch56xx_pdev->name;
571 err = acpi_check_resource_conflict(&res);
573 goto exit_device_put;
575 err = platform_device_add_resources(sch56xx_pdev, &res, 1);
577 pr_err("Device resource addition failed\n");
578 goto exit_device_put;
581 err = platform_device_add(sch56xx_pdev);
583 pr_err("Device addition failed\n");
584 goto exit_device_put;
590 platform_device_put(sch56xx_pdev);
595 static int __init sch56xx_init(void)
598 const char *name = NULL;
600 address = sch56xx_find(0x4e, &name);
602 address = sch56xx_find(0x2e, &name);
606 return sch56xx_device_add(address, name);
609 static void __exit sch56xx_exit(void)
611 platform_device_unregister(sch56xx_pdev);
614 MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
615 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
616 MODULE_LICENSE("GPL");
618 module_init(sch56xx_init);
619 module_exit(sch56xx_exit);