歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> S3C6410在Linux下的WATCHDOG TIMER(看門狗定時器)驅動

S3C6410在Linux下的WATCHDOG TIMER(看門狗定時器)驅動

日期:2017/3/1 10:22:31   编辑:Linux編程

如果對看門狗定時器的硬件不太熟悉,可以看這篇文章《S3C6410硬件WATCHDOG TIMER(看門狗定時器)》見 http://www.linuxidc.com/Linux/2012-05/60749.htm。

還是先說下整體結構,又要說到大家很熟悉的平台設備了,同樣看門狗定時器也是作為平台設備存在的,但與以前的不同的地方是,看門狗定時器是一種混雜設備,先介紹下混雜設備。

1、混雜設備

1.1、混雜設備並沒有明確的定義。它的主設備號是10,不同的設備用次設備號區分。混雜設備用結構體miscdevice表示,源碼如下:

struct miscdevice {
int minor; 次設備號
const char *name; 設備名
const struct file_operations *fops;設備的操作函數,與字符設備相同
struct list_head list; 鏈接混雜設備的鏈表
struct device *parent;指向父設備
struct device *this_device;指向設備結構體
};

對應的看下看門狗中的定義:

在S3c2410_wdt.c (linux2.6.28\drivers\watchdog)文件中

static struct miscdevice s3c2410wdt_miscdev = {
.minor = WATCHDOG_MINOR,
.name = "watchdog",
.fops = &s3c2410wdt_fops,
};

其中還有:

/* kernel interface */
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,
};

1.2、混雜設備的注冊和注銷

內核提供了misc_register用於注冊一個混雜設備。

/**
* misc_register-register a miscellaneous device
* @misc: device structure

int misc_register(struct miscdevice * misc)

同樣對應的內核提供了misc_deregister用於混雜設備的注銷。

/**
* misc_deregister - unregister a miscellaneous device
* @misc: device to unregister

int misc_deregister(struct miscdevice *misc)

2、再來看看門狗定時器作為平台設備存在

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);
}
static void __exit watchdog_exit(void)
{
platform_driver_unregister(&s3c2410wdt_driver);
}
module_init(watchdog_init);
module_exit(watchdog_exit);

看到這些,你可能都看膩了。沒辦法,接著來,平台設備的資源定義在

Devs.c (linux2.6.28\arch\arm\plat-s3c64xx)文件中:

/* Watchdog */
static struct resource s3c_wdt_resource[] = {
[0] = {
.start = S3C64XX_PA_WATCHDOG,
.end = S3C64XX_PA_WATCHDOG + S3C64XX_SZ_WATCHDOG - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_WDT,
.end = IRQ_WDT,
.flags = IORESOURCE_IRQ,
}
};

平台設備定義在同一個文件中:

struct platform_device s3c_device_wdt = {
.name = "s3c2410-wdt",
.id = -1,
.num_resources = ARRAY_SIZE(s3c_wdt_resource),
.resource = s3c_wdt_resource,
};

EXPORT_SYMBOL(s3c_device_wdt);

3、S3c2410_wdt.c (linux2.6.28\drivers\watchdog)文件開頭一些變量的含義(參考於網絡和書籍),如下所示:

(1)、static int nowayout = WATCHDOG_NOWAYOUT;
nowayout為1表示不允許關閉看門狗,為0表示可以關閉。
與CONFIG_WATCHDOG_NOWAYOUT配置相關,當配置為不允許關閉時,應用層調用close函數將不能關閉看門狗
#ifdef CONFIG_WATCHDOG_NOWAYOUT
#define WATCHDOG_NOWAYOUT	1
#else
#define WATCHDOG_NOWAYOUT	0
#endif

(2)、
static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;默認的喂狗時間
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME  (15)
(3)、static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;表示系統啟動時就能使用看門狗。為1表示能,為0表示不能。
 #define CONFIG_S3C2410_WATCHDOG_ATBOOT  (0)
(4)、static int soft_noboot;表示看門狗的工作方式,看門狗可以作為定時器使用,也可以作為復位使用。soft_noboot為1時看門狗將作為一般的中斷定時器使用,為0時作為可復位電路的看門狗,默認為0
(5)、static int debug;  調式模式
(6)、
module_param(tmr_margin,  int, 0);
module_param(tmr_atboot,  int, 0);
module_param(nowayout,    int, 0);
module_param(soft_noboot, int, 0);
module_param(debug,	  int, 0);
驅動程序模塊參數,如果在加載驅動模塊時沒有設定這些參數,則這些參數將采用默認值。
(7)、
typedef enum close_state {
	CLOSE_STATE_NOT,不允許關閉
	CLOSE_STATE_ALLOW = 0x4021允許關閉
} close_state_t;
用來標識看門狗是否允許關閉。
4、萬變不離其宗,下一篇就要說平台設備對應的probe函數了,你做好准備了嗎?下篇見。
Copyright © Linux教程網 All Rights Reserved