From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (NAM12-DM6-obe.outbound.protection.outlook.com [40.107.243.45]) by mx.groups.io with SMTP id smtpd.web11.1470.1593673758322532336 for ; Thu, 02 Jul 2020 00:09:18 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@vmware.com header.s=selector2 header.b=UUFRaIy/; spf=pass (domain: vmware.com, ip: 40.107.243.45, mailfrom: awarkentin@vmware.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=coCa+2KfeYENFmVq+ccKqdoqklWYgXDbNncMlg2r1lx19FwVwEatbNkAMeGR9hSHYJipeeDpgIzjZF6ZtJro58m/x+vjECbCMv61XXbtICnE3Nem06wN0Z1w+99ClakZVsLETYQkHw/n/y5sLQ6Fn22GtEie31yA/Po5DTy8pK6fogg3BVU9wkpltJjFauqiv8Q6qzkkFZ/wZWkaWubMJPzgK1S/g5xPJGqPCABzMDKNtxQRmNLxUKxHgrhU2ggdak/BPAg3Gp62b8JaD02KSIIitZln9UEp17gjHjwa2IdLoRZZ6XkM/sAmRp/CbtPcPWDKRt4EbfVlXt+X3GTm2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=w6hV1YVqZwcxaijJjTwO40iCUW5glbDrryP54MKbX5E=; b=bz1domaPBa3oocmlmgewDf9mmIx1+AVtP7VbDwyXyN7XQvKPcvUYxsPxdBuhm6IrxNT/waSRmvcqSXOEc2mlhOol1xyS8rY5IbQJZnNMHC1mW4Jjc+/uy+p3/HrGwXhDsDYd3zCtUwIWXr72aoy/ZIjwxhXzamU6zY7G7jvNXtbnMqTV6OjO4KUwAQsdShQwekmbuKDk8zmQcni/3WVEK0Ki1a1OSdvHi7sAaYoQ4oTnATYguQCnKsFDU6TMQGyX8lB5jcLyFpkYsUhRr5rGfO/tjUMZbwPxsMRYvM7qmkgT8kBzQH79J9C4b+OEPMZGgS7YZr4B8pBNqNk72whK7Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vmware.com; dmarc=pass action=none header.from=vmware.com; dkim=pass header.d=vmware.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vmware.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=w6hV1YVqZwcxaijJjTwO40iCUW5glbDrryP54MKbX5E=; b=UUFRaIy/8eosKb+7zVfeDsfoidgs7Lq8tQ+vN959RzDZxghDTfY7c3OZ5x/UVu5VGPUb5vtiIjtaOPhawezweB3JI0mqzFqSJseVwxXjlI3WEBecBEjiaLX1k17EmgV4yKGmxt/xlau3BJk3pdoEt07kvUmxeoWANlBjFwPSb70= Received: from BN6PR05MB3411.namprd05.prod.outlook.com (2603:10b6:405:43::23) by BN3PR05MB2434.namprd05.prod.outlook.com (2a01:111:e400:7bb4::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3153.10; Thu, 2 Jul 2020 07:09:16 +0000 Received: from BN6PR05MB3411.namprd05.prod.outlook.com ([fe80::e1ef:31eb:c802:aef0]) by BN6PR05MB3411.namprd05.prod.outlook.com ([fe80::e1ef:31eb:c802:aef0%3]) with mapi id 15.20.3153.022; Thu, 2 Jul 2020 07:09:16 +0000 From: "Andrei Warkentin" To: Pete Batard , "devel@edk2.groups.io" , Andrei Warkentin CC: "ard.biesheuvel@arm.com" , "leif@nuviainc.com" Subject: Re: [edk2-devel] [edk2-platforms][PATCH 1/1] Platform/RaspberryPi/Drivers: Add SD card detection Thread-Topic: [edk2-devel] [edk2-platforms][PATCH 1/1] Platform/RaspberryPi/Drivers: Add SD card detection Thread-Index: AQHWTkRBA4YuzpwBoEepZIISGAB6Z6jwF5sigAPKmhE= Date: Thu, 2 Jul 2020 07:09:16 +0000 Message-ID: References: <20200629183635.8872-1-pete@akeo.ie>,<161D2E94C55C04ED.28420@groups.io> In-Reply-To: <161D2E94C55C04ED.28420@groups.io> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: akeo.ie; dkim=none (message not signed) header.d=none;akeo.ie; dmarc=none action=none header.from=vmware.com; x-originating-ip: [98.214.99.181] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ee69ac0d-1642-4c5e-6f0f-08d81e56d532 x-ms-traffictypediagnostic: BN3PR05MB2434: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:10000; x-forefront-prvs: 0452022BE1 x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: M0ZUW/gCTBtvRi3mb7zajbIMs+zBYnMyOI/ZxXsvNjXQyzzFouQhejq7SwdUrSXUfOBaFqMP61Xb5Z9KbjNeeQpfiqBp/C1oGMNabN6UWMlOT6DQu2Kf00e0Qy/8s4L7EfIxhqfzAaPZiCmYkhYEH0PoK/wqdELiq4DdKwMSQ8l0F3Q6tcydvY+NKcOWMw7Fs/pB7y4iJ+gYGi3gcaTfHEOX2omFEN4VKjhtbLRUYXjZsBCJbqmzHTihR53dA7yV8yDl8C0hwQU8mhMxmydS2+K2MSjgSlPkAShq2XXraeWlO3WLIyZ9dxj9IHEZiVgXDpKG9Vdp/DRuH21b+kyzZWQNN6bRTJG6uiP4so6++oFwTb1QO7EYfa291uWyjp9ig9pZhu2iITHZAT85q0LGyg== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN6PR05MB3411.namprd05.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(346002)(366004)(39860400002)(376002)(136003)(396003)(166002)(26005)(55016002)(64756008)(66946007)(66556008)(76116006)(186003)(66476007)(66446008)(9686003)(4326008)(54906003)(316002)(2906002)(110136005)(5660300002)(52536014)(6506007)(86362001)(19627405001)(83380400001)(7696005)(71200400001)(33656002)(53546011)(8676002)(8936002)(76236003)(478600001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata: 5PqpDks31YaJNN/a0PA5Y1onqjo9L0rw4aFFsf3KZBO3r0owsLSzVphNe5/zaaE1VD+u7JEzZPPs5fPy2XtiCpuqEk8k3qOBZdy0UnNX6MqHxui7cnrZeLUFkSSz5cPpiMn6t+tJJOB/NtuTJmR7kTomxFCvs/OmNGeKHT74/sOiYYZ1APzkBoxcT8fGU+XmOzyMCS5fSRZFyPe1DZHspAZYgUnk3Y/H2kHQBt+/Qshml3gJcg4sPu5WhovE2jNRTe/9KkInGuaiQSxxQscXPegcyUg5BvXB8JnsKyejsoTo9azyBWFjIiHCkTE4BlUysXM4K3TX6vArBWG/WYPjbkbg0Fgo2E0tgjvHRDEi+mHNdiky75J+kMLZ+9cOHr8zdXdNz6VTce1G9XaLRdvwUiBmHu64zoVX1OtUHyh/MWXTSizPCcy9eULJaR2FZQjNK+PYpTi1OuoScYRUGGKLBi+Lgs7l7/9s0OxAAslO5cMLUxVZEMGnNDMBNVH78KDv MIME-Version: 1.0 X-OriginatorOrg: vmware.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BN6PR05MB3411.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ee69ac0d-1642-4c5e-6f0f-08d81e56d532 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Jul 2020 07:09:16.4498 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b39138ca-3cee-4b4a-a4d6-cd83d9dd62f0 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: I7e4YTfp4yoM6WzG7Aoa+3nXGRzQ+z3iHH3k3Q09cG1qW0amXY+blfl6MEzkHPmDEi/XHC9gew3lgy2GHMMa2A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3PR05MB2434 Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_BN6PR05MB3411B40E279E072D51CD3A8FB96D0BN6PR05MB3411namp_" --_000_BN6PR05MB3411B40E279E072D51CD3A8FB96D0BN6PR05MB3411namp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Testing on the CM3, I don't have MMC storage working. So that's a regressio= n. I also tested Pi 2 and it was fine. I'll take a look (with no timeline). I am okay if you consider merging Pet= e's patch. I don't think there are that many CM3 UEFI users out there, and = Pete's change greatly improves the USB boot story for Pi 3 and Pi 4. A ________________________________ From: devel@edk2.groups.io on behalf of Andrei Wark= entin via groups.io Sent: Monday, June 29, 2020 8:37 PM To: Pete Batard ; devel@edk2.groups.io Cc: ard.biesheuvel@arm.com ; leif@nuviainc.com Subject: Re: [edk2-devel] [edk2-platforms][PATCH 1/1] Platform/RaspberryPi= /Drivers: Add SD card detection Looks good to me, but I would like to (personally) first test on CM3 (whic= h has eMMC), before this is merged. Stay tuned... And thanks again for working on this. I like the approach taken. A ________________________________ From: Pete Batard Sent: Monday, June 29, 2020 1:36 PM To: devel@edk2.groups.io Cc: ard.biesheuvel@arm.com ; leif@nuviainc.com ; Andrei Warkentin Subject: [edk2-platforms][PATCH 1/1] Platform/RaspberryPi/Drivers: Add SD = card detection The Raspberry Pi 3 and Pi 4 platforms (with latest EEPROM) can boot straight from USB, without the need for an SD card being present. However, the IsCardPresent () calls from the ArasanMmcHost and SdHost drivers are currently hardwired to return TRUE, which results in straight to USB boot failing due to the SD drivers looping on errors while trying to poke at a non-existent card... Ideally, we would use the Card Detect signal from the uSD slot, to report on the presence or absence of a card, but the Raspberry Pi Foundation did not wire those signals in the Pi 2 and subsequent models, leaving us with only potentially interfering SD commands as means to perform card detection. As a result of this, we are left with no other choice but limit detection to occurring only once, prior to formal SD card init, and then return the detected value for subsequent calls. This, however, is more than good enough for the intended purpose, which is to allow straight to USB boot. Tested on Raspberry Pi 3 and 4, and for both SD controllers. Addresses pftf/RPi3#13, pftf/RPi3#14, pftf/RPi4#37. Signed-off-by: Pete Batard --- Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c | 66 +++= +++++++++++++--- Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c | 69 +++= ++++++++++++++--- Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h | 6 ++ 3 files changed, 122 insertions(+), 19 deletions(-) diff --git a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDx= e.c b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c index 6d706af6f276..08e5be1f015f 100644 --- a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c +++ b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c @@ -11,7 +11,8 @@ #define DEBUG_MMCHOST_SD DEBUG_VERBOSE -BOOLEAN PreviousIsCardPresent =3D FALSE; +BOOLEAN CardIsPresent =3D FALSE; +CARD_DETECT_STATE CardDetectState =3D CardDetectRequired; UINT32 LastExecutedCommand =3D (UINT32) -1; STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol; @@ -239,14 +240,6 @@ CalculateClockFrequencyDivisor ( return EFI_SUCCESS; } -BOOLEAN -MMCIsCardPresent ( - IN EFI_MMC_HOST_PROTOCOL *This -) -{ - return TRUE; -} - BOOLEAN MMCIsReadOnly ( IN EFI_MMC_HOST_PROTOCOL *This @@ -418,6 +411,10 @@ MMCNotifyState ( DEBUG ((DEBUG_MMCHOST_SD, "ArasanMMCHost: MMCNotifyState(State: %d)\n",= State)); + // Stall all operations except init until card detection has occurred. + if (State !=3D MmcHwInitializationState && CardDetectState !=3D CardDet= ectCompleted) + return EFI_NOT_READY; + switch (State) { case MmcHwInitializationState: { @@ -489,6 +486,57 @@ MMCNotifyState ( return EFI_SUCCESS; } +BOOLEAN +MMCIsCardPresent ( + IN EFI_MMC_HOST_PROTOCOL *This +) +{ + EFI_STATUS Status; + + // + // If we are already in progress (we may get concurrent calls) + // or completed the detection, just return the current value. + // + if (CardDetectState !=3D CardDetectRequired) + return CardIsPresent; + + CardDetectState =3D CardDetectInProgress; + CardIsPresent =3D FALSE; + + // + // The two following commands should succeed even if no card is present= . + // + Status =3D MMCNotifyState (This, MmcHwInitializationState); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "MMCIsCardPresent: Error MmcHwInitializationStat= e, Status=3D%r.\n", Status)); + // If we failed init, go back to requiring card detection + CardDetectState =3D CardDetectRequired; + return FALSE; + } + + Status =3D MMCSendCommand (This, MMC_CMD0, 0); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "MMCIsCardPresent: CMD0 Error, Status=3D%r.\n", = Status)); + goto out; + } + + // + // CMD8 should tell us if a card is present. + // + Status =3D MMCSendCommand (This, MMC_CMD8, CMD8_SD_ARG); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "MMCIsCardPresent: No card detected, Status=3D%r.= \n", Status)); + goto out; + } + + DEBUG ((DEBUG_INFO, "MMCIsCardPresent: Card detected.\n")); + CardIsPresent =3D TRUE; + +out: + CardDetectState =3D CardDetectCompleted; + return CardIsPresent; +} + EFI_STATUS MMCReceiveResponse ( IN EFI_MMC_HOST_PROTOCOL *This, diff --git a/Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c b/Platform= /RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c index 2f31c5eb8c46..d96344fd0f8e 100644 --- a/Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c +++ b/Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c @@ -64,7 +64,9 @@ STATIC CONST CHAR8 *mFsmState[] =3D { "identmode", "data= mode", "readdata", "genpulses", "writewait2", "?", "startpowdown" }; #endif /* NDEBUG */ -STATIC UINT32 mLastGoodCmd =3D MMC_GET_INDX (MMC_CMD0); +STATIC BOOLEAN CardIsPresent =3D FALSE; +STATIC CARD_DETECT_STATE CardDetectState =3D CardDetectRequired; +static UINT32 mLastGoodCmd =3D MMC_GET_INDX (MMC_CMD0); STATIC inline BOOLEAN IsAppCmd ( @@ -264,15 +266,7 @@ SdHostSetClockFrequency ( return Status; } -STATIC BOOLEAN -SdIsCardPresent ( - IN EFI_MMC_HOST_PROTOCOL *This - ) -{ - return TRUE; -} - -STATIC BOOLEAN +static BOOLEAN SdIsReadOnly ( IN EFI_MMC_HOST_PROTOCOL *This ) @@ -639,6 +633,10 @@ SdNotifyState ( { DEBUG ((DEBUG_MMCHOST_SD, "SdHost: SdNotifyState(State: %d) ", State)); + // Stall all operations except init until card detection has occurred. + if (State !=3D MmcHwInitializationState && CardDetectState !=3D CardDet= ectCompleted) + return EFI_NOT_READY; + switch (State) { case MmcHwInitializationState: DEBUG ((DEBUG_MMCHOST_SD, "MmcHwInitializationState\n", State)); @@ -718,6 +716,57 @@ SdNotifyState ( return EFI_SUCCESS; } +STATIC BOOLEAN +SdIsCardPresent ( + IN EFI_MMC_HOST_PROTOCOL *This + ) +{ + EFI_STATUS Status; + + // + // If we are already in progress (we may get concurrent calls) + // or completed the detection, just return the current value. + // + if (CardDetectState !=3D CardDetectRequired) + return CardIsPresent; + + CardDetectState =3D CardDetectInProgress; + CardIsPresent =3D FALSE; + + // + // The two following commands should succeed even if no card is present= . + // + Status =3D SdNotifyState (This, MmcHwInitializationState); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "SdIsCardPresent: Error MmcHwInitializationState= , Status=3D%r.\n", Status)); + // If we failed init, go back to requiring card detection + CardDetectState =3D CardDetectRequired; + return FALSE; + } + + Status =3D SdSendCommand (This, MMC_CMD0, 0); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "SdIsCardPresent: CMD0 Error, Status=3D%r.\n", S= tatus)); + goto out; + } + + // + // CMD8 should tell us if a card is present. + // + Status =3D SdSendCommand (This, MMC_CMD8, CMD8_SD_ARG); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "SdIsCardPresent: No card detected, Status=3D%r.\= n", Status)); + goto out; + } + + DEBUG ((DEBUG_INFO, "SdIsCardPresent: Card detected.\n")); + CardIsPresent =3D TRUE; + +out: + CardDetectState =3D CardDetectCompleted; + return CardIsPresent; +} + BOOLEAN SdIsMultiBlock ( IN EFI_MMC_HOST_PROTOCOL *This diff --git a/Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h b/Platform= /RaspberryPi/Include/Protocol/RpiMmcHost.h index c558e00bf500..78514a31bc4e 100644 --- a/Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h +++ b/Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h @@ -82,6 +82,12 @@ typedef enum _MMC_STATE { MmcDisconnectState, } MMC_STATE; +typedef enum _CARD_DETECT_STATE { + CardDetectRequired =3D 0, + CardDetectInProgress, + CardDetectCompleted +} CARD_DETECT_STATE; + #define EMMCBACKWARD (0) #define EMMCHS26 (1 << 0) // High-Speed @26MHz at rated = device voltages #define EMMCHS52 (1 << 1) // High-Speed @52MHz at rated = device voltages -- 2.21.0.windows.1 --_000_BN6PR05MB3411B40E279E072D51CD3A8FB96D0BN6PR05MB3411namp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
Testing on the CM3, I don't have MMC storage working. So that's a regressi= on. I also tested Pi 2 and it was fine.

I'll take a look (with no timeline). I am okay if you consider merging Pet= e's patch. I don't think there are that many CM3 UEFI users out there, and = Pete's change greatly improves the USB boot story for Pi 3 and Pi 4.

A

From: devel@edk2.groups.io= <devel@edk2.groups.io> on behalf of Andrei Warkentin via groups.io &= lt;awarkentin=3Dvmware.com@groups.io>
Sent: Monday, June 29, 2020 8:37 PM
To: Pete Batard <pete@akeo.ie>; devel@edk2.groups.io <deve= l@edk2.groups.io>
Cc: ard.biesheuvel@arm.com <ard.biesheuvel@arm.com>; leif@nuv= iainc.com <leif@nuviainc.com>
Subject: Re: [edk2-devel] [edk2-platforms][PATCH 1/1] Platform/Rasp= berryPi/Drivers: Add SD card detection
 
Looks good to me, but I would like to (personally) first test on CM3 (whic= h has eMMC), before this is merged. Stay tuned...

And thanks again for working on this. I like the approach taken.


From: Pete Batard <pe= te@akeo.ie>
Sent: Monday, June 29, 2020 1:36 PM
To: devel@edk2.groups.io <devel@edk2.groups.io>
Cc: ard.biesheuvel@arm.com <ard.biesheuvel@arm.com>; leif@nuv= iainc.com <leif@nuviainc.com>; Andrei Warkentin <awarkentin@vmware= .com>
Subject: [edk2-platforms][PATCH 1/1] Platform/RaspberryPi/Drivers: = Add SD card detection
 
The Raspberry Pi 3 and Pi 4 platforms (with lat= est EEPROM) can boot
straight from USB, without the need for an SD card being present.
However, the IsCardPresent () calls from the ArasanMmcHost and SdHost
drivers are currently hardwired to return TRUE, which results in
straight to USB boot failing due to the SD drivers looping on
errors while trying to poke at a non-existent card...

Ideally, we would use the Card Detect signal from the uSD slot, to
report on the presence or absence of a card, but the Raspberry Pi
Foundation did not wire those signals in the Pi 2 and subsequent
models, leaving us with only potentially interfering SD commands
as means to perform card detection.

As a result of this, we are left with no other choice but limit
detection to occurring only once, prior to formal SD card init,
and then return the detected value for subsequent calls. This,
however, is more than good enough for the intended purpose, which
is to allow straight to USB boot.

Tested on Raspberry Pi 3 and 4, and for both SD controllers.

Addresses pftf/RPi3#13, pftf/RPi3#14, pftf/RPi4#37.

Signed-off-by: Pete Batard <pete@akeo.ie>
---
 Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c | 6= 6 ++++++++++++++= 3;+---
 Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c   =             | 69 = 3;++++++++++++++= 3;+---
 Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h   =             |  = 6 ++
 3 files changed, 122 insertions(+), 19 deletions(-)

diff --git a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDx= e.c b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
index 6d706af6f276..08e5be1f015f 100644
--- a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
+++ b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcH= ostDxe.c
@@ -11,7 +11,8 @@
 
 #define DEBUG_MMCHOST_SD DEBUG_VERBOSE
 
-BOOLEAN PreviousIsCardPresent =3D FALSE;
+BOOLEAN CardIsPresent =3D FALSE;
+CARD_DETECT_STATE CardDetectState =3D CardDetectRequired;
 UINT32 LastExecutedCommand =3D (UINT32) -1;
 
 STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
@@ -239,14 +240,6 @@ CalculateClockFrequencyDivisor (
   return EFI_SUCCESS;
 }
 
-BOOLEAN
-MMCIsCardPresent (
-  IN EFI_MMC_HOST_PROTOCOL *This
-)
-{
-  return TRUE;
-}
-
 BOOLEAN
 MMCIsReadOnly (
   IN EFI_MMC_HOST_PROTOCOL *This
@@ -418,6 +411,10 @@ MMCNotifyState (
 
   DEBUG ((DEBUG_MMCHOST_SD, "ArasanMMCHost: MMCNotifyState= (State: %d)\n", State));
 
+  // Stall all operations except init until card detection has o= ccurred.
+  if (State !=3D MmcHwInitializationState && CardDetectS= tate !=3D CardDetectCompleted)
+    return EFI_NOT_READY;
+
   switch (State) {
   case MmcHwInitializationState:
     {
@@ -489,6 +486,57 @@ MMCNotifyState (
   return EFI_SUCCESS;
 }
 
+BOOLEAN
+MMCIsCardPresent (
+  IN EFI_MMC_HOST_PROTOCOL *This
+)
+{
+  EFI_STATUS Status;
+
+  //
+  // If we are already in progress (we may get concurrent calls)=
+  // or completed the detection, just return the current value.<= br> +  //
+  if (CardDetectState !=3D CardDetectRequired)
+    return CardIsPresent;
+
+  CardDetectState =3D CardDetectInProgress;
+  CardIsPresent =3D FALSE;
+
+  //
+  // The two following commands should succeed even if no card i= s present.
+  //
+  Status =3D MMCNotifyState (This, MmcHwInitializationState); +  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "MMCIsCardPresent: Error= MmcHwInitializationState, Status=3D%r.\n", Status));
+    // If we failed init, go back to requiring card de= tection
+    CardDetectState =3D CardDetectRequired;
+    return FALSE;
+  }
+
+  Status =3D MMCSendCommand (This, MMC_CMD0, 0);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "MMCIsCardPresent: CMD0 = Error, Status=3D%r.\n", Status));
+    goto out;
+  }
+
+  //
+  // CMD8 should tell us if a card is present.
+  //
+  Status =3D MMCSendCommand (This, MMC_CMD8, CMD8_SD_ARG);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_INFO, "MMCIsCardPresent: No car= d detected, Status=3D%r.\n", Status));
+    goto out;
+  }
+
+  DEBUG ((DEBUG_INFO, "MMCIsCardPresent: Card detected.\n&q= uot;));
+  CardIsPresent =3D TRUE;
+
+out:
+  CardDetectState =3D CardDetectCompleted;
+  return CardIsPresent;
+}
+
 EFI_STATUS
 MMCReceiveResponse (
   IN EFI_MMC_HOST_PROTOCOL    *This,
diff --git a/Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c b/Platform= /RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c
index 2f31c5eb8c46..d96344fd0f8e 100644
--- a/Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c
+++ b/Platform/RaspberryPi/Drivers/SdHostDxe/SdHostDxe.c
@@ -64,7 +64,9 @@ STATIC CONST CHAR8 *mFsmState[] =3D { "identmod= e", "datamode", "readdata",
            &n= bsp;            = ;            "g= enpulses", "writewait2", "?",
            &n= bsp;            = ;            "s= tartpowdown" };
 #endif /* NDEBUG */
-STATIC UINT32 mLastGoodCmd =3D MMC_GET_INDX (MMC_CMD0);
+STATIC BOOLEAN CardIsPresent =3D FALSE;
+STATIC CARD_DETECT_STATE CardDetectState =3D CardDetectRequired;
+static UINT32 mLastGoodCmd =3D MMC_GET_INDX (MMC_CMD0);
 
 STATIC inline BOOLEAN
 IsAppCmd (
@@ -264,15 +266,7 @@ SdHostSetClockFrequency (
   return Status;
 }
 
-STATIC BOOLEAN
-SdIsCardPresent (
-  IN EFI_MMC_HOST_PROTOCOL *This
-  )
-{
-  return TRUE;
-}
-
-STATIC BOOLEAN
+static BOOLEAN
 SdIsReadOnly (
   IN EFI_MMC_HOST_PROTOCOL *This
   )
@@ -639,6 +633,10 @@ SdNotifyState (
 {
   DEBUG ((DEBUG_MMCHOST_SD, "SdHost: SdNotifyState(State: = %d) ", State));
 
+  // Stall all operations except init until card detection has o= ccurred.
+  if (State !=3D MmcHwInitializationState && CardDetectS= tate !=3D CardDetectCompleted)
+    return EFI_NOT_READY;
+
   switch (State) {
   case MmcHwInitializationState:
     DEBUG ((DEBUG_MMCHOST_SD, "MmcHwInitializati= onState\n", State));
@@ -718,6 +716,57 @@ SdNotifyState (
   return EFI_SUCCESS;
 }
 
+STATIC BOOLEAN
+SdIsCardPresent (
+  IN EFI_MMC_HOST_PROTOCOL *This
+  )
+{
+  EFI_STATUS Status;
+
+  //
+  // If we are already in progress (we may get concurrent calls)=
+  // or completed the detection, just return the current value.<= br> +  //
+  if (CardDetectState !=3D CardDetectRequired)
+    return CardIsPresent;
+
+  CardDetectState =3D CardDetectInProgress;
+  CardIsPresent =3D FALSE;
+
+  //
+  // The two following commands should succeed even if no card i= s present.
+  //
+  Status =3D SdNotifyState (This, MmcHwInitializationState);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "SdIsCardPresent: Error = MmcHwInitializationState, Status=3D%r.\n", Status));
+    // If we failed init, go back to requiring card de= tection
+    CardDetectState =3D CardDetectRequired;
+    return FALSE;
+  }
+
+  Status =3D SdSendCommand (This, MMC_CMD0, 0);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "SdIsCardPresent: CMD0 E= rror, Status=3D%r.\n", Status));
+    goto out;
+  }
+
+  //
+  // CMD8 should tell us if a card is present.
+  //
+  Status =3D SdSendCommand (This, MMC_CMD8, CMD8_SD_ARG);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_INFO, "SdIsCardPresent: No card= detected, Status=3D%r.\n", Status));
+    goto out;
+  }
+
+  DEBUG ((DEBUG_INFO, "SdIsCardPresent: Card detected.\n&qu= ot;));
+  CardIsPresent =3D TRUE;
+
+out:
+  CardDetectState =3D CardDetectCompleted;
+  return CardIsPresent;
+}
+
 BOOLEAN
 SdIsMultiBlock (
   IN EFI_MMC_HOST_PROTOCOL *This
diff --git a/Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h b/Platform= /RaspberryPi/Include/Protocol/RpiMmcHost.h
index c558e00bf500..78514a31bc4e 100644
--- a/Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h
+++ b/Platform/RaspberryPi/Include/Protocol/RpiMmcHost.h
@@ -82,6 +82,12 @@ typedef enum _MMC_STATE {
     MmcDisconnectState,
 } MMC_STATE;
 
+typedef enum _CARD_DETECT_STATE {
+    CardDetectRequired =3D 0,
+    CardDetectInProgress,
+    CardDetectCompleted
+} CARD_DETECT_STATE;
+
 #define EMMCBACKWARD        = (0)
 #define EMMCHS26        &nbs= p;    (1 << 0)      // High-S= peed @26MHz at rated device voltages
 #define EMMCHS52        &nbs= p;    (1 << 1)      // High-S= peed @52MHz at rated device voltages
--
2.21.0.windows.1

--_000_BN6PR05MB3411B40E279E072D51CD3A8FB96D0BN6PR05MB3411namp_--