nano_dsi_clk.v 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * nano_dsi_clk.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 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 nano_dsi_clk (
  35. // nano-PMOD - CLK lane
  36. output wire clk_lp,
  37. output wire clk_hs_p,
  38. output wire clk_hs_n,
  39. // Control interface
  40. input wire hs_req,
  41. output wire hs_rdy,
  42. // Clock/Data sync
  43. output wire clk_sync,
  44. // Config
  45. input wire [7:0] cfg_hs_prep,
  46. input wire [7:0] cfg_hs_zero,
  47. input wire [7:0] cfg_hs_trail,
  48. // Clock / Reset
  49. input wire clk,
  50. input wire rst
  51. );
  52. // Signals
  53. // -------
  54. // IO control
  55. reg io_lp;
  56. reg io_hs_active;
  57. reg io_hs_bit;
  58. // FSM
  59. localparam
  60. ST_LP11 = 0,
  61. ST_LP00 = 1,
  62. ST_HS_ZERO = 2,
  63. ST_HS_CLK = 3,
  64. ST_HS_TRAIL = 4;
  65. reg [2:0] fsm_state;
  66. reg [2:0] fsm_state_next;
  67. // Timer
  68. reg [7:0] timer_val;
  69. wire timer_trig;
  70. // Clocking
  71. reg clk_sync_i;
  72. // IOBs
  73. // ----
  74. // LP bias control
  75. SB_IO #(
  76. .PIN_TYPE(6'b100100),
  77. .PULLUP(1'b0),
  78. .NEG_TRIGGER(1'b0),
  79. .IO_STANDARD("SB_LVCMOS")
  80. ) iob_clk_lp_I (
  81. .PACKAGE_PIN(clk_lp),
  82. .CLOCK_ENABLE(1'b1),
  83. .INPUT_CLK(1'b0),
  84. .OUTPUT_CLK(clk),
  85. .OUTPUT_ENABLE(1'b1),
  86. .D_OUT_0(io_lp),
  87. .D_OUT_1(1'b0),
  88. .D_IN_0(),
  89. .D_IN_1()
  90. );
  91. // HS drivers
  92. SB_IO #(
  93. .PIN_TYPE(6'b110000),
  94. .PULLUP(1'b0),
  95. .NEG_TRIGGER(1'b0),
  96. .IO_STANDARD("SB_LVCMOS")
  97. ) iob_clk_hs_p_I (
  98. .PACKAGE_PIN(clk_hs_p),
  99. .CLOCK_ENABLE(1'b1),
  100. .INPUT_CLK(1'b0),
  101. .OUTPUT_CLK(clk),
  102. .OUTPUT_ENABLE(io_hs_active),
  103. .D_OUT_0(io_hs_bit),
  104. .D_OUT_1(io_hs_bit),
  105. .D_IN_0(),
  106. .D_IN_1()
  107. );
  108. SB_IO #(
  109. .PIN_TYPE(6'b110000),
  110. .PULLUP(1'b0),
  111. .NEG_TRIGGER(1'b0),
  112. .IO_STANDARD("SB_LVCMOS")
  113. ) iob_clk_hs_n_I (
  114. .PACKAGE_PIN(clk_hs_n),
  115. .CLOCK_ENABLE(1'b1),
  116. .INPUT_CLK(1'b0),
  117. .OUTPUT_CLK(clk),
  118. .OUTPUT_ENABLE(io_hs_active),
  119. .D_OUT_0(~io_hs_bit),
  120. .D_OUT_1(~io_hs_bit),
  121. .D_IN_0(),
  122. .D_IN_1()
  123. );
  124. // FSM
  125. // ---
  126. // State register
  127. always @(posedge clk or posedge rst)
  128. if (rst)
  129. fsm_state <= ST_LP11;
  130. else
  131. fsm_state <= fsm_state_next;
  132. // Next State logic
  133. always @(*)
  134. begin
  135. // Default is to not move
  136. fsm_state_next = fsm_state;
  137. // Transitions ?
  138. case (fsm_state)
  139. ST_LP11:
  140. if (hs_req)
  141. fsm_state_next = ST_LP00;
  142. ST_LP00:
  143. if (timer_trig)
  144. fsm_state_next = ST_HS_ZERO;
  145. ST_HS_ZERO:
  146. if (timer_trig)
  147. fsm_state_next = ST_HS_CLK;
  148. ST_HS_CLK:
  149. if (~hs_req)
  150. fsm_state_next = ST_HS_TRAIL;
  151. ST_HS_TRAIL:
  152. if (timer_trig)
  153. fsm_state_next = ST_LP11;
  154. endcase
  155. end
  156. // Timer
  157. // -----
  158. always @(posedge clk)
  159. begin
  160. if (fsm_state != fsm_state_next) begin
  161. // Default is to trigger all the time
  162. timer_val <= 8'h80;
  163. // Preload for next state
  164. case (fsm_state_next)
  165. ST_LP00: timer_val <= cfg_hs_prep;
  166. ST_HS_ZERO: timer_val <= cfg_hs_zero;
  167. ST_HS_TRAIL: timer_val <= cfg_hs_trail;
  168. endcase
  169. end else begin
  170. timer_val <= timer_val - 1;
  171. end
  172. end
  173. assign timer_trig = timer_val[7];
  174. // Clock sync
  175. // ----------
  176. always @(posedge clk or posedge rst)
  177. if (rst)
  178. clk_sync_i <= 1'b0;
  179. else
  180. clk_sync_i <= ~clk_sync_i;
  181. assign clk_sync = clk_sync_i;
  182. // IO control
  183. // ----------
  184. always @(posedge clk)
  185. begin
  186. io_lp <= (fsm_state == ST_LP11);
  187. io_hs_active <=
  188. (fsm_state == ST_HS_ZERO) ||
  189. (fsm_state == ST_HS_CLK) ||
  190. (fsm_state == ST_HS_TRAIL);
  191. io_hs_bit <= clk_sync && (fsm_state == ST_HS_CLK);
  192. end
  193. endmodule // nano_dsi_clk