From patchwork Fri Apr 9 20:14:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrick Leis X-Patchwork-Id: 12194993 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29498C433ED for ; Fri, 9 Apr 2021 20:18:27 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C191D6105A for ; Fri, 9 Apr 2021 20:18:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C191D6105A Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:56202 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lUxaD-0001kw-My for qemu-devel@archiver.kernel.org; Fri, 09 Apr 2021 16:18:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50204) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3vbVwYAcKCvQrajpqnackkcha.Ykimaiq-Zarahjkjcjq.knc@flex--venture.bounces.google.com>) id 1lUxWu-0008P7-O0 for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:00 -0400 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]:40835) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3vbVwYAcKCvQrajpqnackkcha.Ykimaiq-Zarahjkjcjq.knc@flex--venture.bounces.google.com>) id 1lUxWq-0000Ee-N0 for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:00 -0400 Received: by mail-yb1-xb4a.google.com with SMTP id w9so6548210ybw.7 for ; Fri, 09 Apr 2021 13:14:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=nv+8xvdz3LSmpwi61uacfMflzrGYkLHq1FKAJV3U2w4=; b=C8CbOhOEOftJsnofUlLpxo5s+C8JQyjo2riZ+Z8CHEnG7HHPbkjDaVCtCULb22+WbI 3YtV91mWDMwaLEhjn4PWiMrMOd1OmXwaB7tvcAv6Cl9LZnFNLHIoYam2rL4SMt9TaqeJ 1nu0auieKOrrCPxzIkKdxxZfA7IqM6RO9+xTaKzUllml1s1/m2RvpUYhg2ShxNKLAz2g wzHv/ntEEEhFV99QgRTOTlDc6qLrex9M5sLVrRUKsLsEzcMnGjYqsYa+KyYg7yi1K+HZ ijhRRUsQRvlOaBPMJ3A08SjSXxOwCG1eOzhIsdFZ42gYt42bBGZOCtwrm1ei7NU+M2wA +Zxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=nv+8xvdz3LSmpwi61uacfMflzrGYkLHq1FKAJV3U2w4=; b=jJzBvj7r/u1cF0McO8noIMIN0Zow6TB/AM5ETS4qGXEFqcinRaLXL1vvYakECLmGel RkiVHYgKkByk2O9TY8nyZHx2ZyPWfhUvbjhXNYzO4xFTkXSN89mARKJmqcBWUSMpjnX/ B9hDiH2XTr4unFoDftoiarEYmDSKBPnPMpxg8yLcFzp2aFePnmR0BV0ivHt/8zYAA2B/ 6D7iDNXaZIMvUPH8Ov9pNEVNA1x/YXUfjnT2oun8rtj1CObn2X8T2LucnYcl9ABWLMfh 6RU2BQyxudy13uUmhssqV5F2wbrB74VMPTgq7V5ara3uCYBoFECKhiygoe6R917My772 +Sqg== X-Gm-Message-State: AOAM53156Ovh+95JtEb1pc+qOk9B1qbyk/7CBe0c2NAqkSf37v8YqE3V IgeaYDH+s2AYIM3JswUnqWk0XjAcXr2X X-Google-Smtp-Source: ABdhPJyfL9r/NAgqH3LzObZkFDZbjFSc1yTzqSVaEZ1SSoG6bRIHNgcn7TZUVg3HTwGm9QzoDGeDdpFM7mWT X-Received: from venture.svl.corp.google.com ([2620:15c:2a3:200:ec3:6619:183b:36d8]) (user=venture job=sendgmr) by 2002:a25:907:: with SMTP id 7mr20460523ybj.346.1617999293031; Fri, 09 Apr 2021 13:14:53 -0700 (PDT) Date: Fri, 9 Apr 2021 13:14:40 -0700 In-Reply-To: <20210409201443.111197-1-venture@google.com> Message-Id: <20210409201443.111197-2-venture@google.com> Mime-Version: 1.0 References: <20210409201443.111197-1-venture@google.com> X-Mailer: git-send-email 2.31.1.295.g9ea45b61b8-goog Subject: [PATCH v3 1/4] hw/i2c: name I2CNode list in I2CBus From: Patrick Venture To: cminyard@mvista.com, wuhaotsh@google.com, hskinnemoen@google.com, f4bug@amsat.org Cc: qemu-devel@nongnu.org, qemu-arm@nongnu.org, Patrick Venture Received-SPF: pass client-ip=2607:f8b0:4864:20::b4a; envelope-from=3vbVwYAcKCvQrajpqnackkcha.Ykimaiq-Zarahjkjcjq.knc@flex--venture.bounces.google.com; helo=mail-yb1-xb4a.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" To enable passing the current_devs field as a parameter, we need to use a named struct type. Tested: BMC firmware with i2c devices booted to userspace. Signed-off-by: Patrick Venture Reviewed-by: Hao Wu Reviewed-by: Philippe Mathieu-Daudé --- include/hw/i2c/i2c.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h index 277dd9f2d6..1f7c268c86 100644 --- a/include/hw/i2c/i2c.h +++ b/include/hw/i2c/i2c.h @@ -58,9 +58,11 @@ struct I2CNode { QLIST_ENTRY(I2CNode) next; }; +typedef QLIST_HEAD(I2CNodeList, I2CNode) I2CNodeList; + struct I2CBus { BusState qbus; - QLIST_HEAD(, I2CNode) current_devs; + I2CNodeList current_devs; uint8_t saved_address; bool broadcast; }; From patchwork Fri Apr 9 20:14:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Leis X-Patchwork-Id: 12194995 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5BFEC433B4 for ; Fri, 9 Apr 2021 20:18:40 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 86A096105A for ; Fri, 9 Apr 2021 20:18:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 86A096105A Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:57024 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lUxaR-00025C-J8 for qemu-devel@archiver.kernel.org; Fri, 09 Apr 2021 16:18:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50238) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3vrVwYAcKCvUsbkqrobdlldib.Zljnbjr-absbiklkdkr.lod@flex--venture.bounces.google.com>) id 1lUxWw-0008Ru-D1 for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:11 -0400 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]:43696) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3vrVwYAcKCvUsbkqrobdlldib.Zljnbjr-absbiklkdkr.lod@flex--venture.bounces.google.com>) id 1lUxWq-0000Eq-SB for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:02 -0400 Received: by mail-yb1-xb49.google.com with SMTP id h69so1836944ybg.10 for ; Fri, 09 Apr 2021 13:14:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=O27tGi51CsQG7yDvwmP852qZ1gjQhBSmWisJIHI1BPM=; b=tbffN6R0v2teDS9kh7RdEHq/CEvrOnfMMMtBXvI29e4mF57EtZ9Ix/NAyFrRgyYFkw IYsbhT9QukaiAJmJKTewUmITNxBDDaWIXcpPJCvsP8oQJS/C2hSO8k8pIQzXm/8ZkC20 Xp5seY284XbuVZE2dKN7EHrOw5aTJl+Ql37f4MuaIZfbGOkprA9SsiijxsiL8XOm2Qa+ 4tc/zedpetICp+mbCN8Cm2hDbBa0g8CRXp2blFZkZ2j4AM4M7/W8kJbJMt3rtd1VfpgF gsFd2BP+4Xkkhn8CqyyoqNhBuTLRpjApULxujsIAo3SP3To84VM+xuU5MfoVCo+34Q20 w/4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=O27tGi51CsQG7yDvwmP852qZ1gjQhBSmWisJIHI1BPM=; b=Gvt99RLN/mLEes8VmkP1TGdRy7VEDpoWPZITtxQoj9ZIms4GlI28/R6JmZ3RuD/i3t c1MREf5W6qGc7KZg7SsiASqkYRn9Lr7xP+OXOUVaFOTZtFJW3vBgbwrTNGRaXZ0HWtRV Q1Ic6cCPqswRWuY7Es+AJdK/eZ4tymfKnyvVlpIRNWdJ2DsSctt08l7Cfo/fxtvF30jC voZsg1RXDVtfnlEgUXXOdDIu6HmsWDviITMdc2RY8o+pXP9TtRdVKQFI3jIr6Ykx0fIo ZMe3rVesJhk/3+05gNDd4tepxVEFKSipWwCxVssG6RM0vbNBYQJSke49fBcXqHflaRau 4gsA== X-Gm-Message-State: AOAM530jJGHZiYIpsbpGNrWGNaArLHTZsi92xDHmNjC9R5+ehWco5+s6 iSY3lYfDFu0KAT+VFOscKkhYpgr0/crA X-Google-Smtp-Source: ABdhPJx+nqrLRjGpouIMCOTS6ZXRdXKNePhqNvnFH2TFVFumg2PvBmpxsdQE3VRPO4n84evdVrzfbuRoVSV9 X-Received: from venture.svl.corp.google.com ([2620:15c:2a3:200:ec3:6619:183b:36d8]) (user=venture job=sendgmr) by 2002:a25:bb90:: with SMTP id y16mr19321177ybg.49.1617999294961; Fri, 09 Apr 2021 13:14:54 -0700 (PDT) Date: Fri, 9 Apr 2021 13:14:41 -0700 In-Reply-To: <20210409201443.111197-1-venture@google.com> Message-Id: <20210409201443.111197-3-venture@google.com> Mime-Version: 1.0 References: <20210409201443.111197-1-venture@google.com> X-Mailer: git-send-email 2.31.1.295.g9ea45b61b8-goog Subject: [PATCH v3 2/4] hw/i2c: add match method for device search From: Patrick Venture To: cminyard@mvista.com, wuhaotsh@google.com, hskinnemoen@google.com, f4bug@amsat.org Cc: qemu-devel@nongnu.org, qemu-arm@nongnu.org, Patrick Venture Received-SPF: pass client-ip=2607:f8b0:4864:20::b49; envelope-from=3vrVwYAcKCvUsbkqrobdlldib.Zljnbjr-absbiklkdkr.lod@flex--venture.bounces.google.com; helo=mail-yb1-xb49.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" At the start of an i2c transaction, the i2c bus searches its list of children to identify which devices correspond to the address (or broadcast). Now the I2CSlave device has a method "match" that encapsulates the lookup behavior. This allows the behavior to be changed to support devices, such as i2c muxes. Tested: A BMC firmware was booted to userspace and i2c devices were detected. Signed-off-by: Patrick Venture Reviewed-by: Hao Wu --- hw/i2c/core.c | 23 +++++++++++++++++++---- include/hw/i2c/i2c.h | 11 +++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/hw/i2c/core.c b/hw/i2c/core.c index 21ec52ac5a..d03b0eea5c 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -118,10 +118,9 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv) QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { DeviceState *qdev = kid->child; I2CSlave *candidate = I2C_SLAVE(qdev); - if ((candidate->address == address) || (bus->broadcast)) { - node = g_malloc(sizeof(struct I2CNode)); - node->elt = candidate; - QLIST_INSERT_HEAD(&bus->current_devs, node, next); + sc = I2C_SLAVE_GET_CLASS(candidate); + if (sc->match_and_add(candidate, address, bus->broadcast, + &bus->current_devs)) { if (!bus->broadcast) { break; } @@ -290,12 +289,28 @@ I2CSlave *i2c_slave_create_simple(I2CBus *bus, const char *name, uint8_t addr) return dev; } +static bool i2c_slave_match(I2CSlave *candidate, uint8_t address, + bool broadcast, I2CNodeList *current_devs) +{ + if ((candidate->address == address) || (broadcast)) { + I2CNode *node = g_malloc(sizeof(struct I2CNode)); + node->elt = candidate; + QLIST_INSERT_HEAD(current_devs, node, next); + return true; + } + + /* Not found and not broadcast. */ + return false; +} + static void i2c_slave_class_init(ObjectClass *klass, void *data) { DeviceClass *k = DEVICE_CLASS(klass); + I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass); set_bit(DEVICE_CATEGORY_MISC, k->categories); k->bus_type = TYPE_I2C_BUS; device_class_set_props(k, i2c_props); + sc->match_and_add = i2c_slave_match; } static const TypeInfo i2c_slave_type_info = { diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h index 1f7c268c86..9b8b95ff4a 100644 --- a/include/hw/i2c/i2c.h +++ b/include/hw/i2c/i2c.h @@ -16,6 +16,7 @@ enum i2c_event { I2C_NACK /* Masker NACKed a receive byte. */ }; +typedef struct I2CNodeList I2CNodeList; #define TYPE_I2C_SLAVE "i2c-slave" OBJECT_DECLARE_TYPE(I2CSlave, I2CSlaveClass, @@ -39,6 +40,16 @@ struct I2CSlaveClass { * return code is not used and should be zero. */ int (*event)(I2CSlave *s, enum i2c_event event); + + /* + * Check if this device matches the address provided. Returns bool of + * true if it matches (or broadcast), and updates the device list, false + * otherwise. + * + * If broadcast is true, match should add the device and return true. + */ + bool (*match_and_add)(I2CSlave *candidate, uint8_t address, bool broadcast, + I2CNodeList *current_devs); }; struct I2CSlave { From patchwork Fri Apr 9 20:14:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Leis X-Patchwork-Id: 12194997 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0254C433ED for ; Fri, 9 Apr 2021 20:20:40 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6CE186113A for ; Fri, 9 Apr 2021 20:20:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6CE186113A Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:34446 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lUxcN-0004NY-7P for qemu-devel@archiver.kernel.org; Fri, 09 Apr 2021 16:20:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50218) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3wLVwYAcKCvcudmstqdfnnfkd.bnlpdlt-cdudkmnmfmt.nqf@flex--venture.bounces.google.com>) id 1lUxWv-0008Rm-ME for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:10 -0400 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]:50326) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3wLVwYAcKCvcudmstqdfnnfkd.bnlpdlt-cdudkmnmfmt.nqf@flex--venture.bounces.google.com>) id 1lUxWs-0000G8-Eq for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:01 -0400 Received: by mail-qk1-x749.google.com with SMTP id p133so4040857qka.17 for ; Fri, 09 Apr 2021 13:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=/nZIqirGm/4hMePzQ47kJsD6GC6cfP2kwpUtkwbOQ3w=; b=nKegeAjGrvU1JWC+3iv34rFEQsL4FBeVyxvluk/nOCLCOzErkRkCH6rfRlUc4rbW8I 4N5sYzHTLXLMZT4A/FPfAM2gx6d2X9ckUYMXnrcGxp9TA+YIIbVHruIJHA0vxswlaQJD Xua3LpGRZ6QlFse+BRyZ0Yao1JB8IilQQBXCW/wA/l0QDSpe5Wi6XQKx0QIP7YE5yorx hTtK6ved20YE2sQ62CMkwXACzTlelisYKY3rL8hBw20vUz6tW7mJuEPYOvsS5kFNsSH5 IQO5YLbLodm6iQns//7WNS+mksE8fTlyOGBgIbzVTN/YO/FkpsQkuOPX7uhhgoobA0C9 kw1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/nZIqirGm/4hMePzQ47kJsD6GC6cfP2kwpUtkwbOQ3w=; b=hz/09l2DGM2UCISZMiHr1AiIyNGzmZHPJARCurlqDyN0m9Ib10/EEoCCvvz+K8X1c3 sgeQIKAw/CyhPCOweLIRnvqJJ9HlCf0O0OI+9B+cNcbhszn3aNtmPJKB7RFfUXVmlJRg +/zOqHzKG6+7Nl8/I3AmZDDDBvtcrSX051Af35+o/eE/Ua3/tTD7ADcQgVZxGeylJwRO BCrssZSt7iC9W2DPe4BDgpXOfvRKF8TqH4hB8MsWpyn0ODTmNN6PZJ78pn5tayWpvQkL ebMY+3H9OdBTgwM/drBASE99pVULKSI2z+jooDdjBXWSqcNtRprDvqhfY8+j2J+exeXc +gfA== X-Gm-Message-State: AOAM5334ll96JDpB1Hm1ysKTdNauRpF+73I7Pft597QEm3Elu9vTNOL9 W9haUp7uoqdkcu6X5JC8EwuK5wB2Gb+q X-Google-Smtp-Source: ABdhPJwoMfk8Dsv4W53L1UAoo2xM5zah8i+/Lwp67mh+jD6JUE4+2t4JGhkPxWG6A51DC2LK8XLcJ3EC9wwD X-Received: from venture.svl.corp.google.com ([2620:15c:2a3:200:ec3:6619:183b:36d8]) (user=venture job=sendgmr) by 2002:a0c:85a5:: with SMTP id o34mr4211593qva.38.1617999296803; Fri, 09 Apr 2021 13:14:56 -0700 (PDT) Date: Fri, 9 Apr 2021 13:14:42 -0700 In-Reply-To: <20210409201443.111197-1-venture@google.com> Message-Id: <20210409201443.111197-4-venture@google.com> Mime-Version: 1.0 References: <20210409201443.111197-1-venture@google.com> X-Mailer: git-send-email 2.31.1.295.g9ea45b61b8-goog Subject: [PATCH v3 3/4] hw/i2c: move search to i2c_scan_bus method From: Patrick Venture To: cminyard@mvista.com, wuhaotsh@google.com, hskinnemoen@google.com, f4bug@amsat.org Cc: qemu-devel@nongnu.org, qemu-arm@nongnu.org, Patrick Venture Received-SPF: pass client-ip=2607:f8b0:4864:20::749; envelope-from=3wLVwYAcKCvcudmstqdfnnfkd.bnlpdlt-cdudkmnmfmt.nqf@flex--venture.bounces.google.com; helo=mail-qk1-x749.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Moves the search for matching devices on an i2c bus into a separate method. This allows for an object that owns an I2CBus can avoid duplicating this method. Tested: A BMC firmware was booted to userspace and i2c devices were detected. Signed-off-by: Patrick Venture Reviewed-by: Hao Wu --- hw/i2c/core.c | 38 ++++++++++++++++++++++++++------------ include/hw/i2c/i2c.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/hw/i2c/core.c b/hw/i2c/core.c index d03b0eea5c..12981cea2d 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -77,6 +77,30 @@ int i2c_bus_busy(I2CBus *bus) return !QLIST_EMPTY(&bus->current_devs); } +bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool broadcast) +{ + BusChild *kid; + + QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { + DeviceState *qdev = kid->child; + I2CSlave *candidate = I2C_SLAVE(qdev); + I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(candidate); + + if (sc->match_and_add(candidate, address, broadcast, + &bus->current_devs)) { + if (!broadcast) { + return true; + } + } + } + + /* + * If broadcast was true, and the list was full or empty, return true. If + * broadcast was false, return false. + */ + return broadcast; +} + /* TODO: Make this handle multiple masters. */ /* * Start or continue an i2c transaction. When this is called for the @@ -93,7 +117,6 @@ int i2c_bus_busy(I2CBus *bus) */ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv) { - BusChild *kid; I2CSlaveClass *sc; I2CNode *node; bool bus_scanned = false; @@ -115,17 +138,8 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv) * terminating the previous transaction. */ if (QLIST_EMPTY(&bus->current_devs)) { - QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { - DeviceState *qdev = kid->child; - I2CSlave *candidate = I2C_SLAVE(qdev); - sc = I2C_SLAVE_GET_CLASS(candidate); - if (sc->match_and_add(candidate, address, bus->broadcast, - &bus->current_devs)) { - if (!bus->broadcast) { - break; - } - } - } + /* Disregard whether devices were found. */ + (void)i2c_scan_bus(bus, address, bus->broadcast); bus_scanned = true; } diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h index 9b8b95ff4a..45915f3303 100644 --- a/include/hw/i2c/i2c.h +++ b/include/hw/i2c/i2c.h @@ -87,6 +87,7 @@ void i2c_nack(I2CBus *bus); int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send); int i2c_send(I2CBus *bus, uint8_t data); uint8_t i2c_recv(I2CBus *bus); +bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool broadcast); /** * Create an I2C slave device on the heap. From patchwork Fri Apr 9 20:14:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Leis X-Patchwork-Id: 12195001 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4735BC433B4 for ; Fri, 9 Apr 2021 20:22:51 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9F4866113A for ; Fri, 9 Apr 2021 20:22:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9F4866113A Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:43264 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lUxeT-0008DQ-PZ for qemu-devel@archiver.kernel.org; Fri, 09 Apr 2021 16:22:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50292) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3wrVwYAcKCvkwfouvsfhpphmf.dpnrfnv-efwfmopohov.psh@flex--venture.bounces.google.com>) id 1lUxX4-0008SA-9R for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:11 -0400 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]:40835) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3wrVwYAcKCvkwfouvsfhpphmf.dpnrfnv-efwfmopohov.psh@flex--venture.bounces.google.com>) id 1lUxWu-0000HU-FH for qemu-devel@nongnu.org; Fri, 09 Apr 2021 16:15:09 -0400 Received: by mail-yb1-xb49.google.com with SMTP id w9so6548412ybw.7 for ; Fri, 09 Apr 2021 13:14:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=op1JVdgG+oNGOVtUXcrtF+6hsQck/NnplQsrYgK1U/M=; b=IaCfkTR6fX6N7uDyio0rsP6k9hJSdo7Ci4sy3YM8CCstE7GaA0OY/trSBoECBxdN8W dX85xW8LRBiIQg6VoduJcUd8eBywQHJo0jrlBh4urLsAHz1mUl7z5gu2JQTQkqiwTqCw NXrWVwY/IdTgS7NU0G7gkpJfsw7zFG+EOedyF+Pz2VG3VhgzmgJufJMkql5aHaiyX4zF ZQi6VaI9d+joNjcv6LrUofRw4sFWd4d5QmKNLUP20RJUTEe3fcwhy4L7dTQw818akK9B rZvbHZi9J6pSHzbHaTR/84A8hwoVjm0xxWzn+Dx+hvPzxLPNhWum5mvaxFgVh01AHdpV buhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=op1JVdgG+oNGOVtUXcrtF+6hsQck/NnplQsrYgK1U/M=; b=ico1nOfhZwxm9Xu9fQTLre8UNNp8AJfbjlp+TZ4GYtdhqwaW/EBXikYszARhqlnuht oPaYmuip3RThZrwcC1sSVtxFykAPlhwx32ZkEKCUJ+QO2kiQNpfTzhUn1O3sRwuY8CEa t1nrpHxnpp7gBxImfxxCpYQeLSRIH0AZX6U8NpJZxTfbWfNdPKrC7+yg6ptacINCHcnY iBpqek7Q/gBv9QGUoNbzLbxGegG64/jiZtWm57Hqb3CmmU7gGLljs1IPnLIyXfQw/W0q JjDVHuWHFarGb3o8/w6nbYa1/oGk+6o+X7+t4kgRMKAPQyNJ/t2+YuRVsDaqKFRBazu8 Vqfg== X-Gm-Message-State: AOAM531v5dwdD/QsI+ia38kLp5o9eX2xbvwmFIRNOL8zyVMzLclbd3Ci 8+ML+j3Ymn9ELONc7hLXnlQGjqodo03D X-Google-Smtp-Source: ABdhPJy/QlDpko8p4fPyOPYy/qgAv3vGm0iGgB7AcArEXj9875hCEEcCWFnIayziapAbBWsuBfjl+oyemw3M X-Received: from venture.svl.corp.google.com ([2620:15c:2a3:200:ec3:6619:183b:36d8]) (user=venture job=sendgmr) by 2002:a25:bed1:: with SMTP id k17mr21055888ybm.125.1617999298996; Fri, 09 Apr 2021 13:14:58 -0700 (PDT) Date: Fri, 9 Apr 2021 13:14:43 -0700 In-Reply-To: <20210409201443.111197-1-venture@google.com> Message-Id: <20210409201443.111197-5-venture@google.com> Mime-Version: 1.0 References: <20210409201443.111197-1-venture@google.com> X-Mailer: git-send-email 2.31.1.295.g9ea45b61b8-goog Subject: [PATCH v3 4/4] hw/i2c: add pca954x i2c-mux switch From: Patrick Venture To: cminyard@mvista.com, wuhaotsh@google.com, hskinnemoen@google.com, f4bug@amsat.org Cc: qemu-devel@nongnu.org, qemu-arm@nongnu.org, Patrick Venture Received-SPF: pass client-ip=2607:f8b0:4864:20::b49; envelope-from=3wrVwYAcKCvkwfouvsfhpphmf.dpnrfnv-efwfmopohov.psh@flex--venture.bounces.google.com; helo=mail-yb1-xb49.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" The pca954x is an i2c mux, and this adds support for two variants of this device: the pca9546 and pca9548. This device is very common on BMCs to route a different channel to each PCIe i2c bus downstream from the BMC. Signed-off-by: Patrick Venture Reviewed-by: Hao Wu Reviewed-by: Havard Skinnemoen --- MAINTAINERS | 6 + hw/i2c/Kconfig | 4 + hw/i2c/i2c_mux_pca954x.c | 289 +++++++++++++++++++++++++++++++ hw/i2c/meson.build | 1 + hw/i2c/trace-events | 5 + include/hw/i2c/i2c_mux_pca954x.h | 19 ++ 6 files changed, 324 insertions(+) create mode 100644 hw/i2c/i2c_mux_pca954x.c create mode 100644 include/hw/i2c/i2c_mux_pca954x.h diff --git a/MAINTAINERS b/MAINTAINERS index 58f342108e..5ea0b60b8a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2039,6 +2039,12 @@ S: Maintained F: hw/net/tulip.c F: hw/net/tulip.h +pca954x +M: Patrick Venture +S: Maintained +F: hw/i2c/i2c_mux_pca954x.c +F: include/hw/i2c/i2c_mux_pca954x.h + Generic Loader M: Alistair Francis S: Maintained diff --git a/hw/i2c/Kconfig b/hw/i2c/Kconfig index 09642a6dcb..8d120a25d5 100644 --- a/hw/i2c/Kconfig +++ b/hw/i2c/Kconfig @@ -28,3 +28,7 @@ config IMX_I2C config MPC_I2C bool select I2C + +config PCA954X + bool + select I2C diff --git a/hw/i2c/i2c_mux_pca954x.c b/hw/i2c/i2c_mux_pca954x.c new file mode 100644 index 0000000000..a716810e02 --- /dev/null +++ b/hw/i2c/i2c_mux_pca954x.c @@ -0,0 +1,289 @@ +/* + * I2C multiplexer for PCA954x series of I2C multiplexer/switch chips. + * + * Copyright 2021 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/i2c/i2c.h" +#include "hw/i2c/i2c_mux_pca954x.h" +#include "hw/i2c/smbus_slave.h" +#include "hw/qdev-core.h" +#include "hw/sysbus.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "qemu/queue.h" +#include "qom/object.h" +#include "trace.h" + +#define PCA9548_CHANNEL_COUNT 8 +#define PCA9546_CHANNEL_COUNT 4 + +/* + * struct Pca954xChannel - The i2c mux device will have N of these states + * that own the i2c channel bus. + * @bus: The owned channel bus. + * @enabled: Is this channel active? + */ +typedef struct Pca954xChannel { + SysBusDevice parent; + + I2CBus *bus; + + bool enabled; +} Pca954xChannel; + +#define TYPE_PCA954X_CHANNEL "pca954x-channel" +#define PCA954X_CHANNEL(obj) \ + OBJECT_CHECK(Pca954xChannel, (obj), TYPE_PCA954X_CHANNEL) + +/* + * struct Pca954xState - The pca954x state object. + * @control: The value written to the mux control. + * @channel: The set of i2c channel buses that act as channels which own the + * i2c children. + */ +typedef struct Pca954xState { + SMBusDevice parent; + + uint8_t control; + + /* The channel i2c buses. */ + Pca954xChannel channel[PCA9548_CHANNEL_COUNT]; +} Pca954xState; + +/* + * struct Pca954xClass - The pca954x class object. + * @nchans: The number of i2c channels this device has. + */ +typedef struct Pca954xClass { + SMBusDeviceClass parent; + + uint8_t nchans; +} Pca954xClass; + +#define TYPE_PCA954X "pca954x" +OBJECT_DECLARE_TYPE(Pca954xState, Pca954xClass, PCA954X) + +/* + * For each channel, if it's enabled, recursively call match on those children. + */ +static bool pca954x_match(I2CSlave *candidate, uint8_t address, + bool broadcast, + I2CNodeList *current_devs) +{ + Pca954xState *mux = PCA954X(candidate); + Pca954xClass *mc = PCA954X_GET_CLASS(mux); + int i; + + /* They are talking to the mux itself (or all devices enabled). */ + if ((candidate->address == address) || broadcast) { + I2CNode *node = g_malloc(sizeof(struct I2CNode)); + node->elt = candidate; + QLIST_INSERT_HEAD(current_devs, node, next); + if (!broadcast) { + return true; + } + } + + for (i = 0; i < mc->nchans; i++) { + if (!mux->channel[i].enabled) { + continue; + } + + if (i2c_scan_bus(mux->channel[i].bus, address, broadcast)) { + if (!broadcast) { + return true; + } + } + } + + /* If we arrived here we didn't find a match, return broadcast. */ + return broadcast; +} + +static void pca954x_enable_channel(Pca954xState *s, uint8_t enable_mask) +{ + Pca954xClass *mc = PCA954X_GET_CLASS(s); + int i; + + /* + * For each channel, check if their bit is set in enable_mask and if yes, + * enable it, otherwise disable, hide it. + */ + for (i = 0; i < mc->nchans; i++) { + if (enable_mask & (1 << i)) { + s->channel[i].enabled = true; + } else { + s->channel[i].enabled = false; + } + } +} + +static void pca954x_write(Pca954xState *s, uint8_t data) +{ + s->control = data; + pca954x_enable_channel(s, data); + + trace_pca954x_write_bytes(data); +} + +static int pca954x_write_data(SMBusDevice *d, uint8_t *buf, uint8_t len) +{ + Pca954xState *s = PCA954X(d); + + if (len == 0) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__); + return -1; + } + + /* + * len should be 1, because they write one byte to enable/disable channels. + */ + if (len > 1) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: extra data after channel selection mask\n", + __func__); + return -1; + } + + pca954x_write(s, buf[0]); + return 0; +} + +static uint8_t pca954x_read_byte(SMBusDevice *d) +{ + Pca954xState *s = PCA954X(d); + uint8_t data = s->control; + trace_pca954x_read_data(data); + return data; +} + +static void pca954x_enter_reset(Object *obj, ResetType type) +{ + Pca954xState *s = PCA954X(obj); + /* Reset will disable all channels. */ + pca954x_write(s, 0); +} + +I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t channel) +{ + Pca954xClass *pc = PCA954X_GET_CLASS(mux); + Pca954xState *pca954x = PCA954X(mux); + + g_assert(channel < pc->nchans); + return I2C_BUS(qdev_get_child_bus(DEVICE(&pca954x->channel[channel]), + "i2c-bus")); +} + +static void pca954x_smbus_init(Object *obj) +{ + Pca954xChannel *s = PCA954X_CHANNEL(obj); + s->bus = i2c_init_bus(DEVICE(s), "i2c-bus"); + + /* Start all channels as disabled. */ + s->enabled = false; +} + +static void pca954x_channel_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + dc->desc = "Pca954x Channel"; +} + +static void pca9546_class_init(ObjectClass *klass, void *data) +{ + Pca954xClass *s = PCA954X_CLASS(klass); + s->nchans = PCA9546_CHANNEL_COUNT; +} + +static void pca9548_class_init(ObjectClass *oc, void *data) +{ + Pca954xClass *s = PCA954X_CLASS(oc); + s->nchans = PCA9548_CHANNEL_COUNT; +} + +static void pca954x_realize(DeviceState *dev, Error **errp) +{ + Pca954xState *s = PCA954X(dev); + Pca954xClass *c = PCA954X_GET_CLASS(s); + int i; + + /* SMBus modules. Cannot fail. */ + for (i = 0; i < c->nchans; i++) { + sysbus_realize(SYS_BUS_DEVICE(&s->channel[i]), &error_abort); + } +} + +static void pca954x_init(Object *obj) +{ + int i; + Pca954xState *s = PCA954X(obj); + Pca954xClass *c = PCA954X_GET_CLASS(obj); + + /* Only initialize the children we expect. */ + for (i = 0; i < c->nchans; i++) { + object_initialize_child(obj, "channel[*]", &s->channel[i], + TYPE_PCA954X_CHANNEL); + } +} + +static void pca954x_class_init(ObjectClass *klass, void *data) +{ + I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass); + ResettableClass *rc = RESETTABLE_CLASS(klass); + DeviceClass *dc = DEVICE_CLASS(klass); + SMBusDeviceClass *k = SMBUS_DEVICE_CLASS(klass); + + sc->match_and_add = pca954x_match; + + rc->phases.enter = pca954x_enter_reset; + + dc->desc = "Pca954x i2c-mux"; + dc->realize = pca954x_realize; + + k->write_data = pca954x_write_data; + k->receive_byte = pca954x_read_byte; +} + +static const TypeInfo pca954x_info[] = { + { + .name = TYPE_PCA954X, + .parent = TYPE_SMBUS_DEVICE, + .instance_size = sizeof(Pca954xState), + .instance_init = pca954x_init, + .class_size = sizeof(Pca954xClass), + .class_init = pca954x_class_init, + .abstract = true, + }, + { + .name = TYPE_PCA9546, + .parent = TYPE_PCA954X, + .class_init = pca9546_class_init, + }, + { + .name = TYPE_PCA9548, + .parent = TYPE_PCA954X, + .class_init = pca9548_class_init, + }, + { + .name = TYPE_PCA954X_CHANNEL, + .parent = TYPE_SYS_BUS_DEVICE, + .class_init = pca954x_channel_class_init, + .instance_size = sizeof(Pca954xChannel), + .instance_init = pca954x_smbus_init, + } +}; + +DEFINE_TYPES(pca954x_info) diff --git a/hw/i2c/meson.build b/hw/i2c/meson.build index cdcd694a7f..dd3aef02b2 100644 --- a/hw/i2c/meson.build +++ b/hw/i2c/meson.build @@ -14,4 +14,5 @@ i2c_ss.add(when: 'CONFIG_SMBUS_EEPROM', if_true: files('smbus_eeprom.c')) i2c_ss.add(when: 'CONFIG_VERSATILE_I2C', if_true: files('versatile_i2c.c')) i2c_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_i2c.c')) i2c_ss.add(when: 'CONFIG_PPC4XX', if_true: files('ppc4xx_i2c.c')) +i2c_ss.add(when: 'CONFIG_PCA954X', if_true: files('i2c_mux_pca954x.c')) softmmu_ss.add_all(when: 'CONFIG_I2C', if_true: i2c_ss) diff --git a/hw/i2c/trace-events b/hw/i2c/trace-events index 82fe6f965f..82f19e6a2d 100644 --- a/hw/i2c/trace-events +++ b/hw/i2c/trace-events @@ -26,3 +26,8 @@ npcm7xx_smbus_recv_byte(const char *id, uint8_t value) "%s recv byte: 0x%02x" npcm7xx_smbus_stop(const char *id) "%s stopping" npcm7xx_smbus_nack(const char *id) "%s nacking" npcm7xx_smbus_recv_fifo(const char *id, uint8_t received, uint8_t expected) "%s recv fifo: received %u, expected %u" + +# i2c-mux-pca954x.c + +pca954x_write_bytes(uint8_t value) "PCA954X write data: 0x%02x" +pca954x_read_data(uint8_t value) "PCA954X read data: 0x%02x" diff --git a/include/hw/i2c/i2c_mux_pca954x.h b/include/hw/i2c/i2c_mux_pca954x.h new file mode 100644 index 0000000000..8aaf9bbc39 --- /dev/null +++ b/include/hw/i2c/i2c_mux_pca954x.h @@ -0,0 +1,19 @@ +#ifndef QEMU_I2C_MUX_PCA954X +#define QEMU_I2C_MUX_PCA954X + +#include "hw/i2c/i2c.h" + +#define TYPE_PCA9546 "pca9546" +#define TYPE_PCA9548 "pca9548" + +/** + * Retrieves the i2c bus associated with the specified channel on this i2c + * mux. + * @mux: an i2c mux device. + * @channel: the i2c channel requested + * + * Returns: a pointer to the associated i2c bus. + */ +I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t channel); + +#endif