linux/drivers/watchdog/bcm_kona_wdt.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2013 Broadcom Corporation
   3 *
   4 * This program is free software; you can redistribute it and/or
   5 * modify it under the terms of the GNU General Public License as
   6 * published by the Free Software Foundation version 2.
   7 *
   8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
   9 * kind, whether express or implied; without even the implied warranty
  10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11 * GNU General Public License for more details.
  12 */
  13
  14#include <linux/debugfs.h>
  15#include <linux/delay.h>
  16#include <linux/err.h>
  17#include <linux/io.h>
  18#include <linux/module.h>
  19#include <linux/of_address.h>
  20#include <linux/platform_device.h>
  21#include <linux/watchdog.h>
  22
  23#define SECWDOG_CTRL_REG                0x00000000
  24#define SECWDOG_COUNT_REG               0x00000004
  25
  26#define SECWDOG_RESERVED_MASK           0x1dffffff
  27#define SECWDOG_WD_LOAD_FLAG            0x10000000
  28#define SECWDOG_EN_MASK                 0x08000000
  29#define SECWDOG_SRSTEN_MASK             0x04000000
  30#define SECWDOG_RES_MASK                0x00f00000
  31#define SECWDOG_COUNT_MASK              0x000fffff
  32
  33#define SECWDOG_MAX_COUNT               SECWDOG_COUNT_MASK
  34#define SECWDOG_CLKS_SHIFT              20
  35#define SECWDOG_MAX_RES                 15
  36#define SECWDOG_DEFAULT_RESOLUTION      4
  37#define SECWDOG_MAX_TRY                 1000
  38
  39#define SECS_TO_TICKS(x, w)             ((x) << (w)->resolution)
  40#define TICKS_TO_SECS(x, w)             ((x) >> (w)->resolution)
  41
  42#define BCM_KONA_WDT_NAME               "bcm_kona_wdt"
  43
  44struct bcm_kona_wdt {
  45        void __iomem *base;
  46        /*
  47         * One watchdog tick is 1/(2^resolution) seconds. Resolution can take
  48         * the values 0-15, meaning one tick can be 1s to 30.52us. Our default
  49         * resolution of 4 means one tick is 62.5ms.
  50         *
  51         * The watchdog counter is 20 bits. Depending on resolution, the maximum
  52         * counter value of 0xfffff expires after about 12 days (resolution 0)
  53         * down to only 32s (resolution 15). The default resolution of 4 gives
  54         * us a maximum of about 18 hours and 12 minutes before the watchdog
  55         * times out.
  56         */
  57        int resolution;
  58        spinlock_t lock;
  59#ifdef CONFIG_BCM_KONA_WDT_DEBUG
  60        unsigned long busy_count;
  61        struct dentry *debugfs;
  62#endif
  63};
  64
  65static int secure_register_read(struct bcm_kona_wdt *wdt, uint32_t offset)
  66{
  67        uint32_t val;
  68        unsigned count = 0;
  69
  70        /*
  71         * If the WD_LOAD_FLAG is set, the watchdog counter field is being
  72         * updated in hardware. Once the WD timer is updated in hardware, it
  73         * gets cleared.
  74         */
  75        do {
  76                if (unlikely(count > 1))
  77                        udelay(5);
  78                val = readl_relaxed(wdt->base + offset);
  79                count++;
  80        } while ((val & SECWDOG_WD_LOAD_FLAG) && count < SECWDOG_MAX_TRY);
  81
  82#ifdef CONFIG_BCM_KONA_WDT_DEBUG
  83        /* Remember the maximum number iterations due to WD_LOAD_FLAG */
  84        if (count > wdt->busy_count)
  85                wdt->busy_count = count;
  86#endif
  87
  88        /* This is the only place we return a negative value. */
  89        if (val & SECWDOG_WD_LOAD_FLAG)
  90                return -ETIMEDOUT;
  91
  92        /* We always mask out reserved bits. */
  93        val &= SECWDOG_RESERVED_MASK;
  94
  95        return val;
  96}
  97
  98#ifdef CONFIG_BCM_KONA_WDT_DEBUG
  99
 100static int bcm_kona_wdt_dbg_show(struct seq_file *s, void *data)
 101{
 102        int ctl_val, cur_val;
 103        unsigned long flags;
 104        struct bcm_kona_wdt *wdt = s->private;
 105
 106        if (!wdt) {
 107                seq_puts(s, "No device pointer\n");
 108                return 0;
 109        }
 110
 111        spin_lock_irqsave(&wdt->lock, flags);
 112        ctl_val = secure_register_read(wdt, SECWDOG_CTRL_REG);
 113        cur_val = secure_register_read(wdt, SECWDOG_COUNT_REG);
 114        spin_unlock_irqrestore(&wdt->lock, flags);
 115
 116        if (ctl_val < 0 || cur_val < 0) {
 117                seq_puts(s, "Error accessing hardware\n");
 118        } else {
 119                int ctl, cur, ctl_sec, cur_sec, res;
 120
 121                ctl = ctl_val & SECWDOG_COUNT_MASK;
 122                res = (ctl_val & SECWDOG_RES_MASK) >> SECWDOG_CLKS_SHIFT;
 123                cur = cur_val & SECWDOG_COUNT_MASK;
 124                ctl_sec = TICKS_TO_SECS(ctl, wdt);
 125                cur_sec = TICKS_TO_SECS(cur, wdt);
 126                seq_printf(s,
 127                           "Resolution: %d / %d\n"
 128                           "Control: %d s / %d (%#x) ticks\n"
 129                           "Current: %d s / %d (%#x) ticks\n"
 130                           "Busy count: %lu\n",
 131                           res, wdt->resolution,
 132                           ctl_sec, ctl, ctl,
 133                           cur_sec, cur, cur,
 134                           wdt->busy_count);
 135        }
 136
 137        return 0;
 138}
 139
 140static int bcm_kona_dbg_open(struct inode *inode, struct file *file)
 141{
 142        return single_open(file, bcm_kona_wdt_dbg_show, inode->i_private);
 143}
 144
 145static const struct file_operations bcm_kona_dbg_operations = {
 146        .open           = bcm_kona_dbg_open,
 147        .read           = seq_read,
 148        .llseek         = seq_lseek,
 149        .release        = single_release,
 150};
 151
 152static void bcm_kona_wdt_debug_init(struct platform_device *pdev)
 153{
 154        struct dentry *dir;
 155        struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
 156
 157        if (!wdt)
 158                return;
 159
 160        wdt->debugfs = NULL;
 161
 162        dir = debugfs_create_dir(BCM_KONA_WDT_NAME, NULL);
 163        if (IS_ERR_OR_NULL(dir))
 164                return;
 165
 166        if (debugfs_create_file("info", S_IFREG | S_IRUGO, dir, wdt,
 167                                &bcm_kona_dbg_operations))
 168                wdt->debugfs = dir;
 169        else
 170                debugfs_remove_recursive(dir);
 171}
 172
 173static void bcm_kona_wdt_debug_exit(struct platform_device *pdev)
 174{
 175        struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
 176
 177        if (wdt && wdt->debugfs) {
 178                debugfs_remove_recursive(wdt->debugfs);
 179                wdt->debugfs = NULL;
 180        }
 181}
 182
 183#else
 184
 185static void bcm_kona_wdt_debug_init(struct platform_device *pdev) {}
 186static void bcm_kona_wdt_debug_exit(struct platform_device *pdev) {}
 187
 188#endif /* CONFIG_BCM_KONA_WDT_DEBUG */
 189
 190static int bcm_kona_wdt_ctrl_reg_modify(struct bcm_kona_wdt *wdt,
 191                                        unsigned mask, unsigned newval)
 192{
 193        int val;
 194        unsigned long flags;
 195        int ret = 0;
 196
 197        spin_lock_irqsave(&wdt->lock, flags);
 198
 199        val = secure_register_read(wdt, SECWDOG_CTRL_REG);
 200        if (val < 0) {
 201                ret = val;
 202        } else {
 203                val &= ~mask;
 204                val |= newval;
 205                writel_relaxed(val, wdt->base + SECWDOG_CTRL_REG);
 206        }
 207
 208        spin_unlock_irqrestore(&wdt->lock, flags);
 209
 210        return ret;
 211}
 212
 213static int bcm_kona_wdt_set_resolution_reg(struct bcm_kona_wdt *wdt)
 214{
 215        if (wdt->resolution > SECWDOG_MAX_RES)
 216                return -EINVAL;
 217
 218        return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_RES_MASK,
 219                                        wdt->resolution << SECWDOG_CLKS_SHIFT);
 220}
 221
 222static int bcm_kona_wdt_set_timeout_reg(struct watchdog_device *wdog,
 223                                        unsigned watchdog_flags)
 224{
 225        struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
 226
 227        return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_COUNT_MASK,
 228                                        SECS_TO_TICKS(wdog->timeout, wdt) |
 229                                        watchdog_flags);
 230}
 231
 232static int bcm_kona_wdt_set_timeout(struct watchdog_device *wdog,
 233        unsigned int t)
 234{
 235        wdog->timeout = t;
 236        return 0;
 237}
 238
 239static unsigned int bcm_kona_wdt_get_timeleft(struct watchdog_device *wdog)
 240{
 241        struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
 242        int val;
 243        unsigned long flags;
 244
 245        spin_lock_irqsave(&wdt->lock, flags);
 246        val = secure_register_read(wdt, SECWDOG_COUNT_REG);
 247        spin_unlock_irqrestore(&wdt->lock, flags);
 248
 249        if (val < 0)
 250                return val;
 251
 252        return TICKS_TO_SECS(val & SECWDOG_COUNT_MASK, wdt);
 253}
 254
 255static int bcm_kona_wdt_start(struct watchdog_device *wdog)
 256{
 257        return bcm_kona_wdt_set_timeout_reg(wdog,
 258                                        SECWDOG_EN_MASK | SECWDOG_SRSTEN_MASK);
 259}
 260
 261static int bcm_kona_wdt_stop(struct watchdog_device *wdog)
 262{
 263        struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
 264
 265        return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_EN_MASK |
 266                                            SECWDOG_SRSTEN_MASK, 0);
 267}
 268
 269static struct watchdog_ops bcm_kona_wdt_ops = {
 270        .owner =        THIS_MODULE,
 271        .start =        bcm_kona_wdt_start,
 272        .stop =         bcm_kona_wdt_stop,
 273        .set_timeout =  bcm_kona_wdt_set_timeout,
 274        .get_timeleft = bcm_kona_wdt_get_timeleft,
 275};
 276
 277static struct watchdog_info bcm_kona_wdt_info = {
 278        .options =      WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
 279                        WDIOF_KEEPALIVEPING,
 280        .identity =     "Broadcom Kona Watchdog Timer",
 281};
 282
 283static struct watchdog_device bcm_kona_wdt_wdd = {
 284        .info =         &bcm_kona_wdt_info,
 285        .ops =          &bcm_kona_wdt_ops,
 286        .min_timeout =  1,
 287        .max_timeout =  SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
 288        .timeout =      SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
 289};
 290
 291static void bcm_kona_wdt_shutdown(struct platform_device *pdev)
 292{
 293        bcm_kona_wdt_stop(&bcm_kona_wdt_wdd);
 294}
 295
 296static int bcm_kona_wdt_probe(struct platform_device *pdev)
 297{
 298        struct device *dev = &pdev->dev;
 299        struct bcm_kona_wdt *wdt;
 300        struct resource *res;
 301        int ret;
 302
 303        wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
 304        if (!wdt)
 305                return -ENOMEM;
 306
 307        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 308        wdt->base = devm_ioremap_resource(dev, res);
 309        if (IS_ERR(wdt->base))
 310                return -ENODEV;
 311
 312        wdt->resolution = SECWDOG_DEFAULT_RESOLUTION;
 313        ret = bcm_kona_wdt_set_resolution_reg(wdt);
 314        if (ret) {
 315                dev_err(dev, "Failed to set resolution (error: %d)", ret);
 316                return ret;
 317        }
 318
 319        spin_lock_init(&wdt->lock);
 320        platform_set_drvdata(pdev, wdt);
 321        watchdog_set_drvdata(&bcm_kona_wdt_wdd, wdt);
 322        bcm_kona_wdt_wdd.parent = &pdev->dev;
 323
 324        ret = bcm_kona_wdt_set_timeout_reg(&bcm_kona_wdt_wdd, 0);
 325        if (ret) {
 326                dev_err(dev, "Failed set watchdog timeout");
 327                return ret;
 328        }
 329
 330        ret = watchdog_register_device(&bcm_kona_wdt_wdd);
 331        if (ret) {
 332                dev_err(dev, "Failed to register watchdog device");
 333                return ret;
 334        }
 335
 336        bcm_kona_wdt_debug_init(pdev);
 337        dev_dbg(dev, "Broadcom Kona Watchdog Timer");
 338
 339        return 0;
 340}
 341
 342static int bcm_kona_wdt_remove(struct platform_device *pdev)
 343{
 344        bcm_kona_wdt_debug_exit(pdev);
 345        bcm_kona_wdt_shutdown(pdev);
 346        watchdog_unregister_device(&bcm_kona_wdt_wdd);
 347        dev_dbg(&pdev->dev, "Watchdog driver disabled");
 348
 349        return 0;
 350}
 351
 352static const struct of_device_id bcm_kona_wdt_of_match[] = {
 353        { .compatible = "brcm,kona-wdt", },
 354        {},
 355};
 356MODULE_DEVICE_TABLE(of, bcm_kona_wdt_of_match);
 357
 358static struct platform_driver bcm_kona_wdt_driver = {
 359        .driver = {
 360                        .name = BCM_KONA_WDT_NAME,
 361                        .of_match_table = bcm_kona_wdt_of_match,
 362                  },
 363        .probe = bcm_kona_wdt_probe,
 364        .remove = bcm_kona_wdt_remove,
 365        .shutdown = bcm_kona_wdt_shutdown,
 366};
 367
 368module_platform_driver(bcm_kona_wdt_driver);
 369
 370MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME);
 371MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
 372MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
 373MODULE_LICENSE("GPL v2");
 374
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.