qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
To: "Alistair Francis" <alistair.francis@xilinx.com>,
	"Edgar E . Iglesias" <edgar.iglesias@xilinx.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Andrey Smirnov" <andrew.smirnov@gmail.com>,
	"Andrey Yurovsky" <yurovsky@gmail.com>,
	"Cleber Rosa" <crosa@redhat.com>, "Kevin Wolf" <kwolf@redhat.com>,
	"Max Reitz" <mreitz@redhat.com>, "John Snow" <jsnow@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"Lukáš Doktor" <ldoktor@redhat.com>,
	"Daniel P . Berrange" <berrange@redhat.com>,
	"Eric Blake" <eblake@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Fam Zheng" <famz@redhat.com>, "Thomas Huth" <thuth@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Cc: "Philippe Mathieu-Daudé" <f4bug@amsat.org>, qemu-devel@nongnu.org
Subject: [Qemu-devel] [RFC PATCH 02/26] sdcard: add a Python qtest
Date: Wed, 13 Dec 2017 20:20:01 -0300	[thread overview]
Message-ID: <20171213232025.24503-3-f4bug@amsat.org> (raw)
In-Reply-To: <20171213232025.24503-1-f4bug@amsat.org>

Use Python to write high-level SD commands.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 tests/Makefile.include |   2 +
 tests/sdcard_tests.py  | 172 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 174 insertions(+)
 create mode 100755 tests/sdcard_tests.py

diff --git a/tests/Makefile.include b/tests/Makefile.include
index 13673f6d1d..4a1afcb499 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -355,8 +355,10 @@ check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
 gcov-files-arm-y += arm-softmmu/hw/block/virtio-blk.c
 check-qtest-arm-y += tests/test-arm-mptimer$(EXESUF)
 gcov-files-arm-y += hw/timer/arm_mptimer.c
+check-qtest-arm-y += tests/sdcard_tests.py
 
 check-qtest-aarch64-y = tests/numa-test$(EXESUF)
+check-qtest-aarch64-y += tests/sdcard_tests.py
 
 check-qtest-microblazeel-y = $(check-qtest-microblaze-y)
 
diff --git a/tests/sdcard_tests.py b/tests/sdcard_tests.py
new file mode 100755
index 0000000000..033b756cf1
--- /dev/null
+++ b/tests/sdcard_tests.py
@@ -0,0 +1,172 @@
+#!/usr/bin/env python
+#
+# Tests for the SD-Bus protocol
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import os
+import base64
+import struct
+import binascii
+import logging
+
+import qtest
+
+
+# CMD
+(GO_IDLE_STATE, SEND_OP_CMD, ALL_SEND_CID, SEND_RELATIVE_ADDR, # 0 ...
+SEND_DSR, CMD5, SWITCH_FUNCTION, CMD7,
+SEND_IF_COND, SEND_CSD, SEND_CID, READ_DAT_UNTIL_STOP, # 8 ...
+STOP_TRANSMISSION, SEND_STATUS, CMD14, GO_INACTIVE_STATE,
+SET_BLOCKLEN, READ_SINGLE_BLOCK, READ_MULTIPLE_BLOCK, CMD19, # 16 ...
+CMD20, CMD21, CMD22, SET_BLOCK_COUNT,
+WRITE_SINGLE_BLOCK, WRITE_MULTIPLE_BLOCK, PROGRAM_CID, PROGRAM_CSD,  # 24 ...
+SET_WRITE_PROT, CLR_WRITE_PROT, SEND_WRITE_PROT, CMD31,
+ERASE_WR_BLK_START, ERASE_WR_BLK_END, CMD34, CMD35, # 32 ...
+CMD36, CMD37, ERASE, CMD39,
+CMD40, CMD41, LOCK_UNLOCK, CMD43,
+CMD44, CMD45, CMD46, CMD47,
+CMD48, CMD49, CMD50, CMD51,
+CMD52, CMD53, CMD54, APP_CMD,
+GEN_CMD,) = range(57)
+
+# ACMD
+SET_BUS_WIDTH = 6
+SD_STATUS = 13
+SEND_NUM_WR_BLOCKS = 22
+SET_WR_BLK_ERASE_COUNT = 23
+SD_APP_OP_COND = 41
+SET_CLR_CARD_DETECT = 42
+SEND_SCR = 51
+
+
+class SDBus(object):
+    def __init__(self, vm, qom_path, verbose=False):
+        self.vm = vm
+        self.path = qom_path
+        self.verbose = verbose
+
+    def do_cmd(self, command, arg=0, verbose=None):
+        assert command < 64
+        if verbose is None:
+            verbose = self.verbose
+
+        data = self.vm.qmp('sdbus-command', qom_path=self.path, command=command,
+                           arg=arg)['return']
+        if 'base64' in data:
+            data = base64.b64decode(data['base64'])
+            logging.info("CMD#%02d -> %s" % (command, binascii.hexlify(data)))
+        else:
+            logging.info("CMD#%02d -> (none)" % (command))
+            data = None
+        return data
+
+    def do_acmd(self, command, arg=0, verbose=None):
+        self.do_cmd(APP_CMD, verbose=False)
+        return self.do_cmd(command, arg, verbose if verbose else self.verbose)
+
+
+def sdbus_get_qom_path(vm, bus=0):
+        qom_paths = []
+
+        result = vm.qmp('query-block')
+        for block in result['return']:
+            if not 'qdev' in block:
+                continue
+            d = vm.qmp('qom-get', path=block['qdev'], property="parent_bus")
+            qom_paths += [d['return']]
+        assert len(qom_paths) > 0
+
+        return qom_paths[bus]
+
+
+# dumb helper
+def l(buf):
+    return struct.unpack("<L", buf)[0]
+
+
+class TestSdCardSpecV2(qtest.QMPTestCase):
+    def setUp(self):
+        self.vm = qtest.createQtestMachine()
+
+        self.vm._args.append('-drive')
+        self.vm._args.append("id=testcard,driver=null-co,if=sd")
+        self.vm._args.append("-machine")
+        self.vm._args.append("xilinx-zynq-a9")
+        self.vm.launch()
+
+        bus_path = sdbus_get_qom_path(self.vm)
+        self.bus = SDBus(self.vm, bus_path, True)
+
+    def tearDown(self):
+        self.vm.shutdown()
+
+    def test_power_on_v2(self):
+
+        self.bus.do_cmd(GO_IDLE_STATE)
+
+        # get voltages
+        for i in range(6):
+            vhs = 1 << (8 + i)
+            data = self.bus.do_cmd(SEND_IF_COND, vhs)
+            v = l(data)
+            self.assertNotEqual(v, 0)
+            self.assertEqual(vhs, v >> 8)
+
+        # get OCR
+        data = self.bus.do_acmd(SD_APP_OP_COND)
+        v = l(data)
+        ocr = (v >> 8) & 0xffff
+        s1_8 = (v >> 24) & 1
+        uhs_ii = (v >> 29) & 1
+        ccs = (v >> 30) & 1
+        init = (v >> 31) & 1
+        # all those are null for v2.00
+        self.assertEqual(s1_8, 0)
+        self.assertEqual(uhs_ii, 0)
+        self.assertEqual(ccs, 0)
+        self.assertEqual(init, 0)
+
+        # ocr << 8
+        # 0 << 24 # use current voltage
+        # 0 << 28 # powersave
+        # 0 << 30 # SDSC
+        data = self.bus.do_acmd(SD_APP_OP_COND, ocr << 8)
+        v = l(data)
+        # check OCR accepted
+        self.assertEqual(ocr, v >> 8)
+
+        # check CID
+        data = self.bus.do_cmd(ALL_SEND_CID)
+        oid, pnm, psn = struct.unpack(">x2s5sxLxxx", data)
+        self.assertEqual(oid, "XY") # QEMU default
+        self.assertEqual(pnm, "QEMU!") # QEMU default
+        self.assertEqual(psn, 0xdeadbeef) # QEMU default
+
+        # check non null RCA
+        data = self.bus.do_cmd(SEND_RELATIVE_ADDR)
+        rca, = struct.unpack(">H", data[:2])
+        self.assertNotEqual(rca, 0)
+
+        self.assertEqual(rca, 0x4567) # QEMU default
+
+        # check for new RCA
+        data = self.bus.do_cmd(SEND_RELATIVE_ADDR)
+        new_rca, = struct.unpack(">H", data[:2])
+        self.assertNotEqual(new_rca, rca)
+
+
+if __name__ == '__main__':
+    qtest.main(supported_machines=['xilinx-zynq-a9'])
-- 
2.15.1

  parent reply	other threads:[~2017-12-13 23:21 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-13 23:19 [Qemu-devel] [PATCH 00/26] SDCard housekeeping Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 01/26] sdbus: add a QMP command to access a SDBus Philippe Mathieu-Daudé
2017-12-14  9:06   ` Kevin Wolf
2017-12-14  9:34     ` Paolo Bonzini
2017-12-14 13:25       ` Philippe Mathieu-Daudé
2017-12-15  8:13         ` Paolo Bonzini
2017-12-14 13:18     ` Philippe Mathieu-Daudé
2017-12-15 19:53     ` Eric Blake
2017-12-13 23:20 ` Philippe Mathieu-Daudé [this message]
2017-12-14  9:34   ` [Qemu-devel] [RFC PATCH 02/26] sdcard: add a Python qtest Paolo Bonzini
2017-12-13 23:20 ` [Qemu-devel] [PATCH 03/26] sdcard: use ldst API Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 04/26] sdcard: replace fprintf() -> qemu_log_mask() Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 05/26] sdcard: rename sd_set_mode() -> sd_update_mode() Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 06/26] sdcard: add sd_set_mode() Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 07/26] sdcard: add sdcard_set_mode() trace event Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 08/26] sdcard: add sd_set_state() Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 09/26] sdcard: add a sdcard_set_state() trace event Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 10/26] sdcard: use more detailled state/mode trace events Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 11/26] sdcard: use warn_report() instead of fprintf() Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 12/26] sdcard: replace DPRINTF() by trace events Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 13/26] sdcard: add more " Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [RFC PATCH 14/26] sdcard: use qemu_hexbuf_strdup() to trace command response Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 15/26] sdcard: use PW_LEN define instead of '16' magic Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 16/26] sdcard: let cmd_valid_while_locked() returns a bool Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 17/26] sdcard: rename sd_set_REG() functions called by sd_reset() as sd_reset_REG() Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 18/26] sdcard: move Memory Card registers together Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 19/26] sdcard: add DSR register Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 20/26] sdcard: add/use SD_CMD_MAX to check valid SD commands Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 21/26] sdcard: add sd_cmd_abbreviation() to resolve the SD command id Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 22/26] sdcard: reduce sd_cmd traces Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 23/26] sdcard: add ACMD trace events Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 24/26] sdcard: use a 16-bit type for the 16-bit RCA register Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 25/26] sdcard: add/use a SDCardCommandClass enum instead of magic numbers Philippe Mathieu-Daudé
2017-12-13 23:20 ` [Qemu-devel] [PATCH 26/26] sdcard: add/use a ccc_spi enum for the commands supported in SPI mode Philippe Mathieu-Daudé
2017-12-14  1:29 ` [Qemu-devel] [PATCH 00/26] SDCard housekeeping Philippe Mathieu-Daudé
2017-12-16  0:13 ` Alistair Francis
2018-01-02 16:40   ` 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=20171213232025.24503-3-f4bug@amsat.org \
    --to=f4bug@amsat.org \
    --cc=alistair.francis@xilinx.com \
    --cc=andrew.smirnov@gmail.com \
    --cc=berrange@redhat.com \
    --cc=crosa@redhat.com \
    --cc=eblake@redhat.com \
    --cc=edgar.iglesias@xilinx.com \
    --cc=ehabkost@redhat.com \
    --cc=famz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=ldoktor@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=thuth@redhat.com \
    --cc=yurovsky@gmail.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).