OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_wdt.vhd] - Blame information for rev 22

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
58
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
59
    ack_o       : out std_ulogic; -- transfer acknowledge
60
    -- clock generator --
61
    clkgen_en_o : out std_ulogic; -- enable clock generator
62
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
63
    -- timeout event --
64
    irq_o       : out std_ulogic; -- timeout IRQ
65
    rstn_o      : out std_ulogic  -- timeout reset, low_active, use as async
66
  );
67
end neorv32_wdt;
68
 
69
architecture neorv32_wdt_rtl of neorv32_wdt is
70
 
71
  -- IO space: module base address --
72
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
73
  constant lo_abb_c : natural := index_size_f(wdt_size_c); -- low address boundary bit
74
 
75
  -- Watchdog access password --
76
  constant wdt_password_c : std_ulogic_vector(07 downto 0) := x"47";
77
 
78
  -- Control register bits --
79
  constant ctrl_clksel0_c : natural := 0; -- r/w: prescaler select bit 0
80
  constant ctrl_clksel1_c : natural := 1; -- r/w: prescaler select bit 1
81
  constant ctrl_clksel2_c : natural := 2; -- r/w: prescaler select bit 2
82
  constant ctrl_enable_c  : natural := 3; -- r/w: WDT enable
83
  constant ctrl_mode_c    : natural := 4; -- r/w: 0: timeout causes interrupt, 1: timeout causes hard reset
84
  constant ctrl_cause_c   : natural := 5; -- r/-: action (reset/IRQ) cause (0: external, 1: watchdog)
85
  constant ctrl_pwfail_c  : natural := 6; -- r/-: watchdog action (reset/IRQ) caused by wrong password access when '1'
86
 
87
  -- access control --
88
  signal acc_en        : std_ulogic; -- module access enable
89
  signal pwd_ok        : std_ulogic; -- password correct
90
  signal fail, fail_ff : std_ulogic; -- unauthorized access
91
  signal wren          : std_ulogic;
92
 
93
  -- accessible regs --
94
  signal source  : std_ulogic; -- source of wdt action: '0' = external, '1' = watchdog
95
  signal pw_fail : std_ulogic; -- watchdog action caused by wrong password access
96
  signal enable  : std_ulogic;
97
  signal mode    : std_ulogic;
98
  signal clk_sel : std_ulogic_vector(02 downto 0);
99
 
100
  -- reset counter --
101
  signal cnt      : std_ulogic_vector(20 downto 0);
102
  signal rst_gen  : std_ulogic_vector(03 downto 0);
103
  signal rst_sync : std_ulogic_vector(01 downto 0);
104
 
105
  -- prescaler clock generator --
106
  signal prsc_tick : std_ulogic;
107
 
108
begin
109
 
110
  -- Access Control -------------------------------------------------------------------------
111
  -- -------------------------------------------------------------------------------------------
112
  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';
113 22 zero_gravi
  pwd_ok <= '1' when (data_i(15 downto 8) = wdt_password_c) else '0'; -- password check
114 2 zero_gravi
  wren   <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '1')) else '0'; -- write access ok
115
  fail   <= '1' when ((acc_en = '1') and (wren_i = '1') and (pwd_ok = '0')) else '0'; -- write access fail!
116
 
117
 
118
  -- Write Access, Reset Generator ----------------------------------------------------------
119
  -- -------------------------------------------------------------------------------------------
120
  wdt_core: process(clk_i)
121
  begin
122
    if rising_edge(clk_i) then
123
      if (rstn_i = '0') or (rst_sync(1) = '0') then -- external or internal reset
124
        enable  <= '0'; -- disable WDT
125
        mode    <= '0'; -- trigger interrupt if watchdog timeouts
126
        clk_sel <= (others => '1'); -- slowest clock rst_source
127
        rst_gen <= (others => '1'); -- do NOT fire on reset!
128
      else
129
        -- control register write access --
130
        if (wren = '1') then -- allow write if password is correct
131 22 zero_gravi
          enable  <= data_i(ctrl_enable_c);
132
          clk_sel <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
133
          mode    <= data_i(ctrl_mode_c);
134 2 zero_gravi
        end if;
135
        -- trigger system reset when enabled AND reset mode AND timeout OR unauthorized access --
136
        if (enable = '1') and (mode = '1') and ((cnt(cnt'left) = '1') or (fail_ff = '1')) then
137
          rst_gen <= (others => '0');
138
        else
139
          rst_gen <= rst_gen(rst_gen'left-1 downto 0) & '1';
140
        end if;
141
      end if;
142
    end if;
143
  end process wdt_core;
144
 
145
  -- enable external clock generator --
146
  clkgen_en_o <= enable;
147
 
148
 
149
  -- Counter Update -------------------------------------------------------------------------
150
  -- -------------------------------------------------------------------------------------------
151
  cnt_sync: process(clk_i)
152
  begin
153
    if rising_edge(clk_i) then
154
      -- clock_en buffer --
155
      prsc_tick <= clkgen_i(to_integer(unsigned(clk_sel)));
156
      -- unauthorized access buffer --
157
      fail_ff <= fail;
158
      -- reset synchronizer --
159
      rst_sync <= rst_sync(0) & rst_gen(rst_gen'left);
160
      -- IRQ mode --
161
      irq_o <= '0';
162
      if (enable = '1') and (mode = '0') and ((cnt(cnt'left) = '1') or (fail_ff = '1')) then
163
        irq_o <= '1'; -- trigger interrupt if watchdog timeout and MODE=0
164
      end if;
165
      -- counter update --
166
      if (wren = '1') then -- clear counter on write access (manual watchdog reset)
167
        cnt <= (others => '0');
168
      elsif (enable = '1') and (prsc_tick = '1') then
169
        cnt <= std_ulogic_vector(unsigned('0' & cnt(cnt'left-1 downto 0)) + 1);
170
      end if;
171
    end if;
172
  end process cnt_sync;
173
 
174
  -- system reset --
175
  rstn_o <= rst_sync(1);
176
 
177
 
178
  -- Reset Cause Indicator ------------------------------------------------------------------
179
  -- -------------------------------------------------------------------------------------------
180
  rst_cause: process(rstn_i, clk_i)
181
  begin
182
    if (rstn_i = '0') then
183
      source  <= '0';
184
      pw_fail <= '0';
185
    elsif rising_edge(clk_i) then
186
      source  <= source or (cnt(cnt'left) and enable) or (fail_ff and enable); -- set on WDT timeout or access error
187
      pw_fail <= (pw_fail or (fail_ff and enable)) and (not (cnt(cnt'left) and enable)); -- set on failed access, clear on WDT timeout
188
    end if;
189
  end process rst_cause;
190
 
191
 
192
  -- Read Access ----------------------------------------------------------------------------
193
  -- -------------------------------------------------------------------------------------------
194
  read_access: process(clk_i)
195
  begin
196
    if rising_edge(clk_i) then
197
      ack_o  <= acc_en and (rden_i or wren_i);
198
      data_o <= (others => '0');
199
      if (acc_en = '1') and (rden_i = '1') then
200
        data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= clk_sel;
201
        data_o(ctrl_enable_c) <= enable;
202
        data_o(ctrl_cause_c)  <= source;
203
        data_o(ctrl_pwfail_c) <= pw_fail;
204
        data_o(ctrl_mode_c)   <= mode;
205
      end if;
206
    end if;
207
  end process read_access;
208
 
209
 
210
end neorv32_wdt_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.