Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5843288rwd; Wed, 24 May 2023 07:30:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ74HfcsZFWQtu3V2e34DvM1EoJ18C7aja4Yx4MFwrmAZ+IUdlpFSltA+fMDmn5/ICI+WHUG X-Received: by 2002:a05:6a20:4289:b0:10d:3134:10d7 with SMTP id o9-20020a056a20428900b0010d313410d7mr2829528pzj.27.1684938607611; Wed, 24 May 2023 07:30:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684938607; cv=none; d=google.com; s=arc-20160816; b=JJGUAZRtk8OKc2QTT6q9ACiFZW0CGpdyyKckffzkg32vXeQ5xy91advJb330y5tXaL vbmfoWo39VNyWHzJZ4PabnvbjVoYgql7ylQQY/GmVRTgqI5u2O+/p3lGiMuiq1GDFln/ 8Hi237rtbn2XAqAgzjATfN8JuuSQkmR6he/RZ63j5A6ffQDiMjqkntkazoM+wlGxaMuM IZ9dwOomV95LmZeGDNasr4Urxz9dhPt+CGpqDh3+REiukr9tzKbZErRUnBsgfL3kCD/Z kk9KqAu73rrSJG/iICAeyMXmYrBGfkbMNknvsxtsMQIn6r4ZNg87uVbEAd498yYv8E9q 2qJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=7E4HH+k3O26McCEUuSgstdKf5ibwMRjX+HV0er0kufw=; b=SVaneVRu0zatuesjzL9maJGr5hCzytZ7SHXpK3ap9RfoY7Zq3A6eJmLl/+8Yf5UtTs zkQFi++Uupn6w5ftbYksOm8hZY9Aux3urdNMKsaTpp6hAgB+d6s7/DOQc9+IzYQavH2n eP8zqC51b+ybyaWp/A7exnSTOcYxiQuFaZLS17fbYVV7lmDuAqFEdrUzW/koRnjSJ2Ga 9Tw7LwA0FAWBusPhEJ2MXcFy11+4mAsQD3C9o5Yf5BXk6etJYSvVBaCz23Qup3tOABk7 nbVH9z1frTxAYXYSv26vnKYjQ0JeMMyLXeo8h0SJODWsDFnY+YPoSdTi1s3JkRTT2r1e sjlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KddEqw3j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b207-20020a621bd8000000b0064f6c2c05bcsi1104332pfb.81.2023.05.24.07.29.53; Wed, 24 May 2023 07:30:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KddEqw3j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235752AbjEXOFx (ORCPT + 99 others); Wed, 24 May 2023 10:05:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231573AbjEXOFw (ORCPT ); Wed, 24 May 2023 10:05:52 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE63AE7; Wed, 24 May 2023 07:05:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684937150; x=1716473150; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=O3ky+HRl+9kQSjMWZ81b6b+99pfCEki8XfJm2dVk39Q=; b=KddEqw3jsh3sZRRg7pkEGGar8EBvrLlLWQisgdVO2V1tHim9WJEHQvOh VuD0KyNuou7WpLZm0PvwppmRlrwGewbAowo99OQvpebch3/lacWkaZsbq vGE9cGy8e5DkC5PJdjTRFFn7mS5RGMgUBBaCi9tv2OMVVr0N9dg+6JQWk p38KAW34jyjdaXn7opVThW7unAIbFsdhev5QLcVDQM9raGX662c9tMnXR ZJwQfTw7Zox21go+akSYblxepKW9p4+Mzj1Ujn61r2z2uKnvXGg3bLTVC NvwF4qVvJFjk3VOw8MDenc2aGqhTgoF87teoYgn1bsRyC9Vy35qSMq1th A==; X-IronPort-AV: E=McAfee;i="6600,9927,10720"; a="351084598" X-IronPort-AV: E=Sophos;i="6.00,189,1681196400"; d="scan'208";a="351084598" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 May 2023 07:05:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10720"; a="951031302" X-IronPort-AV: E=Sophos;i="6.00,189,1681196400"; d="scan'208";a="951031302" Received: from mylly.fi.intel.com (HELO [10.237.72.160]) ([10.237.72.160]) by fmsmga006.fm.intel.com with ESMTP; 24 May 2023 07:05:17 -0700 Message-ID: <49bce4e6-5757-7afe-2173-681305f1be36@linux.intel.com> Date: Wed, 24 May 2023 17:05:16 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.10.0 Subject: Re: [PATCH v1] i2c: designware: fix idx_write_cnt in read loop Content-Language: en-US To: David Zheng , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Cc: andriy.shevchenko@linux.intel.com, mika.westerberg@linux.intel.com, jsd@semihalf.com References: From: Jarkko Nikula In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/23/23 19:46, David Zheng wrote: > With IC_INTR_RX_FULL slave interrupt handler reads data in a loop until > RX FIFO is empty. When testing with the slave-eeprom, each transaction > has 2 bytes for address/index and 1 byte for value, the address byte > can be written as data byte due to dropping STOP condition. > > In the test below, the master continuously writes to the slave, first 2 > bytes are index, 3rd byte is value and follow by a STOP condition. > > i2c_write: i2c-3 #0 a=04b f=0000 l=3 [00-D1-D1] > i2c_write: i2c-3 #0 a=04b f=0000 l=3 [00-D2-D2] > i2c_write: i2c-3 #0 a=04b f=0000 l=3 [00-D3-D3] > > Upon receiving STOP condition slave eeprom would reset `idx_write_cnt` so > next 2 bytes can be treated as buffer index for upcoming transaction. > Supposedly the slave eeprom buffer would be written as > > EEPROM[0x00D1] = 0xD1 > EEPROM[0x00D2] = 0xD2 > EEPROM[0x00D3] = 0xD3 > > When CPU load is high the slave irq handler may not read fast enough, > the interrupt status can be seen as 0x204 with both DW_IC_INTR_STOP_DET > (0x200) and DW_IC_INTR_RX_FULL (0x4) bits. The slave device may see > the transactions below. > > 0x1 STATUS SLAVE_ACTIVITY=0x1 : RAW_INTR_STAT=0x1594 : INTR_STAT=0x4 > 0x1 STATUS SLAVE_ACTIVITY=0x1 : RAW_INTR_STAT=0x1594 : INTR_STAT=0x4 > 0x1 STATUS SLAVE_ACTIVITY=0x1 : RAW_INTR_STAT=0x1594 : INTR_STAT=0x4 > 0x1 STATUS SLAVE_ACTIVITY=0x1 : RAW_INTR_STAT=0x1794 : INTR_STAT=0x204 > 0x1 STATUS SLAVE_ACTIVITY=0x0 : RAW_INTR_STAT=0x1790 : INTR_STAT=0x200 > 0x1 STATUS SLAVE_ACTIVITY=0x1 : RAW_INTR_STAT=0x1594 : INTR_STAT=0x4 > 0x1 STATUS SLAVE_ACTIVITY=0x1 : RAW_INTR_STAT=0x1594 : INTR_STAT=0x4 > 0x1 STATUS SLAVE_ACTIVITY=0x1 : RAW_INTR_STAT=0x1594 : INTR_STAT=0x4 > > After `D1` is received, read loop continues to read `00` which is the > first bype of next index. Since STOP condition is ignored by the loop, > eeprom buffer index increased to `D2` and `00` is written as value. > > So the slave eeprom buffer becomes > > EEPROM[0x00D1] = 0xD1 > EEPROM[0x00D2] = 0x00 > EEPROM[0x00D3] = 0xD3 > > The fix is to use `FIRST_DATA_BYTE` (bit 11) in `IC_DATA_CMD` to split > the transactions. The first index byte in this case would have bit 11 > set. Check this indication to inject I2C_SLAVE_WRITE_REQUESTED event > which will reset `idx_write_cnt` in slave eeprom. > > Signed-off-by: David Zheng > --- > drivers/i2c/busses/i2c-designware-core.h | 2 ++ > drivers/i2c/busses/i2c-designware-slave.c | 4 ++++ > 2 files changed, 6 insertions(+) > > diff --git a/drivers/i2c/busses/i2c-designware-core.h b/drivers/i2c/busses/i2c-designware-core.h > index c5d87aae39c6..8b85147bd518 100644 > --- a/drivers/i2c/busses/i2c-designware-core.h > +++ b/drivers/i2c/busses/i2c-designware-core.h > @@ -123,6 +123,8 @@ > #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3)) > #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2) > > +#define DW_IC_DATA_CMD_FIRST_DATA_BYTE BIT(11) > + Sorry, didn't notice earlier. Could you put this near to DW_IC_DATA_CMD_DAT define.