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