2018-03-16 09:14:11 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-06-18 09:19:45 -06:00
|
|
|
/*
|
|
|
|
* Watchdog driver for the A21 VME CPU Boards
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
#include <linux/uaccess.h>
|
2018-12-02 04:04:10 -07:00
|
|
|
#include <linux/gpio/consumer.h>
|
2013-06-18 09:19:45 -06:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/bitops.h>
|
2018-12-02 04:04:10 -07:00
|
|
|
#include <linux/of.h>
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
#define NUM_GPIOS 6
|
|
|
|
|
|
|
|
enum a21_wdt_gpios {
|
|
|
|
GPIO_WD_ENAB,
|
|
|
|
GPIO_WD_FAST,
|
|
|
|
GPIO_WD_TRIG,
|
|
|
|
GPIO_WD_RST0,
|
|
|
|
GPIO_WD_RST1,
|
|
|
|
GPIO_WD_RST2,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct a21_wdt_drv {
|
|
|
|
struct watchdog_device wdt;
|
2018-12-02 04:04:10 -07:00
|
|
|
struct gpio_desc *gpios[NUM_GPIOS];
|
2013-06-18 09:19:45 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool nowayout = WATCHDOG_NOWAYOUT;
|
|
|
|
module_param(nowayout, bool, 0);
|
|
|
|
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
|
|
|
|
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|
|
|
|
|
|
|
|
static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
|
|
|
|
{
|
|
|
|
int reset = 0;
|
|
|
|
|
2018-12-02 04:04:10 -07:00
|
|
|
reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
|
|
|
|
reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
|
|
|
|
reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
return reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int a21_wdt_start(struct watchdog_device *wdt)
|
|
|
|
{
|
|
|
|
struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
|
|
|
|
|
2018-12-02 04:04:10 -07:00
|
|
|
gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 1);
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int a21_wdt_stop(struct watchdog_device *wdt)
|
|
|
|
{
|
|
|
|
struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
|
|
|
|
|
2018-12-02 04:04:10 -07:00
|
|
|
gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int a21_wdt_ping(struct watchdog_device *wdt)
|
|
|
|
{
|
|
|
|
struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
|
|
|
|
|
2018-12-02 04:04:10 -07:00
|
|
|
gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 0);
|
2013-06-18 09:19:45 -06:00
|
|
|
ndelay(10);
|
2018-12-02 04:04:10 -07:00
|
|
|
gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 1);
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int a21_wdt_set_timeout(struct watchdog_device *wdt,
|
|
|
|
unsigned int timeout)
|
|
|
|
{
|
|
|
|
struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
|
|
|
|
|
|
|
|
if (timeout != 1 && timeout != 30) {
|
2015-12-24 15:22:03 -07:00
|
|
|
dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n");
|
2013-06-18 09:19:45 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == 30 && wdt->timeout == 1) {
|
2015-12-24 15:22:03 -07:00
|
|
|
dev_err(wdt->parent,
|
2013-06-18 09:19:45 -06:00
|
|
|
"Transition from fast to slow mode not allowed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == 1)
|
2018-12-02 04:04:10 -07:00
|
|
|
gpiod_set_value(drv->gpios[GPIO_WD_FAST], 1);
|
2013-06-18 09:19:45 -06:00
|
|
|
else
|
2018-12-02 04:04:10 -07:00
|
|
|
gpiod_set_value(drv->gpios[GPIO_WD_FAST], 0);
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
wdt->timeout = timeout;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct watchdog_info a21_wdt_info = {
|
|
|
|
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
|
|
|
|
.identity = "MEN A21 Watchdog",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct watchdog_ops a21_wdt_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.start = a21_wdt_start,
|
|
|
|
.stop = a21_wdt_stop,
|
|
|
|
.ping = a21_wdt_ping,
|
|
|
|
.set_timeout = a21_wdt_set_timeout,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct watchdog_device a21_wdt = {
|
|
|
|
.info = &a21_wdt_info,
|
|
|
|
.ops = &a21_wdt_ops,
|
|
|
|
.min_timeout = 1,
|
|
|
|
.max_timeout = 30,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int a21_wdt_probe(struct platform_device *pdev)
|
|
|
|
{
|
2019-04-09 11:23:40 -06:00
|
|
|
struct device *dev = &pdev->dev;
|
2013-06-18 09:19:45 -06:00
|
|
|
struct a21_wdt_drv *drv;
|
|
|
|
unsigned int reset = 0;
|
|
|
|
int num_gpios;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2019-04-09 11:23:40 -06:00
|
|
|
drv = devm_kzalloc(dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
|
2013-06-18 09:19:45 -06:00
|
|
|
if (!drv)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-04-09 11:23:40 -06:00
|
|
|
num_gpios = gpiod_count(dev, NULL);
|
2013-06-18 09:19:45 -06:00
|
|
|
if (num_gpios != NUM_GPIOS) {
|
2019-04-09 11:23:40 -06:00
|
|
|
dev_err(dev, "gpios DT property wrong, got %d want %d",
|
2013-06-18 09:19:45 -06:00
|
|
|
num_gpios, NUM_GPIOS);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Request the used GPIOs */
|
|
|
|
for (i = 0; i < num_gpios; i++) {
|
2018-12-02 04:04:10 -07:00
|
|
|
enum gpiod_flags gflags;
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
if (i < GPIO_WD_RST0)
|
2018-12-02 04:04:10 -07:00
|
|
|
gflags = GPIOD_ASIS;
|
|
|
|
else
|
|
|
|
gflags = GPIOD_IN;
|
2019-04-09 11:23:40 -06:00
|
|
|
drv->gpios[i] = devm_gpiod_get_index(dev, NULL, i, gflags);
|
|
|
|
if (IS_ERR(drv->gpios[i]))
|
|
|
|
return PTR_ERR(drv->gpios[i]);
|
2018-12-02 04:04:10 -07:00
|
|
|
|
|
|
|
gpiod_set_consumer_name(drv->gpios[i], "MEN A21 Watchdog");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the initial value from the GPIOs that should be
|
|
|
|
* output, then set up the line as output with that value.
|
|
|
|
*/
|
|
|
|
if (i < GPIO_WD_RST0) {
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = gpiod_get_value(drv->gpios[i]);
|
|
|
|
gpiod_direction_output(drv->gpios[i], val);
|
|
|
|
}
|
2013-06-18 09:19:45 -06:00
|
|
|
}
|
|
|
|
|
2019-04-09 11:23:40 -06:00
|
|
|
watchdog_init_timeout(&a21_wdt, 30, dev);
|
2013-06-18 09:19:45 -06:00
|
|
|
watchdog_set_nowayout(&a21_wdt, nowayout);
|
|
|
|
watchdog_set_drvdata(&a21_wdt, drv);
|
2019-04-09 11:23:40 -06:00
|
|
|
a21_wdt.parent = dev;
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
reset = a21_wdt_get_bootstatus(drv);
|
|
|
|
if (reset == 2)
|
|
|
|
a21_wdt.bootstatus |= WDIOF_EXTERN1;
|
|
|
|
else if (reset == 4)
|
|
|
|
a21_wdt.bootstatus |= WDIOF_CARDRESET;
|
|
|
|
else if (reset == 5)
|
|
|
|
a21_wdt.bootstatus |= WDIOF_POWERUNDER;
|
|
|
|
else if (reset == 7)
|
|
|
|
a21_wdt.bootstatus |= WDIOF_EXTERN2;
|
|
|
|
|
2015-06-02 04:25:26 -06:00
|
|
|
drv->wdt = a21_wdt;
|
2019-04-09 11:23:40 -06:00
|
|
|
dev_set_drvdata(dev, drv);
|
2015-06-02 04:25:26 -06:00
|
|
|
|
2019-04-09 11:23:40 -06:00
|
|
|
ret = devm_watchdog_register_device(dev, &a21_wdt);
|
2019-05-18 15:27:38 -06:00
|
|
|
if (ret)
|
2017-01-10 16:21:52 -07:00
|
|
|
return ret;
|
2013-06-18 09:19:45 -06:00
|
|
|
|
2019-04-09 11:23:40 -06:00
|
|
|
dev_info(dev, "MEN A21 watchdog timer driver enabled\n");
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void a21_wdt_shutdown(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
|
|
|
|
|
2018-12-02 04:04:10 -07:00
|
|
|
gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
|
2013-06-18 09:19:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id a21_wdt_ids[] = {
|
|
|
|
{ .compatible = "men,a021-wdt" },
|
|
|
|
{ },
|
|
|
|
};
|
2015-09-03 05:06:09 -06:00
|
|
|
MODULE_DEVICE_TABLE(of, a21_wdt_ids);
|
2013-06-18 09:19:45 -06:00
|
|
|
|
|
|
|
static struct platform_driver a21_wdt_driver = {
|
|
|
|
.probe = a21_wdt_probe,
|
|
|
|
.shutdown = a21_wdt_shutdown,
|
|
|
|
.driver = {
|
|
|
|
.name = "a21-watchdog",
|
|
|
|
.of_match_table = a21_wdt_ids,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(a21_wdt_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("MEN Mikro Elektronik");
|
|
|
|
MODULE_DESCRIPTION("MEN A21 Watchdog");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("platform:a21-watchdog");
|