From patchwork Fri May 4 15:59:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 10380981 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5668E6037D for ; Fri, 4 May 2018 16:08:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F096294E2 for ; Fri, 4 May 2018 16:08:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 30CE2294E4; Fri, 4 May 2018 16:08:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9E372294E2 for ; Fri, 4 May 2018 16:08:32 +0000 (UTC) Received: from localhost ([::1]:35069 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEdG3-0000dx-F9 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 04 May 2018 12:08:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEd7l-0002oq-V6 for qemu-devel@nongnu.org; Fri, 04 May 2018 11:59:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fEd7j-0001cf-CE for qemu-devel@nongnu.org; Fri, 04 May 2018 11:59:58 -0400 Received: from mail-qt0-x241.google.com ([2607:f8b0:400d:c0d::241]:39865) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fEd7j-0001cX-7O for qemu-devel@nongnu.org; Fri, 04 May 2018 11:59:55 -0400 Received: by mail-qt0-x241.google.com with SMTP id f1-v6so27973161qtj.6 for ; Fri, 04 May 2018 08:59:55 -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=FDQkWRBdTk16khbrk/p7o3cj/hcSlLsdvKrjJkPthx8=; b=ZhWDTxy2Ko207Q6U+rlSid0BT4jb4FSPYA8xE+HUk6T4jWOY6kR8kpKpejy+ZIca7O kWMZV27kEbI80JJxPE4zJWDpFciZSUFOEvzqDBTwAnEuz0dmpbx8hud5W1g20dlduriv qHKGxkJxIDTmvGwQWCvW2FjKB7Y+AMrPQSixEV9M0zk4Zjt3oUMCYQR88xjMry05M6af w92zIaFWwOjZfue1jtw4JLmgWlB5gMiIBjj4+iX9Q/j8wEE5ycBHpWFLVJDkEz9UVzX0 RgGqh/YQBcXDdvh7xiMa/DuXG8zGcyMreTVwOnMW7n2Bv/dw45aY2GudbUyBC8RkqqWA Wzlw== 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=FDQkWRBdTk16khbrk/p7o3cj/hcSlLsdvKrjJkPthx8=; b=Y0nJgdSN2q8WhfaSlNrmR3dqbBw2riZ2J+rm7hSOmjc0N4dGuUOG3+tCH8zy31tiDI DSqkApMFI+oh2JsHRZjy14UnzG9ok+2Fj88zOyxsZXWiUhNRI9sG+gfxMKKiTUu1T985 CHYePcwYHLy5ykgen5ea8x1gv9JK9f7GlNBQr4UsjtvfFuXgHbaq82t2vAcNc2QWG0e3 ixA1lw4MaEcSrXBxF2yGFOzNbcVjBX/6A7E3zoB7YPvowaRyaSqAtQYm+neqrwk48+Eo XnsS/kMpPWwtBBExBqVap1t5K5YcrwYquFGRFaC1sccvfMtMgbhbT6rYH4wEZWm+b2A8 PgkQ== X-Gm-Message-State: ALQs6tBbvZ9s/23XZV5EOSMkauzotB5pZcVwgJttZdxdjhLZOiZoHQB1 pLgsWdCu+zzoieM/+EJxeA8= X-Google-Smtp-Source: AB8JxZpXmbsHlm82+ZNVshl92pJVZztWWybsO2MnBQf1QHTQOD4sz3jwF2o6sHAB9nGJ7/TbSvUJvA== X-Received: by 2002:ac8:354f:: with SMTP id z15-v6mr7536321qtb.295.1525449594647; Fri, 04 May 2018 08:59:54 -0700 (PDT) Received: from x1.local ([138.117.48.222]) by smtp.gmail.com with ESMTPSA id a83sm8587066qkb.22.2018.05.04.08.59.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 May 2018 08:59:54 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: Peter Maydell , "Edgar E . Iglesias" Date: Fri, 4 May 2018 12:59:07 -0300 Message-Id: <20180504155918.21287-10-f4bug@amsat.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180504155918.21287-1-f4bug@amsat.org> References: <20180504155918.21287-1-f4bug@amsat.org> MIME-Version: 1.0 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 09/20] sdcard: Expose sd_crc*() functions for QTest use 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: Paolo Bonzini , Alistair Francis , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Take those functions out of hw/sd/sd.c (via "sdmmc-internal.h") to be able to write QTests for them. Signed-off-by: Philippe Mathieu-Daudé --- hw/sd/sdmmc-internal.h | 22 ++++++++++++++++++++++ hw/sd/sd.c | 37 ------------------------------------- hw/sd/sdmmc-internal.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 37 deletions(-) diff --git a/hw/sd/sdmmc-internal.h b/hw/sd/sdmmc-internal.h index 9aa04766fc..62c0ff250d 100644 --- a/hw/sd/sdmmc-internal.h +++ b/hw/sd/sdmmc-internal.h @@ -36,4 +36,26 @@ const char *sd_cmd_name(uint8_t cmd); */ const char *sd_acmd_name(uint8_t cmd); +/** + * sd_crc7: + * @data: pointer to the data buffer + * @data_len: data length + * + * Calculate the 7-bit CRC of a SD frame. + * + * Returns: The frame CRC. + */ +uint8_t sd_crc7(const void *data, size_t data_len); + +/** + * sd_crc16: + * @data: pointer to the data buffer + * @data_len: data length + * + * Calculate the 16-bit CRC of a SD data frame. + * + * Returns: The frame CRC. + */ +uint16_t sd_crc16(const void *data, size_t data_len); + #endif diff --git a/hw/sd/sd.c b/hw/sd/sd.c index a28ef8de5e..11b4606051 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -237,43 +237,6 @@ static const int sd_cmd_class[SDMMC_CMD_MAX] = { 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8, }; -static uint8_t sd_crc7(const void *message, size_t width) -{ - int i, bit; - uint8_t shift_reg = 0x00; - const uint8_t *msg = (const uint8_t *)message; - - for (i = 0; i < width; i++, msg++) { - for (bit = 7; bit >= 0; bit--) { - shift_reg <<= 1; - if ((shift_reg >> 7) ^ ((*msg >> bit) & 1)) { - shift_reg ^= 0x89; - } - } - } - - return shift_reg; -} - -static uint16_t sd_crc16(const void *message, size_t width) -{ - int i, bit; - uint16_t shift_reg = 0x0000; - const uint16_t *msg = (const uint16_t *)message; - width <<= 1; - - for (i = 0; i < width; i++, msg++) { - for (bit = 15; bit >= 0; bit--) { - shift_reg <<= 1; - if ((shift_reg >> 15) ^ ((*msg >> bit) & 1)) { - shift_reg ^= 0x1011; - } - } - } - - return shift_reg; -} - #define OCR_POWER_DELAY_NS 500000 /* 0.5ms */ FIELD(OCR, VDD_VOLTAGE_WINDOW, 0, 24) diff --git a/hw/sd/sdmmc-internal.c b/hw/sd/sdmmc-internal.c index 2053def3f1..69ad0a99e6 100644 --- a/hw/sd/sdmmc-internal.c +++ b/hw/sd/sdmmc-internal.c @@ -70,3 +70,41 @@ const char *sd_acmd_name(uint8_t cmd) return acmd_abbrev[cmd] ? acmd_abbrev[cmd] : "UNKNOWN_ACMD"; } + +/* 7 bit CRC with polynomial x^7 + x^3 + 1 */ +uint8_t sd_crc7(const void *message, size_t width) +{ + int i, bit; + uint8_t shift_reg = 0x00; + const uint8_t *msg = (const uint8_t *)message; + + for (i = 0; i < width; i++, msg++) { + for (bit = 7; bit >= 0; bit--) { + shift_reg <<= 1; + if ((shift_reg >> 7) ^ ((*msg >> bit) & 1)) { + shift_reg ^= 0x89; + } + } + } + + return shift_reg; +} + +uint16_t sd_crc16(const void *message, size_t width) +{ + int i, bit; + uint16_t shift_reg = 0x0000; + const uint16_t *msg = (const uint16_t *)message; + width <<= 1; + + for (i = 0; i < width; i++, msg++) { + for (bit = 15; bit >= 0; bit--) { + shift_reg <<= 1; + if ((shift_reg >> 15) ^ ((*msg >> bit) & 1)) { + shift_reg ^= 0x1011; + } + } + } + + return shift_reg; +}