From patchwork Wed Sep 25 14:11:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1167279 X-Patchwork-Delegate: bmeng.cn@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="M/AbA7JV"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 46dgL53N1Gz9sNx for ; Thu, 26 Sep 2019 00:24:29 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 2A33EC22004; Wed, 25 Sep 2019 14:14:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id EF080C21FE5; Wed, 25 Sep 2019 14:13:36 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 1F57AC21FDF; Wed, 25 Sep 2019 14:12:25 +0000 (UTC) Received: from mail-io1-f65.google.com (mail-io1-f65.google.com [209.85.166.65]) by lists.denx.de (Postfix) with ESMTPS id 773AAC21FFA for ; Wed, 25 Sep 2019 14:12:25 +0000 (UTC) Received: by mail-io1-f65.google.com with SMTP id z19so14242415ior.0 for ; Wed, 25 Sep 2019 07:12:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B7v64KS7K9+WESi3BdQemfqTZALUcppyRtTN4PcJFMk=; b=M/AbA7JVhSb0DfPgydY4XGbetg6KC1DT/KP80VhndqI+spZuyfb8tj/hxhesAD49wq tch8mvG2T1wdKcdOpMduEsRLO0DDVu8MaBIyFwzrMs9Ma2DHIbk3W6pUrKGxT9jnVhZt o2GJggGk0ZMfbIA7D5MSfmHnJnj1uk155MgPQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B7v64KS7K9+WESi3BdQemfqTZALUcppyRtTN4PcJFMk=; b=LA32xJMFBGhVPxRn2Y/HFPNICusU2Xqe3QUJThbs7baPXm2HtdXTCA06TIB+zU3QhH pjK2VD0Qy1KuCUXNrDJutYgZ9tkUDX+Ugdq5nQwudnszMOj+GxA3LofQxLCVqdra9Yym rW4TFWQb5PBTUGRgtaZFs951LZKcx+FN+xs4tkTydZavuYAxYUjIwMDtxMaTQQA1Kktf 85Z6XTarDUbsvraekXO0F7gO7oCOlKAyBSZ+Ly1w6ekHxnYX5sKDXGbM8N+Ym2Bs3JXo 3p2kzsPLMzM60+G/Voi8HWxgR9AsvfXDRtBakf6Y5WH3mHCwonydoRkD1umdD78LP+el X+Cg== X-Gm-Message-State: APjAAAWa3aMvd4COHr0HsMi66GDp4lZY4yO5zmsuyXwfiuxEe9I928u/ Q+2SUEJJ9MMSJ7arnlhG1W5ErPUzGAU= X-Google-Smtp-Source: APXvYqwYWH3bbGNRugeLTOUb5decMZ1sxLNmmiT2NW5L4aPrn84UKUFcXBcak5bNImlda0T9fvWlug== X-Received: by 2002:a02:cca9:: with SMTP id t9mr5474557jap.69.1569420744190; Wed, 25 Sep 2019 07:12:24 -0700 (PDT) Received: from kiwi.bld.corp.google.com ([2620:15c:183:0:8223:87c:a681:66aa]) by smtp.gmail.com with ESMTPSA id d20sm196878ilk.83.2019.09.25.07.12.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Sep 2019 07:12:23 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Date: Wed, 25 Sep 2019 08:11:22 -0600 Message-Id: <20190925141147.191166-14-sjg@chromium.org> X-Mailer: git-send-email 2.23.0.444.g18eeb5a265-goog In-Reply-To: <20190925141147.191166-1-sjg@chromium.org> References: <20190925141147.191166-1-sjg@chromium.org> MIME-Version: 1.0 Cc: Jagannadha Sutradharudu Teki Subject: [U-Boot] [PATCH v2 13/38] spi: sandbox: Add a test driver for sandbox SPI flash X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" At present SPI-flash testing relies on a sandbox driver which emulates the SPI bus and implements a flash chip behind that emulated bus. This provides good coverage but can only implement features supported by the SPI bus. Add a new 'direct' SPI flash which is implemented directly by sandbox. This allows us to write a very simple test of the uclass interface. Signed-off-by: Simon Glass --- Changes in v2: None arch/sandbox/dts/test.dts | 4 ++ drivers/mtd/spi/Makefile | 2 +- drivers/mtd/spi/sandbox_direct.c | 99 ++++++++++++++++++++++++++++++++ test/dm/sf.c | 42 ++++++++++++++ 4 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 drivers/mtd/spi/sandbox_direct.c diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index 27b0baab278..208551d7c19 100644 --- a/arch/sandbox/dts/test.dts +++ b/arch/sandbox/dts/test.dts @@ -619,6 +619,10 @@ }; }; + spi-flash@0 { + compatible = "sandbox,spi-flash-direct"; + }; + syscon0: syscon@0 { compatible = "sandbox,syscon0"; reg = <0x10 16>; diff --git a/drivers/mtd/spi/Makefile b/drivers/mtd/spi/Makefile index 11a8f55132a..c700258254b 100644 --- a/drivers/mtd/spi/Makefile +++ b/drivers/mtd/spi/Makefile @@ -21,4 +21,4 @@ obj-$(CONFIG_SPI_FLASH) += spi-nor.o obj-$(CONFIG_SPI_FLASH_DATAFLASH) += sf_dataflash.o obj-$(CONFIG_SPI_FLASH_INTEL_FAST) += intel_fast_spi.o obj-$(CONFIG_SPI_FLASH_MTD) += sf_mtd.o -obj-$(CONFIG_SPI_FLASH_SANDBOX) += sandbox.o +obj-$(CONFIG_SPI_FLASH_SANDBOX) += sandbox.o sandbox_direct.o diff --git a/drivers/mtd/spi/sandbox_direct.c b/drivers/mtd/spi/sandbox_direct.c new file mode 100644 index 00000000000..43d8907710c --- /dev/null +++ b/drivers/mtd/spi/sandbox_direct.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Simulate a flash chip without an underlying SPI layer. Behaviour is only + * useful for testing. + * + * Copyright 2019 Google LLC + * + * Licensed under the GPL-2 or later. + */ + +#define LOG_CATEGORY UCLASS_SPI_FLASH + +#include +#include +#include + +/** + * struct sandbox_direct_priv - private data for this driver + * + * @read_byte: Byte to return when reading from the driver + */ +struct sandbox_direct_priv { + char read_byte; + int write_prot; +}; + +static int sandbox_direct_read(struct udevice *dev, u32 offset, size_t len, + void *buf) +{ + struct sandbox_direct_priv *priv = dev_get_priv(dev); + + if (offset == 1) + return -EIO; + memset(buf, priv->read_byte, len); + + return 0; +} + +static int sandbox_direct_write(struct udevice *dev, u32 offset, size_t len, + const void *buf) +{ + struct sandbox_direct_priv *priv = dev_get_priv(dev); + + if (offset == 1) + return -EIO; + if (len > 0) + priv->read_byte = *(u8 *)buf; + + return 0; +} + +static int sandbox_direct_erase(struct udevice *dev, u32 offset, size_t len) +{ + struct sandbox_direct_priv *priv = dev_get_priv(dev); + + if (offset == 1) + return -EIO; + if (len > 0) + priv->read_byte = 'c'; + + return 0; +} + +static int sandbox_direct_get_sw_write_prot(struct udevice *dev) +{ + struct sandbox_direct_priv *priv = dev_get_priv(dev); + + return priv->write_prot++ ? 1 : 0; +} + +static int sandbox_direct_probe(struct udevice *dev) +{ + struct sandbox_direct_priv *priv = dev_get_priv(dev); + + priv->read_byte = 'a'; + + return 0; +} + +static struct dm_spi_flash_ops sandbox_direct_ops = { + .read = sandbox_direct_read, + .write = sandbox_direct_write, + .erase = sandbox_direct_erase, + .get_sw_write_prot = sandbox_direct_get_sw_write_prot, +}; + +static const struct udevice_id sandbox_direct_ids[] = { + { .compatible = "sandbox,spi-flash-direct" }, + { } +}; + +U_BOOT_DRIVER(sandbox_sf_direct) = { + .name = "sandbox_sf_direct", + .id = UCLASS_SPI_FLASH, + .of_match = sandbox_direct_ids, + .probe = sandbox_direct_probe, + .ops = &sandbox_direct_ops, + .priv_auto_alloc_size = sizeof(struct sandbox_direct_priv), +}; diff --git a/test/dm/sf.c b/test/dm/sf.c index 3788d59052e..56277954c23 100644 --- a/test/dm/sf.c +++ b/test/dm/sf.c @@ -14,8 +14,13 @@ #include #include #include +#include #include +enum { + BUF_SIZE = 4, +}; + /* Simple test of sandbox SPI flash */ static int dm_test_spi_flash(struct unit_test_state *uts) { @@ -91,3 +96,40 @@ static int dm_test_spi_flash_func(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_spi_flash_func, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Test of the direct SPI flash interface (no SPI layer) */ +static int dm_test_spi_flash_direct(struct unit_test_state *uts) +{ + struct udevice *dev; + char buf[BUF_SIZE]; + int i; + + ut_assertok(uclass_get_device(UCLASS_SPI_FLASH, 1, &dev)); + + /* Check the read call */ + ut_asserteq(-EIO, spi_flash_read_dm(dev, 1, BUF_SIZE, buf)); + ut_assertok(spi_flash_read_dm(dev, 0, BUF_SIZE, buf)); + for (i = 0; i < BUF_SIZE; i++) + ut_asserteq('a', buf[i]); + + /* Check the write call */ + ut_asserteq(-EIO, spi_flash_write_dm(dev, 1, BUF_SIZE, buf)); + ut_assertok(spi_flash_write_dm(dev, 0, 1, "b")); + ut_assertok(spi_flash_read_dm(dev, 0, BUF_SIZE, buf)); + for (i = 0; i < BUF_SIZE; i++) + ut_asserteq('b', buf[i]); + + /* Check erase */ + ut_asserteq(-EIO, spi_flash_erase_dm(dev, 1, BUF_SIZE)); + ut_assertok(spi_flash_erase_dm(dev, 0, 1)); + ut_assertok(spi_flash_read_dm(dev, 0, BUF_SIZE, buf)); + for (i = 0; i < BUF_SIZE; i++) + ut_asserteq('c', buf[i]); + + /* Check write protection */ + ut_asserteq(0, spl_flash_get_sw_write_prot(dev)); + ut_asserteq(1, spl_flash_get_sw_write_prot(dev)); + + return 0; +} +DM_TEST(dm_test_spi_flash_direct, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);