usb_trans.v 10 KB

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