基於S3C2440的嵌入式Linux驅動——看門狗(watchdog)驅動解讀
本文將介紹看門狗驅動的實現。
目標平臺:TQ2440
CPU:s3c2440
內核版本:2.6.30
1. 看門狗概述
看門狗其實就是一個定時器,當該定時器溢出前必須對看門狗進行"餵狗“,如果不這樣做,定時器溢出後則將復位CPU。
因此,看門狗通常用於對處於異常狀態的CPU進行復位。
具體的概念請自行百度。
2. S3C2440看門狗
s3c2440的看門狗的原理框圖如下:
可以看出,看門狗定時器的頻率由PCLK提供,其預分頻器最大取值為255+1;另外,通過MUX,可以進一步降低頻率。
定時器采用遞減模式,一旦到0,則可以觸發看門狗中斷以及RESET復位信號。
看門狗定時器的頻率的計算公式如下:
3. 看門狗驅動
看門狗驅動代碼位於: linux/drivers/char/watchdog/s3c2410_wdt.c
3.1 模塊註冊以及probe函數
- static struct platform_driver s3c2410wdt_driver = {
- .probe = s3c2410wdt_probe,
- .remove = s3c2410wdt_remove,
- .shutdown = s3c2410wdt_shutdown,
- .suspend = s3c2410wdt_suspend,
- .resume = s3c2410wdt_resume,
- .driver = {
- .owner = THIS_MODULE,
- .name = "s3c2410-wdt",
- },
- };
- static char banner[] __initdata =
- KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
-
static int __init watchdog_init(void){printk(banner);return platform_driver_register(&s3c2410wdt_driver);}
- module_init(watchdog_init)
模塊的註冊函數很簡單,直接調用了 platform的驅動註冊函數platform_driver_register。
該函數在註冊時會調用驅動的probe方法,也即s3c2410wdt_probe函數。
我們來看下這個函數:
- static int s3c2410wdt_probe(struct platform_device *pdev)
- {
- struct resource *res;
- struct device *dev;
- unsigned int wtcon;
- int started = 0;
- int ret;
- int size;
- DBG("%s: probe=%p\n", __func__, pdev);
- dev = &pdev->dev;
- wdt_dev = &pdev->dev;
- /* get the memory region for the watchdog timer */
- /*獲取平臺資源,寄存器地址範圍*/
- 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 = request_mem_region(res->start, size, pdev->name);
- if (wdt_mem == NULL) {
- dev_err(dev, "failed to get memory region\n");
- ret = -ENOENT;
- goto err_req;
- }
- /*內存映射*/
- wdt_base = ioremap(res->start, size);
- if (wdt_base == NULL) {
- dev_err(dev, "failed to ioremap() region\n");
- ret = -EINVAL;
- goto err_req;
- }
- DBG("probe: mapped wdt_base=%p\n", wdt_base);
- /*獲取平臺資源,看門狗定時器中斷號*/
- wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
- if (wdt_irq == NULL) {
- dev_err(dev, "no irq resource specified\n");
- ret = -ENOENT;
- goto err_map;
- }
- /*註冊看門狗定時器中斷*/
- ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
- if (ret != 0) {
- dev_err(dev, "failed to install irq (%d)\n", ret);
- goto err_map;
- }
- /*獲取看門狗模塊的時鐘*/
- wdt_clock = clk_get(&pdev->dev, "watchdog");
- if (IS_ERR(wdt_clock)) {
- dev_err(dev, "failed to find watchdog clock source\n");
- ret = PTR_ERR(wdt_clock);
- goto err_irq;
- }
- /*使能該時鐘*/
- clk_enable(wdt_clock);
- /* see if we can actually set the requested timer margin, and if
- * not, try the default value */
- /*設置定時器模塊的時鐘頻率*/
- if (s3c2410wdt_set_heartbeat(tmr_margin)) {
- started = s3c2410wdt_set_heartbeat(
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- if (started == 0)
- dev_info(dev,
- "tmr_margin value out of range, default %d used\n",
- CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
- else
- dev_info(dev, "default timer value is out of range, cannot start\n");
- }
- /*註冊混雜設備,設備名watchdog,次設備號130*/
- ret = misc_register(&s3c2410wdt_miscdev);
- if (ret) {
- dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
- WATCHDOG_MINOR, ret);
- goto err_clk;
- }
- /*
- *如果需要在看門狗模塊加載時啟動看門狗則
- *調用s3c2410wdt_start,否則調用s3c2410wdt_stop
- */
- if (tmr_atboot && started == 0) {
- dev_info(dev, "starting watchdog timer\n");
- s3c2410wdt_start();
- } else if (!tmr_atboot) {
- /* if we‘re not enabling the watchdog, then ensure it is
- * disabled if it has been left running from the bootloader
- * or other source */
- s3c2410wdt_stop();
- }
- /* print out a statement of readiness */
- /*讀取控制寄存器,打印目前看門狗的狀態*/
- wtcon = readl(wdt_base + S3C2410_WTCON);
- dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
- (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
- (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
- (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
- return 0;
- err_clk:
- clk_disable(wdt_clock);
- clk_put(wdt_clock);
- err_irq:
- free_irq(wdt_irq->start, pdev);
- err_map:
- iounmap(wdt_base);
- err_req:
- release_resource(wdt_mem);
- kfree(wdt_mem);
- return ret;
- }
該函數的前面幾步和其他驅動的類似:獲取平臺資源後進行相應的註冊,並使能時鐘。接著,將調用s3c2410wdt_set_heartbeat函數來設置看門狗的工作頻率。
然後,註冊一個混雜設備,為看門狗註冊相應的API到內核中。最後,判斷是否需要啟動看門狗並調用相應的函數。
上面是probe函數大致的執行過程。隨後我們看下其中被調用的s3c2410wdt_set_heartbeat函數,該函數將設置看門狗的工作頻率。
PS:probe函數的執行依賴於平臺設備,而看門狗平臺設備的添加和註冊在linux/arch/arm/plat-s3c24xx/devs.c和 linux/arch/arm/mach-s3c2410/mach-smdk2410.c中已經完成,因此對於看門狗驅動無需進行移植。
3.2 s3c2410wdt_set_heartbeat
- static int s3c2410wdt_set_heartbeat(int timeout) /*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; /*時鐘源為PCLK/128,不使用16 32 和64*/
- count = timeout * freq; /*得出計數器值*/
- DBG("%s: count=%d, timeout=%d, freq=%d\n",
- __func__, count, timeout, freq);
- /* if the count is bigger than the watchdog register,
- then work out what we need to do (and if) we can
- actually make this value
- */
- /*計數器最大值為0xFFFF,如果大於則要計算Prescaler value*/
- if (count >= 0x10000) {
- for (divisor = 1; divisor <= 0x100; divisor++) { /*Prescaler value最大為0xff*/
- if ((count / divisor) < 0x10000)
- break;
- }
- /*找不到合適的Prescaler value,報錯返回*/
- if ((count / divisor) >= 0x10000) {
- dev_err(wdt_dev, "timeout %d too big\n", timeout);
- return -EINVAL;
- }
- }
- tmr_margin = timeout; /*保存timeout*/
- DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
- __func__, timeout, divisor, count, count/divisor);
- count /= divisor; /*根據Prescaler value計算出新的計數器值*/
- wdt_count = count; /*保存計數器值*/
- /* update the pre-scaler */
- /*
- *設置預分頻計數器和數據寄存器
- * NOTE:此時並未使能看門狗定時器
- */
- wtcon = readl(wdt_base + S3C2410_WTCON);
- wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
- wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
- writel(count, wdt_base + S3C2410_WTDAT);
- writel(wtcon, wdt_base + S3C2410_WTCON);
- return 0;
- }
形參timeout為定時時間,單位為秒。
這裏唯一需要註意的是freq/= 128這一步。在第2節我們看到,通過MUX,可選擇的分頻系數為16,32,64和128,但是在這裏驅動直接使用了128來計算系數。
在下一節我們將會看到,驅動為什麽在這裏只使用了128這個分頻系數。
當該函數調用結束時,Prescalervalue 和計數器值都將計算完成,並寫入寄存器。
3.3 定時器的啟動、停止和保活
3.3.1 停止
定時器的停止由 s3c2410wdt_stop函數完成。
- static void __s3c2410wdt_stop(void)
- {
- unsigned long wtcon;
- wtcon = readl(wdt_base + S3C2410_WTCON);
- /*禁止看門狗,禁止RESET*/
- wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
- writel(wtcon, wdt_base + S3C2410_WTCON);
- }
- static void s3c2410wdt_stop(void)
- {
- spin_lock(&wdt_lock);
- __s3c2410wdt_stop();
- spin_unlock(&wdt_lock);
- }
3.3.2 啟動
定時器的啟動由s3c2410wdt_start函數完成。
- static void s3c2410wdt_start(void)
- {
- unsigned long wtcon;
- spin_lock(&wdt_lock);
- __s3c2410wdt_stop(); ./*先禁止看門狗*/
- wtcon = readl(wdt_base + S3C2410_WTCON); /*讀取控制寄存器*/
- /*啟動定時器,設置分頻系數為128*/
- wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
- if (soft_noboot) { /*判斷許是否需要RESET*/
- wtcon |= S3C2410_WTCON_INTEN; /*使能看門狗中斷*/
- wtcon &= ~S3C2410_WTCON_RSTEN; /*取消RESET*/
- } else { /*復位*/
- wtcon &= ~S3C2410_WTCON_INTEN; /*禁止看門狗中斷*/
- wtcon |= S3C2410_WTCON_RSTEN; /*設置RESET*/
- }
- DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
- __func__, wdt_count, wtcon);
- writel(wdt_count, wdt_base + S3C2410_WTDAT);
- writel(wdt_count, wdt_base + S3C2410_WTCNT);
- /*寫入控制器,此時將啟動看門狗定時器*/
- writel(wtcon, wdt_base + S3C2410_WTCON);
- spin_unlock(&wdt_lock);
- }
在這裏我們到了一個宏S3C2410_WTCON_DIV128,這裏設置了分頻系數為128。而s3c2410wdt_start函數的調用肯定在s3c2410wdt_set_heartbeat之後,這也就是為什麽在3.2節中使用了freq/= 128這一步。
3.3.3 保活
定時器的保活由s3c2410wdt_keepalive函數完成。
- static void s3c2410wdt_keepalive(void)
- {
- spin_lock(&wdt_lock);
- writel(wdt_count, wdt_base + S3C2410_WTCNT); /*重置計數器值*/
- spin_unlock(&wdt_lock);
- }
最後需要說明的是,從3.1節probe函數的執行來看,由於tmr_atboot變量的初值為0,因此看門狗定時器是沒有工作的。
3.4 看門狗驅動API
看門狗驅動提供的API如下:
- static const struct file_operations s3c2410wdt_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .write = s3c2410wdt_write,
- .unlocked_ioctl = s3c2410wdt_ioctl,
- .open = s3c2410wdt_open,
- .release = s3c2410wdt_release,
- };
我們可以看到驅動提供了4個API,同時,驅動並不支持llseek方法。
3.4.1 open方法
- static int s3c2410wdt_open(struct inode *inode, struct file *file)
- {
- if (test_and_set_bit(0, &open_lock))/*看門狗設備文件只能open一次*/
- return -EBUSY;
- if (nowayout)
- __module_get(THIS_MODULE); /*增加模塊引用計數*/
- allow_close = CLOSE_STATE_NOT; /*設置標誌位,不允許關閉看門狗*/
- /* start the timer */
- s3c2410wdt_start(); /*啟動定時器*/
- return nonseekable_open(inode, file); /*告知內核不支持llseek操作*/
- }
這裏需要註意的是,設備文件/dev/watchdog 只能被open一次,大於一次的open都將返回-EBUSY。
3.4.2 release方法
- static int s3c2410wdt_release(struct inode *inode, struct file *file)
- {
- /*
- * Shut off the timer.
- * Lock it in if it‘s 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);
- return 0;
- }
3.4.3 wirte方法
- static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
- size_t len, loff_t *ppos)
- {
- /*
- * Refresh the timer.
- */
- if (len) {
- /*nowayout 為真,不允許看門狗停止,使其保活*/
- 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)) /*從用戶空間獲取一個字節的數據*/
- return -EFAULT;
- /*讀取到字符V,設置標誌位,允許關閉看門狗*/
- if (c == ‘V‘)
- allow_close = CLOSE_STATE_ALLOW;
- }
- }
- s3c2410wdt_keepalive(); /*保活*/
- }
- return len;
- }
只要寫入數據的長度不為0,都會調用s3c2410wdt_keepalive函數來重置定時器。
3.4.4 unlocked_ioctl方法
- 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;
- }
- }
4. 測試程序
- int main(void)
- {
- int fd, val, ret;
- fd = open("/dev/watchdog", O_RDWR);
- if(fd < 0){
- printf("open device fail\n");
- return -1;
- }
- while(1){
- ret = write(fd, &val, sizeof(val));
- if(ret < 0){
- perror("watchdog write wrong\n");
- return -1;
- }
- sleep(5);
- }
- return 0;
- }
該測試程序每隔5秒重置看門狗定時器,而驅動默認的超時時間是15秒。
可以將5秒替換為16秒,你會發現系統自動重啟了。
5. 結束語
本文主要對基於S3C2440的看門狗驅動作出了分析。該驅動只是一個簡單的字符設備,比較簡單。其中,用於計算預分頻系數的s3c2410wdt_set_heartbeat函數比較關鍵,讀者可以好好琢磨下該系數是如何計算出來的。
Thank you for your time。
2013.1.30 添加測試程序
基於S3C2440的嵌入式Linux驅動——看門狗(watchdog)驅動解讀