Line 1... |
Line 1... |
-- #################################################################################################
|
-- #################################################################################################
|
-- # << NEORV32 - Watch Dog Timer (WDT) >> #
|
-- # << NEORV32 - Watch Dog Timer (WDT) >> #
|
-- # ********************************************************************************************* #
|
-- # ********************************************************************************************* #
|
-- # The internal counter is 20 bit wide and increases using 1 out of 8 available clock #
|
-- # Watchdog counter to trigger an action if the CPU gets stuck. #
|
-- # prescalers. When the counter overflows, either a hardware reset (mode = 1) is performed or an #
|
-- # The internal counter is 20-bit wide. If this counter overflows one of two possible actions is #
|
-- # interrupt (mode = 0) is triggered. The WDT can only operate when the enable bit is set. A #
|
-- # triggered: Generate an IRQ or force a hardware reset of the system. #
|
-- # write access to the WDT can only be performed, if the higher byte of the written data #
|
-- # A WDT action can also be triggered manually at any time by setting the FORCE bit. #
|
-- # contains the specific WDT password (0x47). For a write access with a wrong password #
|
-- # #
|
-- # a HW reset or IRQ (depending on mode) is triggered, but only if the WDT is enabled. #
|
-- # Access to the control register can be permanently locked by setting the lock bit. This bit #
|
|
-- # can only be cleared by a hardware reset (external or caused by the watchdog itself). #
|
-- # ********************************************************************************************* #
|
-- # ********************************************************************************************* #
|
-- # BSD 3-Clause License #
|
-- # BSD 3-Clause License #
|
-- # #
|
-- # #
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
-- # #
|
-- # #
|
Line 70... |
Line 71... |
|
|
-- IO space: module base address --
|
-- IO space: module base address --
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
constant lo_abb_c : natural := index_size_f(wdt_size_c); -- low address boundary bit
|
constant lo_abb_c : natural := index_size_f(wdt_size_c); -- low address boundary bit
|
|
|
-- Watchdog access password --
|
|
constant wdt_password_c : std_ulogic_vector(07 downto 0) := x"47";
|
|
|
|
-- Control register bits --
|
-- Control register bits --
|
constant ctrl_clksel0_c : natural := 0; -- r/w: prescaler select bit 0
|
constant ctrl_enable_c : natural := 0; -- r/w: WDT enable
|
constant ctrl_clksel1_c : natural := 1; -- r/w: prescaler select bit 1
|
constant ctrl_clksel0_c : natural := 1; -- r/w: prescaler select bit 0
|
constant ctrl_clksel2_c : natural := 2; -- r/w: prescaler select bit 2
|
constant ctrl_clksel1_c : natural := 2; -- r/w: prescaler select bit 1
|
constant ctrl_enable_c : natural := 3; -- r/w: WDT enable
|
constant ctrl_clksel2_c : natural := 3; -- r/w: prescaler select bit 2
|
constant ctrl_mode_c : natural := 4; -- r/w: 0: timeout causes interrupt, 1: timeout causes hard reset
|
constant ctrl_mode_c : natural := 4; -- r/w: 0: WDT timeout triggers interrupt, 1: WDT timeout triggers hard reset
|
constant ctrl_cause_c : natural := 5; -- r/-: action (reset/IRQ) cause (0: external, 1: watchdog)
|
constant ctrl_rcause_c : natural := 5; -- r/-: cause of last action (reset/IRQ): 0=external reset, 1=watchdog overflow
|
constant ctrl_pwfail_c : natural := 6; -- r/-: watchdog action (reset/IRQ) caused by wrong password access when '1'
|
constant ctrl_reset_c : natural := 6; -- -/w: reset WDT if set
|
|
constant ctrl_force_c : natural := 7; -- -/w: force WDT action
|
|
constant ctrl_lock_c : natural := 8; -- r/w: lock access to control register when set
|
|
|
-- access control --
|
-- access control --
|
signal acc_en : std_ulogic; -- module access enable
|
signal acc_en : std_ulogic; -- module access enable
|
signal pwd_ok : std_ulogic; -- password correct
|
|
signal fail, fail_ff : std_ulogic; -- unauthorized access
|
|
signal wren : std_ulogic;
|
signal wren : std_ulogic;
|
|
signal rden : std_ulogic;
|
|
|
-- accessible regs --
|
-- control register --
|
signal source : std_ulogic; -- source of wdt action: '0' = external, '1' = watchdog
|
type ctrl_reg_t is record
|
signal pw_fail : std_ulogic; -- watchdog action caused by wrong password access
|
enable : std_ulogic; -- 1=WDT enabled
|
signal enable : std_ulogic;
|
clk_sel : std_ulogic_vector(2 downto 0);
|
signal mode : std_ulogic;
|
mode : std_ulogic; -- 0=trigger IRQ on overflow; 1=trigger hard reset on overflow
|
signal clk_sel : std_ulogic_vector(02 downto 0);
|
rcause : std_ulogic; -- cause of last system reset: '0' = external, '1' = watchdog
|
|
reset : std_ulogic; -- reset WDT
|
-- reset counter --
|
force : std_ulogic; -- force action
|
signal cnt : std_ulogic_vector(20 downto 0);
|
lock : std_ulogic; -- lock control register
|
signal rst_gen : std_ulogic_vector(03 downto 0);
|
end record;
|
signal rst_sync : std_ulogic_vector(01 downto 0);
|
signal ctrl_reg : ctrl_reg_t;
|
|
|
-- prescaler clock generator --
|
-- prescaler clock generator --
|
signal prsc_tick : std_ulogic;
|
signal prsc_tick : std_ulogic;
|
|
|
|
-- WDT core --
|
|
signal wdt_cnt : std_ulogic_vector(20 downto 0);
|
|
signal hw_rst : std_ulogic;
|
|
signal rst_gen : std_ulogic_vector(03 downto 0);
|
|
|
|
-- internal reset (sync, low-active) --
|
|
signal rstn_sync : std_ulogic;
|
|
|
begin
|
begin
|
|
|
-- Access Control -------------------------------------------------------------------------
|
-- Access Control -------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = wdt_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = wdt_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
pwd_ok <= '1' when (data_i(15 downto 8) = wdt_password_c) else '0'; -- password check
|
wren <= acc_en and wren_i;
|
wren <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '1')) else '0'; -- write access ok
|
rden <= acc_en and rden_i;
|
fail <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '0')) else '0'; -- write access fail!
|
|
|
|
|
|
-- Write Access, Reset Generator ----------------------------------------------------------
|
-- Write Access ---------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
wdt_core: process(clk_i)
|
write_access: process(rstn_i, clk_i)
|
begin
|
begin
|
if rising_edge(clk_i) then
|
if (rstn_i = '0') then
|
if (rstn_i = '0') or (rst_sync(1) = '0') then -- external or internal reset
|
ctrl_reg.reset <= '0';
|
enable <= '0'; -- disable WDT
|
ctrl_reg.force <= '0';
|
mode <= '0'; -- trigger interrupt if watchdog timeouts
|
ctrl_reg.enable <= '0'; -- disable WDT
|
clk_sel <= (others => '1'); -- slowest clock rst_source
|
ctrl_reg.mode <= '0'; -- trigger interrupt on WDT overflow
|
rst_gen <= (others => '1'); -- do NOT fire on reset!
|
ctrl_reg.clk_sel <= (others => '1'); -- slowest clock source
|
|
ctrl_reg.lock <= '0';
|
|
elsif rising_edge(clk_i) then
|
|
if (rstn_sync = '0') then -- internal reset
|
|
ctrl_reg.reset <= '0';
|
|
ctrl_reg.force <= '0';
|
|
ctrl_reg.enable <= '0'; -- disable WDT
|
|
ctrl_reg.mode <= '0'; -- trigger interrupt on WDT overflow
|
|
ctrl_reg.clk_sel <= (others => '1'); -- slowest clock source
|
|
ctrl_reg.lock <= '0';
|
else
|
else
|
-- control register write access --
|
-- auto-clear WDT reset and WDT force flags --
|
if (wren = '1') then -- allow write if password is correct
|
ctrl_reg.reset <= '0';
|
enable <= data_i(ctrl_enable_c);
|
ctrl_reg.force <= '0';
|
clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
|
-- actual write access --
|
mode <= data_i(ctrl_mode_c);
|
if (wren = '1') then
|
|
ctrl_reg.reset <= data_i(ctrl_reset_c);
|
|
ctrl_reg.force <= data_i(ctrl_force_c);
|
|
if (ctrl_reg.lock = '0') then -- update configuration only if unlocked
|
|
ctrl_reg.enable <= data_i(ctrl_enable_c);
|
|
ctrl_reg.mode <= data_i(ctrl_mode_c);
|
|
ctrl_reg.clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
|
|
ctrl_reg.lock <= data_i(ctrl_lock_c);
|
end if;
|
end if;
|
-- trigger system reset when enabled AND reset mode AND timeout OR unauthorized access --
|
|
if (enable = '1') and (mode = '1') and ((cnt(cnt'left) = '1') or (fail_ff = '1')) then
|
|
rst_gen <= (others => '0');
|
|
else
|
|
rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
|
|
end if;
|
end if;
|
end if;
|
end if;
|
end if;
|
end if;
|
end process wdt_core;
|
end process write_access;
|
|
|
-- enable external clock generator --
|
-- clock generator --
|
clkgen_en_o <= enable;
|
clkgen_en_o <= ctrl_reg.enable; -- enable clock generator
|
|
prsc_tick <= clkgen_i(to_integer(unsigned(ctrl_reg.clk_sel))); -- clock enable tick
|
|
|
|
|
-- Counter Update -------------------------------------------------------------------------
|
-- Watchdog Counter -----------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
cnt_sync: process(clk_i)
|
wdt_counter: process(clk_i)
|
begin
|
begin
|
if rising_edge(clk_i) then
|
if rising_edge(clk_i) then
|
-- clock_en buffer --
|
if (ctrl_reg.reset = '1') then -- watchdog reset
|
prsc_tick <= clkgen_i(to_integer(unsigned(clk_sel)));
|
wdt_cnt <= (others => '0');
|
-- unauthorized access buffer --
|
elsif (ctrl_reg.enable = '1') and (prsc_tick = '1') then
|
fail_ff <= fail;
|
wdt_cnt <= std_ulogic_vector(unsigned(wdt_cnt) + 1);
|
-- reset synchronizer --
|
|
rst_sync <= rst_sync(0) & rst_gen(rst_gen'left);
|
|
-- IRQ mode --
|
|
irq_o <= '0';
|
|
if (enable = '1') and (mode = '0') and ((cnt(cnt'left) = '1') or (fail_ff = '1')) then
|
|
irq_o <= '1'; -- trigger interrupt if watchdog timeout and MODE=0
|
|
end if;
|
|
-- counter update --
|
|
if (wren = '1') then -- clear counter on write access (manual watchdog reset)
|
|
cnt <= (others => '0');
|
|
elsif (enable = '1') and (prsc_tick = '1') then
|
|
cnt <= std_ulogic_vector(unsigned('0' & cnt(cnt'left-1 downto 0)) + 1);
|
|
end if;
|
end if;
|
end if;
|
end if;
|
end process cnt_sync;
|
end process wdt_counter;
|
|
|
-- system reset --
|
-- action trigger --
|
rstn_o <= rst_sync(1);
|
irq_o <= ctrl_reg.enable and (wdt_cnt(wdt_cnt'left) or ctrl_reg.force) and (not ctrl_reg.mode); -- mode 0: IRQ
|
|
hw_rst <= ctrl_reg.enable and (wdt_cnt(wdt_cnt'left) or ctrl_reg.force) and ( ctrl_reg.mode); -- mode 1: RESET
|
|
|
|
|
-- Reset Cause Indicator ------------------------------------------------------------------
|
-- Reset Generator & Action Cause Indicator -----------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
rst_cause: process(rstn_i, clk_i)
|
reset_generator: process(rstn_i, clk_i)
|
begin
|
begin
|
if (rstn_i = '0') then
|
if (rstn_i = '0') then
|
source <= '0';
|
ctrl_reg.rcause <= '0';
|
pw_fail <= '0';
|
rst_gen <= (others => '1'); -- do NOT fire on reset!
|
|
rstn_sync <= '1';
|
elsif rising_edge(clk_i) then
|
elsif rising_edge(clk_i) then
|
source <= source or (cnt(cnt'left) and enable) or (fail_ff and enable); -- set on WDT timeout or access error
|
ctrl_reg.rcause <= ctrl_reg.rcause or hw_rst; -- sticky-set on WDT timeout/force
|
pw_fail <= (pw_fail or (fail_ff and enable)) and (not (cnt(cnt'left) and enable)); -- set on failed access, clear on WDT timeout
|
if (hw_rst = '1') then
|
|
rst_gen <= (others => '0');
|
|
else
|
|
rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
|
|
end if;
|
|
rstn_sync <= rst_gen(rst_gen'left);
|
end if;
|
end if;
|
end process rst_cause;
|
end process reset_generator;
|
|
|
|
-- system reset --
|
|
rstn_o <= rst_gen(rst_gen'left);
|
|
|
|
|
-- Read Access ----------------------------------------------------------------------------
|
-- Read Access ----------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
read_access: process(clk_i)
|
read_access: process(clk_i)
|
begin
|
begin
|
if rising_edge(clk_i) then
|
if rising_edge(clk_i) then
|
ack_o <= acc_en and (rden_i or wren_i);
|
ack_o <= rden or wren;
|
|
if (rden = '1') then
|
|
data_o(ctrl_enable_c) <= ctrl_reg.enable;
|
|
data_o(ctrl_mode_c) <= ctrl_reg.mode;
|
|
data_o(ctrl_rcause_c) <= ctrl_reg.rcause;
|
|
data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= ctrl_reg.clk_sel;
|
|
data_o(ctrl_lock_c) <= ctrl_reg.lock;
|
|
else
|
data_o <= (others => '0');
|
data_o <= (others => '0');
|
if (acc_en = '1') and (rden_i = '1') then
|
|
data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= clk_sel;
|
|
data_o(ctrl_enable_c) <= enable;
|
|
data_o(ctrl_cause_c) <= source;
|
|
data_o(ctrl_pwfail_c) <= pw_fail;
|
|
data_o(ctrl_mode_c) <= mode;
|
|
end if;
|
end if;
|
end if;
|
end if;
|
end process read_access;
|
end process read_access;
|
|
|
|
|