先给出头文件、宏定义及变量定义
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/timer.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/arch/map.h>
/* 定义超时时间为10s */
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (10)
/* 看门狗3个寄存器 */
volatile unsigned long *wtcon, *wtdat, *wtcnt;
static struct resource *wdt_mem;static struct clk *wdt_clock;
static void __iomem *wdt_base;
1. 定义并初始化看门狗的platfom_driver,编写入口出口函数
/* 定义platform driver */
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 int __init watchdog_init(void)
{
return platform_driver_register(&s3c2410wdt_driver); //注册platform driver
}
static void __exit watchdog_exit(void)
{
platform_driver_unregister(&s3c2410wdt_driver); ////注销platform driver
}
module_init(watchdog_init);
module_exit(watchdog_exit);
MODULE_LICENSE("GPL");
注意platfom_driver的名字“s3c2410-wdt”要和platform_device的名字一致。入口和出口函数只需要注册和注销platform_driver。
2. 定义并初始化看门狗杂项设备和文件接口
static const struct file_operations s3c2410wdt_fops = {
.owner = THIS_MODULE,
.write = s3c2410wdt_write,
.open = s3c2410wdt_open,
.release = s3c2410wdt_release,
};
static struct miscdevice s3c2410wdt_miscdev = {
.minor = WATCHDOG_MINOR,
.name = "watchdog",
.fops = &s3c2410wdt_fops,
};
3. 看门狗硬件操作函数:包含启动和停止看门狗、喂狗、设置超时时间,代码如下:
/* 喂狗 */
static int s3c2410wdt_keepalive(void)
{
writel(wdt_count, wtcnt);
return 0;
}
/* 停止看门狗 */
static int s3c2410wdt_stop(void)
{
unsigned long val;
val = readl(wtcon);
val &= ~( (1<<5) | (1<<0) );
writel(val, wtcon);
return 0;
}
/* 启动看门狗 */
static int s3c2410wdt_start(void)
{
unsigned long val;
s3c2410wdt_stop();
val = readl(wtcon);
val |= (1<<5) | (3<<3);
/* 禁止中断,超时产生复位信号 */
val &= ~(1<<2);
val |= (0x01);
writel(wdt_count, wtdat);
writel(wdt_count, wtcnt);
writel(val, wtcon);
return 0;
}
/* 设置看门狗超时时间 */
static int s3c2410wdt_set_heartbeat(int timeout)
{
unsigned int freq = clk_get_rate(wdt_clock);
unsigned int count;
unsigned int divisor = 1;
unsigned long val;
if (timeout < 1)
return -EINVAL;
freq /= 128;
/* 根据timeout值和频率计算计数值 */
count = timeout * freq;
printk("count=%d, timeout=%d, freq=%d\n", count, timeout, freq);
/* 计算分频值和计数值 */
if (count >= 0x10000) {
for (divisor = 1; divisor <= 0x100; divisor++) {
if ((count / divisor) < 0x10000)
break;
}
if ((count / divisor) >= 0x10000) {
printk("timeout %d too big\n", timeout);
return -EINVAL;
}
}
count /= divisor;
/* 设置计数值和分频值 */
val = readl(wtcon);
val &= ~(0xff00);
val |= ((divisor-1) << 8);
writel(count, wtdat);
writel(val, wtcon);
return 0;
}
4. probe函数实现
static int s3c2410wdt_probe(struct platform_device *pdev)
{
struct resource *res;
int started = 0;
int ret;
int size;
printk("find wdt\n");
/* 获取wdt资源 */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
printk("failed to get memory region resouce\n");
return -ENOENT;
}
size = (res->end - res->start) + 1;
wdt_mem = request_mem_region(res->start, size, pdev->name);
if (wdt_mem == NULL) {
printk("failed to get memory region\n");
ret = -ENOENT;
goto err_req;
}
wdt_base = ioremap(res->start, size);//物理地址映射到虚拟地址
if (wdt_base == NULL) {
printk("failed to ioremap() region\n");
ret = -EINVAL;
goto err_req;
}
/* 初始化寄存器地址 */
wtcon = wdt_base;
wtdat = wtcon + 1;
wtcnt = wtdat + 1;
printk("probe: mapped wtcon=%p, wtdat=%p, wtcnt=%p \n", wtcon, wtdat, wtcnt);
/* 获取并使能看门狗时钟 */
wdt_clock = clk_get(&pdev->dev, "watchdog");
if (IS_ERR(wdt_clock)) {
printk("failed to find watchdog clock source\n");
ret = PTR_ERR(wdt_clock);
goto err_map;
}
clk_enable(wdt_clock);
started = s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);if (started == 0) {
printk("tmr_margin value out of range, default %d used\n",
CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
}
else {
printk("default timer value is out of range, cannot start\n");
}
/* 将看门狗注册为一个混杂设备 */
ret = misc_register(&s3c2410wdt_miscdev);
if (ret) {
printk ("cannot register miscdev on minor=%d (%d)\n",
WATCHDOG_MINOR, ret);
goto err_clk;
}
/* 先关闭看门狗 */
s3c2410wdt_stop();
printk("Stopping Watchdog Timer\n");
return 0;
err_clk:
clk_disable(wdt_clock);
clk_put(wdt_clock);
err_map:
iounmap(wdt_base);
err_req:
release_resource(wdt_mem);
kfree(wdt_mem);
return ret;
}
probe函数中获取platform_device提供的资源,并将物理内存映射到虚拟内存,设置看门狗3个寄存器的地址;
为了使看门狗工作,还需要获取并使能看门狗时钟,设置超时时间;
为了实现看门狗的文件接口,还需要将其注册为一个混杂设备;
最后为了避免上电后自动重启,我们先要将看门狗关闭。
5. platform_driver其他成员函数代码:
static int s3c2410wdt_remove(struct platform_device *dev)
{
/* 释放内存 */
release_resource(wdt_mem);
kfree(wdt_mem);
wdt_mem = NULL;
/* 关闭时钟 */
clk_disable(wdt_clock);
clk_put(wdt_clock);
wdt_clock = NULL;
/* 解除物理内存到虚拟内存映射,注销杂项设备 */
iounmap(wdt_base);
misc_deregister(&s3c2410wdt_miscdev);
return 0;
}
static void s3c2410wdt_shutdown(struct platform_device *dev){
s3c2410wdt_stop(); //停止看门狗
}
static unsigned long wtcon_save;
static unsigned long wtdat_save;
static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
{
wtcon_save = readl(wtcon);
wtdat_save = readl(wtdat);
s3c2410wdt_stop();
return 0;
}
static int s3c2410wdt_resume(struct platform_device *dev)
{
writel(wtdat_save, wtdat);
writel(wtdat_save, wtcnt);
writel(wtcon_save, wtcon);
printk("watchdog %sabled\n",
(wtcon_save & (1<<5)) ? "en" : "dis");
return 0;
}
s3c2410wdt_remove函数释放注销相关资源,s3c2410wdt_shutdown函数关闭看门狗,s3c2410wdt_suspend函数保存看门狗寄存器值并停止看门狗,s3c2410wdt_resume恢复看门狗寄存器和状态。
6. 看门狗文件接口函数:
static int s3c2410wdt_open(struct inode *inode, struct file *file)
{s3c2410wdt_start();
return 0;
}
static int s3c2410wdt_release(struct inode *inode, struct file *file)
{
s3c2410wdt_stop();
return 0;
}
static ssize_t s3c2410wdt_write(struct file *file, const char __user *data, size_t len, loff_t *ppos)
{
char c;
unsigned long cnt;
cnt = copy_from_user(&c, (void *)data, 1);
if(cnt != 0)
return -1;
/* 用户写1则喂狗 */
if(c == '1')
s3c2410wdt_keepalive();
return 1;
}
open和release函数只需要简单的启动和停止看门狗即可, write函数接收用户写的数据,若用户写字符'1',则喂狗,写其他字符则什么也不做。
本文介绍了Linux环境下,针对S3C2410处理器的看门狗驱动程序设计,包括头文件、宏定义、变量初始化,以及平台驱动的注册和注销。详细讲解了看门狗的启动、停止、喂狗、设置超时时间等硬件操作函数,同时阐述了probe函数实现,以及如何处理平台设备资源、内存映射和时钟。此外,还涉及看门狗的文件接口函数,如open、write、release等功能。
 代码设计&spm=1001.2101.3001.5002&articleId=80872316&d=1&t=3&u=ed29f6ebc4904a78924e45d973971780)
2325

被折叠的 条评论
为什么被折叠?



