usb_trans.v 10 KB

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