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

Subversion Repositories mips_enhanced

[/] [mips_enhanced/] [trunk/] [grlib-gpl-1.0.19-b3188/] [lib/] [gaisler/] [memctrl/] [spimctrl.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dimamali
------------------------------------------------------------------------------
2
--  This file is a part of the GRLIB VHDL IP LIBRARY
3
--  Copyright (C) 2003, Gaisler Research
4
--
5
--  This program is free software; you can redistribute it and/or modify
6
--  it under the terms of the GNU General Public License as published by
7
--  the Free Software Foundation; either version 2 of the License, or
8
--  (at your option) any later version.
9
--
10
--  This program is distributed in the hope that it will be useful,
11
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
--  GNU General Public License for more details.
14
--
15
--  You should have received a copy of the GNU General Public License
16
--  along with this program; if not, write to the Free Software
17
--  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
18
-------------------------------------------------------------------------------
19
-- Entity:      spimctrl
20
-- File:        spimctrl.vhd
21
-- Author:      Jan Andersson - Gaisler Research AB
22
--              jan@gaisler.com
23
-- Description: SPI flash memory controller. Supports a wide range of SPI
24
--              memory devices with the data read instruction configurable via
25
--              generics. Also has limited support for initializing and reading
26
--              SD Cards in SPI mode.
27
-- 
28
-- The controller has two memory areas. The flash area where the flash memory
29
-- is directly mapped and the I/O area where core registers are mapped.
30
--
31
-------------------------------------------------------------------------------
32
 
33
library ieee;
34
use ieee.std_logic_1164.all;
35
library grlib;
36
use grlib.amba.all;
37
use grlib.devices.all;
38
use grlib.stdlib.all;
39
library gaisler;
40
use gaisler.memctrl.all;
41
 
42
entity spimctrl is
43
  generic (
44
    hindex     : integer := 0;            -- AHB slave index
45
    hirq       : integer := 0;            -- Interrupt line
46
    faddr      : integer := 16#000#;      -- Flash map base address
47
    fmask      : integer := 16#fff#;      -- Flash area mask
48
    ioaddr     : integer := 16#000#;      -- I/O base address
49
    iomask     : integer := 16#fff#;      -- I/O mask
50
    spliten    : integer := 0;            -- AMBA SPLIT support
51
    oepol      : integer := 0;            -- Output enable polarity
52
    sdcard     : integer range 0 to 1   := 0; -- Core is connected to SD card
53
    readcmd    : integer range 0 to 255 := 16#0B#;  -- Mem. dev. READ command
54
    dummybyte  : integer range 0 to 1   := 1;  -- Dummy byte after cmd
55
    dualoutput : integer range 0 to 1   := 0;  -- Enable dual output
56
    scaler     : integer range 1 to 512 := 1; -- SCK scaler
57
    altscaler  : integer range 1 to 512 := 1; -- Alternate SCK scaler 
58
    pwrupcnt   : integer  := 0                -- System clock cycles to init
59
  );
60
  port (
61
    rstn    : in  std_ulogic;
62
    clk     : in  std_ulogic;
63
    ahbsi   : in  ahb_slv_in_type;
64
    ahbso   : out ahb_slv_out_type;
65
    spii    : in  spimctrl_in_type;
66
    spio    : out spimctrl_out_type
67
  );
68
end spimctrl;
69
 
70
architecture rtl of spimctrl is
71
 
72
  constant REVISION : amba_version_type := 0;
73
 
74
  constant HCONFIG : ahb_config_type := (
75
 
76
    4 => ahb_iobar(ioaddr, iomask),
77
    5 => ahb_membar(faddr, '0', '0', fmask),
78
    others => zero32);
79
 
80
  -- BANKs
81
  constant CTRL_BANK  : integer := 0;
82
  constant FLASH_BANK : integer := 1;
83
 
84
  -----------------------------------------------------------------------------
85
  -- SD card constants
86
  -----------------------------------------------------------------------------
87
 
88
  constant SD_BLEN : integer := 4;
89
 
90
  constant SD_CRC_BYTE : std_logic_vector(7 downto 0) := X"95";
91
 
92
  constant SD_BLOCKLEN : std_logic_vector(31 downto 0) :=
93
    conv_std_logic_vector(SD_BLEN, 32);
94
 
95
  -- Commands
96
  constant SD_CMD0   : std_logic_vector(5 downto 0) := "000000";
97
  constant SD_CMD16  : std_logic_vector(5 downto 0) := "010000";
98
  constant SD_CMD17  : std_logic_vector(5 downto 0) := "010001";
99
  constant SD_CMD55  : std_logic_vector(5 downto 0) := "110111";
100
  constant SD_ACMD41 : std_logic_vector(5 downto 0) := "101001";
101
 
102
  -- Command timeout
103
  constant SD_CMD_TIMEOUT : integer := 100;
104
 
105
  -- Data token timeout
106
  constant SD_DATATOK_TIMEOUT : integer := 312500;
107
 
108
  -----------------------------------------------------------------------------
109
  -- SPI device constants
110
  -----------------------------------------------------------------------------
111
 
112
  -- Length of read instruction argument-1 
113
  constant SPI_ARG_LEN : integer := 2 + dummybyte;
114
 
115
  -----------------------------------------------------------------------------
116
  -- Core constants
117
  -----------------------------------------------------------------------------
118
 
119
  -- OEN
120
  constant OUTPUT : std_ulogic := conv_std_logic(oepol = 1);  -- Enable outputs
121
  constant INPUT : std_ulogic := not OUTPUT;   -- Tri-state outputs
122
 
123
  -- Register offsets
124
  constant CONF_REG_OFF  : std_logic_vector(7 downto 2) := "000000";
125
  constant CTRL_REG_OFF  : std_logic_vector(7 downto 2) := "000001";
126
  constant STAT_REG_OFF  : std_logic_vector(7 downto 2) := "000010";
127
  constant RX_REG_OFF    : std_logic_vector(7 downto 2) := "000011";
128
  constant TX_REG_OFF    : std_logic_vector(7 downto 2) := "000100";
129
 
130
  constant SPI_HSIZE_BYTE  : std_logic_vector(1 downto 0) := "00";
131
  constant SPI_HSIZE_HWORD : std_logic_vector(1 downto 0) := "01";
132
  constant SPI_HSIZE_WORD  : std_logic_vector(1 downto 0) := "10";
133
 
134
  -----------------------------------------------------------------------------
135
  -- Subprograms
136
  -----------------------------------------------------------------------------  
137
  -- Description: Determines required size of timer used for clock scaling
138
  function timer_size
139
    return integer is
140
  begin  -- timer_size
141
    if altscaler > scaler then
142
      return altscaler;
143
    end if;
144
    return scaler;
145
  end timer_size;
146
 
147
  -- Description: Returns the number of bits required for the haddr vector to
148
  -- be able to save the Flash area address.
149
  function req_addr_bits
150
    return integer is
151
  begin  -- req_addr_bits
152
    case fmask is
153
      when 16#fff# => return 20;
154
      when 16#ffe# => return 21;
155
      when 16#ffc# => return 22;
156
      when 16#ff8# => return 23;
157
      when 16#ff0# => return 24;
158
      when 16#fe0# => return 25;
159
      when 16#fc0# => return 26;
160
      when 16#f80# => return 27;
161
      when 16#f00# => return 28;
162
      when 16#e00# => return 29;
163
      when 16#c00# => return 30;
164
      when others  => return 31;
165
    end case;
166
  end req_addr_bits;
167
 
168
  -- Description: Returns true if SCK clock should transition
169
  function sck_toggle (
170
    curr         : std_logic_vector((timer_size-1) downto 0);
171
    last         : std_logic_vector((timer_size-1) downto 0);
172
    usealtscaler : boolean)
173
    return boolean is
174
  begin  -- sck_toggle
175
    if usealtscaler then
176
      return (curr(altscaler-1) xor last(altscaler-1)) = '1';
177
    end if;
178
    return (curr(scaler-1) xor last(scaler-1)) = '1';
179
  end sck_toggle;
180
 
181
  -- Description: Short for conv_std_logic_vector, avoiding an alias
182
  function cslv (
183
    i : integer;
184
    w : integer)
185
    return std_logic_vector is
186
  begin  -- cslv
187
    return conv_std_logic_vector(i,w);
188
  end cslv;
189
 
190
  -----------------------------------------------------------------------------
191
  -- States
192
  -----------------------------------------------------------------------------
193
 
194
  -- Main FSM states
195
  type spimstate_type is (IDLE, AHB_RESPOND, USER_SPI, BUSY);
196
 
197
--   subtype spimstate_type is std_logic_vector(1 downto 0);
198
 
199
--   constant IDLE        : std_logic_vector(spimstate_type'range) := "00";
200
--   constant AHB_RESPOND : std_logic_vector(spimstate_type'range) := "01";
201
--   constant USER_SPI    : std_logic_vector(spimstate_type'range) := "10";
202
--   constant BUSY        : std_logic_vector(spimstate_type'range) := "11";
203
 
204
  -- SPI device FSM states
205
 type spistate_type is (SPI_PWRUP,  SPI_READY, SPI_READ, SPI_ADDR, SPI_DATA);
206
 
207
--   subtype spistate_type is std_logic_vector(2 downto 0);
208
 
209
--   constant SPI_PWRUP : std_logic_vector(spistate_type'range) := "000";
210
--   constant SPI_READY : std_logic_vector(spistate_type'range) := "001";
211
--   constant SPI_READ  : std_logic_vector(spistate_type'range) := "010";
212
--   constant SPI_ADDR  : std_logic_vector(spistate_type'range) := "011";
213
--   constant SPI_DATA  : std_logic_vector(spistate_type'range) := "100";
214
 
215
  -- SD FSM states
216
  type sdstate_type is (SD_CHECK_PRES, SD_PWRUP0, SD_PWRUP1, SD_INIT_IDLE,
217
                        SD_ISS_ACMD41, SD_CHECK_CMD16, SD_READY,
218
                        SD_CHECK_CMD17, SD_CHECK_TOKEN, SD_HANDLE_DATA,
219
                        SD_SEND_CMD, SD_GET_RESP);
220
 
221
--   subtype sdstate_type is std_logic_vector(3 downto 0);
222
 
223
--   constant SD_CHECK_PRES   : std_logic_vector(sdstate_type'range) := "0000";
224
--   constant SD_PWRUP0       : std_logic_vector(sdstate_type'range) := "0001";
225
--   constant SD_PWRUP1       : std_logic_vector(sdstate_type'range) := "0010";
226
--   constant SD_INIT_IDLE    : std_logic_vector(sdstate_type'range) := "0011";
227
--   constant SD_ISS_ACMD41   : std_logic_vector(sdstate_type'range) := "0100";
228
--   constant SD_CHECK_CMD16  : std_logic_vector(sdstate_type'range) := "0101";
229
--   constant SD_READY        : std_logic_vector(sdstate_type'range) := "0110";
230
--   constant SD_CHECK_CMD17  : std_logic_vector(sdstate_type'range) := "0111";
231
--   constant SD_CHECK_TOKEN  : std_logic_vector(sdstate_type'range) := "1000";
232
--   constant SD_HANDLE_DATA  : std_logic_vector(sdstate_type'range) := "1001";
233
--   constant SD_SEND_CMD     : std_logic_vector(sdstate_type'range) := "1010";
234
--   constant SD_GET_RESP     : std_logic_vector(sdstate_type'range) := "1011";
235
 
236
  -----------------------------------------------------------------------------
237
  -- Types
238
  -----------------------------------------------------------------------------  
239
 
240
  type spim_ctrl_reg_type is record     -- Control register
241
       eas  : std_ulogic;               -- Enable alternate scaler
242
       ien  : std_ulogic;               -- Interrupt enable
243
       usrc : std_ulogic;               -- User mode
244
  end record;
245
 
246
  type spim_stat_reg_type is record     -- Status register
247
      busy : std_ulogic;                -- Core busy
248
      done : std_ulogic;                -- User operation done
249
  end record;
250
 
251
  type spim_regif_type is record        -- Register bank
252
       ctrl : spim_ctrl_reg_type;       -- Control register
253
       stat : spim_stat_reg_type;       -- Status register
254
  end record;
255
 
256
  type sdcard_type is record            -- Present when SD card
257
      state   : sdstate_type;           -- SD state
258
      tcnt    : std_logic_vector(2 downto 0);  -- Transmit count
259
      rcnt    : std_logic_vector(3 downto 0);  -- Receive count
260
      cmd     : std_logic_vector(5 downto 0);  -- SD command
261
      rstate  : sdstate_type;           -- Return state
262
      htb     : std_ulogic; -- Handle trailing byte
263
      vresp   : std_ulogic; -- Valid response
264
      cd      : std_ulogic; -- Synchronized card detect
265
      timeout : std_ulogic; -- Timeout status bit 
266
      dtocnt  : std_logic_vector(18 downto 0); -- Data token timeout counter
267
      ctocnt  : std_logic_vector(6 downto 0);  -- CMD resp. timeout counter
268
  end record;
269
 
270
  type spiflash_type is record          -- Present when !SD card
271
       state : spistate_type;           -- Mem. device comm. state
272
       cnt   : std_logic_vector(1 downto 0);  -- Generic counter
273
       hsize : std_logic_vector(1 downto 0);  -- Size of access
274
  end record;
275
 
276
  type spim_reg_type is record
277
       -- Common
278
       spimstate      : spimstate_type;  -- Main FSM
279
       rst            : std_ulogic;      -- Reset
280
       reg            : spim_regif_type; -- Register bank
281
       timer          : std_logic_vector((timer_size-1) downto 0);
282
       sample         : std_ulogic;     -- Sample data line
283
       sreg           : std_logic_vector(7 downto 0);  -- Shiftreg
284
       bcnt           : std_logic_vector(2 downto 0);  -- Bit counter
285
       go             : std_ulogic;     -- SPI comm. active
286
       stop           : std_ulogic;     -- Stop SPI comm.
287
       ar             : std_logic_vector(31 downto 0); -- argument/response
288
       hold           : std_ulogic;     -- Do not shift ar
289
       insplit        : std_ulogic;     -- SPLIT response issued
290
       unsplit        : std_ulogic;     -- SPLIT complete not issued
291
       -- SPI flash device
292
       spi            : spiflash_type;  -- Used when !SD card
293
       -- SD
294
       sd             : sdcard_type;    -- Used when SD card
295
       -- AHB
296
       irq            : std_ulogic;     -- Interrupt request
297
       hsize          : std_logic_vector(1 downto 0);
298
       hwrite         : std_ulogic;
299
       hsel           : std_ulogic;
300
       hmbsel         : std_logic_vector(0 to 1);
301
       haddr          : std_logic_vector((req_addr_bits-1) downto 0);
302
       hready         : std_ulogic;
303
       frdata         : std_logic_vector(31 downto 0);  -- Flash response data
304
       rrdata         : std_logic_vector(7 downto 0);  -- Register response data
305
       hresp          : std_logic_vector(1 downto 0);
306
       splmst         : std_logic_vector(3 downto 0);  -- SPLIT:ed master
307
       hsplit         : std_logic_vector(15 downto 0);  -- Other SPLIT:ed masters
308
       ahbcancel      : std_ulogic;     -- Locked access cancels ongoing SPLIT
309
                                        -- response
310
       -- Inputs and outputs
311
       spii           : spimctrl_in_type;
312
       spio           : spimctrl_out_type;
313
  end record;
314
 
315
  -----------------------------------------------------------------------------
316
  -- Signals
317
  -----------------------------------------------------------------------------
318
 
319
  signal r, rin : spim_reg_type;
320
 
321
begin  -- rtl
322
 
323
  comb: process (r, rstn, ahbsi, spii)
324
    variable v                : spim_reg_type;
325
    variable change           : std_ulogic;
326
    variable regaddr          : std_logic_vector(7 downto 2);
327
    variable hsplit           : std_logic_vector(15 downto 0);
328
    variable ahbirq           : std_logic_vector((NAHBIRQ-1) downto 0);
329
    variable lastbit          : std_ulogic;
330
    variable bytedone         : std_ulogic;
331
    variable enable_altscaler : boolean;
332
    variable disable_flash    : boolean;
333
    variable read_flash       : boolean;
334
  begin  -- process comb
335
    v := r; v.spii := spii; v.sample := '0'; change := '0';
336
    v.irq := '0'; v.hresp := HRESP_OKAY; v.hready := '1';
337
    regaddr := r.haddr(7 downto 2); hsplit := (others => '0');
338
    ahbirq := (others => '0'); ahbirq(hirq) := r.irq;
339
    if sdcard = 1 then v.sd.cd := r.spii.cd; else v.sd.cd := '0'; end if;
340
    read_flash := false;
341
    enable_altscaler := (not r.spio.initialized or r.reg.ctrl.eas) = '1';
342
    disable_flash := (r.spio.errorn = '0' or r.reg.ctrl.usrc = '1' or
343
                      r.spio.initialized = '0' or r.spimstate = USER_SPI);
344
    if dualoutput = 1 and sdcard = 0 then
345
      lastbit := andv(r.bcnt(1 downto 0)) and
346
                 ((r.spio.mosioen xnor INPUT) or r.bcnt(2));
347
    else
348
      lastbit := andv(r.bcnt);
349
    end if;
350
    bytedone := lastbit and r.sample;
351
 
352
    ---------------------------------------------------------------------------
353
    -- AHB communication
354
    ---------------------------------------------------------------------------
355
    if ahbsi.hready = '1' then
356
      if (ahbsi.hsel(hindex) and ahbsi.htrans(1)) = '1' then
357
        v.hmbsel := ahbsi.hmbsel(r.hmbsel'range);
358
        if (spliten = 0 or r.spimstate /= AHB_RESPOND or
359
            ahbsi.hmbsel(CTRL_BANK) = '1' or ahbsi.hmastlock = '1') then
360
          -- Writes to register space have no wait state
361
          v.hready := ahbsi.hmbsel(CTRL_BANK) and ahbsi.hwrite;
362
          v.hsize := ahbsi.hsize(1 downto 0);
363
          v.hwrite := ahbsi.hwrite;
364
          v.haddr := ahbsi.haddr(r.haddr'range);
365
          v.hsel := '1';
366
          if ahbsi.hmbsel(FLASH_BANK) = '1' then
367
            if ahbsi.hwrite = '1' or disable_flash then
368
              v.hresp := HRESP_ERROR;
369
              v.hsel := '0';
370
            else
371
              if spliten /= 0 then
372
                if ahbsi.hmastlock = '0' then
373
                  v.hresp := HRESP_SPLIT;
374
                  v.splmst := ahbsi.hmaster;
375
                  v.unsplit := '1';
376
                else
377
                  v.ahbcancel := r.insplit;
378
                end if;
379
                v.insplit := not ahbsi.hmastlock;
380
              end if;
381
            end if;
382
          end if;
383
        else
384
          -- Core is busy, transfer is not locked and access was to flash
385
          -- area. Respond with SPLIT or insert wait states
386
          v.hready := '0';
387
          if spliten /= 0 then
388
            v.hresp := HRESP_SPLIT;
389
            v.hsplit(conv_integer(ahbsi.hmaster)) := '1';
390
          end if;
391
        end if;
392
      else
393
        v.hsel := '0';
394
      end if;
395
    end if;
396
 
397
    if (r.hready = '0') then
398
      if (r.hresp = HRESP_OKAY) then v.hready := '0';
399
      else v.hresp := r.hresp; end if;
400
    end if;
401
 
402
    -- Read access to core registers
403
    if (r.hsel and r.hmbsel(CTRL_BANK) and not r.hwrite) = '1' then
404
      v.rrdata := (others => '0');
405
      v.hready := '1';
406
      v.hsel := '0';
407
      case regaddr is
408
        when CONF_REG_OFF =>
409
          if sdcard = 1 then
410
            v.rrdata := (others => '0');
411
          else
412
            v.rrdata := cslv(readcmd, 8);
413
          end if;
414
        when CTRL_REG_OFF =>
415
          v.rrdata(3) := r.spio.csn;
416
          v.rrdata(2) := r.reg.ctrl.eas;
417
          v.rrdata(1) := r.reg.ctrl.ien;
418
          v.rrdata(0) := r.reg.ctrl.usrc;
419
        when STAT_REG_OFF =>
420
          v.rrdata(5) := r.sd.cd;
421
          v.rrdata(4) := r.sd.timeout;
422
          v.rrdata(3) := not r.spio.errorn;
423
          v.rrdata(2) := r.spio.initialized;
424
          v.rrdata(1) := r.reg.stat.busy;
425
          v.rrdata(0) := r.reg.stat.done;
426
        when RX_REG_OFF => v.rrdata := r.ar(7 downto 0);
427
        when others => null;
428
      end case;
429
    end if;
430
 
431
    -- Write access to core registers
432
    if (r.hsel and r.hmbsel(CTRL_BANK) and r.hwrite) = '1' then
433
      case regaddr is
434
        when CTRL_REG_OFF =>
435
          v.rst           := ahbsi.hwdata(4);
436
          if (r.reg.ctrl.usrc and not ahbsi.hwdata(0)) = '1' then
437
            v.spio.csn := '1';
438
          elsif ahbsi.hwdata(0) = '1' then
439
            v.spio.csn := ahbsi.hwdata(3);
440
          end if;
441
          v.reg.ctrl.eas  := ahbsi.hwdata(2);
442
          v.reg.ctrl.ien  := ahbsi.hwdata(1);
443
          v.reg.ctrl.usrc := ahbsi.hwdata(0);
444
        when STAT_REG_OFF =>
445
          v.spio.errorn := r.spio.errorn or ahbsi.hwdata(3);
446
          v.reg.stat.done := r.reg.stat.done and not ahbsi.hwdata(0);
447
        when RX_REG_OFF => null;
448
        when TX_REG_OFF =>
449
          if r.reg.ctrl.usrc = '1' then
450
            v.sreg := ahbsi.hwdata(7 downto 0);
451
          end if;
452
        when others => null;
453
      end case;
454
    end if;
455
 
456
    ---------------------------------------------------------------------------
457
    -- SPIMCTRL control FSM
458
    ---------------------------------------------------------------------------
459
    v.reg.stat.busy := '1';
460
 
461
    case r.spimstate is
462
      when BUSY =>
463
        if r.spio.ready = '1' then
464
          v.spimstate := IDLE;
465
        end if;
466
 
467
      when AHB_RESPOND =>
468
        if r.spio.ready = '1' then
469
          if spliten /= 0 and r.unsplit = '1' then
470
            hsplit(conv_integer(r.splmst)) := '1';
471
            v.unsplit := '0';
472
          end if;
473
          if ((spliten = 0 or v.ahbcancel = '0') and
474
              (spliten = 0 or ahbsi.hmaster = r.splmst or r.insplit = '0') and
475
              ahbsi.hmbsel(FLASH_BANK) = '1' and
476
              (((ahbsi.hsel(hindex) and ahbsi.hready and ahbsi.htrans(1)) = '1') or
477
               ((spliten = 0 or r.insplit = '0') and r.hready = '0' and r.hresp = HRESP_OKAY))) then
478
            v.spimstate := IDLE;
479
            v.hresp := HRESP_OKAY;
480
            if spliten /= 0 then
481
              v.insplit := '0';
482
              v.hsplit := r.hsplit;
483
            end if;
484
            v.hready := '1';
485
            v.hsel := '0';
486
            if r.spio.errorn = '0' then
487
              v.hready := '0';
488
              v.hresp := HRESP_ERROR;
489
            end if;
490
          elsif spliten /= 0 and v.ahbcancel = '1' then
491
            v.spimstate := IDLE;
492
            v.ahbcancel := '0';
493
          end if;
494
        end if;
495
 
496
      when USER_SPI =>
497
        if bytedone = '1' then
498
          v.spimstate := IDLE;
499
          v.reg.stat.done:= '1';
500
          v.irq := r.reg.ctrl.ien;
501
          v.hold := '1';
502
        end if;
503
 
504
      when others => -- IDLE
505
        if spliten /= 0 and r.hresp /= HRESP_SPLIT then
506
          hsplit := r.hsplit;
507
          v.hsplit := (others => '0');
508
        end if;
509
        v.reg.stat.busy := '0';
510
        if r.hsel = '1' then
511
          if r.hmbsel(FLASH_BANK) = '1' then
512
            -- Access to memory mapped flash area
513
            v.spimstate := AHB_RESPOND;
514
            read_flash := true;
515
          elsif regaddr = TX_REG_OFF and (r.hwrite and r.reg.ctrl.usrc) = '1' then
516
            -- Access to core transmit register
517
            v.spimstate := USER_SPI;
518
            v.go := '1';
519
            v.stop := '1';
520
            change := '1';
521
            v.hold := '0';
522
          end if;
523
        end if;
524
    end case;
525
 
526
    ---------------------------------------------------------------------------
527
    -- SD Card specific code
528
    ---------------------------------------------------------------------------
529
    -- SD card initialization sequence:
530
    -- * Check if card is present
531
    -- * Perform power-up initialization sequence
532
    -- * Issue CMD0   GO_IDLE_STATE
533
    -- * Issue CMD55  APP_CMD
534
    -- * Issue ACMD41 SEND_OP_COND
535
    -- * Issue CMD16  SET_BLOCKLEN
536
    if sdcard = 1 then
537
      case r.sd.state is
538
        when SD_PWRUP0 =>
539
          v.go := '1';
540
          v.sd.vresp := '1';
541
          v.sd.state := SD_GET_RESP;
542
          v.sd.rstate := SD_PWRUP1;
543
          v.sd.rcnt := cslv(2, r.sd.rcnt'length);
544
 
545
        when SD_PWRUP1 =>
546
          v.sd.state := SD_SEND_CMD;
547
          v.sd.rstate := SD_INIT_IDLE;
548
          v.sd.cmd := SD_CMD0;
549
          v.sd.rcnt := (others => '0');
550
          v.ar := (others => '0');
551
 
552
        when SD_INIT_IDLE =>
553
          v.sd.state := SD_SEND_CMD;
554
          v.sd.rcnt := (others => '0');
555
          if r.ar(0) = '0' and r.sd.cmd /= SD_CMD0 then
556
            v.sd.cmd := SD_CMD16;
557
            v.ar := SD_BLOCKLEN;
558
            v.sd.rstate := SD_CHECK_CMD16;
559
          else
560
            v.sd.cmd := SD_CMD55;
561
            v.ar := (others => '0');
562
            v.sd.rstate := SD_ISS_ACMD41;
563
          end if;
564
 
565
        when SD_ISS_ACMD41 =>
566
          v.sd.state := SD_SEND_CMD;
567
          v.sd.cmd := SD_ACMD41;
568
          v.sd.rcnt := (others => '0');
569
          v.ar := (others => '0');
570
          v.sd.rstate := SD_INIT_IDLE;
571
 
572
        when SD_CHECK_CMD16 =>
573
          if r.ar(7 downto 0) /= zero32(7 downto 0) then
574
            v.spio.errorn := '0';
575
          else
576
            v.spio.errorn := '1';
577
            v.spio.initialized := '1';
578
            v.sd.timeout := '0';
579
          end if;
580
          v.sd.state := SD_READY;
581
 
582
        when SD_READY =>
583
          v.spio.ready := '1';
584
          v.sd.cmd := SD_CMD17;
585
          v.sd.rstate := SD_CHECK_CMD17;
586
          if read_flash then
587
            v.sd.state := SD_SEND_CMD;
588
            v.spio.ready := '0';
589
            v.ar := (others => '0');
590
            v.ar(r.haddr'left downto 2) := r.haddr(r.haddr'left downto 2);
591
          end if;
592
 
593
        when SD_CHECK_CMD17 =>
594
          if r.ar(7 downto 0) /= X"00" then
595
            v.sd.state := SD_READY;
596
            v.spio.errorn := '0';
597
          else
598
            v.sd.rstate := SD_CHECK_TOKEN;
599
            v.spio.csn := '0';
600
            v.go := '1';
601
            change := '1';
602
          end if;
603
          v.sd.dtocnt := cslv(SD_DATATOK_TIMEOUT, r.sd.dtocnt'length);
604
          v.sd.state := SD_GET_RESP;
605
          v.sd.vresp := '1';
606
          v.hold := '0';
607
 
608
        when SD_CHECK_TOKEN =>
609
          if (r.ar(7 downto 5) = "111" and
610
              r.sd.dtocnt /= zero32(r.sd.dtocnt'range)) then
611
            v.sd.dtocnt := r.sd.dtocnt - 1;
612
            v.sd.state := SD_GET_RESP;
613
            if r.ar(0) = '0' then
614
              v.sd.rstate := SD_HANDLE_DATA;
615
              v.sd.rcnt := cslv(SD_BLEN-1, r.sd.rcnt'length);
616
            end if;
617
            v.spio.csn := '0';
618
            v.go := '1';
619
            change := '1';
620
          else
621
            v.spio.errorn := '0';
622
            v.sd.state := SD_READY;
623
          end if;
624
          v.sd.timeout := not orv(r.sd.dtocnt);
625
          v.sd.ctocnt := cslv(SD_CMD_TIMEOUT, r.sd.ctocnt'length);
626
          v.hold := '0';
627
 
628
        when SD_HANDLE_DATA =>
629
          v.frdata := r.ar;
630
          -- Receive and discard CRC
631
          v.sd.state := SD_GET_RESP;
632
          v.sd.rstate := SD_READY;
633
          v.sd.htb := '1';
634
          v.spio.csn := '0';
635
          v.go := '1';
636
          change := '1';
637
          v.sd.vresp := '1';
638
          v.spio.errorn := '1';
639
 
640
        when SD_SEND_CMD =>
641
          v.sd.htb := '1';
642
          v.sd.vresp := '0';
643
          v.spio.csn := '0';
644
          v.sd.ctocnt := cslv(SD_CMD_TIMEOUT, r.sd.ctocnt'length);
645
          if (bytedone or not r.go) = '1'then
646
            v.hold := '0';
647
            case r.sd.tcnt is
648
              when "000" => v.sreg := "01" & r.sd.cmd;
649
                            v.hold := '1'; change := '1';
650
              when "001" => v.sreg := r.ar(31 downto 24);
651
              when "010" => v.sreg := r.ar(30 downto 23);
652
              when "011" => v.sreg := r.ar(30 downto 23);
653
              when "100" => v.sreg := r.ar(30 downto 23);
654
              when "101" => v.sreg := SD_CRC_BYTE;
655
              when others => v.sd.state := SD_GET_RESP;
656
            end case;
657
            v.go := '1';
658
            v.sd.tcnt := r.sd.tcnt + 1;
659
          end if;
660
 
661
        when SD_GET_RESP =>
662
          if bytedone = '1' then
663
            if r.sd.vresp = '1' or r.sd.ctocnt = zero32(r.sd.ctocnt'range) then
664
              if r.sd.rcnt = zero32(r.sd.rcnt'range) then
665
                if r.sd.htb = '0' then
666
                  v.spio.csn := '1';
667
                end if;
668
                v.sd.htb := '0';
669
                v.hold := '1';
670
              else
671
                v.sd.rcnt := r.sd.rcnt - 1;
672
              end if;
673
            else
674
              v.sd.ctocnt := r.sd.ctocnt - 1;
675
            end if;
676
          end if;
677
          if lastbit = '1' then
678
            v.sd.vresp := r.sd.vresp or not r.ar(6);
679
            if r.sd.rcnt = zero32(r.sd.rcnt'range) then
680
              v.stop := r.sd.vresp and r.go and not r.sd.htb;
681
            end if;
682
          end if;
683
          if r.sd.ctocnt = zero32(r.sd.ctocnt'range) then
684
            v.stop := r.go;
685
          end if;
686
          if (r.go or r.spio.sck) = '0' then
687
            v.sd.state := r.sd.rstate;
688
            if r.sd.ctocnt = zero32(r.sd.ctocnt'range) then
689
              if r.spio.initialized = '1' then
690
                v.sd.state := SD_READY;
691
              else
692
                -- Try to initialize again
693
                v.sd.state := SD_CHECK_PRES;
694
              end if;
695
              v.spio.errorn := '0';
696
              v.sd.timeout := '1';
697
            end if;
698
            v.spio.csn := '1';
699
          end if;
700
          v.sd.tcnt := (others => '0');
701
 
702
        when others => -- SD_CHECK_PRES
703
          if r.sd.cd = '1' then
704
            v.go := '1';
705
            v.spio.csn := '0';
706
            v.sd.state := SD_GET_RESP;
707
            v.spio.cdcsnoen := OUTPUT;
708
          end if;
709
          v.sd.htb := '0';
710
          v.sd.vresp := '1';
711
          v.sd.rstate := SD_PWRUP0;
712
          v.sd.rcnt := cslv(10, r.sd.rcnt'length);
713
          v.sd.ctocnt := cslv(SD_CMD_TIMEOUT, r.sd.ctocnt'length);
714
      end case;
715
    end if;
716
 
717
    ---------------------------------------------------------------------------
718
    -- SPI Flash (non SD) specific code
719
    ---------------------------------------------------------------------------
720
    if sdcard = 0 then
721
      case r.spi.state is
722
        when SPI_READ =>
723
          if r.go = '0' then
724
            v.go := '1';
725
            change := '1';
726
          end if;
727
          v.hold := '1';
728
          v.spi.cnt := cslv(SPI_ARG_LEN, r.spi.cnt'length);
729
          if bytedone = '1' then
730
            v.spi.state := SPI_ADDR;
731
            v.sreg := r.ar(23 downto 16);
732
            v.hold := '0';
733
          end if;
734
 
735
        when SPI_ADDR =>
736
          if bytedone = '1' then
737
            v.hold := '0';
738
            v.sreg := r.ar(22 downto 15);
739
            if r.spi.cnt = zero32(r.spi.cnt'range) then
740
              v.spi.state := SPI_DATA;
741
              if dualoutput = 1 then
742
                v.spio.mosioen := INPUT;
743
              end if;
744
              if r.spi.hsize = SPI_HSIZE_WORD then
745
                v.spi.cnt := (others => '1');
746
              else
747
                v.spi.cnt := r.spi.hsize;
748
              end if;
749
            else
750
              v.spi.cnt := r.spi.cnt - 1;
751
            end if;
752
          end if;
753
 
754
        when SPI_DATA =>
755
          if bytedone = '1' then
756
            v.spi.cnt := r.spi.cnt - 1;
757
          end if;
758
          if lastbit = '1' and r.spi.cnt = zero32(r.spi.cnt'range) then
759
            v.stop := r.go;
760
          end if;
761
          if (r.go or r.spio.sck) = '0' then
762
            if dualoutput = 1 then
763
              v.spio.mosioen := OUTPUT;
764
            end if;
765
            v.spio.csn := '1';
766
            v.spi.state := SPI_READY;
767
            -- Need to clear MSB in bcnt here since dualoutput may leave it at
768
            -- '1' and thereby making the next command short. 
769
            if dualoutput = 1 then
770
              v.bcnt(2) := '0';
771
            end if;
772
          end if;
773
 
774
        when SPI_READY =>
775
          v.spio.ready := '1';
776
          if read_flash then
777
            v.spi.state := SPI_READ;
778
            v.ar := (others => '0');
779
            v.ar(r.haddr'range) := r.haddr;
780
            v.spio.ready := '0';
781
            v.spio.csn := '0';
782
            v.sreg := cslv(readcmd, 8);
783
          end if;
784
          if r.spio.ready = '0' then
785
            case r.spi.hsize is
786
              when SPI_HSIZE_BYTE =>
787
                v.frdata := (r.ar(7 downto 0) & r.ar(7 downto 0) &
788
                             r.ar(7 downto 0) & r.ar(7 downto 0));
789
              when SPI_HSIZE_HWORD =>
790
                v.frdata := r.ar(15 downto 0) & r.ar(15 downto 0);
791
              when others =>
792
                v.frdata := r.ar;
793
            end case;
794
          end if;
795
          v.spi.hsize := r.hsize;
796
 
797
        when others => -- SPI_PWRUP
798
          if pwrupcnt /= 0 then
799
            v.frdata := r.frdata - 1;
800
            if r.frdata = zero32 then
801
              v.spio.initialized := '1';
802
              v.spi.state := SPI_READY;
803
            end if;
804
          else
805
            v.spio.initialized := '1';
806
            v.spi.state := SPI_READY;
807
          end if;
808
      end case;
809
    end if;
810
 
811
    ---------------------------------------------------------------------------
812
    -- SPI communication
813
    ---------------------------------------------------------------------------
814
    -- Clock generation
815
    if (r.go or r.spio.sck) = '1' then
816
      v.timer := r.timer - 1;
817
      if sck_toggle(v.timer, r.timer, enable_altscaler) then
818
        v.spio.sck := not r.spio.sck;
819
        v.sample := not r.spio.sck;
820
        change := r.spio.sck and r.go;
821
        if (v.stop and lastbit and not r.spio.sck) = '1' then
822
          v.go := '0';
823
          v.stop := '0';
824
        end if;
825
      end if;
826
    else
827
      v.timer := (others => '1');
828
    end if;
829
 
830
    if r.sample = '1' then
831
      if r.hold = '0' then
832
        if dualoutput = 1 and r.spio.mosioen = INPUT then
833
          v.ar := r.ar(29 downto 0) & r.spii.miso & r.spii.mosi;
834
        else
835
          v.ar := r.ar(30 downto 0) & r.spii.miso;
836
        end if;
837
      end if;
838
      v.bcnt := r.bcnt + 1;
839
    end if;
840
 
841
    if change = '1' then
842
      v.spio.mosi := v.sreg(7);
843
      v.sreg(7 downto 0) := v.sreg(6 downto 0) & '1';
844
    end if;
845
 
846
    ---------------------------------------------------------------------------
847
    -- System and core reset
848
    ---------------------------------------------------------------------------
849
    if (not rstn or r.rst) = '1' then
850
      if sdcard = 1 then
851
        v.sd.state := SD_CHECK_PRES;
852
        v.spio.cdcsnoen := INPUT;
853
        v.sd.timeout := '0';
854
      else
855
        v.spi.state := SPI_PWRUP;
856
        v.frdata := cslv(pwrupcnt, r.frdata'length);
857
        v.spio.cdcsnoen := OUTPUT;
858
      end if;
859
      v.spimstate        := IDLE;
860
      v.rst              := '0';
861
      --
862
      v.reg.ctrl         := ('0', '0', '0');
863
      v.reg.stat.done    := '0';
864
      --
865
      v.sample           := '0';
866
      v.sreg             := (others => '1');
867
      v.bcnt             := (others => '0');
868
      v.go               := '0';
869
      v.stop             := '0';
870
      v.hold             := '0';
871
      --
872
      v.hready           := '1';
873
      v.hwrite           := '0';
874
      v.hsel             := '0';
875
      v.hmbsel           := (others => '0');
876
      v.ahbcancel        := '0';
877
      --
878
      v.spio.sck         := '0';
879
      v.spio.mosi        := '1';
880
      v.spio.mosioen     := OUTPUT;
881
      v.spio.csn         := '1';
882
      v.spio.errorn      := '1';
883
      v.spio.initialized := '0';
884
      v.spio.ready       := '0';
885
    end if;
886
 
887
    ---------------------------------------------------------------------------
888
    -- Drive unused signals
889
    ---------------------------------------------------------------------------
890
    if sdcard = 1 then
891
      v.spi.state  := SPI_PWRUP;
892
      v.spi.cnt    := (others => '0');
893
      v.spi.hsize  := (others => '0');
894
    else
895
      v.sd.state   := SD_CHECK_PRES;
896
      v.sd.tcnt    := (others => '0');
897
      v.sd.rcnt    := (others => '0');
898
      v.sd.cmd     := (others => '0');
899
      v.sd.rstate  := SD_CHECK_PRES;
900
      v.sd.htb     := '0';
901
      v.sd.vresp   := '0';
902
      v.sd.timeout := '0';
903
      v.sd.dtocnt  := (others => '0');
904
      v.sd.ctocnt  := (others => '0');
905
    end if;
906
    if spliten = 0 then
907
      v.insplit   := '0';
908
      v.unsplit   := '0';
909
      v.splmst    := (others => '0');
910
      v.hsplit    := (others => '0');
911
      v.ahbcancel := '0';
912
    end if;
913
 
914
    ---------------------------------------------------------------------------
915
    -- Signal assignments
916
    ---------------------------------------------------------------------------    
917
    -- Core registers
918
    rin <= v;
919
 
920
    -- AHB slave output
921
    ahbso.hready  <= r.hready;
922
    ahbso.hresp   <= r.hresp;
923
    if r.hmbsel(CTRL_BANK) = '1' then
924
      ahbso.hrdata <= zero32(31 downto 8) & r.rrdata;
925
    else
926
      ahbso.hrdata <= r.frdata;
927
    end if;
928
    ahbso.hconfig <= HCONFIG;
929
    ahbso.hcache  <= '0';
930
    ahbso.hirq    <= ahbirq;
931
    ahbso.hindex  <= hindex;
932
    ahbso.hsplit  <= hsplit;
933
 
934
    -- SPI signals
935
    spio <= r.spio;
936
  end process comb;
937
 
938
  reg: process (clk)
939
  begin  -- process reg
940
    if rising_edge(clk) then
941
      r <= rin;
942
    end if;
943
  end process reg;
944
 
945
  -- Boot message
946
  -- pragma translate_off
947
  bootmsg : report_version
948
    generic map (
949
      "spimctrl" & tost(hindex) & ": SPI memory controller rev " &
950
      tost(REVISION) & ", irq " & tost(hirq));
951
  -- pragma translate_on
952
 
953
end rtl;

powered by: WebSVN 2.1.0

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