Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com> |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/clk.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/platform_device.h> |
| 9 | #include <linux/watchdog.h> |
| 10 | #include <loongson1.h> |
| 11 | |
| 12 | #define DEFAULT_HEARTBEAT 30 |
| 13 | |
| 14 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 15 | module_param(nowayout, bool, 0444); |
| 16 | |
| 17 | static unsigned int heartbeat; |
| 18 | module_param(heartbeat, uint, 0444); |
| 19 | |
| 20 | struct ls1x_wdt_drvdata { |
| 21 | void __iomem *base; |
| 22 | struct clk *clk; |
| 23 | unsigned long clk_rate; |
| 24 | struct watchdog_device wdt; |
| 25 | }; |
| 26 | |
| 27 | static int ls1x_wdt_ping(struct watchdog_device *wdt_dev) |
| 28 | { |
| 29 | struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
| 30 | |
| 31 | writel(0x1, drvdata->base + WDT_SET); |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev, |
| 37 | unsigned int timeout) |
| 38 | { |
| 39 | struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
| 40 | unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000; |
| 41 | unsigned int counts; |
| 42 | |
| 43 | wdt_dev->timeout = timeout; |
| 44 | |
| 45 | counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat); |
| 46 | writel(counts, drvdata->base + WDT_TIMER); |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int ls1x_wdt_start(struct watchdog_device *wdt_dev) |
| 52 | { |
| 53 | struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
| 54 | |
| 55 | writel(0x1, drvdata->base + WDT_EN); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int ls1x_wdt_stop(struct watchdog_device *wdt_dev) |
| 61 | { |
| 62 | struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev); |
| 63 | |
| 64 | writel(0x0, drvdata->base + WDT_EN); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static const struct watchdog_info ls1x_wdt_info = { |
| 70 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
| 71 | .identity = "Loongson1 Watchdog", |
| 72 | }; |
| 73 | |
| 74 | static const struct watchdog_ops ls1x_wdt_ops = { |
| 75 | .owner = THIS_MODULE, |
| 76 | .start = ls1x_wdt_start, |
| 77 | .stop = ls1x_wdt_stop, |
| 78 | .ping = ls1x_wdt_ping, |
| 79 | .set_timeout = ls1x_wdt_set_timeout, |
| 80 | }; |
| 81 | |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 82 | static void ls1x_clk_disable_unprepare(void *data) |
| 83 | { |
| 84 | clk_disable_unprepare(data); |
| 85 | } |
| 86 | |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 87 | static int ls1x_wdt_probe(struct platform_device *pdev) |
| 88 | { |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 89 | struct device *dev = &pdev->dev; |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 90 | struct ls1x_wdt_drvdata *drvdata; |
| 91 | struct watchdog_device *ls1x_wdt; |
| 92 | unsigned long clk_rate; |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 93 | int err; |
| 94 | |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 95 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 96 | if (!drvdata) |
| 97 | return -ENOMEM; |
| 98 | |
Guenter Roeck | 0f0a6a2 | 2019-04-02 12:01:53 -0700 | [diff] [blame] | 99 | drvdata->base = devm_platform_ioremap_resource(pdev, 0); |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 100 | if (IS_ERR(drvdata->base)) |
| 101 | return PTR_ERR(drvdata->base); |
| 102 | |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 103 | drvdata->clk = devm_clk_get(dev, pdev->name); |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 104 | if (IS_ERR(drvdata->clk)) |
| 105 | return PTR_ERR(drvdata->clk); |
| 106 | |
| 107 | err = clk_prepare_enable(drvdata->clk); |
| 108 | if (err) { |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 109 | dev_err(dev, "clk enable failed\n"); |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 110 | return err; |
| 111 | } |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 112 | err = devm_add_action_or_reset(dev, ls1x_clk_disable_unprepare, |
| 113 | drvdata->clk); |
| 114 | if (err) |
| 115 | return err; |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 116 | |
| 117 | clk_rate = clk_get_rate(drvdata->clk); |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 118 | if (!clk_rate) |
| 119 | return -EINVAL; |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 120 | drvdata->clk_rate = clk_rate; |
| 121 | |
| 122 | ls1x_wdt = &drvdata->wdt; |
| 123 | ls1x_wdt->info = &ls1x_wdt_info; |
| 124 | ls1x_wdt->ops = &ls1x_wdt_ops; |
| 125 | ls1x_wdt->timeout = DEFAULT_HEARTBEAT; |
| 126 | ls1x_wdt->min_timeout = 1; |
| 127 | ls1x_wdt->max_hw_heartbeat_ms = U32_MAX / clk_rate * 1000; |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 128 | ls1x_wdt->parent = dev; |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 129 | |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 130 | watchdog_init_timeout(ls1x_wdt, heartbeat, dev); |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 131 | watchdog_set_nowayout(ls1x_wdt, nowayout); |
| 132 | watchdog_set_drvdata(ls1x_wdt, drvdata); |
| 133 | |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 134 | err = devm_watchdog_register_device(dev, &drvdata->wdt); |
Wolfram Sang | 7da5473 | 2019-05-18 23:27:35 +0200 | [diff] [blame] | 135 | if (err) |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 136 | return err; |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 137 | |
| 138 | platform_set_drvdata(pdev, drvdata); |
| 139 | |
Guenter Roeck | fd56d6c | 2019-04-10 09:27:58 -0700 | [diff] [blame] | 140 | dev_info(dev, "Loongson1 Watchdog driver registered\n"); |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static struct platform_driver ls1x_wdt_driver = { |
| 146 | .probe = ls1x_wdt_probe, |
Yang Ling | 1d8565e | 2016-12-08 00:10:58 +0800 | [diff] [blame] | 147 | .driver = { |
| 148 | .name = "ls1x-wdt", |
| 149 | }, |
| 150 | }; |
| 151 | |
| 152 | module_platform_driver(ls1x_wdt_driver); |
| 153 | |
| 154 | MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>"); |
| 155 | MODULE_DESCRIPTION("Loongson1 Watchdog Driver"); |
| 156 | MODULE_LICENSE("GPL"); |