1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Watch Dog Timer (WDT) >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
|
|
-- # The internal counter is 20 bit wide and increases using 1 out of 8 available clock #
|
5 |
|
|
-- # prescalers. When the counter overflows, either a hardware reset (mode = 1) is performed or an #
|
6 |
|
|
-- # interrupt (mode = 0) is triggered. The WDT can only operate when the enable bit is set. A #
|
7 |
|
|
-- # write access to the WDT can only be performed, if the higher byte of the written data #
|
8 |
|
|
-- # contains the specific WDT password (0x47). For a write access with a wrong password #
|
9 |
|
|
-- # a HW reset or IRQ (depending on mode) is triggered, but only if the WDT is enabled. #
|
10 |
|
|
-- # ********************************************************************************************* #
|
11 |
|
|
-- # BSD 3-Clause License #
|
12 |
|
|
-- # #
|
13 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
14 |
|
|
-- # #
|
15 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
16 |
|
|
-- # permitted provided that the following conditions are met: #
|
17 |
|
|
-- # #
|
18 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
19 |
|
|
-- # conditions and the following disclaimer. #
|
20 |
|
|
-- # #
|
21 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
22 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
23 |
|
|
-- # provided with the distribution. #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
26 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
27 |
|
|
-- # permission. #
|
28 |
|
|
-- # #
|
29 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
30 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
31 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
32 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
33 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
34 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
35 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
36 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
37 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
38 |
|
|
-- # ********************************************************************************************* #
|
39 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
40 |
|
|
-- #################################################################################################
|
41 |
|
|
|
42 |
|
|
library ieee;
|
43 |
|
|
use ieee.std_logic_1164.all;
|
44 |
|
|
use ieee.numeric_std.all;
|
45 |
|
|
|
46 |
|
|
library neorv32;
|
47 |
|
|
use neorv32.neorv32_package.all;
|
48 |
|
|
|
49 |
|
|
entity neorv32_wdt is
|
50 |
|
|
port (
|
51 |
|
|
-- host access --
|
52 |
|
|
clk_i : in std_ulogic; -- global clock line
|
53 |
|
|
rstn_i : in std_ulogic; -- global reset line, low-active
|
54 |
|
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
55 |
|
|
rden_i : in std_ulogic; -- read enable
|
56 |
|
|
wren_i : in std_ulogic; -- write enable
|
57 |
|
|
ben_i : in std_ulogic_vector(03 downto 0); -- byte write enable
|
58 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
59 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
60 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
61 |
|
|
-- clock generator --
|
62 |
|
|
clkgen_en_o : out std_ulogic; -- enable clock generator
|
63 |
|
|
clkgen_i : in std_ulogic_vector(07 downto 0);
|
64 |
|
|
-- timeout event --
|
65 |
|
|
irq_o : out std_ulogic; -- timeout IRQ
|
66 |
|
|
rstn_o : out std_ulogic -- timeout reset, low_active, use as async
|
67 |
|
|
);
|
68 |
|
|
end neorv32_wdt;
|
69 |
|
|
|
70 |
|
|
architecture neorv32_wdt_rtl of neorv32_wdt is
|
71 |
|
|
|
72 |
|
|
-- IO space: module base address --
|
73 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
74 |
|
|
constant lo_abb_c : natural := index_size_f(wdt_size_c); -- low address boundary bit
|
75 |
|
|
|
76 |
|
|
-- Watchdog access password --
|
77 |
|
|
constant wdt_password_c : std_ulogic_vector(07 downto 0) := x"47";
|
78 |
|
|
|
79 |
|
|
-- Control register bits --
|
80 |
|
|
constant ctrl_clksel0_c : natural := 0; -- r/w: prescaler select bit 0
|
81 |
|
|
constant ctrl_clksel1_c : natural := 1; -- r/w: prescaler select bit 1
|
82 |
|
|
constant ctrl_clksel2_c : natural := 2; -- r/w: prescaler select bit 2
|
83 |
|
|
constant ctrl_enable_c : natural := 3; -- r/w: WDT enable
|
84 |
|
|
constant ctrl_mode_c : natural := 4; -- r/w: 0: timeout causes interrupt, 1: timeout causes hard reset
|
85 |
|
|
constant ctrl_cause_c : natural := 5; -- r/-: action (reset/IRQ) cause (0: external, 1: watchdog)
|
86 |
|
|
constant ctrl_pwfail_c : natural := 6; -- r/-: watchdog action (reset/IRQ) caused by wrong password access when '1'
|
87 |
|
|
|
88 |
|
|
-- access control --
|
89 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
90 |
|
|
signal pwd_ok : std_ulogic; -- password correct
|
91 |
|
|
signal fail, fail_ff : std_ulogic; -- unauthorized access
|
92 |
|
|
signal wren : std_ulogic;
|
93 |
|
|
|
94 |
|
|
-- accessible regs --
|
95 |
|
|
signal source : std_ulogic; -- source of wdt action: '0' = external, '1' = watchdog
|
96 |
|
|
signal pw_fail : std_ulogic; -- watchdog action caused by wrong password access
|
97 |
|
|
signal enable : std_ulogic;
|
98 |
|
|
signal mode : std_ulogic;
|
99 |
|
|
signal clk_sel : std_ulogic_vector(02 downto 0);
|
100 |
|
|
|
101 |
|
|
-- reset counter --
|
102 |
|
|
signal cnt : std_ulogic_vector(20 downto 0);
|
103 |
|
|
signal rst_gen : std_ulogic_vector(03 downto 0);
|
104 |
|
|
signal rst_sync : std_ulogic_vector(01 downto 0);
|
105 |
|
|
|
106 |
|
|
-- prescaler clock generator --
|
107 |
|
|
signal prsc_tick : std_ulogic;
|
108 |
|
|
|
109 |
|
|
begin
|
110 |
|
|
|
111 |
|
|
-- Access Control -------------------------------------------------------------------------
|
112 |
|
|
-- -------------------------------------------------------------------------------------------
|
113 |
|
|
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';
|
114 |
|
|
pwd_ok <= '1' when (data_i(15 downto 8) = wdt_password_c) and (ben_i(1) = '1') else '0'; -- password check
|
115 |
|
|
wren <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '1')) else '0'; -- write access ok
|
116 |
|
|
fail <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '0')) else '0'; -- write access fail!
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
-- Write Access, Reset Generator ----------------------------------------------------------
|
120 |
|
|
-- -------------------------------------------------------------------------------------------
|
121 |
|
|
wdt_core: process(clk_i)
|
122 |
|
|
begin
|
123 |
|
|
if rising_edge(clk_i) then
|
124 |
|
|
if (rstn_i = '0') or (rst_sync(1) = '0') then -- external or internal reset
|
125 |
|
|
enable <= '0'; -- disable WDT
|
126 |
|
|
mode <= '0'; -- trigger interrupt if watchdog timeouts
|
127 |
|
|
clk_sel <= (others => '1'); -- slowest clock rst_source
|
128 |
|
|
rst_gen <= (others => '1'); -- do NOT fire on reset!
|
129 |
|
|
else
|
130 |
|
|
-- control register write access --
|
131 |
|
|
if (wren = '1') then -- allow write if password is correct
|
132 |
|
|
if (ben_i(0) = '1') then
|
133 |
|
|
enable <= data_i(ctrl_enable_c);
|
134 |
|
|
clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
|
135 |
|
|
mode <= data_i(ctrl_mode_c);
|
136 |
|
|
end if;
|
137 |
|
|
end if;
|
138 |
|
|
-- trigger system reset when enabled AND reset mode AND timeout OR unauthorized access --
|
139 |
|
|
if (enable = '1') and (mode = '1') and ((cnt(cnt'left) = '1') or (fail_ff = '1')) then
|
140 |
|
|
rst_gen <= (others => '0');
|
141 |
|
|
else
|
142 |
|
|
rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
|
143 |
|
|
end if;
|
144 |
|
|
end if;
|
145 |
|
|
end if;
|
146 |
|
|
end process wdt_core;
|
147 |
|
|
|
148 |
|
|
-- enable external clock generator --
|
149 |
|
|
clkgen_en_o <= enable;
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
-- Counter Update -------------------------------------------------------------------------
|
153 |
|
|
-- -------------------------------------------------------------------------------------------
|
154 |
|
|
cnt_sync: process(clk_i)
|
155 |
|
|
begin
|
156 |
|
|
if rising_edge(clk_i) then
|
157 |
|
|
-- clock_en buffer --
|
158 |
|
|
prsc_tick <= clkgen_i(to_integer(unsigned(clk_sel)));
|
159 |
|
|
-- unauthorized access buffer --
|
160 |
|
|
fail_ff <= fail;
|
161 |
|
|
-- reset synchronizer --
|
162 |
|
|
rst_sync <= rst_sync(0) & rst_gen(rst_gen'left);
|
163 |
|
|
-- IRQ mode --
|
164 |
|
|
irq_o <= '0';
|
165 |
|
|
if (enable = '1') and (mode = '0') and ((cnt(cnt'left) = '1') or (fail_ff = '1')) then
|
166 |
|
|
irq_o <= '1'; -- trigger interrupt if watchdog timeout and MODE=0
|
167 |
|
|
end if;
|
168 |
|
|
-- counter update --
|
169 |
|
|
if (wren = '1') then -- clear counter on write access (manual watchdog reset)
|
170 |
|
|
cnt <= (others => '0');
|
171 |
|
|
elsif (enable = '1') and (prsc_tick = '1') then
|
172 |
|
|
cnt <= std_ulogic_vector(unsigned('0' & cnt(cnt'left-1 downto 0)) + 1);
|
173 |
|
|
end if;
|
174 |
|
|
end if;
|
175 |
|
|
end process cnt_sync;
|
176 |
|
|
|
177 |
|
|
-- system reset --
|
178 |
|
|
rstn_o <= rst_sync(1);
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
-- Reset Cause Indicator ------------------------------------------------------------------
|
182 |
|
|
-- -------------------------------------------------------------------------------------------
|
183 |
|
|
rst_cause: process(rstn_i, clk_i)
|
184 |
|
|
begin
|
185 |
|
|
if (rstn_i = '0') then
|
186 |
|
|
source <= '0';
|
187 |
|
|
pw_fail <= '0';
|
188 |
|
|
elsif rising_edge(clk_i) then
|
189 |
|
|
source <= source or (cnt(cnt'left) and enable) or (fail_ff and enable); -- set on WDT timeout or access error
|
190 |
|
|
pw_fail <= (pw_fail or (fail_ff and enable)) and (not (cnt(cnt'left) and enable)); -- set on failed access, clear on WDT timeout
|
191 |
|
|
end if;
|
192 |
|
|
end process rst_cause;
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
-- Read Access ----------------------------------------------------------------------------
|
196 |
|
|
-- -------------------------------------------------------------------------------------------
|
197 |
|
|
read_access: process(clk_i)
|
198 |
|
|
begin
|
199 |
|
|
if rising_edge(clk_i) then
|
200 |
|
|
ack_o <= acc_en and (rden_i or wren_i);
|
201 |
|
|
data_o <= (others => '0');
|
202 |
|
|
if (acc_en = '1') and (rden_i = '1') then
|
203 |
|
|
data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= clk_sel;
|
204 |
|
|
data_o(ctrl_enable_c) <= enable;
|
205 |
|
|
data_o(ctrl_cause_c) <= source;
|
206 |
|
|
data_o(ctrl_pwfail_c) <= pw_fail;
|
207 |
|
|
data_o(ctrl_mode_c) <= mode;
|
208 |
|
|
end if;
|
209 |
|
|
end if;
|
210 |
|
|
end process read_access;
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
end neorv32_wdt_rtl;
|