OpenCores
URL https://opencores.org/ocsvn/spi_boot/spi_boot/trunk

Subversion Repositories spi_boot

[/] [spi_boot/] [trunk/] [rtl/] [vhdl/] [spi_boot.vhd] - Blame information for rev 77

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 arniml
-------------------------------------------------------------------------------
2
--
3
-- SD/MMC Bootloader
4
--
5 77 arniml
-- $Id: spi_boot.vhd 77 2009-04-01 19:53:14Z arniml $
6 3 arniml
--
7
-- Copyright (c) 2005, Arnim Laeuger (arniml@opencores.org)
8
--
9
-- All rights reserved, see COPYING.
10
--
11
-- Redistribution and use in source and synthezised forms, with or without
12
-- modification, are permitted provided that the following conditions are met:
13
--
14
-- Redistributions of source code must retain the above copyright notice,
15
-- this list of conditions and the following disclaimer.
16
--
17
-- Redistributions in synthesized form must reproduce the above copyright
18
-- notice, this list of conditions and the following disclaimer in the
19
-- documentation and/or other materials provided with the distribution.
20
--
21
-- Neither the name of the author nor the names of other contributors may
22
-- be used to endorse or promote products derived from this software without
23
-- specific prior written permission.
24
--
25
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
29
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
-- POSSIBILITY OF SUCH DAMAGE.
36
--
37
-- Please report bugs to the author, but before you do so, please
38
-- make sure that this is not a derivative work and that
39
-- you have the latest version of this file.
40
--
41
-- The latest version of this file can be found at:
42
--      http://www.opencores.org/projects.cgi/web/spi_boot/overview
43
--
44
-------------------------------------------------------------------------------
45
 
46
library ieee;
47
use ieee.std_logic_1164.all;
48
 
49
 
50
entity spi_boot is
51
 
52
  generic (
53
    -- width of bit counter: minimum 6, maximum 12
54
    width_bit_cnt_g      : integer := 6;
55 17 arniml
    -- width of image counter: minimum 0, maximum n
56
    width_img_cnt_g      : integer := 2;
57
    -- number of bits required to address one image
58
    num_bits_per_img_g   : integer := 18;
59 3 arniml
    -- SD specific initialization
60
    sd_init_g            : integer := 0;
61
    -- clock divider to reach 400 kHz for MMC compatibility
62
    mmc_compat_clk_div_g : integer := 0;
63
    width_mmc_clk_div_g  : integer := 0;
64
    -- active level of reset_i
65
    reset_level_g        : integer := 0
66
  );
67
 
68
  port (
69
    -- System Interface -------------------------------------------------------
70
    clk_i          : in  std_logic;
71
    reset_i        : in  std_logic;
72 60 arniml
    set_sel_i      : in  std_logic_vector(31-width_img_cnt_g-num_bits_per_img_g
73
                                          downto 0);
74 3 arniml
    -- Card Interface ---------------------------------------------------------
75
    spi_clk_o      : out std_logic;
76
    spi_cs_n_o     : out std_logic;
77
    spi_data_in_i  : in  std_logic;
78
    spi_data_out_o : out std_logic;
79 13 arniml
    spi_en_outs_o  : out std_logic;
80 3 arniml
    -- FPGA Configuration Interface -------------------------------------------
81
    start_i        : in  std_logic;
82
    mode_i         : in  std_logic;
83
    config_n_o     : out std_logic;
84 40 arniml
    detached_o     : out std_logic;
85 3 arniml
    cfg_init_n_i   : in  std_logic;
86
    cfg_done_i     : in  std_logic;
87
    dat_done_i     : in  std_logic;
88
    cfg_clk_o      : out std_logic;
89
    cfg_dat_o      : out std_logic
90
  );
91
 
92
end spi_boot;
93
 
94
 
95
library ieee;
96
use ieee.numeric_std.all;
97
use work.spi_boot_pack.all;
98
 
99
architecture rtl of spi_boot is
100
 
101
  component spi_counter
102
    generic (
103
      cnt_width_g   : integer := 4;
104 58 arniml
      cnt_max_g     : integer := 15
105 3 arniml
    );
106
    port (
107
      clk_i      : in  std_logic;
108 58 arniml
      reset_i    : in  boolean;
109 3 arniml
      cnt_en_i   : in  boolean;
110
      cnt_o      : out std_logic_vector(cnt_width_g-1 downto 0);
111
      cnt_ovfl_o : out boolean
112
    );
113
  end component;
114
 
115
 
116
  -----------------------------------------------------------------------------
117
  -- States of the controller FSM
118
  --
119
  type ctrl_states_t is (POWER_UP1, POWER_UP2,
120
                         CMD0,
121
                         CMD1,
122
                         CMD55, ACMD41,
123
                         CMD16,
124
                         WAIT_START,
125
                         WAIT_INIT_LOW, WAIT_INIT_HIGH,
126
                         CMD18, CMD18_DATA,
127
                         CMD12,
128 17 arniml
                         INC_IMG_CNT);
129 3 arniml
  --
130
  signal ctrl_fsm_q,
131
         ctrl_fsm_s  : ctrl_states_t;
132
  --
133
  -----------------------------------------------------------------------------
134
 
135
  -----------------------------------------------------------------------------
136
  -- States of the command FSM
137
  --
138
  type cmd_states_t is (CMD, START, R1, PAUSE);
139
  --
140
  signal cmd_fsm_q,
141
         cmd_fsm_s  : cmd_states_t;
142
  --
143
  -----------------------------------------------------------------------------
144
 
145
  subtype op_r     is integer range 5 downto 0;
146
  type    res_bc_t is (NONE, RES_MAX, RES_47, RES_15, RES_7);
147
  signal  bit_cnt_q  : unsigned(width_bit_cnt_g-1 downto 0);
148
  signal  res_bc_s   : res_bc_t;
149
  signal  upper_bitcnt_zero_s : boolean;
150
 
151
  signal cfg_dat_q : std_logic;
152
 
153
  signal spi_clk_q         : std_logic;
154
  signal spi_clk_rising_q  : boolean;
155
  signal spi_clk_falling_q : boolean;
156
  signal spi_dat_q,
157
         spi_dat_s         : std_logic;
158
  signal spi_cs_n_q,
159
         spi_cs_n_s        : std_logic;
160
 
161
  signal cfg_clk_q      : std_logic;
162
 
163
  signal start_q        : std_logic;
164
 
165 17 arniml
  signal img_cnt_s      : std_logic_vector(width_img_cnt_g downto 0);
166
  signal cnt_en_img_s   : boolean;
167 3 arniml
  signal mmc_cnt_ovfl_s : boolean;
168
  signal mmc_compat_s   : boolean;
169
 
170
  signal cmd_finished_s : boolean;
171
 
172 66 arniml
  signal r1_illcmd_q,
173
         r1_idle_q      : std_logic;
174 3 arniml
  signal done_q,
175
         send_cmd12_q   : boolean;
176
 
177 13 arniml
  signal en_outs_s,
178
         en_outs_q      : boolean;
179
 
180 58 arniml
  signal reset_s        : boolean;
181
 
182 3 arniml
  signal true_s         : boolean;
183
 
184
begin
185
 
186
  true_s <= true;
187
 
188 58 arniml
  reset_s <=   true
189
             when (reset_level_g = 1 and reset_i = '1') or
190
                  (reset_level_g = 0 and reset_i = '0') else
191
               false;
192
 
193 3 arniml
  -----------------------------------------------------------------------------
194
  -- Process seq
195
  --
196
  -- Purpose:
197
  --   Implements several sequential elements.
198
  --
199 58 arniml
  seq: process (clk_i, reset_s)
200 3 arniml
 
201
    variable bit_cnt_v : unsigned(1 downto 0);
202
 
203
  begin
204 58 arniml
    if reset_s then
205 3 arniml
      -- reset bit counter to 63 for power up
206
      bit_cnt_q       <= (others => '0');
207
      bit_cnt_q(op_r) <= "111111";
208
      spi_dat_q    <= '1';
209
      spi_cs_n_q   <= '1';
210
      cfg_dat_q    <= '1';
211
      start_q      <= '0';
212
      done_q       <= false;
213
      send_cmd12_q <= false;
214
      ctrl_fsm_q   <= POWER_UP1;
215
      cmd_fsm_q    <= CMD;
216 66 arniml
      r1_illcmd_q  <= '0';
217
      r1_idle_q    <= '0';
218 57 arniml
      en_outs_q    <= false;
219 3 arniml
 
220
    elsif clk_i'event and clk_i = '1' then
221
      -- bit counter control
222
      if spi_clk_rising_q then
223
        case res_bc_s is
224
          when NONE =>
225
            bit_cnt_q       <= bit_cnt_q - 1;
226
          when RES_MAX =>
227
            bit_cnt_q       <= (others => '1');
228
          when RES_47 =>
229
            bit_cnt_q       <= (others => '0');
230
            bit_cnt_q(op_r) <= "101111";
231
          when RES_15 =>
232
            bit_cnt_q       <= (others => '0');
233
            bit_cnt_q(op_r) <= "001111";
234
          when RES_7 =>
235
            bit_cnt_q       <= (others => '0');
236
            bit_cnt_q(op_r) <= "000111";
237
          when others =>
238
            bit_cnt_q       <= (others => '0');
239
        end case;
240
      end if;
241
 
242
      -- Card data output register
243
      -- spi_clk_falling_q acts as enable during MMC clock compatibility mode.
244
      -- As soon as this mode is left, the register must start latching.
245
      -- There is no explicit relation to spi_clk_q anymore in normal mode.
246
      -- Instead, spi_dat_s is operated by bit_cnt_q above which changes its
247
      -- value after the rising edge of spi_clk_q.
248
      --   -> spi_dat_q changes upon falling edge of spi_clk_q
249
      if spi_clk_falling_q or not mmc_compat_s then
250
        spi_dat_q <= spi_dat_s;
251
      end if;
252
 
253
      -- config data output register
254
      -- a new value is loaded when config clock is high,
255
      -- i.e. input data is sampled with rising spi_clk
256
      -- while output value changes on falling edge of cfg_clk
257
      if cfg_clk_q = '1' and spi_clk_rising_q then
258
        cfg_dat_q <= spi_data_in_i;
259
      end if;
260
 
261
      -- Controller FSM state
262
      ctrl_fsm_q <= ctrl_fsm_s;
263
 
264
      -- Command FSM state
265
      cmd_fsm_q <= cmd_fsm_s;
266
 
267
      -- CS signal for SPI card
268
      if spi_clk_q = '1' then
269
        spi_cs_n_q <= spi_cs_n_s;
270
      end if;
271
 
272
      -- Extract flags from R1 response
273
      if cmd_fsm_q = R1 then
274
        bit_cnt_v := bit_cnt_q(1 downto 0);
275
        case bit_cnt_v(1 downto 0) is
276
          when "10" =>
277 66 arniml
            -- save "Illegal Command" flag
278
            r1_illcmd_q <= to_X01(spi_data_in_i);
279 3 arniml
          when "00" =>
280 66 arniml
            -- save "Idle State" flag
281
            r1_idle_q   <= to_X01(spi_data_in_i);
282 3 arniml
          when others =>
283
            null;
284
        end case;
285
      end if;
286
 
287
      -- Start trigger register for rising edge detection
288
      -- the reset value is '0' thus a rising edge will always be detected
289
      -- after reset even though start_i is tied to '1'
290
      if start_i = '0' then
291
        start_q <= '0';
292 8 arniml
      elsif ctrl_fsm_q = WAIT_START and cmd_finished_s then
293 3 arniml
        start_q <= start_i;
294
      end if;
295
 
296
      -- Marker for cfg_done and dat_done
297
      if ctrl_fsm_q = CMD18_DATA then
298 8 arniml
        if cfg_done_i = '1' and dat_done_i = '1' then
299 3 arniml
          done_q       <= true;
300
        end if;
301
 
302
        if done_q and
303
           (not upper_bitcnt_zero_s or cmd_fsm_q = START) then
304
          -- activate sending of CMD12 when it is safe:
305
          -- * upper bits of bit counter are not zero
306
          --   -> transmission of CMD12 is not running
307
          -- * cmd FSM is in START state
308
          --   -> also no transmission running
309
          send_cmd12_q <= true;
310
        end if;
311
      elsif ctrl_fsm_q = WAIT_START then
312
        -- reset done_q when WAIT_START has been reached
313
        -- this is necessary to let the stop transmission process come to
314
        -- an end without interruption or generation of unwanted cfg_clk_q
315
        done_q         <= false;
316
        send_cmd12_q   <= false;
317
      end if;
318
 
319 13 arniml
      -- output enable
320
      if spi_clk_rising_q then
321
        en_outs_q <= en_outs_s;
322
      end if;
323
 
324 3 arniml
    end if;
325
 
326
  end process seq;
327
  --
328
  -----------------------------------------------------------------------------
329
 
330
 
331
  -----------------------------------------------------------------------------
332
  -- Process upper_bits
333
  --
334
  -- Purpose:
335
  --   Detects that the upper bits of the bit counter are zero.
336
  --   Upper bits = n downto 6, i.e. the optional part that is not required for
337
  --   commands but for extension of data blocks.
338
  --
339
  upper_bits: process (bit_cnt_q)
340
    variable zero_v : boolean;
341
  begin
342
 
343
    zero_v     := true;
344
    for i in bit_cnt_q'high downto 6 loop
345
      if bit_cnt_q(i) = '1' then
346
        zero_v := false;
347
      end if;
348
    end loop;
349
 
350
    upper_bitcnt_zero_s <= zero_v;
351
 
352
  end process upper_bits;
353
  --
354
  -----------------------------------------------------------------------------
355
 
356
 
357
  -----------------------------------------------------------------------------
358
  -- Process clk_gen
359
  --
360
  -- Purpose:
361
  --   Generates clocks for card and FPGA configuration.
362
  --   The card clock is free running with a divide by two of clk_i.
363
  --   The clock for FPGA config has an enable and is stopped on high level.
364
  --   There is a phase shift of half a period between spi_clk and cfg_clk.
365
  --
366 58 arniml
  clk_gen: process (clk_i, reset_s)
367 3 arniml
  begin
368 58 arniml
    if reset_s then
369 3 arniml
      spi_clk_q        <= '0';
370
      cfg_clk_q        <= '1';
371
 
372
    elsif clk_i'event and clk_i = '1' then
373
 
374
      -- spi_clk_q rises according to the flag
375
      -- it falls with overflow indication
376
      -- the resulting duty cycle is not exactly 50:50,
377
      -- high time is a bit longer
378
      if mmc_compat_s then
379
        -- MMC clock compatibility mode:
380
        -- spi_clk_q rises when flagged by spi_clk_rising_q
381
        if spi_clk_rising_q then
382
          spi_clk_q <= '1';
383
        elsif mmc_cnt_ovfl_s then
384
          -- upon counter overflow spi_clk_q falls in case it does not rise
385
          spi_clk_q <= '0';
386
        end if;
387
      else
388
        -- normal mode
389
        -- spi_clk_q follows spi_clk_rising_q
390
        if spi_clk_rising_q then
391
          spi_clk_q <= '1';
392
        else
393
          spi_clk_q <= '0';
394
        end if;
395
      end if;
396
 
397
      -- clock for FPGA config must be enabled and follows spi_clk
398
      if ctrl_fsm_q = CMD18_DATA and cmd_fsm_q = CMD and
399
         not done_q then
400
        cfg_clk_q <= spi_clk_q;
401
      else
402
        cfg_clk_q <= '1';
403
      end if;
404
 
405
    end if;
406
 
407
  end process clk_gen;
408
  --
409
  -----------------------------------------------------------------------------
410
 
411
 
412
  -----------------------------------------------------------------------------
413
  -- Indication flags for rising and falling spi_clk_q.
414
  -- Essential for MMC clock compatibility mode.
415
  -----------------------------------------------------------------------------
416
  mmc_comap: if mmc_compat_clk_div_g > 0 generate
417 58 arniml
    mmc_compat_sig: process (clk_i, reset_s)
418 3 arniml
    begin
419 58 arniml
      if reset_s then
420 3 arniml
        spi_clk_rising_q  <= false;
421
        spi_clk_falling_q <= false;
422
 
423
      elsif clk_i'event and clk_i = '1' then
424
        if mmc_compat_s then
425
          -- MMC clock compatibility mode:
426
          -- spi_clk_rising_q is an impulse right before rising edge of spi_clk_q
427
          -- spi_clk_falling_q is an impulse right before falling edge of spi_clk_q
428
          if mmc_cnt_ovfl_s then
429
            spi_clk_rising_q  <= spi_clk_q = '0';
430
            spi_clk_falling_q <= spi_clk_q = '1';
431
          else
432
            spi_clk_rising_q  <= false;
433
            spi_clk_falling_q <= false;
434
          end if;
435
        else
436
          -- normal mode
437
          spi_clk_rising_q  <= not spi_clk_rising_q;
438
          spi_clk_falling_q <= true;
439
        end if;
440
 
441
      end if;
442
    end process mmc_compat_sig;
443
  end generate;
444
 
445
  no_mmc_compat: if mmc_compat_clk_div_g = 0 generate
446
    -- SPI clock rising whenever spi_clk_q is '0'
447
    spi_clk_rising_q  <= spi_clk_q = '0';
448
    -- SPI clock falling whenever spi_clk_q is '1'
449
    spi_clk_falling_q <= spi_clk_q = '1';
450
  end generate;
451
 
452
 
453
  -----------------------------------------------------------------------------
454
  -- Process ctrl_fsm
455
  --
456
  -- Purpose:
457
  --   Implements the controller FSM.
458
  --
459
  ctrl_fsm: process (ctrl_fsm_q,
460 66 arniml
                     cmd_finished_s, r1_illcmd_q, r1_idle_q,
461 3 arniml
                     start_i, start_q, mode_i,
462
                     cfg_init_n_i)
463
 
464
    variable mmc_compat_v : boolean;
465
 
466
  begin
467
    -- default assignments
468
    ctrl_fsm_s   <= POWER_UP1;
469
    config_n_o   <= '1';
470 17 arniml
    cnt_en_img_s <= false;
471 3 arniml
    spi_cs_n_s   <= '0';
472
    mmc_compat_v := false;
473 13 arniml
    en_outs_s    <= true;
474 3 arniml
 
475
    case ctrl_fsm_q is
476
      -- Let card finish power up, step 1 -------------------------------------
477
      when POWER_UP1 =>
478
        mmc_compat_v := true;
479
        spi_cs_n_s   <= '1';
480
        if cmd_finished_s then
481
          ctrl_fsm_s <= POWER_UP2;
482
        else
483
          ctrl_fsm_s <= POWER_UP1;
484
        end if;
485
 
486
 
487
      -- Let card finish power up, step 2 -------------------------------------
488
      when POWER_UP2 =>
489
        mmc_compat_v := true;
490
        if cmd_finished_s then
491
          ctrl_fsm_s <= CMD0;
492
        else
493
          spi_cs_n_s <= '1';
494
          ctrl_fsm_s <= POWER_UP2;
495
        end if;
496
 
497
 
498
      -- Issue CMD0: GO_IDLE_STATE --------------------------------------------
499
      when CMD0 =>
500
        mmc_compat_v   := true;
501
        if cmd_finished_s then
502
          if sd_init_g = 1 then
503
            ctrl_fsm_s <= CMD55;
504
          else
505
            ctrl_fsm_s <= CMD1;
506
          end if;
507
        else
508
          ctrl_fsm_s   <= CMD0;
509
        end if;
510
 
511
 
512
      -- Issue CMD55: APP_CMD -------------------------------------------------
513
      when CMD55 =>
514
        if sd_init_g = 1 then
515
 
516
          mmc_compat_v   := true;
517
          if cmd_finished_s then
518 66 arniml
            if r1_illcmd_q = '0' then
519
              -- command accepted, continue with ACMD41
520 3 arniml
              ctrl_fsm_s <= ACMD41;
521
            else
522
              -- command rejected, it's an MMC card
523
              ctrl_fsm_s <= CMD1;
524
            end if;
525
          else
526
            ctrl_fsm_s   <= CMD55;
527
          end if;
528
 
529
        end if;
530
 
531
 
532
      -- Issue ACMD41: SEND_OP_COND -------------------------------------------
533
      when ACMD41 =>
534
        if sd_init_g = 1 then
535
 
536 66 arniml
          mmc_compat_v     := true;
537 3 arniml
          if cmd_finished_s then
538 66 arniml
            if r1_illcmd_q = '0' then
539
              -- ok, that's an SD card
540
              if r1_idle_q = '0' then
541
                ctrl_fsm_s <= CMD16;
542
              else
543
                ctrl_fsm_s <= CMD55;
544
              end if;
545
 
546 3 arniml
            else
547 66 arniml
              -- command rejected, though it accepted CMD55 -> it's an MMC
548
              ctrl_fsm_s   <= CMD1;
549 3 arniml
            end if;
550 66 arniml
 
551 3 arniml
          else
552 66 arniml
            ctrl_fsm_s     <= ACMD41;
553 3 arniml
          end if;
554
 
555
        end if;
556
 
557
 
558
      -- Issue CMD1: SEND_OP_COND ---------------------------------------------
559
      when CMD1 =>
560
        mmc_compat_v   := true;
561
        if cmd_finished_s then
562 66 arniml
          if r1_idle_q = '0' then
563 3 arniml
            ctrl_fsm_s <= CMD16;
564
          else
565
            ctrl_fsm_s <= CMD1;
566
          end if;
567
        else
568
          ctrl_fsm_s   <= CMD1;
569
        end if;
570
 
571
 
572
      -- Issue CMD16: SET_BLOCKLEN --------------------------------------------
573
      when CMD16 =>
574
        if cmd_finished_s then
575
          ctrl_fsm_s <= WAIT_START;
576
        else
577
          ctrl_fsm_s <= CMD16;
578
        end if;
579
 
580
 
581
      -- Wait for configuration start request ---------------------------------
582
      when WAIT_START =>
583
        spi_cs_n_s     <= '1';
584
 
585
        -- detect rising edge of start_i
586
        if start_i = '1' and start_q = '0' then
587
          -- decide which mode is requested
588
          if cmd_finished_s then
589
            if mode_i = '0' then
590
              ctrl_fsm_s <= CMD18;
591
            else
592
              ctrl_fsm_s <= WAIT_INIT_LOW;
593
            end if;
594
          else
595 13 arniml
            en_outs_s    <= false;
596 3 arniml
            ctrl_fsm_s   <= WAIT_START;
597
          end if;
598
        else
599 13 arniml
          en_outs_s      <= false;
600 3 arniml
          ctrl_fsm_s     <= WAIT_START;
601
        end if;
602
 
603
 
604
      -- Wait for INIT to become low ------------------------------------------
605
      when WAIT_INIT_LOW =>
606
        spi_cs_n_s   <= '1';
607
        -- activate FPGA configuration
608
        config_n_o   <= '0';
609
 
610
        if cfg_init_n_i = '0' then
611
          ctrl_fsm_s <= WAIT_INIT_HIGH;
612
        else
613
          ctrl_fsm_s <= WAIT_INIT_LOW;
614
        end if;
615
 
616
 
617
      -- Wait for INIT to become high -----------------------------------------
618
      when WAIT_INIT_HIGH =>
619
        spi_cs_n_s   <= '1';
620
 
621
        if cfg_init_n_i = '1' and cmd_finished_s then
622
          ctrl_fsm_s <= CMD18;
623
        else
624
          ctrl_fsm_s <= WAIT_INIT_HIGH;
625
        end if;
626
 
627
 
628
      -- Issue CMD18: READ_MULTIPLE_BLOCKS ------------------------------------
629
      when CMD18 =>
630
        if cmd_finished_s then
631
          ctrl_fsm_s <= CMD18_DATA;
632
        else
633
          ctrl_fsm_s <= CMD18;
634
        end if;
635
      --
636
      -- receive a data block
637
      when CMD18_DATA =>
638 8 arniml
        if cmd_finished_s then
639
          ctrl_fsm_s   <= CMD12;
640
        else
641
          ctrl_fsm_s   <= CMD18_DATA;
642
        end if;
643
 
644
 
645 60 arniml
       -- Issued CMD12: STOP_TRANSMISSION -------------------------------------
646 8 arniml
       when CMD12 =>
647 3 arniml
         if cmd_finished_s then
648 17 arniml
           ctrl_fsm_s <= INC_IMG_CNT;
649 3 arniml
         else
650 8 arniml
           ctrl_fsm_s <= CMD12;
651 3 arniml
         end if;
652
 
653
 
654 17 arniml
      -- Increment Image Counter ----------------------------------------------
655
      when INC_IMG_CNT =>
656 3 arniml
        spi_cs_n_s   <= '1';
657
        ctrl_fsm_s   <= WAIT_START;
658 17 arniml
        cnt_en_img_s <= true;
659 3 arniml
 
660
 
661
 
662
      when others =>
663
        null;
664
 
665
    end case;
666
 
667
    -- mmc_compat_s is suppressed if MMC clock compatibility is not required
668
    if mmc_compat_clk_div_g > 0 then
669
      mmc_compat_s <= mmc_compat_v;
670
    else
671
      mmc_compat_s <= false;
672
    end if;
673
 
674
  end process ctrl_fsm;
675
  --
676
  -----------------------------------------------------------------------------
677
 
678
 
679
  -----------------------------------------------------------------------------
680
  -- Process cmd_fsm
681
  --
682
  -- Purpose:
683
  --   Implements the command FSM.
684
  --
685
  cmd_fsm: process (spi_clk_rising_q,
686
                    spi_data_in_i,
687
                    bit_cnt_q,
688
                    ctrl_fsm_q,
689
                    cmd_fsm_q,
690
                    send_cmd12_q)
691
 
692
    variable cnt_zero_v     : boolean;
693
    variable spi_data_low_v : boolean;
694
    variable no_startbit_v  : boolean;
695
 
696
  begin
697
    -- default assignments
698
    cmd_finished_s <= false;
699
    cmd_fsm_s      <= CMD;
700
    res_bc_s       <= NONE;
701
 
702
    cnt_zero_v     := spi_clk_rising_q and bit_cnt_q = 0;
703
    spi_data_low_v := spi_clk_rising_q and spi_data_in_i = '0';
704
 
705
    -- these are no real commands thus there will be no startbit
706
    case ctrl_fsm_q is
707
      when POWER_UP1  | POWER_UP2 |
708
           WAIT_START | WAIT_INIT_HIGH | WAIT_INIT_LOW =>
709
        no_startbit_v := true;
710
      when others =>
711
        no_startbit_v := false;
712
    end case;
713
 
714
 
715
    case cmd_fsm_q is
716
      -- Send the command -----------------------------------------------------
717
      when CMD =>
718
        if cnt_zero_v then
719
          if ctrl_fsm_q /= CMD18_DATA then
720 8 arniml
            -- normal commands including CMD12 require startbit of R1 response
721 3 arniml
            cmd_fsm_s <= START;
722
          else
723 8 arniml
            if not send_cmd12_q then
724
              -- CMD18_DATA needs to read CRC
725
              cmd_fsm_s <= R1;
726
              res_bc_s <= RES_15;
727
            else
728
              -- CMD18_DATA finished, scan for startbit of response
729
              cmd_finished_s <= true;
730
              cmd_fsm_s      <= START;
731
            end if;
732 3 arniml
          end if;
733
        else
734
          cmd_fsm_s <= CMD;
735
        end if;
736
 
737
      -- Wait for startbit of response ----------------------------------------
738
      when START =>
739
        -- startbit detection or skip of this check
740
        if no_startbit_v and spi_clk_rising_q then
741
          cmd_fsm_s   <= R1;
742
          res_bc_s    <= RES_7;
743
        elsif spi_data_low_v then
744
          if ctrl_fsm_q /= CMD18_DATA then
745
            cmd_fsm_s <= R1;
746
          else
747
            -- CMD18_DATA startbit detected, read payload
748
            cmd_fsm_s <= CMD;
749
            res_bc_s  <= RES_MAX;
750
          end if;
751
        else
752
          cmd_fsm_s   <= START;
753
          res_bc_s    <= RES_7;
754
        end if;
755
 
756
      -- Read R1 response -----------------------------------------------------
757
      when R1 =>
758
        if cnt_zero_v then
759
          res_bc_s  <= RES_7;
760
 
761
          if not (ctrl_fsm_q = CMD18 or ctrl_fsm_q = CMD18_DATA) then
762
            cmd_fsm_s        <= PAUSE;
763
          else
764
            -- CMD18 needs another startbit detection for the data token.
765
            -- CMD18_DATA needs a startbit after having received the CRC, either
766
            --   * next data token
767
            --   * R1 response of CMD12
768
            cmd_fsm_s        <= START;
769
 
770 8 arniml
            if ctrl_fsm_q = CMD18 then
771
              -- CMD18 response received -> advance to CMD18_DATA
772 3 arniml
              cmd_finished_s <= true;
773
            end if;
774
          end if;
775
        else
776
          cmd_fsm_s <= R1;
777
        end if;
778
 
779
      -- PAUSE state -> required for Nrc, card response to host command -------
780
      when PAUSE =>
781
        if cnt_zero_v then
782
          cmd_fsm_s <= CMD;
783
          res_bc_s  <= RES_47;
784
          cmd_finished_s <= true;
785
        else
786
          cmd_fsm_s <= PAUSE;
787
        end if;
788
 
789
      when others =>
790
        null;
791
 
792
    end case;
793
 
794
  end process cmd_fsm;
795
  --
796
  -----------------------------------------------------------------------------
797
 
798
 
799
  -----------------------------------------------------------------------------
800
  -- Process transmit
801
  --
802
  -- Purpose:
803
  --   Generates the serial data output values based on the current FSM state
804
  --
805
  --   The local variable cmd_v is 64 bits wide in contrast to an SPI command
806
  --   with 48 bits. There are two reasons for this:
807
  --   * During "overlaid" sending of CMD12 in FSM state CMD18_DATA, the bit
808
  --     counter will start from 3F on its lowest 6 bits. Therefore, it is
809
  --     necessary to provide all 64 positions in cmd_v.
810
  --   * Reduces logic.
811
  --
812
  transmit: process (ctrl_fsm_q,
813
                     cmd_fsm_q,
814
                     bit_cnt_q,
815 17 arniml
                     img_cnt_s,
816 3 arniml
                     send_cmd12_q,
817 38 arniml
                     set_sel_i,
818 3 arniml
                     upper_bitcnt_zero_s)
819
 
820
    subtype cmd_r is natural range 47 downto 0;
821
    subtype cmd_t is std_logic_vector(cmd_r);
822
    subtype ext_cmd_t is std_logic_vector(63 downto 0);
823
    --                            STCCCCCCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcccccccS
824
    constant cmd0_c   : cmd_t := "010000000000000000000000000000000000000010010101";
825 8 arniml
    constant cmd1_c   : cmd_t := "0100000100000000000000000000000000000000-------1";
826
    constant cmd12_c  : cmd_t := "0100110000000000000000000000000000000000-------1";
827 3 arniml
    constant cmd16_c  : cmd_t := "0101000000000000000000000000000000000000-------1";
828
    constant cmd18_c  : cmd_t := "0101001000000000000000000000000000000000-------1";
829 8 arniml
    constant cmd55_c  : cmd_t := "0111011100000000000000000000000000000000-------1";
830
    constant acmd41_c : cmd_t := "0110100100000000000000000000000000000000-------1";
831 3 arniml
 
832
    variable cmd_v      : ext_cmd_t;
833
    variable tx_v       : boolean;
834
 
835
  begin
836
    -- default assignments
837
    spi_dat_s    <= '1';
838
    cmd_v        := (others => '1');
839
    tx_v         := false;
840
 
841
    if cmd_fsm_q = CMD then
842
      case ctrl_fsm_q is
843
        when CMD0 =>
844
          cmd_v(cmd_r) := cmd0_c;
845
          tx_v := true;
846
        when CMD1 =>
847
          cmd_v(cmd_r) := cmd1_c;
848
          tx_v := true;
849
        when CMD16 =>
850
          cmd_v(cmd_r) := cmd16_c;
851
          cmd_v(8 + width_bit_cnt_g-3) := '1';
852
          tx_v := true;
853
        when CMD18 =>
854
          cmd_v(cmd_r) := cmd18_c;
855 35 arniml
          -- insert image counter
856
          cmd_v(8 + num_bits_per_img_g + width_img_cnt_g
857
                downto 8 + num_bits_per_img_g) := img_cnt_s;
858
          -- insert set selection
859 60 arniml
          cmd_v(8 + 31
860 38 arniml
                downto 8 + num_bits_per_img_g + width_img_cnt_g) := set_sel_i;
861 3 arniml
          tx_v := true;
862
        when CMD18_DATA =>
863
          cmd_v(cmd_r) := cmd12_c;
864
 
865
          if send_cmd12_q and upper_bitcnt_zero_s then
866
            tx_v := true;
867
          end if;
868
        when CMD55 =>
869
          cmd_v(cmd_r) := cmd55_c;
870
          tx_v := true;
871
        when ACMD41 =>
872
          cmd_v(cmd_r) := acmd41_c;
873
          tx_v := true;
874
 
875
        when others =>
876
          null;
877
      end case;
878
    end if;
879
 
880
    if tx_v then
881
      spi_dat_s <= cmd_v(to_integer(bit_cnt_q(5 downto 0)));
882
    end if;
883
 
884
  end process transmit;
885
  --
886
  -----------------------------------------------------------------------------
887
 
888
 
889
  -----------------------------------------------------------------------------
890 17 arniml
  -- Optional Image Counter
891 3 arniml
  -----------------------------------------------------------------------------
892 17 arniml
  img_cnt: if width_img_cnt_g > 0 generate
893
    img_cnt_b : spi_counter
894 3 arniml
      generic map (
895 17 arniml
        cnt_width_g   => width_img_cnt_g,
896 58 arniml
        cnt_max_g     => 2**width_img_cnt_g - 1
897 3 arniml
      )
898
      port map (
899
        clk_i         => clk_i,
900 58 arniml
        reset_i       => reset_s,
901 17 arniml
        cnt_en_i      => cnt_en_img_s,
902
        cnt_o         => img_cnt_s(width_img_cnt_g-1 downto 0),
903 3 arniml
        cnt_ovfl_o    => open
904
      );
905 17 arniml
    img_cnt_s(width_img_cnt_g) <= '0';
906 3 arniml
  end generate;
907
 
908 17 arniml
  no_img_cnt: if width_img_cnt_g = 0 generate
909
    img_cnt_s <= (others => '0');
910 3 arniml
  end generate;
911
 
912
 
913
  -----------------------------------------------------------------------------
914
  -- Optional MMC compatibility counter
915
  -----------------------------------------------------------------------------
916
  mmc_cnt: if mmc_compat_clk_div_g > 0 generate
917
    mmc_cnt_b : spi_counter
918
      generic map (
919
        cnt_width_g   => width_mmc_clk_div_g,
920 58 arniml
        cnt_max_g     => mmc_compat_clk_div_g
921 3 arniml
      )
922
      port map (
923
        clk_i         => clk_i,
924 58 arniml
        reset_i       => reset_s,
925 3 arniml
        cnt_en_i      => true_s,
926
        cnt_o         => open,
927
        cnt_ovfl_o    => mmc_cnt_ovfl_s
928
      );
929
  end generate;
930
 
931
  no_mmc_cnt: if mmc_compat_clk_div_g = 0 generate
932
    mmc_cnt_ovfl_s <= true;
933
  end generate;
934
 
935
 
936
  -----------------------------------------------------------------------------
937
  -- Output Mapping
938
  -----------------------------------------------------------------------------
939 13 arniml
  spi_clk_o      <=   spi_clk_q;
940
  spi_cs_n_o     <=   spi_cs_n_q;
941
  spi_data_out_o <=   spi_dat_q;
942
  spi_en_outs_o  <=   '1'
943
                    when en_outs_q else
944
                      '0';
945 3 arniml
  cfg_clk_o      <= cfg_clk_q;
946
  cfg_dat_o      <= cfg_dat_q;
947 40 arniml
  detached_o     <=   '0'
948
                    when en_outs_q else
949
                      '1';
950 3 arniml
 
951
end rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.