Blame view

drivers/watchdog/loongson1_wdt.c 3.55 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1d8565ee4   Yang Ling   watchdog: loongso...
2
3
  /*
   * Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com>
1d8565ee4   Yang Ling   watchdog: loongso...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
   */
  
  #include <linux/clk.h>
  #include <linux/module.h>
  #include <linux/platform_device.h>
  #include <linux/watchdog.h>
  #include <loongson1.h>
  
  #define DEFAULT_HEARTBEAT	30
  
  static bool nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, bool, 0444);
  
  static unsigned int heartbeat;
  module_param(heartbeat, uint, 0444);
  
  struct ls1x_wdt_drvdata {
  	void __iomem *base;
  	struct clk *clk;
  	unsigned long clk_rate;
  	struct watchdog_device wdt;
  };
  
  static int ls1x_wdt_ping(struct watchdog_device *wdt_dev)
  {
  	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  
  	writel(0x1, drvdata->base + WDT_SET);
  
  	return 0;
  }
  
  static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev,
  				unsigned int timeout)
  {
  	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  	unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000;
  	unsigned int counts;
  
  	wdt_dev->timeout = timeout;
  
  	counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat);
  	writel(counts, drvdata->base + WDT_TIMER);
  
  	return 0;
  }
  
  static int ls1x_wdt_start(struct watchdog_device *wdt_dev)
  {
  	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  
  	writel(0x1, drvdata->base + WDT_EN);
  
  	return 0;
  }
  
  static int ls1x_wdt_stop(struct watchdog_device *wdt_dev)
  {
  	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  
  	writel(0x0, drvdata->base + WDT_EN);
  
  	return 0;
  }
  
  static const struct watchdog_info ls1x_wdt_info = {
  	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  	.identity = "Loongson1 Watchdog",
  };
  
  static const struct watchdog_ops ls1x_wdt_ops = {
  	.owner = THIS_MODULE,
  	.start = ls1x_wdt_start,
  	.stop = ls1x_wdt_stop,
  	.ping = ls1x_wdt_ping,
  	.set_timeout = ls1x_wdt_set_timeout,
  };
fd56d6c9a   Guenter Roeck   watchdog: loongso...
81
82
83
84
  static void ls1x_clk_disable_unprepare(void *data)
  {
  	clk_disable_unprepare(data);
  }
1d8565ee4   Yang Ling   watchdog: loongso...
85
86
  static int ls1x_wdt_probe(struct platform_device *pdev)
  {
fd56d6c9a   Guenter Roeck   watchdog: loongso...
87
  	struct device *dev = &pdev->dev;
1d8565ee4   Yang Ling   watchdog: loongso...
88
89
90
  	struct ls1x_wdt_drvdata *drvdata;
  	struct watchdog_device *ls1x_wdt;
  	unsigned long clk_rate;
1d8565ee4   Yang Ling   watchdog: loongso...
91
  	int err;
fd56d6c9a   Guenter Roeck   watchdog: loongso...
92
  	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1d8565ee4   Yang Ling   watchdog: loongso...
93
94
  	if (!drvdata)
  		return -ENOMEM;
0f0a6a285   Guenter Roeck   watchdog: Convert...
95
  	drvdata->base = devm_platform_ioremap_resource(pdev, 0);
1d8565ee4   Yang Ling   watchdog: loongso...
96
97
  	if (IS_ERR(drvdata->base))
  		return PTR_ERR(drvdata->base);
fd56d6c9a   Guenter Roeck   watchdog: loongso...
98
  	drvdata->clk = devm_clk_get(dev, pdev->name);
1d8565ee4   Yang Ling   watchdog: loongso...
99
100
101
102
103
  	if (IS_ERR(drvdata->clk))
  		return PTR_ERR(drvdata->clk);
  
  	err = clk_prepare_enable(drvdata->clk);
  	if (err) {
fd56d6c9a   Guenter Roeck   watchdog: loongso...
104
105
  		dev_err(dev, "clk enable failed
  ");
1d8565ee4   Yang Ling   watchdog: loongso...
106
107
  		return err;
  	}
fd56d6c9a   Guenter Roeck   watchdog: loongso...
108
109
110
111
  	err = devm_add_action_or_reset(dev, ls1x_clk_disable_unprepare,
  				       drvdata->clk);
  	if (err)
  		return err;
1d8565ee4   Yang Ling   watchdog: loongso...
112
113
  
  	clk_rate = clk_get_rate(drvdata->clk);
fd56d6c9a   Guenter Roeck   watchdog: loongso...
114
115
  	if (!clk_rate)
  		return -EINVAL;
1d8565ee4   Yang Ling   watchdog: loongso...
116
117
118
119
120
121
122
123
  	drvdata->clk_rate = clk_rate;
  
  	ls1x_wdt = &drvdata->wdt;
  	ls1x_wdt->info = &ls1x_wdt_info;
  	ls1x_wdt->ops = &ls1x_wdt_ops;
  	ls1x_wdt->timeout = DEFAULT_HEARTBEAT;
  	ls1x_wdt->min_timeout = 1;
  	ls1x_wdt->max_hw_heartbeat_ms = U32_MAX / clk_rate * 1000;
fd56d6c9a   Guenter Roeck   watchdog: loongso...
124
  	ls1x_wdt->parent = dev;
1d8565ee4   Yang Ling   watchdog: loongso...
125

fd56d6c9a   Guenter Roeck   watchdog: loongso...
126
  	watchdog_init_timeout(ls1x_wdt, heartbeat, dev);
1d8565ee4   Yang Ling   watchdog: loongso...
127
128
  	watchdog_set_nowayout(ls1x_wdt, nowayout);
  	watchdog_set_drvdata(ls1x_wdt, drvdata);
fd56d6c9a   Guenter Roeck   watchdog: loongso...
129
  	err = devm_watchdog_register_device(dev, &drvdata->wdt);
7da547350   Wolfram Sang   watchdog: loongso...
130
  	if (err)
fd56d6c9a   Guenter Roeck   watchdog: loongso...
131
  		return err;
1d8565ee4   Yang Ling   watchdog: loongso...
132
133
  
  	platform_set_drvdata(pdev, drvdata);
fd56d6c9a   Guenter Roeck   watchdog: loongso...
134
135
  	dev_info(dev, "Loongson1 Watchdog driver registered
  ");
1d8565ee4   Yang Ling   watchdog: loongso...
136
137
138
139
140
141
  
  	return 0;
  }
  
  static struct platform_driver ls1x_wdt_driver = {
  	.probe = ls1x_wdt_probe,
1d8565ee4   Yang Ling   watchdog: loongso...
142
143
144
145
146
147
148
149
150
151
  	.driver = {
  		.name = "ls1x-wdt",
  	},
  };
  
  module_platform_driver(ls1x_wdt_driver);
  
  MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>");
  MODULE_DESCRIPTION("Loongson1 Watchdog Driver");
  MODULE_LICENSE("GPL");