OpenCores
URL https://opencores.org/ocsvn/sdhc-sc-core/sdhc-sc-core/trunk

Subversion Repositories sdhc-sc-core

[/] [sdhc-sc-core/] [trunk/] [grpSd/] [pkgSd/] [src/] [Sd-p.vhdl] - Blame information for rev 185

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 164 rkastl
-- SDHC-SC-Core
2
-- Secure Digital High Capacity Self Configuring Core
3
-- 
4 170 rkastl
-- (C) Copyright 2010, Rainer Kastl
5
-- All rights reserved.
6 164 rkastl
-- 
7 170 rkastl
-- Redistribution and use in source and binary forms, with or without
8
-- modification, are permitted provided that the following conditions are met:
9
--     * Redistributions of source code must retain the above copyright
10
--       notice, this list of conditions and the following disclaimer.
11
--     * Redistributions in binary form must reproduce the above copyright
12
--       notice, this list of conditions and the following disclaimer in the
13
--       documentation and/or other materials provided with the distribution.
14
--     * Neither the name of the <organization> nor the
15
--       names of its contributors may be used to endorse or promote products
16
--       derived from this software without specific prior written permission.
17 164 rkastl
-- 
18 170 rkastl
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  "AS IS" AND
19
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
-- DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 164 rkastl
-- 
29
-- File        : Sd-p.vhdl
30
-- Owner       : Rainer Kastl
31
-- Description : Definitions for SD cards and controllers (Spec 2.0)
32
-- Links       : 
33
-- 
34 29 rkastl
 
35
library ieee;
36
use ieee.std_logic_1164.all;
37
use ieee.numeric_std.all;
38 62 rkastl
use work.Global.all;
39 29 rkastl
 
40
package Sd is
41
 
42 54 rkastl
        -- CMD transfer: constants
43 88 rkastl
        constant cSdStartBit      : std_ulogic := '0';
44
        constant cSdEndBit        : std_ulogic := '1';
45
        constant cSdTransBitHost  : std_ulogic := '1';
46 29 rkastl
        constant cSdTransBitSlave : std_ulogic := '0';
47
 
48 54 rkastl
        -- CMD transfer: types
49 29 rkastl
        constant cSdCmdIdHigh : natural := 6;
50
        subtype aSdCmdId is std_ulogic_vector(cSdCmdIdHigh-1 downto 0);
51 34 rkastl
        subtype aSdCmdArg is std_ulogic_vector(31 downto 0);
52 29 rkastl
 
53 34 rkastl
        type aSdCmdContent is record
54 88 rkastl
                id  : aSdCmdId;
55 34 rkastl
                arg : aSdCmdArg;
56
        end record aSdCmdContent;
57
 
58 62 rkastl
        constant cDefaultSdCmdContent : aSdCmdContent := (
59 88 rkastl
        id  => (others => '0'),
60 62 rkastl
        arg => (others => '0'));
61
 
62 34 rkastl
        type aSdCmdToken is record
63 29 rkastl
                startbit : std_ulogic; -- cSdStartBit
64
                transbit : std_ulogic;
65 88 rkastl
                content  : aSdCmdContent;
66
                crc7     : std_ulogic_vector(6 downto 0); -- CRC of content
67
                endbit   : std_ulogic; --cSdEndBit
68 34 rkastl
        end record aSdCmdToken;
69 56 rkastl
 
70 83 rkastl
        constant cDefaultSdCmdToken : aSdCmdToken := (
71
        startbit => cActivated,
72
        transbit => cActivated,
73
        content  => cDefaultSdCmdContent,
74
        crc7     => (others => '0'),
75
        endbit   => cInactivated);
76
 
77 62 rkastl
        -- SD Card Regs
78
        subtype aVoltageWindow is std_ulogic_vector(23 downto 15);
79
        constant cVoltageWindow : aVoltageWindow := (others => '1');
80 88 rkastl
        constant cSdR3Id        : aSdCmdId       := (others => '1');
81
        constant cSdR2Id        : aSdCmdId       := (others => '1');
82 62 rkastl
 
83
        type aSdRegOCR is record
84
                voltagewindow : aVoltageWindow;
85 88 rkastl
                ccs           : std_ulogic;
86
                nBusy         : std_ulogic;
87 62 rkastl
        end record aSdRegOCR;
88 88 rkastl
 
89
        subtype aSdCardStatus is std_ulogic_vector(31 downto 0);
90 98 rkastl
        constant cSdStatusReadyForDataBit : natural := 8;
91 88 rkastl
        constant cDefaultSdCardStatus : aSdCardStatus := (others => '0');
92 75 rkastl
 
93
        constant cSdOCRQuery : aSdRegOCR := (
94
        voltagewindow => (others => '0'),
95 88 rkastl
        ccs           => '0',
96
        nBusy         => '0');
97 62 rkastl
 
98
        function OCRToArg (ocr : in aSdRegOCR) return aSdCmdArg;
99
        function ArgToOcr (arg : in aSdCmdArg) return aSdRegOCR;
100
 
101
        subtype aSdCIDMID is std_ulogic_vector(7 downto 0);
102
        subtype aSdCIDOID is std_ulogic_vector(15 downto 0);
103
        subtype aSdCIDPNM is std_ulogic_vector(39 downto 0);
104
        subtype aSdCIDPRV is std_ulogic_vector(7 downto 0);
105
        subtype aSdCIDPSN is std_ulogic_vector(31 downto 0);
106
        subtype aSdCIDMDT is std_ulogic_vector(11 downto 0);
107
 
108
        type aSdRegCID is record
109 88 rkastl
                mid          : aSdCIDMID;
110
                oid          : aSdCIDOID;
111
                name         : aSdCIDPNM;
112
                revision     : aSdCIDPRV;
113 62 rkastl
                serialnumber : aSdCIDPSN;
114 88 rkastl
                date         : aSdCIDMDT;
115 62 rkastl
        end record;
116
 
117
        constant cCIDLength : natural := 127;
118
 
119
        constant cDefaultSdRegCID : aSdRegCID := (
120 83 rkastl
        mid          => (others => '0'),
121
        oid          => (others => '0'),
122
        name         => (others => '0'),
123
        revision     => (others => '0'),
124 62 rkastl
        serialnumber => (others => '0'),
125 83 rkastl
        date         => (others => '0'));
126 62 rkastl
 
127
        function UpdateCID(icid : in aSdRegCID; data : in std_ulogic; pos : in
128
        natural) return aSdRegCID;
129
 
130 63 rkastl
        subtype aSdRCA is std_ulogic_vector(15 downto 0);
131
        constant cDefaultRCA : aSdRCA := (others => '0');
132
 
133 109 rkastl
        constant cSdWideModeBit : natural := 31-(63-50); -- first word
134 102 rkastl
 
135 92 rkastl
        -- Data types
136 132 rkastl
        subtype aSdData is std_ulogic_vector(3 downto 0);
137
        constant cSdStartBits : aSdData := (others => cSdStartBit);
138
        constant cSdEndBits   : aSdData := (others => cSdEndBit);
139
 
140 92 rkastl
        constant cBlocklen : natural := 512 * 8; -- 512 bytes
141
        subtype aSdDataBlock is std_ulogic_vector(cBlocklen - 1 downto 0);
142
 
143
        type aSdDataBusMode is (standard, wide);
144 100 rkastl
        type aSdDataMode is (usual, widewidth);
145 103 rkastl
        type aSdDataBits is (ScrBits, SwitchFunctionBits);
146 132 rkastl
 
147 134 rkastl
        constant cScrBitsCount             : natural := 4096 - 64 + 7; -- expressed in bits
148
        constant cSwitchFunctionBitsCount  : natural := 4096 - 512 + 7; -- expressed in bits
149 133 rkastl
        constant cWideModeBitAddr          : natural := 50;
150
        constant cHighSpeedBitAddr                 : natural := 401;
151
        constant cSwitchFunctionBitLowAddr : natural := 376;
152 92 rkastl
 
153 54 rkastl
        -- Types for entities
154 92 rkastl
        -- between SdController and SdCmd
155 54 rkastl
        type aSdCmdFromController is record
156 92 rkastl
                Content   : aSdCmdContent; -- id and arg to sent to card
157
                Valid     : std_ulogic; -- gets asserted when Content is valid and can be sent to card
158 62 rkastl
                ExpectCID : std_ulogic; -- gets asserted when next response is R2
159 92 rkastl
                CheckCrc  : std_ulogic; -- gets asserted when CRC has to be checked (exceptions is R3)
160 54 rkastl
        end record aSdCmdFromController;
161 29 rkastl
 
162 54 rkastl
        type aSdCmdToController is record
163 92 rkastl
                Ack       : std_ulogic; -- Gets asserted when crc was sent, but endbit was not. This way we can minimize the wait time between sending 2 cmds.
164
                Receiving : std_ulogic; -- gets asserted when a response is received currently
165
                Content   : aSdCmdContent; -- received id and arg, see valid
166
                Valid     : std_ulogic; -- gets asserted when CmdContent is valid (therefore  a cmd was received and can be saved)
167
                Err       : std_ulogic; -- gets asserted when an error occurred during receiving a cmd, for example the crc check does not hold
168
                Cid       : aSdRegCID; -- received CID register of the card, see valid
169 54 rkastl
        end record aSdCmdToController;
170
 
171 62 rkastl
        constant cDefaultSdCmdToController : aSdCmdToController := (
172 88 rkastl
        Ack       => cInactivated,
173 62 rkastl
        Receiving => cInactivated,
174 88 rkastl
        Valid     => cInactivated,
175
        Content   => cDefaultSdCmdContent,
176
        Err       => cInactivated,
177
        Cid       => cDefaultSdRegCID);
178 62 rkastl
 
179 109 rkastl
        constant cDataRamAddrWidth : natural := 7;
180
        constant cDataRamDataWidth : natural := 32;
181
        subtype aWord is std_ulogic_vector(cDataRamDataWidth - 1 downto 0);
182
        subtype aAddr is natural range 0 to 2**cDataRamAddrWidth - 1;
183
 
184
        type aSdControllerToRam is record
185
                Addr : aAddr;
186
        end record aSdControllerToRam;
187
 
188
        constant cDefaultSdControllerToRam : aSdControllerToRam := (Addr => 0);
189
 
190
        type aSdControllerFromRam is record
191
                Data : aWord;
192
        end record aSdControllerFromRam;
193
 
194 92 rkastl
        -- between SdController and SdData
195
        type aSdDataFromController is record
196 101 rkastl
                Mode       : aSdDataBusMode; -- select 1 bit or 4 bit mode
197
                DataMode   : aSdDataMode; -- select usual or wide width data
198
                ExpectBits : aSdDataBits; -- how many bits are expected in wide with data mode
199
                Valid      : std_ulogic; -- valid, when the datablock is valid and has to be sent
200
                CheckBusy  : std_ulogic; -- check for busy signaling
201 157 rkastl
                DisableRb  : std_ulogic; -- disable read back: do not save read data to fifo
202 92 rkastl
        end record aSdDataFromController;
203
 
204 95 rkastl
        constant cDefaultSdDataFromController : aSdDataFromController := (
205 101 rkastl
        Mode       => standard,
206
        DataMode   => usual,
207
        ExpectBits => ScrBits,
208
        Valid      => cInactivated,
209 157 rkastl
        CheckBusy  => cInactivated,
210
        DisableRb  => cActivated);
211 95 rkastl
 
212 126 rkastl
        type aSpeedBits is record
213
                HighSpeedSupported : std_ulogic;
214
                SwitchFunctionOK   : std_ulogic_vector(3 downto 0); -- 0x01 when ok
215
        end record aSpeedBits;
216
 
217
        constant cDefaultSpeedBits : aSpeedBits := (
218
        HighSpeedSupported => '0',
219
        SwitchFunctionOK   => (others => '0'));
220
 
221 92 rkastl
        type aSdDataToController is record
222
                Ack       : std_ulogic; -- gets asserted when a datablock was sent to the card
223
                Receiving : std_ulogic; -- gets asserted when a datablock is currently received
224
                Valid     : std_ulogic; -- gets asserted when DataBlock is valid and therefore it was received correctly
225
                Busy      : std_ulogic; -- gets asserted when the card returns busy
226
                Err       : std_ulogic; -- gets asserted when an error occurred during receiving a data block (CRC)
227 126 rkastl
                SpeedBits : aSpeedBits; -- gets set, when data for check or switch function cmd is received (DataMode = widewidth and ExpectBits = SwitchFunctionBits
228
                WideMode  : std_ulogic; -- gets set, when scr is read (datamode = widewidth and ExpectBits = ScrBits)
229 92 rkastl
        end record aSdDataToController;
230
 
231
        constant cDefaultSdDataToController : aSdDataToController := (
232
        Ack       => cInactivated,
233
        Receiving => cInactivated,
234
        Valid     => cInactivated,
235
        Busy      => cInactivated,
236 126 rkastl
        Err       => cInactivated,
237
        SpeedBits => cDefaultSpeedBits,
238
        WideMode  => cInactivated);
239 92 rkastl
 
240 109 rkastl
        -- SdData to Ram
241
        type aSdDataToRam is record
242
                En   : std_ulogic;
243
                Addr : aAddr;
244
                Data : aWord;
245
                We   : std_ulogic;
246
        end record aSdDataToRam;
247
 
248
        constant cDefaultSdDataToRam : aSdDataToRam := (
249
        En   => cInactivated,
250
        Addr => 0,
251
        Data => (others => '0'),
252
        We   => cInactivated);
253
 
254
        type aSdDataFromRam is record
255
                Data : aWord;
256
        end record aSdDataFromRam;
257
 
258 92 rkastl
        -- between SdController and wishbone interface
259 88 rkastl
        type aSdRegisters is record
260
                CardStatus : aSdCardStatus;
261
        end record aSdRegisters;
262
 
263 92 rkastl
        -- constants for SdController
264 57 rkastl
        subtype aRCA is std_ulogic_vector(15 downto 0);
265
        constant cSdDefaultRCA : aRCA := (others => '0');
266
 
267 29 rkastl
        -- command ids
268
        -- abbreviations:
269
        -- RCA: relative card address
270
 
271
        constant cSdCmdGoIdleState : aSdCmdId := std_ulogic_vector(to_unsigned(0,
272
        cSdCmdIdHigh)); -- no args
273
 
274
        constant cSdCmdAllSendCID : aSdCmdId := std_ulogic_vector(to_unsigned(2,
275
        cSdCmdIdHigh)); -- no args
276
 
277
        constant cSdCmdSendRelAdr : aSdCmdId := std_ulogic_vector(to_unsigned(3,
278
        cSdCmdIdHigh)); -- no args
279
 
280
        constant cSdCmdSetDSR : aSdCmdId := std_ulogic_vector(to_unsigned(4,
281
        cSdCmdIdHigh)); -- [31:16] DSR
282
 
283
        constant cSdCmdSelCard : aSdCmdId := std_ulogic_vector(to_unsigned(7,
284
        cSdCmdIdHigh)); -- [31:16] RCA
285
 
286
        constant cSdCmdDeselCard : aSdCmdId := cSdCmdSelCard; -- [31:16] RCA
287
 
288
        constant cSdCmdSendIfCond : aSdCmdId := std_ulogic_vector(to_unsigned(8,
289 54 rkastl
        cSdCmdIdHigh));
290 29 rkastl
 
291 54 rkastl
        constant cSdDefaultVoltage : std_ulogic_vector(3 downto 0) := "0001"; -- 2.7
292
        -- - 3.6 V
293
        constant cCheckpattern : std_ulogic_vector(7 downto 0) := "10101010";
294
        -- recommended
295
 
296
        constant cSdArgVoltage : aSdCmdArg :=
297
                "00000000000000000000" & -- reserved 
298
                cSdDefaultVoltage & -- supply voltage
299
                cCheckPattern;
300
 
301 29 rkastl
        constant cSdCmdSendCSD : aSdCmdId := std_ulogic_vector(to_unsigned(9,
302
        cSdCmdIdHigh)); -- [31:16] RCA
303
 
304
        constant cSdCmdSendCID : aSdCmdId := std_ulogic_vector(to_unsigned(10,
305
        cSdCmdIdHigh)); -- [31:16] RCA
306
 
307
        constant cSdCmdStopTrans : aSdCmdId := std_ulogic_vector(to_unsigned(12,
308
        cSdCmdIdHigh)); -- no args
309
 
310
        constant cSdCmdSendStatus : aSdCmdId := std_ulogic_vector(to_unsigned(13,
311
        cSdCmdIdHigh)); -- [31:16] RCA
312
 
313 57 rkastl
        constant cSdNextIsACMD : aSdCmdId := std_ulogic_vector(to_unsigned(55,
314
        cSdCmdIdHigh));
315
        constant cSdACMDArg : aSdCmdArg := cSdDefaultRCA & X"0000"; -- [31:16] RCA
316
        constant cSdArgAppCmdPos : natural := 5;
317
 
318 113 rkastl
        constant cSdCmdReadSingleBlock : aSdCmdId := std_ulogic_vector(to_unsigned(17, cSdCmdIdHigh));
319 123 rkastl
        constant cSdCmdWriteSingleBlock : aSdCmdId := std_ulogic_vector(to_unsigned(24, cSdCmdIdHigh));
320 113 rkastl
 
321 57 rkastl
        constant cSdCmdACMD41 : aSdCmdId := std_ulogic_vector(to_unsigned(41, cSdCmdIdHigh));
322 99 rkastl
        constant cSdCmdSendSCR : aSdCmdId := std_ulogic_vector(to_unsigned(51, cSdCmdIdHigh));
323 102 rkastl
        constant cSdCmdSetBusWidth : aSdCmdId := std_ulogic_vector(to_unsigned(6, cSdCmdIdHigh)); -- [31:2] stuff, [1:0] bus width
324
        constant cSdWideBusWidth : std_ulogic_vector(1 downto 0) := "10";
325
        constant cSdStandardBusWidth : std_ulogic_vector(1 downto 0) := "00";
326 60 rkastl
 
327 103 rkastl
        constant cSdCmdSwitchFunction : aSdCmdId := std_ulogic_vector(to_unsigned(6, cSdCmdIdHigh));
328
        -- 31: mode
329
        constant cSdCmdCheckMode : std_ulogic := '0';
330
        constant cSdCmdSwitchMode : std_ulogic := '1';
331
        -- 30:24 reserved
332
        -- 23:8 function group 6 - 3
333
        -- 7:4 group 2 for command system
334
        -- 3:0 group 1 for access mode
335 104 rkastl
        constant cSdCmdCheckSpeedSupport        : aSdCmdArg := X"00FFFFF1";
336
        constant cSdCmdSwitchSpeed              : aSdCmdArg := X"80FFFFF1";
337 126 rkastl
        constant cSdHighSpeedFunctionSupportBit : natural   := 401;
338 104 rkastl
        constant cSdHighSpeedFunctionGroupLow   : natural   := 376;
339 103 rkastl
 
340 111 rkastl
        type aiSdCmd is record
341
                Cmd : std_ulogic;
342
        end record aiSdCmd;
343
 
344
        type aoSdCmd is record
345
                Cmd : std_ulogic;
346
                En : std_ulogic;
347
        end record aoSdCmd;
348
 
349 132 rkastl
 
350 111 rkastl
        type aiSdData is record
351 132 rkastl
                Data : aSdData;
352 111 rkastl
        end record aiSdData;
353
 
354
        type aoSdData is record
355 132 rkastl
                Data : aSdData;
356
                En   : aSdData;
357 111 rkastl
        end record aoSdData;
358 132 rkastl
 
359
        constant cDefaultSdData : aoSdData := (
360
        Data => (others => '0'),
361
        En   => (others => '0'));
362 126 rkastl
 
363 29 rkastl
end package Sd;
364
 
365 60 rkastl
package body Sd is
366
 
367
        function OCRToArg (ocr : in aSdRegOCR) return aSdCmdArg is
368
                variable temp : aSdCmdArg;
369
        begin
370
                temp := ocr.nBusy & ocr.ccs & "000000" & ocr.voltagewindow &
371
                "000000000000000";
372
                return temp;
373
        end function OCRtoArg;
374
 
375
        function ArgToOcr (arg : in aSdCmdArg) return aSdRegOCR is
376
                variable ocr : aSdRegOCR;
377
        begin
378
                ocr.nBusy := arg(31);
379
                ocr.ccs := arg(30);
380
                ocr.voltagewindow := arg(23 downto 15);
381
                return ocr;
382
        end function ArgToOcr;
383
 
384 62 rkastl
        function UpdateCID(icid : in aSdRegCID; data : in std_ulogic; pos : in
385
        natural) return aSdRegCID is
386
                variable cid : aSdRegCID;
387
        begin
388
                cid := icid;
389
 
390
                if (pos <= 127 and pos >= 120) then
391
                        cid.mid(pos-120) := data;
392
                elsif (pos <= 119 and pos >= 104) then
393
                        cid.oid(pos-104) := data;
394
                elsif (pos <= 103 and pos >= 64) then
395
                        cid.name(pos-64) := data;
396
                elsif (pos <= 63 and pos >= 56) then
397
                        cid.revision(pos-56) := data;
398
                elsif (pos <= 55 and pos >= 24) then
399
                        cid.serialnumber(pos-24) := data;
400
                elsif (pos <= 19 and pos >= 8) then
401
                        cid.date(pos-8) := data;
402
                end if;
403
 
404
                return cid;
405
        end function UpdateCID;
406
 
407 60 rkastl
end package body Sd;
408
 

powered by: WebSVN 2.1.0

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