Debian Codegraph @

hdl/fpga/ip/nuand/pll_reset/vhdl/pll_reset.vhd

raw
1 -- From Altera Solution ID rd05312011_49, need the following for Qsys to use VHDL 2008:
2 -- altera vhdl_input_version vhdl_2008
3
4 -- PLL Reset Controller
5 -- Copyright (c) 2017 Nuand LLC
6 --
7 -- Permission is hereby granted, free of charge, to any person obtaining a copy
8 -- of this software and associated documentation files (the "Software"), to deal
9 -- in the Software without restriction, including without limitation the rights
10 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 -- copies of the Software, and to permit persons to whom the Software is
12 -- furnished to do so, subject to the following conditions:
13 --
14 -- The above copyright notice and this permission notice shall be included in
15 -- all copies or substantial portions of the Software.
16 --
17 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 -- THE SOFTWARE.
24
25 library ieee;
26 use ieee.std_logic_1164.all;
27 use ieee.numeric_std.all;
28
29 entity pll_reset is
30 generic (
31 SYS_CLOCK_FREQ_HZ : natural := 10_000_000;
32 DEVICE_FAMILY : string := "Cyclone V"
33 );
34 port (
35 sys_clock : in std_logic;
36 pll_locked : in std_logic;
37 pll_locked_out : out std_logic;
38 pll_reset : out std_logic
39 );
40 end entity; -- pll_reset
41
42 architecture fsm of pll_reset is
43 type fsm_t is (
44 INIT,
45 ASSERT_RESET,
46 WAIT_FOR_LOCK,
47 WAIT_FOR_UNLOCK
48 );
49
50 type state_t is record
51 state : fsm_t;
52 downcount : natural;
53 pll_lock : std_logic;
54 pll_reset : std_logic;
55 end record;
56
57 constant reset_value : state_t := (
58 state => INIT,
59 downcount => 0,
60 pll_lock => '0',
61 pll_reset => '0'
62 );
63
64 signal current : state_t := reset_value;
65 signal future : state_t := reset_value;
66
67 function reset_duration(device : string) return time is
68 variable rv : time;
69 begin
70 if (device = "Cyclone IV") then
71 rv := 1.1 ms;
72 elsif (device = "Cyclone V") then
73 rv := 1.1 ms;
74 else
75 report "unknown device family: " & device severity failure;
76 end if;
77
78 return rv;
79 end function reset_duration;
80
81 function time_to_ticks(duration : time; freq : natural) return natural is
82 variable period : time;
83 variable rv : natural;
84 begin
85 period := 1 sec / freq;
86 rv := duration / period;
87 return rv;
88 end function time_to_ticks;
89
90 begin
91
92 sync_proc : process(sys_clock)
93 begin
94 if(rising_edge(sys_clock)) then
95 current <= future;
96 end if;
97 end process sync_proc;
98
99 comb_proc : process(all)
100 begin
101 -- outputs
102 pll_locked_out <= current.pll_lock;
103 pll_reset <= current.pll_reset;
104
105 -- defaults
106 future <= current;
107 future.pll_lock <= pll_locked;
108 future.pll_reset <= '0';
109
110 -- states
111 case current.state is
112 when INIT =>
113 -- prepare countdown
114 future.downcount <= time_to_ticks(reset_duration(DEVICE_FAMILY), SYS_CLOCK_FREQ_HZ);
115 future.state <= ASSERT_RESET;
116
117 when ASSERT_RESET =>
118 -- keep reset asserted until countdown is done
119 future.pll_reset <= '1';
120
121 if (current.downcount = 0) then
122 future.downcount <= time_to_ticks(reset_duration(DEVICE_FAMILY), SYS_CLOCK_FREQ_HZ);
123 future.state <= WAIT_FOR_LOCK;
124 else
125 future.downcount <= current.downcount - 1;
126 end if;
127
128 when WAIT_FOR_LOCK =>
129 -- de-assert reset and hope it locks
130
131 if (current.pll_lock = '1') then
132 -- we're good
133 future.state <= WAIT_FOR_UNLOCK;
134 end if;
135
136 if (current.downcount = 0) then
137 -- it didn't lock, let's try again
138 future.downcount <= time_to_ticks(reset_duration(DEVICE_FAMILY), SYS_CLOCK_FREQ_HZ);
139 future.state <= INIT;
140 else
141 future.downcount <= current.downcount - 1;
142 end if;
143
144 when WAIT_FOR_UNLOCK =>
145 -- monitor for PLL unlock
146
147 if (current.pll_lock = '0') then
148 future.state <= INIT;
149 end if;
150
151 end case;
152
153 end process comb_proc;
154
155 end architecture fsm;