Linux Audio

Check our new training course

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