Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp40256imm; Mon, 4 Jun 2018 12:37:49 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJGNzq3NaQ3FA2cdnMh4fyfQKbrQ5i5ylozGouLKOys9lKj5FHlTRfOTecL/nHkKkN/fMVu X-Received: by 2002:a65:6645:: with SMTP id z5-v6mr18476837pgv.43.1528141069519; Mon, 04 Jun 2018 12:37:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528141069; cv=none; d=google.com; s=arc-20160816; b=vmKCB2eU1O2ufuRY6JW5u8i62rB5UKspEQT5Ptt/0923f1k6alSJ4LK5W2CYtx+x8F qqLddNXRQJpTbyIyLEv1C7qSqkr1Zs5zQnM+GkgZnF3jXGJwtWHmxMhIFJYqfA0gQAUO RBqV5N/YHURgbEol33kKEmgOCQxFYtjUxnCDhEDem17/Y6DVFQ68rAx5Bmh8Ovp/eshS tduIXFQyQMU5i8Ow2JAQ6JqrynoDOZzgik0KCbanMA0k818iPcmFdjlKwW6Vy2fmKJ+B NzIdVEsTNXRsNQAS/rkj7LbFRAVZu5/ta2sXtENgHaYQjg3vbOPaIIOrdqUWwlolr0hs 5MCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :from:references:cc:to:subject:arc-authentication-results; bh=ETRd6K3EuEzWxDdCWtucJszGdFeUEou4DTUFKIS/Tew=; b=ij1nXJvCEzl/ccqaQwBxr0b8cDNxRQhSLB5XSepDOaYfo3cqWT7/OJLLHDZIhc+zRP jLmBotLHUIR+gBNfhonHVyPUADHUVDg8gqMT5AYKxEsQGy2UD6jkm2e8DIJt/NUkoR7A LDFLKVqbXca4P4Pg0T5sIH4E55QJ+6nZ3qXZgkDyafOgnsO09tBFKCSq/S12c+MZowz+ IBFy9SFzYPfZZzrg9ilRt/K5nm+zKC+SIOTzSGkmBIAl1PgH3GPJkyVC+QrloJK93+su XEvpMdcCXt6RjORnuE0Cgg6m3UBS8Jqr/vB+UNu6Zc8qKUq6ZPrWk8U489QBczyfsUCZ +QKQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s187-v6si20657981pgc.447.2018.06.04.12.37.34; Mon, 04 Jun 2018 12:37:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751163AbeFDThL (ORCPT + 99 others); Mon, 4 Jun 2018 15:37:11 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:42248 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751010AbeFDThK (ORCPT ); Mon, 4 Jun 2018 15:37:10 -0400 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w54JYPLB145591 for ; Mon, 4 Jun 2018 15:37:09 -0400 Received: from e15.ny.us.ibm.com (e15.ny.us.ibm.com [129.33.205.205]) by mx0a-001b2d01.pphosted.com with ESMTP id 2jdbd18nmr-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 04 Jun 2018 15:37:09 -0400 Received: from localhost by e15.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 4 Jun 2018 15:37:08 -0400 Received: from b01cxnp22034.gho.pok.ibm.com (9.57.198.24) by e15.ny.us.ibm.com (146.89.104.202) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Mon, 4 Jun 2018 15:37:03 -0400 Received: from b01ledav005.gho.pok.ibm.com (b01ledav005.gho.pok.ibm.com [9.57.199.110]) by b01cxnp22034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w54Jb3ef5439946 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 4 Jun 2018 19:37:03 GMT Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1F277AE060; Mon, 4 Jun 2018 15:37:22 -0400 (EDT) Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E9A7DAE063; Mon, 4 Jun 2018 15:37:20 -0400 (EDT) Received: from [9.41.241.240] (unknown [9.41.241.240]) by b01ledav005.gho.pok.ibm.com (Postfix) with ESMTP; Mon, 4 Jun 2018 15:37:20 -0400 (EDT) Subject: Re: [PATCH v9 5/7] i2c: fsi: Add transfer implementation To: Andy Shevchenko Cc: linux-i2c , Linux Kernel Mailing List , devicetree , Wolfram Sang , Rob Herring , Benjamin Herrenschmidt , Joel Stanley , Mark Rutland , Greg Kroah-Hartman , Randy Dunlap References: <1528138850-18259-1-git-send-email-eajames@linux.vnet.ibm.com> <1528138850-18259-6-git-send-email-eajames@linux.vnet.ibm.com> From: Eddie James Date: Mon, 4 Jun 2018 14:37:01 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-TM-AS-GCONF: 00 x-cbid: 18060419-0068-0000-0000-0000030319A7 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009129; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000265; SDB=6.01042314; UDB=6.00533715; IPR=6.00821444; MB=3.00021461; MTD=3.00000008; XFM=3.00000015; UTC=2018-06-04 19:37:07 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18060419-0069-0000-0000-000044917957 Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-06-04_13:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1805220000 definitions=main-1806040225 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 06/04/2018 02:14 PM, Andy Shevchenko wrote: > On Mon, Jun 4, 2018 at 10:00 PM, Eddie James wrote: >> Execute I2C transfers from the FSI-attached I2C master. Use polling >> instead of interrupts as we have no hardware IRQ over FSI. >> >> Signed-off-by: Eddie James >> --- >> drivers/i2c/busses/i2c-fsi.c | 195 ++++++++++++++++++++++++++++++++++++++++++- >> 1 file changed, 193 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c >> index 101b9c5..4e5964e 100644 >> --- a/drivers/i2c/busses/i2c-fsi.c >> +++ b/drivers/i2c/busses/i2c-fsi.c >> @@ -150,6 +150,7 @@ struct fsi_i2c_port { >> struct i2c_adapter adapter; >> struct fsi_i2c_master *master; >> u16 port; >> + u16 xfrd; >> }; >> >> static int fsi_i2c_read_reg(struct fsi_device *fsi, unsigned int reg, >> @@ -225,6 +226,99 @@ static int fsi_i2c_set_port(struct fsi_i2c_port *port) >> return fsi_i2c_write_reg(fsi, I2C_FSI_RESET_ERR, &dummy); >> } >> >> +static int fsi_i2c_start(struct fsi_i2c_port *port, struct i2c_msg *msg, >> + bool stop) >> +{ >> + struct fsi_i2c_master *i2c = port->master; >> + u32 cmd = I2C_CMD_WITH_START | I2C_CMD_WITH_ADDR; >> + >> + port->xfrd = 0; >> + >> + if (msg->flags & I2C_M_RD) >> + cmd |= I2C_CMD_READ; >> + >> + if (stop || msg->flags & I2C_M_STOP) >> + cmd |= I2C_CMD_WITH_STOP; >> + >> + cmd |= FIELD_PREP(I2C_CMD_ADDR, msg->addr >> 1); > Did you consider to unify READ + ADDR as ADDR_8BIT? Yes, however the result from the i2c_8bit_addr_from_msg function is not equivalent. This hardware expects the 8-bit address to be concatenated with the read/not write bit. Also the function returns a u8, which won't work for addresses with bit 7 set, since it left-shifts by one. > >> + cmd |= FIELD_PREP(I2C_CMD_LEN, msg->len); >> + >> + return fsi_i2c_write_reg(i2c->fsi, I2C_FSI_CMD, &cmd); >> +} >> + >> +static int fsi_i2c_get_op_bytes(int op_bytes) >> +{ >> + /* fsi is limited to max 4 byte aligned ops */ >> + if (op_bytes > 4) >> + return 4; >> + else if (op_bytes == 3) > Since you dropped one of 'else', another also is not needed. True. > >> + return 2; >> + return op_bytes; >> +} >> + >> +static int fsi_i2c_write_fifo(struct fsi_i2c_port *port, struct i2c_msg *msg, >> + u8 fifo_count) >> +{ >> + int write; >> + int rc; >> + struct fsi_i2c_master *i2c = port->master; >> + int bytes_to_write = i2c->fifo_size - fifo_count; >> + int bytes_remaining = msg->len - port->xfrd; >> + >> + bytes_to_write = min(bytes_to_write, bytes_remaining); >> + >> + while (bytes_to_write) { >> + write = fsi_i2c_get_op_bytes(bytes_to_write); >> + >> + rc = fsi_device_write(i2c->fsi, I2C_FSI_FIFO, >> + &msg->buf[port->xfrd], write); >> + if (rc) >> + return rc; >> + >> + port->xfrd += write; >> + bytes_to_write -= write; >> + } > Just to be sure, if by some reason write == 0 in the loop for a while, > would it become infinite loop? It's not possible that write == 0. Same for read below. Thanks, Eddie > >> + >> + return 0; >> +} >> + >> +static int fsi_i2c_read_fifo(struct fsi_i2c_port *port, struct i2c_msg *msg, >> + u8 fifo_count) >> +{ >> + int read; >> + int rc; >> + struct fsi_i2c_master *i2c = port->master; >> + int bytes_to_read; >> + int xfr_remaining = msg->len - port->xfrd; >> + u32 dummy; >> + >> + bytes_to_read = min_t(int, fifo_count, xfr_remaining); >> + >> + while (bytes_to_read) { >> + read = fsi_i2c_get_op_bytes(bytes_to_read); >> + >> + if (xfr_remaining) { >> + rc = fsi_device_read(i2c->fsi, I2C_FSI_FIFO, >> + &msg->buf[port->xfrd], read); >> + if (rc) >> + return rc; >> + >> + port->xfrd += read; >> + xfr_remaining -= read; >> + } else { >> + /* no more buffer but data in fifo, need to clear it */ >> + rc = fsi_device_read(i2c->fsi, I2C_FSI_FIFO, &dummy, >> + read); >> + if (rc) >> + return rc; >> + } >> + >> + bytes_to_read -= read; >> + } > Ditto for read == 0. > >> + >> + return 0; >> +} >> + >> static int fsi_i2c_reset_bus(struct fsi_i2c_master *i2c) >> { >> int i, rc; >> @@ -388,17 +482,114 @@ static int fsi_i2c_abort(struct fsi_i2c_port *port, u32 status) >> return -ETIMEDOUT; >> } >> >> +static int fsi_i2c_handle_status(struct fsi_i2c_port *port, >> + struct i2c_msg *msg, u32 status) >> +{ >> + int rc; >> + u8 fifo_count; >> + >> + if (status & I2C_STAT_ERR) { >> + rc = fsi_i2c_abort(port, status); >> + if (rc) >> + return rc; >> + >> + if (status & I2C_STAT_INV_CMD) >> + return -EINVAL; >> + >> + if (status & (I2C_STAT_PARITY | I2C_STAT_BE_OVERRUN | >> + I2C_STAT_BE_ACCESS)) >> + return -EPROTO; >> + >> + if (status & I2C_STAT_NACK) >> + return -ENXIO; >> + >> + if (status & I2C_STAT_LOST_ARB) >> + return -EAGAIN; >> + >> + if (status & I2C_STAT_STOP_ERR) >> + return -EBADMSG; >> + >> + return -EIO; >> + } >> + >> + if (status & I2C_STAT_DAT_REQ) { >> + fifo_count = FIELD_GET(I2C_STAT_FIFO_COUNT, status); >> + >> + if (msg->flags & I2C_M_RD) >> + return fsi_i2c_read_fifo(port, msg, fifo_count); >> + >> + return fsi_i2c_write_fifo(port, msg, fifo_count); >> + } >> + >> + if (status & I2C_STAT_CMD_COMP) { >> + if (port->xfrd < msg->len) >> + return -ENODATA; >> + >> + return msg->len; >> + } >> + >> + return 0; >> +} >> + >> +static int fsi_i2c_wait(struct fsi_i2c_port *port, struct i2c_msg *msg, >> + unsigned long timeout) >> +{ >> + u32 status = 0; >> + int rc; >> + unsigned long start = jiffies; >> + >> + do { >> + rc = fsi_i2c_read_reg(port->master->fsi, I2C_FSI_STAT, >> + &status); >> + if (rc) >> + return rc; >> + >> + if (status & I2C_STAT_ANY_RESP) { >> + rc = fsi_i2c_handle_status(port, msg, status); >> + if (rc < 0) >> + return rc; >> + >> + /* cmd complete and all data xfrd */ >> + if (rc == msg->len) >> + return 0; >> + >> + /* need to xfr more data, but maybe don't need wait */ >> + continue; >> + } >> + >> + usleep_range(I2C_CMD_SLEEP_MIN_US, I2C_CMD_SLEEP_MAX_US); >> + } while (time_after(start + timeout, jiffies)); >> + >> + return -ETIMEDOUT; >> +} >> + >> static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, >> int num) >> { >> - int rc; >> + int i, rc; >> + unsigned long start_time; >> struct fsi_i2c_port *port = adap->algo_data; >> + struct i2c_msg *msg; >> >> rc = fsi_i2c_set_port(port); >> if (rc) >> return rc; >> >> - return -EOPNOTSUPP; >> + for (i = 0; i < num; i++) { >> + msg = msgs + i; >> + start_time = jiffies; >> + >> + rc = fsi_i2c_start(port, msg, i == num - 1); >> + if (rc) >> + return rc; >> + >> + rc = fsi_i2c_wait(port, msg, >> + adap->timeout - (jiffies - start_time)); >> + if (rc) >> + return rc; >> + } >> + >> + return 0; >> } >> >> static u32 fsi_i2c_functionality(struct i2c_adapter *adap) >> -- >> 1.8.3.1 >> > >