Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 1 | /* |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 2 | * omap_wdt.c |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 3 | * |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 4 | * Watchdog driver for the TI OMAP 16xx & 24xx/34xx 32KHz (non-secure) watchdog |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 5 | * |
| 6 | * Author: MontaVista Software, Inc. |
| 7 | * <gdavis@mvista.com> or <source@mvista.com> |
| 8 | * |
| 9 | * 2003 (c) MontaVista Software, Inc. This file is licensed under the |
| 10 | * terms of the GNU General Public License version 2. This program is |
| 11 | * licensed "as is" without any warranty of any kind, whether express |
| 12 | * or implied. |
| 13 | * |
| 14 | * History: |
| 15 | * |
| 16 | * 20030527: George G. Davis <gdavis@mvista.com> |
| 17 | * Initially based on linux-2.4.19-rmk7-pxa1/drivers/char/sa1100_wdt.c |
| 18 | * (c) Copyright 2000 Oleg Drokin <green@crimea.edu> |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 19 | * Based on SoftDog driver by Alan Cox <alan@lxorguk.ukuu.org.uk> |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 20 | * |
| 21 | * Copyright (c) 2004 Texas Instruments. |
| 22 | * 1. Modified to support OMAP1610 32-KHz watchdog timer |
| 23 | * 2. Ported to 2.6 kernel |
| 24 | * |
| 25 | * Copyright (c) 2005 David Brownell |
| 26 | * Use the driver model and standard identifiers; handle bigger timeouts. |
| 27 | */ |
| 28 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 32 | #include <linux/types.h> |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/fs.h> |
| 35 | #include <linux/mm.h> |
| 36 | #include <linux/miscdevice.h> |
| 37 | #include <linux/watchdog.h> |
| 38 | #include <linux/reboot.h> |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 39 | #include <linux/init.h> |
| 40 | #include <linux/err.h> |
| 41 | #include <linux/platform_device.h> |
| 42 | #include <linux/moduleparam.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 43 | #include <linux/bitops.h> |
Wim Van Sebroeck | 089ab07 | 2008-07-15 11:46:11 +0000 | [diff] [blame] | 44 | #include <linux/io.h> |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 45 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
Varadarajan, Charulatha | 7ec5ad0 | 2010-09-23 20:02:43 +0530 | [diff] [blame] | 47 | #include <linux/pm_runtime.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 48 | #include <mach/hardware.h> |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 49 | #include <plat/cpu.h> |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 50 | #include <plat/prcm.h> |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 51 | |
| 52 | #include "omap_wdt.h" |
| 53 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 54 | static struct platform_device *omap_wdt_dev; |
| 55 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 56 | static unsigned timer_margin; |
| 57 | module_param(timer_margin, uint, 0); |
| 58 | MODULE_PARM_DESC(timer_margin, "initial watchdog timeout (in seconds)"); |
| 59 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 60 | static unsigned int wdt_trgr_pattern = 0x1234; |
Axel Lin | 1334f32 | 2011-11-29 13:54:01 +0800 | [diff] [blame] | 61 | static DEFINE_SPINLOCK(wdt_lock); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 62 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 63 | struct omap_wdt_dev { |
| 64 | void __iomem *base; /* physical */ |
| 65 | struct device *dev; |
| 66 | int omap_wdt_users; |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 67 | struct resource *mem; |
| 68 | struct miscdevice omap_wdt_miscdev; |
| 69 | }; |
| 70 | |
| 71 | static void omap_wdt_ping(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 72 | { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 73 | void __iomem *base = wdev->base; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 74 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 75 | /* wait for posted write to complete */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 76 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 77 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 78 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 79 | wdt_trgr_pattern = ~wdt_trgr_pattern; |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 80 | __raw_writel(wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR)); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 81 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 82 | /* wait for posted write to complete */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 83 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 84 | cpu_relax(); |
| 85 | /* reloaded WCRR from WLDR */ |
| 86 | } |
| 87 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 88 | static void omap_wdt_enable(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 89 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 90 | void __iomem *base = wdev->base; |
| 91 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 92 | /* Sequence to enable the watchdog */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 93 | __raw_writel(0xBBBB, base + OMAP_WATCHDOG_SPR); |
| 94 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 95 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 96 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 97 | __raw_writel(0x4444, base + OMAP_WATCHDOG_SPR); |
| 98 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 99 | cpu_relax(); |
| 100 | } |
| 101 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 102 | static void omap_wdt_disable(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 103 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 104 | void __iomem *base = wdev->base; |
| 105 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 106 | /* sequence required to disable watchdog */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 107 | __raw_writel(0xAAAA, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */ |
| 108 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 109 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 110 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 111 | __raw_writel(0x5555, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */ |
| 112 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 113 | cpu_relax(); |
| 114 | } |
| 115 | |
| 116 | static void omap_wdt_adjust_timeout(unsigned new_timeout) |
| 117 | { |
| 118 | if (new_timeout < TIMER_MARGIN_MIN) |
| 119 | new_timeout = TIMER_MARGIN_DEFAULT; |
| 120 | if (new_timeout > TIMER_MARGIN_MAX) |
| 121 | new_timeout = TIMER_MARGIN_MAX; |
| 122 | timer_margin = new_timeout; |
| 123 | } |
| 124 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 125 | static void omap_wdt_set_timeout(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 126 | { |
| 127 | u32 pre_margin = GET_WLDR_VAL(timer_margin); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 128 | void __iomem *base = wdev->base; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 129 | |
| 130 | /* just count up at 32 KHz */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 131 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 132 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 133 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 134 | __raw_writel(pre_margin, base + OMAP_WATCHDOG_LDR); |
| 135 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 136 | cpu_relax(); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Allow only one task to hold it open |
| 141 | */ |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 142 | static int omap_wdt_open(struct inode *inode, struct file *file) |
| 143 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 144 | struct omap_wdt_dev *wdev = platform_get_drvdata(omap_wdt_dev); |
| 145 | void __iomem *base = wdev->base; |
| 146 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 147 | if (test_and_set_bit(1, (unsigned long *)&(wdev->omap_wdt_users))) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 148 | return -EBUSY; |
| 149 | |
Varadarajan, Charulatha | 7ec5ad0 | 2010-09-23 20:02:43 +0530 | [diff] [blame] | 150 | pm_runtime_get_sync(wdev->dev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 151 | |
| 152 | /* initialize prescaler */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 153 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 154 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 155 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 156 | __raw_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL); |
| 157 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 158 | cpu_relax(); |
| 159 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 160 | file->private_data = (void *) wdev; |
| 161 | |
| 162 | omap_wdt_set_timeout(wdev); |
Ulrik Bech Hald | 789cd47 | 2009-06-12 16:18:32 -0500 | [diff] [blame] | 163 | omap_wdt_ping(wdev); /* trigger loading of new timeout value */ |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 164 | omap_wdt_enable(wdev); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 165 | |
Wim Van Sebroeck | ec9505a | 2007-07-20 20:41:37 +0000 | [diff] [blame] | 166 | return nonseekable_open(inode, file); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static int omap_wdt_release(struct inode *inode, struct file *file) |
| 170 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 171 | struct omap_wdt_dev *wdev = file->private_data; |
| 172 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 173 | /* |
| 174 | * Shut off the timer unless NOWAYOUT is defined. |
| 175 | */ |
| 176 | #ifndef CONFIG_WATCHDOG_NOWAYOUT |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 177 | omap_wdt_disable(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 178 | |
Varadarajan, Charulatha | 7ec5ad0 | 2010-09-23 20:02:43 +0530 | [diff] [blame] | 179 | pm_runtime_put_sync(wdev->dev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 180 | #else |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 181 | pr_crit("Unexpected close, not stopping!\n"); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 182 | #endif |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 183 | wdev->omap_wdt_users = 0; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 184 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 188 | static ssize_t omap_wdt_write(struct file *file, const char __user *data, |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 189 | size_t len, loff_t *ppos) |
| 190 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 191 | struct omap_wdt_dev *wdev = file->private_data; |
| 192 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 193 | /* Refresh LOAD_TIME. */ |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 194 | if (len) { |
| 195 | spin_lock(&wdt_lock); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 196 | omap_wdt_ping(wdev); |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 197 | spin_unlock(&wdt_lock); |
| 198 | } |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 199 | return len; |
| 200 | } |
| 201 | |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 202 | static long omap_wdt_ioctl(struct file *file, unsigned int cmd, |
| 203 | unsigned long arg) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 204 | { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 205 | struct omap_wdt_dev *wdev; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 206 | int new_margin; |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 207 | static const struct watchdog_info ident = { |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 208 | .identity = "OMAP Watchdog", |
| 209 | .options = WDIOF_SETTIMEOUT, |
| 210 | .firmware_version = 0, |
| 211 | }; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 212 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 213 | wdev = file->private_data; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 214 | |
| 215 | switch (cmd) { |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 216 | case WDIOC_GETSUPPORT: |
| 217 | return copy_to_user((struct watchdog_info __user *)arg, &ident, |
| 218 | sizeof(ident)); |
| 219 | case WDIOC_GETSTATUS: |
| 220 | return put_user(0, (int __user *)arg); |
| 221 | case WDIOC_GETBOOTSTATUS: |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 222 | #ifdef CONFIG_ARCH_OMAP1 |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 223 | if (cpu_is_omap16xx()) |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 224 | return put_user(__raw_readw(ARM_SYSST), |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 225 | (int __user *)arg); |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 226 | #endif |
| 227 | #ifdef CONFIG_ARCH_OMAP2PLUS |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 228 | if (cpu_is_omap24xx()) |
| 229 | return put_user(omap_prcm_get_reset_sources(), |
| 230 | (int __user *)arg); |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 231 | #endif |
Shubhrajyoti D | e2bf7c4 | 2012-01-04 19:45:28 +0530 | [diff] [blame] | 232 | return put_user(0, (int __user *)arg); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 233 | case WDIOC_KEEPALIVE: |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 234 | spin_lock(&wdt_lock); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 235 | omap_wdt_ping(wdev); |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 236 | spin_unlock(&wdt_lock); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | case WDIOC_SETTIMEOUT: |
| 239 | if (get_user(new_margin, (int __user *)arg)) |
| 240 | return -EFAULT; |
| 241 | omap_wdt_adjust_timeout(new_margin); |
| 242 | |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 243 | spin_lock(&wdt_lock); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 244 | omap_wdt_disable(wdev); |
| 245 | omap_wdt_set_timeout(wdev); |
| 246 | omap_wdt_enable(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 247 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 248 | omap_wdt_ping(wdev); |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 249 | spin_unlock(&wdt_lock); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 250 | /* Fall */ |
| 251 | case WDIOC_GETTIMEOUT: |
| 252 | return put_user(timer_margin, (int __user *)arg); |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 253 | default: |
| 254 | return -ENOTTY; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 258 | static const struct file_operations omap_wdt_fops = { |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 259 | .owner = THIS_MODULE, |
| 260 | .write = omap_wdt_write, |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 261 | .unlocked_ioctl = omap_wdt_ioctl, |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 262 | .open = omap_wdt_open, |
| 263 | .release = omap_wdt_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 264 | .llseek = no_llseek, |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
Uwe Kleine-König | 0e3912c | 2009-03-28 00:26:56 +0100 | [diff] [blame] | 267 | static int __devinit omap_wdt_probe(struct platform_device *pdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 268 | { |
| 269 | struct resource *res, *mem; |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 270 | struct omap_wdt_dev *wdev; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 271 | int ret; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 272 | |
| 273 | /* reserve static register mappings */ |
| 274 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 275 | if (!res) { |
| 276 | ret = -ENOENT; |
| 277 | goto err_get_resource; |
| 278 | } |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 279 | |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 280 | if (omap_wdt_dev) { |
| 281 | ret = -EBUSY; |
| 282 | goto err_busy; |
| 283 | } |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 284 | |
H Hartley Sweeten | b782a56 | 2009-12-04 12:24:04 -0500 | [diff] [blame] | 285 | mem = request_mem_region(res->start, resource_size(res), pdev->name); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 286 | if (!mem) { |
| 287 | ret = -EBUSY; |
| 288 | goto err_busy; |
| 289 | } |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 290 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 291 | wdev = kzalloc(sizeof(struct omap_wdt_dev), GFP_KERNEL); |
| 292 | if (!wdev) { |
| 293 | ret = -ENOMEM; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 294 | goto err_kzalloc; |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 295 | } |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 296 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 297 | wdev->omap_wdt_users = 0; |
| 298 | wdev->mem = mem; |
Varadarajan, Charulatha | 7ec5ad0 | 2010-09-23 20:02:43 +0530 | [diff] [blame] | 299 | wdev->dev = &pdev->dev; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 300 | |
H Hartley Sweeten | b782a56 | 2009-12-04 12:24:04 -0500 | [diff] [blame] | 301 | wdev->base = ioremap(res->start, resource_size(res)); |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 302 | if (!wdev->base) { |
| 303 | ret = -ENOMEM; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 304 | goto err_ioremap; |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 305 | } |
| 306 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 307 | platform_set_drvdata(pdev, wdev); |
| 308 | |
Varadarajan, Charulatha | 7ec5ad0 | 2010-09-23 20:02:43 +0530 | [diff] [blame] | 309 | pm_runtime_enable(wdev->dev); |
| 310 | pm_runtime_get_sync(wdev->dev); |
Ulrik Bech Hald | 789cd47 | 2009-06-12 16:18:32 -0500 | [diff] [blame] | 311 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 312 | omap_wdt_disable(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 313 | omap_wdt_adjust_timeout(timer_margin); |
| 314 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 315 | wdev->omap_wdt_miscdev.parent = &pdev->dev; |
| 316 | wdev->omap_wdt_miscdev.minor = WATCHDOG_MINOR; |
| 317 | wdev->omap_wdt_miscdev.name = "watchdog"; |
| 318 | wdev->omap_wdt_miscdev.fops = &omap_wdt_fops; |
| 319 | |
| 320 | ret = misc_register(&(wdev->omap_wdt_miscdev)); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 321 | if (ret) |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 322 | goto err_misc; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 323 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 324 | pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n", |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 325 | __raw_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF, |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 326 | timer_margin); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 327 | |
Varadarajan, Charulatha | 7ec5ad0 | 2010-09-23 20:02:43 +0530 | [diff] [blame] | 328 | pm_runtime_put_sync(wdev->dev); |
Ulrik Bech Hald | 789cd47 | 2009-06-12 16:18:32 -0500 | [diff] [blame] | 329 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 330 | omap_wdt_dev = pdev; |
| 331 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 332 | return 0; |
| 333 | |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 334 | err_misc: |
Shubhrajyoti D | 12c583d | 2012-01-11 19:50:18 +0530 | [diff] [blame] | 335 | pm_runtime_disable(wdev->dev); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 336 | platform_set_drvdata(pdev, NULL); |
| 337 | iounmap(wdev->base); |
| 338 | |
| 339 | err_ioremap: |
| 340 | wdev->base = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 341 | kfree(wdev); |
| 342 | |
| 343 | err_kzalloc: |
H Hartley Sweeten | b782a56 | 2009-12-04 12:24:04 -0500 | [diff] [blame] | 344 | release_mem_region(res->start, resource_size(res)); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 345 | |
| 346 | err_busy: |
| 347 | err_get_resource: |
| 348 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | static void omap_wdt_shutdown(struct platform_device *pdev) |
| 353 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 354 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 355 | |
Paul Walmsley | 0503add | 2011-03-10 22:40:05 -0700 | [diff] [blame] | 356 | if (wdev->omap_wdt_users) { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 357 | omap_wdt_disable(wdev); |
Paul Walmsley | 0503add | 2011-03-10 22:40:05 -0700 | [diff] [blame] | 358 | pm_runtime_put_sync(wdev->dev); |
| 359 | } |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Uwe Kleine-König | 0e3912c | 2009-03-28 00:26:56 +0100 | [diff] [blame] | 362 | static int __devexit omap_wdt_remove(struct platform_device *pdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 363 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 364 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 365 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 366 | |
Shubhrajyoti D | 12c583d | 2012-01-11 19:50:18 +0530 | [diff] [blame] | 367 | pm_runtime_disable(wdev->dev); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 368 | if (!res) |
| 369 | return -ENOENT; |
| 370 | |
| 371 | misc_deregister(&(wdev->omap_wdt_miscdev)); |
H Hartley Sweeten | b782a56 | 2009-12-04 12:24:04 -0500 | [diff] [blame] | 372 | release_mem_region(res->start, resource_size(res)); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 373 | platform_set_drvdata(pdev, NULL); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 374 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 375 | iounmap(wdev->base); |
| 376 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 377 | kfree(wdev); |
| 378 | omap_wdt_dev = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 379 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | #ifdef CONFIG_PM |
| 384 | |
| 385 | /* REVISIT ... not clear this is the best way to handle system suspend; and |
| 386 | * it's very inappropriate for selective device suspend (e.g. suspending this |
| 387 | * through sysfs rather than by stopping the watchdog daemon). Also, this |
| 388 | * may not play well enough with NOWAYOUT... |
| 389 | */ |
| 390 | |
| 391 | static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state) |
| 392 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 393 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
| 394 | |
Paul Walmsley | 0503add | 2011-03-10 22:40:05 -0700 | [diff] [blame] | 395 | if (wdev->omap_wdt_users) { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 396 | omap_wdt_disable(wdev); |
Paul Walmsley | 0503add | 2011-03-10 22:40:05 -0700 | [diff] [blame] | 397 | pm_runtime_put_sync(wdev->dev); |
| 398 | } |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 399 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static int omap_wdt_resume(struct platform_device *pdev) |
| 404 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 405 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
| 406 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 407 | if (wdev->omap_wdt_users) { |
Paul Walmsley | 0503add | 2011-03-10 22:40:05 -0700 | [diff] [blame] | 408 | pm_runtime_get_sync(wdev->dev); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 409 | omap_wdt_enable(wdev); |
| 410 | omap_wdt_ping(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 411 | } |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 412 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | #else |
| 417 | #define omap_wdt_suspend NULL |
| 418 | #define omap_wdt_resume NULL |
| 419 | #endif |
| 420 | |
Xiao Jiang | e6ca04e | 2012-06-01 12:44:16 +0800 | [diff] [blame] | 421 | static const struct of_device_id omap_wdt_of_match[] = { |
| 422 | { .compatible = "ti,omap3-wdt", }, |
| 423 | {}, |
| 424 | }; |
| 425 | MODULE_DEVICE_TABLE(of, omap_wdt_of_match); |
| 426 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 427 | static struct platform_driver omap_wdt_driver = { |
| 428 | .probe = omap_wdt_probe, |
Uwe Kleine-König | 0e3912c | 2009-03-28 00:26:56 +0100 | [diff] [blame] | 429 | .remove = __devexit_p(omap_wdt_remove), |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 430 | .shutdown = omap_wdt_shutdown, |
| 431 | .suspend = omap_wdt_suspend, |
| 432 | .resume = omap_wdt_resume, |
| 433 | .driver = { |
| 434 | .owner = THIS_MODULE, |
| 435 | .name = "omap_wdt", |
Xiao Jiang | e6ca04e | 2012-06-01 12:44:16 +0800 | [diff] [blame] | 436 | .of_match_table = omap_wdt_of_match, |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 437 | }, |
| 438 | }; |
| 439 | |
Axel Lin | b8ec611 | 2011-11-29 13:56:27 +0800 | [diff] [blame] | 440 | module_platform_driver(omap_wdt_driver); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 441 | |
| 442 | MODULE_AUTHOR("George G. Davis"); |
| 443 | MODULE_LICENSE("GPL"); |
| 444 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
Kay Sievers | f37d193 | 2008-04-10 21:29:23 -0700 | [diff] [blame] | 445 | MODULE_ALIAS("platform:omap_wdt"); |