usb.v 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * usb.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 #(
  27. parameter TARGET = "ICE40",
  28. parameter [3:0] ADDR_MSB = 4'h3,
  29. parameter integer EPDW = 16,
  30. /* Auto-set */
  31. parameter integer EPAW = 11 - $clog2(EPDW / 8)
  32. )(
  33. // Pads
  34. inout wire pad_dp,
  35. inout wire pad_dn,
  36. output reg pad_pu,
  37. // EP buffer interface
  38. input wire [EPAW-1:0] ep_tx_addr_0,
  39. input wire [EPDW-1:0] ep_tx_data_0,
  40. input wire ep_tx_we_0,
  41. input wire [EPAW-1:0] ep_rx_addr_0,
  42. output wire [EPDW-1:0] ep_rx_data_1,
  43. input wire ep_rx_re_0,
  44. input wire ep_clk,
  45. // Bus interface
  46. input wire [15:0] bus_addr,
  47. input wire [15:0] bus_din,
  48. output wire [15:0] bus_dout,
  49. input wire bus_cyc,
  50. input wire bus_we,
  51. output wire bus_ack,
  52. // Debug
  53. output wire [3:0] debug,
  54. // Common
  55. input wire clk,
  56. input wire rst
  57. );
  58. // Signals
  59. // -------
  60. // PHY
  61. wire phy_rx_dp;
  62. wire phy_rx_dn;
  63. wire phy_rx_chg;
  64. wire phy_tx_dp;
  65. wire phy_tx_dn;
  66. wire phy_tx_en;
  67. // TX Low-Level
  68. wire txll_start;
  69. wire txll_bit;
  70. wire txll_last;
  71. wire txll_ack;
  72. // TX Packet
  73. wire txpkt_start;
  74. wire txpkt_done;
  75. wire [3:0] txpkt_pid;
  76. wire [9:0] txpkt_len;
  77. wire [7:0] txpkt_data;
  78. wire txpkt_data_ack;
  79. // RX Low-Level
  80. wire [1:0] rxll_sym;
  81. wire rxll_bit;
  82. wire rxll_valid;
  83. wire rxll_eop;
  84. wire rxll_sync;
  85. wire rxll_bs_skip;
  86. wire rxll_bs_err;
  87. // RX Packet
  88. wire rxpkt_start;
  89. wire rxpkt_done_ok;
  90. wire rxpkt_done_err;
  91. wire [ 3:0] rxpkt_pid;
  92. wire rxpkt_is_sof;
  93. wire rxpkt_is_token;
  94. wire rxpkt_is_data;
  95. wire rxpkt_is_handshake;
  96. wire [10:0] rxpkt_frameno;
  97. wire [ 6:0] rxpkt_addr;
  98. wire [ 3:0] rxpkt_endp;
  99. wire [ 7:0] rxpkt_data;
  100. wire rxpkt_data_stb;
  101. // EP Buffers
  102. wire [10:0] buf_tx_addr_0;
  103. wire [ 7:0] buf_tx_data_1;
  104. wire buf_tx_rden_0;
  105. wire [10:0] buf_rx_addr_0;
  106. wire [ 7:0] buf_rx_data_0;
  107. wire buf_rx_wren_0;
  108. // EP Status
  109. wire eps_read_0;
  110. wire eps_zero_0;
  111. wire eps_write_0;
  112. wire [ 7:0] eps_addr_0;
  113. wire [15:0] eps_wrdata_0;
  114. wire [15:0] eps_rddata_3;
  115. wire eps_bus_ready;
  116. reg eps_bus_read;
  117. reg eps_bus_zero;
  118. reg eps_bus_write;
  119. wire [15:0] eps_bus_dout;
  120. // Config / Status registers
  121. reg cr_pu_ena;
  122. reg [ 6:0] cr_addr;
  123. wire [15:0] sr_notify;
  124. wire irq_stb;
  125. wire irq_state;
  126. reg irq_ack;
  127. // Bus interface
  128. reg eps_bus_req;
  129. wire eps_bus_clear;
  130. reg bus_ack_wait;
  131. wire bus_req_ok;
  132. reg [2:0] bus_req_ok_dly;
  133. // Out-of-band conditions
  134. wire oob_se0;
  135. wire oob_sop;
  136. reg [19:0] timeout_suspend; // 3 ms with no activity
  137. reg [19:0] timeout_reset; // 10 ms SE0
  138. reg rst_usb_l;
  139. reg suspend;
  140. // USB core logic reset
  141. wire rst_usb;
  142. // PHY
  143. // ---
  144. usb_phy #(
  145. .TARGET(TARGET)
  146. ) phy_I (
  147. .pad_dp(pad_dp),
  148. .pad_dn(pad_dn),
  149. .rx_dp(phy_rx_dp),
  150. .rx_dn(phy_rx_dn),
  151. .rx_chg(phy_rx_chg),
  152. .tx_dp(phy_tx_dp),
  153. .tx_dn(phy_tx_dn),
  154. `ifdef SIM
  155. .tx_en(1'b0),
  156. `else
  157. .tx_en(phy_tx_en),
  158. `endif
  159. .clk(clk),
  160. .rst(rst)
  161. );
  162. // TX
  163. // --
  164. usb_tx_ll tx_ll_I (
  165. .phy_tx_dp(phy_tx_dp),
  166. .phy_tx_dn(phy_tx_dn),
  167. .phy_tx_en(phy_tx_en),
  168. .ll_start(txll_start),
  169. .ll_bit(txll_bit),
  170. .ll_last(txll_last),
  171. .ll_ack(txll_ack),
  172. .clk(clk),
  173. .rst(rst)
  174. );
  175. usb_tx_pkt tx_pkt_I (
  176. .ll_start(txll_start),
  177. .ll_bit(txll_bit),
  178. .ll_last(txll_last),
  179. .ll_ack(txll_ack),
  180. .pkt_start(txpkt_start),
  181. .pkt_done(txpkt_done),
  182. .pkt_pid(txpkt_pid),
  183. .pkt_len(txpkt_len),
  184. .pkt_data(txpkt_data),
  185. .pkt_data_ack(txpkt_data_ack),
  186. .clk(clk),
  187. .rst(rst)
  188. );
  189. // RX
  190. // --
  191. usb_rx_ll rx_ll_I (
  192. .phy_rx_dp(phy_rx_dp),
  193. .phy_rx_dn(phy_rx_dn),
  194. .phy_rx_chg(phy_rx_chg),
  195. .ll_sym(rxll_sym),
  196. .ll_bit(rxll_bit),
  197. .ll_valid(rxll_valid),
  198. .ll_eop(rxll_eop),
  199. .ll_sync(rxll_sync),
  200. .ll_bs_skip(rxll_bs_skip),
  201. .ll_bs_err(rxll_bs_err),
  202. .clk(clk),
  203. .rst(rst)
  204. );
  205. usb_rx_pkt rx_pkt_I (
  206. .ll_sym(rxll_sym),
  207. .ll_bit(rxll_bit),
  208. .ll_valid(rxll_valid),
  209. .ll_eop(rxll_eop),
  210. .ll_sync(rxll_sync),
  211. .ll_bs_skip(rxll_bs_skip),
  212. .ll_bs_err(rxll_bs_err),
  213. .pkt_start(rxpkt_start),
  214. .pkt_done_ok(rxpkt_done_ok),
  215. .pkt_done_err(rxpkt_done_err),
  216. .pkt_pid(rxpkt_pid),
  217. .pkt_is_sof(rxpkt_is_sof),
  218. .pkt_is_token(rxpkt_is_token),
  219. .pkt_is_data(rxpkt_is_data),
  220. .pkt_is_handshake(rxpkt_is_handshake),
  221. .pkt_frameno(rxpkt_frameno),
  222. .pkt_addr(rxpkt_addr),
  223. .pkt_endp(rxpkt_endp),
  224. .pkt_data(rxpkt_data),
  225. .pkt_data_stb(rxpkt_data_stb),
  226. .inhibit(phy_tx_en),
  227. .clk(clk),
  228. .rst(rst)
  229. );
  230. // Transaction control
  231. // -------------------
  232. usb_trans trans_I (
  233. .txpkt_start(txpkt_start),
  234. .txpkt_done(txpkt_done),
  235. .txpkt_pid(txpkt_pid),
  236. .txpkt_len(txpkt_len),
  237. .txpkt_data(txpkt_data),
  238. .txpkt_data_ack(txpkt_data_ack),
  239. .rxpkt_start(rxpkt_start),
  240. .rxpkt_done_ok(rxpkt_done_ok),
  241. .rxpkt_done_err(rxpkt_done_err),
  242. .rxpkt_pid(rxpkt_pid),
  243. .rxpkt_is_sof(rxpkt_is_sof),
  244. .rxpkt_is_token(rxpkt_is_token),
  245. .rxpkt_is_data(rxpkt_is_data),
  246. .rxpkt_is_handshake(rxpkt_is_handshake),
  247. .rxpkt_frameno(rxpkt_frameno),
  248. .rxpkt_addr(rxpkt_addr),
  249. .rxpkt_endp(rxpkt_endp),
  250. .rxpkt_data(rxpkt_data),
  251. .rxpkt_data_stb(rxpkt_data_stb),
  252. .buf_tx_addr_0(buf_tx_addr_0),
  253. .buf_tx_data_1(buf_tx_data_1),
  254. .buf_tx_rden_0(buf_tx_rden_0),
  255. .buf_rx_addr_0(buf_rx_addr_0),
  256. .buf_rx_data_0(buf_rx_data_0),
  257. .buf_rx_wren_0(buf_rx_wren_0),
  258. .eps_read_0(eps_read_0),
  259. .eps_zero_0(eps_zero_0),
  260. .eps_write_0(eps_write_0),
  261. .eps_addr_0(eps_addr_0),
  262. .eps_wrdata_0(eps_wrdata_0),
  263. .eps_rddata_3(eps_rddata_3),
  264. .cr_addr(cr_addr),
  265. .sr_notify(sr_notify),
  266. .irq_stb(irq_stb),
  267. .irq_state(irq_state),
  268. .irq_ack(irq_ack),
  269. .debug(debug),
  270. .clk(clk),
  271. .rst(rst)
  272. );
  273. // EP buffers
  274. // ----------
  275. usb_ep_buf #(
  276. .TARGET(TARGET),
  277. .RWIDTH(8),
  278. .WWIDTH(EPDW)
  279. ) tx_buf_I (
  280. .rd_addr_0(buf_tx_addr_0),
  281. .rd_data_1(buf_tx_data_1),
  282. .rd_en_0(buf_tx_rden_0),
  283. .rd_clk(clk),
  284. .wr_addr_0(ep_tx_addr_0),
  285. .wr_data_0(ep_tx_data_0),
  286. .wr_en_0(ep_tx_we_0),
  287. .wr_clk(ep_clk)
  288. );
  289. usb_ep_buf #(
  290. .TARGET(TARGET),
  291. .RWIDTH(EPDW),
  292. .WWIDTH(8)
  293. ) rx_buf_I (
  294. .rd_addr_0(ep_rx_addr_0),
  295. .rd_data_1(ep_rx_data_1),
  296. .rd_en_0(ep_rx_re_0),
  297. .rd_clk(ep_clk),
  298. .wr_addr_0(buf_rx_addr_0),
  299. .wr_data_0(buf_rx_data_0),
  300. .wr_en_0(buf_rx_wren_0),
  301. .wr_clk(clk)
  302. );
  303. // EP Status / Buffer Descriptors
  304. // ------------------------------
  305. usb_ep_status ep_status_I (
  306. .p_addr_0(eps_addr_0),
  307. .p_read_0(eps_read_0),
  308. .p_zero_0(eps_zero_0),
  309. .p_write_0(eps_write_0),
  310. .p_din_0(eps_wrdata_0),
  311. .p_dout_3(eps_rddata_3),
  312. .s_addr_0(bus_addr[7:0]),
  313. .s_read_0(eps_bus_ready),
  314. .s_zero_0(eps_bus_zero),
  315. .s_write_0(eps_bus_write),
  316. .s_din_0(bus_din),
  317. .s_dout_3(eps_bus_dout),
  318. .s_ready_0(eps_bus_ready),
  319. .clk(clk),
  320. .rst(rst)
  321. );
  322. // Bus Interface
  323. // -------------
  324. (* keep="true" *) wire bus_msb_match;
  325. wire [15:0] csr_dout;
  326. wire csr_bus_clear;
  327. reg csr_req;
  328. reg cr_bus_we;
  329. reg sr_bus_re;
  330. // Match the MSB
  331. assign bus_msb_match = bus_addr[15:12] == ADDR_MSB;
  332. // Request lines for registers
  333. always @(posedge clk)
  334. if (csr_bus_clear) begin
  335. csr_req <= 1'b0;
  336. cr_bus_we <= 1'b0;
  337. sr_bus_re <= 1'b0;
  338. end else begin
  339. csr_req <= bus_msb_match & ~bus_addr[11];
  340. cr_bus_we <= bus_msb_match & ~bus_addr[11] & bus_we;
  341. sr_bus_re <= bus_msb_match & ~bus_addr[11] & ~bus_we;
  342. end
  343. // Request lines for EP Status access
  344. always @(posedge clk)
  345. if (eps_bus_clear) begin
  346. eps_bus_read <= 1'b0;
  347. eps_bus_zero <= 1'b1;
  348. eps_bus_write <= 1'b0;
  349. eps_bus_req <= 1'b0;
  350. end else begin
  351. eps_bus_read <= bus_msb_match & bus_addr[11] & ~bus_we;
  352. eps_bus_zero <= ~bus_msb_match | ~bus_addr[11];
  353. eps_bus_write <= bus_msb_match & bus_addr[11] & bus_we;
  354. eps_bus_req <= bus_msb_match & bus_addr[11];
  355. end
  356. // Condition to force the requests to zero :
  357. // no access needed, ack pending or this cycle went through
  358. assign csr_bus_clear = ~bus_cyc | csr_req;
  359. assign eps_bus_clear = ~bus_cyc | bus_ack_wait | (eps_bus_req & eps_bus_ready);
  360. // Track when request are accepted by the RAM
  361. assign bus_req_ok = (eps_bus_req & eps_bus_ready);
  362. always @(posedge clk)
  363. bus_req_ok_dly <= { bus_req_ok_dly[1:0], bus_req_ok & ~bus_we };
  364. // ACK wait state tracking
  365. always @(posedge clk or posedge rst)
  366. if (rst)
  367. bus_ack_wait <= 1'b0;
  368. else
  369. bus_ack_wait <= ((bus_ack_wait & ~bus_we) | bus_req_ok) & ~bus_req_ok_dly[2];
  370. // Bus Ack
  371. assign bus_ack = csr_req | (bus_ack_wait & (bus_we | bus_req_ok_dly[2]));
  372. // Output is simply the OR of all local units since we force them to zero if
  373. // they're not accessed
  374. assign bus_dout = eps_bus_dout | csr_dout;
  375. // Config registers
  376. // ----------------
  377. // Write regs
  378. always @(posedge clk)
  379. if (cr_bus_we) begin
  380. cr_pu_ena <= bus_din[15];
  381. cr_addr <= bus_din[13:8];
  382. end
  383. // Write strobe
  384. always @(posedge clk)
  385. irq_ack <= cr_bus_we & bus_din[0];
  386. // Read mux
  387. assign csr_dout = sr_bus_re ? sr_notify : 16'h0000;
  388. // USB reset/suspend
  389. // -----------------
  390. // Detect some conditions for triggers
  391. assign oob_se0 = !phy_rx_dp && !phy_rx_dn;
  392. assign oob_sop = rxpkt_start & rxpkt_is_sof;
  393. // Suspend timeout counter
  394. always @(posedge clk)
  395. if (rst_usb)
  396. timeout_suspend <= 20'ha3280;
  397. else
  398. timeout_suspend <= oob_sop ? 20'ha3280 : (timeout_suspend - timeout_suspend[19]);
  399. always @(posedge clk)
  400. if (rst_usb)
  401. suspend <= 1'b0;
  402. else
  403. suspend <= ~timeout_suspend[19];
  404. // Reset timeout counter
  405. always @(posedge clk)
  406. if (rst)
  407. timeout_reset <= 20'hf5300;
  408. else
  409. timeout_reset <= oob_se0 ? (timeout_reset - timeout_reset[19]) : 20'hf5300;
  410. always @(posedge clk)
  411. if (rst)
  412. rst_usb_l <= 1'b1;
  413. else
  414. rst_usb_l <= ~timeout_reset[19];
  415. // Global reset driver
  416. generate
  417. if (TARGET == "GENERIC")
  418. assign rst_usb = rst_usb_l;
  419. else if (TARGET == "ICE40")
  420. SB_GB usb_rst_gb_I (
  421. .USER_SIGNAL_TO_GLOBAL_BUFFER(rst_usb_l),
  422. .GLOBAL_BUFFER_OUTPUT(rst_usb)
  423. );
  424. endgenerate
  425. // Detection pin
  426. always @(posedge clk)
  427. if (rst)
  428. pad_pu <= 1'b0;
  429. else
  430. pad_pu <= cr_pu_ena;
  431. endmodule // usb