From patchwork Sat Feb 24 20:20:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 13570601 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1C4851C23 for ; Sat, 24 Feb 2024 20:21:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708806067; cv=none; b=jAEq+m8v6uZY2BAN+djlcfviIg4cGWPr+jfTD3daeixmrdbN6UMHpO37+CzO3RhqkG8rbGLa0ZCXEqk8SLRfSD7S4zR1DWJpF48hFlxsdztwU3dUAKUoAEFplY6UcLmjpV1/nLuiSf4FivYRndR1Fbij6h8R7L+zz0eOg2lzkIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708806067; c=relaxed/simple; bh=9IGEijDoQiVXlFlrlwF4bqCIQO/DVJF6uV65BmN5IJk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ceKlKgsx/4N1nz4TnwICPxwGsETQkUil+CMGnIvw4cFTLiWmSHqTBlcc0gsc/n2zO0bOVo3SQ0wZ7zVygK+TJATGdOaQu/BIm3IncZFhpFTJuTAlwnGOemRs0SXi7T4byb2N1kRlCvihy6ejzFbvlDwFNIpgSyUi/8wQt+Krnw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=jR201hfe; arc=none smtp.client-ip=209.85.167.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="jR201hfe" Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3c19b79535aso69784b6e.0 for ; Sat, 24 Feb 2024 12:21:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708806064; x=1709410864; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bRzpcpTlkocgklC/igIeXP7AAH24T8PbXBQTZ2YqBko=; b=jR201hfe3oJ3bwb2wR25ocp7RuUVPuSglZ7EJB0ecgoDamrm+wyD/uV1SQD8IwSJUw dkUdfm03NRjCNI0Utzs2xoMyiQpID5Jonx1WjZMYKceAsH8pqx6rRWJ5sq5gPSGSGjkZ D4hd5G8MKzzWkuHG69iNb5DU2oOlw/+WP/ZZraxIVItIIzPiBTMLTxdWbSkd3gfTgtOQ TG8ve8/fCfrMlfTKsfYmFEt1S8g+p3kRsdLQ/m5zaELsNuXl/H5mnaxj3XBEL+OHG228 vA9hqVvWqAPP5clDtCJ68/71QRzDCHZYK3uV3niT13hUUazPCWXxY0xDe6kaalDlb8cF dsZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708806064; x=1709410864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bRzpcpTlkocgklC/igIeXP7AAH24T8PbXBQTZ2YqBko=; b=kqP+DPqp+m9jIcts1ElW3+hPO0cG1kdom6j7owKXJwMsBma3veH2NQPVv8Vz979Qhb GoWsHmX+er2+hXN0g/tpj/DXz/26iMh1YQ5T7M71WPXapvBA1/ZQ9+wVF7qQeNtv2ZOj MPGC+HBAW/cTjH8CDDTnJpyA88epshD73MrwilM8/wMo3BYtMrQww7i/TzuWTYIkHiI/ l/z+T2iIo+fjg6NyBviiFpDTa3nKaOkq7b9Fr/LXH4tZ/SD+g1FWMCjMc0fTgPvwItMr nQY9MQss+O7/0yYBl+c1mj+IQUgXu8zqItHO0c894V6MQhMQrg16iG7ET3HrKcBP17u2 bxdQ== X-Forwarded-Encrypted: i=1; AJvYcCV7nOpPTcm4IOYKni+jPatTHoutTB0inahfpYlq6J0rbYFfynwXtUdV8o7plYUAt8hfmqGxOfO2bsjiGGvzNH1lVe9KjGpwWeYL X-Gm-Message-State: AOJu0Yx9LDK70M6tKikJR28AQ2ySp5NTxOjJNH/tgC8iPh7v7CmP45Y7 x82Zk1Tli7Gm5R+1ESRRxXpJ+4d4d2/Fl/BiVs0VuBHBzYPSayPGua/tEZiHn6g= X-Google-Smtp-Source: AGHT+IHdr3BVt2CyrCrzGMJ5Kh3DUvKcHv+fa9gzXmAALOTHsIjD4YFJgQ0NSc7TzQkObjCSfABbYA== X-Received: by 2002:a54:448b:0:b0:3c1:377a:4641 with SMTP id v11-20020a54448b000000b003c1377a4641mr3051812oiv.24.1708806063814; Sat, 24 Feb 2024 12:21:03 -0800 (PST) Received: from localhost ([136.62.192.75]) by smtp.gmail.com with ESMTPSA id bp4-20020a056808238400b003c19a459283sm132430oib.17.2024.02.24.12.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Feb 2024 12:21:03 -0800 (PST) From: Sam Protsenko To: Krzysztof Kozlowski , Sylwester Nawrocki , Chanwoo Choi Cc: Alim Akhtar , Michael Turquette , Stephen Boyd , Rob Herring , Conor Dooley , Tomasz Figa , linux-samsung-soc@vger.kernel.org, linux-clk@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 12/15] clk: samsung: Add CPU clock support for Exynos850 Date: Sat, 24 Feb 2024 14:20:50 -0600 Message-Id: <20240224202053.25313-13-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240224202053.25313-1-semen.protsenko@linaro.org> References: <20240224202053.25313-1-semen.protsenko@linaro.org> Precedence: bulk X-Mailing-List: linux-clk@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Implement CPU clock control for Exynos850 SoC. It follows the same procedure which is already implemented for other SoCs in clk-cpu.c: 1. Set the correct rate for the alternate parent (if needed) before switching to use it as the CPU clock 2. Switch to the alternate parent, so the CPU continues to get clocked while the PLL is being re-configured 3. Adjust the dividers for the CPU related buses (ACLK, ATCLK, etc) 4. Re-configure the PLL for the new CPU clock rate. It's done automatically, as the CPU clock rate change propagates to the PLL clock, because the CPU clock has CLK_SET_RATE_PARENT flag set in exynos_register_cpu_clock() 5. Once the PLL is locked, set it back as the CPU clock source 6. Set alternate parent clock rate back to max speed As in already existing clk-cpu.c code, the divider and mux clocks are configured in a low-level fashion (using direct register access instead of CCF API), to avoid affecting how DIV and MUX clock flags are declared in the actual clock driver (clk-exynos850.c). No functional change. This patch adds support for Exynos850 CPU clock, but doesn't enable it per se. Signed-off-by: Sam Protsenko --- Changes in v3: - Merged struct exynos850_cpuclk_regs fields into struct exynos_cpuclk_regs Changes in v2: - Changed .ctrl_base field to .base, to reflect changes made in previous commits drivers/clk/samsung/clk-cpu.c | 177 ++++++++++++++++++++++++++++++++++ drivers/clk/samsung/clk-cpu.h | 4 + 2 files changed, 181 insertions(+) diff --git a/drivers/clk/samsung/clk-cpu.c b/drivers/clk/samsung/clk-cpu.c index 0cf288138167..6ac884fccac6 100644 --- a/drivers/clk/samsung/clk-cpu.c +++ b/drivers/clk/samsung/clk-cpu.c @@ -29,6 +29,7 @@ * down in order to keep the output clock rate within the previous OPP limits. */ +#include #include #include #include @@ -51,6 +52,8 @@ typedef int (*exynos_rate_change_fn_t)(struct clk_notifier_data *ndata, * @div_cpu1: offset of CPU DIV1 register (for modifying divider values) * @div_stat_cpu0: offset of CPU DIV0_STAT register (for checking DIV status) * @div_stat_cpu1: offset of CPU DIV1_STAT register (for checking DIV status) + * @mux: offset of MUX register for choosing CPU clock source + * @divs: offsets of DIV registers (ACLK, ATCLK, PCLKDBG and PERIPHCLK) */ struct exynos_cpuclk_regs { u32 mux_sel; @@ -59,6 +62,9 @@ struct exynos_cpuclk_regs { u32 div_cpu1; u32 div_stat_cpu0; u32 div_stat_cpu1; + + u32 mux; + u32 divs[4]; }; /** @@ -397,6 +403,167 @@ static int exynos5433_cpuclk_post_rate_change(struct clk_notifier_data *ndata, return 0; } +/* ---- Exynos850 ----------------------------------------------------------- */ + +#define E850_DIV_RATIO_MASK GENMASK(3, 0) +#define E850_BUSY_MASK BIT(16) + +/* Max time for divider or mux to stabilize, usec */ +#define E850_DIV_MUX_STAB_TIME 100 +/* OSCCLK clock rate, Hz */ +#define E850_OSCCLK (26 * MHZ) + +static const struct exynos_cpuclk_regs e850cl0_cpuclk_regs = { + .mux = 0x100c, + .divs = { 0x1800, 0x1808, 0x180c, 0x1810 }, +}; + +static const struct exynos_cpuclk_regs e850cl1_cpuclk_regs = { + .mux = 0x1000, + .divs = { 0x1800, 0x1808, 0x180c, 0x1810 }, +}; + +/* + * Set alternate parent rate to "rate" value or less. + * + * rate: Desired alt_parent rate, or 0 for max alt_parent rate + * + * Exynos850 doesn't have CPU clock divider in CMU_CPUCLx block (CMUREF divider + * doesn't affect CPU speed). So CPUCLx_SWITCH divider from CMU_TOP is used + * instead to adjust alternate parent speed. + * + * It's possible to use clk_set_max_rate() instead of this function, but it + * would set overly pessimistic rate values to alternate parent. + */ +static int exynos850_alt_parent_set_max_rate(const struct clk_hw *alt_parent, + unsigned long rate) +{ + struct clk_hw *clk_div, *clk_divp; + unsigned long divp_rate, div_rate, div; + int ret; + + /* Divider from CMU_TOP */ + clk_div = clk_hw_get_parent(alt_parent); + if (!clk_div) + return -ENOENT; + /* Divider's parent from CMU_TOP */ + clk_divp = clk_hw_get_parent(clk_div); + if (!clk_divp) + return -ENOENT; + /* Divider input rate */ + divp_rate = clk_hw_get_rate(clk_divp); + if (!divp_rate) + return -EINVAL; + + /* Calculate new alt_parent rate for integer divider value */ + if (rate == 0) + div = 1; + else + div = DIV_ROUND_UP(divp_rate, rate); + div_rate = DIV_ROUND_UP(divp_rate, div); + WARN_ON(div >= MAX_DIV); + + /* alt_parent will propagate this change up to the divider */ + ret = clk_set_rate(alt_parent->clk, div_rate); + if (ret) + return ret; + udelay(E850_DIV_MUX_STAB_TIME); + + return 0; +} + +/* Handler for pre-rate change notification from parent clock */ +static int exynos850_cpuclk_pre_rate_change(struct clk_notifier_data *ndata, + struct exynos_cpuclk *cpuclk) +{ + const unsigned int shifts[4] = { 16, 12, 8, 4 }; /* E850_CPU_DIV0() */ + const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs; + const struct exynos_cpuclk_cfg_data *cfg_data = cpuclk->cfg; + const struct clk_hw *alt_parent = cpuclk->alt_parent; + void __iomem *base = cpuclk->base; + unsigned long alt_prate = clk_hw_get_rate(alt_parent); + unsigned long flags; + u32 mux_reg; + size_t i; + int ret; + + /* No actions are needed when switching to or from OSCCLK parent */ + if (ndata->new_rate == E850_OSCCLK || ndata->old_rate == E850_OSCCLK) + return 0; + + /* Find out the divider values to use for clock data */ + while ((cfg_data->prate * 1000) != ndata->new_rate) { + if (cfg_data->prate == 0) + return -EINVAL; + cfg_data++; + } + + /* + * If the old parent clock speed is less than the clock speed of + * the alternate parent, then it should be ensured that at no point + * the armclk speed is more than the old_prate until the dividers are + * set. Also workaround the issue of the dividers being set to lower + * values before the parent clock speed is set to new lower speed + * (this can result in too high speed of armclk output clocks). + */ + if (alt_prate > ndata->old_rate || ndata->old_rate > ndata->new_rate) { + unsigned long tmp_rate = min(ndata->old_rate, ndata->new_rate); + + ret = exynos850_alt_parent_set_max_rate(alt_parent, tmp_rate); + if (ret) + return ret; + } + + spin_lock_irqsave(cpuclk->lock, flags); + + /* Select the alternate parent */ + mux_reg = readl(base + regs->mux); + writel(mux_reg | 1, base + regs->mux); + wait_until_mux_stable(base + regs->mux, 16, 1, 0); + + /* Alternate parent is active now. Set the dividers */ + for (i = 0; i < ARRAY_SIZE(shifts); ++i) { + unsigned long div = (cfg_data->div0 >> shifts[i]) & 0xf; + u32 val; + + val = readl(base + regs->divs[i]); + val = (val & ~E850_DIV_RATIO_MASK) | div; + writel(val, base + regs->divs[i]); + wait_until_divider_stable(base + regs->divs[i], E850_BUSY_MASK); + } + + spin_unlock_irqrestore(cpuclk->lock, flags); + + return 0; +} + +/* Handler for post-rate change notification from parent clock */ +static int exynos850_cpuclk_post_rate_change(struct clk_notifier_data *ndata, + struct exynos_cpuclk *cpuclk) +{ + const struct exynos_cpuclk_regs * const regs = cpuclk->chip->regs; + const struct clk_hw *alt_parent = cpuclk->alt_parent; + void __iomem *base = cpuclk->base; + unsigned long flags; + u32 mux_reg; + + /* No actions are needed when switching to or from OSCCLK parent */ + if (ndata->new_rate == E850_OSCCLK || ndata->old_rate == E850_OSCCLK) + return 0; + + spin_lock_irqsave(cpuclk->lock, flags); + + /* Select main parent (PLL) for mux */ + mux_reg = readl(base + regs->mux); + writel(mux_reg & ~1, base + regs->mux); + wait_until_mux_stable(base + regs->mux, 16, 1, 0); + + spin_unlock_irqrestore(cpuclk->lock, flags); + + /* Set alt_parent rate back to max */ + return exynos850_alt_parent_set_max_rate(alt_parent, 0); +} + /* -------------------------------------------------------------------------- */ /* Common round rate callback usable for all types of CPU clocks */ @@ -459,6 +626,16 @@ static const struct exynos_cpuclk_chip exynos_clkcpu_chips[] = { .pre_rate_cb = exynos5433_cpuclk_pre_rate_change, .post_rate_cb = exynos5433_cpuclk_post_rate_change, }, + [CPUCLK_LAYOUT_E850_CL0] = { + .regs = &e850cl0_cpuclk_regs, + .pre_rate_cb = exynos850_cpuclk_pre_rate_change, + .post_rate_cb = exynos850_cpuclk_post_rate_change, + }, + [CPUCLK_LAYOUT_E850_CL1] = { + .regs = &e850cl1_cpuclk_regs, + .pre_rate_cb = exynos850_cpuclk_pre_rate_change, + .post_rate_cb = exynos850_cpuclk_post_rate_change, + }, }; /* helper function to register a CPU clock */ diff --git a/drivers/clk/samsung/clk-cpu.h b/drivers/clk/samsung/clk-cpu.h index 4382ab005ad3..892843611b0a 100644 --- a/drivers/clk/samsung/clk-cpu.h +++ b/drivers/clk/samsung/clk-cpu.h @@ -17,10 +17,14 @@ * enum exynos_cpuclk_layout - CPU clock registers layout compatibility * @CPUCLK_LAYOUT_E4210: Exynos4210 compatible layout * @CPUCLK_LAYOUT_E5433: Exynos5433 compatible layout + * @CPUCLK_LAYOUT_E850_CL0: Exynos850 cluster 0 compatible layout + * @CPUCLK_LAYOUT_E850_CL1: Exynos850 cluster 1 compatible layout */ enum exynos_cpuclk_layout { CPUCLK_LAYOUT_E4210, CPUCLK_LAYOUT_E5433, + CPUCLK_LAYOUT_E850_CL0, + CPUCLK_LAYOUT_E850_CL1, }; /**