usb.v 11 KB

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