Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp728830img; Thu, 21 Mar 2019 07:46:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqxj7gUCsdgRiO81PlDUJakjOlq3k/UrlSewECFkVZuBh6s4WfoQQJ717V24TSmZy9unnnik X-Received: by 2002:a63:6bc6:: with SMTP id g189mr3690636pgc.427.1553179565442; Thu, 21 Mar 2019 07:46:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553179565; cv=none; d=google.com; s=arc-20160816; b=PDNZEhVoKI6YhFP7ecT4SUrYl4dkMUIoyE5Nf+xD0hogoDyEhz5bs3VPN/fzrOsW2o TXSI5FUI9A3a2W0LlNhNrhvZu+25JiYVUG41bwJIgWAwm3Wi56bhg2m87R+hga5wwyXH 7KMIIEr5/9c2uyohjza7ahsTCURHa7IbR2iDZCIcDVLHU6VflyAavUVq+7WM9s7TxFzY LFygtMIF72/8KIz9Q62ZUuGNb6ztq2wyPQcmpInV/0QmdQkKyvZrPzH07/iBnP61pHyz GNWUxRN3xCSXohzwPPpEtWX4S7NxIZWc/hqwMfzHkBMJ+5Ai0iO31QDtq7DsmtPgtNb0 /YMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=pd7DZgFZHydc22jsnva6HCwIQ6qYSallrsNWhLPd39k=; b=h83vSzLJSkIiiWRh9Nso56s7Xf74n0ghp7o2tpUb4un92AvOtOoxILf3VxlQXi2ofp Wp4QroEGQH5DJQ/51vvnJvhBjPkIMP1N49z52AnoWYqbZ9mbJlIoq80cLN6c160QSzbR EcPUgHSvTwkSFAhNVONnjlR8BzRtVYd3eVBfCWDmdTlYfObyPmCL+gTqJZmBYK/uZvuE 22s0gWJboUa8X1IOhAnBspTI9GHj+uWcCnanlhZtDqSggcgMA87YsOKT6gFtuwBa9f0B bfqXg2TmDnPEJXgPak0OBY+BGbxb67Gb5VbQ9Yo0zG1O956/rfCNS3pbeTnw3ppqNgdd MxVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=rjVNMVrA; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d12si4560294pgt.59.2019.03.21.07.45.47; Thu, 21 Mar 2019 07:46:05 -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=@gmail.com header.s=20161025 header.b=rjVNMVrA; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728138AbfCUOoI (ORCPT + 99 others); Thu, 21 Mar 2019 10:44:08 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:43329 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728326AbfCUOoH (ORCPT ); Thu, 21 Mar 2019 10:44:07 -0400 Received: by mail-pf1-f195.google.com with SMTP id c8so4426749pfd.10; Thu, 21 Mar 2019 07:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=pd7DZgFZHydc22jsnva6HCwIQ6qYSallrsNWhLPd39k=; b=rjVNMVrAZDzk6WalHOKZlusXykVZ74sm6Da1S3WemT13MtWwKW0ojyarUbnDoX9PAs x/Ug4Oo4BUhuxm20/g8Lw74Axo8oVLZYDjnc+HnGNfV3Hpc3f33DJogpHhKhW5Py1T8T UdIexXnKF+1jex93Knu1GMZ4YyAD//pJQ8GCf3nqBTeBUKvVe+uMDM0p847iOqiFC++P vXRXPPCagKrgtTAbZf0ZuBhHBVGfwarhqRTShpJPDSyXBbDGdRcHtwit1xUdMAznsPfp Ok5WpD3EoydwuQU+04j74qLKpDew6ccsAmUm67590ksFaRZSn7LfTR6SiBrnhZjIMA8A cK9g== 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:mime-version :content-transfer-encoding; bh=pd7DZgFZHydc22jsnva6HCwIQ6qYSallrsNWhLPd39k=; b=l+esABcq76rBxa0uDwHveI86JtKNmfmfLB2Ytmx3aXmlmTsl2PR+iurgC2dk7EfIEr 9zbzu6VrWDScbz5ckQIUkTirJCVzJ5jdBAp3Iu0luzpJsk0F9kKzxxqTQ2QWIyNIlZmi ZzYljjsk/axgbn4ETMxhYVoqHVmFcfzu73CtZqW5+zRu08sZwBmD6aOmt3kcitBs/Feo c8h25GW3czdYRW97jdh4XHhXolclF+BQlCB6ylj0AQGI1IVmrDr51Myd+eA2c326bVrG C6Kl0hSa222FkVHrJIIBpYqK6PYXC846y2/qc2YZGWdicsXyusUUQyGWUCegXTE7GmT+ SyDw== X-Gm-Message-State: APjAAAXh9j3xUM1s7v+VITbZuKs7DrL2Z4dS1rYYeR6AVKMV+bHv+sOf 8zOnS80DwlDStQvXTUD09S3a5AXAFLSJkg== X-Received: by 2002:a62:4414:: with SMTP id r20mr3663491pfa.37.1553179446705; Thu, 21 Mar 2019 07:44:06 -0700 (PDT) Received: from GuoGuo-OMEN.lan ([107.151.139.128]) by smtp.gmail.com with ESMTPSA id f4sm6158811pgq.94.2019.03.21.07.44.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Mar 2019 07:44:06 -0700 (PDT) From: Chuanhong Guo To: linux-mips@vger.kernel.org Cc: Chuanhong Guo , Weijie Gao , Ralf Baechle , Paul Burton , James Hogan , John Crispin , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v2] MIPS: ralink: fix cpu clock of mt7621 and add dt clk devices Date: Thu, 21 Mar 2019 22:42:51 +0800 Message-Id: <20190321144251.28313-1-gch981213@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For a long time the mt7621 uses a fixed cpu clock which causes a problem if the cpu frequency is not 880MHz. This patch fixes the cpu clock calculation and adds the cpu/bus clkdev which will be used in dts. Signed-off-by: Weijie Gao Ported from OpenWrt: c7ca224299 ramips: fix cpu clock of mt7621 and add dt clk devices Signed-off-by: Chuanhong Guo --- Change since v1: Correctly fix the following checkpatch.pl warning: WARNING: Missing a blank line after declarations #137: FILE: arch/mips/ralink/mt7621.c:146: + u32 xtal_clk, cpu_clk, bus_clk; + const static u32 prediv_tbl[] = {0, 1, 2, 2}; arch/mips/include/asm/mach-ralink/mt7621.h | 20 ++++ arch/mips/ralink/mt7621.c | 102 ++++++++++++++------- arch/mips/ralink/timer-gic.c | 4 +- 3 files changed, 93 insertions(+), 33 deletions(-) diff --git a/arch/mips/include/asm/mach-ralink/mt7621.h b/arch/mips/include/asm/mach-ralink/mt7621.h index a672e06fa5fd..b8a8834164c8 100644 --- a/arch/mips/include/asm/mach-ralink/mt7621.h +++ b/arch/mips/include/asm/mach-ralink/mt7621.h @@ -19,6 +19,10 @@ #define SYSC_REG_CHIP_REV 0x0c #define SYSC_REG_SYSTEM_CONFIG0 0x10 #define SYSC_REG_SYSTEM_CONFIG1 0x14 +#define SYSC_REG_CLKCFG0 0x2c +#define SYSC_REG_CUR_CLK_STS 0x44 + +#define MEMC_REG_CPU_PLL 0x648 #define CHIP_REV_PKG_MASK 0x1 #define CHIP_REV_PKG_SHIFT 16 @@ -26,6 +30,22 @@ #define CHIP_REV_VER_SHIFT 8 #define CHIP_REV_ECO_MASK 0xf +#define XTAL_MODE_SEL_MASK 0x7 +#define XTAL_MODE_SEL_SHIFT 6 + +#define CPU_CLK_SEL_MASK 0x3 +#define CPU_CLK_SEL_SHIFT 30 + +#define CUR_CPU_FDIV_MASK 0x1f +#define CUR_CPU_FDIV_SHIFT 8 +#define CUR_CPU_FFRAC_MASK 0x1f +#define CUR_CPU_FFRAC_SHIFT 0 + +#define CPU_PLL_PREDIV_MASK 0x3 +#define CPU_PLL_PREDIV_SHIFT 12 +#define CPU_PLL_FBDIV_MASK 0x7f +#define CPU_PLL_FBDIV_SHIFT 4 + #define MT7621_DRAM_BASE 0x0 #define MT7621_DDR2_SIZE_MIN 32 #define MT7621_DDR2_SIZE_MAX 256 diff --git a/arch/mips/ralink/mt7621.c b/arch/mips/ralink/mt7621.c index d2718de60b9b..6828eefb0a85 100644 --- a/arch/mips/ralink/mt7621.c +++ b/arch/mips/ralink/mt7621.c @@ -9,22 +9,22 @@ #include #include +#include +#include +#include +#include #include #include #include #include #include +#include #include #include "common.h" -#define SYSC_REG_SYSCFG 0x10 -#define SYSC_REG_CPLL_CLKCFG0 0x2c -#define SYSC_REG_CUR_CLK_STS 0x44 -#define CPU_CLK_SEL (BIT(30) | BIT(31)) - #define MT7621_GPIO_MODE_UART1 1 #define MT7621_GPIO_MODE_I2C 2 #define MT7621_GPIO_MODE_UART3_MASK 0x3 @@ -110,49 +110,89 @@ static struct rt2880_pmx_group mt7621_pinmux_data[] = { { 0 } }; +static struct clk *clks[MT7621_CLK_MAX]; +static struct clk_onecell_data clk_data = { + .clks = clks, + .clk_num = ARRAY_SIZE(clks), +}; + phys_addr_t mips_cpc_default_phys_base(void) { panic("Cannot detect cpc address"); } +static struct clk *__init mt7621_add_sys_clkdev( + const char *id, unsigned long rate) +{ + struct clk *clk; + int err; + + clk = clk_register_fixed_rate(NULL, id, NULL, 0, rate); + if (IS_ERR(clk)) + panic("failed to allocate %s clock structure", id); + + err = clk_register_clkdev(clk, id, NULL); + if (err) + panic("unable to register %s clock device", id); + + return clk; +} + void __init ralink_clk_init(void) { - int cpu_fdiv = 0; - int cpu_ffrac = 0; - int fbdiv = 0; - u32 clk_sts, syscfg; - u8 clk_sel = 0, xtal_mode; - u32 cpu_clk; + const static u32 prediv_tbl[] = {0, 1, 2, 2}; + u32 syscfg, xtal_sel, clkcfg, clk_sel, curclk, ffiv, ffrac; + u32 pll, prediv, fbdiv; + u32 xtal_clk, cpu_clk, bus_clk; + + syscfg = rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0); + xtal_sel = (syscfg >> XTAL_MODE_SEL_SHIFT) & XTAL_MODE_SEL_MASK; - if ((rt_sysc_r32(SYSC_REG_CPLL_CLKCFG0) & CPU_CLK_SEL) != 0) - clk_sel = 1; + clkcfg = rt_sysc_r32(SYSC_REG_CLKCFG0); + clk_sel = (clkcfg >> CPU_CLK_SEL_SHIFT) & CPU_CLK_SEL_MASK; + + curclk = rt_sysc_r32(SYSC_REG_CUR_CLK_STS); + ffiv = (curclk >> CUR_CPU_FDIV_SHIFT) & CUR_CPU_FDIV_MASK; + ffrac = (curclk >> CUR_CPU_FFRAC_SHIFT) & CUR_CPU_FFRAC_MASK; + + if (xtal_sel <= 2) + xtal_clk = 20 * 1000 * 1000; + else if (xtal_sel <= 5) + xtal_clk = 40 * 1000 * 1000; + else + xtal_clk = 25 * 1000 * 1000; switch (clk_sel) { case 0: - clk_sts = rt_sysc_r32(SYSC_REG_CUR_CLK_STS); - cpu_fdiv = ((clk_sts >> 8) & 0x1F); - cpu_ffrac = (clk_sts & 0x1F); - cpu_clk = (500 * cpu_ffrac / cpu_fdiv) * 1000 * 1000; + cpu_clk = 500 * 1000 * 1000; break; - case 1: - fbdiv = ((rt_sysc_r32(0x648) >> 4) & 0x7F) + 1; - syscfg = rt_sysc_r32(SYSC_REG_SYSCFG); - xtal_mode = (syscfg >> 6) & 0x7; - if (xtal_mode >= 6) { - /* 25Mhz Xtal */ - cpu_clk = 25 * fbdiv * 1000 * 1000; - } else if (xtal_mode >= 3) { - /* 40Mhz Xtal */ - cpu_clk = 40 * fbdiv * 1000 * 1000; - } else { - /* 20Mhz Xtal */ - cpu_clk = 20 * fbdiv * 1000 * 1000; - } + pll = rt_memc_r32(MEMC_REG_CPU_PLL); + fbdiv = (pll >> CPU_PLL_FBDIV_SHIFT) & CPU_PLL_FBDIV_MASK; + prediv = (pll >> CPU_PLL_PREDIV_SHIFT) & CPU_PLL_PREDIV_MASK; + cpu_clk = ((fbdiv + 1) * xtal_clk) >> prediv_tbl[prediv]; break; + default: + cpu_clk = xtal_clk; } + + cpu_clk = cpu_clk / ffiv * ffrac; + bus_clk = cpu_clk / 4; + + clks[MT7621_CLK_CPU] = mt7621_add_sys_clkdev("cpu", cpu_clk); + clks[MT7621_CLK_BUS] = mt7621_add_sys_clkdev("bus", bus_clk); + + pr_info("CPU Clock: %dMHz\n", cpu_clk / 1000000); + mips_hpt_frequency = cpu_clk / 2; } +static void __init mt7621_clocks_init_dt(struct device_node *np) +{ + of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data); +} + +CLK_OF_DECLARE(mt7621_clk, "mediatek,mt7621-pll", mt7621_clocks_init_dt); + void __init ralink_of_remap(void) { rt_sysc_membase = plat_of_remap_node("mtk,mt7621-sysc"); diff --git a/arch/mips/ralink/timer-gic.c b/arch/mips/ralink/timer-gic.c index b5f07d21fcf2..4fed842ae8eb 100644 --- a/arch/mips/ralink/timer-gic.c +++ b/arch/mips/ralink/timer-gic.c @@ -11,14 +11,14 @@ #include #include -#include +#include #include "common.h" void __init plat_time_init(void) { ralink_of_remap(); - + ralink_clk_init(); of_clk_init(NULL); timer_probe(); } -- 2.21.0