Skip to content

Commit

Permalink
dwcsdhc: Adopt rk_sip_sdmmc_lib for clock control
Browse files Browse the repository at this point in the history
This allows the driver to work in crashdump mode.

Signed-off-by: Mario Bălănică <mariobalanica02@gmail.com>
  • Loading branch information
mariobalanica committed Jun 22, 2024
1 parent 7ef5269 commit 57b6fee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 183 deletions.
147 changes: 6 additions & 141 deletions drivers/sd/dwcsdhc/dwcsdhc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Module Name:
#include "precomp.h"
#pragma hdrstop
#include <acpiutil.hpp>
#include <rk_sip_sdmmc.h>

#include "dwcsdhc.h"

Expand Down Expand Up @@ -221,98 +222,6 @@ Return value:
sizeof(SdhcExtension->Capabilities));
}

BOOLEAN
FindPhysicalBaseInCmResList(
_In_ PHYSICAL_ADDRESS PhysicalBase,
_In_ PCM_RESOURCE_LIST ResListTranslated
)
{
PCM_FULL_RESOURCE_DESCRIPTOR FullResDescriptor;
FullResDescriptor = ResListTranslated->List;

for (ULONG FullResIndex = 0; FullResIndex < ResListTranslated->Count; FullResIndex++) {

for (ULONG PartialResIndex = 0;
PartialResIndex < FullResDescriptor->PartialResourceList.Count;
PartialResIndex++) {

PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialResDescriptor;
PartialResDescriptor =
FullResDescriptor->PartialResourceList.PartialDescriptors + PartialResIndex;

switch (PartialResDescriptor->Type)
{
case CmResourceTypeMemory:
if (PartialResDescriptor->u.Memory.Length == SDHC_EXPECTED_ACPI_LENGTH
&& PartialResDescriptor->u.Memory.Start.QuadPart == PhysicalBase.QuadPart) {
return TRUE;
}
break;
}
}

FullResDescriptor = (PCM_FULL_RESOURCE_DESCRIPTOR)(FullResDescriptor->PartialResourceList.PartialDescriptors +
FullResDescriptor->PartialResourceList.Count);
}

return FALSE;
}

NTSTATUS
FindMiniportPdoByPhysicalBase(
_In_ PHYSICAL_ADDRESS PhysicalBase,
_Out_ PDEVICE_OBJECT* FoundPdo
)
{
NTSTATUS Status = STATUS_SUCCESS;
PDEVICE_OBJECT Fdo = NULL;
PDEVICE_OBJECT Pdo = NULL;
ULONG ResultLength = 0;
BOOLEAN FoundAddress = FALSE;

*FoundPdo = NULL;

for (Fdo = s_pDriverObject->DeviceObject; Fdo != NULL; Fdo = Fdo->NextDevice) {
Pdo = Fdo->DeviceObjectExtension->AttachedTo;

Status = IoGetDeviceProperty(Pdo, DevicePropertyBootConfigurationTranslated,
0, NULL, &ResultLength);

if (Status != STATUS_BUFFER_TOO_SMALL) {
return Status;
}

PCM_RESOURCE_LIST ResListTranslated;
ResListTranslated = (PCM_RESOURCE_LIST) ExAllocatePoolZero(NonPagedPool,
ResultLength, SDHC_ALLOC_TAG);

if (ResListTranslated == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
}

Status = IoGetDeviceProperty(Pdo, DevicePropertyBootConfigurationTranslated,
ResultLength, ResListTranslated, &ResultLength);

if (NT_SUCCESS(Status)) {
FoundAddress = FindPhysicalBaseInCmResList(PhysicalBase, ResListTranslated);
}

ExFreePoolWithTag(ResListTranslated, SDHC_ALLOC_TAG);

if (FoundAddress) {
break;
}
}

if (FoundAddress) {
*FoundPdo = Pdo;
} else {
Status = STATUS_DEVICE_CONFIGURATION_ERROR;
}

return Status;
}

NTSTATUS
SdhcSlotInitialize(
_In_ PVOID PrivateExtension,
Expand Down Expand Up @@ -357,20 +266,9 @@ Return value:
ULONG CurrentLimitShift;
PSDHC_EXTENSION SdhcExtension;
USHORT SpecVersion;
NTSTATUS Status;

SdhcExtension = (PSDHC_EXTENSION) PrivateExtension;

//
// Find and save a pointer to this miniport's PDO in the device extension,
// we need it to issue ACPI driver calls.
//

Status = FindMiniportPdoByPhysicalBase(PhysicalBase, &SdhcExtension->PdoPtr);
if (!NT_SUCCESS(Status)) {
return Status;
}

//
// Initialize the SDHC_EXTENSION register space.
//
Expand Down Expand Up @@ -1318,38 +1216,6 @@ DwcSdhcRkConfigurePhy(
}
}

NTSTATUS
DwcSdhcRkDsmSetCardClock(
_In_ PSDHC_EXTENSION SdhcExtension,
_In_ ULONG TargetFrequency,
_Out_ PULONG ActualFrequency
)
{
NTSTATUS Status = STATUS_SUCCESS;
ACPI_EVAL_OUTPUT_BUFFER UNALIGNED* ReturnBufferPtr = nullptr;

ACPI_METHOD_ARGUMENT SetClockArg;
ACPI_METHOD_SET_ARGUMENT_INTEGER((&SetClockArg), TargetFrequency);

Status = AcpiExecuteDsmFunction(SdhcExtension->PdoPtr,
&RKCP0D40_DSM_GUID,
RKCP0D40_DSM_FUNCTION_REVISION_SET_CARD_CLOCK,
RKCP0D40_DSM_FUNCTION_IDX_SET_CARD_CLOCK,
&SetClockArg,
sizeof(SetClockArg),
&ReturnBufferPtr);

if (!NT_SUCCESS(Status)) {
return Status;
}

*ActualFrequency = ReturnBufferPtr->Argument[0].Argument;

ExFreePoolWithTag(ReturnBufferPtr, ACPI_TAG_EVAL_OUTPUT_BUFFER);

return Status;
}

NTSTATUS
DwcSdhcRkSetClock(
_In_ PSDHC_EXTENSION SdhcExtension,
Expand All @@ -1358,17 +1224,16 @@ DwcSdhcRkSetClock(
{
NTSTATUS Status;

ULONG ActualFrequencyHz = 0;

Status = DwcSdhcRkDsmSetCardClock(SdhcExtension,
TargetFrequencyKhz * 1000, // Hz
&ActualFrequencyHz);
Status = RkSipSdmmcClockRateSet(
(ULONG_PTR)SdhcExtension->PhysicalBaseAddress.QuadPart,
RK_SIP_SDMMC_CLOCK_ID_EMMC_CCLK,
TargetFrequencyKhz * 1000);

if (!NT_SUCCESS(Status)) {
return Status;
}

return DwcSdhcRkConfigurePhy(SdhcExtension, ActualFrequencyHz);
return DwcSdhcRkConfigurePhy(SdhcExtension, TargetFrequencyKhz);
}

_IRQL_requires_max_(PASSIVE_LEVEL)
Expand Down
32 changes: 0 additions & 32 deletions drivers/sd/dwcsdhc/dwcsdhc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,11 @@ Module Name:
#pragma warning(disable:4201) // nameless struct/union
#pragma warning(disable:4115) // named type definition in parentheses

//
// RKCP0D40 Device Specific Method UUID
//
// {434addb0-8ff3-49d5-a724-95844b79ad1f}
//
DEFINE_GUID(
RKCP0D40_DSM_GUID,
0x434addb0, 0x8ff3, 0x49d5, 0xa7, 0x24, 0x95, 0x84, 0x4b, 0x79, 0xad, 0x1f);

//
// ACPI _DSM function to set card clock.
//
#define RKCP0D40_DSM_FUNCTION_IDX_SET_CARD_CLOCK 1
#define RKCP0D40_DSM_FUNCTION_REVISION_SET_CARD_CLOCK 0

//
// Allocation tag
//
#define SDHC_ALLOC_TAG ULONG('ScwD')

//
// MMIO length
//
#define SDHC_EXPECTED_ACPI_LENGTH 0x10000

//
// Memory registers
//
Expand Down Expand Up @@ -1413,11 +1393,6 @@ typedef struct _SDHC_EXTENSION {
//

BOOLEAN CrashdumpMode;

//
// Associated PDO
//
PDEVICE_OBJECT PdoPtr;

} SDHC_EXTENSION, *PSDHC_EXTENSION;

Expand Down Expand Up @@ -1822,13 +1797,6 @@ DwcSdhcRkConfigurePhy(
_In_ ULONG Frequency
);

NTSTATUS
DwcSdhcRkDsmSetCardClock(
_In_ PSDHC_EXTENSION SdhcExtension,
_In_ ULONG TargetFrequency,
_Out_ PULONG ActualFrequency
);

NTSTATUS
DwcSdhcRkSetClock(
_In_ PSDHC_EXTENSION SdhcExtension,
Expand Down
17 changes: 7 additions & 10 deletions drivers/sd/dwcsdhc/dwcsdhc.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@
<AdditionalDependencies>%(AdditionalDependencies);$(DDK_LIB_PATH)\sdport.lib;$(DDK_LIB_PATH)\ntoskrnl.lib</AdditionalDependencies>
</Link>
<ClCompile>
<TreatWarningAsError>true</TreatWarningAsError>
<WarningLevel>Level4</WarningLevel>
<ExceptionHandling>
</ExceptionHandling>
<AdditionalIncludeDirectories>..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include;..\rk_sip_sdmmc_lib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<DriverSign>
<FileDigestAlgorithm>sha256</FileDigestAlgorithm>
Expand All @@ -69,11 +65,7 @@
<AdditionalDependencies>%(AdditionalDependencies);$(DDK_LIB_PATH)\sdport.lib;$(DDK_LIB_PATH)\ntoskrnl.lib</AdditionalDependencies>
</Link>
<ClCompile>
<TreatWarningAsError>true</TreatWarningAsError>
<WarningLevel>Level4</WarningLevel>
<ExceptionHandling>
</ExceptionHandling>
<AdditionalIncludeDirectories>..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include;..\rk_sip_sdmmc_lib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<DriverSign>
<FileDigestAlgorithm>sha256</FileDigestAlgorithm>
Expand All @@ -97,5 +89,10 @@
<ItemGroup>
<ClInclude Exclude="@(ClInclude)" Include="*.h;*.hpp;*.hxx;*.hm;*.inl;*.xsd" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\rk_sip_sdmmc_lib\rk_sip_sdmmc_lib.vcxproj">
<Project>{3f0e7d7d-bb1e-42f9-841f-75204a447cc6}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>

0 comments on commit 57b6fee

Please sign in to comment.