AML_看门狗驱动分析

看门狗的工作原理是,定时器中断,设定的时间递减完了,如果此时没有重新启动定时器开始计数,也就是所谓的“喂狗”,模块就会拉低引脚,让平台重启复位

看linux driver从module_init函数开始

module_init(aml_wdt_driver_init);

里面注册了一个平台设备驱动

static struct platform_driver aml_wdt_driver = {
    .probe      = aml_wdt_probe,
    .remove     = aml_wdt_remove,
    .shutdown   = aml_wdt_shutdown,
    .suspend    = aml_wdt_suspend,
    .resume     = aml_wdt_resume,
    .driver     = {
        .owner  = THIS_MODULE,
        .name   = "aml_wdt",
        .of_match_table = aml_wdt_of_match,
    },
}

模块在上电匹配设备层也就是dtd的compatible后,运行probe回调函数

static int aml_wdt_probe(struct platform_device *pdev)
{
    struct watchdog_device *aml_wdt; /*watchdog的核心结构体*/
    struct aml_wdt_dev *wdev;/*pri data*/
    int ret;
    aml_wdt = devm_kzalloc(&pdev->dev, sizeof(*aml_wdt), GFP_KERNEL);/*alloc space*/
    if (!aml_wdt)
        return -ENOMEM;

    wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
    if (!wdev)
        return -ENOMEM;
    wdev->dev       = &pdev->dev;
    mutex_init(&wdev->lock); /*init mutex lock*/
    aml_init_pdata(wdev); /*init platform data*/

    aml_wdt->info         = &aml_wdt_info;
    aml_wdt->ops          = &aml_wdt_ops;/*ops is important*/
    aml_wdt->min_timeout = wdev->min_timeout;
    aml_wdt->max_timeout = wdev->max_timeout;
    aml_wdt->timeout = 0xffffffff;
    wdev->timeout = 0xffffffff;

    watchdog_set_drvdata(aml_wdt, wdev);
    platform_set_drvdata(pdev, aml_wdt);/*save 平台数据*/
    if (wdev->reset_watchdog_method == 1) {

        INIT_DELAYED_WORK(&wdev->boot_queue, boot_moniter_work);
        mod_delayed_work(system_freezable_wq, &wdev->boot_queue,
     round_jiffies(msecs_to_jiffies(wdev->reset_watchdog_time*1000)));
        enable_watchdog(wdev);/*启动定时器用到喂狗*/
        set_watchdog_cnt(wdev,
                 wdev->default_timeout * wdev->one_second);/*设置喂狗时间间隔*/
        dev_info(wdev->dev, "creat work queue for watch dog\n");
    }
    ret = watchdog_register_device(aml_wdt);/*注册接口*/
    if (ret)
        return ret;
    awdtv = wdev;
    register_pm_notifier(&aml_wdt_pm_notifier);
    register_reboot_notifier(&aml_wdt_reboot_notifier);
    dev_info(wdev->dev, "AML Watchdog Timer probed done\n");

    return 0;
}

这样timer会每隔一段时间去喂狗,如果系统繁忙或者其他原因造成系统卡顿没有喂狗,那系统就会被看门狗拉低重启

static void boot_moniter_work(struct work_struct *work)
{
    struct aml_wdt_dev *wdev = container_of(work, struct aml_wdt_dev,
                            boot_queue.work);
    reset_watchdog(wdev); /*喂狗*/
    mod_delayed_work(system_freezable_wq, &wdev->boot_queue,
    round_jiffies(msecs_to_jiffies(wdev->reset_watchdog_time*1000)));/*延时*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值