2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2017-01-29 01:59:37 +03:00
/*
2017-10-16 23:54:24 +03:00
* Watchdog driver for Faraday Technology FTWDT010
2017-01-29 01:59:37 +03:00
*
* Copyright ( C ) 2017 Linus Walleij < linus . walleij @ linaro . org >
*
* Inspired by the out - of - tree drivers from OpenWRT :
* Copyright ( C ) 2009 Paulius Zaleckas < paulius . zaleckas @ teltonika . lt >
*/
# include <linux/bitops.h>
# include <linux/init.h>
# include <linux/interrupt.h>
# include <linux/io.h>
# include <linux/kernel.h>
# include <linux/module.h>
2023-07-27 02:33:00 +03:00
# include <linux/mod_devicetable.h>
2017-01-29 01:59:37 +03:00
# include <linux/platform_device.h>
# include <linux/slab.h>
# include <linux/watchdog.h>
2017-10-16 23:54:24 +03:00
# define FTWDT010_WDCOUNTER 0x0
# define FTWDT010_WDLOAD 0x4
# define FTWDT010_WDRESTART 0x8
# define FTWDT010_WDCR 0xC
2017-01-29 01:59:37 +03:00
# define WDRESTART_MAGIC 0x5AB9
# define WDCR_CLOCK_5MHZ BIT(4)
2017-10-16 23:54:25 +03:00
# define WDCR_WDEXT BIT(3)
# define WDCR_WDINTR BIT(2)
2017-01-29 01:59:37 +03:00
# define WDCR_SYS_RST BIT(1)
# define WDCR_ENABLE BIT(0)
# define WDT_CLOCK 5000000 /* 5 MHz */
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt {
2017-01-29 01:59:37 +03:00
struct watchdog_device wdd ;
struct device * dev ;
void __iomem * base ;
2017-10-16 23:54:25 +03:00
bool has_irq ;
2017-01-29 01:59:37 +03:00
} ;
static inline
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt * to_ftwdt010_wdt ( struct watchdog_device * wdd )
2017-01-29 01:59:37 +03:00
{
2017-10-16 23:54:24 +03:00
return container_of ( wdd , struct ftwdt010_wdt , wdd ) ;
2017-01-29 01:59:37 +03:00
}
2022-08-29 12:04:36 +03:00
static void ftwdt010_enable ( struct ftwdt010_wdt * gwdt ,
unsigned int timeout ,
bool need_irq )
2017-01-29 01:59:37 +03:00
{
2017-10-16 23:54:25 +03:00
u32 enable ;
2017-01-29 01:59:37 +03:00
2022-08-29 12:04:36 +03:00
writel ( timeout * WDT_CLOCK , gwdt - > base + FTWDT010_WDLOAD ) ;
2017-10-16 23:54:24 +03:00
writel ( WDRESTART_MAGIC , gwdt - > base + FTWDT010_WDRESTART ) ;
2017-01-29 01:59:37 +03:00
/* set clock before enabling */
2017-10-16 23:54:25 +03:00
enable = WDCR_CLOCK_5MHZ | WDCR_SYS_RST ;
writel ( enable , gwdt - > base + FTWDT010_WDCR ) ;
2022-08-29 12:04:36 +03:00
if ( need_irq )
2017-10-16 23:54:25 +03:00
enable | = WDCR_WDINTR ;
enable | = WDCR_ENABLE ;
writel ( enable , gwdt - > base + FTWDT010_WDCR ) ;
2022-08-29 12:04:36 +03:00
}
2017-01-29 01:59:37 +03:00
2022-08-29 12:04:36 +03:00
static int ftwdt010_wdt_start ( struct watchdog_device * wdd )
{
struct ftwdt010_wdt * gwdt = to_ftwdt010_wdt ( wdd ) ;
ftwdt010_enable ( gwdt , wdd - > timeout , gwdt - > has_irq ) ;
2017-01-29 01:59:37 +03:00
return 0 ;
}
2017-10-16 23:54:24 +03:00
static int ftwdt010_wdt_stop ( struct watchdog_device * wdd )
2017-01-29 01:59:37 +03:00
{
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt * gwdt = to_ftwdt010_wdt ( wdd ) ;
2017-01-29 01:59:37 +03:00
2017-10-16 23:54:24 +03:00
writel ( 0 , gwdt - > base + FTWDT010_WDCR ) ;
2017-01-29 01:59:37 +03:00
return 0 ;
}
2017-10-16 23:54:24 +03:00
static int ftwdt010_wdt_ping ( struct watchdog_device * wdd )
2017-01-29 01:59:37 +03:00
{
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt * gwdt = to_ftwdt010_wdt ( wdd ) ;
2017-01-29 01:59:37 +03:00
2017-10-16 23:54:24 +03:00
writel ( WDRESTART_MAGIC , gwdt - > base + FTWDT010_WDRESTART ) ;
2017-01-29 01:59:37 +03:00
return 0 ;
}
2017-10-16 23:54:24 +03:00
static int ftwdt010_wdt_set_timeout ( struct watchdog_device * wdd ,
2017-01-29 01:59:37 +03:00
unsigned int timeout )
{
wdd - > timeout = timeout ;
if ( watchdog_active ( wdd ) )
2017-10-16 23:54:24 +03:00
ftwdt010_wdt_start ( wdd ) ;
2017-01-29 01:59:37 +03:00
return 0 ;
}
2022-08-29 12:04:36 +03:00
static int ftwdt010_wdt_restart ( struct watchdog_device * wdd ,
unsigned long action , void * data )
{
ftwdt010_enable ( to_ftwdt010_wdt ( wdd ) , 0 , false ) ;
return 0 ;
}
2017-10-16 23:54:24 +03:00
static irqreturn_t ftwdt010_wdt_interrupt ( int irq , void * data )
2017-01-29 01:59:37 +03:00
{
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt * gwdt = data ;
2017-01-29 01:59:37 +03:00
watchdog_notify_pretimeout ( & gwdt - > wdd ) ;
return IRQ_HANDLED ;
}
2017-10-16 23:54:24 +03:00
static const struct watchdog_ops ftwdt010_wdt_ops = {
. start = ftwdt010_wdt_start ,
. stop = ftwdt010_wdt_stop ,
. ping = ftwdt010_wdt_ping ,
. set_timeout = ftwdt010_wdt_set_timeout ,
2022-08-29 12:04:36 +03:00
. restart = ftwdt010_wdt_restart ,
2017-01-29 01:59:37 +03:00
. owner = THIS_MODULE ,
} ;
2017-10-16 23:54:24 +03:00
static const struct watchdog_info ftwdt010_wdt_info = {
2017-01-29 01:59:37 +03:00
. options = WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
| WDIOF_SETTIMEOUT ,
. identity = KBUILD_MODNAME ,
} ;
2017-10-16 23:54:24 +03:00
static int ftwdt010_wdt_probe ( struct platform_device * pdev )
2017-01-29 01:59:37 +03:00
{
struct device * dev = & pdev - > dev ;
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt * gwdt ;
2017-01-29 01:59:37 +03:00
unsigned int reg ;
int irq ;
int ret ;
gwdt = devm_kzalloc ( dev , sizeof ( * gwdt ) , GFP_KERNEL ) ;
if ( ! gwdt )
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
gwdt - > base = devm_platform_ioremap_resource ( pdev , 0 ) ;
2017-01-29 01:59:37 +03:00
if ( IS_ERR ( gwdt - > base ) )
return PTR_ERR ( gwdt - > base ) ;
gwdt - > dev = dev ;
2017-10-16 23:54:24 +03:00
gwdt - > wdd . info = & ftwdt010_wdt_info ;
gwdt - > wdd . ops = & ftwdt010_wdt_ops ;
2017-01-29 01:59:37 +03:00
gwdt - > wdd . min_timeout = 1 ;
gwdt - > wdd . max_timeout = 0xFFFFFFFF / WDT_CLOCK ;
gwdt - > wdd . parent = dev ;
/*
* If ' timeout - sec ' unspecified in devicetree , assume a 13 second
* default .
*/
gwdt - > wdd . timeout = 13U ;
watchdog_init_timeout ( & gwdt - > wdd , 0 , dev ) ;
2017-10-16 23:54:24 +03:00
reg = readw ( gwdt - > base + FTWDT010_WDCR ) ;
2017-01-29 01:59:37 +03:00
if ( reg & WDCR_ENABLE ) {
/* Watchdog was enabled by the bootloader, disable it. */
reg & = ~ WDCR_ENABLE ;
2017-10-16 23:54:24 +03:00
writel ( reg , gwdt - > base + FTWDT010_WDCR ) ;
2017-01-29 01:59:37 +03:00
}
2017-10-16 23:54:25 +03:00
irq = platform_get_irq ( pdev , 0 ) ;
2022-08-11 13:56:06 +03:00
if ( irq > 0 ) {
2017-10-16 23:54:25 +03:00
ret = devm_request_irq ( dev , irq , ftwdt010_wdt_interrupt , 0 ,
" watchdog bark " , gwdt ) ;
if ( ret )
return ret ;
gwdt - > has_irq = true ;
}
2017-01-29 01:59:37 +03:00
ret = devm_watchdog_register_device ( dev , & gwdt - > wdd ) ;
2019-05-19 00:27:27 +03:00
if ( ret )
2017-01-29 01:59:37 +03:00
return ret ;
/* Set up platform driver data */
platform_set_drvdata ( pdev , gwdt ) ;
2017-10-16 23:54:24 +03:00
dev_info ( dev , " FTWDT010 watchdog driver enabled \n " ) ;
2017-01-29 01:59:37 +03:00
return 0 ;
}
2017-10-16 23:54:24 +03:00
static int __maybe_unused ftwdt010_wdt_suspend ( struct device * dev )
2017-01-29 01:59:37 +03:00
{
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt * gwdt = dev_get_drvdata ( dev ) ;
2017-01-29 01:59:37 +03:00
unsigned int reg ;
2017-10-16 23:54:24 +03:00
reg = readw ( gwdt - > base + FTWDT010_WDCR ) ;
2017-01-29 01:59:37 +03:00
reg & = ~ WDCR_ENABLE ;
2017-10-16 23:54:24 +03:00
writel ( reg , gwdt - > base + FTWDT010_WDCR ) ;
2017-01-29 01:59:37 +03:00
return 0 ;
}
2017-10-16 23:54:24 +03:00
static int __maybe_unused ftwdt010_wdt_resume ( struct device * dev )
2017-01-29 01:59:37 +03:00
{
2017-10-16 23:54:24 +03:00
struct ftwdt010_wdt * gwdt = dev_get_drvdata ( dev ) ;
2017-01-29 01:59:37 +03:00
unsigned int reg ;
if ( watchdog_active ( & gwdt - > wdd ) ) {
2017-10-16 23:54:24 +03:00
reg = readw ( gwdt - > base + FTWDT010_WDCR ) ;
2017-01-29 01:59:37 +03:00
reg | = WDCR_ENABLE ;
2017-10-16 23:54:24 +03:00
writel ( reg , gwdt - > base + FTWDT010_WDCR ) ;
2017-01-29 01:59:37 +03:00
}
return 0 ;
}
2017-10-16 23:54:24 +03:00
static const struct dev_pm_ops ftwdt010_wdt_dev_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS ( ftwdt010_wdt_suspend ,
ftwdt010_wdt_resume )
2017-01-29 01:59:37 +03:00
} ;
2017-10-16 23:54:24 +03:00
static const struct of_device_id ftwdt010_wdt_match [ ] = {
{ . compatible = " faraday,ftwdt010 " } ,
2017-01-29 01:59:37 +03:00
{ . compatible = " cortina,gemini-watchdog " } ,
{ } ,
} ;
2017-10-16 23:54:24 +03:00
MODULE_DEVICE_TABLE ( of , ftwdt010_wdt_match ) ;
2017-01-29 01:59:37 +03:00
2017-10-16 23:54:24 +03:00
static struct platform_driver ftwdt010_wdt_driver = {
. probe = ftwdt010_wdt_probe ,
2017-01-29 01:59:37 +03:00
. driver = {
2017-10-16 23:54:24 +03:00
. name = " ftwdt010-wdt " ,
2023-07-27 02:33:00 +03:00
. of_match_table = ftwdt010_wdt_match ,
2017-10-16 23:54:24 +03:00
. pm = & ftwdt010_wdt_dev_pm_ops ,
2017-01-29 01:59:37 +03:00
} ,
} ;
2017-10-16 23:54:24 +03:00
module_platform_driver ( ftwdt010_wdt_driver ) ;
2017-01-29 01:59:37 +03:00
MODULE_AUTHOR ( " Linus Walleij " ) ;
2017-10-16 23:54:24 +03:00
MODULE_DESCRIPTION ( " Watchdog driver for Faraday Technology FTWDT010 " ) ;
2017-01-29 01:59:37 +03:00
MODULE_LICENSE ( " GPL " ) ;