usb_trans.v 11 KB

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