Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp8839763rwr; Thu, 11 May 2023 06:57:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6EUoHRGBX4fUiM0uuSIxoJRLwD+ENK7lninI/QqaHeoJ7sXMW8P9W/m/Utm4O9A1iEUZvq X-Received: by 2002:a17:90b:1e41:b0:250:a6bd:cb4a with SMTP id pi1-20020a17090b1e4100b00250a6bdcb4amr10529325pjb.29.1683813453094; Thu, 11 May 2023 06:57:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683813453; cv=none; d=google.com; s=arc-20160816; b=GpvtxbY6sbmFAa0UjHWW4Fn1mCsaX3kpemnT+jsk1OvLaKDnoHTQOlWDJSfa6B8GIV 4ZfJXSewAEun1+9iX1sKfDYEgSZNY+4QWiZzAAjGYf4N4NywlEADGgFkFTtDMEFHv3Uz e1aTqPCuqm9DA2pIXxzu7fBcBLs08hkzjTevZU0Lp2du/b468bMsVe01nT5O51Dq7fw/ Taykgdflu3R2cf6yJRKl9MfBjMo5Jsrf255u/Qe2pRFbFKRwH4aY4fyZWV6h3tVVjnlW JNJ6EBuPGxPzZZ+0GHAUjQ7MwtJGVmJ4EE8V3324MuBVNPmXOfkL9khc9imqQHZx7Cdw YDYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date; bh=Dqs6TzjcBGvnPgJ2ovHFvwIHDSvGJBWMlzBylyGM/HI=; b=QlPo5g60XJYsj++bEwbj/c3EGvYZ9Rdl018cUnmzMJhdvzugoScC1PHxj153X2+FgL w9hU9P02+sJ7YsBQSYqIcYC+orp2EU2imSdLioO8Q54u4lKj/anAHBlyoYSBvvW5Hmd6 lO0W8cUpo1uXMT8gKif6s84qEUxCpsROwdGEUUBplm80LFtlURXfDEkNkxGwu7OEEmUA kX5qojmrrR3Scpo0cNMH0CkxH3rItVRfwhFL3QVztCqzDIwexV3Y0ITCSpBEykLrIPrc JCfWTjKXW7ZLqW+IHWaPYN8WoA+fayKBbXb7gkJI5qox3BQQzuBVbe156agnz/kx9aXj lxsw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 14-20020a63184e000000b00513929a450esi6347702pgy.123.2023.05.11.06.57.20; Thu, 11 May 2023 06:57:33 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238366AbjEKNm7 (ORCPT + 99 others); Thu, 11 May 2023 09:42:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238334AbjEKNmj (ORCPT ); Thu, 11 May 2023 09:42:39 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A1712D2D7; Thu, 11 May 2023 06:42:19 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AEDC6113E; Thu, 11 May 2023 06:43:03 -0700 (PDT) Received: from donnerap.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B86013F5A1; Thu, 11 May 2023 06:42:16 -0700 (PDT) Date: Thu, 11 May 2023 14:42:14 +0100 From: Andre Przywara To: Maksim Kiselev Cc: Icenowy Zheng , Mark Brown , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Cristian Ciocaltea , Maxime Ripard , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: Re: [PATCH v5 3/5] spi: sun6i: add quirk for in-controller clock divider Message-ID: <20230511144214.0731b371@donnerap.cambridge.arm.com> In-Reply-To: <20230510081121.3463710-4-bigunclemax@gmail.com> References: <20230510081121.3463710-1-bigunclemax@gmail.com> <20230510081121.3463710-4-bigunclemax@gmail.com> Organization: ARM X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.32; aarch64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 Wed, 10 May 2023 11:11:10 +0300 Maksim Kiselev wrote: > Previously SPI controllers in Allwinner SoCs has a clock divider inside. > However now the clock divider is removed and to set the transfer clock > rate it's only needed to set the SPI module clock to the target value > and configure a proper work mode. > > According to the datasheet there are three work modes: > > | SPI Sample Mode | SDM(bit13) | SDC(bit11) | Run Clock | > |-------------------------|------------|------------|-----------| > | normal sample | 1 | 0 | <= 24 MHz | > | delay half cycle sample | 0 | 0 | <= 40 MHz | > | delay one cycle sample | 0 | 1 | >= 80 MHz | > > Add a quirk for this kind of SPI controllers. > > Co-developed-by: Icenowy Zheng > Signed-off-by: Maksim Kiselev Looks good now. Reviewed-by: Andre Przywara Cheers, Andre > --- > drivers/spi/spi-sun6i.c | 91 +++++++++++++++++++++++++++-------------- > 1 file changed, 61 insertions(+), 30 deletions(-) > > diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c > index 01a01cd86db5..e4efab310469 100644 > --- a/drivers/spi/spi-sun6i.c > +++ b/drivers/spi/spi-sun6i.c > @@ -42,7 +42,9 @@ > #define SUN6I_TFR_CTL_CS_MANUAL BIT(6) > #define SUN6I_TFR_CTL_CS_LEVEL BIT(7) > #define SUN6I_TFR_CTL_DHB BIT(8) > +#define SUN6I_TFR_CTL_SDC BIT(11) > #define SUN6I_TFR_CTL_FBS BIT(12) > +#define SUN6I_TFR_CTL_SDM BIT(13) > #define SUN6I_TFR_CTL_XCH BIT(31) > > #define SUN6I_INT_CTL_REG 0x10 > @@ -87,6 +89,7 @@ > > struct sun6i_spi_cfg { > unsigned long fifo_depth; > + bool has_clk_ctl; > }; > > struct sun6i_spi { > @@ -260,7 +263,7 @@ static int sun6i_spi_transfer_one(struct spi_master *master, > struct spi_transfer *tfr) > { > struct sun6i_spi *sspi = spi_master_get_devdata(master); > - unsigned int mclk_rate, div, div_cdr1, div_cdr2, timeout; > + unsigned int div, div_cdr1, div_cdr2, timeout; > unsigned int start, end, tx_time; > unsigned int trig_level; > unsigned int tx_len = 0, rx_len = 0; > @@ -350,39 +353,65 @@ static int sun6i_spi_transfer_one(struct spi_master *master, > > sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg); > > - /* Ensure that we have a parent clock fast enough */ > - mclk_rate = clk_get_rate(sspi->mclk); > - if (mclk_rate < (2 * tfr->speed_hz)) { > - clk_set_rate(sspi->mclk, 2 * tfr->speed_hz); > - mclk_rate = clk_get_rate(sspi->mclk); > - } > + if (sspi->cfg->has_clk_ctl) { > + unsigned int mclk_rate = clk_get_rate(sspi->mclk); > > - /* > - * Setup clock divider. > - * > - * We have two choices there. Either we can use the clock > - * divide rate 1, which is calculated thanks to this formula: > - * SPI_CLK = MOD_CLK / (2 ^ cdr) > - * Or we can use CDR2, which is calculated with the formula: > - * SPI_CLK = MOD_CLK / (2 * (cdr + 1)) > - * Wether we use the former or the latter is set through the > - * DRS bit. > - * > - * First try CDR2, and if we can't reach the expected > - * frequency, fall back to CDR1. > - */ > - div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz); > - div_cdr2 = DIV_ROUND_UP(div_cdr1, 2); > - if (div_cdr2 <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) { > - reg = SUN6I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN6I_CLK_CTL_DRS; > - tfr->effective_speed_hz = mclk_rate / (2 * div_cdr2); > + /* Ensure that we have a parent clock fast enough */ > + if (mclk_rate < (2 * tfr->speed_hz)) { > + clk_set_rate(sspi->mclk, 2 * tfr->speed_hz); > + mclk_rate = clk_get_rate(sspi->mclk); > + } > + > + /* > + * Setup clock divider. > + * > + * We have two choices there. Either we can use the clock > + * divide rate 1, which is calculated thanks to this formula: > + * SPI_CLK = MOD_CLK / (2 ^ cdr) > + * Or we can use CDR2, which is calculated with the formula: > + * SPI_CLK = MOD_CLK / (2 * (cdr + 1)) > + * Wether we use the former or the latter is set through the > + * DRS bit. > + * > + * First try CDR2, and if we can't reach the expected > + * frequency, fall back to CDR1. > + */ > + div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz); > + div_cdr2 = DIV_ROUND_UP(div_cdr1, 2); > + if (div_cdr2 <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) { > + reg = SUN6I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN6I_CLK_CTL_DRS; > + tfr->effective_speed_hz = mclk_rate / (2 * div_cdr2); > + } else { > + div = min(SUN6I_CLK_CTL_CDR1_MASK, order_base_2(div_cdr1)); > + reg = SUN6I_CLK_CTL_CDR1(div); > + tfr->effective_speed_hz = mclk_rate / (1 << div); > + } > + > + sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg); > } else { > - div = min(SUN6I_CLK_CTL_CDR1_MASK, order_base_2(div_cdr1)); > - reg = SUN6I_CLK_CTL_CDR1(div); > - tfr->effective_speed_hz = mclk_rate / (1 << div); > + clk_set_rate(sspi->mclk, tfr->speed_hz); > + tfr->effective_speed_hz = clk_get_rate(sspi->mclk); > + > + /* > + * Configure work mode. > + * > + * There are three work modes depending on the controller clock > + * frequency: > + * - normal sample mode : CLK <= 24MHz SDM=1 SDC=0 > + * - delay half-cycle sample mode : CLK <= 40MHz SDM=0 SDC=0 > + * - delay one-cycle sample mode : CLK >= 80MHz SDM=0 SDC=1 > + */ > + reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG); > + reg &= ~(SUN6I_TFR_CTL_SDM | SUN6I_TFR_CTL_SDC); > + > + if (tfr->effective_speed_hz <= 24000000) > + reg |= SUN6I_TFR_CTL_SDM; > + else if (tfr->effective_speed_hz >= 80000000) > + reg |= SUN6I_TFR_CTL_SDC; > + > + sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg); > } > > - sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg); > /* Finally enable the bus - doing so before might raise SCK to HIGH */ > reg = sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG); > reg |= SUN6I_GBL_CTL_BUS_ENABLE; > @@ -701,10 +730,12 @@ static void sun6i_spi_remove(struct platform_device *pdev) > > static const struct sun6i_spi_cfg sun6i_a31_spi_cfg = { > .fifo_depth = SUN6I_FIFO_DEPTH, > + .has_clk_ctl = true, > }; > > static const struct sun6i_spi_cfg sun8i_h3_spi_cfg = { > .fifo_depth = SUN8I_FIFO_DEPTH, > + .has_clk_ctl = true, > }; > > static const struct of_device_id sun6i_spi_match[] = {