···139139 To compile this driver as a module, choose M here: the140140 module will be called sa1100_wdt.141141142142+config MPCORE_WATCHDOG143143+ tristate "MPcore watchdog"144144+ depends on WATCHDOG && ARM_MPCORE_PLATFORM && LOCAL_TIMERS145145+ help146146+ Watchdog timer embedded into the MPcore system.147147+148148+ To compile this driver as a module, choose M here: the149149+ module will be called mpcore_wdt.150150+142151# X86 (i386 + ia64 + x86_64) Architecture143152144153config ACQUIRE_WDT
···11+/*22+ * Watchdog driver for the mpcore watchdog timer33+ *44+ * (c) Copyright 2004 ARM Limited55+ *66+ * Based on the SoftDog driver:77+ * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.88+ * http://www.redhat.com99+ *1010+ * This program is free software; you can redistribute it and/or1111+ * modify it under the terms of the GNU General Public License1212+ * as published by the Free Software Foundation; either version1313+ * 2 of the License, or (at your option) any later version.1414+ *1515+ * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide1616+ * warranty for any of this software. This material is provided1717+ * "AS-IS" and at no charge.1818+ *1919+ * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>2020+ *2121+ */2222+#include <linux/module.h>2323+#include <linux/moduleparam.h>2424+#include <linux/config.h>2525+#include <linux/types.h>2626+#include <linux/miscdevice.h>2727+#include <linux/watchdog.h>2828+#include <linux/fs.h>2929+#include <linux/reboot.h>3030+#include <linux/init.h>3131+#include <linux/interrupt.h>3232+#include <linux/device.h>3333+#include <asm/uaccess.h>3434+3535+struct mpcore_wdt {3636+ unsigned long timer_alive;3737+ struct device *dev;3838+ void __iomem *base;3939+ int irq;4040+ unsigned int perturb;4141+ char expect_close;4242+};4343+4444+static struct platform_device *mpcore_wdt_dev;4545+4646+extern unsigned int mpcore_timer_rate;4747+4848+#define TIMER_MARGIN 604949+static int mpcore_margin = TIMER_MARGIN;5050+module_param(mpcore_margin, int, 0);5151+MODULE_PARM_DESC(mpcore_margin, "MPcore timer margin in seconds. (0<mpcore_margin<65536, default=" __MODULE_STRING(TIMER_MARGIN) ")");5252+5353+static int nowayout = WATCHDOG_NOWAYOUT;5454+module_param(nowayout, int, 0);5555+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");5656+5757+#define ONLY_TESTING 05858+static int mpcore_noboot = ONLY_TESTING;5959+module_param(mpcore_noboot, int, 0);6060+MODULE_PARM_DESC(mpcore_noboot, "MPcore watchdog action, set to 1 to ignore reboots, 0 to reboot (default=" __MODULE_STRING(ONLY_TESTING) ")");6161+6262+/*6363+ * This is the interrupt handler. Note that we only use this6464+ * in testing mode, so don't actually do a reboot here.6565+ */6666+static irqreturn_t mpcore_wdt_fire(int irq, void *arg, struct pt_regs *regs)6767+{6868+ struct mpcore_wdt *wdt = arg;6969+7070+ /* Check it really was our interrupt */7171+ if (readl(wdt->base + TWD_WDOG_INTSTAT)) {7272+ dev_printk(KERN_CRIT, wdt->dev, "Triggered - Reboot ignored.\n");7373+7474+ /* Clear the interrupt on the watchdog */7575+ writel(1, wdt->base + TWD_WDOG_INTSTAT);7676+7777+ return IRQ_HANDLED;7878+ }7979+8080+ return IRQ_NONE;8181+}8282+8383+/*8484+ * mpcore_wdt_keepalive - reload the timer8585+ *8686+ * Note that the spec says a DIFFERENT value must be written to the reload8787+ * register each time. The "perturb" variable deals with this by adding 18888+ * to the count every other time the function is called.8989+ */9090+static void mpcore_wdt_keepalive(struct mpcore_wdt *wdt)9191+{9292+ unsigned int count;9393+9494+ /* Assume prescale is set to 256 */9595+ count = (mpcore_timer_rate / 256) * mpcore_margin;9696+9797+ /* Reload the counter */9898+ writel(count + wdt->perturb, wdt->base + TWD_WDOG_LOAD);9999+100100+ wdt->perturb = wdt->perturb ? 0 : 1;101101+}102102+103103+static void mpcore_wdt_stop(struct mpcore_wdt *wdt)104104+{105105+ writel(0x12345678, wdt->base + TWD_WDOG_DISABLE);106106+ writel(0x87654321, wdt->base + TWD_WDOG_DISABLE);107107+ writel(0x0, wdt->base + TWD_WDOG_CONTROL);108108+}109109+110110+static void mpcore_wdt_start(struct mpcore_wdt *wdt)111111+{112112+ dev_printk(KERN_INFO, wdt->dev, "enabling watchdog.\n");113113+114114+ /* This loads the count register but does NOT start the count yet */115115+ mpcore_wdt_keepalive(wdt);116116+117117+ if (mpcore_noboot) {118118+ /* Enable watchdog - prescale=256, watchdog mode=0, enable=1 */119119+ writel(0x0000FF01, wdt->base + TWD_WDOG_CONTROL);120120+ } else {121121+ /* Enable watchdog - prescale=256, watchdog mode=1, enable=1 */122122+ writel(0x0000FF09, wdt->base + TWD_WDOG_CONTROL);123123+ }124124+}125125+126126+static int mpcore_wdt_set_heartbeat(int t)127127+{128128+ if (t < 0x0001 || t > 0xFFFF)129129+ return -EINVAL;130130+131131+ mpcore_margin = t;132132+ return 0;133133+}134134+135135+/*136136+ * /dev/watchdog handling137137+ */138138+static int mpcore_wdt_open(struct inode *inode, struct file *file)139139+{140140+ struct mpcore_wdt *wdt = dev_get_drvdata(&mpcore_wdt_dev->dev);141141+142142+ if (test_and_set_bit(0, &wdt->timer_alive))143143+ return -EBUSY;144144+145145+ if (nowayout)146146+ __module_get(THIS_MODULE);147147+148148+ file->private_data = wdt;149149+150150+ /*151151+ * Activate timer152152+ */153153+ mpcore_wdt_start(wdt);154154+155155+ return nonseekable_open(inode, file);156156+}157157+158158+static int mpcore_wdt_release(struct inode *inode, struct file *file)159159+{160160+ struct mpcore_wdt *wdt = file->private_data;161161+162162+ /*163163+ * Shut off the timer.164164+ * Lock it in if it's a module and we set nowayout165165+ */166166+ if (wdt->expect_close == 42) {167167+ mpcore_wdt_stop(wdt);168168+ } else {169169+ dev_printk(KERN_CRIT, wdt->dev, "unexpected close, not stopping watchdog!\n");170170+ mpcore_wdt_keepalive(wdt);171171+ }172172+ clear_bit(0, &wdt->timer_alive);173173+ wdt->expect_close = 0;174174+ return 0;175175+}176176+177177+static ssize_t mpcore_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)178178+{179179+ struct mpcore_wdt *wdt = file->private_data;180180+181181+ /* Can't seek (pwrite) on this device */182182+ if (ppos != &file->f_pos)183183+ return -ESPIPE;184184+185185+ /*186186+ * Refresh the timer.187187+ */188188+ if (len) {189189+ if (!nowayout) {190190+ size_t i;191191+192192+ /* In case it was set long ago */193193+ wdt->expect_close = 0;194194+195195+ for (i = 0; i != len; i++) {196196+ char c;197197+198198+ if (get_user(c, data + i))199199+ return -EFAULT;200200+ if (c == 'V')201201+ wdt->expect_close = 42;202202+ }203203+ }204204+ mpcore_wdt_keepalive(wdt);205205+ }206206+ return len;207207+}208208+209209+static struct watchdog_info ident = {210210+ .options = WDIOF_SETTIMEOUT |211211+ WDIOF_KEEPALIVEPING |212212+ WDIOF_MAGICCLOSE,213213+ .identity = "MPcore Watchdog",214214+};215215+216216+static int mpcore_wdt_ioctl(struct inode *inode, struct file *file,217217+ unsigned int cmd, unsigned long arg)218218+{219219+ struct mpcore_wdt *wdt = file->private_data;220220+ int ret;221221+ union {222222+ struct watchdog_info ident;223223+ int i;224224+ } uarg;225225+226226+ if (_IOC_DIR(cmd) && _IOC_SIZE(cmd) > sizeof(uarg))227227+ return -ENOIOCTLCMD;228228+229229+ if (_IOC_DIR(cmd) & _IOC_WRITE) {230230+ ret = copy_from_user(&uarg, (void __user *)arg, _IOC_SIZE(cmd));231231+ if (ret)232232+ return -EFAULT;233233+ }234234+235235+ switch (cmd) {236236+ case WDIOC_GETSUPPORT:237237+ uarg.ident = ident;238238+ ret = 0;239239+ break;240240+241241+ case WDIOC_SETOPTIONS:242242+ ret = -EINVAL;243243+ if (uarg.i & WDIOS_DISABLECARD) {244244+ mpcore_wdt_stop(wdt);245245+ ret = 0;246246+ }247247+ if (uarg.i & WDIOS_ENABLECARD) {248248+ mpcore_wdt_start(wdt);249249+ ret = 0;250250+ }251251+ break;252252+253253+ case WDIOC_GETSTATUS:254254+ case WDIOC_GETBOOTSTATUS:255255+ uarg.i = 0;256256+ ret = 0;257257+ break;258258+259259+ case WDIOC_KEEPALIVE:260260+ mpcore_wdt_keepalive(wdt);261261+ ret = 0;262262+ break;263263+264264+ case WDIOC_SETTIMEOUT:265265+ ret = mpcore_wdt_set_heartbeat(uarg.i);266266+ if (ret)267267+ break;268268+269269+ mpcore_wdt_keepalive(wdt);270270+ /* Fall */271271+ case WDIOC_GETTIMEOUT:272272+ uarg.i = mpcore_margin;273273+ ret = 0;274274+ break;275275+276276+ default:277277+ return -ENOIOCTLCMD;278278+ }279279+280280+ if (ret == 0 && _IOC_DIR(cmd) & _IOC_READ) {281281+ ret = copy_to_user((void __user *)arg, &uarg, _IOC_SIZE(cmd));282282+ if (ret)283283+ ret = -EFAULT;284284+ }285285+ return ret;286286+}287287+288288+/*289289+ * System shutdown handler. Turn off the watchdog if we're290290+ * restarting or halting the system.291291+ */292292+static void mpcore_wdt_shutdown(struct device *_dev)293293+{294294+ struct mpcore_wdt *wdt = dev_get_drvdata(_dev);295295+296296+ if (system_state == SYSTEM_RESTART || system_state == SYSTEM_HALT)297297+ mpcore_wdt_stop(wdt);298298+}299299+300300+/*301301+ * Kernel Interfaces302302+ */303303+static struct file_operations mpcore_wdt_fops = {304304+ .owner = THIS_MODULE,305305+ .llseek = no_llseek,306306+ .write = mpcore_wdt_write,307307+ .ioctl = mpcore_wdt_ioctl,308308+ .open = mpcore_wdt_open,309309+ .release = mpcore_wdt_release,310310+};311311+312312+static struct miscdevice mpcore_wdt_miscdev = {313313+ .minor = WATCHDOG_MINOR,314314+ .name = "watchdog",315315+ .fops = &mpcore_wdt_fops,316316+};317317+318318+static int __devinit mpcore_wdt_probe(struct device *_dev)319319+{320320+ struct platform_device *dev = to_platform_device(_dev);321321+ struct mpcore_wdt *wdt;322322+ struct resource *res;323323+ int ret;324324+325325+ /* We only accept one device, and it must have an id of -1 */326326+ if (dev->id != -1)327327+ return -ENODEV;328328+329329+ res = platform_get_resource(dev, IORESOURCE_MEM, 0);330330+ if (!res) {331331+ ret = -ENODEV;332332+ goto err_out;333333+ }334334+335335+ wdt = kmalloc(sizeof(struct mpcore_wdt), GFP_KERNEL);336336+ if (!wdt) {337337+ ret = -ENOMEM;338338+ goto err_out;339339+ }340340+ memset(wdt, 0, sizeof(struct mpcore_wdt));341341+342342+ wdt->dev = &dev->dev;343343+ wdt->irq = platform_get_irq(dev, 0);344344+ wdt->base = ioremap(res->start, res->end - res->start + 1);345345+ if (!wdt->base) {346346+ ret = -ENOMEM;347347+ goto err_free;348348+ }349349+350350+ mpcore_wdt_miscdev.dev = &dev->dev;351351+ ret = misc_register(&mpcore_wdt_miscdev);352352+ if (ret) {353353+ dev_printk(KERN_ERR, _dev, "cannot register miscdev on minor=%d (err=%d)\n",354354+ WATCHDOG_MINOR, ret);355355+ goto err_misc;356356+ }357357+358358+ ret = request_irq(wdt->irq, mpcore_wdt_fire, SA_INTERRUPT, "mpcore_wdt", wdt);359359+ if (ret) {360360+ dev_printk(KERN_ERR, _dev, "cannot register IRQ%d for watchdog\n", wdt->irq);361361+ goto err_irq;362362+ }363363+364364+ mpcore_wdt_stop(wdt);365365+ dev_set_drvdata(&dev->dev, wdt);366366+ mpcore_wdt_dev = dev;367367+368368+ return 0;369369+370370+ err_irq:371371+ misc_deregister(&mpcore_wdt_miscdev);372372+ err_misc:373373+ iounmap(wdt->base);374374+ err_free:375375+ kfree(wdt);376376+ err_out:377377+ return ret;378378+}379379+380380+static int __devexit mpcore_wdt_remove(struct device *dev)381381+{382382+ struct mpcore_wdt *wdt = dev_get_drvdata(dev);383383+384384+ dev_set_drvdata(dev, NULL);385385+386386+ misc_deregister(&mpcore_wdt_miscdev);387387+388388+ mpcore_wdt_dev = NULL;389389+390390+ free_irq(wdt->irq, wdt);391391+ iounmap(wdt->base);392392+ kfree(wdt);393393+ return 0;394394+}395395+396396+static struct device_driver mpcore_wdt_driver = {397397+ .name = "mpcore_wdt",398398+ .bus = &platform_bus_type,399399+ .probe = mpcore_wdt_probe,400400+ .remove = __devexit_p(mpcore_wdt_remove),401401+ .shutdown = mpcore_wdt_shutdown,402402+};403403+404404+static char banner[] __initdata = KERN_INFO "MPcore Watchdog Timer: 0.1. mpcore_noboot=%d mpcore_margin=%d sec (nowayout= %d)\n";405405+406406+static int __init mpcore_wdt_init(void)407407+{408408+ /*409409+ * Check that the margin value is within it's range;410410+ * if not reset to the default411411+ */412412+ if (mpcore_wdt_set_heartbeat(mpcore_margin)) {413413+ mpcore_wdt_set_heartbeat(TIMER_MARGIN);414414+ printk(KERN_INFO "mpcore_margin value must be 0<mpcore_margin<65536, using %d\n",415415+ TIMER_MARGIN);416416+ }417417+418418+ printk(banner, mpcore_noboot, mpcore_margin, nowayout);419419+420420+ return driver_register(&mpcore_wdt_driver);421421+}422422+423423+static void __exit mpcore_wdt_exit(void)424424+{425425+ driver_unregister(&mpcore_wdt_driver);426426+}427427+428428+module_init(mpcore_wdt_init);429429+module_exit(mpcore_wdt_exit);430430+431431+MODULE_AUTHOR("ARM Limited");432432+MODULE_DESCRIPTION("MPcore Watchdog Device Driver");433433+MODULE_LICENSE("GPL");434434+MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);