mini6410 的看門狗驅動位於 linux/drivers/char/watchdog/s3c2410_wdt.c。這份代碼是理解 Linux 字符驅動、平台驅動與電源管理如何疊在同一個模塊上的好材料,下面按功能逐層拆解。
S3C2410 的看門狗同時是字符設備、混雜設備(miscdevice)與平台設備。對上,它以 /dev/watchdog 的形式提供接口;對下,它通過 platform_driver 與板級資源掛接。這種「一體三面」的結構在 ARM 板級驅動中相當常見。
static int nowayout = WATCHDOG_NOWAYOUT;
static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
static int soft_noboot;
static int debug;
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);
含義各不相同:nowayout 為 1 表示看門狗一旦啟動便決不允許關閉;tmr_margin 是默認喂狗周期,這裡取 15 秒;tmr_atboot 決定是否開機即啟動;soft_noboot 選擇看門狗的工作方式——為 1 時它只是一個普通定時器,不發出復位信號;debug 打開後會輸出調試信息。
喂狗就是把計數器重裝一遍:
static void s3c2410wdt_keepalive(void)
{
spin_lock(&wdt_lock);
writel(wdt_count, wdt_base + S3C2410_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);
}
啟動的過程最值得細看:
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) { /* 作為內部定時器 */
wtcon |= S3C2410_WTCON_INTEN;
wtcon &= ~S3C2410_WTCON_RSTEN;
} else { /* 作為復位硬件 */
wtcon &= ~S3C2410_WTCON_INTEN;
wtcon |= S3C2410_WTCON_RSTEN;
}
writel(wdt_count, wdt_base + S3C2410_WTDAT);
writel(wdt_count, wdt_base + S3C2410_WTCNT);
writel(wtcon, wdt_base + S3C2410_WTCON);
spin_unlock(&wdt_lock);
}
這裡有個容易看漏的細節:為什麼要同時往 WTDAT 和 WTCNT 各寫一次?查 datasheet 便知,看門狗定時器初次使能時,WTDAT 的值並不會自動載入計數器 WTCNT,所以必須先把初始值寫進 WTCNT,計時才會按預期開始。
用戶想改變超時時間,最終都落到這個函數上。它先取出看門狗時鐘,除以 128 得到滴答頻率,再乘上秒數得出所需計數值:
static int s3c2410wdt_set_heartbeat(int timeout)
{
unsigned long 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;
/* WTCNT 是 16 位計數器,最大值 0x10000 */
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;
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;
}
計數值一旦超出 16 位能表達的範圍,就靠預分頻器逐步放大分頻係數來「湊」;若一路試到 0x100 仍然放不下,只能報錯拒絕。這個「先算再退而求其次」的思路,在各種定時器驅動裡都能見到。
file_operations 只挂了五個成員,可謂精簡:
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,
};
static struct miscdevice s3c2410wdt_miscdev = {
.minor = WATCHDOG_MINOR,
.name = "watchdog",
.fops = &s3c2410wdt_fops,
};
ioctl 支持一組標準命令:查詢設備信息、喂狗、設置與讀取超時時間,其餘一律返回 -ENOTTY。
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);
若把看門狗配成內部定時器,計數歸零時會觸發中斷,中斷裡做的仍是喂狗:
static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
{
dev_info(wdt_dev, "watchdog timer expired (irq)\n");
s3c2410wdt_keepalive();
return IRQ_HANDLED;
}
此外驅動還註冊了 cpufreq 通知塊:頻率即將變化前先喂一次狗,避免切換過程耗時引發誤復位;變化完成後停下看門狗、重算心跳再啟動。若未開啟 CONFIG_CPU_FREQ,這兩個註冊/去註冊接口被替換成空實現。
probe 是典型的資源申請流水線:取內存資源 → request_mem_region → ioremap 得到 wdt_base → 取中斷資源並 request_irq → clk_get 與 clk_enable → 註冊 cpufreq → 設置心跳 → misc_register。每一步失敗都有對應的 goto 標籤逐層回退,這種「逆序清理」是內核驅動的標準寫法。
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
size = resource_size(res);
wdt_mem = request_mem_region(res->start, size, pdev->name);
wdt_base = ioremap(res->start, size);
末尾它還會讀一次控制寄存器,把當前狀態打印出來,方便開機後一眼確認:
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");
remove 則把順序完全倒過來:注銷混雜設備、去註冊 cpufreq、關時鐘並降引用計數、釋放中斷、iounmap、release_resource,最後 kfree。
掛起時只需保存 WTCON 與 WTDAT 兩個寄存器,WTCNT 不必保存——恢復時它會被重新賦成 WTDAT 的值:
static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
{
wtcon_save = readl(wdt_base + S3C2410_WTCON);
wtdat_save = readl(wdt_base + S3C2410_WTDAT);
s3c2410wdt_stop();
return 0;
}
static int s3c2410wdt_resume(struct platform_device *dev)
{
writel(wtdat_save, wdt_base + S3C2410_WTDAT);
writel(wtdat_save, wdt_base + S3C2410_WTCNT);
writel(wtcon_save, wdt_base + S3C2410_WTCON);
return 0;
}
最後通過 module_init/module_exit 完成平台驅動的註冊與注銷,收尾乾淨。
通讀下來,這份驅動最值得借鑑的有三點:資源申請與回退嚴格成對、對硬件「反直覺」之處(WTCNT 需手動初始化)在註釋裡寫清了原因、以及把同一硬件的三種抽象層次安排得互不干擾。移植到 mini6410 這類板子上時,只要 platform 資源與時鐘源對得上,通常無需大改。