spi_flash_ctrl.vhdl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. library work;
  5. use work.wishbone_types.all;
  6. entity spi_flash_ctrl is
  7. generic (
  8. -- Default config for auto-mode
  9. DEF_CLK_DIV : natural := 2; -- Clock divider SCK = CLK/((CLK_DIV+1)*2)
  10. DEF_QUAD_READ : boolean := false; -- Use quad read with 8 clk dummy
  11. -- Dummy clocks after boot
  12. BOOT_CLOCKS : boolean := true; -- Send 8 dummy clocks after boot
  13. -- Number of data lines (1=MISO/MOSI, otherwise 2 or 4)
  14. DATA_LINES : positive := 1
  15. );
  16. port (
  17. clk : in std_ulogic;
  18. rst : in std_ulogic;
  19. -- Wishbone ports:
  20. wb_in : in wb_io_master_out;
  21. wb_out : out wb_io_slave_out;
  22. -- Wishbone extra selects
  23. wb_sel_reg : in std_ulogic;
  24. wb_sel_map : in std_ulogic;
  25. -- SPI port
  26. sck : out std_ulogic;
  27. cs_n : out std_ulogic;
  28. sdat_o : out std_ulogic_vector(DATA_LINES-1 downto 0);
  29. sdat_oe : out std_ulogic_vector(DATA_LINES-1 downto 0);
  30. sdat_i : in std_ulogic_vector(DATA_LINES-1 downto 0)
  31. );
  32. end entity spi_flash_ctrl;
  33. architecture rtl of spi_flash_ctrl is
  34. -- Register indices
  35. constant SPI_REG_BITS : positive := 3;
  36. -- Register addresses (matches wishbone addr downto 2, ie, 4 bytes per reg)
  37. constant SPI_REG_DATA : std_ulogic_vector(SPI_REG_BITS-1 downto 0) := "000";
  38. constant SPI_REG_CTRL : std_ulogic_vector(SPI_REG_BITS-1 downto 0) := "001";
  39. constant SPI_REG_AUTO_CFG : std_ulogic_vector(SPI_REG_BITS-1 downto 0) := "010";
  40. constant SPI_REG_INVALID : std_ulogic_vector(SPI_REG_BITS-1 downto 0) := "111";
  41. -- Control register
  42. signal ctrl_reg : std_ulogic_vector(15 downto 0) := (others => '0');
  43. alias ctrl_reset : std_ulogic is ctrl_reg(0);
  44. alias ctrl_cs : std_ulogic is ctrl_reg(1);
  45. alias ctrl_rsrv1 : std_ulogic is ctrl_reg(2);
  46. alias ctrl_rsrv2 : std_ulogic is ctrl_reg(3);
  47. alias ctrl_div : std_ulogic_vector(7 downto 0) is ctrl_reg(15 downto 8);
  48. -- Auto mode config register
  49. signal auto_cfg_reg : std_ulogic_vector(29 downto 0) := (others => '0');
  50. alias auto_cfg_cmd : std_ulogic_vector(7 downto 0) is auto_cfg_reg(7 downto 0);
  51. alias auto_cfg_dummies : std_ulogic_vector(2 downto 0) is auto_cfg_reg(10 downto 8);
  52. alias auto_cfg_mode : std_ulogic_vector(1 downto 0) is auto_cfg_reg(12 downto 11);
  53. alias auto_cfg_addr4 : std_ulogic is auto_cfg_reg(13);
  54. alias auto_cfg_rsrv1 : std_ulogic is auto_cfg_reg(14);
  55. alias auto_cfg_rsrv2 : std_ulogic is auto_cfg_reg(15);
  56. alias auto_cfg_div : std_ulogic_vector(7 downto 0) is auto_cfg_reg(23 downto 16);
  57. alias auto_cfg_cstout : std_ulogic_vector(5 downto 0) is auto_cfg_reg(29 downto 24);
  58. -- Constants below match top 2 bits of rxtx "mode"
  59. constant SPI_AUTO_CFG_MODE_SINGLE : std_ulogic_vector(1 downto 0) := "00";
  60. constant SPI_AUTO_CFG_MODE_DUAL : std_ulogic_vector(1 downto 0) := "10";
  61. constant SPI_AUTO_CFG_MODE_QUAD : std_ulogic_vector(1 downto 0) := "11";
  62. -- Signals to rxtx
  63. signal cmd_valid : std_ulogic;
  64. signal cmd_clk_div : natural range 0 to 255;
  65. signal cmd_mode : std_ulogic_vector(2 downto 0);
  66. signal cmd_ready : std_ulogic;
  67. signal d_clks : std_ulogic_vector(2 downto 0);
  68. signal d_rx : std_ulogic_vector(7 downto 0);
  69. signal d_tx : std_ulogic_vector(7 downto 0);
  70. signal d_ack : std_ulogic;
  71. signal bus_idle : std_ulogic;
  72. -- Latch to track that we have a pending read
  73. signal pending_read : std_ulogic;
  74. -- Wishbone latches
  75. signal wb_req : wb_io_master_out;
  76. signal wb_stash : wb_io_master_out;
  77. signal wb_rsp : wb_io_slave_out;
  78. -- Wishbone decode
  79. signal wb_valid : std_ulogic;
  80. signal wb_reg_valid : std_ulogic;
  81. signal wb_reg_dat_v : std_ulogic;
  82. signal wb_map_valid : std_ulogic;
  83. signal wb_reg : std_ulogic_vector(SPI_REG_BITS-1 downto 0);
  84. -- Auto mode clock counts XXX FIXME: Look at reasonable values based
  85. -- on system clock maybe ? Or make them programmable.
  86. constant CS_DELAY_ASSERT : integer := 1; -- CS low to cmd
  87. constant CS_DELAY_RECOVERY : integer := 10; -- CS high to CS low
  88. constant DEFAULT_CS_TIMEOUT : integer := 32;
  89. -- Automatic mode state
  90. type auto_state_t is (AUTO_BOOT, AUTO_IDLE, AUTO_CS_ON, AUTO_CMD,
  91. AUTO_ADR0, AUTO_ADR1, AUTO_ADR2, AUTO_ADR3,
  92. AUTO_DUMMY,
  93. AUTO_DAT0, AUTO_DAT1, AUTO_DAT2, AUTO_DAT3,
  94. AUTO_DAT0_DATA, AUTO_DAT1_DATA, AUTO_DAT2_DATA, AUTO_DAT3_DATA,
  95. AUTO_SEND_ACK, AUTO_WAIT_REQ, AUTO_RECOVERY);
  96. -- Automatic mode signals
  97. signal auto_cs : std_ulogic;
  98. signal auto_cmd_valid : std_ulogic;
  99. signal auto_cmd_mode : std_ulogic_vector(2 downto 0);
  100. signal auto_d_txd : std_ulogic_vector(7 downto 0);
  101. signal auto_d_clks : std_ulogic_vector(2 downto 0);
  102. signal auto_data_next : std_ulogic_vector(wb_out.dat'left downto 0);
  103. signal auto_cnt_next : integer range 0 to 63;
  104. signal auto_ack : std_ulogic;
  105. signal auto_next : auto_state_t;
  106. signal auto_lad_next : std_ulogic_vector(31 downto 0);
  107. signal auto_latch_adr : std_ulogic;
  108. -- Automatic mode latches
  109. signal auto_data : std_ulogic_vector(wb_out.dat'left downto 0) := (others => '0');
  110. signal auto_cnt : integer range 0 to 63 := 0;
  111. signal auto_state : auto_state_t := AUTO_BOOT;
  112. signal auto_last_addr : std_ulogic_vector(31 downto 0);
  113. begin
  114. -- Instanciate low level shifter
  115. spi_rxtx: entity work.spi_rxtx
  116. generic map (
  117. DATA_LINES => DATA_LINES
  118. )
  119. port map(
  120. rst => rst,
  121. clk => clk,
  122. clk_div_i => cmd_clk_div,
  123. cmd_valid_i => cmd_valid,
  124. cmd_ready_o => cmd_ready,
  125. cmd_mode_i => cmd_mode,
  126. cmd_clks_i => d_clks,
  127. cmd_txd_i => d_tx,
  128. d_rxd_o => d_rx,
  129. d_ack_o => d_ack,
  130. bus_idle_o => bus_idle,
  131. sck => sck,
  132. sdat_o => sdat_o,
  133. sdat_oe => sdat_oe,
  134. sdat_i => sdat_i
  135. );
  136. -- Valid wb command
  137. wb_valid <= wb_req.stb and wb_req.cyc;
  138. wb_reg_valid <= wb_valid and wb_sel_reg;
  139. wb_map_valid <= wb_valid and wb_sel_map;
  140. -- Register decode. For map accesses, make it look like "invalid"
  141. wb_reg <= wb_req.adr(SPI_REG_BITS+1 downto 2) when wb_reg_valid else SPI_REG_INVALID;
  142. -- Shortcut because we test that a lot: data register access
  143. wb_reg_dat_v <= '1' when wb_reg = SPI_REG_DATA else '0';
  144. -- Wishbone request -> SPI request
  145. wb_request_sync: process(clk)
  146. begin
  147. if rising_edge(clk) then
  148. -- We need to latch whether a read is in progress to block
  149. -- a subsequent store, otherwise the acks will collide.
  150. --
  151. -- We are heavy handed and force a wait for an idle bus if
  152. -- a store is behind a load. Shouldn't happen with flashes
  153. -- in practice.
  154. --
  155. if cmd_valid = '1' and cmd_ready = '1' then
  156. pending_read <= not wb_req.we;
  157. elsif bus_idle = '1' then
  158. pending_read <= '0';
  159. end if;
  160. end if;
  161. end process;
  162. wb_request_comb: process(all)
  163. begin
  164. if ctrl_cs = '1' then
  165. -- Data register access (see wb_request_sync)
  166. cmd_valid <= wb_reg_dat_v and not (pending_read and wb_req.we);
  167. -- Clock divider from control reg
  168. cmd_clk_div <= to_integer(unsigned(ctrl_div));
  169. -- Mode based on sel
  170. if wb_req.sel = "0010" then
  171. -- dual mode
  172. cmd_mode <= "10" & wb_req.we;
  173. d_clks <= "011";
  174. elsif wb_req.sel = "0100" then
  175. -- quad mode
  176. cmd_mode <= "11" & wb_req.we;
  177. d_clks <= "001";
  178. else
  179. -- single bit
  180. cmd_mode <= "01" & wb_req.we;
  181. d_clks <= "111";
  182. end if;
  183. d_tx <= wb_req.dat(7 downto 0);
  184. cs_n <= not ctrl_cs;
  185. else
  186. cmd_valid <= auto_cmd_valid;
  187. cmd_mode <= auto_cmd_mode;
  188. cmd_clk_div <= to_integer(unsigned(auto_cfg_div));
  189. d_tx <= auto_d_txd;
  190. d_clks <= auto_d_clks;
  191. cs_n <= not auto_cs;
  192. end if;
  193. end process;
  194. -- Generate wishbone responses
  195. --
  196. -- Note: wb_out and wb_in should only appear in this synchronous process
  197. --
  198. -- Everything else should work on wb_req and wb_rsp
  199. wb_response_sync: process(clk)
  200. begin
  201. if rising_edge(clk) then
  202. if rst = '1' then
  203. wb_out.ack <= '0';
  204. wb_out.stall <= '0';
  205. wb_stash.cyc <= '0';
  206. wb_stash.stb <= '0';
  207. wb_stash.sel <= (others => '0');
  208. wb_stash.we <= '0';
  209. else
  210. -- Latch wb responses as well for 1 cycle. Stall is updated
  211. -- below
  212. wb_out <= wb_rsp;
  213. -- Implement a stash buffer. If we are stalled and stash is
  214. -- free, fill it up. This will generate a WB stall on the
  215. -- next cycle.
  216. if wb_rsp.stall = '1' and wb_out.stall = '0' and
  217. wb_in.cyc = '1' and wb_in.stb = '1' then
  218. wb_stash <= wb_in;
  219. wb_out.stall <= '1';
  220. end if;
  221. -- We aren't stalled, see what we can do
  222. if wb_rsp.stall = '0' then
  223. if wb_out.stall = '1' then
  224. -- Something in stash ! use it and clear stash
  225. wb_req <= wb_stash;
  226. wb_out.stall <= '0';
  227. else
  228. -- Nothing in stash, grab request from WB
  229. if wb_in.cyc = '1' then
  230. wb_req <= wb_in;
  231. else
  232. wb_req.cyc <= wb_in.cyc;
  233. wb_req.stb <= wb_in.stb;
  234. end if;
  235. end if;
  236. end if;
  237. end if;
  238. end if;
  239. end process;
  240. wb_response_comb: process(all)
  241. begin
  242. -- Defaults
  243. wb_rsp.ack <= '0';
  244. wb_rsp.dat <= x"00" & d_rx & d_rx & d_rx;
  245. wb_rsp.stall <= '0';
  246. -- Depending on the access type...
  247. if wb_map_valid = '1' then
  248. -- Memory map access
  249. wb_rsp.stall <= not auto_ack; -- XXX FIXME: Allow pipelining
  250. wb_rsp.ack <= auto_ack;
  251. wb_rsp.dat <= auto_data;
  252. elsif ctrl_cs = '1' and wb_reg = SPI_REG_DATA then
  253. -- Data register in manual mode
  254. --
  255. -- Stall stores if there's a pending read to avoid
  256. -- acks colliding. Otherwise accept all accesses
  257. -- immediately if rxtx is ready.
  258. --
  259. -- Note: This must match the logic setting cmd_valid
  260. -- in wb_request_comb.
  261. --
  262. -- We also ack stores immediately when accepted. Loads
  263. -- are handled separately further down.
  264. --
  265. if wb_req.we = '1' and pending_read = '1' then
  266. wb_rsp.stall <= '1';
  267. else
  268. wb_rsp.ack <= wb_req.we and cmd_ready;
  269. wb_rsp.stall <= not cmd_ready;
  270. end if;
  271. -- Note: loads acks are handled elsewhere
  272. elsif wb_reg_valid = '1' then
  273. -- Normal register access
  274. --
  275. -- Normally single cycle but ensure any auto-mode or manual
  276. -- operation is complete first
  277. --
  278. if auto_state = AUTO_IDLE and bus_idle = '1' then
  279. wb_rsp.ack <= '1';
  280. wb_rsp.stall <= '0';
  281. case wb_reg is
  282. when SPI_REG_CTRL =>
  283. wb_rsp.dat <= (ctrl_reg'range => ctrl_reg, others => '0');
  284. when SPI_REG_AUTO_CFG =>
  285. wb_rsp.dat <= (auto_cfg_reg'range => auto_cfg_reg, others => '0');
  286. when others => null;
  287. end case;
  288. else
  289. wb_rsp.stall <= '1';
  290. end if;
  291. end if;
  292. -- For loads in manual mode, we've accepted the command early
  293. -- so none of the above connditions might be true. We thus need
  294. -- to send the ack whenever we are getting it from rxtx.
  295. --
  296. -- This shouldn't collide with any of the above acks because we hold
  297. -- normal register accesses and stores when there is a pending
  298. -- load or the bus is busy.
  299. --
  300. if ctrl_cs = '1' and d_ack = '1' then
  301. assert pending_read = '1' report "d_ack without pending read !" severity failure;
  302. wb_rsp.ack <= '1';
  303. end if;
  304. end process;
  305. -- Automatic mode state machine
  306. auto_sync: process(clk)
  307. begin
  308. if rising_edge(clk) then
  309. if rst = '1' then
  310. auto_last_addr <= (others => '0');
  311. auto_state <= AUTO_BOOT;
  312. else
  313. auto_state <= auto_next;
  314. auto_cnt <= auto_cnt_next;
  315. auto_data <= auto_data_next;
  316. if auto_latch_adr = '1' then
  317. auto_last_addr <= auto_lad_next;
  318. end if;
  319. end if;
  320. end if;
  321. end process;
  322. auto_comb: process(all)
  323. variable addr : std_ulogic_vector(31 downto 0);
  324. variable req_is_next : boolean;
  325. function mode_to_clks(mode: std_ulogic_vector(1 downto 0)) return std_ulogic_vector is
  326. begin
  327. if mode = SPI_AUTO_CFG_MODE_QUAD then
  328. return "001";
  329. elsif mode = SPI_AUTO_CFG_MODE_DUAL then
  330. return "011";
  331. else
  332. return "111";
  333. end if;
  334. end function;
  335. begin
  336. -- Default outputs
  337. auto_ack <= '0';
  338. auto_cs <= '0';
  339. auto_cmd_valid <= '0';
  340. auto_d_txd <= x"00";
  341. auto_cmd_mode <= "001";
  342. auto_d_clks <= "111";
  343. auto_latch_adr <= '0';
  344. -- Default next state
  345. auto_next <= auto_state;
  346. auto_cnt_next <= auto_cnt;
  347. auto_data_next <= auto_data;
  348. -- Convert wishbone address into a flash address. We mask
  349. -- off the 4 top address bits to get rid of the "f" there.
  350. addr := "00" & wb_req.adr(29 downto 2) & "00";
  351. -- Calculate the next address for store & compare later
  352. auto_lad_next <= std_ulogic_vector(unsigned(addr) + 4);
  353. -- Match incoming request address with next address
  354. req_is_next := addr = auto_last_addr;
  355. -- XXX TODO:
  356. -- - Support < 32-bit accesses
  357. -- Reset
  358. if rst = '1' or ctrl_reset = '1' then
  359. auto_cs <= '0';
  360. auto_cnt_next <= 0;
  361. auto_next <= AUTO_BOOT;
  362. else
  363. -- Run counter
  364. if auto_cnt /= 0 then
  365. auto_cnt_next <= auto_cnt - 1;
  366. end if;
  367. -- Automatic CS is set whenever state isn't IDLE or RECOVERY or BOOT
  368. if auto_state /= AUTO_IDLE and
  369. auto_state /= AUTO_RECOVERY and
  370. auto_state /= AUTO_BOOT then
  371. auto_cs <= '1';
  372. end if;
  373. -- State machine
  374. case auto_state is
  375. when AUTO_BOOT =>
  376. if BOOT_CLOCKS then
  377. auto_cmd_valid <= '1';
  378. if cmd_ready = '1' then
  379. auto_next <= AUTO_IDLE;
  380. end if;
  381. else
  382. auto_next <= AUTO_IDLE;
  383. end if;
  384. when AUTO_IDLE =>
  385. -- Access to the memory map only when manual CS isn't set
  386. if wb_map_valid = '1' and ctrl_cs = '0' then
  387. -- Ignore writes, we don't support them yet
  388. if wb_req.we = '1' then
  389. auto_ack <= '1';
  390. else
  391. -- Start machine with CS assertion delay
  392. auto_next <= AUTO_CS_ON;
  393. auto_cnt_next <= CS_DELAY_ASSERT;
  394. end if;
  395. end if;
  396. when AUTO_CS_ON =>
  397. if auto_cnt = 0 then
  398. -- CS asserted long enough, send command
  399. auto_next <= AUTO_CMD;
  400. end if;
  401. when AUTO_CMD =>
  402. auto_d_txd <= auto_cfg_cmd;
  403. auto_cmd_valid <= '1';
  404. if cmd_ready = '1' then
  405. if auto_cfg_addr4 = '1' then
  406. auto_next <= AUTO_ADR3;
  407. else
  408. auto_next <= AUTO_ADR2;
  409. end if;
  410. end if;
  411. when AUTO_ADR3 =>
  412. auto_d_txd <= addr(31 downto 24);
  413. auto_cmd_valid <= '1';
  414. if cmd_ready = '1' then
  415. auto_next <= AUTO_ADR2;
  416. end if;
  417. when AUTO_ADR2 =>
  418. auto_d_txd <= addr(23 downto 16);
  419. auto_cmd_valid <= '1';
  420. if cmd_ready = '1' then
  421. auto_next <= AUTO_ADR1;
  422. end if;
  423. when AUTO_ADR1 =>
  424. auto_d_txd <= addr(15 downto 8);
  425. auto_cmd_valid <= '1';
  426. if cmd_ready = '1' then
  427. auto_next <= AUTO_ADR0;
  428. end if;
  429. when AUTO_ADR0 =>
  430. auto_d_txd <= addr(7 downto 0);
  431. auto_cmd_valid <= '1';
  432. if cmd_ready = '1' then
  433. if auto_cfg_dummies = "000" then
  434. auto_next <= AUTO_DAT0;
  435. else
  436. auto_next <= AUTO_DUMMY;
  437. end if;
  438. end if;
  439. when AUTO_DUMMY =>
  440. auto_cmd_valid <= '1';
  441. auto_d_clks <= auto_cfg_dummies;
  442. if cmd_ready = '1' then
  443. auto_next <= AUTO_DAT0;
  444. end if;
  445. when AUTO_DAT0 =>
  446. auto_cmd_valid <= '1';
  447. auto_cmd_mode <= auto_cfg_mode & "0";
  448. auto_d_clks <= mode_to_clks(auto_cfg_mode);
  449. if cmd_ready = '1' then
  450. auto_next <= AUTO_DAT0_DATA;
  451. end if;
  452. when AUTO_DAT0_DATA =>
  453. if d_ack = '1' then
  454. auto_data_next(7 downto 0) <= d_rx;
  455. auto_next <= AUTO_DAT1;
  456. end if;
  457. when AUTO_DAT1 =>
  458. auto_cmd_valid <= '1';
  459. auto_cmd_mode <= auto_cfg_mode & "0";
  460. auto_d_clks <= mode_to_clks(auto_cfg_mode);
  461. if cmd_ready = '1' then
  462. auto_next <= AUTO_DAT1_DATA;
  463. end if;
  464. when AUTO_DAT1_DATA =>
  465. if d_ack = '1' then
  466. auto_data_next(15 downto 8) <= d_rx;
  467. auto_next <= AUTO_DAT2;
  468. end if;
  469. when AUTO_DAT2 =>
  470. auto_cmd_valid <= '1';
  471. auto_cmd_mode <= auto_cfg_mode & "0";
  472. auto_d_clks <= mode_to_clks(auto_cfg_mode);
  473. if cmd_ready = '1' then
  474. auto_next <= AUTO_DAT2_DATA;
  475. end if;
  476. when AUTO_DAT2_DATA =>
  477. if d_ack = '1' then
  478. auto_data_next(23 downto 16) <= d_rx;
  479. auto_next <= AUTO_DAT3;
  480. end if;
  481. when AUTO_DAT3 =>
  482. auto_cmd_valid <= '1';
  483. auto_cmd_mode <= auto_cfg_mode & "0";
  484. auto_d_clks <= mode_to_clks(auto_cfg_mode);
  485. if cmd_ready = '1' then
  486. auto_next <= AUTO_DAT3_DATA;
  487. end if;
  488. when AUTO_DAT3_DATA =>
  489. if d_ack = '1' then
  490. auto_data_next(31 downto 24) <= d_rx;
  491. auto_next <= AUTO_SEND_ACK;
  492. auto_latch_adr <= '1';
  493. end if;
  494. when AUTO_SEND_ACK =>
  495. auto_ack <= '1';
  496. auto_cnt_next <= to_integer(unsigned(auto_cfg_cstout));
  497. auto_next <= AUTO_WAIT_REQ;
  498. when AUTO_WAIT_REQ =>
  499. -- Incoming bus request we can take ? Otherwise do we need
  500. -- to cancel the wait ?
  501. if wb_map_valid = '1' and req_is_next and wb_req.we = '0' then
  502. auto_next <= AUTO_DAT0;
  503. elsif wb_map_valid = '1' or wb_reg_valid = '1' or auto_cnt = 0 then
  504. -- This means we can drop the CS right on the next clock.
  505. -- We make the assumption here that the two cycles min
  506. -- spent in AUTO_SEND_ACK and AUTO_WAIT_REQ are long enough
  507. -- to deassert CS. If that doesn't hold true in the future,
  508. -- add another state.
  509. auto_cnt_next <= CS_DELAY_RECOVERY;
  510. auto_next <= AUTO_RECOVERY;
  511. end if;
  512. when AUTO_RECOVERY =>
  513. if auto_cnt = 0 then
  514. auto_next <= AUTO_IDLE;
  515. end if;
  516. end case;
  517. end if;
  518. end process;
  519. -- Register write sync machine
  520. reg_write: process(clk)
  521. function reg_wr(r : in std_ulogic_vector;
  522. w : in wb_io_master_out) return std_ulogic_vector is
  523. variable b : natural range 0 to 31;
  524. variable t : std_ulogic_vector(r'range);
  525. begin
  526. t := r;
  527. for i in r'range loop
  528. if w.sel(i/8) = '1' then
  529. t(i) := w.dat(i);
  530. end if;
  531. end loop;
  532. return t;
  533. end function;
  534. begin
  535. if rising_edge(clk) then
  536. -- Reset auto-clear
  537. if rst = '1' or ctrl_reset = '1' then
  538. ctrl_reset <= '0';
  539. ctrl_cs <= '0';
  540. ctrl_rsrv1 <= '0';
  541. ctrl_rsrv2 <= '0';
  542. ctrl_div <= std_ulogic_vector(to_unsigned(DEF_CLK_DIV, 8));
  543. if DEF_QUAD_READ then
  544. auto_cfg_cmd <= x"6b";
  545. auto_cfg_dummies <= "111";
  546. auto_cfg_mode <= SPI_AUTO_CFG_MODE_QUAD;
  547. else
  548. auto_cfg_cmd <= x"03";
  549. auto_cfg_dummies <= "000";
  550. auto_cfg_mode <= SPI_AUTO_CFG_MODE_SINGLE;
  551. end if;
  552. auto_cfg_addr4 <= '0';
  553. auto_cfg_rsrv1 <= '0';
  554. auto_cfg_rsrv2 <= '0';
  555. auto_cfg_div <= std_ulogic_vector(to_unsigned(DEF_CLK_DIV, 8));
  556. auto_cfg_cstout <= std_ulogic_vector(to_unsigned(DEFAULT_CS_TIMEOUT, 6));
  557. end if;
  558. if wb_reg_valid = '1' and wb_req.we = '1' and auto_state = AUTO_IDLE and bus_idle = '1' then
  559. if wb_reg = SPI_REG_CTRL then
  560. ctrl_reg <= reg_wr(ctrl_reg, wb_req);
  561. end if;
  562. if wb_reg = SPI_REG_AUTO_CFG then
  563. auto_cfg_reg <= reg_wr(auto_cfg_reg, wb_req);
  564. end if;
  565. end if;
  566. end if;
  567. end process;
  568. end architecture;