From patchwork Mon Aug 28 14:29:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Brunet X-Patchwork-Id: 9925711 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9792760329 for ; Mon, 28 Aug 2017 14:41:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A02F27FE4 for ; Mon, 28 Aug 2017 14:41:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E933280CF; Mon, 28 Aug 2017 14:41:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B237A27FE4 for ; Mon, 28 Aug 2017 14:41:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=la6oAROjj/QyFPwkXNjfZn+ILxFkW9ogDj4n0E7FPoM=; b=deGj0NnKMICNeg1Rppv6xbGooo WQWMlKfsqPrzvRGDFqOIvwZaWJi1Z5AnfwQVNmNe/xud0vWZEeLOrO/3UHaO1mR1huDbq4qCeWGjz H28u9sq6na5g4yqCSitS3DUqBb0THnFAAtUF8UlNUZ7ioSUPJv1McA6kJW6IwrpsRMxz2BKqX0mxI LfJympOuKkbCh3MpBLe8qk3GCTG2QNNCsHym+M5jKX5KuiwLNzqDfHYzMK1AJUrXFp9IxLwIUkxIO Hs9KzjwnUyDdpi/eNNhmwS8rlBEG1yxejEXmSn7g9cI3NVYPmK0K3Bl0rkejtOqPHxkH/PvM+1ZxK gMlSyu0Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dmLE2-0006ZD-AG; Mon, 28 Aug 2017 14:41:14 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dmLD8-00058H-Ro for linux-arm-kernel@bombadil.infradead.org; Mon, 28 Aug 2017 14:40:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=+0TkBM/ti26Xx5P8b1OzWpSIVfb3nVye+W9JkYLE8QI=; b=MBS0CYlkoT4UpXj4nfoBCsz0S XM99ALnFkgJfQmc/DJS8JQpC9VSGzgJXrnWJbaugDFc3xRkuCWwk84EbxNdkqtX0IF2Cw/zG6IFtN vvbLcSmFu1Tgx3opkhMgTaqwj7VfDxn71hSd8DTt/frE630N1lniJqzBPwj2R5UIKn78ppYutNMfa eY91Aro2Pa9AYKfbYl9ztreXFHhcJOaFNmUPGH0t1AlS0RoFMi24G5RC5RH/5rGPRDNc/Y9MPQ8XR 780sWmP4HhIm4Ow2TXwdj0UjxEU3c5jUk7M5VESdbVJxX+lQnO/5TL4xPmAbxCJ9q40hmuKHRwNYq gxTMG+A5A==; Received: from mail-wr0-x22f.google.com ([2a00:1450:400c:c0c::22f]) by casper.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dmL32-0004CT-LY for linux-arm-kernel@lists.infradead.org; Mon, 28 Aug 2017 14:29:54 +0000 Received: by mail-wr0-x22f.google.com with SMTP id 40so1692284wrv.5 for ; Mon, 28 Aug 2017 07:29:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+0TkBM/ti26Xx5P8b1OzWpSIVfb3nVye+W9JkYLE8QI=; b=eOfV3LP19n3fpOdLshuPhgosIe4b74Ujh0ycS0y2YF+gq0v7OMS94NxMQsULCUPUGS 1l2rYcnD/puVHCK/ZlbqWvMJWXYNYwvHi2aTqWU4CXaIGB7CF36PtqDtOLnereeMoTkz UgHkT0a4+N+IFYWTVTKE9wqjNAE7U5pI2HcCAggO02dLcRclnUQB2PdmW/ayDD6cjcgk 8ExXQXUcziumN9G1g+YYlTDVxKAT2i+5XLbeZylnAesXfDxk8BGkEqx7WIrcVzFT7VDw fUK8CEjrCVaIQzxaf6kaTqnWIsRXjJLSYMcucYyWkVPwv8PdQfyMALaYFDPu1L+bdTtx 0bMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+0TkBM/ti26Xx5P8b1OzWpSIVfb3nVye+W9JkYLE8QI=; b=PmkxDVavvfiHMap6QHUFl/DdH5Dd1MQ0vWp5V2QnqysVp04YFrQ1+UMIeYXt7CaXS+ TRbEHO5KLexcQRwnR0r5boZGJr2GnYmjz4Pd0TuQXc8pHjGAWC0mrGb+rhS7Nr7zboCy p9y/MSYHCg2wk5DGbW4A+ZMpVPfgdjU4u8MUaowGkqPxUXOt9IbPahtE+Wc8t9IYs6L/ YlTfG+dgrf1vJlEC4mYtDLg4Acssla+CHU3cUlVSb038y3IROPibN3wF9D6keRbyoNiZ 0cERouoeQKD7PoE0Qrn5x2hlqV/WCvrAUvqIRwlmfx3b/o2Qt85uvbQC3x4iCitEZJ1T YGkA== X-Gm-Message-State: AHYfb5hl821zuN0dtxEAF6oQj9fZL7MDEV7q2FRCQ0JPzmTEzCJypx5T ONTapHgfK+iooRnK X-Received: by 10.223.196.152 with SMTP id m24mr518103wrf.185.1503930569269; Mon, 28 Aug 2017 07:29:29 -0700 (PDT) Received: from localhost.localdomain (uluru.liltaz.com. [163.172.81.188]) by smtp.googlemail.com with ESMTPSA id z39sm604792wrz.61.2017.08.28.07.29.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Aug 2017 07:29:28 -0700 (PDT) From: Jerome Brunet To: Ulf Hansson , Kevin Hilman , Carlo Caione Subject: [PATCH v3 10/13] mmc: meson-gx: use CCF to handle the clock phases Date: Mon, 28 Aug 2017 16:29:12 +0200 Message-Id: <20170828142915.27020-11-jbrunet@baylibre.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20170828142915.27020-1-jbrunet@baylibre.com> References: <20170828142915.27020-1-jbrunet@baylibre.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170828_152952_820079_4F4D69D3 X-CRM114-Status: GOOD ( 25.45 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-amlogic@lists.infradead.org, linux-mmc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Jerome Brunet MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Several phases can be controlled on the meson-gx controller, the core, tx and rx clock phase. The tx and rx uses delays to allow more fine grained setting of the phase. To properly compute the phase using delays, accessing the clock rate is necessary. Instead of ad-hoc functions, use the common clock framework to set the clock phases (and access the clock rate while doing it). Acked-by: Kevin Hilman Signed-off-by: Jerome Brunet --- drivers/mmc/host/meson-gx-mmc.c | 217 ++++++++++++++++++++++++++++++++-------- 1 file changed, 176 insertions(+), 41 deletions(-) diff --git a/drivers/mmc/host/meson-gx-mmc.c b/drivers/mmc/host/meson-gx-mmc.c index 40fa7ae64c72..2fa18faa7f0f 100644 --- a/drivers/mmc/host/meson-gx-mmc.c +++ b/drivers/mmc/host/meson-gx-mmc.c @@ -46,10 +46,9 @@ #define CLK_CORE_PHASE_MASK GENMASK(9, 8) #define CLK_TX_PHASE_MASK GENMASK(11, 10) #define CLK_RX_PHASE_MASK GENMASK(13, 12) -#define CLK_PHASE_0 0 -#define CLK_PHASE_90 1 -#define CLK_PHASE_180 2 -#define CLK_PHASE_270 3 +#define CLK_TX_DELAY_MASK GENMASK(19, 16) +#define CLK_RX_DELAY_MASK GENMASK(23, 20) +#define CLK_DELAY_STEP_PS 200 #define CLK_ALWAYS_ON BIT(24) #define SD_EMMC_DELAY 0x4 @@ -121,9 +120,9 @@ #define MUX_CLK_NUM_PARENTS 2 struct meson_tuning_params { - u8 core_phase; - u8 tx_phase; - u8 rx_phase; + unsigned int core_phase; + unsigned int tx_phase; + unsigned int rx_phase; }; struct sd_emmc_desc { @@ -142,6 +141,8 @@ struct meson_host { void __iomem *regs; struct clk *core_clk; struct clk *mmc_clk; + struct clk *rx_clk; + struct clk *tx_clk; unsigned long req_rate; struct pinctrl *pinctrl; @@ -181,6 +182,90 @@ struct meson_host { #define CMD_RESP_MASK GENMASK(31, 1) #define CMD_RESP_SRAM BIT(0) +struct meson_mmc_phase { + struct clk_hw hw; + void __iomem *reg; + unsigned long phase_mask; + unsigned long delay_mask; + unsigned int delay_step_ps; +}; + +#define to_meson_mmc_phase(_hw) container_of(_hw, struct meson_mmc_phase, hw) + +static int meson_mmc_clk_get_phase(struct clk_hw *hw) +{ + struct meson_mmc_phase *mmc = to_meson_mmc_phase(hw); + unsigned int phase_num = 1 << hweight_long(mmc->phase_mask); + unsigned long period_ps, p, d; + int degrees; + u32 val; + + val = readl(mmc->reg); + p = (val & mmc->phase_mask) >> __bf_shf(mmc->phase_mask); + degrees = p * 360 / phase_num; + + if (mmc->delay_mask) { + period_ps = DIV_ROUND_UP((unsigned long)NSEC_PER_SEC * 1000, + clk_get_rate(hw->clk)); + d = (val & mmc->delay_mask) >> __bf_shf(mmc->delay_mask); + degrees += d * mmc->delay_step_ps * 360 / period_ps; + degrees %= 360; + } + + return degrees; +} + +static void meson_mmc_apply_phase_delay(struct meson_mmc_phase *mmc, + unsigned int phase, + unsigned int delay) +{ + u32 val; + + val = readl(mmc->reg); + val &= ~mmc->phase_mask; + val |= phase << __bf_shf(mmc->phase_mask); + + if (mmc->delay_mask) { + val &= ~mmc->delay_mask; + val |= delay << __bf_shf(mmc->delay_mask); + } + + writel(val, mmc->reg); +} + +static int meson_mmc_clk_set_phase(struct clk_hw *hw, int degrees) +{ + struct meson_mmc_phase *mmc = to_meson_mmc_phase(hw); + unsigned int phase_num = 1 << hweight_long(mmc->phase_mask); + unsigned long period_ps, d = 0, r; + uint64_t p; + + p = degrees % 360; + + if (!mmc->delay_mask) { + p = DIV_ROUND_CLOSEST_ULL(p, 360 / phase_num); + } else { + period_ps = DIV_ROUND_UP((unsigned long)NSEC_PER_SEC * 1000, + clk_get_rate(hw->clk)); + + /* First compute the phase index (p), the remainder (r) is the + * part we'll try to acheive using the delays (d). + */ + r = do_div(p, 360 / phase_num); + d = DIV_ROUND_CLOSEST(r * period_ps, + 360 * mmc->delay_step_ps); + d = min(d, mmc->delay_mask >> __bf_shf(mmc->delay_mask)); + } + + meson_mmc_apply_phase_delay(mmc, p, d); + return 0; +} + +static const struct clk_ops meson_mmc_clk_phase_ops = { + .get_phase = meson_mmc_clk_get_phase, + .set_phase = meson_mmc_clk_set_phase, +}; + static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data) { unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC; @@ -373,6 +458,13 @@ static int meson_mmc_clk_set(struct meson_host *host, struct mmc_ios *ios) return 0; } +static void meson_mmc_set_phase_params(struct meson_host *host) +{ + clk_set_phase(host->mmc_clk, host->tp.core_phase); + clk_set_phase(host->tx_clk, host->tp.tx_phase); + clk_set_phase(host->rx_clk, host->tp.rx_phase); +} + /* * The SD/eMMC IP block has an internal mux and divider used for * generating the MMC clock. Use the clock framework to create and @@ -383,6 +475,7 @@ static int meson_mmc_clk_init(struct meson_host *host) struct clk_init_data init; struct clk_mux *mux; struct clk_divider *div; + struct meson_mmc_phase *core, *tx, *rx; struct clk *clk; char clk_name[32]; int i, ret = 0; @@ -394,9 +487,6 @@ static int meson_mmc_clk_init(struct meson_host *host) clk_reg = 0; clk_reg |= CLK_ALWAYS_ON; clk_reg |= CLK_DIV_MASK; - clk_reg |= FIELD_PREP(CLK_CORE_PHASE_MASK, host->tp.core_phase); - clk_reg |= FIELD_PREP(CLK_TX_PHASE_MASK, host->tp.tx_phase); - clk_reg |= FIELD_PREP(CLK_RX_PHASE_MASK, host->tp.rx_phase); writel(clk_reg, host->regs + SD_EMMC_CLOCK); /* get the mux parents */ @@ -456,10 +546,80 @@ static int meson_mmc_clk_init(struct meson_host *host) div->flags = (CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ROUND_CLOSEST); - host->mmc_clk = devm_clk_register(host->dev, &div->hw); + clk = devm_clk_register(host->dev, &div->hw); + if (WARN_ON(IS_ERR(clk))) + return PTR_ERR(clk); + + /* create the mmc core clock */ + core = devm_kzalloc(host->dev, sizeof(*core), GFP_KERNEL); + if (!core) + return -ENOMEM; + + snprintf(clk_name, sizeof(clk_name), "%s#core", dev_name(host->dev)); + init.name = clk_name; + init.ops = &meson_mmc_clk_phase_ops; + init.flags = CLK_SET_RATE_PARENT; + clk_parent[0] = __clk_get_name(clk); + init.parent_names = clk_parent; + init.num_parents = 1; + + core->reg = host->regs + SD_EMMC_CLOCK; + core->phase_mask = CLK_CORE_PHASE_MASK; + core->hw.init = &init; + + host->mmc_clk = devm_clk_register(host->dev, &core->hw); if (WARN_ON(PTR_ERR_OR_ZERO(host->mmc_clk))) return PTR_ERR(host->mmc_clk); + /* create the mmc tx clock */ + tx = devm_kzalloc(host->dev, sizeof(*tx), GFP_KERNEL); + if (!tx) + return -ENOMEM; + + snprintf(clk_name, sizeof(clk_name), "%s#tx", dev_name(host->dev)); + init.name = clk_name; + init.ops = &meson_mmc_clk_phase_ops; + init.flags = 0; + clk_parent[0] = __clk_get_name(host->mmc_clk); + init.parent_names = clk_parent; + init.num_parents = 1; + + tx->reg = host->regs + SD_EMMC_CLOCK; + tx->phase_mask = CLK_TX_PHASE_MASK; + tx->delay_mask = CLK_TX_DELAY_MASK; + tx->delay_step_ps = CLK_DELAY_STEP_PS; + tx->hw.init = &init; + + host->tx_clk = devm_clk_register(host->dev, &tx->hw); + if (WARN_ON(PTR_ERR_OR_ZERO(host->tx_clk))) + return PTR_ERR(host->tx_clk); + + /* create the mmc rx clock */ + rx = devm_kzalloc(host->dev, sizeof(*rx), GFP_KERNEL); + if (!rx) + return -ENOMEM; + + snprintf(clk_name, sizeof(clk_name), "%s#rx", dev_name(host->dev)); + init.name = clk_name; + init.ops = &meson_mmc_clk_phase_ops; + init.flags = 0; + clk_parent[0] = __clk_get_name(host->mmc_clk); + init.parent_names = clk_parent; + init.num_parents = 1; + + rx->reg = host->regs + SD_EMMC_CLOCK; + rx->phase_mask = CLK_RX_PHASE_MASK; + rx->delay_mask = CLK_RX_DELAY_MASK; + rx->delay_step_ps = CLK_DELAY_STEP_PS; + rx->hw.init = &init; + + host->rx_clk = devm_clk_register(host->dev, &rx->hw); + if (WARN_ON(PTR_ERR_OR_ZERO(host->rx_clk))) + return PTR_ERR(host->rx_clk); + + /* Set the initial phase parameters */ + meson_mmc_set_phase_params(host); + /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */ host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000); ret = clk_set_rate(host->mmc_clk, host->mmc->f_min); @@ -469,31 +629,6 @@ static int meson_mmc_clk_init(struct meson_host *host) return clk_prepare_enable(host->mmc_clk); } -static void meson_mmc_set_tuning_params(struct mmc_host *mmc) -{ - struct meson_host *host = mmc_priv(mmc); - u32 regval; - - /* stop clock */ - regval = readl(host->regs + SD_EMMC_CFG); - regval |= CFG_STOP_CLOCK; - writel(regval, host->regs + SD_EMMC_CFG); - - regval = readl(host->regs + SD_EMMC_CLOCK); - regval &= ~CLK_CORE_PHASE_MASK; - regval |= FIELD_PREP(CLK_CORE_PHASE_MASK, host->tp.core_phase); - regval &= ~CLK_TX_PHASE_MASK; - regval |= FIELD_PREP(CLK_TX_PHASE_MASK, host->tp.tx_phase); - regval &= ~CLK_RX_PHASE_MASK; - regval |= FIELD_PREP(CLK_RX_PHASE_MASK, host->tp.rx_phase); - writel(regval, host->regs + SD_EMMC_CLOCK); - - /* start clock */ - regval = readl(host->regs + SD_EMMC_CFG); - regval &= ~CFG_STOP_CLOCK; - writel(regval, host->regs + SD_EMMC_CFG); -} - static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) { struct meson_host *host = mmc_priv(mmc); @@ -862,13 +997,13 @@ static int meson_mmc_execute_tuning(struct mmc_host *mmc, u32 opcode) dev_info(mmc_dev(mmc), "(re)tuning...\n"); - for (i = CLK_PHASE_0; i <= CLK_PHASE_270; i++) { + for (i = 0; i < 360; i += 90) { host->tp.rx_phase = i; /* exclude the active parameter set if retuning */ if (!memcmp(&tp_old, &host->tp, sizeof(tp_old)) && mmc->doing_retune) continue; - meson_mmc_set_tuning_params(mmc); + meson_mmc_set_phase_params(host); ret = mmc_send_tuning(mmc, opcode, &cmd_error); if (!ret) break; @@ -999,9 +1134,9 @@ static int meson_mmc_probe(struct platform_device *pdev) if (ret) goto free_host; - host->tp.core_phase = CLK_PHASE_180; - host->tp.tx_phase = CLK_PHASE_0; - host->tp.rx_phase = CLK_PHASE_0; + host->tp.core_phase = 180; + host->tp.tx_phase = 0; + host->tp.rx_phase = 0; ret = meson_mmc_clk_init(host); if (ret)