1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Watchdog timer driver for the WinSystems EBC-C384
4  * Copyright (C) 2016 William Breathitt Gray
5  */
6 #include <linux/device.h>
7 #include <linux/dmi.h>
8 #include <linux/errno.h>
9 #include <linux/io.h>
10 #include <linux/ioport.h>
11 #include <linux/isa.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/watchdog.h>
17 
18 #define MODULE_NAME		"ebc-c384_wdt"
19 #define WATCHDOG_TIMEOUT	60
20 /*
21  * The timeout value in minutes must fit in a single byte when sent to the
22  * watchdog timer; the maximum timeout possible is 15300 (255 * 60) seconds.
23  */
24 #define WATCHDOG_MAX_TIMEOUT	15300
25 #define BASE_ADDR		0x564
26 #define ADDR_EXTENT		5
27 #define CFG_ADDR		(BASE_ADDR + 1)
28 #define PET_ADDR		(BASE_ADDR + 2)
29 
30 static bool nowayout = WATCHDOG_NOWAYOUT;
31 module_param(nowayout, bool, 0);
32 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
33 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
34 
35 static unsigned timeout;
36 module_param(timeout, uint, 0);
37 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
38 	__MODULE_STRING(WATCHDOG_TIMEOUT) ")");
39 
ebc_c384_wdt_start(struct watchdog_device * wdev)40 static int ebc_c384_wdt_start(struct watchdog_device *wdev)
41 {
42 	unsigned t = wdev->timeout;
43 
44 	/* resolution is in minutes for timeouts greater than 255 seconds */
45 	if (t > 255)
46 		t = DIV_ROUND_UP(t, 60);
47 
48 	outb(t, PET_ADDR);
49 
50 	return 0;
51 }
52 
ebc_c384_wdt_stop(struct watchdog_device * wdev)53 static int ebc_c384_wdt_stop(struct watchdog_device *wdev)
54 {
55 	outb(0x00, PET_ADDR);
56 
57 	return 0;
58 }
59 
ebc_c384_wdt_set_timeout(struct watchdog_device * wdev,unsigned t)60 static int ebc_c384_wdt_set_timeout(struct watchdog_device *wdev, unsigned t)
61 {
62 	/* resolution is in minutes for timeouts greater than 255 seconds */
63 	if (t > 255) {
64 		/* round second resolution up to minute granularity */
65 		wdev->timeout = roundup(t, 60);
66 
67 		/* set watchdog timer for minutes */
68 		outb(0x00, CFG_ADDR);
69 	} else {
70 		wdev->timeout = t;
71 
72 		/* set watchdog timer for seconds */
73 		outb(0x80, CFG_ADDR);
74 	}
75 
76 	return 0;
77 }
78 
79 static const struct watchdog_ops ebc_c384_wdt_ops = {
80 	.start = ebc_c384_wdt_start,
81 	.stop = ebc_c384_wdt_stop,
82 	.set_timeout = ebc_c384_wdt_set_timeout
83 };
84 
85 static const struct watchdog_info ebc_c384_wdt_info = {
86 	.options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
87 	.identity = MODULE_NAME
88 };
89 
ebc_c384_wdt_probe(struct device * dev,unsigned int id)90 static int ebc_c384_wdt_probe(struct device *dev, unsigned int id)
91 {
92 	struct watchdog_device *wdd;
93 
94 	if (!devm_request_region(dev, BASE_ADDR, ADDR_EXTENT, dev_name(dev))) {
95 		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
96 			BASE_ADDR, BASE_ADDR + ADDR_EXTENT);
97 		return -EBUSY;
98 	}
99 
100 	wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
101 	if (!wdd)
102 		return -ENOMEM;
103 
104 	wdd->info = &ebc_c384_wdt_info;
105 	wdd->ops = &ebc_c384_wdt_ops;
106 	wdd->timeout = WATCHDOG_TIMEOUT;
107 	wdd->min_timeout = 1;
108 	wdd->max_timeout = WATCHDOG_MAX_TIMEOUT;
109 
110 	watchdog_set_nowayout(wdd, nowayout);
111 	watchdog_init_timeout(wdd, timeout, dev);
112 
113 	return devm_watchdog_register_device(dev, wdd);
114 }
115 
116 static struct isa_driver ebc_c384_wdt_driver = {
117 	.probe = ebc_c384_wdt_probe,
118 	.driver = {
119 		.name = MODULE_NAME
120 	},
121 };
122 
ebc_c384_wdt_init(void)123 static int __init ebc_c384_wdt_init(void)
124 {
125 	if (!dmi_match(DMI_BOARD_NAME, "EBC-C384 SBC"))
126 		return -ENODEV;
127 
128 	return isa_register_driver(&ebc_c384_wdt_driver, 1);
129 }
130 
ebc_c384_wdt_exit(void)131 static void __exit ebc_c384_wdt_exit(void)
132 {
133 	isa_unregister_driver(&ebc_c384_wdt_driver);
134 }
135 
136 module_init(ebc_c384_wdt_init);
137 module_exit(ebc_c384_wdt_exit);
138 
139 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
140 MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver");
141 MODULE_LICENSE("GPL v2");
142 MODULE_ALIAS("isa:" MODULE_NAME);
143