qspi_master.v 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * qspi_master.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. module qspi_master #(
  35. parameter integer CMD_READ = 16'hEBEB,
  36. parameter integer CMD_WRITE = 16'h0202,
  37. parameter integer DUMMY_CLK = 6,
  38. parameter integer PAUSE_CLK = 3,
  39. parameter integer FIFO_DEPTH = 1,
  40. parameter integer N_CS = 2, /* CS count */
  41. parameter integer PHY_SPEED = 1, /* Speed Factor: 1x 2x 4x */
  42. parameter integer PHY_WIDTH = 1, /* Width Factor: 1x 2x */
  43. parameter integer PHY_DELAY = 6, /* See PHY doc */
  44. // auto
  45. parameter integer PTW = (PHY_WIDTH * 4 * PHY_SPEED), /* PHY Total Width */
  46. parameter integer PCW = ( 4 * PHY_SPEED), /* PHY Channel Width */
  47. parameter integer PSW = ( PHY_SPEED) /* PHY Signal Width */
  48. )(
  49. // PHY interface
  50. input wire [PTW-1:0] phy_io_i,
  51. output reg [PTW-1:0] phy_io_o,
  52. output reg [ 3:0] phy_io_oe,
  53. output reg [PSW-1:0] phy_clk_o,
  54. output reg [N_CS-1:0] phy_cs_o,
  55. // Memory interface
  56. input wire [ 1:0] mi_addr_cs,
  57. input wire [23:0] mi_addr,
  58. input wire [ 6:0] mi_len,
  59. input wire mi_rw, /* 0=Write, 1=Read */
  60. input wire mi_valid,
  61. output wire mi_ready,
  62. input wire [31:0] mi_wdata,
  63. output wire mi_wack,
  64. output wire mi_wlast,
  65. output wire [31:0] mi_rdata,
  66. output wire mi_rstb,
  67. output wire mi_rlast,
  68. // Wishbone interface
  69. input wire [ 4:0] wb_addr,
  70. input wire [31:0] wb_wdata,
  71. output reg [31:0] wb_rdata,
  72. input wire wb_we,
  73. input wire wb_cyc,
  74. output reg wb_ack,
  75. // Common
  76. input wire clk,
  77. input wire rst
  78. );
  79. localparam integer STW = 32; /* Shifter Total Width */
  80. localparam integer SCW = STW / PHY_WIDTH; /* Shifter Channel Width */
  81. // Mapping Helpers
  82. // ---------------
  83. /*
  84. * PHY signal mapping:
  85. * phy_io = [ chan_1 | chan_0 ]
  86. * chan_i = [ io_3 | io_2 | io_1 | io_0 ]
  87. * io_i = [ t_0 ... t_n ] (t_0 being the 'first')
  88. *
  89. * Shifter-Out format:
  90. * Shifter-In format:
  91. * shift_data = [ chan_1 | chan_0 ]
  92. * chan_i_qpi = [ io_3(0) io_2(0) io_1(0) io_0(0) io_3(1) ... ]
  93. * chan_i_spi = [ t_0 t_1 ... t_n ] (t_0 being 'first')
  94. *
  95. * Mem IF data:
  96. * mi_{r,w}data = [ b3 | b2 | b1 | b0 ]
  97. *
  98. * Data is stored in memory in big-endian (b3 b2 b1 b0)
  99. * and in case of multiple channel (b1 b0) in chan 0 and (b3 b2) in chan 1
  100. *
  101. * Wishbone data:
  102. * spi_xfer: bits are taken in order and shifted out MSB first
  103. * In case of multiple channel the register is split in 2x16 bits
  104. * qpi_cmd: bits are taken in order and shifted out MSB first
  105. * qpi_read / qpi_data: See Mem IF data mapping
  106. */
  107. function [PTW-1:0] shift2phy_spi;
  108. input [STW-1:0] shift;
  109. input [PTW-1:0] base;
  110. integer chan;
  111. begin
  112. // Set default value for the signals that don't matter for SPI
  113. shift2phy_spi = base;
  114. // Overwrite only the PHY IO0 line (MOSI)
  115. for (chan=0; chan<PHY_WIDTH; chan=chan+1)
  116. shift2phy_spi[chan*PCW+:PSW] = shift[((chan+1)*SCW-1)-:PSW];
  117. end
  118. endfunction
  119. function [STW-1:0] shift_spi;
  120. input [STW-1:0] shift;
  121. integer chan;
  122. begin
  123. for (chan=0; chan<PHY_WIDTH; chan=chan+1)
  124. shift_spi[chan*SCW+:SCW] = { shift[chan*SCW+:SCW-PSW], {PSW{1'bx}} };
  125. end
  126. endfunction
  127. function [PTW-1:0] shift2phy_qpi_cmd;
  128. input [STW-1:0] shift;
  129. integer chan, io, t;
  130. begin
  131. for (chan=0; chan<PHY_WIDTH; chan=chan+1)
  132. for (t=0; t<PHY_SPEED; t=t+1)
  133. for (io=0; io<4; io=io+1)
  134. shift2phy_qpi_cmd[chan*PCW + io*PSW + t] = shift[STW - (4*PHY_SPEED) + t*4 + io];
  135. end
  136. endfunction
  137. function [STW-1:0] shift_qpi_cmd;
  138. input [STW-1:0] shift;
  139. begin
  140. shift_qpi_cmd[STW-1:0] = { shift[STW-PCW-1:0], {PCW{1'bx}} };
  141. end
  142. endfunction
  143. function [PTW-1:0] shift2phy_qpi_data;
  144. input [STW-1:0] shift;
  145. integer chan, io, t;
  146. begin
  147. for (chan=0; chan<PHY_WIDTH; chan=chan+1)
  148. for (t=0; t<PHY_SPEED; t=t+1)
  149. for (io=0; io<4; io=io+1)
  150. shift2phy_qpi_data[chan*PCW + io*PSW + t] = shift[(chan+1)*SCW - (4*PHY_SPEED) + t*4 + io];
  151. end
  152. endfunction
  153. function [STW-1:0] shift_qpi_data;
  154. input [STW-1:0] shift;
  155. integer chan;
  156. begin
  157. if (SCW == PCW)
  158. shift_qpi_data = { STW{1'bx} };
  159. else
  160. for (chan=0; chan<PHY_WIDTH; chan=chan+1)
  161. shift_qpi_data[chan*SCW+:SCW] = { shift[chan*SCW+:SCW-PCW], {PCW{1'bx}} };
  162. end
  163. endfunction
  164. function [STW-1:0] phy2shift_spi;
  165. input [STW-1:0] prev;
  166. input [PTW-1:0] phy;
  167. integer chan, t;
  168. begin
  169. for (chan=0; chan<PHY_WIDTH; chan=chan+1)
  170. begin
  171. // Shift previous data
  172. phy2shift_spi[chan*SCW+PSW+:SCW-PSW] = prev[chan*SCW+:SCW-PSW];
  173. // Map new data
  174. phy2shift_spi[chan*SCW+:PSW] = phy[chan*PCW+PSW+:PSW];
  175. end
  176. end
  177. endfunction
  178. function [STW-1:0] phy2shift_qpi;
  179. input [STW-1:0] prev;
  180. input [PTW-1:0] phy;
  181. integer chan, t, io;
  182. begin
  183. for (chan=0; chan<PHY_WIDTH; chan=chan+1)
  184. begin
  185. // Shift previous data
  186. if (PCW != SCW)
  187. phy2shift_qpi[chan*SCW+PCW+:SCW-PCW] = prev[chan*SCW+:SCW-PCW];
  188. // Map new data
  189. for (t=0; t<PHY_SPEED; t=t+1)
  190. for (io=0; io<4; io=io+1)
  191. phy2shift_qpi[chan*SCW + t*4 + io] = phy[chan*PCW + io*PSW + t];
  192. end
  193. end
  194. endfunction
  195. // Signals
  196. // -------
  197. // Wishbone interface
  198. wire wbi_we_csr;
  199. wire [31:0] wbi_rd_csr;
  200. wire wbi_rd_rst;
  201. // Command & Reponse FIFOs
  202. wire [35:0] cf_di;
  203. reg cf_wren;
  204. wire cf_full;
  205. wire [35:0] cf_do;
  206. wire cf_rden;
  207. wire cf_empty;
  208. wire [31:0] rf_di;
  209. wire rf_wren_safe;
  210. wire rf_wren;
  211. wire rf_full;
  212. wire [31:0] rf_do;
  213. wire rf_rden;
  214. wire rf_empty;
  215. reg rf_overflow;
  216. reg rf_overflow_clr;
  217. reg rf_rden_arm;
  218. // External control
  219. reg [ 1:0] ectl_cs;
  220. reg ectl_req;
  221. wire ectl_grant;
  222. wire ectl_idle;
  223. // Main state machine
  224. localparam
  225. ST_IDLE = 0,
  226. ST_CMD_EXEC = 1,
  227. ST_MI_WR_DATA = 2,
  228. ST_MI_RD_DUMMY = 3,
  229. ST_MI_RD_DATA = 4,
  230. ST_FLUSH = 5,
  231. ST_PAUSE = 6;
  232. reg [2:0] state;
  233. reg [2:0] state_nxt;
  234. // Xfer counter
  235. reg [ 7:0] xfer_cnt;
  236. wire xfer_last;
  237. // Pause counter
  238. reg [ 3:0] pause_cnt;
  239. wire pause_last;
  240. // Memory interface
  241. wire [ 7:0] mi_spi_cmd;
  242. // Shift-Out
  243. localparam
  244. SO_MODE_SPI = 2'b00,
  245. SO_MODE_QPI_RD = 2'b01,
  246. SO_MODE_QPI_WR = 2'b10,
  247. SO_MODE_QPI_CMD = 2'b11;
  248. localparam
  249. SO_LD_SRC_WB = 2'b00,
  250. SO_LD_SRC_MI_DATA = 2'b10,
  251. SO_LD_SRC_MI_CMD = 2'b11;
  252. localparam
  253. SO_DST_NONE = 2'b00,
  254. SO_DST_WB = 2'b10,
  255. SO_DST_MI = 2'b11;
  256. wire so_ld_now;
  257. reg so_ld_valid;
  258. reg [ 1:0] so_ld_mode;
  259. reg [ 1:0] so_ld_dst;
  260. reg [ 5:0] so_ld_cnt;
  261. reg [ 1:0] so_ld_src;
  262. reg so_valid;
  263. reg [ 1:0] so_mode;
  264. reg [ 1:0] so_dst;
  265. reg [ 5:0] so_cnt;
  266. wire so_last;
  267. reg [31:0] so_data;
  268. // Shift-In
  269. wire si_mode_0;
  270. wire si_mode_nm1;
  271. reg [ 1:0] si_dst_1;
  272. wire [ 1:0] si_dst_n;
  273. reg [31:0] si_data_n;
  274. // Wishbone interface
  275. // ------------------
  276. // Ack
  277. always @(posedge clk)
  278. begin
  279. // Default is direct ack
  280. wb_ack <= wb_cyc & ~wb_ack;
  281. // Block on write to full command fifo
  282. if (wb_we & wb_addr[4] & cf_full)
  283. wb_ack <= 1'b0;
  284. // Block on read from empty response fifo if in blocking mode
  285. if (~wb_we & (wb_addr == 5'h3) & rf_empty)
  286. wb_ack <= 1'b0;
  287. end
  288. // CSR
  289. assign wbi_we_csr = wb_ack & wb_we & ~wb_addr[4];
  290. always @(posedge clk)
  291. if (rst)
  292. ectl_req <= 1'b0;
  293. else if (wbi_we_csr)
  294. ectl_req <= (ectl_req & ~wb_wdata[2]) | wb_wdata[1];
  295. always @(posedge clk)
  296. if (wbi_we_csr)
  297. ectl_cs <= wb_wdata[5:4];
  298. assign ectl_idle = (state == ST_IDLE);
  299. assign ectl_grant = (state == ST_CMD_EXEC);
  300. always @(posedge clk)
  301. rf_overflow_clr <= wbi_we_csr & wb_wdata[9];
  302. assign wbi_rd_csr = {
  303. 16'h0000,
  304. rf_empty, rf_full, rf_overflow, 1'b0,
  305. cf_empty, cf_full, 2'b0,
  306. 2'b00, ectl_cs,
  307. 1'b0, ectl_grant, ectl_req, ectl_idle
  308. };
  309. // Command FIFO write
  310. assign cf_di = { wb_addr[3:0], wb_wdata };
  311. always @(posedge clk)
  312. cf_wren <= wb_cyc & wb_we & ~wb_ack & wb_addr[4] & ~cf_full;
  313. // Response FIFO read
  314. always @(posedge clk)
  315. rf_rden_arm <= ~rf_empty & wb_addr[1] & ~wb_we;
  316. assign rf_rden = wb_ack & rf_rden_arm;
  317. // Read mux
  318. assign wbi_rd_rst = ~wb_cyc | wb_ack;
  319. always @(posedge clk)
  320. if (wbi_rd_rst)
  321. wb_rdata <= 32'h0000000;
  322. else
  323. wb_rdata <= wb_addr[1] ? rf_do : wbi_rd_csr;
  324. // FIFOs
  325. generate
  326. if (FIFO_DEPTH > 4) begin
  327. // Command
  328. fifo_sync_ram #(
  329. .DEPTH(FIFO_DEPTH),
  330. .WIDTH(36)
  331. ) cmd_fifo_I (
  332. .wr_data(cf_di),
  333. .wr_ena(cf_wren),
  334. .wr_full(cf_full),
  335. .rd_data(cf_do),
  336. .rd_ena(cf_rden),
  337. .rd_empty(cf_empty),
  338. .clk(clk),
  339. .rst(rst)
  340. );
  341. // Response
  342. fifo_sync_ram #(
  343. .DEPTH(FIFO_DEPTH),
  344. .WIDTH(32)
  345. ) rsp_fifo_I (
  346. .wr_data(rf_di),
  347. .wr_ena(rf_wren_safe),
  348. .wr_full(rf_full),
  349. .rd_data(rf_do),
  350. .rd_ena(rf_rden),
  351. .rd_empty(rf_empty),
  352. .clk(clk),
  353. .rst(rst)
  354. );
  355. end else begin
  356. // Command
  357. fifo_sync_shift #(
  358. .DEPTH(FIFO_DEPTH),
  359. .WIDTH(36)
  360. ) cmd_fifo_I (
  361. .wr_data(cf_di),
  362. .wr_ena(cf_wren),
  363. .wr_full(cf_full),
  364. .rd_data(cf_do),
  365. .rd_ena(cf_rden),
  366. .rd_empty(cf_empty),
  367. .clk(clk),
  368. .rst(rst)
  369. );
  370. // Response
  371. fifo_sync_shift #(
  372. .DEPTH(FIFO_DEPTH),
  373. .WIDTH(32)
  374. ) rsp_fifo_I (
  375. .wr_data(rf_di),
  376. .wr_ena(rf_wren_safe),
  377. .wr_full(rf_full),
  378. .rd_data(rf_do),
  379. .rd_ena(rf_rden),
  380. .rd_empty(rf_empty),
  381. .clk(clk),
  382. .rst(rst)
  383. );
  384. end
  385. endgenerate
  386. // Response overflow tracking
  387. assign rf_wren_safe = rf_wren & ~rf_full;
  388. always @(posedge clk)
  389. rf_overflow <= (rf_overflow & ~rf_overflow_clr) | (rf_wren & rf_full);
  390. // Capture responses
  391. assign rf_di = si_data_n;
  392. assign rf_wren = (si_dst_n == 2'b01);
  393. // Main Control
  394. // ------------
  395. // State register
  396. always @(posedge clk)
  397. if (rst)
  398. state <= ST_IDLE;
  399. else
  400. state <= state_nxt;
  401. // Next-State logic
  402. always @(*)
  403. begin
  404. // Default
  405. state_nxt = state;
  406. // Transitions ?
  407. case (state)
  408. ST_IDLE:
  409. if (mi_valid)
  410. state_nxt = mi_rw ? ST_MI_RD_DUMMY : ST_MI_WR_DATA;
  411. else if (ectl_req)
  412. state_nxt = ST_CMD_EXEC;
  413. ST_CMD_EXEC:
  414. if (~ectl_req & cf_empty)
  415. state_nxt = ST_PAUSE;
  416. ST_MI_WR_DATA:
  417. if (xfer_last & so_ld_now)
  418. state_nxt = ST_FLUSH;
  419. ST_MI_RD_DUMMY:
  420. if (so_ld_now)
  421. state_nxt = ST_MI_RD_DATA;
  422. ST_MI_RD_DATA:
  423. if (xfer_last & so_ld_now)
  424. state_nxt = ST_FLUSH;
  425. ST_FLUSH:
  426. if (~so_valid)
  427. state_nxt = ST_PAUSE;
  428. ST_PAUSE:
  429. if (pause_last)
  430. state_nxt = ST_IDLE;
  431. endcase
  432. end
  433. // Xfer counter
  434. always @(posedge clk)
  435. if (state == ST_IDLE)
  436. xfer_cnt <= { 1'b0, mi_len } - 1;
  437. else if (((state == ST_MI_WR_DATA) || (state == ST_MI_RD_DATA)) && so_ld_now)
  438. xfer_cnt <= xfer_cnt - 1;
  439. assign xfer_last = xfer_cnt[7];
  440. // Pause counter
  441. always @(posedge clk)
  442. if (state == ST_PAUSE)
  443. pause_cnt <= pause_cnt - 1;
  444. else
  445. pause_cnt <= PAUSE_CLK - 2;
  446. assign pause_last = pause_cnt[3];
  447. // SPI command
  448. assign mi_spi_cmd = mi_rw ? CMD_READ[8*mi_addr_cs+:8] : CMD_WRITE[8*mi_addr_cs+:8];
  449. // ROM for command fifo counter
  450. (* mem2reg *)
  451. reg [5:0] cmd_len_rom[0:15];
  452. initial
  453. begin : rom_cmd_len
  454. integer i;
  455. for (i=0; i<16; i=i+1)
  456. cmd_len_rom[i] = (((i >> 2) & 3) == 0) ?
  457. (((i & 3) << 3) - PHY_SPEED + 7) :
  458. (((i & 3) << 1) - PHY_SPEED + 1);
  459. end
  460. // Shift control
  461. // When to load
  462. assign so_ld_now = ~so_valid | so_last;
  463. // What to load
  464. always @(*)
  465. begin
  466. // Defaults
  467. so_ld_valid = 1'b0;
  468. so_ld_mode = 2'bxx;
  469. so_ld_dst = 2'bxx;
  470. so_ld_cnt = 6'bxxxxxx;
  471. so_ld_src = 2'bxx;
  472. case (state)
  473. ST_IDLE: begin
  474. so_ld_valid = mi_valid;
  475. so_ld_mode = SO_MODE_QPI_CMD;
  476. so_ld_dst = SO_DST_NONE;
  477. so_ld_cnt = (32 / 4) - PHY_SPEED - 1;
  478. so_ld_src = SO_LD_SRC_MI_CMD;
  479. end
  480. ST_CMD_EXEC: begin
  481. so_ld_valid = ~cf_empty;
  482. case (cf_do[35:34])
  483. 2'b00: { so_ld_mode, so_ld_dst } = { SO_MODE_SPI, SO_DST_WB };
  484. 2'b01: { so_ld_mode, so_ld_dst } = { SO_MODE_QPI_RD, SO_DST_WB };
  485. 2'b10: { so_ld_mode, so_ld_dst } = { SO_MODE_QPI_WR, SO_DST_NONE };
  486. 2'b11: { so_ld_mode, so_ld_dst } = { SO_MODE_QPI_CMD, SO_DST_NONE };
  487. endcase
  488. so_ld_cnt = cmd_len_rom[cf_do[35:32]];
  489. so_ld_src = SO_LD_SRC_WB;
  490. end
  491. ST_MI_WR_DATA: begin
  492. so_ld_valid = 1'b1;
  493. so_ld_mode = SO_MODE_QPI_WR;
  494. so_ld_dst = SO_DST_NONE;
  495. so_ld_cnt = (32 / (4 * PHY_WIDTH)) - PHY_SPEED - 1;
  496. so_ld_src = SO_LD_SRC_MI_DATA;
  497. end
  498. ST_MI_RD_DUMMY: begin
  499. so_ld_valid = 1'b1;
  500. so_ld_mode = SO_MODE_QPI_RD;
  501. so_ld_dst = SO_DST_NONE;
  502. so_ld_cnt = DUMMY_CLK - PHY_SPEED - 1;
  503. end
  504. ST_MI_RD_DATA: begin
  505. so_ld_valid = 1'b1;
  506. so_ld_mode = SO_MODE_QPI_RD;
  507. so_ld_dst = SO_DST_MI;
  508. so_ld_cnt = (32 / (4 * PHY_WIDTH)) - PHY_SPEED - 1;
  509. end
  510. endcase
  511. end
  512. // Command interface
  513. assign cf_rden = (state == ST_CMD_EXEC) & so_ld_now & ~cf_empty;
  514. // Memory interface
  515. assign mi_ready = (state == ST_IDLE);
  516. assign mi_wack = (state == ST_MI_WR_DATA) & so_ld_now;
  517. assign mi_wlast = xfer_last;
  518. assign mi_rdata = si_data_n;
  519. assign mi_rstb = si_dst_n[1];
  520. assign mi_rlast = si_dst_n[0];
  521. // Chip select
  522. always @(posedge clk)
  523. if (rst)
  524. phy_cs_o <= { N_CS{1'b1} };
  525. else begin
  526. case (state)
  527. ST_IDLE: begin
  528. // Default
  529. phy_cs_o <= { N_CS{1'b1} };
  530. if (mi_valid)
  531. phy_cs_o[mi_addr_cs] <= 1'b0;
  532. else if (ectl_req)
  533. phy_cs_o[ectl_cs] <= 1'b0;
  534. end
  535. ST_FLUSH:
  536. if (~so_valid)
  537. phy_cs_o <= { N_CS{1'b1} };
  538. ST_PAUSE:
  539. phy_cs_o <= { N_CS{1'b1} };
  540. endcase
  541. end
  542. // Shift-Out unit
  543. // --------------
  544. // Shift Output
  545. // SPI mode : Each chan shifts PHY_SPEED Output only defined for MOSI
  546. // QPI read : n/a n/a
  547. // QPI data mode : Each chan shifts 4 * PHY_SPEED Output QPI mode
  548. // QPI command mode : Word shifts 4 * PHY_SPEED chan[1] replicates chan[0]
  549. // Validity
  550. always @(posedge clk)
  551. if (rst)
  552. so_valid <= 1'b0;
  553. else
  554. so_valid <= (so_valid & ~so_last) | (so_ld_now & so_ld_valid);
  555. // Mode / Read-destination
  556. always @(posedge clk)
  557. if (so_ld_now) begin
  558. so_mode <= so_ld_mode;
  559. so_dst <= so_ld_dst;
  560. end
  561. // Counter
  562. always @(posedge clk)
  563. if (so_ld_now)
  564. so_cnt <= so_ld_cnt;
  565. else
  566. so_cnt <= so_cnt - PHY_SPEED;
  567. assign so_last = so_cnt[5];
  568. // Shift register
  569. always @(posedge clk)
  570. begin
  571. casez ({so_ld_now, so_ld_src, so_mode})
  572. { 1'b0, 2'bzz, SO_MODE_SPI }: so_data <= shift_spi(so_data);
  573. { 1'b0, 2'bzz, SO_MODE_QPI_WR }: so_data <= shift_qpi_data(so_data);
  574. { 1'b0, 2'bzz, SO_MODE_QPI_CMD }: so_data <= shift_qpi_cmd(so_data);
  575. { 1'b1, SO_LD_SRC_WB, 2'bzz }: so_data <= cf_do[31:0];
  576. { 1'b1, SO_LD_SRC_MI_DATA, 2'bzz }: so_data <= mi_wdata;
  577. { 1'b1, SO_LD_SRC_MI_CMD, 2'bzz }: so_data <= { mi_spi_cmd, mi_addr };
  578. default: so_data <= 32'hxxxxxxxx;
  579. endcase
  580. end
  581. // IO control
  582. always @(*)
  583. begin : io_ctrl
  584. integer chan, i;
  585. // Control
  586. if (so_valid) begin
  587. // Clock
  588. if (PHY_SPEED > 1)
  589. for (i=0; i<PSW; i=i+1)
  590. phy_clk_o[i] = ~so_last | (i >= (PHY_SPEED-1-so_cnt[$clog2(PHY_SPEED)-1:0]));
  591. else
  592. phy_clk_o <= 1'b1;
  593. // Output Enable
  594. case (so_mode)
  595. SO_MODE_SPI: phy_io_oe = 4'b0001;
  596. SO_MODE_QPI_RD: phy_io_oe = 4'b0000;
  597. SO_MODE_QPI_WR: phy_io_oe = 4'b1111;
  598. SO_MODE_QPI_CMD: phy_io_oe = 4'b1111;
  599. default: phy_io_oe = 4'bxxxx;
  600. endcase
  601. end else begin
  602. // Disable all
  603. phy_clk_o <= {PSW{1'b0}};
  604. phy_io_oe <= 4'b0000;
  605. end
  606. // Data
  607. if (so_mode[0])
  608. phy_io_o = shift2phy_qpi_cmd(so_data);
  609. else
  610. phy_io_o = shift2phy_qpi_data(so_data);
  611. if (~so_mode[1])
  612. phy_io_o = shift2phy_spi(so_data, phy_io_o);
  613. end
  614. // Shift-In unit
  615. // -------------
  616. // Capture control
  617. assign si_mode_0 = so_mode[0];
  618. always @(posedge clk)
  619. begin
  620. // Default destination is 'none'
  621. si_dst_1 <= 2'b00;
  622. // If it's a read, send it somewhere
  623. if (so_valid & so_last & ~so_mode[1] & so_dst[1])
  624. si_dst_1 <= so_dst[0] ? { 1'b1, (state == ST_FLUSH) } : 2'b01;
  625. end
  626. // Delay for PHY pipeline
  627. delay_bit #(PHY_DELAY) dly_si_mode (si_mode_0, si_mode_nm1, clk);
  628. delay_bus #(PHY_DELAY, 2) dly_si_dst (si_dst_1, si_dst_n, clk);
  629. // Shifter
  630. always @(posedge clk)
  631. begin
  632. // 2 modes:
  633. // 0 - SPI shift-in PHY_SPEED bits at a time per channel
  634. // 1 - QPI shift-in 4 * PHY_SPEED bits at a time per channel
  635. if (si_mode_nm1)
  636. si_data_n <= phy2shift_qpi(si_data_n, phy_io_i);
  637. else
  638. si_data_n <= phy2shift_spi(si_data_n, phy_io_i);
  639. end
  640. endmodule