|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Qingsong Gou <[email protected]> |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +#define DT_DRV_COMPAT sifli_sf32lb_wdt |
| 6 | + |
| 7 | +#include <zephyr/arch/cpu.h> |
| 8 | +#include <zephyr/drivers/watchdog.h> |
| 9 | +#include <zephyr/logging/log.h> |
| 10 | + |
| 11 | +#include <register.h> |
| 12 | + |
| 13 | +LOG_MODULE_REGISTER(wdt_sf32lb, CONFIG_WDT_LOG_LEVEL); |
| 14 | + |
| 15 | +#define WDT_CVR0 offsetof(WDT_TypeDef, WDT_CVR0) |
| 16 | +#define WDT_CR offsetof(WDT_TypeDef, WDT_CR) |
| 17 | +#define WDT_CCR offsetof(WDT_TypeDef, WDT_CCR) |
| 18 | + |
| 19 | +#define WDT_CMD_START 0x00000076U |
| 20 | +#define WDT_CMD_STOP 0x00000034U |
| 21 | + |
| 22 | +#define WDT_CVR0_MAX 0xFFFFFF |
| 23 | + |
| 24 | +#define WDT_WDT_CR_RESPONSE_MODE1 0U |
| 25 | + |
| 26 | +/* Assume LRC10 clocks WDT (LRC32 support to be added in the future) */ |
| 27 | +#define WDT_CLK_KHZ 10 |
| 28 | + |
| 29 | +#define WDT_WINDOW_MS_MAX (WDT_CVR0_MAX / WDT_CLK_KHZ) |
| 30 | + |
| 31 | +struct wdt_sf32lb_config { |
| 32 | + uintptr_t base; |
| 33 | +}; |
| 34 | + |
| 35 | +static int wdt_sf32lb_setup(const struct device *dev, uint8_t options) |
| 36 | +{ |
| 37 | + const struct wdt_sf32lb_config *config = dev->config; |
| 38 | + |
| 39 | + if (options != 0U) { |
| 40 | + LOG_ERR("Options not supported"); |
| 41 | + return -ENOTSUP; |
| 42 | + } |
| 43 | + |
| 44 | + sys_write32(WDT_CMD_START, config->base + WDT_CCR); |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static int wdt_sf32lb_disable(const struct device *dev) |
| 50 | +{ |
| 51 | + const struct wdt_sf32lb_config *config = dev->config; |
| 52 | + |
| 53 | + sys_write32(WDT_CMD_STOP, config->base + WDT_CCR); |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +static int wdt_sf32lb_install_timeout(const struct device *dev, |
| 59 | + const struct wdt_timeout_cfg *wdt_cfg) |
| 60 | +{ |
| 61 | + const struct wdt_sf32lb_config *config = dev->config; |
| 62 | + |
| 63 | + if (wdt_cfg->flags != WDT_FLAG_RESET_SOC) { |
| 64 | + LOG_ERR("Only SoC reset supported"); |
| 65 | + return -ENOTSUP; |
| 66 | + } |
| 67 | + |
| 68 | + if (wdt_cfg->callback != NULL) { |
| 69 | + LOG_ERR("Callback not supported"); |
| 70 | + return -ENOTSUP; |
| 71 | + } |
| 72 | + |
| 73 | + if (wdt_cfg->window.min != 0U) { |
| 74 | + LOG_ERR("Window mode not supported!"); |
| 75 | + return -ENOTSUP; |
| 76 | + }; |
| 77 | + |
| 78 | + if (wdt_cfg->window.max > WDT_WINDOW_MS_MAX) { |
| 79 | + return -EINVAL; |
| 80 | + } |
| 81 | + |
| 82 | + sys_write32(wdt_cfg->window.max * WDT_CLK_KHZ, config->base + WDT_CVR0); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +static int wdt_sf32lb_feed(const struct device *dev, int channel_id) |
| 88 | +{ |
| 89 | + const struct wdt_sf32lb_config *config = dev->config; |
| 90 | + |
| 91 | + sys_write32(WDT_CMD_START, config->base + WDT_CCR); |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +static DEVICE_API(wdt, wdt_sf32lb_api) = { |
| 97 | + .setup = wdt_sf32lb_setup, |
| 98 | + .disable = wdt_sf32lb_disable, |
| 99 | + .install_timeout = wdt_sf32lb_install_timeout, |
| 100 | + .feed = wdt_sf32lb_feed, |
| 101 | +}; |
| 102 | + |
| 103 | +static int wdt_sf32lb_init(const struct device *dev) |
| 104 | +{ |
| 105 | + const struct wdt_sf32lb_config *config = dev->config; |
| 106 | + uint32_t cr; |
| 107 | + |
| 108 | + cr = sys_read32(config->base + WDT_CR); |
| 109 | + cr &= ~WDT_WDT_CR_RESPONSE_MODE_Msk; |
| 110 | + cr |= WDT_WDT_CR_RESPONSE_MODE1; |
| 111 | + sys_write32(cr, config->base + WDT_CR); |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
| 115 | + |
| 116 | +#define WDT_SF32LB_INIT(index) \ |
| 117 | + static const struct wdt_sf32lb_config wdt_sf32lb_config_##index = { \ |
| 118 | + .base = DT_INST_REG_ADDR(index), \ |
| 119 | + }; \ |
| 120 | + DEVICE_DT_INST_DEFINE(index, wdt_sf32lb_init, NULL, NULL, \ |
| 121 | + &wdt_sf32lb_config_##index, POST_KERNEL, \ |
| 122 | + CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &wdt_sf32lb_api); |
| 123 | + |
| 124 | +DT_INST_FOREACH_STATUS_OKAY(WDT_SF32LB_INIT) |
0 commit comments