usb_trans.v 11 KB

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