From patchwork Thu Dec 7 21:46:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Minyard X-Patchwork-Id: 10101155 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D8AD960329 for ; Thu, 7 Dec 2017 21:54:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C9FC62882D for ; Thu, 7 Dec 2017 21:54:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C882F28842; Thu, 7 Dec 2017 21:54:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1A1AB2882D for ; Thu, 7 Dec 2017 21:54:51 +0000 (UTC) Received: from localhost ([::1]:34539 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eN483-00057V-2D for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Dec 2017 16:54:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eN3zz-000406-Pp for qemu-devel@nongnu.org; Thu, 07 Dec 2017 16:46:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eN3zx-0006K3-GV for qemu-devel@nongnu.org; Thu, 07 Dec 2017 16:46:31 -0500 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:45556) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eN3zx-0006JR-97 for qemu-devel@nongnu.org; Thu, 07 Dec 2017 16:46:29 -0500 Received: by mail-pg0-x242.google.com with SMTP id m25so5377697pgv.12 for ; Thu, 07 Dec 2017 13:46:29 -0800 (PST) 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=WHrimqKmkjyLEmn1gNtbYkUZxVq1HyjZWMrrEDTcNTo=; b=r3sDHMMEGkTrX1ZIiggpcNqmw4GvsgMNBHzq7FynVANLU1UtsJUext7bsPIRbJLbCz RakA5FOcYgrzQne01p9w00FDMUlXMFYHjlxW/ytq5txXrVeQ/2o6I5/ifEpQViHuPYUh wFEbFxzkZdV5qGadlMWJ66W0gy5X8GrtTm7byhKDc0OrsVwMcCo85HE11XTyYyhq6w0u tJ5OVgTIQbrTLKbGnRGIJ62iky8Po8k1ZmcGgoBOej24+zMmcJVYVF0Q2XhReyzMBcsn xcwZuIxJ71GgsJYISlVwNUjfw8Vf24NtKAlo+qXV+EXpwZW+wfv9waDhMDGcNKLZYlkM 4syA== 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=WHrimqKmkjyLEmn1gNtbYkUZxVq1HyjZWMrrEDTcNTo=; b=SYrkvTkBkynSd94ZUSJ34bJEBMkMFdpkbS4EmL3mtUXxU/rs6dEGYHD5m83sq+fAyW 72RnvT2EPctx7QEw7Lw7PMlET7wgXy8f2yBofG1IoJc3jPmSzOqIq/7+HrcjTcXLBxI6 +Zv7fX3YV26joIlLV1LcqfYtSccsaOoCHVrXp4bpV6L7C3nnsAjRwh4b3ZzA2BCODING 6LJtMwsCvIqijyHdWUtJrPlwuCdSEee9A7tc8obIvGRPMnkKTXz9StGskg2yE9v8MP5m nqejYuN2XaKj6RlbMemM/ebiln/lew0THSzJK/m4USH+uYCjB9q/SnaEcOVgHO1p+bhd sSnw== X-Gm-Message-State: AJaThX4VWsmnSd7X0uRODCI5837VGQoOrVqcroq07WnN8X6cXtKZQ/Ll Ru4jPCQNy+vhkzEWzsxDIA+QubQ= X-Google-Smtp-Source: AGs4zMZZDZUSlMs/TP9MDmz7RFdX85/7yPyGrqrGdpgwnfcxsRqFa0ZCHRMGzaIRTDyraTxNvO0N4w== X-Received: by 10.99.152.68 with SMTP id l4mr27256195pgo.208.1512683188127; Thu, 07 Dec 2017 13:46:28 -0800 (PST) Received: from serve.minyard.net ([47.184.168.85]) by smtp.gmail.com with ESMTPSA id y5sm11252936pfd.63.2017.12.07.13.46.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Dec 2017 13:46:26 -0800 (PST) Received: from t430.minyard.net (t430m.minyard.net [192.168.27.3]) by serve.minyard.net (Postfix) with ESMTPA id E46A6901; Thu, 7 Dec 2017 15:46:23 -0600 (CST) Received: by t430.minyard.net (Postfix, from userid 1000) id DD0E130007F; Thu, 7 Dec 2017 15:46:22 -0600 (CST) From: minyard@acm.org To: qemu-devel@nongnu.org Date: Thu, 7 Dec 2017 15:46:09 -0600 Message-Id: <1512683181-8420-3-git-send-email-minyard@acm.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1512683181-8420-1-git-send-email-minyard@acm.org> References: <1512683181-8420-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:400e:c05::242 Subject: [Qemu-devel] [PATCH 02/14] 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 , Paolo Bonzini , "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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(-) diff --git a/hw/i2c/pm_smbus.c b/hw/i2c/pm_smbus.c index 92c3aebd..679edbc 100644 --- a/hw/i2c/pm_smbus.c +++ b/hw/i2c/pm_smbus.c @@ -115,10 +115,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 2d1b79a..4b0e264 100644 --- a/hw/i2c/smbus.c +++ b/hw/i2c/smbus.c @@ -301,33 +301,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; @@ -338,7 +347,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 544bbc1..f1b8078 100644 --- a/include/hw/i2c/smbus.h +++ b/include/hw/i2c/smbus.h @@ -73,9 +73,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(I2CBus *smbus, int nb_eeprom, const uint8_t *eeprom_spd, int size);