2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2015-01-06 16:19:34 +03:00
/*
* Imagination Technologies PowerDown Controller Watchdog Timer .
*
* Copyright ( c ) 2014 Imagination Technologies Ltd .
*
* Based on drivers / watchdog / sunxi_wdt . c Copyright ( c ) 2013 Carlo Caione
* 2012 Henrik Nordstrom
2015-05-11 20:41:05 +03:00
*
* Notes
* - - - - -
* The timeout value is rounded to the next power of two clock cycles .
* This is configured using the PDC_WDT_CONFIG register , according to this
* formula :
*
* timeout = 2 ^ ( delay + 1 ) clock cycles
*
* Where ' delay ' is the value written in PDC_WDT_CONFIG register .
*
* Therefore , the hardware only allows to program watchdog timeouts , expressed
* as a power of two number of watchdog clock cycles . The current implementation
* guarantees that the actual watchdog timeout will be _at least_ the value
* programmed in the imgpdg_wdt driver .
*
* The following table shows how the user - configured timeout relates
* to the actual hardware timeout ( watchdog clock @ 40000 Hz ) :
*
* input timeout | WD_DELAY | actual timeout
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* 10 | 18 | 13 seconds
* 20 | 19 | 26 seconds
* 30 | 20 | 52 seconds
* 60 | 21 | 104 seconds
*
* Albeit coarse , this granularity would suffice most watchdog uses .
* If the platform allows it , the user should be able to change the watchdog
* clock rate and achieve a finer timeout granularity .
2015-01-06 16:19:34 +03:00
*/
# include <linux/clk.h>
# include <linux/io.h>
# include <linux/log2.h>
# include <linux/module.h>
2018-06-20 08:47:28 +03:00
# include <linux/mod_devicetable.h>
2015-01-06 16:19:34 +03:00
# include <linux/platform_device.h>
# include <linux/slab.h>
# include <linux/watchdog.h>
/* registers */
# define PDC_WDT_SOFT_RESET 0x00
# define PDC_WDT_CONFIG 0x04
# define PDC_WDT_CONFIG_ENABLE BIT(31)
# define PDC_WDT_CONFIG_DELAY_MASK 0x1f
# define PDC_WDT_TICKLE1 0x08
# define PDC_WDT_TICKLE1_MAGIC 0xabcd1234
# define PDC_WDT_TICKLE2 0x0c
# define PDC_WDT_TICKLE2_MAGIC 0x4321dcba
# define PDC_WDT_TICKLE_STATUS_MASK 0x7
# define PDC_WDT_TICKLE_STATUS_SHIFT 0
# define PDC_WDT_TICKLE_STATUS_HRESET 0x0 /* Hard reset */
# define PDC_WDT_TICKLE_STATUS_TIMEOUT 0x1 /* Timeout */
# define PDC_WDT_TICKLE_STATUS_TICKLE 0x2 /* Tickled incorrectly */
# define PDC_WDT_TICKLE_STATUS_SRESET 0x3 /* Soft reset */
# define PDC_WDT_TICKLE_STATUS_USER 0x4 /* User reset */
/* Timeout values are in seconds */
# define PDC_WDT_MIN_TIMEOUT 1
# define PDC_WDT_DEF_TIMEOUT 64
2015-04-03 20:05:20 +03:00
static int heartbeat ;
2015-01-06 16:19:34 +03:00
module_param ( heartbeat , int , 0 ) ;
2015-02-21 02:45:45 +03:00
MODULE_PARM_DESC ( heartbeat , " Watchdog heartbeats in seconds "
" (default= " __MODULE_STRING ( PDC_WDT_DEF_TIMEOUT ) " ) " ) ;
2015-01-06 16:19:34 +03: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 ) " ) " ) ;
struct pdc_wdt_dev {
struct watchdog_device wdt_dev ;
struct clk * wdt_clk ;
struct clk * sys_clk ;
void __iomem * base ;
} ;
static int pdc_wdt_keepalive ( struct watchdog_device * wdt_dev )
{
struct pdc_wdt_dev * wdt = watchdog_get_drvdata ( wdt_dev ) ;
writel ( PDC_WDT_TICKLE1_MAGIC , wdt - > base + PDC_WDT_TICKLE1 ) ;
writel ( PDC_WDT_TICKLE2_MAGIC , wdt - > base + PDC_WDT_TICKLE2 ) ;
return 0 ;
}
static int pdc_wdt_stop ( struct watchdog_device * wdt_dev )
{
unsigned int val ;
struct pdc_wdt_dev * wdt = watchdog_get_drvdata ( wdt_dev ) ;
val = readl ( wdt - > base + PDC_WDT_CONFIG ) ;
val & = ~ PDC_WDT_CONFIG_ENABLE ;
writel ( val , wdt - > base + PDC_WDT_CONFIG ) ;
/* Must tickle to finish the stop */
pdc_wdt_keepalive ( wdt_dev ) ;
return 0 ;
}
2015-04-03 20:05:21 +03:00
static void __pdc_wdt_set_timeout ( struct pdc_wdt_dev * wdt )
{
unsigned long clk_rate = clk_get_rate ( wdt - > wdt_clk ) ;
unsigned int val ;
val = readl ( wdt - > base + PDC_WDT_CONFIG ) & ~ PDC_WDT_CONFIG_DELAY_MASK ;
val | = order_base_2 ( wdt - > wdt_dev . timeout * clk_rate ) - 1 ;
writel ( val , wdt - > base + PDC_WDT_CONFIG ) ;
}
2015-01-06 16:19:34 +03:00
static int pdc_wdt_set_timeout ( struct watchdog_device * wdt_dev ,
unsigned int new_timeout )
{
struct pdc_wdt_dev * wdt = watchdog_get_drvdata ( wdt_dev ) ;
wdt - > wdt_dev . timeout = new_timeout ;
2015-04-03 20:05:21 +03:00
__pdc_wdt_set_timeout ( wdt ) ;
2015-01-06 16:19:34 +03:00
return 0 ;
}
/* Start the watchdog timer (delay should already be set) */
static int pdc_wdt_start ( struct watchdog_device * wdt_dev )
{
unsigned int val ;
struct pdc_wdt_dev * wdt = watchdog_get_drvdata ( wdt_dev ) ;
2015-04-03 20:05:21 +03:00
__pdc_wdt_set_timeout ( wdt ) ;
2015-01-06 16:19:34 +03:00
val = readl ( wdt - > base + PDC_WDT_CONFIG ) ;
val | = PDC_WDT_CONFIG_ENABLE ;
writel ( val , wdt - > base + PDC_WDT_CONFIG ) ;
return 0 ;
}
2016-02-27 04:32:49 +03:00
static int pdc_wdt_restart ( struct watchdog_device * wdt_dev ,
unsigned long action , void * data )
2015-11-16 20:28:03 +03:00
{
struct pdc_wdt_dev * wdt = watchdog_get_drvdata ( wdt_dev ) ;
/* Assert SOFT_RESET */
writel ( 0x1 , wdt - > base + PDC_WDT_SOFT_RESET ) ;
return 0 ;
}
2016-12-26 20:05:11 +03:00
static const struct watchdog_info pdc_wdt_info = {
2015-01-06 16:19:34 +03:00
. identity = " IMG PDC Watchdog " ,
. options = WDIOF_SETTIMEOUT |
WDIOF_KEEPALIVEPING |
WDIOF_MAGICCLOSE ,
} ;
static const struct watchdog_ops pdc_wdt_ops = {
. owner = THIS_MODULE ,
. start = pdc_wdt_start ,
. stop = pdc_wdt_stop ,
. ping = pdc_wdt_keepalive ,
. set_timeout = pdc_wdt_set_timeout ,
2015-11-16 20:28:03 +03:00
. restart = pdc_wdt_restart ,
2015-01-06 16:19:34 +03:00
} ;
2019-04-08 22:38:42 +03:00
static void pdc_clk_disable_unprepare ( void * data )
{
clk_disable_unprepare ( data ) ;
}
2015-01-06 16:19:34 +03:00
static int pdc_wdt_probe ( struct platform_device * pdev )
{
2019-04-08 22:38:42 +03:00
struct device * dev = & pdev - > dev ;
2015-05-11 20:41:04 +03:00
u64 div ;
2015-01-06 16:19:34 +03:00
int ret , val ;
unsigned long clk_rate ;
struct pdc_wdt_dev * pdc_wdt ;
2019-04-08 22:38:42 +03:00
pdc_wdt = devm_kzalloc ( dev , sizeof ( * pdc_wdt ) , GFP_KERNEL ) ;
2015-01-06 16:19:34 +03:00
if ( ! pdc_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
pdc_wdt - > base = devm_platform_ioremap_resource ( pdev , 0 ) ;
2015-01-06 16:19:34 +03:00
if ( IS_ERR ( pdc_wdt - > base ) )
return PTR_ERR ( pdc_wdt - > base ) ;
2019-04-08 22:38:42 +03:00
pdc_wdt - > sys_clk = devm_clk_get ( dev , " sys " ) ;
2015-01-06 16:19:34 +03:00
if ( IS_ERR ( pdc_wdt - > sys_clk ) ) {
2019-04-08 22:38:42 +03:00
dev_err ( dev , " failed to get the sys clock \n " ) ;
2015-01-06 16:19:34 +03:00
return PTR_ERR ( pdc_wdt - > sys_clk ) ;
}
2019-04-08 22:38:42 +03:00
pdc_wdt - > wdt_clk = devm_clk_get ( dev , " wdt " ) ;
2015-01-06 16:19:34 +03:00
if ( IS_ERR ( pdc_wdt - > wdt_clk ) ) {
2019-04-08 22:38:42 +03:00
dev_err ( dev , " failed to get the wdt clock \n " ) ;
2015-01-06 16:19:34 +03:00
return PTR_ERR ( pdc_wdt - > wdt_clk ) ;
}
ret = clk_prepare_enable ( pdc_wdt - > sys_clk ) ;
if ( ret ) {
2019-04-08 22:38:42 +03:00
dev_err ( dev , " could not prepare or enable sys clock \n " ) ;
2015-01-06 16:19:34 +03:00
return ret ;
}
2019-04-08 22:38:42 +03:00
ret = devm_add_action_or_reset ( dev , pdc_clk_disable_unprepare ,
pdc_wdt - > sys_clk ) ;
if ( ret )
return ret ;
2015-01-06 16:19:34 +03:00
ret = clk_prepare_enable ( pdc_wdt - > wdt_clk ) ;
if ( ret ) {
2019-04-08 22:38:42 +03:00
dev_err ( dev , " could not prepare or enable wdt clock \n " ) ;
return ret ;
2015-01-06 16:19:34 +03:00
}
2019-04-08 22:38:42 +03:00
ret = devm_add_action_or_reset ( dev , pdc_clk_disable_unprepare ,
pdc_wdt - > wdt_clk ) ;
if ( ret )
return ret ;
2015-01-06 16:19:34 +03:00
/* We use the clock rate to calculate the max timeout */
clk_rate = clk_get_rate ( pdc_wdt - > wdt_clk ) ;
if ( clk_rate = = 0 ) {
2019-04-08 22:38:42 +03:00
dev_err ( dev , " failed to get clock rate \n " ) ;
return - EINVAL ;
2015-01-06 16:19:34 +03:00
}
if ( order_base_2 ( clk_rate ) > PDC_WDT_CONFIG_DELAY_MASK + 1 ) {
2019-04-08 22:38:42 +03:00
dev_err ( dev , " invalid clock rate \n " ) ;
return - EINVAL ;
2015-01-06 16:19:34 +03:00
}
if ( order_base_2 ( clk_rate ) = = 0 )
pdc_wdt - > wdt_dev . min_timeout = PDC_WDT_MIN_TIMEOUT + 1 ;
else
pdc_wdt - > wdt_dev . min_timeout = PDC_WDT_MIN_TIMEOUT ;
pdc_wdt - > wdt_dev . info = & pdc_wdt_info ;
pdc_wdt - > wdt_dev . ops = & pdc_wdt_ops ;
2015-05-11 20:41:04 +03:00
div = 1ULL < < ( PDC_WDT_CONFIG_DELAY_MASK + 1 ) ;
do_div ( div , clk_rate ) ;
pdc_wdt - > wdt_dev . max_timeout = div ;
2015-04-03 20:05:20 +03:00
pdc_wdt - > wdt_dev . timeout = PDC_WDT_DEF_TIMEOUT ;
2019-04-08 22:38:42 +03:00
pdc_wdt - > wdt_dev . parent = dev ;
2015-02-21 02:45:44 +03:00
watchdog_set_drvdata ( & pdc_wdt - > wdt_dev , pdc_wdt ) ;
2015-01-06 16:19:34 +03:00
2019-04-08 22:38:42 +03:00
watchdog_init_timeout ( & pdc_wdt - > wdt_dev , heartbeat , dev ) ;
2015-01-06 16:19:34 +03:00
pdc_wdt_stop ( & pdc_wdt - > wdt_dev ) ;
/* Find what caused the last reset */
val = readl ( pdc_wdt - > base + PDC_WDT_TICKLE1 ) ;
val = ( val & PDC_WDT_TICKLE_STATUS_MASK ) > > PDC_WDT_TICKLE_STATUS_SHIFT ;
switch ( val ) {
case PDC_WDT_TICKLE_STATUS_TICKLE :
case PDC_WDT_TICKLE_STATUS_TIMEOUT :
pdc_wdt - > wdt_dev . bootstatus | = WDIOF_CARDRESET ;
2019-04-08 22:38:42 +03:00
dev_info ( dev , " watchdog module last reset due to timeout \n " ) ;
2015-01-06 16:19:34 +03:00
break ;
case PDC_WDT_TICKLE_STATUS_HRESET :
2019-04-08 22:38:42 +03:00
dev_info ( dev ,
2015-01-06 16:19:34 +03:00
" watchdog module last reset due to hard reset \n " ) ;
break ;
case PDC_WDT_TICKLE_STATUS_SRESET :
2019-04-08 22:38:42 +03:00
dev_info ( dev ,
2015-01-06 16:19:34 +03:00
" watchdog module last reset due to soft reset \n " ) ;
break ;
case PDC_WDT_TICKLE_STATUS_USER :
2019-04-08 22:38:42 +03:00
dev_info ( dev ,
2015-01-06 16:19:34 +03:00
" watchdog module last reset due to user reset \n " ) ;
break ;
default :
2019-04-08 22:38:42 +03:00
dev_info ( dev , " contains an illegal status code (%08x) \n " , val ) ;
2015-01-06 16:19:34 +03:00
break ;
}
watchdog_set_nowayout ( & pdc_wdt - > wdt_dev , nowayout ) ;
2015-11-16 20:28:03 +03:00
watchdog_set_restart_priority ( & pdc_wdt - > wdt_dev , 128 ) ;
2015-01-06 16:19:34 +03:00
platform_set_drvdata ( pdev , pdc_wdt ) ;
2019-04-08 22:38:42 +03:00
watchdog_stop_on_reboot ( & pdc_wdt - > wdt_dev ) ;
watchdog_stop_on_unregister ( & pdc_wdt - > wdt_dev ) ;
return devm_watchdog_register_device ( dev , & pdc_wdt - > wdt_dev ) ;
2015-01-06 16:19:34 +03:00
}
static const struct of_device_id pdc_wdt_match [ ] = {
{ . compatible = " img,pdc-wdt " } ,
{ }
} ;
MODULE_DEVICE_TABLE ( of , pdc_wdt_match ) ;
static struct platform_driver pdc_wdt_driver = {
. driver = {
. name = " imgpdc-wdt " ,
. of_match_table = pdc_wdt_match ,
} ,
. probe = pdc_wdt_probe ,
} ;
module_platform_driver ( pdc_wdt_driver ) ;
MODULE_AUTHOR ( " Jude Abraham <Jude.Abraham@imgtec.com> " ) ;
MODULE_AUTHOR ( " Naidu Tellapati <Naidu.Tellapati@imgtec.com> " ) ;
MODULE_DESCRIPTION ( " Imagination Technologies PDC Watchdog Timer Driver " ) ;
MODULE_LICENSE ( " GPL v2 " ) ;