From patchwork Thu May 5 17:11:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1627064 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.a=rsa-sha256 header.s=selector1 header.b=GoYCOGV4; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KvL062zVdz9sCq for ; Fri, 6 May 2022 03:14:14 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AA77184104; Thu, 5 May 2022 19:12:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.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=seco.com header.i=@seco.com header.b="GoYCOGV4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1E9A9840FD; Thu, 5 May 2022 19:12:30 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_FILL_THIS_FORM_SHORT,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from EUR02-VE1-obe.outbound.protection.outlook.com (mail-ve1eur02on060f.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe06::60f]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 2C7D283FE0 for ; Thu, 5 May 2022 19:12:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sean.anderson@seco.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XOEOU5F7MNGB4yiCW1Swi51jeybapm14OZtTg0JaxvE+ciLQOW4l94Q6mmVoh2Q1n/t1amIA1OpOJ0trKGSXSofillL00OjjKVDB9AYZvMPgZMK6PlNpdSONmomtr2DH9iW+iiYR2CuBtCzxNs4xEuzrVWaXnudthnfgwOQATmV6pscKG1bbgQspBd5YVsxdAPbW4bdn6WbheiLm2nsmK/jjkI36BZk33KMak1G7ps6pOtJm/4E+pkIyjZoJKmRUPRrOE2VB+xY0aN+TPqmAkmh81W2MBM9PTCIqjkK3bp8dfV5FvQeOXcpopWfF7ZlsCP1j1n2HaDZYSzUfJKxkBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7elP8O8YSAE+Mh4Gy3vMJliOxfpJwrk65icRoomZf0w=; b=ZB0anzMLE/UVj6r6jdy1+kp0Ht2he5uFkq25HWfbc7oj0DPdmeD5rHzOKTGDdurv7qHTgN7FJDzOXA5+SYxTGdC+UTTk1eMKAo0+vhLnRjR2qUTtORwNOhRcdQXY6Xq+T1OUfbE6EePugsblsZRlQtJPECdlS/KMMOlW8rhOs6HOQ9MP4C1fr60GVTUWIDy9GUClhgInZDnsu3yhRBUH6z5C1kHBGLCjsLNwco5FkbxZnwAmF0+c6jVdeXflmzvcNHoPqjYxplSge91RmtD0XrosCQV4pObOEwgaIBrR/+T2GtU1Cq890n0aQi6fUst68bdHeR96oEfAEsy5uAjZmw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=seco.com; dmarc=pass action=none header.from=seco.com; dkim=pass header.d=seco.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seco.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7elP8O8YSAE+Mh4Gy3vMJliOxfpJwrk65icRoomZf0w=; b=GoYCOGV4+j8JO/eGtffmv2hx8hgPdYv1AkqFJ8HStpdz4RdYnZ7y5hfFmymS8JHRWB4EIjsvqS5ZL00a/cgJbzggBu18aDzWH2fqOjILQDad3yXmIcy0oa4M3DYiCXMAAL2ZDUV2YRcmGXGvrzMpj24m8tiL0Hk362/h7wd0kTSoRRu5qm4+xLr9AlsO2cT/usLjUdSG+F7ikTt5qg3KczXT8Of2/ez3T8Zg/5qOoSjSyRXwFqBQukvbhqb+h+G56dYKr7SLRC3P6M4sFoFAN3zCdOXtdPsZlX94yFMxbNzjDGlguA9XDP3CP3GrOyBWa4AVU41n5EiBtQsrlkA1Qw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=seco.com; Received: from VI1PR03MB4973.eurprd03.prod.outlook.com (2603:10a6:803:c5::12) by VI1PR03MB4397.eurprd03.prod.outlook.com (2603:10a6:803:51::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.27; Thu, 5 May 2022 17:12:24 +0000 Received: from VI1PR03MB4973.eurprd03.prod.outlook.com ([fe80::dc24:d72c:4fe6:847e]) by VI1PR03MB4973.eurprd03.prod.outlook.com ([fe80::dc24:d72c:4fe6:847e%7]) with mapi id 15.20.5206.027; Thu, 5 May 2022 17:12:24 +0000 From: Sean Anderson To: u-boot@lists.denx.de, Simon Glass Cc: Heinrich Schuchardt , Mario Six , Ramon Fried , Tom Rini , Joe Hershberger , Sean Anderson Subject: [PATCH v4 10/16] misc: Add support for nvmem cells Date: Thu, 5 May 2022 13:11:39 -0400 Message-Id: <20220505171146.557773-11-sean.anderson@seco.com> X-Mailer: git-send-email 2.35.1.1320.gc452695387.dirty In-Reply-To: <20220505171146.557773-1-sean.anderson@seco.com> References: <20220505171146.557773-1-sean.anderson@seco.com> X-ClientProxiedBy: MN2PR05CA0044.namprd05.prod.outlook.com (2603:10b6:208:236::13) To VI1PR03MB4973.eurprd03.prod.outlook.com (2603:10a6:803:c5::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1810b005-0772-46c8-6f5d-08da2eba6c8d X-MS-TrafficTypeDiagnostic: VI1PR03MB4397:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FH9m6BCSW/2yC2fCLJ+RpekOYLZqFyWRsJ04c/Qv3BOIzeo33VmTvGb21qSFRfQXaxgqw0Dv2w2o/6VxL8zSvCgp/21AMucIRWrRSGP0xVxd4ZddZMaEwuNR7GFNJs2UuImEzJ3eTo5euHLsfGQlhbKXmYn0EKuxT5dzguR1Ip/XK4zk2WixOmJ0UtId4L+JljjmW6VWAfe46pI4bSvrSXlsF9fphGvGy8tCuvd4Q8YwOK3y9ikOjdKmi/fSwDZFwODKTi/7rGAH/qAST96CM4WQzGUNokd+g/b5IYCYGCG1+fJCcVxjS2SmgI9Hu/VQFlbGbSy6ob97bm8CupgbvN/YPDglf6zjsEEDdc2GHoPiNqpDMBLzaMfYxbXJk9oVFGimkgF5dCE9g8CGVpQpe7hMc6ecQsE2FNdysdCkFwN74J1jPgRCnJwaSu3HR8ROkms3+NbV47ou/R5Ig6Aa/3jdLMlOCl/U0nKsSOLvT0bWp76Z/s03Rqqo5opf6ZKkT7kcJiKbyU9l9eMJ3hw6XehinCYn6i9SI9Dq8wGkwA/eYJ9JZnukx6Zc3ayVUS6M+skHZ3xvlcrCwwq/S1NbE+aAl7X5EA9v1xXvELOLvfg2fVFvBcjGf0nDRyoLQh9MjUEWZpKa6lLQy5Egf76V50CUxO01I9KCv7K6ID5cPx1qt9FMEHEIF45fVWtZ0xn0AKZ8HOBMNByow48XrC4t5A== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1PR03MB4973.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(8936002)(4326008)(66476007)(86362001)(66946007)(66556008)(8676002)(44832011)(30864003)(2906002)(5660300002)(38350700002)(38100700002)(52116002)(54906003)(6916009)(508600001)(316002)(186003)(26005)(2616005)(6506007)(6512007)(6666004)(6486002)(1076003)(107886003)(83380400001)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tmNneg0CjVlpgok4mVkgDf8aS8MAiT+Pf2gGXWA5KT9H8Dqrb7GcHoaen6gZej4Rk5m0bm5xC/8/aSeLFoZJUFZgqv8RqxOMZMDqwnxn0FQi1gbeUCYGjv69J3Qbwc9GQj7r2K7j+RRenivsFsv7dqseHsEG8yNP1ZxOPacPJ2OAtfWVc0nGLei03PwcCmIwR6HUBRHsubb3JJoS6kxOgkY5vJjyzSY8r7RLrBkQHBQOMaP5DtGYGHQzHyX+DgwKp7ETT6/GT7/aDkuQVg7B0ewLaQ9H19PoSKsolndolUenNvn8wSbIeO3vQDGd7zhP9qcfIZXjdKHOZnmX4Gd7F+XpKEAovY29OzAUTWLIO9KLAk/dxPWXXOaneOTxHv0oXddAYJnFrdwn8o9s/Fy7AjhrqumJFN+yZNFxiyTFyXANEHZJYLIDvergVjjkmwrCVO5OSOFnbZS84U54k6Xyj4R8ave9uM3lzEA36b1kx57XPLqrAC8G6RxTq1NRfKdDD+OMkL06hSHsBES4ckWZu7qpBlc1mcYgitu67eUzVNr1p8uX7cpfzZifdVLV6AaZ06NFcgAHMBIu4TiwKADm4JNZlxQ5baJCaZcMA0n3DID0qMjf5UkVO/Dl7DTKicUl7dziAC/Gvnkk8U5yCdzWJytssfcNJkVrfd7WInTDwADxRQ8DawiLnl7EeQmpBTkBCskMoQg56PyoUVHXroWb1BJXWzMS82rN8bcayd+6+E+icvXs0wYbeoYu3ektXzdfEbBnqgdCjG9V9SCuOaSBKZ0fKETOHjdT7z2Qg0K3vfrVVia4JMT6YgLFXVHtZ+rb4dLrW3auSHxBFK9pSro4RNvTyNjWbCEjZ7YVSeHVvP8JcTf1JRXB9+PHuvEU8zYARRawKW70rni7XlEeIMEl5BZ3GahtOKbu6ZQD39OtlNa6X6uF5iq7hEXfgJOpTBNjwC84w0vn+LWDWaHSZwRPh+I5zixOVF7pfq8rWp9+ecAlu108PjcdrA3ax7W/UiaNSVO5iCAABiaINDZatu7nPu8hZ1iyQMk3vHj/Ne8rImHUBJQYkKaJLSjFDjcqWxDMp53tNV3TTqAjJdOJ1nnU4+BkVrb6MwOTEd5W8epc53LxeOk/BpRaOsQaM9YE1Xd4b9XconQcpkeNbwXedFnXKDKzLIm7dwsm2JU/avhdLXgPmrryoelYnw5Rl5Hz5sr/65Ja2bixVdgAKlv24GZShECEBVn/OlPqxBeuX98u29UyH61j9IeCt7A8/0onOl+fRFJcYziz2VTCHdfkw3x3cT9SArWI2sjZ56GD4IXVRgFflUWp3BQlKv20YavdwcNlOl5M1tOj1hH8W1STwr/XzjjULKom4olsT4W2pfr5v92lFTBdG9Q3bhYEGd9xSN0c/Fp7g1eg7Zq03UTSwUyimiTunC2PaZ4oLhjn237ozXoYrmIhWQB9gFXKltPXCJm8YXxgwDjJGSjZMPvzsepNVcHAGU5T6CVgBcuhS2WMLDaqHDbp6NvLsSmmhDBOMD9bAjBfv3CCGzcBpnk7Y/m2yqkw52Sy3s0PNmnYqw1kgKjQB51/4XI/T3I3R1JF8+GXgz2a3QQl79cokEzBETqVGvdtAUqa8rqTSB+y7dkIrjOmKYDR5Wt4X1wRO87q4hB/qj4ln9j7vYVG58IqtatMSevZ5Z3HsUO0C7F6fVFVwCpAwVv2j35a5YmWeCR0mCU657lOZ9/B5TNyMFB6+76ERkeGqhdjK7KKIqy2l0CPZPs= X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1810b005-0772-46c8-6f5d-08da2eba6c8d X-MS-Exchange-CrossTenant-AuthSource: VI1PR03MB4973.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2022 17:12:24.9318 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: oHs/7oOiZkKIH8rBjt7sP5xtysxrYxgiM+O7mL6bP5bWQIPQOMQGoQDshO2eF+z9W1OMZwJShsUMuV6TU0o5wg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR03MB4397 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 This adds support for "nvmem cells" as seen in Linux. The nvmem device class in Linux is used for various assorted ROMs and EEPROMs. In this sense, it is similar to UCLASS_MISC, but also includes UCLASS_I2C_EEPROM, UCLASS_RTC, and UCLASS_MTD. New drivers corresponding to a Linux-style nvmem device should be implemented as one of the previously-mentioned uclasses. The nvmem API acts as a compatibility layer to adapt the (slightly different) APIs of these uclasses. It also handles the lookup of nvmem cells. While nvmem devices can be accessed directly, they are most often used by reading/writing contiguous values called "cells". Cells typically hold information like calibration, versions, or configuration (such as mac addresses). nvmem devices can specify "cells" in their device tree: qfprom: eeprom@700000 { #address-cells = <1>; #size-cells = <1>; reg = <0x00700000 0x100000>; /* ... */ tsens_calibration: calib@404 { reg = <0x404 0x10>; }; }; which can then be referenced like: tsens { /* ... */ nvmem-cells = <&tsens_calibration>; nvmem-cell-names = "calibration"; }; The tsens driver could then read the calibration value like: struct nvmem_cell cal_cell; u8 cal[16]; nvmem_cell_get_by_name(dev, "calibration", &cal_cell); nvmem_cell_read(&cal_cell, cal, sizeof(cal)); Because nvmem devices are not all of the same uclass, supported uclasses must register a nvmem_interface struct. This allows CONFIG_NVMEM to be enabled without depending on specific uclasses. At the moment, nvmem_interface is very bare-bones, and assumes that no initialization is necessary. However, this could be amended in the future. Although I2C_EEPROM and MISC are quite similar (and could likely be unified), they present different read/write function signatures. To abstract over this, NVMEM uses the same read/write signature as Linux. In particular, short read/writes are not allowed, which is allowed by MISC. The functionality implemented by nvmem cells is very similar to that provided by i2c_eeprom_partition. "fixed-partition"s for eeproms does not seem to have made its way into Linux or into any device tree other than sandbox. It is possible that with the introduction of this API it would be possible to remove it. Signed-off-by: Sean Anderson --- Changes in v4: - Fix failing to find nvmem devices if some supported uclasses were not enabled. - Remove typedefs from documentation - Document some of the design background, and why a new uclass was not created. Changes in v2: - Call the appropriate API functions directly from nvmem_cell_(read|write). This means we can drop the nvmem_interface machinery. MAINTAINERS | 7 +++ doc/api/index.rst | 1 + doc/api/nvmem.rst | 10 +++ drivers/misc/Kconfig | 16 +++++ drivers/misc/Makefile | 1 + drivers/misc/nvmem.c | 142 ++++++++++++++++++++++++++++++++++++++++++ include/nvmem.h | 134 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 311 insertions(+) create mode 100644 doc/api/nvmem.rst create mode 100644 drivers/misc/nvmem.c create mode 100644 include/nvmem.h diff --git a/MAINTAINERS b/MAINTAINERS index 56be0bfad0..01f4581d6a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1080,6 +1080,13 @@ F: cmd/nvme.c F: include/nvme.h F: doc/develop/driver-model/nvme.rst +NVMEM +M: Sean Anderson +S: Maintained +F: doc/api/nvmem.rst +F: drivers/misc/nvmem.c +F: include/nvmem.h + NXP C45 TJA11XX PHY DRIVER M: Radu Pirea S: Maintained diff --git a/doc/api/index.rst b/doc/api/index.rst index 72fea981b7..a9338cfef9 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -14,6 +14,7 @@ U-Boot API documentation linker_lists lmb logging + nvmem pinctrl rng sandbox diff --git a/doc/api/nvmem.rst b/doc/api/nvmem.rst new file mode 100644 index 0000000000..d923784652 --- /dev/null +++ b/doc/api/nvmem.rst @@ -0,0 +1,10 @@ +.. SPDX-License-Identifier: GPL-2.0+ + +NVMEM API +========= + +.. kernel-doc:: include/nvmem.h + :doc: Design + +.. kernel-doc:: include/nvmem.h + :internal: diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index dab7a99c68..eaf7bb63ba 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -31,6 +31,22 @@ config TPL_MISC set of generic read, write and ioctl methods may be used to access the device. +config NVMEM + bool "NVMEM support" + help + This adds support for a common interface to different types of + non-volatile memory. Consumers can use nvmem-cells properties to look + up hardware configuration data such as MAC addresses and calibration + settings. + +config SPL_NVMEM + bool "NVMEM support in SPL" + help + This adds support for a common interface to different types of + non-volatile memory. Consumers can use nvmem-cells properties to look + up hardware configuration data such as MAC addresses and calibration + settings. + config ALTERA_SYSID bool "Altera Sysid support" depends on MISC diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 6150d01e88..03fad7a23f 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -4,6 +4,7 @@ # Wolfgang Denk, DENX Software Engineering, wd@denx.de. obj-$(CONFIG_MISC) += misc-uclass.o +obj-$(CONFIG_$(SPL_TPL_)NVMEM) += nvmem.o obj-$(CONFIG_$(SPL_TPL_)CROS_EC) += cros_ec.o obj-$(CONFIG_$(SPL_TPL_)CROS_EC_SANDBOX) += cros_ec_sandbox.o diff --git a/drivers/misc/nvmem.c b/drivers/misc/nvmem.c new file mode 100644 index 0000000000..5a2bd1f9f7 --- /dev/null +++ b/drivers/misc/nvmem.c @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2022 Sean Anderson + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int nvmem_cell_read(struct nvmem_cell *cell, void *buf, size_t size) +{ + dev_dbg(cell->nvmem, "%s: off=%u size=%zu\n", __func__, cell->offset, size); + if (size != cell->size) + return -EINVAL; + + switch (cell->nvmem->driver->id) { + case UCLASS_I2C_EEPROM: + return i2c_eeprom_read(cell->nvmem, cell->offset, buf, size); + case UCLASS_MISC: { + int ret = misc_read(cell->nvmem, cell->offset, buf, size); + + if (ret < 0) + return ret; + if (ret != size) + return -EIO; + return 0; + } + case UCLASS_RTC: + return dm_rtc_read(cell->nvmem, cell->offset, buf, size); + default: + return -ENOSYS; + } +} + +int nvmem_cell_write(struct nvmem_cell *cell, const void *buf, size_t size) +{ + dev_dbg(cell->nvmem, "%s: off=%u size=%zu\n", __func__, cell->offset, size); + if (size != cell->size) + return -EINVAL; + + switch (cell->nvmem->driver->id) { + case UCLASS_I2C_EEPROM: + return i2c_eeprom_write(cell->nvmem, cell->offset, buf, size); + case UCLASS_MISC: { + int ret = misc_write(cell->nvmem, cell->offset, buf, size); + + if (ret < 0) + return ret; + if (ret != size) + return -EIO; + return 0; + } + case UCLASS_RTC: + return dm_rtc_write(cell->nvmem, cell->offset, buf, size); + default: + return -ENOSYS; + } +} + +/** + * nvmem_get_device() - Get an nvmem device for a cell + * @node: ofnode of the nvmem device + * @cell: Cell to look up + * + * Try to find a nvmem-compatible device by going through the nvmem interfaces. + * + * Return: + * * 0 on success + * * -ENODEV if we didn't find anything + * * A negative error if there was a problem looking up the device + */ +static int nvmem_get_device(ofnode node, struct nvmem_cell *cell) +{ + int i, ret; + enum uclass_id ids[] = { + UCLASS_I2C_EEPROM, + UCLASS_MISC, + UCLASS_RTC, + }; + + for (i = 0; i < ARRAY_SIZE(ids); i++) { + ret = uclass_get_device_by_ofnode(ids[i], node, &cell->nvmem); + if (!ret) + return 0; + if (ret != -ENODEV && ret != -EPFNOSUPPORT) + return ret; + } + + return -ENODEV; +} + +int nvmem_cell_get_by_index(struct udevice *dev, int index, + struct nvmem_cell *cell) +{ + fdt_addr_t offset; + fdt_size_t size = FDT_SIZE_T_NONE; + int ret; + struct ofnode_phandle_args args; + + dev_dbg(dev, "%s: index=%d\n", __func__, index); + + ret = dev_read_phandle_with_args(dev, "nvmem-cells", NULL, 0, index, + &args); + if (ret) + return ret; + + ret = nvmem_get_device(ofnode_get_parent(args.node), cell); + if (ret) + return ret; + + offset = ofnode_get_addr_size_index_notrans(args.node, 0, &size); + if (offset == FDT_ADDR_T_NONE || size == FDT_SIZE_T_NONE) { + dev_dbg(cell->nvmem, "missing address or size for %s\n", + ofnode_get_name(args.node)); + return -EINVAL; + } + + cell->offset = offset; + cell->size = size; + return 0; +} + +int nvmem_cell_get_by_name(struct udevice *dev, const char *name, + struct nvmem_cell *cell) +{ + int index; + + dev_dbg(dev, "%s, name=%s\n", __func__, name); + + index = dev_read_stringlist_search(dev, "nvmem-cell-names", name); + if (index < 0) + return index; + + return nvmem_cell_get_by_index(dev, index, cell); +} diff --git a/include/nvmem.h b/include/nvmem.h new file mode 100644 index 0000000000..822e698bdd --- /dev/null +++ b/include/nvmem.h @@ -0,0 +1,134 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (c) 2022 Sean Anderson + */ + +#ifndef NVMEM_H +#define NVMEM_H + +/** + * DOC: Design + * + * The NVMEM subsystem is a "meta-uclass" in that it abstracts over several + * different uclasses all with read/write APIs. One approach to implementing + * this could be to add a new sub-device for each nvmem-style device of + * UCLASS_NVMEM. This subsystem has taken the approach of using the existing + * access methods (i2c_eeprom_write, misc_write, etc.) directly. This has the + * advantage of not requiring an extra device/driver, saving on binary size and + * runtime memory usage. On the other hand, it is not idiomatic. Similar + * efforts should generally use a new uclass. + */ + +/** + * struct nvmem_cell - One datum within non-volatile memory + * @nvmem: The backing storage device + * @offset: The offset of the cell from the start of @nvmem + * @size: The size of the cell, in bytes + */ +struct nvmem_cell { + struct udevice *nvmem; + unsigned int offset; + size_t size; +}; + +struct udevice; + +#if CONFIG_IS_ENABLED(NVMEM) + +/** + * nvmem_cell_read() - Read the value of an nvmem cell + * @cell: The nvmem cell to read + * @buf: The buffer to read into + * @size: The size of @buf + * + * Return: + * * 0 on success + * * -EINVAL if @buf is not the same size as @cell. + * * -ENOSYS if CONFIG_NVMEM is disabled + * * A negative error if there was a problem reading the underlying storage + */ +int nvmem_cell_read(struct nvmem_cell *cell, void *buf, size_t size); + +/** + * nvmem_cell_write() - Write a value to an nvmem cell + * @cell: The nvmem cell to write + * @buf: The buffer to write from + * @size: The size of @buf + * + * Return: + * * 0 on success + * * -EINVAL if @buf is not the same size as @cell + * * -ENOSYS if @cell is read-only, or if CONFIG_NVMEM is disabled + * * A negative error if there was a problem writing the underlying storage + */ +int nvmem_cell_write(struct nvmem_cell *cell, const void *buf, size_t size); + +/** + * nvmem_cell_get_by_index() - Get an nvmem cell from a given device and index + * @dev: The device that uses the nvmem cell + * @index: The index of the cell in nvmem-cells + * @cell: The cell to initialize + * + * Look up the nvmem cell referenced by the phandle at @index in nvmem-cells in + * @dev. + * + * Return: + * * 0 on success + * * -EINVAL if the regs property is missing, empty, or undersized + * * -ENODEV if the nvmem device is missing or unimplemented + * * -ENOSYS if CONFIG_NVMEM is disabled + * * A negative error if there was a problem reading nvmem-cells or getting the + * device + */ +int nvmem_cell_get_by_index(struct udevice *dev, int index, + struct nvmem_cell *cell); + +/** + * nvmem_cell_get_by_name() - Get an nvmem cell from a given device and name + * @dev: The device that uses the nvmem cell + * @name: The name of the nvmem cell + * @cell: The cell to initialize + * + * Look up the nvmem cell referenced by @name in the nvmem-cell-names property + * of @dev. + * + * Return: + * * 0 on success + * * -EINVAL if the regs property is missing, empty, or undersized + * * -ENODEV if the nvmem device is missing or unimplemented + * * -ENODATA if @name is not in nvmem-cell-names + * * -ENOSYS if CONFIG_NVMEM is disabled + * * A negative error if there was a problem reading nvmem-cell-names, + * nvmem-cells, or getting the device + */ +int nvmem_cell_get_by_name(struct udevice *dev, const char *name, + struct nvmem_cell *cell); + +#else /* CONFIG_NVMEM */ + +static inline int nvmem_cell_read(struct nvmem_cell *cell, void *buf, int size) +{ + return -ENOSYS; +} + +static inline int nvmem_cell_write(struct nvmem_cell *cell, const void *buf, + int size) +{ + return -ENOSYS; +} + +static inline int nvmem_cell_get_by_index(struct udevice *dev, int index, + struct nvmem_cell *cell) +{ + return -ENOSYS; +} + +static inline int nvmem_cell_get_by_name(struct udevice *dev, const char *name, + struct nvmem_cell *cell) +{ + return -ENOSYS; +} + +#endif /* CONFIG_NVMEM */ + +#endif /* NVMEM_H */