From patchwork Tue Apr 2 11:28:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagan Teki X-Patchwork-Id: 194 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 68CE53F078 for ; Tue, 2 Apr 2019 13:29:29 +0200 (CEST) Received: by mail-pg1-f197.google.com with SMTP id f6sf9735444pgo.15 for ; Tue, 02 Apr 2019 04:29:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1554204568; cv=pass; d=google.com; s=arc-20160816; b=mXdDn46jONyLw4VUBkyZMgnpLGJ2M4phSkaMcv7u1uT0Qv77wPGiWUyUL0rtzjDtPO RWMyw8jNxSCWaFy0G2Uj5cnBciuvoGFfjren5JuL8QLIunyfaI0bxuGvfM8IZWH9mD2p Pl1B290wEIx6BvT2sEQ+iDY7D/ksNOl5D+fc1wlfqRgQfo1rbvb32PjngKL4ghF9nCC2 9DoAgYV7fGKj55zgPm+kJ5TTaspVz7hKZE0xRu5YC0C7E+TLNDxqksB3F9XtzFnnkpIa galJrZ6SlD+2P+QKUUcNtkKlYsVoXg3+oIRt2ZgEWxzjcj7kSJnluBmazfNm3BAcDWg7 3rLg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=W0NNM1sJbvCASIRZabp0DAz8AOspfCFQDnTlDTm213I=; b=1A6sEbJrhU3v93+6hMZv1zWSv20OvD8gQDKg6Q2xkagtfftV4eyZDEjGAKYFXa4PAv GB75/CsszXzkoReT/A2bqFUgaCEY1/zWL60AZOJSx68h05Angtfy8WzWIxf8uM0PVlZe x8vLKH/LqENMBtLHZrdawpumujHEjl4qxYhsHfK5oBP5NA4jwhefn58EggiPqF6+9oyc wnf5hupCLqnf/ZpZvzQBddx4S2tjQ6t3tDCXjgs72F/QIoyhiikBSi1vu3f3mmyks++q 7gwvNPMFvevOMJ7mApaR6xnqOVYgQnCLPM7pHB8mb/NwAJmzPulQeAYUeluhzzZ2WCH+ 6xtg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=RT1Heyi0; spf=pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jagan@amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=W0NNM1sJbvCASIRZabp0DAz8AOspfCFQDnTlDTm213I=; b=DkkCqhboan+6gfKx78iP3CtAIGmXdMZTC7j91zba5/n5coDmw4PWjXUr5CELA7lI3Q 1rYpaNyshXq2+VNs1PV9cGhgh44TKMHIO54J4l+KrHbZUXYzrl9uCmPXD9ccjFcDnlIZ MAPHIYVVtWLqlU3/dD4GFEGhuR3ZJ2ZX/QmDQ= 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:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=W0NNM1sJbvCASIRZabp0DAz8AOspfCFQDnTlDTm213I=; b=MIouYAbj683ZlGmbZjxxKC3Io9uFuMHzP+vKJH9JKfbzFo+Nq06HDF3I2udJuG99qH kxVkRFfiKw8eKgWDINRFdnca5oLCVX1gDucpC32jrZX9qjw2MV4ukwsOg3fAZ6bBYu+s tD2m4F40syncpZPHvpmM57vn3RFbd1rpgAu2YEYh2Aus1WRuzXHdI+PUera/syABcx+l 3TmfJjf2J/H6gcQrrOZCpQrbieebtGBaC0q2jn5Zp1BywrJUSkZoH5OMNebq7MhaWyEj EC2ALSaDJw1Jc+v1hLZaJqPtkxPM0imn0o7/2uiUEESAjltqg87vc85IqkL06Ec0IofS qvKA== X-Gm-Message-State: APjAAAVsk5EkuKQFyUIwDfOsR2gd4nu9AcG+NNwEaRwoLb1dfvkxkCZk 8mJ505IllOKl77Er7ixUVQy+BRyu X-Google-Smtp-Source: APXvYqxQwubwAj4qbvfli5oCeIVePFOlikbvkI35sIoYbxyAeXnMLIPgoLTZGUbJYgPMQo8C29KInA== X-Received: by 2002:a63:7c01:: with SMTP id x1mr1128237pgc.146.1554204568134; Tue, 02 Apr 2019 04:29:28 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:902:bb8c:: with SMTP id m12ls166595pls.16.gmail; Tue, 02 Apr 2019 04:29:27 -0700 (PDT) X-Received: by 2002:a17:902:694c:: with SMTP id k12mr7356072plt.149.1554204567748; Tue, 02 Apr 2019 04:29:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554204567; cv=none; d=google.com; s=arc-20160816; b=lYHsxpGLSeDoh1vJvE+F0K+V6cAVBRVjdIl61CwjhGdw13VT90s8uM9eEz7WvaSPyY jJ9XdDp87c8HqLKNZbHqoEksU1LHyYrNy1Im+Ad/dvR680EkLLe3prAhXjkQEYM4JDud ctPwjE0efzjQmA+19EpD+PWGNNfizcRxxbxBCpLZFwGi+zg/8x9c/Oe8wB1ZY+zYObM0 GBdZUWnlmPnavU3WMSUAQZSrMTO9NrM6ZgtvEAJDfLj6mQ+uYkuFxwu4VR0khMDru/Jb pKaKCkidiErAevMfuu53lHoSHnYydrot4IpYpQz/7QMKC+8TazX/RMz5t6kBF6SGNKK+ xJMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=mLI39kPkQxLonGpvkiyxIm2efsCew9oD0HEv3cCvfy0=; b=d/gaGBuvVheSXY6aKM90kuefEC/RbadcUrFL4DOH/k8V+McpRp85D6bvuWrSfWvJNo tdLWWc59ArcyK41TvHxHRGMvwU1MsQFTvmKPYzRaxLXjqzvXI8VRZumf1++2wmrOk3Lh STY7amuDgL/ORwzw7U13Ka9ZBPApGvsVfHufB62PyngKN7KNxn5T6NrbRqY2w/KhUmiQ mkdWgw1t1aRjYMBo+pet/Ok6mvuEAvW4SPaNT1mMM1e1vJBG00FkIH9cihe3Jiix7XL1 m9FXea7fA/FjBnxY4z5MoHVwmYO2vTVObVQC84g0SWRWe7TA665KE/DyuEIiXNxG0BP+ h+1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=RT1Heyi0; spf=pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jagan@amarulasolutions.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id g6sor14527151pll.50.2019.04.02.04.29.27 for (Google Transport Security); Tue, 02 Apr 2019 04:29:27 -0700 (PDT) Received-SPF: pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; X-Received: by 2002:a17:902:8f92:: with SMTP id z18mr17247416plo.123.1554204567395; Tue, 02 Apr 2019 04:29:27 -0700 (PDT) Received: from jagan-XPS-13-9350.imgcgcw.net ([147.50.13.10]) by smtp.gmail.com with ESMTPSA id u62sm23992715pfa.124.2019.04.02.04.29.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 04:29:26 -0700 (PDT) From: Jagan Teki To: Simon Glass , Tom Rini , Neil Armstrong , Philipp Tomsich , Marek Vasut , Stefano Babic , Fabio Estevam , Peng Fan , Maxime Ripard , Michael Trimarchi , Andre Przywara Cc: u-boot@lists.denx.de, uboot-imx@nxp.com, Shyam Saini , linux-amarula@amarulasolutions.com, Jagan Teki Subject: [PATCH v2 05/10] clk: imx: Add imx6q clock tree support Date: Tue, 2 Apr 2019 16:58:38 +0530 Message-Id: <20190402112843.992-6-jagan@amarulasolutions.com> X-Mailer: git-send-email 2.18.0.321.gffc6fa0e3 In-Reply-To: <20190402112843.992-1-jagan@amarulasolutions.com> References: <20190402112843.992-1-jagan@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: jagan@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=RT1Heyi0; spf=pass (google.com: domain of jagan@amarulasolutions.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jagan@amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , i.MX6 clock control module comprise of parent clocks, gates, multiplexers, dividers, PODF, PLL, fixed rate and etc. So, the U-Boot implementation of ccm has divided into gates and tree. 1) gate clocks are generic clock configuration of enable/disable bit management which can be handle via imx6_clock_gate. 2) tree clocks are handle via tree clock management where it link the clocks based on the parent clock which usually required to get and set the clock rates. This patch add tree clock management for imx6q USDHC clocks, so the mmc driver from imx6 can eventually use this so getting the USDHC clock rates. Unlike Linux, U-Boot implementation may not require to maintain exact clock tree due to various constrains and use cases. So here is how the clock tree differs between them. usdhc clock tree in Linux: ------------------------- USDHC1 => USDHC1_PODF => USDHC1_SEL => PLL2_PFD2 => PLL2_BUS => PLL2_BYPASS => PLL2 => OSC usdhc clock tree in U-Boot: --------------------------- USDHC1 => USDHC1_PODF => USDHC1_SEL => PLL2_PFD2 => PLL2_BUS => OSC Signed-off-by: Jagan Teki --- arch/arm/include/asm/arch-mx6/clock.h | 65 ++++++++++++++++ drivers/clk/imx/clk-imx6-common.c | 103 ++++++++++++++++++++++++++ drivers/clk/imx/clk-imx6q.c | 70 +++++++++++++++++ 3 files changed, 238 insertions(+) diff --git a/arch/arm/include/asm/arch-mx6/clock.h b/arch/arm/include/asm/arch-mx6/clock.h index fa921a9f08..424231c691 100644 --- a/arch/arm/include/asm/arch-mx6/clock.h +++ b/arch/arm/include/asm/arch-mx6/clock.h @@ -21,6 +21,67 @@ #define MXC_CLK32 32768 #endif +#define OSC_24M_ULL 24000000ULL + +enum imx6_clk_type { + IMX6_CLK_TYPE_SIMPLE = 0, + IMX6_CLK_TYPE_FIXED, + IMX6_CLK_TYPE_DIV, + IMX6_CLK_TYPE_MUX, + IMX6_CLK_TYPE_PLL_PFD, + IMX6_CLK_TYPE_PLL_DIV, +}; + +/** + * struct imx6_clk_tree - imx6 ccm clock tree + * + * @parent: parent clock tree + * @type: clock type + * @off: register offset of the specified clock + * @shift: number of bits to shift the bitfield + * @width: width of the bitfield + * @idx: index of the specified clock + * @fixed_rate: fixed clock rate + */ +struct imx6_clk_tree { + const unsigned long *parent; + enum imx6_clk_type type; + u16 off; + + u8 shift; + u8 width; + u8 idx; + ulong fixed_rate; +}; + +#define TREE(_parent, _type, _off, _shift, _width, _idx, _fixed_rate) { \ + .parent = _parent, \ + .type = _type, \ + .off = _off, \ + .shift = _shift, \ + .width = _width, \ + .idx = _idx, \ + .fixed_rate = _fixed_rate, \ +} + +#define SIMPLE(_parent) \ + TREE(_parent, IMX6_CLK_TYPE_SIMPLE, 0, 0, 0, 0, 0) + +#define FIXED(_fixed_rate) \ + TREE(NULL, IMX6_CLK_TYPE_FIXED, 0, 0, 0, 0, _fixed_rate) + +#define DIV(_parent, _off, _shift, _width) \ + TREE(_parent, IMX6_CLK_TYPE_DIV, _off, _shift, _width, 0, 0) + +#define MUX(_parent, _off, _shift, _width) \ + TREE(_parent, IMX6_CLK_TYPE_MUX, _off, _shift, _width, 0, 0) + +#define PLL_PFD(_parent, _off, _width, _idx) \ + TREE(_parent, IMX6_CLK_TYPE_PLL_PFD, _off, 0, _width, _idx, 0) + +#define PLL_DIV(_parent, _off, _shift, _width) \ + TREE(_parent, IMX6_CLK_TYPE_PLL_DIV, _off, _shift, _width, 0, 0) + /** * struct imx6_clk_gate - imx6 ccm clock gate * @@ -41,19 +102,23 @@ struct imx6_clk_gate { * struct imx6_clk_desc - imx6 clock control module descriptor * * @gates: ccm clock gates + * @tree: ccm clock tree */ struct imx6_clk_desc { const struct imx6_clk_gate *gates; + const struct imx6_clk_tree *tree; }; /** * struct imx6_clk_priv - imx6 clock control module * * @base: ccm base address + * @anatop: anatop base address * @desc: ccm descriptor */ struct imx6_clk_priv { void *base; + void *anatop; const struct imx6_clk_desc *desc; }; diff --git a/drivers/clk/imx/clk-imx6-common.c b/drivers/clk/imx/clk-imx6-common.c index 1d38f51f7e..d21facf2e5 100644 --- a/drivers/clk/imx/clk-imx6-common.c +++ b/drivers/clk/imx/clk-imx6-common.c @@ -6,18 +6,117 @@ #include #include +#include #include #include #include #include #include +#include + static const struct imx6_clk_gate *priv_to_gate(struct imx6_clk_priv *priv, unsigned long id) { return &priv->desc->gates[id]; } +static const struct imx6_clk_tree *priv_to_tree(struct imx6_clk_priv *priv, + unsigned long id) +{ + return &priv->desc->tree[id]; +} + +static u8 get_bitfield(void *base, u8 shift, u8 width) +{ + return (readl(base) >> shift) & clk_div_mask(width); +} + +static u8 get_mux_parent(const struct imx6_clk_tree *tree, void *base) +{ + u8 idx = get_bitfield(base + tree->off, tree->shift, tree->width); + + return tree->parent[idx]; +} + +static ulong get_pll2_bus_rate(struct imx6_clk_priv *priv, unsigned long id, + ulong parent_rate) +{ + const struct imx6_clk_tree *tree = priv_to_tree(priv, id); + u8 div; + + div = get_bitfield(priv->anatop + tree->off, tree->shift, tree->width); + return (div == 1) ? parent_rate * 22 : parent_rate * 20; +} + +static ulong get_pfd_rate(struct imx6_clk_priv *priv, unsigned long id, + ulong parent_rate) +{ + const struct imx6_clk_tree *tree = priv_to_tree(priv, id); + u64 tmp = parent_rate; + u8 frac; + + frac = get_bitfield(priv->anatop + tree->off, + tree->idx * 8, tree->width); + tmp *= 18; + do_div(tmp, frac); + + return tmp; +} + +static ulong get_mux_rate(ulong parent_rate) +{ + /* derive clock from respective parent */ + return parent_rate; +} + +static ulong get_div_rate(struct imx6_clk_priv *priv, unsigned long id, + ulong parent_rate) +{ + const struct imx6_clk_tree *tree = priv_to_tree(priv, id); + u8 podf; + + podf = get_bitfield(priv->base + tree->off, tree->shift, tree->width); + return parent_rate / (podf + 1); +} + +static ulong imx6_calc_clk_rate(struct imx6_clk_priv *priv, unsigned long id) +{ + const struct imx6_clk_tree *tree = priv_to_tree(priv, id); + ulong rate = 0; + + switch (tree->type) { + case IMX6_CLK_TYPE_FIXED: + return tree->fixed_rate; + case IMX6_CLK_TYPE_PLL_DIV: + rate = imx6_calc_clk_rate(priv, tree->parent[0]); + return get_pll2_bus_rate(priv, id, rate); + case IMX6_CLK_TYPE_PLL_PFD: + rate = imx6_calc_clk_rate(priv, tree->parent[0]); + return get_pfd_rate(priv, id, rate); + case IMX6_CLK_TYPE_MUX: + rate = imx6_calc_clk_rate(priv, + get_mux_parent(tree, priv->base)); + return get_mux_rate(rate); + case IMX6_CLK_TYPE_DIV: + rate = imx6_calc_clk_rate(priv, tree->parent[0]); + return get_div_rate(priv, id, rate); + case IMX6_CLK_TYPE_SIMPLE: + return imx6_calc_clk_rate(priv, tree->parent[0]); + default: + printf("%s: (TYPE#%d) unhandled\n", __func__, tree->type); + } + + return rate; +} + +static ulong imx6_clk_get_rate(struct clk *clk) +{ + struct imx6_clk_priv *priv = dev_get_priv(clk->dev); + + return imx6_calc_clk_rate(priv, clk->id); +} + static int imx6_set_gate(struct clk *clk, bool on) { struct imx6_clk_priv *priv = dev_get_priv(clk->dev); @@ -51,6 +150,7 @@ static int imx6_clk_disable(struct clk *clk) struct clk_ops imx6_clk_ops = { .enable = imx6_clk_enable, .disable = imx6_clk_disable, + .get_rate = imx6_clk_get_rate, }; int imx6_clk_probe(struct udevice *dev) @@ -61,6 +161,9 @@ int imx6_clk_probe(struct udevice *dev) if (!priv->base) return -ENOMEM; + /* FIXME get the anatop base via OF_LIVE */ + priv->anatop = priv->base + 0x4000; + priv->desc = (const struct imx6_clk_desc *)dev_get_driver_data(dev); if (!priv->desc) return -EINVAL; diff --git a/drivers/clk/imx/clk-imx6q.c b/drivers/clk/imx/clk-imx6q.c index 8ec713298d..a46d2f6f88 100644 --- a/drivers/clk/imx/clk-imx6q.c +++ b/drivers/clk/imx/clk-imx6q.c @@ -10,6 +10,75 @@ #include #include +static const unsigned long pll2_bus[] = { + IMX6QDL_CLK_OSC, +}; + +static const unsigned long pfd_352m[] = { + IMX6QDL_CLK_PLL2_BUS, +}; + +static const unsigned long usdhc_sel[] = { + IMX6QDL_CLK_PLL2_PFD2_396M, + IMX6QDL_CLK_PLL2_PFD0_352M, +}; + +static const unsigned long usdhc1_podf[] = { + IMX6QDL_CLK_USDHC1_SEL, +}; + +static const unsigned long usdhc2_podf[] = { + IMX6QDL_CLK_USDHC2_SEL, +}; + +static const unsigned long usdhc3_podf[] = { + IMX6QDL_CLK_USDHC3_SEL, +}; + +static const unsigned long usdhc4_podf[] = { + IMX6QDL_CLK_USDHC4_SEL, +}; + +static const unsigned long usdhc1[] = { + IMX6QDL_CLK_USDHC1_PODF, +}; + +static const unsigned long usdhc2[] = { + IMX6QDL_CLK_USDHC2_PODF, +}; + +static const unsigned long usdhc3[] = { + IMX6QDL_CLK_USDHC3_PODF, +}; + +static const unsigned long usdhc4[] = { + IMX6QDL_CLK_USDHC4_PODF, +}; + +static const struct imx6_clk_tree imx6q_tree[] = { + [IMX6QDL_CLK_OSC] = FIXED(OSC_24M_ULL), + + [IMX6QDL_CLK_PLL2_BUS] = PLL_DIV(pll2_bus, 0x30, 13, 1), + + [IMX6QDL_CLK_PLL2_PFD0_352M] = PLL_PFD(pfd_352m, 0x100, 6, 0), + [IMX6QDL_CLK_PLL2_PFD2_396M] = PLL_PFD(pfd_352m, 0x100, 6, 2), + + [IMX6QDL_CLK_USDHC1_SEL] = MUX(usdhc_sel, 0x01c, 16, 1), + [IMX6QDL_CLK_USDHC2_SEL] = MUX(usdhc_sel, 0x01c, 17, 1), + [IMX6QDL_CLK_USDHC3_SEL] = MUX(usdhc_sel, 0x01c, 18, 1), + [IMX6QDL_CLK_USDHC4_SEL] = MUX(usdhc_sel, 0x01c, 19, 1), + + [IMX6QDL_CLK_USDHC1_PODF] = DIV(usdhc1_podf, 0x024, 11, 3), + [IMX6QDL_CLK_USDHC2_PODF] = DIV(usdhc2_podf, 0x024, 16, 3), + [IMX6QDL_CLK_USDHC3_PODF] = DIV(usdhc3_podf, 0x024, 19, 3), + [IMX6QDL_CLK_USDHC4_PODF] = DIV(usdhc4_podf, 0x024, 22, 3), + + [IMX6QDL_CLK_USDHC1] = SIMPLE(usdhc1), + [IMX6QDL_CLK_USDHC2] = SIMPLE(usdhc2), + [IMX6QDL_CLK_USDHC3] = SIMPLE(usdhc3), + [IMX6QDL_CLK_USDHC4] = SIMPLE(usdhc4), +}; + static const struct imx6_clk_gate imx6q_gates[] = { [IMX6QDL_CLK_USDHC1] = GATE(0x080, GENMASK(3, 2)), [IMX6QDL_CLK_USDHC2] = GATE(0x080, GENMASK(5, 4)), @@ -19,6 +88,7 @@ static const struct imx6_clk_gate imx6q_gates[] = { static const struct imx6_clk_desc imx6q_clk_desc = { .gates = imx6q_gates, + .tree = imx6q_tree, }; static const struct udevice_id clk_imx6q_ids[] = {