st_lpc_wdt.c 7.9 KB
Newer Older
1 2 3 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/*
 * ST's LPC Watchdog
 *
 * Copyright (C) 2014 STMicroelectronics -- All Rights Reserved
 *
 * Author: David Paris <david.paris@st.com> for STMicroelectronics
 *         Lee Jones <lee.jones@linaro.org> for STMicroelectronics
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

#include <linux/clk.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/watchdog.h>

#include <dt-bindings/mfd/st-lpc.h>

/* Low Power Alarm */
#define LPC_LPA_LSB_OFF			0x410
#define LPC_LPA_START_OFF		0x418

/* LPC as WDT */
#define LPC_WDT_OFF			0x510

static struct watchdog_device st_wdog_dev;

struct st_wdog_syscfg {
	unsigned int reset_type_reg;
	unsigned int reset_type_mask;
	unsigned int enable_reg;
	unsigned int enable_mask;
};

struct st_wdog {
	void __iomem *base;
	struct device *dev;
	struct regmap *regmap;
	struct st_wdog_syscfg *syscfg;
	struct clk *clk;
	unsigned long clkrate;
	bool warm_reset;
};

static struct st_wdog_syscfg stid127_syscfg = {
	.reset_type_reg		= 0x004,
	.reset_type_mask	= BIT(2),
	.enable_reg		= 0x000,
	.enable_mask		= BIT(2),
};

static struct st_wdog_syscfg stih415_syscfg = {
	.reset_type_reg		= 0x0B8,
	.reset_type_mask	= BIT(6),
	.enable_reg		= 0x0B4,
	.enable_mask		= BIT(7),
};

static struct st_wdog_syscfg stih416_syscfg = {
	.reset_type_reg		= 0x88C,
	.reset_type_mask	= BIT(6),
	.enable_reg		= 0x888,
	.enable_mask		= BIT(7),
};

static struct st_wdog_syscfg stih407_syscfg = {
	.enable_reg		= 0x204,
	.enable_mask		= BIT(19),
};

static const struct of_device_id st_wdog_match[] = {
	{
		.compatible = "st,stih407-lpc",
		.data = &stih407_syscfg,
	},
	{
		.compatible = "st,stih416-lpc",
		.data = &stih416_syscfg,
	},
	{
		.compatible = "st,stih415-lpc",
		.data = &stih415_syscfg,
	},
	{
		.compatible = "st,stid127-lpc",
		.data = &stid127_syscfg,
	},
	{},
};
MODULE_DEVICE_TABLE(of, st_wdog_match);

static void st_wdog_setup(struct st_wdog *st_wdog, bool enable)
{
	/* Type of watchdog reset - 0: Cold 1: Warm */
	if (st_wdog->syscfg->reset_type_reg)
		regmap_update_bits(st_wdog->regmap,
				   st_wdog->syscfg->reset_type_reg,
				   st_wdog->syscfg->reset_type_mask,
				   st_wdog->warm_reset);

	/* Mask/unmask watchdog reset */
	regmap_update_bits(st_wdog->regmap,
			   st_wdog->syscfg->enable_reg,
			   st_wdog->syscfg->enable_mask,
			   enable ? 0 : st_wdog->syscfg->enable_mask);
}

static void st_wdog_load_timer(struct st_wdog *st_wdog, unsigned int timeout)
{
	unsigned long clkrate = st_wdog->clkrate;

	writel_relaxed(timeout * clkrate, st_wdog->base + LPC_LPA_LSB_OFF);
	writel_relaxed(1, st_wdog->base + LPC_LPA_START_OFF);
}

static int st_wdog_start(struct watchdog_device *wdd)
{
	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);

	writel_relaxed(1, st_wdog->base + LPC_WDT_OFF);

	return 0;
}

static int st_wdog_stop(struct watchdog_device *wdd)
{
	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);

	writel_relaxed(0, st_wdog->base + LPC_WDT_OFF);

	return 0;
}

static int st_wdog_set_timeout(struct watchdog_device *wdd,
			       unsigned int timeout)
{
	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);

	wdd->timeout = timeout;
	st_wdog_load_timer(st_wdog, timeout);

	return 0;
}

static int st_wdog_keepalive(struct watchdog_device *wdd)
{
	struct st_wdog *st_wdog = watchdog_get_drvdata(wdd);

	st_wdog_load_timer(st_wdog, wdd->timeout);

	return 0;
}

static const struct watchdog_info st_wdog_info = {
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
	.identity = "ST LPC WDT",
};

static const struct watchdog_ops st_wdog_ops = {
	.owner		= THIS_MODULE,
	.start		= st_wdog_start,
	.stop		= st_wdog_stop,
	.ping		= st_wdog_keepalive,
	.set_timeout	= st_wdog_set_timeout,
};

static struct watchdog_device st_wdog_dev = {
	.info		= &st_wdog_info,
	.ops		= &st_wdog_ops,
};

static int st_wdog_probe(struct platform_device *pdev)
{
	const struct of_device_id *match;
	struct device_node *np = pdev->dev.of_node;
	struct st_wdog *st_wdog;
	struct regmap *regmap;
	struct resource *res;
	struct clk *clk;
	void __iomem *base;
	uint32_t mode;
	int ret;

	ret = of_property_read_u32(np, "st,lpc-mode", &mode);
	if (ret) {
		dev_err(&pdev->dev, "An LPC mode must be provided\n");
		return -EINVAL;
	}

200
	/* LPC can either run as a Clocksource or in RTC or WDT mode */
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	if (mode != ST_LPC_MODE_WDT)
		return -ENODEV;

	st_wdog = devm_kzalloc(&pdev->dev, sizeof(*st_wdog), GFP_KERNEL);
	if (!st_wdog)
		return -ENOMEM;

	match = of_match_device(st_wdog_match, &pdev->dev);
	if (!match) {
		dev_err(&pdev->dev, "Couldn't match device\n");
		return -ENODEV;
	}
	st_wdog->syscfg	= (struct st_wdog_syscfg *)match->data;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base))
		return PTR_ERR(base);

	regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
	if (IS_ERR(regmap)) {
		dev_err(&pdev->dev, "No syscfg phandle specified\n");
		return PTR_ERR(regmap);
	}

	clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(clk)) {
		dev_err(&pdev->dev, "Unable to request clock\n");
		return PTR_ERR(clk);
	}

	st_wdog->dev		= &pdev->dev;
	st_wdog->base		= base;
	st_wdog->clk		= clk;
	st_wdog->regmap		= regmap;
	st_wdog->warm_reset	= of_property_read_bool(np, "st,warm_reset");
	st_wdog->clkrate	= clk_get_rate(st_wdog->clk);

	if (!st_wdog->clkrate) {
		dev_err(&pdev->dev, "Unable to fetch clock rate\n");
		return -EINVAL;
	}
	st_wdog_dev.max_timeout = 0xFFFFFFFF / st_wdog->clkrate;

	ret = clk_prepare_enable(clk);
	if (ret) {
		dev_err(&pdev->dev, "Unable to enable clock\n");
		return ret;
	}

	watchdog_set_drvdata(&st_wdog_dev, st_wdog);
	watchdog_set_nowayout(&st_wdog_dev, WATCHDOG_NOWAYOUT);

	/* Init Watchdog timeout with value in DT */
	ret = watchdog_init_timeout(&st_wdog_dev, 0, &pdev->dev);
	if (ret) {
		dev_err(&pdev->dev, "Unable to initialise watchdog timeout\n");
		clk_disable_unprepare(clk);
		return ret;
	}

	ret = watchdog_register_device(&st_wdog_dev);
	if (ret) {
		dev_err(&pdev->dev, "Unable to register watchdog\n");
		clk_disable_unprepare(clk);
		return ret;
	}

	st_wdog_setup(st_wdog, true);

	dev_info(&pdev->dev, "LPC Watchdog driver registered, reset type is %s",
		 st_wdog->warm_reset ? "warm" : "cold");

	return ret;
}

static int st_wdog_remove(struct platform_device *pdev)
{
	struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);

	st_wdog_setup(st_wdog, false);
	watchdog_unregister_device(&st_wdog_dev);
	clk_disable_unprepare(st_wdog->clk);

	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int st_wdog_suspend(struct device *dev)
{
	struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);

	if (watchdog_active(&st_wdog_dev))
		st_wdog_stop(&st_wdog_dev);

	st_wdog_setup(st_wdog, false);

	clk_disable(st_wdog->clk);

	return 0;
}

static int st_wdog_resume(struct device *dev)
{
	struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev);
	int ret;

	ret = clk_enable(st_wdog->clk);
	if (ret) {
		dev_err(dev, "Unable to re-enable clock\n");
		watchdog_unregister_device(&st_wdog_dev);
		clk_unprepare(st_wdog->clk);
		return ret;
	}

	st_wdog_setup(st_wdog, true);

	if (watchdog_active(&st_wdog_dev)) {
		st_wdog_load_timer(st_wdog, st_wdog_dev.timeout);
		st_wdog_start(&st_wdog_dev);
	}

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(st_wdog_pm_ops,
			 st_wdog_suspend,
			 st_wdog_resume);

static struct platform_driver st_wdog_driver = {
	.driver	= {
		.name = "st-lpc-wdt",
		.pm = &st_wdog_pm_ops,
		.of_match_table = st_wdog_match,
	},
	.probe = st_wdog_probe,
	.remove = st_wdog_remove,
};
module_platform_driver(st_wdog_driver);

MODULE_AUTHOR("David Paris <david.paris@st.com>");
MODULE_DESCRIPTION("ST LPC Watchdog Driver");
MODULE_LICENSE("GPL");