2 This file implement the MMC Host Protocol for the ARM PrimeCell PL180.
4 Copyright (c) 2011, ARM Limited. All rights reserved.
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/DevicePathLib.h>
19 #include <Library/BaseMemoryLib.h>
21 EFI_MMC_HOST_PROTOCOL
*gpMmcHost
;
26 #define MMCI0_BLOCKLEN 512
27 #define MMCI0_POW2_BLOCKLEN 9
28 #define MMCI0_TIMEOUT 1000
35 return ((MmioRead32(MCI_POWER_CONTROL_REG
) & 0x3) == MCI_POWER_ON
);
43 MCI_TRACE("MciInitialize()");
52 return (MmioRead32(FixedPcdGet32(PcdPL180SysMciRegAddress
)) & 1);
60 return (MmioRead32(FixedPcdGet32(PcdPL180SysMciRegAddress
)) & 2);
63 // Convert block size to 2^n
76 Loop
= (Loop
>> 1) & 0xFFFF;
78 } while (Pow2BlockLen
&& (!(Loop
& BlockLen
)));
85 IN UINTN TransferDirection
88 // Set Data Length & Data Timer
89 MmioWrite32(MCI_DATA_TIMER_REG
,0xFFFFFFF);
90 MmioWrite32(MCI_DATA_LENGTH_REG
,MMCI0_BLOCKLEN
);
93 //Note: we are using a hardcoded BlockLen (=512). If we decide to use a variable size, we could
94 // compute the pow2 of BlockLen with the above function GetPow2BlockLen()
95 MmioWrite32(MCI_DATA_CTL_REG
, MCI_DATACTL_ENABLE
| MCI_DATACTL_DMA_ENABLE
| TransferDirection
| (MMCI0_POW2_BLOCKLEN
<< 4));
97 MmioWrite32(MCI_DATA_CTL_REG
, MCI_DATACTL_ENABLE
| MCI_DATACTL_DMA_ENABLE
| TransferDirection
| MCI_DATACTL_STREAM_TRANS
);
112 RetVal
= EFI_SUCCESS
;
114 if ((MmcCmd
== MMC_CMD17
) || (MmcCmd
== MMC_CMD11
)) {
115 MciPrepareDataPath(MCI_DATACTL_CARD_TO_CONT
);
116 } else if ((MmcCmd
== MMC_CMD24
) || (MmcCmd
== MMC_CMD20
)) {
117 MciPrepareDataPath(MCI_DATACTL_CONT_TO_CARD
);
120 // Create Command for PL180
122 if (MmcCmd
& MMC_CMD_WAIT_RESPONSE
) {
123 Cmd
|= MCI_CPSM_WAIT_RESPONSE
;
126 if (MmcCmd
& MMC_CMD_LONG_RESPONSE
) {
127 Cmd
|= MCI_CPSM_LONG_RESPONSE
;
130 // Clear Status register static flags
131 MmioWrite32(MCI_CLEAR_STATUS_REG
,0x7FF);
133 //Write to command argument register
134 MmioWrite32(MCI_ARGUMENT_REG
,Argument
);
136 //Write to command register
137 MmioWrite32(MCI_COMMAND_REG
,Cmd
);
139 if (Cmd
& MCI_CPSM_WAIT_RESPONSE
) {
140 Status
= MmioRead32(MCI_STATUS_REG
);
141 while (!(Status
& (MCI_STATUS_CMD_RESPEND
| MCI_STATUS_CMD_CMDCRCFAIL
| MCI_STATUS_CMD_CMDTIMEOUT
| MCI_STATUS_CMD_START_BIT_ERROR
))) {
142 Status
= MmioRead32(MCI_STATUS_REG
);
145 if ((Status
& MCI_STATUS_CMD_START_BIT_ERROR
)) {
146 DEBUG ((EFI_D_ERROR
, "MciSendCommand(CmdIndex:%d) Start bit Error! Response:0x%X Status:0x%x\n",(Cmd
& 0x3F),MmioRead32(MCI_RESPONSE0_REG
),Status
));
147 RetVal
= EFI_NO_RESPONSE
;
149 } else if ((Status
& MCI_STATUS_CMD_CMDTIMEOUT
)) {
150 //DEBUG ((EFI_D_ERROR, "MciSendCommand(CmdIndex:%d) TIMEOUT! Response:0x%X Status:0x%x\n",(Cmd & 0x3F),MmioRead32(MCI_RESPONSE0_REG),Status));
151 RetVal
= EFI_TIMEOUT
;
153 } else if (!((Cmd
& 0x3F) == INDX(1)) && (Status
& MCI_STATUS_CMD_CMDCRCFAIL
)) {
154 // The CMD1 does not contain CRC. We should ignore the CRC failed Status.
155 RetVal
= EFI_CRC_ERROR
;
158 RetVal
= EFI_SUCCESS
;
162 Status
= MmioRead32(MCI_STATUS_REG
);
163 while (!(Status
& (MCI_STATUS_CMD_SENT
| MCI_STATUS_CMD_CMDCRCFAIL
| MCI_STATUS_CMD_CMDTIMEOUT
| MCI_STATUS_CMD_START_BIT_ERROR
))) {
164 Status
= MmioRead32(MCI_STATUS_REG
);
167 if ((Status
& MCI_STATUS_CMD_START_BIT_ERROR
)) {
168 DEBUG ((EFI_D_ERROR
, "MciSendCommand(CmdIndex:%d) Start bit Error! Response:0x%X Status:0x%x\n",(Cmd
& 0x3F),MmioRead32(MCI_RESPONSE0_REG
),Status
));
169 RetVal
= EFI_NO_RESPONSE
;
171 } else if ((Status
& MCI_STATUS_CMD_CMDTIMEOUT
)) {
172 //DEBUG ((EFI_D_ERROR, "MciSendCommand(CmdIndex:%d) TIMEOUT! Response:0x%X Status:0x%x\n",(Cmd & 0x3F),MmioRead32(MCI_RESPONSE0_REG),Status));
173 RetVal
= EFI_TIMEOUT
;
176 if (!((Cmd
& 0x3F) == INDX(1)) && (Status
& MCI_STATUS_CMD_CMDCRCFAIL
)) {
177 // The CMD1 does not contain CRC. We should ignore the CRC failed Status.
178 RetVal
= EFI_CRC_ERROR
;
181 RetVal
= EFI_SUCCESS
;
187 //Disable Command Path
188 CmdCtrlReg
= MmioRead32(MCI_COMMAND_REG
);
189 MmioWrite32(MCI_COMMAND_REG
, (CmdCtrlReg
& ~MCI_CPSM_ENABLED
));
195 IN MMC_RESPONSE_TYPE Type
,
199 if (Buffer
== NULL
) {
200 return EFI_INVALID_PARAMETER
;
203 if ((Type
== MMC_RESPONSE_TYPE_R1
) || (Type
== MMC_RESPONSE_TYPE_R1b
) ||
204 (Type
== MMC_RESPONSE_TYPE_R3
) || (Type
== MMC_RESPONSE_TYPE_R6
) ||
205 (Type
== MMC_RESPONSE_TYPE_R7
))
207 Buffer
[0] = MmioRead32(MCI_RESPONSE0_REG
);
208 Buffer
[1] = MmioRead32(MCI_RESPONSE1_REG
);
209 } else if (Type
== MMC_RESPONSE_TYPE_R2
) {
210 Buffer
[0] = MmioRead32(MCI_RESPONSE0_REG
);
211 Buffer
[1] = MmioRead32(MCI_RESPONSE1_REG
);
212 Buffer
[2] = MmioRead32(MCI_RESPONSE2_REG
);
213 Buffer
[3] = MmioRead32(MCI_RESPONSE3_REG
);
232 RetVal
= EFI_SUCCESS
;
234 // Read data from the RX FIFO
236 Finish
= MMCI0_BLOCKLEN
/ 4;
238 // Read the Status flags
239 Status
= MmioRead32(MCI_STATUS_REG
);
241 // Do eight reads if possible else a single read
242 if (Status
& MCI_STATUS_CMD_RXFIFOHALFFULL
) {
243 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
245 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
247 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
249 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
251 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
253 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
255 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
257 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
259 } else if (Status
& MCI_STATUS_CMD_RXDATAAVAILBL
) {
260 Buffer
[Loop
] = MmioRead32(MCI_FIFO_REG
);
263 //Check for error conditions and timeouts
264 if(Status
& MCI_STATUS_CMD_DATATIMEOUT
) {
265 DEBUG ((EFI_D_ERROR
, "MciReadBlockData(): TIMEOUT! Response:0x%X Status:0x%x\n",MmioRead32(MCI_RESPONSE0_REG
),Status
));
266 RetVal
= EFI_TIMEOUT
;
268 } else if(Status
& MCI_STATUS_CMD_DATACRCFAIL
) {
269 DEBUG ((EFI_D_ERROR
, "MciReadBlockData(): CRC Error! Response:0x%X Status:0x%x\n",MmioRead32(MCI_RESPONSE0_REG
),Status
));
270 RetVal
= EFI_CRC_ERROR
;
272 } else if(Status
& MCI_STATUS_CMD_START_BIT_ERROR
) {
273 DEBUG ((EFI_D_ERROR
, "MciReadBlockData(): Start-bit Error! Response:0x%X Status:0x%x\n",MmioRead32(MCI_RESPONSE0_REG
),Status
));
274 RetVal
= EFI_NO_RESPONSE
;
278 //clear RX over run flag
279 if(Status
& MCI_STATUS_CMD_RXOVERRUN
) {
280 MmioWrite32(MCI_CLEAR_STATUS_REG
, MCI_STATUS_CMD_RXOVERRUN
);
282 } while ((Loop
< Finish
));
285 MmioWrite32(MCI_CLEAR_STATUS_REG
, 0x7FF);
288 DataCtrlReg
= MmioRead32(MCI_DATA_CTL_REG
);
289 MmioWrite32(MCI_DATA_CTL_REG
, (DataCtrlReg
& 0xFE));
308 RetVal
= EFI_SUCCESS
;
310 // Write the data to the TX FIFO
312 Finish
= MMCI0_BLOCKLEN
/ 4;
313 Timer
= MMCI0_TIMEOUT
* 100;
315 // Read the Status flags
316 Status
= MmioRead32(MCI_STATUS_REG
);
318 // Do eight writes if possible else a single write
319 if (Status
& MCI_STATUS_CMD_TXFIFOHALFEMPTY
) {
320 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
322 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
324 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
326 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
328 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
330 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
332 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
334 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
336 } else if ((Status
& MCI_STATUS_CMD_TXFIFOEMPTY
)) {
337 MmioWrite32(MCI_FIFO_REG
, Buffer
[Loop
]);
340 //Check for error conditions and timeouts
341 if(Status
& MCI_STATUS_CMD_DATATIMEOUT
) {
342 DEBUG ((EFI_D_ERROR
, "MciWriteBlockData(): TIMEOUT! Response:0x%X Status:0x%x\n",MmioRead32(MCI_RESPONSE0_REG
),Status
));
343 RetVal
= EFI_TIMEOUT
;
345 } else if(Status
& MCI_STATUS_CMD_DATACRCFAIL
) {
346 DEBUG ((EFI_D_ERROR
, "MciWriteBlockData(): CRC Error! Response:0x%X Status:0x%x\n",MmioRead32(MCI_RESPONSE0_REG
),Status
));
347 RetVal
= EFI_CRC_ERROR
;
349 } else if(Status
& MCI_STATUS_CMD_TX_UNDERRUN
) {
350 DEBUG ((EFI_D_ERROR
, "MciWriteBlockData(): TX buffer Underrun! Response:0x%X Status:0x%x, Number of bytes written 0x%x\n",MmioRead32(MCI_RESPONSE0_REG
),Status
, Loop
));
351 RetVal
= EFI_BUFFER_TOO_SMALL
;
356 } while (Loop
< Finish
);
358 // Wait for FIFO to drain
359 Timer
= MMCI0_TIMEOUT
* 60;
360 Status
= MmioRead32(MCI_STATUS_REG
);
363 while (((Status
& MCI_STATUS_CMD_TXDONE
) != MCI_STATUS_CMD_TXDONE
) && Timer
) {
366 while (((Status
& MCI_STATUS_CMD_DATAEND
) != MCI_STATUS_CMD_DATAEND
) && Timer
) {
369 Status
= MmioRead32(MCI_STATUS_REG
);
374 DEBUG ((EFI_D_ERROR
, "MciWriteBlockData(): Data End timeout Number of bytes written 0x%x\n",Loop
));
380 MmioWrite32(MCI_CLEAR_STATUS_REG
, 0x7FF);
382 RetVal
= EFI_TIMEOUT
;
387 DataCtrlReg
= MmioRead32(MCI_DATA_CTL_REG
);
388 MmioWrite32(MCI_DATA_CTL_REG
, (DataCtrlReg
& 0xFE));
400 case MmcInvalidState
:
403 case MmcHwInitializationState
:
404 // If device already turn on then restart it
405 Data32
= MmioRead32(MCI_POWER_CONTROL_REG
);
406 if ((Data32
& 0x2) == MCI_POWER_UP
) {
407 MCI_TRACE("MciNotifyState(MmcHwInitializationState): TurnOff MCI");
410 MmioWrite32(MCI_CLOCK_CONTROL_REG
, 0);
411 MmioWrite32(MCI_POWER_CONTROL_REG
, 0);
412 MicroSecondDelay(100);
415 MCI_TRACE("MciNotifyState(MmcHwInitializationState): TurnOn MCI");
417 // - 0x1D = 29 => should be the clock divider to be less than 400kHz at MCLK = 24Mhz
418 MmioWrite32(MCI_CLOCK_CONTROL_REG
,0x1D | MCI_CLOCK_ENABLE
| MCI_CLOCK_POWERSAVE
);
419 //MmioWrite32(MCI_CLOCK_CONTROL_REG,0x1D | MCI_CLOCK_ENABLE);
422 MmioWrite32(MCI_POWER_CONTROL_REG
,MCI_POWER_OPENDRAIN
| (15<<2));
423 MmioWrite32(MCI_POWER_CONTROL_REG
,MCI_POWER_ROD
| MCI_POWER_OPENDRAIN
| (15<<2) | MCI_POWER_UP
);
424 MicroSecondDelay(10);
425 MmioWrite32(MCI_POWER_CONTROL_REG
,MCI_POWER_ROD
| MCI_POWER_OPENDRAIN
| (15<<2) | MCI_POWER_ON
);
426 MicroSecondDelay(100);
428 // Set Data Length & Data Timer
429 MmioWrite32(MCI_DATA_TIMER_REG
,0xFFFFF);
430 MmioWrite32(MCI_DATA_LENGTH_REG
,8);
432 ASSERT((MmioRead32(MCI_POWER_CONTROL_REG
) & 0x3) == MCI_POWER_ON
);
435 MCI_TRACE("MciNotifyState(MmcIdleState)");
438 MCI_TRACE("MciNotifyState(MmcReadyState)");
440 case MmcIdentificationState
:
441 MCI_TRACE("MciNotifyState(MmcIdentificationState)");
443 case MmcStandByState
:{
444 volatile UINT32 PwrCtrlReg
;
445 MCI_TRACE("MciNotifyState(MmcStandByState)");
447 // Enable MCICMD push-pull drive
448 PwrCtrlReg
= MmioRead32(MCI_POWER_CONTROL_REG
);
449 //Disable Open Drain output
450 PwrCtrlReg
&=~(MCI_POWER_OPENDRAIN
);
451 MmioWrite32(MCI_POWER_CONTROL_REG
,PwrCtrlReg
);
453 // Set MMCI0 clock to 4MHz (24MHz may be possible with cache enabled)
455 // Note: Increasing clock speed causes TX FIFO under-run errors.
456 // So careful when optimising this driver for higher performance.
458 MmioWrite32(MCI_CLOCK_CONTROL_REG
,0x02 | MCI_CLOCK_ENABLE
| MCI_CLOCK_POWERSAVE
);
459 // Set MMCI0 clock to 24MHz (by bypassing the divider)
460 //MmioWrite32(MCI_CLOCK_CONTROL_REG,MCI_CLOCK_BYPASS | MCI_CLOCK_ENABLE);
463 case MmcTransferState
:
464 //MCI_TRACE("MciNotifyState(MmcTransferState)");
466 case MmcSendingDataState
:
467 MCI_TRACE("MciNotifyState(MmcSendingDataState)");
469 case MmcReceiveDataState
:
470 MCI_TRACE("MciNotifyState(MmcReceiveDataState)");
472 case MmcProgrammingState
:
473 MCI_TRACE("MciNotifyState(MmcProgrammingState)");
475 case MmcDisconnectState
:
476 MCI_TRACE("MciNotifyState(MmcDisconnectState)");
484 EFI_GUID mPL180MciDevicePathGuid
= { 0x621b6fa5, 0x4dc1, 0x476f, 0xb9, 0xd8, 0x52, 0xc5, 0x57, 0xd8, 0x10, 0x70 };
488 IN EFI_DEVICE_PATH_PROTOCOL
**DevicePath
491 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePathNode
;
493 NewDevicePathNode
= CreateDeviceNode(HARDWARE_DEVICE_PATH
,HW_VENDOR_DP
,sizeof(VENDOR_DEVICE_PATH
));
494 CopyGuid(&((VENDOR_DEVICE_PATH
*)NewDevicePathNode
)->Guid
,&mPL180MciDevicePathGuid
);
496 *DevicePath
= NewDevicePathNode
;
500 EFI_MMC_HOST_PROTOCOL gMciHost
= {
512 PL180MciDxeInitialize (
513 IN EFI_HANDLE ImageHandle
,
514 IN EFI_SYSTEM_TABLE
*SystemTable
518 EFI_HANDLE Handle
= NULL
;
520 MCI_TRACE("PL180MciDxeInitialize()");
522 //Publish Component Name, BlockIO protocol interfaces
523 Status
= gBS
->InstallMultipleProtocolInterfaces (
525 &gEfiMmcHostProtocolGuid
, &gMciHost
,
528 ASSERT_EFI_ERROR (Status
);