summaryrefslogtreecommitdiff
path: root/ArmPkg/Drivers/PL180MciDxe/PL180Mci.c
blob: fe6c904ff312a00421fbd012911b21e80c4e9c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/** @file
  This file implement the MMC Host Protocol for the ARM PrimeCell PL180.

  Copyright (c) 2011, ARM Limited. All rights reserved.
  
  This program and the accompanying materials                          
  are licensed and made available under the terms and conditions of the BSD License         
  which accompanies this distribution.  The full text of the license may be found at        
  http://opensource.org/licenses/bsd-license.php                                            

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

**/

#include "PL180Mci.h"

#include <Library/DevicePathLib.h>
#include <Library/BaseMemoryLib.h>

EFI_MMC_HOST_PROTOCOL     *gpMmcHost;

// Untested ...
//#define USE_STREAM

#define MMCI0_BLOCKLEN 512
#define MMCI0_POW2_BLOCKLEN     9
#define MMCI0_TIMEOUT           1000

BOOLEAN MciIsPowerOn() {
    return ((MmioRead32(MCI_POWER_CONTROL_REG) & 0x3) == MCI_POWER_ON);
}

EFI_STATUS MciInitialize() {
    MCI_TRACE("MciInitialize()");
    return EFI_SUCCESS;
}

BOOLEAN MciIsCardPresent() {
    return (MmioRead32(FixedPcdGet32(PcdPL180SysMciRegAddress)) & 1);
}

BOOLEAN MciIsReadOnly() {
    return (MmioRead32(FixedPcdGet32(PcdPL180SysMciRegAddress)) & 2);
}

// Convert block size to 2^n
UINT32 GetPow2BlockLen(UINT32 BlockLen) {
    UINTN Loop;
    UINTN Pow2BlockLen;
    
    Loop    = 0x8000;
    Pow2BlockLen = 15;
    do {
        Loop = (Loop >> 1) & 0xFFFF;
        Pow2BlockLen--;
    } while (Pow2BlockLen && (!(Loop & BlockLen)));

    return Pow2BlockLen;
}

VOID MciPrepareDataPath(UINTN TransferDirection) {
    // Set Data Length & Data Timer
    MmioWrite32(MCI_DATA_TIMER_REG,0xFFFFFFF);
    MmioWrite32(MCI_DATA_LENGTH_REG,MMCI0_BLOCKLEN);

#ifndef USE_STREAM
    //Note: we are using a hardcoded BlockLen (=512). If we decide to use a variable size, we could
    // compute the pow2 of BlockLen with the above function GetPow2BlockLen()
    MmioWrite32(MCI_DATA_CTL_REG, MCI_DATACTL_ENABLE | TransferDirection | (MMCI0_POW2_BLOCKLEN << 4));
#else
    MmioWrite32(MCI_DATA_CTL_REG, MCI_DATACTL_ENABLE | TransferDirection | MCI_DATACTL_STREAM_TRANS);
#endif
}

EFI_STATUS MciSendCommand(MMC_CMD MmcCmd, UINT32 Argument) {
    UINT32 Status;
    UINT32  Timer;
    UINT32 Cmd;

    if ((MmcCmd == MMC_CMD17) || (MmcCmd == MMC_CMD11)) {
        MciPrepareDataPath(MCI_DATACTL_CARD_TO_CONT);
    } else if ((MmcCmd == MMC_CMD24) || (MmcCmd == MMC_CMD20)) {
        MciPrepareDataPath(MCI_DATACTL_CONT_TO_CARD);
    }

    // Create Command for PL180
    Cmd = INDX(MmcCmd);
    if (MmcCmd & MMC_CMD_WAIT_RESPONSE)
        Cmd |= MCI_CPSM_WAIT_RESPONSE;
    if (MmcCmd & MMC_CMD_LONG_RESPONSE)
        Cmd |= MCI_CPSM_LONG_RESPONSE;

    MmioWrite32(MCI_CLEAR_STATUS_REG,0x5FFF);
    MmioWrite32(MCI_ARGUMENT_REG,Argument);
    MmioWrite32(MCI_COMMAND_REG,Cmd);

    Timer = 1000;
    if (Cmd & MCI_CPSM_WAIT_RESPONSE) {
        Status = MmioRead32(MCI_STATUS_REG);
        while (!(Status & (MCI_STATUS_CMD_RESPEND | MCI_STATUS_CMD_CMDCRCFAIL | MCI_STATUS_CMD_CMDTIMEOUT)) && Timer) {
            //NanoSecondDelay(10);
            Status = MmioRead32(MCI_STATUS_REG);
            Timer--;
        }

        if ((Timer == 0) || (Status == MCI_STATUS_CMD_CMDTIMEOUT)) {
            //DEBUG ((EFI_D_ERROR, "MciSendCommand(CmdIndex:%d) TIMEOUT! Response:0x%X Status:0x%X\n",Cmd & 0x3F,MmioRead32(MCI_RESPONSE0_REG),Status));
            return EFI_TIMEOUT;
        } else if (!((Cmd & 0x3F) == INDX(1)) && (Status & MCI_STATUS_CMD_CMDCRCFAIL)) {
            // The CMD1 does not contain CRC. We should ignore the CRC failed Status.
            return EFI_CRC_ERROR;
        } else {
            return EFI_SUCCESS;
        }
    } else {
        Status = MmioRead32(MCI_STATUS_REG);
        while (!(Status & MCI_STATUS_CMD_SENT) && Timer) {
            //NanoSecondDelay(10);
            Status = MmioRead32(MCI_STATUS_REG);
            Timer--;
        }

        if (Timer == 0) {
            //DEBUG ((EFI_D_ERROR, "MciSendCommand(CmdIndex:%d) TIMEOUT2! 0x%X\n",Cmd & 0x3F,MmioRead32(MCI_RESPONSE0_REG)));
            return EFI_TIMEOUT;
        } else {
            return EFI_SUCCESS;
        }
    }
}

EFI_STATUS MciReceiveResponse(MMC_RESPONSE_TYPE Type, UINT32* Buffer) {
    if (Buffer == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    if ((Type == MMC_RESPONSE_TYPE_R1) || (Type == MMC_RESPONSE_TYPE_R1b) ||
        (Type == MMC_RESPONSE_TYPE_R3) || (Type == MMC_RESPONSE_TYPE_R6) ||
        (Type == MMC_RESPONSE_TYPE_R7)) {
        Buffer[0] = MmioRead32(MCI_RESPONSE0_REG);
        Buffer[1] = MmioRead32(MCI_RESPONSE1_REG);
    } else if (Type == MMC_RESPONSE_TYPE_R2) {
        Buffer[0] = MmioRead32(MCI_RESPONSE0_REG);
        Buffer[1] = MmioRead32(MCI_RESPONSE1_REG);
        Buffer[2] = MmioRead32(MCI_RESPONSE2_REG);
        Buffer[3] = MmioRead32(MCI_RESPONSE3_REG);
    }

    return EFI_SUCCESS;
}

EFI_STATUS MciReadBlockData(EFI_LBA Lba, UINTN Length, UINT32* Buffer) {
    UINTN Loop;
    UINTN Finish;
    UINTN Timer;
    UINTN Status;

    // Read data from the RX FIFO
    Loop   = 0;
    Finish = MMCI0_BLOCKLEN / 4;
    Timer  = MMCI0_TIMEOUT * 10;
    do {
        // Read the Status flags
        Status = MmioRead32(MCI_STATUS_REG);
        // Do eight reads if possible else a single read
        if (Status & MCI_STATUS_CMD_RXFIFOHALFFULL) {
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
        }
        else if (!(Status & MCI_STATUS_CMD_RXFIFOEMPTY)) {
            Buffer[Loop] = MmioRead32(MCI_FIFO_REG);
            Loop++;
        } else
            Timer--;
    } while ((Loop < Finish) && Timer);

    if (Timer == 0) {
        DEBUG ((EFI_D_ERROR, "MciReadBlockData: Timeout Status:0x%X Loop:%d // Finish:%d\n",MmioRead32(MCI_STATUS_REG),Loop,Finish));
        return EFI_TIMEOUT;
    } else
        return EFI_SUCCESS;
}

EFI_STATUS MciWriteBlockData(EFI_LBA Lba, UINTN Length, UINT32* Buffer) {
    UINTN Loop;
    UINTN Finish;
    UINTN Timer;
    UINTN Status;

    // Write the data to the TX FIFO
    Loop   = 0;
    Finish = MMCI0_BLOCKLEN / 4;
    Timer  = MMCI0_TIMEOUT * 100;
    do {
        // Read the Status flags
        Status = MmioRead32(MCI_STATUS_REG);

        // Do eight writes if possible else a single write
        if (Status & MCI_STATUS_CMD_TXFIFOHALFEMPTY) {
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
        }
        else if (!(Status & MCI_STATUS_CMD_TXFIFOFULL)) {
            MmioWrite32(MCI_FIFO_REG, Buffer[Loop]);
            Loop++;
        }
        else
            Timer--;
    } while ((Loop < Finish) && Timer);

    ASSERT(Timer > 0);

    // Wait for FIFO to drain
    Timer = MMCI0_TIMEOUT;
    Status = MmioRead32(MCI_STATUS_REG);
/*#ifndef USE_STREAM
    // Single block
    while (((Status & MCI_STATUS_CMD_TXDONE) != MCI_STATUS_CMD_TXDONE) && Timer) {
#else*/
    // Stream
    while (((Status & MCI_STATUS_CMD_DATAEND) != MCI_STATUS_CMD_DATAEND) && Timer) {
//#endif
        NanoSecondDelay(10);
        Status = MmioRead32(MCI_STATUS_REG);
        Timer--;
    }

    ASSERT(Timer > 0);

    if (Timer == 0)
        return EFI_TIMEOUT;
    else
        return EFI_SUCCESS;
}

EFI_STATUS MciNotifyState(MMC_STATE State) {
    UINT32      Data32;

    switch(State) {
    case MmcInvalidState:
        ASSERT(0);
        break;
    case MmcHwInitializationState:
        // If device already turn on then restart it
        Data32 = MmioRead32(MCI_POWER_CONTROL_REG);
        if ((Data32 & 0x2) == MCI_POWER_UP) {
            MCI_TRACE("MciNotifyState(MmcHwInitializationState): TurnOff MCI");

            // Turn off
            MmioWrite32(MCI_CLOCK_CONTROL_REG, 0);
            MmioWrite32(MCI_POWER_CONTROL_REG, 0);
            MicroSecondDelay(100);
        }
        
        MCI_TRACE("MciNotifyState(MmcHwInitializationState): TurnOn MCI");
        // Setup clock
        //  - 0x1D = 29 => should be the clock divider to be less than 400kHz at MCLK = 24Mhz
        MmioWrite32(MCI_CLOCK_CONTROL_REG,0x1D | MCI_CLOCK_ENABLE | MCI_CLOCK_POWERSAVE);
        //MmioWrite32(MCI_CLOCK_CONTROL_REG,0x1D | MCI_CLOCK_ENABLE);

        // Set the voltage
        MmioWrite32(MCI_POWER_CONTROL_REG,MCI_POWER_OPENDRAIN | (15<<2));
        MmioWrite32(MCI_POWER_CONTROL_REG,MCI_POWER_ROD | MCI_POWER_OPENDRAIN | (15<<2) | MCI_POWER_UP);
        MicroSecondDelay(10);
        MmioWrite32(MCI_POWER_CONTROL_REG,MCI_POWER_ROD | MCI_POWER_OPENDRAIN | (15<<2) | MCI_POWER_ON);
        MicroSecondDelay(100);

        // Set Data Length & Data Timer
        MmioWrite32(MCI_DATA_TIMER_REG,0xFFFFF);
        MmioWrite32(MCI_DATA_LENGTH_REG,8);

        ASSERT((MmioRead32(MCI_POWER_CONTROL_REG) & 0x3) == MCI_POWER_ON);
        break;
    case MmcIdleState:
        MCI_TRACE("MciNotifyState(MmcIdleState)");
        break;
    case MmcReadyState:
        MCI_TRACE("MciNotifyState(MmcReadyState)");
        break;
    case MmcIdentificationState:
        MCI_TRACE("MciNotifyState(MmcIdentificationState)");
        break;
    case MmcStandByState:
        MCI_TRACE("MciNotifyState(MmcStandByState)");
 
        // Enable MCICMD push-pull drive
        MmioWrite32(MCI_POWER_CONTROL_REG,MCI_POWER_ROD | (15<<2) | MCI_POWER_ON);

        /*// Set MMCI0 clock to 4MHz (24MHz may be possible with cache enabled)
        MmioWrite32(MCI_CLOCK_CONTROL_REG,0x02 | MCI_CLOCK_ENABLE | MCI_CLOCK_POWERSAVE);*/
        // Set MMCI0 clock to 24MHz (by bypassing the divider)
        MmioWrite32(MCI_CLOCK_CONTROL_REG,MCI_CLOCK_BYPASS | MCI_CLOCK_ENABLE);
        break;
    case MmcTransferState:
        //MCI_TRACE("MciNotifyState(MmcTransferState)");
        break;
    case MmcSendingDataState:
        MCI_TRACE("MciNotifyState(MmcSendingDataState)");
        break;
    case MmcReceiveDataState:
        MCI_TRACE("MciNotifyState(MmcReceiveDataState)");
        break;
    case MmcProgrammingState:
        MCI_TRACE("MciNotifyState(MmcProgrammingState)");
        break;
    case MmcDisconnectState:
        MCI_TRACE("MciNotifyState(MmcDisconnectState)");
        break;
    default:
        ASSERT(0);
    }
    return EFI_SUCCESS;
}

EFI_GUID mPL180MciDevicePathGuid = { 0x621b6fa5, 0x4dc1, 0x476f, 0xb9, 0xd8, 0x52, 0xc5, 0x57, 0xd8, 0x10, 0x70 };

EFI_STATUS MciBuildDevicePath(EFI_DEVICE_PATH_PROTOCOL **DevicePath) {
    EFI_DEVICE_PATH_PROTOCOL    *NewDevicePathNode;

    NewDevicePathNode = CreateDeviceNode(HARDWARE_DEVICE_PATH,HW_VENDOR_DP,sizeof(VENDOR_DEVICE_PATH));
    CopyGuid(&((VENDOR_DEVICE_PATH*)NewDevicePathNode)->Guid,&mPL180MciDevicePathGuid);
    
    *DevicePath = NewDevicePathNode;
    return EFI_SUCCESS;
}

EFI_MMC_HOST_PROTOCOL gMciHost = {
    MciIsCardPresent,
    MciIsReadOnly,
    MciBuildDevicePath,
    MciNotifyState,
    MciSendCommand,
    MciReceiveResponse,
    MciReadBlockData,
    MciWriteBlockData
};

EFI_STATUS
PL180MciDxeInitialize (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
{
  EFI_STATUS    Status;
  EFI_HANDLE    Handle = NULL;

  MCI_TRACE("PL180MciDxeInitialize()");

  //Publish Component Name, BlockIO protocol interfaces
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &Handle, 
                  &gEfiMmcHostProtocolGuid,         &gMciHost,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;
}