Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1953565ima; Thu, 25 Oct 2018 07:30:45 -0700 (PDT) X-Google-Smtp-Source: AJdET5drcwZl33rTSJkQi+T2T7SfwzQBX7bp+DhLIkSinhafXkyMnXUb2IgvKVIWX+oPxnmTnne0 X-Received: by 2002:a17:902:9b84:: with SMTP id y4-v6mr1771250plp.332.1540477845704; Thu, 25 Oct 2018 07:30:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540477845; cv=none; d=google.com; s=arc-20160816; b=aJl/4Ie7D7sb7pWy2b2YDP7GaukIW/73deNNVRKXJUIrIPexJfv0EAm4frZdGPeICg ZLQUpEz7nlcMa6Hg2gDGwCVDTVtmVdIuAICGgsa4TmVIghsOgovQO6dz8jMWCbQFZdpv 1z+cG5ZfGqUibBjQkGqb9D9noD8IfNe0m3pLKtwDjKbeWoX+qqf1wiLwGI/GMao4S8A7 S/QW2mJ9LbkkHRD7uC6E0ohImuZWDmQaVbJrFTuDmApXA0iYUWFa7lVHnEOIrCW2jY/m SZGvku2QuoxLMukn0EfUEFOCdyeByDP3c39GxXFEmE1nQr4lloVLjMnFY/FHX5VtJA0G +Jfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=W1qvQlk7lY8xYs2bkKa2N958khkrSYSnTMm0e8JPuJA=; b=0oG4UD7H+pqbtoa+0T4XmDclgBfOj6BFwbfR0k86mB8xWb5BWgWq+KnW1G5m6z5yC3 uc6NSbHzS/AUY2SQOaWbflgeOASkosbPOGWx2OrLonlZSloa+v8M8qzR/5TfgwHfN/Fb aOtvFFUaWAIGMwROZyJjgBdD3ke+eiKakWLDCMhDVQzx2MJn3Wh0C8yDpsFLGRphxw/u rSq6qhdMcSuDtMRDs3zK0PBGyu9T2cY1ejg9a7jlUx0UMSou9JN7Chx2qc4z3nXPiRMW bve3DHl0TJTFguJjK0pA+bJFliGVp0oV9yd3A9/zceIiYnLNkh0j3Qiokp0E8xQwRZAl 026g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HAkukYdA; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q1-v6si8401843pfb.258.2018.10.25.07.30.12; Thu, 25 Oct 2018 07:30:45 -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=@kernel.org header.s=default header.b=HAkukYdA; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730554AbeJYWw1 (ORCPT + 99 others); Thu, 25 Oct 2018 18:52:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:34486 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730541AbeJYWw1 (ORCPT ); Thu, 25 Oct 2018 18:52:27 -0400 Received: from sasha-vm.mshome.net (unknown [167.98.65.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8D602208A3; Thu, 25 Oct 2018 14:19:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1540477169; bh=drrP7nlw6KiJXkiZKNbj4TWRTeAwc+IgiRbWsKpnb9o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HAkukYdAdggoNKwdz5BIYJTrO9loaShZEo37/ZT5xiNgapeXvNdgl0n8owsF07Izn h+21dc2lPa66mvXUhGyyeMl5/VTIud3rZv7zm3uKSWkKjHsl+h8sUD9/Cet0m2/1UW w1TxXlijHQbez5xRA4POS0SMpnh5juagaX4Xmsog= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Lennart Sorensen , Tony Lindgren , Sasha Levin Subject: [PATCH AUTOSEL 3.18 22/98] ARM: dra7xx: Fix counter frequency drift for AM572x errata i856 Date: Thu, 25 Oct 2018 10:17:37 -0400 Message-Id: <20181025141853.214051-22-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025141853.214051-1-sashal@kernel.org> References: <20181025141853.214051-1-sashal@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lennart Sorensen [ Upstream commit afc9d590b8a150cfeaac0078ef5de6fb21a5ea6a ] Errata i856 for the AM572x (DRA7xx) points out that the 32.768KHz external crystal is not enabled at power up. Instead the CPU falls back to using an emulation for the 32KHz clock which is SYSCLK1/610. SYSCLK1 is usually 20MHz on boards so far (which gives an emulated frequency of 32.786KHz), but can also be 19.2 or 27MHz which result in much larger drift. Since this is used to drive the master counter at 32.768KHz * 375 / 2 = 6.144MHz, the emulated speed for 20MHz is of by 570ppm, or about 43 seconds per day, and more than the 500ppm NTP is able to tolerate. Checking the CTRL_CORE_BOOTSTRAP register can determine if the CPU is using the real 32.768KHz crystal or the emulated SYSCLK1/610, and by known that the real counter frequency can be determined and used. The real speed is then SYSCLK1 / 610 * 375 / 2 or SYSCLK1 * 75 / 244. Signed-off-by: Len Sorensen Tested-by: Lokesh Vutla Signed-off-by: Tony Lindgren Signed-off-by: Sasha Levin --- arch/arm/mach-omap2/control.h | 4 ++++ arch/arm/mach-omap2/timer.c | 36 +++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/arch/arm/mach-omap2/control.h b/arch/arm/mach-omap2/control.h index a3c013345c45..a80ac2d70bb1 100644 --- a/arch/arm/mach-omap2/control.h +++ b/arch/arm/mach-omap2/control.h @@ -286,6 +286,10 @@ #define OMAP5XXX_CONTROL_STATUS 0x134 #define OMAP5_DEVICETYPE_MASK (0x7 << 6) +/* DRA7XX CONTROL CORE BOOTSTRAP */ +#define DRA7_CTRL_CORE_BOOTSTRAP 0x6c4 +#define DRA7_SPEEDSELECT_MASK (0x3 << 8) + /* * REVISIT: This list of registers is not comprehensive - there are more * that should be added. diff --git a/arch/arm/mach-omap2/timer.c b/arch/arm/mach-omap2/timer.c index fb0cb2b817a9..7d45c84c69ba 100644 --- a/arch/arm/mach-omap2/timer.c +++ b/arch/arm/mach-omap2/timer.c @@ -54,6 +54,7 @@ #include "soc.h" #include "common.h" +#include "control.h" #include "powerdomain.h" #include "omap-secure.h" @@ -496,7 +497,8 @@ static void __init realtime_counter_init(void) void __iomem *base; static struct clk *sys_clk; unsigned long rate; - unsigned int reg, num, den; + unsigned int reg; + unsigned long long num, den; base = ioremap(REALTIME_COUNTER_BASE, SZ_32); if (!base) { @@ -511,6 +513,35 @@ static void __init realtime_counter_init(void) } rate = clk_get_rate(sys_clk); + + if (soc_is_dra7xx()) { + /* + * Errata i856 says the 32.768KHz crystal does not start at + * power on, so the CPU falls back to an emulated 32KHz clock + * based on sysclk / 610 instead. This causes the master counter + * frequency to not be 6.144MHz but at sysclk / 610 * 375 / 2 + * (OR sysclk * 75 / 244) + * + * This affects at least the DRA7/AM572x 1.0, 1.1 revisions. + * Of course any board built without a populated 32.768KHz + * crystal would also need this fix even if the CPU is fixed + * later. + * + * Either case can be detected by using the two speedselect bits + * If they are not 0, then the 32.768KHz clock driving the + * coarse counter that corrects the fine counter every time it + * ticks is actually rate/610 rather than 32.768KHz and we + * should compensate to avoid the 570ppm (at 20MHz, much worse + * at other rates) too fast system time. + */ + reg = omap_ctrl_readl(DRA7_CTRL_CORE_BOOTSTRAP); + if (reg & DRA7_SPEEDSELECT_MASK) { + num = 75; + den = 244; + goto sysclk1_based; + } + } + /* Numerator/denumerator values refer TRM Realtime Counter section */ switch (rate) { case 12000000: @@ -545,6 +576,7 @@ static void __init realtime_counter_init(void) break; } +sysclk1_based: /* Program numerator and denumerator registers */ reg = readl_relaxed(base + INCREMENTER_NUMERATOR_OFFSET) & NUMERATOR_DENUMERATOR_MASK; @@ -556,7 +588,7 @@ static void __init realtime_counter_init(void) reg |= den; writel_relaxed(reg, base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET); - arch_timer_freq = (rate / den) * num; + arch_timer_freq = DIV_ROUND_UP_ULL(rate * num, den); set_cntfreq(); iounmap(base); -- 2.17.1