Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1DC73C433EF for ; Tue, 26 Apr 2022 08:25:03 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 027DE83DBA; Tue, 26 Apr 2022 10:24:57 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=brickedbrain.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=aruba.it header.i=@aruba.it header.b="RJqdHd0J"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 646B383C96; Tue, 26 Apr 2022 10:24:53 +0200 (CEST) Received: from smtpcmd01-sp1.aruba.it (smtpcmd01-sp1.aruba.it [62.149.158.218]) by phobos.denx.de (Postfix) with ESMTP id 97AB583BDB for ; Tue, 26 Apr 2022 10:24:49 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=brickedbrain.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=luca.ellero@brickedbrain.com Received: from asem-TANK-H57.asem.intra ([151.1.184.193]) by Aruba Outgoing Smtp with ESMTPSA id jGUyngINR79nYjGV6nUkY8; Tue, 26 Apr 2022 10:24:49 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=aruba.it; s=a1; t=1650961489; bh=e84E5CPPCy/7QMsKFWbUI2V9x34UyiydEet3/QXloPA=; h=From:To:Subject:Date; b=RJqdHd0JZmcaf/+40UdlwLQFA2ld23FhKMkYpU14GpOgQ43vfCr0qKWJsUoDFYGQL xKqfr1Ew90bd3pqv99z0a2awmZn2egARY/v3OgUix9ja8FGLpyym6V3nxakjWP5uJq yVfbKEjAJsQQbY30bcIUUCal6lljl/pJDj9Owfeq9Jp1D8kuI5acYfJQoI+WjxGWOT a2yxbUp1s4UGIP1givStnXVdMw770lN3T2ZWU/UdAXQdjkaij4fwbDQfUaTiWIMvvM F889zgIN9KOfixG5PKKGhjYPSFd/GQm9Qg53NWdEZcYS2qMiAzVqPhzYYajfs6PQxO Cst3kZpV0w6jQ== From: Luca Ellero To: u-boot@lists.denx.de Cc: jagan@amarulasolutions.com, vigneshr@ti.com, bmeng.cn@gmail.com, york.sun@nxp.com, andrew.smirnov@gmail.com, Luca Ellero Subject: [PATCH v2 1/2] sf: dataflash: add support for "extended ID" Date: Tue, 26 Apr 2022 10:24:09 +0200 Message-Id: <20220426082410.4487-2-luca.ellero@brickedbrain.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220426082410.4487-1-luca.ellero@brickedbrain.com> References: <20220426082410.4487-1-luca.ellero@brickedbrain.com> X-CMAE-Envelope: MS4xfAsKmI7h9EsugaOt8gxdItQJ5AncBOpNhYPgNY6dBIlwFzjO9O9Gw0gbfY+w1moiHM0qJQhf3aG7ajSaOvVVs6RcGSHKwsoI2nld+uc4XRLrpN/R/B+6 vPyNt5ApKCK08tu8/vkaUqWdsiVOPE+8MKd0qJtTt8P9Or5a3FZWJNJV3fwTUKkNjxyLNwEWStbGMDFB7ndJJ6wLGphzduDZkbEoABmqwC+zUUrxicrZaqB/ glsV7SXS6+MHRGYNFCo2TdfHADRgeXZufbHRrqY1PLjtsHU+zOSTysP2K0ioEiZ8IXMFXp/39pYCzwsFSHdrBOlFFFSS2IRahoek8DT4cwQCji9V6p7i4TZ/ c9AFTYKFhr3mYVzXvUyc/XF/Su3AId8GAk7eb1FsJRlGCQnuVKczUJgXjzK2x8MqgWVgj/Lz X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean Take as reference Linux kernel code: drivers/mtd/devices/mtd_dataflash.c commit 1da8869a428317a6d3cd8d47184cf87feb34a98b Author: Andrey Smirnov Signed-off-by: Luca Ellero --- Changes for v2: - add reference to Linux kernel commit drivers/mtd/spi/sf_dataflash.c | 98 ++++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/drivers/mtd/spi/sf_dataflash.c b/drivers/mtd/spi/sf_dataflash.c index b59edd152c..351c41039a 100644 --- a/drivers/mtd/spi/sf_dataflash.c +++ b/drivers/mtd/spi/sf_dataflash.c @@ -70,6 +70,9 @@ #define OP_WRITE_SECURITY_REVC 0x9A #define OP_WRITE_SECURITY 0x9B /* revision D */ +#define DATAFLASH_SHIFT_EXTID 24 +#define DATAFLASH_SHIFT_ID 40 + struct dataflash { uint8_t command[16]; unsigned short page_offset; /* offset in flash address */ @@ -455,7 +458,7 @@ struct data_flash_info { * JEDEC id has a high byte of zero plus three data bytes: * the manufacturer id, then a two byte device id. */ - uint32_t jedec_id; + uint64_t jedec_id; /* The size listed here is what works with OP_ERASE_PAGE. */ unsigned nr_pages; @@ -463,6 +466,7 @@ struct data_flash_info { uint16_t pageoffset; uint16_t flags; +#define SUP_EXTID 0x0004 /* supports extended ID data */ #define SUP_POW2PS 0x0002 /* supports 2^N byte pages */ #define IS_POW2PS 0x0001 /* uses 2^N byte pages */ }; @@ -508,48 +512,26 @@ static struct data_flash_info dataflash_data[] = { { "at45db642d", 0x1f2800, 8192, 1024, 10, SUP_POW2PS | IS_POW2PS}, }; -static struct data_flash_info *jedec_probe(struct spi_slave *spi) +static struct data_flash_info *jedec_lookup(struct spi_slave *spi, + u64 jedec, bool use_extid) + { - int tmp; - uint8_t id[5]; - uint32_t jedec; - struct data_flash_info *info; - u8 opcode = CMD_READ_ID; + struct data_flash_info *info; int status; - /* - * JEDEC also defines an optional "extended device information" - * string for after vendor-specific data, after the three bytes - * we use here. Supporting some chips might require using it. - * - * If the vendor ID isn't Atmel's (0x1f), assume this call failed. - * That's not an error; only rev C and newer chips handle it, and - * only Atmel sells these chips. - */ - tmp = spi_write_then_read(spi, &opcode, 1, NULL, id, sizeof(id)); - if (tmp < 0) { - printf("dataflash: error %d reading JEDEC ID\n", tmp); - return ERR_PTR(tmp); - } - if (id[0] != 0x1f) - return NULL; - - jedec = id[0]; - jedec = jedec << 8; - jedec |= id[1]; - jedec = jedec << 8; - jedec |= id[2]; + for (info = dataflash_data; + info < dataflash_data + ARRAY_SIZE(dataflash_data); + info++) { + if (use_extid && !(info->flags & SUP_EXTID)) + continue; - for (tmp = 0, info = dataflash_data; - tmp < ARRAY_SIZE(dataflash_data); - tmp++, info++) { if (info->jedec_id == jedec) { if (info->flags & SUP_POW2PS) { status = dataflash_status(spi); if (status < 0) { debug("dataflash: status error %d\n", status); - return NULL; + return ERR_PTR(status); } if (status & 0x1) { if (info->flags & IS_POW2PS) @@ -564,12 +546,58 @@ static struct data_flash_info *jedec_probe(struct spi_slave *spi) } } + return ERR_PTR(-ENODEV); +} + +static struct data_flash_info *jedec_probe(struct spi_slave *spi) +{ + int tmp; + uint64_t jedec; + uint8_t id[sizeof(jedec)] = {0}; + const unsigned int id_size = 5; + struct data_flash_info *info; + u8 opcode = CMD_READ_ID; + + /* + * JEDEC also defines an optional "extended device information" + * string for after vendor-specific data, after the three bytes + * we use here. Supporting some chips might require using it. + * + * If the vendor ID isn't Atmel's (0x1f), assume this call failed. + * That's not an error; only rev C and newer chips handle it, and + * only Atmel sells these chips. + */ + tmp = spi_write_then_read(spi, &opcode, 1, NULL, id, id_size); + if (tmp < 0) { + printf("dataflash: error %d reading JEDEC ID\n", tmp); + return ERR_PTR(tmp); + } + + if (id[0] != 0x1f) + return NULL; + + jedec = be64_to_cpup((__be64 *)id); + + /* + * First, try to match device using extended device + * information + */ + info = jedec_lookup(spi, jedec >> DATAFLASH_SHIFT_EXTID, true); + if (!IS_ERR(info)) + return info; + /* + * If that fails, make another pass using regular ID + * information + */ + info = jedec_lookup(spi, jedec >> DATAFLASH_SHIFT_ID, false); + if (!IS_ERR(info)) + return info; /* * Treat other chips as errors ... we won't know the right page * size (it might be binary) even when we can tell which density * class is involved (legacy chip id scheme). */ - printf("dataflash: JEDEC id %06x not handled\n", jedec); + printf("dataflash: JEDEC id 0x%016llx not handled\n", jedec); return ERR_PTR(-ENODEV); } @@ -618,6 +646,8 @@ static int spi_dataflash_probe(struct udevice *dev) (info->flags & SUP_POW2PS) ? 'd' : 'c'); if (status < 0) goto err_status; + else + return status; } /* -- 2.17.1