ARMLinux驱动Watch Dog Timer(看门狗)驱动分析
时间:11-19
来源:互联网
点击:
t boot time if set to 1, default="__MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to \reboot (default depends on ONLY_TESTING)");MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");typedef enum close_state {CLOSE_STATE_NOT,CLOSE_STATE_ALLOW = 0x4021} close_state_t;static unsigned long open_lock;static struct device *wdt_dev; /* platform device attached to */static struct resource *wdt_mem;//用来保存IO端口占用的内存资源static struct resource *wdt_irq;//保存wdt中断号static struct clk *wdt_clock;//保存从平台获取的watchdog时钟static void __iomem *wdt_base;//经过ioremap后的内存基地址static unsigned int wdt_count;//保存向WTCNT写的计数值static close_state_t allow_close;static DEFINE_SPINLOCK(wdt_lock);//定义一个自旋锁,用于资源的互斥访问/* watchdog control routines */#define DBG(msg...) do { \if (debug) \printk(KERN_INFO msg); \} while (0)/* functions *///喂狗函数,实际上是将wdt_count写入WTCNT寄存器static void s3c2410wdt_keepalive(void){spin_lock(&wdt_lock);//给资源上锁writel(wdt_count, wdt_base + S3C2410_WTCNT);//写WTCNT寄存器spin_unlock(&wdt_lock);//解锁资源,下同}static void __s3c2410wdt_stop(void){unsigned long wtcon;wtcon = readl(wdt_base + S3C2410_WTCON);wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);writel(wtcon, wdt_base + S3C2410_WTCON);//设备看门狗使能无效、复位功能无效}//停止watchdog计时static void s3c2410wdt_stop(void){spin_lock(&wdt_lock);__s3c2410wdt_stop();spin_unlock(&wdt_lock);}//启动watchdog计时static void s3c2410wdt_start(void){unsigned long wtcon;spin_lock(&wdt_lock);__s3c2410wdt_stop();wtcon = readl(wdt_base + S3C2410_WTCON);wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_div128;//看门狗定时器使能、时钟除数因子128if (soft_noboot) //使用参数soft_noboot来选择看门狗到时是重启还是执行中断函数{wtcon |= S3C2410_WTCON_INTEN;//中断使能wtcon &= ~S3C2410_WTCON_RSTEN;//复位功能无效} else {wtcon &= ~S3C2410_WTCON_INTEN;//中断不使能wtcon |= S3C2410_WTCON_RSTEN;//复位功能有效}writel(wdt_count, wdt_base + S3C2410_WTDAT);//将wdt_count写入WTDATwritel(wdt_count, wdt_base + S3C2410_WTCNT);//将wdt_count写入WTCNTwritel(wtcon, wdt_base + S3C2410_WTCON);//设置WTCONspin_unlock(&wdt_lock);}//设置WatchDog周期timeoutstatic int s3c2410wdt_set_heartbeat(int timeout){unsigned int freq = clk_get_rate(wdt_clock);unsigned int count;unsigned int divisor = 1;unsigned long wtcon;if (timeout < 1)return -EINVAL;freq /= 128;count = timeout * freq;/* if the count is bigger than the watchdog register,then work out what we need to do (and if) we canactually make this value*///如果计时的时间过大,则适当加大预分频值if (count >= 0x10000) {for (divisor = 1; divisor <= 0x100; divisor++) {if ((count / divisor) < 0x10000)break;}//若预分频最大仍不能满足计时周期,则报错if ((count / divisor) >= 0x10000) {dev_err(wdt_dev, "timeout %d too big\n", timeout);return -EINVAL;}}tmr_margin = timeout;count /= divisor;wdt_count = count;/* update the pre-scaler */wtcon = readl(wdt_base + S3C2410_WTCON);wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);writel(count, wdt_base + S3C2410_WTDAT);//是指WTDAT寄存器writel(wtcon, wdt_base + S3C2410_WTCON);//设置预分频值return 0;}/** /dev/watchdog handling*/static int s3c2410wdt_open(struct inode *inode, struct file *file){if (test_and_set_bit(0, &open_lock))//测试并设置open_lock第0位为1return -EBUSY;if (nowayout)__module_get(THIS_MODULE);allow_close = CLOSE_STATE_NOT;/* start the timer */s3c2410wdt_start();//启动watchdogreturn nonseekable_open(inode, file);}static int s3c2410wdt_release(struct inode *inode, struct file *file){/** Shut off the timer.* Lock it in if its a module and we set nowayout*/if (allow_close == CLOSE_STATE_ALLOW)s3c2410wdt_stop();else {dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");s3c2410wdt_keepalive();}allow_close = CLOSE_STATE_NOT;clear_bit(0, &open_lock);//清楚open_lock第0位return 0;}static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,size_t len, loff_t *ppos){/** Refresh the timer.*/if (len) {if (!nowayout) {size_t i;/* In case it was set long ago */allow_close = CLOSE_STATE_NOT;for (i = 0; i != len; i++) {char c;if (get_user(c, data + i))//从用户空间copy数据return -EFAULT;if (c == V)//当输入V时,关闭watchdogallow_close = CLOSE_STATE_ALLOW;}}//注意:这里要手动喂狗一次?!s3c2410wdt_keepalive();//由于将allow_close设置成CLOSE_STATE_ALLOW后,当release时无法再次喂狗}return len;}#define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSEstatic const struct watchdog_info s3c2410_wdt_ident = {.options = OPTIONS,.firmware_version = 0,.identity = "S3C2410 Watchdog",};//IO控制接口函数static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,unsigned long arg){void __user *argp = (void __user *)arg;int __user *p = argp;int new_margin;switch (cmd) {case WDIOC_GETSUPPORT:return copy_to_user(argp, &s3c2410_wdt_ident,sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;case WDIOC_GETSTATUS:case WDIOC_GETBOOTSTATUS:return put_user(0, p);case WDIOC_KEEPALIVE:s3c2410wdt_keepalive();return 0;case WDIOC_SETTIMEOUT:if (get_user(new_margin, p))return -EFAULT;if (s3c2410wdt_set_heartbeat(new_margin))return -EINVAL;s3c2410wdt_keepalive();return put_user(tmr_margin, p);case WDIOC_GETTIMEOUT:return put_user(tmr_margin, p);default:return -ENOTTY;}}/* kernel interface *///文件操作结构体static const struct file_operations s3c2410wdt_fops = {.owner = THIS_MODULE,.llseek = no_llseek,//屏蔽seek操作.write = s3c2410wdt_write,//写方法.unlocked_ioctl = s3c2410wdt_ioctl,//控制方法.open = s3c2410wdt_open,//代开设备方法.release = s3c2410wdt_release,//关闭设备方法};static struct miscdevice s3c2410wdt_miscdev = {.minor = WATCHDOG_MINOR,.name = "watchdog",.fops = &s3c2410wdt_fops,};/* interrupt handler code */static irqreturn_t s3c2410wdt_irq(int irqno, void *param){//dev_info(wdt_dev, "watchdog timer expired (irq)\n");s3c2410_gpio_setpin(S3C2410_GPB10,(count++)%2);s3c2410wdt_keepalive();return IRQ_HANDLED;}/* device interface *///注册设备时执行static int s3c2410wdt_probe(struct platform_device *pdev){struct resource *res;struct device *dev;unsigned int wtcon;int started = 0;int ret;int size;dev = &pdev->dev;wdt_dev = &pdev->dev;/* get the memory region for the watchdog timer *///获取IO端口资源,这里 IORESOURCE_MEM和平台设备中资源的定义一致res = platform_get_resource(pdev, IORESOURCE_MEM, 0);if (res == NULL) {dev_err(dev, "no memory resource specified\n");return -ENOENT;}size = (res->end - res->start) + 1;//申请内存空间wdt_mem = r
ARMLinux驱动WatchDogTime 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)