SD & SDHCI fixes for 11.2
cover SD & SDHCI fixes for 11.2
This series fixes two issues in the SD subsystem. The first one could send QEMU into an endless loop or crash it (depending on whether assert() does something). The second patch fixes possible file system corruption in e500 and imx25-pdk machines, i.e. the ones using eSDHC rather than uSDHC, though I could only verify that with imx53 emulation on my imx53 branch [1].
[1] https://github.com/shentok/qemu/tree/imx53
Bernhard Beschow (2):
hw/sd/sd: Fix error case for CMD18
hw/sd/sdhci: Extract uSDHC-specific quirk
hw/sd/sd.c | 3 ++- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 30 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/2 hw/sd/sd: Fix error case for CMD18
In commit 468fa450a7e0 ("hw/sd: Switch read/write primitive to buf+len"), sd_read_byte() changed its contract to return the read size rather than the read value (and was renamed to sd_read_data() accordingly). In an error case, however, sd_read_data() returns 0 by means of dummy_byte which is the code for the old contract. Moreover, sdbus_read_data() asserts the virtual method read_data() (and thus sd_read_data()) to return a non-zero size, i.e. to make progress and not loop forever. Fix the code to behave like the "DAT read illegal for command" case.
---
cc: Christian Speich <[email protected]>
--- hw/sd/sd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/sd/sd.c +2 -1
@@ -2860,7 +2860,8 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)if (sd->data_offset ==0 ) {if (!address_in_range (sd,"READ_MULTIPLE_BLOCK" , sd->data_start, io_len)) { -return dummy_byte; + *value = dummy_byte; +return length; } partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] &EXT_CSD_PART_CONFIG_ACC_MASK ;
Signature · 1 line
2.55.0
Re: [PATCH 1/2] hw/sd/sd: Fix error case for CMD18
Quoted 11 lines · Bernhard Beschow
In commit
468fa450a7e0("hw/sd: Switch read/write primitive to buf+len"),sd_read_byte()changed its contract to return the read size rather than the read value (and was renamed tosd_read_data()accordingly). In an error case, however,sd_read_data()returns 0 by means ofdummy_bytewhich is the code for the old contract. Moreover,sdbus_read_data()asserts the virtual methodread_data()(and thussd_read_data()) to return a non-zero size, i.e. to make progress and not loop forever. Fix the code to behave like the "DAT read illegal for command" case.
Quoted 1 line
Fixes 468fa450a7e0 ("hw/sd: Switch read/write primitive to buf+len")
Missing SoB ...
Quoted 7 lines
---
cc: Christian Speich <[email protected]>--- hw/sd/sd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Other than that,
PATCH 2/2 hw/sd/sdhci: Extract uSDHC-specific quirk
In reply to Bernhard BeschowIn Linux, the ESDHC_MIX_CTRL qirk is guarded by esdhc_is_usdhc() while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a new usdhc_write() function.
Fixes file system corruption on emulated i.MX53 where Linux' esdhc_is_usdhc() returns false. The same likely happens on e500 and imx25-pdk machines.
--- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)
hw/sd/sdhci.c +44 -29
@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)sdhci_write (opaque, offset, value, size);break ; -case ESDHC_MIX_CTRL : -/* -* So, when SD/MMC stack in Linux tries to write to "Transfer -* Mode Register", ESDHC i.MX quirk code will translate it -* into a write to ESDHC_MIX_CTRL, so we do the opposite in -* order to get where we started -* -* Note that Auto CMD23 Enable bit is located in a wrong place -* on i.MX, but since it is not used by QEMU we do not care. -* -* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) -* here because it will result in a call to -* sdhci_send_command(s) which we don't want. -* -*/ - s->trnmod = value &UINT16_MAX ; -break ; -case SDHC_TRNMOD : -/* -* Similar to above, but this time a write to "Command -* Register" will be translated into a 4-byte write to -* "Transfer Mode register" where lower 16-bit of value would -* be set to zero. So what we do is fill those bits with -* cached value from s->trnmod and let the SDHCI -* infrastructure handle the rest -*/ -sdhci_write (opaque, offset, val | s->trnmod, size); -break ;case SDHC_BLKSIZE :/* * ESDHCI does not implement "Host SDMA Buffer Boundary", and
@@ -1891,9 +1863,52 @@ static void fsl_esdhc_le_init(Object *obj)qdev_prop_set_uint8 (dev,"sd-spec-version" ,2 ); } +static void +usdhc_write (void *opaque,hwaddr offset,uint64_t val,unsigned size) +{ +SDHCIState *s =SYSBUS_SDHCI (opaque); +uint32_t value = (uint32_t )val; + +switch (offset) { +case ESDHC_MIX_CTRL : +/* +* So, when SD/MMC stack in Linux tries to write to "Transfer +* Mode Register", uSDHC i.MX quirk code will translate it +* into a write to ESDHC_MIX_CTRL, so we do the opposite in +* order to get where we started. +* +* Note that Auto CMD23 Enable bit is located in a wrong place +* on i.MX, but since it is not used by QEMU we do not care. +* +* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) +* here because it will result in a call to +* sdhci_send_command(s) which we don't want. +* +*/ + s->trnmod = value &UINT16_MAX ; +break ; + +case SDHC_TRNMOD : +/* +* Similar to above, but this time a write to "Command +* Register" will be translated into a 4-byte write to +* "Transfer Mode register" where lower 16-bit of value would +* be set to zero. So what we do is fill those bits with +* cached value from s->trnmod and let the SDHCI +* infrastructure handle the rest +*/ +sdhci_write (opaque, offset, val | s->trnmod, size); +break ; + +default : +esdhc_write (opaque, offset, val, size); +break ; + } +} +static const MemoryRegionOps usdhc_mmio_ops = { .read = esdhc_read, - .write = esdhc_write, + .write = usdhc_write, .valid = { .min_access_size =1 , .max_access_size =4 ,
Signature · 1 line
2.55.0
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
Quoted 2 lines · Bernhard Beschow
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while
typo: quirk
Quoted 7 lines
the eSDHC code path uses the standard SDHC interface. Extract the quirk into a new
usdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.
Quoted 7 lines
Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>--- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)
Other than that,
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
In reply to Bernhard BeschowQuoted 32 lines · Bernhard Beschow
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a newusdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>--- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)hw/sd/sdhci.c +0
-8@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)sdhci_write (opaque, offset, value, size);break ; -case ESDHC_MIX_CTRL : -/* -* So, when SD/MMC stack in Linux tries to write to "Transfer -* Mode Register", ESDHC i.MX quirk code will translate it -* into a write to ESDHC_MIX_CTRL, so we do the opposite in -* order to get where we started -* -* Note that Auto CMD23 Enable bit is located in a wrong place
hw/sd/sdhci.c esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) · line 1815
Quoted 10 lines
hw/sd/sdhci.c +0
-10-* on i.MX, but since it is not used by QEMU we do not care. -* -* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) -* here because it will result in a call to -* sdhci_send_command(s) which we don't want. -* -*/ - s->trnmod = value &UINT16_MAX ; -break ; -case SDHC_TRNMOD :
Looks like this register is not eSDHC specific? I looked at linux driver source codes and suspect we may need some more turning here other than uSDHC only.
Quoted 58 lines
hw/sd/sdhci.c +41
-10-/* -* Similar to above, but this time a write to "Command -* Register" will be translated into a 4-byte write to -* "Transfer Mode register" where lower 16-bit of value would -* be set to zero. So what we do is fill those bits with -* cached value from s->trnmod and let the SDHCI -* infrastructure handle the rest -*/ -sdhci_write (opaque, offset, val | s->trnmod, size); -break ;case SDHC_BLKSIZE :/* * ESDHCI does not implement "Host SDMA Buffer Boundary", and @@ -1891,9 +1863,52 @@ static void fsl_esdhc_le_init(Object *obj)qdev_prop_set_uint8 (dev,"sd-spec-version" ,2 ); } +static void +usdhc_write (void *opaque,hwaddr offset,uint64_t val,unsigned size) +{ +SDHCIState *s =SYSBUS_SDHCI (opaque); +uint32_t value = (uint32_t )val; + +switch (offset) { +case ESDHC_MIX_CTRL : +/* +* So, when SD/MMC stack in Linux tries to write to "Transfer +* Mode Register", uSDHC i.MX quirk code will translate it +* into a write to ESDHC_MIX_CTRL, so we do the opposite in +* order to get where we started. +* +* Note that Auto CMD23 Enable bit is located in a wrong place +* on i.MX, but since it is not used by QEMU we do not care. +* +* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) +* here because it will result in a call to +* sdhci_send_command(s) which we don't want. +* +*/ + s->trnmod = value &UINT16_MAX ; +break ; + +case SDHC_TRNMOD : +/* +* Similar to above, but this time a write to "Command +* Register" will be translated into a 4-byte write to +* "Transfer Mode register" where lower 16-bit of value would +* be set to zero. So what we do is fill those bits with +* cached value from s->trnmod and let the SDHCI +* infrastructure handle the rest +*/ +sdhci_write (opaque, offset, val | s->trnmod, size); +break ; + +default : +esdhc_write (opaque, offset, val, size); +break ; + }
hw/sd/sdhci.c static void fsl_esdhc_le_init(Object *obj)
Quoted 10 lines
hw/sd/sdhci.c +3
-2+} +static const MemoryRegionOps usdhc_mmio_ops = { .read = esdhc_read, - .write = esdhc_write, + .write = usdhc_write, .valid = { .min_access_size =1 , .max_access_size =4 , --
Regards,
Bin
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
Am 20. Juli 2026 14:50:24 UTC schrieb Bin Meng <[email protected]>:
Hi Bin,
Thanks for yor reviews and R-b tags!
Quoted 37 lines · Bernhard Beschow
Quoted 36 lines
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a newusdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>--- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)hw/sd/sdhci.c +0
-12@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)sdhci_write (opaque, offset, value, size);break ; -case ESDHC_MIX_CTRL : -/* -* So, when SD/MMC stack in Linux tries to write to "Transfer -* Mode Register", ESDHC i.MX quirk code will translate it -* into a write to ESDHC_MIX_CTRL, so we do the opposite in -* order to get where we started -* -* Note that Auto CMD23 Enable bit is located in a wrong place -* on i.MX, but since it is not used by QEMU we do not care. -* -* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) -* here because it will result in a call to
Quoted 8 lines
hw/sd/sdhci.c esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) · line 1815
Quoted 6 lines
hw/sd/sdhci.c +0
-6-* sdhci_send_command(s) which we don't want. -* -*/ - s->trnmod = value &UINT16_MAX ; -break ; -case SDHC_TRNMOD :Looks like this register is not eSDHC specific?
Not quite sure what you mean exactly. The SDHC_TRNMOD isn't eSDHC specific but as per the comment above trnmod partial handling is deferred here, i.e. needs to be intercepted.
Quoted 3 lines
I looked at linux
driver source codes and suspect we may need some more turning here other than uSDHC only.
Yeah, there is certainly a lot of room for improvement. I have some clock-related and SDMA buffer boundary patches in the pipeline which are necessary for satisfying U-Boot. But that's a story for another day.
Best regards,
Bernhard
Quoted 73 lines
Quoted 60 lines
hw/sd/sdhci.c +41
-10-/* -* Similar to above, but this time a write to "Command -* Register" will be translated into a 4-byte write to -* "Transfer Mode register" where lower 16-bit of value would -* be set to zero. So what we do is fill those bits with -* cached value from s->trnmod and let the SDHCI -* infrastructure handle the rest -*/ -sdhci_write (opaque, offset, val | s->trnmod, size); -break ;case SDHC_BLKSIZE :/* * ESDHCI does not implement "Host SDMA Buffer Boundary", and @@ -1891,9 +1863,52 @@ static void fsl_esdhc_le_init(Object *obj)qdev_prop_set_uint8 (dev,"sd-spec-version" ,2 ); } +static void +usdhc_write (void *opaque,hwaddr offset,uint64_t val,unsigned size) +{ +SDHCIState *s =SYSBUS_SDHCI (opaque); +uint32_t value = (uint32_t )val; + +switch (offset) { +case ESDHC_MIX_CTRL : +/* +* So, when SD/MMC stack in Linux tries to write to "Transfer +* Mode Register", uSDHC i.MX quirk code will translate it +* into a write to ESDHC_MIX_CTRL, so we do the opposite in +* order to get where we started. +* +* Note that Auto CMD23 Enable bit is located in a wrong place +* on i.MX, but since it is not used by QEMU we do not care. +* +* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) +* here because it will result in a call to +* sdhci_send_command(s) which we don't want. +* +*/ + s->trnmod = value &UINT16_MAX ; +break ; + +case SDHC_TRNMOD : +/* +* Similar to above, but this time a write to "Command +* Register" will be translated into a 4-byte write to +* "Transfer Mode register" where lower 16-bit of value would +* be set to zero. So what we do is fill those bits with +* cached value from s->trnmod and let the SDHCI +* infrastructure handle the rest +*/ +sdhci_write (opaque, offset, val | s->trnmod, size); +break ; + +default : +esdhc_write (opaque, offset, val, size); +break ; + }hw/sd/sdhci.c static void fsl_esdhc_le_init(Object *obj)
Quoted 10 lines
hw/sd/sdhci.c +3
-2+} +static const MemoryRegionOps usdhc_mmio_ops = { .read = esdhc_read, - .write = esdhc_write, + .write = usdhc_write, .valid = { .min_access_size =1 , .max_access_size =4 , --Regards,
Bin
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
Hi Bernhard,
Quoted 55 lines · Bernhard Beschow
Am 20. Juli 2026 14:50:24 UTC schrieb Bin Meng <[email protected]>:
Hi Bin,
Thanks for yor reviews and R-b tags!
Quoted 37 lines · Bernhard Beschow
Quoted 36 lines
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a newusdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>--- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)hw/sd/sdhci.c +0
-12@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)sdhci_write (opaque, offset, value, size);break ; -case ESDHC_MIX_CTRL : -/* -* So, when SD/MMC stack in Linux tries to write to "Transfer -* Mode Register", ESDHC i.MX quirk code will translate it -* into a write to ESDHC_MIX_CTRL, so we do the opposite in -* order to get where we started -* -* Note that Auto CMD23 Enable bit is located in a wrong place -* on i.MX, but since it is not used by QEMU we do not care. -* -* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) -* here because it will result in a call to Quoted 8 lines
hw/sd/sdhci.c esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) · line 1815
Quoted 6 lines
hw/sd/sdhci.c +0
-6-* sdhci_send_command(s) which we don't want. -* -*/ - s->trnmod = value &UINT16_MAX ; -break ; -case SDHC_TRNMOD :Looks like this register is not eSDHC specific?
Quoted 1 line
Not quite sure what you mean exactly. The
SDHC_TRNMODisn't eSDHC specific but as per the comment above trnmod partial handling is deferred here, i.e. needs to be intercepted.
If I read the Linux driver correctly, I think we should do something like below when writing to SDHC_TRNMOD:
case SDHC_TRNMOD:
if (is_usdhc) {
val |= s->trnmod;
}
sdhci_write(opaque, offset, val, size);
break;Because for eSDHC, the transfer mode is already passed in the value, and oring previous s->trnmod may cause some bits being set to old previous value unexpectedly. Please double check.
Quoted 72 lines
Quoted 4 lines
I looked at linux
driver source codes and suspect we may need some more turning here other than uSDHC only.Yeah, there is certainly a lot of room for improvement. I have some clock-related and SDMA buffer boundary patches in the pipeline which are necessary for satisfying U-Boot. But that's a story for another day.
Best regards,
BernhardQuoted 62 lines
Quoted 62 lines
hw/sd/sdhci.c +43
-10-/* -* Similar to above, but this time a write to "Command -* Register" will be translated into a 4-byte write to -* "Transfer Mode register" where lower 16-bit of value would -* be set to zero. So what we do is fill those bits with -* cached value from s->trnmod and let the SDHCI -* infrastructure handle the rest -*/ -sdhci_write (opaque, offset, val | s->trnmod, size); -break ;case SDHC_BLKSIZE :/* * ESDHCI does not implement "Host SDMA Buffer Boundary", and @@ -1891,9 +1863,52 @@ static void fsl_esdhc_le_init(Object *obj)qdev_prop_set_uint8 (dev,"sd-spec-version" ,2 ); } +static void +usdhc_write (void *opaque,hwaddr offset,uint64_t val,unsigned size) +{ +SDHCIState *s =SYSBUS_SDHCI (opaque); +uint32_t value = (uint32_t )val; + +switch (offset) { +case ESDHC_MIX_CTRL : +/* +* So, when SD/MMC stack in Linux tries to write to "Transfer +* Mode Register", uSDHC i.MX quirk code will translate it +* into a write to ESDHC_MIX_CTRL, so we do the opposite in +* order to get where we started. +* +* Note that Auto CMD23 Enable bit is located in a wrong place +* on i.MX, but since it is not used by QEMU we do not care. +* +* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) +* here because it will result in a call to +* sdhci_send_command(s) which we don't want. +* +*/ + s->trnmod = value &UINT16_MAX ; +break ; + +case SDHC_TRNMOD : +/* +* Similar to above, but this time a write to "Command +* Register" will be translated into a 4-byte write to +* "Transfer Mode register" where lower 16-bit of value would +* be set to zero. So what we do is fill those bits with +* cached value from s->trnmod and let the SDHCI +* infrastructure handle the rest +*/ +sdhci_write (opaque, offset, val | s->trnmod, size); +break ; + +default : +esdhc_write (opaque, offset, val, size); +break ; + } +} +
Quoted 8 lines
Quoted 8 lines
hw/sd/sdhci.c static void fsl_esdhc_le_init(Object *obj)
Quoted 8 lines
hw/sd/sdhci.c +1
-2static const MemoryRegionOps usdhc_mmio_ops = { .read = esdhc_read, - .write = esdhc_write, + .write = usdhc_write, .valid = { .min_access_size =1 , .max_access_size =4 , --
Regards,
Bin
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
Am 21. Juli 2026 08:38:19 UTC schrieb Bin Meng <[email protected]>:
Quoted 70 lines
Hi Bernhard,
Quoted 55 lines · Bernhard Beschow
Am 20. Juli 2026 14:50:24 UTC schrieb Bin Meng <[email protected]>:
Hi Bin,
Thanks for yor reviews and R-b tags!
Quoted 37 lines · Bernhard Beschow
Quoted 36 lines
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a newusdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>--- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)hw/sd/sdhci.c +0
-12@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)sdhci_write (opaque, offset, value, size);break ; -case ESDHC_MIX_CTRL : -/* -* So, when SD/MMC stack in Linux tries to write to "Transfer -* Mode Register", ESDHC i.MX quirk code will translate it -* into a write to ESDHC_MIX_CTRL, so we do the opposite in -* order to get where we started -* -* Note that Auto CMD23 Enable bit is located in a wrong place -* on i.MX, but since it is not used by QEMU we do not care. -* -* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) -* here because it will result in a call to Quoted 8 lines
hw/sd/sdhci.c esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) · line 1815
Quoted 6 lines
hw/sd/sdhci.c +0
-6-* sdhci_send_command(s) which we don't want. -* -*/ - s->trnmod = value &UINT16_MAX ; -break ; -case SDHC_TRNMOD :Looks like this register is not eSDHC specific?
Quoted 1 line
Not quite sure what you mean exactly. The
SDHC_TRNMODisn't eSDHC specific but as per the comment above trnmod partial handling is deferred here, i.e. needs to be intercepted.If I read the Linux driver correctly, I think we should do something like below when writing to
SDHC_TRNMOD:case
SDHC_TRNMOD:if (is_usdhc) { val |= s->trnmod; } sdhci_write(opaque, offset, val, size); break;
Quoted 3 lines
Because for eSDHC, the transfer mode is already passed in the value, and oring previous
s->trnmodmay cause some bits being set to old previous value unexpectedly. Please double check.
By moving out of esdhc_write() into a dedicated usdhc_write() method we achieve exactly that, no?
Best regards,
Bernhard
Quoted 84 lines
Quoted 73 lines
Quoted 5 lines
I looked at linux
driver source codes and suspect we may need some more turning here other than uSDHC only.Yeah, there is certainly a lot of room for improvement. I have some clock-related and SDMA buffer boundary patches in the pipeline which are necessary for satisfying U-Boot. But that's a story for another day.
Best regards,
BernhardQuoted 62 lines
Quoted 62 lines
hw/sd/sdhci.c +43
-10-/* -* Similar to above, but this time a write to "Command -* Register" will be translated into a 4-byte write to -* "Transfer Mode register" where lower 16-bit of value would -* be set to zero. So what we do is fill those bits with -* cached value from s->trnmod and let the SDHCI -* infrastructure handle the rest -*/ -sdhci_write (opaque, offset, val | s->trnmod, size); -break ;case SDHC_BLKSIZE :/* * ESDHCI does not implement "Host SDMA Buffer Boundary", and @@ -1891,9 +1863,52 @@ static void fsl_esdhc_le_init(Object *obj)qdev_prop_set_uint8 (dev,"sd-spec-version" ,2 ); } +static void +usdhc_write (void *opaque,hwaddr offset,uint64_t val,unsigned size) +{ +SDHCIState *s =SYSBUS_SDHCI (opaque); +uint32_t value = (uint32_t )val; + +switch (offset) { +case ESDHC_MIX_CTRL : +/* +* So, when SD/MMC stack in Linux tries to write to "Transfer +* Mode Register", uSDHC i.MX quirk code will translate it +* into a write to ESDHC_MIX_CTRL, so we do the opposite in +* order to get where we started. +* +* Note that Auto CMD23 Enable bit is located in a wrong place +* on i.MX, but since it is not used by QEMU we do not care. +* +* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) +* here because it will result in a call to +* sdhci_send_command(s) which we don't want. +* +*/ + s->trnmod = value &UINT16_MAX ; +break ; + +case SDHC_TRNMOD : +/* +* Similar to above, but this time a write to "Command +* Register" will be translated into a 4-byte write to +* "Transfer Mode register" where lower 16-bit of value would +* be set to zero. So what we do is fill those bits with +* cached value from s->trnmod and let the SDHCI +* infrastructure handle the rest +*/ +sdhci_write (opaque, offset, val | s->trnmod, size); +break ; + +default : +esdhc_write (opaque, offset, val, size); +break ; + } +} +Quoted 8 lines
Quoted 8 lines
hw/sd/sdhci.c static void fsl_esdhc_le_init(Object *obj)
Quoted 8 lines
hw/sd/sdhci.c +1
-2static const MemoryRegionOps usdhc_mmio_ops = { .read = esdhc_read, - .write = esdhc_write, + .write = usdhc_write, .valid = { .min_access_size =1 , .max_access_size =4 , --Regards,
Bin
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
Quoted 73 lines · Bernhard Beschow
Am 21. Juli 2026 08:38:19 UTC schrieb Bin Meng <[email protected]>:
Quoted 70 lines
Hi Bernhard,
Quoted 55 lines · Bernhard Beschow
Am 20. Juli 2026 14:50:24 UTC schrieb Bin Meng <[email protected]>:
Hi Bin,
Thanks for yor reviews and R-b tags!
Quoted 37 lines · Bernhard Beschow
Quoted 36 lines
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a newusdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>--- hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)hw/sd/sdhci.c +0
-12@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)sdhci_write (opaque, offset, value, size);break ; -case ESDHC_MIX_CTRL : -/* -* So, when SD/MMC stack in Linux tries to write to "Transfer -* Mode Register", ESDHC i.MX quirk code will translate it -* into a write to ESDHC_MIX_CTRL, so we do the opposite in -* order to get where we started -* -* Note that Auto CMD23 Enable bit is located in a wrong place -* on i.MX, but since it is not used by QEMU we do not care. -* -* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) -* here because it will result in a call to Quoted 8 lines
hw/sd/sdhci.c esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) · line 1815
Quoted 6 lines
hw/sd/sdhci.c +0
-6-* sdhci_send_command(s) which we don't want. -* -*/ - s->trnmod = value &UINT16_MAX ; -break ; -case SDHC_TRNMOD :Looks like this register is not eSDHC specific?
Quoted 1 line
Not quite sure what you mean exactly. The
SDHC_TRNMODisn't eSDHC specific but as per the comment above trnmod partial handling is deferred here, i.e. needs to be intercepted.If I read the Linux driver correctly, I think we should do something like below when writing to
SDHC_TRNMOD:case SDHC_TRNMOD: if (is_usdhc) { val |= s->trnmod; } sdhci_write(opaque, offset, val, size); break;
Quoted 5 lines
Quoted 3 lines
Because for eSDHC, the transfer mode is already passed in the value, and oring previous
s->trnmodmay cause some bits being set to old previous value unexpectedly. Please double check.By moving out of
esdhc_write()into a dedicatedusdhc_write()method we achieve exactly that, no?
Indeed this is kind of hidden, see below.
Quoted 61 lines
Best regards,
BernhardQuoted 57 lines
Quoted 57 lines
Quoted 5 lines
I looked at linux
driver source codes and suspect we may need some more turning here other than uSDHC only.Yeah, there is certainly a lot of room for improvement. I have some clock-related and SDMA buffer boundary patches in the pipeline which are necessary for satisfying U-Boot. But that's a story for another day.
Best regards,
BernhardQuoted 46 lines
Quoted 46 lines
hw/sd/sdhci.c +27
-10-/* -* Similar to above, but this time a write to "Command -* Register" will be translated into a 4-byte write to -* "Transfer Mode register" where lower 16-bit of value would -* be set to zero. So what we do is fill those bits with -* cached value from s->trnmod and let the SDHCI -* infrastructure handle the rest -*/ -sdhci_write (opaque, offset, val | s->trnmod, size); -break ;case SDHC_BLKSIZE :/* * ESDHCI does not implement "Host SDMA Buffer Boundary", and @@ -1891,9 +1863,52 @@ static void fsl_esdhc_le_init(Object *obj)qdev_prop_set_uint8 (dev,"sd-spec-version" ,2 ); } +static void +usdhc_write (void *opaque,hwaddr offset,uint64_t val,unsigned size) +{ +SDHCIState *s =SYSBUS_SDHCI (opaque); +uint32_t value = (uint32_t )val; + +switch (offset) { +case ESDHC_MIX_CTRL : +/* +* So, when SD/MMC stack in Linux tries to write to "Transfer +* Mode Register", uSDHC i.MX quirk code will translate it +* into a write to ESDHC_MIX_CTRL, so we do the opposite in +* order to get where we started. +* +* Note that Auto CMD23 Enable bit is located in a wrong place +* on i.MX, but since it is not used by QEMU we do not care. +* +* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) +* here because it will result in a call to +* sdhci_send_command(s) which we don't want. +* +*/ + s->trnmod = value &UINT16_MAX ; +break ; + +case SDHC_TRNMOD : +/*
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/sd/sdhci.c static void fsl_esdhc_le_init(Object *obj) · line 1900
Quoted 8 lines
hw/sd/sdhci.c +8
-0+* Similar to above, but this time a write to "Command +* Register" will be translated into a 4-byte write to +* "Transfer Mode register" where lower 16-bit of value would +* be set to zero. So what we do is fill those bits with +* cached value from s->trnmod and let the SDHCI +* infrastructure handle the rest +*/ +sdhci_write (opaque, offset, val | s->trnmod, size);
^^^^^^^^^^^^^^^
Quoted 20 lines
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/sd/sdhci.c +8
-0+break ; + +default : +esdhc_write (opaque, offset, val, size); +break ; + } +} +Quoted 8 lines
Quoted 8 lines
hw/sd/sdhci.c static void fsl_esdhc_le_init(Object *obj)
Quoted 8 lines
hw/sd/sdhci.c +1
-2static const MemoryRegionOps usdhc_mmio_ops = { .read = esdhc_read, - .write = esdhc_write, + .write = usdhc_write, .valid = { .min_access_size =1 , .max_access_size =4 , --Regards,
Bin
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
Hi Bin,
Quoted 78 lines · Philippe Mathieu-Daudé
Quoted 77 lines · Bernhard Beschow
Am 21. Juli 2026 08:38:19 UTC schrieb Bin Meng <[email protected]>:
Quoted 74 lines
Hi Bernhard,
Quoted 59 lines · Bernhard Beschow
Am 20. Juli 2026 14:50:24 UTC schrieb Bin Meng <[email protected]>:
Hi Bin,
Thanks for yor reviews and R-b tags!
Quoted 41 lines · Bernhard Beschow
Quoted 40 lines
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a newusdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>---
hw/sd/sdhci.c| 73 ++++++++++++++++++++++++++++++
+--------------------1 file changed, 44 insertions(+), 29 deletions(-)hw/sd/sdhci.c +0
-3@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) sdhci_write(opaque, offset, value, size); break; - case ESDHC_MIX_CTRL: - /* -* So, when SD/MMC stack in Linux tries to write to "Transfer
- * Mode Register", ESDHC i.MX quirk code will translate it - * into a write toESDHC_MIX_CTRL, so we do the opposite in - * order to get where we started
- *
- * Note that Auto CMD23 Enable bit is located in a wrong place
- * on i.MX, but since it is not used by QEMU we do not care. - *
- * We don't want to callsdhci_write(.., SDHC_TRNMOD, ...)- * here because it will result in a call toQuoted 8 lines
Quoted 6 lines
- *
sdhci_send_command(s)which we don't want. - *
- */
-s->trnmod= value &UINT16_MAX;
- break;
- caseSDHC_TRNMOD:Looks like this register is not eSDHC specific?
Quoted 1 line
Not quite sure what you mean exactly. The
SDHC_TRNMODisn't eSDHC specific but as per the comment above trnmod partial handling is deferred here, i.e. needs to be intercepted.If I read the Linux driver correctly, I think we should do something like below when writing to
SDHC_TRNMOD:case
SDHC_TRNMOD:
if (is_usdhc) {
val |=s->trnmod;
}
sdhci_write(opaque, offset, val, size);
break;
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Because for eSDHC, the transfer mode is already passed in the value, and oring previous
s->trnmodmay cause some bits being set to old previous value unexpectedly. Please double check.By moving out of
esdhc_write()into a dedicatedusdhc_write()method we achieve exactly that, no?
What is your final tought, should we proceed with v2?
Re: [PATCH 2/2] hw/sd/sdhci: Extract uSDHC-specific quirk
Quoted 88 lines · Philippe Mathieu-Daudé
Hi Bin,
Quoted 84 lines · Philippe Mathieu-Daudé
Quoted 83 lines · Bernhard Beschow
Am 21. Juli 2026 08:38:19 UTC schrieb Bin Meng <[email protected]>:
Quoted 80 lines
Hi Bernhard,
Quoted 62 lines · Bernhard Beschow
Am 20. Juli 2026 14:50:24 UTC schrieb Bin Meng <[email protected]>:
Hi Bin,
Thanks for yor reviews and R-b tags!
Quoted 44 lines
Quoted 42 lines · Bernhard Beschow
In Linux, the
ESDHC_MIX_CTRLqirk is guarded byesdhc_is_usdhc()while
the eSDHC code path uses the standard SDHC interface. Extract the quirk
into a newusdhc_write()function.Fixes file system corruption on emulated i.MX53 where Linux'
esdhc_is_usdhc()returns false. The same likely happens on e500 and imx25-pdk machines.Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")cc qemu-stableSigned-off-by Bernhard Beschow <[email protected]>--- hw/sd/sdhci.c | 73 ++++++++++++++++++++++++++++++ +-------------------- 1 file changed, 44 insertions(+), 29 deletions(-)hw/sd/sdhci.c +0
-3@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)sdhci_write (opaque ,offset ,value ,size );break ; -case ESDHC_MIX_CTRL : -/* -* So, when SD/MMC stack in Linux tries to write to "Transfer
hw/sd/sdhci.c +0
-5-* Mode Register", ESDHC i.MX quirk code will translate it -* into a write to ESDHC_MIX_CTRL, so we do the opposite in -* order to get where we started -* -* Note that Auto CMD23 Enable bit is located in a wrong place
hw/sd/sdhci.c +0
-4-* on i.MX, but since it is not used by QEMU we do not care. -* -* We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) -* here because it will result in a call to Quoted 8 lines
hw/sd/sdhci.c
Quoted 6 lines
hw/sd/sdhci.c +0
-6-* sdhci_send_command(s) which we don't want. -* -*/ - s->trnmod = value &UINT16_MAX ; -break ; -case SDHC_TRNMOD :Looks like this register is not eSDHC specific?
Quoted 3 lines
Not quite sure what you mean exactly. The
SDHC_TRNMODisn't eSDHC specific but as per the comment above trnmod partial handling is deferred here, i.e. needs to be intercepted.If I read the Linux driver correctly, I think we should do something like below when writing to
SDHC_TRNMOD:case SDHC_TRNMOD: if (is_usdhc) { val |= s->trnmod; } sdhci_write(opaque, offset, val, size); break;
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Because for eSDHC, the transfer mode is already passed in the value, and oring previous
s->trnmodmay cause some bits being set to old previous value unexpectedly. Please double check.By moving out of
esdhc_write()into a dedicatedusdhc_write()method we achieve exactly that, no?What is your final tought, should we proceed with v2?
Yes, I think so.
Regards,
Bin