Hi Philippe, Bin,

I've completed the investigation using the unchanged STM32F4 firmware ELF.

You were right about the NWR timing. The SPI trace confirms that the
firmware sends 0xFE immediately after R1, without the required clock
interval.

Therefore, the proposed patch is a workaround for the guest behavior rather
than a QEMU bug fix. I won't pursue it upstream.

However, the investigation also revealed a separate QEMU regression in SPI
mode.

After ACMD41, the card remains in sd_ready_state, but CMD16 requires
sd_transfer_state. This causes CMD16 to fail and the subsequent CMD9 to
return R1 = 0x04 (ILLEGAL_COMMAND).

I traced this behavior back to changes introduced in QEMU 10.1.0. Restoring
the SPI transition to sd_transfer_state after ACMD41 fixes the
initialization failure with the unchanged firmware.

I'll address the CMD16/ACMD41 issue separately.

Thanks again for your helpful feedback.

Best regards,
Manuele

Il giorno mer 4 nov 2026 alle ore 21:21 Manuele Conti <
[email protected]> ha scritto:

> Hi Philippe, Bin,
>
> Thank you for the detailed review and for pointing out the NWR timing
> requirement.
>
> I checked section 7.5.4 and Figure 56 of the SD Physical Layer
> Specification v1.10. I agree that NWR has a minimum of one byte (8 clock
> cycles), which means my zero-gap test case does not establish a
> protocol-compliant host sequence.
>
> The other observation about matching the write tokens to CMD24/CMD25 and
> handling the CMD25 data response is also valid.
>
> Before sending another revision, I will investigate the guest-side SPI
> sequence and check whether the issue can be reproduced with the required
> NWR interval.
>
> I will also check whether the firmware is accounting for this delay or
> whether the problem is caused by the interaction with QEMU's response
> handling.
>
> I won't submit a v3 until I can demonstrate a protocol-compliant
> reproducer.
>
> Thanks again for the careful review.
>
> Best regards,
> Manuele
>
> Il giorno mer 4 nov 2026 alle ore 15:25 Philippe Mathieu-Daudé <
> [email protected]> ha scritto:
>
>> Please keep Bin in Cc (as maintainer)
>>
>> $ ./scripts/get_maintainer.pl -f hw/sd/ssi-sd.c
>> "Philippe Mathieu-Daudé" <[email protected]> (odd fixer:SD (Secure
>> Card), gitlab:@philmd)
>> Bin Meng <[email protected]> (odd fixer:SD (Secure Card), gitlab:@lbmeng
>> )
>>
>> On 4/11/26 13:31, Manuele Conti wrote:
>> > When a write start token immediately follows the R1 response,
>> > the SPI SD controller may still be in SSI_SD_RESPONSE state
>> > and discard the token.
>> >
>> > Recognize single-block and multi-block write start tokens
>> > when the SD card is ready to receive data, allowing the
>> > write operation to proceed without an additional dummy byte.
>> >
>> > Signed-off-by: Manuele Conti <[email protected]>
>> > ---
>> > Hi Philippe,
>> >
>> > Thanks for your feedback.
>> >
>> > I encountered this issue while running a bare-metal STM32F4 firmware
>> under QEMU, using SPI mode to communicate with an SD card.
>> >
>> > The firmware performs a block write (CMD24) and sends the data start
>> token (0xFE) immediately after receiving the R1 response, without an
>> additional dummy byte.
>> >
>> > In this case, the SSI SD controller is still in SSI_SD_RESPONSE state
>> and discards the token, causing the subsequent write operation to fail.
>> >
>> > I've now added a regression test to reproduce this behavior without
>> requiring the original firmware.
>>
>> Wow, that was fast! I suppose you already had that qtest written
>> locally, if so next time better share along with a fix.
>>
>> Looking at older spec v1.10 I found section 7.5.4 "Timing Values" and
>> Figure 56 "Write operation - bus timing". The table gives NWR (between
>> the write command response and the data block) a minimum of 1, in
>> units of 8 clock cycles (so at least one byte). If that reading is
>> right, a host sending 0xFE in the very next byte after R1 is outside
>> the card timing, and the model dropping it is not obviously a bug.
>>
>> You described the model but not the guest, what firmware / drivers
>> it uses. Does your firmware work on real HW -- and if so what is the
>> SD card being used --? Does the FW intentionally send the token with
>> no gap, or does it poll/read a byte after the command response that
>> our SPI controller model hides or reorders?
>>
>> Thanks,
>>
>> Phil.
>>
>> > The test exercises the immediate write-token sequence and verifies that
>> the controller correctly transitions to the data-write state.
>> >
>> > I'll submit an updated patch including the regression test so the issue
>> can be reproduced and verified independently.
>> >
>> > Best regards,
>> > Manuele
>> >
>> >   hw/sd/ssi-sd.c                        |   6 +
>> >   tests/qtest/ssi-sd-token-local-test.c | 165 ++++++++++++++++++++++++++
>> >   2 files changed, 171 insertions(+)
>> >   create mode 100644 tests/qtest/ssi-sd-token-local-test.c
>>
>>

Reply via email to