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