2014-09-20 21:06:50 +04:00
/*
* Meson Watchdog Driver
*
* Copyright ( c ) 2014 Carlo Caione
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version
* 2 of the License , or ( at your option ) any later version .
*/
# include <linux/clk.h>
# include <linux/delay.h>
# include <linux/err.h>
# include <linux/init.h>
# include <linux/io.h>
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/moduleparam.h>
# include <linux/of.h>
2015-11-08 15:18:54 +03:00
# include <linux/of_device.h>
2014-09-20 21:06:50 +04:00
# include <linux/platform_device.h>
# include <linux/types.h>
# include <linux/watchdog.h>
# define DRV_NAME "meson_wdt"
# define MESON_WDT_TC 0x00
# define MESON_WDT_DC_RESET (3 << 24)
# define MESON_WDT_RESET 0x04
# define MESON_WDT_TIMEOUT 30
# define MESON_WDT_MIN_TIMEOUT 1
2015-11-08 15:18:54 +03:00
# define MESON_SEC_TO_TC(s, c) ((s) * (c))
2014-09-20 21:06:50 +04:00
static bool nowayout = WATCHDOG_NOWAYOUT ;
2018-02-11 23:08:46 +03:00
static unsigned int timeout ;
2014-09-20 21:06:50 +04:00
2015-11-08 15:18:54 +03:00
struct meson_wdt_data {
unsigned int enable ;
unsigned int terminal_count_mask ;
unsigned int count_unit ;
} ;
static struct meson_wdt_data meson6_wdt_data = {
. enable = BIT ( 22 ) ,
. terminal_count_mask = 0x3fffff ,
. count_unit = 100000 , /* 10 us */
} ;
2015-11-08 15:18:55 +03:00
static struct meson_wdt_data meson8b_wdt_data = {
. enable = BIT ( 19 ) ,
. terminal_count_mask = 0xffff ,
. count_unit = 7812 , /* 128 us */
} ;
2014-09-20 21:06:50 +04:00
struct meson_wdt_dev {
struct watchdog_device wdt_dev ;
void __iomem * wdt_base ;
2015-11-08 15:18:54 +03:00
const struct meson_wdt_data * data ;
2014-09-20 21:06:50 +04:00
} ;
2016-02-27 04:32:49 +03:00
static int meson_wdt_restart ( struct watchdog_device * wdt_dev ,
unsigned long action , void * data )
2014-09-20 21:06:50 +04:00
{
2015-11-16 20:28:06 +03:00
struct meson_wdt_dev * meson_wdt = watchdog_get_drvdata ( wdt_dev ) ;
2015-11-08 15:18:54 +03:00
u32 tc_reboot = MESON_WDT_DC_RESET ;
tc_reboot | = meson_wdt - > data - > enable ;
2014-09-20 21:06:50 +04:00
while ( 1 ) {
writel ( tc_reboot , meson_wdt - > wdt_base + MESON_WDT_TC ) ;
mdelay ( 5 ) ;
}
2015-11-16 20:28:06 +03:00
return 0 ;
2014-09-20 21:06:50 +04:00
}
static int meson_wdt_ping ( struct watchdog_device * wdt_dev )
{
struct meson_wdt_dev * meson_wdt = watchdog_get_drvdata ( wdt_dev ) ;
writel ( 0 , meson_wdt - > wdt_base + MESON_WDT_RESET ) ;
return 0 ;
}
static void meson_wdt_change_timeout ( struct watchdog_device * wdt_dev ,
unsigned int timeout )
{
struct meson_wdt_dev * meson_wdt = watchdog_get_drvdata ( wdt_dev ) ;
u32 reg ;
reg = readl ( meson_wdt - > wdt_base + MESON_WDT_TC ) ;
2015-11-08 15:18:54 +03:00
reg & = ~ meson_wdt - > data - > terminal_count_mask ;
reg | = MESON_SEC_TO_TC ( timeout , meson_wdt - > data - > count_unit ) ;
2014-09-20 21:06:50 +04:00
writel ( reg , meson_wdt - > wdt_base + MESON_WDT_TC ) ;
}
static int meson_wdt_set_timeout ( struct watchdog_device * wdt_dev ,
unsigned int timeout )
{
wdt_dev - > timeout = timeout ;
meson_wdt_change_timeout ( wdt_dev , timeout ) ;
meson_wdt_ping ( wdt_dev ) ;
return 0 ;
}
static int meson_wdt_stop ( struct watchdog_device * wdt_dev )
{
struct meson_wdt_dev * meson_wdt = watchdog_get_drvdata ( wdt_dev ) ;
u32 reg ;
reg = readl ( meson_wdt - > wdt_base + MESON_WDT_TC ) ;
2015-11-08 15:18:54 +03:00
reg & = ~ meson_wdt - > data - > enable ;
2014-09-20 21:06:50 +04:00
writel ( reg , meson_wdt - > wdt_base + MESON_WDT_TC ) ;
return 0 ;
}
static int meson_wdt_start ( struct watchdog_device * wdt_dev )
{
struct meson_wdt_dev * meson_wdt = watchdog_get_drvdata ( wdt_dev ) ;
u32 reg ;
meson_wdt_change_timeout ( wdt_dev , meson_wdt - > wdt_dev . timeout ) ;
meson_wdt_ping ( wdt_dev ) ;
reg = readl ( meson_wdt - > wdt_base + MESON_WDT_TC ) ;
2015-11-08 15:18:54 +03:00
reg | = meson_wdt - > data - > enable ;
2014-09-20 21:06:50 +04:00
writel ( reg , meson_wdt - > wdt_base + MESON_WDT_TC ) ;
return 0 ;
}
static const struct watchdog_info meson_wdt_info = {
. identity = DRV_NAME ,
. options = WDIOF_SETTIMEOUT |
WDIOF_KEEPALIVEPING |
WDIOF_MAGICCLOSE ,
} ;
static const struct watchdog_ops meson_wdt_ops = {
. owner = THIS_MODULE ,
. start = meson_wdt_start ,
. stop = meson_wdt_stop ,
. ping = meson_wdt_ping ,
. set_timeout = meson_wdt_set_timeout ,
2015-11-16 20:28:06 +03:00
. restart = meson_wdt_restart ,
2014-09-20 21:06:50 +04:00
} ;
2015-11-08 15:18:54 +03:00
static const struct of_device_id meson_wdt_dt_ids [ ] = {
{ . compatible = " amlogic,meson6-wdt " , . data = & meson6_wdt_data } ,
2017-07-12 01:14:38 +03:00
{ . compatible = " amlogic,meson8-wdt " , . data = & meson6_wdt_data } ,
2015-11-08 15:18:55 +03:00
{ . compatible = " amlogic,meson8b-wdt " , . data = & meson8b_wdt_data } ,
2017-07-12 01:14:38 +03:00
{ . compatible = " amlogic,meson8m2-wdt " , . data = & meson8b_wdt_data } ,
2015-11-08 15:18:54 +03:00
{ /* sentinel */ }
} ;
MODULE_DEVICE_TABLE ( of , meson_wdt_dt_ids ) ;
2014-09-20 21:06:50 +04:00
static int meson_wdt_probe ( struct platform_device * pdev )
{
2019-04-09 20:23:43 +03:00
struct device * dev = & pdev - > dev ;
2014-09-20 21:06:50 +04:00
struct meson_wdt_dev * meson_wdt ;
2015-11-08 15:18:54 +03:00
const struct of_device_id * of_id ;
2014-09-20 21:06:50 +04:00
int err ;
2019-04-09 20:23:43 +03:00
meson_wdt = devm_kzalloc ( dev , sizeof ( * meson_wdt ) , GFP_KERNEL ) ;
2014-09-20 21:06:50 +04:00
if ( ! meson_wdt )
return - ENOMEM ;
watchdog: Convert to use devm_platform_ioremap_resource
Use devm_platform_ioremap_resource to reduce source code size,
improve readability, and reduce the likelyhood of bugs.
The conversion was done automatically with coccinelle using the
following semantic patch.
@r@
identifier res, pdev;
expression a;
expression index;
expression e;
@@
<+...
- res = platform_get_resource(pdev, IORESOURCE_MEM, index);
- a = devm_ioremap_resource(e, res);
+ a = devm_platform_ioremap_resource(pdev, index);
...+>
@depends on r@
identifier r.res;
@@
- struct resource *res;
... when != res
@@
identifier res, pdev;
expression index;
expression a;
@@
- struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, index);
- a = devm_ioremap_resource(&pdev->dev, res);
+ a = devm_platform_ioremap_resource(pdev, index);
Cc: Joel Stanley <joel@jms.id.au>
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Baruch Siach <baruch@tkos.co.il>
Cc: Keguang Zhang <keguang.zhang@gmail.com>
Cc: Vladimir Zapolskiy <vz@mleia.com>
Cc: Kevin Hilman <khilman@baylibre.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: Avi Fishman <avifishman70@gmail.com>
Cc: Nancy Yuen <yuenn@google.com>
Cc: Brendan Higgins <brendanhiggins@google.com>
Cc: Wan ZongShun <mcuos.com@gmail.com>
Cc: Michal Simek <michal.simek@xilinx.com>
Cc: Sylvain Lemieux <slemieux.tyco@gmail.com>
Cc: Kukjin Kim <kgene@kernel.org>
Cc: Barry Song <baohua@kernel.org>
Cc: Orson Zhai <orsonzhai@gmail.com>
Cc: Patrice Chotard <patrice.chotard@st.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Maxime Ripard <maxime.ripard@bootlin.com>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Marc Gonzalez <marc.w.gonzalez@free.fr>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Shawn Guo <shawnguo@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Tested-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Acked-by: Joel Stanley <joel@jms.id.au>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Maxime Ripard <maxime.ripard@bootlin.com>
Acked-by: Michal Simek <michal.simek@xilinx.com> (cadence/xilinx wdts)
Acked-by: Thierry Reding <treding@nvidia.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Acked-by: Patrice Chotard <patrice.chotard@st.com>
Acked-by: Vladimir Zapolskiy <vz@mleia.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Wim Van Sebroeck <wim@linux-watchdog.org>
2019-04-02 22:01:53 +03:00
meson_wdt - > wdt_base = devm_platform_ioremap_resource ( pdev , 0 ) ;
2014-09-20 21:06:50 +04:00
if ( IS_ERR ( meson_wdt - > wdt_base ) )
return PTR_ERR ( meson_wdt - > wdt_base ) ;
2019-04-09 20:23:43 +03:00
of_id = of_match_device ( meson_wdt_dt_ids , dev ) ;
2015-11-08 15:18:54 +03:00
if ( ! of_id ) {
2019-04-09 20:23:43 +03:00
dev_err ( dev , " Unable to initialize WDT data \n " ) ;
2015-11-08 15:18:54 +03:00
return - ENODEV ;
}
meson_wdt - > data = of_id - > data ;
2019-04-09 20:23:43 +03:00
meson_wdt - > wdt_dev . parent = dev ;
2014-09-20 21:06:50 +04:00
meson_wdt - > wdt_dev . info = & meson_wdt_info ;
meson_wdt - > wdt_dev . ops = & meson_wdt_ops ;
2015-11-08 15:18:54 +03:00
meson_wdt - > wdt_dev . max_timeout =
meson_wdt - > data - > terminal_count_mask / meson_wdt - > data - > count_unit ;
2014-09-20 21:06:50 +04:00
meson_wdt - > wdt_dev . min_timeout = MESON_WDT_MIN_TIMEOUT ;
2015-11-08 15:18:54 +03:00
meson_wdt - > wdt_dev . timeout = min_t ( unsigned int ,
MESON_WDT_TIMEOUT ,
meson_wdt - > wdt_dev . max_timeout ) ;
2014-09-20 21:06:50 +04:00
watchdog_set_drvdata ( & meson_wdt - > wdt_dev , meson_wdt ) ;
2019-04-09 20:23:43 +03:00
watchdog_init_timeout ( & meson_wdt - > wdt_dev , timeout , dev ) ;
2014-09-20 21:06:50 +04:00
watchdog_set_nowayout ( & meson_wdt - > wdt_dev , nowayout ) ;
2015-11-16 20:28:06 +03:00
watchdog_set_restart_priority ( & meson_wdt - > wdt_dev , 128 ) ;
2014-09-20 21:06:50 +04:00
meson_wdt_stop ( & meson_wdt - > wdt_dev ) ;
2017-01-11 02:21:53 +03:00
watchdog_stop_on_reboot ( & meson_wdt - > wdt_dev ) ;
2019-04-09 20:23:43 +03:00
err = devm_watchdog_register_device ( dev , & meson_wdt - > wdt_dev ) ;
2014-09-20 21:06:50 +04:00
if ( err )
return err ;
2019-04-09 20:23:43 +03:00
dev_info ( dev , " Watchdog enabled (timeout=%d sec, nowayout=%d) " ,
2014-09-20 21:06:50 +04:00
meson_wdt - > wdt_dev . timeout , nowayout ) ;
return 0 ;
}
static struct platform_driver meson_wdt_driver = {
. probe = meson_wdt_probe ,
. driver = {
. name = DRV_NAME ,
. of_match_table = meson_wdt_dt_ids ,
} ,
} ;
module_platform_driver ( meson_wdt_driver ) ;
module_param ( timeout , uint , 0 ) ;
MODULE_PARM_DESC ( timeout , " Watchdog heartbeat in seconds " ) ;
module_param ( nowayout , bool , 0 ) ;
MODULE_PARM_DESC ( nowayout ,
" Watchdog cannot be stopped once started (default= "
__MODULE_STRING ( WATCHDOG_NOWAYOUT ) " ) " ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_AUTHOR ( " Carlo Caione <carlo@caione.org> " ) ;
MODULE_DESCRIPTION ( " Meson Watchdog Timer Driver " ) ;