Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1549334imm; Wed, 8 Aug 2018 20:24:58 -0700 (PDT) X-Google-Smtp-Source: AA+uWPy1MG1O5//QnOEMGSxY72CxgY2deEvMheKDjsPVjRRkLQGxa86j1kqWrZNpi1V5H/W1ruRr X-Received: by 2002:a63:144b:: with SMTP id 11-v6mr371346pgu.219.1533785098349; Wed, 08 Aug 2018 20:24:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533785098; cv=none; d=google.com; s=arc-20160816; b=gvGW2Vx3tjoPIECmcZeykBRTCaQeJOjF0f6cNZe3fte8ciBQRAswVFXqpg4C+CH9GB qdkL4PXyRbefD8lSNvUbjiGFWU/ExfJz0LVYp8JV18ZUDLVe7SGaJzua8J9v+QxHmW6s H1YL5eozv5wom8JSj5tPA+D9MfySHll4wGm/ogaWmFTvApG1U1alv6SAHwI2fy8ImeUn 9DU7K6ClzMPJvs+JQEvpEN15xgS80hhXmj3YT+TJZCgoKNytk9z4ZcgsrO5Nn0AS8R/O 9cD8Z0g9lVcGJPP2GOMtNwIPo9NUs6LJZS7HVw0Eu04/w3pKj8kRPAqI40J8s1XwfXSg T26g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=CwSipAou66N3ZN2m/+x0Jk1AqyK1aR4jnQAWexfrwho=; b=ayg/Lq/XKVyUpkhHvfsJb4+Zfgl3htQFPno/EDdR6pxEDXvTXXgkdZAa/8NE5Gy59m vQKdcVcougsm8bwxbDI4cyLBq8GAHdC5B33WLuQeQM+sFwcJNotXuak7Z0TYvPB8xHpw IY4iSjVfEKnc3K079GIOI5h9V8QP9uI2JQfI4CgUdG/jAQujEOoiR/2gkruClKx+h1TN vSsDUs1ge558bSeGv9BnrswS88UO0sOIZucdY8OCDHzgbjK6VX021MLuOBS+m8D0TeB/ B+iJct4LMa0tWCwYTRSMEXXYG59tRd9Eg4DniUZRpMhnC6AqDhK0tI6XMAJY2ODhi/Tw gozQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IjjBcobq; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z24-v6si2807121plo.5.2018.08.08.20.24.42; Wed, 08 Aug 2018 20:24:58 -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; dkim=pass header.i=@linaro.org header.s=google header.b=IjjBcobq; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727570AbeHIFqb (ORCPT + 99 others); Thu, 9 Aug 2018 01:46:31 -0400 Received: from mail-oi0-f68.google.com ([209.85.218.68]:44258 "EHLO mail-oi0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727158AbeHIFqb (ORCPT ); Thu, 9 Aug 2018 01:46:31 -0400 Received: by mail-oi0-f68.google.com with SMTP id s198-v6so7412265oih.11 for ; Wed, 08 Aug 2018 20:23:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=CwSipAou66N3ZN2m/+x0Jk1AqyK1aR4jnQAWexfrwho=; b=IjjBcobqDJz9ObBmRfodUMwFvRUyrjL75FUNO86PnIjGX1ReOnqht6N12EatlIw1zV RLfQDl96pGrTyn60XUNzVArCVxBkffKXg4jAmCaZ5kYGvWPzVc5bhc7c5sioDk7xaS6O Zx/MH8xe6dt1T+2O/FQJxR9sY+iaYGPxNCDrI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=CwSipAou66N3ZN2m/+x0Jk1AqyK1aR4jnQAWexfrwho=; b=jhoVOxjNJbyx8ctKmg/vsEAubHbaRWw7nx2nKu+7zsAc4nzDKX+qT7N0033lct+1yc oOoBy1iJVjWhtAUNkX39GDsQr6TkpTNTsC6JGrcKzqhuqnucpjp4lU9DJepoN4Re9Etr 2O7b88ndJkmm6FdgqGSZKP9KqyyXs9KDF5k71nLYt3m5Nu3j2VSiRJVGYj/MHOtjLBTR Mo0+dn0FSvgqvxCxco0YdNKcYYHt+lWnCxVujtf0832VtbsjiuJG0YtMEOC4ZioghqAY HWEYZ0lCt+IFGJHc8GzTjzB55ZK+knvGQnLaYuL5MtSdZPuJnCK26uw6toI1nFfcWPBf oTXw== X-Gm-Message-State: AOUpUlHeXkJNbYln73Y+xq+wpRfUBmn2S9RMYdE2fU3kZubjvYGmbnLm et72thHIH98GWhhIi3QHvaNOYHvEoVi11aYpx4Sbiw== X-Received: by 2002:aca:dd07:: with SMTP id u7-v6mr337974oig.177.1533785032461; Wed, 08 Aug 2018 20:23:52 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a9d:237a:0:0:0:0:0 with HTTP; Wed, 8 Aug 2018 20:23:51 -0700 (PDT) In-Reply-To: <1533755337.2283.280.camel@impinj.com> References: <64681bf903104c8a02f118294e616e2a12a5ebe4.1533638405.git.baolin.wang@linaro.org> <99befd2badc4dffb59662fca1e11d79f18b64755.1533638405.git.baolin.wang@linaro.org> <1533661818.2283.264.camel@impinj.com> <1533755337.2283.280.camel@impinj.com> From: Baolin Wang Date: Thu, 9 Aug 2018 11:23:51 +0800 Message-ID: Subject: Re: [PATCH 2/2] spi: sprd: Add SPI driver for Spreadtrum SC9860 To: Trent Piepho Cc: "linux-kernel@vger.kernel.org" , "robh+dt@kernel.org" , "orsonzhai@gmail.com" , "devicetree@vger.kernel.org" , "zhang.lyra@gmail.com" , "broonie@kernel.org" , "mark.rutland@arm.com" , "linux-spi@vger.kernel.org" , "lanqing.liu@spreadtrum.com" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Trent, On 9 August 2018 at 03:08, Trent Piepho wrote: > On Wed, 2018-08-08 at 11:19 +0800, Baolin Wang wrote: >> On 8 August 2018 at 01:10, Trent Piepho wrote: >> > On Tue, 2018-08-07 at 18:43 +0800, Baolin Wang wrote: >> > > >> > > +static u32 sprd_spi_transfer_max_timeout(struct sprd_spi *ss, >> > > + struct spi_transfer *t) >> > > +{ >> > > + /* >> > > + * The time spent on transmission of the full FIFO data is the maximum >> > > + * SPI transmission time. >> > > + */ >> > > + u32 size = t->bits_per_word * SPRD_SPI_FIFO_SIZE; >> > > + u32 bit_time_us = SPRD_SPI_HZ / t->speed_hz + 1; > > There's another flaw here in that the transfer speed, t->speed_hz, > might not be exactly what is used, due to limitations of the clock > divider. It would be better to find the actual SPI clock used, then > use that in the calculations. Right, will use the real speed to calculate the transfer time. > >> > > + u32 total_time_us = size * bit_time_us; >> > > + /* >> > > + * There is an interval between data and the data in our SPI hardware, >> > > + * so the total transmission time need add the interval time. >> > > + * >> > > + * The inteval calculation formula: >> > > + * interval time (source clock cycles) = interval * 4 + 10. >> > > + */ >> > > + u32 interval_cycle = SPRD_SPI_FIFO_SIZE * ((ss->interval << 2) + 10); >> > > + u32 interval_time_us = interval_cycle * SPRD_SPI_HZ / ss->src_clk + 1; >> > >> > If interval is greater than 31, this will overflow. >> >> Usually we will not set such a big value for interval, but this is a >> risk like you said. So we will check and limit the interval value to >> make sure the formula will not overflow. >> > > Better would be to limit the inter word delay to whatever maximum value > your hardware supports, and then write code that can calculate that > without error. Yes, will define the maximum word delay values to avoid overflow. > >> > > +static void sprd_spi_set_speed(struct sprd_spi *ss, u32 speed_hz) >> > > +{ >> > > + /* >> > > + * From SPI datasheet, the prescale calculation formula: >> > > + * prescale = SPI source clock / (2 * SPI_freq) - 1; >> > > + */ >> > > + u32 clk_div = ss->src_clk / (speed_hz << 1) - 1; >> > >> > You should probably round up here. The convention is to use the >> > closest speed less than equal to the requested speed, but since this is >> > a divider, rounding it down will select the next highest speed. >> >> Per the datasheet, we do not need round up/down the speed. Since our >> hardware can handle the clock calculated by the above formula if the >> requested speed is in the normal region (less than ->max_speed_hz). > > That is not what I mean. Let me explain differently. > > An integer divider like this can not produce any frequency exactly. > Consider if src_clk is 10 MHz. A clk_div value of 0 produces a 5 MHz > SPI clock. A clk_div value of 1 produces a 2.5 MHz SPI clock. > > What if the transfer requests a SPI clock of 3 MHz? > > Your math above will produce a SPI clock of 5 MHz, faster than > requested. This is not the convention in Linux SPI masters. You > should instead of have chosen a clk_div value of 1 to get a SPI clock > of 2.5 MHz, the closest clock possible that is not greater than the > requested value. > > To do this, round up. > > clk_div = DIV_ROUND_UP(ss->src_clk, speed_hz * 2) - 1; Thanks for your patient explanation. After talking with Lanqing who is the author of the SPI driver, we think you are definitely correct and will fix in next version according to your suggestion. Thanks a lot. -- Baolin Wang Best Regards