blob: e187d4765a8d41a29a83bccd8983e9afb80d9cbe [file] [log] [blame]
agorararmarde5780bf2020-12-09 21:27:56 +00001// Copyright 2020 Efabless Corporation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Matt Venn08cd6eb2020-11-16 12:01:14 +010015`default_nettype none
Tim Edwards3245e2f2020-10-10 14:02:11 -040016// This routine synchronizes the
17
18module caravel_clocking(
Manar61dce922020-11-10 19:26:28 +020019`ifdef USE_POWER_PINS
Tim Edwards3245e2f2020-10-10 14:02:11 -040020 input vdd1v8,
21 input vss,
22`endif
23 input resetb, // Master (negative sense) reset
24 input ext_clk_sel, // 0=use PLL clock, 1=use external (pad) clock
25 input ext_clk, // External pad (slow) clock
26 input pll_clk, // Internal PLL (fast) clock
Tim Edwards7a8cbb12020-10-12 11:32:11 -040027 input pll_clk90, // Internal PLL (fast) clock, 90 degree phase
Tim Edwards3245e2f2020-10-10 14:02:11 -040028 input [2:0] sel, // Select clock divider value (0=thru, 1=divide-by-2, etc.)
Tim Edwards7a8cbb12020-10-12 11:32:11 -040029 input [2:0] sel2, // Select clock divider value for 90 degree phase divided clock
Tim Edwards3245e2f2020-10-10 14:02:11 -040030 input ext_reset, // Positive sense reset from housekeeping SPI.
31 output core_clk, // Output core clock
Tim Edwards7a8cbb12020-10-12 11:32:11 -040032 output user_clk, // Output user (secondary) clock
Tim Edwards3245e2f2020-10-10 14:02:11 -040033 output resetb_sync // Output propagated and buffered reset
34);
35
36 wire pll_clk_sel;
Tim Edwards581068f2020-11-19 12:45:25 -050037 wire pll_clk_divided;
38 wire pll_clk90_divided;
39 wire core_ext_clk;
Tim Edwards3245e2f2020-10-10 14:02:11 -040040 reg use_pll_first;
41 reg use_pll_second;
42 reg ext_clk_syncd_pre;
43 reg ext_clk_syncd;
44
45 assign pll_clk_sel = ~ext_clk_sel;
46
47 // Note that this implementation does not guard against switching to
48 // the PLL clock if the PLL clock is not present.
49
50 always @(posedge pll_clk or negedge resetb) begin
51 if (resetb == 1'b0) begin
52 use_pll_first <= 1'b0;
53 use_pll_second <= 1'b0;
54 ext_clk_syncd <= 1'b0;
55 end else begin
56 use_pll_first <= pll_clk_sel;
57 use_pll_second <= use_pll_first;
58 ext_clk_syncd_pre <= ext_clk; // Sync ext_clk to pll_clk
59 ext_clk_syncd <= ext_clk_syncd_pre; // Do this twice (resolve metastability)
60 end
61 end
62
63 // Apply PLL clock divider
64
65 clock_div #(
66 .SIZE(3)
67 ) divider (
68 .in(pll_clk),
69 .out(pll_clk_divided),
70 .N(sel),
71 .resetb(resetb)
72 );
73
Tim Edwards7a8cbb12020-10-12 11:32:11 -040074 // Secondary PLL clock divider for user space access
75
76 clock_div #(
77 .SIZE(3)
78 ) divider2 (
79 .in(pll_clk90),
80 .out(pll_clk90_divided),
81 .N(sel2),
82 .resetb(resetb)
83 );
84
85
Tim Edwards3245e2f2020-10-10 14:02:11 -040086 // Multiplex the clock output
87
88 assign core_ext_clk = (use_pll_first) ? ext_clk_syncd : ext_clk;
89 assign core_clk = (use_pll_second) ? pll_clk_divided : core_ext_clk;
Tim Edwards7a8cbb12020-10-12 11:32:11 -040090 assign user_clk = (use_pll_second) ? pll_clk90_divided : core_ext_clk;
Tim Edwards3245e2f2020-10-10 14:02:11 -040091
92 // Reset assignment. "reset" comes from POR, while "ext_reset"
93 // comes from standalone SPI (and is normally zero unless
94 // activated from the SPI).
95
96 // Staged-delay reset
97 reg [2:0] reset_delay;
98
99 always @(posedge core_clk or negedge resetb) begin
100 if (resetb == 1'b0) begin
101 reset_delay <= 3'b111;
102 end else begin
103 reset_delay <= {1'b0, reset_delay[2:1]};
104 end
105 end
106
107 assign resetb_sync = ~(reset_delay[0] | ext_reset);
108
109endmodule
Tim Edwards581068f2020-11-19 12:45:25 -0500110`default_nettype wire