qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Bin Meng" <bmeng.cn@gmail.com>,
	"Steven Lee" <steven_lee@aspeedtech.com>,
	"Andrew Jeffery" <andrew@codeconstruct.com.au>,
	"Francisco Iglesias" <francisco.iglesias@amd.com>,
	"Sai Pavan Boddu" <sai.pavan.boddu@amd.com>,
	"Luc Michel" <luc.michel@amd.com>,
	qemu-arm@nongnu.org, qemu-block@nongnu.org,
	"Troy Lee" <leetroy@gmail.com>,
	"Jamin Lin" <jamin_lin@aspeedtech.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Joel Stanley" <joel@jms.id.au>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Edgar E . Iglesias" <edgar.iglesias@amd.com>,
	"Cédric Le Goater" <clg@kaod.org>
Subject: [PATCH v48 10/12] hw/sd/sdcard: Implement eMMC 'boot-mode'
Date: Wed, 10 Jul 2024 16:14:06 +0200	[thread overview]
Message-ID: <20240710141408.69275-11-philmd@linaro.org> (raw)
In-Reply-To: <20240710141408.69275-1-philmd@linaro.org>

Spec v4.3 chapter 7.2.2 "Boot operation":

  If the CMD line is held LOW for 74 clock cycles and more
  after power-up before the first command is issued, the
  slave recognizes that boot mode is being initiated and
  starts preparing boot data internally.

Track uptime since last reset, add the sd_uptime_ns() helper.

When the first command is received, check at least 74 clocks
are elapsed (during the identification phase, at a 10kHz rate)
then enable BOOT_MODE in the Ext_CSD register.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/sd/sd.c         | 38 ++++++++++++++++++++++++++++++++++++++
 hw/sd/trace-events |  1 +
 2 files changed, 39 insertions(+)

diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 57a9eb91d0..c2ea942389 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -163,6 +163,8 @@ struct SDState {
      */
     bool expecting_acmd;
     uint32_t blk_written;
+    int64_t reset_time_ns;
+    uint32_t cmd_count;
 
     uint64_t data_start;
     uint32_t data_offset;
@@ -352,6 +354,11 @@ static uint8_t sd_crc7(const void *message, size_t width)
     return shift_reg;
 }
 
+static int64_t sd_uptime_ns(SDState *sd)
+{
+    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - sd->reset_time_ns;
+}
+
 /* Operation Conditions register */
 
 #define OCR_POWER_DELAY_NS      500000 /* 0.5ms */
@@ -479,6 +486,10 @@ static void emmc_set_cid(SDState *sd)
 #define CMULT_SHIFT     9        /* 512 times HWBLOCK_SIZE */
 #define WPGROUP_SIZE    (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))
 
+#define OD_FREQ_MIN_HZ   10000
+#define OD_FREQ_MAX_HZ  400000
+#define BOOT_MODE_DELAY_CYCLES_MIN 74
+
 static const uint8_t sd_csd_rw_mask[16] = {
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
@@ -797,6 +808,8 @@ static void sd_reset(DeviceState *dev)
 
     sect = sd_addr_to_wpnum(size) + 1;
 
+    sd->reset_time_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+    sd->cmd_count = 0;
     sd->state = sd_idle_state;
 
     /* card registers */
@@ -905,6 +918,18 @@ static const VMStateDescription emmc_extcsd_vmstate = {
     },
 };
 
+static const VMStateDescription sdmmc_uptime_cmdcnt_vmstate = {
+    .name = "sd-card/uptime-command_count-state",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = vmstate_needed_for_emmc,
+    .fields = (const VMStateField[]) {
+        VMSTATE_INT64(reset_time_ns, SDState),
+        VMSTATE_UINT32(cmd_count, SDState),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 static int sd_vmstate_pre_load(void *opaque)
 {
     SDState *sd = opaque;
@@ -953,6 +978,7 @@ static const VMStateDescription sd_vmstate = {
     .subsections = (const VMStateDescription * const []) {
         &sd_ocr_vmstate,
         &emmc_extcsd_vmstate,
+        &sdmmc_uptime_cmdcnt_vmstate,
         NULL
     },
 };
@@ -1925,6 +1951,16 @@ static sd_rsp_type_t sd_cmd_SEND_OP_COND(SDState *sd, SDRequest req)
         sd->state = sd_ready_state;
     }
 
+    if (sd_is_emmc(sd) && sd->cmd_count == 1) {
+        int64_t clk_cycles = sd_uptime_ns(sd) / OD_FREQ_MIN_HZ;
+
+        trace_sdcard_ext_csd_bootmode(sd_uptime_ns(sd), clk_cycles,
+                                      clk_cycles > BOOT_MODE_DELAY_CYCLES_MIN);
+        if (clk_cycles > BOOT_MODE_DELAY_CYCLES_MIN) {
+            sd->ext_csd[EXT_CSD_PART_CONFIG] |= (1 << 3);
+        }
+    }
+
     return sd_r3;
 }
 
@@ -2107,6 +2143,8 @@ int sd_do_command(SDState *sd, SDRequest *req,
         return 0;
     }
 
+    ++sd->cmd_count;
+
     if (sd->state == sd_inactive_state) {
         rtype = sd_illegal;
         goto send_response;
diff --git a/hw/sd/trace-events b/hw/sd/trace-events
index 5dfe6be7b7..7d0de368aa 100644
--- a/hw/sd/trace-events
+++ b/hw/sd/trace-events
@@ -57,6 +57,7 @@ sdcard_write_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x"
 sdcard_write_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t offset, uint8_t value) "%s %20s/ CMD%02d ofs %"PRIu32" value 0x%02x"
 sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t offset, uint64_t size, uint32_t blklen) "%s %20s/ CMD%02d ofs %"PRIu32" size %"PRIu64" blklen %" PRIu32
 sdcard_set_voltage(uint16_t millivolts) "%u mV"
+sdcard_ext_csd_bootmode(int64_t uptime_ns, int64_t clk_cycles, unsigned enabled) "%"PRId64" ns, %"PRId64" cycles, boot mode: %u"
 
 # pxa2xx_mmci.c
 pxa2xx_mmci_read(uint8_t size, uint32_t addr, uint32_t value) "size %d addr 0x%02x value 0x%08x"
-- 
2.41.0



  parent reply	other threads:[~2024-07-10 14:17 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-10 14:13 [PATCH v48 00/12] hw/sd/sdcard: Add eMMC support Philippe Mathieu-Daudé
2024-07-10 14:13 ` [PATCH v48 01/12] hw/sd/sdcard: Basis for " Philippe Mathieu-Daudé
2024-07-10 14:13 ` [PATCH v48 02/12] hw/sd/sdcard: Register generic command handlers Philippe Mathieu-Daudé
2024-07-10 14:13 ` [PATCH v48 03/12] hw/sd/sdcard: Register unimplemented " Philippe Mathieu-Daudé
2024-07-10 14:14 ` [PATCH v48 04/12] hw/sd/sdcard: Add emmc_cmd_SET_RELATIVE_ADDR handler (CMD3) Philippe Mathieu-Daudé
2024-07-10 14:14 ` [PATCH v48 05/12] hw/sd/sdcard: Fix SET_BLOCK_COUNT command argument on eMMC (CMD23) Philippe Mathieu-Daudé
2024-07-10 14:14 ` [PATCH v48 06/12] hw/sd/sdcard: Add mmc_cmd_PROGRAM_CID handler (CMD26) Philippe Mathieu-Daudé
2024-07-10 14:14 ` [PATCH v48 07/12] hw/sd/sdcard: Implement eMMC sleep state (CMD5) Philippe Mathieu-Daudé
2024-07-10 14:14 ` [PATCH v48 08/12] hw/sd/sdcard: Add emmc_cmd_SEND_EXT_CSD handler (CMD8) Philippe Mathieu-Daudé
2024-07-10 14:14 ` [PATCH v48 09/12] hw/sd/sdcard: Add eMMC 'boot-size' property Philippe Mathieu-Daudé
2024-07-10 14:14 ` Philippe Mathieu-Daudé [this message]
2024-07-10 14:14 ` [PATCH v48 11/12] hw/sd/sdcard: Add mmc SWITCH function support (CMD6) Philippe Mathieu-Daudé
2024-07-10 14:14 ` [PATCH v48 12/12] hw/sd/sdcard: Support boot area in emmc image Philippe Mathieu-Daudé
2024-07-10 15:35 ` [PATCH v48 00/12] hw/sd/sdcard: Add eMMC support Cédric Le Goater
2024-07-10 16:57   ` Philippe Mathieu-Daudé
2024-07-10 19:20     ` Cédric Le Goater
2024-07-11 11:03 ` Cédric Le Goater
2024-07-11 14:08   ` Philippe Mathieu-Daudé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240710141408.69275-11-philmd@linaro.org \
    --to=philmd@linaro.org \
    --cc=andrew@codeconstruct.com.au \
    --cc=bmeng.cn@gmail.com \
    --cc=clg@kaod.org \
    --cc=edgar.iglesias@amd.com \
    --cc=francisco.iglesias@amd.com \
    --cc=jamin_lin@aspeedtech.com \
    --cc=joel@jms.id.au \
    --cc=leetroy@gmail.com \
    --cc=luc.michel@amd.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sai.pavan.boddu@amd.com \
    --cc=steven_lee@aspeedtech.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).