blob: 4b849b8e37c2621ab6be50b1b4a0b2f6c81f9297 [file] [log] [blame]
William Breathitt Grayc36a4832016-02-28 23:54:46 -05001/*
2 * Watchdog timer driver for the WinSystems EBC-C384
3 * Copyright (C) 2016 William Breathitt Gray
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 */
14#include <linux/device.h>
15#include <linux/dmi.h>
16#include <linux/errno.h>
17#include <linux/io.h>
18#include <linux/ioport.h>
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -040019#include <linux/isa.h>
William Breathitt Grayc36a4832016-02-28 23:54:46 -050020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/moduleparam.h>
William Breathitt Grayc36a4832016-02-28 23:54:46 -050023#include <linux/types.h>
24#include <linux/watchdog.h>
25
26#define MODULE_NAME "ebc-c384_wdt"
27#define WATCHDOG_TIMEOUT 60
28/*
29 * The timeout value in minutes must fit in a single byte when sent to the
30 * watchdog timer; the maximum timeout possible is 15300 (255 * 60) seconds.
31 */
32#define WATCHDOG_MAX_TIMEOUT 15300
33#define BASE_ADDR 0x564
34#define ADDR_EXTENT 5
35#define CFG_ADDR (BASE_ADDR + 1)
36#define PET_ADDR (BASE_ADDR + 2)
37
38static bool nowayout = WATCHDOG_NOWAYOUT;
39module_param(nowayout, bool, 0);
40MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
41 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42
43static unsigned timeout;
44module_param(timeout, uint, 0);
45MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
46 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
47
48static int ebc_c384_wdt_start(struct watchdog_device *wdev)
49{
50 unsigned t = wdev->timeout;
51
52 /* resolution is in minutes for timeouts greater than 255 seconds */
53 if (t > 255)
54 t = DIV_ROUND_UP(t, 60);
55
56 outb(t, PET_ADDR);
57
58 return 0;
59}
60
61static int ebc_c384_wdt_stop(struct watchdog_device *wdev)
62{
63 outb(0x00, PET_ADDR);
64
65 return 0;
66}
67
68static int ebc_c384_wdt_set_timeout(struct watchdog_device *wdev, unsigned t)
69{
70 /* resolution is in minutes for timeouts greater than 255 seconds */
71 if (t > 255) {
72 /* round second resolution up to minute granularity */
73 wdev->timeout = roundup(t, 60);
74
75 /* set watchdog timer for minutes */
76 outb(0x00, CFG_ADDR);
77 } else {
78 wdev->timeout = t;
79
80 /* set watchdog timer for seconds */
81 outb(0x80, CFG_ADDR);
82 }
83
84 return 0;
85}
86
87static const struct watchdog_ops ebc_c384_wdt_ops = {
88 .start = ebc_c384_wdt_start,
89 .stop = ebc_c384_wdt_stop,
90 .set_timeout = ebc_c384_wdt_set_timeout
91};
92
93static const struct watchdog_info ebc_c384_wdt_info = {
94 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
95 .identity = MODULE_NAME
96};
97
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -040098static int ebc_c384_wdt_probe(struct device *dev, unsigned int id)
William Breathitt Grayc36a4832016-02-28 23:54:46 -050099{
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500100 struct watchdog_device *wdd;
101
102 if (!devm_request_region(dev, BASE_ADDR, ADDR_EXTENT, dev_name(dev))) {
103 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
104 BASE_ADDR, BASE_ADDR + ADDR_EXTENT);
105 return -EBUSY;
106 }
107
108 wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
109 if (!wdd)
110 return -ENOMEM;
111
112 wdd->info = &ebc_c384_wdt_info;
113 wdd->ops = &ebc_c384_wdt_ops;
114 wdd->timeout = WATCHDOG_TIMEOUT;
115 wdd->min_timeout = 1;
116 wdd->max_timeout = WATCHDOG_MAX_TIMEOUT;
117
118 watchdog_set_nowayout(wdd, nowayout);
119
120 if (watchdog_init_timeout(wdd, timeout, dev))
121 dev_warn(dev, "Invalid timeout (%u seconds), using default (%u seconds)\n",
122 timeout, WATCHDOG_TIMEOUT);
123
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -0400124 dev_set_drvdata(dev, wdd);
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500125
126 return watchdog_register_device(wdd);
127}
128
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -0400129static int ebc_c384_wdt_remove(struct device *dev, unsigned int id)
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500130{
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -0400131 struct watchdog_device *wdd = dev_get_drvdata(dev);
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500132
133 watchdog_unregister_device(wdd);
134
135 return 0;
136}
137
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -0400138static struct isa_driver ebc_c384_wdt_driver = {
139 .probe = ebc_c384_wdt_probe,
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500140 .driver = {
141 .name = MODULE_NAME
142 },
143 .remove = ebc_c384_wdt_remove
144};
145
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500146static int __init ebc_c384_wdt_init(void)
147{
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500148 if (!dmi_match(DMI_BOARD_NAME, "EBC-C384 SBC"))
149 return -ENODEV;
150
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -0400151 return isa_register_driver(&ebc_c384_wdt_driver, 1);
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500152}
153
154static void __exit ebc_c384_wdt_exit(void)
155{
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -0400156 isa_unregister_driver(&ebc_c384_wdt_driver);
William Breathitt Grayc36a4832016-02-28 23:54:46 -0500157}
158
159module_init(ebc_c384_wdt_init);
160module_exit(ebc_c384_wdt_exit);
161
162MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
163MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver");
164MODULE_LICENSE("GPL v2");
William Breathitt Gray4ef1bec2016-05-01 18:44:26 -0400165MODULE_ALIAS("isa:" MODULE_NAME);