看門狗的驅動一般來說比較簡單,只要做寄存器的設置實現開啓、關閉、喂狗功能。本項目中我們使用的是at91sam9g45處理器,帶有看門狗定時器。這個看門狗的驅動卻比較複雜,應用層想用它的話,將涉及到boot引導設置,uboot配置及驅動,改寫驅動程序。下面將逐步說明。
1、boot引導(bootstrap-v1.14)
由於該看門狗的MR寄存器只能寫一次(Only a processor reset resets it.),而默認情況下看門狗在boot引導程序中被關閉了,所以在boot引導程序中我們要開啓看門狗。在at91sam9g45ekes.c文件的硬件初始化函數hw_init中註釋掉下面的配置即可開啓看門狗:
/* writel(AT91C_WDTC_WDDIS, AT91C_BASE_WDTC + WDTC_WDMR); */
爲了功能設置:我們配置如下:
writel(AT91C_WDTC_WDV | AT91C_WDTC_WDD | AT91C_WDTC_WDRSTEN | AT91C_WDTC_WDFIEN, AT91C_BASE_WDTC + WDTC_WDMR);
2、uboot配置及驅動(uboot-v1.3.4):
默認情況下,看門狗在uboot中沒有配置,需要手動添加配置,在文件include/configs/at91sam9m10g45ek.h中添加如下配置
#define CONFIG_HW_WATCHDOG 1
#define CONFIG_AT91SAM9_WATCHDOG 1
此時編譯uboot,會提示你找不到hw_watchdog_reset復位函數,這是因爲雖然我們配置看門狗,但看門狗的uboot驅動並不存在,下面就來添加uboot下的看門狗驅動。
1)添加 include/asm-arm/arch-at91sam9/at91_wdt.h,內容如下
/*
* [origin: Linux kernel arch/arm/mach-at91/include/mach/at91_wdt.h]
*
* Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
* Copyright (C) 2007 Andrew Victor
* Copyright (C) 2007 Atmel Corporation.
*
* Watchdog Timer (WDT) - System peripherals regsters.
* Based on AT91SAM9261 datasheet revision D.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef AT91_WDT_H
#define AT91_WDT_H
#define AT91_WDT_CR (AT91_WDT + 0x00) /* Watchdog Control Register */
#define AT91_WDT_WDRSTT (1 << 0) /* Restart */
#define AT91_WDT_KEY (0xa5 << 24) /* KEY Password */
#define AT91_WDT_MR (AT91_WDT + 0x04) /* Watchdog Mode Register */
#define AT91_WDT_WDV (0xfff << 0) /* Counter Value */
#define AT91_WDT_WDFIEN (1 << 12) /* Fault Interrupt Enable */
#define AT91_WDT_WDRSTEN (1 << 13) /* Reset Processor */
#define AT91_WDT_WDRPROC (1 << 14) /* Timer Restart */
#define AT91_WDT_WDDIS (1 << 15) /* Watchdog Disable */
#define AT91_WDT_WDD (0xfff << 16) /* Delta Value */
#define AT91_WDT_WDDBGHLT (1 << 28) /* Debug Halt */
#define AT91_WDT_WDIDLEHLT (1 << 29) /* Idle Halt */
#define AT91_WDT_SR (AT91_WDT + 0x08) /* Watchdog Status Register */
#define AT91_WDT_WDUNF (1 << 0) /* Watchdog Underflow */
#define AT91_WDT_WDERR (1 << 1) /* Watchdog Error */
#endif
2)添加drivers/watchdog/at91sam9_wdt.c,內容如下
/*
* [origin: Linux kernel drivers/watchdog/at91sam9_wdt.c]
*
* Watchdog driver for Atmel AT91SAM9x processors.
*
* Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
* Copyright (C) 2008 Renaud CERRATO r.cerrato at til-technologies.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
/*
* The Watchdog Timer Mode Register can be only written to once. If the
* timeout need to be set from U-Boot, be sure that the bootstrap doesn't
* write to this register. Inform Linux to it too
*/
#include <common.h>
#include <watchdog.h>
#include <asm/arch/hardware.h>
#include <asm/arch/io.h>
#include <asm/arch/at91_wdt.h>
/*
* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
* use this to convert a watchdog
* value from/to milliseconds.
*/
#define ms_to_ticks(t) (((t << 8) / 1000) - 1)
#define ticks_to_ms(t) (((t + 1) * 1000) >> 8)
/* Hardware timeout in seconds */
#define WDT_HW_TIMEOUT 2
/*
* Set the watchdog time interval in 1/256Hz (write-once)
* Counter is 12 bit.
*/
static int at91_wdt_settimeout(unsigned int timeout)
{
unsigned int reg;
unsigned int mr;
/* Check if disabled */
mr = at91_sys_read(AT91_WDT_MR);
if (mr & AT91_WDT_WDDIS) {
printf("sorry, watchdog is disabled/n");
return -1;
}
/*
* All counting occurs at SLOW_CLOCK / 128 = 256 Hz
*
* Since WDV is a 12-bit counter, the maximum period is
* 4096 / 256 = 16 seconds.
*/
reg = AT91_WDT_WDRSTEN /* causes watchdog reset */
/* | AT91_WDT_WDRPROC causes processor reset only */
| AT91_WDT_WDDBGHLT /* disabled in debug mode */
| AT91_WDT_WDD /* restart at any time */
| (timeout & AT91_WDT_WDV); /* timer value */
at91_sys_write(AT91_WDT_MR, reg);
return 0;
}
void hw_watchdog_reset(void)
{
at91_sys_write(AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
}
void hw_watchdog_init(void)
{
/* 16 seconds timer, resets enabled */
at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000));
}
3)添加drivers/watchdog/Makefile
#
# (C) Copyright 2008
# Wolfgang Denk, DENX Software Engineering, wd at denx.de.
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
include $(TOPDIR)/config.mk
LIB := $(obj)libwatchdog.a
COBJS-$(CONFIG_AT91SAM9_WATCHDOG) += at91sam9_wdt.o
COBJS := $(COBJS-y)
SRCS := $(COBJS:.o=.c)
OBJS := $(addprefix $(obj),$(COBJS))
all: $(LIB)
$(LIB): $(obj).depend $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)
#########################################################################
# defines $(obj).depend target
include $(SRCTREE)/rules.mk
sinclude $(obj).depend
#########################################################################
4)修改uboot的Makefile,主要是把watchdog編輯到工程裏
修改1:
LIBS += drivers/video/libvideo.a
+LIBS += drivers/watchdog/libwatchdog.a(添加)
LIBS += common/libcommon.a
修改2:
TAG_SUBDIRS += drivers/usb
TAG_SUBDIRS += drivers/video
+TAG_SUBDIRS += drivers/watchdog (添加)
5)修改串口驅動drivers/serial/atmel_usart.c,主要是在串口讀的時候復位看門狗,防止系統重啓
int serial_getc(void)
{
- while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) ; (刪除)
+ while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) (添加)
+ WATCHDOG_RESET(); (添加)
return usart3_readl(RHR);
}
3、改寫驅動程序(linux2.6.30內核)
1、配置內核
在默認情況,系統並不加載看門狗驅動,需要配置內核:make menuconfig
Device drivers-->Watchdog Timer Support-->AT91SAM9 watchdog
注:在筆者的at91sam9g45處理器,編譯後配置編譯後還是不能加載,後花了很久時間才找出原因,看門狗配置項如下:
CONFIG_AT91SAM9X_WATCHDOG=y (.config文件)
而平臺設備加載中(arch/arm/mach-at91/at91sam9g45_devices.c文件)
#if defined(CONFIG_AT91SAM9_WATCHDOG) || defined(CONFIG_AT91SAM9_WATCHDOG_MODULE)
static struct platform_device at91sam9g45_wdt_device = {
.name = "at91_wdt",
.id = -1,
.num_resources = 0,
};
static void __init at91_add_device_watchdog(void)
{
platform_device_register(&at91sam9g45_wdt_device);
}
#else
static void __init at91_add_device_watchdog(void) {}
#endif
宏定義和配置項不符,難怪加載不進去,修改CONFIG_AT91SAM9_WATCHDOG爲CONFIG_AT91SAM9X_WATCHDOG
正常啓動,系統/dev目錄下有watchdog設備
2、修改驅動
由於,該系統看門狗在處理器復位時纔可以設置模式寄存器(MR),所以看門狗一旦開啓,就不能關閉。
查看看門狗驅動(drivers/watchdog/at91sam9_wdt.c),我們發現驅動在平臺驅動註冊的時候at91wdt_driver函數中調用了:
setup_timer(&at91wdt_private.timer, at91_ping, 0);
mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
調用內核定時器定時復位看門狗。當應用程序要使用看門狗時,可以從內核定時器列表中刪除看門狗定時器,然後手動定時復位看門狗;當應用程序不使用時,再將看門狗定時器添加到內核定時器中。
這些操作我們放在IOCTL中,代碼如下(at91_wdt_ioctl函數):
switch(cmd) {
case WDIOC_SETON: //"開啓"看門狗自動定時復位
setup_timer(&at91wdt_private.timer, at91_ping, 0);
mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
return 0;
case WDIOC_SETOFF: //"關閉"看門狗自動定時復位,變位手動復位
del_timer(&at91wdt_private.timer);
return 0;
case WDIOC_KEEPALIVE:
//at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
at91_wdt_reset();
return 0;
.....
至此,全部結束,雖然該驅動並不涉及複雜的時序操作、中斷處理,連寄存器就僅有3個(控制、模式、狀態各一個),但是這個驅動涉及了uboot+linux啓動的各個過程,對於第一個驅動能有這樣的機會,對於系統的理解有莫大的好處。