usb_trans.v 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * usb_trans.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_trans (
  27. // TX Packet interface
  28. output wire txpkt_start,
  29. input wire txpkt_done,
  30. output reg [3:0] txpkt_pid,
  31. output wire [9:0] txpkt_len,
  32. output wire [7:0] txpkt_data,
  33. input wire txpkt_data_ack,
  34. // RX Packet interface
  35. input wire rxpkt_start,
  36. input wire rxpkt_done_ok,
  37. input wire rxpkt_done_err,
  38. input wire [ 3:0] rxpkt_pid,
  39. input wire rxpkt_is_sof,
  40. input wire rxpkt_is_token,
  41. input wire rxpkt_is_data,
  42. input wire rxpkt_is_handshake,
  43. input wire [10:0] rxpkt_frameno,
  44. input wire [ 6:0] rxpkt_addr,
  45. input wire [ 3:0] rxpkt_endp,
  46. input wire [ 7:0] rxpkt_data,
  47. input wire rxpkt_data_stb,
  48. // EP Data Buffers
  49. output wire [10:0] buf_tx_addr_0,
  50. input wire [ 7:0] buf_tx_data_1,
  51. output wire buf_tx_rden_0,
  52. output wire [10:0] buf_rx_addr_0,
  53. output wire [ 7:0] buf_rx_data_0,
  54. output wire buf_rx_wren_0,
  55. // EP Status RAM
  56. output wire eps_read_0,
  57. output wire eps_zero_0,
  58. output wire eps_write_0,
  59. output wire [ 7:0] eps_addr_0,
  60. output wire [15:0] eps_wrdata_0,
  61. input wire [15:0] eps_rddata_3,
  62. // Config / Status
  63. input wire [ 6:0] cr_addr,
  64. output wire [11:0] evt_data,
  65. output wire evt_stb,
  66. output wire cel_state,
  67. input wire cel_rel,
  68. input wire cel_ena,
  69. // Common
  70. input wire clk,
  71. input wire rst
  72. );
  73. `include "usb_defs.vh"
  74. // Signals
  75. // -------
  76. // Micro-Code
  77. reg [ 3:0] mc_a_reg;
  78. reg mc_rst_n;
  79. (* keep="true" *) wire [ 3:0] mc_match_bits;
  80. wire mc_match;
  81. wire mc_jmp;
  82. wire [ 7:0] mc_pc;
  83. reg [ 7:0] mc_pc_nxt;
  84. wire [15:0] mc_opcode;
  85. (* keep="true" *) wire mc_op_ld;
  86. (* keep="true" *) wire mc_op_ep;
  87. (* keep="true" *) wire mc_op_zlen;
  88. (* keep="true" *) wire mc_op_tx;
  89. (* keep="true" *) wire mc_op_notify;
  90. (* keep="true" *) wire mc_op_evt_clr;
  91. (* keep="true" *) wire mc_op_evt_rto;
  92. // Events
  93. wire [3:0] evt_rst;
  94. wire [3:0] evt_set;
  95. reg [3:0] evt;
  96. wire rto_now;
  97. reg [9:0] rto_cnt;
  98. // Transaction / EndPoint / Buffer infos
  99. reg [3:0] trans_pid;
  100. reg trans_is_setup;
  101. reg trans_addr_zero;
  102. reg trans_addr_match;
  103. reg [3:0] trans_endp;
  104. reg trans_dir;
  105. reg [2:0] ep_type;
  106. reg ep_bd_dual;
  107. reg ep_bd_ctrl;
  108. reg ep_bd_idx_cur;
  109. reg ep_bd_idx_nxt;
  110. reg ep_data_toggle;
  111. reg [2:0] bd_state;
  112. // EP & BD Infos fetch/writeback
  113. localparam
  114. EPFW_IDLE = 4'b0000,
  115. EPFW_RD_STATUS = 4'b0100,
  116. EPFW_RD_BD_W0 = 4'b0110,
  117. EPFW_RD_BD_W1 = 4'b0111,
  118. EPFW_WR_STATUS = 4'b1000,
  119. EPFW_WR_BD_W0 = 4'b1010;
  120. reg [3:0] epfw_state;
  121. reg [5:0] epfw_cap_dl;
  122. reg epfw_issue_wb;
  123. // Control Endpoint Lockout
  124. reg cel_state_i;
  125. // Packet TX
  126. reg txpkt_start_i;
  127. // Address
  128. reg [10:0] addr;
  129. wire addr_inc;
  130. wire addr_ld;
  131. // Length
  132. reg [10:0] bd_length;
  133. reg [ 9:0] xfer_length;
  134. wire len_ld;
  135. wire len_bd_dec;
  136. wire len_xf_inc;
  137. // Micro-Code execution engine
  138. // ---------------------------
  139. // Local reset to avoid being in the critical path
  140. always @(posedge clk or posedge rst)
  141. if (rst)
  142. mc_rst_n <= 1'b0;
  143. else
  144. mc_rst_n <= 1'b1;
  145. // Conditional Jump handling
  146. assign mc_match_bits = (mc_a_reg[3:0] & mc_opcode[7:4]) ^ mc_opcode[3:0];
  147. assign mc_match = ~|mc_match_bits;
  148. assign mc_jmp = mc_opcode[15] & mc_rst_n & (mc_match ^ mc_opcode[14]);
  149. assign mc_pc = mc_jmp ? {mc_opcode[13:8], 2'b00} : mc_pc_nxt;
  150. // Program counter
  151. always @(posedge clk or posedge rst)
  152. if (rst)
  153. mc_pc_nxt <= 8'h00;
  154. else
  155. mc_pc_nxt <= mc_pc + 1;
  156. // Microcode ROM
  157. SB_RAM40_4K #(
  158. .INIT_FILE("usb_trans_mc.hex"),
  159. .WRITE_MODE(0),
  160. .READ_MODE(0)
  161. ) mc_rom_I (
  162. .RDATA(mc_opcode),
  163. .RADDR({3'b000, mc_pc}),
  164. .RCLK(clk),
  165. .RCLKE(1'b1),
  166. .RE(1'b1),
  167. .WDATA(16'h0000),
  168. .WADDR(11'h000),
  169. .MASK(16'h0000),
  170. .WCLK(1'b0),
  171. .WCLKE(1'b0),
  172. .WE(1'b0)
  173. );
  174. // Decode opcodes
  175. assign mc_op_ld = mc_opcode[15:12] == 4'b0001;
  176. assign mc_op_ep = mc_opcode[15:12] == 4'b0010;
  177. assign mc_op_zlen = mc_opcode[15:12] == 4'b0011;
  178. assign mc_op_tx = mc_opcode[15:12] == 4'b0100;
  179. assign mc_op_notify = mc_opcode[15:12] == 4'b0101;
  180. assign mc_op_evt_clr = mc_opcode[15:12] == 4'b0110;
  181. assign mc_op_evt_rto = mc_opcode[15:12] == 4'b0111;
  182. // A-register
  183. always @(posedge clk)
  184. if (mc_op_ld)
  185. casez (mc_opcode[2:1])
  186. 2'b00: mc_a_reg <= evt;
  187. 2'b01: mc_a_reg <= rxpkt_pid ^ { ep_data_toggle & mc_opcode[0], 3'b000 };
  188. 2'b10: mc_a_reg <= { cel_state_i, ep_type };
  189. 2'b11: mc_a_reg <= { 1'b0, bd_state };
  190. default: mc_a_reg <= 4'hx;
  191. endcase
  192. // Events
  193. // ------
  194. // Latch events
  195. always @(posedge clk or posedge rst)
  196. if (rst)
  197. evt <= 4'h0;
  198. else
  199. evt <= (evt & ~evt_rst) | evt_set;
  200. assign evt_rst = {4{mc_op_evt_clr}} & mc_opcode[3:0];
  201. assign evt_set = { rto_now, txpkt_done, rxpkt_done_err, rxpkt_done_ok };
  202. // RX Timeout counter
  203. always @(posedge clk or posedge rst)
  204. if (rst)
  205. rto_cnt <= 0;
  206. else
  207. if (mc_op_evt_rto)
  208. rto_cnt <= { 2'b01, mc_opcode[7:0] };
  209. else
  210. rto_cnt <= {
  211. rto_cnt[9] & rto_cnt[8] & ~rxpkt_start,
  212. rto_cnt[8:0] - rto_cnt[9]
  213. };
  214. assign rto_now = rto_cnt[9] & ~rto_cnt[8];
  215. // Host NOTIFY
  216. // -----------
  217. assign evt_stb = mc_op_notify;
  218. assign evt_data = {
  219. mc_opcode[3:0], // [11:8] Micro-code return value
  220. trans_endp, // [ 7:4] Endpoint
  221. trans_dir, // [3] Direction
  222. trans_is_setup, // [2] SETUP transaction
  223. ep_bd_idx_cur, // [1] BD where it happenned
  224. 1'b0
  225. };
  226. // EP infos
  227. // --------
  228. // Capture EP# and direction when we get a TOKEN packet
  229. always @(posedge clk)
  230. if (rxpkt_done_ok & rxpkt_is_token) begin
  231. trans_pid <= rxpkt_pid;
  232. trans_is_setup <= rxpkt_pid == PID_SETUP;
  233. trans_addr_zero <= rxpkt_addr == 6'h00;
  234. trans_addr_match <= rxpkt_addr == cr_addr;
  235. trans_endp <= rxpkt_endp;
  236. trans_dir <= rxpkt_pid == PID_IN;
  237. end
  238. // EP Status Fetch/WriteBack (epfw)
  239. // State
  240. always @(posedge clk or posedge rst)
  241. if (rst)
  242. epfw_state <= EPFW_IDLE;
  243. else
  244. case (epfw_state)
  245. EPFW_IDLE:
  246. if (epfw_issue_wb)
  247. epfw_state <= EPFW_WR_STATUS;
  248. else if (rxpkt_done_ok & rxpkt_is_token)
  249. epfw_state <= EPFW_RD_STATUS;
  250. else if (epfw_cap_dl[1:0] == 2'b01)
  251. epfw_state <= EPFW_RD_BD_W0;
  252. else
  253. epfw_state <= EPFW_IDLE;
  254. EPFW_RD_STATUS:
  255. epfw_state <= EPFW_IDLE;
  256. EPFW_RD_BD_W0:
  257. epfw_state <= EPFW_RD_BD_W1;
  258. EPFW_RD_BD_W1:
  259. epfw_state <= EPFW_IDLE;
  260. EPFW_WR_STATUS:
  261. epfw_state <= EPFW_WR_BD_W0;
  262. EPFW_WR_BD_W0:
  263. epfw_state <= EPFW_IDLE;
  264. default:
  265. epfw_state <= EPFW_IDLE;
  266. endcase
  267. // Issue command to RAM
  268. assign eps_zero_0 = 1'b0;
  269. assign eps_read_0 = epfw_state[2];
  270. assign eps_write_0 = epfw_state[3];
  271. assign eps_addr_0 = {
  272. trans_endp,
  273. trans_dir,
  274. epfw_state[1],
  275. epfw_state[1] & ep_bd_idx_cur,
  276. epfw_state[0]
  277. };
  278. assign eps_wrdata_0 = epfw_state[1] ?
  279. { bd_state, trans_is_setup, 2'b00, xfer_length[9:0] } :
  280. { 8'h00, ep_data_toggle, ep_bd_idx_nxt, ep_bd_ctrl, ep_bd_dual, 1'b0, ep_type };
  281. // Delay line for what to expect on read data
  282. always @(posedge clk or posedge rst)
  283. if (rst)
  284. epfw_cap_dl = 6'b000000;
  285. else
  286. epfw_cap_dl <= {
  287. epfw_state[1],
  288. epfw_state[2] & ~^epfw_state[1:0],
  289. epfw_cap_dl[5:2]
  290. };
  291. // Capture read data
  292. always @(posedge clk)
  293. begin
  294. // EP Status
  295. if (epfw_cap_dl[1:0] == 2'b01) begin
  296. ep_type <= eps_rddata_3[2:0];
  297. ep_bd_dual <= eps_rddata_3[4];
  298. ep_bd_ctrl <= eps_rddata_3[5];
  299. ep_bd_idx_cur <= eps_rddata_3[5] ? trans_is_setup : eps_rddata_3[6];
  300. ep_bd_idx_nxt <= eps_rddata_3[6];
  301. ep_data_toggle <= eps_rddata_3[7] & ~trans_is_setup; /* For SETUP, DT == 0 */
  302. end else begin
  303. ep_data_toggle <= ep_data_toggle ^ (mc_op_ep & mc_opcode[0]);
  304. ep_bd_idx_nxt <= ep_bd_idx_nxt ^ (mc_op_ep & mc_opcode[1] & ep_bd_dual );
  305. end
  306. // BD Word 0
  307. if (epfw_cap_dl[1:0] == 2'b10) begin
  308. bd_state <= eps_rddata_3[15:13];
  309. end else begin
  310. bd_state <= (mc_op_ep & mc_opcode[2]) ? mc_opcode[5:3]: bd_state;
  311. end
  312. end
  313. // When do to write backs
  314. always @(posedge clk)
  315. epfw_issue_wb <= mc_op_ep & mc_opcode[7];
  316. // Control Endpoint Lockout
  317. // ------------------------
  318. always @(posedge clk or posedge rst)
  319. if (rst)
  320. cel_state_i <= 1'b0;
  321. else
  322. cel_state_i <= cel_ena & ((cel_state_i & ~cel_rel) | (mc_op_ep & mc_opcode[8]));
  323. assign cel_state = cel_state_i;
  324. // Packet TX
  325. // ---------
  326. always @(posedge clk)
  327. if (mc_op_tx)
  328. txpkt_pid <= mc_opcode[3:0] ^ { mc_opcode[4] & ep_data_toggle, 3'b000 };
  329. always @(posedge clk)
  330. txpkt_start_i <= mc_op_tx;
  331. assign txpkt_start = txpkt_start_i;
  332. assign txpkt_len = bd_length[9:0];
  333. // Data Address/Length shared logic
  334. // --------------------------------
  335. // Address
  336. always @(posedge clk)
  337. addr <= addr_ld ? eps_rddata_3[10:0] : (addr + addr_inc);
  338. assign addr_ld = epfw_cap_dl[1:0] == 2'b11;
  339. assign addr_inc = txpkt_data_ack | txpkt_start_i | rxpkt_data_stb;
  340. // Buffer length (decrements)
  341. always @(posedge clk)
  342. if (mc_op_zlen)
  343. bd_length <= 0;
  344. else
  345. bd_length <= len_ld ? { 1'b1, eps_rddata_3[9:0] } : (bd_length - len_bd_dec);
  346. // Xfer length (increments)
  347. always @(posedge clk)
  348. xfer_length <= len_ld ? 10'h000 : (xfer_length + len_xf_inc);
  349. // Length control
  350. assign len_ld = epfw_cap_dl[1:0] == 2'b10;
  351. assign len_bd_dec = (rxpkt_data_stb | rxpkt_start) & bd_length[10];
  352. assign len_xf_inc = rxpkt_data_stb;
  353. // Data read logic
  354. // ---------------
  355. assign buf_tx_addr_0 = addr;
  356. assign buf_tx_rden_0 = txpkt_data_ack | txpkt_start_i;
  357. assign txpkt_data = buf_tx_data_1;
  358. // Data write logic
  359. // ----------------
  360. assign buf_rx_addr_0 = addr;
  361. assign buf_rx_data_0 = rxpkt_data;
  362. assign buf_rx_wren_0 = rxpkt_data_stb & bd_length[10];
  363. endmodule // usb_trans