xref: /linux/drivers/hwmon/sch56xx-common.c (revision a54fe61639d9f3b6765fee32edda7cfceb6d705a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3  *   Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>           *
4  *                                                                         *
5  ***************************************************************************/
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
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>
14 #include <linux/io.h>
15 #include <linux/acpi.h>
16 #include <linux/delay.h>
17 #include <linux/fs.h>
18 #include <linux/watchdog.h>
19 #include <linux/uaccess.h>
20 #include <linux/slab.h>
21 #include "sch56xx-common.h"
22 
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) ")");
28 
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 */
32 
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) */
37 
38 #define SIO_SCH5627_ID		0xC6	/* Chipset ID */
39 #define SIO_SCH5636_ID		0xC7	/* Chipset ID */
40 
41 #define REGION_LENGTH		10
42 
43 #define SCH56XX_CMD_READ	0x02
44 #define SCH56XX_CMD_WRITE	0x03
45 
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
52 
53 struct sch56xx_watchdog_data {
54 	u16 addr;
55 	struct mutex *io_lock;
56 	struct watchdog_info wdinfo;
57 	struct watchdog_device wddev;
58 	u8 watchdog_preset;
59 	u8 watchdog_control;
60 	u8 watchdog_output_enable;
61 };
62 
63 struct sch56xx_bus_context {
64 	struct mutex *lock;	/* Used to serialize access to the mailbox registers */
65 	u16 addr;
66 };
67 
68 static struct platform_device *sch56xx_pdev;
69 
70 /* Super I/O functions */
71 static inline int superio_inb(int base, int reg)
72 {
73 	outb(reg, base);
74 	return inb(base + 1);
75 }
76 
77 static inline int superio_enter(int base)
78 {
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);
82 		return -EBUSY;
83 	}
84 
85 	outb(SIO_UNLOCK_KEY, base);
86 
87 	return 0;
88 }
89 
90 static inline void superio_select(int base, int ld)
91 {
92 	outb(SIO_REG_LDSEL, base);
93 	outb(ld, base + 1);
94 }
95 
96 static inline void superio_exit(int base)
97 {
98 	outb(SIO_LOCK_KEY, base);
99 	release_region(base, 2);
100 }
101 
102 static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
103 {
104 	u8 val;
105 	int i;
106 	/*
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.
112 	 */
113 	const int max_busy_polls = 64;
114 	const int max_lazy_polls = 32;
115 
116 	/* (Optional) Write-Clear the EC to Host Mailbox Register */
117 	val = inb(addr + 1);
118 	outb(val, addr + 1);
119 
120 	/* Set Mailbox Address Pointer to first location in Region 1 */
121 	outb(0x00, addr + 2);
122 	outb(0x80, addr + 3);
123 
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. */
128 
129 	/* Write Value field */
130 	if (cmd == SCH56XX_CMD_WRITE)
131 		outb(v, addr + 4);
132 
133 	/* Write Address field */
134 	outb(reg & 0xff, addr + 6);
135 	outb(reg >> 8, addr + 7);
136 
137 	/* Execute the Random Access Command */
138 	outb(0x01, addr); /* Write 01h to the Host-to-EC register */
139 
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 */
145 		val = inb(addr + 8);
146 		/* Write Clear the interrupt source bits */
147 		if (val)
148 			outb(val, addr + 8);
149 		/* Command Completed ? */
150 		if (val & 0x01)
151 			break;
152 	}
153 	if (i == max_busy_polls + max_lazy_polls) {
154 		pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
155 		       reg, 1);
156 		return -EIO;
157 	}
158 
159 	/*
160 	 * According to SMSC we may need to retry this, but sofar I've always
161 	 * seen this succeed in 1 try.
162 	 */
163 	for (i = 0; i < max_busy_polls; i++) {
164 		/* Read EC-to-Host Register */
165 		val = inb(addr + 1);
166 		/* Command Completed ? */
167 		if (val == 0x01)
168 			break;
169 
170 		if (i == 0)
171 			pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
172 				(unsigned int)val, reg);
173 	}
174 	if (i == max_busy_polls) {
175 		pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
176 		       reg, 2);
177 		return -EIO;
178 	}
179 
180 	/*
181 	 * According to the SMSC app note we should now do:
182 	 *
183 	 * Set Mailbox Address Pointer to first location in Region 1 *
184 	 * outb(0x00, addr + 2);
185 	 * outb(0x80, addr + 3);
186 	 *
187 	 * But if we do that things don't work, so let's not.
188 	 */
189 
190 	/* Read Value field */
191 	if (cmd == SCH56XX_CMD_READ)
192 		return inb(addr + 4);
193 
194 	return 0;
195 }
196 
197 int sch56xx_read_virtual_reg(u16 addr, u16 reg)
198 {
199 	return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
200 }
201 EXPORT_SYMBOL(sch56xx_read_virtual_reg);
202 
203 int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
204 {
205 	return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
206 }
207 EXPORT_SYMBOL(sch56xx_write_virtual_reg);
208 
209 int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
210 {
211 	int lsb, msb;
212 
213 	/* Read LSB first, this will cause the matching MSB to be latched */
214 	lsb = sch56xx_read_virtual_reg(addr, reg);
215 	if (lsb < 0)
216 		return lsb;
217 
218 	msb = sch56xx_read_virtual_reg(addr, reg + 1);
219 	if (msb < 0)
220 		return msb;
221 
222 	return lsb | (msb << 8);
223 }
224 EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
225 
226 int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
227 			       int high_nibble)
228 {
229 	int msb, lsn;
230 
231 	/* Read MSB first, this will cause the matching LSN to be latched */
232 	msb = sch56xx_read_virtual_reg(addr, msb_reg);
233 	if (msb < 0)
234 		return msb;
235 
236 	lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
237 	if (lsn < 0)
238 		return lsn;
239 
240 	if (high_nibble)
241 		return (msb << 4) | (lsn >> 4);
242 	else
243 		return (msb << 4) | (lsn & 0x0f);
244 }
245 EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
246 
247 /*
248  * Regmap support
249  */
250 
251 static int sch56xx_reg_write(void *context, unsigned int reg, unsigned int val)
252 {
253 	struct sch56xx_bus_context *bus = context;
254 	int ret;
255 
256 	mutex_lock(bus->lock);
257 	ret = sch56xx_write_virtual_reg(bus->addr, (u16)reg, (u8)val);
258 	mutex_unlock(bus->lock);
259 
260 	return ret;
261 }
262 
263 static int sch56xx_reg_read(void *context, unsigned int reg, unsigned int *val)
264 {
265 	struct sch56xx_bus_context *bus = context;
266 	int ret;
267 
268 	mutex_lock(bus->lock);
269 	ret = sch56xx_read_virtual_reg(bus->addr, (u16)reg);
270 	mutex_unlock(bus->lock);
271 
272 	if (ret < 0)
273 		return ret;
274 
275 	*val = ret;
276 
277 	return 0;
278 }
279 
280 static void sch56xx_free_context(void *context)
281 {
282 	kfree(context);
283 }
284 
285 static const struct regmap_bus sch56xx_bus = {
286 	.reg_write = sch56xx_reg_write,
287 	.reg_read = sch56xx_reg_read,
288 	.free_context = sch56xx_free_context,
289 	.reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
290 	.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
291 };
292 
293 struct regmap *devm_regmap_init_sch56xx(struct device *dev, struct mutex *lock, u16 addr,
294 					const struct regmap_config *config)
295 {
296 	struct sch56xx_bus_context *context;
297 	struct regmap *map;
298 
299 	if (config->reg_bits != 16 && config->val_bits != 8)
300 		return ERR_PTR(-EOPNOTSUPP);
301 
302 	context = kzalloc(sizeof(*context), GFP_KERNEL);
303 	if (!context)
304 		return ERR_PTR(-ENOMEM);
305 
306 	context->lock = lock;
307 	context->addr = addr;
308 
309 	map = devm_regmap_init(dev, &sch56xx_bus, context, config);
310 	if (IS_ERR(map))
311 		kfree(context);
312 
313 	return map;
314 }
315 EXPORT_SYMBOL(devm_regmap_init_sch56xx);
316 
317 /*
318  * Watchdog routines
319  */
320 
321 static int watchdog_set_timeout(struct watchdog_device *wddev,
322 				unsigned int timeout)
323 {
324 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
325 	unsigned int resolution;
326 	u8 control;
327 	int ret;
328 
329 	/* 1 second or 60 second resolution? */
330 	if (timeout <= 255)
331 		resolution = 1;
332 	else
333 		resolution = 60;
334 
335 	if (timeout < resolution || timeout > (resolution * 255))
336 		return -EINVAL;
337 
338 	if (resolution == 1)
339 		control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC;
340 	else
341 		control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC;
342 
343 	if (data->watchdog_control != control) {
344 		mutex_lock(data->io_lock);
345 		ret = sch56xx_write_virtual_reg(data->addr,
346 						SCH56XX_REG_WDOG_CONTROL,
347 						control);
348 		mutex_unlock(data->io_lock);
349 		if (ret)
350 			return ret;
351 
352 		data->watchdog_control = control;
353 	}
354 
355 	/*
356 	 * Remember new timeout value, but do not write as that (re)starts
357 	 * the watchdog countdown.
358 	 */
359 	data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
360 	wddev->timeout = data->watchdog_preset * resolution;
361 
362 	return 0;
363 }
364 
365 static int watchdog_start(struct watchdog_device *wddev)
366 {
367 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
368 	int ret;
369 	u8 val;
370 
371 	/*
372 	 * The sch56xx's watchdog cannot really be started / stopped
373 	 * it is always running, but we can avoid the timer expiring
374 	 * from causing a system reset by clearing the output enable bit.
375 	 *
376 	 * The sch56xx's watchdog will set the watchdog event bit, bit 0
377 	 * of the second interrupt source register (at base-address + 9),
378 	 * when the timer expires.
379 	 *
380 	 * This will only cause a system reset if the 0-1 flank happens when
381 	 * output enable is true. Setting output enable after the flank will
382 	 * not cause a reset, nor will the timer expiring a second time.
383 	 * This means we must clear the watchdog event bit in case it is set.
384 	 *
385 	 * The timer may still be running (after a recent watchdog_stop) and
386 	 * mere milliseconds away from expiring, so the timer must be reset
387 	 * first!
388 	 */
389 
390 	mutex_lock(data->io_lock);
391 
392 	/* 1. Reset the watchdog countdown counter */
393 	ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
394 					data->watchdog_preset);
395 	if (ret)
396 		goto leave;
397 
398 	/* 2. Enable output */
399 	val = data->watchdog_output_enable | SCH56XX_WDOG_OUTPUT_ENABLE;
400 	ret = sch56xx_write_virtual_reg(data->addr,
401 					SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
402 	if (ret)
403 		goto leave;
404 
405 	data->watchdog_output_enable = val;
406 
407 	/* 3. Clear the watchdog event bit if set */
408 	val = inb(data->addr + 9);
409 	if (val & 0x01)
410 		outb(0x01, data->addr + 9);
411 
412 leave:
413 	mutex_unlock(data->io_lock);
414 	return ret;
415 }
416 
417 static int watchdog_trigger(struct watchdog_device *wddev)
418 {
419 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
420 	int ret;
421 
422 	/* Reset the watchdog countdown counter */
423 	mutex_lock(data->io_lock);
424 	ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
425 					data->watchdog_preset);
426 	mutex_unlock(data->io_lock);
427 
428 	return ret;
429 }
430 
431 static int watchdog_stop(struct watchdog_device *wddev)
432 {
433 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
434 	int ret = 0;
435 	u8 val;
436 
437 	val = data->watchdog_output_enable & ~SCH56XX_WDOG_OUTPUT_ENABLE;
438 	mutex_lock(data->io_lock);
439 	ret = sch56xx_write_virtual_reg(data->addr,
440 					SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
441 	mutex_unlock(data->io_lock);
442 	if (ret)
443 		return ret;
444 
445 	data->watchdog_output_enable = val;
446 	return 0;
447 }
448 
449 static const struct watchdog_ops watchdog_ops = {
450 	.owner		= THIS_MODULE,
451 	.start		= watchdog_start,
452 	.stop		= watchdog_stop,
453 	.ping		= watchdog_trigger,
454 	.set_timeout	= watchdog_set_timeout,
455 };
456 
457 void sch56xx_watchdog_register(struct device *parent, u16 addr, u32 revision,
458 			       struct mutex *io_lock, int check_enabled)
459 {
460 	struct sch56xx_watchdog_data *data;
461 	int err, control, output_enable;
462 
463 	/* Cache the watchdog registers */
464 	mutex_lock(io_lock);
465 	control =
466 		sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL);
467 	output_enable =
468 		sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE);
469 	mutex_unlock(io_lock);
470 
471 	if (control < 0)
472 		return;
473 	if (output_enable < 0)
474 		return;
475 	if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
476 		pr_warn("Watchdog not enabled by BIOS, not registering\n");
477 		return;
478 	}
479 
480 	data = devm_kzalloc(parent, sizeof(struct sch56xx_watchdog_data), GFP_KERNEL);
481 	if (!data)
482 		return;
483 
484 	data->addr = addr;
485 	data->io_lock = io_lock;
486 
487 	strscpy(data->wdinfo.identity, "sch56xx watchdog", sizeof(data->wdinfo.identity));
488 	data->wdinfo.firmware_version = revision;
489 	data->wdinfo.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT;
490 	if (!nowayout)
491 		data->wdinfo.options |= WDIOF_MAGICCLOSE;
492 
493 	data->wddev.info = &data->wdinfo;
494 	data->wddev.ops = &watchdog_ops;
495 	data->wddev.parent = parent;
496 	data->wddev.timeout = 60;
497 	data->wddev.min_timeout = 1;
498 	data->wddev.max_timeout = 255 * 60;
499 	watchdog_set_nowayout(&data->wddev, nowayout);
500 	if (output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)
501 		set_bit(WDOG_HW_RUNNING, &data->wddev.status);
502 
503 	/* Since the watchdog uses a downcounter there is no register to read
504 	   the BIOS set timeout from (if any was set at all) ->
505 	   Choose a preset which will give us a 1 minute timeout */
506 	if (control & SCH56XX_WDOG_TIME_BASE_SEC)
507 		data->watchdog_preset = 60; /* seconds */
508 	else
509 		data->watchdog_preset = 1; /* minute */
510 
511 	data->watchdog_control = control;
512 	data->watchdog_output_enable = output_enable;
513 
514 	watchdog_set_drvdata(&data->wddev, data);
515 	err = devm_watchdog_register_device(parent, &data->wddev);
516 	if (err) {
517 		pr_err("Registering watchdog chardev: %d\n", err);
518 		devm_kfree(parent, data);
519 	}
520 }
521 EXPORT_SYMBOL(sch56xx_watchdog_register);
522 
523 /*
524  * platform dev find, add and remove functions
525  */
526 
527 static int __init sch56xx_find(int sioaddr, const char **name)
528 {
529 	u8 devid;
530 	unsigned short address;
531 	int err;
532 
533 	err = superio_enter(sioaddr);
534 	if (err)
535 		return err;
536 
537 	devid = superio_inb(sioaddr, SIO_REG_DEVID);
538 	switch (devid) {
539 	case SIO_SCH5627_ID:
540 		*name = "sch5627";
541 		break;
542 	case SIO_SCH5636_ID:
543 		*name = "sch5636";
544 		break;
545 	default:
546 		pr_debug("Unsupported device id: 0x%02x\n",
547 			 (unsigned int)devid);
548 		err = -ENODEV;
549 		goto exit;
550 	}
551 
552 	superio_select(sioaddr, SIO_SCH56XX_LD_EM);
553 
554 	if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
555 		pr_warn("Device not activated\n");
556 		err = -ENODEV;
557 		goto exit;
558 	}
559 
560 	/*
561 	 * Warning the order of the low / high byte is the other way around
562 	 * as on most other superio devices!!
563 	 */
564 	address = superio_inb(sioaddr, SIO_REG_ADDR) |
565 		   superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
566 	if (address == 0) {
567 		pr_warn("Base address not set\n");
568 		err = -ENODEV;
569 		goto exit;
570 	}
571 	err = address;
572 
573 exit:
574 	superio_exit(sioaddr);
575 	return err;
576 }
577 
578 static int __init sch56xx_device_add(int address, const char *name)
579 {
580 	struct resource res = {
581 		.start	= address,
582 		.end	= address + REGION_LENGTH - 1,
583 		.name	= name,
584 		.flags	= IORESOURCE_IO,
585 	};
586 	int err;
587 
588 	err = acpi_check_resource_conflict(&res);
589 	if (err)
590 		return err;
591 
592 	sch56xx_pdev = platform_device_register_simple(name, -1, &res, 1);
593 
594 	return PTR_ERR_OR_ZERO(sch56xx_pdev);
595 }
596 
597 static int __init sch56xx_init(void)
598 {
599 	int address;
600 	const char *name = NULL;
601 
602 	address = sch56xx_find(0x4e, &name);
603 	if (address < 0)
604 		address = sch56xx_find(0x2e, &name);
605 	if (address < 0)
606 		return address;
607 
608 	return sch56xx_device_add(address, name);
609 }
610 
611 static void __exit sch56xx_exit(void)
612 {
613 	platform_device_unregister(sch56xx_pdev);
614 }
615 
616 MODULE_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
617 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
618 MODULE_LICENSE("GPL");
619 
620 module_init(sch56xx_init);
621 module_exit(sch56xx_exit);
622