ice40_serdes_crg.v 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * ice40_serdes_crg.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2020 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. module ice40_serdes_crg #(
  35. parameter integer NO_CLOCK_2X = 0
  36. )(
  37. // Input from PLL
  38. input wire clk_4x,
  39. input wire pll_lock,
  40. // Outputs
  41. output wire clk_1x,
  42. output wire clk_2x,
  43. output wire rst
  44. );
  45. // Signals
  46. // -------
  47. // Reset
  48. reg [3:0] rst_cnt_nxt[0:15];
  49. reg [3:0] rst_cnt = 4'h8;
  50. reg rst_i;
  51. // Clock Divider
  52. reg [1:0] clk_div;
  53. wire clk_sync_i;
  54. // Reset
  55. // -----
  56. // Counter
  57. initial begin : rst_init
  58. integer i;
  59. for (i=0; i<16; i=i+1)
  60. rst_cnt_nxt[i] = i==15 ? i : (i+1);
  61. end
  62. always @(posedge clk_4x or negedge pll_lock)
  63. if (~pll_lock)
  64. rst_cnt <= 4'h0;
  65. else
  66. rst_cnt <= rst_cnt_nxt[rst_cnt];
  67. // Final FF
  68. always @(posedge clk_4x or negedge pll_lock)
  69. if (~pll_lock)
  70. rst_i <= 1'b1;
  71. else
  72. rst_i <= (rst_cnt != 4'hf);
  73. // Buffer reset
  74. SB_GB gbuf_rst_I (
  75. .USER_SIGNAL_TO_GLOBAL_BUFFER(rst_i),
  76. .GLOBAL_BUFFER_OUTPUT(rst)
  77. );
  78. // Clock Divider & Sync
  79. // --------------------
  80. // Simple counter to generate the edges
  81. always @(posedge clk_4x or negedge pll_lock)
  82. if (~pll_lock)
  83. clk_div <= 2'b00;
  84. else
  85. clk_div <= clk_div + rst_cnt[3];
  86. // Buffer clk_2x
  87. generate
  88. if (NO_CLOCK_2X)
  89. assign clk_2x = 1'b0;
  90. else
  91. (* BEL="X13/Y0/gb" *)
  92. SB_GB gbuf_2x_I (
  93. .USER_SIGNAL_TO_GLOBAL_BUFFER(clk_div[0]),
  94. .GLOBAL_BUFFER_OUTPUT(clk_2x)
  95. );
  96. endgenerate
  97. // Buffer clk_1x
  98. (* BEL="X12/Y0/gb" *)
  99. SB_GB gbuf_1x_I (
  100. .USER_SIGNAL_TO_GLOBAL_BUFFER(clk_div[1]),
  101. .GLOBAL_BUFFER_OUTPUT(clk_1x)
  102. );
  103. endmodule