All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
To: Peter Maydell <peter.maydell@linaro.org>,
	"Edgar E . Iglesias" <edgar.iglesias@xilinx.com>
Cc: "Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	qemu-devel@nongnu.org, "Paolo Bonzini" <pbonzini@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Alistair Francis" <alistair@alistair23.me>
Subject: [Qemu-devel] [PATCH 09/20] sdcard: Expose sd_crc*() functions for QTest use
Date: Fri,  4 May 2018 12:59:07 -0300	[thread overview]
Message-ID: <20180504155918.21287-10-f4bug@amsat.org> (raw)
In-Reply-To: <20180504155918.21287-1-f4bug@amsat.org>

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é <f4bug@amsat.org>
---
 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;
+}
-- 
2.17.0

  parent reply	other threads:[~2018-05-04 15:59 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-04 15:58 [Qemu-devel] [PATCH 00/20] sdcard: proper implementation of CRC Philippe Mathieu-Daudé
2018-05-04 15:58 ` [Qemu-devel] [PATCH 01/20] sdcard: Use the ldst API Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 02/20] sdcard: Extract sd_calc_frame48_crc7() from sd_req_crc_validate() Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 03/20] sdcard: Rename the SDRequest as SDFrame48 Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 04/20] sdcard: Add sd_prepare_request[_with_crc]() Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 05/20] sdcard: Use the sd_prepare_request*() functions Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 06/20] sdcard: Add a "validate-crc" property Philippe Mathieu-Daudé
2018-05-04 23:33   ` Alistair Francis
2018-05-04 15:59 ` [Qemu-devel] [PATCH 07/20] sdcard: Constify sd_crc*()'s message argument Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 08/20] sdcard: Fix sd_crc*() style Philippe Mathieu-Daudé
2018-05-04 23:34   ` Alistair Francis
2018-05-04 15:59 ` Philippe Mathieu-Daudé [this message]
2018-05-04 15:59 ` [Qemu-devel] [PATCH 10/20] sdcard: Expose sd_prepare_request*() functions for QTest use Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 11/20] sdcard: Add test_sd_request_frame_crc7() qtest (request command CRC7) Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 12/20] sdcard: Let sd_frame48_crc7_calc() work on response frames Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 13/20] sdcard: Expose sd_prepare_frame48() for QTest use Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 14/20] sdcard: Add test_sd_response_frame48_crc7 qtest (command response CRC7) Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 15/20] sdcard: Add SDFrame136 struct and 136-bit SD response frames functions Philippe Mathieu-Daudé
2018-05-04 16:57   ` Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 16/20] sdcard: Add test_sd_response_frame136_crc7() qtest Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 17/20] sdcard: Add SDFrameData struct and data frame checksum functions Philippe Mathieu-Daudé
2018-05-05  2:22   ` Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 18/20] sdcard: Fix sd_crc16() Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 19/20] sdcard: Add test_sd_data_frame_crc16() qtest Philippe Mathieu-Daudé
2018-05-04 15:59 ` [Qemu-devel] [PATCH 20/20] sdcard: Add test_sd_verify_cksum_frame48() qtest Philippe Mathieu-Daudé
2018-05-07 20:09 ` [Qemu-devel] [PATCH 00/20] sdcard: proper implementation of CRC 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=20180504155918.21287-10-f4bug@amsat.org \
    --to=f4bug@amsat.org \
    --cc=alistair@alistair23.me \
    --cc=edgar.iglesias@xilinx.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.