1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3 * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com> *
5 ***************************************************************************/
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/init.h>
12 #include <linux/platform_device.h>
13 #include <linux/dmi.h>
14 #include <linux/err.h>
16 #include <linux/acpi.h>
17 #include <linux/delay.h>
19 #include <linux/watchdog.h>
20 #include <linux/uaccess.h>
21 #include <linux/slab.h>
22 #include "sch56xx-common.h"
24 static bool ignore_dmi;
25 module_param(ignore_dmi, bool, 0);
26 MODULE_PARM_DESC(ignore_dmi, "Omit DMI check for supported devices (default=0)");
28 static bool nowayout = WATCHDOG_NOWAYOUT;
29 module_param(nowayout, bool, 0);
30 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
31 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
33 #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */
34 #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
35 #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
37 #define SIO_REG_LDSEL 0x07 /* Logical device select */
38 #define SIO_REG_DEVID 0x20 /* Device ID */
39 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
40 #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
42 #define SIO_SCH5627_ID 0xC6 /* Chipset ID */
43 #define SIO_SCH5636_ID 0xC7 /* Chipset ID */
45 #define REGION_LENGTH 10
47 #define SCH56XX_CMD_READ 0x02
48 #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
57 struct sch56xx_watchdog_data {
59 struct mutex *io_lock;
60 struct watchdog_info wdinfo;
61 struct watchdog_device wddev;
64 u8 watchdog_output_enable;
67 static struct platform_device *sch56xx_pdev;
69 /* Super I/O functions */
70 static inline int superio_inb(int base, int reg)
76 static inline int superio_enter(int base)
78 /* Don't step on other drivers' I/O space by accident */
79 if (!request_muxed_region(base, 2, "sch56xx")) {
80 pr_err("I/O address 0x%04x already in use\n", base);
84 outb(SIO_UNLOCK_KEY, base);
89 static inline void superio_select(int base, int ld)
91 outb(SIO_REG_LDSEL, base);
95 static inline void superio_exit(int base)
97 outb(SIO_LOCK_KEY, base);
98 release_region(base, 2);
101 static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
106 * According to SMSC for the commands we use the maximum time for
107 * the EM to respond is 15 ms, but testing shows in practice it
108 * responds within 15-32 reads, so we first busy poll, and if
109 * that fails sleep a bit and try again until we are way past
110 * the 15 ms maximum response time.
112 const int max_busy_polls = 64;
113 const int max_lazy_polls = 32;
115 /* (Optional) Write-Clear the EC to Host Mailbox Register */
119 /* Set Mailbox Address Pointer to first location in Region 1 */
120 outb(0x00, addr + 2);
121 outb(0x80, addr + 3);
123 /* Write Request Packet Header */
124 outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */
125 outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */
126 outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */
128 /* Write Value field */
129 if (cmd == SCH56XX_CMD_WRITE)
132 /* Write Address field */
133 outb(reg & 0xff, addr + 6);
134 outb(reg >> 8, addr + 7);
136 /* Execute the Random Access Command */
137 outb(0x01, addr); /* Write 01h to the Host-to-EC register */
139 /* EM Interface Polling "Algorithm" */
140 for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
141 if (i >= max_busy_polls)
142 usleep_range(1000, 2000);
143 /* Read Interrupt source Register */
145 /* Write Clear the interrupt source bits */
148 /* Command Completed ? */
152 if (i == max_busy_polls + max_lazy_polls) {
153 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
159 * According to SMSC we may need to retry this, but sofar I've always
160 * seen this succeed in 1 try.
162 for (i = 0; i < max_busy_polls; i++) {
163 /* Read EC-to-Host Register */
165 /* Command Completed ? */
170 pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
171 (unsigned int)val, reg);
173 if (i == max_busy_polls) {
174 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
180 * According to the SMSC app note we should now do:
182 * Set Mailbox Address Pointer to first location in Region 1 *
183 * outb(0x00, addr + 2);
184 * outb(0x80, addr + 3);
186 * But if we do that things don't work, so let's not.
189 /* Read Value field */
190 if (cmd == SCH56XX_CMD_READ)
191 return inb(addr + 4);
196 int sch56xx_read_virtual_reg(u16 addr, u16 reg)
198 return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
200 EXPORT_SYMBOL(sch56xx_read_virtual_reg);
202 int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
204 return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
206 EXPORT_SYMBOL(sch56xx_write_virtual_reg);
208 int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
212 /* Read LSB first, this will cause the matching MSB to be latched */
213 lsb = sch56xx_read_virtual_reg(addr, reg);
217 msb = sch56xx_read_virtual_reg(addr, reg + 1);
221 return lsb | (msb << 8);
223 EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
225 int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
230 /* Read MSB first, this will cause the matching LSN to be latched */
231 msb = sch56xx_read_virtual_reg(addr, msb_reg);
235 lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
240 return (msb << 4) | (lsn >> 4);
242 return (msb << 4) | (lsn & 0x0f);
244 EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
250 static int watchdog_set_timeout(struct watchdog_device *wddev,
251 unsigned int timeout)
253 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
254 unsigned int resolution;
258 /* 1 second or 60 second resolution? */
264 if (timeout < resolution || timeout > (resolution * 255))
268 control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC;
270 control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC;
272 if (data->watchdog_control != control) {
273 mutex_lock(data->io_lock);
274 ret = sch56xx_write_virtual_reg(data->addr,
275 SCH56XX_REG_WDOG_CONTROL,
277 mutex_unlock(data->io_lock);
281 data->watchdog_control = control;
285 * Remember new timeout value, but do not write as that (re)starts
286 * the watchdog countdown.
288 data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
289 wddev->timeout = data->watchdog_preset * resolution;
294 static int watchdog_start(struct watchdog_device *wddev)
296 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
301 * The sch56xx's watchdog cannot really be started / stopped
302 * it is always running, but we can avoid the timer expiring
303 * from causing a system reset by clearing the output enable bit.
305 * The sch56xx's watchdog will set the watchdog event bit, bit 0
306 * of the second interrupt source register (at base-address + 9),
307 * when the timer expires.
309 * This will only cause a system reset if the 0-1 flank happens when
310 * output enable is true. Setting output enable after the flank will
311 * not cause a reset, nor will the timer expiring a second time.
312 * This means we must clear the watchdog event bit in case it is set.
314 * The timer may still be running (after a recent watchdog_stop) and
315 * mere milliseconds away from expiring, so the timer must be reset
319 mutex_lock(data->io_lock);
321 /* 1. Reset the watchdog countdown counter */
322 ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
323 data->watchdog_preset);
327 /* 2. Enable output */
328 val = data->watchdog_output_enable | SCH56XX_WDOG_OUTPUT_ENABLE;
329 ret = sch56xx_write_virtual_reg(data->addr,
330 SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
334 data->watchdog_output_enable = val;
336 /* 3. Clear the watchdog event bit if set */
337 val = inb(data->addr + 9);
339 outb(0x01, data->addr + 9);
342 mutex_unlock(data->io_lock);
346 static int watchdog_trigger(struct watchdog_device *wddev)
348 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
351 /* Reset the watchdog countdown counter */
352 mutex_lock(data->io_lock);
353 ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
354 data->watchdog_preset);
355 mutex_unlock(data->io_lock);
360 static int watchdog_stop(struct watchdog_device *wddev)
362 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
366 val = data->watchdog_output_enable & ~SCH56XX_WDOG_OUTPUT_ENABLE;
367 mutex_lock(data->io_lock);
368 ret = sch56xx_write_virtual_reg(data->addr,
369 SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
370 mutex_unlock(data->io_lock);
374 data->watchdog_output_enable = val;
378 static const struct watchdog_ops watchdog_ops = {
379 .owner = THIS_MODULE,
380 .start = watchdog_start,
381 .stop = watchdog_stop,
382 .ping = watchdog_trigger,
383 .set_timeout = watchdog_set_timeout,
386 void sch56xx_watchdog_register(struct device *parent, u16 addr, u32 revision,
387 struct mutex *io_lock, int check_enabled)
389 struct sch56xx_watchdog_data *data;
390 int err, control, output_enable;
392 /* Cache the watchdog registers */
395 sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL);
397 sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE);
398 mutex_unlock(io_lock);
402 if (output_enable < 0)
404 if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
405 pr_warn("Watchdog not enabled by BIOS, not registering\n");
409 data = devm_kzalloc(parent, sizeof(struct sch56xx_watchdog_data), GFP_KERNEL);
414 data->io_lock = io_lock;
416 strscpy(data->wdinfo.identity, "sch56xx watchdog", sizeof(data->wdinfo.identity));
417 data->wdinfo.firmware_version = revision;
418 data->wdinfo.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT;
420 data->wdinfo.options |= WDIOF_MAGICCLOSE;
422 data->wddev.info = &data->wdinfo;
423 data->wddev.ops = &watchdog_ops;
424 data->wddev.parent = parent;
425 data->wddev.timeout = 60;
426 data->wddev.min_timeout = 1;
427 data->wddev.max_timeout = 255 * 60;
428 watchdog_set_nowayout(&data->wddev, nowayout);
429 if (output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)
430 set_bit(WDOG_HW_RUNNING, &data->wddev.status);
432 /* Since the watchdog uses a downcounter there is no register to read
433 the BIOS set timeout from (if any was set at all) ->
434 Choose a preset which will give us a 1 minute timeout */
435 if (control & SCH56XX_WDOG_TIME_BASE_SEC)
436 data->watchdog_preset = 60; /* seconds */
438 data->watchdog_preset = 1; /* minute */
440 data->watchdog_control = control;
441 data->watchdog_output_enable = output_enable;
443 watchdog_set_drvdata(&data->wddev, data);
444 err = devm_watchdog_register_device(parent, &data->wddev);
446 pr_err("Registering watchdog chardev: %d\n", err);
447 devm_kfree(parent, data);
450 EXPORT_SYMBOL(sch56xx_watchdog_register);
453 * platform dev find, add and remove functions
456 static int __init sch56xx_find(int sioaddr, const char **name)
459 unsigned short address;
462 err = superio_enter(sioaddr);
466 devid = superio_inb(sioaddr, SIO_REG_DEVID);
475 pr_debug("Unsupported device id: 0x%02x\n",
476 (unsigned int)devid);
481 superio_select(sioaddr, SIO_SCH56XX_LD_EM);
483 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
484 pr_warn("Device not activated\n");
490 * Warning the order of the low / high byte is the other way around
491 * as on most other superio devices!!
493 address = superio_inb(sioaddr, SIO_REG_ADDR) |
494 superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
496 pr_warn("Base address not set\n");
503 superio_exit(sioaddr);
507 static int __init sch56xx_device_add(int address, const char *name)
509 struct resource res = {
511 .end = address + REGION_LENGTH - 1,
513 .flags = IORESOURCE_IO,
517 err = acpi_check_resource_conflict(&res);
521 sch56xx_pdev = platform_device_register_simple(name, -1, &res, 1);
523 return PTR_ERR_OR_ZERO(sch56xx_pdev);
526 static const struct dmi_system_id sch56xx_dmi_override_table[] __initconst = {
529 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
530 DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS W380"),
535 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
536 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO P710"),
541 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
542 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO E9900"),
548 /* For autoloading only */
549 static const struct dmi_system_id sch56xx_dmi_table[] __initconst = {
552 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
557 MODULE_DEVICE_TABLE(dmi, sch56xx_dmi_table);
559 static int __init sch56xx_init(void)
561 const char *name = NULL;
565 if (!dmi_check_system(sch56xx_dmi_table))
568 if (!dmi_check_system(sch56xx_dmi_override_table)) {
570 * Some machines like the Esprimo P720 and Esprimo C700 have
571 * onboard devices named " Antiope"/" Theseus" instead of
572 * "Antiope"/"Theseus", so we need to check for both.
574 if (!dmi_find_device(DMI_DEV_TYPE_OTHER, "Antiope", NULL) &&
575 !dmi_find_device(DMI_DEV_TYPE_OTHER, " Antiope", NULL) &&
576 !dmi_find_device(DMI_DEV_TYPE_OTHER, "Theseus", NULL) &&
577 !dmi_find_device(DMI_DEV_TYPE_OTHER, " Theseus", NULL))
583 * Some devices like the Esprimo C700 have both onboard devices,
584 * so we still have to check manually
586 address = sch56xx_find(0x4e, &name);
588 address = sch56xx_find(0x2e, &name);
592 return sch56xx_device_add(address, name);
595 static void __exit sch56xx_exit(void)
597 platform_device_unregister(sch56xx_pdev);
600 MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
601 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
602 MODULE_LICENSE("GPL");
604 module_init(sch56xx_init);
605 module_exit(sch56xx_exit);