usb_rx_ll.v 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * usb_rx_ll.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut
  7. * All rights reserved.
  8. *
  9. * LGPL v3+, see LICENSE.lgpl3
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. `default_nettype none
  26. module usb_rx_ll (
  27. // PHY
  28. input wire phy_rx_dp,
  29. input wire phy_rx_dn,
  30. input wire phy_rx_chg,
  31. // Low-Level
  32. output wire [1:0] ll_sym,
  33. output wire ll_bit,
  34. output wire ll_valid,
  35. output wire ll_eop,
  36. output wire ll_sync,
  37. output wire ll_bs_skip,
  38. output wire ll_bs_err,
  39. // Common
  40. input wire clk,
  41. input wire rst
  42. );
  43. // Signals
  44. // -------
  45. // Sampling
  46. reg samp_active;
  47. (* keep="true" *) wire samp_sync;
  48. reg [2:0] samp_cnt;
  49. wire [1:0] samp_sym_0;
  50. reg samp_valid_0;
  51. // Decoding
  52. (* keep="true" *) wire dec_sym_same_0;
  53. (* keep="true" *) wire dec_sym_se_0; /* Symbol is SE0 or SE1 */
  54. reg [2:0] dec_eop_state_1;
  55. reg [3:0] dec_sync_state_1;
  56. reg [3:0] dec_rep_state_1;
  57. reg [1:0] dec_sym_1 = 2'b00; /* Init is for simulation benefit only */
  58. reg dec_bit_1;
  59. reg dec_valid_1;
  60. wire dec_eop_1;
  61. wire dec_sync_1;
  62. wire [2:0] dec_rep_1;
  63. reg dec_bs_skip_1;
  64. wire dec_bs_err_1;
  65. // Sampling
  66. // --------
  67. // Active
  68. // The EOP and Error signals are from the next stage, but the pipeline
  69. // violation doesn't matter, we just want to stop before the next
  70. // packet so that the resync works well at the beginning of the next
  71. // packet.
  72. always @(posedge clk or posedge rst)
  73. if (rst)
  74. samp_active <= 1'b0;
  75. else
  76. samp_active <= (samp_active | phy_rx_chg) & ~(dec_valid_1 & (dec_eop_1 | dec_bs_err_1));
  77. // When to resync
  78. assign samp_sync = ~samp_active | (~samp_cnt[2] && phy_rx_chg);
  79. // Sampling phase tracking
  80. always @(posedge clk)
  81. if (samp_sync)
  82. samp_cnt <= 3'b101;
  83. else
  84. /* The following case implements :
  85. * samp_cnt <= (samp_cnt - 1) & { samp_cnt[2], 2'b11 };
  86. * but in a way that synthesis understands well */
  87. case (samp_cnt)
  88. 3'b000: samp_cnt <= 3'b011;
  89. 3'b001: samp_cnt <= 3'b000;
  90. 3'b010: samp_cnt <= 3'b001;
  91. 3'b011: samp_cnt <= 3'b010;
  92. 3'b100: samp_cnt <= 3'b011;
  93. 3'b101: samp_cnt <= 3'b100;
  94. 3'b110: samp_cnt <= 3'b101;
  95. 3'b111: samp_cnt <= 3'b110;
  96. default: samp_cnt <= 3'bxxx;
  97. endcase
  98. // Output to next stage
  99. always @(posedge clk)
  100. samp_valid_0 <= samp_active & (samp_cnt[1:0] == 2'b01) & ~samp_valid_0;
  101. assign samp_sym_0 = { phy_rx_dp, phy_rx_dn };
  102. // Bit de-stuffing & NRZI
  103. // ----------------------
  104. // Compare with previous
  105. assign dec_sym_same_0 = (samp_sym_0 == dec_sym_1);
  106. assign dec_sym_se_0 = ~^samp_sym_0;
  107. // Symbol and Bit-value
  108. always @(posedge clk)
  109. if (samp_valid_0)
  110. begin
  111. dec_sym_1 <= samp_sym_0;
  112. dec_bit_1 <= (samp_sym_0[0] ^ samp_sym_0[1]) & // Symbol is J or K
  113. (dec_sym_1[0] ^ dec_sym_1[1]) & // Previous symbol is J or K
  114. ~(samp_sym_0[1] ^ dec_sym_1[1]); // Same symbol
  115. end
  116. always @(posedge clk)
  117. dec_valid_1 <= samp_valid_0;
  118. // EOP detect
  119. always @(posedge clk)
  120. if (samp_valid_0)
  121. case ({dec_eop_state_1[1:0], samp_sym_0})
  122. 4'b0000: dec_eop_state_1 <= 3'b001; // SE0
  123. 4'b0100: dec_eop_state_1 <= 3'b010; // SE0
  124. 4'b1000: dec_eop_state_1 <= 3'b010; // We should get J but maybe we tolerate >2 SE0 ?
  125. 4'b1010: dec_eop_state_1 <= 3'b111; // J
  126. default: dec_eop_state_1 <= 3'b000;
  127. endcase
  128. assign dec_eop_1 = dec_eop_state_1[2];
  129. // Sync tracking
  130. always @(posedge clk)
  131. if (samp_valid_0)
  132. begin
  133. if (dec_sym_se_0)
  134. dec_sync_state_1 <= 4'b0000;
  135. else
  136. casez ({dec_sync_state_1[2:0], samp_sym_0[1]})
  137. 4'b0000: dec_sync_state_1 <= 4'b0001;
  138. 4'b0011: dec_sync_state_1 <= 4'b0010;
  139. 4'b0100: dec_sync_state_1 <= 4'b0011;
  140. 4'b0111: dec_sync_state_1 <= 4'b0100;
  141. 4'b1000: dec_sync_state_1 <= 4'b0101;
  142. 4'b1011: dec_sync_state_1 <= 4'b0110;
  143. 4'b1100: dec_sync_state_1 <= 4'b0111;
  144. 4'b1110: dec_sync_state_1 <= 4'b1001;
  145. 4'b???0: dec_sync_state_1 <= 4'b0001;
  146. default: dec_sync_state_1 <= 4'b0000;
  147. endcase
  148. end
  149. assign dec_sync_1 = dec_sync_state_1[3];
  150. // Repeat tracking
  151. always @(posedge clk)
  152. if (samp_valid_0)
  153. if (dec_sym_same_0 == 1'b0)
  154. dec_rep_state_1 <= 4'b0000;
  155. else
  156. // This is basically a saturated increment with flag for >=6
  157. case (dec_rep_state_1[2:0])
  158. 3'b000: dec_rep_state_1 <= 4'b0001;
  159. 3'b001: dec_rep_state_1 <= 4'b0010;
  160. 3'b010: dec_rep_state_1 <= 4'b0011;
  161. 3'b011: dec_rep_state_1 <= 4'b0100;
  162. 3'b100: dec_rep_state_1 <= 4'b0101;
  163. 3'b101: dec_rep_state_1 <= 4'b0110;
  164. 3'b110: dec_rep_state_1 <= 4'b1111;
  165. 3'b111: dec_rep_state_1 <= 4'b1111;
  166. default: dec_rep_state_1 <= 4'bxxxx;
  167. endcase
  168. assign dec_bs_err_1 = dec_rep_state_1[3];
  169. assign dec_rep_1 = dec_rep_state_1[2:0];
  170. always @(posedge clk)
  171. if (samp_valid_0)
  172. dec_bs_skip_1 <= (dec_rep_state_1[2:0] == 3'b110);
  173. // Output
  174. // ------
  175. assign ll_sym = dec_sym_1;
  176. assign ll_bit = dec_bit_1;
  177. assign ll_valid = dec_valid_1;
  178. assign ll_eop = dec_eop_1;
  179. assign ll_sync = dec_sync_1;
  180. assign ll_bs_skip = dec_bs_skip_1;
  181. assign ll_bs_err = dec_bs_err_1;
  182. endmodule // usb_rx_ll