From nobody Wed Oct 29 22:55:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1525845866717401.0172848801841; Tue, 8 May 2018 23:04:26 -0700 (PDT) Received: from localhost ([::1]:54684 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGIDA-0001km-SW for importer@patchew.org; Wed, 09 May 2018 02:04:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50307) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGIAE-00004y-0U for qemu-devel@nongnu.org; Wed, 09 May 2018 02:01:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGIAC-0005cL-IN for qemu-devel@nongnu.org; Wed, 09 May 2018 02:01:22 -0400 Received: from mail-qt0-x241.google.com ([2607:f8b0:400d:c0d::241]:45975) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fGIAC-0005b0-Cq for qemu-devel@nongnu.org; Wed, 09 May 2018 02:01:20 -0400 Received: by mail-qt0-x241.google.com with SMTP id j42-v6so44200191qtj.12 for ; Tue, 08 May 2018 23:01:20 -0700 (PDT) Received: from x1.local ([138.117.48.222]) by smtp.gmail.com with ESMTPSA id 42-v6sm1707217qte.41.2018.05.08.23.01.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 23:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=G2Z3qZi9UvSIpvFOl93Ph9HOe2jrgUqjNP1nVSkUTMI=; b=ARyt+N68kwmv2hPiUWAb6hWCwgojnqfm1S3Zm7CupNVTxAwJK8cwYsfpuarx//xbcb cM5pjfmcEdJn2byT3cEcMI7qQcZxIm4pORQawUKlakELdUJr4onyQgfBeBRXAiYEsCYu 9yl9rLN8Z1vGqyD4QBBP1ZJcR7I3eMDDyZe9y8Sc7ROLvmOQAo4y0P4wcWyuJAgzNbPr 2Q0S1yOz+sCfDi2DkVhiPFuxGsg24lSc4FUfFzXG0VsBFttS3JTT09zj0VaaNxGH3Vf2 moaGMJj9EuGBE+xfVHVdugKvaHZb80iZfVZzmTwd5tM9T/6JRJOQyXWU7ZezW8QlkfE3 1ZrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=G2Z3qZi9UvSIpvFOl93Ph9HOe2jrgUqjNP1nVSkUTMI=; b=e1XbBiFeSvT9/WIqKpWBQZhaBs4JzYoEsFRP6A8B/3QSNU301lIdct0ueUvi2Ga8Gq K0DDXeNB68/49B28Mh9f4LUQ4GYbAU1bIIIgPGs3W+XfOqzmTeNCFs64vYRYpPTZNPzb Vodj4tzFszwCjRwu+NreWaJWqS1CO9iTGWpmoOLUY6hUPlQf8G4te2P8ibAuRQ7hJwkb ELbZQNe4nMhfuILac1blbtDVZ3xweVbUZxDr0VteLf2Lyl4Mbezj13qW4ui73Fd0PWgo nPICVGe5QUSGZYPvp1kiiTh719h7F6QKAD8NEwVO1+WViMBdr4D7KqZWUOxZbHc97LM7 w7uA== X-Gm-Message-State: ALQs6tDoPQe8/tmeJFXLCR3JthW8qBHzUrBmBZ2NYNlhn5IB222w7sYA axIXn7pdaAkvM17c0S4H9xwOF9X9 X-Google-Smtp-Source: AB8JxZp0/Btu5i0Cxko+R2KDL3NeXx8EAF9SZ5G6VbGcqMBe2ayH/80nGtI2KQi/rphAx2tdZtRljQ== X-Received: by 2002:a0c:bd84:: with SMTP id n4-v6mr40602259qvg.148.1525845679415; Tue, 08 May 2018 23:01:19 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: Peter Maydell , "Edgar E . Iglesias" , Alistair Francis Date: Wed, 9 May 2018 03:01:03 -0300 Message-Id: <20180509060104.4458-4-f4bug@amsat.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180509060104.4458-1-f4bug@amsat.org> References: <20180509060104.4458-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::241 Subject: [Qemu-devel] [PATCH v2 3/4] sdcard: Implement the UHS-I SWITCH_FUNCTION entries (Spec v3) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 [based on a patch from Alistair Francis from qemu/xilinx tag xilinx-v2015.2] Signed-off-by: Edgar E. Iglesias [PMD: rebased, changed magic by definitions, use stw_be_p, add tracing, check all functions in group are correct before setting the values] Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- hw/sd/sd.c | 186 +++++++++++++++++++++++++++++++++++++++------ hw/sd/trace-events | 1 + 2 files changed, 165 insertions(+), 22 deletions(-) diff --git a/hw/sd/sd.c b/hw/sd/sd.c index 24aaf0c767..d8dd88f872 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -128,6 +128,11 @@ struct SDState { bool enable; uint8_t dat_lines; bool cmd_line; + /** + * Whether the card entered the UHS mode with voltage level adjusted + * (use by soft-reset) + */ + bool uhs_activated; }; =20 static const char *sd_state_name(enum SDCardStates state) @@ -567,6 +572,7 @@ static void sd_reset(DeviceState *dev) sd->expecting_acmd =3D false; sd->dat_lines =3D 0xf; sd->cmd_line =3D true; + sd->uhs_activated =3D false; sd->multi_blk_cnt =3D 0; } =20 @@ -765,32 +771,168 @@ static uint32_t sd_wpbits(SDState *sd, uint64_t addr) return ret; } =20 +/* Function Group */ +enum { + SD_FG_MIN =3D 1, + SD_FG_ACCESS_MODE =3D 1, + SD_FG_COMMAND_SYSTEM =3D 2, + SD_FG_DRIVER_STRENGTH =3D 3, + SD_FG_CURRENT_LIMIT =3D 4, + SD_FG_RSVD_5 =3D 5, + SD_FG_RSVD_6 =3D 6, + SD_FG_MAX =3D 6, + SD_FG_COUNT +}; + +static const char *sd_fn_grp_name[SD_FG_COUNT] =3D { + [SD_FG_ACCESS_MODE] =3D "ACCESS_MODE", + [SD_FG_COMMAND_SYSTEM] =3D "COMMAND_SYSTEM", + [SD_FG_DRIVER_STRENGTH] =3D "DRIVER_STRENGTH", + [SD_FG_CURRENT_LIMIT] =3D "CURRENT_LIMIT", + [SD_FG_RSVD_5] =3D "RSVD5", + [SD_FG_RSVD_6] =3D "RSVD6", +}; + +/* Function name */ +enum { + SD_FN_NO_INFLUENCE =3D 0xf, + SD_FN_COUNT =3D 16 +}; + +typedef struct sd_fn_support { + const char *name; + bool uhs_only; + bool unimp; +} sd_fn_support; + +static const sd_fn_support *sd_fn_support_defs[SD_FG_COUNT] =3D { + [SD_FG_ACCESS_MODE] =3D (sd_fn_support [SD_FN_COUNT]) { + [0] =3D { .name =3D "default/SDR12" }, + [1] =3D { .name =3D "high-speed/SDR25" }, + [2] =3D { .name =3D "SDR50", .uhs_only =3D true }, + [3] =3D { .name =3D "SDR104", .uhs_only =3D true }, + [4] =3D { .name =3D "DDR50", .uhs_only =3D true }, + }, + [SD_FG_COMMAND_SYSTEM] =3D (sd_fn_support [SD_FN_COUNT]) { + [0] =3D { .name =3D "default" }, + [1] =3D { .name =3D "For eC" }, + [3] =3D { .name =3D "OTP", .unimp =3D true }, + [4] =3D { .name =3D "ASSD", .unimp =3D true }, + }, + [SD_FG_DRIVER_STRENGTH] =3D (sd_fn_support [SD_FN_COUNT]) { + [0] =3D { .name =3D "default/Type B" }, + [1] =3D { .name =3D "Type A", .uhs_only =3D true }, + [2] =3D { .name =3D "Type C", .uhs_only =3D true }, + [3] =3D { .name =3D "Type D", .uhs_only =3D true }, + }, + [SD_FG_CURRENT_LIMIT] =3D (sd_fn_support [SD_FN_COUNT]) { + [0] =3D { .name =3D "default/200mA" }, + [1] =3D { .name =3D "400mA", .uhs_only =3D true }, + [2] =3D { .name =3D "600mA", .uhs_only =3D true }, + [3] =3D { .name =3D "800mA", .uhs_only =3D true }, + }, + [SD_FG_RSVD_5] =3D (sd_fn_support [SD_FN_COUNT]) { + [0] =3D { .name =3D "default" }, + }, + [SD_FG_RSVD_6] =3D (sd_fn_support [SD_FN_COUNT]) { + [0] =3D { .name =3D "default" }, + }, +}; + +#define SD_FN_BUFSZ (SD_FG_MAX * 4 / BITS_PER_BYTE) + static void sd_function_switch(SDState *sd, uint32_t arg) { - int i, mode, new_func; - mode =3D !!(arg & 0x80000000); - - sd->data[0] =3D 0x00; /* Maximum current consumption */ - sd->data[1] =3D 0x01; - sd->data[2] =3D 0x80; /* Supported group 6 functions */ - sd->data[3] =3D 0x01; - sd->data[4] =3D 0x80; /* Supported group 5 functions */ - sd->data[5] =3D 0x01; - sd->data[6] =3D 0x80; /* Supported group 4 functions */ - sd->data[7] =3D 0x01; - sd->data[8] =3D 0x80; /* Supported group 3 functions */ - sd->data[9] =3D 0x01; - sd->data[10] =3D 0x80; /* Supported group 2 functions */ - sd->data[11] =3D 0x43; - sd->data[12] =3D 0x80; /* Supported group 1 functions */ - sd->data[13] =3D 0x03; - for (i =3D 0; i < 6; i ++) { - new_func =3D (arg >> (i * 4)) & 0x0f; - if (mode && new_func !=3D 0x0f) - sd->function_group[i] =3D new_func; - sd->data[14 + (i >> 1)] =3D new_func << ((i * 4) & 4); + bool invalid_function_selected =3D false; + int fn_grp, new_func, i; + uint8_t *data_p, tmpbuf[SD_FN_BUFSZ]; + uint16_t max_current_mA =3D 1; + bool mode =3D extract32(arg, 31, 1); /* 0: check only, 1: do switch */ + + /* Bits 400-495: information (16 bits each group) */ + data_p =3D &sd->data[2]; + for (fn_grp =3D SD_FG_MAX; fn_grp >=3D SD_FG_MIN; fn_grp--) { + uint16_t supported_fns =3D 1 << SD_FN_NO_INFLUENCE; + for (i =3D 0; i < SD_FN_COUNT; ++i) { + const sd_fn_support *def =3D &sd_fn_support_defs[fn_grp][i]; + + if (def->name && !def->unimp && + !(def->uhs_only && !sd->uhs_activated)) { + supported_fns |=3D 1 << i; + } + } + stw_be_p(data_p, supported_fns); + data_p +=3D 2; } + + /* Bits 376-399: function (4 bits each group) + * + * Do not write the values back directly: + * Check everything first writing to 'tmpbuf' + */ + data_p =3D tmpbuf; + for (fn_grp =3D SD_FG_MAX; fn_grp >=3D SD_FG_MIN; fn_grp--) { + new_func =3D (arg >> ((fn_grp - 1) * 4)) & 0x0f; + if (new_func =3D=3D SD_FN_NO_INFLUENCE) { + /* Guest requested no influence, so this function group + * stays the same. + */ + new_func =3D sd->function_group[fn_grp - 1]; + } else { + const sd_fn_support *def =3D &sd_fn_support_defs[fn_grp][new_f= unc]; + if (mode) { + if (!def->name) { + qemu_log_mask(LOG_GUEST_ERROR, + "Function %d not valid for " + "function group %d\n", + new_func, fn_grp); + invalid_function_selected =3D true; + new_func =3D SD_FN_NO_INFLUENCE; + } else if (def->unimp) { + qemu_log_mask(LOG_UNIMP, + "Function %s (fn grp %d) not implemented= \n", + def->name, fn_grp); + invalid_function_selected =3D true; + new_func =3D SD_FN_NO_INFLUENCE; + } else if (def->uhs_only && !sd->uhs_activated) { + qemu_log_mask(LOG_GUEST_ERROR, + "Function %s (fn grp %d) only " + "valid in UHS mode\n", + def->name, fn_grp); + invalid_function_selected =3D true; + new_func =3D SD_FN_NO_INFLUENCE; + } else { + sd->function_group[fn_grp - 1] =3D new_func; + } + } + trace_sdcard_function_select(def->name, sd_fn_grp_name[fn_grp], + mode); + } + if (!(fn_grp & 0x1)) { /* evens go in high nibble */ + *data_p =3D new_func << 4; + } else { /* odds go in low nibble */ + *(data_p++) |=3D new_func; + } + } + if (invalid_function_selected) { + /* Ignore all the set values */ + memset(&sd->data[14], 0, SD_FN_BUFSZ); + } else { + /* Write back if all functions in the group are correct. */ + memcpy(&sd->data[14], tmpbuf, SD_FN_BUFSZ); + } + + /* Data Structure Version =3D 0x00: 376 bits reserved (undefined) */ memset(&sd->data[17], 0, 47); + + if (invalid_function_selected) { + /* Maximum current consumption: If one of the selected functions + * was wrong, the return value will be 0. + */ + max_current_mA =3D 0; + } + stw_be_p(sd->data, max_current_mA); + stw_be_p(sd->data + 64, sd_crc16(sd->data, 64)); } =20 diff --git a/hw/sd/trace-events b/hw/sd/trace-events index bfd1d62efc..eba5ab7b90 100644 --- a/hw/sd/trace-events +++ b/hw/sd/trace-events @@ -48,6 +48,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, ui= nt8_t value) "%s %20s/ CMD%02d value 0x%02x" sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, int= length) "%s %20s/ CMD%02d len %d" sdcard_set_voltage(uint16_t millivolts) "%u mV" +sdcard_function_select(const char *fn_name, const char *grp_name, bool do_= switch) "Function %s (group: %s, sw: %u)" =20 # hw/sd/milkymist-memcard.c milkymist_memcard_memory_read(uint32_t addr, uint32_t value) "addr 0x%08x = value 0x%08x" --=20 2.17.0