Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2597533ybc; Mon, 18 Nov 2019 01:29:21 -0800 (PST) X-Google-Smtp-Source: APXvYqw3YE/ZuWLvMz+UMZgiwA/Ehp2VfHlvV8T5+rQkq1sjUWZkioKFj40d132uT4Lhrrttvoz4 X-Received: by 2002:a17:906:1249:: with SMTP id u9mr25742719eja.253.1574069361147; Mon, 18 Nov 2019 01:29:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574069361; cv=none; d=google.com; s=arc-20160816; b=OrBkolJhrzBw8cq2uGqoV3LXLep9fx7PN6sA/x7zAokybU1jgG1Y6eGqlMa4sNJMH9 LQoEOjLGmORClotcCcJYKuO6wfu/nBes3SnoLn8672XUpua4EQxdBwKA+/DXAT2neZDp qiw53HFFIxV7MMEL/2uVF0KlxLDGp+h5lKZ+GxH6KsWQK6Oejh6hVaFNjtxqCoItYn4l UHu3ic1DWS/EJEcvvQLh2jcByvy6SAjg5J9eiSeNtaeK8kgdLl1cgEKqFBQ7ifDY4F87 +3LCx8YiRB9aFRQHbvP+XaRtoahSfhoIqS0Qg78IDhGC2wPOrfCaZSkqwejkeZhaifrd gr+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date; bh=eeRnKAimW+u6eV90gEAnO4XsHmHPzl1pBLzKGRojLa4=; b=V53qsePHbXHsIHuLM6yalPY1Kmog015nSSQ0Q5YvvgqXst58Soia3BbrC0HDlU2atJ BuGPO83qyoXmBQ6JHtsQzrjxCYXKX7GQGIIQDW2YyFUUexjaPcc32SSUdMYcoAArRs59 xWnqkFaKM+oU38m7N7XNGX7Y4ZVEs+/EhbVrLx07ZEa0rI3oHNlAkM0I85Xsa0ivFC8K mAxnZ9eO4UXkBDeYzzfZzX5iHYwa5OQ4kUtiM/2ivHCbWlxbR293cMAC/3iplzlsECC7 piUCTIW8iXSljPiP57SzO3gGHNN8JDVCl7rop5AQFZZwnVBJ9cZeMwZF1tdmZWcloIxs 7URQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f6si11602474ejx.0.2019.11.18.01.28.56; Mon, 18 Nov 2019 01:29:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726894AbfKRJ1p (ORCPT + 99 others); Mon, 18 Nov 2019 04:27:45 -0500 Received: from mx2.suse.de ([195.135.220.15]:57576 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726460AbfKRJ1p (ORCPT ); Mon, 18 Nov 2019 04:27:45 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id EE08FAC4D; Mon, 18 Nov 2019 09:27:42 +0000 (UTC) Date: Mon, 18 Nov 2019 10:27:41 +0100 From: Jean Delvare To: Linux I2C , LKML Cc: Wolfram Sang Subject: [PATCH 3/4 v2] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically Message-ID: <20191118102741.794187d4@endymion> In-Reply-To: <20191118102410.78cd8e6e@endymion> References: <20191118102410.78cd8e6e@endymion> Organization: SUSE Linux X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-suse-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In simple cases we can instantiate SPD EEPROMs on the SMBus automatically. Start with just DDR2, DDR3 and DDR4 on x86 for now, and only for systems with no more than 4 memory slots. These limitations may be lifted later. Signed-off-by: Jean Delvare --- Changes since v1: * Fixed build warning reported by kbuild test bot drivers/i2c/i2c-smbus.c | 104 +++++++++++++++++++++++++++++++++++++++++++++- include/linux/i2c-smbus.h | 8 +++ 2 files changed, 110 insertions(+), 2 deletions(-) --- linux-5.3.orig/drivers/i2c/i2c-smbus.c 2019-10-14 11:33:20.372781769 +0200 +++ linux-5.3/drivers/i2c/i2c-smbus.c 2019-10-14 16:42:12.815302881 +0200 @@ -3,10 +3,11 @@ * i2c-smbus.c - SMBus extensions to the I2C protocol * * Copyright (C) 2008 David Brownell - * Copyright (C) 2010 Jean Delvare + * Copyright (C) 2010-2019 Jean Delvare */ #include +#include #include #include #include @@ -203,6 +204,107 @@ EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert module_i2c_driver(smbalert_driver); +/* + * SPD is not part of SMBus but we include it here for convenience as the + * target systems are the same. + * Restrictions to automatic SPD instantiation: + * - Only works if all filled slots have the same memory type + * - Only works for DDR2, DDR3 and DDR4 for now + * - Only works on systems with 1 to 4 memory slots + */ +#if IS_ENABLED(CONFIG_DMI) +void i2c_register_spd(struct i2c_adapter *adap) +{ + int n, slot_count = 0, dimm_count = 0; + u16 handle; + u8 common_mem_type = 0x0, mem_type; + u64 mem_size; + const char *name; + + while ((handle = dmi_memdev_handle(slot_count)) != 0xffff) { + slot_count++; + + /* Skip empty slots */ + mem_size = dmi_memdev_size(handle); + if (!mem_size) + continue; + + /* Skip undefined memory type */ + mem_type = dmi_memdev_type(handle); + if (mem_type <= 0x02) /* Invalid, Other, Unknown */ + continue; + + if (!common_mem_type) { + /* First filled slot */ + common_mem_type = mem_type; + } else { + /* Check that all filled slots have the same type */ + if (mem_type != common_mem_type) { + dev_warn(&adap->dev, + "Different memory types mixed, not instantiating SPD\n"); + return; + } + } + dimm_count++; + } + + /* No useful DMI data, bail out */ + if (!dimm_count) + return; + + dev_info(&adap->dev, "%d/%d memory slots populated (from DMI)\n", + dimm_count, slot_count); + + if (slot_count > 4) { + dev_warn(&adap->dev, + "Systems with more than 4 memory slots not supported yet, not instantiating SPD\n"); + return; + } + + switch (common_mem_type) { + case 0x13: /* DDR2 */ + case 0x18: /* DDR3 */ + case 0x1C: /* LPDDR2 */ + case 0x1D: /* LPDDR3 */ + name = "spd"; + break; + case 0x1A: /* DDR4 */ + case 0x1E: /* LPDDR4 */ + name = "ee1004"; + break; + default: + dev_info(&adap->dev, + "Memory type 0x%02x not supported yet, not instantiating SPD\n", + common_mem_type); + return; + } + + /* + * We don't know in which slots the memory modules are. We could + * try to guess from the slot names, but that would be rather complex + * and unreliable, so better probe all possible addresses until we + * have found all memory modules. + */ + for (n = 0; n < slot_count && dimm_count; n++) { + struct i2c_board_info info; + unsigned short addr_list[2]; + + memset(&info, 0, sizeof(struct i2c_board_info)); + strlcpy(info.type, name, I2C_NAME_SIZE); + addr_list[0] = 0x50 + n; + addr_list[1] = I2C_CLIENT_END; + + if (i2c_new_probed_device(adap, &info, addr_list, NULL)) { + dev_info(&adap->dev, + "Successfully instantiated SPD at 0x%hx\n", + addr_list[0]); + dimm_count--; + } + } +} +EXPORT_SYMBOL_GPL(i2c_register_spd); +#endif + MODULE_AUTHOR("Jean Delvare "); MODULE_DESCRIPTION("SMBus protocol extensions support"); MODULE_LICENSE("GPL"); --- linux-5.3.orig/include/linux/i2c-smbus.h 2019-10-14 11:33:20.372781769 +0200 +++ linux-5.3/include/linux/i2c-smbus.h 2019-10-14 16:44:22.636959738 +0200 @@ -2,7 +2,7 @@ /* * i2c-smbus.h - SMBus extensions to the I2C protocol * - * Copyright (C) 2010 Jean Delvare + * Copyright (C) 2010-2019 Jean Delvare */ #ifndef _LINUX_I2C_SMBUS_H @@ -44,4 +44,10 @@ static inline int of_i2c_setup_smbus_ale } #endif +#if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_DMI) +void i2c_register_spd(struct i2c_adapter *adap); +#else +static inline void i2c_register_spd(struct i2c_adapter *adap) { } +#endif + #endif /* _LINUX_I2C_SMBUS_H */ -- Jean Delvare SUSE L3 Support