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> |
| 19 | * Based on SoftDog driver by Alan Cox <alan@redhat.com> |
| 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 | |
| 29 | #include <linux/module.h> |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/miscdevice.h> |
| 35 | #include <linux/watchdog.h> |
| 36 | #include <linux/reboot.h> |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 37 | #include <linux/init.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/platform_device.h> |
| 40 | #include <linux/moduleparam.h> |
| 41 | #include <linux/clk.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 42 | #include <linux/bitops.h> |
Wim Van Sebroeck | 089ab07 | 2008-07-15 11:46:11 +0000 | [diff] [blame] | 43 | #include <linux/io.h> |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 44 | #include <linux/uaccess.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 45 | #include <mach/hardware.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 46 | #include <mach/prcm.h> |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 47 | |
| 48 | #include "omap_wdt.h" |
| 49 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 50 | static struct platform_device *omap_wdt_dev; |
| 51 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 52 | static unsigned timer_margin; |
| 53 | module_param(timer_margin, uint, 0); |
| 54 | MODULE_PARM_DESC(timer_margin, "initial watchdog timeout (in seconds)"); |
| 55 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 56 | static unsigned int wdt_trgr_pattern = 0x1234; |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 57 | static spinlock_t wdt_lock; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 58 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 59 | struct omap_wdt_dev { |
| 60 | void __iomem *base; /* physical */ |
| 61 | struct device *dev; |
| 62 | int omap_wdt_users; |
| 63 | struct clk *armwdt_ck; |
| 64 | struct clk *mpu_wdt_ick; |
| 65 | struct clk *mpu_wdt_fck; |
| 66 | struct resource *mem; |
| 67 | struct miscdevice omap_wdt_miscdev; |
| 68 | }; |
| 69 | |
| 70 | static void omap_wdt_ping(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 71 | { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 72 | void __iomem *base = wdev->base; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 73 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 74 | /* wait for posted write to complete */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 75 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 76 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 77 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 78 | wdt_trgr_pattern = ~wdt_trgr_pattern; |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 79 | __raw_writel(wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR)); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 80 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 81 | /* wait for posted write to complete */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 82 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 83 | cpu_relax(); |
| 84 | /* reloaded WCRR from WLDR */ |
| 85 | } |
| 86 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 87 | static void omap_wdt_enable(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 88 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 89 | void __iomem *base = wdev->base; |
| 90 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 91 | /* Sequence to enable the watchdog */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 92 | __raw_writel(0xBBBB, base + OMAP_WATCHDOG_SPR); |
| 93 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 94 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 95 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 96 | __raw_writel(0x4444, base + OMAP_WATCHDOG_SPR); |
| 97 | while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 98 | cpu_relax(); |
| 99 | } |
| 100 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 101 | static void omap_wdt_disable(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 102 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 103 | void __iomem *base = wdev->base; |
| 104 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 105 | /* sequence required to disable watchdog */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 106 | __raw_writel(0xAAAA, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */ |
| 107 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 108 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 109 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 110 | __raw_writel(0x5555, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */ |
| 111 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 112 | cpu_relax(); |
| 113 | } |
| 114 | |
| 115 | static void omap_wdt_adjust_timeout(unsigned new_timeout) |
| 116 | { |
| 117 | if (new_timeout < TIMER_MARGIN_MIN) |
| 118 | new_timeout = TIMER_MARGIN_DEFAULT; |
| 119 | if (new_timeout > TIMER_MARGIN_MAX) |
| 120 | new_timeout = TIMER_MARGIN_MAX; |
| 121 | timer_margin = new_timeout; |
| 122 | } |
| 123 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 124 | static void omap_wdt_set_timeout(struct omap_wdt_dev *wdev) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 125 | { |
| 126 | u32 pre_margin = GET_WLDR_VAL(timer_margin); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 127 | void __iomem *base = wdev->base; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 128 | |
| 129 | /* just count up at 32 KHz */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 130 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 131 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 132 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 133 | __raw_writel(pre_margin, base + OMAP_WATCHDOG_LDR); |
| 134 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 135 | cpu_relax(); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Allow only one task to hold it open |
| 140 | */ |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 141 | static int omap_wdt_open(struct inode *inode, struct file *file) |
| 142 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 143 | struct omap_wdt_dev *wdev = platform_get_drvdata(omap_wdt_dev); |
| 144 | void __iomem *base = wdev->base; |
| 145 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 146 | if (test_and_set_bit(1, (unsigned long *)&(wdev->omap_wdt_users))) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 147 | return -EBUSY; |
| 148 | |
| 149 | if (cpu_is_omap16xx()) |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 150 | clk_enable(wdev->armwdt_ck); /* Enable the clock */ |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 151 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 152 | if (cpu_is_omap24xx() || cpu_is_omap34xx()) { |
| 153 | clk_enable(wdev->mpu_wdt_ick); /* Enable the interface clock */ |
| 154 | clk_enable(wdev->mpu_wdt_fck); /* Enable the functional clock */ |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* initialize prescaler */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 158 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 159 | cpu_relax(); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 160 | |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 161 | __raw_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL); |
| 162 | while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 163 | cpu_relax(); |
| 164 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 165 | file->private_data = (void *) wdev; |
| 166 | |
| 167 | omap_wdt_set_timeout(wdev); |
| 168 | omap_wdt_enable(wdev); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 169 | |
Wim Van Sebroeck | ec9505a | 2007-07-20 20:41:37 +0000 | [diff] [blame] | 170 | return nonseekable_open(inode, file); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static int omap_wdt_release(struct inode *inode, struct file *file) |
| 174 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 175 | struct omap_wdt_dev *wdev = file->private_data; |
| 176 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 177 | /* |
| 178 | * Shut off the timer unless NOWAYOUT is defined. |
| 179 | */ |
| 180 | #ifndef CONFIG_WATCHDOG_NOWAYOUT |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 181 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 182 | omap_wdt_disable(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 183 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 184 | if (cpu_is_omap16xx()) |
| 185 | clk_disable(wdev->armwdt_ck); /* Disable the clock */ |
| 186 | |
| 187 | if (cpu_is_omap24xx() || cpu_is_omap34xx()) { |
| 188 | clk_disable(wdev->mpu_wdt_ick); /* Disable the clock */ |
| 189 | clk_disable(wdev->mpu_wdt_fck); /* Disable the clock */ |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 190 | } |
| 191 | #else |
| 192 | printk(KERN_CRIT "omap_wdt: Unexpected close, not stopping!\n"); |
| 193 | #endif |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 194 | wdev->omap_wdt_users = 0; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 195 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 199 | 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] | 200 | size_t len, loff_t *ppos) |
| 201 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 202 | struct omap_wdt_dev *wdev = file->private_data; |
| 203 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 204 | /* Refresh LOAD_TIME. */ |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 205 | if (len) { |
| 206 | spin_lock(&wdt_lock); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 207 | omap_wdt_ping(wdev); |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 208 | spin_unlock(&wdt_lock); |
| 209 | } |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 210 | return len; |
| 211 | } |
| 212 | |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 213 | static long omap_wdt_ioctl(struct file *file, unsigned int cmd, |
| 214 | unsigned long arg) |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 215 | { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 216 | struct omap_wdt_dev *wdev; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 217 | int new_margin; |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 218 | static const struct watchdog_info ident = { |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 219 | .identity = "OMAP Watchdog", |
| 220 | .options = WDIOF_SETTIMEOUT, |
| 221 | .firmware_version = 0, |
| 222 | }; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 223 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 224 | wdev = file->private_data; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 225 | |
| 226 | switch (cmd) { |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 227 | case WDIOC_GETSUPPORT: |
| 228 | return copy_to_user((struct watchdog_info __user *)arg, &ident, |
| 229 | sizeof(ident)); |
| 230 | case WDIOC_GETSTATUS: |
| 231 | return put_user(0, (int __user *)arg); |
| 232 | case WDIOC_GETBOOTSTATUS: |
| 233 | if (cpu_is_omap16xx()) |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 234 | return put_user(__raw_readw(ARM_SYSST), |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 235 | (int __user *)arg); |
| 236 | if (cpu_is_omap24xx()) |
| 237 | return put_user(omap_prcm_get_reset_sources(), |
| 238 | (int __user *)arg); |
| 239 | case WDIOC_KEEPALIVE: |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 240 | spin_lock(&wdt_lock); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 241 | omap_wdt_ping(wdev); |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 242 | spin_unlock(&wdt_lock); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 243 | return 0; |
| 244 | case WDIOC_SETTIMEOUT: |
| 245 | if (get_user(new_margin, (int __user *)arg)) |
| 246 | return -EFAULT; |
| 247 | omap_wdt_adjust_timeout(new_margin); |
| 248 | |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 249 | spin_lock(&wdt_lock); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 250 | omap_wdt_disable(wdev); |
| 251 | omap_wdt_set_timeout(wdev); |
| 252 | omap_wdt_enable(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 253 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 254 | omap_wdt_ping(wdev); |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 255 | spin_unlock(&wdt_lock); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 256 | /* Fall */ |
| 257 | case WDIOC_GETTIMEOUT: |
| 258 | return put_user(timer_margin, (int __user *)arg); |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 259 | default: |
| 260 | return -ENOTTY; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 264 | static const struct file_operations omap_wdt_fops = { |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 265 | .owner = THIS_MODULE, |
| 266 | .write = omap_wdt_write, |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 267 | .unlocked_ioctl = omap_wdt_ioctl, |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 268 | .open = omap_wdt_open, |
| 269 | .release = omap_wdt_release, |
| 270 | }; |
| 271 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 272 | static int __init omap_wdt_probe(struct platform_device *pdev) |
| 273 | { |
| 274 | struct resource *res, *mem; |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 275 | struct omap_wdt_dev *wdev; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 276 | int ret; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 277 | |
| 278 | /* reserve static register mappings */ |
| 279 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 280 | if (!res) { |
| 281 | ret = -ENOENT; |
| 282 | goto err_get_resource; |
| 283 | } |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 284 | |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 285 | if (omap_wdt_dev) { |
| 286 | ret = -EBUSY; |
| 287 | goto err_busy; |
| 288 | } |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 289 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 290 | mem = request_mem_region(res->start, res->end - res->start + 1, |
| 291 | pdev->name); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 292 | if (!mem) { |
| 293 | ret = -EBUSY; |
| 294 | goto err_busy; |
| 295 | } |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 296 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 297 | wdev = kzalloc(sizeof(struct omap_wdt_dev), GFP_KERNEL); |
| 298 | if (!wdev) { |
| 299 | ret = -ENOMEM; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 300 | goto err_kzalloc; |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 301 | } |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 302 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 303 | wdev->omap_wdt_users = 0; |
| 304 | wdev->mem = mem; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 305 | |
| 306 | if (cpu_is_omap16xx()) { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 307 | wdev->armwdt_ck = clk_get(&pdev->dev, "armwdt_ck"); |
| 308 | if (IS_ERR(wdev->armwdt_ck)) { |
| 309 | ret = PTR_ERR(wdev->armwdt_ck); |
| 310 | wdev->armwdt_ck = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 311 | goto err_clk; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | if (cpu_is_omap24xx()) { |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 316 | wdev->mpu_wdt_ick = clk_get(&pdev->dev, "mpu_wdt_ick"); |
| 317 | if (IS_ERR(wdev->mpu_wdt_ick)) { |
| 318 | ret = PTR_ERR(wdev->mpu_wdt_ick); |
| 319 | wdev->mpu_wdt_ick = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 320 | goto err_clk; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 321 | } |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 322 | wdev->mpu_wdt_fck = clk_get(&pdev->dev, "mpu_wdt_fck"); |
| 323 | if (IS_ERR(wdev->mpu_wdt_fck)) { |
| 324 | ret = PTR_ERR(wdev->mpu_wdt_fck); |
| 325 | wdev->mpu_wdt_fck = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 326 | goto err_clk; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 330 | if (cpu_is_omap34xx()) { |
| 331 | wdev->mpu_wdt_ick = clk_get(&pdev->dev, "wdt2_ick"); |
| 332 | if (IS_ERR(wdev->mpu_wdt_ick)) { |
| 333 | ret = PTR_ERR(wdev->mpu_wdt_ick); |
| 334 | wdev->mpu_wdt_ick = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 335 | goto err_clk; |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 336 | } |
| 337 | wdev->mpu_wdt_fck = clk_get(&pdev->dev, "wdt2_fck"); |
| 338 | if (IS_ERR(wdev->mpu_wdt_fck)) { |
| 339 | ret = PTR_ERR(wdev->mpu_wdt_fck); |
| 340 | wdev->mpu_wdt_fck = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 341 | goto err_clk; |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 342 | } |
| 343 | } |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 344 | wdev->base = ioremap(res->start, res->end - res->start + 1); |
| 345 | if (!wdev->base) { |
| 346 | ret = -ENOMEM; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 347 | goto err_ioremap; |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 348 | } |
| 349 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 350 | platform_set_drvdata(pdev, wdev); |
| 351 | |
| 352 | omap_wdt_disable(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 353 | omap_wdt_adjust_timeout(timer_margin); |
| 354 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 355 | wdev->omap_wdt_miscdev.parent = &pdev->dev; |
| 356 | wdev->omap_wdt_miscdev.minor = WATCHDOG_MINOR; |
| 357 | wdev->omap_wdt_miscdev.name = "watchdog"; |
| 358 | wdev->omap_wdt_miscdev.fops = &omap_wdt_fops; |
| 359 | |
| 360 | ret = misc_register(&(wdev->omap_wdt_miscdev)); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 361 | if (ret) |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 362 | goto err_misc; |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 363 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 364 | 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] | 365 | __raw_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF, |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 366 | timer_margin); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 367 | |
| 368 | /* autogate OCP interface clock */ |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 369 | __raw_writel(0x01, wdev->base + OMAP_WATCHDOG_SYS_CONFIG); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 370 | |
| 371 | omap_wdt_dev = pdev; |
| 372 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 373 | return 0; |
| 374 | |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 375 | err_misc: |
| 376 | platform_set_drvdata(pdev, NULL); |
| 377 | iounmap(wdev->base); |
| 378 | |
| 379 | err_ioremap: |
| 380 | wdev->base = NULL; |
| 381 | |
| 382 | err_clk: |
| 383 | if (wdev->armwdt_ck) |
| 384 | clk_put(wdev->armwdt_ck); |
| 385 | if (wdev->mpu_wdt_ick) |
| 386 | clk_put(wdev->mpu_wdt_ick); |
| 387 | if (wdev->mpu_wdt_fck) |
| 388 | clk_put(wdev->mpu_wdt_fck); |
| 389 | kfree(wdev); |
| 390 | |
| 391 | err_kzalloc: |
| 392 | release_mem_region(res->start, res->end - res->start + 1); |
| 393 | |
| 394 | err_busy: |
| 395 | err_get_resource: |
| 396 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static void omap_wdt_shutdown(struct platform_device *pdev) |
| 401 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 402 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 403 | |
| 404 | if (wdev->omap_wdt_users) |
| 405 | omap_wdt_disable(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static int omap_wdt_remove(struct platform_device *pdev) |
| 409 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 410 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 411 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 412 | |
| 413 | if (!res) |
| 414 | return -ENOENT; |
| 415 | |
| 416 | misc_deregister(&(wdev->omap_wdt_miscdev)); |
| 417 | release_mem_region(res->start, res->end - res->start + 1); |
| 418 | platform_set_drvdata(pdev, NULL); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 419 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 420 | if (wdev->armwdt_ck) { |
| 421 | clk_put(wdev->armwdt_ck); |
| 422 | wdev->armwdt_ck = NULL; |
| 423 | } |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 424 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 425 | if (wdev->mpu_wdt_ick) { |
| 426 | clk_put(wdev->mpu_wdt_ick); |
| 427 | wdev->mpu_wdt_ick = NULL; |
| 428 | } |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 429 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 430 | if (wdev->mpu_wdt_fck) { |
| 431 | clk_put(wdev->mpu_wdt_fck); |
| 432 | wdev->mpu_wdt_fck = NULL; |
| 433 | } |
Felipe Balbi | 9f69e3b | 2008-09-20 04:14:02 +0300 | [diff] [blame] | 434 | iounmap(wdev->base); |
| 435 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 436 | kfree(wdev); |
| 437 | omap_wdt_dev = NULL; |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 438 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | #ifdef CONFIG_PM |
| 443 | |
| 444 | /* REVISIT ... not clear this is the best way to handle system suspend; and |
| 445 | * it's very inappropriate for selective device suspend (e.g. suspending this |
| 446 | * through sysfs rather than by stopping the watchdog daemon). Also, this |
| 447 | * may not play well enough with NOWAYOUT... |
| 448 | */ |
| 449 | |
| 450 | static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state) |
| 451 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 452 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
| 453 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 454 | if (wdev->omap_wdt_users) |
| 455 | omap_wdt_disable(wdev); |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 456 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int omap_wdt_resume(struct platform_device *pdev) |
| 461 | { |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 462 | struct omap_wdt_dev *wdev = platform_get_drvdata(pdev); |
| 463 | |
Felipe Balbi | 2817142 | 2008-09-20 04:14:01 +0300 | [diff] [blame] | 464 | if (wdev->omap_wdt_users) { |
| 465 | omap_wdt_enable(wdev); |
| 466 | omap_wdt_ping(wdev); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 467 | } |
Felipe Balbi | b311218 | 2008-09-20 04:14:03 +0300 | [diff] [blame] | 468 | |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | #else |
| 473 | #define omap_wdt_suspend NULL |
| 474 | #define omap_wdt_resume NULL |
| 475 | #endif |
| 476 | |
| 477 | static struct platform_driver omap_wdt_driver = { |
| 478 | .probe = omap_wdt_probe, |
| 479 | .remove = omap_wdt_remove, |
| 480 | .shutdown = omap_wdt_shutdown, |
| 481 | .suspend = omap_wdt_suspend, |
| 482 | .resume = omap_wdt_resume, |
| 483 | .driver = { |
| 484 | .owner = THIS_MODULE, |
| 485 | .name = "omap_wdt", |
| 486 | }, |
| 487 | }; |
| 488 | |
| 489 | static int __init omap_wdt_init(void) |
| 490 | { |
Alan Cox | 12b9df7 | 2008-05-19 14:07:32 +0100 | [diff] [blame] | 491 | spin_lock_init(&wdt_lock); |
Komal Shah | 7768a13 | 2006-09-29 01:59:18 -0700 | [diff] [blame] | 492 | return platform_driver_register(&omap_wdt_driver); |
| 493 | } |
| 494 | |
| 495 | static void __exit omap_wdt_exit(void) |
| 496 | { |
| 497 | platform_driver_unregister(&omap_wdt_driver); |
| 498 | } |
| 499 | |
| 500 | module_init(omap_wdt_init); |
| 501 | module_exit(omap_wdt_exit); |
| 502 | |
| 503 | MODULE_AUTHOR("George G. Davis"); |
| 504 | MODULE_LICENSE("GPL"); |
| 505 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
Kay Sievers | f37d193 | 2008-04-10 21:29:23 -0700 | [diff] [blame] | 506 | MODULE_ALIAS("platform:omap_wdt"); |