From patchwork Wed May 13 10:12:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: haikun wang X-Patchwork-Id: 6396011 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 213209F32E for ; Wed, 13 May 2015 10:30:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 11EDF203EB for ; Wed, 13 May 2015 10:30:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E6337202FE for ; Wed, 13 May 2015 10:30:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933620AbbEMKaY (ORCPT ); Wed, 13 May 2015 06:30:24 -0400 Received: from mail-bl2on0111.outbound.protection.outlook.com ([65.55.169.111]:26924 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933526AbbEMKaX (ORCPT ); Wed, 13 May 2015 06:30:23 -0400 Received: from BN3PR0301CA0034.namprd03.prod.outlook.com (25.160.180.172) by CY1PR0301MB0619.namprd03.prod.outlook.com (25.160.142.26) with Microsoft SMTP Server (TLS) id 15.1.148.16; Wed, 13 May 2015 10:15:45 +0000 Received: from BN1BFFO11OLC002.protection.gbl (2a01:111:f400:7c10::1:160) by BN3PR0301CA0034.outlook.office365.com (2a01:111:e400:4000::44) with Microsoft SMTP Server (TLS) id 15.1.166.22 via Frontend Transport; Wed, 13 May 2015 10:15:45 +0000 Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none; Received-SPF: Fail (protection.outlook.com: domain of freescale.com does not designate 192.88.158.2 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.158.2; helo=az84smr01.freescale.net; Received: from az84smr01.freescale.net (192.88.158.2) by BN1BFFO11OLC002.mail.protection.outlook.com (10.58.145.13) with Microsoft SMTP Server (TLS) id 15.1.160.8 via Frontend Transport; Wed, 13 May 2015 10:15:45 +0000 Received: from titan.ap.freescale.net ([10.192.208.233]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id t4DAFgvj007082; Wed, 13 May 2015 03:15:43 -0700 From: Haikun Wang To: CC: , Haikun Wang Subject: [PATCH v1 2/3] spi: spi-fsl-dspi: Change the way of increasing spi_message->actual_length Date: Wed, 13 May 2015 18:12:01 +0800 Message-ID: <1431511921-19548-1-git-send-email-haikun.wang@freescale.com> X-Mailer: git-send-email 2.1.0.27.g96db324 X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BN1BFFO11OLC002; 1:Tt10QN2/5l9f4UY7Ax5TgtHfrzOQyO8jxsz7OCvtWU8VzLRpBMHxBIcJ5C29VmvMRHnPIlxY+6cn8fYusFsP4xnV7nAeTmzlIGou3SYB5UBer+avTaI8yfYg+Odt+mWO34SMHqVTYObV49WurVQU0wacvSXqxUsZfzAEC9BALOCByWLapN8L7LRSyDf/54Z3y+Js++h/+ceGNFWLB83HMMC8ycRVtM/4gUp308XWAeMCY0FAqI1jKcvA6EH4zwUsFJvVt0JNGG+OIxWN5RCodw6EVO/k8qdDTLQILfBiomSwyd9DmnfLL0M4bKxk+JQOlzBaOdQSfntb4vhRwcfbxQ== X-Forefront-Antispam-Report: CIP:192.88.158.2; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10019020)(979002)(6009001)(339900001)(189002)(199003)(575784001)(85426001)(50226001)(33646002)(86362001)(92566002)(77096005)(77156002)(62966003)(47776003)(50466002)(6806004)(106466001)(104016003)(48376002)(230783001)(46102003)(189998001)(87936001)(36756003)(110136002)(5001960100002)(107886002)(50986999)(105606002)(2351001)(81156007)(229853001)(19580395003)(19580405001)(21314002)(4001430100001)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1102; SCL:1; SRVR:CY1PR0301MB0619; H:az84smr01.freescale.net; FPR:; SPF:Fail; MLV:ovrnspm; MX:1; A:1; PTR:InfoDomainNonexistent; LANG:en; MIME-Version: 1.0 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:CY1PR0301MB0619; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(5005006)(3002001); SRVR:CY1PR0301MB0619; BCL:0; PCL:0; RULEID:; SRVR:CY1PR0301MB0619; X-Forefront-PRVS: 0575F81B58 X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2015 10:15:45.2625 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d; Ip=[192.88.158.2]; Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR0301MB0619 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In current driver, we increase actual_length in the following way: message->actual_length += dspi_xxx_transfer() It has two defects. First, transmitting maybe in process when the function call finished and we don't know the transmitting result in this moment. Secondly, the last sentence in function before returning is accessing the SPI register and trigger the data transmitting. If we enable interrupt, interrupt may be generated before function return and we also have the same sentence "message->actual_length += dspi_xxx_transfer()" in the IRQ handler. And usually dspi_xxx_transfer will trigger a new IRQ. The original dspi_xxx_transfer call may return when no new IRQ generate. This may mess the variable spi_message->actual_length. Now we increase the variable in the IRQ handler and only when we get the TCF or EOQ interrupt And we get the transmitted data length from the SPI transfer counter instead of the function return value. Signed-off-by: Haikun Wang --- drivers/spi/spi-fsl-dspi.c | 89 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c index 75c5796..f6e2ef9 100644 --- a/drivers/spi/spi-fsl-dspi.c +++ b/drivers/spi/spi-fsl-dspi.c @@ -47,6 +47,7 @@ #define SPI_MCR_CLR_RXF (1 << 10) #define SPI_TCR 0x08 +#define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16) #define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4)) #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27) @@ -104,6 +105,7 @@ #define SPI_CS_ASSERT 0x02 #define SPI_CS_DROP 0x04 +#define SPI_TCR_TCNT_MAX 0x10000 struct chip_data { u32 mcr_val; u32 ctar_val; @@ -139,6 +141,8 @@ struct fsl_dspi { wait_queue_head_t waitq; u32 waitflags; + + u32 spi_tcnt; }; static inline int is_double_byte_mode(struct fsl_dspi *dspi) @@ -258,7 +262,6 @@ static int dspi_eoq_write(struct fsl_dspi *dspi) int tx_count = 0; int tx_word; u32 dspi_pushr = 0; - int first = 1; tx_word = is_double_byte_mode(dspi); @@ -284,11 +287,6 @@ static int dspi_eoq_write(struct fsl_dspi *dspi) } else if (tx_word && (dspi->len == 1)) dspi_pushr |= SPI_PUSHR_EOQ; - if (first) { - first = 0; - dspi_pushr |= SPI_PUSHR_CTCNT; /* clear counter */ - } - regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr); tx_count++; @@ -355,6 +353,10 @@ static int dspi_transfer_one_message(struct spi_master *master, struct spi_device *spi = message->spi; struct spi_transfer *transfer; int status = 0; + u32 spi_tcr; + + regmap_read(dspi->regmap, SPI_TCR, &spi_tcr); + dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr); message->actual_length = 0; @@ -394,10 +396,10 @@ static int dspi_transfer_one_message(struct spi_master *master, if (dspi->trans_mode == DSPI_EOQ_MODE) { regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE); - message->actual_length += dspi_eoq_write(dspi); + dspi_eoq_write(dspi); } else if (dspi->trans_mode == DSPI_TCFQ_MODE) { regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE); - message->actual_length += dspi_tcfq_write(dspi); + dspi_tcfq_write(dspi); } if (wait_event_interruptible(dspi->waitq, dspi->waitflags)) @@ -488,29 +490,62 @@ static irqreturn_t dspi_interrupt(int irq, void *dev_id) { struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id; struct spi_message *msg = dspi->cur_msg; + u32 spi_sr, spi_tcr; + u32 spi_tcnt, tcnt_diff; + int tx_word; - if (dspi->trans_mode == DSPI_EOQ_MODE) { - regmap_write(dspi->regmap, SPI_SR, SPI_SR_EOQF); - dspi_eoq_read(dspi); - } else if (dspi->trans_mode == DSPI_TCFQ_MODE) { - regmap_write(dspi->regmap, SPI_SR, SPI_SR_TCFQF); - dspi_tcfq_read(dspi); - } + regmap_read(dspi->regmap, SPI_SR, &spi_sr); - if (!dspi->len) { - if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) { - regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs), - SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(16)); - dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM; + if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) { + tx_word = is_double_byte_mode(dspi); + + regmap_read(dspi->regmap, SPI_TCR, &spi_tcr); + spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr); + + /* + * The width of SPI Transfer Counter in SPI_TCR is 16bits, + * so the max couner is 65535. When the counter reach 65535, + * it will wrap around, counter reset to zero. + * spi_tcnt my be less than dspi->spi_tcnt, it means the + * counter already wrapped around. + * SPI Transfer Counter is a counter of transmitted frames. + * The size of frame maybe two bytes. + */ + tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt) + % SPI_TCR_TCNT_MAX; + tcnt_diff *= (tx_word + 1); + if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) + tcnt_diff--; + + msg->actual_length += tcnt_diff; + + dspi->spi_tcnt = spi_tcnt; + + if (dspi->trans_mode == DSPI_EOQ_MODE) { + regmap_write(dspi->regmap, SPI_SR, SPI_SR_EOQF); + dspi_eoq_read(dspi); + } else if (dspi->trans_mode == DSPI_TCFQ_MODE) { + regmap_write(dspi->regmap, SPI_SR, SPI_SR_TCFQF); + dspi_tcfq_read(dspi); } - dspi->waitflags = 1; - wake_up_interruptible(&dspi->waitq); - } else { - if (dspi->trans_mode == DSPI_EOQ_MODE) - msg->actual_length += dspi_eoq_write(dspi); - else if (dspi->trans_mode == DSPI_TCFQ_MODE) - msg->actual_length += dspi_tcfq_write(dspi); + if (!dspi->len) { + if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) { + regmap_update_bits(dspi->regmap, + SPI_CTAR(dspi->cs), + SPI_FRAME_BITS_MASK, + SPI_FRAME_BITS(16)); + dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM; + } + + dspi->waitflags = 1; + wake_up_interruptible(&dspi->waitq); + } else { + if (dspi->trans_mode == DSPI_EOQ_MODE) + dspi_eoq_write(dspi); + else if (dspi->trans_mode == DSPI_TCFQ_MODE) + dspi_tcfq_write(dspi); + } } return IRQ_HANDLED; }