Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Watchdog driver for Technologic Systems TS-72xx based SBCs |
| 3 | * (TS-7200, TS-7250 and TS-7260). These boards have external |
| 4 | * glue logic CPLD chip, which includes programmable watchdog |
| 5 | * timer. |
| 6 | * |
| 7 | * Copyright (c) 2009 Mika Westerberg <mika.westerberg@iki.fi> |
| 8 | * |
| 9 | * This driver is based on ep93xx_wdt and wm831x_wdt drivers. |
| 10 | * |
| 11 | * This file is licensed under the terms of the GNU General Public |
| 12 | * License version 2. This program is licensed "as is" without any |
| 13 | * warranty of any kind, whether express or implied. |
| 14 | */ |
| 15 | |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 18 | #include <linux/watchdog.h> |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 19 | #include <linux/io.h> |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 20 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 21 | #define TS72XX_WDT_DEFAULT_TIMEOUT 30 |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 22 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 23 | static int timeout; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 24 | module_param(timeout, int, 0); |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 25 | MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds."); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 26 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 27 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 28 | module_param(nowayout, bool, 0); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 29 | MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close"); |
| 30 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 31 | /* priv->control_reg */ |
| 32 | #define TS72XX_WDT_CTRL_DISABLE 0x00 |
| 33 | #define TS72XX_WDT_CTRL_250MS 0x01 |
| 34 | #define TS72XX_WDT_CTRL_500MS 0x02 |
| 35 | #define TS72XX_WDT_CTRL_1SEC 0x03 |
| 36 | #define TS72XX_WDT_CTRL_RESERVED 0x04 |
| 37 | #define TS72XX_WDT_CTRL_2SEC 0x05 |
| 38 | #define TS72XX_WDT_CTRL_4SEC 0x06 |
| 39 | #define TS72XX_WDT_CTRL_8SEC 0x07 |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 40 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 41 | /* priv->feed_reg */ |
| 42 | #define TS72XX_WDT_FEED_VAL 0x05 |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 43 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 44 | struct ts72xx_wdt_priv { |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 45 | void __iomem *control_reg; |
| 46 | void __iomem *feed_reg; |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 47 | struct watchdog_device wdd; |
| 48 | unsigned char regval; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 51 | static int ts72xx_wdt_start(struct watchdog_device *wdd) |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 52 | { |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 53 | struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 54 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 55 | writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg); |
| 56 | writeb(priv->regval, priv->control_reg); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 57 | |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 61 | static int ts72xx_wdt_stop(struct watchdog_device *wdd) |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 62 | { |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 63 | struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 64 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 65 | writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg); |
| 66 | writeb(TS72XX_WDT_CTRL_DISABLE, priv->control_reg); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 67 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 68 | return 0; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 69 | } |
| 70 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 71 | static int ts72xx_wdt_ping(struct watchdog_device *wdd) |
| 72 | { |
| 73 | struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd); |
| 74 | |
| 75 | writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int ts72xx_wdt_settimeout(struct watchdog_device *wdd, unsigned int to) |
| 81 | { |
| 82 | struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd); |
| 83 | |
| 84 | if (to == 1) { |
| 85 | priv->regval = TS72XX_WDT_CTRL_1SEC; |
| 86 | } else if (to == 2) { |
| 87 | priv->regval = TS72XX_WDT_CTRL_2SEC; |
| 88 | } else if (to <= 4) { |
| 89 | priv->regval = TS72XX_WDT_CTRL_4SEC; |
| 90 | to = 4; |
| 91 | } else { |
| 92 | priv->regval = TS72XX_WDT_CTRL_8SEC; |
| 93 | if (to <= 8) |
| 94 | to = 8; |
| 95 | } |
| 96 | |
| 97 | wdd->timeout = to; |
| 98 | |
| 99 | if (watchdog_active(wdd)) { |
| 100 | ts72xx_wdt_stop(wdd); |
| 101 | ts72xx_wdt_start(wdd); |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static const struct watchdog_info ts72xx_wdt_ident = { |
| 108 | .options = WDIOF_KEEPALIVEPING | |
| 109 | WDIOF_SETTIMEOUT | |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 110 | WDIOF_MAGICCLOSE, |
| 111 | .firmware_version = 1, |
| 112 | .identity = "TS-72XX WDT", |
| 113 | }; |
| 114 | |
Gustavo A. R. Silva | 5870ac7 | 2017-07-07 19:18:40 -0500 | [diff] [blame] | 115 | static const struct watchdog_ops ts72xx_wdt_ops = { |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 116 | .owner = THIS_MODULE, |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 117 | .start = ts72xx_wdt_start, |
| 118 | .stop = ts72xx_wdt_stop, |
| 119 | .ping = ts72xx_wdt_ping, |
| 120 | .set_timeout = ts72xx_wdt_settimeout, |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 123 | static int ts72xx_wdt_probe(struct platform_device *pdev) |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 124 | { |
Guenter Roeck | 16c4614 | 2019-04-10 09:27:45 -0700 | [diff] [blame] | 125 | struct device *dev = &pdev->dev; |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 126 | struct ts72xx_wdt_priv *priv; |
| 127 | struct watchdog_device *wdd; |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 128 | int ret; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 129 | |
Guenter Roeck | 16c4614 | 2019-04-10 09:27:45 -0700 | [diff] [blame] | 130 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 131 | if (!priv) |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 132 | return -ENOMEM; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 133 | |
Guenter Roeck | 0f0a6a2 | 2019-04-02 12:01:53 -0700 | [diff] [blame] | 134 | priv->control_reg = devm_platform_ioremap_resource(pdev, 0); |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 135 | if (IS_ERR(priv->control_reg)) |
| 136 | return PTR_ERR(priv->control_reg); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 137 | |
Guenter Roeck | 0f0a6a2 | 2019-04-02 12:01:53 -0700 | [diff] [blame] | 138 | priv->feed_reg = devm_platform_ioremap_resource(pdev, 1); |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 139 | if (IS_ERR(priv->feed_reg)) |
| 140 | return PTR_ERR(priv->feed_reg); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 141 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 142 | wdd = &priv->wdd; |
| 143 | wdd->info = &ts72xx_wdt_ident; |
| 144 | wdd->ops = &ts72xx_wdt_ops; |
| 145 | wdd->min_timeout = 1; |
| 146 | wdd->max_hw_heartbeat_ms = 8000; |
Guenter Roeck | 16c4614 | 2019-04-10 09:27:45 -0700 | [diff] [blame] | 147 | wdd->parent = dev; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 148 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 149 | watchdog_set_nowayout(wdd, nowayout); |
Mika Westerberg | 0e901be | 2010-08-29 13:53:14 +0300 | [diff] [blame] | 150 | |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 151 | wdd->timeout = TS72XX_WDT_DEFAULT_TIMEOUT; |
Guenter Roeck | 16c4614 | 2019-04-10 09:27:45 -0700 | [diff] [blame] | 152 | watchdog_init_timeout(wdd, timeout, dev); |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 153 | |
| 154 | watchdog_set_drvdata(wdd, priv); |
| 155 | |
Guenter Roeck | 16c4614 | 2019-04-10 09:27:45 -0700 | [diff] [blame] | 156 | ret = devm_watchdog_register_device(dev, wdd); |
H Hartley Sweeten | 8f8dc7b | 2017-01-31 09:33:30 -0700 | [diff] [blame] | 157 | if (ret) |
| 158 | return ret; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 159 | |
Guenter Roeck | 16c4614 | 2019-04-10 09:27:45 -0700 | [diff] [blame] | 160 | dev_info(dev, "TS-72xx Watchdog driver\n"); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 161 | |
| 162 | return 0; |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 165 | static struct platform_driver ts72xx_wdt_driver = { |
| 166 | .probe = ts72xx_wdt_probe, |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 167 | .driver = { |
| 168 | .name = "ts72xx-wdt", |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 169 | }, |
| 170 | }; |
| 171 | |
Axel Lin | b8ec611 | 2011-11-29 13:56:27 +0800 | [diff] [blame] | 172 | module_platform_driver(ts72xx_wdt_driver); |
Mika Westerberg | c90bf2a | 2009-11-29 16:58:38 +0200 | [diff] [blame] | 173 | |
| 174 | MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>"); |
| 175 | MODULE_DESCRIPTION("TS-72xx SBC Watchdog"); |
| 176 | MODULE_LICENSE("GPL"); |
| 177 | MODULE_ALIAS("platform:ts72xx-wdt"); |