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/regmap.h>
13 #include <linux/err.h>
15 #include <linux/acpi.h>
16 #include <linux/delay.h>
18 #include <linux/watchdog.h>
19 #include <linux/uaccess.h>
20 #include <linux/slab.h>
21 #include "sch56xx-common.h"
23 /* Insmod parameters */
24 static bool nowayout
= WATCHDOG_NOWAYOUT
;
25 module_param(nowayout
, bool, 0);
26 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
27 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
29 #define SIO_SCH56XX_LD_EM 0x0C /* Embedded uController Logical Dev */
30 #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
31 #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
33 #define SIO_REG_LDSEL 0x07 /* Logical device select */
34 #define SIO_REG_DEVID 0x20 /* Device ID */
35 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
36 #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
38 #define SIO_SCH5627_ID 0xC6 /* Chipset ID */
39 #define SIO_SCH5636_ID 0xC7 /* Chipset ID */
41 #define REGION_LENGTH 10
43 #define SCH56XX_CMD_READ 0x02
44 #define SCH56XX_CMD_WRITE 0x03
46 /* Watchdog registers */
47 #define SCH56XX_REG_WDOG_PRESET 0x58B
48 #define SCH56XX_REG_WDOG_CONTROL 0x58C
49 #define SCH56XX_WDOG_TIME_BASE_SEC 0x01
50 #define SCH56XX_REG_WDOG_OUTPUT_ENABLE 0x58E
51 #define SCH56XX_WDOG_OUTPUT_ENABLE 0x02
53 struct sch56xx_watchdog_data
{
55 struct mutex
*io_lock
;
56 struct watchdog_info wdinfo
;
57 struct watchdog_device wddev
;
60 u8 watchdog_output_enable
;
63 struct sch56xx_bus_context
{
64 struct mutex
*lock
; /* Used to serialize access to the mailbox registers */
68 static struct platform_device
*sch56xx_pdev
;
70 /* Super I/O functions */
71 static inline int superio_inb(int base
, int reg
)
77 static inline int superio_enter(int base
)
79 /* Don't step on other drivers' I/O space by accident */
80 if (!request_muxed_region(base
, 2, "sch56xx")) {
81 pr_err("I/O address 0x%04x already in use\n", base
);
85 outb(SIO_UNLOCK_KEY
, base
);
90 static inline void superio_select(int base
, int ld
)
92 outb(SIO_REG_LDSEL
, base
);
96 static inline void superio_exit(int base
)
98 outb(SIO_LOCK_KEY
, base
);
99 release_region(base
, 2);
102 static int sch56xx_send_cmd(u16 addr
, u8 cmd
, u16 reg
, u8 v
)
107 * According to SMSC for the commands we use the maximum time for
108 * the EM to respond is 15 ms, but testing shows in practice it
109 * responds within 15-32 reads, so we first busy poll, and if
110 * that fails sleep a bit and try again until we are way past
111 * the 15 ms maximum response time.
113 const int max_busy_polls
= 64;
114 const int max_lazy_polls
= 32;
116 /* (Optional) Write-Clear the EC to Host Mailbox Register */
120 /* Set Mailbox Address Pointer to first location in Region 1 */
121 outb(0x00, addr
+ 2);
122 outb(0x80, addr
+ 3);
124 /* Write Request Packet Header */
125 outb(cmd
, addr
+ 4); /* VREG Access Type read:0x02 write:0x03 */
126 outb(0x01, addr
+ 5); /* # of Entries: 1 Byte (8-bit) */
127 outb(0x04, addr
+ 2); /* Mailbox AP to first data entry loc. */
129 /* Write Value field */
130 if (cmd
== SCH56XX_CMD_WRITE
)
133 /* Write Address field */
134 outb(reg
& 0xff, addr
+ 6);
135 outb(reg
>> 8, addr
+ 7);
137 /* Execute the Random Access Command */
138 outb(0x01, addr
); /* Write 01h to the Host-to-EC register */
140 /* EM Interface Polling "Algorithm" */
141 for (i
= 0; i
< max_busy_polls
+ max_lazy_polls
; i
++) {
142 if (i
>= max_busy_polls
)
143 usleep_range(1000, 2000);
144 /* Read Interrupt source Register */
146 /* Write Clear the interrupt source bits */
149 /* Command Completed ? */
153 if (i
== max_busy_polls
+ max_lazy_polls
) {
154 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
160 * According to SMSC we may need to retry this, but sofar I've always
161 * seen this succeed in 1 try.
163 for (i
= 0; i
< max_busy_polls
; i
++) {
164 /* Read EC-to-Host Register */
166 /* Command Completed ? */
171 pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
172 (unsigned int)val
, reg
);
174 if (i
== max_busy_polls
) {
175 pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
181 * According to the SMSC app note we should now do:
183 * Set Mailbox Address Pointer to first location in Region 1 *
184 * outb(0x00, addr + 2);
185 * outb(0x80, addr + 3);
187 * But if we do that things don't work, so let's not.
190 /* Read Value field */
191 if (cmd
== SCH56XX_CMD_READ
)
192 return inb(addr
+ 4);
197 int sch56xx_read_virtual_reg(u16 addr
, u16 reg
)
199 return sch56xx_send_cmd(addr
, SCH56XX_CMD_READ
, reg
, 0);
201 EXPORT_SYMBOL(sch56xx_read_virtual_reg
);
203 int sch56xx_write_virtual_reg(u16 addr
, u16 reg
, u8 val
)
205 return sch56xx_send_cmd(addr
, SCH56XX_CMD_WRITE
, reg
, val
);
207 EXPORT_SYMBOL(sch56xx_write_virtual_reg
);
209 int sch56xx_read_virtual_reg16(u16 addr
, u16 reg
)
213 /* Read LSB first, this will cause the matching MSB to be latched */
214 lsb
= sch56xx_read_virtual_reg(addr
, reg
);
218 msb
= sch56xx_read_virtual_reg(addr
, reg
+ 1);
222 return lsb
| (msb
<< 8);
224 EXPORT_SYMBOL(sch56xx_read_virtual_reg16
);
226 int sch56xx_read_virtual_reg12(u16 addr
, u16 msb_reg
, u16 lsn_reg
,
231 /* Read MSB first, this will cause the matching LSN to be latched */
232 msb
= sch56xx_read_virtual_reg(addr
, msb_reg
);
236 lsn
= sch56xx_read_virtual_reg(addr
, lsn_reg
);
241 return (msb
<< 4) | (lsn
>> 4);
243 return (msb
<< 4) | (lsn
& 0x0f);
245 EXPORT_SYMBOL(sch56xx_read_virtual_reg12
);
251 int sch56xx_regmap_read16(struct regmap
*map
, unsigned int reg
, unsigned int *val
)
255 /* See sch56xx_read_virtual_reg16() */
256 ret
= regmap_read(map
, reg
, &lsb
);
260 ret
= regmap_read(map
, reg
+ 1, &msb
);
264 *val
= lsb
| (msb
<< 8);
268 EXPORT_SYMBOL(sch56xx_regmap_read16
);
270 int sch56xx_regmap_write16(struct regmap
*map
, unsigned int reg
, unsigned int val
)
274 ret
= regmap_write(map
, reg
, val
& 0xff);
278 return regmap_write(map
, reg
+ 1, (val
>> 8) & 0xff);
280 EXPORT_SYMBOL(sch56xx_regmap_write16
);
282 static int sch56xx_reg_write(void *context
, unsigned int reg
, unsigned int val
)
284 struct sch56xx_bus_context
*bus
= context
;
287 mutex_lock(bus
->lock
);
288 ret
= sch56xx_write_virtual_reg(bus
->addr
, (u16
)reg
, (u8
)val
);
289 mutex_unlock(bus
->lock
);
294 static int sch56xx_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
296 struct sch56xx_bus_context
*bus
= context
;
299 mutex_lock(bus
->lock
);
300 ret
= sch56xx_read_virtual_reg(bus
->addr
, (u16
)reg
);
301 mutex_unlock(bus
->lock
);
311 static void sch56xx_free_context(void *context
)
316 static const struct regmap_bus sch56xx_bus
= {
317 .reg_write
= sch56xx_reg_write
,
318 .reg_read
= sch56xx_reg_read
,
319 .free_context
= sch56xx_free_context
,
320 .reg_format_endian_default
= REGMAP_ENDIAN_LITTLE
,
321 .val_format_endian_default
= REGMAP_ENDIAN_LITTLE
,
324 struct regmap
*devm_regmap_init_sch56xx(struct device
*dev
, struct mutex
*lock
, u16 addr
,
325 const struct regmap_config
*config
)
327 struct sch56xx_bus_context
*context
;
330 if (config
->reg_bits
!= 16 && config
->val_bits
!= 8)
331 return ERR_PTR(-EOPNOTSUPP
);
333 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
335 return ERR_PTR(-ENOMEM
);
337 context
->lock
= lock
;
338 context
->addr
= addr
;
340 map
= devm_regmap_init(dev
, &sch56xx_bus
, context
, config
);
346 EXPORT_SYMBOL(devm_regmap_init_sch56xx
);
352 static int watchdog_set_timeout(struct watchdog_device
*wddev
,
353 unsigned int timeout
)
355 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
356 unsigned int resolution
;
360 /* 1 second or 60 second resolution? */
366 if (timeout
< resolution
|| timeout
> (resolution
* 255))
370 control
= data
->watchdog_control
| SCH56XX_WDOG_TIME_BASE_SEC
;
372 control
= data
->watchdog_control
& ~SCH56XX_WDOG_TIME_BASE_SEC
;
374 if (data
->watchdog_control
!= control
) {
375 mutex_lock(data
->io_lock
);
376 ret
= sch56xx_write_virtual_reg(data
->addr
,
377 SCH56XX_REG_WDOG_CONTROL
,
379 mutex_unlock(data
->io_lock
);
383 data
->watchdog_control
= control
;
387 * Remember new timeout value, but do not write as that (re)starts
388 * the watchdog countdown.
390 data
->watchdog_preset
= DIV_ROUND_UP(timeout
, resolution
);
391 wddev
->timeout
= data
->watchdog_preset
* resolution
;
396 static int watchdog_start(struct watchdog_device
*wddev
)
398 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
403 * The sch56xx's watchdog cannot really be started / stopped
404 * it is always running, but we can avoid the timer expiring
405 * from causing a system reset by clearing the output enable bit.
407 * The sch56xx's watchdog will set the watchdog event bit, bit 0
408 * of the second interrupt source register (at base-address + 9),
409 * when the timer expires.
411 * This will only cause a system reset if the 0-1 flank happens when
412 * output enable is true. Setting output enable after the flank will
413 * not cause a reset, nor will the timer expiring a second time.
414 * This means we must clear the watchdog event bit in case it is set.
416 * The timer may still be running (after a recent watchdog_stop) and
417 * mere milliseconds away from expiring, so the timer must be reset
421 mutex_lock(data
->io_lock
);
423 /* 1. Reset the watchdog countdown counter */
424 ret
= sch56xx_write_virtual_reg(data
->addr
, SCH56XX_REG_WDOG_PRESET
,
425 data
->watchdog_preset
);
429 /* 2. Enable output */
430 val
= data
->watchdog_output_enable
| SCH56XX_WDOG_OUTPUT_ENABLE
;
431 ret
= sch56xx_write_virtual_reg(data
->addr
,
432 SCH56XX_REG_WDOG_OUTPUT_ENABLE
, val
);
436 data
->watchdog_output_enable
= val
;
438 /* 3. Clear the watchdog event bit if set */
439 val
= inb(data
->addr
+ 9);
441 outb(0x01, data
->addr
+ 9);
444 mutex_unlock(data
->io_lock
);
448 static int watchdog_trigger(struct watchdog_device
*wddev
)
450 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
453 /* Reset the watchdog countdown counter */
454 mutex_lock(data
->io_lock
);
455 ret
= sch56xx_write_virtual_reg(data
->addr
, SCH56XX_REG_WDOG_PRESET
,
456 data
->watchdog_preset
);
457 mutex_unlock(data
->io_lock
);
462 static int watchdog_stop(struct watchdog_device
*wddev
)
464 struct sch56xx_watchdog_data
*data
= watchdog_get_drvdata(wddev
);
468 val
= data
->watchdog_output_enable
& ~SCH56XX_WDOG_OUTPUT_ENABLE
;
469 mutex_lock(data
->io_lock
);
470 ret
= sch56xx_write_virtual_reg(data
->addr
,
471 SCH56XX_REG_WDOG_OUTPUT_ENABLE
, val
);
472 mutex_unlock(data
->io_lock
);
476 data
->watchdog_output_enable
= val
;
480 static const struct watchdog_ops watchdog_ops
= {
481 .owner
= THIS_MODULE
,
482 .start
= watchdog_start
,
483 .stop
= watchdog_stop
,
484 .ping
= watchdog_trigger
,
485 .set_timeout
= watchdog_set_timeout
,
488 void sch56xx_watchdog_register(struct device
*parent
, u16 addr
, u32 revision
,
489 struct mutex
*io_lock
, int check_enabled
)
491 struct sch56xx_watchdog_data
*data
;
492 int err
, control
, output_enable
;
494 /* Cache the watchdog registers */
497 sch56xx_read_virtual_reg(addr
, SCH56XX_REG_WDOG_CONTROL
);
499 sch56xx_read_virtual_reg(addr
, SCH56XX_REG_WDOG_OUTPUT_ENABLE
);
500 mutex_unlock(io_lock
);
504 if (output_enable
< 0)
506 if (check_enabled
&& !(output_enable
& SCH56XX_WDOG_OUTPUT_ENABLE
)) {
507 pr_warn("Watchdog not enabled by BIOS, not registering\n");
511 data
= devm_kzalloc(parent
, sizeof(struct sch56xx_watchdog_data
), GFP_KERNEL
);
516 data
->io_lock
= io_lock
;
518 strscpy(data
->wdinfo
.identity
, "sch56xx watchdog", sizeof(data
->wdinfo
.identity
));
519 data
->wdinfo
.firmware_version
= revision
;
520 data
->wdinfo
.options
= WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
;
522 data
->wdinfo
.options
|= WDIOF_MAGICCLOSE
;
524 data
->wddev
.info
= &data
->wdinfo
;
525 data
->wddev
.ops
= &watchdog_ops
;
526 data
->wddev
.parent
= parent
;
527 data
->wddev
.timeout
= 60;
528 data
->wddev
.min_timeout
= 1;
529 data
->wddev
.max_timeout
= 255 * 60;
530 watchdog_set_nowayout(&data
->wddev
, nowayout
);
531 if (output_enable
& SCH56XX_WDOG_OUTPUT_ENABLE
)
532 set_bit(WDOG_HW_RUNNING
, &data
->wddev
.status
);
534 /* Since the watchdog uses a downcounter there is no register to read
535 the BIOS set timeout from (if any was set at all) ->
536 Choose a preset which will give us a 1 minute timeout */
537 if (control
& SCH56XX_WDOG_TIME_BASE_SEC
)
538 data
->watchdog_preset
= 60; /* seconds */
540 data
->watchdog_preset
= 1; /* minute */
542 data
->watchdog_control
= control
;
543 data
->watchdog_output_enable
= output_enable
;
545 watchdog_set_drvdata(&data
->wddev
, data
);
546 err
= devm_watchdog_register_device(parent
, &data
->wddev
);
548 pr_err("Registering watchdog chardev: %d\n", err
);
549 devm_kfree(parent
, data
);
552 EXPORT_SYMBOL(sch56xx_watchdog_register
);
555 * platform dev find, add and remove functions
558 static int __init
sch56xx_find(int sioaddr
, const char **name
)
561 unsigned short address
;
564 err
= superio_enter(sioaddr
);
568 devid
= superio_inb(sioaddr
, SIO_REG_DEVID
);
577 pr_debug("Unsupported device id: 0x%02x\n",
578 (unsigned int)devid
);
583 superio_select(sioaddr
, SIO_SCH56XX_LD_EM
);
585 if (!(superio_inb(sioaddr
, SIO_REG_ENABLE
) & 0x01)) {
586 pr_warn("Device not activated\n");
592 * Warning the order of the low / high byte is the other way around
593 * as on most other superio devices!!
595 address
= superio_inb(sioaddr
, SIO_REG_ADDR
) |
596 superio_inb(sioaddr
, SIO_REG_ADDR
+ 1) << 8;
598 pr_warn("Base address not set\n");
605 superio_exit(sioaddr
);
609 static int __init
sch56xx_device_add(int address
, const char *name
)
611 struct resource res
= {
613 .end
= address
+ REGION_LENGTH
- 1,
615 .flags
= IORESOURCE_IO
,
619 err
= acpi_check_resource_conflict(&res
);
623 sch56xx_pdev
= platform_device_register_simple(name
, -1, &res
, 1);
625 return PTR_ERR_OR_ZERO(sch56xx_pdev
);
628 static int __init
sch56xx_init(void)
631 const char *name
= NULL
;
633 address
= sch56xx_find(0x4e, &name
);
635 address
= sch56xx_find(0x2e, &name
);
639 return sch56xx_device_add(address
, name
);
642 static void __exit
sch56xx_exit(void)
644 platform_device_unregister(sch56xx_pdev
);
647 MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
648 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
649 MODULE_LICENSE("GPL");
651 module_init(sch56xx_init
);
652 module_exit(sch56xx_exit
);