From nobody Mon Nov 4 20:56:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1720620893; cv=none; d=zohomail.com; s=zohoarc; b=CXdWBhZCF3LczP2AE2iXgswEiH4uiIfRuvpPXg9J0ZxfAmek1vCGTIFB6NtmvdIwSQ9LtbiYpIQeS3G9x19n/9mWkUYdkaYZf+6NUaO0Zob5TnIKDHhn9j58ySTtaJIQ6hTyoRyTmF1fMITNXpNBJK7ovXZzIl/Up9cDby5vyb0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720620893; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=7+n7iA8FGhZGBefoarGDOiJy21w0ojlipWHKW8I5p4Y=; b=eY/fmUq2XNIMOHzcMdgUo2CSCLC6EO60+rEEq7I4zQO3HVgnmvP17W91DnAbRPBLaq3ffOiBmUubff0blpDSQyd9+0wAs9xqw9fkvfykOkn7MbRLeHcalVWQfD6JTMHAvwRY3Bf9EHJM+Of1DyWaSzFEfjDiE/nz1XiSVej0GOQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720620892993193.43153259348412; Wed, 10 Jul 2024 07:14:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sRY5S-0005O6-Ts; Wed, 10 Jul 2024 10:14:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sRY5R-0005Ey-8U for qemu-devel@nongnu.org; Wed, 10 Jul 2024 10:14:25 -0400 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sRY5O-0003oQ-Te for qemu-devel@nongnu.org; Wed, 10 Jul 2024 10:14:24 -0400 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-36796d2e5a9so4248276f8f.3 for ; Wed, 10 Jul 2024 07:14:22 -0700 (PDT) Received: from m1x-phil.lan ([176.176.167.117]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfab16csm5390472f8f.93.2024.07.10.07.14.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 10 Jul 2024 07:14:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1720620859; x=1721225659; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7+n7iA8FGhZGBefoarGDOiJy21w0ojlipWHKW8I5p4Y=; b=UQAz8XMIw/1TEXi0nbXIajjc4pCT7cAglQV8h/v1N7xyWFAYcSQQT7jlMlL+cRhlJp Wy9mDzca748iC6M7V7FHZSHDI3YJY2yyowkYqyR9p+Io5Esfam5+RaEKqXEdsg3Rhi2k TRCUC6CNIrhEaIBi3i3B9yjbStViFFCKa1YdH+onPZyEob635Tzoqf3pqmqqe1XK7Af6 fM7slkUAafWk192TqZS/6jtkVrAydn6/TXeqxicD+1RtF2F8iFBUPspNxz3amit1H53q B9+7oBc/+MA3uA8/qntwrkxO1OwmGYYYDHjoYJS+Im6Fx0ZxO1UhQgsQ6N9YnGiwUd+J cN/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720620859; x=1721225659; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7+n7iA8FGhZGBefoarGDOiJy21w0ojlipWHKW8I5p4Y=; b=j+ZF8qIS82aK4xdDg7j7isOxRTK6bVmz1yc+rmRRy13SmO5ftiZpu5RZQlc8q60+W8 JpJUQ5mDruS8Z9163h/Ygyctbsdrws69EhUJbPQbeb5h66mofxk/leEH1Y2tSIPEd/Ov drT2RYjVxw6gaX8fEuPKypT28QwUuTv5JqYaPtUtkFS1EFueiT6GK9NuXI2/eCG1sVtf rMoHFjF3PaIFX0kX845e9AWxRmURdRDwX6h5ZJdGnQIpEHg7ofWFwltgJF+XLxE5NjmL H6EPIHWw8IlHCGLumyeP+MsmnGOkB+pVRYuEKbRBPN4Nx237zVSTH3WWXL9z5MQggOvM mp0A== X-Gm-Message-State: AOJu0YxInKa8dFZBFOJnpqo3/5kcP5jVA2XVSWePimtx+/rFpSYtaK9a cL2KiDHnd/bkULMVXQwfqvV0/fOAhRXJ4lYnO85LzQ9a1mmUSkXV7gNcQisulMUb2Igv1raiR+A L X-Google-Smtp-Source: AGHT+IHEmKsViy5gW8ZJF7bb5wzXJ8yTMMi98BAp7u1fQE1m221IcePuXQaYRF5zqE8sSRKGq2PDIw== X-Received: by 2002:a5d:4e46:0:b0:367:9522:5e70 with SMTP id ffacd0b85a97d-367ceacb5c0mr4075492f8f.52.1720620859363; Wed, 10 Jul 2024 07:14:19 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Bin Meng , Steven Lee , Andrew Jeffery , Francisco Iglesias , Sai Pavan Boddu , Luc Michel , qemu-arm@nongnu.org, qemu-block@nongnu.org, Troy Lee , Jamin Lin , Peter Maydell , Joel Stanley , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , "Edgar E . Iglesias" , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v48 01/12] hw/sd/sdcard: Basis for eMMC support Date: Wed, 10 Jul 2024 16:13:57 +0200 Message-ID: <20240710141408.69275-2-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240710141408.69275-1-philmd@linaro.org> References: <20240710141408.69275-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=philmd@linaro.org; helo=mail-wr1-x429.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1720620894385116600 From: Philippe Mathieu-Daud=C3=A9 Since eMMC are soldered on boards, it is not user-creatable. RCA register is initialized to 0x0001, per spec v4.3, chapter 8.5 "RCA register": The default value of the RCA register is 0x0001. The value 0x0000 is reserved to set all cards into the Stand-by State with CMD7. The CSD register is very similar to SD one, except the version announced is v4.3. eMMC CID register is slightly different from SD: - One extra PNM (5 -> 6) - MDT is only 1 byte (2 -> 1). Signed-off-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: C=C3=A9dric Le Goater Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- TODO: comment magic CSD values? --- include/hw/sd/sd.h | 3 ++ hw/sd/sd.c | 107 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/include/hw/sd/sd.h b/include/hw/sd/sd.h index 0d6d9e452b..d35a839f5e 100644 --- a/include/hw/sd/sd.h +++ b/include/hw/sd/sd.h @@ -96,6 +96,9 @@ OBJECT_DECLARE_TYPE(SDState, SDCardClass, SD_CARD) #define TYPE_SD_CARD_SPI "sd-card-spi" DECLARE_INSTANCE_CHECKER(SDState, SD_CARD_SPI, TYPE_SD_CARD_SPI) =20 +#define TYPE_EMMC "emmc" +DECLARE_INSTANCE_CHECKER(SDState, EMMC, TYPE_EMMC) + struct SDCardClass { /*< private >*/ DeviceClass parent_class; diff --git a/hw/sd/sd.c b/hw/sd/sd.c index d6a07f0ade..a4b6c7baba 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -2,6 +2,8 @@ * SD Memory Card emulation as defined in the "SD Memory Card Physical * layer specification, Version 2.00." * + * eMMC emulation defined in "JEDEC Standard No. 84-A43" + * * Copyright (c) 2006 Andrzej Zaborowski * Copyright (c) 2007 CodeSourcery * Copyright (c) 2018 Philippe Mathieu-Daud=C3=A9 @@ -169,12 +171,18 @@ struct SDState { static void sd_realize(DeviceState *dev, Error **errp); =20 static const SDProto sd_proto_spi; +static const SDProto sd_proto_emmc; =20 static bool sd_is_spi(SDState *sd) { return sd->proto =3D=3D &sd_proto_spi; } =20 +static bool sd_is_emmc(SDState *sd) +{ + return sd->proto =3D=3D &sd_proto_emmc; +} + static const char *sd_version_str(enum SDPhySpecificationVersion version) { static const char *sdphy_version[] =3D { @@ -438,6 +446,23 @@ static void sd_set_cid(SDState *sd) sd->cid[15] =3D (sd_crc7(sd->cid, 15) << 1) | 1; } =20 +static void emmc_set_cid(SDState *sd) +{ + sd->cid[0] =3D MID; /* Fake card manufacturer ID (MID) */ + sd->cid[1] =3D 0b01; /* CBX: soldered BGA */ + sd->cid[2] =3D OID[0]; /* OEM/Application ID (OID) */ + sd->cid[3] =3D PNM[0]; /* Fake product name (PNM) */ + sd->cid[4] =3D PNM[1]; + sd->cid[5] =3D PNM[2]; + sd->cid[6] =3D PNM[3]; + sd->cid[7] =3D PNM[4]; + sd->cid[8] =3D PNM[4]; + sd->cid[9] =3D PRV; /* Fake product revision (PRV) */ + stl_be_p(&sd->cid[10], 0xdeadbeef); /* Fake serial number (PSN) */ + sd->cid[14] =3D (MDT_MON << 4) | (MDT_YR - 1997); /* Manufacture date = (MDT) */ + sd->cid[15] =3D (sd_crc7(sd->cid, 15) << 1) | 1; +} + /* Card-Specific Data register */ =20 #define HWBLOCK_SHIFT 9 /* 512 bytes */ @@ -451,6 +476,44 @@ static const uint8_t sd_csd_rw_mask[16] =3D { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe, }; =20 +static void emmc_set_csd(SDState *sd, uint64_t size) +{ + int hwblock_shift =3D HWBLOCK_SHIFT; + uint32_t sectsize =3D (1 << (SECTOR_SHIFT + 1)) - 1; + uint32_t wpsize =3D (1 << (WPGROUP_SHIFT + 1)) - 1; + + sd->csd[0] =3D (2 << 6) | (4 << 2); + sd->csd[1] =3D 0x07; + sd->csd[2] =3D 0x00; + sd->csd[3] =3D 0x32; + sd->csd[4] =3D 0x0f; + if (size <=3D 2 * GiB) { + /* use 1k blocks */ + uint32_t csize1k =3D (size >> (CMULT_SHIFT + 10)) - 1; + sd->csd[5] =3D 0x5a; + sd->csd[6] =3D 0x80 | ((csize1k >> 10) & 0xf); + sd->csd[7] =3D (csize1k >> 2) & 0xff; + } else { /* >=3D 2GB : size stored in ext CSD, block addressing */ + sd->csd[5] =3D 0x59; + sd->csd[6] =3D 0x8f; + sd->csd[7] =3D 0xff; + sd->ocr =3D FIELD_DP32(sd->ocr, OCR, CARD_CAPACITY, 1); + } + sd->csd[8] =3D 0xff; + sd->csd[9] =3D 0xfc | /* Max. write current */ + ((CMULT_SHIFT - 2) >> 1); + sd->csd[10] =3D 0x40 | /* Erase sector size */ + (((CMULT_SHIFT - 2) << 7) & 0x80) | (sectsize >> 1); + sd->csd[11] =3D 0x00 | /* Write protect group size */ + ((sectsize << 7) & 0x80) | wpsize; + sd->csd[12] =3D 0x90 | /* Write speed factor */ + (hwblock_shift >> 2); + sd->csd[13] =3D 0x20 | /* Max. write data block length */ + ((hwblock_shift << 6) & 0xc0); + sd->csd[14] =3D 0x00; + sd->csd[15] =3D (sd_crc7(sd->csd, 15) << 1) | 1; +} + static void sd_set_csd(SDState *sd, uint64_t size) { int hwblock_shift =3D HWBLOCK_SHIFT; @@ -697,7 +760,7 @@ static void sd_reset(DeviceState *dev) sd->state =3D sd_idle_state; =20 /* card registers */ - sd->rca =3D 0x0000; + sd->rca =3D sd_is_emmc(sd) ? 0x0001 : 0x0000; sd->size =3D size; sd_set_ocr(sd); sd_set_scr(sd); @@ -2375,6 +2438,13 @@ static const SDProto sd_proto_sd =3D { }, }; =20 +static const SDProto sd_proto_emmc =3D { + /* Only v4.3 is supported */ + .name =3D "eMMC", + .cmd =3D { + }, +}; + static void sd_instance_init(Object *obj) { SDState *sd =3D SDMMC_COMMON(obj); @@ -2446,6 +2516,15 @@ static void sd_realize(DeviceState *dev, Error **err= p) } } =20 +static void emmc_realize(DeviceState *dev, Error **errp) +{ + SDState *sd =3D SDMMC_COMMON(dev); + + sd->spec_version =3D SD_PHY_SPECv3_01_VERS; /* Actually v4.3 */ + + sd_realize(dev, errp); +} + static Property sdmmc_common_properties[] =3D { DEFINE_PROP_DRIVE("drive", SDState, blk), DEFINE_PROP_END_OF_LIST() @@ -2457,6 +2536,10 @@ static Property sd_properties[] =3D { DEFINE_PROP_END_OF_LIST() }; =20 +static Property emmc_properties[] =3D { + DEFINE_PROP_END_OF_LIST() +}; + static void sdmmc_common_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); @@ -2509,6 +2592,23 @@ static void sd_spi_class_init(ObjectClass *klass, vo= id *data) sc->proto =3D &sd_proto_spi; } =20 +static void emmc_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + SDCardClass *sc =3D SDMMC_COMMON_CLASS(klass); + + dc->desc =3D "eMMC"; + dc->realize =3D emmc_realize; + device_class_set_props(dc, emmc_properties); + /* Reason: Soldered on board */ + dc->user_creatable =3D false; + + sc->proto =3D &sd_proto_emmc; + + sc->set_cid =3D emmc_set_cid; + sc->set_csd =3D emmc_set_csd; +} + static const TypeInfo sd_types[] =3D { { .name =3D TYPE_SDMMC_COMMON, @@ -2530,6 +2630,11 @@ static const TypeInfo sd_types[] =3D { .parent =3D TYPE_SD_CARD, .class_init =3D sd_spi_class_init, }, + { + .name =3D TYPE_EMMC, + .parent =3D TYPE_SDMMC_COMMON, + .class_init =3D emmc_class_init, + }, }; =20 DEFINE_TYPES(sd_types) --=20 2.41.0