From nobody Fri May 17 06:53:51 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+85723+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+85723+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1642389021; cv=none; d=zohomail.com; s=zohoarc; b=idmvhjrRW/Ibzjx2kwUbRXC49kgivoVkM910Ypoc5vyW2GeIZACtSVN3/e010Y2JHWU7AKERbYPGwnSOQB5WFu8BOM8QNs/6qh8K90b39PzcUUtSULOyFIBLA8aBtWTJocGhc6qPg4bwmRR3nX7DqpU7smUW84mLrTv76OriqSc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1642389021; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=D395L14uSXdUr1LuloBQUQR5MXY2EbxdFzxYhJSHBgA=; b=RcoHy6ojP31c6f4izjz9kVHva0P6/ULOVdrgjHhgE/OHhS142bnsJYtzB3hc4GWsKkcMyiJuF3EM8vkIydv2o9RwDrvZ+ZLwbCHOZ7U1erOrJ7y4GlynHRBKgB2+I/7nNyqDviKv3dM5dRtchyVMwqQXV8J5Im/kUUBywOr3LYM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+85723+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1642389021676381.04094560784085; Sun, 16 Jan 2022 19:10:21 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id VUdxYY1788612x3r79lJ6IJT; Sun, 16 Jan 2022 19:10:21 -0800 X-Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web11.6341.1642387290280943867 for ; Sun, 16 Jan 2022 18:41:30 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10229"; a="305272943" X-IronPort-AV: E=Sophos;i="5.88,294,1635231600"; d="scan'208";a="305272943" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jan 2022 18:41:24 -0800 X-IronPort-AV: E=Sophos;i="5.88,294,1635231600"; d="scan'208";a="577904327" X-Received: from roslim1-mobl1.gar.corp.intel.com ([10.249.70.75]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jan 2022 18:41:23 -0800 From: "Aiman Rosli" To: devel@edk2.groups.io Cc: Aiman Rosli Subject: [edk2-devel] [PATCH] MdeModulePkg: Enabling OS boot from SD card through UEFI payload Date: Mon, 17 Jan 2022 10:40:46 +0800 Message-Id: <29abb9443f6246488cd993e2766bddf694af6f1d.1642387164.git.muhammad.aiman.rosli@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,muhammad.aiman.rosli@intel.com X-Gm-Message-State: IDFAUmaxVi7RVNLYok3aYRfAx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1642389021; bh=WXD7RFwIlHPjyN3DQrWak1P4YkicYE6h4+kOWmj6g6Y=; h=Cc:Date:From:Reply-To:Subject:To; b=pHGVq5Ccb9i3HVMBLCukVxnwSZNJpklNz+PpRcgZhfLXmSOk4nQF9JqKrAuGEijdpsJ /31H4RaBPzBptP+jg22cRZ16hfbaQhgcEQR2Yw+UAXPQg5pfUdSm7sYAIPbAa4pn1G9tw yegIyL5JPIvmu7iUpB/s6sacIHQy6p/Eun4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1642389024132100001 Content-Type: text/plain; charset="utf-8" This changes is by adding 50ms delay during voltage swithcing from 3.3V to= 1.8V, plus adding a while loop for 3.3V checking and retrying. Signed-off-by: Aiman Rosli --- MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdDevice.c | 273 ++++++++++-------- 1 file changed, 146 insertions(+), 127 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdDevice.c b/MdeModulePkg/B= us/Pci/SdMmcPciHcDxe/SdDevice.c index 662f9f483c..e91251d457 100644 --- a/MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdDevice.c +++ b/MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdDevice.c @@ -1213,167 +1213,186 @@ SdCardIdentification ( UINT32 PresentState; UINT8 HostCtrl2; UINTN Retry; + BOOLEAN Force3p3v; + + Force3p3v =3D FALSE; =20 PciIo =3D Private->PciIo; PassThru =3D &Private->PassThru; - // - // 1. Send Cmd0 to the device - // - Status =3D SdCardReset (PassThru, Slot); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_INFO, "SdCardIdentification: Executing Cmd0 fails with %= r\n", Status)); - return Status; - } - - // - // 2. Send Cmd8 to the device - // - Status =3D SdCardVoltageCheck (PassThru, Slot, 0x1, 0xFF); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_INFO, "SdCardIdentification: Executing Cmd8 fails with %= r\n", Status)); - return Status; - } - - // - // 3. Send SDIO Cmd5 to the device to the SDIO device OCR register. - // - Status =3D SdioSendOpCond (PassThru, Slot, 0, FALSE); - if (!EFI_ERROR (Status)) { - DEBUG ((DEBUG_INFO, "SdCardIdentification: Found SDIO device, ignore i= t as we don't support\n")); - return EFI_DEVICE_ERROR; - } =20 - // - // 4. Send Acmd41 with voltage window 0 to the device - // - Status =3D SdCardSendOpCond (PassThru, Slot, 0, 0, FALSE, FALSE, FALSE, = &Ocr); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_INFO, "SdCardIdentification: Executing SdCardSendOpCond = fails with %r\n", Status)); - return EFI_DEVICE_ERROR; - } - - if (Private->Capability[Slot].Voltage33 !=3D 0) { + do { // - // Support 3.3V + // 1. Send Cmd0 to the device // - MaxCurrent =3D ((UINT32)Private->MaxCurrent[Slot] & 0xFF) * 4; - } else if (Private->Capability[Slot].Voltage30 !=3D 0) { + Status =3D SdCardReset (PassThru, Slot); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "SdCardIdentification: Executing Cmd0 fails with= %r\n", Status)); + return Status; + } + // - // Support 3.0V + // 2. Send Cmd8 to the device // - MaxCurrent =3D (((UINT32)Private->MaxCurrent[Slot] >> 8) & 0xFF) * 4; - } else if (Private->Capability[Slot].Voltage18 !=3D 0) { + Status =3D SdCardVoltageCheck (PassThru, Slot, 0x1, 0xFF); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "SdCardIdentification: Executing Cmd8 fails with= %r\n", Status)); + return Status; + } + // - // Support 1.8V + // 3. Send SDIO Cmd5 to the device to the SDIO device OCR register. // - MaxCurrent =3D (((UINT32)Private->MaxCurrent[Slot] >> 16) & 0xFF) * 4; - } else { - ASSERT (FALSE); - return EFI_DEVICE_ERROR; - } - - if (MaxCurrent >=3D 150) { - Xpc =3D TRUE; - } else { - Xpc =3D FALSE; - } - - Status =3D SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CTRL_VER, TRUE, sizeof = (ControllerVer), &ControllerVer); - if (EFI_ERROR (Status)) { - return Status; - } - - if (((ControllerVer & 0xFF) >=3D SD_MMC_HC_CTRL_VER_300) && - ((ControllerVer & 0xFF) <=3D SD_MMC_HC_CTRL_VER_420)) - { - S18r =3D TRUE; - } else if (((ControllerVer & 0xFF) =3D=3D SD_MMC_HC_CTRL_VER_100) || ((C= ontrollerVer & 0xFF) =3D=3D SD_MMC_HC_CTRL_VER_200)) { - S18r =3D FALSE; - } else { - ASSERT (FALSE); - return EFI_UNSUPPORTED; - } + Status =3D SdioSendOpCond (PassThru, Slot, 0, FALSE); + if (!EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "SdCardIdentification: Found SDIO device, ignore= it as we don't support\n")); + return EFI_DEVICE_ERROR; + } =20 - // - // 5. Repeatly send Acmd41 with supply voltage window to the device. - // Note here we only support the cards complied with SD physical - // layer simplified spec version 2.0 and version 3.0 and above. - // - Ocr =3D 0; - Retry =3D 0; - do { - Status =3D SdCardSendOpCond (PassThru, Slot, 0, Ocr, S18r, Xpc, TRUE, = &Ocr); + // + // 4. Send Acmd41 with voltage window 0 to the device + // + Status =3D SdCardSendOpCond (PassThru, Slot, 0, 0, FALSE, FALSE, FALSE= , &Ocr); if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "SdCardIdentification: SdCardSendOpCond fails w= ith %r Ocr %x, S18r %x, Xpc %x\n", Status, Ocr, S18r, Xpc)); + DEBUG ((DEBUG_INFO, "SdCardIdentification: Executing SdCardSendOpCon= d fails with %r\n", Status)); return EFI_DEVICE_ERROR; } =20 - if (Retry++ =3D=3D 100) { - DEBUG ((DEBUG_ERROR, "SdCardIdentification: SdCardSendOpCond fails t= oo many times\n")); + if (Private->Capability[Slot].Voltage33 !=3D 0) { + // + // Support 3.3V + // + MaxCurrent =3D ((UINT32)Private->MaxCurrent[Slot] & 0xFF) * 4; + } else if (Private->Capability[Slot].Voltage30 !=3D 0) { + // + // Support 3.0V + // + MaxCurrent =3D (((UINT32)Private->MaxCurrent[Slot] >> 8) & 0xFF) * 4; + } else if (Private->Capability[Slot].Voltage18 !=3D 0) { + // + // Support 1.8V + // + MaxCurrent =3D (((UINT32)Private->MaxCurrent[Slot] >> 16) & 0xFF) * = 4; + } else { + ASSERT (FALSE); return EFI_DEVICE_ERROR; } =20 - gBS->Stall (10 * 1000); - } while ((Ocr & BIT31) =3D=3D 0); + if (MaxCurrent >=3D 150) { + Xpc =3D TRUE; + } else { + Xpc =3D FALSE; + } =20 - // - // 6. If the S18A bit is set and the Host Controller supports 1.8V signa= ling - // (One of support bits is set to 1: SDR50, SDR104 or DDR50 in the - // Capabilities register), switch its voltage to 1.8V. - // - if (((Private->Capability[Slot].Sdr50 !=3D 0) || - (Private->Capability[Slot].Sdr104 !=3D 0) || - (Private->Capability[Slot].Ddr50 !=3D 0)) && - ((Ocr & BIT24) !=3D 0)) - { - Status =3D SdCardVoltageSwitch (PassThru, Slot); + Status =3D SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CTRL_VER, TRUE, sizeo= f (ControllerVer), &ControllerVer); if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "SdCardIdentification: Executing SdCardVoltageS= witch fails with %r\n", Status)); - Status =3D EFI_DEVICE_ERROR; - goto Error; + return Status; + } + + if (((ControllerVer & 0xFF) >=3D SD_MMC_HC_CTRL_VER_300) && + ((ControllerVer & 0xFF) <=3D SD_MMC_HC_CTRL_VER_420)) + { + S18r =3D TRUE; + } else if (((ControllerVer & 0xFF) =3D=3D SD_MMC_HC_CTRL_VER_100) || (= (ControllerVer & 0xFF) =3D=3D SD_MMC_HC_CTRL_VER_200)) { + S18r =3D FALSE; } else { - Status =3D SdMmcHcStopClock (PciIo, Slot); + ASSERT (FALSE); + return EFI_UNSUPPORTED; + } + + // + // 1.8V had failed in the previous run, forcing a retry with 3.3V inst= ead + // + if (Force3p3v =3D=3D TRUE) { + S18r =3D FALSE; + Force3p3v =3D FALSE; + } + + // + // 5. Repeatly send Acmd41 with supply voltage window to the device. + // Note here we only support the cards complied with SD physical + // layer simplified spec version 2.0 and version 3.0 and above. + // + Ocr =3D 0; + Retry =3D 0; + do { + Status =3D SdCardSendOpCond (PassThru, Slot, 0, Ocr, S18r, Xpc, TRUE= , &Ocr); if (EFI_ERROR (Status)) { - Status =3D EFI_DEVICE_ERROR; - goto Error; + DEBUG ((DEBUG_ERROR, "SdCardIdentification: SdCardSendOpCond fails= with %r Ocr %x, S18r %x, Xpc %x\n", Status, Ocr, S18r, Xpc)); + return EFI_DEVICE_ERROR; } =20 - SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_PRESENT_STATE, TRUE, sizeof (P= resentState), &PresentState); - if (((PresentState >> 20) & 0xF) !=3D 0) { - DEBUG ((DEBUG_ERROR, "SdCardIdentification: SwitchVoltage fails wi= th PresentState =3D 0x%x\n", PresentState)); - Status =3D EFI_DEVICE_ERROR; - goto Error; + if (Retry++ =3D=3D 100) { + DEBUG ((DEBUG_ERROR, "SdCardIdentification: SdCardSendOpCond fails= too many times\n")); + return EFI_DEVICE_ERROR; } =20 - HostCtrl2 =3D BIT3; - SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, sizeof (HostCtrl2)= , &HostCtrl2); - - gBS->Stall (5000); + gBS->Stall (10 * 1000); + } while ((Ocr & BIT31) =3D=3D 0); =20 - SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, TRUE, sizeof (Host= Ctrl2), &HostCtrl2); - if ((HostCtrl2 & BIT3) =3D=3D 0) { - DEBUG ((DEBUG_ERROR, "SdCardIdentification: SwitchVoltage fails wi= th HostCtrl2 =3D 0x%x\n", HostCtrl2)); + // + // 6. If the S18A bit is set and the Host Controller supports 1.8V sig= naling + // (One of support bits is set to 1: SDR50, SDR104 or DDR50 in the + // Capabilities register), switch its voltage to 1.8V. + // + if (((Private->Capability[Slot].Sdr50 !=3D 0) || + (Private->Capability[Slot].Sdr104 !=3D 0) || + (Private->Capability[Slot].Ddr50 !=3D 0)) && + ((Ocr & BIT24) !=3D 0)) + { + Status =3D SdCardVoltageSwitch (PassThru, Slot); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "SdCardIdentification: Executing SdCardVoltag= eSwitch fails with %r\n", Status)); Status =3D EFI_DEVICE_ERROR; goto Error; - } + } else { + Status =3D SdMmcHcStopClock (PciIo, Slot); + if (EFI_ERROR (Status)) { + Status =3D EFI_DEVICE_ERROR; + goto Error; + } =20 - Status =3D SdMmcHcStartSdClock (PciIo, Slot); - if (EFI_ERROR (Status)) { - goto Error; - } + SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_PRESENT_STATE, TRUE, sizeof = (PresentState), &PresentState); + if (((PresentState >> 20) & 0xF) !=3D 0) { + DEBUG ((DEBUG_ERROR, "SdCardIdentification: SwitchVoltage fails = with PresentState =3D 0x%x\n", PresentState)); + Status =3D EFI_DEVICE_ERROR; + goto Error; + } =20 - gBS->Stall (1000); + HostCtrl2 =3D BIT3; + SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, sizeof (HostCtrl= 2), &HostCtrl2); =20 - SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_PRESENT_STATE, TRUE, sizeof (P= resentState), &PresentState); - if (((PresentState >> 20) & 0xF) !=3D 0xF) { - DEBUG ((DEBUG_ERROR, "SdCardIdentification: SwitchVoltage fails wi= th PresentState =3D 0x%x, It should be 0xF\n", PresentState)); - Status =3D EFI_DEVICE_ERROR; - goto Error; + gBS->Stall (5000); + + SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, TRUE, sizeof (Ho= stCtrl2), &HostCtrl2); + if ((HostCtrl2 & BIT3) =3D=3D 0) { + DEBUG ((DEBUG_ERROR, "SdCardIdentification: SwitchVoltage fails = with HostCtrl2 =3D 0x%x\n", HostCtrl2)); + Status =3D EFI_DEVICE_ERROR; + goto Error; + } + + Status =3D SdMmcHcStartSdClock (PciIo, Slot); + if (EFI_ERROR (Status)) { + goto Error; + } + + // Workaround to add a delay of 100ms in order for clock to stabil= ize before turning on the SD card again. + gBS->Stall (50000); + + gBS->Stall (1000); + + SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_PRESENT_STATE, TRUE, sizeof = (PresentState), &PresentState); + if (((PresentState >> 20) & 0xF) !=3D 0xF) { + DEBUG ((DEBUG_ERROR, "SdCardIdentification: SwitchVoltage fails = with PresentState =3D 0x%x, It should be 0xF\n", PresentState)); + Status =3D SdMmcHcReset (Private, Slot); + Status =3D SdMmcHcInitHost (Private, Slot); + Force3p3v =3D TRUE; + DEBUG ((DEBUG_ERROR, "SdCardIdentification: Switching to 1.8V ha= d failed in the previous run, forcing a retry with 3.3V instead\n")); + } } - } =20 - DEBUG ((DEBUG_INFO, "SdCardIdentification: Switch to 1.8v signal volta= ge success\n")); - } + DEBUG ((DEBUG_INFO, "SdCardIdentification: Switch to 1.8v signal vol= tage success\n")); + } + } while (Force3p3v); =20 Status =3D SdCardAllSendCid (PassThru, Slot); if (EFI_ERROR (Status)) { --=20 2.34.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#85723): https://edk2.groups.io/g/devel/message/85723 Mute This Topic: https://groups.io/mt/88477724/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-