From patchwork Mon Aug 20 20:26:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Minyard X-Patchwork-Id: 144664 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp4416793ljj; Mon, 20 Aug 2018 13:39:02 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbL9Ija2LkOu6d8/xRprqaO5qOjWxRq+/TwNlse1cFxknt298c8U+yIVJU0tyuMcf9LsZcT X-Received: by 2002:aed:3d71:: with SMTP id h46-v6mr14462974qtf.416.1534797542512; Mon, 20 Aug 2018 13:39:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534797542; cv=none; d=google.com; s=arc-20160816; b=B45Xjfc/FR+pW6fkrebOY9/HFZBLs8IWx+W0zKx6P2iE889knjUTLN7ATJELbnNOSG 31BeYc5buSx2x+KSJFIaklxY2e3hnoOKZ0esx18rKlbzwKwmATMNnCNZFreCI29D7eXp j0hcS9iOtjVky/xIKdfGypfpFJx5HiGLYrcUJqbmdJblB9qJNC5UNb9NJRV51IuzsY8B 9Tut+ZzHSgEpXIB0+rmHQe2yQo7cLIPzPS8K5SYlUREIW2OJWzLsfmF4BKzY1qJ7Lvjt 7f+stiJcRIemyJdP635giZUFli39m65Vzhj6x3H5hNrXsagWIZQGIH3dH8MzPgUbVlaE i8Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=LRO8EElwYxFb7czCHQ6NC/4MgeOza6zCgrSvHqTY8Jo=; b=P+biiisLD0w9yiia7Yih61saZWPkQduWsAgwF1SME6NHKC2WQa5fa9byXtFU5I6Ygj CiLx18aH65u4CUkap43K9nbWBL7FVflx5Q2x5eGK5/ZpZq5ksqV6a8d4dbjq56Y3qC+1 KY/hwTaSG0dRhbfoPpLl0qEYIH3FvirO4NLGMC/xgL2GCsGOZufilzRFBym52sc36wVZ EYzQRMqEBwrZX4So0Ed7KVgc+uAgEovBnUHZQWpNinRzmvc8J/Eiic+JK0Z16oA8dSST +7AYGTB7x1lkrcDfp02IjaGg1S5J8Y7FpsE5LM8WXEBrqMwsES4F+PxVMFYqXLqq0FL5 6aBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=YLgxRlDh; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id h98-v6si2373219qkh.363.2018.08.20.13.39.02 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 20 Aug 2018 13:39:02 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=YLgxRlDh; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Received: from localhost ([::1]:49195 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frqx3-0002hv-Qg for patch@linaro.org; Mon, 20 Aug 2018 16:39:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51553) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frqlC-0002pR-Rw for qemu-devel@nongnu.org; Mon, 20 Aug 2018 16:26:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1frql0-0002SL-Ff for qemu-devel@nongnu.org; Mon, 20 Aug 2018 16:26:45 -0400 Received: from mail-oi0-x235.google.com ([2607:f8b0:4003:c06::235]:39195) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1frqkw-0002F7-8s for qemu-devel@nongnu.org; Mon, 20 Aug 2018 16:26:30 -0400 Received: by mail-oi0-x235.google.com with SMTP id d189-v6so28171248oib.6 for ; Mon, 20 Aug 2018 13:26:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=LRO8EElwYxFb7czCHQ6NC/4MgeOza6zCgrSvHqTY8Jo=; b=YLgxRlDhueOmvN2xXJ5LogI38KRCH4iFCQWxByEbGPCQcQsT3QYlInpFKpcH6Vh1GX Z6+dQWVhbnRiTYe1whJmk/xRKPiwHwc7WGBLPjMqjzN/m7d5se3M2dOKp5x6Qyu37hI4 Gzz+j4kg0rv1JhifkUa9Aic2g8Ymd47tPYkMJY4uYNfFDJeZpafmGCAVoNFUjmzLwYhq wMklN9hK9vsl+6JWjwGmFmiaN4PQnlMmydwOOpMu942uVz7WSYzmIPQnWs9++Yi4SH+Z Tpfon2YZiOlsHHZweQkpWQA35wZz5c66oI6YdqR96cujXQ4n1eNj0FQRhKz3USOdChNl qJmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=LRO8EElwYxFb7czCHQ6NC/4MgeOza6zCgrSvHqTY8Jo=; b=XRBNl3KwwIjla0zyf/JhoV1TNz6g/tBkg2vYZMk5RQ+ezXKbmE6hpDhErb5uDpC+fZ xU3ro35qqB8b1TYo/HW7mWx4H6ms3bLGjkI3PIdT/Zx6z1k6U1CdHmzsE75ienpXps8Q YV+zh39cLujgWXY6LtYIOZ5gSuEWZb5F4t6+uI5eP7NgCBY3d9rBqXvbq2tMzA1OVGd+ Y92SNg46r+1UTm9TTXFMcDecAVOSptzssJ5MB0zI/mPSW5Kk4zc0m8slgkKrgKbC31Ch +dFEWOLRf/miRwgYfuX32yogRXQTEq9fe37xZP/iTOvgIew+bUew9+0YPu/Tb2kIWmld OcyQ== X-Gm-Message-State: AOUpUlFN1U9HI2D6twHg8wGHXZOXpqmh7ozwqjYd84Nh2g/YOw93NMHg J41Em9H9wq/8Vg5OCiAt2A== X-Received: by 2002:a54:4f88:: with SMTP id g8-v6mr16003957oiy.191.1534796786319; Mon, 20 Aug 2018 13:26:26 -0700 (PDT) Received: from serve.minyard.net ([47.184.170.128]) by smtp.gmail.com with ESMTPSA id x5-v6sm10839832oix.3.2018.08.20.13.26.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Aug 2018 13:26:25 -0700 (PDT) Received: from t430.minyard.net (unknown [IPv6:2001:470:b8f6:1b:7d98:76b7:e7fd:495f]) by serve.minyard.net (Postfix) with ESMTPA id C016CC16; Mon, 20 Aug 2018 15:26:23 -0500 (CDT) Received: by t430.minyard.net (Postfix, from userid 1000) id 476273000FF; Mon, 20 Aug 2018 15:26:20 -0500 (CDT) From: minyard@acm.org To: Paolo Bonzini , qemu-devel@nongnu.org Date: Mon, 20 Aug 2018 15:26:02 -0500 Message-Id: <1534796770-10295-3-git-send-email-minyard@acm.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534796770-10295-1-git-send-email-minyard@acm.org> References: <1534796770-10295-1-git-send-email-minyard@acm.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4003:c06::235 Subject: [Qemu-devel] [PATCH v2 02/10] i2c:pm_smbus: Fix the semantics of block I2C transfers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Corey Minyard , "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Corey Minyard The I2C block transfer commands was not implemented correctly, it read a length byte and such like it was an smbus transfer. So fix the smbus_read_block() and smbus_write_block() functions so they can properly handle I2C transfers, and normal SMBus transfers (for upcoming changes). Pass in a transfer size and a bool to know whether to use the size byte (like SMBus) or use the length given (like I2C). Signed-off-by: Corey Minyard Cc: Michael S. Tsirkin Cc: Paolo Bonzini --- hw/i2c/pm_smbus.c | 10 ++++++++-- hw/i2c/smbus.c | 37 ++++++++++++++++++++++++------------- include/hw/i2c/smbus.h | 17 +++++++++++++++-- 3 files changed, 47 insertions(+), 17 deletions(-) -- 2.7.4 diff --git a/hw/i2c/pm_smbus.c b/hw/i2c/pm_smbus.c index 83c2377..f1fe889 100644 --- a/hw/i2c/pm_smbus.c +++ b/hw/i2c/pm_smbus.c @@ -117,10 +117,16 @@ static void smb_transaction(PMSMBus *s) break; case PROT_I2C_BLOCK_DATA: if (read) { - ret = smbus_read_block(bus, addr, cmd, s->smb_data); + int xfersize = s->smb_data0; + if (xfersize > sizeof(s->smb_data)) { + xfersize = sizeof(s->smb_data); + } + ret = smbus_read_block(bus, addr, s->smb_data1, s->smb_data, + xfersize, false, true); goto data8; } else { - ret = smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0); + ret = smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0, + false); goto done; } break; diff --git a/hw/i2c/smbus.c b/hw/i2c/smbus.c index 587ce1a..6ff77c5 100644 --- a/hw/i2c/smbus.c +++ b/hw/i2c/smbus.c @@ -293,33 +293,42 @@ int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data) return 0; } -int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data) +int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data, + int len, bool recv_len, bool send_cmd) { - int len; + int rlen; int i; - if (i2c_start_transfer(bus, addr, 0)) { - return -1; + if (send_cmd) { + if (i2c_start_transfer(bus, addr, 0)) { + return -1; + } + i2c_send(bus, command); } - i2c_send(bus, command); if (i2c_start_transfer(bus, addr, 1)) { - i2c_end_transfer(bus); + if (send_cmd) { + i2c_end_transfer(bus); + } return -1; } - len = i2c_recv(bus); - if (len > 32) { - len = 0; + if (recv_len) { + rlen = i2c_recv(bus); + } else { + rlen = len; } - for (i = 0; i < len; i++) { + if (rlen > len) { + rlen = 0; + } + for (i = 0; i < rlen; i++) { data[i] = i2c_recv(bus); } i2c_nack(bus); i2c_end_transfer(bus); - return len; + return rlen; } int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data, - int len) + int len, bool send_len) { int i; @@ -330,7 +339,9 @@ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data, return -1; } i2c_send(bus, command); - i2c_send(bus, len); + if (send_len) { + i2c_send(bus, len); + } for (i = 0; i < len; i++) { i2c_send(bus, data[i]); } diff --git a/include/hw/i2c/smbus.h b/include/hw/i2c/smbus.h index 4fdba02..d8b1b9e 100644 --- a/include/hw/i2c/smbus.h +++ b/include/hw/i2c/smbus.h @@ -72,9 +72,22 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command); int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data); int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command); int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data); -int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data); + +/* + * Do a block transfer from an I2C device. If recv_len is set, then the + * first received byte is a length field and is used to know how much data + * to receive. Otherwise receive "len" bytes. If send_cmd is set, send + * the command byte first before receiving the data. + */ +int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data, + int len, bool recv_len, bool send_cmd); + +/* + * Do a block transfer to an I2C device. If send_len is set, send the + * "len" value before the data. + */ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data, - int len); + int len, bool send_len); void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf); void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,