From patchwork Thu Feb 28 18:16:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Minyard X-Patchwork-Id: 159409 Delivered-To: patch@linaro.org Received: by 2002:ac9:18c7:0:0:0:0:0 with SMTP id i7csp979228oce; Thu, 28 Feb 2019 10:23:50 -0800 (PST) X-Google-Smtp-Source: APXvYqwHd4EodHy0FVLW58UTYtc4S31RdrNu63VxR/HQX/vSmmarENB5W23vsxfk3Ze/Rf5F1mU+ X-Received: by 2002:a25:57c2:: with SMTP id l185mr700208ybb.161.1551378230582; Thu, 28 Feb 2019 10:23:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551378230; cv=none; d=google.com; s=arc-20160816; b=RpYL5+NuHyHYN6KT05bOTsyhhlYiRSLcDoZUz96zsD2ekFAqMJpcQV1pZAo3quSGmq IbdeG7l9DPK3Ne2IHHhj+1O+cHQwzXg7KyZ8K2viKueJYBzruxZY4CjeTtFIG5C5v2Ly JTrFSfgPscMiFCPBLJGXjc33qKbLSAlFHSBZ6a0I3VVSazAj9CeJ7dV0hMDg4ITeuaQK Rf80R+Ge/YfbzrrHhPNL/n3WSrj1NeKBBBH8fMBoJT49Jdrlaw72Nbpik/7yySTHvzS3 oSTEg5gQHNVGDQZ/tre/AxEjndkJ2CRAseODZMgXEH5/rImTCK9hAujM/KfBjcLM1XAQ dA4w== 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; bh=kmGuLPE0xd0NHZLHoAWNBJlvkfppEobdtKRTK+UBiaQ=; b=t5i0xeIOa+Muj0XhsHgwmUnEIntm1gINvPADOu/Za51+vR0eYw+jLkHAAOVywY75Nd VoNC9SqcfnV09IIQL0sGnJsaaQfSPNut0Y5yq6sWmAKpOLkblftqdpQawWUuyolHRH3j jvpwmK5BeT5VrTvKgi9e0g+VVrRLRZDELcgKwOK/AMr9corWO+ozxYjPvUFeS7IlRMyd COwvq7JqYkc6ktL9D8m/RrhuxwBV+TLZ1uqyw4BeyF0N1IWxiiAvLUXJU0qsnAgRBzQ0 Buuu6iKmhvp6gMLbQdoKhMNlQRPL1bAlxQsHm5Awd7HaAK2fSabxrjkfzhSpTJxzmZzb AztQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=r1yae0yL; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id p22si8979571yba.370.2019.02.28.10.23.50 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 28 Feb 2019 10:23:50 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=r1yae0yL; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Received: from localhost ([127.0.0.1]:44557 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gzQLW-0003pf-1u for patch@linaro.org; Thu, 28 Feb 2019 13:23:50 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46636) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gzQFe-0000VN-BM for qemu-devel@nongnu.org; Thu, 28 Feb 2019 13:17:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gzQFV-0002Hp-Gd for qemu-devel@nongnu.org; Thu, 28 Feb 2019 13:17:44 -0500 Received: from mail-ot1-x343.google.com ([2607:f8b0:4864:20::343]:35200) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gzQFV-0002AK-6O for qemu-devel@nongnu.org; Thu, 28 Feb 2019 13:17:37 -0500 Received: by mail-ot1-x343.google.com with SMTP id z19so18560171otm.2 for ; Thu, 28 Feb 2019 10:17:19 -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=kmGuLPE0xd0NHZLHoAWNBJlvkfppEobdtKRTK+UBiaQ=; b=r1yae0yLZfY9IS0XWH4WnTI+QuskbL6m/NJI0S/I6MzuqrT7H2BNxCr+KyveGWx+3V YxB0IzCH0bNMS+K9rzsJBbcpOLtGpiVsg/c3IgcaeO72FppgYw1NaU8Z2oahhwfrHdJM A3GX145NTeQeOD95EfAfMeJlNv1pA2XYlpDxHW+Ll7fsqHn5FT77CXRNmI8OWhS06kcU dO5fhGj2lLpwX2VW0dkRptAjVWCAzx305gPdG283G3U2IEifLYc9yiQgeDMWCLlPoyAK UlyZgaKWzKcHOV5aDXQPogb1yjHK+mYE+gF7eP98BtQQIij9TgDiuNKLH8C/Nq8dMr+t wQkA== 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=kmGuLPE0xd0NHZLHoAWNBJlvkfppEobdtKRTK+UBiaQ=; b=RQSsK0fS4FUdBR7bMOWK5l4fJBSZIdorOMLIO4jhKDhgBFIJKxrO65DEDdV6UCE8jg fLSEPDxfSWPzhKMvFshwvWWvoR4nDu1DoUuCB82PECZCtNCdufzEttvLWAl3xnHDXCFV Vz7Ep3cBUobma+jPzXOPuktq6+7iKgNjEyHjmRxROb5vfettuqUcnXUMw6LPfZoSyckG 2M2LhQW/5/uOChRbEWdcZ5m3I1OTSjuvQS2yBaElQsyz5I8YkNcwqTfPkqr4DO9eQTns WwG72NHTM0Y9eW6grIcbVlodkJ77Jlv1RhVBMihzHxiUehpXahDU+XbEIYCFyxowGePb IbaQ== X-Gm-Message-State: APjAAAX1R4+HRlFWGPJN9LvefpOgNm/i0f7HRB2XZJt8A4y5bU7/oI8y l1u+FCXIAzCZVLzl14xqNg== X-Received: by 2002:a05:6830:1458:: with SMTP id w24mr603762otp.303.1551377838883; Thu, 28 Feb 2019 10:17:18 -0800 (PST) Received: from serve.minyard.net (serve.minyard.net. [2001:470:b8f6:1b::1]) by smtp.gmail.com with ESMTPSA id g2sm8073618otq.24.2019.02.28.10.17.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 28 Feb 2019 10:17:16 -0800 (PST) Received: from t430.minyard.net (unknown [IPv6:2001:470:b8f6:1b:b89e:c7bf:12ab:ad14]) by serve.minyard.net (Postfix) with ESMTPA id D9E9C1808F6; Thu, 28 Feb 2019 18:17:13 +0000 (UTC) Received: by t430.minyard.net (Postfix, from userid 1000) id A9082302AF7; Thu, 28 Feb 2019 12:17:13 -0600 (CST) From: minyard@acm.org To: Peter Maydell Date: Thu, 28 Feb 2019 12:16:57 -0600 Message-Id: <20190228181710.2477-7-minyard@acm.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190228181710.2477-1-minyard@acm.org> References: <20190228181710.2477-1-minyard@acm.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::343 Subject: [Qemu-devel] [PULL 06/19] i2c:smbus: Simplify write operation 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 , QEMU Developers , minyard@acm.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Corey Minyard There were two different write functions and the SMBus code kept track of the command. Keeping track of the command wasn't useful, in fact it wasn't quite correct for the eeprom_smbus code. And there is no need for two write functions. Just have one write function and the first byte in the buffer is the command. Signed-off-by: Corey Minyard --- hw/i2c/smbus_eeprom.c | 47 ++++++++++++------------------------ hw/i2c/smbus_slave.c | 25 ++++--------------- include/hw/i2c/smbus_slave.h | 21 ++++++++++------ 3 files changed, 34 insertions(+), 59 deletions(-) -- 2.17.1 diff --git a/hw/i2c/smbus_eeprom.c b/hw/i2c/smbus_eeprom.c index c8f6f4b442..7fbb5ca27f 100644 --- a/hw/i2c/smbus_eeprom.c +++ b/hw/i2c/smbus_eeprom.c @@ -45,16 +45,6 @@ static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read) #endif } -static void eeprom_send_byte(SMBusDevice *dev, uint8_t val) -{ - SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; -#ifdef DEBUG - printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", - dev->i2c.address, val); -#endif - eeprom->offset = val; -} - static uint8_t eeprom_receive_byte(SMBusDevice *dev) { SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; @@ -67,34 +57,30 @@ static uint8_t eeprom_receive_byte(SMBusDevice *dev) return val; } -static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len) +static int eeprom_write_data(SMBusDevice *dev, uint8_t *buf, uint8_t len) { SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; - int n; + uint8_t *data = eeprom->data; + #ifdef DEBUG printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", - dev->i2c.address, cmd, buf[0]); + dev->i2c.address, buf[0], buf[1]); #endif - /* A page write operation is not a valid SMBus command. - It is a block write without a length byte. Fortunately we - get the full block anyway. */ - /* TODO: Should this set the current location? */ - if (cmd + len > 256) - n = 256 - cmd; - else - n = len; - memcpy(eeprom->data + cmd, buf, n); - len -= n; - if (len) - memcpy(eeprom->data, buf + n, len); + /* len is guaranteed to be > 0 */ + eeprom->offset = buf[0]; + buf++; + len--; + + for (; len > 0; len--) { + data[eeprom->offset] = *buf++; + eeprom->offset = (eeprom->offset + 1) % 256; + } + + return 0; } -static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n) +static uint8_t eeprom_read_data(SMBusDevice *dev, int n) { - SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; - /* If this is the first byte then set the current position. */ - if (n == 0) - eeprom->offset = cmd; /* As with writes, we implement block reads without the SMBus length byte. */ return eeprom_receive_byte(dev); @@ -119,7 +105,6 @@ static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data) dc->realize = smbus_eeprom_realize; sc->quick_cmd = eeprom_quick_cmd; - sc->send_byte = eeprom_send_byte; sc->receive_byte = eeprom_receive_byte; sc->write_data = eeprom_write_data; sc->read_data = eeprom_read_data; diff --git a/hw/i2c/smbus_slave.c b/hw/i2c/smbus_slave.c index 6a89a286e3..92c7a5086c 100644 --- a/hw/i2c/smbus_slave.c +++ b/hw/i2c/smbus_slave.c @@ -54,18 +54,9 @@ static void smbus_do_write(SMBusDevice *dev) { SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev); - if (dev->data_len == 1) { - DPRINTF("Send Byte\n"); - if (sc->send_byte) { - sc->send_byte(dev, dev->data_buf[0]); - } - } else { - dev->command = dev->data_buf[0]; - DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1); - if (sc->write_data) { - sc->write_data(dev, dev->command, dev->data_buf + 1, - dev->data_len - 1); - } + DPRINTF("Command %d len %d\n", dev->data_buf[0], dev->data_len); + if (sc->write_data) { + sc->write_data(dev, dev->data_buf, dev->data_len); } } @@ -98,13 +89,7 @@ static int smbus_i2c_event(I2CSlave *s, enum i2c_event event) BADF("Read after write with no data\n"); dev->mode = SMBUS_CONFUSED; } else { - if (dev->data_len > 1) { - smbus_do_write(dev); - } else { - dev->command = dev->data_buf[0]; - DPRINTF("%02x: Command %d\n", dev->i2c.address, - dev->command); - } + smbus_do_write(dev); DPRINTF("Read mode\n"); dev->data_len = 0; dev->mode = SMBUS_READ_DATA; @@ -177,7 +162,7 @@ static uint8_t smbus_i2c_recv(I2CSlave *s) break; case SMBUS_READ_DATA: if (sc->read_data) { - ret = sc->read_data(dev, dev->command, dev->data_len); + ret = sc->read_data(dev, dev->data_len); dev->data_len++; } else { ret = 0; diff --git a/include/hw/i2c/smbus_slave.h b/include/hw/i2c/smbus_slave.h index 5ef1c72ad0..fa92201ec6 100644 --- a/include/hw/i2c/smbus_slave.h +++ b/include/hw/i2c/smbus_slave.h @@ -46,18 +46,24 @@ typedef struct SMBusDeviceClass * This may be NULL, quick commands are ignore in that case. */ void (*quick_cmd)(SMBusDevice *dev, uint8_t read); - void (*send_byte)(SMBusDevice *dev, uint8_t val); + uint8_t (*receive_byte)(SMBusDevice *dev); - /* We can't distinguish between a word write and a block write with - length 1, so pass the whole data block including the length byte - (if present). The device is responsible figuring out what type of - command this is. */ - void (*write_data)(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len); + + /* + * We can't distinguish between a word write and a block write with + * length 1, so pass the whole data block including the length byte + * (if present). The device is responsible figuring out what type of + * command this is. + * This may be NULL if no data is written to the device. Writes + * will be ignore in that case. + */ + int (*write_data)(SMBusDevice *dev, uint8_t *buf, uint8_t len); + /* Likewise we can't distinguish between different reads, or even know the length of the read until the read is complete, so read data a byte at a time. The device is responsible for adding the length byte on block reads. */ - uint8_t (*read_data)(SMBusDevice *dev, uint8_t cmd, int n); + uint8_t (*read_data)(SMBusDevice *dev, int n); } SMBusDeviceClass; struct SMBusDevice { @@ -68,7 +74,6 @@ struct SMBusDevice { int mode; int data_len; uint8_t data_buf[34]; /* command + len + 32 bytes of data. */ - uint8_t command; }; #endif