From patchwork Tue Oct 31 07:38:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1857379 X-Patchwork-Delegate: hs@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=NwFEKtJs; dkim=pass (2048-bit key) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=idf8ibf0; dkim-atps=neutral Authentication-Results: legolas.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=patchwork.ozlabs.org) 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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SKMWt1p51z1yQ5 for ; Tue, 31 Oct 2023 18:41:14 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D691E87AB4; Tue, 31 Oct 2023 08:41:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738071; bh=pAxu0zx0Y765aKBkTU3DoS17fT+mLQEj8gZH5vi1IWs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=NwFEKtJsGVAeCetY8LMCHkJq1QhGH9SjpWiioMbzbRCcrjsMtW6bCQ1jVg6/U6ZH9 f0Vw4vNUNZJP0yaMOSiMO4SnYdlVqkXBfMBtksPJmun47tfLFL1J4sCnuCQwx4IIJQ vM/eeBXMI5xEH21PAuIky2Vaqiyn1Fv6I7cyLXWEULDFvh3OaI9oLshPm2QV2B2JC5 V1msk1wC/Fx9b4FCR5qmKDzsiMCDyDQCkyvq6/JjDqXyABwsgGdXk9B2Lv/I7maVIM ruTEkALAVmuqUkzl0LjM9ADPntEkWyXvimPcc/4aJSRu8MJHOBNMmi3bG9915BOPLg /Zl/zIBf2BjBQ== Received: from localhost (unknown [IPv6:2a01:cb18:989:a700:4b67:3ae7:15fa:40a0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: pro@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 10BB087256; Tue, 31 Oct 2023 08:41:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738070; bh=pAxu0zx0Y765aKBkTU3DoS17fT+mLQEj8gZH5vi1IWs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=idf8ibf0Sg73X4zg1JmJNjvgsm2kSN0ptU0321CbvRtIVrPV0KA4fz2Ks45XKuWTt 7MNu4s5N+OMw5cdpQKaLxQogBinXd9oR/OwtmUE9jWOLN1WEFkqaWCf2s3UhEfeP6+ RsFgQOCk4uHYznYC/2qk1an2axy3uH8BQYYU926RCmG2xKtCeF4NbMyg293xwVrcFz 7EatiW04rSpTYl89xLzKz5+4Qor6fqcIYVxQWw7/yZeS0LNUDjBnlWt14OmmDnWwiy rtAxsbrkPYvnctciAmbLSndT1Rm66OmakmyqHuqa4M9JxcPtOtzGgXzF2Q66UF/+Dc MOje952zamVDw== From: Philip Richard Oberfichtner To: u-boot@lists.denx.de Cc: hs@denx.de, sjg@chromium.org, trini@konsulko.com, Philip Richard Oberfichtner Subject: [PATCH v3 1/3] bootcount: Remove legacy I2C driver Date: Tue, 31 Oct 2023 08:38:44 +0100 Message-ID: <20231031073844.15683-4-pro@denx.de> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231031073844.15683-2-pro@denx.de> References: <20231031073844.15683-2-pro@denx.de> MIME-Version: 1.0 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.8 at phobos.denx.de X-Virus-Status: Clean The legacy I2C bootcounter will hereby be removed and eventually be replaced by a driver model implementation in the follow-up commit. The legacy driver has the following drawbacks: - It's not adhering to the driver model - Settings are grabbed from Kconfig rather than device tree - i2c_{read,write} are being used instead of dm_i2c_{read,write} Signed-off-by: Philip Richard Oberfichtner Reviewed-by: Heiko Schocher --- Notes: Changes in v3: none Changes in v2: none drivers/bootcount/Kconfig | 24 +++-------------- drivers/bootcount/Makefile | 1 - drivers/bootcount/bootcount_i2c.c | 43 ------------------------------- 3 files changed, 3 insertions(+), 65 deletions(-) delete mode 100644 drivers/bootcount/bootcount_i2c.c diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig index 570252d186..7a2548ace2 100644 --- a/drivers/bootcount/Kconfig +++ b/drivers/bootcount/Kconfig @@ -79,14 +79,6 @@ config BOOTCOUNT_RAM Store the bootcount in DRAM protected against bit errors due to short power loss or holding a system in RESET. -config BOOTCOUNT_I2C - bool "Boot counter on I2C device" - help - Enable support for the bootcounter on an i2c (like RTC) device. - CFG_SYS_I2C_RTC_ADDR = i2c chip address - CONFIG_SYS_BOOTCOUNT_ADDR = i2c addr which is used for - the bootcounter. - config BOOTCOUNT_AT91 bool "Boot counter for Atmel AT91SAM9XE" depends on AT91SAM9XE @@ -175,14 +167,6 @@ config BOOTCOUNT_BOOTLIMIT counter being cleared. If set to 0, do not set a boot limit in the environment. -config BOOTCOUNT_ALEN - int "I2C address length" - default 1 - depends on BOOTCOUNT_I2C - help - Length of the the I2C address at SYS_BOOTCOUNT_ADDR for storing - the boot counter. - config SYS_BOOTCOUNT_SINGLEWORD bool "Use single word to pack boot count and magic value" depends on BOOTCOUNT_GENERIC @@ -218,7 +202,7 @@ config SYS_BOOTCOUNT_ADDR default 0x44E3E000 if BOOTCOUNT_AM33XX || BOOTCOUNT_AM33XX_NVMEM default 0xE0115FF8 if ARCH_LS1043A || ARCH_LS1021A depends on BOOTCOUNT_AM33XX || BOOTCOUNT_GENERIC || BOOTCOUNT_EXT || \ - BOOTCOUNT_I2C || BOOTCOUNT_AM33XX_NVMEM + BOOTCOUNT_AM33XX_NVMEM help Set the address used for reading and writing the boot counter. @@ -226,13 +210,11 @@ config SYS_BOOTCOUNT_MAGIC hex "Magic value for the boot counter" default 0xB001C041 if BOOTCOUNT_GENERIC || BOOTCOUNT_EXT || \ BOOTCOUNT_AM33XX || BOOTCOUNT_ENV || \ - BOOTCOUNT_RAM || BOOTCOUNT_I2C || \ - BOOTCOUNT_AT91 || DM_BOOTCOUNT + BOOTCOUNT_RAM || BOOTCOUNT_AT91 || DM_BOOTCOUNT default 0xB0 if BOOTCOUNT_AM33XX_NVMEM depends on BOOTCOUNT_GENERIC || BOOTCOUNT_EXT || \ BOOTCOUNT_AM33XX || BOOTCOUNT_ENV || \ - BOOTCOUNT_RAM || BOOTCOUNT_I2C || \ - BOOTCOUNT_AT91 || DM_BOOTCOUNT || \ + BOOTCOUNT_RAM || BOOTCOUNT_AT91 || DM_BOOTCOUNT || \ BOOTCOUNT_AM33XX_NVMEM help Set the magic value used for the boot counter. diff --git a/drivers/bootcount/Makefile b/drivers/bootcount/Makefile index b65959a384..d6d2389c16 100644 --- a/drivers/bootcount/Makefile +++ b/drivers/bootcount/Makefile @@ -6,7 +6,6 @@ obj-$(CONFIG_BOOTCOUNT_AT91) += bootcount_at91.o obj-$(CONFIG_BOOTCOUNT_AM33XX) += bootcount_davinci.o obj-$(CONFIG_BOOTCOUNT_RAM) += bootcount_ram.o obj-$(CONFIG_BOOTCOUNT_ENV) += bootcount_env.o -obj-$(CONFIG_BOOTCOUNT_I2C) += bootcount_i2c.o obj-$(CONFIG_BOOTCOUNT_EXT) += bootcount_ext.o obj-$(CONFIG_BOOTCOUNT_AM33XX_NVMEM) += bootcount_nvmem.o diff --git a/drivers/bootcount/bootcount_i2c.c b/drivers/bootcount/bootcount_i2c.c deleted file mode 100644 index b3ac67ea35..0000000000 --- a/drivers/bootcount/bootcount_i2c.c +++ /dev/null @@ -1,43 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* - * (C) Copyright 2013 - * Heiko Schocher, DENX Software Engineering, hs@denx.de. - */ - -#include -#include -#include - -#define BC_MAGIC 0xbc - -void bootcount_store(ulong a) -{ - unsigned char buf[3]; - int ret; - - buf[0] = BC_MAGIC; - buf[1] = (a & 0xff); - ret = i2c_write(CFG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR, - CONFIG_BOOTCOUNT_ALEN, buf, 2); - if (ret != 0) - puts("Error writing bootcount\n"); -} - -ulong bootcount_load(void) -{ - unsigned char buf[3]; - int ret; - - ret = i2c_read(CFG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR, - CONFIG_BOOTCOUNT_ALEN, buf, 2); - if (ret != 0) { - puts("Error loading bootcount\n"); - return 0; - } - if (buf[0] == BC_MAGIC) - return buf[1]; - - bootcount_store(0); - - return 0; -} From patchwork Tue Oct 31 07:38:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1857380 X-Patchwork-Delegate: hs@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=jaai/eAR; dkim=pass (2048-bit key) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=kdFfC3g7; dkim-atps=neutral Authentication-Results: legolas.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=patchwork.ozlabs.org) 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 ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SKMX53Wm1z1yQ5 for ; Tue, 31 Oct 2023 18:41:25 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 625EC87C66; Tue, 31 Oct 2023 08:41:23 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738083; bh=aMZJhUXVGXnRgek9MX/fSpNLDfeju/VEHlpNYTqH/9I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=jaai/eARKU9EiEkOrcjFiZs5pjz90S/8uxvTCSVnv8w57cBTVMn1nH4tLYD2pUoFx Dn75mQpT5viHyUo/PQmphnES+UWH7b9BHhJV3yAUcNlIOvOeSZtplP39iFtm/nz+E/ OxGq7HGVc91VO2qOe1QF7ozt5grfwMIPerSoL/z8A+w5MJFVs4tfQnZ0DFlk8cFHUJ dNrXy2y2yjpT74Urb50F7+yCF6i+a2WEn9thvYfXNLytrhfG9ISddkWTHQFIW68W17 Bt+pEWZtsUHCP+OteCKywe3r/kP1sk6qFbkghtSkXWoqZzSLRNyks2oPIlxnrq0rWe Q1jki96aOOEIw== Received: from localhost (unknown [IPv6:2a01:cb18:989:a700:4b67:3ae7:15fa:40a0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: pro@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 426CE87C6E; Tue, 31 Oct 2023 08:41:22 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738082; bh=aMZJhUXVGXnRgek9MX/fSpNLDfeju/VEHlpNYTqH/9I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kdFfC3g7SjHUHOQAbyIDuAfJCGl1he57q4xiZ4/tK9au8fBbOEdejsmUK6OMFUTVW y6ookJTsAEff4fRPhw+VRiL6uOwdbdNIs16CdHkdYQy8J2AcIXn+nizcdjaKOBmbdM x1JHj8s9c8Rzmm3IRQcZZGDNSkxZgyTQh36eU3cfJoFk2jk9IFffqnUfzDTJuCm0dH 1AN7muJO0svA+Syk+vNlM9t0imULfiDwAhXGk+u06rDGLJLOhXwWPt6a9zV9v/yxgm 0yE7Xv+x0KhguDf032Y9sSoVMbYb5z3cjmCl+nYGrqEsviibZjyKxmD4U4koWZvpW4 EQd4oTZwE1tFw== From: Philip Richard Oberfichtner To: u-boot@lists.denx.de Cc: hs@denx.de, sjg@chromium.org, trini@konsulko.com, Philip Richard Oberfichtner Subject: [PATCH v3 2/3] i2c: Implement i2c_get_chip_by_phandle() Date: Tue, 31 Oct 2023 08:38:46 +0100 Message-ID: <20231031073844.15683-6-pro@denx.de> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231031073844.15683-2-pro@denx.de> References: <20231031073844.15683-2-pro@denx.de> MIME-Version: 1.0 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.8 at phobos.denx.de X-Virus-Status: Clean This new function enhances the i2c_get_chip*() toolbox by implementing a variant that does not require a chip_addr. Instead, the desired device is pointed to by a phandle. Signed-off-by: Philip Richard Oberfichtner Reviewed-by: Heiko Schocher --- Notes: Changes in v3: none Changes in v2: new drivers/i2c/i2c-uclass.c | 75 ++++++++++++++++++++++++++++++++++++++++ include/i2c.h | 12 +++++++ 2 files changed, 87 insertions(+) diff --git a/drivers/i2c/i2c-uclass.c b/drivers/i2c/i2c-uclass.c index 8867a560bd..5405067861 100644 --- a/drivers/i2c/i2c-uclass.c +++ b/drivers/i2c/i2c-uclass.c @@ -388,6 +388,81 @@ int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, return 0; } +/* Find and probe I2C bus based on a chip attached to it */ +static int i2c_get_parent_bus(ofnode chip, struct udevice **devp) +{ + ofnode node; + struct udevice *dev; + int ret; + + node = ofnode_get_parent(chip); + if (!ofnode_valid(node)) + return -ENODEV; + + ret = uclass_get_device_by_ofnode(UCLASS_I2C, node, &dev); + if (ret) { + *devp = NULL; + return ret; + } + + *devp = dev; + return 0; +} + +int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name, + struct udevice **devp) +{ + ofnode node; + uint phandle; + struct udevice *bus, *chip; + char *dev_name; + int ret; + + debug("%s: Searching I2C chip for phandle \"%s\"\n", + __func__, prop_name); + + dev_name = strdup(prop_name); + if (!dev_name) { + ret = -ENOMEM; + goto err_exit; + } + + ret = dev_read_u32(parent, "i2cbcdev", &phandle); + if (ret) + goto err_exit; + + node = ofnode_get_by_phandle(phandle); + if (!ofnode_valid(node)) { + ret = -ENODEV; + goto err_exit; + } + + ret = i2c_get_parent_bus(node, &bus); + if (ret) + goto err_exit; + + ret = device_bind_driver_to_node(bus, "i2c_generic_chip_drv", + dev_name, node, &chip); + if (ret) + goto err_exit; + + ret = device_probe(chip); + if (ret) { + device_unbind(chip); + goto err_exit; + } + + debug("%s succeeded\n", __func__); + *devp = chip; + return 0; + +err_exit: + free(dev_name); + debug("%s failed, ret = %d\n", __func__, ret); + *devp = NULL; + return ret; +} + int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, struct udevice **devp) { diff --git a/include/i2c.h b/include/i2c.h index ef3820eaba..4e59009cd9 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -537,6 +537,18 @@ int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, struct udevice **devp); +/** + * i2c_get_chip_by_phandle() - get a device to use to access a chip + * based on a phandle property pointing to it + * + * @parent: Parent device containing the phandle pointer + * @name: Name of phandle property in the parent device node + * @devp: Returns pointer to new device or NULL if not found + * Return: 0 on success, -ve on failure + */ +int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name, + struct udevice **devp); + /** * i2c_chip_of_to_plat() - Decode standard I2C platform data * From patchwork Tue Oct 31 07:38:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1857381 X-Patchwork-Delegate: hs@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=PIK6dOcE; dkim=pass (2048-bit key) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=eu8v2YFS; dkim-atps=neutral Authentication-Results: legolas.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=patchwork.ozlabs.org) 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 ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SKMXL2tvNz1yQ5 for ; Tue, 31 Oct 2023 18:41:38 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EA4C7874C4; Tue, 31 Oct 2023 08:41:35 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738096; bh=x1nH8MYZaahmFNf6xOkinK1vT/iM+ChtUCQotdXH6KA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=PIK6dOcE5VV5f2j1m2XCgRXiEXmg8uaEMnuFb+6rUKWt0lceDhXICasZwbO6DdGhe t4E5sZ+Hk20tOc1S0W+cLIUj8Hzep7nD31xw2cCHEpdR13RulAWU+pBuXa9kw1g0nc Mx/K+z9XdE+XWKgl5Zk1gOKPWteVuocD2VHSBF+HE6sx2EsIZI/UXM3JNVjcZVtGmy k1+Vw5w6RR5g1jpkESBI90he/ASgV1Fg7pW8J5TALMjOLtVQCHor8OC2Agf3TQ9WpA 1CVxigxAVVUCd4FCHd6rpllXIqhGhoLR1C8pVtvXb+VLO8SkbvmZQ+Z13ksSvANPPm J4xTDoyjy8MJw== Received: from localhost (unknown [IPv6:2a01:cb18:989:a700:4b67:3ae7:15fa:40a0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: pro@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 419F287256; Tue, 31 Oct 2023 08:41:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1698738094; bh=x1nH8MYZaahmFNf6xOkinK1vT/iM+ChtUCQotdXH6KA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eu8v2YFSvIIkJq5eyqX/cbmLG4OgiMdEiA8zr2qV07bhUTXFEQ2sE998sDGlR97GU 3cZdNgnhCttcI0V97Xq2hgiTlFkdqDrX8bHU9k0T3wOj/fXsqOFTw4priW2VrR6WRj lCD1VZrEghhoHxsmz4WoY7BII8N++omeH7ttE5J7gL/1bgPfJJuZ1MCw5k+vRgblUK Av8wBK11SoMy+rERQ3gZxNxgsXpubuv8HtNQmlX+JhFihCg8ZuZRquO2cs7r51Of9e KC+zVX02/4XFlxatptKchgqYoQ00ZEWjBuAzfp7X4pP9b8G1BgT9NOOwNQyMrn7L+N V/CBvjb9SX6ag== From: Philip Richard Oberfichtner To: u-boot@lists.denx.de Cc: hs@denx.de, sjg@chromium.org, trini@konsulko.com, Philip Richard Oberfichtner Subject: [PATCH v3 3/3] bootcount: Add driver model I2C driver Date: Tue, 31 Oct 2023 08:38:48 +0100 Message-ID: <20231031073844.15683-8-pro@denx.de> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231031073844.15683-2-pro@denx.de> References: <20231031073844.15683-2-pro@denx.de> MIME-Version: 1.0 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.8 at phobos.denx.de X-Virus-Status: Clean This adds a generic I2C bootcounter adhering to driver model to replace the previously removed legacy implementation. There is no change in functionality, it can be used on any I2C device. The device tree configuration may look like this for example: bootcount { compatible = "u-boot,bootcount-i2c"; i2cbcdev = <&i2c_rtc>; offset = <0x11>; }; Signed-off-by: Philip Richard Oberfichtner Reviewed-by: Heiko Schocher --- Notes: Changes in v3: - Remove common.h #include Changes in v2: - Adaption of Kconfig help message - Rename chip to bcdev - Adapt probe to use i2c_get_chip_by_phandle() drivers/bootcount/Kconfig | 10 +++ drivers/bootcount/Makefile | 1 + drivers/bootcount/bootcount_dm_i2c.c | 102 +++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 drivers/bootcount/bootcount_dm_i2c.c diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig index 7a2548ace2..3c56253b1e 100644 --- a/drivers/bootcount/Kconfig +++ b/drivers/bootcount/Kconfig @@ -109,6 +109,16 @@ config DM_BOOTCOUNT_RTC Accesses to the backing store are performed using the write16 and read16 ops of DM RTC devices. +config DM_BOOTCOUNT_I2C + bool "Driver Model boot counter on I2C device" + depends on DM_I2C + help + Enable support for the bootcounter on a generic i2c device, like a RTC + or PMIC. The bootcounter is configured in the device tree using the + "u-boot,bootcount-i2c" compatible string. It requires a phandle + 'i2cbcdev' for the i2c device and an 'offset' property used within the + device. + config DM_BOOTCOUNT_I2C_EEPROM bool "Support i2c eeprom devices as a backing store for bootcount" depends on I2C_EEPROM diff --git a/drivers/bootcount/Makefile b/drivers/bootcount/Makefile index d6d2389c16..e7771f5b36 100644 --- a/drivers/bootcount/Makefile +++ b/drivers/bootcount/Makefile @@ -13,5 +13,6 @@ obj-$(CONFIG_DM_BOOTCOUNT) += bootcount-uclass.o obj-$(CONFIG_DM_BOOTCOUNT_PMIC_PFUZE100) += pmic_pfuze100.o obj-$(CONFIG_DM_BOOTCOUNT_RTC) += rtc.o obj-$(CONFIG_DM_BOOTCOUNT_I2C_EEPROM) += i2c-eeprom.o +obj-$(CONFIG_DM_BOOTCOUNT_I2C) += bootcount_dm_i2c.o obj-$(CONFIG_DM_BOOTCOUNT_SPI_FLASH) += spi-flash.o obj-$(CONFIG_DM_BOOTCOUNT_SYSCON) += bootcount_syscon.o diff --git a/drivers/bootcount/bootcount_dm_i2c.c b/drivers/bootcount/bootcount_dm_i2c.c new file mode 100644 index 0000000000..e27034cbeb --- /dev/null +++ b/drivers/bootcount/bootcount_dm_i2c.c @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2023 + * Philip Richard Oberfichtner + * + * Based on previous work from Heiko Schocher (legacy bootcount_i2c.c driver) + */ + +#include +#include +#include + +#define BC_MAGIC 0x55 + +struct bootcount_i2c_priv { + struct udevice *bcdev; + unsigned int offset; +}; + +static int bootcount_i2c_set(struct udevice *dev, const u32 val) +{ + int ret; + struct bootcount_i2c_priv *priv = dev_get_priv(dev); + + ret = dm_i2c_reg_write(priv->bcdev, priv->offset, BC_MAGIC); + if (ret < 0) + goto err_exit; + + ret = dm_i2c_reg_write(priv->bcdev, priv->offset + 1, val & 0xff); + if (ret < 0) + goto err_exit; + + return 0; + +err_exit: + log_debug("%s: Error writing to I2C device (%d)\n", __func__, ret); + return ret; +} + +static int bootcount_i2c_get(struct udevice *dev, u32 *val) +{ + int ret; + struct bootcount_i2c_priv *priv = dev_get_priv(dev); + + ret = dm_i2c_reg_read(priv->bcdev, priv->offset); + if (ret < 0) + goto err_exit; + + if ((ret & 0xff) != BC_MAGIC) { + log_debug("%s: Invalid Magic, reset bootcounter.\n", __func__); + *val = 0; + return bootcount_i2c_set(dev, 0); + } + + ret = dm_i2c_reg_read(priv->bcdev, priv->offset + 1); + if (ret < 0) + goto err_exit; + + *val = ret; + return 0; + +err_exit: + log_debug("%s: Error reading from I2C device (%d)\n", __func__, ret); + return ret; +} + +static int bootcount_i2c_probe(struct udevice *dev) +{ + struct bootcount_i2c_priv *priv = dev_get_priv(dev); + int ret; + + ret = dev_read_u32(dev, "offset", &priv->offset); + if (ret) + goto exit; + + ret = i2c_get_chip_by_phandle(dev, "i2cbcdev", &priv->bcdev); + +exit: + if (ret) + log_debug("%s failed, ret = %d\n", __func__, ret); + + return ret; +} + +static const struct bootcount_ops bootcount_i2c_ops = { + .get = bootcount_i2c_get, + .set = bootcount_i2c_set, +}; + +static const struct udevice_id bootcount_i2c_ids[] = { + { .compatible = "u-boot,bootcount-i2c" }, + { } +}; + +U_BOOT_DRIVER(bootcount_i2c) = { + .name = "bootcount-i2c", + .id = UCLASS_BOOTCOUNT, + .priv_auto = sizeof(struct bootcount_i2c_priv), + .probe = bootcount_i2c_probe, + .of_match = bootcount_i2c_ids, + .ops = &bootcount_i2c_ops, +};