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/init.h>
11 #include <linux/platform_device.h>
12 #include <linux/err.h>
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
17 #include <linux/watchdog.h>
18 #include <linux/uaccess.h>
19 #include <linux/slab.h>
20 #include "sch56xx-common.h"
22 /* Insmod parameters */
23 static int nowayout
= WATCHDOG_NOWAYOUT
;
24 module_param(nowayout
, int, 0);
25 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
26 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
28 #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */
29 #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
30 #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
32 #define SIO_REG_LDSEL 0x07 /* Logical device select */
33 #define SIO_REG_DEVID 0x20 /* Device ID */
34 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
35 #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
37 #define SIO_SCH5627_ID 0xC6 /* Chipset ID */
38 #define SIO_SCH5636_ID 0xC7 /* Chipset ID */
40 #define REGION_LENGTH 10
42 #define SCH56XX_CMD_READ 0x02
43 #define SCH56XX_CMD_WRITE 0x03
45 /* Watchdog registers */
46 #define SCH56XX_REG_WDOG_PRESET 0x58B
47 #define SCH56XX_REG_WDOG_CONTROL 0x58C
48 #define SCH56XX_WDOG_TIME_BASE_SEC 0x01
49 #define SCH56XX_REG_WDOG_OUTPUT_ENABLE 0x58E
50 #define SCH56XX_WDOG_OUTPUT_ENABLE 0x02
52 struct sch56xx_watchdog_data
{
54 struct mutex
*io_lock
;
55 struct watchdog_info wdinfo
;
56 struct watchdog_device wddev
;
59 u8 watchdog_output_enable
;
62 static struct platform_device
*sch56xx_pdev
;
64 /* Super I/O functions */
65 static inline int superio_inb(int base
, int reg
)
71 static inline int superio_enter(int base
)
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
);
79 outb(SIO_UNLOCK_KEY
, base
);
84 static inline void superio_select(int base
, int ld
)
86 outb(SIO_REG_LDSEL
, base
);
90 static inline void superio_exit(int base
)
92 outb(SIO_LOCK_KEY
, base
);
93 release_region(base
, 2);
96 static int sch56xx_send_cmd(u16 addr
, u8 cmd
, u16 reg
, u8 v
)
101 * According to SMSC for the commands we use the maximum time for
102 * the EM to respond is 15 ms, but testing shows in practice it
103 * responds within 15-32 reads, so we first busy poll, and if
104 * that fails sleep a bit and try again until we are way past
105 * the 15 ms maximum response time.
107 const int max_busy_polls
= 64;
108 const int max_lazy_polls
= 32;
110 /* (Optional) Write-Clear the EC to Host Mailbox Register */
114 /* Set Mailbox Address Pointer to first location in Region 1 */
115 outb(0x00, addr
+ 2);
116 outb(0x80, addr
+ 3);
118 /* Write Request Packet Header */
119 outb(cmd
, addr
+ 4); /* VREG Access Type read:0x02 write:0x03 */
120 outb(0x01, addr
+ 5); /* # of Entries: 1 Byte (8-bit) */
121 outb(0x04, addr
+ 2); /* Mailbox AP to first data entry loc. */
123 /* Write Value field */
124 if (cmd
== SCH56XX_CMD_WRITE
)
127 /* Write Address field */
128 outb(reg
& 0xff, addr
+ 6);
129 outb(reg
>> 8, addr
+ 7);
131 /* Execute the Random Access Command */
132 outb(0x01, addr
); /* Write 01h to the Host-to-EC register */
134 /* EM Interface Polling "Algorithm" */
135 for (i
= 0; i
< max_busy_polls
+ max_lazy_polls
; i
++) {
136 if (i
>= max_busy_polls
)
138 /* Read Interrupt source Register */
140 /* Write Clear the interrupt source bits */
143 /* Command Completed ? */
147 if (i
== max_busy_polls
+ max_lazy_polls
) {
148 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
154 * According to SMSC we may need to retry this, but sofar I've always
155 * seen this succeed in 1 try.
157 for (i
= 0; i
< max_busy_polls
; i
++) {
158 /* Read EC-to-Host Register */
160 /* Command Completed ? */
165 pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
166 (unsigned int)val
, reg
);
168 if (i
== max_busy_polls
) {
169 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
175 * According to the SMSC app note we should now do:
177 * Set Mailbox Address Pointer to first location in Region 1 *
178 * outb(0x00, addr + 2);
179 * outb(0x80, addr + 3);
181 * But if we do that things don't work, so let's not.
184 /* Read Value field */
185 if (cmd
== SCH56XX_CMD_READ
)
186 return inb(addr
+ 4);
191 int sch56xx_read_virtual_reg(u16 addr
, u16 reg
)
193 return sch56xx_send_cmd(addr
, SCH56XX_CMD_READ
, reg
, 0);
195 EXPORT_SYMBOL(sch56xx_read_virtual_reg
);
197 int sch56xx_write_virtual_reg(u16 addr
, u16 reg
, u8 val
)
199 return sch56xx_send_cmd(addr
, SCH56XX_CMD_WRITE
, reg
, val
);
201 EXPORT_SYMBOL(sch56xx_write_virtual_reg
);
203 int sch56xx_read_virtual_reg16(u16 addr
, u16 reg
)
207 /* Read LSB first, this will cause the matching MSB to be latched */
208 lsb
= sch56xx_read_virtual_reg(addr
, reg
);
212 msb
= sch56xx_read_virtual_reg(addr
, reg
+ 1);
216 return lsb
| (msb
<< 8);
218 EXPORT_SYMBOL(sch56xx_read_virtual_reg16
);
220 int sch56xx_read_virtual_reg12(u16 addr
, u16 msb_reg
, u16 lsn_reg
,
225 /* Read MSB first, this will cause the matching LSN to be latched */
226 msb
= sch56xx_read_virtual_reg(addr
, msb_reg
);
230 lsn
= sch56xx_read_virtual_reg(addr
, lsn_reg
);
235 return (msb
<< 4) | (lsn
>> 4);
237 return (msb
<< 4) | (lsn
& 0x0f);
239 EXPORT_SYMBOL(sch56xx_read_virtual_reg12
);
245 static int watchdog_set_timeout(struct watchdog_device
*wddev
,
246 unsigned int timeout
)
248 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
249 unsigned int resolution
;
253 /* 1 second or 60 second resolution? */
259 if (timeout
< resolution
|| timeout
> (resolution
* 255))
263 control
= data
->watchdog_control
| SCH56XX_WDOG_TIME_BASE_SEC
;
265 control
= data
->watchdog_control
& ~SCH56XX_WDOG_TIME_BASE_SEC
;
267 if (data
->watchdog_control
!= control
) {
268 mutex_lock(data
->io_lock
);
269 ret
= sch56xx_write_virtual_reg(data
->addr
,
270 SCH56XX_REG_WDOG_CONTROL
,
272 mutex_unlock(data
->io_lock
);
276 data
->watchdog_control
= control
;
280 * Remember new timeout value, but do not write as that (re)starts
281 * the watchdog countdown.
283 data
->watchdog_preset
= DIV_ROUND_UP(timeout
, resolution
);
284 wddev
->timeout
= data
->watchdog_preset
* resolution
;
289 static int watchdog_start(struct watchdog_device
*wddev
)
291 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
296 * The sch56xx's watchdog cannot really be started / stopped
297 * it is always running, but we can avoid the timer expiring
298 * from causing a system reset by clearing the output enable bit.
300 * The sch56xx's watchdog will set the watchdog event bit, bit 0
301 * of the second interrupt source register (at base-address + 9),
302 * when the timer expires.
304 * This will only cause a system reset if the 0-1 flank happens when
305 * output enable is true. Setting output enable after the flank will
306 * not cause a reset, nor will the timer expiring a second time.
307 * This means we must clear the watchdog event bit in case it is set.
309 * The timer may still be running (after a recent watchdog_stop) and
310 * mere milliseconds away from expiring, so the timer must be reset
314 mutex_lock(data
->io_lock
);
316 /* 1. Reset the watchdog countdown counter */
317 ret
= sch56xx_write_virtual_reg(data
->addr
, SCH56XX_REG_WDOG_PRESET
,
318 data
->watchdog_preset
);
322 /* 2. Enable output */
323 val
= data
->watchdog_output_enable
| SCH56XX_WDOG_OUTPUT_ENABLE
;
324 ret
= sch56xx_write_virtual_reg(data
->addr
,
325 SCH56XX_REG_WDOG_OUTPUT_ENABLE
, val
);
329 data
->watchdog_output_enable
= val
;
331 /* 3. Clear the watchdog event bit if set */
332 val
= inb(data
->addr
+ 9);
334 outb(0x01, data
->addr
+ 9);
337 mutex_unlock(data
->io_lock
);
341 static int watchdog_trigger(struct watchdog_device
*wddev
)
343 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
346 /* Reset the watchdog countdown counter */
347 mutex_lock(data
->io_lock
);
348 ret
= sch56xx_write_virtual_reg(data
->addr
, SCH56XX_REG_WDOG_PRESET
,
349 data
->watchdog_preset
);
350 mutex_unlock(data
->io_lock
);
355 static int watchdog_stop(struct watchdog_device
*wddev
)
357 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
361 val
= data
->watchdog_output_enable
& ~SCH56XX_WDOG_OUTPUT_ENABLE
;
362 mutex_lock(data
->io_lock
);
363 ret
= sch56xx_write_virtual_reg(data
->addr
,
364 SCH56XX_REG_WDOG_OUTPUT_ENABLE
, val
);
365 mutex_unlock(data
->io_lock
);
369 data
->watchdog_output_enable
= val
;
373 static const struct watchdog_ops watchdog_ops
= {
374 .owner
= THIS_MODULE
,
375 .start
= watchdog_start
,
376 .stop
= watchdog_stop
,
377 .ping
= watchdog_trigger
,
378 .set_timeout
= watchdog_set_timeout
,
381 struct sch56xx_watchdog_data
*sch56xx_watchdog_register(struct device
*parent
,
382 u16 addr
, u32 revision
, struct mutex
*io_lock
, int check_enabled
)
384 struct sch56xx_watchdog_data
*data
;
385 int err
, control
, output_enable
;
387 /* Cache the watchdog registers */
390 sch56xx_read_virtual_reg(addr
, SCH56XX_REG_WDOG_CONTROL
);
392 sch56xx_read_virtual_reg(addr
, SCH56XX_REG_WDOG_OUTPUT_ENABLE
);
393 mutex_unlock(io_lock
);
397 if (output_enable
< 0)
399 if (check_enabled
&& !(output_enable
& SCH56XX_WDOG_OUTPUT_ENABLE
)) {
400 pr_warn("Watchdog not enabled by BIOS, not registering\n");
404 data
= kzalloc(sizeof(struct sch56xx_watchdog_data
), GFP_KERNEL
);
409 data
->io_lock
= io_lock
;
411 strlcpy(data
->wdinfo
.identity
, "sch56xx watchdog",
412 sizeof(data
->wdinfo
.identity
));
413 data
->wdinfo
.firmware_version
= revision
;
414 data
->wdinfo
.options
= WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
;
416 data
->wdinfo
.options
|= WDIOF_MAGICCLOSE
;
418 data
->wddev
.info
= &data
->wdinfo
;
419 data
->wddev
.ops
= &watchdog_ops
;
420 data
->wddev
.parent
= parent
;
421 data
->wddev
.timeout
= 60;
422 data
->wddev
.min_timeout
= 1;
423 data
->wddev
.max_timeout
= 255 * 60;
425 set_bit(WDOG_NO_WAY_OUT
, &data
->wddev
.status
);
426 if (output_enable
& SCH56XX_WDOG_OUTPUT_ENABLE
)
427 set_bit(WDOG_ACTIVE
, &data
->wddev
.status
);
429 /* Since the watchdog uses a downcounter there is no register to read
430 the BIOS set timeout from (if any was set at all) ->
431 Choose a preset which will give us a 1 minute timeout */
432 if (control
& SCH56XX_WDOG_TIME_BASE_SEC
)
433 data
->watchdog_preset
= 60; /* seconds */
435 data
->watchdog_preset
= 1; /* minute */
437 data
->watchdog_control
= control
;
438 data
->watchdog_output_enable
= output_enable
;
440 watchdog_set_drvdata(&data
->wddev
, data
);
441 err
= watchdog_register_device(&data
->wddev
);
443 pr_err("Registering watchdog chardev: %d\n", err
);
450 EXPORT_SYMBOL(sch56xx_watchdog_register
);
452 void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data
*data
)
454 watchdog_unregister_device(&data
->wddev
);
457 EXPORT_SYMBOL(sch56xx_watchdog_unregister
);
460 * platform dev find, add and remove functions
463 static int __init
sch56xx_find(int sioaddr
, const char **name
)
466 unsigned short address
;
469 err
= superio_enter(sioaddr
);
473 devid
= superio_inb(sioaddr
, SIO_REG_DEVID
);
482 pr_debug("Unsupported device id: 0x%02x\n",
483 (unsigned int)devid
);
488 superio_select(sioaddr
, SIO_SCH56XX_LD_EM
);
490 if (!(superio_inb(sioaddr
, SIO_REG_ENABLE
) & 0x01)) {
491 pr_warn("Device not activated\n");
497 * Warning the order of the low / high byte is the other way around
498 * as on most other superio devices!!
500 address
= superio_inb(sioaddr
, SIO_REG_ADDR
) |
501 superio_inb(sioaddr
, SIO_REG_ADDR
+ 1) << 8;
503 pr_warn("Base address not set\n");
510 superio_exit(sioaddr
);
514 static int __init
sch56xx_device_add(int address
, const char *name
)
516 struct resource res
= {
518 .end
= address
+ REGION_LENGTH
- 1,
519 .flags
= IORESOURCE_IO
,
523 sch56xx_pdev
= platform_device_alloc(name
, address
);
527 res
.name
= sch56xx_pdev
->name
;
528 err
= acpi_check_resource_conflict(&res
);
530 goto exit_device_put
;
532 err
= platform_device_add_resources(sch56xx_pdev
, &res
, 1);
534 pr_err("Device resource addition failed\n");
535 goto exit_device_put
;
538 err
= platform_device_add(sch56xx_pdev
);
540 pr_err("Device addition failed\n");
541 goto exit_device_put
;
547 platform_device_put(sch56xx_pdev
);
552 static int __init
sch56xx_init(void)
555 const char *name
= NULL
;
557 address
= sch56xx_find(0x4e, &name
);
559 address
= sch56xx_find(0x2e, &name
);
563 return sch56xx_device_add(address
, name
);
566 static void __exit
sch56xx_exit(void)
568 platform_device_unregister(sch56xx_pdev
);
571 MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
572 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
573 MODULE_LICENSE("GPL");
575 module_init(sch56xx_init
);
576 module_exit(sch56xx_exit
);