Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/miscdevice.h> |
| 4 | #include <linux/watchdog.h> |
| 5 | #include <linux/io.h> |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 6 | #include <linux/spinlock.h> |
Alan Cox | f26ef3d | 2008-05-19 14:07:09 +0100 | [diff] [blame] | 7 | #include <linux/of_platform.h> |
| 8 | #include <linux/uaccess.h> |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 9 | #include <asm/mpc52xx.h> |
| 10 | |
| 11 | |
| 12 | #define GPT_MODE_WDT (1<<15) |
| 13 | #define GPT_MODE_CE (1<<12) |
| 14 | #define GPT_MODE_MS_TIMER (0x4) |
| 15 | |
| 16 | |
| 17 | struct mpc5200_wdt { |
| 18 | unsigned count; /* timer ticks before watchdog kicks in */ |
| 19 | long ipb_freq; |
| 20 | struct miscdevice miscdev; |
| 21 | struct resource mem; |
| 22 | struct mpc52xx_gpt __iomem *regs; |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 23 | spinlock_t io_lock; |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 26 | /* is_active stores wether or not the /dev/watchdog device is opened */ |
| 27 | static unsigned long is_active; |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 28 | |
| 29 | /* misc devices don't provide a way, to get back to 'dev' or 'miscdev' from |
| 30 | * file operations, which sucks. But there can be max 1 watchdog anyway, so... |
| 31 | */ |
| 32 | static struct mpc5200_wdt *wdt_global; |
| 33 | |
| 34 | |
| 35 | /* helper to calculate timeout in timer counts */ |
| 36 | static void mpc5200_wdt_set_timeout(struct mpc5200_wdt *wdt, int timeout) |
| 37 | { |
| 38 | /* use biggest prescaler of 64k */ |
| 39 | wdt->count = (wdt->ipb_freq + 0xffff) / 0x10000 * timeout; |
| 40 | |
| 41 | if (wdt->count > 0xffff) |
| 42 | wdt->count = 0xffff; |
| 43 | } |
| 44 | /* return timeout in seconds (calculated from timer count) */ |
| 45 | static int mpc5200_wdt_get_timeout(struct mpc5200_wdt *wdt) |
| 46 | { |
| 47 | return wdt->count * 0x10000 / wdt->ipb_freq; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /* watchdog operations */ |
| 52 | static int mpc5200_wdt_start(struct mpc5200_wdt *wdt) |
| 53 | { |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 54 | spin_lock(&wdt->io_lock); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 55 | /* disable */ |
| 56 | out_be32(&wdt->regs->mode, 0); |
| 57 | /* set timeout, with maximum prescaler */ |
| 58 | out_be32(&wdt->regs->count, 0x0 | wdt->count); |
| 59 | /* enable watchdog */ |
Alan Cox | f26ef3d | 2008-05-19 14:07:09 +0100 | [diff] [blame] | 60 | out_be32(&wdt->regs->mode, GPT_MODE_CE | GPT_MODE_WDT | |
| 61 | GPT_MODE_MS_TIMER); |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 62 | spin_unlock(&wdt->io_lock); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | static int mpc5200_wdt_ping(struct mpc5200_wdt *wdt) |
| 67 | { |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 68 | spin_lock(&wdt->io_lock); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 69 | /* writing A5 to OCPW resets the watchdog */ |
Alan Cox | f26ef3d | 2008-05-19 14:07:09 +0100 | [diff] [blame] | 70 | out_be32(&wdt->regs->mode, 0xA5000000 | |
| 71 | (0xffffff & in_be32(&wdt->regs->mode))); |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 72 | spin_unlock(&wdt->io_lock); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | static int mpc5200_wdt_stop(struct mpc5200_wdt *wdt) |
| 76 | { |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 77 | spin_lock(&wdt->io_lock); |
| 78 | /* disable */ |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 79 | out_be32(&wdt->regs->mode, 0); |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 80 | spin_unlock(&wdt->io_lock); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* file operations */ |
Al Viro | a39f9d0 | 2007-10-14 19:34:20 +0100 | [diff] [blame] | 86 | static ssize_t mpc5200_wdt_write(struct file *file, const char __user *data, |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 87 | size_t len, loff_t *ppos) |
| 88 | { |
| 89 | struct mpc5200_wdt *wdt = file->private_data; |
| 90 | mpc5200_wdt_ping(wdt); |
| 91 | return 0; |
| 92 | } |
| 93 | static struct watchdog_info mpc5200_wdt_info = { |
| 94 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 95 | .identity = "mpc5200 watchdog on GPT0", |
| 96 | }; |
Alan Cox | f26ef3d | 2008-05-19 14:07:09 +0100 | [diff] [blame] | 97 | static long mpc5200_wdt_ioctl(struct file *file, unsigned int cmd, |
| 98 | unsigned long arg) |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 99 | { |
| 100 | struct mpc5200_wdt *wdt = file->private_data; |
| 101 | int __user *data = (int __user *)arg; |
| 102 | int timeout; |
| 103 | int ret = 0; |
| 104 | |
| 105 | switch (cmd) { |
| 106 | case WDIOC_GETSUPPORT: |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 107 | ret = copy_to_user(data, &mpc5200_wdt_info, |
Alan Cox | f26ef3d | 2008-05-19 14:07:09 +0100 | [diff] [blame] | 108 | sizeof(mpc5200_wdt_info)); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 109 | if (ret) |
| 110 | ret = -EFAULT; |
| 111 | break; |
| 112 | |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 113 | case WDIOC_GETSTATUS: |
| 114 | case WDIOC_GETBOOTSTATUS: |
| 115 | ret = put_user(0, data); |
| 116 | break; |
| 117 | |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 118 | case WDIOC_KEEPALIVE: |
| 119 | mpc5200_wdt_ping(wdt); |
| 120 | break; |
| 121 | |
| 122 | case WDIOC_SETTIMEOUT: |
| 123 | ret = get_user(timeout, data); |
| 124 | if (ret) |
| 125 | break; |
| 126 | mpc5200_wdt_set_timeout(wdt, timeout); |
| 127 | mpc5200_wdt_start(wdt); |
| 128 | /* fall through and return the timeout */ |
| 129 | |
| 130 | case WDIOC_GETTIMEOUT: |
| 131 | timeout = mpc5200_wdt_get_timeout(wdt); |
| 132 | ret = put_user(timeout, data); |
| 133 | break; |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 134 | |
| 135 | default: |
| 136 | ret = -ENOTTY; |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 137 | } |
| 138 | return ret; |
| 139 | } |
Alan Cox | f26ef3d | 2008-05-19 14:07:09 +0100 | [diff] [blame] | 140 | |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 141 | static int mpc5200_wdt_open(struct inode *inode, struct file *file) |
| 142 | { |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 143 | /* /dev/watchdog can only be opened once */ |
| 144 | if (test_and_set_bit(0, &is_active)) |
| 145 | return -EBUSY; |
| 146 | |
| 147 | /* Set and activate the watchdog */ |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 148 | mpc5200_wdt_set_timeout(wdt_global, 30); |
| 149 | mpc5200_wdt_start(wdt_global); |
| 150 | file->private_data = wdt_global; |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 151 | return nonseekable_open(inode, file); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 152 | } |
| 153 | static int mpc5200_wdt_release(struct inode *inode, struct file *file) |
| 154 | { |
| 155 | #if WATCHDOG_NOWAYOUT == 0 |
| 156 | struct mpc5200_wdt *wdt = file->private_data; |
| 157 | mpc5200_wdt_stop(wdt); |
| 158 | wdt->count = 0; /* == disabled */ |
| 159 | #endif |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 160 | clear_bit(0, &is_active); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Jan Engelhardt | b47a166 | 2008-01-22 20:48:10 +0100 | [diff] [blame] | 164 | static const struct file_operations mpc5200_wdt_fops = { |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 165 | .owner = THIS_MODULE, |
| 166 | .write = mpc5200_wdt_write, |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 167 | .unlocked_ioctl = mpc5200_wdt_ioctl, |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 168 | .open = mpc5200_wdt_open, |
| 169 | .release = mpc5200_wdt_release, |
| 170 | }; |
| 171 | |
| 172 | /* module operations */ |
Alan Cox | f26ef3d | 2008-05-19 14:07:09 +0100 | [diff] [blame] | 173 | static int mpc5200_wdt_probe(struct of_device *op, |
| 174 | const struct of_device_id *match) |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 175 | { |
| 176 | struct mpc5200_wdt *wdt; |
| 177 | int err; |
| 178 | const void *has_wdt; |
| 179 | int size; |
| 180 | |
| 181 | has_wdt = of_get_property(op->node, "has-wdt", NULL); |
| 182 | if (!has_wdt) |
Marian Balakowicz | d24bc31 | 2007-10-19 04:44:24 +1000 | [diff] [blame] | 183 | has_wdt = of_get_property(op->node, "fsl,has-wdt", NULL); |
| 184 | if (!has_wdt) |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 185 | return -ENODEV; |
| 186 | |
| 187 | wdt = kzalloc(sizeof(*wdt), GFP_KERNEL); |
| 188 | if (!wdt) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | wdt->ipb_freq = mpc52xx_find_ipb_freq(op->node); |
| 192 | |
| 193 | err = of_address_to_resource(op->node, 0, &wdt->mem); |
| 194 | if (err) |
| 195 | goto out_free; |
| 196 | size = wdt->mem.end - wdt->mem.start + 1; |
| 197 | if (!request_mem_region(wdt->mem.start, size, "mpc5200_wdt")) { |
| 198 | err = -ENODEV; |
| 199 | goto out_free; |
| 200 | } |
| 201 | wdt->regs = ioremap(wdt->mem.start, size); |
| 202 | if (!wdt->regs) { |
| 203 | err = -ENODEV; |
| 204 | goto out_release; |
| 205 | } |
| 206 | |
| 207 | dev_set_drvdata(&op->dev, wdt); |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 208 | spin_lock_init(&wdt->io_lock); |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 209 | |
| 210 | wdt->miscdev = (struct miscdevice) { |
| 211 | .minor = WATCHDOG_MINOR, |
| 212 | .name = "watchdog", |
| 213 | .fops = &mpc5200_wdt_fops, |
| 214 | .parent = &op->dev, |
| 215 | }; |
| 216 | wdt_global = wdt; |
| 217 | err = misc_register(&wdt->miscdev); |
| 218 | if (!err) |
| 219 | return 0; |
| 220 | |
| 221 | iounmap(wdt->regs); |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 222 | out_release: |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 223 | release_mem_region(wdt->mem.start, size); |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 224 | out_free: |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 225 | kfree(wdt); |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | static int mpc5200_wdt_remove(struct of_device *op) |
| 230 | { |
| 231 | struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev); |
| 232 | |
| 233 | mpc5200_wdt_stop(wdt); |
| 234 | misc_deregister(&wdt->miscdev); |
| 235 | iounmap(wdt->regs); |
| 236 | release_mem_region(wdt->mem.start, wdt->mem.end - wdt->mem.start + 1); |
| 237 | kfree(wdt); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | static int mpc5200_wdt_suspend(struct of_device *op, pm_message_t state) |
| 242 | { |
| 243 | struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev); |
| 244 | mpc5200_wdt_stop(wdt); |
| 245 | return 0; |
| 246 | } |
| 247 | static int mpc5200_wdt_resume(struct of_device *op) |
| 248 | { |
| 249 | struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev); |
| 250 | if (wdt->count) |
| 251 | mpc5200_wdt_start(wdt); |
| 252 | return 0; |
| 253 | } |
| 254 | static int mpc5200_wdt_shutdown(struct of_device *op) |
| 255 | { |
| 256 | struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev); |
| 257 | mpc5200_wdt_stop(wdt); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static struct of_device_id mpc5200_wdt_match[] = { |
| 262 | { .compatible = "mpc5200-gpt", }, |
Marian Balakowicz | d24bc31 | 2007-10-19 04:44:24 +1000 | [diff] [blame] | 263 | { .compatible = "fsl,mpc5200-gpt", }, |
Domen Puncer | 8cf1897 | 2007-06-18 08:17:57 +0200 | [diff] [blame] | 264 | {}, |
| 265 | }; |
| 266 | static struct of_platform_driver mpc5200_wdt_driver = { |
| 267 | .owner = THIS_MODULE, |
| 268 | .name = "mpc5200-gpt-wdt", |
| 269 | .match_table = mpc5200_wdt_match, |
| 270 | .probe = mpc5200_wdt_probe, |
| 271 | .remove = mpc5200_wdt_remove, |
| 272 | .suspend = mpc5200_wdt_suspend, |
| 273 | .resume = mpc5200_wdt_resume, |
| 274 | .shutdown = mpc5200_wdt_shutdown, |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | static int __init mpc5200_wdt_init(void) |
| 279 | { |
| 280 | return of_register_platform_driver(&mpc5200_wdt_driver); |
| 281 | } |
| 282 | |
| 283 | static void __exit mpc5200_wdt_exit(void) |
| 284 | { |
| 285 | of_unregister_platform_driver(&mpc5200_wdt_driver); |
| 286 | } |
| 287 | |
| 288 | module_init(mpc5200_wdt_init); |
| 289 | module_exit(mpc5200_wdt_exit); |
| 290 | |
| 291 | MODULE_AUTHOR("Domen Puncer <domen.puncer@telargo.com>"); |
| 292 | MODULE_LICENSE("Dual BSD/GPL"); |
Wim Van Sebroeck | de81225 | 2007-07-20 21:22:58 +0000 | [diff] [blame] | 293 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |