Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp960103pxk; Fri, 25 Sep 2020 02:27:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwylIm47ff839vIc3TcaHv9tGWIlWZEpPnC1iaJt2DmlY+n+V2ez5XnCOkbLj5I1qwuiAcN X-Received: by 2002:a50:bb65:: with SMTP id y92mr278406ede.53.1601026066118; Fri, 25 Sep 2020 02:27:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601026066; cv=none; d=google.com; s=arc-20160816; b=aMxkS/XxWOWni6bZQ1XZU1nk74xTDq+H8nLES8rrDYYy5oPTTbIdLPnGbleL+MEl3B uMkqoLrXJ4AQn8FGkMdi6F/k4LzPKtxRFG/NrxU7NcoJUMNm7hpEtBcF+q89GKvPhWAK wlOkRZ05Rdzv07G9DaRb+PLH3aHo2DYiqgOcrl6S4TcrLHo0ID8s1HbuzuZoR4wz8aI2 pC+LSfvmFNP340YW8pp5KCn554VROCvdPjMt90QVZieiIPuXduysNLZCzCrHKv/M5A9P 2Q/WNEJwisxmBv4kY9of6jaChfdxw3T4NlbI8QCdXYCAzzfO3f2zA9wOect8REQ3ijdc pl5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=xwgHnfwJO4p3e2k/8UmICPhqPe1pJTwIO3qj/AAMTOs=; b=DpF0nw7AjGZkr9QsKwA03wd7Gy+nO5lEfWMJvSt4tBV2Y+b0HBC4GW/hrLRhnfb5Gt rEq0zck+vF/KJcCREm5YIApS1Fol8P2EOwa0Jgv9bnUf1nFqtHE+uKhuZV2LMWZtyPBw AN9SuEHHzsyHP4UqKlwxtXGcNVZmn8jlwpDcdxjHRPUj5H1bl/RWMaJd6dAAooVwe134 ruPql80YAX1NdN8f7dXCrsF8dFGbg+IY3U1+JREb/e8QsTp4SIZTn1aebdvLYpJrUz/D eN61wXwn5dCu6hir7twN1bkmuxcPwXVND0jzO40wvhwsnm6Af83u11a0+5pbDjj8gB44 7Bug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NCvFcGrJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b20si1344407eds.592.2020.09.25.02.27.21; Fri, 25 Sep 2020 02:27:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NCvFcGrJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727817AbgIYJZA (ORCPT + 99 others); Fri, 25 Sep 2020 05:25:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727795AbgIYJZA (ORCPT ); Fri, 25 Sep 2020 05:25:00 -0400 Received: from mail-ua1-x944.google.com (mail-ua1-x944.google.com [IPv6:2607:f8b0:4864:20::944]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52F33C0613D4 for ; Fri, 25 Sep 2020 02:25:00 -0700 (PDT) Received: by mail-ua1-x944.google.com with SMTP id o64so721601uao.1 for ; Fri, 25 Sep 2020 02:25:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xwgHnfwJO4p3e2k/8UmICPhqPe1pJTwIO3qj/AAMTOs=; b=NCvFcGrJ4lAMHxPsuRNAM6CQqIi8DN2DhKWzJy934kPHwe8Q25N/LRBK3oE3ruxgdO jMCZ3ABixw8fJ8qWStplc3e7oLYbrPCm7yWKBM1VkbU5poVN6p97MMqg/MyPSLbb72Sj cZ6nhCwkAnlntTsedZEux/9pumM7CSlH2r0Sc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xwgHnfwJO4p3e2k/8UmICPhqPe1pJTwIO3qj/AAMTOs=; b=qlw+tjSVQjUIX9Iq88UM+Nw3u7zmTG2i9KeDn0mHrproZEO3rVaETLD2qZc4eGWYwT H/gMs6lLicP6SOnMrKKV5yDyWbVcXB+eS3obQJZW1OC/6NBDRLFBzrIb9b9pYVKyFtY7 xfX/rzz35OLLYacStALhbreysKPlYg9qrbjTgiimqMs7CLuXXmm4w4ImccuxvF+se1cV wZerqhhKV+GOC3qm2q2Mk/olIcWBSzH1Ld8iGxSOof1Iuz6a4WXHdo4gjga1NmcwOTHb mVzQqcDIKrYgcMBQPo2xaSerQRFlO16RUU2NKQShXoYCIs5tA9QigVhHZ2V5eeVhswrX mjkw== X-Gm-Message-State: AOAM5324JDqY6cvu4mTTrf0g0L0Qqsx/QdBgAIpdIzzoJsKXLatfQLVm 0vffa1vN43kNsfG9qw2LpMMEfvTaivw1QzO8Yof0YA== X-Received: by 2002:ab0:2404:: with SMTP id f4mr1874548uan.108.1601025899341; Fri, 25 Sep 2020 02:24:59 -0700 (PDT) MIME-Version: 1.0 References: <20200925065418.1077472-1-ikjn@chromium.org> <20200925145255.v3.3.I7a3fc5678a81654574e8852d920db94bcc4d3eb8@changeid> In-Reply-To: From: Ikjoon Jang Date: Fri, 25 Sep 2020 17:24:48 +0800 Message-ID: Subject: Re: [PATCH v3 3/6] spi: spi-mtk-nor: support 7 bytes transfer of generic spi To: Chuanhong Guo Cc: Rob Herring , Mark Brown , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , linux-spi@vger.kernel.org, linux-mtd@lists.infradead.org, Matthias Brugger , "moderated list:ARM/Mediatek SoC support" , open list , "moderated list:ARM/Mediatek SoC support" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 25, 2020 at 3:47 PM Chuanhong Guo wrote: > > Hi! > > On Fri, Sep 25, 2020 at 2:55 PM Ikjoon Jang wrote: > > > > When mtk-nor fallbacks to generic spi transfers, it can actually > > transfer up to 7 bytes. > > generic transfer_one_message should support full-duplex transfers, > not transfers with special format requirements. (e.g. here the last > byte is rx only.) These transfers with format requirements should > be implemented with spi-mem interface instead. yep, that's correct. > > > > > This patch fixes adjust_op_size() and supports_op() to explicitly > > check 7 bytes range and also fixes possible under/overflow conditions > > in register offsets calculation. > > > > Signed-off-by: Ikjoon Jang > > I was notified by Bayi about your discussion and sent some > patches yesterday for the same purpose. Whoops... > As transfer_one_message isn't the proper place to implement > this, maybe we could work on my version instead? > I didn't noticed that before, Sure, please go ahead, I'll follow up with your patch in v4. > > --- > > > > (no changes since v1) > > This should be "new patch" not "no changes" :P oops, it seems my script did something wrong. > > > > > > drivers/spi/spi-mtk-nor.c | 102 ++++++++++++++++++++++++++++---------- > > 1 file changed, 76 insertions(+), 26 deletions(-) > > > > diff --git a/drivers/spi/spi-mtk-nor.c b/drivers/spi/spi-mtk-nor.c > > index 0f7d4ec68730..e7719d249095 100644 > > --- a/drivers/spi/spi-mtk-nor.c > > +++ b/drivers/spi/spi-mtk-nor.c > > @@ -79,7 +79,11 @@ > > #define MTK_NOR_REG_DMA_DADR 0x720 > > #define MTK_NOR_REG_DMA_END_DADR 0x724 > > > > +/* maximum bytes of TX in PRG mode */ > > #define MTK_NOR_PRG_MAX_SIZE 6 > > +/* maximum bytes of TX + RX is 7, last 1 byte is always being sent as zero */ > > +#define MTK_NOR_PRG_MAX_CYCLES 7 > > + > > // Reading DMA src/dst addresses have to be 16-byte aligned > > #define MTK_NOR_DMA_ALIGN 16 > > #define MTK_NOR_DMA_ALIGN_MASK (MTK_NOR_DMA_ALIGN - 1) > > @@ -167,6 +171,24 @@ static bool mtk_nor_match_read(const struct spi_mem_op *op) > > return false; > > } > > > > +static bool mtk_nor_check_prg(const struct spi_mem_op *op) > > +{ > > + size_t len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; > > + > > + if (len > MTK_NOR_PRG_MAX_SIZE) > > + return false; > > + > > + if (!op->data.nbytes) > > + return true; > > + > > + if (op->data.dir == SPI_MEM_DATA_OUT) > > + return ((len + op->data.nbytes) <= MTK_NOR_PRG_MAX_SIZE); > > + else if (op->data.dir == SPI_MEM_DATA_IN) > > + return ((len + op->data.nbytes) <= MTK_NOR_PRG_MAX_CYCLES); > > + else > > + return true; > > +} > > + > > static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) > > { > > size_t len; > > @@ -195,10 +217,22 @@ static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) > > } > > } > > > > - len = MTK_NOR_PRG_MAX_SIZE - op->cmd.nbytes - op->addr.nbytes - > > - op->dummy.nbytes; > > - if (op->data.nbytes > len) > > - op->data.nbytes = len; > > + if (mtk_nor_check_prg(op)) > > + return 0; > > + > > + len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; > > + > > + if (op->data.dir == SPI_MEM_DATA_OUT) { > > + if (len == MTK_NOR_PRG_MAX_SIZE) > > + return -EINVAL; > > + op->data.nbytes = min_t(unsigned int, op->data.nbytes, > > + MTK_NOR_PRG_MAX_SIZE - len); > > + } else { > > + if (len == MTK_NOR_PRG_MAX_CYCLES) > > + return -EINVAL; > > + op->data.nbytes = min_t(unsigned int, op->data.nbytes, > > + MTK_NOR_PRG_MAX_CYCLES - len); > > + } > > > > return 0; > > } > > @@ -206,8 +240,6 @@ static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) > > static bool mtk_nor_supports_op(struct spi_mem *mem, > > const struct spi_mem_op *op) > > { > > - size_t len; > > - > > if (op->cmd.buswidth != 1) > > return false; > > > > @@ -223,12 +255,11 @@ static bool mtk_nor_supports_op(struct spi_mem *mem, > > (op->data.buswidth == 1); > > } > > > > - len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; > > - if ((len > MTK_NOR_PRG_MAX_SIZE) || > > - ((op->data.nbytes) && (len == MTK_NOR_PRG_MAX_SIZE))) > > + /* fallback to generic spi xfer */ > > + if (op->cmd.buswidth > 1 || op->addr.buswidth > 1 || op->data.buswidth > 1) > > return false; > > Rejecting an op in supports_op doesn't tell it to fall back to generic > spi transfer. > It instead tells caller to abort this transfer completely. > A fallback only happens when exec_op returns -ENOTSUPP. yep but I think that case always going PRG mode in exec_op() with the same condition? > This comment is incorrect. I'd put this buswidth checking in mtk_nor_check_prg > instead because mtk_nor_check_prg is checking whether an op is supported > by prg mode, thus it should reject ops with buswidth > 1. > > > > > - return true; > > + return mtk_nor_check_prg(op); > > } > > > > static void mtk_nor_setup_bus(struct mtk_nor *sp, const struct spi_mem_op *op) > > @@ -459,22 +490,36 @@ static int mtk_nor_transfer_one_message(struct spi_controller *master, > > int stat = 0; > > int reg_offset = MTK_NOR_REG_PRGDATA_MAX; > > void __iomem *reg; > > - const u8 *txbuf; > > - u8 *rxbuf; > > - int i; > > + int i, tx_len = 0, rx_len = 0; > > > > list_for_each_entry(t, &m->transfers, transfer_list) { > > - txbuf = t->tx_buf; > > - for (i = 0; i < t->len; i++, reg_offset--) { > > + const u8 *txbuf = t->tx_buf; > > + > > + if (!txbuf) { > > + rx_len += t->len; > > + continue; > > + } > > + > > + if (rx_len) { > > + stat = -EPROTO; > > + goto msg_done; > > + } > > NACK. you are unnecessarily rejecting possible transfers. yep, ditto > > > + > > + for (i = 0; i < t->len && reg_offset >= 0; i++, reg_offset--) { > > reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset); > > - if (txbuf) > > - writeb(txbuf[i], reg); > > - else > > - writeb(0, reg); > > + writeb(txbuf[i], reg); > > + tx_len++; > > According to SPI standard, during a rx transfer, tx should be kept low. > These PROGDATA registers doesn't clear itself so it'll keep sending > data from last transfer, which violates this rule. That's > why the original code writes 0 to PRGDATA for rx bytes. following lines with while() will set 0s to the rest of registers. > > > } > > - trx_len += t->len; > > } > > > > + while (reg_offset >= 0) { > > + writeb(0, sp->base + MTK_NOR_REG_PRGDATA(reg_offset)); > > + reg_offset--; > > + } > > + > > + rx_len = min_t(unsigned long, MTK_NOR_PRG_MAX_CYCLES - tx_len, rx_len); > > + trx_len = tx_len + rx_len; > > + > > writel(trx_len * BITS_PER_BYTE, sp->base + MTK_NOR_REG_PRG_CNT); > > > > stat = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_PROGRAM, > > @@ -482,13 +527,18 @@ static int mtk_nor_transfer_one_message(struct spi_controller *master, > > if (stat < 0) > > goto msg_done; > > > > - reg_offset = trx_len - 1; > > - list_for_each_entry(t, &m->transfers, transfer_list) { > > - rxbuf = t->rx_buf; > > - for (i = 0; i < t->len; i++, reg_offset--) { > > - reg = sp->base + MTK_NOR_REG_SHIFT(reg_offset); > > - if (rxbuf) > > + if (rx_len > 0) { > > + reg_offset = rx_len - 1; > > + list_for_each_entry(t, &m->transfers, transfer_list) { > > + u8 *rxbuf = t->rx_buf; > > + > > + if (!rxbuf) > > + continue; > > + > > + for (i = 0; i < t->len && reg_offset >= 0; i++, reg_offset--) { > > + reg = sp->base + MTK_NOR_REG_SHIFT(reg_offset); > > rxbuf[i] = readb(reg); > > + } > > I think this is replacing original code with some equivalent ones, which > seems unnecessary. This patch addressed the issue with 1+6 bytes transfer (e.g JEDEC ID) can have negative reg_offset. And there's skipping the loop if (rx_len < 0) anyway I'd like to follow with your new patch. :-) Thanks! > > > } > > } > > > -- > Regards, > Chuanhong Guo