xref: /openbmc/linux/drivers/hwmon/sch56xx-common.c (revision b97d6790d03b763eca08847a9a5869a4291b9f9a)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
228ff2f7aSHans de Goede /***************************************************************************
3312869ecSHans de Goede  *   Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>           *
428ff2f7aSHans de Goede  *                                                                         *
528ff2f7aSHans de Goede  ***************************************************************************/
628ff2f7aSHans de Goede 
728ff2f7aSHans de Goede #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
828ff2f7aSHans de Goede 
928ff2f7aSHans de Goede #include <linux/module.h>
1028ff2f7aSHans de Goede #include <linux/init.h>
1128ff2f7aSHans de Goede #include <linux/platform_device.h>
1228ff2f7aSHans de Goede #include <linux/err.h>
1328ff2f7aSHans de Goede #include <linux/io.h>
1428ff2f7aSHans de Goede #include <linux/acpi.h>
1528ff2f7aSHans de Goede #include <linux/delay.h>
16312869ecSHans de Goede #include <linux/fs.h>
17312869ecSHans de Goede #include <linux/watchdog.h>
18312869ecSHans de Goede #include <linux/uaccess.h>
19312869ecSHans de Goede #include <linux/slab.h>
2028ff2f7aSHans de Goede #include "sch56xx-common.h"
2128ff2f7aSHans de Goede 
22*a32a93ecSGuenter Roeck /* Insmod parameters */
23989c9c67SArmin Wolf static bool nowayout = WATCHDOG_NOWAYOUT;
24989c9c67SArmin Wolf module_param(nowayout, bool, 0);
25312869ecSHans de Goede MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
26312869ecSHans de Goede 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
27312869ecSHans de Goede 
2828ff2f7aSHans de Goede #define SIO_SCH56XX_LD_EM	0x0C	/* Embedded uController Logical Dev */
2928ff2f7aSHans de Goede #define SIO_UNLOCK_KEY		0x55	/* Key to enable Super-I/O */
3028ff2f7aSHans de Goede #define SIO_LOCK_KEY		0xAA	/* Key to disable Super-I/O */
3128ff2f7aSHans de Goede 
3228ff2f7aSHans de Goede #define SIO_REG_LDSEL		0x07	/* Logical device select */
3328ff2f7aSHans de Goede #define SIO_REG_DEVID		0x20	/* Device ID */
3428ff2f7aSHans de Goede #define SIO_REG_ENABLE		0x30	/* Logical device enable */
3528ff2f7aSHans de Goede #define SIO_REG_ADDR		0x66	/* Logical device address (2 bytes) */
3628ff2f7aSHans de Goede 
3728ff2f7aSHans de Goede #define SIO_SCH5627_ID		0xC6	/* Chipset ID */
380772a640SHans de Goede #define SIO_SCH5636_ID		0xC7	/* Chipset ID */
3928ff2f7aSHans de Goede 
40312869ecSHans de Goede #define REGION_LENGTH		10
4128ff2f7aSHans de Goede 
4228ff2f7aSHans de Goede #define SCH56XX_CMD_READ	0x02
4328ff2f7aSHans de Goede #define SCH56XX_CMD_WRITE	0x03
4428ff2f7aSHans de Goede 
45312869ecSHans de Goede /* Watchdog registers */
46312869ecSHans de Goede #define SCH56XX_REG_WDOG_PRESET		0x58B
47312869ecSHans de Goede #define SCH56XX_REG_WDOG_CONTROL	0x58C
48312869ecSHans de Goede #define SCH56XX_WDOG_TIME_BASE_SEC	0x01
49312869ecSHans de Goede #define SCH56XX_REG_WDOG_OUTPUT_ENABLE	0x58E
50312869ecSHans de Goede #define SCH56XX_WDOG_OUTPUT_ENABLE	0x02
51312869ecSHans de Goede 
52312869ecSHans de Goede struct sch56xx_watchdog_data {
53312869ecSHans de Goede 	u16 addr;
54312869ecSHans de Goede 	struct mutex *io_lock;
55fb551405SHans de Goede 	struct watchdog_info wdinfo;
56fb551405SHans de Goede 	struct watchdog_device wddev;
57312869ecSHans de Goede 	u8 watchdog_preset;
58312869ecSHans de Goede 	u8 watchdog_control;
59312869ecSHans de Goede 	u8 watchdog_output_enable;
60312869ecSHans de Goede };
61312869ecSHans de Goede 
6228ff2f7aSHans de Goede static struct platform_device *sch56xx_pdev;
6328ff2f7aSHans de Goede 
6428ff2f7aSHans de Goede /* Super I/O functions */
superio_inb(int base,int reg)6528ff2f7aSHans de Goede static inline int superio_inb(int base, int reg)
6628ff2f7aSHans de Goede {
6728ff2f7aSHans de Goede 	outb(reg, base);
6828ff2f7aSHans de Goede 	return inb(base + 1);
6928ff2f7aSHans de Goede }
7028ff2f7aSHans de Goede 
superio_enter(int base)7128ff2f7aSHans de Goede static inline int superio_enter(int base)
7228ff2f7aSHans de Goede {
7328ff2f7aSHans de Goede 	/* Don't step on other drivers' I/O space by accident */
7428ff2f7aSHans de Goede 	if (!request_muxed_region(base, 2, "sch56xx")) {
7528ff2f7aSHans de Goede 		pr_err("I/O address 0x%04x already in use\n", base);
7628ff2f7aSHans de Goede 		return -EBUSY;
7728ff2f7aSHans de Goede 	}
7828ff2f7aSHans de Goede 
7928ff2f7aSHans de Goede 	outb(SIO_UNLOCK_KEY, base);
8028ff2f7aSHans de Goede 
8128ff2f7aSHans de Goede 	return 0;
8228ff2f7aSHans de Goede }
8328ff2f7aSHans de Goede 
superio_select(int base,int ld)8428ff2f7aSHans de Goede static inline void superio_select(int base, int ld)
8528ff2f7aSHans de Goede {
8628ff2f7aSHans de Goede 	outb(SIO_REG_LDSEL, base);
8728ff2f7aSHans de Goede 	outb(ld, base + 1);
8828ff2f7aSHans de Goede }
8928ff2f7aSHans de Goede 
superio_exit(int base)9028ff2f7aSHans de Goede static inline void superio_exit(int base)
9128ff2f7aSHans de Goede {
9228ff2f7aSHans de Goede 	outb(SIO_LOCK_KEY, base);
9328ff2f7aSHans de Goede 	release_region(base, 2);
9428ff2f7aSHans de Goede }
9528ff2f7aSHans de Goede 
sch56xx_send_cmd(u16 addr,u8 cmd,u16 reg,u8 v)9628ff2f7aSHans de Goede static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
9728ff2f7aSHans de Goede {
9828ff2f7aSHans de Goede 	u8 val;
9928ff2f7aSHans de Goede 	int i;
10028ff2f7aSHans de Goede 	/*
10128ff2f7aSHans de Goede 	 * According to SMSC for the commands we use the maximum time for
10228ff2f7aSHans de Goede 	 * the EM to respond is 15 ms, but testing shows in practice it
10328ff2f7aSHans de Goede 	 * responds within 15-32 reads, so we first busy poll, and if
10428ff2f7aSHans de Goede 	 * that fails sleep a bit and try again until we are way past
10528ff2f7aSHans de Goede 	 * the 15 ms maximum response time.
10628ff2f7aSHans de Goede 	 */
10728ff2f7aSHans de Goede 	const int max_busy_polls = 64;
10828ff2f7aSHans de Goede 	const int max_lazy_polls = 32;
10928ff2f7aSHans de Goede 
11028ff2f7aSHans de Goede 	/* (Optional) Write-Clear the EC to Host Mailbox Register */
11128ff2f7aSHans de Goede 	val = inb(addr + 1);
11228ff2f7aSHans de Goede 	outb(val, addr + 1);
11328ff2f7aSHans de Goede 
11428ff2f7aSHans de Goede 	/* Set Mailbox Address Pointer to first location in Region 1 */
11528ff2f7aSHans de Goede 	outb(0x00, addr + 2);
11628ff2f7aSHans de Goede 	outb(0x80, addr + 3);
11728ff2f7aSHans de Goede 
11828ff2f7aSHans de Goede 	/* Write Request Packet Header */
11928ff2f7aSHans de Goede 	outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */
12028ff2f7aSHans de Goede 	outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */
12128ff2f7aSHans de Goede 	outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */
12228ff2f7aSHans de Goede 
12328ff2f7aSHans de Goede 	/* Write Value field */
12428ff2f7aSHans de Goede 	if (cmd == SCH56XX_CMD_WRITE)
12528ff2f7aSHans de Goede 		outb(v, addr + 4);
12628ff2f7aSHans de Goede 
12728ff2f7aSHans de Goede 	/* Write Address field */
12828ff2f7aSHans de Goede 	outb(reg & 0xff, addr + 6);
12928ff2f7aSHans de Goede 	outb(reg >> 8, addr + 7);
13028ff2f7aSHans de Goede 
13128ff2f7aSHans de Goede 	/* Execute the Random Access Command */
13228ff2f7aSHans de Goede 	outb(0x01, addr); /* Write 01h to the Host-to-EC register */
13328ff2f7aSHans de Goede 
13428ff2f7aSHans de Goede 	/* EM Interface Polling "Algorithm" */
13528ff2f7aSHans de Goede 	for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
13628ff2f7aSHans de Goede 		if (i >= max_busy_polls)
137799c3e1eSArmin Wolf 			usleep_range(1000, 2000);
13828ff2f7aSHans de Goede 		/* Read Interrupt source Register */
13928ff2f7aSHans de Goede 		val = inb(addr + 8);
14028ff2f7aSHans de Goede 		/* Write Clear the interrupt source bits */
14128ff2f7aSHans de Goede 		if (val)
14228ff2f7aSHans de Goede 			outb(val, addr + 8);
14328ff2f7aSHans de Goede 		/* Command Completed ? */
14428ff2f7aSHans de Goede 		if (val & 0x01)
14528ff2f7aSHans de Goede 			break;
14628ff2f7aSHans de Goede 	}
14728ff2f7aSHans de Goede 	if (i == max_busy_polls + max_lazy_polls) {
148b55f3757SGuenter Roeck 		pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
149b55f3757SGuenter Roeck 		       reg, 1);
15028ff2f7aSHans de Goede 		return -EIO;
15128ff2f7aSHans de Goede 	}
15228ff2f7aSHans de Goede 
15328ff2f7aSHans de Goede 	/*
15428ff2f7aSHans de Goede 	 * According to SMSC we may need to retry this, but sofar I've always
15528ff2f7aSHans de Goede 	 * seen this succeed in 1 try.
15628ff2f7aSHans de Goede 	 */
15728ff2f7aSHans de Goede 	for (i = 0; i < max_busy_polls; i++) {
15828ff2f7aSHans de Goede 		/* Read EC-to-Host Register */
15928ff2f7aSHans de Goede 		val = inb(addr + 1);
16028ff2f7aSHans de Goede 		/* Command Completed ? */
16128ff2f7aSHans de Goede 		if (val == 0x01)
16228ff2f7aSHans de Goede 			break;
16328ff2f7aSHans de Goede 
16428ff2f7aSHans de Goede 		if (i == 0)
165b55f3757SGuenter Roeck 			pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
166b55f3757SGuenter Roeck 				(unsigned int)val, reg);
16728ff2f7aSHans de Goede 	}
16828ff2f7aSHans de Goede 	if (i == max_busy_polls) {
169b55f3757SGuenter Roeck 		pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
170b55f3757SGuenter Roeck 		       reg, 2);
17128ff2f7aSHans de Goede 		return -EIO;
17228ff2f7aSHans de Goede 	}
17328ff2f7aSHans de Goede 
17428ff2f7aSHans de Goede 	/*
17528ff2f7aSHans de Goede 	 * According to the SMSC app note we should now do:
17628ff2f7aSHans de Goede 	 *
17728ff2f7aSHans de Goede 	 * Set Mailbox Address Pointer to first location in Region 1 *
17828ff2f7aSHans de Goede 	 * outb(0x00, addr + 2);
17928ff2f7aSHans de Goede 	 * outb(0x80, addr + 3);
18028ff2f7aSHans de Goede 	 *
18128ff2f7aSHans de Goede 	 * But if we do that things don't work, so let's not.
18228ff2f7aSHans de Goede 	 */
18328ff2f7aSHans de Goede 
18428ff2f7aSHans de Goede 	/* Read Value field */
18528ff2f7aSHans de Goede 	if (cmd == SCH56XX_CMD_READ)
18628ff2f7aSHans de Goede 		return inb(addr + 4);
18728ff2f7aSHans de Goede 
18828ff2f7aSHans de Goede 	return 0;
18928ff2f7aSHans de Goede }
19028ff2f7aSHans de Goede 
sch56xx_read_virtual_reg(u16 addr,u16 reg)19128ff2f7aSHans de Goede int sch56xx_read_virtual_reg(u16 addr, u16 reg)
19228ff2f7aSHans de Goede {
19328ff2f7aSHans de Goede 	return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
19428ff2f7aSHans de Goede }
19528ff2f7aSHans de Goede EXPORT_SYMBOL(sch56xx_read_virtual_reg);
19628ff2f7aSHans de Goede 
sch56xx_write_virtual_reg(u16 addr,u16 reg,u8 val)19728ff2f7aSHans de Goede int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
19828ff2f7aSHans de Goede {
19928ff2f7aSHans de Goede 	return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
20028ff2f7aSHans de Goede }
20128ff2f7aSHans de Goede EXPORT_SYMBOL(sch56xx_write_virtual_reg);
20228ff2f7aSHans de Goede 
sch56xx_read_virtual_reg16(u16 addr,u16 reg)20328ff2f7aSHans de Goede int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
20428ff2f7aSHans de Goede {
20528ff2f7aSHans de Goede 	int lsb, msb;
20628ff2f7aSHans de Goede 
20728ff2f7aSHans de Goede 	/* Read LSB first, this will cause the matching MSB to be latched */
20828ff2f7aSHans de Goede 	lsb = sch56xx_read_virtual_reg(addr, reg);
20928ff2f7aSHans de Goede 	if (lsb < 0)
21028ff2f7aSHans de Goede 		return lsb;
21128ff2f7aSHans de Goede 
21228ff2f7aSHans de Goede 	msb = sch56xx_read_virtual_reg(addr, reg + 1);
21328ff2f7aSHans de Goede 	if (msb < 0)
21428ff2f7aSHans de Goede 		return msb;
21528ff2f7aSHans de Goede 
21628ff2f7aSHans de Goede 	return lsb | (msb << 8);
21728ff2f7aSHans de Goede }
21828ff2f7aSHans de Goede EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
21928ff2f7aSHans de Goede 
sch56xx_read_virtual_reg12(u16 addr,u16 msb_reg,u16 lsn_reg,int high_nibble)22028ff2f7aSHans de Goede int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
22128ff2f7aSHans de Goede 			       int high_nibble)
22228ff2f7aSHans de Goede {
22328ff2f7aSHans de Goede 	int msb, lsn;
22428ff2f7aSHans de Goede 
22528ff2f7aSHans de Goede 	/* Read MSB first, this will cause the matching LSN to be latched */
22628ff2f7aSHans de Goede 	msb = sch56xx_read_virtual_reg(addr, msb_reg);
22728ff2f7aSHans de Goede 	if (msb < 0)
22828ff2f7aSHans de Goede 		return msb;
22928ff2f7aSHans de Goede 
23028ff2f7aSHans de Goede 	lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
23128ff2f7aSHans de Goede 	if (lsn < 0)
23228ff2f7aSHans de Goede 		return lsn;
23328ff2f7aSHans de Goede 
23428ff2f7aSHans de Goede 	if (high_nibble)
23528ff2f7aSHans de Goede 		return (msb << 4) | (lsn >> 4);
23628ff2f7aSHans de Goede 	else
23728ff2f7aSHans de Goede 		return (msb << 4) | (lsn & 0x0f);
23828ff2f7aSHans de Goede }
23928ff2f7aSHans de Goede EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
24028ff2f7aSHans de Goede 
241312869ecSHans de Goede /*
242312869ecSHans de Goede  * Watchdog routines
243312869ecSHans de Goede  */
244312869ecSHans de Goede 
watchdog_set_timeout(struct watchdog_device * wddev,unsigned int timeout)245fb551405SHans de Goede static int watchdog_set_timeout(struct watchdog_device *wddev,
246fb551405SHans de Goede 				unsigned int timeout)
247312869ecSHans de Goede {
248fb551405SHans de Goede 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
249fb551405SHans de Goede 	unsigned int resolution;
250312869ecSHans de Goede 	u8 control;
251fb551405SHans de Goede 	int ret;
252312869ecSHans de Goede 
253312869ecSHans de Goede 	/* 1 second or 60 second resolution? */
254312869ecSHans de Goede 	if (timeout <= 255)
255312869ecSHans de Goede 		resolution = 1;
256312869ecSHans de Goede 	else
257312869ecSHans de Goede 		resolution = 60;
258312869ecSHans de Goede 
259312869ecSHans de Goede 	if (timeout < resolution || timeout > (resolution * 255))
260312869ecSHans de Goede 		return -EINVAL;
261312869ecSHans de Goede 
262312869ecSHans de Goede 	if (resolution == 1)
263312869ecSHans de Goede 		control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC;
264312869ecSHans de Goede 	else
265312869ecSHans de Goede 		control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC;
266312869ecSHans de Goede 
267312869ecSHans de Goede 	if (data->watchdog_control != control) {
268312869ecSHans de Goede 		mutex_lock(data->io_lock);
269312869ecSHans de Goede 		ret = sch56xx_write_virtual_reg(data->addr,
270312869ecSHans de Goede 						SCH56XX_REG_WDOG_CONTROL,
271312869ecSHans de Goede 						control);
272312869ecSHans de Goede 		mutex_unlock(data->io_lock);
273312869ecSHans de Goede 		if (ret)
274fb551405SHans de Goede 			return ret;
275312869ecSHans de Goede 
276312869ecSHans de Goede 		data->watchdog_control = control;
277312869ecSHans de Goede 	}
278312869ecSHans de Goede 
279312869ecSHans de Goede 	/*
280312869ecSHans de Goede 	 * Remember new timeout value, but do not write as that (re)starts
281312869ecSHans de Goede 	 * the watchdog countdown.
282312869ecSHans de Goede 	 */
283312869ecSHans de Goede 	data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
284fb551405SHans de Goede 	wddev->timeout = data->watchdog_preset * resolution;
285312869ecSHans de Goede 
286fb551405SHans de Goede 	return 0;
287312869ecSHans de Goede }
288312869ecSHans de Goede 
watchdog_start(struct watchdog_device * wddev)289fb551405SHans de Goede static int watchdog_start(struct watchdog_device *wddev)
290312869ecSHans de Goede {
291fb551405SHans de Goede 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
292312869ecSHans de Goede 	int ret;
293312869ecSHans de Goede 	u8 val;
294312869ecSHans de Goede 
295312869ecSHans de Goede 	/*
296312869ecSHans de Goede 	 * The sch56xx's watchdog cannot really be started / stopped
297312869ecSHans de Goede 	 * it is always running, but we can avoid the timer expiring
298312869ecSHans de Goede 	 * from causing a system reset by clearing the output enable bit.
299312869ecSHans de Goede 	 *
300312869ecSHans de Goede 	 * The sch56xx's watchdog will set the watchdog event bit, bit 0
301312869ecSHans de Goede 	 * of the second interrupt source register (at base-address + 9),
302312869ecSHans de Goede 	 * when the timer expires.
303312869ecSHans de Goede 	 *
304312869ecSHans de Goede 	 * This will only cause a system reset if the 0-1 flank happens when
305312869ecSHans de Goede 	 * output enable is true. Setting output enable after the flank will
306312869ecSHans de Goede 	 * not cause a reset, nor will the timer expiring a second time.
307312869ecSHans de Goede 	 * This means we must clear the watchdog event bit in case it is set.
308312869ecSHans de Goede 	 *
309312869ecSHans de Goede 	 * The timer may still be running (after a recent watchdog_stop) and
310312869ecSHans de Goede 	 * mere milliseconds away from expiring, so the timer must be reset
311312869ecSHans de Goede 	 * first!
312312869ecSHans de Goede 	 */
313312869ecSHans de Goede 
314312869ecSHans de Goede 	mutex_lock(data->io_lock);
315312869ecSHans de Goede 
316312869ecSHans de Goede 	/* 1. Reset the watchdog countdown counter */
317312869ecSHans de Goede 	ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
318312869ecSHans de Goede 					data->watchdog_preset);
319312869ecSHans de Goede 	if (ret)
320312869ecSHans de Goede 		goto leave;
321312869ecSHans de Goede 
32285a2e40cSHans de Goede 	/* 2. Enable output */
32385a2e40cSHans de Goede 	val = data->watchdog_output_enable | SCH56XX_WDOG_OUTPUT_ENABLE;
324312869ecSHans de Goede 	ret = sch56xx_write_virtual_reg(data->addr,
32585a2e40cSHans de Goede 					SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
326312869ecSHans de Goede 	if (ret)
327312869ecSHans de Goede 		goto leave;
328312869ecSHans de Goede 
329312869ecSHans de Goede 	data->watchdog_output_enable = val;
330312869ecSHans de Goede 
331312869ecSHans de Goede 	/* 3. Clear the watchdog event bit if set */
332312869ecSHans de Goede 	val = inb(data->addr + 9);
333312869ecSHans de Goede 	if (val & 0x01)
334312869ecSHans de Goede 		outb(0x01, data->addr + 9);
335312869ecSHans de Goede 
336312869ecSHans de Goede leave:
337312869ecSHans de Goede 	mutex_unlock(data->io_lock);
338312869ecSHans de Goede 	return ret;
339312869ecSHans de Goede }
340312869ecSHans de Goede 
watchdog_trigger(struct watchdog_device * wddev)341fb551405SHans de Goede static int watchdog_trigger(struct watchdog_device *wddev)
342312869ecSHans de Goede {
343fb551405SHans de Goede 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
344312869ecSHans de Goede 	int ret;
345312869ecSHans de Goede 
346312869ecSHans de Goede 	/* Reset the watchdog countdown counter */
347312869ecSHans de Goede 	mutex_lock(data->io_lock);
348312869ecSHans de Goede 	ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
349312869ecSHans de Goede 					data->watchdog_preset);
350312869ecSHans de Goede 	mutex_unlock(data->io_lock);
351fb551405SHans de Goede 
352312869ecSHans de Goede 	return ret;
353312869ecSHans de Goede }
354312869ecSHans de Goede 
watchdog_stop(struct watchdog_device * wddev)355fb551405SHans de Goede static int watchdog_stop(struct watchdog_device *wddev)
356312869ecSHans de Goede {
357fb551405SHans de Goede 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
358312869ecSHans de Goede 	int ret = 0;
359312869ecSHans de Goede 	u8 val;
360312869ecSHans de Goede 
36185a2e40cSHans de Goede 	val = data->watchdog_output_enable & ~SCH56XX_WDOG_OUTPUT_ENABLE;
362312869ecSHans de Goede 	mutex_lock(data->io_lock);
363312869ecSHans de Goede 	ret = sch56xx_write_virtual_reg(data->addr,
36485a2e40cSHans de Goede 					SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
365312869ecSHans de Goede 	mutex_unlock(data->io_lock);
366312869ecSHans de Goede 	if (ret)
367312869ecSHans de Goede 		return ret;
368312869ecSHans de Goede 
369312869ecSHans de Goede 	data->watchdog_output_enable = val;
37085a2e40cSHans de Goede 	return 0;
371312869ecSHans de Goede }
372312869ecSHans de Goede 
373fb551405SHans de Goede static const struct watchdog_ops watchdog_ops = {
374312869ecSHans de Goede 	.owner		= THIS_MODULE,
375fb551405SHans de Goede 	.start		= watchdog_start,
376fb551405SHans de Goede 	.stop		= watchdog_stop,
377fb551405SHans de Goede 	.ping		= watchdog_trigger,
378fb551405SHans de Goede 	.set_timeout	= watchdog_set_timeout,
379312869ecSHans de Goede };
380312869ecSHans de Goede 
sch56xx_watchdog_register(struct device * parent,u16 addr,u32 revision,struct mutex * io_lock,int check_enabled)3812be5f0d7SArmin Wolf void sch56xx_watchdog_register(struct device *parent, u16 addr, u32 revision,
3822be5f0d7SArmin Wolf 			       struct mutex *io_lock, int check_enabled)
383312869ecSHans de Goede {
384312869ecSHans de Goede 	struct sch56xx_watchdog_data *data;
385fb551405SHans de Goede 	int err, control, output_enable;
386312869ecSHans de Goede 
387312869ecSHans de Goede 	/* Cache the watchdog registers */
388312869ecSHans de Goede 	mutex_lock(io_lock);
389312869ecSHans de Goede 	control =
390312869ecSHans de Goede 		sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL);
391312869ecSHans de Goede 	output_enable =
392312869ecSHans de Goede 		sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE);
393312869ecSHans de Goede 	mutex_unlock(io_lock);
394312869ecSHans de Goede 
395312869ecSHans de Goede 	if (control < 0)
3962be5f0d7SArmin Wolf 		return;
397312869ecSHans de Goede 	if (output_enable < 0)
3982be5f0d7SArmin Wolf 		return;
399312869ecSHans de Goede 	if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
400312869ecSHans de Goede 		pr_warn("Watchdog not enabled by BIOS, not registering\n");
4012be5f0d7SArmin Wolf 		return;
402312869ecSHans de Goede 	}
403312869ecSHans de Goede 
4042be5f0d7SArmin Wolf 	data = devm_kzalloc(parent, sizeof(struct sch56xx_watchdog_data), GFP_KERNEL);
405312869ecSHans de Goede 	if (!data)
4062be5f0d7SArmin Wolf 		return;
407312869ecSHans de Goede 
408312869ecSHans de Goede 	data->addr = addr;
409312869ecSHans de Goede 	data->io_lock = io_lock;
410fb551405SHans de Goede 
4116df5cba5SArmin Wolf 	strscpy(data->wdinfo.identity, "sch56xx watchdog", sizeof(data->wdinfo.identity));
412fb551405SHans de Goede 	data->wdinfo.firmware_version = revision;
413fb551405SHans de Goede 	data->wdinfo.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT;
414fb551405SHans de Goede 	if (!nowayout)
415fb551405SHans de Goede 		data->wdinfo.options |= WDIOF_MAGICCLOSE;
416fb551405SHans de Goede 
417fb551405SHans de Goede 	data->wddev.info = &data->wdinfo;
418fb551405SHans de Goede 	data->wddev.ops = &watchdog_ops;
419fb551405SHans de Goede 	data->wddev.parent = parent;
420fb551405SHans de Goede 	data->wddev.timeout = 60;
421fb551405SHans de Goede 	data->wddev.min_timeout = 1;
422fb551405SHans de Goede 	data->wddev.max_timeout = 255 * 60;
423989c9c67SArmin Wolf 	watchdog_set_nowayout(&data->wddev, nowayout);
424fb551405SHans de Goede 	if (output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)
425647d6f09SArmin Wolf 		set_bit(WDOG_HW_RUNNING, &data->wddev.status);
426fb551405SHans de Goede 
427fb551405SHans de Goede 	/* Since the watchdog uses a downcounter there is no register to read
428fb551405SHans de Goede 	   the BIOS set timeout from (if any was set at all) ->
429fb551405SHans de Goede 	   Choose a preset which will give us a 1 minute timeout */
430fb551405SHans de Goede 	if (control & SCH56XX_WDOG_TIME_BASE_SEC)
431fb551405SHans de Goede 		data->watchdog_preset = 60; /* seconds */
432fb551405SHans de Goede 	else
433fb551405SHans de Goede 		data->watchdog_preset = 1; /* minute */
434fb551405SHans de Goede 
435312869ecSHans de Goede 	data->watchdog_control = control;
436312869ecSHans de Goede 	data->watchdog_output_enable = output_enable;
437312869ecSHans de Goede 
438fb551405SHans de Goede 	watchdog_set_drvdata(&data->wddev, data);
4392be5f0d7SArmin Wolf 	err = devm_watchdog_register_device(parent, &data->wddev);
440312869ecSHans de Goede 	if (err) {
441312869ecSHans de Goede 		pr_err("Registering watchdog chardev: %d\n", err);
4422be5f0d7SArmin Wolf 		devm_kfree(parent, data);
443312869ecSHans de Goede 	}
444312869ecSHans de Goede }
445312869ecSHans de Goede EXPORT_SYMBOL(sch56xx_watchdog_register);
446312869ecSHans de Goede 
447312869ecSHans de Goede /*
448312869ecSHans de Goede  * platform dev find, add and remove functions
449312869ecSHans de Goede  */
450312869ecSHans de Goede 
sch56xx_find(int sioaddr,const char ** name)451313829eeSGuenter Roeck static int __init sch56xx_find(int sioaddr, const char **name)
45228ff2f7aSHans de Goede {
45328ff2f7aSHans de Goede 	u8 devid;
454313829eeSGuenter Roeck 	unsigned short address;
45528ff2f7aSHans de Goede 	int err;
45628ff2f7aSHans de Goede 
45728ff2f7aSHans de Goede 	err = superio_enter(sioaddr);
45828ff2f7aSHans de Goede 	if (err)
45928ff2f7aSHans de Goede 		return err;
46028ff2f7aSHans de Goede 
46128ff2f7aSHans de Goede 	devid = superio_inb(sioaddr, SIO_REG_DEVID);
46228ff2f7aSHans de Goede 	switch (devid) {
46328ff2f7aSHans de Goede 	case SIO_SCH5627_ID:
46428ff2f7aSHans de Goede 		*name = "sch5627";
46528ff2f7aSHans de Goede 		break;
4660772a640SHans de Goede 	case SIO_SCH5636_ID:
4670772a640SHans de Goede 		*name = "sch5636";
4680772a640SHans de Goede 		break;
46928ff2f7aSHans de Goede 	default:
47028ff2f7aSHans de Goede 		pr_debug("Unsupported device id: 0x%02x\n",
47128ff2f7aSHans de Goede 			 (unsigned int)devid);
47228ff2f7aSHans de Goede 		err = -ENODEV;
47328ff2f7aSHans de Goede 		goto exit;
47428ff2f7aSHans de Goede 	}
47528ff2f7aSHans de Goede 
47628ff2f7aSHans de Goede 	superio_select(sioaddr, SIO_SCH56XX_LD_EM);
47728ff2f7aSHans de Goede 
47828ff2f7aSHans de Goede 	if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
47928ff2f7aSHans de Goede 		pr_warn("Device not activated\n");
48028ff2f7aSHans de Goede 		err = -ENODEV;
48128ff2f7aSHans de Goede 		goto exit;
48228ff2f7aSHans de Goede 	}
48328ff2f7aSHans de Goede 
48428ff2f7aSHans de Goede 	/*
48528ff2f7aSHans de Goede 	 * Warning the order of the low / high byte is the other way around
48628ff2f7aSHans de Goede 	 * as on most other superio devices!!
48728ff2f7aSHans de Goede 	 */
488313829eeSGuenter Roeck 	address = superio_inb(sioaddr, SIO_REG_ADDR) |
48928ff2f7aSHans de Goede 		   superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
490313829eeSGuenter Roeck 	if (address == 0) {
49128ff2f7aSHans de Goede 		pr_warn("Base address not set\n");
49228ff2f7aSHans de Goede 		err = -ENODEV;
49328ff2f7aSHans de Goede 		goto exit;
49428ff2f7aSHans de Goede 	}
495313829eeSGuenter Roeck 	err = address;
49628ff2f7aSHans de Goede 
49728ff2f7aSHans de Goede exit:
49828ff2f7aSHans de Goede 	superio_exit(sioaddr);
49928ff2f7aSHans de Goede 	return err;
50028ff2f7aSHans de Goede }
50128ff2f7aSHans de Goede 
sch56xx_device_add(int address,const char * name)502313829eeSGuenter Roeck static int __init sch56xx_device_add(int address, const char *name)
50328ff2f7aSHans de Goede {
50428ff2f7aSHans de Goede 	struct resource res = {
50528ff2f7aSHans de Goede 		.start	= address,
50628ff2f7aSHans de Goede 		.end	= address + REGION_LENGTH - 1,
5075c1c78e0SArmin Wolf 		.name	= name,
50828ff2f7aSHans de Goede 		.flags	= IORESOURCE_IO,
50928ff2f7aSHans de Goede 	};
51028ff2f7aSHans de Goede 	int err;
51128ff2f7aSHans de Goede 
51228ff2f7aSHans de Goede 	err = acpi_check_resource_conflict(&res);
51328ff2f7aSHans de Goede 	if (err)
51428ff2f7aSHans de Goede 		return err;
5155c1c78e0SArmin Wolf 
5165c1c78e0SArmin Wolf 	sch56xx_pdev = platform_device_register_simple(name, -1, &res, 1);
5175c1c78e0SArmin Wolf 
5185c1c78e0SArmin Wolf 	return PTR_ERR_OR_ZERO(sch56xx_pdev);
51928ff2f7aSHans de Goede }
52028ff2f7aSHans de Goede 
sch56xx_init(void)52128ff2f7aSHans de Goede static int __init sch56xx_init(void)
52228ff2f7aSHans de Goede {
523393935baSArmin Wolf 	int address;
524*a32a93ecSGuenter Roeck 	const char *name = NULL;
52528ff2f7aSHans de Goede 
526313829eeSGuenter Roeck 	address = sch56xx_find(0x4e, &name);
527313829eeSGuenter Roeck 	if (address < 0)
528313829eeSGuenter Roeck 		address = sch56xx_find(0x2e, &name);
529313829eeSGuenter Roeck 	if (address < 0)
530313829eeSGuenter Roeck 		return address;
53128ff2f7aSHans de Goede 
53228ff2f7aSHans de Goede 	return sch56xx_device_add(address, name);
53328ff2f7aSHans de Goede }
53428ff2f7aSHans de Goede 
sch56xx_exit(void)53528ff2f7aSHans de Goede static void __exit sch56xx_exit(void)
53628ff2f7aSHans de Goede {
53728ff2f7aSHans de Goede 	platform_device_unregister(sch56xx_pdev);
53828ff2f7aSHans de Goede }
53928ff2f7aSHans de Goede 
54028ff2f7aSHans de Goede MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
54128ff2f7aSHans de Goede MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
54228ff2f7aSHans de Goede MODULE_LICENSE("GPL");
54328ff2f7aSHans de Goede 
54428ff2f7aSHans de Goede module_init(sch56xx_init);
54528ff2f7aSHans de Goede module_exit(sch56xx_exit);
546