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

Subversion Repositories sdhc-sc-core

[/] [sdhc-sc-core/] [trunk/] [grpSd/] [unitSdController/] [src/] [SdController-Rtl-a.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 98 rkastl
-- 
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        : SdController-Rtl-a.vhdl
30
-- Owner       : Rainer Kastl
31
-- Description : Main FSM controlling Cmd and Data FSMs, communicates with Wb
32
-- Links       : 
33
-- 
34 54 rkastl
 
35
architecture Rtl of SdController is
36
 
37 122 rkastl
        type aSdControllerState is (startup, init, config, requestnewoperation, idle, invalidCard, read, write);
38 105 rkastl
        type aCmdRegion is (CMD0, CMD8, ACMD41, CMD2, CMD3, SelectCard, CheckBusWidth, SetBusWidth, CheckSpeed, ChangeSpeed, GetStatus);
39 98 rkastl
        type aRegion is (idle, send, response, waitstate, senddata, receivedata, checkbusy, waitstatedata);
40 105 rkastl
 
41 110 rkastl
        subtype aCounter is natural range 0 to (512/32)-1;
42
 
43 95 rkastl
        constant cDefaultToSdCmd : aSdCmdFromController := (
44 84 rkastl
        (id       => (others        => '0'),
45
        arg       => (others        => '0')),
46
        Valid     => cInactivated,
47 89 rkastl
        ExpectCID => cInactivated,
48
        CheckCrc  => cActivated);
49 54 rkastl
 
50 57 rkastl
        type aSdControllerReg is record
51 122 rkastl
                State          : aSdControllerState;
52
                CmdRegion      : aCmdRegion;
53
                Region         : aRegion;
54
                Counter        : aCounter;
55
                SendCMD55      : std_ulogic;
56
                SentCMD55      : std_ulogic;
57
                HCS            : std_ulogic;
58
                CCS            : std_ulogic;
59
                RCA            : aSdRCA;
60
                CardStatus     : aSdCardStatus;
61
                ToSdCmd        : aSdCmdFromController;
62
                ToSdData       : aSdDataFromController;
63
                ToDataRam      : aSdControllerToRam;
64
                ToSdWbSlave    : aSdControllerToSdWbSlave;
65
                HighSpeed      : std_ulogic;
66
                OperationBlock : aOperationBlock;
67 167 rkastl
                LedBank            : std_ulogic_vector(7 downto 0);
68 57 rkastl
        end record aSdControllerReg;
69
 
70 84 rkastl
        constant cDefaultSdControllerReg : aSdControllerReg := (
71 122 rkastl
        State          => startup,
72
        CmdRegion      => CMD0,
73
        Region         => idle,
74
        Counter        => 0,
75
        SendCMD55      => cInactivated,
76
        SentCMD55      => cInactivated,
77
        HCS            => cActivated,
78
        CCS            => cInactivated,
79
        RCA            => cDefaultRCA,
80
        CardStatus     => cDefaultSdCardStatus,
81
        ToSdCmd        => cDefaultToSdCmd,
82
        ToSdData       => cDefaultSdDataFromController,
83
        ToDataRam      => cDefaultSdControllerToRam,
84
        ToSdWbSlave    => cDefaultSdControllerToSdWbSlave,
85
        HighSpeed      => cInactivated,
86 167 rkastl
        OperationBlock => cDefaultOperationBlock,
87
        LedBank        => (others => '0'));
88 57 rkastl
 
89 100 rkastl
        signal R, NextR       : aSdControllerReg;
90 106 rkastl
 
91
        constant cReadTimeoutNat    : natural := gClkFrequency / (1 sec / gReadTimeout) - 1;
92 167 rkastl
        constant cWriteTimeoutNat   : natural := gClkFrequency / (1 sec / gWriteTimeout) - 1;
93 106 rkastl
        constant cNcrTimeoutNatLow  : natural := gClkFrequency / (1 sec / (1 sec / 25E6 * 8)) - 1;
94
        constant cNcrTimeoutNatHigh : natural := gClkFrequency / (1 sec / (1 sec / 50E6 * 8)) - 1;
95
        constant cStartupTimeoutNat : natural := gClkFrequency / (1 sec / gStartupTimeout) - 1;
96
 
97 167 rkastl
        constant cMaxTimeoutBitWidth : natural := LogDualis(cWriteTimeoutNat);
98 106 rkastl
        subtype aTimeoutValue is unsigned(cMaxTimeoutBitWidth - 1 downto 0);
99
 
100
        constant cNcrTimeoutLow  : aTimeoutValue := to_unsigned(cNcrTimeoutNatLow, aTimeoutValue'length);
101
        constant cNcrTimeoutHigh : aTimeoutValue := to_unsigned(cNcrTimeoutNatHigh, aTimeoutValue'length);
102
        constant cStartupTimeout : aTimeoutValue := to_unsigned(cStartupTimeoutNat, aTimeoutValue'length);
103
        constant cReadTimeout    : aTimeoutValue := to_unsigned(cReadTimeoutNat, aTimeoutValue'length);
104 167 rkastl
        constant cWriteTimeout   : aTimeoutValue := to_unsigned(cWriteTimeoutNat, aTimeoutValue'length);
105 106 rkastl
 
106 100 rkastl
        signal TimeoutEnable  : std_ulogic;
107
        signal TimeoutDisable : std_ulogic;
108
        signal Timeout        : std_ulogic;
109 106 rkastl
        signal TimeoutMax     : unsigned(cMaxTimeoutBitWidth - 1 downto 0);
110
 
111 54 rkastl
begin
112
 
113 110 rkastl
        oSdCmd     <= R.ToSdCmd;
114
        oSdData    <= R.ToSdData;
115 122 rkastl
        oSdWbSlave <= R.ToSdWbSlave;
116 110 rkastl
        oHighSpeed <= R.HighSpeed;
117 167 rkastl
        oLedBank   <= R.LedBank;
118 88 rkastl
 
119 165 rkastl
        Regs : process (iClk, iRstSync)
120 54 rkastl
        begin
121 165 rkastl
                if (iClk'event and iClk = cActivated) then
122
                        if (iRstSync = cActivated) then
123
                                R <= cDefaultSdControllerReg;
124
                        else
125
                                R <= NextR;
126
                        end if;
127 54 rkastl
                end if;
128
        end process Regs;
129
 
130 126 rkastl
        Comb : process (iSdCmd, iSdData, iSdWbSlave, Timeout, R)
131 96 rkastl
                variable ocr           : aSdRegOCR;
132
                variable arg           : aSdCmdArg;
133
                variable NextRegion    : aRegion;
134
                variable NextCmdRegion : aCmdRegion;
135
                variable NextState     : aSdControllerState;
136 106 rkastl
 
137
                procedure EnableNcrTimeout is
138
                begin
139
                        TimeoutEnable <= cActivated;
140
 
141
                        if (R.HighSpeed = cInactivated) then
142
                                TimeoutMax <= cNcrTimeoutLow;
143
                        else
144
                                TimeoutMax <= cNcrTimeoutHigh;
145
                        end if;
146
                end procedure EnableNcrTimeout;
147 110 rkastl
 
148 54 rkastl
        begin
149
                -- default assignments
150 106 rkastl
                NextR          <= R;
151
                NextR.ToSdCmd  <= cDefaultToSdCmd;
152
                TimeoutEnable  <= cInactivated;
153
                TimeoutDisable <= cInactivated;
154
                TimeoutMax     <= to_unsigned(0, TimeoutMax'length);
155
                NextRegion     := R.Region;
156
                NextCmdRegion  := R.CmdRegion;
157
                NextState      := R.State;
158 54 rkastl
 
159 84 rkastl
                case R.State is
160 106 rkastl
                        when startup =>
161 85 rkastl
                                TimeoutEnable <= cActivated;
162 106 rkastl
                                TimeoutMax    <= cStartupTimeout;
163 105 rkastl
 
164 85 rkastl
                                if (Timeout = cActivated) then
165 106 rkastl
                                        TimeoutDisable  <= cActivated;
166
                                        NextR.State     <= init;
167
                                        NextR.CmdRegion <= CMD0;
168
                                        NextR.Region    <= send;
169 88 rkastl
                                end if;
170 85 rkastl
 
171 84 rkastl
                        when init =>
172
                                case R.CmdRegion is
173
                                        when CMD0 =>
174
                                                case R.Region is
175
                                                        when send =>
176 95 rkastl
                                                                NextR.ToSdCmd.Content.id <= cSdCmdGoIdleState;
177 96 rkastl
                                                                NextR.CardStatus         <= cDefaultSdCardStatus;
178
                                                                NextRegion               := waitstate;
179 54 rkastl
 
180 86 rkastl
                                                        when waitstate =>
181 96 rkastl
                                                                NextRegion    := send;
182
                                                                NextCmdRegion := CMD8;
183 85 rkastl
 
184 84 rkastl
                                                        when others =>
185
                                                                report "SdController: Unhandled state" severity error;
186
                                                end case;
187 54 rkastl
 
188 84 rkastl
                                        when CMD8 =>
189
                                                case R.Region is
190
                                                        when send =>
191 95 rkastl
                                                                NextR.ToSdCmd.Content.id  <= cSdCmdSendIfCond;
192
                                                                NextR.ToSdCmd.Content.arg <= cSdArgVoltage;
193 98 rkastl
                                                                NextRegion                := response;
194 54 rkastl
 
195 98 rkastl
                                                        when response =>
196 84 rkastl
                                                                if (iSdCmd.Valid = cActivated) then
197
                                                                        if (iSdCmd.Content.id = cSdCmdSendIfCond and iSdCmd.Content.arg = cSdArgVoltage) then
198 86 rkastl
                                                                                NextR.Region <= waitstate;
199
                                                                                NextR.HCS    <= cActivated;
200 54 rkastl
 
201 84 rkastl
                                                                        else
202
                                                                                NextR.State <= invalidCard;
203
                                                                        end if;
204 86 rkastl
                                                                elsif (Timeout = cActivated) then
205
                                                                        NextR.HCS       <= cInactivated;
206 99 rkastl
                                                                        NextR.CmdRegion <= ACMD41;
207 86 rkastl
                                                                        NextR.Region    <= send;
208 99 rkastl
                                                                        NextR.SendCMD55 <= cActivated;
209 84 rkastl
                                                                end if;
210 86 rkastl
 
211
                                                        when waitstate =>
212 99 rkastl
                                                                NextCmdRegion   := ACMD41;
213
                                                                NextRegion      := send;
214
                                                                NextR.SendCMD55 <= cActivated;
215 86 rkastl
 
216 84 rkastl
                                                        when others =>
217
                                                                report "SdController: Unhandled state" severity error;
218
                                                end case;
219 57 rkastl
 
220 84 rkastl
                                        when ACMD41 =>
221 99 rkastl
                                                if (R.SendCMD55 = cInactivated) then
222
                                                        case R.Region is
223
                                                                when send =>
224
                                                                        ocr.nBusy                 := '0';
225
                                                                        ocr.ccs                   := R.HCS;
226
                                                                        ocr.voltagewindow         := cVoltageWindow;
227
                                                                        NextR.ToSdCmd.Content.id  <= cSdCmdACMD41;
228
                                                                        NextR.ToSdCmd.Content.arg <= OCRToArg(ocr);
229
                                                                        NextRegion                := response;
230 63 rkastl
 
231 99 rkastl
                                                                when response =>
232
                                                                        NextR.ToSdCmd.CheckCrc <= cInactivated;
233 84 rkastl
 
234 99 rkastl
                                                                        if (iSdCmd.Valid = cActivated) then
235
                                                                                NextR.CmdRegion <= CMD8;
236
                                                                                NextR.Region    <= waitstate;
237 84 rkastl
 
238 99 rkastl
                                                                                if (iSdCmd.Content.id = cSdR3Id) then
239
                                                                                        ocr := ArgToOcr(iSdCmd.Content.arg);
240
 
241
                                                                                        if (ocr.nBusy = cnInactivated) then
242
                                                                                                if (ocr.voltagewindow /= cVoltageWindow) then
243
                                                                                                        NextR.State <= invalidCard;
244
                                                                                                else
245
                                                                                                        NextR.CCS       <= ocr.ccs;
246
                                                                                                        NextR.CmdRegion <= ACMD41;
247
                                                                                                        NextR.Region    <= waitstate;
248
                                                                                                end if;
249 84 rkastl
                                                                                        end if;
250
                                                                                end if;
251 99 rkastl
                                                                        elsif (Timeout = cActivated) then
252
                                                                                NextR.State <= invalidCard;
253 84 rkastl
                                                                        end if;
254 105 rkastl
 
255 99 rkastl
                                                                when waitstate =>
256
                                                                        NextCmdRegion := CMD2;
257
                                                                        NextRegion    := send;
258 89 rkastl
 
259 99 rkastl
                                                                when others =>
260
                                                                        report "SdController: Unhandled state" severity error;
261
                                                        end case;
262
                                                end if;
263 84 rkastl
 
264
                                        when CMD2 =>
265
                                                case R.Region is
266
                                                        when send =>
267 95 rkastl
                                                                NextR.ToSdCmd.Content.id <= cSdCmdAllSendCID;
268
                                                                NextR.ToSdCmd.Valid      <= cActivated;
269 84 rkastl
 
270 98 rkastl
                                                                NextRegion := response;
271 84 rkastl
 
272 98 rkastl
                                                        when response =>
273 95 rkastl
                                                                NextR.ToSdCmd.ExpectCID <= cActivated;
274 84 rkastl
 
275
                                                                if (iSdCmd.Valid = cActivated) then
276 106 rkastl
                                                                        NextR.State    <= invalidCard;
277 84 rkastl
 
278
                                                                        if (iSdCmd.Content.id = cSdR2Id) then
279
                                                                                NextR.State     <= init;
280 89 rkastl
                                                                                NextR.Region    <= waitstate;
281 84 rkastl
                                                                        end if;
282 90 rkastl
                                                                elsif (Timeout = cActivated) then
283
                                                                        NextR.State <= invalidCard;
284 84 rkastl
                                                                end if;
285 105 rkastl
 
286 89 rkastl
                                                        when waitstate =>
287 96 rkastl
                                                                NextCmdRegion := CMD3;
288
                                                                NextRegion    := send;
289 84 rkastl
 
290
                                                        when others =>
291
                                                                report "SdController: Unhandled state" severity error;
292
                                                end case;
293
 
294
                                        when CMD3 =>
295
                                                case R.Region is
296
                                                        when send =>
297 95 rkastl
                                                                NextR.ToSdCmd.Content.id <= cSdCmdSendRelAdr;
298
                                                                NextR.ToSdCmd.Valid      <= cActivated;
299 99 rkastl
                                                                NextRegion               := response;
300 84 rkastl
 
301 98 rkastl
                                                        when response =>
302 84 rkastl
                                                                if (iSdCmd.Valid = cActivated) then
303
                                                                        if (iSdCmd.Content.id = cSdCmdSendRelAdr) then
304 96 rkastl
                                                                                NextR.RCA    <= iSdCmd.Content.arg(31 downto 16);
305
                                                                                NextR.Region <= waitstate;
306 84 rkastl
                                                                        end if;
307 90 rkastl
                                                                elsif (Timeout = cActivated) then
308
                                                                        NextR.State <= invalidCard;
309 84 rkastl
                                                                end if;
310
 
311 96 rkastl
                                                        when waitstate =>
312 105 rkastl
                                                                NextState     := config;
313
                                                                NextCmdRegion := SelectCard;
314
                                                                NextRegion    := send;
315 96 rkastl
 
316 84 rkastl
                                                        when others =>
317 103 rkastl
                                                                report "SdController: Unhandled region" severity error;
318 84 rkastl
                                                end case;
319
 
320
                                        when others =>
321 103 rkastl
                                                report "SdController: Unhandled CmdRegion" severity error;
322 84 rkastl
                                end case;
323
 
324 167 rkastl
                        when config =>
325 95 rkastl
                                case R.CmdRegion is
326
                                        when SelectCard =>
327
                                                case R.Region is
328
                                                        when send =>
329 96 rkastl
                                                                NextR.ToSdCmd.Content.id  <= cSdCmdSelCard;
330
                                                                NextR.ToSdCmd.Content.arg <= R.RCA & X"0000";
331 99 rkastl
                                                                NextRegion                := response;
332 95 rkastl
 
333 98 rkastl
                                                        when response => -- Response R1b: with busy!
334 96 rkastl
                                                                if (iSdCmd.Valid = cActivated) then
335
                                                                        if (iSdCmd.Content.id = cSdCmdSelCard) then
336
                                                                                NextR.CardStatus <= iSdCmd.Content.arg;
337 98 rkastl
 
338
                                                                                if (iSdCmd.Content.arg(cSdStatusReadyForDataBit) = cActivated) then
339
                                                                                        NextR.Region <= waitstatedata;
340
                                                                                else
341
                                                                                        NextR.Region <= waitstate;
342
                                                                                end if;
343
 
344 99 rkastl
                                                                        else
345
                                                                                NextR.State <= invalidCard;
346 96 rkastl
                                                                        end if;
347 99 rkastl
 
348 96 rkastl
                                                                elsif (Timeout = cActivated) then
349
                                                                        NextR.State <= invalidCard;
350
                                                                end if;
351 95 rkastl
 
352
                                                        when waitstate =>
353 98 rkastl
                                                                NextRegion := checkbusy;
354 95 rkastl
 
355 98 rkastl
                                                        when waitstatedata =>
356 99 rkastl
                                                                NextR.SendCMD55 <= cActivated;
357
                                                                NextCmdRegion   := CheckBusWidth;
358
                                                                NextRegion      := send;
359 98 rkastl
 
360 95 rkastl
                                                        when others =>
361
                                                                report "Unhandled Region" severity error;
362
                                                end case;
363
 
364 99 rkastl
                                        when CheckBusWidth =>
365
                                                if (R.SendCMD55 = cInactivated) then
366 100 rkastl
                                                        NextR.ToSdData.DataMode <= widewidth;
367 101 rkastl
                                                        NextR.ToSdData.ExpectBits <= ScrBits;
368 100 rkastl
 
369 99 rkastl
                                                        case R.Region is
370
                                                                when send =>
371
                                                                        NextR.ToSdCmd.Content.id  <= cSdCmdSendSCR;
372
                                                                        NextR.ToSdCmd.Content.arg <= (others => '0'); -- stuff bits
373
                                                                        NextRegion                := response;
374
 
375
                                                                when response =>
376
                                                                        if (iSdCmd.Valid = cActivated) then
377
                                                                                if (iSdCmd.Content.id = cSdCmdSendSCR) then
378
                                                                                        NextR.CardStatus <= iSdCmd.Content.arg;
379 100 rkastl
                                                                                        NextR.Region     <= receivedata;
380
                                                                                        TimeoutDisable   <= cActivated;
381 99 rkastl
 
382
                                                                                else
383
                                                                                        NextR.State <= invalidCard;
384
                                                                                end if;
385
                                                                        elsif (Timeout = cActivated) then
386
                                                                                NextR.State <= invalidCard;
387
                                                                        end if;
388
 
389 100 rkastl
                                                                when receivedata =>
390 103 rkastl
                                                                        null;
391 102 rkastl
 
392
                                                                when waitstatedata =>
393 109 rkastl
                                                                        NextRegion           := send;
394 102 rkastl
 
395 106 rkastl
                                                                        if (Timeout = cActivated) then
396 126 rkastl
                                                                                if (iSdData.WideMode = cActivated) then
397 105 rkastl
                                                                                        NextCmdRegion   := SetBusWidth;
398
                                                                                        NextR.SendCMD55 <= cActivated;
399
 
400
                                                                                else
401
                                                                                        NextCmdRegion := CheckSpeed;
402 102 rkastl
                                                                                end if;
403 105 rkastl
                                                                        end if;
404 102 rkastl
 
405 99 rkastl
                                                                when others =>
406
                                                                        report "Unhandled region" severity error;
407
                                                        end case;
408
                                                end if;
409
 
410 102 rkastl
                                        when SetBusWidth =>
411
                                                if (R.SendCMD55 = cInactivated) then
412
                                                        case R.Region is
413
                                                                when send =>
414
                                                                        NextR.ToSdCmd.Content.id              <= cSdCmdSetBusWidth;
415
                                                                        NextR.ToSdCmd.Content.arg(1 downto 0) <= cSdWideBusWidth;
416
                                                                        NextRegion                            := response;
417
 
418
                                                                when response =>
419
                                                                        if (iSdCmd.Valid = cActivated) then
420
                                                                                if (iSdCmd.Content.id = cSdCmdSetBusWidth) then
421
                                                                                        NextR.CardStatus <= iSdCmd.Content.arg;
422
                                                                                        NextR.Region <= waitstate;
423 105 rkastl
 
424 102 rkastl
                                                                                else
425
                                                                                        NextR.State <= invalidCard;
426
                                                                                end if;
427
                                                                        elsif (Timeout = cActivated) then
428
                                                                                NextR.State <= invalidCard;
429
                                                                        end if;
430
 
431 167 rkastl
                                                                        when waitstate =>
432 112 rkastl
                                                                        NextR.ToSdData.Mode <= wide;
433 167 rkastl
                                                                        -- NextR.LedBank(4) <= cActivated;
434 102 rkastl
 
435 109 rkastl
                                                                        if gHighSpeedMode = true then
436
 
437
                                                                                NextRegion          := send;
438
                                                                                NextCmdRegion       := CheckSpeed;
439
 
440
                                                                        else
441
 
442
                                                                                NextState := idle;
443
 
444
                                                                        end if;
445
 
446 102 rkastl
                                                                when others =>
447
                                                                        report "Unhandled region" severity error;
448
                                                        end case;
449
                                                end if;
450
 
451
                                        when CheckSpeed =>
452 104 rkastl
                                                NextR.ToSdData.DataMode   <= widewidth;
453 103 rkastl
                                                NextR.ToSdData.ExpectBits <= SwitchFunctionBits;
454 102 rkastl
 
455 103 rkastl
                                                case R.Region is
456
                                                        when send =>
457 104 rkastl
                                                                NextR.ToSdCmd.Content.id  <= cSdCmdSwitchFunction;
458 103 rkastl
                                                                NextR.ToSdCmd.Content.arg <= cSdCmdCheckSpeedSupport;
459 104 rkastl
                                                                NextRegion                := response;
460 103 rkastl
 
461
                                                        when response =>
462
                                                                if (iSdCmd.Valid = cActivated) then
463
                                                                        if (iSdCmd.Content.id = cSdCmdSwitchFunction) then
464
                                                                                NextR.CardStatus <= iSdCmd.Content.arg;
465 104 rkastl
                                                                                NextR.Region     <= receivedata;
466 105 rkastl
 
467 103 rkastl
                                                                        else
468 122 rkastl
                                                                                NextR.State <= invalidCard;
469 103 rkastl
                                                                        end if;
470
                                                                elsif (Timeout = cActivated) then
471
                                                                        NextR.State <= invalidCard;
472
                                                                end if;
473
 
474
                                                        when receivedata =>
475 126 rkastl
                                                                null;
476 105 rkastl
 
477 103 rkastl
                                                        when waitstatedata =>
478 106 rkastl
                                                                if (Timeout = cActivated) then
479 126 rkastl
                                                                        -- check if high speed mode is supported
480
                                                                        if (iSdData.SpeedBits.HighSpeedSupported = cActivated and
481
                                                                        iSdData.SpeedBits.SwitchFunctionOK = X"1") then
482
                                                                                NextRegion    := send;
483
                                                                                NextCmdRegion := ChangeSpeed;
484
                                                                        else
485
                                                                                NextRegion := idle;
486
                                                                                NextState  := requestnewoperation;
487
                                                                        end if;
488 103 rkastl
                                                                end if;
489
 
490
                                                        when others =>
491 104 rkastl
                                                                report "Unhandled region" severity error;
492 103 rkastl
                                                end case;
493
 
494
                                        when ChangeSpeed =>
495 104 rkastl
                                                case R.Region is
496
                                                        when send =>
497
                                                                NextR.ToSdCmd.Content.id  <= cSdCmdSwitchFunction;
498
                                                                NextR.ToSdCmd.Content.arg <= cSdCmdSwitchSpeed;
499
                                                                NextRegion                := response;
500 103 rkastl
 
501 104 rkastl
                                                        when response =>
502
                                                                if (iSdCmd.Valid = cActivated) then
503
                                                                        if (iSdCmd.Content.id = cSdCmdSwitchFunction) then
504
                                                                                NextR.CardStatus <= iSdCmd.Content.arg;
505
                                                                                NextR.Region     <= receivedata;
506 105 rkastl
 
507 104 rkastl
                                                                        else
508
                                                                                NextR.State <= invalidCard;
509
                                                                        end if;
510
                                                                elsif (Timeout = cActivated) then
511
                                                                        NextR.State <= invalidCard;
512
                                                                end if;
513
 
514
                                                        when receivedata =>
515 105 rkastl
 
516 104 rkastl
                                                        when waitstatedata =>
517 106 rkastl
                                                                if (Timeout = cActivated) then
518 126 rkastl
                                                                        -- check if switching was successful
519
                                                                        if (iSdData.SpeedBits.HighSpeedSupported = cActivated and
520
                                                                        iSdData.SpeedBits.SwitchFunctionOK = X"1") then
521
                                                                                NextR.HighSpeed <= cActivated;
522
                                                                                NextRegion      := send;
523
                                                                                NextCmdRegion   := GetStatus;
524 167 rkastl
                                                                                --NextR.LedBank(5)<= cActivated;
525 126 rkastl
                                                                        else
526
                                                                                NextRegion := idle;
527
                                                                                NextState  := requestnewoperation;
528
                                                                        end if;
529 104 rkastl
                                                                end if;
530
 
531
                                                        when others =>
532
                                                                report "Unhandled region" severity error;
533
                                                end case;
534
 
535 105 rkastl
                                        when GetStatus =>
536
                                                case R.Region is
537
                                                        when idle =>
538 106 rkastl
                                                                EnableNcrTimeout;
539 105 rkastl
 
540 106 rkastl
                                                                if (Timeout = cActivated) then
541 105 rkastl
                                                                        NextR.Region <= send;
542
                                                                end if;
543
 
544
                                                        when send =>
545
                                                                NextR.ToSdCmd.Content.id              <= cSdCmdSendStatus;
546
                                                                NextR.ToSdCmd.Content.arg(31 downto 16) <= R.RCA;
547
                                                                NextRegion                            := response;
548
 
549
                                                        when response =>
550
                                                                if (iSdCmd.Valid = cActivated) then
551
                                                                        if (iSdCmd.Content.id = cSdCmdSendStatus) then
552
                                                                                NextR.CardStatus <= iSdCmd.Content.arg;
553
                                                                                NextR.Region <= waitstate;
554
 
555
                                                                        else
556
                                                                                NextR.State <= invalidCard;
557
                                                                        end if;
558
                                                                elsif (Timeout = cActivated) then
559
                                                                        NextR.State <= invalidCard;
560
                                                                end if;
561
 
562
                                                        when waitstate =>
563 122 rkastl
                                                                NextRegion    := idle;
564
                                                                NextState     := requestnewoperation;
565 105 rkastl
 
566
                                                        when others =>
567
                                                                report "Unhandled region" severity error;
568
                                                end case;
569
 
570 95 rkastl
                                        when others =>
571
                                                report "Unhandled CmdRegion" severity error;
572
                                end case;
573
 
574 113 rkastl
                        when read =>
575
                                NextR.ToSdData.DataMode <= usual;
576
 
577
                                case R.Region is
578
                                        when send =>
579 167 rkastl
                                                NextR.LedBank(0) <= cActivated;
580 123 rkastl
                                                -- send a read command
581 113 rkastl
                                                NextR.ToSdCmd.Content.id  <= cSdCmdReadSingleBlock;
582 123 rkastl
 
583
                                                -- we get a block address, but a standard capacity card needs a byte address
584
                                                case R.CCS is
585
                                                        when cActivated =>
586
                                                                NextR.ToSdCmd.Content.arg <= R.OperationBlock.StartAddr;
587
 
588
                                                        when cInactivated =>
589
                                                                NextR.ToSdCmd.Content.arg <= R.OperationBlock.StartAddr(31-9 downto 0) & X"00" & '0'; -- calculate byte address
590
 
591
                                                        when others =>
592
                                                                report "Invalid CCS" severity error;
593
                                                end case;
594
 
595 113 rkastl
                                                NextRegion                := response;
596
 
597
                                        when response =>
598 167 rkastl
                                                NextR.LedBank(1) <= cActivated;
599
 
600 123 rkastl
                                                -- wait for the response and handle it
601 113 rkastl
                                                if (iSdCmd.Valid = cActivated) then
602
                                                        if (iSdCmd.Content.id = cSdCmdReadSingleBlock) then
603
                                                                NextR.CardStatus <= iSdCmd.Content.arg;
604
                                                                NextR.Region     <= receivedata;
605
 
606
                                                        else
607
                                                                NextR.State <= invalidCard;
608
                                                        end if;
609
                                                elsif (Timeout = cActivated) then
610
                                                        NextR.State <= invalidCard;
611
                                                end if;
612
 
613
                                        when receivedata =>
614 167 rkastl
                                                NextR.LedBank(2) <= cActivated;
615 122 rkastl
 
616 113 rkastl
                                        when waitstatedata =>
617 167 rkastl
                                                NextR.LedBank(3) <= cActivated;
618
                                                NextRegion := idle;
619
                                                NextState  := requestnewoperation;
620 113 rkastl
 
621
                                        when others =>
622
                                                report "Unhandled region";
623
                                end case;
624
 
625 123 rkastl
                        when write =>
626
                                NextR.ToSdData.DataMode <= usual;
627
 
628
                                case R.Region is
629
                                        when send =>
630
                                                -- send a write command
631
                                                NextR.ToSdCmd.Content.id  <= cSdCmdWriteSingleBlock;
632
 
633
                                                -- we get a block address, but a standard capacity card needs a byte address
634
                                                case R.CCS is
635
                                                        when cActivated =>
636
                                                                NextR.ToSdCmd.Content.arg <= R.OperationBlock.StartAddr;
637
 
638
                                                        when cInactivated =>
639
                                                                NextR.ToSdCmd.Content.arg <= R.OperationBlock.StartAddr(31-9 downto 0) & X"00" & '0'; -- calculate byte address
640
 
641
                                                        when others =>
642
                                                                report "Invalid CCS" severity error;
643
                                                end case;
644
 
645
                                                NextRegion                := response;
646
 
647
                                        when response =>
648
                                                -- wait for the response and handle it
649
                                                if (iSdCmd.Valid = cActivated) then
650
                                                        if (iSdCmd.Content.id = cSdCmdWriteSingleBlock) then
651
                                                                NextR.CardStatus <= iSdCmd.Content.arg;
652
                                                                NextR.Region     <= senddata;
653
 
654
                                                        else
655
                                                                NextR.State <= invalidCard;
656
                                                        end if;
657
                                                elsif (Timeout = cActivated) then
658
                                                        NextR.State <= invalidCard;
659
                                                end if;
660 167 rkastl
 
661 147 rkastl
                                        when senddata =>
662
                                                NextRegion := checkbusy;
663 123 rkastl
 
664 167 rkastl
                                        when checkbusy => null;
665 147 rkastl
 
666 123 rkastl
                                        when waitstatedata =>
667 126 rkastl
                                                NextRegion := idle;
668
                                                NextState  := requestnewoperation;
669 123 rkastl
 
670
                                        when others =>
671
                                                report "Unhandled region";
672
                                end case;
673
 
674 122 rkastl
                        when requestnewoperation =>
675 167 rkastl
                                NextR.LedBank <= (others => '0');
676 157 rkastl
                                NextR.ToSdData.DisableRb       <= cInactivated;
677 122 rkastl
                                NextR.ToSdWbSlave.ReqOperation <= not R.ToSdWbSlave.ReqOperation;
678
                                NextR.State                    <= idle;
679
 
680 84 rkastl
                        when idle =>
681 167 rkastl
                                NextR.LedBank(6) <= cActivated;
682 84 rkastl
 
683 122 rkastl
                                -- wait for next operation
684
                                if (iSdWbSlave.AckOperation = cActivated) then
685
 
686
                                        -- save operation
687
                                        NextR.OperationBlock <= iSdWbSlave.OperationBlock;
688
 
689
                                        -- handle operations
690
                                        case iSdWbSlave.OperationBlock.Operation is
691
                                                when cOperationRead =>
692
 
693
                                                        NextR.State  <= read;
694
                                                        NextR.Region <= send;
695
 
696 123 rkastl
                                                when cOperationWrite =>
697
 
698
                                                        NextR.State  <= write;
699
                                                        NextR.Region <= send;
700
 
701 122 rkastl
                                                when others =>
702
                                                        NextR.State <= requestnewoperation;
703
                                                        report "Unknown operation" severity error;
704
                                        end case;
705
 
706 167 rkastl
                                        NextR.LedBank(6) <= cInactivated;
707 122 rkastl
                                end if;
708
 
709 84 rkastl
                        when invalidCard =>
710 167 rkastl
                                NextR.LedBank(7) <= cActivated;
711 84 rkastl
 
712 54 rkastl
                        when others =>
713 84 rkastl
                                report "SdController: Unhandled state" severity error;
714 54 rkastl
                end case;
715 96 rkastl
 
716
                case R.Region is
717
                        when idle => -- do nothing
718
                                null;
719
 
720
                        when send =>
721
                                NextR.ToSdCmd.Valid <= cActivated;
722
 
723 99 rkastl
                                if (R.SendCMD55 = cActivated) then
724 97 rkastl
                                        NextR.ToSdCmd.Content.id  <= cSdNextIsACMD;
725 98 rkastl
                                        NextR.ToSdCmd.Content.arg <= R.RCA & X"0000";
726
                                        NextRegion                := response;
727 97 rkastl
                                end if;
728
 
729 96 rkastl
                                if (iSdCmd.Ack = cActivated) then
730
                                        NextR.ToSdCmd.Valid <= cInactivated;
731
                                        NextR.Region        <= NextRegion;
732
                                end if;
733
 
734 98 rkastl
                        when response =>
735 167 rkastl
                                -- oLedBank(0)   <= cActivated;
736 96 rkastl
                                TimeoutEnable <= cActivated;
737 106 rkastl
                                TimeoutMax    <= cReadTimeout;
738 96 rkastl
 
739 106 rkastl
                                if (iSdCmd.Valid = cActivated) then
740
                                        TimeoutDisable <= cActivated;
741
                                end if;
742
 
743 99 rkastl
                                if (R.SendCMD55 = cActivated) then
744 97 rkastl
                                        if (iSdCmd.Valid = cActivated) then
745
                                                if (iSdCmd.Content.id = cSdNextIsACMD) then
746
                                                        NextR.CardStatus <= iSdCmd.Content.arg;
747 99 rkastl
                                                        NextR.Region <= waitstate;
748 97 rkastl
 
749
                                                        if (iSdCmd.Content.arg(cSdArgAppCmdPos) = cActivated) then
750 99 rkastl
                                                                NextR.SentCMD55 <= cActivated;
751 97 rkastl
                                                        end if;
752
                                                else
753
                                                        NextR.State <= invalidCard;
754
                                                end if;
755
                                        elsif (Timeout = cActivated) then
756 106 rkastl
                                                NextR.State     <= startup;
757
                                                NextR.CmdRegion <= CMD0;
758
                                                NextR.Region    <= idle;
759 97 rkastl
                                        end if;
760
                                end if;
761
 
762 106 rkastl
 
763 96 rkastl
                        when waitstate =>
764 106 rkastl
                                EnableNcrTimeout;
765 96 rkastl
 
766 106 rkastl
                                if (Timeout = cActivated) then
767 99 rkastl
                                        if (R.SentCMD55 = cActivated) then
768
                                                NextR.SentCMD55 <= cInactivated;
769
                                                NextR.SendCMD55 <= cInactivated;
770
                                                NextRegion := send;
771
                                        end if;
772 97 rkastl
 
773 106 rkastl
                                        TimeoutDisable  <= cActivated;
774
                                        NextR.Region    <= NextRegion;
775
                                        NextR.CmdRegion <= NextCmdRegion;
776
                                        NextR.State     <= NextState;
777 96 rkastl
                                end if;
778
 
779 123 rkastl
                        when senddata =>
780
                                NextR.ToSdData.Valid <= cActivated;
781
 
782
                                if (iSdData.Ack = cActivated) then
783 124 rkastl
                                        NextR.ToSdData.Valid <= cInactivated;
784
                                        NextR.Region         <= NextRegion;
785 123 rkastl
                                end if;
786
 
787 98 rkastl
                        when checkbusy =>
788 147 rkastl
                                NextR.ToSdData.CheckBusy <= cActivated;
789 167 rkastl
                                TimeoutEnable <= cActivated;
790
                                TimeoutMax    <= cWriteTimeout;
791 98 rkastl
 
792 167 rkastl
                                if (iSdData.Valid = cActivated) then
793
                                        NextR.Region <= waitstatedata;
794 147 rkastl
                                        NextR.ToSdData.CheckBusy <= cInactivated;
795 167 rkastl
                                        TimeoutDisable <= cActivated;
796 147 rkastl
                                end if;
797
 
798 100 rkastl
                        when receivedata =>
799
                                TimeoutEnable <= cActivated;
800 106 rkastl
                                TimeoutMax    <= cReadTimeout;
801 98 rkastl
 
802 103 rkastl
                                if (iSdData.Err = cActivated) then
803 106 rkastl
                                        NextR.State    <= init;
804
                                        TimeoutDisable <= cActivated;
805 103 rkastl
 
806
                                elsif (iSdData.Valid = cActivated) then
807 106 rkastl
                                        NextR.Region   <= waitstatedata;
808
                                        TimeoutDisable <= cActivated;
809 103 rkastl
 
810
                                elsif (Timeout = cActivated) then
811
                                        NextR.State <= invalidCard;
812 167 rkastl
                                        NextR.LedBank(5) <= cActivated;
813 103 rkastl
                                end if;
814
 
815
 
816 98 rkastl
                        when waitstatedata =>
817 106 rkastl
                                EnableNcrTimeout;
818 105 rkastl
 
819 106 rkastl
                                if (Timeout = cActivated) then
820
                                        TimeoutDisable  <= cActivated;
821
                                        NextR.Region    <= NextRegion;
822
                                        NextR.CmdRegion <= NextCmdRegion;
823
                                        NextR.State     <= NextState;
824 98 rkastl
                                end if;
825
 
826 96 rkastl
                        when others =>
827
                                report "Unhandled region" severity error;
828
                end case;
829 54 rkastl
        end process Comb;
830 85 rkastl
 
831 106 rkastl
        TimeoutCounter_inst : entity work.Counter
832 85 rkastl
        generic map (
833 106 rkastl
                gBitWidth => cMaxTimeoutBitWidth
834 85 rkastl
        )
835
        port map (
836 106 rkastl
                iClk         => iClk,
837 165 rkastl
                iRstSync     => iRstSync,
838 106 rkastl
                iEnable      => TimeoutEnable,
839
                iDisable     => TimeoutDisable,
840
                iMax         => TimeoutMax,
841
                oStrobe      => Timeout);
842 85 rkastl
 
843 54 rkastl
end architecture Rtl;
844
 

powered by: WebSVN 2.1.0

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