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