From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web10.8164.1609864473866386545 for ; Tue, 05 Jan 2021 08:34:34 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: jeremy.linton@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8B5501435; Tue, 5 Jan 2021 08:34:28 -0800 (PST) Received: from mammon-tx2.austin.arm.com (mammon-tx2.austin.arm.com [10.118.28.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8317B3F70D; Tue, 5 Jan 2021 08:34:28 -0800 (PST) From: "Jeremy Linton" To: devel@edk2.groups.io Cc: ard.biesheuvel@arm.com, leif@nuviainc.com, pete@akeo.ie, samer.el-haj-mahmoud@arm.com, awarkentin@vmware.com, Jeremy Linton Subject: [PATCH v4 4/7] Platform/RaspberryPi/Arasan: Add write delay and voltage/clock config Date: Tue, 5 Jan 2021 10:34:17 -0600 Message-Id: <20210105163420.1711652-5-jeremy.linton@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210105163420.1711652-1-jeremy.linton@arm.com> References: <20210105163420.1711652-1-jeremy.linton@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable The uboot and Linux drivers have notes that there is a clock domain crossing problem that happens with back to back writes to the SD controllers on the rpi. Its not clear if this is still applicable to the rpi4/eMMC2 but it seems wise to add it. Further, we need to assure that the card voltage is set to 3.3V, and we should try and follow some of the SDHCI docs when it comes to changing the clock. Signed-off-by: Jeremy Linton Reviewed-by: Andrei Warkentin --- .../Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c | 112 +++++++++++++++++= ---- .../Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.h | 1 + 2 files changed, 93 insertions(+), 20 deletions(-) diff --git a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe= .c b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c index c8fdfc193b..7e7caf40e3 100644 --- a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c +++ b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c @@ -18,6 +18,56 @@ UINT32 LastExecutedCommand =3D (UINT32) -1; STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;=0D STATIC UINTN MmcHsBase;=0D =0D +STATIC=0D +UINT32=0D +EFIAPI=0D +SdMmioWrite32 (=0D + IN UINTN Address,=0D + IN UINT32 Value=0D + )=0D +{=0D + UINT32 ret;=0D + ret =3D (UINT32)MmioWrite32 (Address, Value);=0D + // There is a bug about clock domain crossing on writes, delay to avoid = it=0D + gBS->Stall (STALL_AFTER_REG_WRITE_US);=0D + return ret;=0D +}=0D +=0D +STATIC=0D +UINT32=0D +EFIAPI=0D +SdMmioOr32 (=0D + IN UINTN Address,=0D + IN UINT32 OrData=0D + )=0D +{=0D + return SdMmioWrite32 (Address, MmioRead32 (Address) | OrData);=0D +}=0D +=0D +STATIC=0D +UINT32=0D +EFIAPI=0D +SdMmioAnd32 (=0D + IN UINTN Address,=0D + IN UINT32 AndData=0D + )=0D +{=0D + return SdMmioWrite32 (Address, MmioRead32 (Address) & AndData);=0D +}=0D +=0D +STATIC=0D +UINT32=0D +EFIAPI=0D +SdMmioAndThenOr32 (=0D + IN UINTN Address,=0D + IN UINT32 AndData,=0D + IN UINT32 OrData=0D + )=0D +{=0D + return SdMmioWrite32 (Address, (MmioRead32 (Address) & AndData) | OrData= );=0D +}=0D +=0D +=0D /**=0D These SD commands are optional, according to the SD Spec=0D **/=0D @@ -175,7 +225,9 @@ SoftReset ( IN UINT32 Mask=0D )=0D {=0D - MmioOr32 (MMCHS_SYSCTL, Mask);=0D + DEBUG ((DEBUG_MMCHOST_SD, "SoftReset with mask 0x%x\n", Mask));=0D +=0D + SdMmioOr32 (MMCHS_SYSCTL, Mask);=0D if (PollRegisterWithMask (MMCHS_SYSCTL, Mask, 0) =3D=3D EFI_TIMEOUT) {=0D DEBUG ((DEBUG_ERROR, "Failed to SoftReset with mask 0x%x\n", Mask));=0D return EFI_TIMEOUT;=0D @@ -326,29 +378,29 @@ MMCSendCommand ( }=0D =0D if (IsAppCmd && MmcCmd =3D=3D ACMD22) {=0D - MmioWrite32 (MMCHS_BLK, 4);=0D + SdMmioWrite32 (MMCHS_BLK, 4);=0D } else if (IsAppCmd && MmcCmd =3D=3D ACMD51) {=0D - MmioWrite32 (MMCHS_BLK, 8);=0D + SdMmioWrite32 (MMCHS_BLK, 8);=0D } else if (!IsAppCmd && MmcCmd =3D=3D CMD6) {=0D - MmioWrite32 (MMCHS_BLK, 64);=0D + SdMmioWrite32 (MMCHS_BLK, 64);=0D } else if (IsADTCCmd) {=0D - MmioWrite32 (MMCHS_BLK, BLEN_512BYTES);=0D + SdMmioWrite32 (MMCHS_BLK, BLEN_512BYTES);=0D }=0D =0D // Set Data timeout counter value to max value.=0D - MmioAndThenOr32 (MMCHS_SYSCTL, (UINT32) ~DTO_MASK, DTO_VAL);=0D + SdMmioAndThenOr32 (MMCHS_SYSCTL, (UINT32) ~DTO_MASK, DTO_VAL);=0D =0D //=0D // Clear Interrupt Status Register, but not the Card Inserted bit=0D // to avoid messing with card detection logic.=0D //=0D - MmioWrite32 (MMCHS_INT_STAT, ALL_EN & ~(CARD_INS));=0D + SdMmioWrite32 (MMCHS_INT_STAT, ALL_EN & ~(CARD_INS));=0D =0D // Set command argument register=0D - MmioWrite32 (MMCHS_ARG, Argument);=0D + SdMmioWrite32 (MMCHS_ARG, Argument);=0D =0D // Send the command=0D - MmioWrite32 (MMCHS_CMD, MmcCmd);=0D + SdMmioWrite32 (MMCHS_CMD, MmcCmd);=0D =0D // Check for the command status.=0D while (RetryCount < MAX_RETRY_COUNT) {=0D @@ -373,7 +425,7 @@ MMCSendCommand ( =0D // Check if command is completed.=0D if ((MmcStatus & CC) =3D=3D CC) {=0D - MmioWrite32 (MMCHS_INT_STAT, CC);=0D + SdMmioWrite32 (MMCHS_INT_STAT, CC);=0D break;=0D }=0D =0D @@ -428,6 +480,21 @@ MMCNotifyState ( return Status;=0D }=0D =0D + DEBUG ((DEBUG_MMCHOST_SD, "ArasanMMCHost: CAP %X CAPH %X\n", MmioRea= d32(MMCHS_CAPA),MmioRead32(MMCHS_CUR_CAPA)));=0D +=0D + // Lets switch to card detect test mode.=0D + SdMmioOr32 (MMCHS_HCTL, BIT7|BIT6);=0D +=0D + // set card voltage=0D + SdMmioAnd32 (MMCHS_HCTL, ~SDBP_ON);=0D + SdMmioAndThenOr32 (MMCHS_HCTL, (UINT32) ~SDBP_MASK, SDVS_3_3_V);=0D + SdMmioOr32 (MMCHS_HCTL, SDBP_ON);=0D +=0D + DEBUG ((DEBUG_MMCHOST_SD, "ArasanMMCHost: AC12 %X HCTL %X\n", MmioRe= ad32(MMCHS_AC12),MmioRead32(MMCHS_HCTL)));=0D +=0D + // First turn off the clock=0D + SdMmioAnd32 (MMCHS_SYSCTL, ~CEN);=0D +=0D // Attempt to set the clock to 400Khz which is the expected initiali= zation speed=0D Status =3D CalculateClockFrequencyDivisor (400000, &Divisor, NULL);= =0D if (EFI_ERROR (Status)) {=0D @@ -436,10 +503,15 @@ MMCNotifyState ( }=0D =0D // Set Data Timeout Counter value, set clock frequency, enable inter= nal clock=0D - MmioOr32 (MMCHS_SYSCTL, DTO_VAL | Divisor | CEN | ICS | ICE);=0D + SdMmioOr32 (MMCHS_SYSCTL, DTO_VAL | Divisor | CEN | ICS | ICE);=0D + SdMmioOr32 (MMCHS_HCTL, SDBP_ON);=0D + // wait for ICS=0D + while ((MmioRead32 (MMCHS_SYSCTL) & ICS_MASK) !=3D ICS);=0D +=0D + DEBUG ((DEBUG_MMCHOST_SD, "ArasanMMCHost: AC12 %X HCTL %X\n", MmioRe= ad32(MMCHS_AC12),MmioRead32(MMCHS_HCTL)));=0D =0D // Enable interrupts=0D - MmioWrite32 (MMCHS_IE, ALL_EN);=0D + SdMmioWrite32 (MMCHS_IE, ALL_EN);=0D }=0D break;=0D case MmcIdleState:=0D @@ -452,7 +524,7 @@ MMCNotifyState ( ClockFrequency =3D 25000000;=0D =0D // First turn off the clock=0D - MmioAnd32 (MMCHS_SYSCTL, ~CEN);=0D + SdMmioAnd32 (MMCHS_SYSCTL, ~CEN);=0D =0D Status =3D CalculateClockFrequencyDivisor (ClockFrequency, &Divisor, N= ULL);=0D if (EFI_ERROR (Status)) {=0D @@ -462,13 +534,13 @@ MMCNotifyState ( }=0D =0D // Setup new divisor=0D - MmioAndThenOr32 (MMCHS_SYSCTL, (UINT32) ~CLKD_MASK, Divisor);=0D + SdMmioAndThenOr32 (MMCHS_SYSCTL, (UINT32) ~CLKD_MASK, Divisor);=0D =0D // Wait for the clock to stabilise=0D while ((MmioRead32 (MMCHS_SYSCTL) & ICS_MASK) !=3D ICS);=0D =0D // Set Data Timeout Counter value, set clock frequency, enable interna= l clock=0D - MmioOr32 (MMCHS_SYSCTL, CEN);=0D + SdMmioOr32 (MMCHS_SYSCTL, CEN);=0D break;=0D case MmcTransferState:=0D break;=0D @@ -635,7 +707,7 @@ MMCReadBlockData ( while (RetryCount < MAX_RETRY_COUNT) {=0D MmcStatus =3D MmioRead32 (MMCHS_INT_STAT);=0D if ((MmcStatus & BRR) !=3D 0) {=0D - MmioWrite32 (MMCHS_INT_STAT, BRR);=0D + SdMmioWrite32 (MMCHS_INT_STAT, BRR);=0D /*=0D * Data is ready.=0D */=0D @@ -662,7 +734,7 @@ MMCReadBlockData ( gBS->Stall (STALL_AFTER_READ_US);=0D }=0D =0D - MmioWrite32 (MMCHS_INT_STAT, BRR);=0D + SdMmioWrite32 (MMCHS_INT_STAT, BRR);=0D return EFI_SUCCESS;=0D }=0D =0D @@ -699,13 +771,13 @@ MMCWriteBlockData ( while (RetryCount < MAX_RETRY_COUNT) {=0D MmcStatus =3D MmioRead32 (MMCHS_INT_STAT);=0D if ((MmcStatus & BWR) !=3D 0) {=0D - MmioWrite32 (MMCHS_INT_STAT, BWR);=0D + SdMmioWrite32 (MMCHS_INT_STAT, BWR);=0D /*=0D * Can write data.=0D */=0D mFwProtocol->SetLed (TRUE);=0D for (Count =3D 0; Count < BlockLen; Count +=3D 4, Buffer++) {=0D - MmioWrite32 (MMCHS_DATA, *Buffer);=0D + SdMmioWrite32 (MMCHS_DATA, *Buffer);=0D }=0D =0D mFwProtocol->SetLed (FALSE);=0D @@ -726,7 +798,7 @@ MMCWriteBlockData ( gBS->Stall (STALL_AFTER_WRITE_US);=0D }=0D =0D - MmioWrite32 (MMCHS_INT_STAT, BWR);=0D + SdMmioWrite32 (MMCHS_INT_STAT, BWR);=0D return EFI_SUCCESS;=0D }=0D =0D diff --git a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe= .h b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.h index 6cd600f738..e94606cc5b 100644 --- a/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.h +++ b/Platform/RaspberryPi/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.h @@ -37,6 +37,7 @@ #define STALL_AFTER_REC_RESP_US (50)=0D #define STALL_AFTER_WRITE_US (200)=0D #define STALL_AFTER_READ_US (20)=0D +#define STALL_AFTER_REG_WRITE_US (10)=0D #define STALL_AFTER_RETRY_US (20)=0D =0D #define MAX_DIVISOR_VALUE 1023=0D --=20 2.13.7