sysmgr.v 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * sysmgr.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2020-2021 Sylvain Munaut <tnt@246tNt.com>
  7. * SPDX-License-Identifier: CERN-OHL-P-2.0
  8. */
  9. `default_nettype none
  10. `include "boards.vh"
  11. module sysmgr (
  12. // Memory clocks
  13. input wire [3:0] delay,
  14. input wire clk_in,
  15. output wire clk_1x,
  16. output wire clk_2x,
  17. output wire clk_4x,
  18. output wire clk_rd,
  19. output wire sync_4x,
  20. output wire sync_rd,
  21. output wire rst,
  22. // USB
  23. output wire clk_usb,
  24. output wire rst_usb
  25. );
  26. // Memory clocks / reset
  27. // ---------------------
  28. // Signals
  29. wire pll_lock;
  30. // PLL
  31. `ifdef PLL_CORE
  32. SB_PLL40_2F_CORE #(
  33. `else
  34. SB_PLL40_2F_PAD #(
  35. `endif
  36. .FEEDBACK_PATH ("SIMPLE"),
  37. .FILTER_RANGE (`PLL_FILTER_RANGE),
  38. .DIVR (`PLL_DIVR),
  39. .DIVF (`PLL_DIVF),
  40. .DIVQ (`PLL_DIVQ),
  41. .DELAY_ADJUSTMENT_MODE_RELATIVE ("DYNAMIC"),
  42. .FDA_RELATIVE (15),
  43. .SHIFTREG_DIV_MODE (0),
  44. .PLLOUT_SELECT_PORTA ("GENCLK"),
  45. .PLLOUT_SELECT_PORTB ("GENCLK")
  46. ) pll_I (
  47. `ifdef PLL_CORE
  48. .REFERENCECLK (clk_in),
  49. `else
  50. .PACKAGEPIN (clk_in),
  51. `endif
  52. .DYNAMICDELAY ({delay, 4'h0}),
  53. .PLLOUTGLOBALA (clk_rd),
  54. .PLLOUTGLOBALB (clk_4x),
  55. .RESETB (1'b1),
  56. .LOCK (pll_lock)
  57. );
  58. // Fabric derived clocks
  59. ice40_serdes_crg #(
  60. .NO_CLOCK_2X(0)
  61. ) crg_I (
  62. .clk_4x (clk_4x),
  63. .pll_lock (pll_lock),
  64. .clk_1x (clk_1x),
  65. .clk_2x (clk_2x),
  66. .rst (rst)
  67. );
  68. // SPI - Sync signals
  69. `ifdef MEM_spi
  70. ice40_serdes_sync #(
  71. .PHASE (2),
  72. .NEG_EDGE (0),
  73. `ifdef VIDEO_none
  74. .GLOBAL_BUF (0),
  75. .LOCAL_BUF (0),
  76. .BEL_COL ("X21"),
  77. .BEL_ROW ("Y4"),
  78. `else
  79. .GLOBAL_BUF (0),
  80. .LOCAL_BUF (1),
  81. .BEL_COL ("X15")
  82. `endif
  83. ) sync_4x_I (
  84. .clk_slow (clk_1x),
  85. .clk_fast (clk_4x),
  86. .rst (rst),
  87. .sync (sync_4x)
  88. );
  89. assign sync_rd = 1'b0;
  90. `endif
  91. // HyperRAM - Sync signals
  92. `ifdef MEM_hyperram
  93. ice40_serdes_sync #(
  94. .PHASE (2),
  95. .NEG_EDGE (0),
  96. .GLOBAL_BUF (0),
  97. .LOCAL_BUF (1),
  98. .BEL_COL ("X12"),
  99. .BEL_ROW ("Y15")
  100. ) sync_4x_I (
  101. .clk_slow (clk_1x),
  102. .clk_fast (clk_4x),
  103. .rst (rst),
  104. .sync (sync_4x)
  105. );
  106. ice40_serdes_sync #(
  107. .PHASE (2),
  108. .NEG_EDGE (0),
  109. .GLOBAL_BUF (0),
  110. .LOCAL_BUF (1),
  111. .BEL_COL ("X13"),
  112. .BEL_ROW ("Y15")
  113. ) sync_rd_I (
  114. .clk_slow (clk_1x),
  115. .clk_fast (clk_rd),
  116. .rst (rst),
  117. .sync (sync_rd)
  118. );
  119. `endif
  120. // USB clock / reset
  121. // -----------------
  122. // Signals
  123. wire rst_usb_i;
  124. reg [3:0] rst_usb_cnt;
  125. // 48 MHz source
  126. SB_HFOSC #(
  127. .TRIM_EN ("0b0"),
  128. .CLKHF_DIV ("0b00") // 48 MHz
  129. ) osc_I (
  130. .CLKHFPU (1'b1),
  131. .CLKHFEN (1'b1),
  132. .CLKHF (clk_usb)
  133. );
  134. // Logic reset generation
  135. always @(posedge clk_usb or negedge pll_lock)
  136. if (~pll_lock)
  137. rst_usb_cnt <= 4'h8;
  138. else if (rst_usb_i)
  139. rst_usb_cnt <= rst_usb_cnt + 1;
  140. assign rst_usb_i = rst_usb_cnt[3];
  141. SB_GB rst_gbuf_I (
  142. .USER_SIGNAL_TO_GLOBAL_BUFFER (rst_usb_i),
  143. .GLOBAL_BUFFER_OUTPUT (rst_usb)
  144. );
  145. endmodule // sysmgr